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