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*58304Seric static char sccsid[] = "@(#)savemail.c	6.15 (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 			{
203*58304Seric 				char *rpath;
20458178Seric 
205*58304Seric 				if (e->e_returnpath != e->e_sender)
206*58304Seric 					rpath = e->e_returnpath;
207*58304Seric 				else
208*58304Seric 					rpath = e->e_from.q_paddr;
209*58304Seric 				if (strcmp(rpath, "<>") != 0)
210*58304Seric 					(void) sendtolist(rpath,
211*58304Seric 						  (ADDRESS *) NULL,
212*58304Seric 						  &e->e_errorqueue, e);
21324981Seric 
21424981Seric 				/* deliver a cc: to the postmaster if desired */
21524981Seric 				if (PostMasterCopy != NULL)
21658082Seric 					(void) sendtolist(PostMasterCopy,
21758082Seric 							  (ADDRESS *) NULL,
21858082Seric 							  &e->e_errorqueue, e);
21924942Seric 				q = e->e_errorqueue;
22058111Seric 				if (q == NULL)
22158111Seric 				{
22258111Seric 					/* this is an error-error */
22358111Seric 					state = ESM_USRTMP;
22458111Seric 					break;
22558111Seric 				}
22624942Seric 			}
22724942Seric 			else
22824942Seric 			{
22955012Seric 				if (parseaddr("postmaster", q, 0, '\0', e) == NULL)
23024942Seric 				{
23158151Seric 					syserr("553 cannot parse postmaster!");
23224942Seric 					ExitStat = EX_SOFTWARE;
23324942Seric 					state = ESM_USRTMP;
23424942Seric 					break;
23524942Seric 				}
23624942Seric 			}
23724942Seric 			if (returntosender(e->e_message != NULL ? e->e_message :
23824942Seric 					   "Unable to deliver mail",
23957438Seric 					   q, (e->e_class >= 0), e) == 0)
24024942Seric 			{
24124942Seric 				state = ESM_DONE;
24224942Seric 				break;
24324942Seric 			}
244297Seric 
24524942Seric 			state = state == ESM_MAIL ? ESM_POSTMASTER : ESM_USRTMP;
24624942Seric 			break;
247297Seric 
24824942Seric 		  case ESM_DEADLETTER:
24924942Seric 			/*
25024942Seric 			**  Save the message in dead.letter.
25124942Seric 			**	If we weren't mailing back, and the user is
25224942Seric 			**	local, we should save the message in
25324942Seric 			**	~/dead.letter so that the poor person doesn't
25424942Seric 			**	have to type it over again -- and we all know
25524942Seric 			**	what poor typists UNIX users are.
25624942Seric 			*/
2575315Seric 
25824942Seric 			p = NULL;
25924942Seric 			if (e->e_from.q_mailer == LocalMailer)
26024942Seric 			{
26124942Seric 				if (e->e_from.q_home != NULL)
26224942Seric 					p = e->e_from.q_home;
26324942Seric 				else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
26424942Seric 					p = pw->pw_dir;
26524942Seric 			}
26624942Seric 			if (p == NULL)
26724942Seric 			{
26858151Seric 				syserr("554 Can't return mail to %s", e->e_from.q_paddr);
26924942Seric 				state = ESM_MAIL;
27024942Seric 				break;
27124942Seric 			}
27224942Seric 			if (e->e_dfp != NULL)
27324942Seric 			{
27424942Seric 				auto ADDRESS *q;
27524942Seric 				bool oldverb = Verbose;
27624942Seric 
27724942Seric 				/* we have a home directory; open dead.letter */
27824942Seric 				define('z', p, e);
27958050Seric 				expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e);
28024942Seric 				Verbose = TRUE;
28158151Seric 				message("Saving message in %s", buf);
28224942Seric 				Verbose = oldverb;
28324942Seric 				e->e_to = buf;
28424942Seric 				q = NULL;
28558082Seric 				(void) sendtolist(buf, &e->e_from, &q, e);
28624942Seric 				if (deliver(e, q) == 0)
28724942Seric 					state = ESM_DONE;
28824942Seric 				else
28924942Seric 					state = ESM_MAIL;
29024942Seric 			}
29125569Seric 			else
29225569Seric 			{
29325569Seric 				/* no data file -- try mailing back */
29425569Seric 				state = ESM_MAIL;
29525569Seric 			}
29624942Seric 			break;
29724942Seric 
29824942Seric 		  case ESM_USRTMP:
29924942Seric 			/*
30024942Seric 			**  Log the mail in /usr/tmp/dead.letter.
30124942Seric 			*/
30224942Seric 
30357438Seric 			if (e->e_class < 0)
30457438Seric 			{
30557438Seric 				state = ESM_DONE;
30657438Seric 				break;
30757438Seric 			}
30857438Seric 
30924942Seric 			fp = dfopen("/usr/tmp/dead.letter", "a");
31024942Seric 			if (fp == NULL)
31124942Seric 			{
31224942Seric 				state = ESM_PANIC;
31324942Seric 				break;
31424942Seric 			}
31524942Seric 
31658010Seric 			putfromline(fp, FileMailer, e);
31758010Seric 			(*e->e_puthdr)(fp, FileMailer, e);
31858010Seric 			putline("\n", fp, FileMailer);
31958010Seric 			(*e->e_putbody)(fp, FileMailer, e);
32058010Seric 			putline("\n", fp, FileMailer);
32124942Seric 			(void) fflush(fp);
32224942Seric 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
32324942Seric 			(void) fclose(fp);
32424942Seric 			break;
32524942Seric 
32624942Seric 		  default:
32758151Seric 			syserr("554 savemail: unknown state %d", state);
32824942Seric 
32924942Seric 			/* fall through ... */
33024942Seric 
33124942Seric 		  case ESM_PANIC:
33224942Seric 			/* leave the locked queue & transcript files around */
33358151Seric 			syserr("554 savemail: cannot save rejected email anywhere");
33424942Seric 			exit(EX_SOFTWARE);
33524942Seric 		}
336297Seric 	}
337297Seric }
338297Seric /*
3394633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
3404633Seric **
3414633Seric **	Parameters:
3424633Seric **		msg -- the explanatory message.
34316479Seric **		returnq -- the queue of people to send the message to.
3445984Seric **		sendbody -- if TRUE, also send back the body of the
3455984Seric **			message; otherwise just send the header.
34655012Seric **		e -- the current envelope.
3474633Seric **
3484633Seric **	Returns:
3494633Seric **		zero -- if everything went ok.
3504633Seric **		else -- some error.
3514633Seric **
3524633Seric **	Side Effects:
3534633Seric **		Returns the current message to the sender via
3544633Seric **		mail.
3554633Seric */
3564633Seric 
3575984Seric static bool	SendBody;
3584633Seric 
3597045Seric #define MAXRETURNS	6	/* max depth of returning messages */
3607045Seric 
36155012Seric returntosender(msg, returnq, sendbody, e)
3624633Seric 	char *msg;
36316479Seric 	ADDRESS *returnq;
3645984Seric 	bool sendbody;
36555012Seric 	register ENVELOPE *e;
3664633Seric {
3674633Seric 	char buf[MAXNAME];
3686978Seric 	extern putheader(), errbody();
3696978Seric 	register ENVELOPE *ee;
3706978Seric 	extern ENVELOPE *newenvelope();
3716978Seric 	ENVELOPE errenvelope;
3727045Seric 	static int returndepth;
3739375Seric 	register ADDRESS *q;
3744633Seric 
3757676Seric 	if (tTd(6, 1))
3767287Seric 	{
37755012Seric 		printf("Return To Sender: msg=\"%s\", depth=%d, e=%x,\n",
37855012Seric 		       msg, returndepth, e);
37924942Seric 		printf("\treturnq=");
38016479Seric 		printaddr(returnq, TRUE);
3817287Seric 	}
3827287Seric 
3837045Seric 	if (++returndepth >= MAXRETURNS)
3847045Seric 	{
3857045Seric 		if (returndepth != MAXRETURNS)
38658151Seric 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
3877045Seric 		/* don't "unrecurse" and fake a clean exit */
3887045Seric 		/* returndepth--; */
3897045Seric 		return (0);
3907045Seric 	}
3917045Seric 
3925984Seric 	SendBody = sendbody;
39358179Seric 	define('g', e->e_sender, e);
39458179Seric 	define('<', e->e_returnpath, e);
39558179Seric 	ee = newenvelope(&errenvelope, e);
39658050Seric 	define('a', "\201b", ee);
3976978Seric 	ee->e_puthdr = putheader;
3986978Seric 	ee->e_putbody = errbody;
3999375Seric 	ee->e_flags |= EF_RESPONSE;
40055012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
40145155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
40216479Seric 	ee->e_sendqueue = returnq;
4039542Seric 	openxscript(ee);
40416479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4059375Seric 	{
40658144Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
40758144Seric 			parseaddr(q->q_paddr, q, 0, '\0', e);
40858144Seric 
4099375Seric 		if (q->q_alias == NULL)
4109375Seric 			addheader("to", q->q_paddr, ee);
4119375Seric 	}
41224942Seric 
41357642Seric # ifdef LOG
41458020Seric 	if (LogLevel > 5)
41557642Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
41657642Seric 			e->e_id, ee->e_id, msg);
41757642Seric # endif
41857642Seric 
41910845Seric 	(void) sprintf(buf, "Returned mail: %s", msg);
42010106Seric 	addheader("subject", buf, ee);
4214633Seric 
4224633Seric 	/* fake up an address header for the from person */
42358050Seric 	expand("\201n", buf, &buf[sizeof buf - 1], e);
42458111Seric 	ee->e_sender = newstr(buf);
42558121Seric 	if (ConfigLevel >= 4)
42658121Seric 		ee->e_returnpath = "<>";
42758121Seric 	else
42858121Seric 		ee->e_returnpath = ee->e_sender;
42955012Seric 	if (parseaddr(buf, &ee->e_from, -1, '\0', e) == NULL)
4304633Seric 	{
43158151Seric 		syserr("553 Can't parse myself!");
4324633Seric 		ExitStat = EX_SOFTWARE;
4337045Seric 		returndepth--;
4344633Seric 		return (-1);
4354633Seric 	}
43616159Seric 	loweraddr(&ee->e_from);
4375984Seric 
4386978Seric 	/* push state into submessage */
4396978Seric 	CurEnv = ee;
44058050Seric 	define('f', "\201n", ee);
4419375Seric 	define('x', "Mail Delivery Subsystem", ee);
44257642Seric 	eatheader(ee, FALSE);
4435984Seric 
4446978Seric 	/* actually deliver the error message */
44514876Seric 	sendall(ee, SM_DEFAULT);
4466978Seric 
4476978Seric 	/* restore state */
4487811Seric 	dropenvelope(ee);
4496978Seric 	CurEnv = CurEnv->e_parent;
4507045Seric 	returndepth--;
4516978Seric 
4527045Seric 	/* should check for delivery errors here */
4534633Seric 	return (0);
4544633Seric }
4554633Seric /*
4566978Seric **  ERRBODY -- output the body of an error message.
4576978Seric **
4586978Seric **	Typically this is a copy of the transcript plus a copy of the
4596978Seric **	original offending message.
4606978Seric **
461297Seric **	Parameters:
462297Seric **		fp -- the output file.
46310170Seric **		m -- the mailer to output to.
4649542Seric **		e -- the envelope we are working in.
465297Seric **
466297Seric **	Returns:
467297Seric **		none
468297Seric **
469297Seric **	Side Effects:
4706978Seric **		Outputs the body of an error message.
471297Seric */
472297Seric 
47310170Seric errbody(fp, m, e)
474297Seric 	register FILE *fp;
4754318Seric 	register struct mailer *m;
4769542Seric 	register ENVELOPE *e;
477297Seric {
4786978Seric 	register FILE *xfile;
4793189Seric 	char buf[MAXLINE];
4809337Seric 	char *p;
481297Seric 
4829057Seric 	/*
48355372Seric 	**  Output error message header (if specified and available).
48455372Seric 	*/
48555372Seric 
48655372Seric 	if (ErrMsgFile != NULL)
48755372Seric 	{
48855372Seric 		if (*ErrMsgFile == '/')
48955372Seric 		{
49055372Seric 			xfile = fopen(ErrMsgFile, "r");
49155372Seric 			if (xfile != NULL)
49255372Seric 			{
49355372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
49455425Seric 				{
49555425Seric 					expand(buf, buf, &buf[sizeof buf - 1], e);
49655372Seric 					putline(buf, fp, m);
49755425Seric 				}
49855372Seric 				(void) fclose(xfile);
49955372Seric 				fprintf(fp, "\n");
50055372Seric 			}
50155372Seric 		}
50255372Seric 		else
50355372Seric 		{
50455425Seric 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
50555425Seric 			putline(buf, fp, m);
50655372Seric 			fprintf(fp, "\n");
50755372Seric 		}
50855372Seric 	}
50955372Seric 
51055372Seric 	/*
5119057Seric 	**  Output transcript of errors
5129057Seric 	*/
5139057Seric 
5144086Seric 	(void) fflush(stdout);
5159542Seric 	p = queuename(e->e_parent, 'x');
5169337Seric 	if ((xfile = fopen(p, "r")) == NULL)
5179057Seric 	{
5189337Seric 		syserr("Cannot open %s", p);
5199057Seric 		fprintf(fp, "  ----- Transcript of session is unavailable -----\n");
5209057Seric 	}
5219057Seric 	else
5229057Seric 	{
5239057Seric 		fprintf(fp, "   ----- Transcript of session follows -----\n");
5249542Seric 		if (e->e_xfp != NULL)
5259542Seric 			(void) fflush(e->e_xfp);
5269057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
52710170Seric 			putline(buf, fp, m);
5289057Seric 		(void) fclose(xfile);
5299057Seric 	}
530297Seric 	errno = 0;
5314318Seric 
5324318Seric 	/*
5334318Seric 	**  Output text of original message
5344318Seric 	*/
5354318Seric 
5364289Seric 	if (NoReturn)
5374289Seric 		fprintf(fp, "\n   ----- Return message suppressed -----\n\n");
5389542Seric 	else if (e->e_parent->e_dfp != NULL)
5394199Seric 	{
5405984Seric 		if (SendBody)
5415984Seric 		{
54210170Seric 			putline("\n", fp, m);
54310170Seric 			putline("   ----- Unsent message follows -----\n", fp, m);
5445984Seric 			(void) fflush(fp);
54510170Seric 			putheader(fp, m, e->e_parent);
54610170Seric 			putline("\n", fp, m);
54710170Seric 			putbody(fp, m, e->e_parent);
5485984Seric 		}
5495984Seric 		else
5505984Seric 		{
55110170Seric 			putline("\n", fp, m);
55210170Seric 			putline("  ----- Message header follows -----\n", fp, m);
5535984Seric 			(void) fflush(fp);
55410170Seric 			putheader(fp, m, e->e_parent);
5555984Seric 		}
5564199Seric 	}
5574199Seric 	else
55810170Seric 	{
55910170Seric 		putline("\n", fp, m);
56010170Seric 		putline("  ----- No message was collected -----\n", fp, m);
56110170Seric 		putline("\n", fp, m);
56210170Seric 	}
5634318Seric 
5644318Seric 	/*
5654318Seric 	**  Cleanup and exit
5664318Seric 	*/
5674318Seric 
568297Seric 	if (errno != 0)
5696978Seric 		syserr("errbody: I/O error");
570297Seric }
57158144Seric /*
57258144Seric **  PRUNEROUTE -- prune an RFC-822 source route
57358144Seric **
57458144Seric **	Trims down a source route to the last internet-registered hop.
57558144Seric **	This is encouraged by RFC 1123 section 5.3.3.
57658144Seric **
57758144Seric **	Parameters:
57858144Seric **		addr -- the address
57958144Seric **
58058144Seric **	Returns:
58158144Seric **		TRUE -- address was modified
58258144Seric **		FALSE -- address could not be pruned
58358144Seric **
58458144Seric **	Side Effects:
58558144Seric **		modifies addr in-place
58658144Seric */
58758144Seric 
58858144Seric pruneroute(addr)
58958144Seric 	char *addr;
59058144Seric {
59158144Seric #ifdef NAMED_BIND
59258144Seric 	char *start, *at, *comma;
59358144Seric 	char c;
59458144Seric 	int rcode;
59558144Seric 	char hostbuf[BUFSIZ];
59658144Seric 	char *mxhosts[MAXMXHOSTS + 1];
59758144Seric 
59858144Seric 	/* check to see if this is really a route-addr */
59958144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
60058144Seric 		return FALSE;
60158144Seric 	start = strchr(addr, ':');
60258144Seric 	at = strrchr(addr, '@');
60358144Seric 	if (start == NULL || at == NULL || at < start)
60458144Seric 		return FALSE;
60558144Seric 
60658144Seric 	/* slice off the angle brackets */
60758144Seric 	strcpy(hostbuf, at + 1);
60858144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
60958144Seric 
61058144Seric 	while (start)
61158144Seric 	{
61258144Seric 		if (getmxrr(hostbuf, mxhosts, "", &rcode) > 0)
61358144Seric 		{
61458144Seric 			strcpy(addr + 1, start + 1);
61558144Seric 			return TRUE;
61658144Seric 		}
61758144Seric 		c = *start;
61858144Seric 		*start = '\0';
61958144Seric 		comma = strrchr(addr, ',');
62058144Seric 		if (comma && comma[1] == '@')
62158144Seric 			strcpy(hostbuf, comma + 2);
62258144Seric 		else
62358144Seric 			comma = 0;
62458144Seric 		*start = c;
62558144Seric 		start = comma;
62658144Seric 	}
62758144Seric #endif
62858144Seric 	return FALSE;
62958144Seric }
630