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*58680Seric static char sccsid[] = "@(#)savemail.c	6.22 (Berkeley) 03/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;
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))
61*58680Seric 	{
62*58680Seric 		printf("\nsavemail, ErrorMode = %c\n  e_from=", ErrorMode);
63*58680Seric 		printaddr(&e->e_from, FALSE);
64*58680Seric 	}
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 	{
7758333Seric 		if (parseaddr("root", &e->e_from, 0, '\0', NULL, e) == NULL)
78297Seric 		{
7958151Seric 			syserr("553 Cannot parse root!");
80297Seric 			ExitStat = EX_SOFTWARE;
81297Seric 			finis();
82297Seric 		}
83297Seric 	}
849337Seric 	e->e_to = NULL;
85297Seric 
86297Seric 	/*
8724942Seric 	**  Basic state machine.
8824942Seric 	**
8924942Seric 	**	This machine runs through the following states:
9024942Seric 	**
9124942Seric 	**	ESM_QUIET	Errors have already been printed iff the
9224942Seric 	**			sender is local.
9324942Seric 	**	ESM_REPORT	Report directly to the sender's terminal.
9424942Seric 	**	ESM_MAIL	Mail response to the sender.
9524942Seric 	**	ESM_DEADLETTER	Save response in ~/dead.letter.
9624942Seric 	**	ESM_POSTMASTER	Mail response to the postmaster.
9724942Seric 	**	ESM_PANIC	Save response anywhere possible.
98297Seric 	*/
99297Seric 
10024942Seric 	/* determine starting state */
10124942Seric 	switch (ErrorMode)
102297Seric 	{
10324942Seric 	  case EM_WRITE:
10424942Seric 		state = ESM_REPORT;
10524942Seric 		break;
10624942Seric 
10724942Seric 	  case EM_BERKNET:
10824942Seric 		/* mail back, but return o.k. exit status */
109401Seric 		ExitStat = EX_OK;
11024942Seric 
11124942Seric 		/* fall through.... */
11224942Seric 
11324942Seric 	  case EM_MAIL:
11424942Seric 		state = ESM_MAIL;
11524942Seric 		break;
11624942Seric 
11724942Seric 	  case EM_PRINT:
11824979Seric 	  case '\0':
11924942Seric 		state = ESM_QUIET;
12024942Seric 		break;
12124942Seric 
12224942Seric 	  case EM_QUIET:
12324942Seric 		/* no need to return anything at all */
12424942Seric 		return;
12524979Seric 
12624979Seric 	  default:
12758151Seric 		syserr("554 savemail: ErrorMode x%x\n");
12824979Seric 		state = ESM_MAIL;
12924979Seric 		break;
130297Seric 	}
131297Seric 
13224942Seric 	while (state != ESM_DONE)
133297Seric 	{
13424979Seric 		if (tTd(6, 5))
13524979Seric 			printf("  state %d\n", state);
13624979Seric 
13724942Seric 		switch (state)
138297Seric 		{
13924979Seric 		  case ESM_QUIET:
14024979Seric 			if (e->e_from.q_mailer == LocalMailer)
14124979Seric 				state = ESM_DEADLETTER;
14224979Seric 			else
14324979Seric 				state = ESM_MAIL;
14424979Seric 			break;
14524979Seric 
14624942Seric 		  case ESM_REPORT:
14724942Seric 
14824942Seric 			/*
14924942Seric 			**  If the user is still logged in on the same terminal,
15024942Seric 			**  then write the error messages back to hir (sic).
15124942Seric 			*/
15224942Seric 
15324942Seric 			p = ttypath();
15424942Seric 			if (p == NULL || freopen(p, "w", stdout) == NULL)
15524942Seric 			{
15624942Seric 				state = ESM_MAIL;
15724942Seric 				break;
15824942Seric 			}
15924942Seric 
16058050Seric 			expand("\201n", buf, &buf[sizeof buf - 1], e);
1619375Seric 			printf("\r\nMessage from %s...\r\n", buf);
1629375Seric 			printf("Errors occurred while sending mail.\r\n");
1639542Seric 			if (e->e_xfp != NULL)
1649375Seric 			{
1659542Seric 				(void) fflush(e->e_xfp);
16624942Seric 				fp = fopen(queuename(e, 'x'), "r");
1679375Seric 			}
1689375Seric 			else
16924942Seric 				fp = NULL;
17024942Seric 			if (fp == NULL)
1719375Seric 			{
1729337Seric 				syserr("Cannot open %s", queuename(e, 'x'));
1739375Seric 				printf("Transcript of session is unavailable.\r\n");
1749375Seric 			}
1759375Seric 			else
1769375Seric 			{
1779375Seric 				printf("Transcript follows:\r\n");
17824942Seric 				while (fgets(buf, sizeof buf, fp) != NULL &&
1799375Seric 				       !ferror(stdout))
1809375Seric 					fputs(buf, stdout);
181*58680Seric 				(void) xfclose(fp, "savemail transcript", e->e_id);
1829375Seric 			}
18324942Seric 			printf("Original message will be saved in dead.letter.\r\n");
18424942Seric 			state = ESM_DEADLETTER;
18524942Seric 			break;
186297Seric 
18724942Seric 		  case ESM_MAIL:
18824942Seric 			/*
18924942Seric 			**  If mailing back, do it.
19024942Seric 			**	Throw away all further output.  Don't alias,
19124942Seric 			**	since this could cause loops, e.g., if joe
19224942Seric 			**	mails to joe@x, and for some reason the network
19324942Seric 			**	for @x is down, then the response gets sent to
19424942Seric 			**	joe@x, which gives a response, etc.  Also force
19524942Seric 			**	the mail to be delivered even if a version of
19624942Seric 			**	it has already been sent to the sender.
19724942Seric 			*/
198297Seric 
199*58680Seric 			if (strcmp(e->e_from.q_paddr, "<>") != 0)
200*58680Seric 				(void) sendtolist(e->e_from.q_paddr,
201*58680Seric 					  (ADDRESS *) NULL,
202*58680Seric 					  &e->e_errorqueue, e);
203*58680Seric 
204*58680Seric 			/* deliver a cc: to the postmaster if desired */
205*58680Seric 			if (PostMasterCopy != NULL)
20624942Seric 			{
207*58680Seric 				auto ADDRESS *rlist = NULL;
20858178Seric 
209*58680Seric 				(void) sendtolist(PostMasterCopy,
21058304Seric 						  (ADDRESS *) NULL,
211*58680Seric 						  &rlist, e);
212*58680Seric 				(void) returntosender(e->e_message,
213*58680Seric 						      rlist, FALSE, e);
214*58680Seric 			}
215*58680Seric 			q = e->e_errorqueue;
216*58680Seric 			if (q == NULL)
217*58680Seric 			{
218*58680Seric 				/* this is an error-error */
219*58680Seric 				state = ESM_POSTMASTER;
220*58680Seric 				break;
221*58680Seric 			}
222*58680Seric 			if (returntosender(e->e_message != NULL ? e->e_message :
223*58680Seric 					   "Unable to deliver mail",
224*58680Seric 					   q, (e->e_class >= 0), e) == 0)
225*58680Seric 			{
226*58680Seric 				state = ESM_DONE;
227*58680Seric 				break;
228*58680Seric 			}
22924981Seric 
230*58680Seric 			/* didn't work -- return to postmaster */
231*58680Seric 			state = ESM_POSTMASTER;
232*58680Seric 			break;
23358306Seric 
234*58680Seric 		  case ESM_POSTMASTER:
235*58680Seric 			/*
236*58680Seric 			**  Similar to previous case, but to system postmaster.
237*58680Seric 			*/
238*58680Seric 
239*58680Seric 			if (parseaddr("postmaster", q, 0, '\0', NULL, e) == NULL)
24024942Seric 			{
241*58680Seric 				syserr("553 cannot parse postmaster!");
242*58680Seric 				ExitStat = EX_SOFTWARE;
243*58680Seric 				state = ESM_USRTMP;
244*58680Seric 				break;
24524942Seric 			}
24624942Seric 			if (returntosender(e->e_message != NULL ? e->e_message :
24724942Seric 					   "Unable to deliver mail",
24857438Seric 					   q, (e->e_class >= 0), e) == 0)
24924942Seric 			{
25024942Seric 				state = ESM_DONE;
25124942Seric 				break;
25224942Seric 			}
253297Seric 
254*58680Seric 			/* didn't work -- last resort */
255*58680Seric 			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 			{
27858151Seric 				syserr("554 Can't return mail to %s", e->e_from.q_paddr);
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;
333*58680Seric 			(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;
381*58680Seric 	ENVELOPE *oldcur = CurEnv;
3826978Seric 	extern ENVELOPE *newenvelope();
3836978Seric 	ENVELOPE errenvelope;
3847045Seric 	static int returndepth;
3859375Seric 	register ADDRESS *q;
3864633Seric 
3877676Seric 	if (tTd(6, 1))
3887287Seric 	{
389*58680Seric 		printf("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
39055012Seric 		       msg, returndepth, e);
39116479Seric 		printaddr(returnq, TRUE);
3927287Seric 	}
3937287Seric 
3947045Seric 	if (++returndepth >= MAXRETURNS)
3957045Seric 	{
3967045Seric 		if (returndepth != MAXRETURNS)
39758151Seric 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
3987045Seric 		/* don't "unrecurse" and fake a clean exit */
3997045Seric 		/* returndepth--; */
4007045Seric 		return (0);
4017045Seric 	}
4027045Seric 
4035984Seric 	SendBody = sendbody;
404*58680Seric 	define('g', e->e_from.q_paddr, e);
40558179Seric 	ee = newenvelope(&errenvelope, e);
40658050Seric 	define('a', "\201b", ee);
4076978Seric 	ee->e_puthdr = putheader;
4086978Seric 	ee->e_putbody = errbody;
4099375Seric 	ee->e_flags |= EF_RESPONSE;
41055012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
41145155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
41216479Seric 	ee->e_sendqueue = returnq;
41358559Seric 	ee->e_msgsize = e->e_msgsize + ERRORFUDGE;
4149542Seric 	openxscript(ee);
41516479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4169375Seric 	{
41758559Seric 		if (bitset(QDONTSEND, q->q_flags))
41858559Seric 			continue;
41958559Seric 
42058559Seric 		ee->e_nrcpts++;
42158559Seric 
42258144Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
42358333Seric 			parseaddr(q->q_paddr, q, 0, '\0', NULL, e);
42458144Seric 
4259375Seric 		if (q->q_alias == NULL)
4269375Seric 			addheader("to", q->q_paddr, ee);
4279375Seric 	}
42824942Seric 
42957642Seric # ifdef LOG
43058020Seric 	if (LogLevel > 5)
43157642Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
43257642Seric 			e->e_id, ee->e_id, msg);
43357642Seric # endif
43457642Seric 
43510845Seric 	(void) sprintf(buf, "Returned mail: %s", msg);
43610106Seric 	addheader("subject", buf, ee);
4374633Seric 
4384633Seric 	/* fake up an address header for the from person */
43958050Seric 	expand("\201n", buf, &buf[sizeof buf - 1], e);
440*58680Seric 	if (parseaddr(buf, &ee->e_from, 1, '\0', NULL, e) == NULL)
4414633Seric 	{
44258151Seric 		syserr("553 Can't parse myself!");
4434633Seric 		ExitStat = EX_SOFTWARE;
4447045Seric 		returndepth--;
4454633Seric 		return (-1);
4464633Seric 	}
4475984Seric 
4486978Seric 	/* push state into submessage */
4496978Seric 	CurEnv = ee;
45058050Seric 	define('f', "\201n", ee);
4519375Seric 	define('x', "Mail Delivery Subsystem", ee);
45257642Seric 	eatheader(ee, FALSE);
4535984Seric 
4546978Seric 	/* actually deliver the error message */
45514876Seric 	sendall(ee, SM_DEFAULT);
4566978Seric 
4576978Seric 	/* restore state */
4587811Seric 	dropenvelope(ee);
459*58680Seric 	CurEnv = oldcur;
4607045Seric 	returndepth--;
4616978Seric 
4627045Seric 	/* should check for delivery errors here */
4634633Seric 	return (0);
4644633Seric }
4654633Seric /*
4666978Seric **  ERRBODY -- output the body of an error message.
4676978Seric **
4686978Seric **	Typically this is a copy of the transcript plus a copy of the
4696978Seric **	original offending message.
4706978Seric **
471297Seric **	Parameters:
472297Seric **		fp -- the output file.
47310170Seric **		m -- the mailer to output to.
4749542Seric **		e -- the envelope we are working in.
475297Seric **
476297Seric **	Returns:
477297Seric **		none
478297Seric **
479297Seric **	Side Effects:
4806978Seric **		Outputs the body of an error message.
481297Seric */
482297Seric 
48310170Seric errbody(fp, m, e)
484297Seric 	register FILE *fp;
4854318Seric 	register struct mailer *m;
4869542Seric 	register ENVELOPE *e;
487297Seric {
4886978Seric 	register FILE *xfile;
4893189Seric 	char buf[MAXLINE];
4909337Seric 	char *p;
491297Seric 
492*58680Seric 	if (e->e_parent == NULL)
493*58680Seric 	{
494*58680Seric 		syserr("errbody: null parent");
495*58680Seric 		putline("\n", fp, m);
496*58680Seric 		putline("   ----- Original message lost -----\n", fp, m);
497*58680Seric 		return;
498*58680Seric 	}
499*58680Seric 
5009057Seric 	/*
50155372Seric 	**  Output error message header (if specified and available).
50255372Seric 	*/
50355372Seric 
50455372Seric 	if (ErrMsgFile != NULL)
50555372Seric 	{
50655372Seric 		if (*ErrMsgFile == '/')
50755372Seric 		{
50855372Seric 			xfile = fopen(ErrMsgFile, "r");
50955372Seric 			if (xfile != NULL)
51055372Seric 			{
51155372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
51255425Seric 				{
51355425Seric 					expand(buf, buf, &buf[sizeof buf - 1], e);
51455372Seric 					putline(buf, fp, m);
51555425Seric 				}
51655372Seric 				(void) fclose(xfile);
51755372Seric 				fprintf(fp, "\n");
51855372Seric 			}
51955372Seric 		}
52055372Seric 		else
52155372Seric 		{
52255425Seric 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
52355425Seric 			putline(buf, fp, m);
52455372Seric 			fprintf(fp, "\n");
52555372Seric 		}
52655372Seric 	}
52755372Seric 
52855372Seric 	/*
5299057Seric 	**  Output transcript of errors
5309057Seric 	*/
5319057Seric 
5324086Seric 	(void) fflush(stdout);
5339542Seric 	p = queuename(e->e_parent, 'x');
5349337Seric 	if ((xfile = fopen(p, "r")) == NULL)
5359057Seric 	{
5369337Seric 		syserr("Cannot open %s", p);
537*58680Seric 		putline("  ----- Transcript of session is unavailable -----\n", fp, m);
5389057Seric 	}
5399057Seric 	else
5409057Seric 	{
541*58680Seric 		putline("   ----- Transcript of session follows -----\n", fp, m);
5429542Seric 		if (e->e_xfp != NULL)
5439542Seric 			(void) fflush(e->e_xfp);
5449057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
54510170Seric 			putline(buf, fp, m);
546*58680Seric 		(void) xfclose(xfile, "errbody xscript", p);
5479057Seric 	}
548297Seric 	errno = 0;
5494318Seric 
5504318Seric 	/*
5514318Seric 	**  Output text of original message
5524318Seric 	*/
5534318Seric 
5544289Seric 	if (NoReturn)
55558665Seric 		SendBody = FALSE;
556*58680Seric 	if (e->e_parent->e_df != NULL)
5574199Seric 	{
5585984Seric 		if (SendBody)
5595984Seric 		{
56010170Seric 			putline("\n", fp, m);
56110170Seric 			putline("   ----- Unsent message follows -----\n", fp, m);
5625984Seric 			(void) fflush(fp);
56310170Seric 			putheader(fp, m, e->e_parent);
56410170Seric 			putline("\n", fp, m);
56510170Seric 			putbody(fp, m, e->e_parent);
5665984Seric 		}
5675984Seric 		else
5685984Seric 		{
56910170Seric 			putline("\n", fp, m);
57010170Seric 			putline("  ----- Message header follows -----\n", fp, m);
5715984Seric 			(void) fflush(fp);
57210170Seric 			putheader(fp, m, e->e_parent);
5735984Seric 		}
5744199Seric 	}
5754199Seric 	else
57610170Seric 	{
57710170Seric 		putline("\n", fp, m);
57810170Seric 		putline("  ----- No message was collected -----\n", fp, m);
57910170Seric 		putline("\n", fp, m);
58010170Seric 	}
5814318Seric 
5824318Seric 	/*
5834318Seric 	**  Cleanup and exit
5844318Seric 	*/
5854318Seric 
586297Seric 	if (errno != 0)
5876978Seric 		syserr("errbody: I/O error");
588297Seric }
58958144Seric /*
59058144Seric **  PRUNEROUTE -- prune an RFC-822 source route
59158144Seric **
59258144Seric **	Trims down a source route to the last internet-registered hop.
59358144Seric **	This is encouraged by RFC 1123 section 5.3.3.
59458144Seric **
59558144Seric **	Parameters:
59658144Seric **		addr -- the address
59758144Seric **
59858144Seric **	Returns:
59958144Seric **		TRUE -- address was modified
60058144Seric **		FALSE -- address could not be pruned
60158144Seric **
60258144Seric **	Side Effects:
60358144Seric **		modifies addr in-place
60458144Seric */
60558144Seric 
60658144Seric pruneroute(addr)
60758144Seric 	char *addr;
60858144Seric {
60958144Seric #ifdef NAMED_BIND
61058144Seric 	char *start, *at, *comma;
61158144Seric 	char c;
61258144Seric 	int rcode;
61358144Seric 	char hostbuf[BUFSIZ];
61458144Seric 	char *mxhosts[MAXMXHOSTS + 1];
61558144Seric 
61658144Seric 	/* check to see if this is really a route-addr */
61758144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
61858144Seric 		return FALSE;
61958144Seric 	start = strchr(addr, ':');
62058144Seric 	at = strrchr(addr, '@');
62158144Seric 	if (start == NULL || at == NULL || at < start)
62258144Seric 		return FALSE;
62358144Seric 
62458144Seric 	/* slice off the angle brackets */
62558144Seric 	strcpy(hostbuf, at + 1);
62658144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
62758144Seric 
62858144Seric 	while (start)
62958144Seric 	{
63058144Seric 		if (getmxrr(hostbuf, mxhosts, "", &rcode) > 0)
63158144Seric 		{
63258144Seric 			strcpy(addr + 1, start + 1);
63358144Seric 			return TRUE;
63458144Seric 		}
63558144Seric 		c = *start;
63658144Seric 		*start = '\0';
63758144Seric 		comma = strrchr(addr, ',');
63858144Seric 		if (comma && comma[1] == '@')
63958144Seric 			strcpy(hostbuf, comma + 2);
64058144Seric 		else
64158144Seric 			comma = 0;
64258144Seric 		*start = c;
64358144Seric 		start = comma;
64458144Seric 	}
64558144Seric #endif
64658144Seric 	return FALSE;
64758144Seric }
648