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*58144Seric static char sccsid[] = "@(#)savemail.c	6.10 (Berkeley) 02/22/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 		{
78297Seric 			syserr("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:
12624979Seric 		syserr("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 			{
20158111Seric 				if (e->e_errorqueue == NULL &&
20258111Seric 				    strcmp(e->e_from.q_paddr, "<>") != 0)
20358082Seric 					(void) sendtolist(e->e_from.q_paddr,
20458082Seric 							  (ADDRESS *) NULL,
20558082Seric 							  &e->e_errorqueue, e);
20624981Seric 
20724981Seric 				/* deliver a cc: to the postmaster if desired */
20824981Seric 				if (PostMasterCopy != NULL)
20958082Seric 					(void) sendtolist(PostMasterCopy,
21058082Seric 							  (ADDRESS *) NULL,
21158082Seric 							  &e->e_errorqueue, e);
21224942Seric 				q = e->e_errorqueue;
21358111Seric 				if (q == NULL)
21458111Seric 				{
21558111Seric 					/* this is an error-error */
21658111Seric 					state = ESM_USRTMP;
21758111Seric 					break;
21858111Seric 				}
21924942Seric 			}
22024942Seric 			else
22124942Seric 			{
22255012Seric 				if (parseaddr("postmaster", q, 0, '\0', e) == NULL)
22324942Seric 				{
22424942Seric 					syserr("cannot parse postmaster!");
22524942Seric 					ExitStat = EX_SOFTWARE;
22624942Seric 					state = ESM_USRTMP;
22724942Seric 					break;
22824942Seric 				}
22924942Seric 			}
23024942Seric 			if (returntosender(e->e_message != NULL ? e->e_message :
23124942Seric 					   "Unable to deliver mail",
23257438Seric 					   q, (e->e_class >= 0), e) == 0)
23324942Seric 			{
23424942Seric 				state = ESM_DONE;
23524942Seric 				break;
23624942Seric 			}
237297Seric 
23824942Seric 			state = state == ESM_MAIL ? ESM_POSTMASTER : ESM_USRTMP;
23924942Seric 			break;
240297Seric 
24124942Seric 		  case ESM_DEADLETTER:
24224942Seric 			/*
24324942Seric 			**  Save the message in dead.letter.
24424942Seric 			**	If we weren't mailing back, and the user is
24524942Seric 			**	local, we should save the message in
24624942Seric 			**	~/dead.letter so that the poor person doesn't
24724942Seric 			**	have to type it over again -- and we all know
24824942Seric 			**	what poor typists UNIX users are.
24924942Seric 			*/
2505315Seric 
25124942Seric 			p = NULL;
25224942Seric 			if (e->e_from.q_mailer == LocalMailer)
25324942Seric 			{
25424942Seric 				if (e->e_from.q_home != NULL)
25524942Seric 					p = e->e_from.q_home;
25624942Seric 				else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
25724942Seric 					p = pw->pw_dir;
25824942Seric 			}
25924942Seric 			if (p == NULL)
26024942Seric 			{
26124942Seric 				syserr("Can't return mail to %s", e->e_from.q_paddr);
26224942Seric 				state = ESM_MAIL;
26324942Seric 				break;
26424942Seric 			}
26524942Seric 			if (e->e_dfp != NULL)
26624942Seric 			{
26724942Seric 				auto ADDRESS *q;
26824942Seric 				bool oldverb = Verbose;
26924942Seric 
27024942Seric 				/* we have a home directory; open dead.letter */
27124942Seric 				define('z', p, e);
27258050Seric 				expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e);
27324942Seric 				Verbose = TRUE;
27424942Seric 				message(Arpa_Info, "Saving message in %s", buf);
27524942Seric 				Verbose = oldverb;
27624942Seric 				e->e_to = buf;
27724942Seric 				q = NULL;
27858082Seric 				(void) sendtolist(buf, &e->e_from, &q, e);
27924942Seric 				if (deliver(e, q) == 0)
28024942Seric 					state = ESM_DONE;
28124942Seric 				else
28224942Seric 					state = ESM_MAIL;
28324942Seric 			}
28425569Seric 			else
28525569Seric 			{
28625569Seric 				/* no data file -- try mailing back */
28725569Seric 				state = ESM_MAIL;
28825569Seric 			}
28924942Seric 			break;
29024942Seric 
29124942Seric 		  case ESM_USRTMP:
29224942Seric 			/*
29324942Seric 			**  Log the mail in /usr/tmp/dead.letter.
29424942Seric 			*/
29524942Seric 
29657438Seric 			if (e->e_class < 0)
29757438Seric 			{
29857438Seric 				state = ESM_DONE;
29957438Seric 				break;
30057438Seric 			}
30157438Seric 
30224942Seric 			fp = dfopen("/usr/tmp/dead.letter", "a");
30324942Seric 			if (fp == NULL)
30424942Seric 			{
30524942Seric 				state = ESM_PANIC;
30624942Seric 				break;
30724942Seric 			}
30824942Seric 
30958010Seric 			putfromline(fp, FileMailer, e);
31058010Seric 			(*e->e_puthdr)(fp, FileMailer, e);
31158010Seric 			putline("\n", fp, FileMailer);
31258010Seric 			(*e->e_putbody)(fp, FileMailer, e);
31358010Seric 			putline("\n", fp, FileMailer);
31424942Seric 			(void) fflush(fp);
31524942Seric 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
31624942Seric 			(void) fclose(fp);
31724942Seric 			break;
31824942Seric 
31924942Seric 		  default:
32024942Seric 			syserr("savemail: unknown state %d", state);
32124942Seric 
32224942Seric 			/* fall through ... */
32324942Seric 
32424942Seric 		  case ESM_PANIC:
32524942Seric 			/* leave the locked queue & transcript files around */
32657438Seric 			syserr("savemail: cannot save rejected email anywhere");
32724942Seric 			exit(EX_SOFTWARE);
32824942Seric 		}
329297Seric 	}
330297Seric }
331297Seric /*
3324633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
3334633Seric **
3344633Seric **	Parameters:
3354633Seric **		msg -- the explanatory message.
33616479Seric **		returnq -- the queue of people to send the message to.
3375984Seric **		sendbody -- if TRUE, also send back the body of the
3385984Seric **			message; otherwise just send the header.
33955012Seric **		e -- the current envelope.
3404633Seric **
3414633Seric **	Returns:
3424633Seric **		zero -- if everything went ok.
3434633Seric **		else -- some error.
3444633Seric **
3454633Seric **	Side Effects:
3464633Seric **		Returns the current message to the sender via
3474633Seric **		mail.
3484633Seric */
3494633Seric 
3505984Seric static bool	SendBody;
3514633Seric 
3527045Seric #define MAXRETURNS	6	/* max depth of returning messages */
3537045Seric 
35455012Seric returntosender(msg, returnq, sendbody, e)
3554633Seric 	char *msg;
35616479Seric 	ADDRESS *returnq;
3575984Seric 	bool sendbody;
35855012Seric 	register ENVELOPE *e;
3594633Seric {
3604633Seric 	char buf[MAXNAME];
3616978Seric 	extern putheader(), errbody();
3626978Seric 	register ENVELOPE *ee;
3636978Seric 	extern ENVELOPE *newenvelope();
3646978Seric 	ENVELOPE errenvelope;
3657045Seric 	static int returndepth;
3669375Seric 	register ADDRESS *q;
3674633Seric 
3687676Seric 	if (tTd(6, 1))
3697287Seric 	{
37055012Seric 		printf("Return To Sender: msg=\"%s\", depth=%d, e=%x,\n",
37155012Seric 		       msg, returndepth, e);
37224942Seric 		printf("\treturnq=");
37316479Seric 		printaddr(returnq, TRUE);
3747287Seric 	}
3757287Seric 
3767045Seric 	if (++returndepth >= MAXRETURNS)
3777045Seric 	{
3787045Seric 		if (returndepth != MAXRETURNS)
37916479Seric 			syserr("returntosender: infinite recursion on %s", returnq->q_paddr);
3807045Seric 		/* don't "unrecurse" and fake a clean exit */
3817045Seric 		/* returndepth--; */
3827045Seric 		return (0);
3837045Seric 	}
3847045Seric 
3855984Seric 	SendBody = sendbody;
38658050Seric 	define('g', "\201f", e);
38758050Seric 	define('<', "\201f", e);
3886978Seric 	ee = newenvelope(&errenvelope);
38958050Seric 	define('a', "\201b", ee);
3906978Seric 	ee->e_puthdr = putheader;
3916978Seric 	ee->e_putbody = errbody;
3929375Seric 	ee->e_flags |= EF_RESPONSE;
39355012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
39445155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
39516479Seric 	ee->e_sendqueue = returnq;
3969542Seric 	openxscript(ee);
39716479Seric 	for (q = returnq; q != NULL; q = q->q_next)
3989375Seric 	{
399*58144Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
400*58144Seric 			parseaddr(q->q_paddr, q, 0, '\0', e);
401*58144Seric 
4029375Seric 		if (q->q_alias == NULL)
4039375Seric 			addheader("to", q->q_paddr, ee);
4049375Seric 	}
40524942Seric 
40657642Seric # ifdef LOG
40758020Seric 	if (LogLevel > 5)
40857642Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
40957642Seric 			e->e_id, ee->e_id, msg);
41057642Seric # endif
41157642Seric 
41210845Seric 	(void) sprintf(buf, "Returned mail: %s", msg);
41310106Seric 	addheader("subject", buf, ee);
4144633Seric 
4154633Seric 	/* fake up an address header for the from person */
41658050Seric 	expand("\201n", buf, &buf[sizeof buf - 1], e);
41758111Seric 	ee->e_sender = newstr(buf);
41858121Seric 	if (ConfigLevel >= 4)
41958121Seric 		ee->e_returnpath = "<>";
42058121Seric 	else
42158121Seric 		ee->e_returnpath = ee->e_sender;
42255012Seric 	if (parseaddr(buf, &ee->e_from, -1, '\0', e) == NULL)
4234633Seric 	{
4244633Seric 		syserr("Can't parse myself!");
4254633Seric 		ExitStat = EX_SOFTWARE;
4267045Seric 		returndepth--;
4274633Seric 		return (-1);
4284633Seric 	}
42916159Seric 	loweraddr(&ee->e_from);
4305984Seric 
4316978Seric 	/* push state into submessage */
4326978Seric 	CurEnv = ee;
43358050Seric 	define('f', "\201n", ee);
4349375Seric 	define('x', "Mail Delivery Subsystem", ee);
43557642Seric 	eatheader(ee, FALSE);
4365984Seric 
4376978Seric 	/* actually deliver the error message */
43814876Seric 	sendall(ee, SM_DEFAULT);
4396978Seric 
4406978Seric 	/* restore state */
4417811Seric 	dropenvelope(ee);
4426978Seric 	CurEnv = CurEnv->e_parent;
4437045Seric 	returndepth--;
4446978Seric 
4457045Seric 	/* should check for delivery errors here */
4464633Seric 	return (0);
4474633Seric }
4484633Seric /*
4496978Seric **  ERRBODY -- output the body of an error message.
4506978Seric **
4516978Seric **	Typically this is a copy of the transcript plus a copy of the
4526978Seric **	original offending message.
4536978Seric **
454297Seric **	Parameters:
455297Seric **		fp -- the output file.
45610170Seric **		m -- the mailer to output to.
4579542Seric **		e -- the envelope we are working in.
458297Seric **
459297Seric **	Returns:
460297Seric **		none
461297Seric **
462297Seric **	Side Effects:
4636978Seric **		Outputs the body of an error message.
464297Seric */
465297Seric 
46610170Seric errbody(fp, m, e)
467297Seric 	register FILE *fp;
4684318Seric 	register struct mailer *m;
4699542Seric 	register ENVELOPE *e;
470297Seric {
4716978Seric 	register FILE *xfile;
4723189Seric 	char buf[MAXLINE];
4739337Seric 	char *p;
474297Seric 
4759057Seric 	/*
47655372Seric 	**  Output error message header (if specified and available).
47755372Seric 	*/
47855372Seric 
47955372Seric 	if (ErrMsgFile != NULL)
48055372Seric 	{
48155372Seric 		if (*ErrMsgFile == '/')
48255372Seric 		{
48355372Seric 			xfile = fopen(ErrMsgFile, "r");
48455372Seric 			if (xfile != NULL)
48555372Seric 			{
48655372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
48755425Seric 				{
48855425Seric 					expand(buf, buf, &buf[sizeof buf - 1], e);
48955372Seric 					putline(buf, fp, m);
49055425Seric 				}
49155372Seric 				(void) fclose(xfile);
49255372Seric 				fprintf(fp, "\n");
49355372Seric 			}
49455372Seric 		}
49555372Seric 		else
49655372Seric 		{
49755425Seric 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
49855425Seric 			putline(buf, fp, m);
49955372Seric 			fprintf(fp, "\n");
50055372Seric 		}
50155372Seric 	}
50255372Seric 
50355372Seric 	/*
5049057Seric 	**  Output transcript of errors
5059057Seric 	*/
5069057Seric 
5074086Seric 	(void) fflush(stdout);
5089542Seric 	p = queuename(e->e_parent, 'x');
5099337Seric 	if ((xfile = fopen(p, "r")) == NULL)
5109057Seric 	{
5119337Seric 		syserr("Cannot open %s", p);
5129057Seric 		fprintf(fp, "  ----- Transcript of session is unavailable -----\n");
5139057Seric 	}
5149057Seric 	else
5159057Seric 	{
5169057Seric 		fprintf(fp, "   ----- Transcript of session follows -----\n");
5179542Seric 		if (e->e_xfp != NULL)
5189542Seric 			(void) fflush(e->e_xfp);
5199057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
52010170Seric 			putline(buf, fp, m);
5219057Seric 		(void) fclose(xfile);
5229057Seric 	}
523297Seric 	errno = 0;
5244318Seric 
5254318Seric 	/*
5264318Seric 	**  Output text of original message
5274318Seric 	*/
5284318Seric 
5294289Seric 	if (NoReturn)
5304289Seric 		fprintf(fp, "\n   ----- Return message suppressed -----\n\n");
5319542Seric 	else if (e->e_parent->e_dfp != NULL)
5324199Seric 	{
5335984Seric 		if (SendBody)
5345984Seric 		{
53510170Seric 			putline("\n", fp, m);
53610170Seric 			putline("   ----- Unsent message follows -----\n", fp, m);
5375984Seric 			(void) fflush(fp);
53810170Seric 			putheader(fp, m, e->e_parent);
53910170Seric 			putline("\n", fp, m);
54010170Seric 			putbody(fp, m, e->e_parent);
5415984Seric 		}
5425984Seric 		else
5435984Seric 		{
54410170Seric 			putline("\n", fp, m);
54510170Seric 			putline("  ----- Message header follows -----\n", fp, m);
5465984Seric 			(void) fflush(fp);
54710170Seric 			putheader(fp, m, e->e_parent);
5485984Seric 		}
5494199Seric 	}
5504199Seric 	else
55110170Seric 	{
55210170Seric 		putline("\n", fp, m);
55310170Seric 		putline("  ----- No message was collected -----\n", fp, m);
55410170Seric 		putline("\n", fp, m);
55510170Seric 	}
5564318Seric 
5574318Seric 	/*
5584318Seric 	**  Cleanup and exit
5594318Seric 	*/
5604318Seric 
561297Seric 	if (errno != 0)
5626978Seric 		syserr("errbody: I/O error");
563297Seric }
564*58144Seric /*
565*58144Seric **  PRUNEROUTE -- prune an RFC-822 source route
566*58144Seric **
567*58144Seric **	Trims down a source route to the last internet-registered hop.
568*58144Seric **	This is encouraged by RFC 1123 section 5.3.3.
569*58144Seric **
570*58144Seric **	Parameters:
571*58144Seric **		addr -- the address
572*58144Seric **
573*58144Seric **	Returns:
574*58144Seric **		TRUE -- address was modified
575*58144Seric **		FALSE -- address could not be pruned
576*58144Seric **
577*58144Seric **	Side Effects:
578*58144Seric **		modifies addr in-place
579*58144Seric */
580*58144Seric 
581*58144Seric pruneroute(addr)
582*58144Seric 	char *addr;
583*58144Seric {
584*58144Seric #ifdef NAMED_BIND
585*58144Seric 	char *start, *at, *comma;
586*58144Seric 	char c;
587*58144Seric 	int rcode;
588*58144Seric 	char hostbuf[BUFSIZ];
589*58144Seric 	char *mxhosts[MAXMXHOSTS + 1];
590*58144Seric 
591*58144Seric 	/* check to see if this is really a route-addr */
592*58144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
593*58144Seric 		return FALSE;
594*58144Seric 	start = strchr(addr, ':');
595*58144Seric 	at = strrchr(addr, '@');
596*58144Seric 	if (start == NULL || at == NULL || at < start)
597*58144Seric 		return FALSE;
598*58144Seric 
599*58144Seric 	/* slice off the angle brackets */
600*58144Seric 	strcpy(hostbuf, at + 1);
601*58144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
602*58144Seric 
603*58144Seric 	while (start)
604*58144Seric 	{
605*58144Seric 		if (getmxrr(hostbuf, mxhosts, "", &rcode) > 0)
606*58144Seric 		{
607*58144Seric 			strcpy(addr + 1, start + 1);
608*58144Seric 			return TRUE;
609*58144Seric 		}
610*58144Seric 		c = *start;
611*58144Seric 		*start = '\0';
612*58144Seric 		comma = strrchr(addr, ',');
613*58144Seric 		if (comma && comma[1] == '@')
614*58144Seric 			strcpy(hostbuf, comma + 2);
615*58144Seric 		else
616*58144Seric 			comma = 0;
617*58144Seric 		*start = c;
618*58144Seric 		start = comma;
619*58144Seric 	}
620*58144Seric #endif
621*58144Seric 	return FALSE;
622*58144Seric }
623