1 /*
2 **  Sendmail
3 **  Copyright (c) 1983  Eric P. Allman
4 **  Berkeley, California
5 **
6 **  Copyright (c) 1983 Regents of the University of California.
7 **  All rights reserved.  The Berkeley software License Agreement
8 **  specifies the terms and conditions for redistribution.
9 */
10 
11 
12 # include <errno.h>
13 # include "sendmail.h"
14 # include <signal.h>
15 
16 # ifndef SMTP
17 # ifndef lint
18 static char	SccsId[] = "@(#)srvrsmtp.c	5.9 (Berkeley) 09/19/85	(no SMTP)";
19 # endif not lint
20 # else SMTP
21 
22 # ifndef lint
23 static char	SccsId[] = "@(#)srvrsmtp.c	5.9 (Berkeley) 09/19/85";
24 # endif not lint
25 
26 /*
27 **  SMTP -- run the SMTP protocol.
28 **
29 **	Parameters:
30 **		none.
31 **
32 **	Returns:
33 **		never.
34 **
35 **	Side Effects:
36 **		Reads commands from the input channel and processes
37 **			them.
38 */
39 
40 struct cmd
41 {
42 	char	*cmdname;	/* command name */
43 	int	cmdcode;	/* internal code, see below */
44 };
45 
46 /* values for cmdcode */
47 # define CMDERROR	0	/* bad command */
48 # define CMDMAIL	1	/* mail -- designate sender */
49 # define CMDRCPT	2	/* rcpt -- designate recipient */
50 # define CMDDATA	3	/* data -- send message text */
51 # define CMDRSET	4	/* rset -- reset state */
52 # define CMDVRFY	5	/* vrfy -- verify address */
53 # define CMDHELP	6	/* help -- give usage info */
54 # define CMDNOOP	7	/* noop -- do nothing */
55 # define CMDQUIT	8	/* quit -- close connection and die */
56 # define CMDHELO	9	/* helo -- be polite */
57 # define CMDDBGQSHOW	10	/* showq -- show send queue (DEBUG) */
58 # define CMDDBGDEBUG	11	/* debug -- set debug mode */
59 # define CMDVERB	12	/* verb -- go into verbose mode */
60 # define CMDDBGKILL	13	/* kill -- kill sendmail */
61 # define CMDDBGWIZ	14	/* wiz -- become a wizard */
62 # define CMDONEX	15	/* onex -- sending one transaction only */
63 
64 static struct cmd	CmdTab[] =
65 {
66 	"mail",		CMDMAIL,
67 	"rcpt",		CMDRCPT,
68 	"data",		CMDDATA,
69 	"rset",		CMDRSET,
70 	"vrfy",		CMDVRFY,
71 	"expn",		CMDVRFY,
72 	"help",		CMDHELP,
73 	"noop",		CMDNOOP,
74 	"quit",		CMDQUIT,
75 	"helo",		CMDHELO,
76 	"verb",		CMDVERB,
77 	"onex",		CMDONEX,
78 # ifdef DEBUG
79 	"showq",	CMDDBGQSHOW,
80 	"debug",	CMDDBGDEBUG,
81 # endif DEBUG
82 # ifdef WIZ
83 	"kill",		CMDDBGKILL,
84 # endif WIZ
85 	"wiz",		CMDDBGWIZ,
86 	NULL,		CMDERROR,
87 };
88 
89 # ifdef WIZ
90 bool	IsWiz = FALSE;			/* set if we are a wizard */
91 # endif WIZ
92 char	*WizWord;			/* the wizard word to compare against */
93 bool	InChild = FALSE;		/* true if running in a subprocess */
94 bool	OneXact = FALSE;		/* one xaction only this run */
95 
96 #define EX_QUIT		22		/* special code for QUIT command */
97 
98 smtp()
99 {
100 	register char *p;
101 	register struct cmd *c;
102 	char *cmd;
103 	extern char *skipword();
104 	extern bool sameword();
105 	bool hasmail;			/* mail command received */
106 	auto ADDRESS *vrfyqueue;
107 	ADDRESS *a;
108 	char inp[MAXLINE];
109 	extern char Version[];
110 	extern tick();
111 	extern bool iswiz();
112 	extern char *arpadate();
113 	extern char *macvalue();
114 	extern ADDRESS *recipient();
115 	extern ENVELOPE BlankEnvelope;
116 	extern ENVELOPE *newenvelope();
117 
118 	hasmail = FALSE;
119 	if (OutChannel != stdout)
120 	{
121 		/* arrange for debugging output to go to remote host */
122 		(void) close(1);
123 		(void) dup(fileno(OutChannel));
124 	}
125 	settime();
126 	expand("\001e", inp, &inp[sizeof inp], CurEnv);
127 	message("220", inp);
128 	SmtpPhase = "startup";
129 	for (;;)
130 	{
131 		/* arrange for backout */
132 		if (setjmp(TopFrame) > 0 && InChild)
133 			finis();
134 		QuickAbort = FALSE;
135 		HoldErrs = FALSE;
136 
137 		/* setup for the read */
138 		CurEnv->e_to = NULL;
139 		Errors = 0;
140 		(void) fflush(stdout);
141 
142 		/* read the input line */
143 		p = sfgets(inp, sizeof inp, InChannel);
144 
145 		/* handle errors */
146 		if (p == NULL)
147 		{
148 			/* end of file, just die */
149 			message("421", "%s Lost input channel", HostName);
150 			finis();
151 		}
152 
153 		/* clean up end of line */
154 		fixcrlf(inp, TRUE);
155 
156 		/* echo command to transcript */
157 		if (CurEnv->e_xfp != NULL)
158 			fprintf(CurEnv->e_xfp, "<<< %s\n", inp);
159 
160 		/* break off command */
161 		for (p = inp; isspace(*p); p++)
162 			continue;
163 		cmd = p;
164 		while (*++p != '\0' && !isspace(*p))
165 			continue;
166 		if (*p != '\0')
167 			*p++ = '\0';
168 
169 		/* decode command */
170 		for (c = CmdTab; c->cmdname != NULL; c++)
171 		{
172 			if (sameword(c->cmdname, cmd))
173 				break;
174 		}
175 
176 		/* process command */
177 		switch (c->cmdcode)
178 		{
179 		  case CMDHELO:		/* hello -- introduce yourself */
180 			SmtpPhase = "HELO";
181 			if (sameword(p, HostName))
182 			{
183 				/* connected to an echo server */
184 				message("553", "%s I refuse to talk to myself",
185 					HostName);
186 				break;
187 			}
188 			if (RealHostName != NULL && !sameword(p, RealHostName))
189 			{
190 				char buf[MAXNAME];
191 
192 				(void) sprintf(buf, "%s (%s)", p, RealHostName);
193 				define('s', newstr(buf), CurEnv);
194 			}
195 			else
196 				define('s', newstr(p), CurEnv);
197 			message("250", "%s Hello %s, pleased to meet you",
198 				HostName, p);
199 			break;
200 
201 		  case CMDMAIL:		/* mail -- designate sender */
202 			SmtpPhase = "MAIL";
203 
204 			/* force a sending host even if no HELO given */
205 			if (RealHostName != NULL && macvalue('s', CurEnv) == NULL)
206 				define('s', RealHostName, CurEnv);
207 
208 			/* check for validity of this command */
209 			if (hasmail)
210 			{
211 				message("503", "Sender already specified");
212 				break;
213 			}
214 			if (InChild)
215 			{
216 				syserr("Nested MAIL command");
217 				exit(0);
218 			}
219 
220 			/* fork a subprocess to process this command */
221 			if (runinchild("SMTP-MAIL") > 0)
222 				break;
223 			initsys();
224 
225 			/* child -- go do the processing */
226 			p = skipword(p, "from");
227 			if (p == NULL)
228 				break;
229 			setsender(p);
230 			if (Errors == 0)
231 			{
232 				message("250", "Sender ok");
233 				hasmail = TRUE;
234 			}
235 			else if (InChild)
236 				finis();
237 			break;
238 
239 		  case CMDRCPT:		/* rcpt -- designate recipient */
240 			SmtpPhase = "RCPT";
241 			if (setjmp(TopFrame) > 0)
242 			{
243 				CurEnv->e_flags &= ~EF_FATALERRS;
244 				break;
245 			}
246 			QuickAbort = TRUE;
247 			p = skipword(p, "to");
248 			if (p == NULL)
249 				break;
250 			a = parseaddr(p, (ADDRESS *) NULL, 1, '\0');
251 			if (a == NULL)
252 				break;
253 			a->q_flags |= QPRIMARY;
254 			a = recipient(a, &CurEnv->e_sendqueue);
255 			if (Errors != 0)
256 				break;
257 
258 			/* no errors during parsing, but might be a duplicate */
259 			CurEnv->e_to = p;
260 			if (!bitset(QBADADDR, a->q_flags))
261 				message("250", "Recipient ok");
262 			else
263 			{
264 				/* punt -- should keep message in ADDRESS.... */
265 				message("550", "Addressee unknown");
266 			}
267 			CurEnv->e_to = NULL;
268 			break;
269 
270 		  case CMDDATA:		/* data -- text of mail */
271 			SmtpPhase = "DATA";
272 			if (!hasmail)
273 			{
274 				message("503", "Need MAIL command");
275 				break;
276 			}
277 			else if (CurEnv->e_nrcpts <= 0)
278 			{
279 				message("503", "Need RCPT (recipient)");
280 				break;
281 			}
282 
283 			/* collect the text of the message */
284 			SmtpPhase = "collect";
285 			collect(TRUE);
286 			if (Errors != 0)
287 				break;
288 
289 			/*
290 			**  Arrange to send to everyone.
291 			**	If sending to multiple people, mail back
292 			**		errors rather than reporting directly.
293 			**	In any case, don't mail back errors for
294 			**		anything that has happened up to
295 			**		now (the other end will do this).
296 			**	Truncate our transcript -- the mail has gotten
297 			**		to us successfully, and if we have
298 			**		to mail this back, it will be easier
299 			**		on the reader.
300 			**	Then send to everyone.
301 			**	Finally give a reply code.  If an error has
302 			**		already been given, don't mail a
303 			**		message back.
304 			**	We goose error returns by clearing error bit.
305 			*/
306 
307 			SmtpPhase = "delivery";
308 			if (CurEnv->e_nrcpts != 1)
309 			{
310 				HoldErrs = TRUE;
311 				ErrorMode = EM_MAIL;
312 			}
313 			CurEnv->e_flags &= ~EF_FATALERRS;
314 			CurEnv->e_xfp = freopen(queuename(CurEnv, 'x'), "w", CurEnv->e_xfp);
315 
316 			/* send to all recipients */
317 			sendall(CurEnv, SM_DEFAULT);
318 			CurEnv->e_to = NULL;
319 
320 			/* save statistics */
321 			markstats(CurEnv, (ADDRESS *) NULL);
322 
323 			/* issue success if appropriate and reset */
324 			if (Errors == 0 || HoldErrs)
325 				message("250", "Ok");
326 			else
327 				CurEnv->e_flags &= ~EF_FATALERRS;
328 
329 			/* if in a child, pop back to our parent */
330 			if (InChild)
331 				finis();
332 
333 			/* clean up a bit */
334 			hasmail = 0;
335 			dropenvelope(CurEnv);
336 			CurEnv = newenvelope(CurEnv);
337 			CurEnv->e_flags = BlankEnvelope.e_flags;
338 			break;
339 
340 		  case CMDRSET:		/* rset -- reset state */
341 			message("250", "Reset state");
342 			if (InChild)
343 				finis();
344 			break;
345 
346 		  case CMDVRFY:		/* vrfy -- verify address */
347 			if (runinchild("SMTP-VRFY") > 0)
348 				break;
349 			vrfyqueue = NULL;
350 			QuickAbort = TRUE;
351 			sendtolist(p, (ADDRESS *) NULL, &vrfyqueue);
352 			if (Errors != 0)
353 			{
354 				if (InChild)
355 					finis();
356 				break;
357 			}
358 			while (vrfyqueue != NULL)
359 			{
360 				register ADDRESS *a = vrfyqueue->q_next;
361 				char *code;
362 
363 				while (a != NULL && bitset(QDONTSEND|QBADADDR, a->q_flags))
364 					a = a->q_next;
365 
366 				if (!bitset(QDONTSEND|QBADADDR, vrfyqueue->q_flags))
367 				{
368 					if (a != NULL)
369 						code = "250-";
370 					else
371 						code = "250";
372 					if (vrfyqueue->q_fullname == NULL)
373 						message(code, "<%s>", vrfyqueue->q_paddr);
374 					else
375 						message(code, "%s <%s>",
376 						    vrfyqueue->q_fullname, vrfyqueue->q_paddr);
377 				}
378 				else if (a == NULL)
379 					message("554", "Self destructive alias loop");
380 				vrfyqueue = a;
381 			}
382 			if (InChild)
383 				finis();
384 			break;
385 
386 		  case CMDHELP:		/* help -- give user info */
387 			if (*p == '\0')
388 				p = "SMTP";
389 			help(p);
390 			break;
391 
392 		  case CMDNOOP:		/* noop -- do nothing */
393 			message("200", "OK");
394 			break;
395 
396 		  case CMDQUIT:		/* quit -- leave mail */
397 			message("221", "%s closing connection", HostName);
398 			if (InChild)
399 				ExitStat = EX_QUIT;
400 			finis();
401 
402 		  case CMDVERB:		/* set verbose mode */
403 			Verbose = TRUE;
404 			message("200", "Verbose mode");
405 			break;
406 
407 		  case CMDONEX:		/* doing one transaction only */
408 			OneXact = TRUE;
409 			message("200", "Only one transaction");
410 			break;
411 
412 # ifdef DEBUG
413 		  case CMDDBGQSHOW:	/* show queues */
414 			printf("Send Queue=");
415 			printaddr(CurEnv->e_sendqueue, TRUE);
416 			break;
417 
418 		  case CMDDBGDEBUG:	/* set debug mode */
419 			tTsetup(tTdvect, sizeof tTdvect, "0-99.1");
420 			tTflag(p);
421 			message("200", "Debug set");
422 			break;
423 # endif DEBUG
424 
425 # ifdef WIZ
426 		  case CMDDBGKILL:	/* kill the parent */
427 			if (!iswiz())
428 				break;
429 			if (kill(MotherPid, SIGTERM) >= 0)
430 				message("200", "Mother is dead");
431 			else
432 				message("500", "Can't kill Mom");
433 			break;
434 
435 		  case CMDDBGWIZ:	/* become a wizard */
436 			if (WizWord != NULL)
437 			{
438 				char seed[3];
439 				extern char *crypt();
440 
441 				(void) strncpy(seed, WizWord, 2);
442 				if (strcmp(WizWord, crypt(p, seed)) == 0)
443 				{
444 					IsWiz = TRUE;
445 					message("200", "Please pass, oh mighty wizard");
446 					break;
447 				}
448 			}
449 			message("500", "You are no wizard!");
450 			break;
451 
452 # else WIZ
453 		  case CMDDBGWIZ:	/* try to become a wizard */
454 			message("500", "You wascal wabbit!  Wandering wizards won't win!");
455 			break;
456 # endif WIZ
457 
458 		  case CMDERROR:	/* unknown command */
459 			message("500", "Command unrecognized");
460 			break;
461 
462 		  default:
463 			syserr("smtp: unknown code %d", c->cmdcode);
464 			break;
465 		}
466 	}
467 }
468 /*
469 **  SKIPWORD -- skip a fixed word.
470 **
471 **	Parameters:
472 **		p -- place to start looking.
473 **		w -- word to skip.
474 **
475 **	Returns:
476 **		p following w.
477 **		NULL on error.
478 **
479 **	Side Effects:
480 **		clobbers the p data area.
481 */
482 
483 static char *
484 skipword(p, w)
485 	register char *p;
486 	char *w;
487 {
488 	register char *q;
489 	extern bool sameword();
490 
491 	/* find beginning of word */
492 	while (isspace(*p))
493 		p++;
494 	q = p;
495 
496 	/* find end of word */
497 	while (*p != '\0' && *p != ':' && !isspace(*p))
498 		p++;
499 	while (isspace(*p))
500 		*p++ = '\0';
501 	if (*p != ':')
502 	{
503 	  syntax:
504 		message("501", "Syntax error");
505 		Errors++;
506 		return (NULL);
507 	}
508 	*p++ = '\0';
509 	while (isspace(*p))
510 		p++;
511 
512 	/* see if the input word matches desired word */
513 	if (!sameword(q, w))
514 		goto syntax;
515 
516 	return (p);
517 }
518 /*
519 **  HELP -- implement the HELP command.
520 **
521 **	Parameters:
522 **		topic -- the topic we want help for.
523 **
524 **	Returns:
525 **		none.
526 **
527 **	Side Effects:
528 **		outputs the help file to message output.
529 */
530 
531 help(topic)
532 	char *topic;
533 {
534 	register FILE *hf;
535 	int len;
536 	char buf[MAXLINE];
537 	bool noinfo;
538 
539 	if (HelpFile == NULL || (hf = fopen(HelpFile, "r")) == NULL)
540 	{
541 		/* no help */
542 		errno = 0;
543 		message("502", "HELP not implemented");
544 		return;
545 	}
546 
547 	len = strlen(topic);
548 	makelower(topic);
549 	noinfo = TRUE;
550 
551 	while (fgets(buf, sizeof buf, hf) != NULL)
552 	{
553 		if (strncmp(buf, topic, len) == 0)
554 		{
555 			register char *p;
556 
557 			p = index(buf, '\t');
558 			if (p == NULL)
559 				p = buf;
560 			else
561 				p++;
562 			fixcrlf(p, TRUE);
563 			message("214-", p);
564 			noinfo = FALSE;
565 		}
566 	}
567 
568 	if (noinfo)
569 		message("504", "HELP topic unknown");
570 	else
571 		message("214", "End of HELP info");
572 	(void) fclose(hf);
573 }
574 /*
575 **  ISWIZ -- tell us if we are a wizard
576 **
577 **	If not, print a nasty message.
578 **
579 **	Parameters:
580 **		none.
581 **
582 **	Returns:
583 **		TRUE if we are a wizard.
584 **		FALSE if we are not a wizard.
585 **
586 **	Side Effects:
587 **		Prints a 500 exit stat if we are not a wizard.
588 */
589 
590 #ifdef WIZ
591 
592 bool
593 iswiz()
594 {
595 	if (!IsWiz)
596 		message("500", "Mere mortals musn't mutter that mantra");
597 	return (IsWiz);
598 }
599 
600 #endif WIZ
601 /*
602 **  RUNINCHILD -- return twice -- once in the child, then in the parent again
603 **
604 **	Parameters:
605 **		label -- a string used in error messages
606 **
607 **	Returns:
608 **		zero in the child
609 **		one in the parent
610 **
611 **	Side Effects:
612 **		none.
613 */
614 
615 runinchild(label)
616 	char *label;
617 {
618 	int childpid;
619 
620 	if (!OneXact)
621 	{
622 		childpid = dofork();
623 		if (childpid < 0)
624 		{
625 			syserr("%s: cannot fork", label);
626 			return (1);
627 		}
628 		if (childpid > 0)
629 		{
630 			auto int st;
631 
632 			/* parent -- wait for child to complete */
633 			st = waitfor(childpid);
634 			if (st == -1)
635 				syserr("%s: lost child", label);
636 
637 			/* if we exited on a QUIT command, complete the process */
638 			if (st == (EX_QUIT << 8))
639 				finis();
640 
641 			return (1);
642 		}
643 		else
644 		{
645 			/* child */
646 			InChild = TRUE;
647 			clearenvelope(CurEnv);
648 		}
649 	}
650 
651 	/* open alias database */
652 	initaliases(AliasFile, FALSE);
653 
654 	return (0);
655 }
656 
657 # endif SMTP
658