122712Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
362532Sbostic  * Copyright (c) 1988, 1993
462532Sbostic  *	The Regents of the University of California.  All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822712Sdist 
933731Sbostic # include "sendmail.h"
1022712Sdist 
1133731Sbostic #ifndef lint
1233731Sbostic #ifdef SMTP
13*66745Seric static char sccsid[] = "@(#)srvrsmtp.c	8.33 (Berkeley) 04/12/94 (with SMTP)";
1433731Sbostic #else
15*66745Seric static char sccsid[] = "@(#)srvrsmtp.c	8.33 (Berkeley) 04/12/94 (without SMTP)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
199339Seric # include <errno.h>
204549Seric 
2133731Sbostic # ifdef SMTP
224556Seric 
234549Seric /*
244549Seric **  SMTP -- run the SMTP protocol.
254549Seric **
264549Seric **	Parameters:
274549Seric **		none.
284549Seric **
294549Seric **	Returns:
304549Seric **		never.
314549Seric **
324549Seric **	Side Effects:
334549Seric **		Reads commands from the input channel and processes
344549Seric **			them.
354549Seric */
364549Seric 
374549Seric struct cmd
384549Seric {
394549Seric 	char	*cmdname;	/* command name */
404549Seric 	int	cmdcode;	/* internal code, see below */
414549Seric };
424549Seric 
434549Seric /* values for cmdcode */
444549Seric # define CMDERROR	0	/* bad command */
454549Seric # define CMDMAIL	1	/* mail -- designate sender */
464976Seric # define CMDRCPT	2	/* rcpt -- designate recipient */
474549Seric # define CMDDATA	3	/* data -- send message text */
489339Seric # define CMDRSET	4	/* rset -- reset state */
499339Seric # define CMDVRFY	5	/* vrfy -- verify address */
5058092Seric # define CMDEXPN	6	/* expn -- expand address */
519339Seric # define CMDNOOP	7	/* noop -- do nothing */
529339Seric # define CMDQUIT	8	/* quit -- close connection and die */
539339Seric # define CMDHELO	9	/* helo -- be polite */
5458092Seric # define CMDHELP	10	/* help -- give usage info */
5558323Seric # define CMDEHLO	11	/* ehlo -- extended helo (RFC 1425) */
5658092Seric /* non-standard commands */
5758092Seric # define CMDONEX	16	/* onex -- sending one transaction only */
5858092Seric # define CMDVERB	17	/* verb -- go into verbose mode */
5964685Seric /* use this to catch and log "door handle" attempts on your system */
6064685Seric # define CMDLOGBOGUS	23	/* bogus command that should be logged */
6136230Skarels /* debugging-only commands, only enabled if SMTPDEBUG is defined */
6258092Seric # define CMDDBGQSHOW	24	/* showq -- show send queue */
6358092Seric # define CMDDBGDEBUG	25	/* debug -- set debug mode */
644549Seric 
654549Seric static struct cmd	CmdTab[] =
664549Seric {
674549Seric 	"mail",		CMDMAIL,
684976Seric 	"rcpt",		CMDRCPT,
694549Seric 	"data",		CMDDATA,
704549Seric 	"rset",		CMDRSET,
714549Seric 	"vrfy",		CMDVRFY,
7258092Seric 	"expn",		CMDEXPN,
734549Seric 	"help",		CMDHELP,
744549Seric 	"noop",		CMDNOOP,
754549Seric 	"quit",		CMDQUIT,
764976Seric 	"helo",		CMDHELO,
7758323Seric 	"ehlo",		CMDEHLO,
788544Seric 	"verb",		CMDVERB,
799314Seric 	"onex",		CMDONEX,
8036230Skarels 	/*
8136230Skarels 	 * remaining commands are here only
8236230Skarels 	 * to trap and log attempts to use them
8336230Skarels 	 */
849339Seric 	"showq",	CMDDBGQSHOW,
858544Seric 	"debug",	CMDDBGDEBUG,
8664685Seric 	"wiz",		CMDLOGBOGUS,
874549Seric 	NULL,		CMDERROR,
884549Seric };
894549Seric 
909378Seric bool	OneXact = FALSE;		/* one xaction only this run */
9165017Seric char	*CurSmtpClient;			/* who's at the other end of channel */
9211146Seric 
9363937Seric static char	*skipword();
9466325Seric extern char	RealUserName[];
9563937Seric 
9666325Seric 
9766283Seric #define MAXBADCOMMANDS	25		/* maximum number of bad commands */
9866283Seric 
9955012Seric smtp(e)
10055012Seric 	register ENVELOPE *e;
1014549Seric {
1024549Seric 	register char *p;
1038544Seric 	register struct cmd *c;
1044549Seric 	char *cmd;
1055003Seric 	auto ADDRESS *vrfyqueue;
10612612Seric 	ADDRESS *a;
10758109Seric 	bool gotmail;			/* mail command received */
10858092Seric 	bool gothello;			/* helo command received */
10958092Seric 	bool vrfy;			/* set if this is a vrfy command */
11058323Seric 	char *protocol;			/* sending protocol */
11159016Seric 	char *sendinghost;		/* sending hostname */
11258333Seric 	long msize;			/* approximate maximum message size */
11366005Seric 	char *peerhostname;		/* name of SMTP peer or "localhost" */
11458333Seric 	auto char *delimptr;
11558714Seric 	char *id;
11659747Seric 	int nrcpts;			/* number of RCPT commands */
11763787Seric 	bool doublequeue;
11866283Seric 	int badcommands = 0;		/* count of bad commands */
1198544Seric 	char inp[MAXLINE];
12057232Seric 	char cmdbuf[MAXLINE];
1217124Seric 	extern char Version[];
12224943Seric 	extern ENVELOPE BlankEnvelope;
1234549Seric 
12459066Seric 	if (fileno(OutChannel) != fileno(stdout))
1257363Seric 	{
1267363Seric 		/* arrange for debugging output to go to remote host */
12759066Seric 		(void) dup2(fileno(OutChannel), fileno(stdout));
1287363Seric 	}
12955012Seric 	settime(e);
13066005Seric 	peerhostname = RealHostName;
13166005Seric 	if (peerhostname == NULL)
13266005Seric 		peerhostname = "localhost";
13366005Seric 	CurHostName = peerhostname;
13465017Seric 	CurSmtpClient = macvalue('_', e);
13565017Seric 	if (CurSmtpClient == NULL)
13666003Seric 		CurSmtpClient = CurHostName;
13765017Seric 
13865017Seric 	setproctitle("server %s startup", CurSmtpClient);
13958050Seric 	expand("\201e", inp, &inp[sizeof inp], e);
140*66745Seric 	p = strchr(inp, '\n');
141*66745Seric 	if (p != NULL)
142*66745Seric 		*p++ = '\0';
14364496Seric 	if (BrokenSmtpPeers)
14464496Seric 	{
14564496Seric 		message("220 %s", inp);
14664496Seric 	}
14764496Seric 	else
14864496Seric 	{
149*66745Seric 		char *q = inp;
150*66745Seric 
151*66745Seric 		while (q != NULL)
152*66745Seric 		{
153*66745Seric 			message("220-%s", q);
154*66745Seric 			q = p;
155*66745Seric 			p = strchr(p, '\n');
156*66745Seric 			if (p != NULL)
157*66745Seric 				*p++ = '\0';
158*66745Seric 		}
15964496Seric 		message("220 ESMTP spoken here");
16064496Seric 	}
16158330Seric 	protocol = NULL;
16259016Seric 	sendinghost = macvalue('s', e);
16358082Seric 	gothello = FALSE;
16458330Seric 	gotmail = FALSE;
1654549Seric 	for (;;)
1664549Seric 	{
16712612Seric 		/* arrange for backout */
16865751Seric 		if (setjmp(TopFrame) > 0)
16959058Seric 		{
17065751Seric 			/* if() nesting is necessary for Cray UNICOS */
17165751Seric 			if (InChild)
17265751Seric 			{
17365751Seric 				QuickAbort = FALSE;
17465751Seric 				SuprErrs = TRUE;
17565751Seric 				finis();
17665751Seric 			}
17759058Seric 		}
17812612Seric 		QuickAbort = FALSE;
17912612Seric 		HoldErrs = FALSE;
18051951Seric 		LogUsrErrs = FALSE;
18163843Seric 		e->e_flags &= ~(EF_VRFYONLY|EF_GLOBALERRS);
18212612Seric 
1837356Seric 		/* setup for the read */
18455012Seric 		e->e_to = NULL;
1854577Seric 		Errors = 0;
1867275Seric 		(void) fflush(stdout);
1877356Seric 
1887356Seric 		/* read the input line */
18961093Seric 		SmtpPhase = "server cmd read";
19061093Seric 		setproctitle("server %s cmd read", CurHostName);
19161093Seric 		p = sfgets(inp, sizeof inp, InChannel, TimeOuts.to_nextcommand,
19261093Seric 				SmtpPhase);
1937356Seric 
1947685Seric 		/* handle errors */
1957356Seric 		if (p == NULL)
1967356Seric 		{
1974549Seric 			/* end of file, just die */
19866017Seric 			disconnect(1, e);
19958151Seric 			message("421 %s Lost input channel from %s",
20065017Seric 				MyHostName, CurSmtpClient);
20155464Seric #ifdef LOG
20263843Seric 			if (LogLevel > (gotmail ? 1 : 19))
20355464Seric 				syslog(LOG_NOTICE, "lost input channel from %s",
20465017Seric 					CurSmtpClient);
20555464Seric #endif
20658069Seric 			if (InChild)
20758069Seric 				ExitStat = EX_QUIT;
2084549Seric 			finis();
2094549Seric 		}
2104549Seric 
2114549Seric 		/* clean up end of line */
2124558Seric 		fixcrlf(inp, TRUE);
2134549Seric 
2144713Seric 		/* echo command to transcript */
21555012Seric 		if (e->e_xfp != NULL)
21655012Seric 			fprintf(e->e_xfp, "<<< %s\n", inp);
2174713Seric 
21859060Seric 		if (e->e_id == NULL)
21965058Seric 			setproctitle("%s: %.80s", CurSmtpClient, inp);
22059060Seric 		else
22165058Seric 			setproctitle("%s %s: %.80s", e->e_id, CurSmtpClient, inp);
22259060Seric 
2234549Seric 		/* break off command */
22458050Seric 		for (p = inp; isascii(*p) && isspace(*p); p++)
2254549Seric 			continue;
22657232Seric 		cmd = cmdbuf;
22758050Seric 		while (*p != '\0' &&
22858050Seric 		       !(isascii(*p) && isspace(*p)) &&
22958050Seric 		       cmd < &cmdbuf[sizeof cmdbuf - 2])
23024981Seric 			*cmd++ = *p++;
23124981Seric 		*cmd = '\0';
2324549Seric 
23325691Seric 		/* throw away leading whitespace */
23458050Seric 		while (isascii(*p) && isspace(*p))
23525691Seric 			p++;
23625691Seric 
2374549Seric 		/* decode command */
2384549Seric 		for (c = CmdTab; c->cmdname != NULL; c++)
2394549Seric 		{
24033725Sbostic 			if (!strcasecmp(c->cmdname, cmdbuf))
2414549Seric 				break;
2424549Seric 		}
2434549Seric 
24451954Seric 		/* reset errors */
24551954Seric 		errno = 0;
24651954Seric 
2474549Seric 		/* process command */
2484549Seric 		switch (c->cmdcode)
2494549Seric 		{
2504976Seric 		  case CMDHELO:		/* hello -- introduce yourself */
25158323Seric 		  case CMDEHLO:		/* extended hello */
25258323Seric 			if (c->cmdcode == CMDEHLO)
25358323Seric 			{
25458323Seric 				protocol = "ESMTP";
25561093Seric 				SmtpPhase = "server EHLO";
25658323Seric 			}
25758323Seric 			else
25858323Seric 			{
25958323Seric 				protocol = "SMTP";
26061093Seric 				SmtpPhase = "server HELO";
26158323Seric 			}
26259016Seric 			sendinghost = newstr(p);
26360210Seric 			gothello = TRUE;
26460210Seric 			if (c->cmdcode != CMDEHLO)
26560239Seric 			{
26660239Seric 				/* print old message and be done with it */
26760239Seric 				message("250 %s Hello %s, pleased to meet you",
26865017Seric 					MyHostName, CurSmtpClient);
26960210Seric 				break;
27060239Seric 			}
27160239Seric 
27260239Seric 			/* print extended message and brag */
27360239Seric 			message("250-%s Hello %s, pleased to meet you",
27460239Seric 				MyHostName, p);
27558323Seric 			if (!bitset(PRIV_NOEXPN, PrivacyFlags))
27658323Seric 				message("250-EXPN");
27764359Seric 			if (MaxMessageSize > 0)
27864359Seric 				message("250-SIZE %ld", MaxMessageSize);
27959271Seric 			else
28059271Seric 				message("250-SIZE");
28158323Seric 			message("250 HELP");
2824976Seric 			break;
2834976Seric 
2844549Seric 		  case CMDMAIL:		/* mail -- designate sender */
28561093Seric 			SmtpPhase = "server MAIL";
28624943Seric 
2879314Seric 			/* check for validity of this command */
28858789Seric 			if (!gothello)
28958082Seric 			{
29058957Seric 				/* set sending host to our known value */
29159016Seric 				if (sendinghost == NULL)
29266005Seric 					sendinghost = peerhostname;
29358957Seric 
29458789Seric 				if (bitset(PRIV_NEEDMAILHELO, PrivacyFlags))
29558821Seric 				{
29658789Seric 					message("503 Polite people say HELO first");
29758821Seric 					break;
29858821Seric 				}
29958082Seric 			}
30058109Seric 			if (gotmail)
3014558Seric 			{
30258151Seric 				message("503 Sender already specified");
30363843Seric 				if (InChild)
30463843Seric 					finis();
3054558Seric 				break;
3064558Seric 			}
3079339Seric 			if (InChild)
3089339Seric 			{
30936230Skarels 				errno = 0;
31058151Seric 				syserr("503 Nested MAIL command: MAIL %s", p);
31158069Seric 				finis();
3129339Seric 			}
3139339Seric 
3149339Seric 			/* fork a subprocess to process this command */
31555012Seric 			if (runinchild("SMTP-MAIL", e) > 0)
3169339Seric 				break;
31763753Seric 			if (!gothello)
31863753Seric 			{
31963753Seric 				auth_warning(e,
32066005Seric 					"Host %s didn't use HELO protocol",
32166005Seric 					peerhostname);
32263753Seric 			}
32365947Seric #ifdef PICKY_HELO_CHECK
32466005Seric 			if (strcasecmp(sendinghost, peerhostname) != 0 &&
32566005Seric 			    (strcasecmp(peerhostname, "localhost") != 0 ||
32665823Seric 			     strcasecmp(sendinghost, MyHostName) != 0))
32765823Seric 			{
32865823Seric 				auth_warning(e, "Host %s claimed to be %s",
32966005Seric 					peerhostname, sendinghost);
33065823Seric 			}
33165947Seric #endif
33265823Seric 
33358323Seric 			if (protocol == NULL)
33458323Seric 				protocol = "SMTP";
33558323Seric 			define('r', protocol, e);
33659016Seric 			define('s', sendinghost, e);
33755012Seric 			initsys(e);
33859747Seric 			nrcpts = 0;
33965089Seric 			e->e_flags |= EF_LOGSENDER;
34065058Seric 			setproctitle("%s %s: %.80s", e->e_id, CurSmtpClient, inp);
3419339Seric 
3429339Seric 			/* child -- go do the processing */
3434549Seric 			p = skipword(p, "from");
3444549Seric 			if (p == NULL)
3454549Seric 				break;
34657977Seric 			if (setjmp(TopFrame) > 0)
34758147Seric 			{
34858147Seric 				/* this failed -- undo work */
34958147Seric 				if (InChild)
35059058Seric 				{
35159058Seric 					QuickAbort = FALSE;
35259058Seric 					SuprErrs = TRUE;
35363787Seric 					e->e_flags &= ~EF_FATALERRS;
35458147Seric 					finis();
35559058Seric 				}
35657977Seric 				break;
35758147Seric 			}
35857977Seric 			QuickAbort = TRUE;
35958333Seric 
36058333Seric 			/* must parse sender first */
36158333Seric 			delimptr = NULL;
36258704Seric 			setsender(p, e, &delimptr, FALSE);
36358333Seric 			p = delimptr;
36458333Seric 			if (p != NULL && *p != '\0')
36558333Seric 				*p++ = '\0';
36658333Seric 
36766325Seric 			/* check for possible spoofing */
36866325Seric 			if (RealUid != 0 && OpMode == MD_SMTP &&
36966325Seric 			    (e->e_from.q_mailer != LocalMailer &&
37066325Seric 			     strcmp(e->e_from.q_user, RealUserName) != 0))
37166325Seric 			{
37266325Seric 				auth_warning(e, "%s owned process doing -bs",
37366325Seric 					RealUserName);
37466325Seric 			}
37566325Seric 
37658333Seric 			/* now parse ESMTP arguments */
37758333Seric 			msize = 0;
37858333Seric 			for (; p != NULL && *p != '\0'; p++)
37958333Seric 			{
38058333Seric 				char *kp;
38166304Seric 				char *vp = NULL;
38258333Seric 
38358333Seric 				/* locate the beginning of the keyword */
38458333Seric 				while (isascii(*p) && isspace(*p))
38558333Seric 					p++;
38658333Seric 				if (*p == '\0')
38758333Seric 					break;
38858333Seric 				kp = p;
38958333Seric 
39058333Seric 				/* skip to the value portion */
39158333Seric 				while (isascii(*p) && isalnum(*p) || *p == '-')
39258333Seric 					p++;
39358333Seric 				if (*p == '=')
39458333Seric 				{
39558333Seric 					*p++ = '\0';
39658333Seric 					vp = p;
39758333Seric 
39858333Seric 					/* skip to the end of the value */
39958333Seric 					while (*p != '\0' && *p != ' ' &&
40058333Seric 					       !(isascii(*p) && iscntrl(*p)) &&
40158333Seric 					       *p != '=')
40258333Seric 						p++;
40358333Seric 				}
40458333Seric 
40558333Seric 				if (*p != '\0')
40658333Seric 					*p++ = '\0';
40758333Seric 
40858333Seric 				if (tTd(19, 1))
40958333Seric 					printf("MAIL: got arg %s=%s\n", kp,
41058333Seric 						vp == NULL ? "<null>" : vp);
41158333Seric 
41258333Seric 				if (strcasecmp(kp, "size") == 0)
41358333Seric 				{
41459093Seric 					if (vp == NULL)
41558333Seric 					{
41658333Seric 						usrerr("501 SIZE requires a value");
41758333Seric 						/* NOTREACHED */
41858333Seric 					}
41958333Seric 					msize = atol(vp);
42058333Seric 				}
42159093Seric 				else if (strcasecmp(kp, "body") == 0)
42259093Seric 				{
42359093Seric 					if (vp == NULL)
42459093Seric 					{
42559093Seric 						usrerr("501 BODY requires a value");
42659093Seric 						/* NOTREACHED */
42759093Seric 					}
42859093Seric # ifdef MIME
42959093Seric 					if (strcasecmp(vp, "8bitmime") == 0)
43059093Seric 					{
43159093Seric 						e->e_bodytype = "8BITMIME";
43259709Seric 						SevenBit = FALSE;
43359093Seric 					}
43459093Seric 					else if (strcasecmp(vp, "7bit") == 0)
43559093Seric 					{
43659093Seric 						e->e_bodytype = "7BIT";
43759709Seric 						SevenBit = TRUE;
43859093Seric 					}
43959093Seric 					else
44059093Seric 					{
44159093Seric 						usrerr("501 Unknown BODY type %s",
44259093Seric 							vp);
44359093Seric 					}
44459093Seric # endif
44559093Seric 				}
44658333Seric 				else
44758333Seric 				{
44858333Seric 					usrerr("501 %s parameter unrecognized", kp);
44958333Seric 					/* NOTREACHED */
45058333Seric 				}
45158333Seric 			}
45259284Seric 
45359284Seric 			if (MaxMessageSize > 0 && msize > MaxMessageSize)
45459284Seric 			{
45559284Seric 				usrerr("552 Message size exceeds fixed maximum message size (%ld)",
45659284Seric 					MaxMessageSize);
45759284Seric 				/* NOTREACHED */
45859284Seric 			}
45958333Seric 
46058333Seric 			if (!enoughspace(msize))
46158333Seric 			{
46258333Seric 				message("452 Insufficient disk space; try again later");
46358333Seric 				break;
46458333Seric 			}
46558151Seric 			message("250 Sender ok");
46658147Seric 			gotmail = TRUE;
4674549Seric 			break;
4684549Seric 
4694976Seric 		  case CMDRCPT:		/* rcpt -- designate recipient */
47058850Seric 			if (!gotmail)
47158850Seric 			{
47258850Seric 				usrerr("503 Need MAIL before RCPT");
47358850Seric 				break;
47458850Seric 			}
47561093Seric 			SmtpPhase = "server RCPT";
47612612Seric 			if (setjmp(TopFrame) > 0)
47714785Seric 			{
47855012Seric 				e->e_flags &= ~EF_FATALERRS;
47912612Seric 				break;
48014785Seric 			}
48112612Seric 			QuickAbort = TRUE;
48251951Seric 			LogUsrErrs = TRUE;
48358093Seric 
48459699Seric 			if (e->e_sendmode != SM_DELIVER)
48559699Seric 				e->e_flags |= EF_VRFYONLY;
48658919Seric 
4874549Seric 			p = skipword(p, "to");
4884549Seric 			if (p == NULL)
4894549Seric 				break;
49064284Seric 			a = parseaddr(p, NULLADDR, RF_COPYALL, ' ', NULL, e);
49112612Seric 			if (a == NULL)
49212612Seric 				break;
49316886Seric 			a->q_flags |= QPRIMARY;
49455012Seric 			a = recipient(a, &e->e_sendqueue, e);
49512612Seric 			if (Errors != 0)
49612612Seric 				break;
49712612Seric 
49812612Seric 			/* no errors during parsing, but might be a duplicate */
49955012Seric 			e->e_to = p;
50012612Seric 			if (!bitset(QBADADDR, a->q_flags))
50159747Seric 			{
50264718Seric 				message("250 Recipient ok%s",
50364718Seric 					bitset(QQUEUEUP, a->q_flags) ?
50464718Seric 						" (will queue)" : "");
50559747Seric 				nrcpts++;
50659747Seric 			}
50712612Seric 			else
5084549Seric 			{
50912612Seric 				/* punt -- should keep message in ADDRESS.... */
51058151Seric 				message("550 Addressee unknown");
5114549Seric 			}
51255012Seric 			e->e_to = NULL;
5134549Seric 			break;
5144549Seric 
5154549Seric 		  case CMDDATA:		/* data -- text of mail */
51661093Seric 			SmtpPhase = "server DATA";
51758109Seric 			if (!gotmail)
5184549Seric 			{
51958151Seric 				message("503 Need MAIL command");
5204976Seric 				break;
5214549Seric 			}
52264718Seric 			else if (nrcpts <= 0)
5234549Seric 			{
52458151Seric 				message("503 Need RCPT (recipient)");
5254976Seric 				break;
5264549Seric 			}
5274976Seric 
52858929Seric 			/* check to see if we need to re-expand aliases */
52963787Seric 			/* also reset QBADADDR on already-diagnosted addrs */
53063787Seric 			doublequeue = FALSE;
53158929Seric 			for (a = e->e_sendqueue; a != NULL; a = a->q_next)
53258929Seric 			{
53358929Seric 				if (bitset(QVERIFIED, a->q_flags))
53463787Seric 				{
53563787Seric 					/* need to re-expand aliases */
53663787Seric 					doublequeue = TRUE;
53763787Seric 				}
53863787Seric 				if (bitset(QBADADDR, a->q_flags))
53963787Seric 				{
54063787Seric 					/* make this "go away" */
54163787Seric 					a->q_flags |= QDONTSEND;
54263787Seric 					a->q_flags &= ~QBADADDR;
54363787Seric 				}
54458929Seric 			}
54558929Seric 
5464976Seric 			/* collect the text of the message */
54724943Seric 			SmtpPhase = "collect";
54864766Seric 			collect(TRUE, doublequeue, e);
54964766Seric 			if (Errors != 0)
55064766Seric 				goto abortmessage;
55163965Seric 			HoldErrs = TRUE;
5524976Seric 
5538238Seric 			/*
5548238Seric 			**  Arrange to send to everyone.
5558238Seric 			**	If sending to multiple people, mail back
5568238Seric 			**		errors rather than reporting directly.
5578238Seric 			**	In any case, don't mail back errors for
5588238Seric 			**		anything that has happened up to
5598238Seric 			**		now (the other end will do this).
56010197Seric 			**	Truncate our transcript -- the mail has gotten
56110197Seric 			**		to us successfully, and if we have
56210197Seric 			**		to mail this back, it will be easier
56310197Seric 			**		on the reader.
5648238Seric 			**	Then send to everyone.
5658238Seric 			**	Finally give a reply code.  If an error has
5668238Seric 			**		already been given, don't mail a
5678238Seric 			**		message back.
5689339Seric 			**	We goose error returns by clearing error bit.
5698238Seric 			*/
5708238Seric 
57124943Seric 			SmtpPhase = "delivery";
57263787Seric 			if (nrcpts != 1 && !doublequeue)
5739378Seric 			{
5749378Seric 				HoldErrs = TRUE;
57558734Seric 				e->e_errormode = EM_MAIL;
5769378Seric 			}
57755012Seric 			e->e_xfp = freopen(queuename(e, 'x'), "w", e->e_xfp);
57858714Seric 			id = e->e_id;
5794976Seric 
5804976Seric 			/* send to all recipients */
58163787Seric 			sendall(e, doublequeue ? SM_QUEUE : SM_DEFAULT);
58255012Seric 			e->e_to = NULL;
5834976Seric 
5848238Seric 			/* issue success if appropriate and reset */
5858238Seric 			if (Errors == 0 || HoldErrs)
58658855Seric 				message("250 %s Message accepted for delivery", id);
58759747Seric 
58859747Seric 			if (bitset(EF_FATALERRS, e->e_flags) && !HoldErrs)
58959730Seric 			{
59059730Seric 				/* avoid sending back an extra message */
59159730Seric 				e->e_flags &= ~EF_FATALERRS;
59259747Seric 				e->e_flags |= EF_CLRQUEUE;
59359730Seric 			}
5948238Seric 			else
59558919Seric 			{
59659747Seric 				/* from now on, we have to operate silently */
59759747Seric 				HoldErrs = TRUE;
59859747Seric 				e->e_errormode = EM_MAIL;
59959747Seric 
60059730Seric 				/* if we just queued, poke it */
60163787Seric 				if (doublequeue && e->e_sendmode != SM_QUEUE)
60259730Seric 				{
60364296Seric 					extern pid_t dowork();
60464296Seric 
60559730Seric 					unlockqueue(e);
60664296Seric 					(void) dowork(id, TRUE, TRUE, e);
60759730Seric 				}
60858919Seric 			}
60958883Seric 
61059747Seric   abortmessage:
6119339Seric 			/* if in a child, pop back to our parent */
6129339Seric 			if (InChild)
6139339Seric 				finis();
61424943Seric 
61524943Seric 			/* clean up a bit */
61658109Seric 			gotmail = FALSE;
61755012Seric 			dropenvelope(e);
61858179Seric 			CurEnv = e = newenvelope(e, CurEnv);
61955012Seric 			e->e_flags = BlankEnvelope.e_flags;
6204549Seric 			break;
6214549Seric 
6224549Seric 		  case CMDRSET:		/* rset -- reset state */
62358151Seric 			message("250 Reset state");
62464359Seric 			e->e_flags |= EF_CLRQUEUE;
6259339Seric 			if (InChild)
6269339Seric 				finis();
62758109Seric 
62858109Seric 			/* clean up a bit */
62958109Seric 			gotmail = FALSE;
63058109Seric 			dropenvelope(e);
63158179Seric 			CurEnv = e = newenvelope(e, CurEnv);
6329339Seric 			break;
6334549Seric 
6344549Seric 		  case CMDVRFY:		/* vrfy -- verify address */
63558092Seric 		  case CMDEXPN:		/* expn -- expand address */
63658092Seric 			vrfy = c->cmdcode == CMDVRFY;
63758092Seric 			if (bitset(vrfy ? PRIV_NOVRFY : PRIV_NOEXPN,
63858092Seric 						PrivacyFlags))
63958082Seric 			{
64058412Seric 				if (vrfy)
64158412Seric 					message("252 Who's to say?");
64258412Seric 				else
64365192Seric 					message("502 Sorry, we do not allow this operation");
64465017Seric #ifdef LOG
64565017Seric 				if (LogLevel > 5)
64665017Seric 					syslog(LOG_INFO, "%s: %s [rejected]",
64765017Seric 						CurSmtpClient, inp);
64865017Seric #endif
64958082Seric 				break;
65058082Seric 			}
65158082Seric 			else if (!gothello &&
65258092Seric 				 bitset(vrfy ? PRIV_NEEDVRFYHELO : PRIV_NEEDEXPNHELO,
65358092Seric 						PrivacyFlags))
65458082Seric 			{
65558151Seric 				message("503 I demand that you introduce yourself first");
65658082Seric 				break;
65758082Seric 			}
65858092Seric 			if (runinchild(vrfy ? "SMTP-VRFY" : "SMTP-EXPN", e) > 0)
6599339Seric 				break;
66055173Seric #ifdef LOG
66158020Seric 			if (LogLevel > 5)
66265017Seric 				syslog(LOG_INFO, "%s: %s", CurSmtpClient, inp);
66355173Seric #endif
6645003Seric 			vrfyqueue = NULL;
6657762Seric 			QuickAbort = TRUE;
66658092Seric 			if (vrfy)
66758092Seric 				e->e_flags |= EF_VRFYONLY;
66862373Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
66962373Seric 				*p++;
67062373Seric 			if (*p == '\0')
67162373Seric 			{
67262373Seric 				message("501 Argument required");
67362373Seric 				Errors++;
67462373Seric 			}
67562373Seric 			else
67662373Seric 			{
67764284Seric 				(void) sendtolist(p, NULLADDR, &vrfyqueue, e);
67862373Seric 			}
6797762Seric 			if (Errors != 0)
6809339Seric 			{
6819339Seric 				if (InChild)
6829339Seric 					finis();
6837762Seric 				break;
6849339Seric 			}
68562373Seric 			if (vrfyqueue == NULL)
68662373Seric 			{
68762373Seric 				message("554 Nothing to %s", vrfy ? "VRFY" : "EXPN");
68862373Seric 			}
6895003Seric 			while (vrfyqueue != NULL)
6905003Seric 			{
69163971Seric 				a = vrfyqueue;
69263971Seric 				while ((a = a->q_next) != NULL &&
69363971Seric 				       bitset(QDONTSEND|QBADADDR, a->q_flags))
69463971Seric 					continue;
6957685Seric 				if (!bitset(QDONTSEND|QBADADDR, vrfyqueue->q_flags))
69658151Seric 					printvrfyaddr(vrfyqueue, a == NULL);
69763847Seric 				vrfyqueue = vrfyqueue->q_next;
6985003Seric 			}
6999339Seric 			if (InChild)
7009339Seric 				finis();
7014549Seric 			break;
7024549Seric 
7034549Seric 		  case CMDHELP:		/* help -- give user info */
7044577Seric 			help(p);
7054549Seric 			break;
7064549Seric 
7074549Seric 		  case CMDNOOP:		/* noop -- do nothing */
70864122Seric 			message("250 OK");
7094549Seric 			break;
7104549Seric 
7114549Seric 		  case CMDQUIT:		/* quit -- leave mail */
71258151Seric 			message("221 %s closing connection", MyHostName);
71361051Seric 
71466283Seric doquit:
71561051Seric 			/* avoid future 050 messages */
71666017Seric 			disconnect(1, e);
71761051Seric 
7189339Seric 			if (InChild)
7199339Seric 				ExitStat = EX_QUIT;
7204549Seric 			finis();
7214549Seric 
7228544Seric 		  case CMDVERB:		/* set verbose mode */
72359957Seric 			if (bitset(PRIV_NOEXPN, PrivacyFlags))
72459957Seric 			{
72559957Seric 				/* this would give out the same info */
72659957Seric 				message("502 Verbose unavailable");
72759957Seric 				break;
72859957Seric 			}
7298544Seric 			Verbose = TRUE;
73058734Seric 			e->e_sendmode = SM_DELIVER;
73159957Seric 			message("250 Verbose mode");
7328544Seric 			break;
7338544Seric 
7349314Seric 		  case CMDONEX:		/* doing one transaction only */
7359378Seric 			OneXact = TRUE;
73659957Seric 			message("250 Only one transaction");
7379314Seric 			break;
7389314Seric 
73936230Skarels # ifdef SMTPDEBUG
7409339Seric 		  case CMDDBGQSHOW:	/* show queues */
7416907Seric 			printf("Send Queue=");
74255012Seric 			printaddr(e->e_sendqueue, TRUE);
7435003Seric 			break;
7447275Seric 
7457275Seric 		  case CMDDBGDEBUG:	/* set debug mode */
7467676Seric 			tTsetup(tTdvect, sizeof tTdvect, "0-99.1");
7477676Seric 			tTflag(p);
74858151Seric 			message("200 Debug set");
7497275Seric 			break;
7507275Seric 
75136230Skarels # else /* not SMTPDEBUG */
75236230Skarels 		  case CMDDBGQSHOW:	/* show queues */
75336230Skarels 		  case CMDDBGDEBUG:	/* set debug mode */
75464685Seric # endif /* SMTPDEBUG */
75564685Seric 		  case CMDLOGBOGUS:	/* bogus command */
75636233Skarels # ifdef LOG
75758308Seric 			if (LogLevel > 0)
75864685Seric 				syslog(LOG_CRIT,
75958020Seric 				    "\"%s\" command from %s (%s)",
76066005Seric 				    c->cmdname, peerhostname,
76158755Seric 				    anynet_ntoa(&RealHostAddr));
76236233Skarels # endif
76336230Skarels 			/* FALL THROUGH */
76436230Skarels 
7654549Seric 		  case CMDERROR:	/* unknown command */
76666283Seric 			if (++badcommands > MAXBADCOMMANDS)
76766283Seric 			{
76866283Seric 				message("421 %s Too many bad commands; closing connection",
76966283Seric 					MyHostName);
77066283Seric 				goto doquit;
77166283Seric 			}
77266283Seric 
77358151Seric 			message("500 Command unrecognized");
7744549Seric 			break;
7754549Seric 
7764549Seric 		  default:
77736230Skarels 			errno = 0;
77858151Seric 			syserr("500 smtp: unknown code %d", c->cmdcode);
7794549Seric 			break;
7804549Seric 		}
7814549Seric 	}
7824549Seric }
7834549Seric /*
7844549Seric **  SKIPWORD -- skip a fixed word.
7854549Seric **
7864549Seric **	Parameters:
7874549Seric **		p -- place to start looking.
7884549Seric **		w -- word to skip.
7894549Seric **
7904549Seric **	Returns:
7914549Seric **		p following w.
7924549Seric **		NULL on error.
7934549Seric **
7944549Seric **	Side Effects:
7954549Seric **		clobbers the p data area.
7964549Seric */
7974549Seric 
7984549Seric static char *
7994549Seric skipword(p, w)
8004549Seric 	register char *p;
8014549Seric 	char *w;
8024549Seric {
8034549Seric 	register char *q;
80466005Seric 	char *firstp = p;
8054549Seric 
8064549Seric 	/* find beginning of word */
80758050Seric 	while (isascii(*p) && isspace(*p))
8084549Seric 		p++;
8094549Seric 	q = p;
8104549Seric 
8114549Seric 	/* find end of word */
81258050Seric 	while (*p != '\0' && *p != ':' && !(isascii(*p) && isspace(*p)))
8134549Seric 		p++;
81458050Seric 	while (isascii(*p) && isspace(*p))
8154549Seric 		*p++ = '\0';
8164549Seric 	if (*p != ':')
8174549Seric 	{
8184549Seric 	  syntax:
81966005Seric 		message("501 Syntax error in parameters scanning \"%s\"",
82066005Seric 			firstp);
8214549Seric 		Errors++;
8224549Seric 		return (NULL);
8234549Seric 	}
8244549Seric 	*p++ = '\0';
82558050Seric 	while (isascii(*p) && isspace(*p))
8264549Seric 		p++;
8274549Seric 
82862373Seric 	if (*p == '\0')
82962373Seric 		goto syntax;
83062373Seric 
8314549Seric 	/* see if the input word matches desired word */
83233725Sbostic 	if (strcasecmp(q, w))
8334549Seric 		goto syntax;
8344549Seric 
8354549Seric 	return (p);
8364549Seric }
8374577Seric /*
83858151Seric **  PRINTVRFYADDR -- print an entry in the verify queue
83958151Seric **
84058151Seric **	Parameters:
84158151Seric **		a -- the address to print
84258151Seric **		last -- set if this is the last one.
84358151Seric **
84458151Seric **	Returns:
84558151Seric **		none.
84658151Seric **
84758151Seric **	Side Effects:
84858151Seric **		Prints the appropriate 250 codes.
84958151Seric */
85058151Seric 
85158151Seric printvrfyaddr(a, last)
85258151Seric 	register ADDRESS *a;
85358151Seric 	bool last;
85458151Seric {
85558151Seric 	char fmtbuf[20];
85658151Seric 
85758151Seric 	strcpy(fmtbuf, "250");
85858151Seric 	fmtbuf[3] = last ? ' ' : '-';
85958151Seric 
86059746Seric 	if (a->q_fullname == NULL)
86159746Seric 	{
86259746Seric 		if (strchr(a->q_user, '@') == NULL)
86359746Seric 			strcpy(&fmtbuf[4], "<%s@%s>");
86459746Seric 		else
86559746Seric 			strcpy(&fmtbuf[4], "<%s>");
86659746Seric 		message(fmtbuf, a->q_user, MyHostName);
86759746Seric 	}
86858151Seric 	else
86958151Seric 	{
87059746Seric 		if (strchr(a->q_user, '@') == NULL)
87159746Seric 			strcpy(&fmtbuf[4], "%s <%s@%s>");
87259746Seric 		else
87359746Seric 			strcpy(&fmtbuf[4], "%s <%s>");
87459746Seric 		message(fmtbuf, a->q_fullname, a->q_user, MyHostName);
87558151Seric 	}
87658151Seric }
87758151Seric /*
8784577Seric **  HELP -- implement the HELP command.
8794577Seric **
8804577Seric **	Parameters:
8814577Seric **		topic -- the topic we want help for.
8824577Seric **
8834577Seric **	Returns:
8844577Seric **		none.
8854577Seric **
8864577Seric **	Side Effects:
8874577Seric **		outputs the help file to message output.
8884577Seric */
8894577Seric 
8904577Seric help(topic)
8914577Seric 	char *topic;
8924577Seric {
8934577Seric 	register FILE *hf;
8944577Seric 	int len;
8954577Seric 	char buf[MAXLINE];
8964577Seric 	bool noinfo;
8974577Seric 
8988269Seric 	if (HelpFile == NULL || (hf = fopen(HelpFile, "r")) == NULL)
8994577Seric 	{
9004577Seric 		/* no help */
90111931Seric 		errno = 0;
90258151Seric 		message("502 HELP not implemented");
9034577Seric 		return;
9044577Seric 	}
9054577Seric 
90649669Seric 	if (topic == NULL || *topic == '\0')
90749669Seric 		topic = "smtp";
90849669Seric 	else
90949669Seric 		makelower(topic);
91049669Seric 
9114577Seric 	len = strlen(topic);
9124577Seric 	noinfo = TRUE;
9134577Seric 
9144577Seric 	while (fgets(buf, sizeof buf, hf) != NULL)
9154577Seric 	{
9164577Seric 		if (strncmp(buf, topic, len) == 0)
9174577Seric 		{
9184577Seric 			register char *p;
9194577Seric 
92056795Seric 			p = strchr(buf, '\t');
9214577Seric 			if (p == NULL)
9224577Seric 				p = buf;
9234577Seric 			else
9244577Seric 				p++;
9254577Seric 			fixcrlf(p, TRUE);
92658151Seric 			message("214-%s", p);
9274577Seric 			noinfo = FALSE;
9284577Seric 		}
9294577Seric 	}
9304577Seric 
9314577Seric 	if (noinfo)
93258151Seric 		message("504 HELP topic unknown");
9334577Seric 	else
93458151Seric 		message("214 End of HELP info");
9354628Seric 	(void) fclose(hf);
9364577Seric }
9378544Seric /*
9389339Seric **  RUNINCHILD -- return twice -- once in the child, then in the parent again
9399339Seric **
9409339Seric **	Parameters:
9419339Seric **		label -- a string used in error messages
9429339Seric **
9439339Seric **	Returns:
9449339Seric **		zero in the child
9459339Seric **		one in the parent
9469339Seric **
9479339Seric **	Side Effects:
9489339Seric **		none.
9499339Seric */
9508544Seric 
95155012Seric runinchild(label, e)
9529339Seric 	char *label;
95355012Seric 	register ENVELOPE *e;
9549339Seric {
9559339Seric 	int childpid;
9569339Seric 
95716158Seric 	if (!OneXact)
9589339Seric 	{
95916158Seric 		childpid = dofork();
96016158Seric 		if (childpid < 0)
96116158Seric 		{
96216158Seric 			syserr("%s: cannot fork", label);
96316158Seric 			return (1);
96416158Seric 		}
96516158Seric 		if (childpid > 0)
96616158Seric 		{
96716158Seric 			auto int st;
9689339Seric 
96916158Seric 			/* parent -- wait for child to complete */
97061093Seric 			setproctitle("server %s child wait", CurHostName);
97116158Seric 			st = waitfor(childpid);
97216158Seric 			if (st == -1)
97316158Seric 				syserr("%s: lost child", label);
97464948Seric 			else if (!WIFEXITED(st))
97564948Seric 				syserr("%s: died on signal %d",
97664948Seric 					label, st & 0177);
9779339Seric 
97816158Seric 			/* if we exited on a QUIT command, complete the process */
97966017Seric 			if (WEXITSTATUS(st) == EX_QUIT)
98066017Seric 			{
98166017Seric 				disconnect(1, e);
98216158Seric 				finis();
98366017Seric 			}
9849339Seric 
98516158Seric 			return (1);
98616158Seric 		}
98716158Seric 		else
98816158Seric 		{
98916158Seric 			/* child */
99016158Seric 			InChild = TRUE;
99125050Seric 			QuickAbort = FALSE;
99255012Seric 			clearenvelope(e, FALSE);
99316158Seric 		}
9949339Seric 	}
99515256Seric 
99616158Seric 	/* open alias database */
99760537Seric 	initmaps(FALSE, e);
99816158Seric 
99916158Seric 	return (0);
10009339Seric }
10019339Seric 
100256795Seric # endif /* SMTP */
1003