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*64025Seric static char sccsid[] = "@(#)savemail.c	8.8 (Berkeley) 07/23/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";
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.
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",
22763841Seric 					  (ADDRESS *) NULL,
22863841Seric 					  &e->e_errorqueue, e);
22963841Seric 			}
23058680Seric 			if (strcmp(e->e_from.q_paddr, "<>") != 0)
23163841Seric 			{
23258680Seric 				(void) sendtolist(e->e_from.q_paddr,
23358680Seric 					  (ADDRESS *) NULL,
23458680Seric 					  &e->e_errorqueue, e);
23563841Seric 			}
23658680Seric 
23763841Seric 			/*
23863841Seric 			**  Deliver a non-delivery report to the
23963841Seric 			**  Postmaster-designate (not necessarily
24063841Seric 			**  Postmaster).  This does not include the
24163841Seric 			**  body of the message, for privacy reasons.
24263841Seric 			**  You really shouldn't need this.
24363841Seric 			*/
24463841Seric 
24563849Seric 			e->e_flags |= EF_PM_NOTIFY;
24658178Seric 
24758680Seric 			q = e->e_errorqueue;
24858680Seric 			if (q == NULL)
24958680Seric 			{
25058680Seric 				/* this is an error-error */
25158680Seric 				state = ESM_POSTMASTER;
25258680Seric 				break;
25358680Seric 			}
25458966Seric 			if (returntosender(e->e_message,
25558680Seric 					   q, (e->e_class >= 0), e) == 0)
25658680Seric 			{
25758680Seric 				state = ESM_DONE;
25858680Seric 				break;
25958680Seric 			}
26024981Seric 
26158680Seric 			/* didn't work -- return to postmaster */
26258680Seric 			state = ESM_POSTMASTER;
26358680Seric 			break;
26458306Seric 
26558680Seric 		  case ESM_POSTMASTER:
26658680Seric 			/*
26758680Seric 			**  Similar to previous case, but to system postmaster.
26858680Seric 			*/
26958680Seric 
27059432Seric 			q = NULL;
27159432Seric 			if (sendtolist("postmaster", NULL, &q, e) <= 0)
27224942Seric 			{
27358680Seric 				syserr("553 cannot parse postmaster!");
27458680Seric 				ExitStat = EX_SOFTWARE;
27558680Seric 				state = ESM_USRTMP;
27658680Seric 				break;
27724942Seric 			}
27858966Seric 			if (returntosender(e->e_message,
27957438Seric 					   q, (e->e_class >= 0), e) == 0)
28024942Seric 			{
28124942Seric 				state = ESM_DONE;
28224942Seric 				break;
28324942Seric 			}
284297Seric 
28558680Seric 			/* didn't work -- last resort */
28658680Seric 			state = ESM_USRTMP;
28724942Seric 			break;
288297Seric 
28924942Seric 		  case ESM_DEADLETTER:
29024942Seric 			/*
29124942Seric 			**  Save the message in dead.letter.
29224942Seric 			**	If we weren't mailing back, and the user is
29324942Seric 			**	local, we should save the message in
29424942Seric 			**	~/dead.letter so that the poor person doesn't
29524942Seric 			**	have to type it over again -- and we all know
29624942Seric 			**	what poor typists UNIX users are.
29724942Seric 			*/
2985315Seric 
29924942Seric 			p = NULL;
30024942Seric 			if (e->e_from.q_mailer == LocalMailer)
30124942Seric 			{
30224942Seric 				if (e->e_from.q_home != NULL)
30324942Seric 					p = e->e_from.q_home;
30424942Seric 				else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
30524942Seric 					p = pw->pw_dir;
30624942Seric 			}
30724942Seric 			if (p == NULL)
30824942Seric 			{
30958865Seric 				/* no local directory */
31024942Seric 				state = ESM_MAIL;
31124942Seric 				break;
31224942Seric 			}
31324942Seric 			if (e->e_dfp != NULL)
31424942Seric 			{
31524942Seric 				bool oldverb = Verbose;
31624942Seric 
31724942Seric 				/* we have a home directory; open dead.letter */
31824942Seric 				define('z', p, e);
31958050Seric 				expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e);
32024942Seric 				Verbose = TRUE;
32158151Seric 				message("Saving message in %s", buf);
32224942Seric 				Verbose = oldverb;
32324942Seric 				e->e_to = buf;
32424942Seric 				q = NULL;
32558082Seric 				(void) sendtolist(buf, &e->e_from, &q, e);
32624942Seric 				if (deliver(e, q) == 0)
32724942Seric 					state = ESM_DONE;
32824942Seric 				else
32924942Seric 					state = ESM_MAIL;
33024942Seric 			}
33125569Seric 			else
33225569Seric 			{
33325569Seric 				/* no data file -- try mailing back */
33425569Seric 				state = ESM_MAIL;
33525569Seric 			}
33624942Seric 			break;
33724942Seric 
33824942Seric 		  case ESM_USRTMP:
33924942Seric 			/*
34024942Seric 			**  Log the mail in /usr/tmp/dead.letter.
34124942Seric 			*/
34224942Seric 
34357438Seric 			if (e->e_class < 0)
34457438Seric 			{
34557438Seric 				state = ESM_DONE;
34657438Seric 				break;
34757438Seric 			}
34857438Seric 
34959745Seric 			fp = dfopen("/usr/tmp/dead.letter",
35059745Seric 				    O_WRONLY|O_CREAT|O_APPEND, FileMode);
35124942Seric 			if (fp == NULL)
35224942Seric 			{
35324942Seric 				state = ESM_PANIC;
35424942Seric 				break;
35524942Seric 			}
35624942Seric 
35758010Seric 			putfromline(fp, FileMailer, e);
35858010Seric 			(*e->e_puthdr)(fp, FileMailer, e);
35958010Seric 			putline("\n", fp, FileMailer);
36059730Seric 			(*e->e_putbody)(fp, FileMailer, e, NULL);
36158010Seric 			putline("\n", fp, FileMailer);
36224942Seric 			(void) fflush(fp);
36324942Seric 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
36458680Seric 			(void) xfclose(fp, "savemail", "/usr/tmp/dead.letter");
36524942Seric 			break;
36624942Seric 
36724942Seric 		  default:
36858151Seric 			syserr("554 savemail: unknown state %d", state);
36924942Seric 
37024942Seric 			/* fall through ... */
37124942Seric 
37224942Seric 		  case ESM_PANIC:
37324942Seric 			/* leave the locked queue & transcript files around */
37458151Seric 			syserr("554 savemail: cannot save rejected email anywhere");
37524942Seric 			exit(EX_SOFTWARE);
37624942Seric 		}
377297Seric 	}
378297Seric }
379297Seric /*
3804633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
3814633Seric **
3824633Seric **	Parameters:
3834633Seric **		msg -- the explanatory message.
38416479Seric **		returnq -- the queue of people to send the message to.
3855984Seric **		sendbody -- if TRUE, also send back the body of the
3865984Seric **			message; otherwise just send the header.
38755012Seric **		e -- the current envelope.
3884633Seric **
3894633Seric **	Returns:
3904633Seric **		zero -- if everything went ok.
3914633Seric **		else -- some error.
3924633Seric **
3934633Seric **	Side Effects:
3944633Seric **		Returns the current message to the sender via
3954633Seric **		mail.
3964633Seric */
3974633Seric 
3985984Seric static bool	SendBody;
3994633Seric 
4007045Seric #define MAXRETURNS	6	/* max depth of returning messages */
40158559Seric #define ERRORFUDGE	100	/* nominal size of error message text */
4027045Seric 
40355012Seric returntosender(msg, returnq, sendbody, e)
4044633Seric 	char *msg;
40516479Seric 	ADDRESS *returnq;
4065984Seric 	bool sendbody;
40755012Seric 	register ENVELOPE *e;
4084633Seric {
4094633Seric 	char buf[MAXNAME];
4106978Seric 	extern putheader(), errbody();
4116978Seric 	register ENVELOPE *ee;
41258680Seric 	ENVELOPE *oldcur = CurEnv;
4136978Seric 	ENVELOPE errenvelope;
4147045Seric 	static int returndepth;
4159375Seric 	register ADDRESS *q;
4164633Seric 
41760010Seric 	if (returnq == NULL)
41860010Seric 		return (-1);
41960010Seric 
42058966Seric 	if (msg == NULL)
42158966Seric 		msg = "Unable to deliver mail";
42258966Seric 
4237676Seric 	if (tTd(6, 1))
4247287Seric 	{
42558680Seric 		printf("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
42655012Seric 		       msg, returndepth, e);
42716479Seric 		printaddr(returnq, TRUE);
4287287Seric 	}
4297287Seric 
4307045Seric 	if (++returndepth >= MAXRETURNS)
4317045Seric 	{
4327045Seric 		if (returndepth != MAXRETURNS)
43358151Seric 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
4347045Seric 		/* don't "unrecurse" and fake a clean exit */
4357045Seric 		/* returndepth--; */
4367045Seric 		return (0);
4377045Seric 	}
4387045Seric 
4395984Seric 	SendBody = sendbody;
44058680Seric 	define('g', e->e_from.q_paddr, e);
44158179Seric 	ee = newenvelope(&errenvelope, e);
44258050Seric 	define('a', "\201b", ee);
44359057Seric 	define('r', "internal", ee);
44459057Seric 	define('s', "localhost", ee);
44559057Seric 	define('_', "localhost", ee);
4466978Seric 	ee->e_puthdr = putheader;
4476978Seric 	ee->e_putbody = errbody;
4489375Seric 	ee->e_flags |= EF_RESPONSE;
44955012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
45045155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
45116479Seric 	ee->e_sendqueue = returnq;
45258559Seric 	ee->e_msgsize = e->e_msgsize + ERRORFUDGE;
4539542Seric 	openxscript(ee);
45416479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4559375Seric 	{
45659989Seric 		if (bitset(QBADADDR, q->q_flags))
45758559Seric 			continue;
45858559Seric 
45959989Seric 		if (!bitset(QDONTSEND, q->q_flags))
46059989Seric 			ee->e_nrcpts++;
46158559Seric 
46258144Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
46358333Seric 			parseaddr(q->q_paddr, q, 0, '\0', NULL, e);
46458144Seric 
4659375Seric 		if (q->q_alias == NULL)
46659580Seric 			addheader("To", q->q_paddr, ee);
4679375Seric 	}
46824942Seric 
46957642Seric # ifdef LOG
47058020Seric 	if (LogLevel > 5)
47157642Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
47257642Seric 			e->e_id, ee->e_id, msg);
47357642Seric # endif
47457642Seric 
47510845Seric 	(void) sprintf(buf, "Returned mail: %s", msg);
47659580Seric 	addheader("Subject", buf, ee);
47759730Seric 	if (SendMIMEErrors)
47859730Seric 	{
47959987Seric 		addheader("MIME-Version", "1.0", ee);
48059730Seric 		(void) sprintf(buf, "%s.%ld/%s",
48159730Seric 			ee->e_id, curtime(), MyHostName);
48259730Seric 		ee->e_msgboundary = newstr(buf);
48359730Seric 		(void) sprintf(buf, "multipart/mixed; boundary=\"%s\"",
48459730Seric 					ee->e_msgboundary);
48559730Seric 		addheader("Content-Type", buf, ee);
48659730Seric 	}
4874633Seric 
4884633Seric 	/* fake up an address header for the from person */
48958050Seric 	expand("\201n", buf, &buf[sizeof buf - 1], e);
49058680Seric 	if (parseaddr(buf, &ee->e_from, 1, '\0', NULL, e) == NULL)
4914633Seric 	{
49258151Seric 		syserr("553 Can't parse myself!");
4934633Seric 		ExitStat = EX_SOFTWARE;
4947045Seric 		returndepth--;
4954633Seric 		return (-1);
4964633Seric 	}
49758704Seric 	ee->e_sender = ee->e_from.q_paddr;
4985984Seric 
4996978Seric 	/* push state into submessage */
5006978Seric 	CurEnv = ee;
50158050Seric 	define('f', "\201n", ee);
5029375Seric 	define('x', "Mail Delivery Subsystem", ee);
50358929Seric 	eatheader(ee, TRUE);
5045984Seric 
50563753Seric 	/* mark statistics */
50663753Seric 	markstats(ee, (ADDRESS *) NULL);
50763753Seric 
5086978Seric 	/* actually deliver the error message */
50914876Seric 	sendall(ee, SM_DEFAULT);
5106978Seric 
5116978Seric 	/* restore state */
5127811Seric 	dropenvelope(ee);
51358680Seric 	CurEnv = oldcur;
5147045Seric 	returndepth--;
5156978Seric 
5167045Seric 	/* should check for delivery errors here */
5174633Seric 	return (0);
5184633Seric }
5194633Seric /*
5206978Seric **  ERRBODY -- output the body of an error message.
5216978Seric **
5226978Seric **	Typically this is a copy of the transcript plus a copy of the
5236978Seric **	original offending message.
5246978Seric **
525297Seric **	Parameters:
526297Seric **		fp -- the output file.
52710170Seric **		m -- the mailer to output to.
5289542Seric **		e -- the envelope we are working in.
529297Seric **
530297Seric **	Returns:
531297Seric **		none
532297Seric **
533297Seric **	Side Effects:
5346978Seric **		Outputs the body of an error message.
535297Seric */
536297Seric 
53710170Seric errbody(fp, m, e)
538297Seric 	register FILE *fp;
5394318Seric 	register struct mailer *m;
5409542Seric 	register ENVELOPE *e;
541297Seric {
5426978Seric 	register FILE *xfile;
54359082Seric 	char *p;
54459082Seric 	register ADDRESS *q;
54559082Seric 	bool printheader;
5463189Seric 	char buf[MAXLINE];
547297Seric 
54858680Seric 	if (e->e_parent == NULL)
54958680Seric 	{
55058680Seric 		syserr("errbody: null parent");
55158680Seric 		putline("   ----- Original message lost -----\n", fp, m);
55258680Seric 		return;
55358680Seric 	}
55458680Seric 
5559057Seric 	/*
55659730Seric 	**  Output MIME header.
55759730Seric 	*/
55859730Seric 
55959730Seric 	if (e->e_msgboundary != NULL)
56059730Seric 	{
56159730Seric 		putline("This is a MIME-encapsulated message", fp, m);
56259730Seric 		putline("", fp, m);
56359730Seric 		(void) sprintf(buf, "--%s", e->e_msgboundary);
56459730Seric 		putline(buf, fp, m);
56559730Seric 		putline("", fp, m);
56659730Seric 	}
56759730Seric 
56859730Seric 	/*
56963852Seric 	**  Output introductory information.
57063852Seric 	*/
57163852Seric 
57263852Seric 	sprintf(buf, "The original message was received at %s", arpadate(NULL));
57363852Seric 	putline(buf, fp, m);
574*64025Seric 	expand("from \201_", buf, &buf[sizeof buf - 1], e->e_parent);
57563852Seric 	putline(buf, fp, m);
57663852Seric 	putline("", fp, m);
57763852Seric 
57863852Seric 	/*
57955372Seric 	**  Output error message header (if specified and available).
58055372Seric 	*/
58155372Seric 
58255372Seric 	if (ErrMsgFile != NULL)
58355372Seric 	{
58455372Seric 		if (*ErrMsgFile == '/')
58555372Seric 		{
58655372Seric 			xfile = fopen(ErrMsgFile, "r");
58755372Seric 			if (xfile != NULL)
58855372Seric 			{
58955372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
59055425Seric 				{
59155425Seric 					expand(buf, buf, &buf[sizeof buf - 1], e);
59255372Seric 					putline(buf, fp, m);
59355425Seric 				}
59455372Seric 				(void) fclose(xfile);
59559082Seric 				putline("\n", fp, m);
59655372Seric 			}
59755372Seric 		}
59855372Seric 		else
59955372Seric 		{
60055425Seric 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
60155425Seric 			putline(buf, fp, m);
60259730Seric 			putline("", fp, m);
60355372Seric 		}
60455372Seric 	}
60555372Seric 
60655372Seric 	/*
60759082Seric 	**  Output message introduction
60859082Seric 	*/
60959082Seric 
61059082Seric 	printheader = TRUE;
61159082Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
61259082Seric 	{
61363787Seric 		if (bitset(QBADADDR|QREPORT, q->q_flags))
61459082Seric 		{
61559082Seric 			if (printheader)
61659082Seric 			{
61763787Seric 				putline("   ----- The following addresses had delivery problems -----",
61859082Seric 					fp, m);
61959082Seric 				printheader = FALSE;
62059082Seric 			}
62163849Seric 			strcpy(buf, q->q_paddr);
62263787Seric 			if (bitset(QBADADDR, q->q_flags))
62363849Seric 				strcat(buf, "  (unrecoverable error)");
62463787Seric 			else
62563849Seric 				strcat(buf, "  (transient failure)");
62663787Seric 			putline(buf, fp, m);
62763849Seric 			if (q->q_alias != NULL)
62863849Seric 			{
62963849Seric 				strcpy(buf, "    (expanded from: ");
63063849Seric 				strcat(buf, q->q_alias->q_paddr);
63163849Seric 				strcat(buf, ")");
63263849Seric 				putline(buf, fp, m);
63363849Seric 			}
63459082Seric 		}
63559082Seric 	}
63659082Seric 	if (!printheader)
63759082Seric 		putline("\n", fp, m);
63859082Seric 
63959082Seric 	/*
6409057Seric 	**  Output transcript of errors
6419057Seric 	*/
6429057Seric 
6434086Seric 	(void) fflush(stdout);
6449542Seric 	p = queuename(e->e_parent, 'x');
6459337Seric 	if ((xfile = fopen(p, "r")) == NULL)
6469057Seric 	{
6479337Seric 		syserr("Cannot open %s", p);
64859082Seric 		putline("   ----- Transcript of session is unavailable -----\n", fp, m);
6499057Seric 	}
6509057Seric 	else
6519057Seric 	{
65258680Seric 		putline("   ----- Transcript of session follows -----\n", fp, m);
6539542Seric 		if (e->e_xfp != NULL)
6549542Seric 			(void) fflush(e->e_xfp);
6559057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
65610170Seric 			putline(buf, fp, m);
65758680Seric 		(void) xfclose(xfile, "errbody xscript", p);
6589057Seric 	}
659297Seric 	errno = 0;
6604318Seric 
6614318Seric 	/*
6624318Seric 	**  Output text of original message
6634318Seric 	*/
6644318Seric 
6654289Seric 	if (NoReturn)
66658665Seric 		SendBody = FALSE;
66759730Seric 	putline("", fp, m);
66858680Seric 	if (e->e_parent->e_df != NULL)
6694199Seric 	{
6705984Seric 		if (SendBody)
67163852Seric 			putline("   ----- Original message follows -----\n", fp, m);
6725984Seric 		else
67359987Seric 			putline("   ----- Message header follows -----\n", fp, m);
67459730Seric 		(void) fflush(fp);
67559730Seric 
67659730Seric 		if (e->e_msgboundary != NULL)
6775984Seric 		{
67859730Seric 			putline("", fp, m);
67959730Seric 			(void) sprintf(buf, "--%s", e->e_msgboundary);
68059730Seric 			putline(buf, fp, m);
68159987Seric 			putline("Content-Type: message/rfc822", fp, m);
68259730Seric 			putline("", fp, m);
6835984Seric 		}
68459730Seric 		putheader(fp, m, e->e_parent);
68559730Seric 		putline("", fp, m);
68659730Seric 		if (SendBody)
68759730Seric 			putbody(fp, m, e->e_parent, e->e_msgboundary);
68859987Seric 		else
68960010Seric 			putline("   ----- Message body suppressed -----", fp, m);
6904199Seric 	}
6914199Seric 	else
69210170Seric 	{
69310170Seric 		putline("  ----- No message was collected -----\n", fp, m);
69410170Seric 	}
6954318Seric 
69660010Seric 	if (e->e_msgboundary != NULL)
69760010Seric 	{
69860010Seric 		putline("", fp, m);
69960010Seric 		(void) sprintf(buf, "--%s--", e->e_msgboundary);
70060010Seric 		putline(buf, fp, m);
70160010Seric 	}
70259730Seric 	putline("", fp, m);
70359730Seric 
7044318Seric 	/*
7054318Seric 	**  Cleanup and exit
7064318Seric 	*/
7074318Seric 
708297Seric 	if (errno != 0)
7096978Seric 		syserr("errbody: I/O error");
710297Seric }
71158144Seric /*
71258144Seric **  PRUNEROUTE -- prune an RFC-822 source route
71358144Seric **
71458144Seric **	Trims down a source route to the last internet-registered hop.
71558144Seric **	This is encouraged by RFC 1123 section 5.3.3.
71658144Seric **
71758144Seric **	Parameters:
71858144Seric **		addr -- the address
71958144Seric **
72058144Seric **	Returns:
72158144Seric **		TRUE -- address was modified
72258144Seric **		FALSE -- address could not be pruned
72358144Seric **
72458144Seric **	Side Effects:
72558144Seric **		modifies addr in-place
72658144Seric */
72758144Seric 
72858144Seric pruneroute(addr)
72958144Seric 	char *addr;
73058144Seric {
73158144Seric #ifdef NAMED_BIND
73258144Seric 	char *start, *at, *comma;
73358144Seric 	char c;
73458144Seric 	int rcode;
73558144Seric 	char hostbuf[BUFSIZ];
73658144Seric 	char *mxhosts[MAXMXHOSTS + 1];
73758144Seric 
73858144Seric 	/* check to see if this is really a route-addr */
73958144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
74058144Seric 		return FALSE;
74158144Seric 	start = strchr(addr, ':');
74258144Seric 	at = strrchr(addr, '@');
74358144Seric 	if (start == NULL || at == NULL || at < start)
74458144Seric 		return FALSE;
74558144Seric 
74658144Seric 	/* slice off the angle brackets */
74758144Seric 	strcpy(hostbuf, at + 1);
74858144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
74958144Seric 
75058144Seric 	while (start)
75158144Seric 	{
75259273Seric 		if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0)
75358144Seric 		{
75458144Seric 			strcpy(addr + 1, start + 1);
75558144Seric 			return TRUE;
75658144Seric 		}
75758144Seric 		c = *start;
75858144Seric 		*start = '\0';
75958144Seric 		comma = strrchr(addr, ',');
76058144Seric 		if (comma && comma[1] == '@')
76158144Seric 			strcpy(hostbuf, comma + 2);
76258144Seric 		else
76358144Seric 			comma = 0;
76458144Seric 		*start = c;
76558144Seric 		start = comma;
76658144Seric 	}
76758144Seric #endif
76858144Seric 	return FALSE;
76958144Seric }
770