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*59082Seric static char sccsid[] = "@(#)savemail.c	6.31 (Berkeley) 04/14/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 
669375Seric 	if (bitset(EF_RESPONSE, e->e_flags))
67297Seric 		return;
689337Seric 	e->e_flags &= ~EF_FATALERRS;
69297Seric 
70297Seric 	/*
71297Seric 	**  In the unhappy event we don't know who to return the mail
72297Seric 	**  to, make someone up.
73297Seric 	*/
74297Seric 
759337Seric 	if (e->e_from.q_paddr == NULL)
76297Seric 	{
7758733Seric 		e->e_sender = "Postmaster";
7858704Seric 		if (parseaddr(e->e_sender, &e->e_from, 0, '\0', NULL, e) == NULL)
79297Seric 		{
8058733Seric 			syserr("553 Cannot parse Postmaster!");
81297Seric 			ExitStat = EX_SOFTWARE;
82297Seric 			finis();
83297Seric 		}
84297Seric 	}
859337Seric 	e->e_to = NULL;
86297Seric 
87297Seric 	/*
8824942Seric 	**  Basic state machine.
8924942Seric 	**
9024942Seric 	**	This machine runs through the following states:
9124942Seric 	**
9224942Seric 	**	ESM_QUIET	Errors have already been printed iff the
9324942Seric 	**			sender is local.
9424942Seric 	**	ESM_REPORT	Report directly to the sender's terminal.
9524942Seric 	**	ESM_MAIL	Mail response to the sender.
9624942Seric 	**	ESM_DEADLETTER	Save response in ~/dead.letter.
9724942Seric 	**	ESM_POSTMASTER	Mail response to the postmaster.
9824942Seric 	**	ESM_PANIC	Save response anywhere possible.
99297Seric 	*/
100297Seric 
10124942Seric 	/* determine starting state */
10258734Seric 	switch (e->e_errormode)
103297Seric 	{
10424942Seric 	  case EM_WRITE:
10524942Seric 		state = ESM_REPORT;
10624942Seric 		break;
10724942Seric 
10824942Seric 	  case EM_BERKNET:
10924942Seric 		/* mail back, but return o.k. exit status */
110401Seric 		ExitStat = EX_OK;
11124942Seric 
11224942Seric 		/* fall through.... */
11324942Seric 
11424942Seric 	  case EM_MAIL:
11524942Seric 		state = ESM_MAIL;
11624942Seric 		break;
11724942Seric 
11824942Seric 	  case EM_PRINT:
11924979Seric 	  case '\0':
12024942Seric 		state = ESM_QUIET;
12124942Seric 		break;
12224942Seric 
12324942Seric 	  case EM_QUIET:
12424942Seric 		/* no need to return anything at all */
12524942Seric 		return;
12624979Seric 
12724979Seric 	  default:
12858734Seric 		syserr("554 savemail: bogus errormode x%x\n", e->e_errormode);
12924979Seric 		state = ESM_MAIL;
13024979Seric 		break;
131297Seric 	}
132297Seric 
13324942Seric 	while (state != ESM_DONE)
134297Seric 	{
13524979Seric 		if (tTd(6, 5))
13624979Seric 			printf("  state %d\n", state);
13724979Seric 
13824942Seric 		switch (state)
139297Seric 		{
14024979Seric 		  case ESM_QUIET:
14124979Seric 			if (e->e_from.q_mailer == LocalMailer)
14224979Seric 				state = ESM_DEADLETTER;
14324979Seric 			else
14424979Seric 				state = ESM_MAIL;
14524979Seric 			break;
14624979Seric 
14724942Seric 		  case ESM_REPORT:
14824942Seric 
14924942Seric 			/*
15024942Seric 			**  If the user is still logged in on the same terminal,
15124942Seric 			**  then write the error messages back to hir (sic).
15224942Seric 			*/
15324942Seric 
15424942Seric 			p = ttypath();
15524942Seric 			if (p == NULL || freopen(p, "w", stdout) == NULL)
15624942Seric 			{
15724942Seric 				state = ESM_MAIL;
15824942Seric 				break;
15924942Seric 			}
16024942Seric 
16158050Seric 			expand("\201n", buf, &buf[sizeof buf - 1], e);
1629375Seric 			printf("\r\nMessage from %s...\r\n", buf);
1639375Seric 			printf("Errors occurred while sending mail.\r\n");
1649542Seric 			if (e->e_xfp != NULL)
1659375Seric 			{
1669542Seric 				(void) fflush(e->e_xfp);
16724942Seric 				fp = fopen(queuename(e, 'x'), "r");
1689375Seric 			}
1699375Seric 			else
17024942Seric 				fp = NULL;
17124942Seric 			if (fp == NULL)
1729375Seric 			{
1739337Seric 				syserr("Cannot open %s", queuename(e, 'x'));
1749375Seric 				printf("Transcript of session is unavailable.\r\n");
1759375Seric 			}
1769375Seric 			else
1779375Seric 			{
1789375Seric 				printf("Transcript follows:\r\n");
17924942Seric 				while (fgets(buf, sizeof buf, fp) != NULL &&
1809375Seric 				       !ferror(stdout))
1819375Seric 					fputs(buf, stdout);
18258680Seric 				(void) xfclose(fp, "savemail transcript", e->e_id);
1839375Seric 			}
18424942Seric 			printf("Original message will be saved in dead.letter.\r\n");
18524942Seric 			state = ESM_DEADLETTER;
18624942Seric 			break;
187297Seric 
18824942Seric 		  case ESM_MAIL:
18924942Seric 			/*
19024942Seric 			**  If mailing back, do it.
19124942Seric 			**	Throw away all further output.  Don't alias,
19224942Seric 			**	since this could cause loops, e.g., if joe
19324942Seric 			**	mails to joe@x, and for some reason the network
19424942Seric 			**	for @x is down, then the response gets sent to
19524942Seric 			**	joe@x, which gives a response, etc.  Also force
19624942Seric 			**	the mail to be delivered even if a version of
19724942Seric 			**	it has already been sent to the sender.
19824942Seric 			*/
199297Seric 
20058680Seric 			if (strcmp(e->e_from.q_paddr, "<>") != 0)
20158680Seric 				(void) sendtolist(e->e_from.q_paddr,
20258680Seric 					  (ADDRESS *) NULL,
20358680Seric 					  &e->e_errorqueue, e);
20458680Seric 
20558680Seric 			/* deliver a cc: to the postmaster if desired */
20658680Seric 			if (PostMasterCopy != NULL)
20724942Seric 			{
20858680Seric 				auto ADDRESS *rlist = NULL;
20958178Seric 
21058680Seric 				(void) sendtolist(PostMasterCopy,
21158304Seric 						  (ADDRESS *) NULL,
21258680Seric 						  &rlist, e);
21358680Seric 				(void) returntosender(e->e_message,
21458680Seric 						      rlist, FALSE, e);
21558680Seric 			}
21658680Seric 			q = e->e_errorqueue;
21758680Seric 			if (q == NULL)
21858680Seric 			{
21958680Seric 				/* this is an error-error */
22058680Seric 				state = ESM_POSTMASTER;
22158680Seric 				break;
22258680Seric 			}
22358966Seric 			if (returntosender(e->e_message,
22458680Seric 					   q, (e->e_class >= 0), e) == 0)
22558680Seric 			{
22658680Seric 				state = ESM_DONE;
22758680Seric 				break;
22858680Seric 			}
22924981Seric 
23058680Seric 			/* didn't work -- return to postmaster */
23158680Seric 			state = ESM_POSTMASTER;
23258680Seric 			break;
23358306Seric 
23458680Seric 		  case ESM_POSTMASTER:
23558680Seric 			/*
23658680Seric 			**  Similar to previous case, but to system postmaster.
23758680Seric 			*/
23858680Seric 
23959057Seric 			q = parseaddr("postmaster", q, 0, '\0', NULL, e);
24059057Seric 			if (q == NULL)
24124942Seric 			{
24258680Seric 				syserr("553 cannot parse postmaster!");
24358680Seric 				ExitStat = EX_SOFTWARE;
24458680Seric 				state = ESM_USRTMP;
24558680Seric 				break;
24624942Seric 			}
24758966Seric 			if (returntosender(e->e_message,
24857438Seric 					   q, (e->e_class >= 0), e) == 0)
24924942Seric 			{
25024942Seric 				state = ESM_DONE;
25124942Seric 				break;
25224942Seric 			}
253297Seric 
25458680Seric 			/* didn't work -- last resort */
25558680Seric 			state = ESM_USRTMP;
25624942Seric 			break;
257297Seric 
25824942Seric 		  case ESM_DEADLETTER:
25924942Seric 			/*
26024942Seric 			**  Save the message in dead.letter.
26124942Seric 			**	If we weren't mailing back, and the user is
26224942Seric 			**	local, we should save the message in
26324942Seric 			**	~/dead.letter so that the poor person doesn't
26424942Seric 			**	have to type it over again -- and we all know
26524942Seric 			**	what poor typists UNIX users are.
26624942Seric 			*/
2675315Seric 
26824942Seric 			p = NULL;
26924942Seric 			if (e->e_from.q_mailer == LocalMailer)
27024942Seric 			{
27124942Seric 				if (e->e_from.q_home != NULL)
27224942Seric 					p = e->e_from.q_home;
27324942Seric 				else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
27424942Seric 					p = pw->pw_dir;
27524942Seric 			}
27624942Seric 			if (p == NULL)
27724942Seric 			{
27858865Seric 				/* no local directory */
27924942Seric 				state = ESM_MAIL;
28024942Seric 				break;
28124942Seric 			}
28224942Seric 			if (e->e_dfp != NULL)
28324942Seric 			{
28424942Seric 				auto ADDRESS *q;
28524942Seric 				bool oldverb = Verbose;
28624942Seric 
28724942Seric 				/* we have a home directory; open dead.letter */
28824942Seric 				define('z', p, e);
28958050Seric 				expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e);
29024942Seric 				Verbose = TRUE;
29158151Seric 				message("Saving message in %s", buf);
29224942Seric 				Verbose = oldverb;
29324942Seric 				e->e_to = buf;
29424942Seric 				q = NULL;
29558082Seric 				(void) sendtolist(buf, &e->e_from, &q, e);
29624942Seric 				if (deliver(e, q) == 0)
29724942Seric 					state = ESM_DONE;
29824942Seric 				else
29924942Seric 					state = ESM_MAIL;
30024942Seric 			}
30125569Seric 			else
30225569Seric 			{
30325569Seric 				/* no data file -- try mailing back */
30425569Seric 				state = ESM_MAIL;
30525569Seric 			}
30624942Seric 			break;
30724942Seric 
30824942Seric 		  case ESM_USRTMP:
30924942Seric 			/*
31024942Seric 			**  Log the mail in /usr/tmp/dead.letter.
31124942Seric 			*/
31224942Seric 
31357438Seric 			if (e->e_class < 0)
31457438Seric 			{
31557438Seric 				state = ESM_DONE;
31657438Seric 				break;
31757438Seric 			}
31857438Seric 
31924942Seric 			fp = dfopen("/usr/tmp/dead.letter", "a");
32024942Seric 			if (fp == NULL)
32124942Seric 			{
32224942Seric 				state = ESM_PANIC;
32324942Seric 				break;
32424942Seric 			}
32524942Seric 
32658010Seric 			putfromline(fp, FileMailer, e);
32758010Seric 			(*e->e_puthdr)(fp, FileMailer, e);
32858010Seric 			putline("\n", fp, FileMailer);
32958010Seric 			(*e->e_putbody)(fp, FileMailer, e);
33058010Seric 			putline("\n", fp, FileMailer);
33124942Seric 			(void) fflush(fp);
33224942Seric 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
33358680Seric 			(void) xfclose(fp, "savemail", "/usr/tmp/dead.letter");
33424942Seric 			break;
33524942Seric 
33624942Seric 		  default:
33758151Seric 			syserr("554 savemail: unknown state %d", state);
33824942Seric 
33924942Seric 			/* fall through ... */
34024942Seric 
34124942Seric 		  case ESM_PANIC:
34224942Seric 			/* leave the locked queue & transcript files around */
34358151Seric 			syserr("554 savemail: cannot save rejected email anywhere");
34424942Seric 			exit(EX_SOFTWARE);
34524942Seric 		}
346297Seric 	}
347297Seric }
348297Seric /*
3494633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
3504633Seric **
3514633Seric **	Parameters:
3524633Seric **		msg -- the explanatory message.
35316479Seric **		returnq -- the queue of people to send the message to.
3545984Seric **		sendbody -- if TRUE, also send back the body of the
3555984Seric **			message; otherwise just send the header.
35655012Seric **		e -- the current envelope.
3574633Seric **
3584633Seric **	Returns:
3594633Seric **		zero -- if everything went ok.
3604633Seric **		else -- some error.
3614633Seric **
3624633Seric **	Side Effects:
3634633Seric **		Returns the current message to the sender via
3644633Seric **		mail.
3654633Seric */
3664633Seric 
3675984Seric static bool	SendBody;
3684633Seric 
3697045Seric #define MAXRETURNS	6	/* max depth of returning messages */
37058559Seric #define ERRORFUDGE	100	/* nominal size of error message text */
3717045Seric 
37255012Seric returntosender(msg, returnq, sendbody, e)
3734633Seric 	char *msg;
37416479Seric 	ADDRESS *returnq;
3755984Seric 	bool sendbody;
37655012Seric 	register ENVELOPE *e;
3774633Seric {
3784633Seric 	char buf[MAXNAME];
3796978Seric 	extern putheader(), errbody();
3806978Seric 	register ENVELOPE *ee;
38158680Seric 	ENVELOPE *oldcur = CurEnv;
3826978Seric 	extern ENVELOPE *newenvelope();
3836978Seric 	ENVELOPE errenvelope;
3847045Seric 	static int returndepth;
3859375Seric 	register ADDRESS *q;
3864633Seric 
38758966Seric 	if (msg == NULL)
38858966Seric 		msg = "Unable to deliver mail";
38958966Seric 
3907676Seric 	if (tTd(6, 1))
3917287Seric 	{
39258680Seric 		printf("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
39355012Seric 		       msg, returndepth, e);
39416479Seric 		printaddr(returnq, TRUE);
3957287Seric 	}
3967287Seric 
3977045Seric 	if (++returndepth >= MAXRETURNS)
3987045Seric 	{
3997045Seric 		if (returndepth != MAXRETURNS)
40058151Seric 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
4017045Seric 		/* don't "unrecurse" and fake a clean exit */
4027045Seric 		/* returndepth--; */
4037045Seric 		return (0);
4047045Seric 	}
4057045Seric 
4065984Seric 	SendBody = sendbody;
40758680Seric 	define('g', e->e_from.q_paddr, e);
40858179Seric 	ee = newenvelope(&errenvelope, e);
40958050Seric 	define('a', "\201b", ee);
41059057Seric 	define('r', "internal", ee);
41159057Seric 	define('s', "localhost", ee);
41259057Seric 	define('_', "localhost", ee);
4136978Seric 	ee->e_puthdr = putheader;
4146978Seric 	ee->e_putbody = errbody;
4159375Seric 	ee->e_flags |= EF_RESPONSE;
41655012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
41745155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
41816479Seric 	ee->e_sendqueue = returnq;
41958559Seric 	ee->e_msgsize = e->e_msgsize + ERRORFUDGE;
4209542Seric 	openxscript(ee);
42116479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4229375Seric 	{
42358559Seric 		if (bitset(QDONTSEND, q->q_flags))
42458559Seric 			continue;
42558559Seric 
42658559Seric 		ee->e_nrcpts++;
42758559Seric 
42858144Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
42958333Seric 			parseaddr(q->q_paddr, q, 0, '\0', NULL, e);
43058144Seric 
4319375Seric 		if (q->q_alias == NULL)
4329375Seric 			addheader("to", q->q_paddr, ee);
4339375Seric 	}
43424942Seric 
43557642Seric # ifdef LOG
43658020Seric 	if (LogLevel > 5)
43757642Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
43857642Seric 			e->e_id, ee->e_id, msg);
43957642Seric # endif
44057642Seric 
44110845Seric 	(void) sprintf(buf, "Returned mail: %s", msg);
44210106Seric 	addheader("subject", buf, ee);
4434633Seric 
4444633Seric 	/* fake up an address header for the from person */
44558050Seric 	expand("\201n", buf, &buf[sizeof buf - 1], e);
44658680Seric 	if (parseaddr(buf, &ee->e_from, 1, '\0', NULL, e) == NULL)
4474633Seric 	{
44858151Seric 		syserr("553 Can't parse myself!");
4494633Seric 		ExitStat = EX_SOFTWARE;
4507045Seric 		returndepth--;
4514633Seric 		return (-1);
4524633Seric 	}
45358704Seric 	ee->e_sender = ee->e_from.q_paddr;
4545984Seric 
4556978Seric 	/* push state into submessage */
4566978Seric 	CurEnv = ee;
45758050Seric 	define('f', "\201n", ee);
4589375Seric 	define('x', "Mail Delivery Subsystem", ee);
45958929Seric 	eatheader(ee, TRUE);
4605984Seric 
4616978Seric 	/* actually deliver the error message */
46214876Seric 	sendall(ee, SM_DEFAULT);
4636978Seric 
4646978Seric 	/* restore state */
4657811Seric 	dropenvelope(ee);
46658680Seric 	CurEnv = oldcur;
4677045Seric 	returndepth--;
4686978Seric 
4697045Seric 	/* should check for delivery errors here */
4704633Seric 	return (0);
4714633Seric }
4724633Seric /*
4736978Seric **  ERRBODY -- output the body of an error message.
4746978Seric **
4756978Seric **	Typically this is a copy of the transcript plus a copy of the
4766978Seric **	original offending message.
4776978Seric **
478297Seric **	Parameters:
479297Seric **		fp -- the output file.
48010170Seric **		m -- the mailer to output to.
4819542Seric **		e -- the envelope we are working in.
482297Seric **
483297Seric **	Returns:
484297Seric **		none
485297Seric **
486297Seric **	Side Effects:
4876978Seric **		Outputs the body of an error message.
488297Seric */
489297Seric 
49010170Seric errbody(fp, m, e)
491297Seric 	register FILE *fp;
4924318Seric 	register struct mailer *m;
4939542Seric 	register ENVELOPE *e;
494297Seric {
4956978Seric 	register FILE *xfile;
496*59082Seric 	char *p;
497*59082Seric 	register ADDRESS *q;
498*59082Seric 	bool printheader;
4993189Seric 	char buf[MAXLINE];
500297Seric 
50158680Seric 	if (e->e_parent == NULL)
50258680Seric 	{
50358680Seric 		syserr("errbody: null parent");
50458680Seric 		putline("\n", fp, m);
50558680Seric 		putline("   ----- Original message lost -----\n", fp, m);
50658680Seric 		return;
50758680Seric 	}
50858680Seric 
5099057Seric 	/*
51055372Seric 	**  Output error message header (if specified and available).
51155372Seric 	*/
51255372Seric 
51355372Seric 	if (ErrMsgFile != NULL)
51455372Seric 	{
51555372Seric 		if (*ErrMsgFile == '/')
51655372Seric 		{
51755372Seric 			xfile = fopen(ErrMsgFile, "r");
51855372Seric 			if (xfile != NULL)
51955372Seric 			{
52055372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
52155425Seric 				{
52255425Seric 					expand(buf, buf, &buf[sizeof buf - 1], e);
52355372Seric 					putline(buf, fp, m);
52455425Seric 				}
52555372Seric 				(void) fclose(xfile);
526*59082Seric 				putline("\n", fp, m);
52755372Seric 			}
52855372Seric 		}
52955372Seric 		else
53055372Seric 		{
53155425Seric 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
53255425Seric 			putline(buf, fp, m);
533*59082Seric 			putline("\n", fp, m);
53455372Seric 		}
53555372Seric 	}
53655372Seric 
53755372Seric 	/*
538*59082Seric 	**  Output message introduction
539*59082Seric 	*/
540*59082Seric 
541*59082Seric 	printheader = TRUE;
542*59082Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
543*59082Seric 	{
544*59082Seric 		if (bitset(QBADADDR, q->q_flags))
545*59082Seric 		{
546*59082Seric 			if (printheader)
547*59082Seric 			{
548*59082Seric 				putline("The following addresses failed:\n",
549*59082Seric 					fp, m);
550*59082Seric 				printheader = FALSE;
551*59082Seric 			}
552*59082Seric 			sprintf(buf, "\t%s\n", q->q_paddr);
553*59082Seric 			putline(buf, fp, m);
554*59082Seric 		}
555*59082Seric 	}
556*59082Seric 	if (!printheader)
557*59082Seric 		putline("\n", fp, m);
558*59082Seric 
559*59082Seric 	/*
5609057Seric 	**  Output transcript of errors
5619057Seric 	*/
5629057Seric 
5634086Seric 	(void) fflush(stdout);
5649542Seric 	p = queuename(e->e_parent, 'x');
5659337Seric 	if ((xfile = fopen(p, "r")) == NULL)
5669057Seric 	{
5679337Seric 		syserr("Cannot open %s", p);
568*59082Seric 		putline("   ----- Transcript of session is unavailable -----\n", fp, m);
5699057Seric 	}
5709057Seric 	else
5719057Seric 	{
57258680Seric 		putline("   ----- Transcript of session follows -----\n", fp, m);
5739542Seric 		if (e->e_xfp != NULL)
5749542Seric 			(void) fflush(e->e_xfp);
5759057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
57610170Seric 			putline(buf, fp, m);
57758680Seric 		(void) xfclose(xfile, "errbody xscript", p);
5789057Seric 	}
579297Seric 	errno = 0;
5804318Seric 
5814318Seric 	/*
5824318Seric 	**  Output text of original message
5834318Seric 	*/
5844318Seric 
5854289Seric 	if (NoReturn)
58658665Seric 		SendBody = FALSE;
58758680Seric 	if (e->e_parent->e_df != NULL)
5884199Seric 	{
5895984Seric 		if (SendBody)
5905984Seric 		{
59110170Seric 			putline("\n", fp, m);
59210170Seric 			putline("   ----- Unsent message follows -----\n", fp, m);
5935984Seric 			(void) fflush(fp);
59410170Seric 			putheader(fp, m, e->e_parent);
59510170Seric 			putline("\n", fp, m);
59610170Seric 			putbody(fp, m, e->e_parent);
5975984Seric 		}
5985984Seric 		else
5995984Seric 		{
60010170Seric 			putline("\n", fp, m);
60110170Seric 			putline("  ----- Message header follows -----\n", fp, m);
6025984Seric 			(void) fflush(fp);
60310170Seric 			putheader(fp, m, e->e_parent);
6045984Seric 		}
6054199Seric 	}
6064199Seric 	else
60710170Seric 	{
60810170Seric 		putline("\n", fp, m);
60910170Seric 		putline("  ----- No message was collected -----\n", fp, m);
61010170Seric 		putline("\n", fp, m);
61110170Seric 	}
6124318Seric 
6134318Seric 	/*
6144318Seric 	**  Cleanup and exit
6154318Seric 	*/
6164318Seric 
617297Seric 	if (errno != 0)
6186978Seric 		syserr("errbody: I/O error");
619297Seric }
62058144Seric /*
62158144Seric **  PRUNEROUTE -- prune an RFC-822 source route
62258144Seric **
62358144Seric **	Trims down a source route to the last internet-registered hop.
62458144Seric **	This is encouraged by RFC 1123 section 5.3.3.
62558144Seric **
62658144Seric **	Parameters:
62758144Seric **		addr -- the address
62858144Seric **
62958144Seric **	Returns:
63058144Seric **		TRUE -- address was modified
63158144Seric **		FALSE -- address could not be pruned
63258144Seric **
63358144Seric **	Side Effects:
63458144Seric **		modifies addr in-place
63558144Seric */
63658144Seric 
63758144Seric pruneroute(addr)
63858144Seric 	char *addr;
63958144Seric {
64058144Seric #ifdef NAMED_BIND
64158144Seric 	char *start, *at, *comma;
64258144Seric 	char c;
64358144Seric 	int rcode;
64458144Seric 	char hostbuf[BUFSIZ];
64558144Seric 	char *mxhosts[MAXMXHOSTS + 1];
64658144Seric 
64758144Seric 	/* check to see if this is really a route-addr */
64858144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
64958144Seric 		return FALSE;
65058144Seric 	start = strchr(addr, ':');
65158144Seric 	at = strrchr(addr, '@');
65258144Seric 	if (start == NULL || at == NULL || at < start)
65358144Seric 		return FALSE;
65458144Seric 
65558144Seric 	/* slice off the angle brackets */
65658144Seric 	strcpy(hostbuf, at + 1);
65758144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
65858144Seric 
65958144Seric 	while (start)
66058144Seric 	{
66158144Seric 		if (getmxrr(hostbuf, mxhosts, "", &rcode) > 0)
66258144Seric 		{
66358144Seric 			strcpy(addr + 1, start + 1);
66458144Seric 			return TRUE;
66558144Seric 		}
66658144Seric 		c = *start;
66758144Seric 		*start = '\0';
66858144Seric 		comma = strrchr(addr, ',');
66958144Seric 		if (comma && comma[1] == '@')
67058144Seric 			strcpy(hostbuf, comma + 2);
67158144Seric 		else
67258144Seric 			comma = 0;
67358144Seric 		*start = c;
67458144Seric 		start = comma;
67558144Seric 	}
67658144Seric #endif
67758144Seric 	return FALSE;
67858144Seric }
679