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*63841Seric static char sccsid[] = "@(#)savemail.c	8.4 (Berkeley) 07/16/93";
1133731Sbostic #endif /* not lint */
1222711Sdist 
13297Seric # include <pwd.h>
143313Seric # include "sendmail.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";
8358704Seric 		if (parseaddr(e->e_sender, &e->e_from, 0, '\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.
215*63841Seric 			**
216*63841Seric 			**  If this is a configuration or local software
217*63841Seric 			**	error, send to the local postmaster as well,
218*63841Seric 			**	since the originator can't do anything
219*63841Seric 			**	about it anyway.  Note that this is a full
220*63841Seric 			**	copy of the message (intentionally) so that
221*63841Seric 			**	the Postmaster can forward things along.
22224942Seric 			*/
223297Seric 
224*63841Seric 			if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE)
225*63841Seric 			{
226*63841Seric 				(void) sendtolist("postmaster",
227*63841Seric 					  (ADDRESS *) NULL,
228*63841Seric 					  &e->e_errorqueue, e);
229*63841Seric 			}
23058680Seric 			if (strcmp(e->e_from.q_paddr, "<>") != 0)
231*63841Seric 			{
23258680Seric 				(void) sendtolist(e->e_from.q_paddr,
23358680Seric 					  (ADDRESS *) NULL,
23458680Seric 					  &e->e_errorqueue, e);
235*63841Seric 			}
23658680Seric 
237*63841Seric 			/*
238*63841Seric 			**  Deliver a non-delivery report to the
239*63841Seric 			**  Postmaster-designate (not necessarily
240*63841Seric 			**  Postmaster).  This does not include the
241*63841Seric 			**  body of the message, for privacy reasons.
242*63841Seric 			**  You really shouldn't need this.
243*63841Seric 			*/
244*63841Seric 
24558680Seric 			if (PostMasterCopy != NULL)
24624942Seric 			{
24758680Seric 				auto ADDRESS *rlist = NULL;
24858178Seric 
24958680Seric 				(void) sendtolist(PostMasterCopy,
25058304Seric 						  (ADDRESS *) NULL,
25158680Seric 						  &rlist, e);
25258680Seric 				(void) returntosender(e->e_message,
25358680Seric 						      rlist, FALSE, e);
25458680Seric 			}
255*63841Seric 
25658680Seric 			q = e->e_errorqueue;
25758680Seric 			if (q == NULL)
25858680Seric 			{
25958680Seric 				/* this is an error-error */
26058680Seric 				state = ESM_POSTMASTER;
26158680Seric 				break;
26258680Seric 			}
26358966Seric 			if (returntosender(e->e_message,
26458680Seric 					   q, (e->e_class >= 0), e) == 0)
26558680Seric 			{
26658680Seric 				state = ESM_DONE;
26758680Seric 				break;
26858680Seric 			}
26924981Seric 
27058680Seric 			/* didn't work -- return to postmaster */
27158680Seric 			state = ESM_POSTMASTER;
27258680Seric 			break;
27358306Seric 
27458680Seric 		  case ESM_POSTMASTER:
27558680Seric 			/*
27658680Seric 			**  Similar to previous case, but to system postmaster.
27758680Seric 			*/
27858680Seric 
27959432Seric 			q = NULL;
28059432Seric 			if (sendtolist("postmaster", NULL, &q, e) <= 0)
28124942Seric 			{
28258680Seric 				syserr("553 cannot parse postmaster!");
28358680Seric 				ExitStat = EX_SOFTWARE;
28458680Seric 				state = ESM_USRTMP;
28558680Seric 				break;
28624942Seric 			}
28758966Seric 			if (returntosender(e->e_message,
28857438Seric 					   q, (e->e_class >= 0), 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;
30924942Seric 			if (e->e_from.q_mailer == LocalMailer)
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;
33458082Seric 				(void) sendtolist(buf, &e->e_from, &q, e);
33524942Seric 				if (deliver(e, q) == 0)
33624942Seric 					state = ESM_DONE;
33724942Seric 				else
33824942Seric 					state = ESM_MAIL;
33924942Seric 			}
34025569Seric 			else
34125569Seric 			{
34225569Seric 				/* no data file -- try mailing back */
34325569Seric 				state = ESM_MAIL;
34425569Seric 			}
34524942Seric 			break;
34624942Seric 
34724942Seric 		  case ESM_USRTMP:
34824942Seric 			/*
34924942Seric 			**  Log the mail in /usr/tmp/dead.letter.
35024942Seric 			*/
35124942Seric 
35257438Seric 			if (e->e_class < 0)
35357438Seric 			{
35457438Seric 				state = ESM_DONE;
35557438Seric 				break;
35657438Seric 			}
35757438Seric 
35859745Seric 			fp = dfopen("/usr/tmp/dead.letter",
35959745Seric 				    O_WRONLY|O_CREAT|O_APPEND, FileMode);
36024942Seric 			if (fp == NULL)
36124942Seric 			{
36224942Seric 				state = ESM_PANIC;
36324942Seric 				break;
36424942Seric 			}
36524942Seric 
36658010Seric 			putfromline(fp, FileMailer, e);
36758010Seric 			(*e->e_puthdr)(fp, FileMailer, e);
36858010Seric 			putline("\n", fp, FileMailer);
36959730Seric 			(*e->e_putbody)(fp, FileMailer, e, NULL);
37058010Seric 			putline("\n", fp, FileMailer);
37124942Seric 			(void) fflush(fp);
37224942Seric 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
37358680Seric 			(void) xfclose(fp, "savemail", "/usr/tmp/dead.letter");
37424942Seric 			break;
37524942Seric 
37624942Seric 		  default:
37758151Seric 			syserr("554 savemail: unknown state %d", state);
37824942Seric 
37924942Seric 			/* fall through ... */
38024942Seric 
38124942Seric 		  case ESM_PANIC:
38224942Seric 			/* leave the locked queue & transcript files around */
38358151Seric 			syserr("554 savemail: cannot save rejected email anywhere");
38424942Seric 			exit(EX_SOFTWARE);
38524942Seric 		}
386297Seric 	}
387297Seric }
388297Seric /*
3894633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
3904633Seric **
3914633Seric **	Parameters:
3924633Seric **		msg -- the explanatory message.
39316479Seric **		returnq -- the queue of people to send the message to.
3945984Seric **		sendbody -- if TRUE, also send back the body of the
3955984Seric **			message; otherwise just send the header.
39655012Seric **		e -- the current envelope.
3974633Seric **
3984633Seric **	Returns:
3994633Seric **		zero -- if everything went ok.
4004633Seric **		else -- some error.
4014633Seric **
4024633Seric **	Side Effects:
4034633Seric **		Returns the current message to the sender via
4044633Seric **		mail.
4054633Seric */
4064633Seric 
4075984Seric static bool	SendBody;
4084633Seric 
4097045Seric #define MAXRETURNS	6	/* max depth of returning messages */
41058559Seric #define ERRORFUDGE	100	/* nominal size of error message text */
4117045Seric 
41255012Seric returntosender(msg, returnq, sendbody, e)
4134633Seric 	char *msg;
41416479Seric 	ADDRESS *returnq;
4155984Seric 	bool sendbody;
41655012Seric 	register ENVELOPE *e;
4174633Seric {
4184633Seric 	char buf[MAXNAME];
4196978Seric 	extern putheader(), errbody();
4206978Seric 	register ENVELOPE *ee;
42158680Seric 	ENVELOPE *oldcur = CurEnv;
4226978Seric 	ENVELOPE errenvelope;
4237045Seric 	static int returndepth;
4249375Seric 	register ADDRESS *q;
4254633Seric 
42660010Seric 	if (returnq == NULL)
42760010Seric 		return (-1);
42860010Seric 
42958966Seric 	if (msg == NULL)
43058966Seric 		msg = "Unable to deliver mail";
43158966Seric 
4327676Seric 	if (tTd(6, 1))
4337287Seric 	{
43458680Seric 		printf("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
43555012Seric 		       msg, returndepth, e);
43616479Seric 		printaddr(returnq, TRUE);
4377287Seric 	}
4387287Seric 
4397045Seric 	if (++returndepth >= MAXRETURNS)
4407045Seric 	{
4417045Seric 		if (returndepth != MAXRETURNS)
44258151Seric 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
4437045Seric 		/* don't "unrecurse" and fake a clean exit */
4447045Seric 		/* returndepth--; */
4457045Seric 		return (0);
4467045Seric 	}
4477045Seric 
4485984Seric 	SendBody = sendbody;
44958680Seric 	define('g', e->e_from.q_paddr, e);
45058179Seric 	ee = newenvelope(&errenvelope, e);
45158050Seric 	define('a', "\201b", ee);
45259057Seric 	define('r', "internal", ee);
45359057Seric 	define('s', "localhost", ee);
45459057Seric 	define('_', "localhost", ee);
4556978Seric 	ee->e_puthdr = putheader;
4566978Seric 	ee->e_putbody = errbody;
4579375Seric 	ee->e_flags |= EF_RESPONSE;
45855012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
45945155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
46016479Seric 	ee->e_sendqueue = returnq;
46158559Seric 	ee->e_msgsize = e->e_msgsize + ERRORFUDGE;
4629542Seric 	openxscript(ee);
46316479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4649375Seric 	{
46559989Seric 		if (bitset(QBADADDR, q->q_flags))
46658559Seric 			continue;
46758559Seric 
46859989Seric 		if (!bitset(QDONTSEND, q->q_flags))
46959989Seric 			ee->e_nrcpts++;
47058559Seric 
47158144Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
47258333Seric 			parseaddr(q->q_paddr, q, 0, '\0', NULL, e);
47358144Seric 
4749375Seric 		if (q->q_alias == NULL)
47559580Seric 			addheader("To", q->q_paddr, ee);
4769375Seric 	}
47724942Seric 
47857642Seric # ifdef LOG
47958020Seric 	if (LogLevel > 5)
48057642Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
48157642Seric 			e->e_id, ee->e_id, msg);
48257642Seric # endif
48357642Seric 
48410845Seric 	(void) sprintf(buf, "Returned mail: %s", msg);
48559580Seric 	addheader("Subject", buf, ee);
48659730Seric 	if (SendMIMEErrors)
48759730Seric 	{
48859987Seric 		addheader("MIME-Version", "1.0", ee);
48959730Seric 		(void) sprintf(buf, "%s.%ld/%s",
49059730Seric 			ee->e_id, curtime(), MyHostName);
49159730Seric 		ee->e_msgboundary = newstr(buf);
49259730Seric 		(void) sprintf(buf, "multipart/mixed; boundary=\"%s\"",
49359730Seric 					ee->e_msgboundary);
49459730Seric 		addheader("Content-Type", buf, ee);
49559730Seric 	}
4964633Seric 
4974633Seric 	/* fake up an address header for the from person */
49858050Seric 	expand("\201n", buf, &buf[sizeof buf - 1], e);
49958680Seric 	if (parseaddr(buf, &ee->e_from, 1, '\0', NULL, e) == NULL)
5004633Seric 	{
50158151Seric 		syserr("553 Can't parse myself!");
5024633Seric 		ExitStat = EX_SOFTWARE;
5037045Seric 		returndepth--;
5044633Seric 		return (-1);
5054633Seric 	}
50658704Seric 	ee->e_sender = ee->e_from.q_paddr;
5075984Seric 
5086978Seric 	/* push state into submessage */
5096978Seric 	CurEnv = ee;
51058050Seric 	define('f', "\201n", ee);
5119375Seric 	define('x', "Mail Delivery Subsystem", ee);
51258929Seric 	eatheader(ee, TRUE);
5135984Seric 
51463753Seric 	/* mark statistics */
51563753Seric 	markstats(ee, (ADDRESS *) NULL);
51663753Seric 
5176978Seric 	/* actually deliver the error message */
51814876Seric 	sendall(ee, SM_DEFAULT);
5196978Seric 
5206978Seric 	/* restore state */
5217811Seric 	dropenvelope(ee);
52258680Seric 	CurEnv = oldcur;
5237045Seric 	returndepth--;
5246978Seric 
5257045Seric 	/* should check for delivery errors here */
5264633Seric 	return (0);
5274633Seric }
5284633Seric /*
5296978Seric **  ERRBODY -- output the body of an error message.
5306978Seric **
5316978Seric **	Typically this is a copy of the transcript plus a copy of the
5326978Seric **	original offending message.
5336978Seric **
534297Seric **	Parameters:
535297Seric **		fp -- the output file.
53610170Seric **		m -- the mailer to output to.
5379542Seric **		e -- the envelope we are working in.
538297Seric **
539297Seric **	Returns:
540297Seric **		none
541297Seric **
542297Seric **	Side Effects:
5436978Seric **		Outputs the body of an error message.
544297Seric */
545297Seric 
54610170Seric errbody(fp, m, e)
547297Seric 	register FILE *fp;
5484318Seric 	register struct mailer *m;
5499542Seric 	register ENVELOPE *e;
550297Seric {
5516978Seric 	register FILE *xfile;
55259082Seric 	char *p;
55359082Seric 	register ADDRESS *q;
55459082Seric 	bool printheader;
5553189Seric 	char buf[MAXLINE];
556297Seric 
55758680Seric 	if (e->e_parent == NULL)
55858680Seric 	{
55958680Seric 		syserr("errbody: null parent");
56058680Seric 		putline("   ----- Original message lost -----\n", fp, m);
56158680Seric 		return;
56258680Seric 	}
56358680Seric 
5649057Seric 	/*
56559730Seric 	**  Output MIME header.
56659730Seric 	*/
56759730Seric 
56859730Seric 	if (e->e_msgboundary != NULL)
56959730Seric 	{
57059730Seric 		putline("This is a MIME-encapsulated message", fp, m);
57159730Seric 		putline("", fp, m);
57259730Seric 		(void) sprintf(buf, "--%s", e->e_msgboundary);
57359730Seric 		putline(buf, fp, m);
57459730Seric 		putline("", fp, m);
57559730Seric 	}
57659730Seric 
57759730Seric 	/*
57855372Seric 	**  Output error message header (if specified and available).
57955372Seric 	*/
58055372Seric 
58155372Seric 	if (ErrMsgFile != NULL)
58255372Seric 	{
58355372Seric 		if (*ErrMsgFile == '/')
58455372Seric 		{
58555372Seric 			xfile = fopen(ErrMsgFile, "r");
58655372Seric 			if (xfile != NULL)
58755372Seric 			{
58855372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
58955425Seric 				{
59055425Seric 					expand(buf, buf, &buf[sizeof buf - 1], e);
59155372Seric 					putline(buf, fp, m);
59255425Seric 				}
59355372Seric 				(void) fclose(xfile);
59459082Seric 				putline("\n", fp, m);
59555372Seric 			}
59655372Seric 		}
59755372Seric 		else
59855372Seric 		{
59955425Seric 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
60055425Seric 			putline(buf, fp, m);
60159730Seric 			putline("", fp, m);
60255372Seric 		}
60355372Seric 	}
60455372Seric 
60555372Seric 	/*
60659082Seric 	**  Output message introduction
60759082Seric 	*/
60859082Seric 
60959082Seric 	printheader = TRUE;
61059082Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
61159082Seric 	{
61263787Seric 		if (bitset(QBADADDR|QREPORT, q->q_flags))
61359082Seric 		{
61459082Seric 			if (printheader)
61559082Seric 			{
61663787Seric 				putline("   ----- The following addresses had delivery problems -----",
61759082Seric 					fp, m);
61859082Seric 				printheader = FALSE;
61959082Seric 			}
62059113Seric 			if (q->q_alias != NULL)
62163787Seric 				strcpy(buf, q->q_alias->q_paddr);
62259113Seric 			else
62363787Seric 				strcpy(buf, q->q_paddr);
62463787Seric 			if (bitset(QBADADDR, q->q_flags))
62563787Seric 				strcat(buf, "  (hard error -- address deleted)");
62663787Seric 			else
62763787Seric 				strcat(buf, "  (temporary failure -- will retry)");
62863787Seric 			putline(buf, fp, m);
62959082Seric 		}
63059082Seric 	}
63159082Seric 	if (!printheader)
63259082Seric 		putline("\n", fp, m);
63359082Seric 
63459082Seric 	/*
6359057Seric 	**  Output transcript of errors
6369057Seric 	*/
6379057Seric 
6384086Seric 	(void) fflush(stdout);
6399542Seric 	p = queuename(e->e_parent, 'x');
6409337Seric 	if ((xfile = fopen(p, "r")) == NULL)
6419057Seric 	{
6429337Seric 		syserr("Cannot open %s", p);
64359082Seric 		putline("   ----- Transcript of session is unavailable -----\n", fp, m);
6449057Seric 	}
6459057Seric 	else
6469057Seric 	{
64758680Seric 		putline("   ----- Transcript of session follows -----\n", fp, m);
6489542Seric 		if (e->e_xfp != NULL)
6499542Seric 			(void) fflush(e->e_xfp);
6509057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
65110170Seric 			putline(buf, fp, m);
65258680Seric 		(void) xfclose(xfile, "errbody xscript", p);
6539057Seric 	}
654297Seric 	errno = 0;
6554318Seric 
6564318Seric 	/*
6574318Seric 	**  Output text of original message
6584318Seric 	*/
6594318Seric 
6604289Seric 	if (NoReturn)
66158665Seric 		SendBody = FALSE;
66259730Seric 	putline("", fp, m);
66358680Seric 	if (e->e_parent->e_df != NULL)
6644199Seric 	{
6655984Seric 		if (SendBody)
66610170Seric 			putline("   ----- Unsent message follows -----\n", fp, m);
6675984Seric 		else
66859987Seric 			putline("   ----- Message header follows -----\n", fp, m);
66959730Seric 		(void) fflush(fp);
67059730Seric 
67159730Seric 		if (e->e_msgboundary != NULL)
6725984Seric 		{
67359730Seric 			putline("", fp, m);
67459730Seric 			(void) sprintf(buf, "--%s", e->e_msgboundary);
67559730Seric 			putline(buf, fp, m);
67659987Seric 			putline("Content-Type: message/rfc822", fp, m);
67759730Seric 			putline("", fp, m);
6785984Seric 		}
67959730Seric 		putheader(fp, m, e->e_parent);
68059730Seric 		putline("", fp, m);
68159730Seric 		if (SendBody)
68259730Seric 			putbody(fp, m, e->e_parent, e->e_msgboundary);
68359987Seric 		else
68460010Seric 			putline("   ----- Message body suppressed -----", fp, m);
6854199Seric 	}
6864199Seric 	else
68710170Seric 	{
68810170Seric 		putline("  ----- No message was collected -----\n", fp, m);
68910170Seric 	}
6904318Seric 
69160010Seric 	if (e->e_msgboundary != NULL)
69260010Seric 	{
69360010Seric 		putline("", fp, m);
69460010Seric 		(void) sprintf(buf, "--%s--", e->e_msgboundary);
69560010Seric 		putline(buf, fp, m);
69660010Seric 	}
69759730Seric 	putline("", fp, m);
69859730Seric 
6994318Seric 	/*
7004318Seric 	**  Cleanup and exit
7014318Seric 	*/
7024318Seric 
703297Seric 	if (errno != 0)
7046978Seric 		syserr("errbody: I/O error");
705297Seric }
70658144Seric /*
70758144Seric **  PRUNEROUTE -- prune an RFC-822 source route
70858144Seric **
70958144Seric **	Trims down a source route to the last internet-registered hop.
71058144Seric **	This is encouraged by RFC 1123 section 5.3.3.
71158144Seric **
71258144Seric **	Parameters:
71358144Seric **		addr -- the address
71458144Seric **
71558144Seric **	Returns:
71658144Seric **		TRUE -- address was modified
71758144Seric **		FALSE -- address could not be pruned
71858144Seric **
71958144Seric **	Side Effects:
72058144Seric **		modifies addr in-place
72158144Seric */
72258144Seric 
72358144Seric pruneroute(addr)
72458144Seric 	char *addr;
72558144Seric {
72658144Seric #ifdef NAMED_BIND
72758144Seric 	char *start, *at, *comma;
72858144Seric 	char c;
72958144Seric 	int rcode;
73058144Seric 	char hostbuf[BUFSIZ];
73158144Seric 	char *mxhosts[MAXMXHOSTS + 1];
73258144Seric 
73358144Seric 	/* check to see if this is really a route-addr */
73458144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
73558144Seric 		return FALSE;
73658144Seric 	start = strchr(addr, ':');
73758144Seric 	at = strrchr(addr, '@');
73858144Seric 	if (start == NULL || at == NULL || at < start)
73958144Seric 		return FALSE;
74058144Seric 
74158144Seric 	/* slice off the angle brackets */
74258144Seric 	strcpy(hostbuf, at + 1);
74358144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
74458144Seric 
74558144Seric 	while (start)
74658144Seric 	{
74759273Seric 		if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0)
74858144Seric 		{
74958144Seric 			strcpy(addr + 1, start + 1);
75058144Seric 			return TRUE;
75158144Seric 		}
75258144Seric 		c = *start;
75358144Seric 		*start = '\0';
75458144Seric 		comma = strrchr(addr, ',');
75558144Seric 		if (comma && comma[1] == '@')
75658144Seric 			strcpy(hostbuf, comma + 2);
75758144Seric 		else
75858144Seric 			comma = 0;
75958144Seric 		*start = c;
76058144Seric 		start = comma;
76158144Seric 	}
76258144Seric #endif
76358144Seric 	return FALSE;
76458144Seric }
765