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*58821Seric static char sccsid[] = "@(#)srvrsmtp.c	6.30 (Berkeley) 03/26/93 (with SMTP)";
1433731Sbostic #else
15*58821Seric static char sccsid[] = "@(#)srvrsmtp.c	6.30 (Berkeley) 03/26/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))
262*58821Seric 				{
26358789Seric 					message("503 Polite people say HELO first");
264*58821Seric 					break;
265*58821Seric 				}
26658789Seric 				else
267*58821Seric 				{
26858789Seric 					auth_warning(e,
26958789Seric 						"Host %s didn't use HELO protocol",
27058789Seric 						RealHostName);
271*58821Seric 				}
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 */
37824943Seric 			SmtpPhase = "RCPT";
37957389Seric 			setproctitle("%s %s: %s", e->e_id, CurHostName, inp);
38012612Seric 			if (setjmp(TopFrame) > 0)
38114785Seric 			{
38255012Seric 				e->e_flags &= ~EF_FATALERRS;
38312612Seric 				break;
38414785Seric 			}
38512612Seric 			QuickAbort = TRUE;
38651951Seric 			LogUsrErrs = TRUE;
38758093Seric 
38858093Seric 			/* optimization -- if queueing, don't expand aliases */
38958734Seric 			if (e->e_sendmode == SM_QUEUE)
39058093Seric 				e->e_flags |= EF_VRFYONLY;
39158093Seric 
3924549Seric 			p = skipword(p, "to");
3934549Seric 			if (p == NULL)
3944549Seric 				break;
39558333Seric 			a = parseaddr(p, (ADDRESS *) NULL, 1, ' ', NULL, e);
39612612Seric 			if (a == NULL)
39712612Seric 				break;
39816886Seric 			a->q_flags |= QPRIMARY;
39955012Seric 			a = recipient(a, &e->e_sendqueue, e);
40012612Seric 			if (Errors != 0)
40112612Seric 				break;
40212612Seric 
40312612Seric 			/* no errors during parsing, but might be a duplicate */
40455012Seric 			e->e_to = p;
40512612Seric 			if (!bitset(QBADADDR, a->q_flags))
40658151Seric 				message("250 Recipient ok");
40712612Seric 			else
4084549Seric 			{
40912612Seric 				/* punt -- should keep message in ADDRESS.... */
41058151Seric 				message("550 Addressee unknown");
4114549Seric 			}
41255012Seric 			e->e_to = NULL;
4134549Seric 			break;
4144549Seric 
4154549Seric 		  case CMDDATA:		/* data -- text of mail */
41624943Seric 			SmtpPhase = "DATA";
41758109Seric 			if (!gotmail)
4184549Seric 			{
41958151Seric 				message("503 Need MAIL command");
4204976Seric 				break;
4214549Seric 			}
42255012Seric 			else if (e->e_nrcpts <= 0)
4234549Seric 			{
42458151Seric 				message("503 Need RCPT (recipient)");
4254976Seric 				break;
4264549Seric 			}
4274976Seric 
4284976Seric 			/* collect the text of the message */
42924943Seric 			SmtpPhase = "collect";
43057389Seric 			setproctitle("%s %s: %s", e->e_id, CurHostName, inp);
43155012Seric 			collect(TRUE, e);
4324976Seric 			if (Errors != 0)
4334976Seric 				break;
4344976Seric 
4358238Seric 			/*
4368238Seric 			**  Arrange to send to everyone.
4378238Seric 			**	If sending to multiple people, mail back
4388238Seric 			**		errors rather than reporting directly.
4398238Seric 			**	In any case, don't mail back errors for
4408238Seric 			**		anything that has happened up to
4418238Seric 			**		now (the other end will do this).
44210197Seric 			**	Truncate our transcript -- the mail has gotten
44310197Seric 			**		to us successfully, and if we have
44410197Seric 			**		to mail this back, it will be easier
44510197Seric 			**		on the reader.
4468238Seric 			**	Then send to everyone.
4478238Seric 			**	Finally give a reply code.  If an error has
4488238Seric 			**		already been given, don't mail a
4498238Seric 			**		message back.
4509339Seric 			**	We goose error returns by clearing error bit.
4518238Seric 			*/
4528238Seric 
45324943Seric 			SmtpPhase = "delivery";
45455012Seric 			if (e->e_nrcpts != 1)
4559378Seric 			{
4569378Seric 				HoldErrs = TRUE;
45758734Seric 				e->e_errormode = EM_MAIL;
4589378Seric 			}
45955012Seric 			e->e_flags &= ~EF_FATALERRS;
46055012Seric 			e->e_xfp = freopen(queuename(e, 'x'), "w", e->e_xfp);
46158714Seric 			id = e->e_id;
4624976Seric 
4634976Seric 			/* send to all recipients */
46455012Seric 			sendall(e, SM_DEFAULT);
46555012Seric 			e->e_to = NULL;
4664976Seric 
46723516Seric 			/* save statistics */
46855012Seric 			markstats(e, (ADDRESS *) NULL);
46923516Seric 
4708238Seric 			/* issue success if appropriate and reset */
4718238Seric 			if (Errors == 0 || HoldErrs)
47258714Seric 				message("250 %s OK", id);
4738238Seric 			else
47455012Seric 				e->e_flags &= ~EF_FATALERRS;
4759339Seric 
4769339Seric 			/* if in a child, pop back to our parent */
4779339Seric 			if (InChild)
4789339Seric 				finis();
47924943Seric 
48024943Seric 			/* clean up a bit */
48158109Seric 			gotmail = FALSE;
48255012Seric 			dropenvelope(e);
48358179Seric 			CurEnv = e = newenvelope(e, CurEnv);
48455012Seric 			e->e_flags = BlankEnvelope.e_flags;
4854549Seric 			break;
4864549Seric 
4874549Seric 		  case CMDRSET:		/* rset -- reset state */
48858151Seric 			message("250 Reset state");
4899339Seric 			if (InChild)
4909339Seric 				finis();
49158109Seric 
49258109Seric 			/* clean up a bit */
49358109Seric 			gotmail = FALSE;
49458109Seric 			dropenvelope(e);
49558179Seric 			CurEnv = e = newenvelope(e, CurEnv);
4969339Seric 			break;
4974549Seric 
4984549Seric 		  case CMDVRFY:		/* vrfy -- verify address */
49958092Seric 		  case CMDEXPN:		/* expn -- expand address */
50058092Seric 			vrfy = c->cmdcode == CMDVRFY;
50158092Seric 			if (bitset(vrfy ? PRIV_NOVRFY : PRIV_NOEXPN,
50258092Seric 						PrivacyFlags))
50358082Seric 			{
50458412Seric 				if (vrfy)
50558412Seric 					message("252 Who's to say?");
50658412Seric 				else
50758412Seric 					message("502 That's none of your business");
50858082Seric 				break;
50958082Seric 			}
51058082Seric 			else if (!gothello &&
51158092Seric 				 bitset(vrfy ? PRIV_NEEDVRFYHELO : PRIV_NEEDEXPNHELO,
51258092Seric 						PrivacyFlags))
51358082Seric 			{
51458151Seric 				message("503 I demand that you introduce yourself first");
51558082Seric 				break;
51658082Seric 			}
51758092Seric 			if (runinchild(vrfy ? "SMTP-VRFY" : "SMTP-EXPN", e) > 0)
5189339Seric 				break;
51925050Seric 			setproctitle("%s: %s", CurHostName, inp);
52055173Seric #ifdef LOG
52158020Seric 			if (LogLevel > 5)
52255173Seric 				syslog(LOG_INFO, "%s: %s", CurHostName, inp);
52355173Seric #endif
5245003Seric 			vrfyqueue = NULL;
5257762Seric 			QuickAbort = TRUE;
52658092Seric 			if (vrfy)
52758092Seric 				e->e_flags |= EF_VRFYONLY;
52858082Seric 			(void) sendtolist(p, (ADDRESS *) NULL, &vrfyqueue, e);
5297762Seric 			if (Errors != 0)
5309339Seric 			{
5319339Seric 				if (InChild)
5329339Seric 					finis();
5337762Seric 				break;
5349339Seric 			}
5355003Seric 			while (vrfyqueue != NULL)
5365003Seric 			{
5375003Seric 				register ADDRESS *a = vrfyqueue->q_next;
5385003Seric 				char *code;
5395003Seric 
5407685Seric 				while (a != NULL && bitset(QDONTSEND|QBADADDR, a->q_flags))
5415003Seric 					a = a->q_next;
5425003Seric 
5437685Seric 				if (!bitset(QDONTSEND|QBADADDR, vrfyqueue->q_flags))
54458151Seric 					printvrfyaddr(vrfyqueue, a == NULL);
5455003Seric 				else if (a == NULL)
54658151Seric 					message("554 Self destructive alias loop");
5475003Seric 				vrfyqueue = a;
5485003Seric 			}
5499339Seric 			if (InChild)
5509339Seric 				finis();
5514549Seric 			break;
5524549Seric 
5534549Seric 		  case CMDHELP:		/* help -- give user info */
5544577Seric 			help(p);
5554549Seric 			break;
5564549Seric 
5574549Seric 		  case CMDNOOP:		/* noop -- do nothing */
55858151Seric 			message("200 OK");
5594549Seric 			break;
5604549Seric 
5614549Seric 		  case CMDQUIT:		/* quit -- leave mail */
56258151Seric 			message("221 %s closing connection", MyHostName);
5639339Seric 			if (InChild)
5649339Seric 				ExitStat = EX_QUIT;
5654549Seric 			finis();
5664549Seric 
5678544Seric 		  case CMDVERB:		/* set verbose mode */
5688544Seric 			Verbose = TRUE;
56958734Seric 			e->e_sendmode = SM_DELIVER;
57058151Seric 			message("200 Verbose mode");
5718544Seric 			break;
5728544Seric 
5739314Seric 		  case CMDONEX:		/* doing one transaction only */
5749378Seric 			OneXact = TRUE;
57558151Seric 			message("200 Only one transaction");
5769314Seric 			break;
5779314Seric 
57836230Skarels # ifdef SMTPDEBUG
5799339Seric 		  case CMDDBGQSHOW:	/* show queues */
5806907Seric 			printf("Send Queue=");
58155012Seric 			printaddr(e->e_sendqueue, TRUE);
5825003Seric 			break;
5837275Seric 
5847275Seric 		  case CMDDBGDEBUG:	/* set debug mode */
5857676Seric 			tTsetup(tTdvect, sizeof tTdvect, "0-99.1");
5867676Seric 			tTflag(p);
58758151Seric 			message("200 Debug set");
5887275Seric 			break;
5897275Seric 
59036230Skarels # else /* not SMTPDEBUG */
59124945Seric 
59236230Skarels 		  case CMDDBGQSHOW:	/* show queues */
59336230Skarels 		  case CMDDBGDEBUG:	/* set debug mode */
59436233Skarels # ifdef LOG
59558308Seric 			if (LogLevel > 0)
59636230Skarels 				syslog(LOG_NOTICE,
59758020Seric 				    "\"%s\" command from %s (%s)",
59836230Skarels 				    c->cmdname, RealHostName,
59958755Seric 				    anynet_ntoa(&RealHostAddr));
60036233Skarels # endif
60136230Skarels 			/* FALL THROUGH */
60236230Skarels # endif /* SMTPDEBUG */
60336230Skarels 
6044549Seric 		  case CMDERROR:	/* unknown command */
60558151Seric 			message("500 Command unrecognized");
6064549Seric 			break;
6074549Seric 
6084549Seric 		  default:
60936230Skarels 			errno = 0;
61058151Seric 			syserr("500 smtp: unknown code %d", c->cmdcode);
6114549Seric 			break;
6124549Seric 		}
6134549Seric 	}
6144549Seric }
6154549Seric /*
6164549Seric **  SKIPWORD -- skip a fixed word.
6174549Seric **
6184549Seric **	Parameters:
6194549Seric **		p -- place to start looking.
6204549Seric **		w -- word to skip.
6214549Seric **
6224549Seric **	Returns:
6234549Seric **		p following w.
6244549Seric **		NULL on error.
6254549Seric **
6264549Seric **	Side Effects:
6274549Seric **		clobbers the p data area.
6284549Seric */
6294549Seric 
6304549Seric static char *
6314549Seric skipword(p, w)
6324549Seric 	register char *p;
6334549Seric 	char *w;
6344549Seric {
6354549Seric 	register char *q;
6364549Seric 
6374549Seric 	/* find beginning of word */
63858050Seric 	while (isascii(*p) && isspace(*p))
6394549Seric 		p++;
6404549Seric 	q = p;
6414549Seric 
6424549Seric 	/* find end of word */
64358050Seric 	while (*p != '\0' && *p != ':' && !(isascii(*p) && isspace(*p)))
6444549Seric 		p++;
64558050Seric 	while (isascii(*p) && isspace(*p))
6464549Seric 		*p++ = '\0';
6474549Seric 	if (*p != ':')
6484549Seric 	{
6494549Seric 	  syntax:
65058151Seric 		message("501 Syntax error");
6514549Seric 		Errors++;
6524549Seric 		return (NULL);
6534549Seric 	}
6544549Seric 	*p++ = '\0';
65558050Seric 	while (isascii(*p) && isspace(*p))
6564549Seric 		p++;
6574549Seric 
6584549Seric 	/* see if the input word matches desired word */
65933725Sbostic 	if (strcasecmp(q, w))
6604549Seric 		goto syntax;
6614549Seric 
6624549Seric 	return (p);
6634549Seric }
6644577Seric /*
66558151Seric **  PRINTVRFYADDR -- print an entry in the verify queue
66658151Seric **
66758151Seric **	Parameters:
66858151Seric **		a -- the address to print
66958151Seric **		last -- set if this is the last one.
67058151Seric **
67158151Seric **	Returns:
67258151Seric **		none.
67358151Seric **
67458151Seric **	Side Effects:
67558151Seric **		Prints the appropriate 250 codes.
67658151Seric */
67758151Seric 
67858151Seric printvrfyaddr(a, last)
67958151Seric 	register ADDRESS *a;
68058151Seric 	bool last;
68158151Seric {
68258151Seric 	char fmtbuf[20];
68358151Seric 
68458151Seric 	strcpy(fmtbuf, "250");
68558151Seric 	fmtbuf[3] = last ? ' ' : '-';
68658151Seric 
68758151Seric 	if (strchr(a->q_paddr, '<') != NULL)
68858151Seric 		strcpy(&fmtbuf[4], "%s");
68958151Seric 	else if (a->q_fullname == NULL)
69058151Seric 		strcpy(&fmtbuf[4], "<%s>");
69158151Seric 	else
69258151Seric 	{
69358151Seric 		strcpy(&fmtbuf[4], "%s <%s>");
69458151Seric 		message(fmtbuf, a->q_fullname, a->q_paddr);
69558151Seric 		return;
69658151Seric 	}
69758151Seric 	message(fmtbuf, a->q_paddr);
69858151Seric }
69958151Seric /*
7004577Seric **  HELP -- implement the HELP command.
7014577Seric **
7024577Seric **	Parameters:
7034577Seric **		topic -- the topic we want help for.
7044577Seric **
7054577Seric **	Returns:
7064577Seric **		none.
7074577Seric **
7084577Seric **	Side Effects:
7094577Seric **		outputs the help file to message output.
7104577Seric */
7114577Seric 
7124577Seric help(topic)
7134577Seric 	char *topic;
7144577Seric {
7154577Seric 	register FILE *hf;
7164577Seric 	int len;
7174577Seric 	char buf[MAXLINE];
7184577Seric 	bool noinfo;
7194577Seric 
7208269Seric 	if (HelpFile == NULL || (hf = fopen(HelpFile, "r")) == NULL)
7214577Seric 	{
7224577Seric 		/* no help */
72311931Seric 		errno = 0;
72458151Seric 		message("502 HELP not implemented");
7254577Seric 		return;
7264577Seric 	}
7274577Seric 
72849669Seric 	if (topic == NULL || *topic == '\0')
72949669Seric 		topic = "smtp";
73049669Seric 	else
73149669Seric 		makelower(topic);
73249669Seric 
7334577Seric 	len = strlen(topic);
7344577Seric 	noinfo = TRUE;
7354577Seric 
7364577Seric 	while (fgets(buf, sizeof buf, hf) != NULL)
7374577Seric 	{
7384577Seric 		if (strncmp(buf, topic, len) == 0)
7394577Seric 		{
7404577Seric 			register char *p;
7414577Seric 
74256795Seric 			p = strchr(buf, '\t');
7434577Seric 			if (p == NULL)
7444577Seric 				p = buf;
7454577Seric 			else
7464577Seric 				p++;
7474577Seric 			fixcrlf(p, TRUE);
74858151Seric 			message("214-%s", p);
7494577Seric 			noinfo = FALSE;
7504577Seric 		}
7514577Seric 	}
7524577Seric 
7534577Seric 	if (noinfo)
75458151Seric 		message("504 HELP topic unknown");
7554577Seric 	else
75658151Seric 		message("214 End of HELP info");
7574628Seric 	(void) fclose(hf);
7584577Seric }
7598544Seric /*
7609339Seric **  RUNINCHILD -- return twice -- once in the child, then in the parent again
7619339Seric **
7629339Seric **	Parameters:
7639339Seric **		label -- a string used in error messages
7649339Seric **
7659339Seric **	Returns:
7669339Seric **		zero in the child
7679339Seric **		one in the parent
7689339Seric **
7699339Seric **	Side Effects:
7709339Seric **		none.
7719339Seric */
7728544Seric 
77355012Seric runinchild(label, e)
7749339Seric 	char *label;
77555012Seric 	register ENVELOPE *e;
7769339Seric {
7779339Seric 	int childpid;
7789339Seric 
77916158Seric 	if (!OneXact)
7809339Seric 	{
78116158Seric 		childpid = dofork();
78216158Seric 		if (childpid < 0)
78316158Seric 		{
78416158Seric 			syserr("%s: cannot fork", label);
78516158Seric 			return (1);
78616158Seric 		}
78716158Seric 		if (childpid > 0)
78816158Seric 		{
78916158Seric 			auto int st;
7909339Seric 
79116158Seric 			/* parent -- wait for child to complete */
79216158Seric 			st = waitfor(childpid);
79316158Seric 			if (st == -1)
79416158Seric 				syserr("%s: lost child", label);
7959339Seric 
79616158Seric 			/* if we exited on a QUIT command, complete the process */
79716158Seric 			if (st == (EX_QUIT << 8))
79816158Seric 				finis();
7999339Seric 
80016158Seric 			return (1);
80116158Seric 		}
80216158Seric 		else
80316158Seric 		{
80416158Seric 			/* child */
80516158Seric 			InChild = TRUE;
80625050Seric 			QuickAbort = FALSE;
80755012Seric 			clearenvelope(e, FALSE);
80816158Seric 		}
8099339Seric 	}
81015256Seric 
81116158Seric 	/* open alias database */
81255012Seric 	initaliases(AliasFile, FALSE, e);
81316158Seric 
81416158Seric 	return (0);
8159339Seric }
8169339Seric 
81756795Seric # endif /* SMTP */
818