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*65112Seric static char sccsid[] = "@(#)savemail.c	8.23 (Berkeley) 12/13/93";
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.
26297Seric **
27297Seric **	Returns:
28297Seric **		none
29297Seric **
30297Seric **	Side Effects:
31297Seric **		Saves the letter, by writing or mailing it back to the
32297Seric **		sender, or by putting it in dead.letter in her home
33297Seric **		directory.
34297Seric */
35297Seric 
3624942Seric /* defines for state machine */
3724942Seric # define ESM_REPORT	0	/* report to sender's terminal */
3824942Seric # define ESM_MAIL	1	/* mail back to sender */
3924942Seric # define ESM_QUIET	2	/* messages have already been returned */
4024942Seric # define ESM_DEADLETTER	3	/* save in ~/dead.letter */
4124942Seric # define ESM_POSTMASTER	4	/* return to postmaster */
4224942Seric # define ESM_USRTMP	5	/* save in /usr/tmp/dead.letter */
4324942Seric # define ESM_PANIC	6	/* leave the locked queue/transcript files */
4424942Seric # define ESM_DONE	7	/* the message is successfully delivered */
4524942Seric 
4624942Seric 
479337Seric savemail(e)
489337Seric 	register ENVELOPE *e;
49297Seric {
50297Seric 	register struct passwd *pw;
5124942Seric 	register FILE *fp;
5224942Seric 	int state;
5359290Seric 	auto ADDRESS *q = NULL;
54297Seric 	char buf[MAXLINE+1];
55297Seric 	extern struct passwd *getpwnam();
56297Seric 	register char *p;
57297Seric 	extern char *ttypath();
585846Seric 	typedef int (*fnptr)();
5964945Seric 	extern bool writable();
60297Seric 
617676Seric 	if (tTd(6, 1))
6258680Seric 	{
6359101Seric 		printf("\nsavemail, errormode = %c, id = %s\n  e_from=",
6459101Seric 			e->e_errormode, e->e_id == NULL ? "NONE" : e->e_id);
6558680Seric 		printaddr(&e->e_from, FALSE);
6658680Seric 	}
677361Seric 
6859101Seric 	if (e->e_id == NULL)
6959101Seric 	{
7059101Seric 		/* can't return a message with no id */
7159101Seric 		return;
7259101Seric 	}
7359101Seric 
74297Seric 	/*
75297Seric 	**  In the unhappy event we don't know who to return the mail
76297Seric 	**  to, make someone up.
77297Seric 	*/
78297Seric 
799337Seric 	if (e->e_from.q_paddr == NULL)
80297Seric 	{
8158733Seric 		e->e_sender = "Postmaster";
8264284Seric 		if (parseaddr(e->e_sender, &e->e_from,
8364284Seric 			      RF_COPYPARSE|RF_SENDERADDR, '\0', NULL, e) == NULL)
84297Seric 		{
8558733Seric 			syserr("553 Cannot parse Postmaster!");
86297Seric 			ExitStat = EX_SOFTWARE;
87297Seric 			finis();
88297Seric 		}
89297Seric 	}
909337Seric 	e->e_to = NULL;
91297Seric 
92297Seric 	/*
9324942Seric 	**  Basic state machine.
9424942Seric 	**
9524942Seric 	**	This machine runs through the following states:
9624942Seric 	**
9724942Seric 	**	ESM_QUIET	Errors have already been printed iff the
9824942Seric 	**			sender is local.
9924942Seric 	**	ESM_REPORT	Report directly to the sender's terminal.
10024942Seric 	**	ESM_MAIL	Mail response to the sender.
10124942Seric 	**	ESM_DEADLETTER	Save response in ~/dead.letter.
10224942Seric 	**	ESM_POSTMASTER	Mail response to the postmaster.
10324942Seric 	**	ESM_PANIC	Save response anywhere possible.
104297Seric 	*/
105297Seric 
10624942Seric 	/* determine starting state */
10758734Seric 	switch (e->e_errormode)
108297Seric 	{
10924942Seric 	  case EM_WRITE:
11024942Seric 		state = ESM_REPORT;
11124942Seric 		break;
11224942Seric 
11324942Seric 	  case EM_BERKNET:
11424942Seric 		/* mail back, but return o.k. exit status */
115401Seric 		ExitStat = EX_OK;
11624942Seric 
11724942Seric 		/* fall through.... */
11824942Seric 
11924942Seric 	  case EM_MAIL:
12024942Seric 		state = ESM_MAIL;
12124942Seric 		break;
12224942Seric 
12324942Seric 	  case EM_PRINT:
12424979Seric 	  case '\0':
12524942Seric 		state = ESM_QUIET;
12624942Seric 		break;
12724942Seric 
12824942Seric 	  case EM_QUIET:
12924942Seric 		/* no need to return anything at all */
13024942Seric 		return;
13124979Seric 
13224979Seric 	  default:
13358734Seric 		syserr("554 savemail: bogus errormode x%x\n", e->e_errormode);
13424979Seric 		state = ESM_MAIL;
13524979Seric 		break;
136297Seric 	}
137297Seric 
13859094Seric 	/* if this is already an error response, send to postmaster */
13959094Seric 	if (bitset(EF_RESPONSE, e->e_flags))
14059094Seric 	{
14159094Seric 		if (e->e_parent != NULL &&
14259094Seric 		    bitset(EF_RESPONSE, e->e_parent->e_flags))
14359094Seric 		{
14459094Seric 			/* got an error sending a response -- can it */
14559094Seric 			return;
14659094Seric 		}
14759094Seric 		state = ESM_POSTMASTER;
14859094Seric 	}
14959094Seric 
15024942Seric 	while (state != ESM_DONE)
151297Seric 	{
15224979Seric 		if (tTd(6, 5))
15324979Seric 			printf("  state %d\n", state);
15424979Seric 
15524942Seric 		switch (state)
156297Seric 		{
15724979Seric 		  case ESM_QUIET:
15824979Seric 			if (e->e_from.q_mailer == LocalMailer)
15924979Seric 				state = ESM_DEADLETTER;
16024979Seric 			else
16124979Seric 				state = ESM_MAIL;
16224979Seric 			break;
16324979Seric 
16424942Seric 		  case ESM_REPORT:
16524942Seric 
16624942Seric 			/*
16724942Seric 			**  If the user is still logged in on the same terminal,
16824942Seric 			**  then write the error messages back to hir (sic).
16924942Seric 			*/
17024942Seric 
17124942Seric 			p = ttypath();
17224942Seric 			if (p == NULL || freopen(p, "w", stdout) == NULL)
17324942Seric 			{
17424942Seric 				state = ESM_MAIL;
17524942Seric 				break;
17624942Seric 			}
17724942Seric 
17858050Seric 			expand("\201n", buf, &buf[sizeof buf - 1], e);
1799375Seric 			printf("\r\nMessage from %s...\r\n", buf);
1809375Seric 			printf("Errors occurred while sending mail.\r\n");
1819542Seric 			if (e->e_xfp != NULL)
1829375Seric 			{
1839542Seric 				(void) fflush(e->e_xfp);
18424942Seric 				fp = fopen(queuename(e, 'x'), "r");
1859375Seric 			}
1869375Seric 			else
18724942Seric 				fp = NULL;
18824942Seric 			if (fp == NULL)
1899375Seric 			{
1909337Seric 				syserr("Cannot open %s", queuename(e, 'x'));
1919375Seric 				printf("Transcript of session is unavailable.\r\n");
1929375Seric 			}
1939375Seric 			else
1949375Seric 			{
1959375Seric 				printf("Transcript follows:\r\n");
19624942Seric 				while (fgets(buf, sizeof buf, fp) != NULL &&
1979375Seric 				       !ferror(stdout))
1989375Seric 					fputs(buf, stdout);
19958680Seric 				(void) xfclose(fp, "savemail transcript", e->e_id);
2009375Seric 			}
20124942Seric 			printf("Original message will be saved in dead.letter.\r\n");
20224942Seric 			state = ESM_DEADLETTER;
20324942Seric 			break;
204297Seric 
20524942Seric 		  case ESM_MAIL:
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.
21563841Seric 			**
21663841Seric 			**  If this is a configuration or local software
21763841Seric 			**	error, send to the local postmaster as well,
21863841Seric 			**	since the originator can't do anything
21963841Seric 			**	about it anyway.  Note that this is a full
22063841Seric 			**	copy of the message (intentionally) so that
22163841Seric 			**	the Postmaster can forward things along.
22224942Seric 			*/
223297Seric 
22463841Seric 			if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE)
22563841Seric 			{
22663841Seric 				(void) sendtolist("postmaster",
22764284Seric 					  NULLADDR, &e->e_errorqueue, e);
22863841Seric 			}
22958680Seric 			if (strcmp(e->e_from.q_paddr, "<>") != 0)
23063841Seric 			{
23158680Seric 				(void) sendtolist(e->e_from.q_paddr,
23264284Seric 					  NULLADDR, &e->e_errorqueue, e);
23363841Seric 			}
23458680Seric 
23563841Seric 			/*
23663841Seric 			**  Deliver a non-delivery report to the
23763841Seric 			**  Postmaster-designate (not necessarily
23863841Seric 			**  Postmaster).  This does not include the
23963841Seric 			**  body of the message, for privacy reasons.
24063841Seric 			**  You really shouldn't need this.
24163841Seric 			*/
24263841Seric 
24363849Seric 			e->e_flags |= EF_PM_NOTIFY;
24458178Seric 
24564792Seric 			/* check to see if there are any good addresses */
24664792Seric 			for (q = e->e_errorqueue; q != NULL; q = q->q_next)
24764792Seric 				if (!bitset(QBADADDR|QDONTSEND, q->q_flags))
24864792Seric 					break;
24958680Seric 			if (q == NULL)
25058680Seric 			{
25158680Seric 				/* this is an error-error */
25258680Seric 				state = ESM_POSTMASTER;
25358680Seric 				break;
25458680Seric 			}
25558966Seric 			if (returntosender(e->e_message,
25658680Seric 					   q, (e->e_class >= 0), e) == 0)
25758680Seric 			{
25858680Seric 				state = ESM_DONE;
25958680Seric 				break;
26058680Seric 			}
26124981Seric 
26258680Seric 			/* didn't work -- return to postmaster */
26358680Seric 			state = ESM_POSTMASTER;
26458680Seric 			break;
26558306Seric 
26658680Seric 		  case ESM_POSTMASTER:
26758680Seric 			/*
26858680Seric 			**  Similar to previous case, but to system postmaster.
26958680Seric 			*/
27058680Seric 
27159432Seric 			q = NULL;
27259432Seric 			if (sendtolist("postmaster", NULL, &q, e) <= 0)
27324942Seric 			{
27458680Seric 				syserr("553 cannot parse postmaster!");
27558680Seric 				ExitStat = EX_SOFTWARE;
27658680Seric 				state = ESM_USRTMP;
27758680Seric 				break;
27824942Seric 			}
27958966Seric 			if (returntosender(e->e_message,
28057438Seric 					   q, (e->e_class >= 0), e) == 0)
28124942Seric 			{
28224942Seric 				state = ESM_DONE;
28324942Seric 				break;
28424942Seric 			}
285297Seric 
28658680Seric 			/* didn't work -- last resort */
28758680Seric 			state = ESM_USRTMP;
28824942Seric 			break;
289297Seric 
29024942Seric 		  case ESM_DEADLETTER:
29124942Seric 			/*
29224942Seric 			**  Save the message in dead.letter.
29324942Seric 			**	If we weren't mailing back, and the user is
29424942Seric 			**	local, we should save the message in
29524942Seric 			**	~/dead.letter so that the poor person doesn't
29624942Seric 			**	have to type it over again -- and we all know
29724942Seric 			**	what poor typists UNIX users are.
29824942Seric 			*/
2995315Seric 
30024942Seric 			p = NULL;
30124942Seric 			if (e->e_from.q_mailer == LocalMailer)
30224942Seric 			{
30324942Seric 				if (e->e_from.q_home != NULL)
30424942Seric 					p = e->e_from.q_home;
30524942Seric 				else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
30624942Seric 					p = pw->pw_dir;
30724942Seric 			}
30824942Seric 			if (p == NULL)
30924942Seric 			{
31058865Seric 				/* no local directory */
31124942Seric 				state = ESM_MAIL;
31224942Seric 				break;
31324942Seric 			}
31424942Seric 			if (e->e_dfp != NULL)
31524942Seric 			{
31624942Seric 				bool oldverb = Verbose;
31724942Seric 
31824942Seric 				/* we have a home directory; open dead.letter */
31924942Seric 				define('z', p, e);
32058050Seric 				expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e);
32124942Seric 				Verbose = TRUE;
32258151Seric 				message("Saving message in %s", buf);
32324942Seric 				Verbose = oldverb;
32424942Seric 				e->e_to = buf;
32524942Seric 				q = NULL;
32658082Seric 				(void) sendtolist(buf, &e->e_from, &q, e);
32764354Seric 				if (q != NULL &&
32864354Seric 				    !bitset(QBADADDR, q->q_flags) &&
32964354Seric 				    deliver(e, q) == 0)
33024942Seric 					state = ESM_DONE;
33124942Seric 				else
33224942Seric 					state = ESM_MAIL;
33324942Seric 			}
33425569Seric 			else
33525569Seric 			{
33625569Seric 				/* no data file -- try mailing back */
33725569Seric 				state = ESM_MAIL;
33825569Seric 			}
33924942Seric 			break;
34024942Seric 
34124942Seric 		  case ESM_USRTMP:
34224942Seric 			/*
34324942Seric 			**  Log the mail in /usr/tmp/dead.letter.
34424942Seric 			*/
34524942Seric 
34657438Seric 			if (e->e_class < 0)
34757438Seric 			{
34857438Seric 				state = ESM_DONE;
34957438Seric 				break;
35057438Seric 			}
35157438Seric 
35264945Seric 			strcpy(buf, "/usr/tmp/dead.letter");
353*65112Seric 			if (!writable(buf, NULLADDR, SFF_NOSLINK))
35464945Seric 			{
35564945Seric 				state = ESM_PANIC;
35664945Seric 				break;
35764945Seric 			}
35864945Seric 			fp = dfopen(buf, O_WRONLY|O_CREAT|O_APPEND, FileMode);
35924942Seric 			if (fp == NULL)
36024942Seric 			{
36124942Seric 				state = ESM_PANIC;
36224942Seric 				break;
36324942Seric 			}
36424942Seric 
36558010Seric 			putfromline(fp, FileMailer, e);
36658010Seric 			(*e->e_puthdr)(fp, FileMailer, e);
36758010Seric 			putline("\n", fp, FileMailer);
36859730Seric 			(*e->e_putbody)(fp, FileMailer, e, NULL);
36958010Seric 			putline("\n", fp, FileMailer);
37024942Seric 			(void) fflush(fp);
37124942Seric 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
37258680Seric 			(void) xfclose(fp, "savemail", "/usr/tmp/dead.letter");
37324942Seric 			break;
37424942Seric 
37524942Seric 		  default:
37658151Seric 			syserr("554 savemail: unknown state %d", state);
37724942Seric 
37824942Seric 			/* fall through ... */
37924942Seric 
38024942Seric 		  case ESM_PANIC:
38124942Seric 			/* leave the locked queue & transcript files around */
38264949Seric 			syserr("!554 savemail: cannot save rejected email anywhere");
38324942Seric 		}
384297Seric 	}
385297Seric }
386297Seric /*
3874633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
3884633Seric **
3894633Seric **	Parameters:
3904633Seric **		msg -- the explanatory message.
39116479Seric **		returnq -- the queue of people to send the message to.
3925984Seric **		sendbody -- if TRUE, also send back the body of the
3935984Seric **			message; otherwise just send the header.
39455012Seric **		e -- the current envelope.
3954633Seric **
3964633Seric **	Returns:
3974633Seric **		zero -- if everything went ok.
3984633Seric **		else -- some error.
3994633Seric **
4004633Seric **	Side Effects:
4014633Seric **		Returns the current message to the sender via
4024633Seric **		mail.
4034633Seric */
4044633Seric 
4055984Seric static bool	SendBody;
4064633Seric 
4077045Seric #define MAXRETURNS	6	/* max depth of returning messages */
40858559Seric #define ERRORFUDGE	100	/* nominal size of error message text */
4097045Seric 
41055012Seric returntosender(msg, returnq, sendbody, e)
4114633Seric 	char *msg;
41216479Seric 	ADDRESS *returnq;
4135984Seric 	bool sendbody;
41455012Seric 	register ENVELOPE *e;
4154633Seric {
4164633Seric 	char buf[MAXNAME];
4176978Seric 	extern putheader(), errbody();
4186978Seric 	register ENVELOPE *ee;
41958680Seric 	ENVELOPE *oldcur = CurEnv;
4206978Seric 	ENVELOPE errenvelope;
4217045Seric 	static int returndepth;
4229375Seric 	register ADDRESS *q;
4234633Seric 
42460010Seric 	if (returnq == NULL)
42560010Seric 		return (-1);
42660010Seric 
42758966Seric 	if (msg == NULL)
42858966Seric 		msg = "Unable to deliver mail";
42958966Seric 
4307676Seric 	if (tTd(6, 1))
4317287Seric 	{
43258680Seric 		printf("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
43355012Seric 		       msg, returndepth, e);
43416479Seric 		printaddr(returnq, TRUE);
4357287Seric 	}
4367287Seric 
4377045Seric 	if (++returndepth >= MAXRETURNS)
4387045Seric 	{
4397045Seric 		if (returndepth != MAXRETURNS)
44058151Seric 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
4417045Seric 		/* don't "unrecurse" and fake a clean exit */
4427045Seric 		/* returndepth--; */
4437045Seric 		return (0);
4447045Seric 	}
4457045Seric 
4465984Seric 	SendBody = sendbody;
44758680Seric 	define('g', e->e_from.q_paddr, e);
44864940Seric 	define('u', NULL, e);
44958179Seric 	ee = newenvelope(&errenvelope, e);
45058050Seric 	define('a', "\201b", ee);
45159057Seric 	define('r', "internal", ee);
45259057Seric 	define('s', "localhost", ee);
45359057Seric 	define('_', "localhost", ee);
4546978Seric 	ee->e_puthdr = putheader;
4556978Seric 	ee->e_putbody = errbody;
45664120Seric 	ee->e_flags |= EF_RESPONSE|EF_METOO;
45755012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
45845155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
45916479Seric 	ee->e_sendqueue = returnq;
46064655Seric 	ee->e_msgsize = ERRORFUDGE;
46164655Seric 	if (!NoReturn)
46264655Seric 		ee->e_msgsize += e->e_msgsize;
46364737Seric 	initsys(ee);
46416479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4659375Seric 	{
46659989Seric 		if (bitset(QBADADDR, q->q_flags))
46758559Seric 			continue;
46858559Seric 
46959989Seric 		if (!bitset(QDONTSEND, q->q_flags))
47059989Seric 			ee->e_nrcpts++;
47158559Seric 
47258144Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
47364284Seric 			parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e);
47458144Seric 
4759375Seric 		if (q->q_alias == NULL)
47659580Seric 			addheader("To", q->q_paddr, ee);
4779375Seric 	}
47824942Seric 
47957642Seric # ifdef LOG
48058020Seric 	if (LogLevel > 5)
48165054Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
48257642Seric 			e->e_id, ee->e_id, msg);
48357642Seric # endif
48457642Seric 
48565054Seric 	(void) sprintf(buf, "Returned mail: %s", msg);
48665054Seric 	addheader("Subject", buf, ee);
48759730Seric 	if (SendMIMEErrors)
48859730Seric 	{
48959987Seric 		addheader("MIME-Version", "1.0", ee);
49059730Seric 		(void) sprintf(buf, "%s.%ld/%s",
49159730Seric 			ee->e_id, curtime(), MyHostName);
49259730Seric 		ee->e_msgboundary = newstr(buf);
49359730Seric 		(void) sprintf(buf, "multipart/mixed; boundary=\"%s\"",
49459730Seric 					ee->e_msgboundary);
49559730Seric 		addheader("Content-Type", buf, ee);
49659730Seric 	}
4974633Seric 
4984633Seric 	/* fake up an address header for the from person */
49958050Seric 	expand("\201n", buf, &buf[sizeof buf - 1], e);
50064284Seric 	if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL)
5014633Seric 	{
50258151Seric 		syserr("553 Can't parse myself!");
5034633Seric 		ExitStat = EX_SOFTWARE;
5047045Seric 		returndepth--;
5054633Seric 		return (-1);
5064633Seric 	}
50758704Seric 	ee->e_sender = ee->e_from.q_paddr;
5085984Seric 
5096978Seric 	/* push state into submessage */
5106978Seric 	CurEnv = ee;
51158050Seric 	define('f', "\201n", ee);
5129375Seric 	define('x', "Mail Delivery Subsystem", ee);
51358929Seric 	eatheader(ee, TRUE);
5145984Seric 
51563753Seric 	/* mark statistics */
51664284Seric 	markstats(ee, NULLADDR);
51763753Seric 
5186978Seric 	/* actually deliver the error message */
51914876Seric 	sendall(ee, SM_DEFAULT);
5206978Seric 
5216978Seric 	/* restore state */
5227811Seric 	dropenvelope(ee);
52358680Seric 	CurEnv = oldcur;
5247045Seric 	returndepth--;
5256978Seric 
5267045Seric 	/* should check for delivery errors here */
5274633Seric 	return (0);
5284633Seric }
5294633Seric /*
5306978Seric **  ERRBODY -- output the body of an error message.
5316978Seric **
5326978Seric **	Typically this is a copy of the transcript plus a copy of the
5336978Seric **	original offending message.
5346978Seric **
535297Seric **	Parameters:
536297Seric **		fp -- the output file.
53710170Seric **		m -- the mailer to output to.
5389542Seric **		e -- the envelope we are working in.
539297Seric **
540297Seric **	Returns:
541297Seric **		none
542297Seric **
543297Seric **	Side Effects:
5446978Seric **		Outputs the body of an error message.
545297Seric */
546297Seric 
54710170Seric errbody(fp, m, e)
548297Seric 	register FILE *fp;
5494318Seric 	register struct mailer *m;
5509542Seric 	register ENVELOPE *e;
551297Seric {
5526978Seric 	register FILE *xfile;
55359082Seric 	char *p;
55459082Seric 	register ADDRESS *q;
55559082Seric 	bool printheader;
5563189Seric 	char buf[MAXLINE];
557297Seric 
55858680Seric 	if (e->e_parent == NULL)
55958680Seric 	{
56058680Seric 		syserr("errbody: null parent");
56158680Seric 		putline("   ----- Original message lost -----\n", fp, m);
56258680Seric 		return;
56358680Seric 	}
56458680Seric 
5659057Seric 	/*
56659730Seric 	**  Output MIME header.
56759730Seric 	*/
56859730Seric 
56959730Seric 	if (e->e_msgboundary != NULL)
57059730Seric 	{
57159730Seric 		putline("This is a MIME-encapsulated message", fp, m);
57259730Seric 		putline("", fp, m);
57359730Seric 		(void) sprintf(buf, "--%s", e->e_msgboundary);
57459730Seric 		putline(buf, fp, m);
57559730Seric 		putline("", fp, m);
57659730Seric 	}
57759730Seric 
57859730Seric 	/*
57963852Seric 	**  Output introductory information.
58063852Seric 	*/
58163852Seric 
58264718Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
58364718Seric 		if (bitset(QBADADDR, q->q_flags))
58464718Seric 			break;
58565054Seric 	if (q == NULL &&
58665054Seric 	    !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags))
58764718Seric 	{
58864718Seric 		putline("    **********************************************",
58964718Seric 			fp, m);
59064718Seric 		putline("    **      THIS IS A WARNING MESSAGE ONLY      **",
59164718Seric 			fp, m);
59264718Seric 		putline("    **  YOU DO NOT NEED TO RESEND YOUR MESSAGE  **",
59364718Seric 			fp, m);
59464718Seric 		putline("    **********************************************",
59564718Seric 			fp, m);
59664718Seric 		putline("", fp, m);
59764718Seric 	}
59864718Seric 	sprintf(buf, "The original message was received at %s",
59964718Seric 		arpadate(ctime(&e->e_parent->e_ctime)));
60063852Seric 	putline(buf, fp, m);
60164025Seric 	expand("from \201_", buf, &buf[sizeof buf - 1], e->e_parent);
60263852Seric 	putline(buf, fp, m);
60363852Seric 	putline("", fp, m);
60463852Seric 
60563852Seric 	/*
60655372Seric 	**  Output error message header (if specified and available).
60755372Seric 	*/
60855372Seric 
60955372Seric 	if (ErrMsgFile != NULL)
61055372Seric 	{
61155372Seric 		if (*ErrMsgFile == '/')
61255372Seric 		{
61355372Seric 			xfile = fopen(ErrMsgFile, "r");
61455372Seric 			if (xfile != NULL)
61555372Seric 			{
61655372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
61755425Seric 				{
61855425Seric 					expand(buf, buf, &buf[sizeof buf - 1], e);
61955372Seric 					putline(buf, fp, m);
62055425Seric 				}
62155372Seric 				(void) fclose(xfile);
62259082Seric 				putline("\n", fp, m);
62355372Seric 			}
62455372Seric 		}
62555372Seric 		else
62655372Seric 		{
62755425Seric 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
62855425Seric 			putline(buf, fp, m);
62959730Seric 			putline("", fp, m);
63055372Seric 		}
63155372Seric 	}
63255372Seric 
63355372Seric 	/*
63459082Seric 	**  Output message introduction
63559082Seric 	*/
63659082Seric 
63759082Seric 	printheader = TRUE;
63859082Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
63959082Seric 	{
64063787Seric 		if (bitset(QBADADDR|QREPORT, q->q_flags))
64159082Seric 		{
64259082Seric 			if (printheader)
64359082Seric 			{
64463787Seric 				putline("   ----- The following addresses had delivery problems -----",
64559082Seric 					fp, m);
64659082Seric 				printheader = FALSE;
64759082Seric 			}
64863849Seric 			strcpy(buf, q->q_paddr);
64963787Seric 			if (bitset(QBADADDR, q->q_flags))
65063849Seric 				strcat(buf, "  (unrecoverable error)");
65163787Seric 			else
65263849Seric 				strcat(buf, "  (transient failure)");
65363787Seric 			putline(buf, fp, m);
65463849Seric 			if (q->q_alias != NULL)
65563849Seric 			{
65663849Seric 				strcpy(buf, "    (expanded from: ");
65763849Seric 				strcat(buf, q->q_alias->q_paddr);
65863849Seric 				strcat(buf, ")");
65963849Seric 				putline(buf, fp, m);
66063849Seric 			}
66159082Seric 		}
66259082Seric 	}
66359082Seric 	if (!printheader)
66459082Seric 		putline("\n", fp, m);
66559082Seric 
66659082Seric 	/*
6679057Seric 	**  Output transcript of errors
6689057Seric 	*/
6699057Seric 
6704086Seric 	(void) fflush(stdout);
6719542Seric 	p = queuename(e->e_parent, 'x');
6729337Seric 	if ((xfile = fopen(p, "r")) == NULL)
6739057Seric 	{
6749337Seric 		syserr("Cannot open %s", p);
67559082Seric 		putline("   ----- Transcript of session is unavailable -----\n", fp, m);
6769057Seric 	}
6779057Seric 	else
6789057Seric 	{
67958680Seric 		putline("   ----- Transcript of session follows -----\n", fp, m);
6809542Seric 		if (e->e_xfp != NULL)
6819542Seric 			(void) fflush(e->e_xfp);
6829057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
68310170Seric 			putline(buf, fp, m);
68458680Seric 		(void) xfclose(xfile, "errbody xscript", p);
6859057Seric 	}
686297Seric 	errno = 0;
6874318Seric 
6884318Seric 	/*
6894318Seric 	**  Output text of original message
6904318Seric 	*/
6914318Seric 
6924289Seric 	if (NoReturn)
69358665Seric 		SendBody = FALSE;
69459730Seric 	putline("", fp, m);
69558680Seric 	if (e->e_parent->e_df != NULL)
6964199Seric 	{
6975984Seric 		if (SendBody)
69863852Seric 			putline("   ----- Original message follows -----\n", fp, m);
6995984Seric 		else
70059987Seric 			putline("   ----- Message header follows -----\n", fp, m);
70159730Seric 		(void) fflush(fp);
70259730Seric 
70359730Seric 		if (e->e_msgboundary != NULL)
7045984Seric 		{
70559730Seric 			putline("", fp, m);
70659730Seric 			(void) sprintf(buf, "--%s", e->e_msgboundary);
70759730Seric 			putline(buf, fp, m);
70859987Seric 			putline("Content-Type: message/rfc822", fp, m);
70959730Seric 			putline("", fp, m);
7105984Seric 		}
71159730Seric 		putheader(fp, m, e->e_parent);
71259730Seric 		putline("", fp, m);
71359730Seric 		if (SendBody)
71459730Seric 			putbody(fp, m, e->e_parent, e->e_msgboundary);
71559987Seric 		else
71660010Seric 			putline("   ----- Message body suppressed -----", fp, m);
7174199Seric 	}
7184199Seric 	else
71910170Seric 	{
72010170Seric 		putline("  ----- No message was collected -----\n", fp, m);
72110170Seric 	}
7224318Seric 
72360010Seric 	if (e->e_msgboundary != NULL)
72460010Seric 	{
72560010Seric 		putline("", fp, m);
72660010Seric 		(void) sprintf(buf, "--%s--", e->e_msgboundary);
72760010Seric 		putline(buf, fp, m);
72860010Seric 	}
72959730Seric 	putline("", fp, m);
73059730Seric 
7314318Seric 	/*
7324318Seric 	**  Cleanup and exit
7334318Seric 	*/
7344318Seric 
735297Seric 	if (errno != 0)
7366978Seric 		syserr("errbody: I/O error");
737297Seric }
73858144Seric /*
73958144Seric **  PRUNEROUTE -- prune an RFC-822 source route
74058144Seric **
74158144Seric **	Trims down a source route to the last internet-registered hop.
74258144Seric **	This is encouraged by RFC 1123 section 5.3.3.
74358144Seric **
74458144Seric **	Parameters:
74558144Seric **		addr -- the address
74658144Seric **
74758144Seric **	Returns:
74858144Seric **		TRUE -- address was modified
74958144Seric **		FALSE -- address could not be pruned
75058144Seric **
75158144Seric **	Side Effects:
75258144Seric **		modifies addr in-place
75358144Seric */
75458144Seric 
75558144Seric pruneroute(addr)
75658144Seric 	char *addr;
75758144Seric {
75858144Seric #ifdef NAMED_BIND
75958144Seric 	char *start, *at, *comma;
76058144Seric 	char c;
76158144Seric 	int rcode;
76258144Seric 	char hostbuf[BUFSIZ];
76358144Seric 	char *mxhosts[MAXMXHOSTS + 1];
76458144Seric 
76558144Seric 	/* check to see if this is really a route-addr */
76658144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
76758144Seric 		return FALSE;
76858144Seric 	start = strchr(addr, ':');
76958144Seric 	at = strrchr(addr, '@');
77058144Seric 	if (start == NULL || at == NULL || at < start)
77158144Seric 		return FALSE;
77258144Seric 
77358144Seric 	/* slice off the angle brackets */
77458144Seric 	strcpy(hostbuf, at + 1);
77558144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
77658144Seric 
77758144Seric 	while (start)
77858144Seric 	{
77959273Seric 		if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0)
78058144Seric 		{
78158144Seric 			strcpy(addr + 1, start + 1);
78258144Seric 			return TRUE;
78358144Seric 		}
78458144Seric 		c = *start;
78558144Seric 		*start = '\0';
78658144Seric 		comma = strrchr(addr, ',');
78758144Seric 		if (comma && comma[1] == '@')
78858144Seric 			strcpy(hostbuf, comma + 2);
78958144Seric 		else
79058144Seric 			comma = 0;
79158144Seric 		*start = c;
79258144Seric 		start = comma;
79358144Seric 	}
79458144Seric #endif
79558144Seric 	return FALSE;
79658144Seric }
797