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*58665Seric static char sccsid[] = "@(#)savemail.c	6.20 (Berkeley) 03/13/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))
6124979Seric 		printf("\nsavemail, ErrorMode = %c\n", ErrorMode);
627361Seric 
639375Seric 	if (bitset(EF_RESPONSE, e->e_flags))
64297Seric 		return;
659337Seric 	e->e_flags &= ~EF_FATALERRS;
66297Seric 
67297Seric 	/*
68297Seric 	**  In the unhappy event we don't know who to return the mail
69297Seric 	**  to, make someone up.
70297Seric 	*/
71297Seric 
729337Seric 	if (e->e_from.q_paddr == NULL)
73297Seric 	{
7458333Seric 		if (parseaddr("root", &e->e_from, 0, '\0', NULL, e) == NULL)
75297Seric 		{
7658151Seric 			syserr("553 Cannot parse root!");
77297Seric 			ExitStat = EX_SOFTWARE;
78297Seric 			finis();
79297Seric 		}
80297Seric 	}
819337Seric 	e->e_to = NULL;
82297Seric 
83297Seric 	/*
8424942Seric 	**  Basic state machine.
8524942Seric 	**
8624942Seric 	**	This machine runs through the following states:
8724942Seric 	**
8824942Seric 	**	ESM_QUIET	Errors have already been printed iff the
8924942Seric 	**			sender is local.
9024942Seric 	**	ESM_REPORT	Report directly to the sender's terminal.
9124942Seric 	**	ESM_MAIL	Mail response to the sender.
9224942Seric 	**	ESM_DEADLETTER	Save response in ~/dead.letter.
9324942Seric 	**	ESM_POSTMASTER	Mail response to the postmaster.
9424942Seric 	**	ESM_PANIC	Save response anywhere possible.
95297Seric 	*/
96297Seric 
9724942Seric 	/* determine starting state */
9824942Seric 	switch (ErrorMode)
99297Seric 	{
10024942Seric 	  case EM_WRITE:
10124942Seric 		state = ESM_REPORT;
10224942Seric 		break;
10324942Seric 
10424942Seric 	  case EM_BERKNET:
10524942Seric 		/* mail back, but return o.k. exit status */
106401Seric 		ExitStat = EX_OK;
10724942Seric 
10824942Seric 		/* fall through.... */
10924942Seric 
11024942Seric 	  case EM_MAIL:
11124942Seric 		state = ESM_MAIL;
11224942Seric 		break;
11324942Seric 
11424942Seric 	  case EM_PRINT:
11524979Seric 	  case '\0':
11624942Seric 		state = ESM_QUIET;
11724942Seric 		break;
11824942Seric 
11924942Seric 	  case EM_QUIET:
12024942Seric 		/* no need to return anything at all */
12124942Seric 		return;
12224979Seric 
12324979Seric 	  default:
12458151Seric 		syserr("554 savemail: ErrorMode x%x\n");
12524979Seric 		state = ESM_MAIL;
12624979Seric 		break;
127297Seric 	}
128297Seric 
12924942Seric 	while (state != ESM_DONE)
130297Seric 	{
13124979Seric 		if (tTd(6, 5))
13224979Seric 			printf("  state %d\n", state);
13324979Seric 
13424942Seric 		switch (state)
135297Seric 		{
13624979Seric 		  case ESM_QUIET:
13724979Seric 			if (e->e_from.q_mailer == LocalMailer)
13824979Seric 				state = ESM_DEADLETTER;
13924979Seric 			else
14024979Seric 				state = ESM_MAIL;
14124979Seric 			break;
14224979Seric 
14324942Seric 		  case ESM_REPORT:
14424942Seric 
14524942Seric 			/*
14624942Seric 			**  If the user is still logged in on the same terminal,
14724942Seric 			**  then write the error messages back to hir (sic).
14824942Seric 			*/
14924942Seric 
15024942Seric 			p = ttypath();
15124942Seric 			if (p == NULL || freopen(p, "w", stdout) == NULL)
15224942Seric 			{
15324942Seric 				state = ESM_MAIL;
15424942Seric 				break;
15524942Seric 			}
15624942Seric 
15758050Seric 			expand("\201n", buf, &buf[sizeof buf - 1], e);
1589375Seric 			printf("\r\nMessage from %s...\r\n", buf);
1599375Seric 			printf("Errors occurred while sending mail.\r\n");
1609542Seric 			if (e->e_xfp != NULL)
1619375Seric 			{
1629542Seric 				(void) fflush(e->e_xfp);
16324942Seric 				fp = fopen(queuename(e, 'x'), "r");
1649375Seric 			}
1659375Seric 			else
16624942Seric 				fp = NULL;
16724942Seric 			if (fp == NULL)
1689375Seric 			{
1699337Seric 				syserr("Cannot open %s", queuename(e, 'x'));
1709375Seric 				printf("Transcript of session is unavailable.\r\n");
1719375Seric 			}
1729375Seric 			else
1739375Seric 			{
1749375Seric 				printf("Transcript follows:\r\n");
17524942Seric 				while (fgets(buf, sizeof buf, fp) != NULL &&
1769375Seric 				       !ferror(stdout))
1779375Seric 					fputs(buf, stdout);
17824942Seric 				(void) fclose(fp);
1799375Seric 			}
18024942Seric 			printf("Original message will be saved in dead.letter.\r\n");
18124942Seric 			state = ESM_DEADLETTER;
18224942Seric 			break;
183297Seric 
18424942Seric 		  case ESM_MAIL:
18524942Seric 		  case ESM_POSTMASTER:
18624942Seric 			/*
18724942Seric 			**  If mailing back, do it.
18824942Seric 			**	Throw away all further output.  Don't alias,
18924942Seric 			**	since this could cause loops, e.g., if joe
19024942Seric 			**	mails to joe@x, and for some reason the network
19124942Seric 			**	for @x is down, then the response gets sent to
19224942Seric 			**	joe@x, which gives a response, etc.  Also force
19324942Seric 			**	the mail to be delivered even if a version of
19424942Seric 			**	it has already been sent to the sender.
19558295Seric 			**
19658295Seric 			**	Clever technique for computing rpath from
19758295Seric 			**	Eric Wassenaar <e07@nikhef.nl>.
19824942Seric 			*/
199297Seric 
20024942Seric 			if (state == ESM_MAIL)
20124942Seric 			{
20258304Seric 				char *rpath;
20358178Seric 
20458304Seric 				if (e->e_returnpath != e->e_sender)
20558304Seric 					rpath = e->e_returnpath;
20658304Seric 				else
20758304Seric 					rpath = e->e_from.q_paddr;
20858304Seric 				if (strcmp(rpath, "<>") != 0)
20958304Seric 					(void) sendtolist(rpath,
21058304Seric 						  (ADDRESS *) NULL,
21158304Seric 						  &e->e_errorqueue, e);
21224981Seric 
21324981Seric 				/* deliver a cc: to the postmaster if desired */
21424981Seric 				if (PostMasterCopy != NULL)
21558306Seric 				{
21658306Seric 					auto ADDRESS *rlist = NULL;
21758306Seric 
21858082Seric 					(void) sendtolist(PostMasterCopy,
21958082Seric 							  (ADDRESS *) NULL,
22058306Seric 							  &rlist, e);
22158306Seric 					(void) returntosender(e->e_message,
22258306Seric 							      rlist, FALSE, e);
22358306Seric 				}
22424942Seric 				q = e->e_errorqueue;
22558111Seric 				if (q == NULL)
22658111Seric 				{
22758111Seric 					/* this is an error-error */
22858111Seric 					state = ESM_USRTMP;
22958111Seric 					break;
23058111Seric 				}
23124942Seric 			}
23224942Seric 			else
23324942Seric 			{
23458333Seric 				if (parseaddr("postmaster", q, 0, '\0', NULL, e) == NULL)
23524942Seric 				{
23658151Seric 					syserr("553 cannot parse postmaster!");
23724942Seric 					ExitStat = EX_SOFTWARE;
23824942Seric 					state = ESM_USRTMP;
23924942Seric 					break;
24024942Seric 				}
24124942Seric 			}
24224942Seric 			if (returntosender(e->e_message != NULL ? e->e_message :
24324942Seric 					   "Unable to deliver mail",
24457438Seric 					   q, (e->e_class >= 0), e) == 0)
24524942Seric 			{
24624942Seric 				state = ESM_DONE;
24724942Seric 				break;
24824942Seric 			}
249297Seric 
25024942Seric 			state = state == ESM_MAIL ? ESM_POSTMASTER : ESM_USRTMP;
25124942Seric 			break;
252297Seric 
25324942Seric 		  case ESM_DEADLETTER:
25424942Seric 			/*
25524942Seric 			**  Save the message in dead.letter.
25624942Seric 			**	If we weren't mailing back, and the user is
25724942Seric 			**	local, we should save the message in
25824942Seric 			**	~/dead.letter so that the poor person doesn't
25924942Seric 			**	have to type it over again -- and we all know
26024942Seric 			**	what poor typists UNIX users are.
26124942Seric 			*/
2625315Seric 
26324942Seric 			p = NULL;
26424942Seric 			if (e->e_from.q_mailer == LocalMailer)
26524942Seric 			{
26624942Seric 				if (e->e_from.q_home != NULL)
26724942Seric 					p = e->e_from.q_home;
26824942Seric 				else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
26924942Seric 					p = pw->pw_dir;
27024942Seric 			}
27124942Seric 			if (p == NULL)
27224942Seric 			{
27358151Seric 				syserr("554 Can't return mail to %s", e->e_from.q_paddr);
27424942Seric 				state = ESM_MAIL;
27524942Seric 				break;
27624942Seric 			}
27724942Seric 			if (e->e_dfp != NULL)
27824942Seric 			{
27924942Seric 				auto ADDRESS *q;
28024942Seric 				bool oldverb = Verbose;
28124942Seric 
28224942Seric 				/* we have a home directory; open dead.letter */
28324942Seric 				define('z', p, e);
28458050Seric 				expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e);
28524942Seric 				Verbose = TRUE;
28658151Seric 				message("Saving message in %s", buf);
28724942Seric 				Verbose = oldverb;
28824942Seric 				e->e_to = buf;
28924942Seric 				q = NULL;
29058082Seric 				(void) sendtolist(buf, &e->e_from, &q, e);
29124942Seric 				if (deliver(e, q) == 0)
29224942Seric 					state = ESM_DONE;
29324942Seric 				else
29424942Seric 					state = ESM_MAIL;
29524942Seric 			}
29625569Seric 			else
29725569Seric 			{
29825569Seric 				/* no data file -- try mailing back */
29925569Seric 				state = ESM_MAIL;
30025569Seric 			}
30124942Seric 			break;
30224942Seric 
30324942Seric 		  case ESM_USRTMP:
30424942Seric 			/*
30524942Seric 			**  Log the mail in /usr/tmp/dead.letter.
30624942Seric 			*/
30724942Seric 
30857438Seric 			if (e->e_class < 0)
30957438Seric 			{
31057438Seric 				state = ESM_DONE;
31157438Seric 				break;
31257438Seric 			}
31357438Seric 
31424942Seric 			fp = dfopen("/usr/tmp/dead.letter", "a");
31524942Seric 			if (fp == NULL)
31624942Seric 			{
31724942Seric 				state = ESM_PANIC;
31824942Seric 				break;
31924942Seric 			}
32024942Seric 
32158010Seric 			putfromline(fp, FileMailer, e);
32258010Seric 			(*e->e_puthdr)(fp, FileMailer, e);
32358010Seric 			putline("\n", fp, FileMailer);
32458010Seric 			(*e->e_putbody)(fp, FileMailer, e);
32558010Seric 			putline("\n", fp, FileMailer);
32624942Seric 			(void) fflush(fp);
32724942Seric 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
32824942Seric 			(void) fclose(fp);
32924942Seric 			break;
33024942Seric 
33124942Seric 		  default:
33258151Seric 			syserr("554 savemail: unknown state %d", state);
33324942Seric 
33424942Seric 			/* fall through ... */
33524942Seric 
33624942Seric 		  case ESM_PANIC:
33724942Seric 			/* leave the locked queue & transcript files around */
33858151Seric 			syserr("554 savemail: cannot save rejected email anywhere");
33924942Seric 			exit(EX_SOFTWARE);
34024942Seric 		}
341297Seric 	}
342297Seric }
343297Seric /*
3444633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
3454633Seric **
3464633Seric **	Parameters:
3474633Seric **		msg -- the explanatory message.
34816479Seric **		returnq -- the queue of people to send the message to.
3495984Seric **		sendbody -- if TRUE, also send back the body of the
3505984Seric **			message; otherwise just send the header.
35155012Seric **		e -- the current envelope.
3524633Seric **
3534633Seric **	Returns:
3544633Seric **		zero -- if everything went ok.
3554633Seric **		else -- some error.
3564633Seric **
3574633Seric **	Side Effects:
3584633Seric **		Returns the current message to the sender via
3594633Seric **		mail.
3604633Seric */
3614633Seric 
3625984Seric static bool	SendBody;
3634633Seric 
3647045Seric #define MAXRETURNS	6	/* max depth of returning messages */
36558559Seric #define ERRORFUDGE	100	/* nominal size of error message text */
3667045Seric 
36755012Seric returntosender(msg, returnq, sendbody, e)
3684633Seric 	char *msg;
36916479Seric 	ADDRESS *returnq;
3705984Seric 	bool sendbody;
37155012Seric 	register ENVELOPE *e;
3724633Seric {
3734633Seric 	char buf[MAXNAME];
3746978Seric 	extern putheader(), errbody();
3756978Seric 	register ENVELOPE *ee;
3766978Seric 	extern ENVELOPE *newenvelope();
3776978Seric 	ENVELOPE errenvelope;
3787045Seric 	static int returndepth;
3799375Seric 	register ADDRESS *q;
3804633Seric 
3817676Seric 	if (tTd(6, 1))
3827287Seric 	{
38355012Seric 		printf("Return To Sender: msg=\"%s\", depth=%d, e=%x,\n",
38455012Seric 		       msg, returndepth, e);
38524942Seric 		printf("\treturnq=");
38616479Seric 		printaddr(returnq, TRUE);
3877287Seric 	}
3887287Seric 
3897045Seric 	if (++returndepth >= MAXRETURNS)
3907045Seric 	{
3917045Seric 		if (returndepth != MAXRETURNS)
39258151Seric 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
3937045Seric 		/* don't "unrecurse" and fake a clean exit */
3947045Seric 		/* returndepth--; */
3957045Seric 		return (0);
3967045Seric 	}
3977045Seric 
3985984Seric 	SendBody = sendbody;
39958179Seric 	define('g', e->e_sender, e);
40058179Seric 	define('<', e->e_returnpath, e);
40158179Seric 	ee = newenvelope(&errenvelope, e);
40258050Seric 	define('a', "\201b", ee);
4036978Seric 	ee->e_puthdr = putheader;
4046978Seric 	ee->e_putbody = errbody;
4059375Seric 	ee->e_flags |= EF_RESPONSE;
40655012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
40745155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
40816479Seric 	ee->e_sendqueue = returnq;
40958559Seric 	ee->e_msgsize = e->e_msgsize + ERRORFUDGE;
4109542Seric 	openxscript(ee);
41116479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4129375Seric 	{
41358559Seric 		if (bitset(QDONTSEND, q->q_flags))
41458559Seric 			continue;
41558559Seric 
41658559Seric 		ee->e_nrcpts++;
41758559Seric 
41858144Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
41958333Seric 			parseaddr(q->q_paddr, q, 0, '\0', NULL, e);
42058144Seric 
4219375Seric 		if (q->q_alias == NULL)
4229375Seric 			addheader("to", q->q_paddr, ee);
4239375Seric 	}
42424942Seric 
42557642Seric # ifdef LOG
42658020Seric 	if (LogLevel > 5)
42757642Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
42857642Seric 			e->e_id, ee->e_id, msg);
42957642Seric # endif
43057642Seric 
43110845Seric 	(void) sprintf(buf, "Returned mail: %s", msg);
43210106Seric 	addheader("subject", buf, ee);
4334633Seric 
4344633Seric 	/* fake up an address header for the from person */
43558050Seric 	expand("\201n", buf, &buf[sizeof buf - 1], e);
43658111Seric 	ee->e_sender = newstr(buf);
43758121Seric 	if (ConfigLevel >= 4)
43858121Seric 		ee->e_returnpath = "<>";
43958121Seric 	else
44058121Seric 		ee->e_returnpath = ee->e_sender;
44158333Seric 	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 	}
44816159Seric 	loweraddr(&ee->e_from);
4495984Seric 
4506978Seric 	/* push state into submessage */
4516978Seric 	CurEnv = ee;
45258050Seric 	define('f', "\201n", ee);
4539375Seric 	define('x', "Mail Delivery Subsystem", ee);
45457642Seric 	eatheader(ee, FALSE);
4555984Seric 
4566978Seric 	/* actually deliver the error message */
45714876Seric 	sendall(ee, SM_DEFAULT);
4586978Seric 
4596978Seric 	/* restore state */
4607811Seric 	dropenvelope(ee);
4616978Seric 	CurEnv = CurEnv->e_parent;
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 
4949057Seric 	/*
49555372Seric 	**  Output error message header (if specified and available).
49655372Seric 	*/
49755372Seric 
49855372Seric 	if (ErrMsgFile != NULL)
49955372Seric 	{
50055372Seric 		if (*ErrMsgFile == '/')
50155372Seric 		{
50255372Seric 			xfile = fopen(ErrMsgFile, "r");
50355372Seric 			if (xfile != NULL)
50455372Seric 			{
50555372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
50655425Seric 				{
50755425Seric 					expand(buf, buf, &buf[sizeof buf - 1], e);
50855372Seric 					putline(buf, fp, m);
50955425Seric 				}
51055372Seric 				(void) fclose(xfile);
51155372Seric 				fprintf(fp, "\n");
51255372Seric 			}
51355372Seric 		}
51455372Seric 		else
51555372Seric 		{
51655425Seric 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
51755425Seric 			putline(buf, fp, m);
51855372Seric 			fprintf(fp, "\n");
51955372Seric 		}
52055372Seric 	}
52155372Seric 
52255372Seric 	/*
5239057Seric 	**  Output transcript of errors
5249057Seric 	*/
5259057Seric 
5264086Seric 	(void) fflush(stdout);
5279542Seric 	p = queuename(e->e_parent, 'x');
5289337Seric 	if ((xfile = fopen(p, "r")) == NULL)
5299057Seric 	{
5309337Seric 		syserr("Cannot open %s", p);
5319057Seric 		fprintf(fp, "  ----- Transcript of session is unavailable -----\n");
5329057Seric 	}
5339057Seric 	else
5349057Seric 	{
5359057Seric 		fprintf(fp, "   ----- Transcript of session follows -----\n");
5369542Seric 		if (e->e_xfp != NULL)
5379542Seric 			(void) fflush(e->e_xfp);
5389057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
53910170Seric 			putline(buf, fp, m);
5409057Seric 		(void) fclose(xfile);
5419057Seric 	}
542297Seric 	errno = 0;
5434318Seric 
5444318Seric 	/*
5454318Seric 	**  Output text of original message
5464318Seric 	*/
5474318Seric 
5484289Seric 	if (NoReturn)
549*58665Seric 		SendBody = FALSE;
550*58665Seric 	if (e->e_parent->e_dfp != NULL)
5514199Seric 	{
5525984Seric 		if (SendBody)
5535984Seric 		{
55410170Seric 			putline("\n", fp, m);
55510170Seric 			putline("   ----- Unsent message follows -----\n", fp, m);
5565984Seric 			(void) fflush(fp);
55710170Seric 			putheader(fp, m, e->e_parent);
55810170Seric 			putline("\n", fp, m);
55910170Seric 			putbody(fp, m, e->e_parent);
5605984Seric 		}
5615984Seric 		else
5625984Seric 		{
56310170Seric 			putline("\n", fp, m);
56410170Seric 			putline("  ----- Message header follows -----\n", fp, m);
5655984Seric 			(void) fflush(fp);
56610170Seric 			putheader(fp, m, e->e_parent);
5675984Seric 		}
5684199Seric 	}
5694199Seric 	else
57010170Seric 	{
57110170Seric 		putline("\n", fp, m);
57210170Seric 		putline("  ----- No message was collected -----\n", fp, m);
57310170Seric 		putline("\n", fp, m);
57410170Seric 	}
5754318Seric 
5764318Seric 	/*
5774318Seric 	**  Cleanup and exit
5784318Seric 	*/
5794318Seric 
580297Seric 	if (errno != 0)
5816978Seric 		syserr("errbody: I/O error");
582297Seric }
58358144Seric /*
58458144Seric **  PRUNEROUTE -- prune an RFC-822 source route
58558144Seric **
58658144Seric **	Trims down a source route to the last internet-registered hop.
58758144Seric **	This is encouraged by RFC 1123 section 5.3.3.
58858144Seric **
58958144Seric **	Parameters:
59058144Seric **		addr -- the address
59158144Seric **
59258144Seric **	Returns:
59358144Seric **		TRUE -- address was modified
59458144Seric **		FALSE -- address could not be pruned
59558144Seric **
59658144Seric **	Side Effects:
59758144Seric **		modifies addr in-place
59858144Seric */
59958144Seric 
60058144Seric pruneroute(addr)
60158144Seric 	char *addr;
60258144Seric {
60358144Seric #ifdef NAMED_BIND
60458144Seric 	char *start, *at, *comma;
60558144Seric 	char c;
60658144Seric 	int rcode;
60758144Seric 	char hostbuf[BUFSIZ];
60858144Seric 	char *mxhosts[MAXMXHOSTS + 1];
60958144Seric 
61058144Seric 	/* check to see if this is really a route-addr */
61158144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
61258144Seric 		return FALSE;
61358144Seric 	start = strchr(addr, ':');
61458144Seric 	at = strrchr(addr, '@');
61558144Seric 	if (start == NULL || at == NULL || at < start)
61658144Seric 		return FALSE;
61758144Seric 
61858144Seric 	/* slice off the angle brackets */
61958144Seric 	strcpy(hostbuf, at + 1);
62058144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
62158144Seric 
62258144Seric 	while (start)
62358144Seric 	{
62458144Seric 		if (getmxrr(hostbuf, mxhosts, "", &rcode) > 0)
62558144Seric 		{
62658144Seric 			strcpy(addr + 1, start + 1);
62758144Seric 			return TRUE;
62858144Seric 		}
62958144Seric 		c = *start;
63058144Seric 		*start = '\0';
63158144Seric 		comma = strrchr(addr, ',');
63258144Seric 		if (comma && comma[1] == '@')
63358144Seric 			strcpy(hostbuf, comma + 2);
63458144Seric 		else
63558144Seric 			comma = 0;
63658144Seric 		*start = c;
63758144Seric 		start = comma;
63858144Seric 	}
63958144Seric #endif
64058144Seric 	return FALSE;
64158144Seric }
642