122711Sdist /*
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  */
822711Sdist 
922711Sdist #ifndef lint
10*57438Seric static char sccsid[] = "@(#)savemail.c	6.2 (Berkeley) 01/09/93";
1133731Sbostic #endif /* not lint */
1222711Sdist 
1336928Sbostic # include <sys/types.h>
14297Seric # include <pwd.h>
153313Seric # include "sendmail.h"
16297Seric 
17297Seric /*
18297Seric **  SAVEMAIL -- Save mail on error
19297Seric **
209375Seric **	If mailing back errors, mail it back to the originator
21297Seric **	together with an error message; otherwise, just put it in
22297Seric **	dead.letter in the user's home directory (if he exists on
23297Seric **	this machine).
24297Seric **
25297Seric **	Parameters:
269337Seric **		e -- the envelope containing the message in error.
27297Seric **
28297Seric **	Returns:
29297Seric **		none
30297Seric **
31297Seric **	Side Effects:
32297Seric **		Saves the letter, by writing or mailing it back to the
33297Seric **		sender, or by putting it in dead.letter in her home
34297Seric **		directory.
35297Seric */
36297Seric 
3724942Seric /* defines for state machine */
3824942Seric # define ESM_REPORT	0	/* report to sender's terminal */
3924942Seric # define ESM_MAIL	1	/* mail back to sender */
4024942Seric # define ESM_QUIET	2	/* messages have already been returned */
4124942Seric # define ESM_DEADLETTER	3	/* save in ~/dead.letter */
4224942Seric # define ESM_POSTMASTER	4	/* return to postmaster */
4324942Seric # define ESM_USRTMP	5	/* save in /usr/tmp/dead.letter */
4424942Seric # define ESM_PANIC	6	/* leave the locked queue/transcript files */
4524942Seric # define ESM_DONE	7	/* the message is successfully delivered */
4624942Seric 
4724942Seric 
489337Seric savemail(e)
499337Seric 	register ENVELOPE *e;
50297Seric {
51297Seric 	register struct passwd *pw;
5224942Seric 	register FILE *fp;
5324942Seric 	int state;
5424942Seric 	auto ADDRESS *q;
55297Seric 	char buf[MAXLINE+1];
56297Seric 	extern struct passwd *getpwnam();
57297Seric 	register char *p;
58297Seric 	extern char *ttypath();
595846Seric 	typedef int (*fnptr)();
60297Seric 
617676Seric 	if (tTd(6, 1))
6224979Seric 		printf("\nsavemail, ErrorMode = %c\n", ErrorMode);
637361Seric 
649375Seric 	if (bitset(EF_RESPONSE, e->e_flags))
65297Seric 		return;
667053Seric 	ForceMail = TRUE;
679337Seric 	e->e_flags &= ~EF_FATALERRS;
68297Seric 
69297Seric 	/*
70297Seric 	**  In the unhappy event we don't know who to return the mail
71297Seric 	**  to, make someone up.
72297Seric 	*/
73297Seric 
749337Seric 	if (e->e_from.q_paddr == NULL)
75297Seric 	{
7655012Seric 		if (parseaddr("root", &e->e_from, 0, '\0', e) == NULL)
77297Seric 		{
78297Seric 			syserr("Cannot parse root!");
79297Seric 			ExitStat = EX_SOFTWARE;
80297Seric 			finis();
81297Seric 		}
82297Seric 	}
839337Seric 	e->e_to = NULL;
84297Seric 
85297Seric 	/*
8624942Seric 	**  Basic state machine.
8724942Seric 	**
8824942Seric 	**	This machine runs through the following states:
8924942Seric 	**
9024942Seric 	**	ESM_QUIET	Errors have already been printed iff the
9124942Seric 	**			sender is local.
9224942Seric 	**	ESM_REPORT	Report directly to the sender's terminal.
9324942Seric 	**	ESM_MAIL	Mail response to the sender.
9424942Seric 	**	ESM_DEADLETTER	Save response in ~/dead.letter.
9524942Seric 	**	ESM_POSTMASTER	Mail response to the postmaster.
9624942Seric 	**	ESM_PANIC	Save response anywhere possible.
97297Seric 	*/
98297Seric 
9924942Seric 	/* determine starting state */
10024942Seric 	switch (ErrorMode)
101297Seric 	{
10224942Seric 	  case EM_WRITE:
10324942Seric 		state = ESM_REPORT;
10424942Seric 		break;
10524942Seric 
10624942Seric 	  case EM_BERKNET:
10724942Seric 		/* mail back, but return o.k. exit status */
108401Seric 		ExitStat = EX_OK;
10924942Seric 
11024942Seric 		/* fall through.... */
11124942Seric 
11224942Seric 	  case EM_MAIL:
11324942Seric 		state = ESM_MAIL;
11424942Seric 		break;
11524942Seric 
11624942Seric 	  case EM_PRINT:
11724979Seric 	  case '\0':
11824942Seric 		state = ESM_QUIET;
11924942Seric 		break;
12024942Seric 
12124942Seric 	  case EM_QUIET:
12224942Seric 		/* no need to return anything at all */
12324942Seric 		return;
12424979Seric 
12524979Seric 	  default:
12624979Seric 		syserr("savemail: ErrorMode x%x\n");
12724979Seric 		state = ESM_MAIL;
12824979Seric 		break;
129297Seric 	}
130297Seric 
13124942Seric 	while (state != ESM_DONE)
132297Seric 	{
13324979Seric 		if (tTd(6, 5))
13424979Seric 			printf("  state %d\n", state);
13524979Seric 
13624942Seric 		switch (state)
137297Seric 		{
13824979Seric 		  case ESM_QUIET:
13924979Seric 			if (e->e_from.q_mailer == LocalMailer)
14024979Seric 				state = ESM_DEADLETTER;
14124979Seric 			else
14224979Seric 				state = ESM_MAIL;
14324979Seric 			break;
14424979Seric 
14524942Seric 		  case ESM_REPORT:
14624942Seric 
14724942Seric 			/*
14824942Seric 			**  If the user is still logged in on the same terminal,
14924942Seric 			**  then write the error messages back to hir (sic).
15024942Seric 			*/
15124942Seric 
15224942Seric 			p = ttypath();
15324942Seric 			if (p == NULL || freopen(p, "w", stdout) == NULL)
15424942Seric 			{
15524942Seric 				state = ESM_MAIL;
15624942Seric 				break;
15724942Seric 			}
15824942Seric 
15916152Seric 			expand("\001n", buf, &buf[sizeof buf - 1], e);
1609375Seric 			printf("\r\nMessage from %s...\r\n", buf);
1619375Seric 			printf("Errors occurred while sending mail.\r\n");
1629542Seric 			if (e->e_xfp != NULL)
1639375Seric 			{
1649542Seric 				(void) fflush(e->e_xfp);
16524942Seric 				fp = fopen(queuename(e, 'x'), "r");
1669375Seric 			}
1679375Seric 			else
16824942Seric 				fp = NULL;
16924942Seric 			if (fp == NULL)
1709375Seric 			{
1719337Seric 				syserr("Cannot open %s", queuename(e, 'x'));
1729375Seric 				printf("Transcript of session is unavailable.\r\n");
1739375Seric 			}
1749375Seric 			else
1759375Seric 			{
1769375Seric 				printf("Transcript follows:\r\n");
17724942Seric 				while (fgets(buf, sizeof buf, fp) != NULL &&
1789375Seric 				       !ferror(stdout))
1799375Seric 					fputs(buf, stdout);
18024942Seric 				(void) fclose(fp);
1819375Seric 			}
18224942Seric 			printf("Original message will be saved in dead.letter.\r\n");
18324942Seric 			state = ESM_DEADLETTER;
18424942Seric 			break;
185297Seric 
18624942Seric 		  case ESM_MAIL:
18724942Seric 		  case ESM_POSTMASTER:
18824942Seric 			/*
18924942Seric 			**  If mailing back, do it.
19024942Seric 			**	Throw away all further output.  Don't alias,
19124942Seric 			**	since this could cause loops, e.g., if joe
19224942Seric 			**	mails to joe@x, and for some reason the network
19324942Seric 			**	for @x is down, then the response gets sent to
19424942Seric 			**	joe@x, which gives a response, etc.  Also force
19524942Seric 			**	the mail to be delivered even if a version of
19624942Seric 			**	it has already been sent to the sender.
19724942Seric 			*/
198297Seric 
19924942Seric 			if (state == ESM_MAIL)
20024942Seric 			{
20124942Seric 				if (e->e_errorqueue == NULL)
20224942Seric 					sendtolist(e->e_from.q_paddr,
20324942Seric 						(ADDRESS *) NULL,
20455012Seric 						&e->e_errorqueue, e);
20524981Seric 
20624981Seric 				/* deliver a cc: to the postmaster if desired */
20724981Seric 				if (PostMasterCopy != NULL)
20824981Seric 					sendtolist(PostMasterCopy,
20924981Seric 						(ADDRESS *) NULL,
21055012Seric 						&e->e_errorqueue, e);
21124942Seric 				q = e->e_errorqueue;
21224942Seric 			}
21324942Seric 			else
21424942Seric 			{
21555012Seric 				if (parseaddr("postmaster", q, 0, '\0', e) == NULL)
21624942Seric 				{
21724942Seric 					syserr("cannot parse postmaster!");
21824942Seric 					ExitStat = EX_SOFTWARE;
21924942Seric 					state = ESM_USRTMP;
22024942Seric 					break;
22124942Seric 				}
22224942Seric 			}
22324942Seric 			if (returntosender(e->e_message != NULL ? e->e_message :
22424942Seric 					   "Unable to deliver mail",
225*57438Seric 					   q, (e->e_class >= 0), e) == 0)
22624942Seric 			{
22724942Seric 				state = ESM_DONE;
22824942Seric 				break;
22924942Seric 			}
230297Seric 
23124942Seric 			state = state == ESM_MAIL ? ESM_POSTMASTER : ESM_USRTMP;
23224942Seric 			break;
233297Seric 
23424942Seric 		  case ESM_DEADLETTER:
23524942Seric 			/*
23624942Seric 			**  Save the message in dead.letter.
23724942Seric 			**	If we weren't mailing back, and the user is
23824942Seric 			**	local, we should save the message in
23924942Seric 			**	~/dead.letter so that the poor person doesn't
24024942Seric 			**	have to type it over again -- and we all know
24124942Seric 			**	what poor typists UNIX users are.
24224942Seric 			*/
2435315Seric 
24424942Seric 			p = NULL;
24524942Seric 			if (e->e_from.q_mailer == LocalMailer)
24624942Seric 			{
24724942Seric 				if (e->e_from.q_home != NULL)
24824942Seric 					p = e->e_from.q_home;
24924942Seric 				else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
25024942Seric 					p = pw->pw_dir;
25124942Seric 			}
25224942Seric 			if (p == NULL)
25324942Seric 			{
25424942Seric 				syserr("Can't return mail to %s", e->e_from.q_paddr);
25524942Seric 				state = ESM_MAIL;
25624942Seric 				break;
25724942Seric 			}
25824942Seric 			if (e->e_dfp != NULL)
25924942Seric 			{
26024942Seric 				auto ADDRESS *q;
26124942Seric 				bool oldverb = Verbose;
26224942Seric 
26324942Seric 				/* we have a home directory; open dead.letter */
26424942Seric 				define('z', p, e);
26524942Seric 				expand("\001z/dead.letter", buf, &buf[sizeof buf - 1], e);
26624942Seric 				Verbose = TRUE;
26724942Seric 				message(Arpa_Info, "Saving message in %s", buf);
26824942Seric 				Verbose = oldverb;
26924942Seric 				e->e_to = buf;
27024942Seric 				q = NULL;
27155012Seric 				sendtolist(buf, (ADDRESS *) NULL, &q, e);
27224942Seric 				if (deliver(e, q) == 0)
27324942Seric 					state = ESM_DONE;
27424942Seric 				else
27524942Seric 					state = ESM_MAIL;
27624942Seric 			}
27725569Seric 			else
27825569Seric 			{
27925569Seric 				/* no data file -- try mailing back */
28025569Seric 				state = ESM_MAIL;
28125569Seric 			}
28224942Seric 			break;
28324942Seric 
28424942Seric 		  case ESM_USRTMP:
28524942Seric 			/*
28624942Seric 			**  Log the mail in /usr/tmp/dead.letter.
28724942Seric 			*/
28824942Seric 
289*57438Seric 			if (e->e_class < 0)
290*57438Seric 			{
291*57438Seric 				state = ESM_DONE;
292*57438Seric 				break;
293*57438Seric 			}
294*57438Seric 
29524942Seric 			fp = dfopen("/usr/tmp/dead.letter", "a");
29624942Seric 			if (fp == NULL)
29724942Seric 			{
29824942Seric 				state = ESM_PANIC;
29924942Seric 				break;
30024942Seric 			}
30124942Seric 
30254967Seric 			putfromline(fp, ProgMailer, e);
30324942Seric 			(*e->e_puthdr)(fp, ProgMailer, e);
30424942Seric 			putline("\n", fp, ProgMailer);
30524942Seric 			(*e->e_putbody)(fp, ProgMailer, e);
30624942Seric 			putline("\n", fp, ProgMailer);
30724942Seric 			(void) fflush(fp);
30824942Seric 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
30924942Seric 			(void) fclose(fp);
31024942Seric 			break;
31124942Seric 
31224942Seric 		  default:
31324942Seric 			syserr("savemail: unknown state %d", state);
31424942Seric 
31524942Seric 			/* fall through ... */
31624942Seric 
31724942Seric 		  case ESM_PANIC:
31824942Seric 			/* leave the locked queue & transcript files around */
319*57438Seric 			syserr("savemail: cannot save rejected email anywhere");
32024942Seric 			exit(EX_SOFTWARE);
32124942Seric 		}
322297Seric 	}
323297Seric }
324297Seric /*
3254633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
3264633Seric **
3274633Seric **	Parameters:
3284633Seric **		msg -- the explanatory message.
32916479Seric **		returnq -- the queue of people to send the message to.
3305984Seric **		sendbody -- if TRUE, also send back the body of the
3315984Seric **			message; otherwise just send the header.
33255012Seric **		e -- the current envelope.
3334633Seric **
3344633Seric **	Returns:
3354633Seric **		zero -- if everything went ok.
3364633Seric **		else -- some error.
3374633Seric **
3384633Seric **	Side Effects:
3394633Seric **		Returns the current message to the sender via
3404633Seric **		mail.
3414633Seric */
3424633Seric 
3435984Seric static bool	SendBody;
3444633Seric 
3457045Seric #define MAXRETURNS	6	/* max depth of returning messages */
3467045Seric 
34755012Seric returntosender(msg, returnq, sendbody, e)
3484633Seric 	char *msg;
34916479Seric 	ADDRESS *returnq;
3505984Seric 	bool sendbody;
35155012Seric 	register ENVELOPE *e;
3524633Seric {
3534633Seric 	char buf[MAXNAME];
3546978Seric 	extern putheader(), errbody();
3556978Seric 	register ENVELOPE *ee;
3566978Seric 	extern ENVELOPE *newenvelope();
3576978Seric 	ENVELOPE errenvelope;
3587045Seric 	static int returndepth;
3599375Seric 	register ADDRESS *q;
3604633Seric 
3617676Seric 	if (tTd(6, 1))
3627287Seric 	{
36355012Seric 		printf("Return To Sender: msg=\"%s\", depth=%d, e=%x,\n",
36455012Seric 		       msg, returndepth, e);
36524942Seric 		printf("\treturnq=");
36616479Seric 		printaddr(returnq, TRUE);
3677287Seric 	}
3687287Seric 
3697045Seric 	if (++returndepth >= MAXRETURNS)
3707045Seric 	{
3717045Seric 		if (returndepth != MAXRETURNS)
37216479Seric 			syserr("returntosender: infinite recursion on %s", returnq->q_paddr);
3737045Seric 		/* don't "unrecurse" and fake a clean exit */
3747045Seric 		/* returndepth--; */
3757045Seric 		return (0);
3767045Seric 	}
3777045Seric 
3785984Seric 	SendBody = sendbody;
37955012Seric 	define('g', "\001f", e);
38055012Seric 	define('<', "\001f", e);
3816978Seric 	ee = newenvelope(&errenvelope);
38224942Seric 	define('a', "\001b", ee);
3836978Seric 	ee->e_puthdr = putheader;
3846978Seric 	ee->e_putbody = errbody;
3859375Seric 	ee->e_flags |= EF_RESPONSE;
38655012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
38745155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
38816479Seric 	ee->e_sendqueue = returnq;
3899542Seric 	openxscript(ee);
39016479Seric 	for (q = returnq; q != NULL; q = q->q_next)
3919375Seric 	{
3929375Seric 		if (q->q_alias == NULL)
3939375Seric 			addheader("to", q->q_paddr, ee);
3949375Seric 	}
39524942Seric 
39610845Seric 	(void) sprintf(buf, "Returned mail: %s", msg);
39710106Seric 	addheader("subject", buf, ee);
3984633Seric 
3994633Seric 	/* fake up an address header for the from person */
40055012Seric 	expand("\001n", buf, &buf[sizeof buf - 1], e);
40153181Seric 	ee->e_sender = ee->e_returnpath = newstr(buf);
40255012Seric 	if (parseaddr(buf, &ee->e_from, -1, '\0', e) == NULL)
4034633Seric 	{
4044633Seric 		syserr("Can't parse myself!");
4054633Seric 		ExitStat = EX_SOFTWARE;
4067045Seric 		returndepth--;
4074633Seric 		return (-1);
4084633Seric 	}
40916159Seric 	loweraddr(&ee->e_from);
4105984Seric 
4116978Seric 	/* push state into submessage */
4126978Seric 	CurEnv = ee;
41316152Seric 	define('f', "\001n", ee);
4149375Seric 	define('x', "Mail Delivery Subsystem", ee);
41511291Seric 	eatheader(ee);
4165984Seric 
4176978Seric 	/* actually deliver the error message */
41814876Seric 	sendall(ee, SM_DEFAULT);
4196978Seric 
4206978Seric 	/* restore state */
4217811Seric 	dropenvelope(ee);
4226978Seric 	CurEnv = CurEnv->e_parent;
4237045Seric 	returndepth--;
4246978Seric 
4257045Seric 	/* should check for delivery errors here */
4264633Seric 	return (0);
4274633Seric }
4284633Seric /*
4296978Seric **  ERRBODY -- output the body of an error message.
4306978Seric **
4316978Seric **	Typically this is a copy of the transcript plus a copy of the
4326978Seric **	original offending message.
4336978Seric **
434297Seric **	Parameters:
435297Seric **		fp -- the output file.
43610170Seric **		m -- the mailer to output to.
4379542Seric **		e -- the envelope we are working in.
438297Seric **
439297Seric **	Returns:
440297Seric **		none
441297Seric **
442297Seric **	Side Effects:
4436978Seric **		Outputs the body of an error message.
444297Seric */
445297Seric 
44610170Seric errbody(fp, m, e)
447297Seric 	register FILE *fp;
4484318Seric 	register struct mailer *m;
4499542Seric 	register ENVELOPE *e;
450297Seric {
4516978Seric 	register FILE *xfile;
4523189Seric 	char buf[MAXLINE];
4539337Seric 	char *p;
454297Seric 
4559057Seric 	/*
45655372Seric 	**  Output error message header (if specified and available).
45755372Seric 	*/
45855372Seric 
45955372Seric 	if (ErrMsgFile != NULL)
46055372Seric 	{
46155372Seric 		if (*ErrMsgFile == '/')
46255372Seric 		{
46355372Seric 			xfile = fopen(ErrMsgFile, "r");
46455372Seric 			if (xfile != NULL)
46555372Seric 			{
46655372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
46755425Seric 				{
46855425Seric 					expand(buf, buf, &buf[sizeof buf - 1], e);
46955372Seric 					putline(buf, fp, m);
47055425Seric 				}
47155372Seric 				(void) fclose(xfile);
47255372Seric 				fprintf(fp, "\n");
47355372Seric 			}
47455372Seric 		}
47555372Seric 		else
47655372Seric 		{
47755425Seric 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
47855425Seric 			putline(buf, fp, m);
47955372Seric 			fprintf(fp, "\n");
48055372Seric 		}
48155372Seric 	}
48255372Seric 
48355372Seric 	/*
4849057Seric 	**  Output transcript of errors
4859057Seric 	*/
4869057Seric 
4874086Seric 	(void) fflush(stdout);
4889542Seric 	p = queuename(e->e_parent, 'x');
4899337Seric 	if ((xfile = fopen(p, "r")) == NULL)
4909057Seric 	{
4919337Seric 		syserr("Cannot open %s", p);
4929057Seric 		fprintf(fp, "  ----- Transcript of session is unavailable -----\n");
4939057Seric 	}
4949057Seric 	else
4959057Seric 	{
4969057Seric 		fprintf(fp, "   ----- Transcript of session follows -----\n");
4979542Seric 		if (e->e_xfp != NULL)
4989542Seric 			(void) fflush(e->e_xfp);
4999057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
50010170Seric 			putline(buf, fp, m);
5019057Seric 		(void) fclose(xfile);
5029057Seric 	}
503297Seric 	errno = 0;
5044318Seric 
5054318Seric 	/*
5064318Seric 	**  Output text of original message
5074318Seric 	*/
5084318Seric 
5094289Seric 	if (NoReturn)
5104289Seric 		fprintf(fp, "\n   ----- Return message suppressed -----\n\n");
5119542Seric 	else if (e->e_parent->e_dfp != NULL)
5124199Seric 	{
5135984Seric 		if (SendBody)
5145984Seric 		{
51510170Seric 			putline("\n", fp, m);
51610170Seric 			putline("   ----- Unsent message follows -----\n", fp, m);
5175984Seric 			(void) fflush(fp);
51810170Seric 			putheader(fp, m, e->e_parent);
51910170Seric 			putline("\n", fp, m);
52010170Seric 			putbody(fp, m, e->e_parent);
5215984Seric 		}
5225984Seric 		else
5235984Seric 		{
52410170Seric 			putline("\n", fp, m);
52510170Seric 			putline("  ----- Message header follows -----\n", fp, m);
5265984Seric 			(void) fflush(fp);
52710170Seric 			putheader(fp, m, e->e_parent);
5285984Seric 		}
5294199Seric 	}
5304199Seric 	else
53110170Seric 	{
53210170Seric 		putline("\n", fp, m);
53310170Seric 		putline("  ----- No message was collected -----\n", fp, m);
53410170Seric 		putline("\n", fp, m);
53510170Seric 	}
5364318Seric 
5374318Seric 	/*
5384318Seric 	**  Cleanup and exit
5394318Seric 	*/
5404318Seric 
541297Seric 	if (errno != 0)
5426978Seric 		syserr("errbody: I/O error");
543297Seric }
544