14549Seric # include "sendmail.h"
24549Seric 
35181Seric # ifndef SMTP
4*7685Seric SCCSID(@(#)srvrsmtp.c	3.25		08/08/82	(no SMTP));
55181Seric # else SMTP
64556Seric 
7*7685Seric SCCSID(@(#)srvrsmtp.c	3.25		08/08/82);
85181Seric 
94549Seric /*
104549Seric **  SMTP -- run the SMTP protocol.
114549Seric **
124549Seric **	Parameters:
134549Seric **		none.
144549Seric **
154549Seric **	Returns:
164549Seric **		never.
174549Seric **
184549Seric **	Side Effects:
194549Seric **		Reads commands from the input channel and processes
204549Seric **			them.
214549Seric */
224549Seric 
234549Seric struct cmd
244549Seric {
254549Seric 	char	*cmdname;	/* command name */
264549Seric 	int	cmdcode;	/* internal code, see below */
274549Seric };
284549Seric 
294549Seric /* values for cmdcode */
304549Seric # define CMDERROR	0	/* bad command */
314549Seric # define CMDMAIL	1	/* mail -- designate sender */
324976Seric # define CMDRCPT	2	/* rcpt -- designate recipient */
334549Seric # define CMDDATA	3	/* data -- send message text */
344549Seric # define CMDRSET	5	/* rset -- reset state */
354549Seric # define CMDVRFY	6	/* vrfy -- verify address */
364549Seric # define CMDHELP	7	/* help -- give usage info */
374549Seric # define CMDNOOP	8	/* noop -- do nothing */
384549Seric # define CMDQUIT	9	/* quit -- close connection and die */
394577Seric # define CMDMRSQ	10	/* mrsq -- for old mtp compat only */
404976Seric # define CMDHELO	11	/* helo -- be polite */
417275Seric # define CMDDBGSHOWQ	12	/* _showq -- show send queue (DEBUG) */
427275Seric # define CMDDBGDEBUG	13	/* _debug -- set debug mode */
437275Seric # define CMDDBGVERBOSE	14	/* _verbose -- go into verbose mode */
447282Seric # define CMDDBGKILL	15	/* _kill -- kill sendmail */
454549Seric 
464549Seric static struct cmd	CmdTab[] =
474549Seric {
484549Seric 	"mail",		CMDMAIL,
494976Seric 	"rcpt",		CMDRCPT,
504976Seric 	"mrcp",		CMDRCPT,	/* for old MTP compatability */
514549Seric 	"data",		CMDDATA,
524549Seric 	"rset",		CMDRSET,
534549Seric 	"vrfy",		CMDVRFY,
544549Seric 	"help",		CMDHELP,
554549Seric 	"noop",		CMDNOOP,
564549Seric 	"quit",		CMDQUIT,
574577Seric 	"mrsq",		CMDMRSQ,
584976Seric 	"helo",		CMDHELO,
595003Seric # ifdef DEBUG
607275Seric 	"_showq",	CMDDBGSHOWQ,
617275Seric 	"_debug",	CMDDBGDEBUG,
627275Seric 	"_verbose",	CMDDBGVERBOSE,
637282Seric 	"_kill",	CMDDBGKILL,
645003Seric # endif DEBUG
654549Seric 	NULL,		CMDERROR,
664549Seric };
674549Seric 
684549Seric smtp()
694549Seric {
704549Seric 	char inp[MAXLINE];
714549Seric 	register char *p;
724549Seric 	struct cmd *c;
734549Seric 	char *cmd;
744549Seric 	extern char *skipword();
754549Seric 	extern bool sameword();
764549Seric 	bool hasmail;			/* mail command received */
774713Seric 	int rcps;			/* number of recipients */
785003Seric 	auto ADDRESS *vrfyqueue;
797124Seric 	extern char Version[];
807356Seric 	extern tick();
814549Seric 
825003Seric 	hasmail = FALSE;
834713Seric 	rcps = 0;
847363Seric 	if (OutChannel != stdout)
857363Seric 	{
867363Seric 		/* arrange for debugging output to go to remote host */
877363Seric 		(void) close(1);
887363Seric 		(void) dup(fileno(OutChannel));
897363Seric 	}
907124Seric 	message("220", "%s Sendmail version %s at your service", HostName, Version);
917356Seric 	(void) signal(SIGALRM, tick);
924549Seric 	for (;;)
934549Seric 	{
947356Seric 		/* setup for the read */
956907Seric 		CurEnv->e_to = NULL;
964577Seric 		Errors = 0;
977275Seric 		(void) fflush(stdout);
987356Seric 
997356Seric 		/* read the input line */
100*7685Seric 		p = sfgets(inp, sizeof inp, InChannel);
1017356Seric 
102*7685Seric 		/* handle errors */
1037356Seric 		if (p == NULL)
1047356Seric 		{
1054549Seric 			/* end of file, just die */
1064558Seric 			message("421", "%s Lost input channel", HostName);
1074549Seric 			finis();
1084549Seric 		}
1094549Seric 
1104549Seric 		/* clean up end of line */
1114558Seric 		fixcrlf(inp, TRUE);
1124549Seric 
1134713Seric 		/* echo command to transcript */
1147557Seric 		fprintf(Xscript, "<<< %s\n", inp);
1154713Seric 
1164549Seric 		/* break off command */
1174549Seric 		for (p = inp; isspace(*p); p++)
1184549Seric 			continue;
1194549Seric 		cmd = p;
1204549Seric 		while (*++p != '\0' && !isspace(*p))
1214549Seric 			continue;
1224549Seric 		if (*p != '\0')
1234549Seric 			*p++ = '\0';
1244549Seric 
1254549Seric 		/* decode command */
1264549Seric 		for (c = CmdTab; c->cmdname != NULL; c++)
1274549Seric 		{
1284549Seric 			if (sameword(c->cmdname, cmd))
1294549Seric 				break;
1304549Seric 		}
1314549Seric 
1324549Seric 		/* process command */
1334549Seric 		switch (c->cmdcode)
1344549Seric 		{
1354976Seric 		  case CMDHELO:		/* hello -- introduce yourself */
1365187Seric 			define('s', newstr(p));
1374997Seric 			message("250", "%s Hello %s, pleased to meet you",
1384997Seric 				HostName, p);
1394976Seric 			break;
1404976Seric 
1414549Seric 		  case CMDMAIL:		/* mail -- designate sender */
1424558Seric 			if (hasmail)
1434558Seric 			{
1444558Seric 				message("503", "Sender already specified");
1454558Seric 				break;
1464558Seric 			}
1474549Seric 			p = skipword(p, "from");
1484549Seric 			if (p == NULL)
1494549Seric 				break;
1504549Seric 			if (index(p, ',') != NULL)
1514549Seric 			{
1524549Seric 				message("501", "Source routing not implemented");
1534549Seric 				Errors++;
1544549Seric 				break;
1554549Seric 			}
1564549Seric 			setsender(p);
1574577Seric 			if (Errors == 0)
1584549Seric 			{
1594549Seric 				message("250", "Sender ok");
1604549Seric 				hasmail = TRUE;
1614549Seric 			}
1624549Seric 			break;
1634549Seric 
1644976Seric 		  case CMDRCPT:		/* rcpt -- designate recipient */
1654549Seric 			p = skipword(p, "to");
1664549Seric 			if (p == NULL)
1674549Seric 				break;
1684549Seric 			if (index(p, ',') != NULL)
1694549Seric 			{
1704549Seric 				message("501", "Source routing not implemented");
1714549Seric 				Errors++;
1724549Seric 				break;
1734549Seric 			}
1746907Seric 			sendto(p, 1, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
1754577Seric 			if (Errors == 0)
1764549Seric 			{
1776057Seric 				message("250", "%s... Recipient ok", p);
1784713Seric 				rcps++;
1794549Seric 			}
1804549Seric 			break;
1814549Seric 
1824549Seric 		  case CMDDATA:		/* data -- text of mail */
1834976Seric 			if (!hasmail)
1844549Seric 			{
1854976Seric 				message("503", "Need MAIL command");
1864976Seric 				break;
1874549Seric 			}
1884713Seric 			else if (rcps <= 0)
1894549Seric 			{
1904976Seric 				message("503", "Need RCPT (recipient)");
1914976Seric 				break;
1924549Seric 			}
1934976Seric 
1944976Seric 			/* collect the text of the message */
1954976Seric 			collect(TRUE);
1964976Seric 			if (Errors != 0)
1974976Seric 				break;
1984976Seric 
1994976Seric 			/* if sending to multiple people, mail back errors */
2004976Seric 			if (rcps != 1)
2014976Seric 				HoldErrs = MailBack = TRUE;
2024976Seric 
2034976Seric 			/* send to all recipients */
2047046Seric 			sendall(CurEnv, FALSE);
2054976Seric 
2064976Seric 			/* reset strange modes */
2074976Seric 			HoldErrs = FALSE;
2086907Seric 			CurEnv->e_to = NULL;
2094976Seric 
2104976Seric 			/* issue success if appropriate */
2114976Seric 			if (Errors == 0 || rcps != 1)
2124976Seric 				message("250", "Sent");
2134549Seric 			break;
2144549Seric 
2154549Seric 		  case CMDRSET:		/* rset -- reset state */
2164549Seric 			message("250", "Reset state");
2174549Seric 			finis();
2184549Seric 
2194549Seric 		  case CMDVRFY:		/* vrfy -- verify address */
2205003Seric 			vrfyqueue = NULL;
2215003Seric 			sendto(p, 1, (ADDRESS *) NULL, &vrfyqueue);
2225003Seric 			while (vrfyqueue != NULL)
2235003Seric 			{
2245003Seric 				register ADDRESS *a = vrfyqueue->q_next;
2255003Seric 				char *code;
2265003Seric 
227*7685Seric 				while (a != NULL && bitset(QDONTSEND|QBADADDR, a->q_flags))
2285003Seric 					a = a->q_next;
2295003Seric 
230*7685Seric 				if (!bitset(QDONTSEND|QBADADDR, vrfyqueue->q_flags))
2315003Seric 				{
2325003Seric 					if (a != NULL)
2335003Seric 						code = "250-";
2345003Seric 					else
2355003Seric 						code = "250";
2365003Seric 					if (vrfyqueue->q_fullname == NULL)
2375003Seric 						message(code, "<%s>", vrfyqueue->q_paddr);
2385003Seric 					else
2395003Seric 						message(code, "%s <%s>",
2405003Seric 						    vrfyqueue->q_fullname, vrfyqueue->q_paddr);
2415003Seric 				}
2425003Seric 				else if (a == NULL)
2435003Seric 					message("554", "Self destructive alias loop");
2445003Seric 				vrfyqueue = a;
2455003Seric 			}
2464549Seric 			break;
2474549Seric 
2484549Seric 		  case CMDHELP:		/* help -- give user info */
2494577Seric 			if (*p == '\0')
2504577Seric 				p = "SMTP";
2514577Seric 			help(p);
2524549Seric 			break;
2534549Seric 
2544549Seric 		  case CMDNOOP:		/* noop -- do nothing */
2554549Seric 			message("200", "OK");
2564549Seric 			break;
2574549Seric 
2584549Seric 		  case CMDQUIT:		/* quit -- leave mail */
2594549Seric 			message("221", "%s closing connection", HostName);
2604549Seric 			finis();
2614549Seric 
2624577Seric 		  case CMDMRSQ:		/* mrsq -- negotiate protocol */
2634577Seric 			if (*p == 'R' || *p == 'T')
2644577Seric 			{
2654577Seric 				/* recipients first or text first */
2664577Seric 				message("200", "%c ok, please continue", *p);
2674577Seric 			}
2684577Seric 			else if (*p == '?')
2694577Seric 			{
2704577Seric 				/* what do I prefer?  anything, anytime */
2714577Seric 				message("215", "R Recipients first is my choice");
2724577Seric 			}
2734577Seric 			else if (*p == '\0')
2744577Seric 			{
2754577Seric 				/* no meaningful scheme */
2764577Seric 				message("200", "okey dokie boobie");
2774577Seric 			}
2784577Seric 			else
2794577Seric 			{
2804577Seric 				/* bad argument */
2814577Seric 				message("504", "Scheme unknown");
2824577Seric 			}
2834577Seric 			break;
2844577Seric 
2855003Seric # ifdef DEBUG
2865003Seric 		  case CMDDBGSHOWQ:	/* show queues */
2876907Seric 			printf("Send Queue=");
2886907Seric 			printaddr(CurEnv->e_sendqueue, TRUE);
2895003Seric 			break;
2907275Seric 
2917275Seric 		  case CMDDBGDEBUG:	/* set debug mode */
2927676Seric 			tTsetup(tTdvect, sizeof tTdvect, "0-99.1");
2937676Seric 			tTflag(p);
2947676Seric 			message("200", "Debug set");
2957275Seric 			break;
2967275Seric 
2977275Seric 		  case CMDDBGVERBOSE:	/* set verbose mode */
2987275Seric 			Verbose = TRUE;
2997275Seric 			message("200", "Verbose mode");
3007275Seric 			break;
3017282Seric 
3027282Seric 		  case CMDDBGKILL:	/* kill the parent */
3037282Seric 			if (kill(MotherPid, SIGTERM) >= 0)
3047282Seric 				message("200", "Mother is dead");
3057282Seric 			else
3067282Seric 				message("500", "Can't kill Mom");
3077282Seric 			break;
3085003Seric # endif DEBUG
3095003Seric 
3104549Seric 		  case CMDERROR:	/* unknown command */
3114549Seric 			message("500", "Command unrecognized");
3124549Seric 			break;
3134549Seric 
3144549Seric 		  default:
3154549Seric 			syserr("smtp: unknown code %d", c->cmdcode);
3164549Seric 			break;
3174549Seric 		}
3184549Seric 	}
3194549Seric }
3204549Seric /*
3214549Seric **  SKIPWORD -- skip a fixed word.
3224549Seric **
3234549Seric **	Parameters:
3244549Seric **		p -- place to start looking.
3254549Seric **		w -- word to skip.
3264549Seric **
3274549Seric **	Returns:
3284549Seric **		p following w.
3294549Seric **		NULL on error.
3304549Seric **
3314549Seric **	Side Effects:
3324549Seric **		clobbers the p data area.
3334549Seric */
3344549Seric 
3354549Seric static char *
3364549Seric skipword(p, w)
3374549Seric 	register char *p;
3384549Seric 	char *w;
3394549Seric {
3404549Seric 	register char *q;
3414549Seric 	extern bool sameword();
3424549Seric 
3434549Seric 	/* find beginning of word */
3444549Seric 	while (isspace(*p))
3454549Seric 		p++;
3464549Seric 	q = p;
3474549Seric 
3484549Seric 	/* find end of word */
3494549Seric 	while (*p != '\0' && *p != ':' && !isspace(*p))
3504549Seric 		p++;
3514549Seric 	while (isspace(*p))
3524549Seric 		*p++ = '\0';
3534549Seric 	if (*p != ':')
3544549Seric 	{
3554549Seric 	  syntax:
3564549Seric 		message("501", "Syntax error");
3574549Seric 		Errors++;
3584549Seric 		return (NULL);
3594549Seric 	}
3604549Seric 	*p++ = '\0';
3614549Seric 	while (isspace(*p))
3624549Seric 		p++;
3634549Seric 
3644549Seric 	/* see if the input word matches desired word */
3654549Seric 	if (!sameword(q, w))
3664549Seric 		goto syntax;
3674549Seric 
3684549Seric 	return (p);
3694549Seric }
3704577Seric /*
3714577Seric **  HELP -- implement the HELP command.
3724577Seric **
3734577Seric **	Parameters:
3744577Seric **		topic -- the topic we want help for.
3754577Seric **
3764577Seric **	Returns:
3774577Seric **		none.
3784577Seric **
3794577Seric **	Side Effects:
3804577Seric **		outputs the help file to message output.
3814577Seric */
3824577Seric 
3834577Seric help(topic)
3844577Seric 	char *topic;
3854577Seric {
3864577Seric 	register FILE *hf;
3874577Seric 	int len;
3884577Seric 	char buf[MAXLINE];
3894577Seric 	bool noinfo;
3904582Seric 	extern char *HelpFile;
3914577Seric 
3924582Seric 	hf = fopen(HelpFile, "r");
3934577Seric 	if (hf == NULL)
3944577Seric 	{
3954577Seric 		/* no help */
3964577Seric 		message("502", "HELP not implemented");
3974577Seric 		return;
3984577Seric 	}
3994577Seric 
4004577Seric 	len = strlen(topic);
4014577Seric 	makelower(topic);
4024577Seric 	noinfo = TRUE;
4034577Seric 
4044577Seric 	while (fgets(buf, sizeof buf, hf) != NULL)
4054577Seric 	{
4064577Seric 		if (strncmp(buf, topic, len) == 0)
4074577Seric 		{
4084577Seric 			register char *p;
4094577Seric 
4104577Seric 			p = index(buf, '\t');
4114577Seric 			if (p == NULL)
4124577Seric 				p = buf;
4134577Seric 			else
4144577Seric 				p++;
4154577Seric 			fixcrlf(p, TRUE);
4164577Seric 			message("214-", p);
4174577Seric 			noinfo = FALSE;
4184577Seric 		}
4194577Seric 	}
4204577Seric 
4214577Seric 	if (noinfo)
4224577Seric 		message("504", "HELP topic unknown");
4234577Seric 	else
4244577Seric 		message("214", "End of HELP info");
4254628Seric 	(void) fclose(hf);
4264577Seric }
4275181Seric 
4285181Seric # endif SMTP
429