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*58306Seric static char sccsid[] = "@(#)savemail.c	6.16 (Berkeley) 02/28/93";
1133731Sbostic #endif /* not lint */
1222711Sdist 
1336928Sbostic # include <sys/types.h>
14297Seric # include <pwd.h>
153313Seric # include "sendmail.h"
16297Seric 
17297Seric /*
18297Seric **  SAVEMAIL -- Save mail on error
19297Seric **
209375Seric **	If mailing back errors, mail it back to the originator
21297Seric **	together with an error message; otherwise, just put it in
22297Seric **	dead.letter in the user's home directory (if he exists on
23297Seric **	this machine).
24297Seric **
25297Seric **	Parameters:
269337Seric **		e -- the envelope containing the message in error.
27297Seric **
28297Seric **	Returns:
29297Seric **		none
30297Seric **
31297Seric **	Side Effects:
32297Seric **		Saves the letter, by writing or mailing it back to the
33297Seric **		sender, or by putting it in dead.letter in her home
34297Seric **		directory.
35297Seric */
36297Seric 
3724942Seric /* defines for state machine */
3824942Seric # define ESM_REPORT	0	/* report to sender's terminal */
3924942Seric # define ESM_MAIL	1	/* mail back to sender */
4024942Seric # define ESM_QUIET	2	/* messages have already been returned */
4124942Seric # define ESM_DEADLETTER	3	/* save in ~/dead.letter */
4224942Seric # define ESM_POSTMASTER	4	/* return to postmaster */
4324942Seric # define ESM_USRTMP	5	/* save in /usr/tmp/dead.letter */
4424942Seric # define ESM_PANIC	6	/* leave the locked queue/transcript files */
4524942Seric # define ESM_DONE	7	/* the message is successfully delivered */
4624942Seric 
4724942Seric 
489337Seric savemail(e)
499337Seric 	register ENVELOPE *e;
50297Seric {
51297Seric 	register struct passwd *pw;
5224942Seric 	register FILE *fp;
5324942Seric 	int state;
5424942Seric 	auto ADDRESS *q;
55297Seric 	char buf[MAXLINE+1];
56297Seric 	extern struct passwd *getpwnam();
57297Seric 	register char *p;
58297Seric 	extern char *ttypath();
595846Seric 	typedef int (*fnptr)();
60297Seric 
617676Seric 	if (tTd(6, 1))
6224979Seric 		printf("\nsavemail, ErrorMode = %c\n", ErrorMode);
637361Seric 
649375Seric 	if (bitset(EF_RESPONSE, e->e_flags))
65297Seric 		return;
669337Seric 	e->e_flags &= ~EF_FATALERRS;
67297Seric 
68297Seric 	/*
69297Seric 	**  In the unhappy event we don't know who to return the mail
70297Seric 	**  to, make someone up.
71297Seric 	*/
72297Seric 
739337Seric 	if (e->e_from.q_paddr == NULL)
74297Seric 	{
7555012Seric 		if (parseaddr("root", &e->e_from, 0, '\0', e) == NULL)
76297Seric 		{
7758151Seric 			syserr("553 Cannot parse root!");
78297Seric 			ExitStat = EX_SOFTWARE;
79297Seric 			finis();
80297Seric 		}
81297Seric 	}
829337Seric 	e->e_to = NULL;
83297Seric 
84297Seric 	/*
8524942Seric 	**  Basic state machine.
8624942Seric 	**
8724942Seric 	**	This machine runs through the following states:
8824942Seric 	**
8924942Seric 	**	ESM_QUIET	Errors have already been printed iff the
9024942Seric 	**			sender is local.
9124942Seric 	**	ESM_REPORT	Report directly to the sender's terminal.
9224942Seric 	**	ESM_MAIL	Mail response to the sender.
9324942Seric 	**	ESM_DEADLETTER	Save response in ~/dead.letter.
9424942Seric 	**	ESM_POSTMASTER	Mail response to the postmaster.
9524942Seric 	**	ESM_PANIC	Save response anywhere possible.
96297Seric 	*/
97297Seric 
9824942Seric 	/* determine starting state */
9924942Seric 	switch (ErrorMode)
100297Seric 	{
10124942Seric 	  case EM_WRITE:
10224942Seric 		state = ESM_REPORT;
10324942Seric 		break;
10424942Seric 
10524942Seric 	  case EM_BERKNET:
10624942Seric 		/* mail back, but return o.k. exit status */
107401Seric 		ExitStat = EX_OK;
10824942Seric 
10924942Seric 		/* fall through.... */
11024942Seric 
11124942Seric 	  case EM_MAIL:
11224942Seric 		state = ESM_MAIL;
11324942Seric 		break;
11424942Seric 
11524942Seric 	  case EM_PRINT:
11624979Seric 	  case '\0':
11724942Seric 		state = ESM_QUIET;
11824942Seric 		break;
11924942Seric 
12024942Seric 	  case EM_QUIET:
12124942Seric 		/* no need to return anything at all */
12224942Seric 		return;
12324979Seric 
12424979Seric 	  default:
12558151Seric 		syserr("554 savemail: ErrorMode x%x\n");
12624979Seric 		state = ESM_MAIL;
12724979Seric 		break;
128297Seric 	}
129297Seric 
13024942Seric 	while (state != ESM_DONE)
131297Seric 	{
13224979Seric 		if (tTd(6, 5))
13324979Seric 			printf("  state %d\n", state);
13424979Seric 
13524942Seric 		switch (state)
136297Seric 		{
13724979Seric 		  case ESM_QUIET:
13824979Seric 			if (e->e_from.q_mailer == LocalMailer)
13924979Seric 				state = ESM_DEADLETTER;
14024979Seric 			else
14124979Seric 				state = ESM_MAIL;
14224979Seric 			break;
14324979Seric 
14424942Seric 		  case ESM_REPORT:
14524942Seric 
14624942Seric 			/*
14724942Seric 			**  If the user is still logged in on the same terminal,
14824942Seric 			**  then write the error messages back to hir (sic).
14924942Seric 			*/
15024942Seric 
15124942Seric 			p = ttypath();
15224942Seric 			if (p == NULL || freopen(p, "w", stdout) == NULL)
15324942Seric 			{
15424942Seric 				state = ESM_MAIL;
15524942Seric 				break;
15624942Seric 			}
15724942Seric 
15858050Seric 			expand("\201n", buf, &buf[sizeof buf - 1], e);
1599375Seric 			printf("\r\nMessage from %s...\r\n", buf);
1609375Seric 			printf("Errors occurred while sending mail.\r\n");
1619542Seric 			if (e->e_xfp != NULL)
1629375Seric 			{
1639542Seric 				(void) fflush(e->e_xfp);
16424942Seric 				fp = fopen(queuename(e, 'x'), "r");
1659375Seric 			}
1669375Seric 			else
16724942Seric 				fp = NULL;
16824942Seric 			if (fp == NULL)
1699375Seric 			{
1709337Seric 				syserr("Cannot open %s", queuename(e, 'x'));
1719375Seric 				printf("Transcript of session is unavailable.\r\n");
1729375Seric 			}
1739375Seric 			else
1749375Seric 			{
1759375Seric 				printf("Transcript follows:\r\n");
17624942Seric 				while (fgets(buf, sizeof buf, fp) != NULL &&
1779375Seric 				       !ferror(stdout))
1789375Seric 					fputs(buf, stdout);
17924942Seric 				(void) fclose(fp);
1809375Seric 			}
18124942Seric 			printf("Original message will be saved in dead.letter.\r\n");
18224942Seric 			state = ESM_DEADLETTER;
18324942Seric 			break;
184297Seric 
18524942Seric 		  case ESM_MAIL:
18624942Seric 		  case ESM_POSTMASTER:
18724942Seric 			/*
18824942Seric 			**  If mailing back, do it.
18924942Seric 			**	Throw away all further output.  Don't alias,
19024942Seric 			**	since this could cause loops, e.g., if joe
19124942Seric 			**	mails to joe@x, and for some reason the network
19224942Seric 			**	for @x is down, then the response gets sent to
19324942Seric 			**	joe@x, which gives a response, etc.  Also force
19424942Seric 			**	the mail to be delivered even if a version of
19524942Seric 			**	it has already been sent to the sender.
19658295Seric 			**
19758295Seric 			**	Clever technique for computing rpath from
19858295Seric 			**	Eric Wassenaar <e07@nikhef.nl>.
19924942Seric 			*/
200297Seric 
20124942Seric 			if (state == ESM_MAIL)
20224942Seric 			{
20358304Seric 				char *rpath;
20458178Seric 
20558304Seric 				if (e->e_returnpath != e->e_sender)
20658304Seric 					rpath = e->e_returnpath;
20758304Seric 				else
20858304Seric 					rpath = e->e_from.q_paddr;
20958304Seric 				if (strcmp(rpath, "<>") != 0)
21058304Seric 					(void) sendtolist(rpath,
21158304Seric 						  (ADDRESS *) NULL,
21258304Seric 						  &e->e_errorqueue, e);
21324981Seric 
21424981Seric 				/* deliver a cc: to the postmaster if desired */
21524981Seric 				if (PostMasterCopy != NULL)
216*58306Seric 				{
217*58306Seric 					auto ADDRESS *rlist = NULL;
218*58306Seric 
21958082Seric 					(void) sendtolist(PostMasterCopy,
22058082Seric 							  (ADDRESS *) NULL,
221*58306Seric 							  &rlist, e);
222*58306Seric 					(void) returntosender(e->e_message,
223*58306Seric 							      rlist, FALSE, e);
224*58306Seric 				}
22524942Seric 				q = e->e_errorqueue;
22658111Seric 				if (q == NULL)
22758111Seric 				{
22858111Seric 					/* this is an error-error */
22958111Seric 					state = ESM_USRTMP;
23058111Seric 					break;
23158111Seric 				}
23224942Seric 			}
23324942Seric 			else
23424942Seric 			{
23555012Seric 				if (parseaddr("postmaster", q, 0, '\0', e) == NULL)
23624942Seric 				{
23758151Seric 					syserr("553 cannot parse postmaster!");
23824942Seric 					ExitStat = EX_SOFTWARE;
23924942Seric 					state = ESM_USRTMP;
24024942Seric 					break;
24124942Seric 				}
24224942Seric 			}
24324942Seric 			if (returntosender(e->e_message != NULL ? e->e_message :
24424942Seric 					   "Unable to deliver mail",
24557438Seric 					   q, (e->e_class >= 0), e) == 0)
24624942Seric 			{
24724942Seric 				state = ESM_DONE;
24824942Seric 				break;
24924942Seric 			}
250297Seric 
25124942Seric 			state = state == ESM_MAIL ? ESM_POSTMASTER : ESM_USRTMP;
25224942Seric 			break;
253297Seric 
25424942Seric 		  case ESM_DEADLETTER:
25524942Seric 			/*
25624942Seric 			**  Save the message in dead.letter.
25724942Seric 			**	If we weren't mailing back, and the user is
25824942Seric 			**	local, we should save the message in
25924942Seric 			**	~/dead.letter so that the poor person doesn't
26024942Seric 			**	have to type it over again -- and we all know
26124942Seric 			**	what poor typists UNIX users are.
26224942Seric 			*/
2635315Seric 
26424942Seric 			p = NULL;
26524942Seric 			if (e->e_from.q_mailer == LocalMailer)
26624942Seric 			{
26724942Seric 				if (e->e_from.q_home != NULL)
26824942Seric 					p = e->e_from.q_home;
26924942Seric 				else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
27024942Seric 					p = pw->pw_dir;
27124942Seric 			}
27224942Seric 			if (p == NULL)
27324942Seric 			{
27458151Seric 				syserr("554 Can't return mail to %s", e->e_from.q_paddr);
27524942Seric 				state = ESM_MAIL;
27624942Seric 				break;
27724942Seric 			}
27824942Seric 			if (e->e_dfp != NULL)
27924942Seric 			{
28024942Seric 				auto ADDRESS *q;
28124942Seric 				bool oldverb = Verbose;
28224942Seric 
28324942Seric 				/* we have a home directory; open dead.letter */
28424942Seric 				define('z', p, e);
28558050Seric 				expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e);
28624942Seric 				Verbose = TRUE;
28758151Seric 				message("Saving message in %s", buf);
28824942Seric 				Verbose = oldverb;
28924942Seric 				e->e_to = buf;
29024942Seric 				q = NULL;
29158082Seric 				(void) sendtolist(buf, &e->e_from, &q, e);
29224942Seric 				if (deliver(e, q) == 0)
29324942Seric 					state = ESM_DONE;
29424942Seric 				else
29524942Seric 					state = ESM_MAIL;
29624942Seric 			}
29725569Seric 			else
29825569Seric 			{
29925569Seric 				/* no data file -- try mailing back */
30025569Seric 				state = ESM_MAIL;
30125569Seric 			}
30224942Seric 			break;
30324942Seric 
30424942Seric 		  case ESM_USRTMP:
30524942Seric 			/*
30624942Seric 			**  Log the mail in /usr/tmp/dead.letter.
30724942Seric 			*/
30824942Seric 
30957438Seric 			if (e->e_class < 0)
31057438Seric 			{
31157438Seric 				state = ESM_DONE;
31257438Seric 				break;
31357438Seric 			}
31457438Seric 
31524942Seric 			fp = dfopen("/usr/tmp/dead.letter", "a");
31624942Seric 			if (fp == NULL)
31724942Seric 			{
31824942Seric 				state = ESM_PANIC;
31924942Seric 				break;
32024942Seric 			}
32124942Seric 
32258010Seric 			putfromline(fp, FileMailer, e);
32358010Seric 			(*e->e_puthdr)(fp, FileMailer, e);
32458010Seric 			putline("\n", fp, FileMailer);
32558010Seric 			(*e->e_putbody)(fp, FileMailer, e);
32658010Seric 			putline("\n", fp, FileMailer);
32724942Seric 			(void) fflush(fp);
32824942Seric 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
32924942Seric 			(void) fclose(fp);
33024942Seric 			break;
33124942Seric 
33224942Seric 		  default:
33358151Seric 			syserr("554 savemail: unknown state %d", state);
33424942Seric 
33524942Seric 			/* fall through ... */
33624942Seric 
33724942Seric 		  case ESM_PANIC:
33824942Seric 			/* leave the locked queue & transcript files around */
33958151Seric 			syserr("554 savemail: cannot save rejected email anywhere");
34024942Seric 			exit(EX_SOFTWARE);
34124942Seric 		}
342297Seric 	}
343297Seric }
344297Seric /*
3454633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
3464633Seric **
3474633Seric **	Parameters:
3484633Seric **		msg -- the explanatory message.
34916479Seric **		returnq -- the queue of people to send the message to.
3505984Seric **		sendbody -- if TRUE, also send back the body of the
3515984Seric **			message; otherwise just send the header.
35255012Seric **		e -- the current envelope.
3534633Seric **
3544633Seric **	Returns:
3554633Seric **		zero -- if everything went ok.
3564633Seric **		else -- some error.
3574633Seric **
3584633Seric **	Side Effects:
3594633Seric **		Returns the current message to the sender via
3604633Seric **		mail.
3614633Seric */
3624633Seric 
3635984Seric static bool	SendBody;
3644633Seric 
3657045Seric #define MAXRETURNS	6	/* max depth of returning messages */
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;
4099542Seric 	openxscript(ee);
41016479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4119375Seric 	{
41258144Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
41358144Seric 			parseaddr(q->q_paddr, q, 0, '\0', e);
41458144Seric 
4159375Seric 		if (q->q_alias == NULL)
4169375Seric 			addheader("to", q->q_paddr, ee);
4179375Seric 	}
41824942Seric 
41957642Seric # ifdef LOG
42058020Seric 	if (LogLevel > 5)
42157642Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
42257642Seric 			e->e_id, ee->e_id, msg);
42357642Seric # endif
42457642Seric 
42510845Seric 	(void) sprintf(buf, "Returned mail: %s", msg);
42610106Seric 	addheader("subject", buf, ee);
4274633Seric 
4284633Seric 	/* fake up an address header for the from person */
42958050Seric 	expand("\201n", buf, &buf[sizeof buf - 1], e);
43058111Seric 	ee->e_sender = newstr(buf);
43158121Seric 	if (ConfigLevel >= 4)
43258121Seric 		ee->e_returnpath = "<>";
43358121Seric 	else
43458121Seric 		ee->e_returnpath = ee->e_sender;
43555012Seric 	if (parseaddr(buf, &ee->e_from, -1, '\0', e) == NULL)
4364633Seric 	{
43758151Seric 		syserr("553 Can't parse myself!");
4384633Seric 		ExitStat = EX_SOFTWARE;
4397045Seric 		returndepth--;
4404633Seric 		return (-1);
4414633Seric 	}
44216159Seric 	loweraddr(&ee->e_from);
4435984Seric 
4446978Seric 	/* push state into submessage */
4456978Seric 	CurEnv = ee;
44658050Seric 	define('f', "\201n", ee);
4479375Seric 	define('x', "Mail Delivery Subsystem", ee);
44857642Seric 	eatheader(ee, FALSE);
4495984Seric 
4506978Seric 	/* actually deliver the error message */
45114876Seric 	sendall(ee, SM_DEFAULT);
4526978Seric 
4536978Seric 	/* restore state */
4547811Seric 	dropenvelope(ee);
4556978Seric 	CurEnv = CurEnv->e_parent;
4567045Seric 	returndepth--;
4576978Seric 
4587045Seric 	/* should check for delivery errors here */
4594633Seric 	return (0);
4604633Seric }
4614633Seric /*
4626978Seric **  ERRBODY -- output the body of an error message.
4636978Seric **
4646978Seric **	Typically this is a copy of the transcript plus a copy of the
4656978Seric **	original offending message.
4666978Seric **
467297Seric **	Parameters:
468297Seric **		fp -- the output file.
46910170Seric **		m -- the mailer to output to.
4709542Seric **		e -- the envelope we are working in.
471297Seric **
472297Seric **	Returns:
473297Seric **		none
474297Seric **
475297Seric **	Side Effects:
4766978Seric **		Outputs the body of an error message.
477297Seric */
478297Seric 
47910170Seric errbody(fp, m, e)
480297Seric 	register FILE *fp;
4814318Seric 	register struct mailer *m;
4829542Seric 	register ENVELOPE *e;
483297Seric {
4846978Seric 	register FILE *xfile;
4853189Seric 	char buf[MAXLINE];
4869337Seric 	char *p;
487297Seric 
4889057Seric 	/*
48955372Seric 	**  Output error message header (if specified and available).
49055372Seric 	*/
49155372Seric 
49255372Seric 	if (ErrMsgFile != NULL)
49355372Seric 	{
49455372Seric 		if (*ErrMsgFile == '/')
49555372Seric 		{
49655372Seric 			xfile = fopen(ErrMsgFile, "r");
49755372Seric 			if (xfile != NULL)
49855372Seric 			{
49955372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
50055425Seric 				{
50155425Seric 					expand(buf, buf, &buf[sizeof buf - 1], e);
50255372Seric 					putline(buf, fp, m);
50355425Seric 				}
50455372Seric 				(void) fclose(xfile);
50555372Seric 				fprintf(fp, "\n");
50655372Seric 			}
50755372Seric 		}
50855372Seric 		else
50955372Seric 		{
51055425Seric 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
51155425Seric 			putline(buf, fp, m);
51255372Seric 			fprintf(fp, "\n");
51355372Seric 		}
51455372Seric 	}
51555372Seric 
51655372Seric 	/*
5179057Seric 	**  Output transcript of errors
5189057Seric 	*/
5199057Seric 
5204086Seric 	(void) fflush(stdout);
5219542Seric 	p = queuename(e->e_parent, 'x');
5229337Seric 	if ((xfile = fopen(p, "r")) == NULL)
5239057Seric 	{
5249337Seric 		syserr("Cannot open %s", p);
5259057Seric 		fprintf(fp, "  ----- Transcript of session is unavailable -----\n");
5269057Seric 	}
5279057Seric 	else
5289057Seric 	{
5299057Seric 		fprintf(fp, "   ----- Transcript of session follows -----\n");
5309542Seric 		if (e->e_xfp != NULL)
5319542Seric 			(void) fflush(e->e_xfp);
5329057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
53310170Seric 			putline(buf, fp, m);
5349057Seric 		(void) fclose(xfile);
5359057Seric 	}
536297Seric 	errno = 0;
5374318Seric 
5384318Seric 	/*
5394318Seric 	**  Output text of original message
5404318Seric 	*/
5414318Seric 
5424289Seric 	if (NoReturn)
5434289Seric 		fprintf(fp, "\n   ----- Return message suppressed -----\n\n");
5449542Seric 	else if (e->e_parent->e_dfp != NULL)
5454199Seric 	{
5465984Seric 		if (SendBody)
5475984Seric 		{
54810170Seric 			putline("\n", fp, m);
54910170Seric 			putline("   ----- Unsent message follows -----\n", fp, m);
5505984Seric 			(void) fflush(fp);
55110170Seric 			putheader(fp, m, e->e_parent);
55210170Seric 			putline("\n", fp, m);
55310170Seric 			putbody(fp, m, e->e_parent);
5545984Seric 		}
5555984Seric 		else
5565984Seric 		{
55710170Seric 			putline("\n", fp, m);
55810170Seric 			putline("  ----- Message header follows -----\n", fp, m);
5595984Seric 			(void) fflush(fp);
56010170Seric 			putheader(fp, m, e->e_parent);
5615984Seric 		}
5624199Seric 	}
5634199Seric 	else
56410170Seric 	{
56510170Seric 		putline("\n", fp, m);
56610170Seric 		putline("  ----- No message was collected -----\n", fp, m);
56710170Seric 		putline("\n", fp, m);
56810170Seric 	}
5694318Seric 
5704318Seric 	/*
5714318Seric 	**  Cleanup and exit
5724318Seric 	*/
5734318Seric 
574297Seric 	if (errno != 0)
5756978Seric 		syserr("errbody: I/O error");
576297Seric }
57758144Seric /*
57858144Seric **  PRUNEROUTE -- prune an RFC-822 source route
57958144Seric **
58058144Seric **	Trims down a source route to the last internet-registered hop.
58158144Seric **	This is encouraged by RFC 1123 section 5.3.3.
58258144Seric **
58358144Seric **	Parameters:
58458144Seric **		addr -- the address
58558144Seric **
58658144Seric **	Returns:
58758144Seric **		TRUE -- address was modified
58858144Seric **		FALSE -- address could not be pruned
58958144Seric **
59058144Seric **	Side Effects:
59158144Seric **		modifies addr in-place
59258144Seric */
59358144Seric 
59458144Seric pruneroute(addr)
59558144Seric 	char *addr;
59658144Seric {
59758144Seric #ifdef NAMED_BIND
59858144Seric 	char *start, *at, *comma;
59958144Seric 	char c;
60058144Seric 	int rcode;
60158144Seric 	char hostbuf[BUFSIZ];
60258144Seric 	char *mxhosts[MAXMXHOSTS + 1];
60358144Seric 
60458144Seric 	/* check to see if this is really a route-addr */
60558144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
60658144Seric 		return FALSE;
60758144Seric 	start = strchr(addr, ':');
60858144Seric 	at = strrchr(addr, '@');
60958144Seric 	if (start == NULL || at == NULL || at < start)
61058144Seric 		return FALSE;
61158144Seric 
61258144Seric 	/* slice off the angle brackets */
61358144Seric 	strcpy(hostbuf, at + 1);
61458144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
61558144Seric 
61658144Seric 	while (start)
61758144Seric 	{
61858144Seric 		if (getmxrr(hostbuf, mxhosts, "", &rcode) > 0)
61958144Seric 		{
62058144Seric 			strcpy(addr + 1, start + 1);
62158144Seric 			return TRUE;
62258144Seric 		}
62358144Seric 		c = *start;
62458144Seric 		*start = '\0';
62558144Seric 		comma = strrchr(addr, ',');
62658144Seric 		if (comma && comma[1] == '@')
62758144Seric 			strcpy(hostbuf, comma + 2);
62858144Seric 		else
62958144Seric 			comma = 0;
63058144Seric 		*start = c;
63158144Seric 		start = comma;
63258144Seric 	}
63358144Seric #endif
63458144Seric 	return FALSE;
63558144Seric }
636