1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988 Regents of the University of California.
4  * 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	6.60 (Berkeley) 06/05/93 (with SMTP)";
14 #else
15 static char sccsid[] = "@(#)srvrsmtp.c	6.60 (Berkeley) 06/05/93 (without SMTP)";
16 #endif
17 #endif /* not lint */
18 
19 # include <errno.h>
20 # include <signal.h>
21 
22 # ifdef SMTP
23 
24 /*
25 **  SMTP -- run the SMTP protocol.
26 **
27 **	Parameters:
28 **		none.
29 **
30 **	Returns:
31 **		never.
32 **
33 **	Side Effects:
34 **		Reads commands from the input channel and processes
35 **			them.
36 */
37 
38 struct cmd
39 {
40 	char	*cmdname;	/* command name */
41 	int	cmdcode;	/* internal code, see below */
42 };
43 
44 /* values for cmdcode */
45 # define CMDERROR	0	/* bad command */
46 # define CMDMAIL	1	/* mail -- designate sender */
47 # define CMDRCPT	2	/* rcpt -- designate recipient */
48 # define CMDDATA	3	/* data -- send message text */
49 # define CMDRSET	4	/* rset -- reset state */
50 # define CMDVRFY	5	/* vrfy -- verify address */
51 # define CMDEXPN	6	/* expn -- expand address */
52 # define CMDNOOP	7	/* noop -- do nothing */
53 # define CMDQUIT	8	/* quit -- close connection and die */
54 # define CMDHELO	9	/* helo -- be polite */
55 # define CMDHELP	10	/* help -- give usage info */
56 # define CMDEHLO	11	/* ehlo -- extended helo (RFC 1425) */
57 /* non-standard commands */
58 # define CMDONEX	16	/* onex -- sending one transaction only */
59 # define CMDVERB	17	/* verb -- go into verbose mode */
60 /* debugging-only commands, only enabled if SMTPDEBUG is defined */
61 # define CMDDBGQSHOW	24	/* showq -- show send queue */
62 # define CMDDBGDEBUG	25	/* debug -- set debug mode */
63 
64 static struct cmd	CmdTab[] =
65 {
66 	"mail",		CMDMAIL,
67 	"rcpt",		CMDRCPT,
68 	"data",		CMDDATA,
69 	"rset",		CMDRSET,
70 	"vrfy",		CMDVRFY,
71 	"expn",		CMDEXPN,
72 	"help",		CMDHELP,
73 	"noop",		CMDNOOP,
74 	"quit",		CMDQUIT,
75 	"helo",		CMDHELO,
76 	"ehlo",		CMDEHLO,
77 	"verb",		CMDVERB,
78 	"onex",		CMDONEX,
79 	/*
80 	 * remaining commands are here only
81 	 * to trap and log attempts to use them
82 	 */
83 	"showq",	CMDDBGQSHOW,
84 	"debug",	CMDDBGDEBUG,
85 	NULL,		CMDERROR,
86 };
87 
88 bool	InChild = FALSE;		/* true if running in a subprocess */
89 bool	OneXact = FALSE;		/* one xaction only this run */
90 
91 #define EX_QUIT		22		/* special code for QUIT command */
92 
93 smtp(e)
94 	register ENVELOPE *e;
95 {
96 	register char *p;
97 	register struct cmd *c;
98 	char *cmd;
99 	static char *skipword();
100 	auto ADDRESS *vrfyqueue;
101 	ADDRESS *a;
102 	bool gotmail;			/* mail command received */
103 	bool gothello;			/* helo command received */
104 	bool vrfy;			/* set if this is a vrfy command */
105 	char *protocol;			/* sending protocol */
106 	char *sendinghost;		/* sending hostname */
107 	long msize;			/* approximate maximum message size */
108 	auto char *delimptr;
109 	char *id;
110 	int nrcpts;			/* number of RCPT commands */
111 	char inp[MAXLINE];
112 	char cmdbuf[MAXLINE];
113 	extern char Version[];
114 	extern ENVELOPE BlankEnvelope;
115 
116 	if (fileno(OutChannel) != fileno(stdout))
117 	{
118 		/* arrange for debugging output to go to remote host */
119 		(void) dup2(fileno(OutChannel), fileno(stdout));
120 	}
121 	settime(e);
122 	CurHostName = RealHostName;
123 	setproctitle("server %s startup", CurHostName);
124 	expand("\201e", inp, &inp[sizeof inp], e);
125 	message("220-%s", inp);
126 	message("220 ESMTP spoken here");
127 	protocol = NULL;
128 	sendinghost = macvalue('s', e);
129 	gothello = FALSE;
130 	gotmail = FALSE;
131 	for (;;)
132 	{
133 		/* arrange for backout */
134 		if (setjmp(TopFrame) > 0 && InChild)
135 		{
136 			QuickAbort = FALSE;
137 			SuprErrs = TRUE;
138 			finis();
139 		}
140 		QuickAbort = FALSE;
141 		HoldErrs = FALSE;
142 		LogUsrErrs = FALSE;
143 		e->e_flags &= ~EF_VRFYONLY;
144 
145 		/* setup for the read */
146 		e->e_to = NULL;
147 		Errors = 0;
148 		(void) fflush(stdout);
149 
150 		/* read the input line */
151 		SmtpPhase = "server cmd read";
152 		setproctitle("server %s cmd read", CurHostName);
153 		p = sfgets(inp, sizeof inp, InChannel, TimeOuts.to_nextcommand,
154 				SmtpPhase);
155 
156 		/* handle errors */
157 		if (p == NULL)
158 		{
159 			/* end of file, just die */
160 			message("421 %s Lost input channel from %s",
161 				MyHostName, CurHostName);
162 #ifdef LOG
163 			if (LogLevel > 1)
164 				syslog(LOG_NOTICE, "lost input channel from %s",
165 					CurHostName);
166 #endif
167 			if (InChild)
168 				ExitStat = EX_QUIT;
169 			finis();
170 		}
171 
172 		/* clean up end of line */
173 		fixcrlf(inp, TRUE);
174 
175 		/* echo command to transcript */
176 		if (e->e_xfp != NULL)
177 			fprintf(e->e_xfp, "<<< %s\n", inp);
178 
179 		if (e->e_id == NULL)
180 			setproctitle("%s: %s", CurHostName, inp);
181 		else
182 			setproctitle("%s %s: %s", e->e_id, CurHostName, inp);
183 
184 		/* break off command */
185 		for (p = inp; isascii(*p) && isspace(*p); p++)
186 			continue;
187 		cmd = cmdbuf;
188 		while (*p != '\0' &&
189 		       !(isascii(*p) && isspace(*p)) &&
190 		       cmd < &cmdbuf[sizeof cmdbuf - 2])
191 			*cmd++ = *p++;
192 		*cmd = '\0';
193 
194 		/* throw away leading whitespace */
195 		while (isascii(*p) && isspace(*p))
196 			p++;
197 
198 		/* decode command */
199 		for (c = CmdTab; c->cmdname != NULL; c++)
200 		{
201 			if (!strcasecmp(c->cmdname, cmdbuf))
202 				break;
203 		}
204 
205 		/* reset errors */
206 		errno = 0;
207 
208 		/* process command */
209 		switch (c->cmdcode)
210 		{
211 		  case CMDHELO:		/* hello -- introduce yourself */
212 		  case CMDEHLO:		/* extended hello */
213 			if (c->cmdcode == CMDEHLO)
214 			{
215 				protocol = "ESMTP";
216 				SmtpPhase = "server EHLO";
217 			}
218 			else
219 			{
220 				protocol = "SMTP";
221 				SmtpPhase = "server HELO";
222 			}
223 			sendinghost = newstr(p);
224 			if (strcasecmp(p, RealHostName) != 0)
225 			{
226 				auth_warning(e, "Host %s claimed to be %s",
227 					RealHostName, p);
228 			}
229 			p = macvalue('_', e);
230 			if (p == NULL)
231 				p = RealHostName;
232 
233 			gothello = TRUE;
234 			if (c->cmdcode != CMDEHLO)
235 			{
236 				/* print old message and be done with it */
237 				message("250 %s Hello %s, pleased to meet you",
238 					MyHostName, p);
239 				break;
240 			}
241 
242 			/* print extended message and brag */
243 			message("250-%s Hello %s, pleased to meet you",
244 				MyHostName, p);
245 			if (!bitset(PRIV_NOEXPN, PrivacyFlags))
246 				message("250-EXPN");
247 			if (MaxMessageSize > 0)
248 				message("250-SIZE %ld", MaxMessageSize);
249 			else
250 				message("250-SIZE");
251 			message("250 HELP");
252 			break;
253 
254 		  case CMDMAIL:		/* mail -- designate sender */
255 			SmtpPhase = "server MAIL";
256 
257 			/* check for validity of this command */
258 			if (!gothello)
259 			{
260 				/* set sending host to our known value */
261 				if (sendinghost == NULL)
262 					sendinghost = RealHostName;
263 
264 				if (bitset(PRIV_NEEDMAILHELO, PrivacyFlags))
265 				{
266 					message("503 Polite people say HELO first");
267 					break;
268 				}
269 				else
270 				{
271 					auth_warning(e,
272 						"Host %s didn't use HELO protocol",
273 						RealHostName);
274 				}
275 			}
276 			if (gotmail)
277 			{
278 				message("503 Sender already specified");
279 				break;
280 			}
281 			if (InChild)
282 			{
283 				errno = 0;
284 				syserr("503 Nested MAIL command: MAIL %s", p);
285 				finis();
286 			}
287 
288 			/* fork a subprocess to process this command */
289 			if (runinchild("SMTP-MAIL", e) > 0)
290 				break;
291 			if (protocol == NULL)
292 				protocol = "SMTP";
293 			define('r', protocol, e);
294 			define('s', sendinghost, e);
295 			initsys(e);
296 			nrcpts = 0;
297 			setproctitle("%s %s: %s", e->e_id, CurHostName, inp);
298 
299 			/* child -- go do the processing */
300 			p = skipword(p, "from");
301 			if (p == NULL)
302 				break;
303 			if (setjmp(TopFrame) > 0)
304 			{
305 				/* this failed -- undo work */
306 				if (InChild)
307 				{
308 					QuickAbort = FALSE;
309 					SuprErrs = TRUE;
310 					finis();
311 				}
312 				break;
313 			}
314 			QuickAbort = TRUE;
315 
316 			/* must parse sender first */
317 			delimptr = NULL;
318 			setsender(p, e, &delimptr, FALSE);
319 			p = delimptr;
320 			if (p != NULL && *p != '\0')
321 				*p++ = '\0';
322 
323 			/* now parse ESMTP arguments */
324 			msize = 0;
325 			for (; p != NULL && *p != '\0'; p++)
326 			{
327 				char *kp;
328 				char *vp;
329 
330 				/* locate the beginning of the keyword */
331 				while (isascii(*p) && isspace(*p))
332 					p++;
333 				if (*p == '\0')
334 					break;
335 				kp = p;
336 
337 				/* skip to the value portion */
338 				while (isascii(*p) && isalnum(*p) || *p == '-')
339 					p++;
340 				if (*p == '=')
341 				{
342 					*p++ = '\0';
343 					vp = p;
344 
345 					/* skip to the end of the value */
346 					while (*p != '\0' && *p != ' ' &&
347 					       !(isascii(*p) && iscntrl(*p)) &&
348 					       *p != '=')
349 						p++;
350 				}
351 
352 				if (*p != '\0')
353 					*p++ = '\0';
354 
355 				if (tTd(19, 1))
356 					printf("MAIL: got arg %s=%s\n", kp,
357 						vp == NULL ? "<null>" : vp);
358 
359 				if (strcasecmp(kp, "size") == 0)
360 				{
361 					if (vp == NULL)
362 					{
363 						usrerr("501 SIZE requires a value");
364 						/* NOTREACHED */
365 					}
366 					msize = atol(vp);
367 				}
368 				else if (strcasecmp(kp, "body") == 0)
369 				{
370 					if (vp == NULL)
371 					{
372 						usrerr("501 BODY requires a value");
373 						/* NOTREACHED */
374 					}
375 # ifdef MIME
376 					if (strcasecmp(vp, "8bitmime") == 0)
377 					{
378 						e->e_bodytype = "8BITMIME";
379 						SevenBit = FALSE;
380 					}
381 					else if (strcasecmp(vp, "7bit") == 0)
382 					{
383 						e->e_bodytype = "7BIT";
384 						SevenBit = TRUE;
385 					}
386 					else
387 					{
388 						usrerr("501 Unknown BODY type %s",
389 							vp);
390 					}
391 # endif
392 				}
393 				else
394 				{
395 					usrerr("501 %s parameter unrecognized", kp);
396 					/* NOTREACHED */
397 				}
398 			}
399 
400 			if (MaxMessageSize > 0 && msize > MaxMessageSize)
401 			{
402 				usrerr("552 Message size exceeds fixed maximum message size (%ld)",
403 					MaxMessageSize);
404 				/* NOTREACHED */
405 			}
406 
407 			if (!enoughspace(msize))
408 			{
409 				message("452 Insufficient disk space; try again later");
410 				break;
411 			}
412 			message("250 Sender ok");
413 			gotmail = TRUE;
414 			break;
415 
416 		  case CMDRCPT:		/* rcpt -- designate recipient */
417 			if (!gotmail)
418 			{
419 				usrerr("503 Need MAIL before RCPT");
420 				break;
421 			}
422 			SmtpPhase = "server RCPT";
423 			if (setjmp(TopFrame) > 0)
424 			{
425 				e->e_flags &= ~EF_FATALERRS;
426 				break;
427 			}
428 			QuickAbort = TRUE;
429 			LogUsrErrs = TRUE;
430 
431 			if (e->e_sendmode != SM_DELIVER)
432 				e->e_flags |= EF_VRFYONLY;
433 
434 			p = skipword(p, "to");
435 			if (p == NULL)
436 				break;
437 			a = parseaddr(p, (ADDRESS *) NULL, 1, ' ', NULL, e);
438 			if (a == NULL)
439 				break;
440 			a->q_flags |= QPRIMARY;
441 			a = recipient(a, &e->e_sendqueue, e);
442 			if (Errors != 0)
443 				break;
444 
445 			/* no errors during parsing, but might be a duplicate */
446 			e->e_to = p;
447 			if (!bitset(QBADADDR, a->q_flags))
448 			{
449 				message("250 Recipient ok");
450 				nrcpts++;
451 			}
452 			else
453 			{
454 				/* punt -- should keep message in ADDRESS.... */
455 				message("550 Addressee unknown");
456 			}
457 			e->e_to = NULL;
458 			break;
459 
460 		  case CMDDATA:		/* data -- text of mail */
461 			SmtpPhase = "server DATA";
462 			if (!gotmail)
463 			{
464 				message("503 Need MAIL command");
465 				break;
466 			}
467 			else if (e->e_nrcpts <= 0)
468 			{
469 				message("503 Need RCPT (recipient)");
470 				break;
471 			}
472 
473 			/* check to see if we need to re-expand aliases */
474 			for (a = e->e_sendqueue; a != NULL; a = a->q_next)
475 			{
476 				if (bitset(QVERIFIED, a->q_flags))
477 					break;
478 			}
479 
480 			/* collect the text of the message */
481 			SmtpPhase = "collect";
482 			collect(TRUE, a != NULL, e);
483 			e->e_flags &= ~EF_FATALERRS;
484 			if (Errors != 0)
485 				goto abortmessage;
486 
487 			/*
488 			**  Arrange to send to everyone.
489 			**	If sending to multiple people, mail back
490 			**		errors rather than reporting directly.
491 			**	In any case, don't mail back errors for
492 			**		anything that has happened up to
493 			**		now (the other end will do this).
494 			**	Truncate our transcript -- the mail has gotten
495 			**		to us successfully, and if we have
496 			**		to mail this back, it will be easier
497 			**		on the reader.
498 			**	Then send to everyone.
499 			**	Finally give a reply code.  If an error has
500 			**		already been given, don't mail a
501 			**		message back.
502 			**	We goose error returns by clearing error bit.
503 			*/
504 
505 			SmtpPhase = "delivery";
506 			if (nrcpts != 1 && a == NULL)
507 			{
508 				HoldErrs = TRUE;
509 				e->e_errormode = EM_MAIL;
510 			}
511 			e->e_xfp = freopen(queuename(e, 'x'), "w", e->e_xfp);
512 			id = e->e_id;
513 
514 			/* send to all recipients */
515 			sendall(e, a == NULL ? SM_DEFAULT : SM_QUEUE);
516 			e->e_to = NULL;
517 
518 			/* save statistics */
519 			markstats(e, (ADDRESS *) NULL);
520 
521 			/* issue success if appropriate and reset */
522 			if (Errors == 0 || HoldErrs)
523 				message("250 %s Message accepted for delivery", id);
524 
525 			if (bitset(EF_FATALERRS, e->e_flags) && !HoldErrs)
526 			{
527 				/* avoid sending back an extra message */
528 				e->e_flags &= ~EF_FATALERRS;
529 				e->e_flags |= EF_CLRQUEUE;
530 			}
531 			else
532 			{
533 				/* from now on, we have to operate silently */
534 				HoldErrs = TRUE;
535 				e->e_errormode = EM_MAIL;
536 
537 				/* if we just queued, poke it */
538 				if (a != NULL && e->e_sendmode != SM_QUEUE)
539 				{
540 					unlockqueue(e);
541 					dowork(id, TRUE, TRUE, e);
542 					e->e_id = NULL;
543 				}
544 			}
545 
546   abortmessage:
547 			/* if in a child, pop back to our parent */
548 			if (InChild)
549 				finis();
550 
551 			/* clean up a bit */
552 			gotmail = FALSE;
553 			dropenvelope(e);
554 			CurEnv = e = newenvelope(e, CurEnv);
555 			e->e_flags = BlankEnvelope.e_flags;
556 			break;
557 
558 		  case CMDRSET:		/* rset -- reset state */
559 			message("250 Reset state");
560 			if (InChild)
561 				finis();
562 
563 			/* clean up a bit */
564 			gotmail = FALSE;
565 			dropenvelope(e);
566 			CurEnv = e = newenvelope(e, CurEnv);
567 			break;
568 
569 		  case CMDVRFY:		/* vrfy -- verify address */
570 		  case CMDEXPN:		/* expn -- expand address */
571 			vrfy = c->cmdcode == CMDVRFY;
572 			if (bitset(vrfy ? PRIV_NOVRFY : PRIV_NOEXPN,
573 						PrivacyFlags))
574 			{
575 				if (vrfy)
576 					message("252 Who's to say?");
577 				else
578 					message("502 That's none of your business");
579 				break;
580 			}
581 			else if (!gothello &&
582 				 bitset(vrfy ? PRIV_NEEDVRFYHELO : PRIV_NEEDEXPNHELO,
583 						PrivacyFlags))
584 			{
585 				message("503 I demand that you introduce yourself first");
586 				break;
587 			}
588 			if (runinchild(vrfy ? "SMTP-VRFY" : "SMTP-EXPN", e) > 0)
589 				break;
590 #ifdef LOG
591 			if (LogLevel > 5)
592 				syslog(LOG_INFO, "%s: %s", CurHostName, inp);
593 #endif
594 			vrfyqueue = NULL;
595 			QuickAbort = TRUE;
596 			if (vrfy)
597 				e->e_flags |= EF_VRFYONLY;
598 			(void) sendtolist(p, (ADDRESS *) NULL, &vrfyqueue, e);
599 			if (Errors != 0)
600 			{
601 				if (InChild)
602 					finis();
603 				break;
604 			}
605 			while (vrfyqueue != NULL)
606 			{
607 				register ADDRESS *a = vrfyqueue->q_next;
608 
609 				while (a != NULL && bitset(QDONTSEND|QBADADDR, a->q_flags))
610 					a = a->q_next;
611 
612 				if (!bitset(QDONTSEND|QBADADDR, vrfyqueue->q_flags))
613 					printvrfyaddr(vrfyqueue, a == NULL);
614 				else if (a == NULL)
615 					message("554 Self destructive alias loop");
616 				vrfyqueue = a;
617 			}
618 			if (InChild)
619 				finis();
620 			break;
621 
622 		  case CMDHELP:		/* help -- give user info */
623 			help(p);
624 			break;
625 
626 		  case CMDNOOP:		/* noop -- do nothing */
627 			message("200 OK");
628 			break;
629 
630 		  case CMDQUIT:		/* quit -- leave mail */
631 			message("221 %s closing connection", MyHostName);
632 
633 			/* avoid future 050 messages */
634 			Verbose = FALSE;
635 
636 			if (InChild)
637 				ExitStat = EX_QUIT;
638 			finis();
639 
640 		  case CMDVERB:		/* set verbose mode */
641 			if (bitset(PRIV_NOEXPN, PrivacyFlags))
642 			{
643 				/* this would give out the same info */
644 				message("502 Verbose unavailable");
645 				break;
646 			}
647 			Verbose = TRUE;
648 			e->e_sendmode = SM_DELIVER;
649 			message("250 Verbose mode");
650 			break;
651 
652 		  case CMDONEX:		/* doing one transaction only */
653 			OneXact = TRUE;
654 			message("250 Only one transaction");
655 			break;
656 
657 # ifdef SMTPDEBUG
658 		  case CMDDBGQSHOW:	/* show queues */
659 			printf("Send Queue=");
660 			printaddr(e->e_sendqueue, TRUE);
661 			break;
662 
663 		  case CMDDBGDEBUG:	/* set debug mode */
664 			tTsetup(tTdvect, sizeof tTdvect, "0-99.1");
665 			tTflag(p);
666 			message("200 Debug set");
667 			break;
668 
669 # else /* not SMTPDEBUG */
670 
671 		  case CMDDBGQSHOW:	/* show queues */
672 		  case CMDDBGDEBUG:	/* set debug mode */
673 # ifdef LOG
674 			if (LogLevel > 0)
675 				syslog(LOG_NOTICE,
676 				    "\"%s\" command from %s (%s)",
677 				    c->cmdname, RealHostName,
678 				    anynet_ntoa(&RealHostAddr));
679 # endif
680 			/* FALL THROUGH */
681 # endif /* SMTPDEBUG */
682 
683 		  case CMDERROR:	/* unknown command */
684 			message("500 Command unrecognized");
685 			break;
686 
687 		  default:
688 			errno = 0;
689 			syserr("500 smtp: unknown code %d", c->cmdcode);
690 			break;
691 		}
692 	}
693 }
694 /*
695 **  SKIPWORD -- skip a fixed word.
696 **
697 **	Parameters:
698 **		p -- place to start looking.
699 **		w -- word to skip.
700 **
701 **	Returns:
702 **		p following w.
703 **		NULL on error.
704 **
705 **	Side Effects:
706 **		clobbers the p data area.
707 */
708 
709 static char *
710 skipword(p, w)
711 	register char *p;
712 	char *w;
713 {
714 	register char *q;
715 
716 	/* find beginning of word */
717 	while (isascii(*p) && isspace(*p))
718 		p++;
719 	q = p;
720 
721 	/* find end of word */
722 	while (*p != '\0' && *p != ':' && !(isascii(*p) && isspace(*p)))
723 		p++;
724 	while (isascii(*p) && isspace(*p))
725 		*p++ = '\0';
726 	if (*p != ':')
727 	{
728 	  syntax:
729 		message("501 Syntax error");
730 		Errors++;
731 		return (NULL);
732 	}
733 	*p++ = '\0';
734 	while (isascii(*p) && isspace(*p))
735 		p++;
736 
737 	/* see if the input word matches desired word */
738 	if (strcasecmp(q, w))
739 		goto syntax;
740 
741 	return (p);
742 }
743 /*
744 **  PRINTVRFYADDR -- print an entry in the verify queue
745 **
746 **	Parameters:
747 **		a -- the address to print
748 **		last -- set if this is the last one.
749 **
750 **	Returns:
751 **		none.
752 **
753 **	Side Effects:
754 **		Prints the appropriate 250 codes.
755 */
756 
757 printvrfyaddr(a, last)
758 	register ADDRESS *a;
759 	bool last;
760 {
761 	char fmtbuf[20];
762 
763 	strcpy(fmtbuf, "250");
764 	fmtbuf[3] = last ? ' ' : '-';
765 
766 	if (a->q_fullname == NULL)
767 	{
768 		if (strchr(a->q_user, '@') == NULL)
769 			strcpy(&fmtbuf[4], "<%s@%s>");
770 		else
771 			strcpy(&fmtbuf[4], "<%s>");
772 		message(fmtbuf, a->q_user, MyHostName);
773 	}
774 	else
775 	{
776 		if (strchr(a->q_user, '@') == NULL)
777 			strcpy(&fmtbuf[4], "%s <%s@%s>");
778 		else
779 			strcpy(&fmtbuf[4], "%s <%s>");
780 		message(fmtbuf, a->q_fullname, a->q_user, MyHostName);
781 	}
782 }
783 /*
784 **  HELP -- implement the HELP command.
785 **
786 **	Parameters:
787 **		topic -- the topic we want help for.
788 **
789 **	Returns:
790 **		none.
791 **
792 **	Side Effects:
793 **		outputs the help file to message output.
794 */
795 
796 help(topic)
797 	char *topic;
798 {
799 	register FILE *hf;
800 	int len;
801 	char buf[MAXLINE];
802 	bool noinfo;
803 
804 	if (HelpFile == NULL || (hf = fopen(HelpFile, "r")) == NULL)
805 	{
806 		/* no help */
807 		errno = 0;
808 		message("502 HELP not implemented");
809 		return;
810 	}
811 
812 	if (topic == NULL || *topic == '\0')
813 		topic = "smtp";
814 	else
815 		makelower(topic);
816 
817 	len = strlen(topic);
818 	noinfo = TRUE;
819 
820 	while (fgets(buf, sizeof buf, hf) != NULL)
821 	{
822 		if (strncmp(buf, topic, len) == 0)
823 		{
824 			register char *p;
825 
826 			p = strchr(buf, '\t');
827 			if (p == NULL)
828 				p = buf;
829 			else
830 				p++;
831 			fixcrlf(p, TRUE);
832 			message("214-%s", p);
833 			noinfo = FALSE;
834 		}
835 	}
836 
837 	if (noinfo)
838 		message("504 HELP topic unknown");
839 	else
840 		message("214 End of HELP info");
841 	(void) fclose(hf);
842 }
843 /*
844 **  RUNINCHILD -- return twice -- once in the child, then in the parent again
845 **
846 **	Parameters:
847 **		label -- a string used in error messages
848 **
849 **	Returns:
850 **		zero in the child
851 **		one in the parent
852 **
853 **	Side Effects:
854 **		none.
855 */
856 
857 runinchild(label, e)
858 	char *label;
859 	register ENVELOPE *e;
860 {
861 	int childpid;
862 
863 	if (!OneXact)
864 	{
865 		childpid = dofork();
866 		if (childpid < 0)
867 		{
868 			syserr("%s: cannot fork", label);
869 			return (1);
870 		}
871 		if (childpid > 0)
872 		{
873 			auto int st;
874 
875 			/* parent -- wait for child to complete */
876 			setproctitle("server %s child wait", CurHostName);
877 			st = waitfor(childpid);
878 			if (st == -1)
879 				syserr("%s: lost child", label);
880 
881 			/* if we exited on a QUIT command, complete the process */
882 			if (st == (EX_QUIT << 8))
883 				finis();
884 
885 			return (1);
886 		}
887 		else
888 		{
889 			/* child */
890 			InChild = TRUE;
891 			QuickAbort = FALSE;
892 			clearenvelope(e, FALSE);
893 		}
894 	}
895 
896 	/* open alias database */
897 	initmaps(FALSE, e);
898 
899 	return (0);
900 }
901 
902 # endif /* SMTP */
903