122711Sdist /*
268839Seric  * Copyright (c) 1983, 1995 Eric P. Allman
362531Sbostic  * Copyright (c) 1988, 1993
462531Sbostic  *	The Regents of the University of California.  All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822711Sdist 
922711Sdist #ifndef lint
10*68848Seric static char sccsid[] = "@(#)savemail.c	8.65 (Berkeley) 04/22/95";
1133731Sbostic #endif /* not lint */
1222711Sdist 
1363937Seric # include "sendmail.h"
14297Seric 
15297Seric /*
16297Seric **  SAVEMAIL -- Save mail on error
17297Seric **
189375Seric **	If mailing back errors, mail it back to the originator
19297Seric **	together with an error message; otherwise, just put it in
20297Seric **	dead.letter in the user's home directory (if he exists on
21297Seric **	this machine).
22297Seric **
23297Seric **	Parameters:
249337Seric **		e -- the envelope containing the message in error.
2567981Seric **		sendbody -- if TRUE, also send back the body of the
2667981Seric **			message; otherwise just send the header.
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 
4765174Seric # ifndef _PATH_VARTMP
4865174Seric #  define _PATH_VARTMP	"/usr/tmp/"
4965174Seric # endif
5024942Seric 
5165174Seric 
5267981Seric savemail(e, sendbody)
539337Seric 	register ENVELOPE *e;
5467981Seric 	bool sendbody;
55297Seric {
56297Seric 	register struct passwd *pw;
5724942Seric 	register FILE *fp;
5824942Seric 	int state;
5959290Seric 	auto ADDRESS *q = NULL;
6065870Seric 	register char *p;
6165870Seric 	MCI mcibuf;
6268802Seric 	int sfflags;
63297Seric 	char buf[MAXLINE+1];
64297Seric 	extern char *ttypath();
655846Seric 	typedef int (*fnptr)();
6664945Seric 	extern bool writable();
67297Seric 
687676Seric 	if (tTd(6, 1))
6958680Seric 	{
7066317Seric 		printf("\nsavemail, errormode = %c, id = %s, ExitStat = %d\n  e_from=",
7166317Seric 			e->e_errormode, e->e_id == NULL ? "NONE" : e->e_id,
7266317Seric 			ExitStat);
7358680Seric 		printaddr(&e->e_from, FALSE);
7458680Seric 	}
757361Seric 
7659101Seric 	if (e->e_id == NULL)
7759101Seric 	{
7859101Seric 		/* can't return a message with no id */
7959101Seric 		return;
8059101Seric 	}
8159101Seric 
82297Seric 	/*
83297Seric 	**  In the unhappy event we don't know who to return the mail
84297Seric 	**  to, make someone up.
85297Seric 	*/
86297Seric 
879337Seric 	if (e->e_from.q_paddr == NULL)
88297Seric 	{
8958733Seric 		e->e_sender = "Postmaster";
9064284Seric 		if (parseaddr(e->e_sender, &e->e_from,
9164284Seric 			      RF_COPYPARSE|RF_SENDERADDR, '\0', NULL, e) == NULL)
92297Seric 		{
9358733Seric 			syserr("553 Cannot parse Postmaster!");
94297Seric 			ExitStat = EX_SOFTWARE;
95297Seric 			finis();
96297Seric 		}
97297Seric 	}
989337Seric 	e->e_to = NULL;
99297Seric 
100297Seric 	/*
10124942Seric 	**  Basic state machine.
10224942Seric 	**
10324942Seric 	**	This machine runs through the following states:
10424942Seric 	**
10524942Seric 	**	ESM_QUIET	Errors have already been printed iff the
10624942Seric 	**			sender is local.
10724942Seric 	**	ESM_REPORT	Report directly to the sender's terminal.
10824942Seric 	**	ESM_MAIL	Mail response to the sender.
10924942Seric 	**	ESM_DEADLETTER	Save response in ~/dead.letter.
11024942Seric 	**	ESM_POSTMASTER	Mail response to the postmaster.
11124942Seric 	**	ESM_PANIC	Save response anywhere possible.
112297Seric 	*/
113297Seric 
11424942Seric 	/* determine starting state */
11558734Seric 	switch (e->e_errormode)
116297Seric 	{
11724942Seric 	  case EM_WRITE:
11824942Seric 		state = ESM_REPORT;
11924942Seric 		break;
12024942Seric 
12124942Seric 	  case EM_BERKNET:
12224942Seric 		/* mail back, but return o.k. exit status */
123401Seric 		ExitStat = EX_OK;
12424942Seric 
12524942Seric 		/* fall through.... */
12624942Seric 
12724942Seric 	  case EM_MAIL:
12824942Seric 		state = ESM_MAIL;
12924942Seric 		break;
13024942Seric 
13124942Seric 	  case EM_PRINT:
13224979Seric 	  case '\0':
13324942Seric 		state = ESM_QUIET;
13424942Seric 		break;
13524942Seric 
13624942Seric 	  case EM_QUIET:
13724942Seric 		/* no need to return anything at all */
13824942Seric 		return;
13924979Seric 
14024979Seric 	  default:
14158734Seric 		syserr("554 savemail: bogus errormode x%x\n", e->e_errormode);
14224979Seric 		state = ESM_MAIL;
14324979Seric 		break;
144297Seric 	}
145297Seric 
14659094Seric 	/* if this is already an error response, send to postmaster */
14759094Seric 	if (bitset(EF_RESPONSE, e->e_flags))
14859094Seric 	{
14959094Seric 		if (e->e_parent != NULL &&
15059094Seric 		    bitset(EF_RESPONSE, e->e_parent->e_flags))
15159094Seric 		{
15259094Seric 			/* got an error sending a response -- can it */
15359094Seric 			return;
15459094Seric 		}
15559094Seric 		state = ESM_POSTMASTER;
15659094Seric 	}
15759094Seric 
15824942Seric 	while (state != ESM_DONE)
159297Seric 	{
16024979Seric 		if (tTd(6, 5))
16124979Seric 			printf("  state %d\n", state);
16224979Seric 
16324942Seric 		switch (state)
164297Seric 		{
16524979Seric 		  case ESM_QUIET:
16667473Seric 			if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags))
16724979Seric 				state = ESM_DEADLETTER;
16824979Seric 			else
16924979Seric 				state = ESM_MAIL;
17024979Seric 			break;
17124979Seric 
17224942Seric 		  case ESM_REPORT:
17324942Seric 
17424942Seric 			/*
17524942Seric 			**  If the user is still logged in on the same terminal,
17624942Seric 			**  then write the error messages back to hir (sic).
17724942Seric 			*/
17824942Seric 
17924942Seric 			p = ttypath();
18024942Seric 			if (p == NULL || freopen(p, "w", stdout) == NULL)
18124942Seric 			{
18224942Seric 				state = ESM_MAIL;
18324942Seric 				break;
18424942Seric 			}
18524942Seric 
18668529Seric 			expand("\201n", buf, sizeof buf, e);
1879375Seric 			printf("\r\nMessage from %s...\r\n", buf);
1889375Seric 			printf("Errors occurred while sending mail.\r\n");
1899542Seric 			if (e->e_xfp != NULL)
1909375Seric 			{
1919542Seric 				(void) fflush(e->e_xfp);
19224942Seric 				fp = fopen(queuename(e, 'x'), "r");
1939375Seric 			}
1949375Seric 			else
19524942Seric 				fp = NULL;
19624942Seric 			if (fp == NULL)
1979375Seric 			{
1989337Seric 				syserr("Cannot open %s", queuename(e, 'x'));
1999375Seric 				printf("Transcript of session is unavailable.\r\n");
2009375Seric 			}
2019375Seric 			else
2029375Seric 			{
2039375Seric 				printf("Transcript follows:\r\n");
20424942Seric 				while (fgets(buf, sizeof buf, fp) != NULL &&
2059375Seric 				       !ferror(stdout))
2069375Seric 					fputs(buf, stdout);
20758680Seric 				(void) xfclose(fp, "savemail transcript", e->e_id);
2089375Seric 			}
20924942Seric 			printf("Original message will be saved in dead.letter.\r\n");
21024942Seric 			state = ESM_DEADLETTER;
21124942Seric 			break;
212297Seric 
21324942Seric 		  case ESM_MAIL:
21424942Seric 			/*
21524942Seric 			**  If mailing back, do it.
21624942Seric 			**	Throw away all further output.  Don't alias,
21724942Seric 			**	since this could cause loops, e.g., if joe
21824942Seric 			**	mails to joe@x, and for some reason the network
21924942Seric 			**	for @x is down, then the response gets sent to
22024942Seric 			**	joe@x, which gives a response, etc.  Also force
22124942Seric 			**	the mail to be delivered even if a version of
22224942Seric 			**	it has already been sent to the sender.
22363841Seric 			**
22463841Seric 			**  If this is a configuration or local software
22563841Seric 			**	error, send to the local postmaster as well,
22663841Seric 			**	since the originator can't do anything
22763841Seric 			**	about it anyway.  Note that this is a full
22863841Seric 			**	copy of the message (intentionally) so that
22963841Seric 			**	the Postmaster can forward things along.
23024942Seric 			*/
231297Seric 
23263841Seric 			if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE)
23363841Seric 			{
23463841Seric 				(void) sendtolist("postmaster",
23567982Seric 					  NULLADDR, &e->e_errorqueue, 0, e);
23663841Seric 			}
23767939Seric 			if (!emptyaddr(&e->e_from))
23863841Seric 			{
23958680Seric 				(void) sendtolist(e->e_from.q_paddr,
24067982Seric 					  NULLADDR, &e->e_errorqueue, 0, e);
24163841Seric 			}
24258680Seric 
24363841Seric 			/*
24463841Seric 			**  Deliver a non-delivery report to the
24563841Seric 			**  Postmaster-designate (not necessarily
24663841Seric 			**  Postmaster).  This does not include the
24763841Seric 			**  body of the message, for privacy reasons.
24863841Seric 			**  You really shouldn't need this.
24963841Seric 			*/
25063841Seric 
25163849Seric 			e->e_flags |= EF_PM_NOTIFY;
25258178Seric 
25364792Seric 			/* check to see if there are any good addresses */
25464792Seric 			for (q = e->e_errorqueue; q != NULL; q = q->q_next)
25564792Seric 				if (!bitset(QBADADDR|QDONTSEND, q->q_flags))
25664792Seric 					break;
25758680Seric 			if (q == NULL)
25858680Seric 			{
25958680Seric 				/* this is an error-error */
26058680Seric 				state = ESM_POSTMASTER;
26158680Seric 				break;
26258680Seric 			}
26366303Seric 			if (returntosender(e->e_message, e->e_errorqueue,
26467981Seric 					   sendbody, e) == 0)
26558680Seric 			{
26658680Seric 				state = ESM_DONE;
26758680Seric 				break;
26858680Seric 			}
26924981Seric 
27058680Seric 			/* didn't work -- return to postmaster */
27158680Seric 			state = ESM_POSTMASTER;
27258680Seric 			break;
27358306Seric 
27458680Seric 		  case ESM_POSTMASTER:
27558680Seric 			/*
27658680Seric 			**  Similar to previous case, but to system postmaster.
27758680Seric 			*/
27858680Seric 
27959432Seric 			q = NULL;
28067982Seric 			if (sendtolist("postmaster", NULL, &q, 0, e) <= 0)
28124942Seric 			{
28258680Seric 				syserr("553 cannot parse postmaster!");
28358680Seric 				ExitStat = EX_SOFTWARE;
28458680Seric 				state = ESM_USRTMP;
28558680Seric 				break;
28624942Seric 			}
28767981Seric 			if (returntosender(e->e_message, q, sendbody, e) == 0)
28824942Seric 			{
28924942Seric 				state = ESM_DONE;
29024942Seric 				break;
29124942Seric 			}
292297Seric 
29358680Seric 			/* didn't work -- last resort */
29458680Seric 			state = ESM_USRTMP;
29524942Seric 			break;
296297Seric 
29724942Seric 		  case ESM_DEADLETTER:
29824942Seric 			/*
29924942Seric 			**  Save the message in dead.letter.
30024942Seric 			**	If we weren't mailing back, and the user is
30124942Seric 			**	local, we should save the message in
30224942Seric 			**	~/dead.letter so that the poor person doesn't
30324942Seric 			**	have to type it over again -- and we all know
30424942Seric 			**	what poor typists UNIX users are.
30524942Seric 			*/
3065315Seric 
30724942Seric 			p = NULL;
30867473Seric 			if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
30924942Seric 			{
31024942Seric 				if (e->e_from.q_home != NULL)
31124942Seric 					p = e->e_from.q_home;
31268693Seric 				else if ((pw = sm_getpwnam(e->e_from.q_user)) != NULL)
31324942Seric 					p = pw->pw_dir;
31424942Seric 			}
31568802Seric 			if (p == NULL || e->e_dfp == NULL)
31624942Seric 			{
31768802Seric 				/* no local directory or no data file */
31824942Seric 				state = ESM_MAIL;
31924942Seric 				break;
32024942Seric 			}
32124942Seric 
32268802Seric 			/* we have a home directory; open dead.letter */
32368802Seric 			define('z', p, e);
32468802Seric 			expand("\201z/dead.letter", buf, sizeof buf, e);
32568802Seric 			sfflags = SFF_NOSLINK|SFF_CREAT|SFF_REGONLY|SFF_RUNASREALUID;
32668802Seric 			e->e_to = buf;
32768802Seric 			goto writefile;
32824942Seric 
32924942Seric 		  case ESM_USRTMP:
33024942Seric 			/*
33124942Seric 			**  Log the mail in /usr/tmp/dead.letter.
33224942Seric 			*/
33324942Seric 
33457438Seric 			if (e->e_class < 0)
33557438Seric 			{
33657438Seric 				state = ESM_DONE;
33757438Seric 				break;
33857438Seric 			}
33957438Seric 
34068490Seric 			if (SafeFileEnv != NULL && SafeFileEnv[0] != '\0')
34168490Seric 			{
34268490Seric 				state = ESM_PANIC;
34368490Seric 				break;
34468490Seric 			}
34568490Seric 
34665174Seric 			strcpy(buf, _PATH_VARTMP);
34765174Seric 			strcat(buf, "dead.letter");
34868802Seric 			sfflags = SFF_NOSLINK|SFF_CREAT|SFF_REGONLY;
34968802Seric 
35068802Seric   writefile:
35168802Seric 			if (!writable(buf, q, sfflags) ||
35268802Seric 			    (fp = safefopen(buf, O_WRONLY|O_CREAT|O_APPEND,
35368802Seric 					    FileMode, sfflags)) == NULL)
35464945Seric 			{
35568802Seric 				if (state == ESM_USRTMP)
35668802Seric 					state = ESM_PANIC;
35768802Seric 				else
35868802Seric 					state = ESM_MAIL;
35964945Seric 				break;
36064945Seric 			}
36124942Seric 
36265870Seric 			bzero(&mcibuf, sizeof mcibuf);
36365870Seric 			mcibuf.mci_out = fp;
36465870Seric 			mcibuf.mci_mailer = FileMailer;
36565870Seric 			if (bitnset(M_7BITS, FileMailer->m_flags))
36665870Seric 				mcibuf.mci_flags |= MCIF_7BIT;
36765870Seric 
36865870Seric 			putfromline(&mcibuf, e);
36968228Seric 			(*e->e_puthdr)(&mcibuf, e->e_header, e);
37068228Seric 			(*e->e_putbody)(&mcibuf, e, NULL);
37165870Seric 			putline("\n", &mcibuf);
37224942Seric 			(void) fflush(fp);
37368802Seric 			if (!ferror(fp))
37468802Seric 			{
37568802Seric 				bool oldverb = Verbose;
37668802Seric 
37768802Seric 				Verbose = TRUE;
37868802Seric 				message("Saved message in %s", buf);
37968802Seric 				Verbose = oldverb;
38068802Seric 				state = ESM_DONE;
38168802Seric 			}
38268802Seric 			else if (state == ESM_USRTMP)
38368802Seric 				state = ESM_PANIC;
38468802Seric 			else
38568802Seric 				state = ESM_MAIL;
38667809Seric 			(void) xfclose(fp, "savemail", buf);
38724942Seric 			break;
38824942Seric 
38924942Seric 		  default:
39058151Seric 			syserr("554 savemail: unknown state %d", state);
39124942Seric 
39224942Seric 			/* fall through ... */
39324942Seric 
39424942Seric 		  case ESM_PANIC:
39524942Seric 			/* leave the locked queue & transcript files around */
39668490Seric 			loseqfile(e, "savemail panic");
39764949Seric 			syserr("!554 savemail: cannot save rejected email anywhere");
39824942Seric 		}
399297Seric 	}
400297Seric }
401297Seric /*
4024633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
4034633Seric **
4044633Seric **	Parameters:
4054633Seric **		msg -- the explanatory message.
40616479Seric **		returnq -- the queue of people to send the message to.
4075984Seric **		sendbody -- if TRUE, also send back the body of the
4085984Seric **			message; otherwise just send the header.
40955012Seric **		e -- the current envelope.
4104633Seric **
4114633Seric **	Returns:
4124633Seric **		zero -- if everything went ok.
4134633Seric **		else -- some error.
4144633Seric **
4154633Seric **	Side Effects:
4164633Seric **		Returns the current message to the sender via
4174633Seric **		mail.
4184633Seric */
4194633Seric 
4207045Seric #define MAXRETURNS	6	/* max depth of returning messages */
42158559Seric #define ERRORFUDGE	100	/* nominal size of error message text */
4227045Seric 
42355012Seric returntosender(msg, returnq, sendbody, e)
4244633Seric 	char *msg;
42516479Seric 	ADDRESS *returnq;
4265984Seric 	bool sendbody;
42755012Seric 	register ENVELOPE *e;
4284633Seric {
42968528Seric 	char buf[MAXNAME + 1];
4306978Seric 	extern putheader(), errbody();
4316978Seric 	register ENVELOPE *ee;
43258680Seric 	ENVELOPE *oldcur = CurEnv;
4336978Seric 	ENVELOPE errenvelope;
4347045Seric 	static int returndepth;
4359375Seric 	register ADDRESS *q;
43668228Seric 	char *p;
4374633Seric 
43860010Seric 	if (returnq == NULL)
43960010Seric 		return (-1);
44060010Seric 
44158966Seric 	if (msg == NULL)
44258966Seric 		msg = "Unable to deliver mail";
44358966Seric 
4447676Seric 	if (tTd(6, 1))
4457287Seric 	{
44667987Seric 		printf("\n*** Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
44755012Seric 		       msg, returndepth, e);
44816479Seric 		printaddr(returnq, TRUE);
44967987Seric 		if (tTd(6, 20))
45067987Seric 		{
45167987Seric 			printf("Sendq=");
45267987Seric 			printaddr(e->e_sendqueue, TRUE);
45367987Seric 		}
4547287Seric 	}
4557287Seric 
4567045Seric 	if (++returndepth >= MAXRETURNS)
4577045Seric 	{
4587045Seric 		if (returndepth != MAXRETURNS)
45958151Seric 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
4607045Seric 		/* don't "unrecurse" and fake a clean exit */
4617045Seric 		/* returndepth--; */
4627045Seric 		return (0);
4637045Seric 	}
4647045Seric 
46558680Seric 	define('g', e->e_from.q_paddr, e);
46664940Seric 	define('u', NULL, e);
46767880Seric 
46867880Seric 	/* initialize error envelope */
46958179Seric 	ee = newenvelope(&errenvelope, e);
47058050Seric 	define('a', "\201b", ee);
47159057Seric 	define('r', "internal", ee);
47259057Seric 	define('s', "localhost", ee);
47359057Seric 	define('_', "localhost", ee);
4746978Seric 	ee->e_puthdr = putheader;
4756978Seric 	ee->e_putbody = errbody;
47664120Seric 	ee->e_flags |= EF_RESPONSE|EF_METOO;
47755012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
47845155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
47916479Seric 	ee->e_sendqueue = returnq;
48064655Seric 	ee->e_msgsize = ERRORFUDGE;
48168559Seric 	if (sendbody)
48264655Seric 		ee->e_msgsize += e->e_msgsize;
48368802Seric 	else
48468802Seric 		ee->e_flags |= EF_NO_BODY_RETN;
48564737Seric 	initsys(ee);
48616479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4879375Seric 	{
48859989Seric 		if (bitset(QBADADDR, q->q_flags))
48958559Seric 			continue;
49058559Seric 
49168141Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
49268141Seric 		{
49368141Seric 			register ADDRESS *p;
49468141Seric 
49568141Seric 			parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e);
49668141Seric 			for (p = returnq; p != NULL; p = p->q_next)
49768141Seric 			{
49868141Seric 				if (p != q && sameaddr(p, q))
49968141Seric 					q->q_flags |= QDONTSEND;
50068141Seric 			}
50168141Seric 		}
50268141Seric 
50359989Seric 		if (!bitset(QDONTSEND, q->q_flags))
50459989Seric 			ee->e_nrcpts++;
50558559Seric 
5069375Seric 		if (q->q_alias == NULL)
50767546Seric 			addheader("To", q->q_paddr, &ee->e_header);
5089375Seric 	}
50924942Seric 
51057642Seric # ifdef LOG
51158020Seric 	if (LogLevel > 5)
51265054Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
51357642Seric 			e->e_id, ee->e_id, msg);
51457642Seric # endif
51557642Seric 
51668003Seric 	if (SendMIMEErrors)
51767261Seric 	{
51868003Seric 		addheader("MIME-Version", "1.0", &ee->e_header);
51968003Seric 		(void) sprintf(buf, "%s.%ld/%s",
52068003Seric 			ee->e_id, curtime(), MyHostName);
52168003Seric 		ee->e_msgboundary = newstr(buf);
52268003Seric 		(void) sprintf(buf,
523*68848Seric #if DSN
52468085Seric 			"multipart/report; report-type=X-delivery-status-1; boundary=\"%s\"",
52568028Seric #else
52668028Seric 			"multipart/mixed; boundary=\"%s\"",
52768028Seric #endif
52868003Seric 			ee->e_msgboundary);
52968003Seric 		addheader("Content-Type", buf, &ee->e_header);
53068003Seric 	}
53168228Seric 	if (strncmp(msg, "Warning:", 8) == 0)
53268003Seric 	{
53367261Seric 		addheader("Subject", msg, ee);
53468228Seric 		p = "warning-timeout";
53567261Seric 	}
53667429Seric 	else if (strcmp(msg, "Return receipt") == 0)
53767429Seric 	{
53867429Seric 		addheader("Subject", msg, ee);
53968228Seric 		p = "return-receipt";
54067429Seric 	}
54167261Seric 	else
54267261Seric 	{
54367261Seric 		sprintf(buf, "Returned mail: %.*s", sizeof buf - 20, msg);
54467546Seric 		addheader("Subject", buf, &ee->e_header);
54568228Seric 		p = "failure";
54667261Seric 	}
54768228Seric 	(void) sprintf(buf, "auto-generated (%s)", p);
54868228Seric 	addheader("Auto-Submitted", buf, &ee->e_header);
5494633Seric 
5504633Seric 	/* fake up an address header for the from person */
55168529Seric 	expand("\201n", buf, sizeof buf, e);
55264284Seric 	if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL)
5534633Seric 	{
55458151Seric 		syserr("553 Can't parse myself!");
5554633Seric 		ExitStat = EX_SOFTWARE;
5567045Seric 		returndepth--;
5574633Seric 		return (-1);
5584633Seric 	}
55958704Seric 	ee->e_sender = ee->e_from.q_paddr;
5605984Seric 
5616978Seric 	/* push state into submessage */
5626978Seric 	CurEnv = ee;
56358050Seric 	define('f', "\201n", ee);
5649375Seric 	define('x', "Mail Delivery Subsystem", ee);
56558929Seric 	eatheader(ee, TRUE);
5665984Seric 
56763753Seric 	/* mark statistics */
56864284Seric 	markstats(ee, NULLADDR);
56963753Seric 
5706978Seric 	/* actually deliver the error message */
57114876Seric 	sendall(ee, SM_DEFAULT);
5726978Seric 
5736978Seric 	/* restore state */
5747811Seric 	dropenvelope(ee);
57558680Seric 	CurEnv = oldcur;
5767045Seric 	returndepth--;
5776978Seric 
5787045Seric 	/* should check for delivery errors here */
5794633Seric 	return (0);
5804633Seric }
5814633Seric /*
5826978Seric **  ERRBODY -- output the body of an error message.
5836978Seric **
5846978Seric **	Typically this is a copy of the transcript plus a copy of the
5856978Seric **	original offending message.
5866978Seric **
587297Seric **	Parameters:
58865870Seric **		mci -- the mailer connection information.
5899542Seric **		e -- the envelope we are working in.
59067546Seric **		separator -- any possible MIME separator.
59167936Seric **		flags -- to modify the behaviour.
592297Seric **
593297Seric **	Returns:
594297Seric **		none
595297Seric **
596297Seric **	Side Effects:
5976978Seric **		Outputs the body of an error message.
598297Seric */
599297Seric 
60068228Seric errbody(mci, e, separator)
60165870Seric 	register MCI *mci;
6029542Seric 	register ENVELOPE *e;
60367546Seric 	char *separator;
604297Seric {
6056978Seric 	register FILE *xfile;
60659082Seric 	char *p;
60759082Seric 	register ADDRESS *q;
60859082Seric 	bool printheader;
60968559Seric 	bool sendbody;
6103189Seric 	char buf[MAXLINE];
61168228Seric 	extern char *xtextify();
612297Seric 
61367546Seric 	if (bitset(MCIF_INHEADER, mci->mci_flags))
61467546Seric 	{
61567546Seric 		putline("", mci);
61667546Seric 		mci->mci_flags &= ~MCIF_INHEADER;
61767546Seric 	}
61858680Seric 	if (e->e_parent == NULL)
61958680Seric 	{
62058680Seric 		syserr("errbody: null parent");
62165870Seric 		putline("   ----- Original message lost -----\n", mci);
62258680Seric 		return;
62358680Seric 	}
62458680Seric 
6259057Seric 	/*
62659730Seric 	**  Output MIME header.
62759730Seric 	*/
62859730Seric 
62959730Seric 	if (e->e_msgboundary != NULL)
63059730Seric 	{
63165870Seric 		putline("This is a MIME-encapsulated message", mci);
63265870Seric 		putline("", mci);
63359730Seric 		(void) sprintf(buf, "--%s", e->e_msgboundary);
63465870Seric 		putline(buf, mci);
63565870Seric 		putline("", mci);
63659730Seric 	}
63759730Seric 
63859730Seric 	/*
63963852Seric 	**  Output introductory information.
64063852Seric 	*/
64163852Seric 
64264718Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
64364718Seric 		if (bitset(QBADADDR, q->q_flags))
64464718Seric 			break;
64565054Seric 	if (q == NULL &&
64665054Seric 	    !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags))
64764718Seric 	{
64864718Seric 		putline("    **********************************************",
64965870Seric 			mci);
65064718Seric 		putline("    **      THIS IS A WARNING MESSAGE ONLY      **",
65165870Seric 			mci);
65264718Seric 		putline("    **  YOU DO NOT NEED TO RESEND YOUR MESSAGE  **",
65365870Seric 			mci);
65464718Seric 		putline("    **********************************************",
65565870Seric 			mci);
65665870Seric 		putline("", mci);
65764718Seric 	}
65864718Seric 	sprintf(buf, "The original message was received at %s",
65964718Seric 		arpadate(ctime(&e->e_parent->e_ctime)));
66065870Seric 	putline(buf, mci);
66168529Seric 	expand("from \201_", buf, sizeof buf, e->e_parent);
66265870Seric 	putline(buf, mci);
66365870Seric 	putline("", mci);
66463852Seric 
66563852Seric 	/*
66655372Seric 	**  Output error message header (if specified and available).
66755372Seric 	*/
66855372Seric 
66967682Seric 	if (ErrMsgFile != NULL && !bitset(EF_SENDRECEIPT, e->e_parent->e_flags))
67055372Seric 	{
67155372Seric 		if (*ErrMsgFile == '/')
67255372Seric 		{
67355372Seric 			xfile = fopen(ErrMsgFile, "r");
67455372Seric 			if (xfile != NULL)
67555372Seric 			{
67655372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
67755425Seric 				{
67868529Seric 					expand(buf, buf, sizeof buf, e);
67965870Seric 					putline(buf, mci);
68055425Seric 				}
68155372Seric 				(void) fclose(xfile);
68265870Seric 				putline("\n", mci);
68355372Seric 			}
68455372Seric 		}
68555372Seric 		else
68655372Seric 		{
68768529Seric 			expand(ErrMsgFile, buf, sizeof buf, e);
68865870Seric 			putline(buf, mci);
68965870Seric 			putline("", mci);
69055372Seric 		}
69155372Seric 	}
69255372Seric 
69355372Seric 	/*
69459082Seric 	**  Output message introduction
69559082Seric 	*/
69659082Seric 
69759082Seric 	printheader = TRUE;
69859082Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
69959082Seric 	{
70068603Seric 		if (bitset(QBADADDR|QREPORT|QRELAYED|QEXPLODED, q->q_flags))
70159082Seric 		{
70263849Seric 			strcpy(buf, q->q_paddr);
70363787Seric 			if (bitset(QBADADDR, q->q_flags))
70463849Seric 				strcat(buf, "  (unrecoverable error)");
70568603Seric 			else if (!bitset(QPRIMARY, q->q_flags))
70668603Seric 				continue;
70767981Seric 			else if (bitset(QRELAYED, q->q_flags))
70867981Seric 				strcat(buf, "  (relayed to non-DSN-aware mailer)");
70967880Seric 			else if (bitset(QSENT, q->q_flags))
71067880Seric 				strcat(buf, "  (successfully delivered)");
71168603Seric 			else if (bitset(QEXPLODED, q->q_flags))
71268603Seric 				strcat(buf, "  (expanded by mailing list)");
71363787Seric 			else
71463849Seric 				strcat(buf, "  (transient failure)");
71568603Seric 			if (printheader)
71668603Seric 			{
71768603Seric 				putline("   ----- The following addresses have delivery notifications -----",
71868603Seric 					mci);
71968603Seric 				printheader = FALSE;
72068603Seric 			}
72165870Seric 			putline(buf, mci);
72263849Seric 			if (q->q_alias != NULL)
72363849Seric 			{
72463849Seric 				strcpy(buf, "    (expanded from: ");
72563849Seric 				strcat(buf, q->q_alias->q_paddr);
72663849Seric 				strcat(buf, ")");
72765870Seric 				putline(buf, mci);
72863849Seric 			}
72959082Seric 		}
73059082Seric 	}
73159082Seric 	if (!printheader)
73265870Seric 		putline("\n", mci);
73359082Seric 
73459082Seric 	/*
7359057Seric 	**  Output transcript of errors
7369057Seric 	*/
7379057Seric 
7384086Seric 	(void) fflush(stdout);
7399542Seric 	p = queuename(e->e_parent, 'x');
7409337Seric 	if ((xfile = fopen(p, "r")) == NULL)
7419057Seric 	{
7429337Seric 		syserr("Cannot open %s", p);
74365870Seric 		putline("   ----- Transcript of session is unavailable -----\n", mci);
7449057Seric 	}
7459057Seric 	else
7469057Seric 	{
74765870Seric 		putline("   ----- Transcript of session follows -----\n", mci);
7489542Seric 		if (e->e_xfp != NULL)
7499542Seric 			(void) fflush(e->e_xfp);
7509057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
75165870Seric 			putline(buf, mci);
75258680Seric 		(void) xfclose(xfile, "errbody xscript", p);
7539057Seric 	}
754297Seric 	errno = 0;
7554318Seric 
756*68848Seric #if DSN
7574318Seric 	/*
75867880Seric 	**  Output machine-readable version.
75967880Seric 	*/
76067880Seric 
76167880Seric 	if (e->e_msgboundary != NULL)
76267880Seric 	{
76367880Seric 		putline("", mci);
76467880Seric 		(void) sprintf(buf, "--%s", e->e_msgboundary);
76567880Seric 		putline(buf, mci);
76668606Seric 		putline("Content-Type: message/X-delivery-status-04 (Draft of 20 January 1995)", mci);
76767880Seric 		putline("", mci);
76867880Seric 
76967880Seric 		/*
77067880Seric 		**  Output per-message information.
77167880Seric 		*/
77267880Seric 
77367880Seric 		/* original envelope id from MAIL FROM: line */
77467880Seric 		if (e->e_parent->e_envid != NULL)
77567880Seric 		{
77667880Seric 			(void) sprintf(buf, "Original-Envelope-Id: %s",
77768228Seric 				xtextify(e->e_parent->e_envid));
77867880Seric 			putline(buf, mci);
77967880Seric 		}
78067880Seric 
78168228Seric 		/* Reporting-MTA: is us (required) */
78268228Seric 		p = e->e_parent->e_from.q_mailer->m_mtatype;
78368228Seric 		if (p == NULL)
78468228Seric 			p = "dns";
78568583Seric 		(void) sprintf(buf, "Reporting-MTA: %s; %s", p,
78668583Seric 			xtextify(MyHostName));
78767880Seric 		putline(buf, mci);
78867880Seric 
78968228Seric 		/* Received-From-MTA: shows where we got this message from */
79067990Seric 		if (RealHostName != NULL)
79167990Seric 		{
79268228Seric 			/* XXX use $s for type? */
79368228Seric 			p = e->e_parent->e_from.q_mailer->m_mtatype;
79468228Seric 			if (p == NULL)
79568228Seric 				p = "dns";
79668228Seric 			(void) sprintf(buf, "Received-From-MTA: %s; %s",
79768583Seric 				p, xtextify(RealHostName));
79867990Seric 			putline(buf, mci);
79967990Seric 		}
80067963Seric 
80167963Seric 		/* Arrival-Date: -- when it arrived here */
80267963Seric 		(void) sprintf(buf, "Arrival-Date: %s",
80367963Seric 			arpadate(ctime(&e->e_parent->e_ctime)));
80467963Seric 		putline(buf, mci);
80567963Seric 
80667880Seric 		/*
80767880Seric 		**  Output per-address information.
80867880Seric 		*/
80967880Seric 
81067880Seric 		for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
81167880Seric 		{
81267880Seric 			register ADDRESS *r;
81368603Seric 			char *action;
81467880Seric 
81568603Seric 			if (bitset(QBADADDR, q->q_flags))
81668603Seric 				action = "failure";
81768603Seric 			else if (!bitset(QPRIMARY, q->q_flags))
81867880Seric 				continue;
81968603Seric 			else if (bitset(QRELAYED, q->q_flags))
82068603Seric 				action = "relayed";
82168603Seric 			else if (bitset(QEXPLODED, q->q_flags))
82268603Seric 				action = "delivered (to mailing list)";
82368603Seric 			else if (bitset(QSENT, q->q_flags) &&
82468603Seric 				 bitnset(M_LOCALMAILER, q->q_mailer->m_flags))
82568603Seric 				action = "delivered (final delivery)";
82668603Seric 			else if (bitset(QREPORT, q->q_flags))
82768603Seric 				action = "delayed";
82868603Seric 			else
82968603Seric 				continue;
83068603Seric 
83167880Seric 			putline("", mci);
83267880Seric 
83368228Seric 			/* Original-Recipient: -- passed from on high */
83468228Seric 			if (q->q_orcpt != NULL)
83568228Seric 			{
83668228Seric 				(void) sprintf(buf, "Original-Recipient: %s",
83768228Seric 					xtextify(q->q_orcpt));
83868228Seric 				putline(buf, mci);
83968228Seric 			}
84068228Seric 
84168228Seric 			/* Final-Recipient: -- the name from the RCPT command */
84268228Seric 			p = e->e_parent->e_from.q_mailer->m_addrtype;
84368228Seric 			if (p == NULL)
84468228Seric 				p = "rfc822";
84568228Seric 			for (r = q; r->q_alias != NULL; r = r->q_alias)
84668228Seric 				continue;
84768228Seric 			if (strchr(r->q_user, '@') == NULL)
84868583Seric 			{
84968583Seric 				(void) sprintf(buf, "Final-Recipient: %s; %s@",
85068583Seric 					p, xtextify(r->q_user));
85168583Seric 				strcat(buf, xtextify(MyHostName));
85268583Seric 			}
85367998Seric 			else
85468583Seric 			{
85568228Seric 				(void) sprintf(buf, "Final-Recipient: %s; %s",
85668228Seric 					p, xtextify(r->q_user));
85768583Seric 			}
85867880Seric 			putline(buf, mci);
85967880Seric 
86068603Seric 			/* X-Actual-Recipient: -- the real problem address */
86168603Seric 			if (r != q)
86268603Seric 			{
86368603Seric 				if (strchr(q->q_user, '@') == NULL)
86468603Seric 				{
86568603Seric 					(void) sprintf(buf, "X-Actual-Recipient: %s; %s@",
86668603Seric 						p, xtextify(q->q_user));
86768603Seric 					strcat(buf, xtextify(MyHostName));
86868603Seric 				}
86968603Seric 				else
87068603Seric 				{
87168603Seric 					(void) sprintf(buf, "X-Actual-Recipient: %s; %s",
87268603Seric 						p, xtextify(q->q_user));
87368603Seric 				}
87468603Seric 				putline(buf, mci);
87568603Seric 			}
87668603Seric 
87767880Seric 			/* Action: -- what happened? */
87868603Seric 			sprintf(buf, "Action: %s", action);
87968603Seric 			putline(buf, mci);
88067880Seric 
88167880Seric 			/* Status: -- what _really_ happened? */
88267880Seric 			strcpy(buf, "Status: ");
88367880Seric 			if (q->q_status != NULL)
88467880Seric 				strcat(buf, q->q_status);
88567880Seric 			else if (bitset(QBADADDR, q->q_flags))
88668228Seric 				strcat(buf, "5.0.0");
88767880Seric 			else if (bitset(QQUEUEUP, q->q_flags))
88868228Seric 				strcat(buf, "4.0.0");
88967880Seric 			else
89068228Seric 				strcat(buf, "2.0.0");
89167880Seric 			putline(buf, mci);
89267880Seric 
89368228Seric 			/* Remote-MTA: -- who was I talking to? */
89468228Seric 			p = q->q_mailer->m_mtatype;
89568228Seric 			if (p == NULL)
89668228Seric 				p = "dns";
89768228Seric 			(void) sprintf(buf, "Remote-MTA: %s; ", p);
89868228Seric 			if (q->q_statmta != NULL)
89968228Seric 				p = q->q_statmta;
90068603Seric 			else if (q->q_host != NULL && q->q_host[0] != '\0')
90168228Seric 				p = q->q_host;
90268228Seric 			else
90368228Seric 				p = NULL;
90468228Seric 			if (p != NULL)
90568228Seric 			{
90668228Seric 				strcat(buf, p);
90768228Seric 				p = &buf[strlen(buf) - 1];
90868228Seric 				if (*p == '.')
90968228Seric 					*p = '\0';
91068228Seric 				putline(buf, mci);
91168228Seric 			}
91268228Seric 
91368228Seric 			/* Diagnostic-Code: -- actual result from other end */
91468228Seric 			if (q->q_rstatus != NULL)
91568228Seric 			{
91668228Seric 				p = q->q_mailer->m_diagtype;
91768228Seric 				if (p == NULL)
91868228Seric 					p = "smtp";
91968228Seric 				(void) sprintf(buf, "Diagnostic-Code: %s; %s",
92068603Seric 					p, q->q_rstatus);
92168228Seric 				putline(buf, mci);
92268228Seric 			}
92368228Seric 
92468228Seric 			/* Last-Attempt-Date: -- fine granularity */
92567880Seric 			if (q->q_statdate == (time_t) 0L)
92667880Seric 				q->q_statdate = curtime();
92768228Seric 			(void) sprintf(buf, "Last-Attempt-Date: %s",
92867880Seric 				arpadate(ctime(&q->q_statdate)));
92967880Seric 			putline(buf, mci);
93067880Seric 
93167963Seric 			/* Expiry-Date: -- for delayed messages only */
93267963Seric 			if (bitset(QQUEUEUP, q->q_flags) &&
93367963Seric 			    !bitset(QBADADDR, q->q_flags))
93467963Seric 			{
93567963Seric 				time_t xdate;
93667963Seric 
93767963Seric 				xdate = e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass];
93867963Seric 				sprintf(buf, "Expiry-Date: %s",
93967963Seric 					arpadate(ctime(&xdate)));
94067963Seric 				putline(buf, mci);
94167963Seric 			}
94267880Seric 		}
94367880Seric 	}
94468028Seric #endif
94567880Seric 
94667880Seric 	/*
9474318Seric 	**  Output text of original message
9484318Seric 	*/
9494318Seric 
95065870Seric 	putline("", mci);
95168564Seric 	if (bitset(EF_HAS_DF, e->e_parent->e_flags))
9524199Seric 	{
95368802Seric 		sendbody = !bitset(EF_NO_BODY_RETN, e->e_parent->e_flags) &&
95468802Seric 			   !bitset(EF_NO_BODY_RETN, e->e_flags);
95568559Seric 
95667880Seric 		if (e->e_msgboundary == NULL)
95767880Seric 		{
95868559Seric 			if (sendbody)
95967880Seric 				putline("   ----- Original message follows -----\n", mci);
96067880Seric 			else
96167880Seric 				putline("   ----- Message header follows -----\n", mci);
96267880Seric 			(void) fflush(mci->mci_out);
96367880Seric 		}
9645984Seric 		else
9655984Seric 		{
96659730Seric 			(void) sprintf(buf, "--%s", e->e_msgboundary);
96765870Seric 			putline(buf, mci);
96868228Seric 			(void) sprintf(buf, "Content-Type: message/rfc822%s",
96968802Seric 				sendbody ? "" : "-headers");
97068228Seric 			putline(buf, mci);
9715984Seric 		}
97267981Seric 		putline("", mci);
97368228Seric 		putheader(mci, e->e_parent->e_header, e->e_parent);
97468559Seric 		if (sendbody)
97568228Seric 			putbody(mci, e->e_parent, e->e_msgboundary);
97668228Seric 		else if (e->e_msgboundary == NULL)
97767546Seric 		{
97867546Seric 			putline("", mci);
97965870Seric 			putline("   ----- Message body suppressed -----", mci);
98067546Seric 		}
9814199Seric 	}
98268228Seric 	else if (e->e_msgboundary == NULL)
98310170Seric 	{
98465870Seric 		putline("  ----- No message was collected -----\n", mci);
98510170Seric 	}
9864318Seric 
98760010Seric 	if (e->e_msgboundary != NULL)
98860010Seric 	{
98965870Seric 		putline("", mci);
99060010Seric 		(void) sprintf(buf, "--%s--", e->e_msgboundary);
99165870Seric 		putline(buf, mci);
99260010Seric 	}
99365870Seric 	putline("", mci);
99459730Seric 
9954318Seric 	/*
9964318Seric 	**  Cleanup and exit
9974318Seric 	*/
9984318Seric 
999297Seric 	if (errno != 0)
10006978Seric 		syserr("errbody: I/O error");
1001297Seric }
100258144Seric /*
100368228Seric **  SMTPTODSN -- convert SMTP to DSN status code
100468228Seric **
100568228Seric **	Parameters:
100668228Seric **		smtpstat -- the smtp status code (e.g., 550).
100768228Seric **
100868228Seric **	Returns:
100968228Seric **		The DSN version of the status code.
101068228Seric */
101168228Seric 
101268228Seric char *
101368228Seric smtptodsn(smtpstat)
101468228Seric 	int smtpstat;
101568228Seric {
101668228Seric 	switch (smtpstat)
101768228Seric 	{
101868228Seric 	  case 450:	/* Req mail action not taken: mailbox unavailable */
101968228Seric 		return "4.2.0";
102068228Seric 
102168228Seric 	  case 451:	/* Req action aborted: local error in processing */
102268228Seric 		return "4.3.0";
102368228Seric 
102468228Seric 	  case 452:	/* Req action not taken: insufficient sys storage */
102568228Seric 		return "4.3.1";
102668228Seric 
102768228Seric 	  case 500:	/* Syntax error, command unrecognized */
102868228Seric 		return "5.5.2";
102968228Seric 
103068228Seric 	  case 501:	/* Syntax error in parameters or arguments */
103168228Seric 		return "5.5.4";
103268228Seric 
103368228Seric 	  case 502:	/* Command not implemented */
103468228Seric 		return "5.5.1";
103568228Seric 
103668228Seric 	  case 503:	/* Bad sequence of commands */
103768228Seric 		return "5.5.1";
103868228Seric 
103968228Seric 	  case 504:	/* Command parameter not implemented */
104068228Seric 		return "5.5.4";
104168228Seric 
104268228Seric 	  case 550:	/* Req mail action not taken: mailbox unavailable */
104368228Seric 		return "5.2.0";
104468228Seric 
104568228Seric 	  case 551:	/* User not local; please try <...> */
104668228Seric 		return "5.1.6";
104768228Seric 
104868228Seric 	  case 552:	/* Req mail action aborted: exceeded storage alloc */
104968228Seric 		return "5.2.2";
105068228Seric 
105168228Seric 	  case 553:	/* Req action not taken: mailbox name not allowed */
105268228Seric 		return "5.1.3";
105368228Seric 
105468228Seric 	  case 554:	/* Transaction failed */
105568228Seric 		return "5.0.0";
105668228Seric 	}
105768228Seric 
105868228Seric 	if ((smtpstat / 100) == 2)
105968228Seric 		return "2.0.0";
106068228Seric 	if ((smtpstat / 100) == 4)
106168228Seric 		return "4.0.0";
106268228Seric 	return "5.0.0";
106368228Seric }
106468228Seric /*
106568228Seric **  XTEXTIFY -- take regular text and turn it into DSN-style xtext
106668228Seric **
106768228Seric **	Parameters:
106868228Seric **		t -- the text to convert.
106968228Seric **
107068228Seric **	Returns:
107168228Seric **		The xtext-ified version of the same string.
107268228Seric */
107368228Seric 
107468228Seric char *
107568228Seric xtextify(t)
107668228Seric 	register char *t;
107768228Seric {
107868228Seric 	register char *p;
107968228Seric 	int l;
108068228Seric 	int nbogus;
108168228Seric 	static char *bp = NULL;
108268228Seric 	static int bplen = 0;
108368228Seric 
108468228Seric 	/* figure out how long this xtext will have to be */
108568228Seric 	nbogus = l = 0;
108668228Seric 	for (p = t; *p != '\0'; p++)
108768228Seric 	{
108868228Seric 		register int c = (*p & 0xff);
108968228Seric 
109068228Seric 		/* ASCII dependence here -- this is the way the spec words it */
109168603Seric 		if ((c < ' ' || c > '~' || c == '+' || c == '\\' || c == '(') &&
109268603Seric 		    c != '\t')
109368228Seric 			nbogus++;
109468228Seric 		l++;
109568228Seric 	}
109668228Seric 	if (nbogus == 0)
109768228Seric 		return t;
109868228Seric 	l += nbogus * 2 + 1;
109968228Seric 
110068228Seric 	/* now allocate space if necessary for the new string */
110168228Seric 	if (l > bplen)
110268228Seric 	{
110368228Seric 		if (bp != NULL)
110468228Seric 			free(bp);
110568228Seric 		bp = xalloc(l);
110668228Seric 		bplen = l;
110768228Seric 	}
110868228Seric 
110968228Seric 	/* ok, copy the text with byte expansion */
111068228Seric 	for (p = bp; *t != '\0'; )
111168228Seric 	{
111268228Seric 		register int c = (*t++ & 0xff);
111368228Seric 
111468228Seric 		/* ASCII dependence here -- this is the way the spec words it */
111568228Seric 		if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(')
111668228Seric 		{
111768228Seric 			*p++ = '+';
111868228Seric 			*p++ = "0123456789abcdef"[c >> 4];
111968228Seric 			*p++ = "0123456789abcdef"[c & 0xf];
112068228Seric 		}
112168228Seric 		else
112268228Seric 			*p++ = c;
112368228Seric 	}
112468228Seric 	*p = '\0';
112568228Seric 	return bp;
112668228Seric }
112768228Seric /*
112868583Seric **  XTEXTOK -- check if a string is legal xtext
112968583Seric **
113068583Seric **	Xtext is used in Delivery Status Notifications.  The spec was
113168583Seric **	taken from draft-ietf-notary-mime-delivery-04.txt.
113268583Seric **
113368583Seric **	Parameters:
113468583Seric **		s -- the string to check.
113568583Seric **
113668583Seric **	Returns:
113768583Seric **		TRUE -- if 's' is legal xtext.
113868583Seric **		FALSE -- if it has any illegal characters in it.
113968583Seric */
114068583Seric 
114168583Seric bool
114268583Seric xtextok(s)
114368583Seric 	char *s;
114468583Seric {
114568583Seric 	int c;
114668583Seric 
114768583Seric 	while ((c = *s++) != '\0')
114868583Seric 	{
114968583Seric 		if (c == '+')
115068583Seric 		{
115168583Seric 			c = *s++;
115268583Seric 			if (!isascii(c) || !isxdigit(c))
115368583Seric 				return FALSE;
115468583Seric 			c = *s++;
115568583Seric 			if (!isascii(c) || !isxdigit(c))
115668583Seric 				return FALSE;
115768583Seric 		}
115868583Seric 		else if (c < '!' || c > '~' || c == '\\' || c == '(')
115968583Seric 			return FALSE;
116068583Seric 	}
116168583Seric 	return TRUE;
116268583Seric }
116368583Seric /*
116458144Seric **  PRUNEROUTE -- prune an RFC-822 source route
116558144Seric **
116658144Seric **	Trims down a source route to the last internet-registered hop.
116758144Seric **	This is encouraged by RFC 1123 section 5.3.3.
116858144Seric **
116958144Seric **	Parameters:
117058144Seric **		addr -- the address
117158144Seric **
117258144Seric **	Returns:
117358144Seric **		TRUE -- address was modified
117458144Seric **		FALSE -- address could not be pruned
117558144Seric **
117658144Seric **	Side Effects:
117758144Seric **		modifies addr in-place
117858144Seric */
117958144Seric 
118058144Seric pruneroute(addr)
118158144Seric 	char *addr;
118258144Seric {
118366334Seric #if NAMED_BIND
118458144Seric 	char *start, *at, *comma;
118558144Seric 	char c;
118658144Seric 	int rcode;
118758144Seric 	char hostbuf[BUFSIZ];
118858144Seric 	char *mxhosts[MAXMXHOSTS + 1];
118958144Seric 
119058144Seric 	/* check to see if this is really a route-addr */
119158144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
119258144Seric 		return FALSE;
119358144Seric 	start = strchr(addr, ':');
119458144Seric 	at = strrchr(addr, '@');
119558144Seric 	if (start == NULL || at == NULL || at < start)
119658144Seric 		return FALSE;
119758144Seric 
119858144Seric 	/* slice off the angle brackets */
119958144Seric 	strcpy(hostbuf, at + 1);
120058144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
120158144Seric 
120258144Seric 	while (start)
120358144Seric 	{
120459273Seric 		if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0)
120558144Seric 		{
120658144Seric 			strcpy(addr + 1, start + 1);
120758144Seric 			return TRUE;
120858144Seric 		}
120958144Seric 		c = *start;
121058144Seric 		*start = '\0';
121158144Seric 		comma = strrchr(addr, ',');
121258144Seric 		if (comma && comma[1] == '@')
121358144Seric 			strcpy(hostbuf, comma + 2);
121458144Seric 		else
121558144Seric 			comma = 0;
121658144Seric 		*start = c;
121758144Seric 		start = comma;
121858144Seric 	}
121958144Seric #endif
122058144Seric 	return FALSE;
122158144Seric }
1222