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*58178Seric static char sccsid[] = "@(#)savemail.c	6.12 (Berkeley) 02/24/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;
667053Seric 	ForceMail = TRUE;
679337Seric 	e->e_flags &= ~EF_FATALERRS;
68297Seric 
69297Seric 	/*
70297Seric 	**  In the unhappy event we don't know who to return the mail
71297Seric 	**  to, make someone up.
72297Seric 	*/
73297Seric 
749337Seric 	if (e->e_from.q_paddr == NULL)
75297Seric 	{
7655012Seric 		if (parseaddr("root", &e->e_from, 0, '\0', e) == NULL)
77297Seric 		{
7858151Seric 			syserr("553 Cannot parse root!");
79297Seric 			ExitStat = EX_SOFTWARE;
80297Seric 			finis();
81297Seric 		}
82297Seric 	}
839337Seric 	e->e_to = NULL;
84297Seric 
85297Seric 	/*
8624942Seric 	**  Basic state machine.
8724942Seric 	**
8824942Seric 	**	This machine runs through the following states:
8924942Seric 	**
9024942Seric 	**	ESM_QUIET	Errors have already been printed iff the
9124942Seric 	**			sender is local.
9224942Seric 	**	ESM_REPORT	Report directly to the sender's terminal.
9324942Seric 	**	ESM_MAIL	Mail response to the sender.
9424942Seric 	**	ESM_DEADLETTER	Save response in ~/dead.letter.
9524942Seric 	**	ESM_POSTMASTER	Mail response to the postmaster.
9624942Seric 	**	ESM_PANIC	Save response anywhere possible.
97297Seric 	*/
98297Seric 
9924942Seric 	/* determine starting state */
10024942Seric 	switch (ErrorMode)
101297Seric 	{
10224942Seric 	  case EM_WRITE:
10324942Seric 		state = ESM_REPORT;
10424942Seric 		break;
10524942Seric 
10624942Seric 	  case EM_BERKNET:
10724942Seric 		/* mail back, but return o.k. exit status */
108401Seric 		ExitStat = EX_OK;
10924942Seric 
11024942Seric 		/* fall through.... */
11124942Seric 
11224942Seric 	  case EM_MAIL:
11324942Seric 		state = ESM_MAIL;
11424942Seric 		break;
11524942Seric 
11624942Seric 	  case EM_PRINT:
11724979Seric 	  case '\0':
11824942Seric 		state = ESM_QUIET;
11924942Seric 		break;
12024942Seric 
12124942Seric 	  case EM_QUIET:
12224942Seric 		/* no need to return anything at all */
12324942Seric 		return;
12424979Seric 
12524979Seric 	  default:
12658151Seric 		syserr("554 savemail: ErrorMode x%x\n");
12724979Seric 		state = ESM_MAIL;
12824979Seric 		break;
129297Seric 	}
130297Seric 
13124942Seric 	while (state != ESM_DONE)
132297Seric 	{
13324979Seric 		if (tTd(6, 5))
13424979Seric 			printf("  state %d\n", state);
13524979Seric 
13624942Seric 		switch (state)
137297Seric 		{
13824979Seric 		  case ESM_QUIET:
13924979Seric 			if (e->e_from.q_mailer == LocalMailer)
14024979Seric 				state = ESM_DEADLETTER;
14124979Seric 			else
14224979Seric 				state = ESM_MAIL;
14324979Seric 			break;
14424979Seric 
14524942Seric 		  case ESM_REPORT:
14624942Seric 
14724942Seric 			/*
14824942Seric 			**  If the user is still logged in on the same terminal,
14924942Seric 			**  then write the error messages back to hir (sic).
15024942Seric 			*/
15124942Seric 
15224942Seric 			p = ttypath();
15324942Seric 			if (p == NULL || freopen(p, "w", stdout) == NULL)
15424942Seric 			{
15524942Seric 				state = ESM_MAIL;
15624942Seric 				break;
15724942Seric 			}
15824942Seric 
15958050Seric 			expand("\201n", buf, &buf[sizeof buf - 1], e);
1609375Seric 			printf("\r\nMessage from %s...\r\n", buf);
1619375Seric 			printf("Errors occurred while sending mail.\r\n");
1629542Seric 			if (e->e_xfp != NULL)
1639375Seric 			{
1649542Seric 				(void) fflush(e->e_xfp);
16524942Seric 				fp = fopen(queuename(e, 'x'), "r");
1669375Seric 			}
1679375Seric 			else
16824942Seric 				fp = NULL;
16924942Seric 			if (fp == NULL)
1709375Seric 			{
1719337Seric 				syserr("Cannot open %s", queuename(e, 'x'));
1729375Seric 				printf("Transcript of session is unavailable.\r\n");
1739375Seric 			}
1749375Seric 			else
1759375Seric 			{
1769375Seric 				printf("Transcript follows:\r\n");
17724942Seric 				while (fgets(buf, sizeof buf, fp) != NULL &&
1789375Seric 				       !ferror(stdout))
1799375Seric 					fputs(buf, stdout);
18024942Seric 				(void) fclose(fp);
1819375Seric 			}
18224942Seric 			printf("Original message will be saved in dead.letter.\r\n");
18324942Seric 			state = ESM_DEADLETTER;
18424942Seric 			break;
185297Seric 
18624942Seric 		  case ESM_MAIL:
18724942Seric 		  case ESM_POSTMASTER:
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 
19924942Seric 			if (state == ESM_MAIL)
20024942Seric 			{
201*58178Seric 				if (e->e_errorqueue == NULL)
202*58178Seric 				{
203*58178Seric 					char *rpath;
204*58178Seric 
205*58178Seric 					if (strcmp(e->e_returnpath, "<>") != 0)
206*58178Seric 						rpath = e->e_returnpath;
207*58178Seric 					else if (strcmp(e->e_from.q_paddr, "<>") != 0)
208*58178Seric 						rpath = e->e_from.q_paddr;
209*58178Seric 					else
210*58178Seric 						rpath = NULL;
211*58178Seric 					if (rpath != NULL)
212*58178Seric 						(void) sendtolist(rpath,
21358082Seric 							  (ADDRESS *) NULL,
21458082Seric 							  &e->e_errorqueue, e);
215*58178Seric 				}
21624981Seric 
21724981Seric 				/* deliver a cc: to the postmaster if desired */
21824981Seric 				if (PostMasterCopy != NULL)
21958082Seric 					(void) sendtolist(PostMasterCopy,
22058082Seric 							  (ADDRESS *) NULL,
22158082Seric 							  &e->e_errorqueue, e);
22224942Seric 				q = e->e_errorqueue;
22358111Seric 				if (q == NULL)
22458111Seric 				{
22558111Seric 					/* this is an error-error */
22658111Seric 					state = ESM_USRTMP;
22758111Seric 					break;
22858111Seric 				}
22924942Seric 			}
23024942Seric 			else
23124942Seric 			{
23255012Seric 				if (parseaddr("postmaster", q, 0, '\0', e) == NULL)
23324942Seric 				{
23458151Seric 					syserr("553 cannot parse postmaster!");
23524942Seric 					ExitStat = EX_SOFTWARE;
23624942Seric 					state = ESM_USRTMP;
23724942Seric 					break;
23824942Seric 				}
23924942Seric 			}
24024942Seric 			if (returntosender(e->e_message != NULL ? e->e_message :
24124942Seric 					   "Unable to deliver mail",
24257438Seric 					   q, (e->e_class >= 0), e) == 0)
24324942Seric 			{
24424942Seric 				state = ESM_DONE;
24524942Seric 				break;
24624942Seric 			}
247297Seric 
24824942Seric 			state = state == ESM_MAIL ? ESM_POSTMASTER : ESM_USRTMP;
24924942Seric 			break;
250297Seric 
25124942Seric 		  case ESM_DEADLETTER:
25224942Seric 			/*
25324942Seric 			**  Save the message in dead.letter.
25424942Seric 			**	If we weren't mailing back, and the user is
25524942Seric 			**	local, we should save the message in
25624942Seric 			**	~/dead.letter so that the poor person doesn't
25724942Seric 			**	have to type it over again -- and we all know
25824942Seric 			**	what poor typists UNIX users are.
25924942Seric 			*/
2605315Seric 
26124942Seric 			p = NULL;
26224942Seric 			if (e->e_from.q_mailer == LocalMailer)
26324942Seric 			{
26424942Seric 				if (e->e_from.q_home != NULL)
26524942Seric 					p = e->e_from.q_home;
26624942Seric 				else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
26724942Seric 					p = pw->pw_dir;
26824942Seric 			}
26924942Seric 			if (p == NULL)
27024942Seric 			{
27158151Seric 				syserr("554 Can't return mail to %s", e->e_from.q_paddr);
27224942Seric 				state = ESM_MAIL;
27324942Seric 				break;
27424942Seric 			}
27524942Seric 			if (e->e_dfp != NULL)
27624942Seric 			{
27724942Seric 				auto ADDRESS *q;
27824942Seric 				bool oldverb = Verbose;
27924942Seric 
28024942Seric 				/* we have a home directory; open dead.letter */
28124942Seric 				define('z', p, e);
28258050Seric 				expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e);
28324942Seric 				Verbose = TRUE;
28458151Seric 				message("Saving message in %s", buf);
28524942Seric 				Verbose = oldverb;
28624942Seric 				e->e_to = buf;
28724942Seric 				q = NULL;
28858082Seric 				(void) sendtolist(buf, &e->e_from, &q, e);
28924942Seric 				if (deliver(e, q) == 0)
29024942Seric 					state = ESM_DONE;
29124942Seric 				else
29224942Seric 					state = ESM_MAIL;
29324942Seric 			}
29425569Seric 			else
29525569Seric 			{
29625569Seric 				/* no data file -- try mailing back */
29725569Seric 				state = ESM_MAIL;
29825569Seric 			}
29924942Seric 			break;
30024942Seric 
30124942Seric 		  case ESM_USRTMP:
30224942Seric 			/*
30324942Seric 			**  Log the mail in /usr/tmp/dead.letter.
30424942Seric 			*/
30524942Seric 
30657438Seric 			if (e->e_class < 0)
30757438Seric 			{
30857438Seric 				state = ESM_DONE;
30957438Seric 				break;
31057438Seric 			}
31157438Seric 
31224942Seric 			fp = dfopen("/usr/tmp/dead.letter", "a");
31324942Seric 			if (fp == NULL)
31424942Seric 			{
31524942Seric 				state = ESM_PANIC;
31624942Seric 				break;
31724942Seric 			}
31824942Seric 
31958010Seric 			putfromline(fp, FileMailer, e);
32058010Seric 			(*e->e_puthdr)(fp, FileMailer, e);
32158010Seric 			putline("\n", fp, FileMailer);
32258010Seric 			(*e->e_putbody)(fp, FileMailer, e);
32358010Seric 			putline("\n", fp, FileMailer);
32424942Seric 			(void) fflush(fp);
32524942Seric 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
32624942Seric 			(void) fclose(fp);
32724942Seric 			break;
32824942Seric 
32924942Seric 		  default:
33058151Seric 			syserr("554 savemail: unknown state %d", state);
33124942Seric 
33224942Seric 			/* fall through ... */
33324942Seric 
33424942Seric 		  case ESM_PANIC:
33524942Seric 			/* leave the locked queue & transcript files around */
33658151Seric 			syserr("554 savemail: cannot save rejected email anywhere");
33724942Seric 			exit(EX_SOFTWARE);
33824942Seric 		}
339297Seric 	}
340297Seric }
341297Seric /*
3424633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
3434633Seric **
3444633Seric **	Parameters:
3454633Seric **		msg -- the explanatory message.
34616479Seric **		returnq -- the queue of people to send the message to.
3475984Seric **		sendbody -- if TRUE, also send back the body of the
3485984Seric **			message; otherwise just send the header.
34955012Seric **		e -- the current envelope.
3504633Seric **
3514633Seric **	Returns:
3524633Seric **		zero -- if everything went ok.
3534633Seric **		else -- some error.
3544633Seric **
3554633Seric **	Side Effects:
3564633Seric **		Returns the current message to the sender via
3574633Seric **		mail.
3584633Seric */
3594633Seric 
3605984Seric static bool	SendBody;
3614633Seric 
3627045Seric #define MAXRETURNS	6	/* max depth of returning messages */
3637045Seric 
36455012Seric returntosender(msg, returnq, sendbody, e)
3654633Seric 	char *msg;
36616479Seric 	ADDRESS *returnq;
3675984Seric 	bool sendbody;
36855012Seric 	register ENVELOPE *e;
3694633Seric {
3704633Seric 	char buf[MAXNAME];
3716978Seric 	extern putheader(), errbody();
3726978Seric 	register ENVELOPE *ee;
3736978Seric 	extern ENVELOPE *newenvelope();
3746978Seric 	ENVELOPE errenvelope;
3757045Seric 	static int returndepth;
3769375Seric 	register ADDRESS *q;
3774633Seric 
3787676Seric 	if (tTd(6, 1))
3797287Seric 	{
38055012Seric 		printf("Return To Sender: msg=\"%s\", depth=%d, e=%x,\n",
38155012Seric 		       msg, returndepth, e);
38224942Seric 		printf("\treturnq=");
38316479Seric 		printaddr(returnq, TRUE);
3847287Seric 	}
3857287Seric 
3867045Seric 	if (++returndepth >= MAXRETURNS)
3877045Seric 	{
3887045Seric 		if (returndepth != MAXRETURNS)
38958151Seric 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
3907045Seric 		/* don't "unrecurse" and fake a clean exit */
3917045Seric 		/* returndepth--; */
3927045Seric 		return (0);
3937045Seric 	}
3947045Seric 
3955984Seric 	SendBody = sendbody;
39658050Seric 	define('g', "\201f", e);
39758050Seric 	define('<', "\201f", e);
3986978Seric 	ee = newenvelope(&errenvelope);
39958050Seric 	define('a', "\201b", ee);
4006978Seric 	ee->e_puthdr = putheader;
4016978Seric 	ee->e_putbody = errbody;
4029375Seric 	ee->e_flags |= EF_RESPONSE;
40355012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
40445155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
40516479Seric 	ee->e_sendqueue = returnq;
4069542Seric 	openxscript(ee);
40716479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4089375Seric 	{
40958144Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
41058144Seric 			parseaddr(q->q_paddr, q, 0, '\0', e);
41158144Seric 
4129375Seric 		if (q->q_alias == NULL)
4139375Seric 			addheader("to", q->q_paddr, ee);
4149375Seric 	}
41524942Seric 
41657642Seric # ifdef LOG
41758020Seric 	if (LogLevel > 5)
41857642Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
41957642Seric 			e->e_id, ee->e_id, msg);
42057642Seric # endif
42157642Seric 
42210845Seric 	(void) sprintf(buf, "Returned mail: %s", msg);
42310106Seric 	addheader("subject", buf, ee);
4244633Seric 
4254633Seric 	/* fake up an address header for the from person */
42658050Seric 	expand("\201n", buf, &buf[sizeof buf - 1], e);
42758111Seric 	ee->e_sender = newstr(buf);
42858121Seric 	if (ConfigLevel >= 4)
42958121Seric 		ee->e_returnpath = "<>";
43058121Seric 	else
43158121Seric 		ee->e_returnpath = ee->e_sender;
43255012Seric 	if (parseaddr(buf, &ee->e_from, -1, '\0', e) == NULL)
4334633Seric 	{
43458151Seric 		syserr("553 Can't parse myself!");
4354633Seric 		ExitStat = EX_SOFTWARE;
4367045Seric 		returndepth--;
4374633Seric 		return (-1);
4384633Seric 	}
43916159Seric 	loweraddr(&ee->e_from);
4405984Seric 
4416978Seric 	/* push state into submessage */
4426978Seric 	CurEnv = ee;
44358050Seric 	define('f', "\201n", ee);
4449375Seric 	define('x', "Mail Delivery Subsystem", ee);
44557642Seric 	eatheader(ee, FALSE);
4465984Seric 
4476978Seric 	/* actually deliver the error message */
44814876Seric 	sendall(ee, SM_DEFAULT);
4496978Seric 
4506978Seric 	/* restore state */
4517811Seric 	dropenvelope(ee);
4526978Seric 	CurEnv = CurEnv->e_parent;
4537045Seric 	returndepth--;
4546978Seric 
4557045Seric 	/* should check for delivery errors here */
4564633Seric 	return (0);
4574633Seric }
4584633Seric /*
4596978Seric **  ERRBODY -- output the body of an error message.
4606978Seric **
4616978Seric **	Typically this is a copy of the transcript plus a copy of the
4626978Seric **	original offending message.
4636978Seric **
464297Seric **	Parameters:
465297Seric **		fp -- the output file.
46610170Seric **		m -- the mailer to output to.
4679542Seric **		e -- the envelope we are working in.
468297Seric **
469297Seric **	Returns:
470297Seric **		none
471297Seric **
472297Seric **	Side Effects:
4736978Seric **		Outputs the body of an error message.
474297Seric */
475297Seric 
47610170Seric errbody(fp, m, e)
477297Seric 	register FILE *fp;
4784318Seric 	register struct mailer *m;
4799542Seric 	register ENVELOPE *e;
480297Seric {
4816978Seric 	register FILE *xfile;
4823189Seric 	char buf[MAXLINE];
4839337Seric 	char *p;
484297Seric 
4859057Seric 	/*
48655372Seric 	**  Output error message header (if specified and available).
48755372Seric 	*/
48855372Seric 
48955372Seric 	if (ErrMsgFile != NULL)
49055372Seric 	{
49155372Seric 		if (*ErrMsgFile == '/')
49255372Seric 		{
49355372Seric 			xfile = fopen(ErrMsgFile, "r");
49455372Seric 			if (xfile != NULL)
49555372Seric 			{
49655372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
49755425Seric 				{
49855425Seric 					expand(buf, buf, &buf[sizeof buf - 1], e);
49955372Seric 					putline(buf, fp, m);
50055425Seric 				}
50155372Seric 				(void) fclose(xfile);
50255372Seric 				fprintf(fp, "\n");
50355372Seric 			}
50455372Seric 		}
50555372Seric 		else
50655372Seric 		{
50755425Seric 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
50855425Seric 			putline(buf, fp, m);
50955372Seric 			fprintf(fp, "\n");
51055372Seric 		}
51155372Seric 	}
51255372Seric 
51355372Seric 	/*
5149057Seric 	**  Output transcript of errors
5159057Seric 	*/
5169057Seric 
5174086Seric 	(void) fflush(stdout);
5189542Seric 	p = queuename(e->e_parent, 'x');
5199337Seric 	if ((xfile = fopen(p, "r")) == NULL)
5209057Seric 	{
5219337Seric 		syserr("Cannot open %s", p);
5229057Seric 		fprintf(fp, "  ----- Transcript of session is unavailable -----\n");
5239057Seric 	}
5249057Seric 	else
5259057Seric 	{
5269057Seric 		fprintf(fp, "   ----- Transcript of session follows -----\n");
5279542Seric 		if (e->e_xfp != NULL)
5289542Seric 			(void) fflush(e->e_xfp);
5299057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
53010170Seric 			putline(buf, fp, m);
5319057Seric 		(void) fclose(xfile);
5329057Seric 	}
533297Seric 	errno = 0;
5344318Seric 
5354318Seric 	/*
5364318Seric 	**  Output text of original message
5374318Seric 	*/
5384318Seric 
5394289Seric 	if (NoReturn)
5404289Seric 		fprintf(fp, "\n   ----- Return message suppressed -----\n\n");
5419542Seric 	else if (e->e_parent->e_dfp != NULL)
5424199Seric 	{
5435984Seric 		if (SendBody)
5445984Seric 		{
54510170Seric 			putline("\n", fp, m);
54610170Seric 			putline("   ----- Unsent message follows -----\n", fp, m);
5475984Seric 			(void) fflush(fp);
54810170Seric 			putheader(fp, m, e->e_parent);
54910170Seric 			putline("\n", fp, m);
55010170Seric 			putbody(fp, m, e->e_parent);
5515984Seric 		}
5525984Seric 		else
5535984Seric 		{
55410170Seric 			putline("\n", fp, m);
55510170Seric 			putline("  ----- Message header follows -----\n", fp, m);
5565984Seric 			(void) fflush(fp);
55710170Seric 			putheader(fp, m, e->e_parent);
5585984Seric 		}
5594199Seric 	}
5604199Seric 	else
56110170Seric 	{
56210170Seric 		putline("\n", fp, m);
56310170Seric 		putline("  ----- No message was collected -----\n", fp, m);
56410170Seric 		putline("\n", fp, m);
56510170Seric 	}
5664318Seric 
5674318Seric 	/*
5684318Seric 	**  Cleanup and exit
5694318Seric 	*/
5704318Seric 
571297Seric 	if (errno != 0)
5726978Seric 		syserr("errbody: I/O error");
573297Seric }
57458144Seric /*
57558144Seric **  PRUNEROUTE -- prune an RFC-822 source route
57658144Seric **
57758144Seric **	Trims down a source route to the last internet-registered hop.
57858144Seric **	This is encouraged by RFC 1123 section 5.3.3.
57958144Seric **
58058144Seric **	Parameters:
58158144Seric **		addr -- the address
58258144Seric **
58358144Seric **	Returns:
58458144Seric **		TRUE -- address was modified
58558144Seric **		FALSE -- address could not be pruned
58658144Seric **
58758144Seric **	Side Effects:
58858144Seric **		modifies addr in-place
58958144Seric */
59058144Seric 
59158144Seric pruneroute(addr)
59258144Seric 	char *addr;
59358144Seric {
59458144Seric #ifdef NAMED_BIND
59558144Seric 	char *start, *at, *comma;
59658144Seric 	char c;
59758144Seric 	int rcode;
59858144Seric 	char hostbuf[BUFSIZ];
59958144Seric 	char *mxhosts[MAXMXHOSTS + 1];
60058144Seric 
60158144Seric 	/* check to see if this is really a route-addr */
60258144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
60358144Seric 		return FALSE;
60458144Seric 	start = strchr(addr, ':');
60558144Seric 	at = strrchr(addr, '@');
60658144Seric 	if (start == NULL || at == NULL || at < start)
60758144Seric 		return FALSE;
60858144Seric 
60958144Seric 	/* slice off the angle brackets */
61058144Seric 	strcpy(hostbuf, at + 1);
61158144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
61258144Seric 
61358144Seric 	while (start)
61458144Seric 	{
61558144Seric 		if (getmxrr(hostbuf, mxhosts, "", &rcode) > 0)
61658144Seric 		{
61758144Seric 			strcpy(addr + 1, start + 1);
61858144Seric 			return TRUE;
61958144Seric 		}
62058144Seric 		c = *start;
62158144Seric 		*start = '\0';
62258144Seric 		comma = strrchr(addr, ',');
62358144Seric 		if (comma && comma[1] == '@')
62458144Seric 			strcpy(hostbuf, comma + 2);
62558144Seric 		else
62658144Seric 			comma = 0;
62758144Seric 		*start = c;
62858144Seric 		start = comma;
62958144Seric 	}
63058144Seric #endif
63158144Seric 	return FALSE;
63258144Seric }
633