122712Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333731Sbostic  * Copyright (c) 1988 Regents of the University of California.
433731Sbostic  * All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822712Sdist 
933731Sbostic # include "sendmail.h"
1022712Sdist 
1133731Sbostic #ifndef lint
1233731Sbostic #ifdef SMTP
13*58855Seric static char sccsid[] = "@(#)srvrsmtp.c	6.32 (Berkeley) 03/29/93 (with SMTP)";
1433731Sbostic #else
15*58855Seric static char sccsid[] = "@(#)srvrsmtp.c	6.32 (Berkeley) 03/29/93 (without SMTP)";
1633731Sbostic #endif
1733731Sbostic #endif /* not lint */
1833731Sbostic 
199339Seric # include <errno.h>
2011728Seric # include <signal.h>
214549Seric 
2233731Sbostic # ifdef SMTP
234556Seric 
244549Seric /*
254549Seric **  SMTP -- run the SMTP protocol.
264549Seric **
274549Seric **	Parameters:
284549Seric **		none.
294549Seric **
304549Seric **	Returns:
314549Seric **		never.
324549Seric **
334549Seric **	Side Effects:
344549Seric **		Reads commands from the input channel and processes
354549Seric **			them.
364549Seric */
374549Seric 
384549Seric struct cmd
394549Seric {
404549Seric 	char	*cmdname;	/* command name */
414549Seric 	int	cmdcode;	/* internal code, see below */
424549Seric };
434549Seric 
444549Seric /* values for cmdcode */
454549Seric # define CMDERROR	0	/* bad command */
464549Seric # define CMDMAIL	1	/* mail -- designate sender */
474976Seric # define CMDRCPT	2	/* rcpt -- designate recipient */
484549Seric # define CMDDATA	3	/* data -- send message text */
499339Seric # define CMDRSET	4	/* rset -- reset state */
509339Seric # define CMDVRFY	5	/* vrfy -- verify address */
5158092Seric # define CMDEXPN	6	/* expn -- expand address */
529339Seric # define CMDNOOP	7	/* noop -- do nothing */
539339Seric # define CMDQUIT	8	/* quit -- close connection and die */
549339Seric # define CMDHELO	9	/* helo -- be polite */
5558092Seric # define CMDHELP	10	/* help -- give usage info */
5658323Seric # define CMDEHLO	11	/* ehlo -- extended helo (RFC 1425) */
5758092Seric /* non-standard commands */
5858092Seric # define CMDONEX	16	/* onex -- sending one transaction only */
5958092Seric # define CMDVERB	17	/* verb -- go into verbose mode */
6036230Skarels /* debugging-only commands, only enabled if SMTPDEBUG is defined */
6158092Seric # define CMDDBGQSHOW	24	/* showq -- show send queue */
6258092Seric # define CMDDBGDEBUG	25	/* debug -- set debug mode */
634549Seric 
644549Seric static struct cmd	CmdTab[] =
654549Seric {
664549Seric 	"mail",		CMDMAIL,
674976Seric 	"rcpt",		CMDRCPT,
684549Seric 	"data",		CMDDATA,
694549Seric 	"rset",		CMDRSET,
704549Seric 	"vrfy",		CMDVRFY,
7158092Seric 	"expn",		CMDEXPN,
724549Seric 	"help",		CMDHELP,
734549Seric 	"noop",		CMDNOOP,
744549Seric 	"quit",		CMDQUIT,
754976Seric 	"helo",		CMDHELO,
7658323Seric 	"ehlo",		CMDEHLO,
778544Seric 	"verb",		CMDVERB,
789314Seric 	"onex",		CMDONEX,
7936230Skarels 	/*
8036230Skarels 	 * remaining commands are here only
8136230Skarels 	 * to trap and log attempts to use them
8236230Skarels 	 */
839339Seric 	"showq",	CMDDBGQSHOW,
848544Seric 	"debug",	CMDDBGDEBUG,
854549Seric 	NULL,		CMDERROR,
864549Seric };
874549Seric 
889339Seric bool	InChild = FALSE;		/* true if running in a subprocess */
899378Seric bool	OneXact = FALSE;		/* one xaction only this run */
9011146Seric 
919339Seric #define EX_QUIT		22		/* special code for QUIT command */
928544Seric 
9355012Seric smtp(e)
9455012Seric 	register ENVELOPE *e;
954549Seric {
964549Seric 	register char *p;
978544Seric 	register struct cmd *c;
984549Seric 	char *cmd;
9946928Sbostic 	static char *skipword();
1005003Seric 	auto ADDRESS *vrfyqueue;
10112612Seric 	ADDRESS *a;
10230448Seric 	char *sendinghost;
10358109Seric 	bool gotmail;			/* mail command received */
10458092Seric 	bool gothello;			/* helo command received */
10558092Seric 	bool vrfy;			/* set if this is a vrfy command */
10658323Seric 	char *protocol;			/* sending protocol */
10758333Seric 	long msize;			/* approximate maximum message size */
10858333Seric 	auto char *delimptr;
10958714Seric 	char *id;
1108544Seric 	char inp[MAXLINE];
11157232Seric 	char cmdbuf[MAXLINE];
11258512Seric 	char hostbuf[MAXNAME];
1137124Seric 	extern char Version[];
11411151Seric 	extern char *macvalue();
11512612Seric 	extern ADDRESS *recipient();
11624943Seric 	extern ENVELOPE BlankEnvelope;
11724943Seric 	extern ENVELOPE *newenvelope();
11858755Seric 	extern char *anynet_ntoa();
1194549Seric 
1207363Seric 	if (OutChannel != stdout)
1217363Seric 	{
1227363Seric 		/* arrange for debugging output to go to remote host */
1237363Seric 		(void) close(1);
1247363Seric 		(void) dup(fileno(OutChannel));
1257363Seric 	}
12655012Seric 	settime(e);
12757642Seric 	CurHostName = RealHostName;
12857642Seric 	setproctitle("srvrsmtp %s", CurHostName);
12958050Seric 	expand("\201e", inp, &inp[sizeof inp], e);
13058151Seric 	message("220 %s", inp);
13124943Seric 	SmtpPhase = "startup";
13230448Seric 	sendinghost = NULL;
13358330Seric 	protocol = NULL;
13458082Seric 	gothello = FALSE;
13558330Seric 	gotmail = FALSE;
1364549Seric 	for (;;)
1374549Seric 	{
13812612Seric 		/* arrange for backout */
13912612Seric 		if (setjmp(TopFrame) > 0 && InChild)
14012612Seric 			finis();
14112612Seric 		QuickAbort = FALSE;
14212612Seric 		HoldErrs = FALSE;
14351951Seric 		LogUsrErrs = FALSE;
14458092Seric 		e->e_flags &= ~EF_VRFYONLY;
14512612Seric 
1467356Seric 		/* setup for the read */
14755012Seric 		e->e_to = NULL;
1484577Seric 		Errors = 0;
1497275Seric 		(void) fflush(stdout);
1507356Seric 
1517356Seric 		/* read the input line */
15258109Seric 		p = sfgets(inp, sizeof inp, InChannel, TimeOuts.to_nextcommand);
1537356Seric 
1547685Seric 		/* handle errors */
1557356Seric 		if (p == NULL)
1567356Seric 		{
1574549Seric 			/* end of file, just die */
15858151Seric 			message("421 %s Lost input channel from %s",
15925050Seric 				MyHostName, CurHostName);
16055464Seric #ifdef LOG
16158020Seric 			if (LogLevel > 1)
16255464Seric 				syslog(LOG_NOTICE, "lost input channel from %s",
16355464Seric 					CurHostName);
16455464Seric #endif
16558069Seric 			if (InChild)
16658069Seric 				ExitStat = EX_QUIT;
1674549Seric 			finis();
1684549Seric 		}
1694549Seric 
1704549Seric 		/* clean up end of line */
1714558Seric 		fixcrlf(inp, TRUE);
1724549Seric 
1734713Seric 		/* echo command to transcript */
17455012Seric 		if (e->e_xfp != NULL)
17555012Seric 			fprintf(e->e_xfp, "<<< %s\n", inp);
1764713Seric 
1774549Seric 		/* break off command */
17858050Seric 		for (p = inp; isascii(*p) && isspace(*p); p++)
1794549Seric 			continue;
18057232Seric 		cmd = cmdbuf;
18158050Seric 		while (*p != '\0' &&
18258050Seric 		       !(isascii(*p) && isspace(*p)) &&
18358050Seric 		       cmd < &cmdbuf[sizeof cmdbuf - 2])
18424981Seric 			*cmd++ = *p++;
18524981Seric 		*cmd = '\0';
1864549Seric 
18725691Seric 		/* throw away leading whitespace */
18858050Seric 		while (isascii(*p) && isspace(*p))
18925691Seric 			p++;
19025691Seric 
1914549Seric 		/* decode command */
1924549Seric 		for (c = CmdTab; c->cmdname != NULL; c++)
1934549Seric 		{
19433725Sbostic 			if (!strcasecmp(c->cmdname, cmdbuf))
1954549Seric 				break;
1964549Seric 		}
1974549Seric 
19851954Seric 		/* reset errors */
19951954Seric 		errno = 0;
20051954Seric 
2014549Seric 		/* process command */
2024549Seric 		switch (c->cmdcode)
2034549Seric 		{
2044976Seric 		  case CMDHELO:		/* hello -- introduce yourself */
20558323Seric 		  case CMDEHLO:		/* extended hello */
20658323Seric 			if (c->cmdcode == CMDEHLO)
20758323Seric 			{
20858323Seric 				protocol = "ESMTP";
20958323Seric 				SmtpPhase = "EHLO";
21058323Seric 			}
21158323Seric 			else
21258323Seric 			{
21358323Seric 				protocol = "SMTP";
21458323Seric 				SmtpPhase = "HELO";
21558323Seric 			}
21625050Seric 			setproctitle("%s: %s", CurHostName, inp);
21758109Seric 			if (strcasecmp(p, MyHostName) == 0)
21814877Seric 			{
21936230Skarels 				/*
22058109Seric 				**  Didn't know about alias or MX,
22158109Seric 				**  or connected to an echo server
22258109Seric 				*/
22358109Seric 
22458151Seric 				message("553 %s config error: mail loops back to myself",
22547570Seric 					MyHostName);
22614877Seric 				break;
22714877Seric 			}
22858512Seric 			(void) strcpy(hostbuf, p);
22958512Seric 			(void) strcat(hostbuf, " (");
23058755Seric 			(void) strcat(hostbuf, anynet_ntoa(&RealHostAddr));
23158308Seric 			if (strcasecmp(p, RealHostName) != 0)
23211146Seric 			{
23358789Seric 				auth_warning(e, "Host %s claimed to be %s",
23458789Seric 					RealHostName, p);
23558512Seric 				(void) strcat(hostbuf, "; ");
23658512Seric 				(void) strcat(hostbuf, RealHostName);
23711146Seric 			}
23858512Seric 			(void) strcat(hostbuf, ")");
23958512Seric 			sendinghost = newstr(hostbuf);
24058323Seric 
24158323Seric 			/* send ext. message -- old systems must ignore */
24258323Seric 			message("250-%s Hello %s, pleased to meet you",
24336230Skarels 				MyHostName, sendinghost);
24458323Seric 			if (!bitset(PRIV_NOEXPN, PrivacyFlags))
24558323Seric 				message("250-EXPN");
24658338Seric 			message("250-SIZE");
24758323Seric 			message("250 HELP");
24858082Seric 			gothello = TRUE;
2494976Seric 			break;
2504976Seric 
2514549Seric 		  case CMDMAIL:		/* mail -- designate sender */
25224943Seric 			SmtpPhase = "MAIL";
25324943Seric 
25411151Seric 			/* force a sending host even if no HELO given */
25558064Seric 			if (sendinghost == NULL && macvalue('s', e) == NULL)
25630448Seric 				sendinghost = RealHostName;
25711151Seric 
2589314Seric 			/* check for validity of this command */
25958789Seric 			if (!gothello)
26058082Seric 			{
26158789Seric 				if (bitset(PRIV_NEEDMAILHELO, PrivacyFlags))
26258821Seric 				{
26358789Seric 					message("503 Polite people say HELO first");
26458821Seric 					break;
26558821Seric 				}
26658789Seric 				else
26758821Seric 				{
26858789Seric 					auth_warning(e,
26958789Seric 						"Host %s didn't use HELO protocol",
27058789Seric 						RealHostName);
27158821Seric 				}
27258082Seric 			}
27358109Seric 			if (gotmail)
2744558Seric 			{
27558151Seric 				message("503 Sender already specified");
2764558Seric 				break;
2774558Seric 			}
2789339Seric 			if (InChild)
2799339Seric 			{
28036230Skarels 				errno = 0;
28158151Seric 				syserr("503 Nested MAIL command: MAIL %s", p);
28258069Seric 				finis();
2839339Seric 			}
2849339Seric 
2859339Seric 			/* fork a subprocess to process this command */
28655012Seric 			if (runinchild("SMTP-MAIL", e) > 0)
2879339Seric 				break;
28858064Seric 			if (sendinghost != NULL)
28958064Seric 				define('s', sendinghost, e);
29058323Seric 			if (protocol == NULL)
29158323Seric 				protocol = "SMTP";
29258323Seric 			define('r', protocol, e);
29355012Seric 			initsys(e);
29457389Seric 			setproctitle("%s %s: %s", e->e_id, CurHostName, inp);
2959339Seric 
2969339Seric 			/* child -- go do the processing */
2974549Seric 			p = skipword(p, "from");
2984549Seric 			if (p == NULL)
2994549Seric 				break;
30057977Seric 			if (setjmp(TopFrame) > 0)
30158147Seric 			{
30258147Seric 				/* this failed -- undo work */
30358147Seric 				if (InChild)
30458147Seric 					finis();
30557977Seric 				break;
30658147Seric 			}
30757977Seric 			QuickAbort = TRUE;
30858333Seric 
30958333Seric 			/* must parse sender first */
31058333Seric 			delimptr = NULL;
31158704Seric 			setsender(p, e, &delimptr, FALSE);
31258333Seric 			p = delimptr;
31358333Seric 			if (p != NULL && *p != '\0')
31458333Seric 				*p++ = '\0';
31558333Seric 
31658333Seric 			/* now parse ESMTP arguments */
31758333Seric 			msize = 0;
31858333Seric 			for (; p != NULL && *p != '\0'; p++)
31958333Seric 			{
32058333Seric 				char *kp;
32158333Seric 				char *vp;
32258333Seric 
32358333Seric 				/* locate the beginning of the keyword */
32458333Seric 				while (isascii(*p) && isspace(*p))
32558333Seric 					p++;
32658333Seric 				if (*p == '\0')
32758333Seric 					break;
32858333Seric 				kp = p;
32958333Seric 
33058333Seric 				/* skip to the value portion */
33158333Seric 				while (isascii(*p) && isalnum(*p) || *p == '-')
33258333Seric 					p++;
33358333Seric 				if (*p == '=')
33458333Seric 				{
33558333Seric 					*p++ = '\0';
33658333Seric 					vp = p;
33758333Seric 
33858333Seric 					/* skip to the end of the value */
33958333Seric 					while (*p != '\0' && *p != ' ' &&
34058333Seric 					       !(isascii(*p) && iscntrl(*p)) &&
34158333Seric 					       *p != '=')
34258333Seric 						p++;
34358333Seric 				}
34458333Seric 
34558333Seric 				if (*p != '\0')
34658333Seric 					*p++ = '\0';
34758333Seric 
34858333Seric 				if (tTd(19, 1))
34958333Seric 					printf("MAIL: got arg %s=%s\n", kp,
35058333Seric 						vp == NULL ? "<null>" : vp);
35158333Seric 
35258333Seric 				if (strcasecmp(kp, "size") == 0)
35358333Seric 				{
35458333Seric 					if (kp == NULL)
35558333Seric 					{
35658333Seric 						usrerr("501 SIZE requires a value");
35758333Seric 						/* NOTREACHED */
35858333Seric 					}
35958333Seric 					msize = atol(vp);
36058333Seric 				}
36158333Seric 				else
36258333Seric 				{
36358333Seric 					usrerr("501 %s parameter unrecognized", kp);
36458333Seric 					/* NOTREACHED */
36558333Seric 				}
36658333Seric 			}
36758333Seric 
36858333Seric 			if (!enoughspace(msize))
36958333Seric 			{
37058333Seric 				message("452 Insufficient disk space; try again later");
37158333Seric 				break;
37258333Seric 			}
37358151Seric 			message("250 Sender ok");
37458147Seric 			gotmail = TRUE;
3754549Seric 			break;
3764549Seric 
3774976Seric 		  case CMDRCPT:		/* rcpt -- designate recipient */
37858850Seric 			if (!gotmail)
37958850Seric 			{
38058850Seric 				usrerr("503 Need MAIL before RCPT");
38158850Seric 				break;
38258850Seric 			}
38324943Seric 			SmtpPhase = "RCPT";
38457389Seric 			setproctitle("%s %s: %s", e->e_id, CurHostName, inp);
38512612Seric 			if (setjmp(TopFrame) > 0)
38614785Seric 			{
38755012Seric 				e->e_flags &= ~EF_FATALERRS;
38812612Seric 				break;
38914785Seric 			}
39012612Seric 			QuickAbort = TRUE;
39151951Seric 			LogUsrErrs = TRUE;
39258093Seric 
39358093Seric 			/* optimization -- if queueing, don't expand aliases */
39458734Seric 			if (e->e_sendmode == SM_QUEUE)
39558093Seric 				e->e_flags |= EF_VRFYONLY;
39658093Seric 
3974549Seric 			p = skipword(p, "to");
3984549Seric 			if (p == NULL)
3994549Seric 				break;
40058333Seric 			a = parseaddr(p, (ADDRESS *) NULL, 1, ' ', NULL, e);
40112612Seric 			if (a == NULL)
40212612Seric 				break;
40316886Seric 			a->q_flags |= QPRIMARY;
40455012Seric 			a = recipient(a, &e->e_sendqueue, e);
40512612Seric 			if (Errors != 0)
40612612Seric 				break;
40712612Seric 
40812612Seric 			/* no errors during parsing, but might be a duplicate */
40955012Seric 			e->e_to = p;
41012612Seric 			if (!bitset(QBADADDR, a->q_flags))
41158151Seric 				message("250 Recipient ok");
41212612Seric 			else
4134549Seric 			{
41412612Seric 				/* punt -- should keep message in ADDRESS.... */
41558151Seric 				message("550 Addressee unknown");
4164549Seric 			}
41755012Seric 			e->e_to = NULL;
4184549Seric 			break;
4194549Seric 
4204549Seric 		  case CMDDATA:		/* data -- text of mail */
42124943Seric 			SmtpPhase = "DATA";
42258109Seric 			if (!gotmail)
4234549Seric 			{
42458151Seric 				message("503 Need MAIL command");
4254976Seric 				break;
4264549Seric 			}
42755012Seric 			else if (e->e_nrcpts <= 0)
4284549Seric 			{
42958151Seric 				message("503 Need RCPT (recipient)");
4304976Seric 				break;
4314549Seric 			}
4324976Seric 
4334976Seric 			/* collect the text of the message */
43424943Seric 			SmtpPhase = "collect";
43557389Seric 			setproctitle("%s %s: %s", e->e_id, CurHostName, inp);
43655012Seric 			collect(TRUE, e);
4374976Seric 			if (Errors != 0)
4384976Seric 				break;
4394976Seric 
4408238Seric 			/*
4418238Seric 			**  Arrange to send to everyone.
4428238Seric 			**	If sending to multiple people, mail back
4438238Seric 			**		errors rather than reporting directly.
4448238Seric 			**	In any case, don't mail back errors for
4458238Seric 			**		anything that has happened up to
4468238Seric 			**		now (the other end will do this).
44710197Seric 			**	Truncate our transcript -- the mail has gotten
44810197Seric 			**		to us successfully, and if we have
44910197Seric 			**		to mail this back, it will be easier
45010197Seric 			**		on the reader.
4518238Seric 			**	Then send to everyone.
4528238Seric 			**	Finally give a reply code.  If an error has
4538238Seric 			**		already been given, don't mail a
4548238Seric 			**		message back.
4559339Seric 			**	We goose error returns by clearing error bit.
4568238Seric 			*/
4578238Seric 
45824943Seric 			SmtpPhase = "delivery";
45955012Seric 			if (e->e_nrcpts != 1)
4609378Seric 			{
4619378Seric 				HoldErrs = TRUE;
46258734Seric 				e->e_errormode = EM_MAIL;
4639378Seric 			}
46455012Seric 			e->e_flags &= ~EF_FATALERRS;
46555012Seric 			e->e_xfp = freopen(queuename(e, 'x'), "w", e->e_xfp);
46658714Seric 			id = e->e_id;
4674976Seric 
4684976Seric 			/* send to all recipients */
46955012Seric 			sendall(e, SM_DEFAULT);
47055012Seric 			e->e_to = NULL;
4714976Seric 
47223516Seric 			/* save statistics */
47355012Seric 			markstats(e, (ADDRESS *) NULL);
47423516Seric 
4758238Seric 			/* issue success if appropriate and reset */
4768238Seric 			if (Errors == 0 || HoldErrs)
477*58855Seric 				message("250 %s Message accepted for delivery", id);
4788238Seric 			else
47955012Seric 				e->e_flags &= ~EF_FATALERRS;
4809339Seric 
4819339Seric 			/* if in a child, pop back to our parent */
4829339Seric 			if (InChild)
4839339Seric 				finis();
48424943Seric 
48524943Seric 			/* clean up a bit */
48658109Seric 			gotmail = FALSE;
48755012Seric 			dropenvelope(e);
48858179Seric 			CurEnv = e = newenvelope(e, CurEnv);
48955012Seric 			e->e_flags = BlankEnvelope.e_flags;
4904549Seric 			break;
4914549Seric 
4924549Seric 		  case CMDRSET:		/* rset -- reset state */
49358151Seric 			message("250 Reset state");
4949339Seric 			if (InChild)
4959339Seric 				finis();
49658109Seric 
49758109Seric 			/* clean up a bit */
49858109Seric 			gotmail = FALSE;
49958109Seric 			dropenvelope(e);
50058179Seric 			CurEnv = e = newenvelope(e, CurEnv);
5019339Seric 			break;
5024549Seric 
5034549Seric 		  case CMDVRFY:		/* vrfy -- verify address */
50458092Seric 		  case CMDEXPN:		/* expn -- expand address */
50558092Seric 			vrfy = c->cmdcode == CMDVRFY;
50658092Seric 			if (bitset(vrfy ? PRIV_NOVRFY : PRIV_NOEXPN,
50758092Seric 						PrivacyFlags))
50858082Seric 			{
50958412Seric 				if (vrfy)
51058412Seric 					message("252 Who's to say?");
51158412Seric 				else
51258412Seric 					message("502 That's none of your business");
51358082Seric 				break;
51458082Seric 			}
51558082Seric 			else if (!gothello &&
51658092Seric 				 bitset(vrfy ? PRIV_NEEDVRFYHELO : PRIV_NEEDEXPNHELO,
51758092Seric 						PrivacyFlags))
51858082Seric 			{
51958151Seric 				message("503 I demand that you introduce yourself first");
52058082Seric 				break;
52158082Seric 			}
52258092Seric 			if (runinchild(vrfy ? "SMTP-VRFY" : "SMTP-EXPN", e) > 0)
5239339Seric 				break;
52425050Seric 			setproctitle("%s: %s", CurHostName, inp);
52555173Seric #ifdef LOG
52658020Seric 			if (LogLevel > 5)
52755173Seric 				syslog(LOG_INFO, "%s: %s", CurHostName, inp);
52855173Seric #endif
5295003Seric 			vrfyqueue = NULL;
5307762Seric 			QuickAbort = TRUE;
53158092Seric 			if (vrfy)
53258092Seric 				e->e_flags |= EF_VRFYONLY;
53358082Seric 			(void) sendtolist(p, (ADDRESS *) NULL, &vrfyqueue, e);
5347762Seric 			if (Errors != 0)
5359339Seric 			{
5369339Seric 				if (InChild)
5379339Seric 					finis();
5387762Seric 				break;
5399339Seric 			}
5405003Seric 			while (vrfyqueue != NULL)
5415003Seric 			{
5425003Seric 				register ADDRESS *a = vrfyqueue->q_next;
5435003Seric 				char *code;
5445003Seric 
5457685Seric 				while (a != NULL && bitset(QDONTSEND|QBADADDR, a->q_flags))
5465003Seric 					a = a->q_next;
5475003Seric 
5487685Seric 				if (!bitset(QDONTSEND|QBADADDR, vrfyqueue->q_flags))
54958151Seric 					printvrfyaddr(vrfyqueue, a == NULL);
5505003Seric 				else if (a == NULL)
55158151Seric 					message("554 Self destructive alias loop");
5525003Seric 				vrfyqueue = a;
5535003Seric 			}
5549339Seric 			if (InChild)
5559339Seric 				finis();
5564549Seric 			break;
5574549Seric 
5584549Seric 		  case CMDHELP:		/* help -- give user info */
5594577Seric 			help(p);
5604549Seric 			break;
5614549Seric 
5624549Seric 		  case CMDNOOP:		/* noop -- do nothing */
56358151Seric 			message("200 OK");
5644549Seric 			break;
5654549Seric 
5664549Seric 		  case CMDQUIT:		/* quit -- leave mail */
56758151Seric 			message("221 %s closing connection", MyHostName);
5689339Seric 			if (InChild)
5699339Seric 				ExitStat = EX_QUIT;
5704549Seric 			finis();
5714549Seric 
5728544Seric 		  case CMDVERB:		/* set verbose mode */
5738544Seric 			Verbose = TRUE;
57458734Seric 			e->e_sendmode = SM_DELIVER;
57558151Seric 			message("200 Verbose mode");
5768544Seric 			break;
5778544Seric 
5789314Seric 		  case CMDONEX:		/* doing one transaction only */
5799378Seric 			OneXact = TRUE;
58058151Seric 			message("200 Only one transaction");
5819314Seric 			break;
5829314Seric 
58336230Skarels # ifdef SMTPDEBUG
5849339Seric 		  case CMDDBGQSHOW:	/* show queues */
5856907Seric 			printf("Send Queue=");
58655012Seric 			printaddr(e->e_sendqueue, TRUE);
5875003Seric 			break;
5887275Seric 
5897275Seric 		  case CMDDBGDEBUG:	/* set debug mode */
5907676Seric 			tTsetup(tTdvect, sizeof tTdvect, "0-99.1");
5917676Seric 			tTflag(p);
59258151Seric 			message("200 Debug set");
5937275Seric 			break;
5947275Seric 
59536230Skarels # else /* not SMTPDEBUG */
59624945Seric 
59736230Skarels 		  case CMDDBGQSHOW:	/* show queues */
59836230Skarels 		  case CMDDBGDEBUG:	/* set debug mode */
59936233Skarels # ifdef LOG
60058308Seric 			if (LogLevel > 0)
60136230Skarels 				syslog(LOG_NOTICE,
60258020Seric 				    "\"%s\" command from %s (%s)",
60336230Skarels 				    c->cmdname, RealHostName,
60458755Seric 				    anynet_ntoa(&RealHostAddr));
60536233Skarels # endif
60636230Skarels 			/* FALL THROUGH */
60736230Skarels # endif /* SMTPDEBUG */
60836230Skarels 
6094549Seric 		  case CMDERROR:	/* unknown command */
61058151Seric 			message("500 Command unrecognized");
6114549Seric 			break;
6124549Seric 
6134549Seric 		  default:
61436230Skarels 			errno = 0;
61558151Seric 			syserr("500 smtp: unknown code %d", c->cmdcode);
6164549Seric 			break;
6174549Seric 		}
6184549Seric 	}
6194549Seric }
6204549Seric /*
6214549Seric **  SKIPWORD -- skip a fixed word.
6224549Seric **
6234549Seric **	Parameters:
6244549Seric **		p -- place to start looking.
6254549Seric **		w -- word to skip.
6264549Seric **
6274549Seric **	Returns:
6284549Seric **		p following w.
6294549Seric **		NULL on error.
6304549Seric **
6314549Seric **	Side Effects:
6324549Seric **		clobbers the p data area.
6334549Seric */
6344549Seric 
6354549Seric static char *
6364549Seric skipword(p, w)
6374549Seric 	register char *p;
6384549Seric 	char *w;
6394549Seric {
6404549Seric 	register char *q;
6414549Seric 
6424549Seric 	/* find beginning of word */
64358050Seric 	while (isascii(*p) && isspace(*p))
6444549Seric 		p++;
6454549Seric 	q = p;
6464549Seric 
6474549Seric 	/* find end of word */
64858050Seric 	while (*p != '\0' && *p != ':' && !(isascii(*p) && isspace(*p)))
6494549Seric 		p++;
65058050Seric 	while (isascii(*p) && isspace(*p))
6514549Seric 		*p++ = '\0';
6524549Seric 	if (*p != ':')
6534549Seric 	{
6544549Seric 	  syntax:
65558151Seric 		message("501 Syntax error");
6564549Seric 		Errors++;
6574549Seric 		return (NULL);
6584549Seric 	}
6594549Seric 	*p++ = '\0';
66058050Seric 	while (isascii(*p) && isspace(*p))
6614549Seric 		p++;
6624549Seric 
6634549Seric 	/* see if the input word matches desired word */
66433725Sbostic 	if (strcasecmp(q, w))
6654549Seric 		goto syntax;
6664549Seric 
6674549Seric 	return (p);
6684549Seric }
6694577Seric /*
67058151Seric **  PRINTVRFYADDR -- print an entry in the verify queue
67158151Seric **
67258151Seric **	Parameters:
67358151Seric **		a -- the address to print
67458151Seric **		last -- set if this is the last one.
67558151Seric **
67658151Seric **	Returns:
67758151Seric **		none.
67858151Seric **
67958151Seric **	Side Effects:
68058151Seric **		Prints the appropriate 250 codes.
68158151Seric */
68258151Seric 
68358151Seric printvrfyaddr(a, last)
68458151Seric 	register ADDRESS *a;
68558151Seric 	bool last;
68658151Seric {
68758151Seric 	char fmtbuf[20];
68858151Seric 
68958151Seric 	strcpy(fmtbuf, "250");
69058151Seric 	fmtbuf[3] = last ? ' ' : '-';
69158151Seric 
69258151Seric 	if (strchr(a->q_paddr, '<') != NULL)
69358151Seric 		strcpy(&fmtbuf[4], "%s");
69458151Seric 	else if (a->q_fullname == NULL)
69558151Seric 		strcpy(&fmtbuf[4], "<%s>");
69658151Seric 	else
69758151Seric 	{
69858151Seric 		strcpy(&fmtbuf[4], "%s <%s>");
69958151Seric 		message(fmtbuf, a->q_fullname, a->q_paddr);
70058151Seric 		return;
70158151Seric 	}
70258151Seric 	message(fmtbuf, a->q_paddr);
70358151Seric }
70458151Seric /*
7054577Seric **  HELP -- implement the HELP command.
7064577Seric **
7074577Seric **	Parameters:
7084577Seric **		topic -- the topic we want help for.
7094577Seric **
7104577Seric **	Returns:
7114577Seric **		none.
7124577Seric **
7134577Seric **	Side Effects:
7144577Seric **		outputs the help file to message output.
7154577Seric */
7164577Seric 
7174577Seric help(topic)
7184577Seric 	char *topic;
7194577Seric {
7204577Seric 	register FILE *hf;
7214577Seric 	int len;
7224577Seric 	char buf[MAXLINE];
7234577Seric 	bool noinfo;
7244577Seric 
7258269Seric 	if (HelpFile == NULL || (hf = fopen(HelpFile, "r")) == NULL)
7264577Seric 	{
7274577Seric 		/* no help */
72811931Seric 		errno = 0;
72958151Seric 		message("502 HELP not implemented");
7304577Seric 		return;
7314577Seric 	}
7324577Seric 
73349669Seric 	if (topic == NULL || *topic == '\0')
73449669Seric 		topic = "smtp";
73549669Seric 	else
73649669Seric 		makelower(topic);
73749669Seric 
7384577Seric 	len = strlen(topic);
7394577Seric 	noinfo = TRUE;
7404577Seric 
7414577Seric 	while (fgets(buf, sizeof buf, hf) != NULL)
7424577Seric 	{
7434577Seric 		if (strncmp(buf, topic, len) == 0)
7444577Seric 		{
7454577Seric 			register char *p;
7464577Seric 
74756795Seric 			p = strchr(buf, '\t');
7484577Seric 			if (p == NULL)
7494577Seric 				p = buf;
7504577Seric 			else
7514577Seric 				p++;
7524577Seric 			fixcrlf(p, TRUE);
75358151Seric 			message("214-%s", p);
7544577Seric 			noinfo = FALSE;
7554577Seric 		}
7564577Seric 	}
7574577Seric 
7584577Seric 	if (noinfo)
75958151Seric 		message("504 HELP topic unknown");
7604577Seric 	else
76158151Seric 		message("214 End of HELP info");
7624628Seric 	(void) fclose(hf);
7634577Seric }
7648544Seric /*
7659339Seric **  RUNINCHILD -- return twice -- once in the child, then in the parent again
7669339Seric **
7679339Seric **	Parameters:
7689339Seric **		label -- a string used in error messages
7699339Seric **
7709339Seric **	Returns:
7719339Seric **		zero in the child
7729339Seric **		one in the parent
7739339Seric **
7749339Seric **	Side Effects:
7759339Seric **		none.
7769339Seric */
7778544Seric 
77855012Seric runinchild(label, e)
7799339Seric 	char *label;
78055012Seric 	register ENVELOPE *e;
7819339Seric {
7829339Seric 	int childpid;
7839339Seric 
78416158Seric 	if (!OneXact)
7859339Seric 	{
78616158Seric 		childpid = dofork();
78716158Seric 		if (childpid < 0)
78816158Seric 		{
78916158Seric 			syserr("%s: cannot fork", label);
79016158Seric 			return (1);
79116158Seric 		}
79216158Seric 		if (childpid > 0)
79316158Seric 		{
79416158Seric 			auto int st;
7959339Seric 
79616158Seric 			/* parent -- wait for child to complete */
79716158Seric 			st = waitfor(childpid);
79816158Seric 			if (st == -1)
79916158Seric 				syserr("%s: lost child", label);
8009339Seric 
80116158Seric 			/* if we exited on a QUIT command, complete the process */
80216158Seric 			if (st == (EX_QUIT << 8))
80316158Seric 				finis();
8049339Seric 
80516158Seric 			return (1);
80616158Seric 		}
80716158Seric 		else
80816158Seric 		{
80916158Seric 			/* child */
81016158Seric 			InChild = TRUE;
81125050Seric 			QuickAbort = FALSE;
81255012Seric 			clearenvelope(e, FALSE);
81316158Seric 		}
8149339Seric 	}
81515256Seric 
81616158Seric 	/* open alias database */
81755012Seric 	initaliases(AliasFile, FALSE, e);
81816158Seric 
81916158Seric 	return (0);
8209339Seric }
8219339Seric 
82256795Seric # endif /* SMTP */
823