1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 # include "sendmail.h"
10 
11 #ifndef lint
12 #ifdef SMTP
13 static char sccsid[] = "@(#)srvrsmtp.c	8.61 (Berkeley) 03/25/95 (with SMTP)";
14 #else
15 static char sccsid[] = "@(#)srvrsmtp.c	8.61 (Berkeley) 03/25/95 (without SMTP)";
16 #endif
17 #endif /* not lint */
18 
19 # include <errno.h>
20 
21 # ifdef SMTP
22 
23 /*
24 **  SMTP -- run the SMTP protocol.
25 **
26 **	Parameters:
27 **		none.
28 **
29 **	Returns:
30 **		never.
31 **
32 **	Side Effects:
33 **		Reads commands from the input channel and processes
34 **			them.
35 */
36 
37 struct cmd
38 {
39 	char	*cmdname;	/* command name */
40 	int	cmdcode;	/* internal code, see below */
41 };
42 
43 /* values for cmdcode */
44 # define CMDERROR	0	/* bad command */
45 # define CMDMAIL	1	/* mail -- designate sender */
46 # define CMDRCPT	2	/* rcpt -- designate recipient */
47 # define CMDDATA	3	/* data -- send message text */
48 # define CMDRSET	4	/* rset -- reset state */
49 # define CMDVRFY	5	/* vrfy -- verify address */
50 # define CMDEXPN	6	/* expn -- expand address */
51 # define CMDNOOP	7	/* noop -- do nothing */
52 # define CMDQUIT	8	/* quit -- close connection and die */
53 # define CMDHELO	9	/* helo -- be polite */
54 # define CMDHELP	10	/* help -- give usage info */
55 # define CMDEHLO	11	/* ehlo -- extended helo (RFC 1425) */
56 /* non-standard commands */
57 # define CMDONEX	16	/* onex -- sending one transaction only */
58 # define CMDVERB	17	/* verb -- go into verbose mode */
59 /* use this to catch and log "door handle" attempts on your system */
60 # define CMDLOGBOGUS	23	/* bogus command that should be logged */
61 /* debugging-only commands, only enabled if SMTPDEBUG is defined */
62 # define CMDDBGQSHOW	24	/* showq -- show send queue */
63 # define CMDDBGDEBUG	25	/* debug -- set debug mode */
64 
65 static struct cmd	CmdTab[] =
66 {
67 	"mail",		CMDMAIL,
68 	"rcpt",		CMDRCPT,
69 	"data",		CMDDATA,
70 	"rset",		CMDRSET,
71 	"vrfy",		CMDVRFY,
72 	"expn",		CMDEXPN,
73 	"help",		CMDHELP,
74 	"noop",		CMDNOOP,
75 	"quit",		CMDQUIT,
76 	"helo",		CMDHELO,
77 	"ehlo",		CMDEHLO,
78 	"verb",		CMDVERB,
79 	"onex",		CMDONEX,
80 	/*
81 	 * remaining commands are here only
82 	 * to trap and log attempts to use them
83 	 */
84 	"showq",	CMDDBGQSHOW,
85 	"debug",	CMDDBGDEBUG,
86 	"wiz",		CMDLOGBOGUS,
87 	NULL,		CMDERROR,
88 };
89 
90 bool	OneXact = FALSE;		/* one xaction only this run */
91 char	*CurSmtpClient;			/* who's at the other end of channel */
92 
93 static char	*skipword();
94 extern char	RealUserName[];
95 
96 
97 #define MAXBADCOMMANDS	25		/* maximum number of bad commands */
98 
99 smtp(e)
100 	register ENVELOPE *e;
101 {
102 	register char *p;
103 	register struct cmd *c;
104 	char *cmd;
105 	auto ADDRESS *vrfyqueue;
106 	ADDRESS *a;
107 	bool gotmail;			/* mail command received */
108 	bool gothello;			/* helo command received */
109 	bool vrfy;			/* set if this is a vrfy command */
110 	char *protocol;			/* sending protocol */
111 	char *sendinghost;		/* sending hostname */
112 	char *peerhostname;		/* name of SMTP peer or "localhost" */
113 	auto char *delimptr;
114 	char *id;
115 	int nrcpts = 0;			/* number of RCPT commands */
116 	bool doublequeue;
117 	int badcommands = 0;		/* count of bad commands */
118 	char inp[MAXLINE];
119 	char cmdbuf[MAXLINE];
120 	extern char Version[];
121 	extern ENVELOPE BlankEnvelope;
122 
123 	if (fileno(OutChannel) != fileno(stdout))
124 	{
125 		/* arrange for debugging output to go to remote host */
126 		(void) dup2(fileno(OutChannel), fileno(stdout));
127 	}
128 	settime(e);
129 	peerhostname = RealHostName;
130 	if (peerhostname == NULL)
131 		peerhostname = "localhost";
132 	CurHostName = peerhostname;
133 	CurSmtpClient = macvalue('_', e);
134 	if (CurSmtpClient == NULL)
135 		CurSmtpClient = CurHostName;
136 
137 	setproctitle("server %s startup", CurSmtpClient);
138 	expand("\201e", inp, sizeof inp, e);
139 	if (BrokenSmtpPeers)
140 	{
141 		p = strchr(inp, '\n');
142 		if (p != NULL)
143 			*p = '\0';
144 		message("220 %s", inp);
145 	}
146 	else
147 	{
148 		char *q = inp;
149 
150 		while (q != NULL)
151 		{
152 			p = strchr(q, '\n');
153 			if (p != NULL)
154 				*p++ = '\0';
155 			message("220-%s", q);
156 			q = p;
157 		}
158 		message("220 ESMTP spoken here");
159 	}
160 	protocol = NULL;
161 	sendinghost = macvalue('s', e);
162 	gothello = FALSE;
163 	gotmail = FALSE;
164 	for (;;)
165 	{
166 		/* arrange for backout */
167 		if (setjmp(TopFrame) > 0)
168 		{
169 			/* if() nesting is necessary for Cray UNICOS */
170 			if (InChild)
171 			{
172 				QuickAbort = FALSE;
173 				SuprErrs = TRUE;
174 				finis();
175 			}
176 		}
177 		QuickAbort = FALSE;
178 		HoldErrs = FALSE;
179 		LogUsrErrs = FALSE;
180 		e->e_flags &= ~(EF_VRFYONLY|EF_GLOBALERRS);
181 
182 		/* setup for the read */
183 		e->e_to = NULL;
184 		Errors = 0;
185 		(void) fflush(stdout);
186 
187 		/* read the input line */
188 		SmtpPhase = "server cmd read";
189 		setproctitle("server %s cmd read", CurHostName);
190 		p = sfgets(inp, sizeof inp, InChannel, TimeOuts.to_nextcommand,
191 				SmtpPhase);
192 
193 		/* handle errors */
194 		if (p == NULL)
195 		{
196 			/* end of file, just die */
197 			disconnect(1, e);
198 			message("421 %s Lost input channel from %s",
199 				MyHostName, CurSmtpClient);
200 #ifdef LOG
201 			if (LogLevel > (gotmail ? 1 : 19))
202 				syslog(LOG_NOTICE, "lost input channel from %s",
203 					CurSmtpClient);
204 #endif
205 			if (InChild)
206 				ExitStat = EX_QUIT;
207 			finis();
208 		}
209 
210 		/* clean up end of line */
211 		fixcrlf(inp, TRUE);
212 
213 		/* echo command to transcript */
214 		if (e->e_xfp != NULL)
215 			fprintf(e->e_xfp, "<<< %s\n", inp);
216 
217 		if (e->e_id == NULL)
218 			setproctitle("%s: %.80s", CurSmtpClient, inp);
219 		else
220 			setproctitle("%s %s: %.80s", e->e_id, CurSmtpClient, inp);
221 
222 		/* break off command */
223 		for (p = inp; isascii(*p) && isspace(*p); p++)
224 			continue;
225 		cmd = cmdbuf;
226 		while (*p != '\0' &&
227 		       !(isascii(*p) && isspace(*p)) &&
228 		       cmd < &cmdbuf[sizeof cmdbuf - 2])
229 			*cmd++ = *p++;
230 		*cmd = '\0';
231 
232 		/* throw away leading whitespace */
233 		while (isascii(*p) && isspace(*p))
234 			p++;
235 
236 		/* decode command */
237 		for (c = CmdTab; c->cmdname != NULL; c++)
238 		{
239 			if (!strcasecmp(c->cmdname, cmdbuf))
240 				break;
241 		}
242 
243 		/* reset errors */
244 		errno = 0;
245 
246 		/* process command */
247 		switch (c->cmdcode)
248 		{
249 		  case CMDHELO:		/* hello -- introduce yourself */
250 		  case CMDEHLO:		/* extended hello */
251 			if (c->cmdcode == CMDEHLO)
252 			{
253 				protocol = "ESMTP";
254 				SmtpPhase = "server EHLO";
255 			}
256 			else
257 			{
258 				protocol = "SMTP";
259 				SmtpPhase = "server HELO";
260 			}
261 
262 			/* check for valid domain name (re 1123 5.2.5) */
263 			if (*p == '\0')
264 			{
265 				message("501 %s requires domain address",
266 					cmdbuf);
267 				break;
268 			}
269 			else
270 			{
271 				register char *q;
272 
273 				for (q = p; *q != '\0'; q++)
274 				{
275 					if (!isascii(*q))
276 						break;
277 					if (isalnum(*q))
278 						continue;
279 					if (strchr("[].-_#", *q) == NULL)
280 						break;
281 				}
282 				if (*q != '\0')
283 				{
284 					message("501 Invalid domain name");
285 					break;
286 				}
287 			}
288 
289 			sendinghost = newstr(p);
290 			gothello = TRUE;
291 			if (c->cmdcode != CMDEHLO)
292 			{
293 				/* print old message and be done with it */
294 				message("250 %s Hello %s, pleased to meet you",
295 					MyHostName, CurSmtpClient);
296 				break;
297 			}
298 
299 			/* print extended message and brag */
300 			message("250-%s Hello %s, pleased to meet you",
301 				MyHostName, CurSmtpClient);
302 			if (!bitset(PRIV_NOEXPN, PrivacyFlags))
303 				message("250-EXPN");
304 			message("250-8BITMIME");
305 			if (MaxMessageSize > 0)
306 				message("250-SIZE %ld", MaxMessageSize);
307 			else
308 				message("250-SIZE");
309 #ifdef DSN
310 			message("250-X-DSN-3 (Unpublished draft of 12 Mar 1995)");
311 #endif
312 			message("250 HELP");
313 			break;
314 
315 		  case CMDMAIL:		/* mail -- designate sender */
316 			SmtpPhase = "server MAIL";
317 
318 			/* check for validity of this command */
319 			if (!gothello)
320 			{
321 				/* set sending host to our known value */
322 				if (sendinghost == NULL)
323 					sendinghost = peerhostname;
324 
325 				if (bitset(PRIV_NEEDMAILHELO, PrivacyFlags))
326 				{
327 					message("503 Polite people say HELO first");
328 					break;
329 				}
330 			}
331 			if (gotmail)
332 			{
333 				message("503 Sender already specified");
334 				if (InChild)
335 					finis();
336 				break;
337 			}
338 			if (InChild)
339 			{
340 				errno = 0;
341 				syserr("503 Nested MAIL command: MAIL %s", p);
342 				finis();
343 			}
344 
345 			/* fork a subprocess to process this command */
346 			if (runinchild("SMTP-MAIL", e) > 0)
347 				break;
348 			if (!gothello)
349 			{
350 				auth_warning(e,
351 					"Host %s didn't use HELO protocol",
352 					peerhostname);
353 			}
354 #ifdef PICKY_HELO_CHECK
355 			if (strcasecmp(sendinghost, peerhostname) != 0 &&
356 			    (strcasecmp(peerhostname, "localhost") != 0 ||
357 			     strcasecmp(sendinghost, MyHostName) != 0))
358 			{
359 				auth_warning(e, "Host %s claimed to be %s",
360 					peerhostname, sendinghost);
361 			}
362 #endif
363 
364 			if (protocol == NULL)
365 				protocol = "SMTP";
366 			define('r', protocol, e);
367 			define('s', sendinghost, e);
368 			initsys(e);
369 			nrcpts = 0;
370 			e->e_flags |= EF_LOGSENDER|EF_CLRQUEUE;
371 			setproctitle("%s %s: %.80s", e->e_id, CurSmtpClient, inp);
372 
373 			/* child -- go do the processing */
374 			p = skipword(p, "from");
375 			if (p == NULL)
376 				break;
377 			if (setjmp(TopFrame) > 0)
378 			{
379 				/* this failed -- undo work */
380 				if (InChild)
381 				{
382 					QuickAbort = FALSE;
383 					SuprErrs = TRUE;
384 					e->e_flags &= ~EF_FATALERRS;
385 					finis();
386 				}
387 				break;
388 			}
389 			QuickAbort = TRUE;
390 
391 			/* must parse sender first */
392 			delimptr = NULL;
393 			setsender(p, e, &delimptr, FALSE);
394 			p = delimptr;
395 			if (p != NULL && *p != '\0')
396 				*p++ = '\0';
397 
398 			/* check for possible spoofing */
399 			if (RealUid != 0 && OpMode == MD_SMTP &&
400 			    !bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags) &&
401 			    strcmp(e->e_from.q_user, RealUserName) != 0)
402 			{
403 				auth_warning(e, "%s owned process doing -bs",
404 					RealUserName);
405 			}
406 
407 			/* now parse ESMTP arguments */
408 			e->e_msgsize = 0;
409 			while (p != NULL && *p != '\0')
410 			{
411 				char *kp;
412 				char *vp = NULL;
413 
414 				/* locate the beginning of the keyword */
415 				while (isascii(*p) && isspace(*p))
416 					p++;
417 				if (*p == '\0')
418 					break;
419 				kp = p;
420 
421 				/* skip to the value portion */
422 				while (isascii(*p) && isalnum(*p) || *p == '-')
423 					p++;
424 				if (*p == '=')
425 				{
426 					*p++ = '\0';
427 					vp = p;
428 
429 					/* skip to the end of the value */
430 					while (*p != '\0' && *p != ' ' &&
431 					       !(isascii(*p) && iscntrl(*p)) &&
432 					       *p != '=')
433 						p++;
434 				}
435 
436 				if (*p != '\0')
437 					*p++ = '\0';
438 
439 				if (tTd(19, 1))
440 					printf("MAIL: got arg %s=\"%s\"\n", kp,
441 						vp == NULL ? "<null>" : vp);
442 
443 				mail_esmtp_args(kp, vp, e);
444 			}
445 
446 			if (MaxMessageSize > 0 && e->e_msgsize > MaxMessageSize)
447 			{
448 				usrerr("552 Message size exceeds fixed maximum message size (%ld)",
449 					MaxMessageSize);
450 				/* NOTREACHED */
451 			}
452 
453 			if (!enoughspace(e->e_msgsize))
454 			{
455 				message("452 Insufficient disk space; try again later");
456 				break;
457 			}
458 			message("250 Sender ok");
459 			gotmail = TRUE;
460 			break;
461 
462 		  case CMDRCPT:		/* rcpt -- designate recipient */
463 			if (!gotmail)
464 			{
465 				usrerr("503 Need MAIL before RCPT");
466 				break;
467 			}
468 			SmtpPhase = "server RCPT";
469 			if (setjmp(TopFrame) > 0)
470 			{
471 				e->e_flags &= ~EF_FATALERRS;
472 				break;
473 			}
474 			QuickAbort = TRUE;
475 			LogUsrErrs = TRUE;
476 
477 			if (e->e_sendmode != SM_DELIVER)
478 				e->e_flags |= EF_VRFYONLY;
479 
480 			p = skipword(p, "to");
481 			if (p == NULL)
482 				break;
483 			a = parseaddr(p, NULLADDR, RF_COPYALL, ' ', &delimptr, e);
484 			if (a == NULL)
485 				break;
486 			p = delimptr;
487 
488 			/* now parse ESMTP arguments */
489 			while (p != NULL && *p != '\0')
490 			{
491 				char *kp;
492 				char *vp = NULL;
493 
494 				/* locate the beginning of the keyword */
495 				while (isascii(*p) && isspace(*p))
496 					p++;
497 				if (*p == '\0')
498 					break;
499 				kp = p;
500 
501 				/* skip to the value portion */
502 				while (isascii(*p) && isalnum(*p) || *p == '-')
503 					p++;
504 				if (*p == '=')
505 				{
506 					*p++ = '\0';
507 					vp = p;
508 
509 					/* skip to the end of the value */
510 					while (*p != '\0' && *p != ' ' &&
511 					       !(isascii(*p) && iscntrl(*p)) &&
512 					       *p != '=')
513 						p++;
514 				}
515 
516 				if (*p != '\0')
517 					*p++ = '\0';
518 
519 				if (tTd(19, 1))
520 					printf("RCPT: got arg %s=\"%s\"\n", kp,
521 						vp == NULL ? "<null>" : vp);
522 
523 				rcpt_esmtp_args(a, kp, vp, e);
524 
525 			}
526 
527 			/* save in recipient list after ESMTP mods */
528 			a->q_flags |= QPRIMARY;
529 			a = recipient(a, &e->e_sendqueue, 0, e);
530 
531 			if (Errors != 0)
532 				break;
533 
534 			/* no errors during parsing, but might be a duplicate */
535 			e->e_to = p;
536 			if (!bitset(QBADADDR, a->q_flags))
537 			{
538 				message("250 Recipient ok%s",
539 					bitset(QQUEUEUP, a->q_flags) ?
540 						" (will queue)" : "");
541 				nrcpts++;
542 			}
543 			else
544 			{
545 				/* punt -- should keep message in ADDRESS.... */
546 				message("550 Addressee unknown");
547 			}
548 			e->e_to = NULL;
549 			break;
550 
551 		  case CMDDATA:		/* data -- text of mail */
552 			SmtpPhase = "server DATA";
553 			if (!gotmail)
554 			{
555 				message("503 Need MAIL command");
556 				break;
557 			}
558 			else if (nrcpts <= 0)
559 			{
560 				message("503 Need RCPT (recipient)");
561 				break;
562 			}
563 
564 			/* check to see if we need to re-expand aliases */
565 			/* also reset QBADADDR on already-diagnosted addrs */
566 			doublequeue = FALSE;
567 			for (a = e->e_sendqueue; a != NULL; a = a->q_next)
568 			{
569 				if (bitset(QVERIFIED, a->q_flags))
570 				{
571 					/* need to re-expand aliases */
572 					doublequeue = TRUE;
573 				}
574 				if (bitset(QBADADDR, a->q_flags))
575 				{
576 					/* make this "go away" */
577 					a->q_flags |= QDONTSEND;
578 					a->q_flags &= ~QBADADDR;
579 				}
580 			}
581 
582 			/* collect the text of the message */
583 			SmtpPhase = "collect";
584 			collect(InChannel, TRUE, doublequeue, NULL, e);
585 			if (Errors != 0)
586 				goto abortmessage;
587 
588 			/* make sure we actually do delivery */
589 			e->e_flags &= ~EF_CLRQUEUE;
590 
591 			/* from now on, we have to operate silently */
592 			HoldErrs = TRUE;
593 			e->e_errormode = EM_MAIL;
594 
595 			/*
596 			**  Arrange to send to everyone.
597 			**	If sending to multiple people, mail back
598 			**		errors rather than reporting directly.
599 			**	In any case, don't mail back errors for
600 			**		anything that has happened up to
601 			**		now (the other end will do this).
602 			**	Truncate our transcript -- the mail has gotten
603 			**		to us successfully, and if we have
604 			**		to mail this back, it will be easier
605 			**		on the reader.
606 			**	Then send to everyone.
607 			**	Finally give a reply code.  If an error has
608 			**		already been given, don't mail a
609 			**		message back.
610 			**	We goose error returns by clearing error bit.
611 			*/
612 
613 			SmtpPhase = "delivery";
614 			e->e_xfp = freopen(queuename(e, 'x'), "w", e->e_xfp);
615 			id = e->e_id;
616 
617 			if (doublequeue)
618 			{
619 				/* make sure it is in the queue */
620 				queueup(e, TRUE, FALSE);
621 				if (e->e_sendmode == SM_QUEUE)
622 					e->e_flags |= EF_KEEPQUEUE;
623 			}
624 			else
625 			{
626 				/* send to all recipients */
627 				sendall(e, SM_DEFAULT);
628 			}
629 			e->e_to = NULL;
630 
631 			/* issue success message */
632 			message("250 %s Message accepted for delivery", id);
633 
634 			/* if we just queued, poke it */
635 			if (doublequeue && e->e_sendmode != SM_QUEUE)
636 			{
637 				extern pid_t dowork();
638 
639 				unlockqueue(e);
640 				(void) dowork(id, TRUE, TRUE, e);
641 			}
642 
643   abortmessage:
644 			/* if in a child, pop back to our parent */
645 			if (InChild)
646 				finis();
647 
648 			/* clean up a bit */
649 			gotmail = FALSE;
650 			dropenvelope(e);
651 			CurEnv = e = newenvelope(e, CurEnv);
652 			e->e_flags = BlankEnvelope.e_flags;
653 			break;
654 
655 		  case CMDRSET:		/* rset -- reset state */
656 			message("250 Reset state");
657 			e->e_flags |= EF_CLRQUEUE;
658 			if (InChild)
659 				finis();
660 
661 			/* clean up a bit */
662 			gotmail = FALSE;
663 			dropenvelope(e);
664 			CurEnv = e = newenvelope(e, CurEnv);
665 			break;
666 
667 		  case CMDVRFY:		/* vrfy -- verify address */
668 		  case CMDEXPN:		/* expn -- expand address */
669 			vrfy = c->cmdcode == CMDVRFY;
670 			if (bitset(vrfy ? PRIV_NOVRFY : PRIV_NOEXPN,
671 						PrivacyFlags))
672 			{
673 				if (vrfy)
674 					message("252 Cannot VRFY user; try RCPT to attempt delivery (or try finger)");
675 				else
676 					message("502 Sorry, we do not allow this operation");
677 #ifdef LOG
678 				if (LogLevel > 5)
679 					syslog(LOG_INFO, "%s: %s [rejected]",
680 						CurSmtpClient, inp);
681 #endif
682 				break;
683 			}
684 			else if (!gothello &&
685 				 bitset(vrfy ? PRIV_NEEDVRFYHELO : PRIV_NEEDEXPNHELO,
686 						PrivacyFlags))
687 			{
688 				message("503 I demand that you introduce yourself first");
689 				break;
690 			}
691 			if (runinchild(vrfy ? "SMTP-VRFY" : "SMTP-EXPN", e) > 0)
692 				break;
693 #ifdef LOG
694 			if (LogLevel > 5)
695 				syslog(LOG_INFO, "%s: %s", CurSmtpClient, inp);
696 #endif
697 			vrfyqueue = NULL;
698 			QuickAbort = TRUE;
699 			if (vrfy)
700 				e->e_flags |= EF_VRFYONLY;
701 			while (*p != '\0' && isascii(*p) && isspace(*p))
702 				p++;
703 			if (*p == '\0')
704 			{
705 				message("501 Argument required");
706 				Errors++;
707 			}
708 			else
709 			{
710 				(void) sendtolist(p, NULLADDR, &vrfyqueue, 0, e);
711 			}
712 			if (Errors != 0)
713 			{
714 				if (InChild)
715 					finis();
716 				break;
717 			}
718 			if (vrfyqueue == NULL)
719 			{
720 				message("554 Nothing to %s", vrfy ? "VRFY" : "EXPN");
721 			}
722 			while (vrfyqueue != NULL)
723 			{
724 				a = vrfyqueue;
725 				while ((a = a->q_next) != NULL &&
726 				       bitset(QDONTSEND|QBADADDR, a->q_flags))
727 					continue;
728 				if (!bitset(QDONTSEND|QBADADDR, vrfyqueue->q_flags))
729 					printvrfyaddr(vrfyqueue, a == NULL);
730 				vrfyqueue = vrfyqueue->q_next;
731 			}
732 			if (InChild)
733 				finis();
734 			break;
735 
736 		  case CMDHELP:		/* help -- give user info */
737 			help(p);
738 			break;
739 
740 		  case CMDNOOP:		/* noop -- do nothing */
741 			message("250 OK");
742 			break;
743 
744 		  case CMDQUIT:		/* quit -- leave mail */
745 			message("221 %s closing connection", MyHostName);
746 
747 doquit:
748 			/* avoid future 050 messages */
749 			disconnect(1, e);
750 
751 			if (InChild)
752 				ExitStat = EX_QUIT;
753 			finis();
754 
755 		  case CMDVERB:		/* set verbose mode */
756 			if (bitset(PRIV_NOEXPN, PrivacyFlags))
757 			{
758 				/* this would give out the same info */
759 				message("502 Verbose unavailable");
760 				break;
761 			}
762 			Verbose = TRUE;
763 			e->e_sendmode = SM_DELIVER;
764 			message("250 Verbose mode");
765 			break;
766 
767 		  case CMDONEX:		/* doing one transaction only */
768 			OneXact = TRUE;
769 			message("250 Only one transaction");
770 			break;
771 
772 # ifdef SMTPDEBUG
773 		  case CMDDBGQSHOW:	/* show queues */
774 			printf("Send Queue=");
775 			printaddr(e->e_sendqueue, TRUE);
776 			break;
777 
778 		  case CMDDBGDEBUG:	/* set debug mode */
779 			tTsetup(tTdvect, sizeof tTdvect, "0-99.1");
780 			tTflag(p);
781 			message("200 Debug set");
782 			break;
783 
784 # else /* not SMTPDEBUG */
785 		  case CMDDBGQSHOW:	/* show queues */
786 		  case CMDDBGDEBUG:	/* set debug mode */
787 # endif /* SMTPDEBUG */
788 		  case CMDLOGBOGUS:	/* bogus command */
789 # ifdef LOG
790 			if (LogLevel > 0)
791 				syslog(LOG_CRIT,
792 				    "\"%s\" command from %s (%s)",
793 				    c->cmdname, peerhostname,
794 				    anynet_ntoa(&RealHostAddr));
795 # endif
796 			/* FALL THROUGH */
797 
798 		  case CMDERROR:	/* unknown command */
799 			if (++badcommands > MAXBADCOMMANDS)
800 			{
801 				message("421 %s Too many bad commands; closing connection",
802 					MyHostName);
803 				goto doquit;
804 			}
805 
806 			message("500 Command unrecognized");
807 			break;
808 
809 		  default:
810 			errno = 0;
811 			syserr("500 smtp: unknown code %d", c->cmdcode);
812 			break;
813 		}
814 	}
815 }
816 /*
817 **  SKIPWORD -- skip a fixed word.
818 **
819 **	Parameters:
820 **		p -- place to start looking.
821 **		w -- word to skip.
822 **
823 **	Returns:
824 **		p following w.
825 **		NULL on error.
826 **
827 **	Side Effects:
828 **		clobbers the p data area.
829 */
830 
831 static char *
832 skipword(p, w)
833 	register char *p;
834 	char *w;
835 {
836 	register char *q;
837 	char *firstp = p;
838 
839 	/* find beginning of word */
840 	while (isascii(*p) && isspace(*p))
841 		p++;
842 	q = p;
843 
844 	/* find end of word */
845 	while (*p != '\0' && *p != ':' && !(isascii(*p) && isspace(*p)))
846 		p++;
847 	while (isascii(*p) && isspace(*p))
848 		*p++ = '\0';
849 	if (*p != ':')
850 	{
851 	  syntax:
852 		message("501 Syntax error in parameters scanning \"%s\"",
853 			firstp);
854 		Errors++;
855 		return (NULL);
856 	}
857 	*p++ = '\0';
858 	while (isascii(*p) && isspace(*p))
859 		p++;
860 
861 	if (*p == '\0')
862 		goto syntax;
863 
864 	/* see if the input word matches desired word */
865 	if (strcasecmp(q, w))
866 		goto syntax;
867 
868 	return (p);
869 }
870 /*
871 **  MAIL_ESMTP_ARGS -- process ESMTP arguments from MAIL line
872 **
873 **	Parameters:
874 **		kp -- the parameter key.
875 **		vp -- the value of that parameter.
876 **		e -- the envelope.
877 **
878 **	Returns:
879 **		none.
880 */
881 
882 mail_esmtp_args(kp, vp, e)
883 	char *kp;
884 	char *vp;
885 	ENVELOPE *e;
886 {
887 	if (strcasecmp(kp, "size") == 0)
888 	{
889 		if (vp == NULL)
890 		{
891 			usrerr("501 SIZE requires a value");
892 			/* NOTREACHED */
893 		}
894 # ifdef __STDC__
895 		e->e_msgsize = strtoul(vp, (char **) NULL, 10);
896 # else
897 		e->e_msgsize = strtol(vp, (char **) NULL, 10);
898 # endif
899 	}
900 	else if (strcasecmp(kp, "body") == 0)
901 	{
902 		if (vp == NULL)
903 		{
904 			usrerr("501 BODY requires a value");
905 			/* NOTREACHED */
906 		}
907 		if (strcasecmp(vp, "8bitmime") == 0)
908 		{
909 			SevenBitInput = FALSE;
910 		}
911 		else if (strcasecmp(vp, "7bit") == 0)
912 		{
913 			SevenBitInput = TRUE;
914 		}
915 		else
916 		{
917 			usrerr("501 Unknown BODY type %s",
918 				vp);
919 			/* NOTREACHED */
920 		}
921 		e->e_bodytype = newstr(vp);
922 	}
923 	else if (strcasecmp(kp, "envid") == 0)
924 	{
925 		if (vp == NULL)
926 		{
927 			usrerr("501 ENVID requires a value");
928 			/* NOTREACHED */
929 		}
930 		if (!xtextok(vp))
931 		{
932 			usrerr("501 Syntax error in ENVID parameter value");
933 			/* NOTREACHED */
934 		}
935 		if (e->e_envid != NULL)
936 		{
937 			usrerr("501 Duplicate ENVID parameter");
938 			/* NOTREACHED */
939 		}
940 		e->e_envid = newstr(vp);
941 	}
942 	else if (strcasecmp(kp, "ret") == 0)
943 	{
944 		if (vp == NULL)
945 		{
946 			usrerr("501 RET requires a value");
947 			/* NOTREACHED */
948 		}
949 		if (bitset(EF_RET_PARAM, e->e_flags))
950 		{
951 			usrerr("501 Duplicate RET parameter");
952 			/* NOTREACHED */
953 		}
954 		e->e_flags |= EF_RET_PARAM;
955 		if (strcasecmp(vp, "hdrs") == 0)
956 			e->e_flags |= EF_NO_BODY_RETN;
957 		else if (strcasecmp(vp, "full") != 0)
958 		{
959 			usrerr("501 Bad argument \"%s\" to RET", vp);
960 			/* NOTREACHED */
961 		}
962 	}
963 	else
964 	{
965 		usrerr("501 %s parameter unrecognized", kp);
966 		/* NOTREACHED */
967 	}
968 }
969 /*
970 **  RCPT_ESMTP_ARGS -- process ESMTP arguments from RCPT line
971 **
972 **	Parameters:
973 **		a -- the address corresponding to the To: parameter.
974 **		kp -- the parameter key.
975 **		vp -- the value of that parameter.
976 **		e -- the envelope.
977 **
978 **	Returns:
979 **		none.
980 */
981 
982 rcpt_esmtp_args(a, kp, vp, e)
983 	ADDRESS *a;
984 	char *kp;
985 	char *vp;
986 	ENVELOPE *e;
987 {
988 	if (strcasecmp(kp, "notify") == 0)
989 	{
990 		char *p;
991 
992 		if (vp == NULL)
993 		{
994 			usrerr("501 NOTIFY requires a value");
995 			/* NOTREACHED */
996 		}
997 		a->q_flags &= ~(QPINGONSUCCESS|QPINGONFAILURE|QPINGONDELAY);
998 		a->q_flags |= QHASNOTIFY;
999 		if (strcasecmp(vp, "never") == 0)
1000 			return;
1001 		for (p = vp; p != NULL; vp = p)
1002 		{
1003 			p = strchr(p, ',');
1004 			if (p != NULL)
1005 				*p++ = '\0';
1006 			if (strcasecmp(vp, "success") == 0)
1007 				a->q_flags |= QPINGONSUCCESS;
1008 			else if (strcasecmp(vp, "failure") == 0)
1009 				a->q_flags |= QPINGONFAILURE;
1010 			else if (strcasecmp(vp, "delay") == 0)
1011 				a->q_flags |= QPINGONDELAY;
1012 			else
1013 			{
1014 				usrerr("501 Bad argument \"%s\"  to NOTIFY",
1015 					vp);
1016 				/* NOTREACHED */
1017 			}
1018 		}
1019 	}
1020 	else if (strcasecmp(kp, "orcpt") == 0)
1021 	{
1022 		if (vp == NULL)
1023 		{
1024 			usrerr("501 ORCPT requires a value");
1025 			/* NOTREACHED */
1026 		}
1027 		if (!xtextok(vp))
1028 		{
1029 			usrerr("501 Syntax error in ORCPT parameter value");
1030 			/* NOTREACHED */
1031 		}
1032 		if (a->q_orcpt != NULL)
1033 		{
1034 			usrerr("501 Duplicate ORCPT parameter");
1035 			/* NOTREACHED */
1036 		}
1037 		a->q_orcpt = newstr(vp);
1038 	}
1039 	else
1040 	{
1041 		usrerr("501 %s parameter unrecognized", kp);
1042 		/* NOTREACHED */
1043 	}
1044 }
1045 /*
1046 **  PRINTVRFYADDR -- print an entry in the verify queue
1047 **
1048 **	Parameters:
1049 **		a -- the address to print
1050 **		last -- set if this is the last one.
1051 **
1052 **	Returns:
1053 **		none.
1054 **
1055 **	Side Effects:
1056 **		Prints the appropriate 250 codes.
1057 */
1058 
1059 printvrfyaddr(a, last)
1060 	register ADDRESS *a;
1061 	bool last;
1062 {
1063 	char fmtbuf[20];
1064 
1065 	strcpy(fmtbuf, "250");
1066 	fmtbuf[3] = last ? ' ' : '-';
1067 
1068 	if (a->q_fullname == NULL)
1069 	{
1070 		if (strchr(a->q_user, '@') == NULL)
1071 			strcpy(&fmtbuf[4], "<%s@%s>");
1072 		else
1073 			strcpy(&fmtbuf[4], "<%s>");
1074 		message(fmtbuf, a->q_user, MyHostName);
1075 	}
1076 	else
1077 	{
1078 		if (strchr(a->q_user, '@') == NULL)
1079 			strcpy(&fmtbuf[4], "%s <%s@%s>");
1080 		else
1081 			strcpy(&fmtbuf[4], "%s <%s>");
1082 		message(fmtbuf, a->q_fullname, a->q_user, MyHostName);
1083 	}
1084 }
1085 /*
1086 **  HELP -- implement the HELP command.
1087 **
1088 **	Parameters:
1089 **		topic -- the topic we want help for.
1090 **
1091 **	Returns:
1092 **		none.
1093 **
1094 **	Side Effects:
1095 **		outputs the help file to message output.
1096 */
1097 
1098 help(topic)
1099 	char *topic;
1100 {
1101 	register FILE *hf;
1102 	int len;
1103 	char buf[MAXLINE];
1104 	bool noinfo;
1105 
1106 	if (HelpFile == NULL || (hf = fopen(HelpFile, "r")) == NULL)
1107 	{
1108 		/* no help */
1109 		errno = 0;
1110 		message("502 HELP not implemented");
1111 		return;
1112 	}
1113 
1114 	if (topic == NULL || *topic == '\0')
1115 		topic = "smtp";
1116 	else
1117 		makelower(topic);
1118 
1119 	len = strlen(topic);
1120 	noinfo = TRUE;
1121 
1122 	while (fgets(buf, sizeof buf, hf) != NULL)
1123 	{
1124 		if (strncmp(buf, topic, len) == 0)
1125 		{
1126 			register char *p;
1127 
1128 			p = strchr(buf, '\t');
1129 			if (p == NULL)
1130 				p = buf;
1131 			else
1132 				p++;
1133 			fixcrlf(p, TRUE);
1134 			message("214-%s", p);
1135 			noinfo = FALSE;
1136 		}
1137 	}
1138 
1139 	if (noinfo)
1140 		message("504 HELP topic unknown");
1141 	else
1142 		message("214 End of HELP info");
1143 	(void) fclose(hf);
1144 }
1145 /*
1146 **  RUNINCHILD -- return twice -- once in the child, then in the parent again
1147 **
1148 **	Parameters:
1149 **		label -- a string used in error messages
1150 **
1151 **	Returns:
1152 **		zero in the child
1153 **		one in the parent
1154 **
1155 **	Side Effects:
1156 **		none.
1157 */
1158 
1159 runinchild(label, e)
1160 	char *label;
1161 	register ENVELOPE *e;
1162 {
1163 	int childpid;
1164 
1165 	if (!OneXact)
1166 	{
1167 		childpid = dofork();
1168 		if (childpid < 0)
1169 		{
1170 			syserr("%s: cannot fork", label);
1171 			return (1);
1172 		}
1173 		if (childpid > 0)
1174 		{
1175 			auto int st;
1176 
1177 			/* parent -- wait for child to complete */
1178 			setproctitle("server %s child wait", CurHostName);
1179 			st = waitfor(childpid);
1180 			if (st == -1)
1181 				syserr("%s: lost child", label);
1182 			else if (!WIFEXITED(st))
1183 				syserr("%s: died on signal %d",
1184 					label, st & 0177);
1185 
1186 			/* if we exited on a QUIT command, complete the process */
1187 			if (WEXITSTATUS(st) == EX_QUIT)
1188 			{
1189 				disconnect(1, e);
1190 				finis();
1191 			}
1192 
1193 			return (1);
1194 		}
1195 		else
1196 		{
1197 			/* child */
1198 			InChild = TRUE;
1199 			QuickAbort = FALSE;
1200 			clearenvelope(e, FALSE);
1201 		}
1202 	}
1203 
1204 	/* open alias database */
1205 	initmaps(FALSE, e);
1206 
1207 	return (0);
1208 }
1209 
1210 # endif /* SMTP */
1211