122711Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
362531Sbostic  * Copyright (c) 1988, 1993
462531Sbostic  *	The Regents of the University of California.  All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822711Sdist 
922711Sdist #ifndef lint
10*67996Seric static char sccsid[] = "@(#)savemail.c	8.45 (Berkeley) 11/27/94";
1133731Sbostic #endif /* not lint */
1222711Sdist 
1363937Seric # include "sendmail.h"
14297Seric # include <pwd.h>
15297Seric 
16297Seric /*
17297Seric **  SAVEMAIL -- Save mail on error
18297Seric **
199375Seric **	If mailing back errors, mail it back to the originator
20297Seric **	together with an error message; otherwise, just put it in
21297Seric **	dead.letter in the user's home directory (if he exists on
22297Seric **	this machine).
23297Seric **
24297Seric **	Parameters:
259337Seric **		e -- the envelope containing the message in error.
2667981Seric **		sendbody -- if TRUE, also send back the body of the
2767981Seric **			message; otherwise just send the header.
28297Seric **
29297Seric **	Returns:
30297Seric **		none
31297Seric **
32297Seric **	Side Effects:
33297Seric **		Saves the letter, by writing or mailing it back to the
34297Seric **		sender, or by putting it in dead.letter in her home
35297Seric **		directory.
36297Seric */
37297Seric 
3824942Seric /* defines for state machine */
3924942Seric # define ESM_REPORT	0	/* report to sender's terminal */
4024942Seric # define ESM_MAIL	1	/* mail back to sender */
4124942Seric # define ESM_QUIET	2	/* messages have already been returned */
4224942Seric # define ESM_DEADLETTER	3	/* save in ~/dead.letter */
4324942Seric # define ESM_POSTMASTER	4	/* return to postmaster */
4424942Seric # define ESM_USRTMP	5	/* save in /usr/tmp/dead.letter */
4524942Seric # define ESM_PANIC	6	/* leave the locked queue/transcript files */
4624942Seric # define ESM_DONE	7	/* the message is successfully delivered */
4724942Seric 
4865174Seric # ifndef _PATH_VARTMP
4965174Seric #  define _PATH_VARTMP	"/usr/tmp/"
5065174Seric # endif
5124942Seric 
5265174Seric 
5367981Seric savemail(e, sendbody)
549337Seric 	register ENVELOPE *e;
5567981Seric 	bool sendbody;
56297Seric {
57297Seric 	register struct passwd *pw;
5824942Seric 	register FILE *fp;
5924942Seric 	int state;
6059290Seric 	auto ADDRESS *q = NULL;
6165870Seric 	register char *p;
6265870Seric 	MCI mcibuf;
63297Seric 	char buf[MAXLINE+1];
64297Seric 	extern struct passwd *getpwnam();
65297Seric 	extern char *ttypath();
665846Seric 	typedef int (*fnptr)();
6764945Seric 	extern bool writable();
68297Seric 
697676Seric 	if (tTd(6, 1))
7058680Seric 	{
7166317Seric 		printf("\nsavemail, errormode = %c, id = %s, ExitStat = %d\n  e_from=",
7266317Seric 			e->e_errormode, e->e_id == NULL ? "NONE" : e->e_id,
7366317Seric 			ExitStat);
7458680Seric 		printaddr(&e->e_from, FALSE);
7558680Seric 	}
767361Seric 
7759101Seric 	if (e->e_id == NULL)
7859101Seric 	{
7959101Seric 		/* can't return a message with no id */
8059101Seric 		return;
8159101Seric 	}
8259101Seric 
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 	{
9058733Seric 		e->e_sender = "Postmaster";
9164284Seric 		if (parseaddr(e->e_sender, &e->e_from,
9264284Seric 			      RF_COPYPARSE|RF_SENDERADDR, '\0', NULL, e) == NULL)
93297Seric 		{
9458733Seric 			syserr("553 Cannot parse Postmaster!");
95297Seric 			ExitStat = EX_SOFTWARE;
96297Seric 			finis();
97297Seric 		}
98297Seric 	}
999337Seric 	e->e_to = NULL;
100297Seric 
101297Seric 	/*
10224942Seric 	**  Basic state machine.
10324942Seric 	**
10424942Seric 	**	This machine runs through the following states:
10524942Seric 	**
10624942Seric 	**	ESM_QUIET	Errors have already been printed iff the
10724942Seric 	**			sender is local.
10824942Seric 	**	ESM_REPORT	Report directly to the sender's terminal.
10924942Seric 	**	ESM_MAIL	Mail response to the sender.
11024942Seric 	**	ESM_DEADLETTER	Save response in ~/dead.letter.
11124942Seric 	**	ESM_POSTMASTER	Mail response to the postmaster.
11224942Seric 	**	ESM_PANIC	Save response anywhere possible.
113297Seric 	*/
114297Seric 
11524942Seric 	/* determine starting state */
11658734Seric 	switch (e->e_errormode)
117297Seric 	{
11824942Seric 	  case EM_WRITE:
11924942Seric 		state = ESM_REPORT;
12024942Seric 		break;
12124942Seric 
12224942Seric 	  case EM_BERKNET:
12324942Seric 		/* mail back, but return o.k. exit status */
124401Seric 		ExitStat = EX_OK;
12524942Seric 
12624942Seric 		/* fall through.... */
12724942Seric 
12824942Seric 	  case EM_MAIL:
12924942Seric 		state = ESM_MAIL;
13024942Seric 		break;
13124942Seric 
13224942Seric 	  case EM_PRINT:
13324979Seric 	  case '\0':
13424942Seric 		state = ESM_QUIET;
13524942Seric 		break;
13624942Seric 
13724942Seric 	  case EM_QUIET:
13824942Seric 		/* no need to return anything at all */
13924942Seric 		return;
14024979Seric 
14124979Seric 	  default:
14258734Seric 		syserr("554 savemail: bogus errormode x%x\n", e->e_errormode);
14324979Seric 		state = ESM_MAIL;
14424979Seric 		break;
145297Seric 	}
146297Seric 
14759094Seric 	/* if this is already an error response, send to postmaster */
14859094Seric 	if (bitset(EF_RESPONSE, e->e_flags))
14959094Seric 	{
15059094Seric 		if (e->e_parent != NULL &&
15159094Seric 		    bitset(EF_RESPONSE, e->e_parent->e_flags))
15259094Seric 		{
15359094Seric 			/* got an error sending a response -- can it */
15459094Seric 			return;
15559094Seric 		}
15659094Seric 		state = ESM_POSTMASTER;
15759094Seric 	}
15859094Seric 
15924942Seric 	while (state != ESM_DONE)
160297Seric 	{
16124979Seric 		if (tTd(6, 5))
16224979Seric 			printf("  state %d\n", state);
16324979Seric 
16424942Seric 		switch (state)
165297Seric 		{
16624979Seric 		  case ESM_QUIET:
16767473Seric 			if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags))
16824979Seric 				state = ESM_DEADLETTER;
16924979Seric 			else
17024979Seric 				state = ESM_MAIL;
17124979Seric 			break;
17224979Seric 
17324942Seric 		  case ESM_REPORT:
17424942Seric 
17524942Seric 			/*
17624942Seric 			**  If the user is still logged in on the same terminal,
17724942Seric 			**  then write the error messages back to hir (sic).
17824942Seric 			*/
17924942Seric 
18024942Seric 			p = ttypath();
18124942Seric 			if (p == NULL || freopen(p, "w", stdout) == NULL)
18224942Seric 			{
18324942Seric 				state = ESM_MAIL;
18424942Seric 				break;
18524942Seric 			}
18624942Seric 
18758050Seric 			expand("\201n", buf, &buf[sizeof buf - 1], e);
1889375Seric 			printf("\r\nMessage from %s...\r\n", buf);
1899375Seric 			printf("Errors occurred while sending mail.\r\n");
1909542Seric 			if (e->e_xfp != NULL)
1919375Seric 			{
1929542Seric 				(void) fflush(e->e_xfp);
19324942Seric 				fp = fopen(queuename(e, 'x'), "r");
1949375Seric 			}
1959375Seric 			else
19624942Seric 				fp = NULL;
19724942Seric 			if (fp == NULL)
1989375Seric 			{
1999337Seric 				syserr("Cannot open %s", queuename(e, 'x'));
2009375Seric 				printf("Transcript of session is unavailable.\r\n");
2019375Seric 			}
2029375Seric 			else
2039375Seric 			{
2049375Seric 				printf("Transcript follows:\r\n");
20524942Seric 				while (fgets(buf, sizeof buf, fp) != NULL &&
2069375Seric 				       !ferror(stdout))
2079375Seric 					fputs(buf, stdout);
20858680Seric 				(void) xfclose(fp, "savemail transcript", e->e_id);
2099375Seric 			}
21024942Seric 			printf("Original message will be saved in dead.letter.\r\n");
21124942Seric 			state = ESM_DEADLETTER;
21224942Seric 			break;
213297Seric 
21424942Seric 		  case ESM_MAIL:
21524942Seric 			/*
21624942Seric 			**  If mailing back, do it.
21724942Seric 			**	Throw away all further output.  Don't alias,
21824942Seric 			**	since this could cause loops, e.g., if joe
21924942Seric 			**	mails to joe@x, and for some reason the network
22024942Seric 			**	for @x is down, then the response gets sent to
22124942Seric 			**	joe@x, which gives a response, etc.  Also force
22224942Seric 			**	the mail to be delivered even if a version of
22324942Seric 			**	it has already been sent to the sender.
22463841Seric 			**
22563841Seric 			**  If this is a configuration or local software
22663841Seric 			**	error, send to the local postmaster as well,
22763841Seric 			**	since the originator can't do anything
22863841Seric 			**	about it anyway.  Note that this is a full
22963841Seric 			**	copy of the message (intentionally) so that
23063841Seric 			**	the Postmaster can forward things along.
23124942Seric 			*/
232297Seric 
23363841Seric 			if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE)
23463841Seric 			{
23563841Seric 				(void) sendtolist("postmaster",
23667982Seric 					  NULLADDR, &e->e_errorqueue, 0, e);
23763841Seric 			}
23867939Seric 			if (!emptyaddr(&e->e_from))
23963841Seric 			{
24058680Seric 				(void) sendtolist(e->e_from.q_paddr,
24167982Seric 					  NULLADDR, &e->e_errorqueue, 0, e);
24263841Seric 			}
24358680Seric 
24463841Seric 			/*
24563841Seric 			**  Deliver a non-delivery report to the
24663841Seric 			**  Postmaster-designate (not necessarily
24763841Seric 			**  Postmaster).  This does not include the
24863841Seric 			**  body of the message, for privacy reasons.
24963841Seric 			**  You really shouldn't need this.
25063841Seric 			*/
25163841Seric 
25263849Seric 			e->e_flags |= EF_PM_NOTIFY;
25358178Seric 
25464792Seric 			/* check to see if there are any good addresses */
25564792Seric 			for (q = e->e_errorqueue; q != NULL; q = q->q_next)
25664792Seric 				if (!bitset(QBADADDR|QDONTSEND, q->q_flags))
25764792Seric 					break;
25858680Seric 			if (q == NULL)
25958680Seric 			{
26058680Seric 				/* this is an error-error */
26158680Seric 				state = ESM_POSTMASTER;
26258680Seric 				break;
26358680Seric 			}
26466303Seric 			if (returntosender(e->e_message, e->e_errorqueue,
26567981Seric 					   sendbody, e) == 0)
26658680Seric 			{
26758680Seric 				state = ESM_DONE;
26858680Seric 				break;
26958680Seric 			}
27024981Seric 
27158680Seric 			/* didn't work -- return to postmaster */
27258680Seric 			state = ESM_POSTMASTER;
27358680Seric 			break;
27458306Seric 
27558680Seric 		  case ESM_POSTMASTER:
27658680Seric 			/*
27758680Seric 			**  Similar to previous case, but to system postmaster.
27858680Seric 			*/
27958680Seric 
28059432Seric 			q = NULL;
28167982Seric 			if (sendtolist("postmaster", NULL, &q, 0, e) <= 0)
28224942Seric 			{
28358680Seric 				syserr("553 cannot parse postmaster!");
28458680Seric 				ExitStat = EX_SOFTWARE;
28558680Seric 				state = ESM_USRTMP;
28658680Seric 				break;
28724942Seric 			}
28867981Seric 			if (returntosender(e->e_message, q, sendbody, e) == 0)
28924942Seric 			{
29024942Seric 				state = ESM_DONE;
29124942Seric 				break;
29224942Seric 			}
293297Seric 
29458680Seric 			/* didn't work -- last resort */
29558680Seric 			state = ESM_USRTMP;
29624942Seric 			break;
297297Seric 
29824942Seric 		  case ESM_DEADLETTER:
29924942Seric 			/*
30024942Seric 			**  Save the message in dead.letter.
30124942Seric 			**	If we weren't mailing back, and the user is
30224942Seric 			**	local, we should save the message in
30324942Seric 			**	~/dead.letter so that the poor person doesn't
30424942Seric 			**	have to type it over again -- and we all know
30524942Seric 			**	what poor typists UNIX users are.
30624942Seric 			*/
3075315Seric 
30824942Seric 			p = NULL;
30967473Seric 			if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
31024942Seric 			{
31124942Seric 				if (e->e_from.q_home != NULL)
31224942Seric 					p = e->e_from.q_home;
31324942Seric 				else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
31424942Seric 					p = pw->pw_dir;
31524942Seric 			}
31624942Seric 			if (p == NULL)
31724942Seric 			{
31858865Seric 				/* no local directory */
31924942Seric 				state = ESM_MAIL;
32024942Seric 				break;
32124942Seric 			}
32224942Seric 			if (e->e_dfp != NULL)
32324942Seric 			{
32424942Seric 				bool oldverb = Verbose;
32524942Seric 
32624942Seric 				/* we have a home directory; open dead.letter */
32724942Seric 				define('z', p, e);
32858050Seric 				expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e);
32924942Seric 				Verbose = TRUE;
33058151Seric 				message("Saving message in %s", buf);
33124942Seric 				Verbose = oldverb;
33224942Seric 				e->e_to = buf;
33324942Seric 				q = NULL;
33467982Seric 				(void) sendtolist(buf, &e->e_from, &q, 0, e);
33564354Seric 				if (q != NULL &&
33664354Seric 				    !bitset(QBADADDR, q->q_flags) &&
33764354Seric 				    deliver(e, q) == 0)
33824942Seric 					state = ESM_DONE;
33924942Seric 				else
34024942Seric 					state = ESM_MAIL;
34124942Seric 			}
34225569Seric 			else
34325569Seric 			{
34425569Seric 				/* no data file -- try mailing back */
34525569Seric 				state = ESM_MAIL;
34625569Seric 			}
34724942Seric 			break;
34824942Seric 
34924942Seric 		  case ESM_USRTMP:
35024942Seric 			/*
35124942Seric 			**  Log the mail in /usr/tmp/dead.letter.
35224942Seric 			*/
35324942Seric 
35457438Seric 			if (e->e_class < 0)
35557438Seric 			{
35657438Seric 				state = ESM_DONE;
35757438Seric 				break;
35857438Seric 			}
35957438Seric 
36065174Seric 			strcpy(buf, _PATH_VARTMP);
36165174Seric 			strcat(buf, "dead.letter");
36265112Seric 			if (!writable(buf, NULLADDR, SFF_NOSLINK))
36364945Seric 			{
36464945Seric 				state = ESM_PANIC;
36564945Seric 				break;
36664945Seric 			}
36764945Seric 			fp = dfopen(buf, O_WRONLY|O_CREAT|O_APPEND, FileMode);
36824942Seric 			if (fp == NULL)
36924942Seric 			{
37024942Seric 				state = ESM_PANIC;
37124942Seric 				break;
37224942Seric 			}
37324942Seric 
37465870Seric 			bzero(&mcibuf, sizeof mcibuf);
37565870Seric 			mcibuf.mci_out = fp;
37665870Seric 			mcibuf.mci_mailer = FileMailer;
37765870Seric 			if (bitnset(M_7BITS, FileMailer->m_flags))
37865870Seric 				mcibuf.mci_flags |= MCIF_7BIT;
37965870Seric 
38065870Seric 			putfromline(&mcibuf, e);
38167936Seric 			(*e->e_puthdr)(&mcibuf, e->e_header, e, 0);
38267936Seric 			(*e->e_putbody)(&mcibuf, e, NULL, 0);
38365870Seric 			putline("\n", &mcibuf);
38424942Seric 			(void) fflush(fp);
38524942Seric 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
38667809Seric 			(void) xfclose(fp, "savemail", buf);
38724942Seric 			break;
38824942Seric 
38924942Seric 		  default:
39058151Seric 			syserr("554 savemail: unknown state %d", state);
39124942Seric 
39224942Seric 			/* fall through ... */
39324942Seric 
39424942Seric 		  case ESM_PANIC:
39524942Seric 			/* leave the locked queue & transcript files around */
39664949Seric 			syserr("!554 savemail: cannot save rejected email anywhere");
39724942Seric 		}
398297Seric 	}
399297Seric }
400297Seric /*
4014633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
4024633Seric **
4034633Seric **	Parameters:
4044633Seric **		msg -- the explanatory message.
40516479Seric **		returnq -- the queue of people to send the message to.
4065984Seric **		sendbody -- if TRUE, also send back the body of the
4075984Seric **			message; otherwise just send the header.
40855012Seric **		e -- the current envelope.
4094633Seric **
4104633Seric **	Returns:
4114633Seric **		zero -- if everything went ok.
4124633Seric **		else -- some error.
4134633Seric **
4144633Seric **	Side Effects:
4154633Seric **		Returns the current message to the sender via
4164633Seric **		mail.
4174633Seric */
4184633Seric 
4195984Seric static bool	SendBody;
4204633Seric 
4217045Seric #define MAXRETURNS	6	/* max depth of returning messages */
42258559Seric #define ERRORFUDGE	100	/* nominal size of error message text */
4237045Seric 
42455012Seric returntosender(msg, returnq, sendbody, e)
4254633Seric 	char *msg;
42616479Seric 	ADDRESS *returnq;
4275984Seric 	bool sendbody;
42855012Seric 	register ENVELOPE *e;
4294633Seric {
4304633Seric 	char buf[MAXNAME];
4316978Seric 	extern putheader(), errbody();
4326978Seric 	register ENVELOPE *ee;
43358680Seric 	ENVELOPE *oldcur = CurEnv;
4346978Seric 	ENVELOPE errenvelope;
4357045Seric 	static int returndepth;
4369375Seric 	register ADDRESS *q;
4374633Seric 
43860010Seric 	if (returnq == NULL)
43960010Seric 		return (-1);
44060010Seric 
44158966Seric 	if (msg == NULL)
44258966Seric 		msg = "Unable to deliver mail";
44358966Seric 
4447676Seric 	if (tTd(6, 1))
4457287Seric 	{
44667987Seric 		printf("\n*** Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
44755012Seric 		       msg, returndepth, e);
44816479Seric 		printaddr(returnq, TRUE);
44967987Seric 		if (tTd(6, 20))
45067987Seric 		{
45167987Seric 			printf("Sendq=");
45267987Seric 			printaddr(e->e_sendqueue, TRUE);
45367987Seric 		}
4547287Seric 	}
4557287Seric 
4567045Seric 	if (++returndepth >= MAXRETURNS)
4577045Seric 	{
4587045Seric 		if (returndepth != MAXRETURNS)
45958151Seric 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
4607045Seric 		/* don't "unrecurse" and fake a clean exit */
4617045Seric 		/* returndepth--; */
4627045Seric 		return (0);
4637045Seric 	}
4647045Seric 
4655984Seric 	SendBody = sendbody;
46658680Seric 	define('g', e->e_from.q_paddr, e);
46764940Seric 	define('u', NULL, e);
46867880Seric 
46967880Seric 	/* initialize error envelope */
47058179Seric 	ee = newenvelope(&errenvelope, e);
47158050Seric 	define('a', "\201b", ee);
47259057Seric 	define('r', "internal", ee);
47359057Seric 	define('s', "localhost", ee);
47459057Seric 	define('_', "localhost", ee);
4756978Seric 	ee->e_puthdr = putheader;
4766978Seric 	ee->e_putbody = errbody;
47764120Seric 	ee->e_flags |= EF_RESPONSE|EF_METOO;
47855012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
47945155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
48016479Seric 	ee->e_sendqueue = returnq;
48164655Seric 	ee->e_msgsize = ERRORFUDGE;
48267473Seric 	if (!bitset(EF_NORETURN, e->e_flags))
48364655Seric 		ee->e_msgsize += e->e_msgsize;
48464737Seric 	initsys(ee);
48516479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4869375Seric 	{
48759989Seric 		if (bitset(QBADADDR, q->q_flags))
48858559Seric 			continue;
48958559Seric 
49059989Seric 		if (!bitset(QDONTSEND, q->q_flags))
49159989Seric 			ee->e_nrcpts++;
49258559Seric 
49358144Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
49464284Seric 			parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e);
49558144Seric 
4969375Seric 		if (q->q_alias == NULL)
49767546Seric 			addheader("To", q->q_paddr, &ee->e_header);
4989375Seric 	}
49924942Seric 
50057642Seric # ifdef LOG
50158020Seric 	if (LogLevel > 5)
50265054Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
50357642Seric 			e->e_id, ee->e_id, msg);
50457642Seric # endif
50557642Seric 
50667429Seric 	if (strncmp(msg, "Warning:", 8) == 0)
50767261Seric 	{
50867261Seric 		addheader("Subject", msg, ee);
50967546Seric 		addheader("Precedence", "autoreply warning-timeout", &ee->e_header);
51067261Seric 	}
51167429Seric 	else if (strcmp(msg, "Return receipt") == 0)
51267429Seric 	{
51367429Seric 		addheader("Subject", msg, ee);
51467546Seric 		addheader("Precedence", "autoreply return-receipt", &ee->e_header);
51567429Seric 	}
51667261Seric 	else
51767261Seric 	{
51867261Seric 		sprintf(buf, "Returned mail: %.*s", sizeof buf - 20, msg);
51967546Seric 		addheader("Subject", buf, &ee->e_header);
52067546Seric 		addheader("Precedence", "autoreply failure-message", &ee->e_header);
52167261Seric 	}
52259730Seric 	if (SendMIMEErrors)
52359730Seric 	{
52467546Seric 		addheader("MIME-Version", "1.0", &ee->e_header);
52559730Seric 		(void) sprintf(buf, "%s.%ld/%s",
52659730Seric 			ee->e_id, curtime(), MyHostName);
52759730Seric 		ee->e_msgboundary = newstr(buf);
52867880Seric 		(void) sprintf(buf,
52967880Seric 			"multipart/report; report-type=delivery-status; boundary=\"%s\"",
53067880Seric 			ee->e_msgboundary);
53167546Seric 		addheader("Content-Type", buf, &ee->e_header);
53259730Seric 	}
5334633Seric 
5344633Seric 	/* fake up an address header for the from person */
53558050Seric 	expand("\201n", buf, &buf[sizeof buf - 1], e);
53664284Seric 	if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL)
5374633Seric 	{
53858151Seric 		syserr("553 Can't parse myself!");
5394633Seric 		ExitStat = EX_SOFTWARE;
5407045Seric 		returndepth--;
5414633Seric 		return (-1);
5424633Seric 	}
54358704Seric 	ee->e_sender = ee->e_from.q_paddr;
5445984Seric 
5456978Seric 	/* push state into submessage */
5466978Seric 	CurEnv = ee;
54758050Seric 	define('f', "\201n", ee);
5489375Seric 	define('x', "Mail Delivery Subsystem", ee);
54958929Seric 	eatheader(ee, TRUE);
5505984Seric 
55163753Seric 	/* mark statistics */
55264284Seric 	markstats(ee, NULLADDR);
55363753Seric 
5546978Seric 	/* actually deliver the error message */
55514876Seric 	sendall(ee, SM_DEFAULT);
5566978Seric 
5576978Seric 	/* restore state */
5587811Seric 	dropenvelope(ee);
55958680Seric 	CurEnv = oldcur;
5607045Seric 	returndepth--;
5616978Seric 
5627045Seric 	/* should check for delivery errors here */
5634633Seric 	return (0);
5644633Seric }
5654633Seric /*
5666978Seric **  ERRBODY -- output the body of an error message.
5676978Seric **
5686978Seric **	Typically this is a copy of the transcript plus a copy of the
5696978Seric **	original offending message.
5706978Seric **
571297Seric **	Parameters:
57265870Seric **		mci -- the mailer connection information.
5739542Seric **		e -- the envelope we are working in.
57467546Seric **		separator -- any possible MIME separator.
57567936Seric **		flags -- to modify the behaviour.
576297Seric **
577297Seric **	Returns:
578297Seric **		none
579297Seric **
580297Seric **	Side Effects:
5816978Seric **		Outputs the body of an error message.
582297Seric */
583297Seric 
58467936Seric errbody(mci, e, separator, flags)
58565870Seric 	register MCI *mci;
5869542Seric 	register ENVELOPE *e;
58767546Seric 	char *separator;
588297Seric {
5896978Seric 	register FILE *xfile;
59059082Seric 	char *p;
59159082Seric 	register ADDRESS *q;
59259082Seric 	bool printheader;
59367936Seric 	int pflags = flags;
5943189Seric 	char buf[MAXLINE];
595297Seric 
59667546Seric 	if (bitset(MCIF_INHEADER, mci->mci_flags))
59767546Seric 	{
59867546Seric 		putline("", mci);
59967546Seric 		mci->mci_flags &= ~MCIF_INHEADER;
60067546Seric 	}
60158680Seric 	if (e->e_parent == NULL)
60258680Seric 	{
60358680Seric 		syserr("errbody: null parent");
60465870Seric 		putline("   ----- Original message lost -----\n", mci);
60558680Seric 		return;
60658680Seric 	}
60758680Seric 
6089057Seric 	/*
60959730Seric 	**  Output MIME header.
61059730Seric 	*/
61159730Seric 
61259730Seric 	if (e->e_msgboundary != NULL)
61359730Seric 	{
61465870Seric 		putline("This is a MIME-encapsulated message", mci);
61565870Seric 		putline("", mci);
61659730Seric 		(void) sprintf(buf, "--%s", e->e_msgboundary);
61765870Seric 		putline(buf, mci);
61865870Seric 		putline("", mci);
61959730Seric 	}
62059730Seric 
62159730Seric 	/*
62263852Seric 	**  Output introductory information.
62363852Seric 	*/
62463852Seric 
62564718Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
62664718Seric 		if (bitset(QBADADDR, q->q_flags))
62764718Seric 			break;
62865054Seric 	if (q == NULL &&
62965054Seric 	    !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags))
63064718Seric 	{
63164718Seric 		putline("    **********************************************",
63265870Seric 			mci);
63364718Seric 		putline("    **      THIS IS A WARNING MESSAGE ONLY      **",
63465870Seric 			mci);
63564718Seric 		putline("    **  YOU DO NOT NEED TO RESEND YOUR MESSAGE  **",
63665870Seric 			mci);
63764718Seric 		putline("    **********************************************",
63865870Seric 			mci);
63965870Seric 		putline("", mci);
64064718Seric 	}
64164718Seric 	sprintf(buf, "The original message was received at %s",
64264718Seric 		arpadate(ctime(&e->e_parent->e_ctime)));
64365870Seric 	putline(buf, mci);
64464025Seric 	expand("from \201_", buf, &buf[sizeof buf - 1], e->e_parent);
64565870Seric 	putline(buf, mci);
64665870Seric 	putline("", mci);
64763852Seric 
64863852Seric 	/*
64955372Seric 	**  Output error message header (if specified and available).
65055372Seric 	*/
65155372Seric 
65267682Seric 	if (ErrMsgFile != NULL && !bitset(EF_SENDRECEIPT, e->e_parent->e_flags))
65355372Seric 	{
65455372Seric 		if (*ErrMsgFile == '/')
65555372Seric 		{
65655372Seric 			xfile = fopen(ErrMsgFile, "r");
65755372Seric 			if (xfile != NULL)
65855372Seric 			{
65955372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
66055425Seric 				{
66155425Seric 					expand(buf, buf, &buf[sizeof buf - 1], e);
66265870Seric 					putline(buf, mci);
66355425Seric 				}
66455372Seric 				(void) fclose(xfile);
66565870Seric 				putline("\n", mci);
66655372Seric 			}
66755372Seric 		}
66855372Seric 		else
66955372Seric 		{
67055425Seric 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
67165870Seric 			putline(buf, mci);
67265870Seric 			putline("", mci);
67355372Seric 		}
67455372Seric 	}
67555372Seric 
67655372Seric 	/*
67759082Seric 	**  Output message introduction
67859082Seric 	*/
67959082Seric 
68059082Seric 	printheader = TRUE;
68159082Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
68259082Seric 	{
68367981Seric 		if (bitset(QBADADDR|QREPORT|QRELAYED, q->q_flags))
68459082Seric 		{
68559082Seric 			if (printheader)
68659082Seric 			{
68767880Seric 				putline("   ----- The following addresses have delivery notifications -----",
68865870Seric 					mci);
68959082Seric 				printheader = FALSE;
69059082Seric 			}
69163849Seric 			strcpy(buf, q->q_paddr);
69263787Seric 			if (bitset(QBADADDR, q->q_flags))
69363849Seric 				strcat(buf, "  (unrecoverable error)");
69467981Seric 			else if (bitset(QRELAYED, q->q_flags))
69567981Seric 				strcat(buf, "  (relayed to non-DSN-aware mailer)");
69667880Seric 			else if (bitset(QSENT, q->q_flags))
69767880Seric 				strcat(buf, "  (successfully delivered)");
69863787Seric 			else
69963849Seric 				strcat(buf, "  (transient failure)");
70065870Seric 			putline(buf, mci);
70163849Seric 			if (q->q_alias != NULL)
70263849Seric 			{
70363849Seric 				strcpy(buf, "    (expanded from: ");
70463849Seric 				strcat(buf, q->q_alias->q_paddr);
70563849Seric 				strcat(buf, ")");
70665870Seric 				putline(buf, mci);
70763849Seric 			}
70859082Seric 		}
70959082Seric 	}
71059082Seric 	if (!printheader)
71165870Seric 		putline("\n", mci);
71259082Seric 
71359082Seric 	/*
7149057Seric 	**  Output transcript of errors
7159057Seric 	*/
7169057Seric 
7174086Seric 	(void) fflush(stdout);
7189542Seric 	p = queuename(e->e_parent, 'x');
7199337Seric 	if ((xfile = fopen(p, "r")) == NULL)
7209057Seric 	{
7219337Seric 		syserr("Cannot open %s", p);
72265870Seric 		putline("   ----- Transcript of session is unavailable -----\n", mci);
7239057Seric 	}
7249057Seric 	else
7259057Seric 	{
72665870Seric 		putline("   ----- Transcript of session follows -----\n", mci);
7279542Seric 		if (e->e_xfp != NULL)
7289542Seric 			(void) fflush(e->e_xfp);
7299057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
73065870Seric 			putline(buf, mci);
73158680Seric 		(void) xfclose(xfile, "errbody xscript", p);
7329057Seric 	}
733297Seric 	errno = 0;
7344318Seric 
7354318Seric 	/*
73667880Seric 	**  Output machine-readable version.
73767880Seric 	*/
73867880Seric 
73967880Seric 	if (e->e_msgboundary != NULL)
74067880Seric 	{
74167880Seric 		putline("", mci);
74267880Seric 		(void) sprintf(buf, "--%s", e->e_msgboundary);
74367880Seric 		putline(buf, mci);
74467880Seric 		putline("Content-Type: message/delivery-status", mci);
74567880Seric 		putline("", mci);
74667880Seric 
74767880Seric 		/*
74867880Seric 		**  Output per-message information.
74967880Seric 		*/
75067880Seric 
75167963Seric 		/* OMTS from MAIL FROM: line */
75267963Seric 		if (e->e_parent->e_omts != NULL)
75367963Seric 		{
75467963Seric 			(void) sprintf(buf, "Original-MTS-Type: %s",
75567963Seric 				e->e_parent->e_omts);
75667963Seric 			putline(buf, mci);
75767963Seric 		}
75867880Seric 
75967880Seric 		/* original envelope id from MAIL FROM: line */
76067880Seric 		if (e->e_parent->e_envid != NULL)
76167880Seric 		{
76267880Seric 			(void) sprintf(buf, "Original-Envelope-Id: %s",
76367880Seric 				e->e_parent->e_envid);
76467880Seric 			putline(buf, mci);
76567880Seric 		}
76667880Seric 
76767963Seric 		/* Final-MTS-Type: is required -- our type */
768*67996Seric 		if (e->e_parent->e_from.q_mailer->m_mtstype == NULL)
769*67996Seric 			putline("Final-MTS-Type: Internet", mci);
770*67996Seric 		else
771*67996Seric 		{
772*67996Seric 			(void) sprintf(buf, "Final-MTS-Type: %s",
773*67996Seric 				e->e_parent->e_from.q_mailer->m_mtstype);
774*67996Seric 			putline(buf, mci);
775*67996Seric 		}
77667880Seric 
77767880Seric 		/* Final-MTA: seems silly -- this is in the From: line */
77867880Seric 		(void) sprintf(buf, "Final-MTA: %s", MyHostName);
77967880Seric 		putline(buf, mci);
78067880Seric 
78167963Seric 		/* Received-From: shows where we got this message from */
78267990Seric 		if (RealHostName != NULL)
78367990Seric 		{
78467990Seric 			(void) sprintf(buf, "Received-From: %s", RealHostName);
78567990Seric 			putline(buf, mci);
78667990Seric 		}
78767963Seric 
78867963Seric 		/* Arrival-Date: -- when it arrived here */
78967963Seric 		(void) sprintf(buf, "Arrival-Date: %s",
79067963Seric 			arpadate(ctime(&e->e_parent->e_ctime)));
79167963Seric 		putline(buf, mci);
79267963Seric 
79367880Seric 		/*
79467880Seric 		**  Output per-address information.
79567880Seric 		*/
79667880Seric 
79767880Seric 		for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
79867880Seric 		{
79967880Seric 			register ADDRESS *r;
80067880Seric 
80167880Seric 			if (!bitset(QBADADDR|QREPORT|QRELAYED, q->q_flags))
80267880Seric 				continue;
80367880Seric 			putline("", mci);
80467880Seric 
80567963Seric 			/* Recipient: -- the name from the RCPT command */
80667880Seric 			for (r = q; r->q_alias != NULL; r = r->q_alias)
80767880Seric 				continue;
80867963Seric 			(void) sprintf(buf, "Recipient: %s", r->q_paddr);
80967880Seric 			putline(buf, mci);
81067880Seric 
81167880Seric 			/* Action: -- what happened? */
81267880Seric 			if (bitset(QBADADDR, q->q_flags))
81367880Seric 				putline("Action: failed", mci);
81467880Seric 			else if (bitset(QQUEUEUP, q->q_flags))
81567880Seric 				putline("Action: delayed", mci);
81667880Seric 			else if (bitset(QRELAYED, q->q_flags))
81767880Seric 				putline("Action: relayed", mci);
81867880Seric 			else
81967880Seric 				putline("Action: delivered", mci);
82067880Seric 
82167880Seric 			/* Status: -- what _really_ happened? */
82267880Seric 			strcpy(buf, "Status: ");
82367880Seric 			if (q->q_status != NULL)
82467880Seric 				strcat(buf, q->q_status);
82567990Seric 			else if (q->q_fstatus != NULL)
82667990Seric 				strcat(buf, q->q_fstatus);
82767880Seric 			else if (bitset(QBADADDR, q->q_flags))
82867880Seric 				strcat(buf, "500");
82967880Seric 			else if (bitset(QQUEUEUP, q->q_flags))
83067880Seric 				strcat(buf, "400");
83167880Seric 			else if (bitset(QRELAYED, q->q_flags))
83267880Seric 				strcat(buf, "601");
83367880Seric 			else
83467880Seric 				strcat(buf, "200");
83567880Seric 			putline(buf, mci);
83667880Seric 
83767880Seric 			/* Date: -- fine granularity */
83867880Seric 			if (q->q_statdate == (time_t) 0L)
83967880Seric 				q->q_statdate = curtime();
84067880Seric 			(void) sprintf(buf, "Date: %s",
84167880Seric 				arpadate(ctime(&q->q_statdate)));
84267880Seric 			putline(buf, mci);
84367880Seric 
84467880Seric 			/* Final-Log-Id: -- why isn't this per-message? */
84567880Seric 			(void) sprintf(buf, "Final-Log-Id: %s", e->e_id);
84667880Seric 			putline(buf, mci);
84767880Seric 
84867963Seric 			/* Expiry-Date: -- for delayed messages only */
84967963Seric 			if (bitset(QQUEUEUP, q->q_flags) &&
85067963Seric 			    !bitset(QBADADDR, q->q_flags))
85167963Seric 			{
85267963Seric 				time_t xdate;
85367963Seric 
85467963Seric 				xdate = e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass];
85567963Seric 				sprintf(buf, "Expiry-Date: %s",
85667963Seric 					arpadate(ctime(&xdate)));
85767963Seric 				putline(buf, mci);
85867963Seric 			}
85967963Seric 
86067990Seric 			/* Original-Recipient: -- passed from on high */
86167880Seric 			if (q->q_orcpt != NULL)
86267880Seric 			{
86367990Seric 				(void) sprintf(buf, "Original-Recipient: %s",
86467880Seric 					q->q_orcpt);
86567880Seric 				putline(buf, mci);
86667880Seric 			}
86767880Seric 
86867990Seric 			/* Final-Recipient: -- if through alias */
86967880Seric 			if (q->q_alias != NULL)
87067880Seric 			{
87167990Seric 				(void) sprintf(buf, "Final-Recipient: %s",
87267880Seric 					q->q_paddr);
87367880Seric 				putline(buf, mci);
87467880Seric 			}
87567880Seric 
87667880Seric 			/* Final-Status: -- same as Status?  XXX */
87767990Seric 			if (q->q_fstatus != NULL && q->q_status != NULL)
87867990Seric 			{
87967990Seric 				(void) sprintf(buf, "Final-Status: %s",
88067990Seric 					q->q_fstatus);
88167990Seric 				putline(buf, mci);
88267990Seric 			}
88367880Seric 
884*67996Seric 			/* Remote-MTS-Type: -- depends on mailer */
88567990Seric 			if (q->q_mailer->m_mtstype != NULL)
88667990Seric 			{
88767990Seric 				(void) sprintf(buf, "Remote-MTS-Type: %s",
88867990Seric 					q->q_mailer->m_mtstype);
88967990Seric 				putline(buf, mci);
89067990Seric 			}
89167880Seric 
89267880Seric 			/* Remote-MTA: -- who was I talking to? */
89367880Seric 			if (q->q_statmta != NULL)
89467880Seric 			{
89567880Seric 				(void) sprintf(buf, "Remote-MTA: %s",
89667880Seric 					q->q_statmta);
89767880Seric 				putline(buf, mci);
89867880Seric 			}
89967880Seric 
90067990Seric 			/* Remote-Recipient: -- same as Final-Recipient?  XXX */
90167990Seric 			if (strcmp(q->q_user, q->q_paddr) != 0)
90267990Seric 			{
90367990Seric 				(void) sprintf(buf, "Remote-Recipient: %s",
90467990Seric 					q->q_user);
90567990Seric 				putline(buf, mci);
90667990Seric 			}
90767880Seric 
90867990Seric 			/* Remote-Status: -- return code from remote mailer */
90967990Seric 			if (q->q_rstatus != NULL)
91067990Seric 			{
91167990Seric 				(void) sprintf(buf, "Remote-Status: %s",
91267990Seric 					q->q_rstatus);
91367990Seric 				putline(buf, mci);
91467990Seric 			}
91567880Seric 		}
91667880Seric 	}
91767880Seric 
91867880Seric 	/*
9194318Seric 	**  Output text of original message
9204318Seric 	*/
9214318Seric 
92267880Seric 	if (bitset(EF_NORETURN, e->e_parent->e_flags))
92358665Seric 		SendBody = FALSE;
92467937Seric 	if (!SendBody && e->e_msgboundary != NULL)
92567937Seric 		pflags |= PF_DELETEMIMEHDRS;
92665870Seric 	putline("", mci);
92758680Seric 	if (e->e_parent->e_df != NULL)
9284199Seric 	{
92967880Seric 		if (e->e_msgboundary == NULL)
93067880Seric 		{
93167880Seric 			if (SendBody)
93267880Seric 				putline("   ----- Original message follows -----\n", mci);
93367880Seric 			else
93467880Seric 				putline("   ----- Message header follows -----\n", mci);
93567880Seric 			(void) fflush(mci->mci_out);
93667880Seric 		}
9375984Seric 		else
9385984Seric 		{
93959730Seric 			(void) sprintf(buf, "--%s", e->e_msgboundary);
94065870Seric 			putline(buf, mci);
94165870Seric 			putline("Content-Type: message/rfc822", mci);
9425984Seric 		}
94367981Seric 		putline("", mci);
94467936Seric 		putheader(mci, e->e_parent->e_header, e->e_parent, pflags);
94559730Seric 		if (SendBody)
94667936Seric 			putbody(mci, e->e_parent, e->e_msgboundary, pflags);
94759987Seric 		else
94867546Seric 		{
94967546Seric 			putline("", mci);
95065870Seric 			putline("   ----- Message body suppressed -----", mci);
95167546Seric 		}
9524199Seric 	}
9534199Seric 	else
95410170Seric 	{
95565870Seric 		putline("  ----- No message was collected -----\n", mci);
95610170Seric 	}
9574318Seric 
95860010Seric 	if (e->e_msgboundary != NULL)
95960010Seric 	{
96065870Seric 		putline("", mci);
96160010Seric 		(void) sprintf(buf, "--%s--", e->e_msgboundary);
96265870Seric 		putline(buf, mci);
96360010Seric 	}
96465870Seric 	putline("", mci);
96559730Seric 
9664318Seric 	/*
9674318Seric 	**  Cleanup and exit
9684318Seric 	*/
9694318Seric 
970297Seric 	if (errno != 0)
9716978Seric 		syserr("errbody: I/O error");
972297Seric }
97358144Seric /*
97458144Seric **  PRUNEROUTE -- prune an RFC-822 source route
97558144Seric **
97658144Seric **	Trims down a source route to the last internet-registered hop.
97758144Seric **	This is encouraged by RFC 1123 section 5.3.3.
97858144Seric **
97958144Seric **	Parameters:
98058144Seric **		addr -- the address
98158144Seric **
98258144Seric **	Returns:
98358144Seric **		TRUE -- address was modified
98458144Seric **		FALSE -- address could not be pruned
98558144Seric **
98658144Seric **	Side Effects:
98758144Seric **		modifies addr in-place
98858144Seric */
98958144Seric 
99058144Seric pruneroute(addr)
99158144Seric 	char *addr;
99258144Seric {
99366334Seric #if NAMED_BIND
99458144Seric 	char *start, *at, *comma;
99558144Seric 	char c;
99658144Seric 	int rcode;
99758144Seric 	char hostbuf[BUFSIZ];
99858144Seric 	char *mxhosts[MAXMXHOSTS + 1];
99958144Seric 
100058144Seric 	/* check to see if this is really a route-addr */
100158144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
100258144Seric 		return FALSE;
100358144Seric 	start = strchr(addr, ':');
100458144Seric 	at = strrchr(addr, '@');
100558144Seric 	if (start == NULL || at == NULL || at < start)
100658144Seric 		return FALSE;
100758144Seric 
100858144Seric 	/* slice off the angle brackets */
100958144Seric 	strcpy(hostbuf, at + 1);
101058144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
101158144Seric 
101258144Seric 	while (start)
101358144Seric 	{
101459273Seric 		if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0)
101558144Seric 		{
101658144Seric 			strcpy(addr + 1, start + 1);
101758144Seric 			return TRUE;
101858144Seric 		}
101958144Seric 		c = *start;
102058144Seric 		*start = '\0';
102158144Seric 		comma = strrchr(addr, ',');
102258144Seric 		if (comma && comma[1] == '@')
102358144Seric 			strcpy(hostbuf, comma + 2);
102458144Seric 		else
102558144Seric 			comma = 0;
102658144Seric 		*start = c;
102758144Seric 		start = comma;
102858144Seric 	}
102958144Seric #endif
103058144Seric 	return FALSE;
103158144Seric }
1032