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.62 (Berkeley) 03/27/95 (with SMTP)";
14 #else
15 static char sccsid[] = "@(#)srvrsmtp.c	8.62 (Berkeley) 03/27/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 = recipient(a, &e->e_sendqueue, 0, e);
529 
530 			if (Errors != 0)
531 				break;
532 
533 			/* no errors during parsing, but might be a duplicate */
534 			e->e_to = p;
535 			if (!bitset(QBADADDR, a->q_flags))
536 			{
537 				message("250 Recipient ok%s",
538 					bitset(QQUEUEUP, a->q_flags) ?
539 						" (will queue)" : "");
540 				nrcpts++;
541 			}
542 			else
543 			{
544 				/* punt -- should keep message in ADDRESS.... */
545 				message("550 Addressee unknown");
546 			}
547 			e->e_to = NULL;
548 			break;
549 
550 		  case CMDDATA:		/* data -- text of mail */
551 			SmtpPhase = "server DATA";
552 			if (!gotmail)
553 			{
554 				message("503 Need MAIL command");
555 				break;
556 			}
557 			else if (nrcpts <= 0)
558 			{
559 				message("503 Need RCPT (recipient)");
560 				break;
561 			}
562 
563 			/* check to see if we need to re-expand aliases */
564 			/* also reset QBADADDR on already-diagnosted addrs */
565 			doublequeue = FALSE;
566 			for (a = e->e_sendqueue; a != NULL; a = a->q_next)
567 			{
568 				if (bitset(QVERIFIED, a->q_flags))
569 				{
570 					/* need to re-expand aliases */
571 					doublequeue = TRUE;
572 				}
573 				if (bitset(QBADADDR, a->q_flags))
574 				{
575 					/* make this "go away" */
576 					a->q_flags |= QDONTSEND;
577 					a->q_flags &= ~QBADADDR;
578 				}
579 			}
580 
581 			/* collect the text of the message */
582 			SmtpPhase = "collect";
583 			collect(InChannel, TRUE, doublequeue, NULL, e);
584 			if (Errors != 0)
585 				goto abortmessage;
586 
587 			/* make sure we actually do delivery */
588 			e->e_flags &= ~EF_CLRQUEUE;
589 
590 			/* from now on, we have to operate silently */
591 			HoldErrs = TRUE;
592 			e->e_errormode = EM_MAIL;
593 
594 			/*
595 			**  Arrange to send to everyone.
596 			**	If sending to multiple people, mail back
597 			**		errors rather than reporting directly.
598 			**	In any case, don't mail back errors for
599 			**		anything that has happened up to
600 			**		now (the other end will do this).
601 			**	Truncate our transcript -- the mail has gotten
602 			**		to us successfully, and if we have
603 			**		to mail this back, it will be easier
604 			**		on the reader.
605 			**	Then send to everyone.
606 			**	Finally give a reply code.  If an error has
607 			**		already been given, don't mail a
608 			**		message back.
609 			**	We goose error returns by clearing error bit.
610 			*/
611 
612 			SmtpPhase = "delivery";
613 			e->e_xfp = freopen(queuename(e, 'x'), "w", e->e_xfp);
614 			id = e->e_id;
615 
616 			if (doublequeue)
617 			{
618 				/* make sure it is in the queue */
619 				queueup(e, TRUE, FALSE);
620 				if (e->e_sendmode == SM_QUEUE)
621 					e->e_flags |= EF_KEEPQUEUE;
622 			}
623 			else
624 			{
625 				/* send to all recipients */
626 				sendall(e, SM_DEFAULT);
627 			}
628 			e->e_to = NULL;
629 
630 			/* issue success message */
631 			message("250 %s Message accepted for delivery", id);
632 
633 			/* if we just queued, poke it */
634 			if (doublequeue && e->e_sendmode != SM_QUEUE)
635 			{
636 				extern pid_t dowork();
637 
638 				unlockqueue(e);
639 				(void) dowork(id, TRUE, TRUE, e);
640 			}
641 
642   abortmessage:
643 			/* if in a child, pop back to our parent */
644 			if (InChild)
645 				finis();
646 
647 			/* clean up a bit */
648 			gotmail = FALSE;
649 			dropenvelope(e);
650 			CurEnv = e = newenvelope(e, CurEnv);
651 			e->e_flags = BlankEnvelope.e_flags;
652 			break;
653 
654 		  case CMDRSET:		/* rset -- reset state */
655 			message("250 Reset state");
656 
657 			/* arrange to ignore any current send list */
658 			e->e_sendqueue = NULL;
659 			e->e_flags |= EF_CLRQUEUE;
660 			if (InChild)
661 				finis();
662 
663 			/* clean up a bit */
664 			gotmail = FALSE;
665 			dropenvelope(e);
666 			CurEnv = e = newenvelope(e, CurEnv);
667 			break;
668 
669 		  case CMDVRFY:		/* vrfy -- verify address */
670 		  case CMDEXPN:		/* expn -- expand address */
671 			vrfy = c->cmdcode == CMDVRFY;
672 			if (bitset(vrfy ? PRIV_NOVRFY : PRIV_NOEXPN,
673 						PrivacyFlags))
674 			{
675 				if (vrfy)
676 					message("252 Cannot VRFY user; try RCPT to attempt delivery (or try finger)");
677 				else
678 					message("502 Sorry, we do not allow this operation");
679 #ifdef LOG
680 				if (LogLevel > 5)
681 					syslog(LOG_INFO, "%s: %s [rejected]",
682 						CurSmtpClient, inp);
683 #endif
684 				break;
685 			}
686 			else if (!gothello &&
687 				 bitset(vrfy ? PRIV_NEEDVRFYHELO : PRIV_NEEDEXPNHELO,
688 						PrivacyFlags))
689 			{
690 				message("503 I demand that you introduce yourself first");
691 				break;
692 			}
693 			if (runinchild(vrfy ? "SMTP-VRFY" : "SMTP-EXPN", e) > 0)
694 				break;
695 #ifdef LOG
696 			if (LogLevel > 5)
697 				syslog(LOG_INFO, "%s: %s", CurSmtpClient, inp);
698 #endif
699 			vrfyqueue = NULL;
700 			QuickAbort = TRUE;
701 			if (vrfy)
702 				e->e_flags |= EF_VRFYONLY;
703 			while (*p != '\0' && isascii(*p) && isspace(*p))
704 				p++;
705 			if (*p == '\0')
706 			{
707 				message("501 Argument required");
708 				Errors++;
709 			}
710 			else
711 			{
712 				(void) sendtolist(p, NULLADDR, &vrfyqueue, 0, e);
713 			}
714 			if (Errors != 0)
715 			{
716 				if (InChild)
717 					finis();
718 				break;
719 			}
720 			if (vrfyqueue == NULL)
721 			{
722 				message("554 Nothing to %s", vrfy ? "VRFY" : "EXPN");
723 			}
724 			while (vrfyqueue != NULL)
725 			{
726 				a = vrfyqueue;
727 				while ((a = a->q_next) != NULL &&
728 				       bitset(QDONTSEND|QBADADDR, a->q_flags))
729 					continue;
730 				if (!bitset(QDONTSEND|QBADADDR, vrfyqueue->q_flags))
731 					printvrfyaddr(vrfyqueue, a == NULL);
732 				vrfyqueue = vrfyqueue->q_next;
733 			}
734 			if (InChild)
735 				finis();
736 			break;
737 
738 		  case CMDHELP:		/* help -- give user info */
739 			help(p);
740 			break;
741 
742 		  case CMDNOOP:		/* noop -- do nothing */
743 			message("250 OK");
744 			break;
745 
746 		  case CMDQUIT:		/* quit -- leave mail */
747 			message("221 %s closing connection", MyHostName);
748 
749 doquit:
750 			/* arrange to ignore any current send list */
751 			e->e_sendqueue = NULL;
752 
753 			/* avoid future 050 messages */
754 			disconnect(1, e);
755 
756 			if (InChild)
757 				ExitStat = EX_QUIT;
758 			finis();
759 
760 		  case CMDVERB:		/* set verbose mode */
761 			if (bitset(PRIV_NOEXPN, PrivacyFlags))
762 			{
763 				/* this would give out the same info */
764 				message("502 Verbose unavailable");
765 				break;
766 			}
767 			Verbose = TRUE;
768 			e->e_sendmode = SM_DELIVER;
769 			message("250 Verbose mode");
770 			break;
771 
772 		  case CMDONEX:		/* doing one transaction only */
773 			OneXact = TRUE;
774 			message("250 Only one transaction");
775 			break;
776 
777 # ifdef SMTPDEBUG
778 		  case CMDDBGQSHOW:	/* show queues */
779 			printf("Send Queue=");
780 			printaddr(e->e_sendqueue, TRUE);
781 			break;
782 
783 		  case CMDDBGDEBUG:	/* set debug mode */
784 			tTsetup(tTdvect, sizeof tTdvect, "0-99.1");
785 			tTflag(p);
786 			message("200 Debug set");
787 			break;
788 
789 # else /* not SMTPDEBUG */
790 		  case CMDDBGQSHOW:	/* show queues */
791 		  case CMDDBGDEBUG:	/* set debug mode */
792 # endif /* SMTPDEBUG */
793 		  case CMDLOGBOGUS:	/* bogus command */
794 # ifdef LOG
795 			if (LogLevel > 0)
796 				syslog(LOG_CRIT,
797 				    "\"%s\" command from %s (%s)",
798 				    c->cmdname, peerhostname,
799 				    anynet_ntoa(&RealHostAddr));
800 # endif
801 			/* FALL THROUGH */
802 
803 		  case CMDERROR:	/* unknown command */
804 			if (++badcommands > MAXBADCOMMANDS)
805 			{
806 				message("421 %s Too many bad commands; closing connection",
807 					MyHostName);
808 				goto doquit;
809 			}
810 
811 			message("500 Command unrecognized");
812 			break;
813 
814 		  default:
815 			errno = 0;
816 			syserr("500 smtp: unknown code %d", c->cmdcode);
817 			break;
818 		}
819 	}
820 }
821 /*
822 **  SKIPWORD -- skip a fixed word.
823 **
824 **	Parameters:
825 **		p -- place to start looking.
826 **		w -- word to skip.
827 **
828 **	Returns:
829 **		p following w.
830 **		NULL on error.
831 **
832 **	Side Effects:
833 **		clobbers the p data area.
834 */
835 
836 static char *
837 skipword(p, w)
838 	register char *p;
839 	char *w;
840 {
841 	register char *q;
842 	char *firstp = p;
843 
844 	/* find beginning of word */
845 	while (isascii(*p) && isspace(*p))
846 		p++;
847 	q = p;
848 
849 	/* find end of word */
850 	while (*p != '\0' && *p != ':' && !(isascii(*p) && isspace(*p)))
851 		p++;
852 	while (isascii(*p) && isspace(*p))
853 		*p++ = '\0';
854 	if (*p != ':')
855 	{
856 	  syntax:
857 		message("501 Syntax error in parameters scanning \"%s\"",
858 			firstp);
859 		Errors++;
860 		return (NULL);
861 	}
862 	*p++ = '\0';
863 	while (isascii(*p) && isspace(*p))
864 		p++;
865 
866 	if (*p == '\0')
867 		goto syntax;
868 
869 	/* see if the input word matches desired word */
870 	if (strcasecmp(q, w))
871 		goto syntax;
872 
873 	return (p);
874 }
875 /*
876 **  MAIL_ESMTP_ARGS -- process ESMTP arguments from MAIL line
877 **
878 **	Parameters:
879 **		kp -- the parameter key.
880 **		vp -- the value of that parameter.
881 **		e -- the envelope.
882 **
883 **	Returns:
884 **		none.
885 */
886 
887 mail_esmtp_args(kp, vp, e)
888 	char *kp;
889 	char *vp;
890 	ENVELOPE *e;
891 {
892 	if (strcasecmp(kp, "size") == 0)
893 	{
894 		if (vp == NULL)
895 		{
896 			usrerr("501 SIZE requires a value");
897 			/* NOTREACHED */
898 		}
899 # ifdef __STDC__
900 		e->e_msgsize = strtoul(vp, (char **) NULL, 10);
901 # else
902 		e->e_msgsize = strtol(vp, (char **) NULL, 10);
903 # endif
904 	}
905 	else if (strcasecmp(kp, "body") == 0)
906 	{
907 		if (vp == NULL)
908 		{
909 			usrerr("501 BODY requires a value");
910 			/* NOTREACHED */
911 		}
912 		if (strcasecmp(vp, "8bitmime") == 0)
913 		{
914 			SevenBitInput = FALSE;
915 		}
916 		else if (strcasecmp(vp, "7bit") == 0)
917 		{
918 			SevenBitInput = TRUE;
919 		}
920 		else
921 		{
922 			usrerr("501 Unknown BODY type %s",
923 				vp);
924 			/* NOTREACHED */
925 		}
926 		e->e_bodytype = newstr(vp);
927 	}
928 	else if (strcasecmp(kp, "envid") == 0)
929 	{
930 		if (vp == NULL)
931 		{
932 			usrerr("501 ENVID requires a value");
933 			/* NOTREACHED */
934 		}
935 		if (!xtextok(vp))
936 		{
937 			usrerr("501 Syntax error in ENVID parameter value");
938 			/* NOTREACHED */
939 		}
940 		if (e->e_envid != NULL)
941 		{
942 			usrerr("501 Duplicate ENVID parameter");
943 			/* NOTREACHED */
944 		}
945 		e->e_envid = newstr(vp);
946 	}
947 	else if (strcasecmp(kp, "ret") == 0)
948 	{
949 		if (vp == NULL)
950 		{
951 			usrerr("501 RET requires a value");
952 			/* NOTREACHED */
953 		}
954 		if (bitset(EF_RET_PARAM, e->e_flags))
955 		{
956 			usrerr("501 Duplicate RET parameter");
957 			/* NOTREACHED */
958 		}
959 		e->e_flags |= EF_RET_PARAM;
960 		if (strcasecmp(vp, "hdrs") == 0)
961 			e->e_flags |= EF_NO_BODY_RETN;
962 		else if (strcasecmp(vp, "full") != 0)
963 		{
964 			usrerr("501 Bad argument \"%s\" to RET", vp);
965 			/* NOTREACHED */
966 		}
967 	}
968 	else
969 	{
970 		usrerr("501 %s parameter unrecognized", kp);
971 		/* NOTREACHED */
972 	}
973 }
974 /*
975 **  RCPT_ESMTP_ARGS -- process ESMTP arguments from RCPT line
976 **
977 **	Parameters:
978 **		a -- the address corresponding to the To: parameter.
979 **		kp -- the parameter key.
980 **		vp -- the value of that parameter.
981 **		e -- the envelope.
982 **
983 **	Returns:
984 **		none.
985 */
986 
987 rcpt_esmtp_args(a, kp, vp, e)
988 	ADDRESS *a;
989 	char *kp;
990 	char *vp;
991 	ENVELOPE *e;
992 {
993 	if (strcasecmp(kp, "notify") == 0)
994 	{
995 		char *p;
996 
997 		if (vp == NULL)
998 		{
999 			usrerr("501 NOTIFY requires a value");
1000 			/* NOTREACHED */
1001 		}
1002 		a->q_flags &= ~(QPINGONSUCCESS|QPINGONFAILURE|QPINGONDELAY);
1003 		a->q_flags |= QHASNOTIFY;
1004 		if (strcasecmp(vp, "never") == 0)
1005 			return;
1006 		for (p = vp; p != NULL; vp = p)
1007 		{
1008 			p = strchr(p, ',');
1009 			if (p != NULL)
1010 				*p++ = '\0';
1011 			if (strcasecmp(vp, "success") == 0)
1012 				a->q_flags |= QPINGONSUCCESS;
1013 			else if (strcasecmp(vp, "failure") == 0)
1014 				a->q_flags |= QPINGONFAILURE;
1015 			else if (strcasecmp(vp, "delay") == 0)
1016 				a->q_flags |= QPINGONDELAY;
1017 			else
1018 			{
1019 				usrerr("501 Bad argument \"%s\"  to NOTIFY",
1020 					vp);
1021 				/* NOTREACHED */
1022 			}
1023 		}
1024 	}
1025 	else if (strcasecmp(kp, "orcpt") == 0)
1026 	{
1027 		if (vp == NULL)
1028 		{
1029 			usrerr("501 ORCPT requires a value");
1030 			/* NOTREACHED */
1031 		}
1032 		if (!xtextok(vp))
1033 		{
1034 			usrerr("501 Syntax error in ORCPT parameter value");
1035 			/* NOTREACHED */
1036 		}
1037 		if (a->q_orcpt != NULL)
1038 		{
1039 			usrerr("501 Duplicate ORCPT parameter");
1040 			/* NOTREACHED */
1041 		}
1042 		a->q_orcpt = newstr(vp);
1043 	}
1044 	else
1045 	{
1046 		usrerr("501 %s parameter unrecognized", kp);
1047 		/* NOTREACHED */
1048 	}
1049 }
1050 /*
1051 **  PRINTVRFYADDR -- print an entry in the verify queue
1052 **
1053 **	Parameters:
1054 **		a -- the address to print
1055 **		last -- set if this is the last one.
1056 **
1057 **	Returns:
1058 **		none.
1059 **
1060 **	Side Effects:
1061 **		Prints the appropriate 250 codes.
1062 */
1063 
1064 printvrfyaddr(a, last)
1065 	register ADDRESS *a;
1066 	bool last;
1067 {
1068 	char fmtbuf[20];
1069 
1070 	strcpy(fmtbuf, "250");
1071 	fmtbuf[3] = last ? ' ' : '-';
1072 
1073 	if (a->q_fullname == NULL)
1074 	{
1075 		if (strchr(a->q_user, '@') == NULL)
1076 			strcpy(&fmtbuf[4], "<%s@%s>");
1077 		else
1078 			strcpy(&fmtbuf[4], "<%s>");
1079 		message(fmtbuf, a->q_user, MyHostName);
1080 	}
1081 	else
1082 	{
1083 		if (strchr(a->q_user, '@') == NULL)
1084 			strcpy(&fmtbuf[4], "%s <%s@%s>");
1085 		else
1086 			strcpy(&fmtbuf[4], "%s <%s>");
1087 		message(fmtbuf, a->q_fullname, a->q_user, MyHostName);
1088 	}
1089 }
1090 /*
1091 **  HELP -- implement the HELP command.
1092 **
1093 **	Parameters:
1094 **		topic -- the topic we want help for.
1095 **
1096 **	Returns:
1097 **		none.
1098 **
1099 **	Side Effects:
1100 **		outputs the help file to message output.
1101 */
1102 
1103 help(topic)
1104 	char *topic;
1105 {
1106 	register FILE *hf;
1107 	int len;
1108 	char buf[MAXLINE];
1109 	bool noinfo;
1110 
1111 	if (HelpFile == NULL || (hf = fopen(HelpFile, "r")) == NULL)
1112 	{
1113 		/* no help */
1114 		errno = 0;
1115 		message("502 HELP not implemented");
1116 		return;
1117 	}
1118 
1119 	if (topic == NULL || *topic == '\0')
1120 		topic = "smtp";
1121 	else
1122 		makelower(topic);
1123 
1124 	len = strlen(topic);
1125 	noinfo = TRUE;
1126 
1127 	while (fgets(buf, sizeof buf, hf) != NULL)
1128 	{
1129 		if (strncmp(buf, topic, len) == 0)
1130 		{
1131 			register char *p;
1132 
1133 			p = strchr(buf, '\t');
1134 			if (p == NULL)
1135 				p = buf;
1136 			else
1137 				p++;
1138 			fixcrlf(p, TRUE);
1139 			message("214-%s", p);
1140 			noinfo = FALSE;
1141 		}
1142 	}
1143 
1144 	if (noinfo)
1145 		message("504 HELP topic unknown");
1146 	else
1147 		message("214 End of HELP info");
1148 	(void) fclose(hf);
1149 }
1150 /*
1151 **  RUNINCHILD -- return twice -- once in the child, then in the parent again
1152 **
1153 **	Parameters:
1154 **		label -- a string used in error messages
1155 **
1156 **	Returns:
1157 **		zero in the child
1158 **		one in the parent
1159 **
1160 **	Side Effects:
1161 **		none.
1162 */
1163 
1164 runinchild(label, e)
1165 	char *label;
1166 	register ENVELOPE *e;
1167 {
1168 	int childpid;
1169 
1170 	if (!OneXact)
1171 	{
1172 		childpid = dofork();
1173 		if (childpid < 0)
1174 		{
1175 			syserr("%s: cannot fork", label);
1176 			return (1);
1177 		}
1178 		if (childpid > 0)
1179 		{
1180 			auto int st;
1181 
1182 			/* parent -- wait for child to complete */
1183 			setproctitle("server %s child wait", CurHostName);
1184 			st = waitfor(childpid);
1185 			if (st == -1)
1186 				syserr("%s: lost child", label);
1187 			else if (!WIFEXITED(st))
1188 				syserr("%s: died on signal %d",
1189 					label, st & 0177);
1190 
1191 			/* if we exited on a QUIT command, complete the process */
1192 			if (WEXITSTATUS(st) == EX_QUIT)
1193 			{
1194 				disconnect(1, e);
1195 				finis();
1196 			}
1197 
1198 			return (1);
1199 		}
1200 		else
1201 		{
1202 			/* child */
1203 			InChild = TRUE;
1204 			QuickAbort = FALSE;
1205 			clearenvelope(e, FALSE);
1206 		}
1207 	}
1208 
1209 	/* open alias database */
1210 	initmaps(FALSE, e);
1211 
1212 	return (0);
1213 }
1214 
1215 # endif /* SMTP */
1216