122711Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333731Sbostic  * Copyright (c) 1988 Regents of the University of California.
433731Sbostic  * All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822711Sdist 
922711Sdist #ifndef lint
10*59094Seric static char sccsid[] = "@(#)savemail.c	6.32 (Berkeley) 04/15/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;
5324942Seric 	auto ADDRESS *q;
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 	{
6258734Seric 		printf("\nsavemail, errormode = %c\n  e_from=", e->e_errormode);
6358680Seric 		printaddr(&e->e_from, FALSE);
6458680Seric 	}
657361Seric 
669337Seric 	e->e_flags &= ~EF_FATALERRS;
67297Seric 
68297Seric 	/*
69297Seric 	**  In the unhappy event we don't know who to return the mail
70297Seric 	**  to, make someone up.
71297Seric 	*/
72297Seric 
739337Seric 	if (e->e_from.q_paddr == NULL)
74297Seric 	{
7558733Seric 		e->e_sender = "Postmaster";
7658704Seric 		if (parseaddr(e->e_sender, &e->e_from, 0, '\0', NULL, e) == NULL)
77297Seric 		{
7858733Seric 			syserr("553 Cannot parse Postmaster!");
79297Seric 			ExitStat = EX_SOFTWARE;
80297Seric 			finis();
81297Seric 		}
82297Seric 	}
839337Seric 	e->e_to = NULL;
84297Seric 
85297Seric 	/*
8624942Seric 	**  Basic state machine.
8724942Seric 	**
8824942Seric 	**	This machine runs through the following states:
8924942Seric 	**
9024942Seric 	**	ESM_QUIET	Errors have already been printed iff the
9124942Seric 	**			sender is local.
9224942Seric 	**	ESM_REPORT	Report directly to the sender's terminal.
9324942Seric 	**	ESM_MAIL	Mail response to the sender.
9424942Seric 	**	ESM_DEADLETTER	Save response in ~/dead.letter.
9524942Seric 	**	ESM_POSTMASTER	Mail response to the postmaster.
9624942Seric 	**	ESM_PANIC	Save response anywhere possible.
97297Seric 	*/
98297Seric 
9924942Seric 	/* determine starting state */
10058734Seric 	switch (e->e_errormode)
101297Seric 	{
10224942Seric 	  case EM_WRITE:
10324942Seric 		state = ESM_REPORT;
10424942Seric 		break;
10524942Seric 
10624942Seric 	  case EM_BERKNET:
10724942Seric 		/* mail back, but return o.k. exit status */
108401Seric 		ExitStat = EX_OK;
10924942Seric 
11024942Seric 		/* fall through.... */
11124942Seric 
11224942Seric 	  case EM_MAIL:
11324942Seric 		state = ESM_MAIL;
11424942Seric 		break;
11524942Seric 
11624942Seric 	  case EM_PRINT:
11724979Seric 	  case '\0':
11824942Seric 		state = ESM_QUIET;
11924942Seric 		break;
12024942Seric 
12124942Seric 	  case EM_QUIET:
12224942Seric 		/* no need to return anything at all */
12324942Seric 		return;
12424979Seric 
12524979Seric 	  default:
12658734Seric 		syserr("554 savemail: bogus errormode x%x\n", e->e_errormode);
12724979Seric 		state = ESM_MAIL;
12824979Seric 		break;
129297Seric 	}
130297Seric 
131*59094Seric 	/* if this is already an error response, send to postmaster */
132*59094Seric 	if (bitset(EF_RESPONSE, e->e_flags))
133*59094Seric 	{
134*59094Seric 		if (e->e_parent != NULL &&
135*59094Seric 		    bitset(EF_RESPONSE, e->e_parent->e_flags))
136*59094Seric 		{
137*59094Seric 			/* got an error sending a response -- can it */
138*59094Seric 			return;
139*59094Seric 		}
140*59094Seric 		state = ESM_POSTMASTER;
141*59094Seric 	}
142*59094Seric 
14324942Seric 	while (state != ESM_DONE)
144297Seric 	{
14524979Seric 		if (tTd(6, 5))
14624979Seric 			printf("  state %d\n", state);
14724979Seric 
14824942Seric 		switch (state)
149297Seric 		{
15024979Seric 		  case ESM_QUIET:
15124979Seric 			if (e->e_from.q_mailer == LocalMailer)
15224979Seric 				state = ESM_DEADLETTER;
15324979Seric 			else
15424979Seric 				state = ESM_MAIL;
15524979Seric 			break;
15624979Seric 
15724942Seric 		  case ESM_REPORT:
15824942Seric 
15924942Seric 			/*
16024942Seric 			**  If the user is still logged in on the same terminal,
16124942Seric 			**  then write the error messages back to hir (sic).
16224942Seric 			*/
16324942Seric 
16424942Seric 			p = ttypath();
16524942Seric 			if (p == NULL || freopen(p, "w", stdout) == NULL)
16624942Seric 			{
16724942Seric 				state = ESM_MAIL;
16824942Seric 				break;
16924942Seric 			}
17024942Seric 
17158050Seric 			expand("\201n", buf, &buf[sizeof buf - 1], e);
1729375Seric 			printf("\r\nMessage from %s...\r\n", buf);
1739375Seric 			printf("Errors occurred while sending mail.\r\n");
1749542Seric 			if (e->e_xfp != NULL)
1759375Seric 			{
1769542Seric 				(void) fflush(e->e_xfp);
17724942Seric 				fp = fopen(queuename(e, 'x'), "r");
1789375Seric 			}
1799375Seric 			else
18024942Seric 				fp = NULL;
18124942Seric 			if (fp == NULL)
1829375Seric 			{
1839337Seric 				syserr("Cannot open %s", queuename(e, 'x'));
1849375Seric 				printf("Transcript of session is unavailable.\r\n");
1859375Seric 			}
1869375Seric 			else
1879375Seric 			{
1889375Seric 				printf("Transcript follows:\r\n");
18924942Seric 				while (fgets(buf, sizeof buf, fp) != NULL &&
1909375Seric 				       !ferror(stdout))
1919375Seric 					fputs(buf, stdout);
19258680Seric 				(void) xfclose(fp, "savemail transcript", e->e_id);
1939375Seric 			}
19424942Seric 			printf("Original message will be saved in dead.letter.\r\n");
19524942Seric 			state = ESM_DEADLETTER;
19624942Seric 			break;
197297Seric 
19824942Seric 		  case ESM_MAIL:
19924942Seric 			/*
20024942Seric 			**  If mailing back, do it.
20124942Seric 			**	Throw away all further output.  Don't alias,
20224942Seric 			**	since this could cause loops, e.g., if joe
20324942Seric 			**	mails to joe@x, and for some reason the network
20424942Seric 			**	for @x is down, then the response gets sent to
20524942Seric 			**	joe@x, which gives a response, etc.  Also force
20624942Seric 			**	the mail to be delivered even if a version of
20724942Seric 			**	it has already been sent to the sender.
20824942Seric 			*/
209297Seric 
21058680Seric 			if (strcmp(e->e_from.q_paddr, "<>") != 0)
21158680Seric 				(void) sendtolist(e->e_from.q_paddr,
21258680Seric 					  (ADDRESS *) NULL,
21358680Seric 					  &e->e_errorqueue, e);
21458680Seric 
21558680Seric 			/* deliver a cc: to the postmaster if desired */
21658680Seric 			if (PostMasterCopy != NULL)
21724942Seric 			{
21858680Seric 				auto ADDRESS *rlist = NULL;
21958178Seric 
22058680Seric 				(void) sendtolist(PostMasterCopy,
22158304Seric 						  (ADDRESS *) NULL,
22258680Seric 						  &rlist, e);
22358680Seric 				(void) returntosender(e->e_message,
22458680Seric 						      rlist, FALSE, e);
22558680Seric 			}
22658680Seric 			q = e->e_errorqueue;
22758680Seric 			if (q == NULL)
22858680Seric 			{
22958680Seric 				/* this is an error-error */
23058680Seric 				state = ESM_POSTMASTER;
23158680Seric 				break;
23258680Seric 			}
23358966Seric 			if (returntosender(e->e_message,
23458680Seric 					   q, (e->e_class >= 0), e) == 0)
23558680Seric 			{
23658680Seric 				state = ESM_DONE;
23758680Seric 				break;
23858680Seric 			}
23924981Seric 
24058680Seric 			/* didn't work -- return to postmaster */
24158680Seric 			state = ESM_POSTMASTER;
24258680Seric 			break;
24358306Seric 
24458680Seric 		  case ESM_POSTMASTER:
24558680Seric 			/*
24658680Seric 			**  Similar to previous case, but to system postmaster.
24758680Seric 			*/
24858680Seric 
24959057Seric 			q = parseaddr("postmaster", q, 0, '\0', NULL, e);
25059057Seric 			if (q == NULL)
25124942Seric 			{
25258680Seric 				syserr("553 cannot parse postmaster!");
25358680Seric 				ExitStat = EX_SOFTWARE;
25458680Seric 				state = ESM_USRTMP;
25558680Seric 				break;
25624942Seric 			}
25758966Seric 			if (returntosender(e->e_message,
25857438Seric 					   q, (e->e_class >= 0), e) == 0)
25924942Seric 			{
26024942Seric 				state = ESM_DONE;
26124942Seric 				break;
26224942Seric 			}
263297Seric 
26458680Seric 			/* didn't work -- last resort */
26558680Seric 			state = ESM_USRTMP;
26624942Seric 			break;
267297Seric 
26824942Seric 		  case ESM_DEADLETTER:
26924942Seric 			/*
27024942Seric 			**  Save the message in dead.letter.
27124942Seric 			**	If we weren't mailing back, and the user is
27224942Seric 			**	local, we should save the message in
27324942Seric 			**	~/dead.letter so that the poor person doesn't
27424942Seric 			**	have to type it over again -- and we all know
27524942Seric 			**	what poor typists UNIX users are.
27624942Seric 			*/
2775315Seric 
27824942Seric 			p = NULL;
27924942Seric 			if (e->e_from.q_mailer == LocalMailer)
28024942Seric 			{
28124942Seric 				if (e->e_from.q_home != NULL)
28224942Seric 					p = e->e_from.q_home;
28324942Seric 				else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
28424942Seric 					p = pw->pw_dir;
28524942Seric 			}
28624942Seric 			if (p == NULL)
28724942Seric 			{
28858865Seric 				/* no local directory */
28924942Seric 				state = ESM_MAIL;
29024942Seric 				break;
29124942Seric 			}
29224942Seric 			if (e->e_dfp != NULL)
29324942Seric 			{
29424942Seric 				auto ADDRESS *q;
29524942Seric 				bool oldverb = Verbose;
29624942Seric 
29724942Seric 				/* we have a home directory; open dead.letter */
29824942Seric 				define('z', p, e);
29958050Seric 				expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e);
30024942Seric 				Verbose = TRUE;
30158151Seric 				message("Saving message in %s", buf);
30224942Seric 				Verbose = oldverb;
30324942Seric 				e->e_to = buf;
30424942Seric 				q = NULL;
30558082Seric 				(void) sendtolist(buf, &e->e_from, &q, e);
30624942Seric 				if (deliver(e, q) == 0)
30724942Seric 					state = ESM_DONE;
30824942Seric 				else
30924942Seric 					state = ESM_MAIL;
31024942Seric 			}
31125569Seric 			else
31225569Seric 			{
31325569Seric 				/* no data file -- try mailing back */
31425569Seric 				state = ESM_MAIL;
31525569Seric 			}
31624942Seric 			break;
31724942Seric 
31824942Seric 		  case ESM_USRTMP:
31924942Seric 			/*
32024942Seric 			**  Log the mail in /usr/tmp/dead.letter.
32124942Seric 			*/
32224942Seric 
32357438Seric 			if (e->e_class < 0)
32457438Seric 			{
32557438Seric 				state = ESM_DONE;
32657438Seric 				break;
32757438Seric 			}
32857438Seric 
32924942Seric 			fp = dfopen("/usr/tmp/dead.letter", "a");
33024942Seric 			if (fp == NULL)
33124942Seric 			{
33224942Seric 				state = ESM_PANIC;
33324942Seric 				break;
33424942Seric 			}
33524942Seric 
33658010Seric 			putfromline(fp, FileMailer, e);
33758010Seric 			(*e->e_puthdr)(fp, FileMailer, e);
33858010Seric 			putline("\n", fp, FileMailer);
33958010Seric 			(*e->e_putbody)(fp, FileMailer, e);
34058010Seric 			putline("\n", fp, FileMailer);
34124942Seric 			(void) fflush(fp);
34224942Seric 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
34358680Seric 			(void) xfclose(fp, "savemail", "/usr/tmp/dead.letter");
34424942Seric 			break;
34524942Seric 
34624942Seric 		  default:
34758151Seric 			syserr("554 savemail: unknown state %d", state);
34824942Seric 
34924942Seric 			/* fall through ... */
35024942Seric 
35124942Seric 		  case ESM_PANIC:
35224942Seric 			/* leave the locked queue & transcript files around */
35358151Seric 			syserr("554 savemail: cannot save rejected email anywhere");
35424942Seric 			exit(EX_SOFTWARE);
35524942Seric 		}
356297Seric 	}
357297Seric }
358297Seric /*
3594633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
3604633Seric **
3614633Seric **	Parameters:
3624633Seric **		msg -- the explanatory message.
36316479Seric **		returnq -- the queue of people to send the message to.
3645984Seric **		sendbody -- if TRUE, also send back the body of the
3655984Seric **			message; otherwise just send the header.
36655012Seric **		e -- the current envelope.
3674633Seric **
3684633Seric **	Returns:
3694633Seric **		zero -- if everything went ok.
3704633Seric **		else -- some error.
3714633Seric **
3724633Seric **	Side Effects:
3734633Seric **		Returns the current message to the sender via
3744633Seric **		mail.
3754633Seric */
3764633Seric 
3775984Seric static bool	SendBody;
3784633Seric 
3797045Seric #define MAXRETURNS	6	/* max depth of returning messages */
38058559Seric #define ERRORFUDGE	100	/* nominal size of error message text */
3817045Seric 
38255012Seric returntosender(msg, returnq, sendbody, e)
3834633Seric 	char *msg;
38416479Seric 	ADDRESS *returnq;
3855984Seric 	bool sendbody;
38655012Seric 	register ENVELOPE *e;
3874633Seric {
3884633Seric 	char buf[MAXNAME];
3896978Seric 	extern putheader(), errbody();
3906978Seric 	register ENVELOPE *ee;
39158680Seric 	ENVELOPE *oldcur = CurEnv;
3926978Seric 	extern ENVELOPE *newenvelope();
3936978Seric 	ENVELOPE errenvelope;
3947045Seric 	static int returndepth;
3959375Seric 	register ADDRESS *q;
3964633Seric 
39758966Seric 	if (msg == NULL)
39858966Seric 		msg = "Unable to deliver mail";
39958966Seric 
4007676Seric 	if (tTd(6, 1))
4017287Seric 	{
40258680Seric 		printf("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
40355012Seric 		       msg, returndepth, e);
40416479Seric 		printaddr(returnq, TRUE);
4057287Seric 	}
4067287Seric 
4077045Seric 	if (++returndepth >= MAXRETURNS)
4087045Seric 	{
4097045Seric 		if (returndepth != MAXRETURNS)
41058151Seric 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
4117045Seric 		/* don't "unrecurse" and fake a clean exit */
4127045Seric 		/* returndepth--; */
4137045Seric 		return (0);
4147045Seric 	}
4157045Seric 
4165984Seric 	SendBody = sendbody;
41758680Seric 	define('g', e->e_from.q_paddr, e);
41858179Seric 	ee = newenvelope(&errenvelope, e);
41958050Seric 	define('a', "\201b", ee);
42059057Seric 	define('r', "internal", ee);
42159057Seric 	define('s', "localhost", ee);
42259057Seric 	define('_', "localhost", ee);
4236978Seric 	ee->e_puthdr = putheader;
4246978Seric 	ee->e_putbody = errbody;
4259375Seric 	ee->e_flags |= EF_RESPONSE;
42655012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
42745155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
42816479Seric 	ee->e_sendqueue = returnq;
42958559Seric 	ee->e_msgsize = e->e_msgsize + ERRORFUDGE;
4309542Seric 	openxscript(ee);
43116479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4329375Seric 	{
43358559Seric 		if (bitset(QDONTSEND, q->q_flags))
43458559Seric 			continue;
43558559Seric 
43658559Seric 		ee->e_nrcpts++;
43758559Seric 
43858144Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
43958333Seric 			parseaddr(q->q_paddr, q, 0, '\0', NULL, e);
44058144Seric 
4419375Seric 		if (q->q_alias == NULL)
4429375Seric 			addheader("to", q->q_paddr, ee);
4439375Seric 	}
44424942Seric 
44557642Seric # ifdef LOG
44658020Seric 	if (LogLevel > 5)
44757642Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
44857642Seric 			e->e_id, ee->e_id, msg);
44957642Seric # endif
45057642Seric 
45110845Seric 	(void) sprintf(buf, "Returned mail: %s", msg);
45210106Seric 	addheader("subject", buf, ee);
4534633Seric 
4544633Seric 	/* fake up an address header for the from person */
45558050Seric 	expand("\201n", buf, &buf[sizeof buf - 1], e);
45658680Seric 	if (parseaddr(buf, &ee->e_from, 1, '\0', NULL, e) == NULL)
4574633Seric 	{
45858151Seric 		syserr("553 Can't parse myself!");
4594633Seric 		ExitStat = EX_SOFTWARE;
4607045Seric 		returndepth--;
4614633Seric 		return (-1);
4624633Seric 	}
46358704Seric 	ee->e_sender = ee->e_from.q_paddr;
4645984Seric 
4656978Seric 	/* push state into submessage */
4666978Seric 	CurEnv = ee;
46758050Seric 	define('f', "\201n", ee);
4689375Seric 	define('x', "Mail Delivery Subsystem", ee);
46958929Seric 	eatheader(ee, TRUE);
4705984Seric 
4716978Seric 	/* actually deliver the error message */
47214876Seric 	sendall(ee, SM_DEFAULT);
4736978Seric 
4746978Seric 	/* restore state */
4757811Seric 	dropenvelope(ee);
47658680Seric 	CurEnv = oldcur;
4777045Seric 	returndepth--;
4786978Seric 
4797045Seric 	/* should check for delivery errors here */
4804633Seric 	return (0);
4814633Seric }
4824633Seric /*
4836978Seric **  ERRBODY -- output the body of an error message.
4846978Seric **
4856978Seric **	Typically this is a copy of the transcript plus a copy of the
4866978Seric **	original offending message.
4876978Seric **
488297Seric **	Parameters:
489297Seric **		fp -- the output file.
49010170Seric **		m -- the mailer to output to.
4919542Seric **		e -- the envelope we are working in.
492297Seric **
493297Seric **	Returns:
494297Seric **		none
495297Seric **
496297Seric **	Side Effects:
4976978Seric **		Outputs the body of an error message.
498297Seric */
499297Seric 
50010170Seric errbody(fp, m, e)
501297Seric 	register FILE *fp;
5024318Seric 	register struct mailer *m;
5039542Seric 	register ENVELOPE *e;
504297Seric {
5056978Seric 	register FILE *xfile;
50659082Seric 	char *p;
50759082Seric 	register ADDRESS *q;
50859082Seric 	bool printheader;
5093189Seric 	char buf[MAXLINE];
510297Seric 
51158680Seric 	if (e->e_parent == NULL)
51258680Seric 	{
51358680Seric 		syserr("errbody: null parent");
51458680Seric 		putline("\n", fp, m);
51558680Seric 		putline("   ----- Original message lost -----\n", fp, m);
51658680Seric 		return;
51758680Seric 	}
51858680Seric 
5199057Seric 	/*
52055372Seric 	**  Output error message header (if specified and available).
52155372Seric 	*/
52255372Seric 
52355372Seric 	if (ErrMsgFile != NULL)
52455372Seric 	{
52555372Seric 		if (*ErrMsgFile == '/')
52655372Seric 		{
52755372Seric 			xfile = fopen(ErrMsgFile, "r");
52855372Seric 			if (xfile != NULL)
52955372Seric 			{
53055372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
53155425Seric 				{
53255425Seric 					expand(buf, buf, &buf[sizeof buf - 1], e);
53355372Seric 					putline(buf, fp, m);
53455425Seric 				}
53555372Seric 				(void) fclose(xfile);
53659082Seric 				putline("\n", fp, m);
53755372Seric 			}
53855372Seric 		}
53955372Seric 		else
54055372Seric 		{
54155425Seric 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
54255425Seric 			putline(buf, fp, m);
54359082Seric 			putline("\n", fp, m);
54455372Seric 		}
54555372Seric 	}
54655372Seric 
54755372Seric 	/*
54859082Seric 	**  Output message introduction
54959082Seric 	*/
55059082Seric 
55159082Seric 	printheader = TRUE;
55259082Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
55359082Seric 	{
55459082Seric 		if (bitset(QBADADDR, q->q_flags))
55559082Seric 		{
55659082Seric 			if (printheader)
55759082Seric 			{
55859082Seric 				putline("The following addresses failed:\n",
55959082Seric 					fp, m);
56059082Seric 				printheader = FALSE;
56159082Seric 			}
56259082Seric 			sprintf(buf, "\t%s\n", q->q_paddr);
56359082Seric 			putline(buf, fp, m);
56459082Seric 		}
56559082Seric 	}
56659082Seric 	if (!printheader)
56759082Seric 		putline("\n", fp, m);
56859082Seric 
56959082Seric 	/*
5709057Seric 	**  Output transcript of errors
5719057Seric 	*/
5729057Seric 
5734086Seric 	(void) fflush(stdout);
5749542Seric 	p = queuename(e->e_parent, 'x');
5759337Seric 	if ((xfile = fopen(p, "r")) == NULL)
5769057Seric 	{
5779337Seric 		syserr("Cannot open %s", p);
57859082Seric 		putline("   ----- Transcript of session is unavailable -----\n", fp, m);
5799057Seric 	}
5809057Seric 	else
5819057Seric 	{
58258680Seric 		putline("   ----- Transcript of session follows -----\n", fp, m);
5839542Seric 		if (e->e_xfp != NULL)
5849542Seric 			(void) fflush(e->e_xfp);
5859057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
58610170Seric 			putline(buf, fp, m);
58758680Seric 		(void) xfclose(xfile, "errbody xscript", p);
5889057Seric 	}
589297Seric 	errno = 0;
5904318Seric 
5914318Seric 	/*
5924318Seric 	**  Output text of original message
5934318Seric 	*/
5944318Seric 
5954289Seric 	if (NoReturn)
59658665Seric 		SendBody = FALSE;
59758680Seric 	if (e->e_parent->e_df != NULL)
5984199Seric 	{
5995984Seric 		if (SendBody)
6005984Seric 		{
60110170Seric 			putline("\n", fp, m);
60210170Seric 			putline("   ----- Unsent message follows -----\n", fp, m);
6035984Seric 			(void) fflush(fp);
60410170Seric 			putheader(fp, m, e->e_parent);
60510170Seric 			putline("\n", fp, m);
60610170Seric 			putbody(fp, m, e->e_parent);
6075984Seric 		}
6085984Seric 		else
6095984Seric 		{
61010170Seric 			putline("\n", fp, m);
61110170Seric 			putline("  ----- Message header follows -----\n", fp, m);
6125984Seric 			(void) fflush(fp);
61310170Seric 			putheader(fp, m, e->e_parent);
6145984Seric 		}
6154199Seric 	}
6164199Seric 	else
61710170Seric 	{
61810170Seric 		putline("\n", fp, m);
61910170Seric 		putline("  ----- No message was collected -----\n", fp, m);
62010170Seric 		putline("\n", fp, m);
62110170Seric 	}
6224318Seric 
6234318Seric 	/*
6244318Seric 	**  Cleanup and exit
6254318Seric 	*/
6264318Seric 
627297Seric 	if (errno != 0)
6286978Seric 		syserr("errbody: I/O error");
629297Seric }
63058144Seric /*
63158144Seric **  PRUNEROUTE -- prune an RFC-822 source route
63258144Seric **
63358144Seric **	Trims down a source route to the last internet-registered hop.
63458144Seric **	This is encouraged by RFC 1123 section 5.3.3.
63558144Seric **
63658144Seric **	Parameters:
63758144Seric **		addr -- the address
63858144Seric **
63958144Seric **	Returns:
64058144Seric **		TRUE -- address was modified
64158144Seric **		FALSE -- address could not be pruned
64258144Seric **
64358144Seric **	Side Effects:
64458144Seric **		modifies addr in-place
64558144Seric */
64658144Seric 
64758144Seric pruneroute(addr)
64858144Seric 	char *addr;
64958144Seric {
65058144Seric #ifdef NAMED_BIND
65158144Seric 	char *start, *at, *comma;
65258144Seric 	char c;
65358144Seric 	int rcode;
65458144Seric 	char hostbuf[BUFSIZ];
65558144Seric 	char *mxhosts[MAXMXHOSTS + 1];
65658144Seric 
65758144Seric 	/* check to see if this is really a route-addr */
65858144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
65958144Seric 		return FALSE;
66058144Seric 	start = strchr(addr, ':');
66158144Seric 	at = strrchr(addr, '@');
66258144Seric 	if (start == NULL || at == NULL || at < start)
66358144Seric 		return FALSE;
66458144Seric 
66558144Seric 	/* slice off the angle brackets */
66658144Seric 	strcpy(hostbuf, at + 1);
66758144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
66858144Seric 
66958144Seric 	while (start)
67058144Seric 	{
67158144Seric 		if (getmxrr(hostbuf, mxhosts, "", &rcode) > 0)
67258144Seric 		{
67358144Seric 			strcpy(addr + 1, start + 1);
67458144Seric 			return TRUE;
67558144Seric 		}
67658144Seric 		c = *start;
67758144Seric 		*start = '\0';
67858144Seric 		comma = strrchr(addr, ',');
67958144Seric 		if (comma && comma[1] == '@')
68058144Seric 			strcpy(hostbuf, comma + 2);
68158144Seric 		else
68258144Seric 			comma = 0;
68358144Seric 		*start = c;
68458144Seric 		start = comma;
68558144Seric 	}
68658144Seric #endif
68758144Seric 	return FALSE;
68858144Seric }
689