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*58865Seric static char sccsid[] = "@(#)savemail.c	6.27 (Berkeley) 03/30/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 			}
22358680Seric 			if (returntosender(e->e_message != NULL ? e->e_message :
22458680Seric 					   "Unable to deliver mail",
22558680Seric 					   q, (e->e_class >= 0), e) == 0)
22658680Seric 			{
22758680Seric 				state = ESM_DONE;
22858680Seric 				break;
22958680Seric 			}
23024981Seric 
23158680Seric 			/* didn't work -- return to postmaster */
23258680Seric 			state = ESM_POSTMASTER;
23358680Seric 			break;
23458306Seric 
23558680Seric 		  case ESM_POSTMASTER:
23658680Seric 			/*
23758680Seric 			**  Similar to previous case, but to system postmaster.
23858680Seric 			*/
23958680Seric 
24058680Seric 			if (parseaddr("postmaster", q, 0, '\0', NULL, e) == NULL)
24124942Seric 			{
24258680Seric 				syserr("553 cannot parse postmaster!");
24358680Seric 				ExitStat = EX_SOFTWARE;
24458680Seric 				state = ESM_USRTMP;
24558680Seric 				break;
24624942Seric 			}
24724942Seric 			if (returntosender(e->e_message != NULL ? e->e_message :
24824942Seric 					   "Unable to deliver mail",
24957438Seric 					   q, (e->e_class >= 0), e) == 0)
25024942Seric 			{
25124942Seric 				state = ESM_DONE;
25224942Seric 				break;
25324942Seric 			}
254297Seric 
25558680Seric 			/* didn't work -- last resort */
25658680Seric 			state = ESM_USRTMP;
25724942Seric 			break;
258297Seric 
25924942Seric 		  case ESM_DEADLETTER:
26024942Seric 			/*
26124942Seric 			**  Save the message in dead.letter.
26224942Seric 			**	If we weren't mailing back, and the user is
26324942Seric 			**	local, we should save the message in
26424942Seric 			**	~/dead.letter so that the poor person doesn't
26524942Seric 			**	have to type it over again -- and we all know
26624942Seric 			**	what poor typists UNIX users are.
26724942Seric 			*/
2685315Seric 
26924942Seric 			p = NULL;
27024942Seric 			if (e->e_from.q_mailer == LocalMailer)
27124942Seric 			{
27224942Seric 				if (e->e_from.q_home != NULL)
27324942Seric 					p = e->e_from.q_home;
27424942Seric 				else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
27524942Seric 					p = pw->pw_dir;
27624942Seric 			}
27724942Seric 			if (p == NULL)
27824942Seric 			{
279*58865Seric 				/* no local directory */
28024942Seric 				state = ESM_MAIL;
28124942Seric 				break;
28224942Seric 			}
28324942Seric 			if (e->e_dfp != NULL)
28424942Seric 			{
28524942Seric 				auto ADDRESS *q;
28624942Seric 				bool oldverb = Verbose;
28724942Seric 
28824942Seric 				/* we have a home directory; open dead.letter */
28924942Seric 				define('z', p, e);
29058050Seric 				expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e);
29124942Seric 				Verbose = TRUE;
29258151Seric 				message("Saving message in %s", buf);
29324942Seric 				Verbose = oldverb;
29424942Seric 				e->e_to = buf;
29524942Seric 				q = NULL;
29658082Seric 				(void) sendtolist(buf, &e->e_from, &q, e);
29724942Seric 				if (deliver(e, q) == 0)
29824942Seric 					state = ESM_DONE;
29924942Seric 				else
30024942Seric 					state = ESM_MAIL;
30124942Seric 			}
30225569Seric 			else
30325569Seric 			{
30425569Seric 				/* no data file -- try mailing back */
30525569Seric 				state = ESM_MAIL;
30625569Seric 			}
30724942Seric 			break;
30824942Seric 
30924942Seric 		  case ESM_USRTMP:
31024942Seric 			/*
31124942Seric 			**  Log the mail in /usr/tmp/dead.letter.
31224942Seric 			*/
31324942Seric 
31457438Seric 			if (e->e_class < 0)
31557438Seric 			{
31657438Seric 				state = ESM_DONE;
31757438Seric 				break;
31857438Seric 			}
31957438Seric 
32024942Seric 			fp = dfopen("/usr/tmp/dead.letter", "a");
32124942Seric 			if (fp == NULL)
32224942Seric 			{
32324942Seric 				state = ESM_PANIC;
32424942Seric 				break;
32524942Seric 			}
32624942Seric 
32758010Seric 			putfromline(fp, FileMailer, e);
32858010Seric 			(*e->e_puthdr)(fp, FileMailer, e);
32958010Seric 			putline("\n", fp, FileMailer);
33058010Seric 			(*e->e_putbody)(fp, FileMailer, e);
33158010Seric 			putline("\n", fp, FileMailer);
33224942Seric 			(void) fflush(fp);
33324942Seric 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
33458680Seric 			(void) xfclose(fp, "savemail", "/usr/tmp/dead.letter");
33524942Seric 			break;
33624942Seric 
33724942Seric 		  default:
33858151Seric 			syserr("554 savemail: unknown state %d", state);
33924942Seric 
34024942Seric 			/* fall through ... */
34124942Seric 
34224942Seric 		  case ESM_PANIC:
34324942Seric 			/* leave the locked queue & transcript files around */
34458151Seric 			syserr("554 savemail: cannot save rejected email anywhere");
34524942Seric 			exit(EX_SOFTWARE);
34624942Seric 		}
347297Seric 	}
348297Seric }
349297Seric /*
3504633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
3514633Seric **
3524633Seric **	Parameters:
3534633Seric **		msg -- the explanatory message.
35416479Seric **		returnq -- the queue of people to send the message to.
3555984Seric **		sendbody -- if TRUE, also send back the body of the
3565984Seric **			message; otherwise just send the header.
35755012Seric **		e -- the current envelope.
3584633Seric **
3594633Seric **	Returns:
3604633Seric **		zero -- if everything went ok.
3614633Seric **		else -- some error.
3624633Seric **
3634633Seric **	Side Effects:
3644633Seric **		Returns the current message to the sender via
3654633Seric **		mail.
3664633Seric */
3674633Seric 
3685984Seric static bool	SendBody;
3694633Seric 
3707045Seric #define MAXRETURNS	6	/* max depth of returning messages */
37158559Seric #define ERRORFUDGE	100	/* nominal size of error message text */
3727045Seric 
37355012Seric returntosender(msg, returnq, sendbody, e)
3744633Seric 	char *msg;
37516479Seric 	ADDRESS *returnq;
3765984Seric 	bool sendbody;
37755012Seric 	register ENVELOPE *e;
3784633Seric {
3794633Seric 	char buf[MAXNAME];
3806978Seric 	extern putheader(), errbody();
3816978Seric 	register ENVELOPE *ee;
38258680Seric 	ENVELOPE *oldcur = CurEnv;
3836978Seric 	extern ENVELOPE *newenvelope();
3846978Seric 	ENVELOPE errenvelope;
3857045Seric 	static int returndepth;
3869375Seric 	register ADDRESS *q;
3874633Seric 
3887676Seric 	if (tTd(6, 1))
3897287Seric 	{
39058680Seric 		printf("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
39155012Seric 		       msg, returndepth, e);
39216479Seric 		printaddr(returnq, TRUE);
3937287Seric 	}
3947287Seric 
3957045Seric 	if (++returndepth >= MAXRETURNS)
3967045Seric 	{
3977045Seric 		if (returndepth != MAXRETURNS)
39858151Seric 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
3997045Seric 		/* don't "unrecurse" and fake a clean exit */
4007045Seric 		/* returndepth--; */
4017045Seric 		return (0);
4027045Seric 	}
4037045Seric 
4045984Seric 	SendBody = sendbody;
40558680Seric 	define('g', e->e_from.q_paddr, e);
40658179Seric 	ee = newenvelope(&errenvelope, e);
40758050Seric 	define('a', "\201b", ee);
4086978Seric 	ee->e_puthdr = putheader;
4096978Seric 	ee->e_putbody = errbody;
4109375Seric 	ee->e_flags |= EF_RESPONSE;
41155012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
41245155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
41316479Seric 	ee->e_sendqueue = returnq;
41458559Seric 	ee->e_msgsize = e->e_msgsize + ERRORFUDGE;
4159542Seric 	openxscript(ee);
41616479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4179375Seric 	{
41858559Seric 		if (bitset(QDONTSEND, q->q_flags))
41958559Seric 			continue;
42058559Seric 
42158559Seric 		ee->e_nrcpts++;
42258559Seric 
42358144Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
42458333Seric 			parseaddr(q->q_paddr, q, 0, '\0', NULL, e);
42558144Seric 
4269375Seric 		if (q->q_alias == NULL)
4279375Seric 			addheader("to", q->q_paddr, ee);
4289375Seric 	}
42924942Seric 
43057642Seric # ifdef LOG
43158020Seric 	if (LogLevel > 5)
43257642Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
43357642Seric 			e->e_id, ee->e_id, msg);
43457642Seric # endif
43557642Seric 
43610845Seric 	(void) sprintf(buf, "Returned mail: %s", msg);
43710106Seric 	addheader("subject", buf, ee);
4384633Seric 
4394633Seric 	/* fake up an address header for the from person */
44058050Seric 	expand("\201n", buf, &buf[sizeof buf - 1], e);
44158680Seric 	if (parseaddr(buf, &ee->e_from, 1, '\0', NULL, e) == NULL)
4424633Seric 	{
44358151Seric 		syserr("553 Can't parse myself!");
4444633Seric 		ExitStat = EX_SOFTWARE;
4457045Seric 		returndepth--;
4464633Seric 		return (-1);
4474633Seric 	}
44858704Seric 	ee->e_sender = ee->e_from.q_paddr;
4495984Seric 
4506978Seric 	/* push state into submessage */
4516978Seric 	CurEnv = ee;
45258050Seric 	define('f', "\201n", ee);
4539375Seric 	define('x', "Mail Delivery Subsystem", ee);
45458737Seric 	eatheader(ee);
4555984Seric 
4566978Seric 	/* actually deliver the error message */
45714876Seric 	sendall(ee, SM_DEFAULT);
4586978Seric 
4596978Seric 	/* restore state */
4607811Seric 	dropenvelope(ee);
46158680Seric 	CurEnv = oldcur;
4627045Seric 	returndepth--;
4636978Seric 
4647045Seric 	/* should check for delivery errors here */
4654633Seric 	return (0);
4664633Seric }
4674633Seric /*
4686978Seric **  ERRBODY -- output the body of an error message.
4696978Seric **
4706978Seric **	Typically this is a copy of the transcript plus a copy of the
4716978Seric **	original offending message.
4726978Seric **
473297Seric **	Parameters:
474297Seric **		fp -- the output file.
47510170Seric **		m -- the mailer to output to.
4769542Seric **		e -- the envelope we are working in.
477297Seric **
478297Seric **	Returns:
479297Seric **		none
480297Seric **
481297Seric **	Side Effects:
4826978Seric **		Outputs the body of an error message.
483297Seric */
484297Seric 
48510170Seric errbody(fp, m, e)
486297Seric 	register FILE *fp;
4874318Seric 	register struct mailer *m;
4889542Seric 	register ENVELOPE *e;
489297Seric {
4906978Seric 	register FILE *xfile;
4913189Seric 	char buf[MAXLINE];
4929337Seric 	char *p;
493297Seric 
49458680Seric 	if (e->e_parent == NULL)
49558680Seric 	{
49658680Seric 		syserr("errbody: null parent");
49758680Seric 		putline("\n", fp, m);
49858680Seric 		putline("   ----- Original message lost -----\n", fp, m);
49958680Seric 		return;
50058680Seric 	}
50158680Seric 
5029057Seric 	/*
50355372Seric 	**  Output error message header (if specified and available).
50455372Seric 	*/
50555372Seric 
50655372Seric 	if (ErrMsgFile != NULL)
50755372Seric 	{
50855372Seric 		if (*ErrMsgFile == '/')
50955372Seric 		{
51055372Seric 			xfile = fopen(ErrMsgFile, "r");
51155372Seric 			if (xfile != NULL)
51255372Seric 			{
51355372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
51455425Seric 				{
51555425Seric 					expand(buf, buf, &buf[sizeof buf - 1], e);
51655372Seric 					putline(buf, fp, m);
51755425Seric 				}
51855372Seric 				(void) fclose(xfile);
51955372Seric 				fprintf(fp, "\n");
52055372Seric 			}
52155372Seric 		}
52255372Seric 		else
52355372Seric 		{
52455425Seric 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
52555425Seric 			putline(buf, fp, m);
52655372Seric 			fprintf(fp, "\n");
52755372Seric 		}
52855372Seric 	}
52955372Seric 
53055372Seric 	/*
5319057Seric 	**  Output transcript of errors
5329057Seric 	*/
5339057Seric 
5344086Seric 	(void) fflush(stdout);
5359542Seric 	p = queuename(e->e_parent, 'x');
5369337Seric 	if ((xfile = fopen(p, "r")) == NULL)
5379057Seric 	{
5389337Seric 		syserr("Cannot open %s", p);
53958680Seric 		putline("  ----- Transcript of session is unavailable -----\n", fp, m);
5409057Seric 	}
5419057Seric 	else
5429057Seric 	{
54358680Seric 		putline("   ----- Transcript of session follows -----\n", fp, m);
5449542Seric 		if (e->e_xfp != NULL)
5459542Seric 			(void) fflush(e->e_xfp);
5469057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
54710170Seric 			putline(buf, fp, m);
54858680Seric 		(void) xfclose(xfile, "errbody xscript", p);
5499057Seric 	}
550297Seric 	errno = 0;
5514318Seric 
5524318Seric 	/*
5534318Seric 	**  Output text of original message
5544318Seric 	*/
5554318Seric 
5564289Seric 	if (NoReturn)
55758665Seric 		SendBody = FALSE;
55858680Seric 	if (e->e_parent->e_df != NULL)
5594199Seric 	{
5605984Seric 		if (SendBody)
5615984Seric 		{
56210170Seric 			putline("\n", fp, m);
56310170Seric 			putline("   ----- Unsent message follows -----\n", fp, m);
5645984Seric 			(void) fflush(fp);
56510170Seric 			putheader(fp, m, e->e_parent);
56610170Seric 			putline("\n", fp, m);
56710170Seric 			putbody(fp, m, e->e_parent);
5685984Seric 		}
5695984Seric 		else
5705984Seric 		{
57110170Seric 			putline("\n", fp, m);
57210170Seric 			putline("  ----- Message header follows -----\n", fp, m);
5735984Seric 			(void) fflush(fp);
57410170Seric 			putheader(fp, m, e->e_parent);
5755984Seric 		}
5764199Seric 	}
5774199Seric 	else
57810170Seric 	{
57910170Seric 		putline("\n", fp, m);
58010170Seric 		putline("  ----- No message was collected -----\n", fp, m);
58110170Seric 		putline("\n", fp, m);
58210170Seric 	}
5834318Seric 
5844318Seric 	/*
5854318Seric 	**  Cleanup and exit
5864318Seric 	*/
5874318Seric 
588297Seric 	if (errno != 0)
5896978Seric 		syserr("errbody: I/O error");
590297Seric }
59158144Seric /*
59258144Seric **  PRUNEROUTE -- prune an RFC-822 source route
59358144Seric **
59458144Seric **	Trims down a source route to the last internet-registered hop.
59558144Seric **	This is encouraged by RFC 1123 section 5.3.3.
59658144Seric **
59758144Seric **	Parameters:
59858144Seric **		addr -- the address
59958144Seric **
60058144Seric **	Returns:
60158144Seric **		TRUE -- address was modified
60258144Seric **		FALSE -- address could not be pruned
60358144Seric **
60458144Seric **	Side Effects:
60558144Seric **		modifies addr in-place
60658144Seric */
60758144Seric 
60858144Seric pruneroute(addr)
60958144Seric 	char *addr;
61058144Seric {
61158144Seric #ifdef NAMED_BIND
61258144Seric 	char *start, *at, *comma;
61358144Seric 	char c;
61458144Seric 	int rcode;
61558144Seric 	char hostbuf[BUFSIZ];
61658144Seric 	char *mxhosts[MAXMXHOSTS + 1];
61758144Seric 
61858144Seric 	/* check to see if this is really a route-addr */
61958144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
62058144Seric 		return FALSE;
62158144Seric 	start = strchr(addr, ':');
62258144Seric 	at = strrchr(addr, '@');
62358144Seric 	if (start == NULL || at == NULL || at < start)
62458144Seric 		return FALSE;
62558144Seric 
62658144Seric 	/* slice off the angle brackets */
62758144Seric 	strcpy(hostbuf, at + 1);
62858144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
62958144Seric 
63058144Seric 	while (start)
63158144Seric 	{
63258144Seric 		if (getmxrr(hostbuf, mxhosts, "", &rcode) > 0)
63358144Seric 		{
63458144Seric 			strcpy(addr + 1, start + 1);
63558144Seric 			return TRUE;
63658144Seric 		}
63758144Seric 		c = *start;
63858144Seric 		*start = '\0';
63958144Seric 		comma = strrchr(addr, ',');
64058144Seric 		if (comma && comma[1] == '@')
64158144Seric 			strcpy(hostbuf, comma + 2);
64258144Seric 		else
64358144Seric 			comma = 0;
64458144Seric 		*start = c;
64558144Seric 		start = comma;
64658144Seric 	}
64758144Seric #endif
64858144Seric 	return FALSE;
64958144Seric }
650