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