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*58082Seric static char sccsid[] = "@(#)savemail.c	6.7 (Berkeley) 02/20/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 
15958050Seric 			expand("\201n", 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)
202*58082Seric 					(void) sendtolist(e->e_from.q_paddr,
203*58082Seric 							  (ADDRESS *) NULL,
204*58082Seric 							  &e->e_errorqueue, e);
20524981Seric 
20624981Seric 				/* deliver a cc: to the postmaster if desired */
20724981Seric 				if (PostMasterCopy != NULL)
208*58082Seric 					(void) sendtolist(PostMasterCopy,
209*58082Seric 							  (ADDRESS *) NULL,
210*58082Seric 							  &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",
22557438Seric 					   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);
26558050Seric 				expand("\201z/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;
271*58082Seric 				(void) sendtolist(buf, &e->e_from, &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 
28957438Seric 			if (e->e_class < 0)
29057438Seric 			{
29157438Seric 				state = ESM_DONE;
29257438Seric 				break;
29357438Seric 			}
29457438Seric 
29524942Seric 			fp = dfopen("/usr/tmp/dead.letter", "a");
29624942Seric 			if (fp == NULL)
29724942Seric 			{
29824942Seric 				state = ESM_PANIC;
29924942Seric 				break;
30024942Seric 			}
30124942Seric 
30258010Seric 			putfromline(fp, FileMailer, e);
30358010Seric 			(*e->e_puthdr)(fp, FileMailer, e);
30458010Seric 			putline("\n", fp, FileMailer);
30558010Seric 			(*e->e_putbody)(fp, FileMailer, e);
30658010Seric 			putline("\n", fp, FileMailer);
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 */
31957438Seric 			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;
37958050Seric 	define('g', "\201f", e);
38058050Seric 	define('<', "\201f", e);
3816978Seric 	ee = newenvelope(&errenvelope);
38258050Seric 	define('a', "\201b", 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 
39657642Seric # ifdef LOG
39758020Seric 	if (LogLevel > 5)
39857642Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
39957642Seric 			e->e_id, ee->e_id, msg);
40057642Seric # endif
40157642Seric 
40210845Seric 	(void) sprintf(buf, "Returned mail: %s", msg);
40310106Seric 	addheader("subject", buf, ee);
4044633Seric 
4054633Seric 	/* fake up an address header for the from person */
40658050Seric 	expand("\201n", buf, &buf[sizeof buf - 1], e);
40753181Seric 	ee->e_sender = ee->e_returnpath = newstr(buf);
40855012Seric 	if (parseaddr(buf, &ee->e_from, -1, '\0', e) == NULL)
4094633Seric 	{
4104633Seric 		syserr("Can't parse myself!");
4114633Seric 		ExitStat = EX_SOFTWARE;
4127045Seric 		returndepth--;
4134633Seric 		return (-1);
4144633Seric 	}
41516159Seric 	loweraddr(&ee->e_from);
4165984Seric 
4176978Seric 	/* push state into submessage */
4186978Seric 	CurEnv = ee;
41958050Seric 	define('f', "\201n", ee);
4209375Seric 	define('x', "Mail Delivery Subsystem", ee);
42157642Seric 	eatheader(ee, FALSE);
4225984Seric 
4236978Seric 	/* actually deliver the error message */
42414876Seric 	sendall(ee, SM_DEFAULT);
4256978Seric 
4266978Seric 	/* restore state */
4277811Seric 	dropenvelope(ee);
4286978Seric 	CurEnv = CurEnv->e_parent;
4297045Seric 	returndepth--;
4306978Seric 
4317045Seric 	/* should check for delivery errors here */
4324633Seric 	return (0);
4334633Seric }
4344633Seric /*
4356978Seric **  ERRBODY -- output the body of an error message.
4366978Seric **
4376978Seric **	Typically this is a copy of the transcript plus a copy of the
4386978Seric **	original offending message.
4396978Seric **
440297Seric **	Parameters:
441297Seric **		fp -- the output file.
44210170Seric **		m -- the mailer to output to.
4439542Seric **		e -- the envelope we are working in.
444297Seric **
445297Seric **	Returns:
446297Seric **		none
447297Seric **
448297Seric **	Side Effects:
4496978Seric **		Outputs the body of an error message.
450297Seric */
451297Seric 
45210170Seric errbody(fp, m, e)
453297Seric 	register FILE *fp;
4544318Seric 	register struct mailer *m;
4559542Seric 	register ENVELOPE *e;
456297Seric {
4576978Seric 	register FILE *xfile;
4583189Seric 	char buf[MAXLINE];
4599337Seric 	char *p;
460297Seric 
4619057Seric 	/*
46255372Seric 	**  Output error message header (if specified and available).
46355372Seric 	*/
46455372Seric 
46555372Seric 	if (ErrMsgFile != NULL)
46655372Seric 	{
46755372Seric 		if (*ErrMsgFile == '/')
46855372Seric 		{
46955372Seric 			xfile = fopen(ErrMsgFile, "r");
47055372Seric 			if (xfile != NULL)
47155372Seric 			{
47255372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
47355425Seric 				{
47455425Seric 					expand(buf, buf, &buf[sizeof buf - 1], e);
47555372Seric 					putline(buf, fp, m);
47655425Seric 				}
47755372Seric 				(void) fclose(xfile);
47855372Seric 				fprintf(fp, "\n");
47955372Seric 			}
48055372Seric 		}
48155372Seric 		else
48255372Seric 		{
48355425Seric 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
48455425Seric 			putline(buf, fp, m);
48555372Seric 			fprintf(fp, "\n");
48655372Seric 		}
48755372Seric 	}
48855372Seric 
48955372Seric 	/*
4909057Seric 	**  Output transcript of errors
4919057Seric 	*/
4929057Seric 
4934086Seric 	(void) fflush(stdout);
4949542Seric 	p = queuename(e->e_parent, 'x');
4959337Seric 	if ((xfile = fopen(p, "r")) == NULL)
4969057Seric 	{
4979337Seric 		syserr("Cannot open %s", p);
4989057Seric 		fprintf(fp, "  ----- Transcript of session is unavailable -----\n");
4999057Seric 	}
5009057Seric 	else
5019057Seric 	{
5029057Seric 		fprintf(fp, "   ----- Transcript of session follows -----\n");
5039542Seric 		if (e->e_xfp != NULL)
5049542Seric 			(void) fflush(e->e_xfp);
5059057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
50610170Seric 			putline(buf, fp, m);
5079057Seric 		(void) fclose(xfile);
5089057Seric 	}
509297Seric 	errno = 0;
5104318Seric 
5114318Seric 	/*
5124318Seric 	**  Output text of original message
5134318Seric 	*/
5144318Seric 
5154289Seric 	if (NoReturn)
5164289Seric 		fprintf(fp, "\n   ----- Return message suppressed -----\n\n");
5179542Seric 	else if (e->e_parent->e_dfp != NULL)
5184199Seric 	{
5195984Seric 		if (SendBody)
5205984Seric 		{
52110170Seric 			putline("\n", fp, m);
52210170Seric 			putline("   ----- Unsent message follows -----\n", fp, m);
5235984Seric 			(void) fflush(fp);
52410170Seric 			putheader(fp, m, e->e_parent);
52510170Seric 			putline("\n", fp, m);
52610170Seric 			putbody(fp, m, e->e_parent);
5275984Seric 		}
5285984Seric 		else
5295984Seric 		{
53010170Seric 			putline("\n", fp, m);
53110170Seric 			putline("  ----- Message header follows -----\n", fp, m);
5325984Seric 			(void) fflush(fp);
53310170Seric 			putheader(fp, m, e->e_parent);
5345984Seric 		}
5354199Seric 	}
5364199Seric 	else
53710170Seric 	{
53810170Seric 		putline("\n", fp, m);
53910170Seric 		putline("  ----- No message was collected -----\n", fp, m);
54010170Seric 		putline("\n", fp, m);
54110170Seric 	}
5424318Seric 
5434318Seric 	/*
5444318Seric 	**  Cleanup and exit
5454318Seric 	*/
5464318Seric 
547297Seric 	if (errno != 0)
5486978Seric 		syserr("errbody: I/O error");
549297Seric }
550