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*64354Seric static char sccsid[] = "@(#)savemail.c	8.12 (Berkeley) 08/25/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)();
59297Seric 
607676Seric 	if (tTd(6, 1))
6158680Seric 	{
6259101Seric 		printf("\nsavemail, errormode = %c, id = %s\n  e_from=",
6359101Seric 			e->e_errormode, e->e_id == NULL ? "NONE" : e->e_id);
6458680Seric 		printaddr(&e->e_from, FALSE);
6558680Seric 	}
667361Seric 
6759101Seric 	if (e->e_id == NULL)
6859101Seric 	{
6959101Seric 		/* can't return a message with no id */
7059101Seric 		return;
7159101Seric 	}
7259101Seric 
739337Seric 	e->e_flags &= ~EF_FATALERRS;
74297Seric 
75297Seric 	/*
76297Seric 	**  In the unhappy event we don't know who to return the mail
77297Seric 	**  to, make someone up.
78297Seric 	*/
79297Seric 
809337Seric 	if (e->e_from.q_paddr == NULL)
81297Seric 	{
8258733Seric 		e->e_sender = "Postmaster";
8364284Seric 		if (parseaddr(e->e_sender, &e->e_from,
8464284Seric 			      RF_COPYPARSE|RF_SENDERADDR, '\0', NULL, e) == NULL)
85297Seric 		{
8658733Seric 			syserr("553 Cannot parse Postmaster!");
87297Seric 			ExitStat = EX_SOFTWARE;
88297Seric 			finis();
89297Seric 		}
90297Seric 	}
919337Seric 	e->e_to = NULL;
92297Seric 
93297Seric 	/*
9424942Seric 	**  Basic state machine.
9524942Seric 	**
9624942Seric 	**	This machine runs through the following states:
9724942Seric 	**
9824942Seric 	**	ESM_QUIET	Errors have already been printed iff the
9924942Seric 	**			sender is local.
10024942Seric 	**	ESM_REPORT	Report directly to the sender's terminal.
10124942Seric 	**	ESM_MAIL	Mail response to the sender.
10224942Seric 	**	ESM_DEADLETTER	Save response in ~/dead.letter.
10324942Seric 	**	ESM_POSTMASTER	Mail response to the postmaster.
10424942Seric 	**	ESM_PANIC	Save response anywhere possible.
105297Seric 	*/
106297Seric 
10724942Seric 	/* determine starting state */
10858734Seric 	switch (e->e_errormode)
109297Seric 	{
11024942Seric 	  case EM_WRITE:
11124942Seric 		state = ESM_REPORT;
11224942Seric 		break;
11324942Seric 
11424942Seric 	  case EM_BERKNET:
11524942Seric 		/* mail back, but return o.k. exit status */
116401Seric 		ExitStat = EX_OK;
11724942Seric 
11824942Seric 		/* fall through.... */
11924942Seric 
12024942Seric 	  case EM_MAIL:
12124942Seric 		state = ESM_MAIL;
12224942Seric 		break;
12324942Seric 
12424942Seric 	  case EM_PRINT:
12524979Seric 	  case '\0':
12624942Seric 		state = ESM_QUIET;
12724942Seric 		break;
12824942Seric 
12924942Seric 	  case EM_QUIET:
13024942Seric 		/* no need to return anything at all */
13124942Seric 		return;
13224979Seric 
13324979Seric 	  default:
13458734Seric 		syserr("554 savemail: bogus errormode x%x\n", e->e_errormode);
13524979Seric 		state = ESM_MAIL;
13624979Seric 		break;
137297Seric 	}
138297Seric 
13959094Seric 	/* if this is already an error response, send to postmaster */
14059094Seric 	if (bitset(EF_RESPONSE, e->e_flags))
14159094Seric 	{
14259094Seric 		if (e->e_parent != NULL &&
14359094Seric 		    bitset(EF_RESPONSE, e->e_parent->e_flags))
14459094Seric 		{
14559094Seric 			/* got an error sending a response -- can it */
14659094Seric 			return;
14759094Seric 		}
14859094Seric 		state = ESM_POSTMASTER;
14959094Seric 	}
15059094Seric 
15124942Seric 	while (state != ESM_DONE)
152297Seric 	{
15324979Seric 		if (tTd(6, 5))
15424979Seric 			printf("  state %d\n", state);
15524979Seric 
15624942Seric 		switch (state)
157297Seric 		{
15824979Seric 		  case ESM_QUIET:
15924979Seric 			if (e->e_from.q_mailer == LocalMailer)
16024979Seric 				state = ESM_DEADLETTER;
16124979Seric 			else
16224979Seric 				state = ESM_MAIL;
16324979Seric 			break;
16424979Seric 
16524942Seric 		  case ESM_REPORT:
16624942Seric 
16724942Seric 			/*
16824942Seric 			**  If the user is still logged in on the same terminal,
16924942Seric 			**  then write the error messages back to hir (sic).
17024942Seric 			*/
17124942Seric 
17224942Seric 			p = ttypath();
17324942Seric 			if (p == NULL || freopen(p, "w", stdout) == NULL)
17424942Seric 			{
17524942Seric 				state = ESM_MAIL;
17624942Seric 				break;
17724942Seric 			}
17824942Seric 
17958050Seric 			expand("\201n", buf, &buf[sizeof buf - 1], e);
1809375Seric 			printf("\r\nMessage from %s...\r\n", buf);
1819375Seric 			printf("Errors occurred while sending mail.\r\n");
1829542Seric 			if (e->e_xfp != NULL)
1839375Seric 			{
1849542Seric 				(void) fflush(e->e_xfp);
18524942Seric 				fp = fopen(queuename(e, 'x'), "r");
1869375Seric 			}
1879375Seric 			else
18824942Seric 				fp = NULL;
18924942Seric 			if (fp == NULL)
1909375Seric 			{
1919337Seric 				syserr("Cannot open %s", queuename(e, 'x'));
1929375Seric 				printf("Transcript of session is unavailable.\r\n");
1939375Seric 			}
1949375Seric 			else
1959375Seric 			{
1969375Seric 				printf("Transcript follows:\r\n");
19724942Seric 				while (fgets(buf, sizeof buf, fp) != NULL &&
1989375Seric 				       !ferror(stdout))
1999375Seric 					fputs(buf, stdout);
20058680Seric 				(void) xfclose(fp, "savemail transcript", e->e_id);
2019375Seric 			}
20224942Seric 			printf("Original message will be saved in dead.letter.\r\n");
20324942Seric 			state = ESM_DEADLETTER;
20424942Seric 			break;
205297Seric 
20624942Seric 		  case ESM_MAIL:
20724942Seric 			/*
20824942Seric 			**  If mailing back, do it.
20924942Seric 			**	Throw away all further output.  Don't alias,
21024942Seric 			**	since this could cause loops, e.g., if joe
21124942Seric 			**	mails to joe@x, and for some reason the network
21224942Seric 			**	for @x is down, then the response gets sent to
21324942Seric 			**	joe@x, which gives a response, etc.  Also force
21424942Seric 			**	the mail to be delivered even if a version of
21524942Seric 			**	it has already been sent to the sender.
21663841Seric 			**
21763841Seric 			**  If this is a configuration or local software
21863841Seric 			**	error, send to the local postmaster as well,
21963841Seric 			**	since the originator can't do anything
22063841Seric 			**	about it anyway.  Note that this is a full
22163841Seric 			**	copy of the message (intentionally) so that
22263841Seric 			**	the Postmaster can forward things along.
22324942Seric 			*/
224297Seric 
22563841Seric 			if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE)
22663841Seric 			{
22763841Seric 				(void) sendtolist("postmaster",
22864284Seric 					  NULLADDR, &e->e_errorqueue, e);
22963841Seric 			}
23058680Seric 			if (strcmp(e->e_from.q_paddr, "<>") != 0)
23163841Seric 			{
23258680Seric 				(void) sendtolist(e->e_from.q_paddr,
23364284Seric 					  NULLADDR, &e->e_errorqueue, e);
23463841Seric 			}
23558680Seric 
23663841Seric 			/*
23763841Seric 			**  Deliver a non-delivery report to the
23863841Seric 			**  Postmaster-designate (not necessarily
23963841Seric 			**  Postmaster).  This does not include the
24063841Seric 			**  body of the message, for privacy reasons.
24163841Seric 			**  You really shouldn't need this.
24263841Seric 			*/
24363841Seric 
24463849Seric 			e->e_flags |= EF_PM_NOTIFY;
24558178Seric 
24658680Seric 			q = e->e_errorqueue;
24758680Seric 			if (q == NULL)
24858680Seric 			{
24958680Seric 				/* this is an error-error */
25058680Seric 				state = ESM_POSTMASTER;
25158680Seric 				break;
25258680Seric 			}
25358966Seric 			if (returntosender(e->e_message,
25458680Seric 					   q, (e->e_class >= 0), e) == 0)
25558680Seric 			{
25658680Seric 				state = ESM_DONE;
25758680Seric 				break;
25858680Seric 			}
25924981Seric 
26058680Seric 			/* didn't work -- return to postmaster */
26158680Seric 			state = ESM_POSTMASTER;
26258680Seric 			break;
26358306Seric 
26458680Seric 		  case ESM_POSTMASTER:
26558680Seric 			/*
26658680Seric 			**  Similar to previous case, but to system postmaster.
26758680Seric 			*/
26858680Seric 
26959432Seric 			q = NULL;
27059432Seric 			if (sendtolist("postmaster", NULL, &q, e) <= 0)
27124942Seric 			{
27258680Seric 				syserr("553 cannot parse postmaster!");
27358680Seric 				ExitStat = EX_SOFTWARE;
27458680Seric 				state = ESM_USRTMP;
27558680Seric 				break;
27624942Seric 			}
27758966Seric 			if (returntosender(e->e_message,
27857438Seric 					   q, (e->e_class >= 0), e) == 0)
27924942Seric 			{
28024942Seric 				state = ESM_DONE;
28124942Seric 				break;
28224942Seric 			}
283297Seric 
28458680Seric 			/* didn't work -- last resort */
28558680Seric 			state = ESM_USRTMP;
28624942Seric 			break;
287297Seric 
28824942Seric 		  case ESM_DEADLETTER:
28924942Seric 			/*
29024942Seric 			**  Save the message in dead.letter.
29124942Seric 			**	If we weren't mailing back, and the user is
29224942Seric 			**	local, we should save the message in
29324942Seric 			**	~/dead.letter so that the poor person doesn't
29424942Seric 			**	have to type it over again -- and we all know
29524942Seric 			**	what poor typists UNIX users are.
29624942Seric 			*/
2975315Seric 
29824942Seric 			p = NULL;
29924942Seric 			if (e->e_from.q_mailer == LocalMailer)
30024942Seric 			{
30124942Seric 				if (e->e_from.q_home != NULL)
30224942Seric 					p = e->e_from.q_home;
30324942Seric 				else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
30424942Seric 					p = pw->pw_dir;
30524942Seric 			}
30624942Seric 			if (p == NULL)
30724942Seric 			{
30858865Seric 				/* no local directory */
30924942Seric 				state = ESM_MAIL;
31024942Seric 				break;
31124942Seric 			}
31224942Seric 			if (e->e_dfp != NULL)
31324942Seric 			{
31424942Seric 				bool oldverb = Verbose;
31524942Seric 
31624942Seric 				/* we have a home directory; open dead.letter */
31724942Seric 				define('z', p, e);
31858050Seric 				expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e);
31924942Seric 				Verbose = TRUE;
32058151Seric 				message("Saving message in %s", buf);
32124942Seric 				Verbose = oldverb;
32224942Seric 				e->e_to = buf;
32324942Seric 				q = NULL;
32458082Seric 				(void) sendtolist(buf, &e->e_from, &q, e);
325*64354Seric 				if (q != NULL &&
326*64354Seric 				    !bitset(QBADADDR, q->q_flags) &&
327*64354Seric 				    deliver(e, q) == 0)
32824942Seric 					state = ESM_DONE;
32924942Seric 				else
33024942Seric 					state = ESM_MAIL;
33124942Seric 			}
33225569Seric 			else
33325569Seric 			{
33425569Seric 				/* no data file -- try mailing back */
33525569Seric 				state = ESM_MAIL;
33625569Seric 			}
33724942Seric 			break;
33824942Seric 
33924942Seric 		  case ESM_USRTMP:
34024942Seric 			/*
34124942Seric 			**  Log the mail in /usr/tmp/dead.letter.
34224942Seric 			*/
34324942Seric 
34457438Seric 			if (e->e_class < 0)
34557438Seric 			{
34657438Seric 				state = ESM_DONE;
34757438Seric 				break;
34857438Seric 			}
34957438Seric 
35059745Seric 			fp = dfopen("/usr/tmp/dead.letter",
35159745Seric 				    O_WRONLY|O_CREAT|O_APPEND, FileMode);
35224942Seric 			if (fp == NULL)
35324942Seric 			{
35424942Seric 				state = ESM_PANIC;
35524942Seric 				break;
35624942Seric 			}
35724942Seric 
35858010Seric 			putfromline(fp, FileMailer, e);
35958010Seric 			(*e->e_puthdr)(fp, FileMailer, e);
36058010Seric 			putline("\n", fp, FileMailer);
36159730Seric 			(*e->e_putbody)(fp, FileMailer, e, NULL);
36258010Seric 			putline("\n", fp, FileMailer);
36324942Seric 			(void) fflush(fp);
36424942Seric 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
36558680Seric 			(void) xfclose(fp, "savemail", "/usr/tmp/dead.letter");
36624942Seric 			break;
36724942Seric 
36824942Seric 		  default:
36958151Seric 			syserr("554 savemail: unknown state %d", state);
37024942Seric 
37124942Seric 			/* fall through ... */
37224942Seric 
37324942Seric 		  case ESM_PANIC:
37424942Seric 			/* leave the locked queue & transcript files around */
37558151Seric 			syserr("554 savemail: cannot save rejected email anywhere");
37624942Seric 			exit(EX_SOFTWARE);
37724942Seric 		}
378297Seric 	}
379297Seric }
380297Seric /*
3814633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
3824633Seric **
3834633Seric **	Parameters:
3844633Seric **		msg -- the explanatory message.
38516479Seric **		returnq -- the queue of people to send the message to.
3865984Seric **		sendbody -- if TRUE, also send back the body of the
3875984Seric **			message; otherwise just send the header.
38855012Seric **		e -- the current envelope.
3894633Seric **
3904633Seric **	Returns:
3914633Seric **		zero -- if everything went ok.
3924633Seric **		else -- some error.
3934633Seric **
3944633Seric **	Side Effects:
3954633Seric **		Returns the current message to the sender via
3964633Seric **		mail.
3974633Seric */
3984633Seric 
3995984Seric static bool	SendBody;
4004633Seric 
4017045Seric #define MAXRETURNS	6	/* max depth of returning messages */
40258559Seric #define ERRORFUDGE	100	/* nominal size of error message text */
4037045Seric 
40455012Seric returntosender(msg, returnq, sendbody, e)
4054633Seric 	char *msg;
40616479Seric 	ADDRESS *returnq;
4075984Seric 	bool sendbody;
40855012Seric 	register ENVELOPE *e;
4094633Seric {
4104633Seric 	char buf[MAXNAME];
4116978Seric 	extern putheader(), errbody();
4126978Seric 	register ENVELOPE *ee;
41358680Seric 	ENVELOPE *oldcur = CurEnv;
4146978Seric 	ENVELOPE errenvelope;
4157045Seric 	static int returndepth;
4169375Seric 	register ADDRESS *q;
4174633Seric 
41860010Seric 	if (returnq == NULL)
41960010Seric 		return (-1);
42060010Seric 
42158966Seric 	if (msg == NULL)
42258966Seric 		msg = "Unable to deliver mail";
42358966Seric 
4247676Seric 	if (tTd(6, 1))
4257287Seric 	{
42658680Seric 		printf("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
42755012Seric 		       msg, returndepth, e);
42816479Seric 		printaddr(returnq, TRUE);
4297287Seric 	}
4307287Seric 
4317045Seric 	if (++returndepth >= MAXRETURNS)
4327045Seric 	{
4337045Seric 		if (returndepth != MAXRETURNS)
43458151Seric 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
4357045Seric 		/* don't "unrecurse" and fake a clean exit */
4367045Seric 		/* returndepth--; */
4377045Seric 		return (0);
4387045Seric 	}
4397045Seric 
4405984Seric 	SendBody = sendbody;
44158680Seric 	define('g', e->e_from.q_paddr, e);
44258179Seric 	ee = newenvelope(&errenvelope, e);
44358050Seric 	define('a', "\201b", ee);
44459057Seric 	define('r', "internal", ee);
44559057Seric 	define('s', "localhost", ee);
44659057Seric 	define('_', "localhost", ee);
4476978Seric 	ee->e_puthdr = putheader;
4486978Seric 	ee->e_putbody = errbody;
44964120Seric 	ee->e_flags |= EF_RESPONSE|EF_METOO;
45055012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
45145155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
45216479Seric 	ee->e_sendqueue = returnq;
45358559Seric 	ee->e_msgsize = e->e_msgsize + ERRORFUDGE;
4549542Seric 	openxscript(ee);
45516479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4569375Seric 	{
45759989Seric 		if (bitset(QBADADDR, q->q_flags))
45858559Seric 			continue;
45958559Seric 
46059989Seric 		if (!bitset(QDONTSEND, q->q_flags))
46159989Seric 			ee->e_nrcpts++;
46258559Seric 
46358144Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
46464284Seric 			parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e);
46558144Seric 
4669375Seric 		if (q->q_alias == NULL)
46759580Seric 			addheader("To", q->q_paddr, ee);
4689375Seric 	}
46924942Seric 
47057642Seric # ifdef LOG
47158020Seric 	if (LogLevel > 5)
47257642Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
47357642Seric 			e->e_id, ee->e_id, msg);
47457642Seric # endif
47557642Seric 
47610845Seric 	(void) sprintf(buf, "Returned mail: %s", msg);
47759580Seric 	addheader("Subject", buf, ee);
47859730Seric 	if (SendMIMEErrors)
47959730Seric 	{
48059987Seric 		addheader("MIME-Version", "1.0", ee);
48159730Seric 		(void) sprintf(buf, "%s.%ld/%s",
48259730Seric 			ee->e_id, curtime(), MyHostName);
48359730Seric 		ee->e_msgboundary = newstr(buf);
48459730Seric 		(void) sprintf(buf, "multipart/mixed; boundary=\"%s\"",
48559730Seric 					ee->e_msgboundary);
48659730Seric 		addheader("Content-Type", buf, ee);
48759730Seric 	}
4884633Seric 
4894633Seric 	/* fake up an address header for the from person */
49058050Seric 	expand("\201n", buf, &buf[sizeof buf - 1], e);
49164284Seric 	if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL)
4924633Seric 	{
49358151Seric 		syserr("553 Can't parse myself!");
4944633Seric 		ExitStat = EX_SOFTWARE;
4957045Seric 		returndepth--;
4964633Seric 		return (-1);
4974633Seric 	}
49858704Seric 	ee->e_sender = ee->e_from.q_paddr;
4995984Seric 
5006978Seric 	/* push state into submessage */
5016978Seric 	CurEnv = ee;
50258050Seric 	define('f', "\201n", ee);
5039375Seric 	define('x', "Mail Delivery Subsystem", ee);
50458929Seric 	eatheader(ee, TRUE);
5055984Seric 
50663753Seric 	/* mark statistics */
50764284Seric 	markstats(ee, NULLADDR);
50863753Seric 
5096978Seric 	/* actually deliver the error message */
51014876Seric 	sendall(ee, SM_DEFAULT);
5116978Seric 
5126978Seric 	/* restore state */
5137811Seric 	dropenvelope(ee);
51458680Seric 	CurEnv = oldcur;
5157045Seric 	returndepth--;
5166978Seric 
5177045Seric 	/* should check for delivery errors here */
5184633Seric 	return (0);
5194633Seric }
5204633Seric /*
5216978Seric **  ERRBODY -- output the body of an error message.
5226978Seric **
5236978Seric **	Typically this is a copy of the transcript plus a copy of the
5246978Seric **	original offending message.
5256978Seric **
526297Seric **	Parameters:
527297Seric **		fp -- the output file.
52810170Seric **		m -- the mailer to output to.
5299542Seric **		e -- the envelope we are working in.
530297Seric **
531297Seric **	Returns:
532297Seric **		none
533297Seric **
534297Seric **	Side Effects:
5356978Seric **		Outputs the body of an error message.
536297Seric */
537297Seric 
53810170Seric errbody(fp, m, e)
539297Seric 	register FILE *fp;
5404318Seric 	register struct mailer *m;
5419542Seric 	register ENVELOPE *e;
542297Seric {
5436978Seric 	register FILE *xfile;
54459082Seric 	char *p;
54559082Seric 	register ADDRESS *q;
54659082Seric 	bool printheader;
5473189Seric 	char buf[MAXLINE];
548297Seric 
54958680Seric 	if (e->e_parent == NULL)
55058680Seric 	{
55158680Seric 		syserr("errbody: null parent");
55258680Seric 		putline("   ----- Original message lost -----\n", fp, m);
55358680Seric 		return;
55458680Seric 	}
55558680Seric 
5569057Seric 	/*
55759730Seric 	**  Output MIME header.
55859730Seric 	*/
55959730Seric 
56059730Seric 	if (e->e_msgboundary != NULL)
56159730Seric 	{
56259730Seric 		putline("This is a MIME-encapsulated message", fp, m);
56359730Seric 		putline("", fp, m);
56459730Seric 		(void) sprintf(buf, "--%s", e->e_msgboundary);
56559730Seric 		putline(buf, fp, m);
56659730Seric 		putline("", fp, m);
56759730Seric 	}
56859730Seric 
56959730Seric 	/*
57063852Seric 	**  Output introductory information.
57163852Seric 	*/
57263852Seric 
57363852Seric 	sprintf(buf, "The original message was received at %s", arpadate(NULL));
57463852Seric 	putline(buf, fp, m);
57564025Seric 	expand("from \201_", buf, &buf[sizeof buf - 1], e->e_parent);
57663852Seric 	putline(buf, fp, m);
57763852Seric 	putline("", fp, m);
57863852Seric 
57963852Seric 	/*
58055372Seric 	**  Output error message header (if specified and available).
58155372Seric 	*/
58255372Seric 
58355372Seric 	if (ErrMsgFile != NULL)
58455372Seric 	{
58555372Seric 		if (*ErrMsgFile == '/')
58655372Seric 		{
58755372Seric 			xfile = fopen(ErrMsgFile, "r");
58855372Seric 			if (xfile != NULL)
58955372Seric 			{
59055372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
59155425Seric 				{
59255425Seric 					expand(buf, buf, &buf[sizeof buf - 1], e);
59355372Seric 					putline(buf, fp, m);
59455425Seric 				}
59555372Seric 				(void) fclose(xfile);
59659082Seric 				putline("\n", fp, m);
59755372Seric 			}
59855372Seric 		}
59955372Seric 		else
60055372Seric 		{
60155425Seric 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
60255425Seric 			putline(buf, fp, m);
60359730Seric 			putline("", fp, m);
60455372Seric 		}
60555372Seric 	}
60655372Seric 
60755372Seric 	/*
60859082Seric 	**  Output message introduction
60959082Seric 	*/
61059082Seric 
61159082Seric 	printheader = TRUE;
61259082Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
61359082Seric 	{
61463787Seric 		if (bitset(QBADADDR|QREPORT, q->q_flags))
61559082Seric 		{
61659082Seric 			if (printheader)
61759082Seric 			{
61863787Seric 				putline("   ----- The following addresses had delivery problems -----",
61959082Seric 					fp, m);
62059082Seric 				printheader = FALSE;
62159082Seric 			}
62263849Seric 			strcpy(buf, q->q_paddr);
62363787Seric 			if (bitset(QBADADDR, q->q_flags))
62463849Seric 				strcat(buf, "  (unrecoverable error)");
62563787Seric 			else
62663849Seric 				strcat(buf, "  (transient failure)");
62763787Seric 			putline(buf, fp, m);
62863849Seric 			if (q->q_alias != NULL)
62963849Seric 			{
63063849Seric 				strcpy(buf, "    (expanded from: ");
63163849Seric 				strcat(buf, q->q_alias->q_paddr);
63263849Seric 				strcat(buf, ")");
63363849Seric 				putline(buf, fp, m);
63463849Seric 			}
63559082Seric 		}
63659082Seric 	}
63759082Seric 	if (!printheader)
63859082Seric 		putline("\n", fp, m);
63959082Seric 
64059082Seric 	/*
6419057Seric 	**  Output transcript of errors
6429057Seric 	*/
6439057Seric 
6444086Seric 	(void) fflush(stdout);
6459542Seric 	p = queuename(e->e_parent, 'x');
6469337Seric 	if ((xfile = fopen(p, "r")) == NULL)
6479057Seric 	{
6489337Seric 		syserr("Cannot open %s", p);
64959082Seric 		putline("   ----- Transcript of session is unavailable -----\n", fp, m);
6509057Seric 	}
6519057Seric 	else
6529057Seric 	{
65358680Seric 		putline("   ----- Transcript of session follows -----\n", fp, m);
6549542Seric 		if (e->e_xfp != NULL)
6559542Seric 			(void) fflush(e->e_xfp);
6569057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
65710170Seric 			putline(buf, fp, m);
65858680Seric 		(void) xfclose(xfile, "errbody xscript", p);
6599057Seric 	}
660297Seric 	errno = 0;
6614318Seric 
6624318Seric 	/*
6634318Seric 	**  Output text of original message
6644318Seric 	*/
6654318Seric 
6664289Seric 	if (NoReturn)
66758665Seric 		SendBody = FALSE;
66859730Seric 	putline("", fp, m);
66958680Seric 	if (e->e_parent->e_df != NULL)
6704199Seric 	{
6715984Seric 		if (SendBody)
67263852Seric 			putline("   ----- Original message follows -----\n", fp, m);
6735984Seric 		else
67459987Seric 			putline("   ----- Message header follows -----\n", fp, m);
67559730Seric 		(void) fflush(fp);
67659730Seric 
67759730Seric 		if (e->e_msgboundary != NULL)
6785984Seric 		{
67959730Seric 			putline("", fp, m);
68059730Seric 			(void) sprintf(buf, "--%s", e->e_msgboundary);
68159730Seric 			putline(buf, fp, m);
68259987Seric 			putline("Content-Type: message/rfc822", fp, m);
68359730Seric 			putline("", fp, m);
6845984Seric 		}
68559730Seric 		putheader(fp, m, e->e_parent);
68659730Seric 		putline("", fp, m);
68759730Seric 		if (SendBody)
68859730Seric 			putbody(fp, m, e->e_parent, e->e_msgboundary);
68959987Seric 		else
69060010Seric 			putline("   ----- Message body suppressed -----", fp, m);
6914199Seric 	}
6924199Seric 	else
69310170Seric 	{
69410170Seric 		putline("  ----- No message was collected -----\n", fp, m);
69510170Seric 	}
6964318Seric 
69760010Seric 	if (e->e_msgboundary != NULL)
69860010Seric 	{
69960010Seric 		putline("", fp, m);
70060010Seric 		(void) sprintf(buf, "--%s--", e->e_msgboundary);
70160010Seric 		putline(buf, fp, m);
70260010Seric 	}
70359730Seric 	putline("", fp, m);
70459730Seric 
7054318Seric 	/*
7064318Seric 	**  Cleanup and exit
7074318Seric 	*/
7084318Seric 
709297Seric 	if (errno != 0)
7106978Seric 		syserr("errbody: I/O error");
711297Seric }
71258144Seric /*
71358144Seric **  PRUNEROUTE -- prune an RFC-822 source route
71458144Seric **
71558144Seric **	Trims down a source route to the last internet-registered hop.
71658144Seric **	This is encouraged by RFC 1123 section 5.3.3.
71758144Seric **
71858144Seric **	Parameters:
71958144Seric **		addr -- the address
72058144Seric **
72158144Seric **	Returns:
72258144Seric **		TRUE -- address was modified
72358144Seric **		FALSE -- address could not be pruned
72458144Seric **
72558144Seric **	Side Effects:
72658144Seric **		modifies addr in-place
72758144Seric */
72858144Seric 
72958144Seric pruneroute(addr)
73058144Seric 	char *addr;
73158144Seric {
73258144Seric #ifdef NAMED_BIND
73358144Seric 	char *start, *at, *comma;
73458144Seric 	char c;
73558144Seric 	int rcode;
73658144Seric 	char hostbuf[BUFSIZ];
73758144Seric 	char *mxhosts[MAXMXHOSTS + 1];
73858144Seric 
73958144Seric 	/* check to see if this is really a route-addr */
74058144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
74158144Seric 		return FALSE;
74258144Seric 	start = strchr(addr, ':');
74358144Seric 	at = strrchr(addr, '@');
74458144Seric 	if (start == NULL || at == NULL || at < start)
74558144Seric 		return FALSE;
74658144Seric 
74758144Seric 	/* slice off the angle brackets */
74858144Seric 	strcpy(hostbuf, at + 1);
74958144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
75058144Seric 
75158144Seric 	while (start)
75258144Seric 	{
75359273Seric 		if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0)
75458144Seric 		{
75558144Seric 			strcpy(addr + 1, start + 1);
75658144Seric 			return TRUE;
75758144Seric 		}
75858144Seric 		c = *start;
75958144Seric 		*start = '\0';
76058144Seric 		comma = strrchr(addr, ',');
76158144Seric 		if (comma && comma[1] == '@')
76258144Seric 			strcpy(hostbuf, comma + 2);
76358144Seric 		else
76458144Seric 			comma = 0;
76558144Seric 		*start = c;
76658144Seric 		start = comma;
76758144Seric 	}
76858144Seric #endif
76958144Seric 	return FALSE;
77058144Seric }
771