122711Sdist /*
2*33731Sbostic  * Copyright (c) 1988 Regents of the University of California.
3*33731Sbostic  * All rights reserved.
4*33731Sbostic  *
5*33731Sbostic  * Redistribution and use in source and binary forms are permitted
6*33731Sbostic  * provided that this notice is preserved and that due credit is given
7*33731Sbostic  * to the University of California at Berkeley. The name of the University
8*33731Sbostic  * may not be used to endorse or promote products derived from this
9*33731Sbostic  * software without specific prior written permission. This software
10*33731Sbostic  * is provided ``as is'' without express or implied warranty.
11*33731Sbostic  *
12*33731Sbostic  *  Sendmail
13*33731Sbostic  *  Copyright (c) 1983  Eric P. Allman
14*33731Sbostic  *  Berkeley, California
15*33731Sbostic  */
1622711Sdist 
1722711Sdist #ifndef lint
18*33731Sbostic static char sccsid[] = "@(#)savemail.c	5.8 (Berkeley) 03/13/88";
19*33731Sbostic #endif /* not lint */
2022711Sdist 
21297Seric # include <pwd.h>
223313Seric # include "sendmail.h"
23297Seric 
24297Seric /*
25297Seric **  SAVEMAIL -- Save mail on error
26297Seric **
279375Seric **	If mailing back errors, mail it back to the originator
28297Seric **	together with an error message; otherwise, just put it in
29297Seric **	dead.letter in the user's home directory (if he exists on
30297Seric **	this machine).
31297Seric **
32297Seric **	Parameters:
339337Seric **		e -- the envelope containing the message in error.
34297Seric **
35297Seric **	Returns:
36297Seric **		none
37297Seric **
38297Seric **	Side Effects:
39297Seric **		Saves the letter, by writing or mailing it back to the
40297Seric **		sender, or by putting it in dead.letter in her home
41297Seric **		directory.
42297Seric */
43297Seric 
4424942Seric /* defines for state machine */
4524942Seric # define ESM_REPORT	0	/* report to sender's terminal */
4624942Seric # define ESM_MAIL	1	/* mail back to sender */
4724942Seric # define ESM_QUIET	2	/* messages have already been returned */
4824942Seric # define ESM_DEADLETTER	3	/* save in ~/dead.letter */
4924942Seric # define ESM_POSTMASTER	4	/* return to postmaster */
5024942Seric # define ESM_USRTMP	5	/* save in /usr/tmp/dead.letter */
5124942Seric # define ESM_PANIC	6	/* leave the locked queue/transcript files */
5224942Seric # define ESM_DONE	7	/* the message is successfully delivered */
5324942Seric 
5424942Seric 
559337Seric savemail(e)
569337Seric 	register ENVELOPE *e;
57297Seric {
58297Seric 	register struct passwd *pw;
5924942Seric 	register FILE *fp;
6024942Seric 	int state;
6124942Seric 	auto ADDRESS *q;
62297Seric 	char buf[MAXLINE+1];
63297Seric 	extern struct passwd *getpwnam();
64297Seric 	register char *p;
65297Seric 	extern char *ttypath();
665846Seric 	typedef int (*fnptr)();
67297Seric 
687361Seric # ifdef DEBUG
697676Seric 	if (tTd(6, 1))
7024979Seric 		printf("\nsavemail, ErrorMode = %c\n", ErrorMode);
717361Seric # endif DEBUG
727361Seric 
739375Seric 	if (bitset(EF_RESPONSE, e->e_flags))
74297Seric 		return;
759337Seric 	if (e->e_class < 0)
766978Seric 	{
777053Seric 		message(Arpa_Info, "Dumping junk mail");
786978Seric 		return;
796978Seric 	}
807053Seric 	ForceMail = TRUE;
819337Seric 	e->e_flags &= ~EF_FATALERRS;
82297Seric 
83297Seric 	/*
84297Seric 	**  In the unhappy event we don't know who to return the mail
85297Seric 	**  to, make someone up.
86297Seric 	*/
87297Seric 
889337Seric 	if (e->e_from.q_paddr == NULL)
89297Seric 	{
9011447Seric 		if (parseaddr("root", &e->e_from, 0, '\0') == NULL)
91297Seric 		{
92297Seric 			syserr("Cannot parse root!");
93297Seric 			ExitStat = EX_SOFTWARE;
94297Seric 			finis();
95297Seric 		}
96297Seric 	}
979337Seric 	e->e_to = NULL;
98297Seric 
99297Seric 	/*
10024942Seric 	**  Basic state machine.
10124942Seric 	**
10224942Seric 	**	This machine runs through the following states:
10324942Seric 	**
10424942Seric 	**	ESM_QUIET	Errors have already been printed iff the
10524942Seric 	**			sender is local.
10624942Seric 	**	ESM_REPORT	Report directly to the sender's terminal.
10724942Seric 	**	ESM_MAIL	Mail response to the sender.
10824942Seric 	**	ESM_DEADLETTER	Save response in ~/dead.letter.
10924942Seric 	**	ESM_POSTMASTER	Mail response to the postmaster.
11024942Seric 	**	ESM_PANIC	Save response anywhere possible.
111297Seric 	*/
112297Seric 
11324942Seric 	/* determine starting state */
11424942Seric 	switch (ErrorMode)
115297Seric 	{
11624942Seric 	  case EM_WRITE:
11724942Seric 		state = ESM_REPORT;
11824942Seric 		break;
11924942Seric 
12024942Seric 	  case EM_BERKNET:
12124942Seric 		/* mail back, but return o.k. exit status */
122401Seric 		ExitStat = EX_OK;
12324942Seric 
12424942Seric 		/* fall through.... */
12524942Seric 
12624942Seric 	  case EM_MAIL:
12724942Seric 		state = ESM_MAIL;
12824942Seric 		break;
12924942Seric 
13024942Seric 	  case EM_PRINT:
13124979Seric 	  case '\0':
13224942Seric 		state = ESM_QUIET;
13324942Seric 		break;
13424942Seric 
13524942Seric 	  case EM_QUIET:
13624942Seric 		/* no need to return anything at all */
13724942Seric 		return;
13824979Seric 
13924979Seric 	  default:
14024979Seric 		syserr("savemail: ErrorMode x%x\n");
14124979Seric 		state = ESM_MAIL;
14224979Seric 		break;
143297Seric 	}
144297Seric 
14524942Seric 	while (state != ESM_DONE)
146297Seric 	{
14724979Seric # ifdef DEBUG
14824979Seric 		if (tTd(6, 5))
14924979Seric 			printf("  state %d\n", state);
15024979Seric # endif DEBUG
15124979Seric 
15224942Seric 		switch (state)
153297Seric 		{
15424979Seric 		  case ESM_QUIET:
15524979Seric 			if (e->e_from.q_mailer == LocalMailer)
15624979Seric 				state = ESM_DEADLETTER;
15724979Seric 			else
15824979Seric 				state = ESM_MAIL;
15924979Seric 			break;
16024979Seric 
16124942Seric 		  case ESM_REPORT:
16224942Seric 
16324942Seric 			/*
16424942Seric 			**  If the user is still logged in on the same terminal,
16524942Seric 			**  then write the error messages back to hir (sic).
16624942Seric 			*/
16724942Seric 
16824942Seric 			p = ttypath();
16924942Seric 			if (p == NULL || freopen(p, "w", stdout) == NULL)
17024942Seric 			{
17124942Seric 				state = ESM_MAIL;
17224942Seric 				break;
17324942Seric 			}
17424942Seric 
17516152Seric 			expand("\001n", buf, &buf[sizeof buf - 1], e);
1769375Seric 			printf("\r\nMessage from %s...\r\n", buf);
1779375Seric 			printf("Errors occurred while sending mail.\r\n");
1789542Seric 			if (e->e_xfp != NULL)
1799375Seric 			{
1809542Seric 				(void) fflush(e->e_xfp);
18124942Seric 				fp = fopen(queuename(e, 'x'), "r");
1829375Seric 			}
1839375Seric 			else
18424942Seric 				fp = NULL;
18524942Seric 			if (fp == NULL)
1869375Seric 			{
1879337Seric 				syserr("Cannot open %s", queuename(e, 'x'));
1889375Seric 				printf("Transcript of session is unavailable.\r\n");
1899375Seric 			}
1909375Seric 			else
1919375Seric 			{
1929375Seric 				printf("Transcript follows:\r\n");
19324942Seric 				while (fgets(buf, sizeof buf, fp) != NULL &&
1949375Seric 				       !ferror(stdout))
1959375Seric 					fputs(buf, stdout);
19624942Seric 				(void) fclose(fp);
1979375Seric 			}
19824942Seric 			printf("Original message will be saved in dead.letter.\r\n");
199297Seric 			if (ferror(stdout))
2004086Seric 				(void) syserr("savemail: stdout: write err");
20124942Seric 			state = ESM_DEADLETTER;
20224942Seric 			break;
203297Seric 
20424942Seric 		  case ESM_MAIL:
20524942Seric 		  case ESM_POSTMASTER:
20624942Seric 			/*
20724942Seric 			**  If mailing back, do it.
20824942Seric 			**	Throw away all further output.  Don't alias,
20924942Seric 			**	since this could cause loops, e.g., if joe
21024942Seric 			**	mails to joe@x, and for some reason the network
21124942Seric 			**	for @x is down, then the response gets sent to
21224942Seric 			**	joe@x, which gives a response, etc.  Also force
21324942Seric 			**	the mail to be delivered even if a version of
21424942Seric 			**	it has already been sent to the sender.
21524942Seric 			*/
216297Seric 
21724942Seric 			if (state == ESM_MAIL)
21824942Seric 			{
21924942Seric 				if (e->e_errorqueue == NULL)
22024942Seric 					sendtolist(e->e_from.q_paddr,
22124942Seric 						(ADDRESS *) NULL,
22224942Seric 						&e->e_errorqueue);
22324981Seric 
22424981Seric 				/* deliver a cc: to the postmaster if desired */
22524981Seric 				if (PostMasterCopy != NULL)
22624981Seric 					sendtolist(PostMasterCopy,
22724981Seric 						(ADDRESS *) NULL,
22824981Seric 						&e->e_errorqueue);
22924942Seric 				q = e->e_errorqueue;
23024942Seric 			}
23124942Seric 			else
23224942Seric 			{
23324955Seric 				if (parseaddr("postmaster", q, 0, '\0') == NULL)
23424942Seric 				{
23524942Seric 					syserr("cannot parse postmaster!");
23624942Seric 					ExitStat = EX_SOFTWARE;
23724942Seric 					state = ESM_USRTMP;
23824942Seric 					break;
23924942Seric 				}
24024942Seric 			}
24124942Seric 			if (returntosender(e->e_message != NULL ? e->e_message :
24224942Seric 					   "Unable to deliver mail",
24324942Seric 					   q, TRUE) == 0)
24424942Seric 			{
24524942Seric 				state = ESM_DONE;
24624942Seric 				break;
24724942Seric 			}
248297Seric 
24924942Seric 			state = state == ESM_MAIL ? ESM_POSTMASTER : ESM_USRTMP;
25024942Seric 			break;
251297Seric 
25224942Seric 		  case ESM_DEADLETTER:
25324942Seric 			/*
25424942Seric 			**  Save the message in dead.letter.
25524942Seric 			**	If we weren't mailing back, and the user is
25624942Seric 			**	local, we should save the message in
25724942Seric 			**	~/dead.letter so that the poor person doesn't
25824942Seric 			**	have to type it over again -- and we all know
25924942Seric 			**	what poor typists UNIX users are.
26024942Seric 			*/
2615315Seric 
26224942Seric 			p = NULL;
26324942Seric 			if (e->e_from.q_mailer == LocalMailer)
26424942Seric 			{
26524942Seric 				if (e->e_from.q_home != NULL)
26624942Seric 					p = e->e_from.q_home;
26724942Seric 				else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
26824942Seric 					p = pw->pw_dir;
26924942Seric 			}
27024942Seric 			if (p == NULL)
27124942Seric 			{
27224942Seric 				syserr("Can't return mail to %s", e->e_from.q_paddr);
27324942Seric 				state = ESM_MAIL;
27424942Seric 				break;
27524942Seric 			}
27624942Seric 			if (e->e_dfp != NULL)
27724942Seric 			{
27824942Seric 				auto ADDRESS *q;
27924942Seric 				bool oldverb = Verbose;
28024942Seric 
28124942Seric 				/* we have a home directory; open dead.letter */
28224942Seric 				define('z', p, e);
28324942Seric 				expand("\001z/dead.letter", buf, &buf[sizeof buf - 1], e);
28424942Seric 				Verbose = TRUE;
28524942Seric 				message(Arpa_Info, "Saving message in %s", buf);
28624942Seric 				Verbose = oldverb;
28724942Seric 				e->e_to = buf;
28824942Seric 				q = NULL;
28924942Seric 				sendtolist(buf, (ADDRESS *) NULL, &q);
29024942Seric 				if (deliver(e, q) == 0)
29124942Seric 					state = ESM_DONE;
29224942Seric 				else
29324942Seric 					state = ESM_MAIL;
29424942Seric 			}
29525569Seric 			else
29625569Seric 			{
29725569Seric 				/* no data file -- try mailing back */
29825569Seric 				state = ESM_MAIL;
29925569Seric 			}
30024942Seric 			break;
30124942Seric 
30224942Seric 		  case ESM_USRTMP:
30324942Seric 			/*
30424942Seric 			**  Log the mail in /usr/tmp/dead.letter.
30524942Seric 			*/
30624942Seric 
30724942Seric 			fp = dfopen("/usr/tmp/dead.letter", "a");
30824942Seric 			if (fp == NULL)
30924942Seric 			{
31024942Seric 				state = ESM_PANIC;
31124942Seric 				break;
31224942Seric 			}
31324942Seric 
31424942Seric 			putfromline(fp, ProgMailer);
31524942Seric 			(*e->e_puthdr)(fp, ProgMailer, e);
31624942Seric 			putline("\n", fp, ProgMailer);
31724942Seric 			(*e->e_putbody)(fp, ProgMailer, e);
31824942Seric 			putline("\n", fp, ProgMailer);
31924942Seric 			(void) fflush(fp);
32024942Seric 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
32124942Seric 			(void) fclose(fp);
32224942Seric 			break;
32324942Seric 
32424942Seric 		  default:
32524942Seric 			syserr("savemail: unknown state %d", state);
32624942Seric 
32724942Seric 			/* fall through ... */
32824942Seric 
32924942Seric 		  case ESM_PANIC:
33024942Seric 			syserr("savemail: HELP!!!!");
33124942Seric # ifdef LOG
33224942Seric 			if (LogLevel >= 1)
33324942Seric 				syslog(LOG_ALERT, "savemail: HELP!!!!");
33424942Seric # endif LOG
33524942Seric 
33624942Seric 			/* leave the locked queue & transcript files around */
33724942Seric 			exit(EX_SOFTWARE);
33824942Seric 		}
339297Seric 	}
340297Seric }
341297Seric /*
3424633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
3434633Seric **
3444633Seric **	Parameters:
3454633Seric **		msg -- the explanatory message.
34616479Seric **		returnq -- the queue of people to send the message to.
3475984Seric **		sendbody -- if TRUE, also send back the body of the
3485984Seric **			message; otherwise just send the header.
3494633Seric **
3504633Seric **	Returns:
3514633Seric **		zero -- if everything went ok.
3524633Seric **		else -- some error.
3534633Seric **
3544633Seric **	Side Effects:
3554633Seric **		Returns the current message to the sender via
3564633Seric **		mail.
3574633Seric */
3584633Seric 
3595984Seric static bool	SendBody;
3604633Seric 
3617045Seric #define MAXRETURNS	6	/* max depth of returning messages */
3627045Seric 
36316479Seric returntosender(msg, returnq, sendbody)
3644633Seric 	char *msg;
36516479Seric 	ADDRESS *returnq;
3665984Seric 	bool sendbody;
3674633Seric {
3684633Seric 	char buf[MAXNAME];
3696978Seric 	extern putheader(), errbody();
3706978Seric 	register ENVELOPE *ee;
3716978Seric 	extern ENVELOPE *newenvelope();
3726978Seric 	ENVELOPE errenvelope;
3737045Seric 	static int returndepth;
3749375Seric 	register ADDRESS *q;
3754633Seric 
3767287Seric # ifdef DEBUG
3777676Seric 	if (tTd(6, 1))
3787287Seric 	{
3797287Seric 		printf("Return To Sender: msg=\"%s\", depth=%d, CurEnv=%x,\n",
3807287Seric 		       msg, returndepth, CurEnv);
38124942Seric 		printf("\treturnq=");
38216479Seric 		printaddr(returnq, TRUE);
3837287Seric 	}
3847287Seric # endif DEBUG
3857287Seric 
3867045Seric 	if (++returndepth >= MAXRETURNS)
3877045Seric 	{
3887045Seric 		if (returndepth != MAXRETURNS)
38916479Seric 			syserr("returntosender: infinite recursion on %s", returnq->q_paddr);
3907045Seric 		/* don't "unrecurse" and fake a clean exit */
3917045Seric 		/* returndepth--; */
3927045Seric 		return (0);
3937045Seric 	}
3947045Seric 
3955984Seric 	SendBody = sendbody;
39616152Seric 	define('g', "\001f", CurEnv);
3976978Seric 	ee = newenvelope(&errenvelope);
39824942Seric 	define('a', "\001b", ee);
3996978Seric 	ee->e_puthdr = putheader;
4006978Seric 	ee->e_putbody = errbody;
4019375Seric 	ee->e_flags |= EF_RESPONSE;
40216479Seric 	ee->e_sendqueue = returnq;
4039542Seric 	openxscript(ee);
40416479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4059375Seric 	{
4069375Seric 		if (q->q_alias == NULL)
4079375Seric 			addheader("to", q->q_paddr, ee);
4089375Seric 	}
40924942Seric 
41010845Seric 	(void) sprintf(buf, "Returned mail: %s", msg);
41110106Seric 	addheader("subject", buf, ee);
4124633Seric 
4134633Seric 	/* fake up an address header for the from person */
41416152Seric 	expand("\001n", buf, &buf[sizeof buf - 1], CurEnv);
41511447Seric 	if (parseaddr(buf, &ee->e_from, -1, '\0') == NULL)
4164633Seric 	{
4174633Seric 		syserr("Can't parse myself!");
4184633Seric 		ExitStat = EX_SOFTWARE;
4197045Seric 		returndepth--;
4204633Seric 		return (-1);
4214633Seric 	}
42216159Seric 	loweraddr(&ee->e_from);
4235984Seric 
4246978Seric 	/* push state into submessage */
4256978Seric 	CurEnv = ee;
42616152Seric 	define('f', "\001n", ee);
4279375Seric 	define('x', "Mail Delivery Subsystem", ee);
42811291Seric 	eatheader(ee);
4295984Seric 
4306978Seric 	/* actually deliver the error message */
43114876Seric 	sendall(ee, SM_DEFAULT);
4326978Seric 
4336978Seric 	/* restore state */
4347811Seric 	dropenvelope(ee);
4356978Seric 	CurEnv = CurEnv->e_parent;
4367045Seric 	returndepth--;
4376978Seric 
4387045Seric 	/* should check for delivery errors here */
4394633Seric 	return (0);
4404633Seric }
4414633Seric /*
4426978Seric **  ERRBODY -- output the body of an error message.
4436978Seric **
4446978Seric **	Typically this is a copy of the transcript plus a copy of the
4456978Seric **	original offending message.
4466978Seric **
447297Seric **	Parameters:
448297Seric **		fp -- the output file.
44910170Seric **		m -- the mailer to output to.
4509542Seric **		e -- the envelope we are working in.
451297Seric **
452297Seric **	Returns:
453297Seric **		none
454297Seric **
455297Seric **	Side Effects:
4566978Seric **		Outputs the body of an error message.
457297Seric */
458297Seric 
45910170Seric errbody(fp, m, e)
460297Seric 	register FILE *fp;
4614318Seric 	register struct mailer *m;
4629542Seric 	register ENVELOPE *e;
463297Seric {
4646978Seric 	register FILE *xfile;
4653189Seric 	char buf[MAXLINE];
4669337Seric 	char *p;
467297Seric 
4689057Seric 	/*
4699057Seric 	**  Output transcript of errors
4709057Seric 	*/
4719057Seric 
4724086Seric 	(void) fflush(stdout);
4739542Seric 	p = queuename(e->e_parent, 'x');
4749337Seric 	if ((xfile = fopen(p, "r")) == NULL)
4759057Seric 	{
4769337Seric 		syserr("Cannot open %s", p);
4779057Seric 		fprintf(fp, "  ----- Transcript of session is unavailable -----\n");
4789057Seric 	}
4799057Seric 	else
4809057Seric 	{
4819057Seric 		fprintf(fp, "   ----- Transcript of session follows -----\n");
4829542Seric 		if (e->e_xfp != NULL)
4839542Seric 			(void) fflush(e->e_xfp);
4849057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
48510170Seric 			putline(buf, fp, m);
4869057Seric 		(void) fclose(xfile);
4879057Seric 	}
488297Seric 	errno = 0;
4894318Seric 
4904318Seric 	/*
4914318Seric 	**  Output text of original message
4924318Seric 	*/
4934318Seric 
4944289Seric 	if (NoReturn)
4954289Seric 		fprintf(fp, "\n   ----- Return message suppressed -----\n\n");
4969542Seric 	else if (e->e_parent->e_dfp != NULL)
4974199Seric 	{
4985984Seric 		if (SendBody)
4995984Seric 		{
50010170Seric 			putline("\n", fp, m);
50110170Seric 			putline("   ----- Unsent message follows -----\n", fp, m);
5025984Seric 			(void) fflush(fp);
50310170Seric 			putheader(fp, m, e->e_parent);
50410170Seric 			putline("\n", fp, m);
50510170Seric 			putbody(fp, m, e->e_parent);
5065984Seric 		}
5075984Seric 		else
5085984Seric 		{
50910170Seric 			putline("\n", fp, m);
51010170Seric 			putline("  ----- Message header follows -----\n", fp, m);
5115984Seric 			(void) fflush(fp);
51210170Seric 			putheader(fp, m, e->e_parent);
5135984Seric 		}
5144199Seric 	}
5154199Seric 	else
51610170Seric 	{
51710170Seric 		putline("\n", fp, m);
51810170Seric 		putline("  ----- No message was collected -----\n", fp, m);
51910170Seric 		putline("\n", fp, m);
52010170Seric 	}
5214318Seric 
5224318Seric 	/*
5234318Seric 	**  Cleanup and exit
5244318Seric 	*/
5254318Seric 
526297Seric 	if (errno != 0)
5276978Seric 		syserr("errbody: I/O error");
528297Seric }
529