122711Sdist /*
234921Sbostic  * Copyright (c) 1983 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*68603Seric static char sccsid[] = "@(#)savemail.c	8.60 (Berkeley) 03/27/95";
1133731Sbostic #endif /* not lint */
1222711Sdist 
1363937Seric # include "sendmail.h"
14297Seric # include <pwd.h>
15297Seric 
16297Seric /*
17297Seric **  SAVEMAIL -- Save mail on error
18297Seric **
199375Seric **	If mailing back errors, mail it back to the originator
20297Seric **	together with an error message; otherwise, just put it in
21297Seric **	dead.letter in the user's home directory (if he exists on
22297Seric **	this machine).
23297Seric **
24297Seric **	Parameters:
259337Seric **		e -- the envelope containing the message in error.
2667981Seric **		sendbody -- if TRUE, also send back the body of the
2767981Seric **			message; otherwise just send the header.
28297Seric **
29297Seric **	Returns:
30297Seric **		none
31297Seric **
32297Seric **	Side Effects:
33297Seric **		Saves the letter, by writing or mailing it back to the
34297Seric **		sender, or by putting it in dead.letter in her home
35297Seric **		directory.
36297Seric */
37297Seric 
3824942Seric /* defines for state machine */
3924942Seric # define ESM_REPORT	0	/* report to sender's terminal */
4024942Seric # define ESM_MAIL	1	/* mail back to sender */
4124942Seric # define ESM_QUIET	2	/* messages have already been returned */
4224942Seric # define ESM_DEADLETTER	3	/* save in ~/dead.letter */
4324942Seric # define ESM_POSTMASTER	4	/* return to postmaster */
4424942Seric # define ESM_USRTMP	5	/* save in /usr/tmp/dead.letter */
4524942Seric # define ESM_PANIC	6	/* leave the locked queue/transcript files */
4624942Seric # define ESM_DONE	7	/* the message is successfully delivered */
4724942Seric 
4865174Seric # ifndef _PATH_VARTMP
4965174Seric #  define _PATH_VARTMP	"/usr/tmp/"
5065174Seric # endif
5124942Seric 
5265174Seric 
5367981Seric savemail(e, sendbody)
549337Seric 	register ENVELOPE *e;
5567981Seric 	bool sendbody;
56297Seric {
57297Seric 	register struct passwd *pw;
5824942Seric 	register FILE *fp;
5924942Seric 	int state;
6059290Seric 	auto ADDRESS *q = NULL;
6165870Seric 	register char *p;
6265870Seric 	MCI mcibuf;
63297Seric 	char buf[MAXLINE+1];
64297Seric 	extern struct passwd *getpwnam();
65297Seric 	extern char *ttypath();
665846Seric 	typedef int (*fnptr)();
6764945Seric 	extern bool writable();
68297Seric 
697676Seric 	if (tTd(6, 1))
7058680Seric 	{
7166317Seric 		printf("\nsavemail, errormode = %c, id = %s, ExitStat = %d\n  e_from=",
7266317Seric 			e->e_errormode, e->e_id == NULL ? "NONE" : e->e_id,
7366317Seric 			ExitStat);
7458680Seric 		printaddr(&e->e_from, FALSE);
7558680Seric 	}
767361Seric 
7759101Seric 	if (e->e_id == NULL)
7859101Seric 	{
7959101Seric 		/* can't return a message with no id */
8059101Seric 		return;
8159101Seric 	}
8259101Seric 
83297Seric 	/*
84297Seric 	**  In the unhappy event we don't know who to return the mail
85297Seric 	**  to, make someone up.
86297Seric 	*/
87297Seric 
889337Seric 	if (e->e_from.q_paddr == NULL)
89297Seric 	{
9058733Seric 		e->e_sender = "Postmaster";
9164284Seric 		if (parseaddr(e->e_sender, &e->e_from,
9264284Seric 			      RF_COPYPARSE|RF_SENDERADDR, '\0', NULL, e) == NULL)
93297Seric 		{
9458733Seric 			syserr("553 Cannot parse Postmaster!");
95297Seric 			ExitStat = EX_SOFTWARE;
96297Seric 			finis();
97297Seric 		}
98297Seric 	}
999337Seric 	e->e_to = NULL;
100297Seric 
101297Seric 	/*
10224942Seric 	**  Basic state machine.
10324942Seric 	**
10424942Seric 	**	This machine runs through the following states:
10524942Seric 	**
10624942Seric 	**	ESM_QUIET	Errors have already been printed iff the
10724942Seric 	**			sender is local.
10824942Seric 	**	ESM_REPORT	Report directly to the sender's terminal.
10924942Seric 	**	ESM_MAIL	Mail response to the sender.
11024942Seric 	**	ESM_DEADLETTER	Save response in ~/dead.letter.
11124942Seric 	**	ESM_POSTMASTER	Mail response to the postmaster.
11224942Seric 	**	ESM_PANIC	Save response anywhere possible.
113297Seric 	*/
114297Seric 
11524942Seric 	/* determine starting state */
11658734Seric 	switch (e->e_errormode)
117297Seric 	{
11824942Seric 	  case EM_WRITE:
11924942Seric 		state = ESM_REPORT;
12024942Seric 		break;
12124942Seric 
12224942Seric 	  case EM_BERKNET:
12324942Seric 		/* mail back, but return o.k. exit status */
124401Seric 		ExitStat = EX_OK;
12524942Seric 
12624942Seric 		/* fall through.... */
12724942Seric 
12824942Seric 	  case EM_MAIL:
12924942Seric 		state = ESM_MAIL;
13024942Seric 		break;
13124942Seric 
13224942Seric 	  case EM_PRINT:
13324979Seric 	  case '\0':
13424942Seric 		state = ESM_QUIET;
13524942Seric 		break;
13624942Seric 
13724942Seric 	  case EM_QUIET:
13824942Seric 		/* no need to return anything at all */
13924942Seric 		return;
14024979Seric 
14124979Seric 	  default:
14258734Seric 		syserr("554 savemail: bogus errormode x%x\n", e->e_errormode);
14324979Seric 		state = ESM_MAIL;
14424979Seric 		break;
145297Seric 	}
146297Seric 
14759094Seric 	/* if this is already an error response, send to postmaster */
14859094Seric 	if (bitset(EF_RESPONSE, e->e_flags))
14959094Seric 	{
15059094Seric 		if (e->e_parent != NULL &&
15159094Seric 		    bitset(EF_RESPONSE, e->e_parent->e_flags))
15259094Seric 		{
15359094Seric 			/* got an error sending a response -- can it */
15459094Seric 			return;
15559094Seric 		}
15659094Seric 		state = ESM_POSTMASTER;
15759094Seric 	}
15859094Seric 
15924942Seric 	while (state != ESM_DONE)
160297Seric 	{
16124979Seric 		if (tTd(6, 5))
16224979Seric 			printf("  state %d\n", state);
16324979Seric 
16424942Seric 		switch (state)
165297Seric 		{
16624979Seric 		  case ESM_QUIET:
16767473Seric 			if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags))
16824979Seric 				state = ESM_DEADLETTER;
16924979Seric 			else
17024979Seric 				state = ESM_MAIL;
17124979Seric 			break;
17224979Seric 
17324942Seric 		  case ESM_REPORT:
17424942Seric 
17524942Seric 			/*
17624942Seric 			**  If the user is still logged in on the same terminal,
17724942Seric 			**  then write the error messages back to hir (sic).
17824942Seric 			*/
17924942Seric 
18024942Seric 			p = ttypath();
18124942Seric 			if (p == NULL || freopen(p, "w", stdout) == NULL)
18224942Seric 			{
18324942Seric 				state = ESM_MAIL;
18424942Seric 				break;
18524942Seric 			}
18624942Seric 
18768529Seric 			expand("\201n", buf, sizeof buf, e);
1889375Seric 			printf("\r\nMessage from %s...\r\n", buf);
1899375Seric 			printf("Errors occurred while sending mail.\r\n");
1909542Seric 			if (e->e_xfp != NULL)
1919375Seric 			{
1929542Seric 				(void) fflush(e->e_xfp);
19324942Seric 				fp = fopen(queuename(e, 'x'), "r");
1949375Seric 			}
1959375Seric 			else
19624942Seric 				fp = NULL;
19724942Seric 			if (fp == NULL)
1989375Seric 			{
1999337Seric 				syserr("Cannot open %s", queuename(e, 'x'));
2009375Seric 				printf("Transcript of session is unavailable.\r\n");
2019375Seric 			}
2029375Seric 			else
2039375Seric 			{
2049375Seric 				printf("Transcript follows:\r\n");
20524942Seric 				while (fgets(buf, sizeof buf, fp) != NULL &&
2069375Seric 				       !ferror(stdout))
2079375Seric 					fputs(buf, stdout);
20858680Seric 				(void) xfclose(fp, "savemail transcript", e->e_id);
2099375Seric 			}
21024942Seric 			printf("Original message will be saved in dead.letter.\r\n");
21124942Seric 			state = ESM_DEADLETTER;
21224942Seric 			break;
213297Seric 
21424942Seric 		  case ESM_MAIL:
21524942Seric 			/*
21624942Seric 			**  If mailing back, do it.
21724942Seric 			**	Throw away all further output.  Don't alias,
21824942Seric 			**	since this could cause loops, e.g., if joe
21924942Seric 			**	mails to joe@x, and for some reason the network
22024942Seric 			**	for @x is down, then the response gets sent to
22124942Seric 			**	joe@x, which gives a response, etc.  Also force
22224942Seric 			**	the mail to be delivered even if a version of
22324942Seric 			**	it has already been sent to the sender.
22463841Seric 			**
22563841Seric 			**  If this is a configuration or local software
22663841Seric 			**	error, send to the local postmaster as well,
22763841Seric 			**	since the originator can't do anything
22863841Seric 			**	about it anyway.  Note that this is a full
22963841Seric 			**	copy of the message (intentionally) so that
23063841Seric 			**	the Postmaster can forward things along.
23124942Seric 			*/
232297Seric 
23363841Seric 			if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE)
23463841Seric 			{
23563841Seric 				(void) sendtolist("postmaster",
23667982Seric 					  NULLADDR, &e->e_errorqueue, 0, e);
23763841Seric 			}
23867939Seric 			if (!emptyaddr(&e->e_from))
23963841Seric 			{
24058680Seric 				(void) sendtolist(e->e_from.q_paddr,
24167982Seric 					  NULLADDR, &e->e_errorqueue, 0, e);
24263841Seric 			}
24358680Seric 
24463841Seric 			/*
24563841Seric 			**  Deliver a non-delivery report to the
24663841Seric 			**  Postmaster-designate (not necessarily
24763841Seric 			**  Postmaster).  This does not include the
24863841Seric 			**  body of the message, for privacy reasons.
24963841Seric 			**  You really shouldn't need this.
25063841Seric 			*/
25163841Seric 
25263849Seric 			e->e_flags |= EF_PM_NOTIFY;
25358178Seric 
25464792Seric 			/* check to see if there are any good addresses */
25564792Seric 			for (q = e->e_errorqueue; q != NULL; q = q->q_next)
25664792Seric 				if (!bitset(QBADADDR|QDONTSEND, q->q_flags))
25764792Seric 					break;
25858680Seric 			if (q == NULL)
25958680Seric 			{
26058680Seric 				/* this is an error-error */
26158680Seric 				state = ESM_POSTMASTER;
26258680Seric 				break;
26358680Seric 			}
26466303Seric 			if (returntosender(e->e_message, e->e_errorqueue,
26567981Seric 					   sendbody, e) == 0)
26658680Seric 			{
26758680Seric 				state = ESM_DONE;
26858680Seric 				break;
26958680Seric 			}
27024981Seric 
27158680Seric 			/* didn't work -- return to postmaster */
27258680Seric 			state = ESM_POSTMASTER;
27358680Seric 			break;
27458306Seric 
27558680Seric 		  case ESM_POSTMASTER:
27658680Seric 			/*
27758680Seric 			**  Similar to previous case, but to system postmaster.
27858680Seric 			*/
27958680Seric 
28059432Seric 			q = NULL;
28167982Seric 			if (sendtolist("postmaster", NULL, &q, 0, e) <= 0)
28224942Seric 			{
28358680Seric 				syserr("553 cannot parse postmaster!");
28458680Seric 				ExitStat = EX_SOFTWARE;
28558680Seric 				state = ESM_USRTMP;
28658680Seric 				break;
28724942Seric 			}
28867981Seric 			if (returntosender(e->e_message, q, sendbody, e) == 0)
28924942Seric 			{
29024942Seric 				state = ESM_DONE;
29124942Seric 				break;
29224942Seric 			}
293297Seric 
29458680Seric 			/* didn't work -- last resort */
29558680Seric 			state = ESM_USRTMP;
29624942Seric 			break;
297297Seric 
29824942Seric 		  case ESM_DEADLETTER:
29924942Seric 			/*
30024942Seric 			**  Save the message in dead.letter.
30124942Seric 			**	If we weren't mailing back, and the user is
30224942Seric 			**	local, we should save the message in
30324942Seric 			**	~/dead.letter so that the poor person doesn't
30424942Seric 			**	have to type it over again -- and we all know
30524942Seric 			**	what poor typists UNIX users are.
30624942Seric 			*/
3075315Seric 
30824942Seric 			p = NULL;
30967473Seric 			if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
31024942Seric 			{
31124942Seric 				if (e->e_from.q_home != NULL)
31224942Seric 					p = e->e_from.q_home;
31324942Seric 				else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
31424942Seric 					p = pw->pw_dir;
31524942Seric 			}
31624942Seric 			if (p == NULL)
31724942Seric 			{
31858865Seric 				/* no local directory */
31924942Seric 				state = ESM_MAIL;
32024942Seric 				break;
32124942Seric 			}
32224942Seric 			if (e->e_dfp != NULL)
32324942Seric 			{
32424942Seric 				bool oldverb = Verbose;
32524942Seric 
32624942Seric 				/* we have a home directory; open dead.letter */
32724942Seric 				define('z', p, e);
32868529Seric 				expand("\201z/dead.letter", buf, sizeof buf, e);
32924942Seric 				Verbose = TRUE;
33058151Seric 				message("Saving message in %s", buf);
33124942Seric 				Verbose = oldverb;
33224942Seric 				e->e_to = buf;
33324942Seric 				q = NULL;
33467982Seric 				(void) sendtolist(buf, &e->e_from, &q, 0, e);
33564354Seric 				if (q != NULL &&
33664354Seric 				    !bitset(QBADADDR, q->q_flags) &&
33764354Seric 				    deliver(e, q) == 0)
33824942Seric 					state = ESM_DONE;
33924942Seric 				else
34024942Seric 					state = ESM_MAIL;
34124942Seric 			}
34225569Seric 			else
34325569Seric 			{
34425569Seric 				/* no data file -- try mailing back */
34525569Seric 				state = ESM_MAIL;
34625569Seric 			}
34724942Seric 			break;
34824942Seric 
34924942Seric 		  case ESM_USRTMP:
35024942Seric 			/*
35124942Seric 			**  Log the mail in /usr/tmp/dead.letter.
35224942Seric 			*/
35324942Seric 
35457438Seric 			if (e->e_class < 0)
35557438Seric 			{
35657438Seric 				state = ESM_DONE;
35757438Seric 				break;
35857438Seric 			}
35957438Seric 
36068490Seric 			if (SafeFileEnv != NULL && SafeFileEnv[0] != '\0')
36168490Seric 			{
36268490Seric 				state = ESM_PANIC;
36368490Seric 				break;
36468490Seric 			}
36568490Seric 
36665174Seric 			strcpy(buf, _PATH_VARTMP);
36765174Seric 			strcat(buf, "dead.letter");
36868494Seric 			if (!writable(buf, NULLADDR, SFF_NOSLINK|SFF_CREAT))
36964945Seric 			{
37064945Seric 				state = ESM_PANIC;
37164945Seric 				break;
37264945Seric 			}
37368494Seric 			fp = safefopen(buf, O_WRONLY|O_CREAT|O_APPEND,
37468513Seric 					FileMode, SFF_NOSLINK|SFF_REGONLY);
37524942Seric 			if (fp == NULL)
37624942Seric 			{
37724942Seric 				state = ESM_PANIC;
37824942Seric 				break;
37924942Seric 			}
38024942Seric 
38165870Seric 			bzero(&mcibuf, sizeof mcibuf);
38265870Seric 			mcibuf.mci_out = fp;
38365870Seric 			mcibuf.mci_mailer = FileMailer;
38465870Seric 			if (bitnset(M_7BITS, FileMailer->m_flags))
38565870Seric 				mcibuf.mci_flags |= MCIF_7BIT;
38665870Seric 
38765870Seric 			putfromline(&mcibuf, e);
38868228Seric 			(*e->e_puthdr)(&mcibuf, e->e_header, e);
38968228Seric 			(*e->e_putbody)(&mcibuf, e, NULL);
39065870Seric 			putline("\n", &mcibuf);
39124942Seric 			(void) fflush(fp);
39224942Seric 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
39367809Seric 			(void) xfclose(fp, "savemail", buf);
39424942Seric 			break;
39524942Seric 
39624942Seric 		  default:
39758151Seric 			syserr("554 savemail: unknown state %d", state);
39824942Seric 
39924942Seric 			/* fall through ... */
40024942Seric 
40124942Seric 		  case ESM_PANIC:
40224942Seric 			/* leave the locked queue & transcript files around */
40368490Seric 			loseqfile(e, "savemail panic");
40464949Seric 			syserr("!554 savemail: cannot save rejected email anywhere");
40524942Seric 		}
406297Seric 	}
407297Seric }
408297Seric /*
4094633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
4104633Seric **
4114633Seric **	Parameters:
4124633Seric **		msg -- the explanatory message.
41316479Seric **		returnq -- the queue of people to send the message to.
4145984Seric **		sendbody -- if TRUE, also send back the body of the
4155984Seric **			message; otherwise just send the header.
41655012Seric **		e -- the current envelope.
4174633Seric **
4184633Seric **	Returns:
4194633Seric **		zero -- if everything went ok.
4204633Seric **		else -- some error.
4214633Seric **
4224633Seric **	Side Effects:
4234633Seric **		Returns the current message to the sender via
4244633Seric **		mail.
4254633Seric */
4264633Seric 
4277045Seric #define MAXRETURNS	6	/* max depth of returning messages */
42858559Seric #define ERRORFUDGE	100	/* nominal size of error message text */
4297045Seric 
43055012Seric returntosender(msg, returnq, sendbody, e)
4314633Seric 	char *msg;
43216479Seric 	ADDRESS *returnq;
4335984Seric 	bool sendbody;
43455012Seric 	register ENVELOPE *e;
4354633Seric {
43668528Seric 	char buf[MAXNAME + 1];
4376978Seric 	extern putheader(), errbody();
4386978Seric 	register ENVELOPE *ee;
43958680Seric 	ENVELOPE *oldcur = CurEnv;
4406978Seric 	ENVELOPE errenvelope;
4417045Seric 	static int returndepth;
4429375Seric 	register ADDRESS *q;
44368228Seric 	char *p;
4444633Seric 
44560010Seric 	if (returnq == NULL)
44660010Seric 		return (-1);
44760010Seric 
44858966Seric 	if (msg == NULL)
44958966Seric 		msg = "Unable to deliver mail";
45058966Seric 
4517676Seric 	if (tTd(6, 1))
4527287Seric 	{
45367987Seric 		printf("\n*** Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
45455012Seric 		       msg, returndepth, e);
45516479Seric 		printaddr(returnq, TRUE);
45667987Seric 		if (tTd(6, 20))
45767987Seric 		{
45867987Seric 			printf("Sendq=");
45967987Seric 			printaddr(e->e_sendqueue, TRUE);
46067987Seric 		}
4617287Seric 	}
4627287Seric 
4637045Seric 	if (++returndepth >= MAXRETURNS)
4647045Seric 	{
4657045Seric 		if (returndepth != MAXRETURNS)
46658151Seric 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
4677045Seric 		/* don't "unrecurse" and fake a clean exit */
4687045Seric 		/* returndepth--; */
4697045Seric 		return (0);
4707045Seric 	}
4717045Seric 
47258680Seric 	define('g', e->e_from.q_paddr, e);
47364940Seric 	define('u', NULL, e);
47467880Seric 
47567880Seric 	/* initialize error envelope */
47658179Seric 	ee = newenvelope(&errenvelope, e);
47758050Seric 	define('a', "\201b", ee);
47859057Seric 	define('r', "internal", ee);
47959057Seric 	define('s', "localhost", ee);
48059057Seric 	define('_', "localhost", ee);
4816978Seric 	ee->e_puthdr = putheader;
4826978Seric 	ee->e_putbody = errbody;
48364120Seric 	ee->e_flags |= EF_RESPONSE|EF_METOO;
48455012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
48545155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
48616479Seric 	ee->e_sendqueue = returnq;
48764655Seric 	ee->e_msgsize = ERRORFUDGE;
48868559Seric 	if (sendbody)
48964655Seric 		ee->e_msgsize += e->e_msgsize;
49064737Seric 	initsys(ee);
49116479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4929375Seric 	{
49359989Seric 		if (bitset(QBADADDR, q->q_flags))
49458559Seric 			continue;
49558559Seric 
49668141Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
49768141Seric 		{
49868141Seric 			register ADDRESS *p;
49968141Seric 
50068141Seric 			parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e);
50168141Seric 			for (p = returnq; p != NULL; p = p->q_next)
50268141Seric 			{
50368141Seric 				if (p != q && sameaddr(p, q))
50468141Seric 					q->q_flags |= QDONTSEND;
50568141Seric 			}
50668141Seric 		}
50768141Seric 
50859989Seric 		if (!bitset(QDONTSEND, q->q_flags))
50959989Seric 			ee->e_nrcpts++;
51058559Seric 
5119375Seric 		if (q->q_alias == NULL)
51267546Seric 			addheader("To", q->q_paddr, &ee->e_header);
5139375Seric 	}
51424942Seric 
51557642Seric # ifdef LOG
51658020Seric 	if (LogLevel > 5)
51765054Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
51857642Seric 			e->e_id, ee->e_id, msg);
51957642Seric # endif
52057642Seric 
52168003Seric 	if (SendMIMEErrors)
52267261Seric 	{
52368003Seric 		addheader("MIME-Version", "1.0", &ee->e_header);
52468003Seric 		(void) sprintf(buf, "%s.%ld/%s",
52568003Seric 			ee->e_id, curtime(), MyHostName);
52668003Seric 		ee->e_msgboundary = newstr(buf);
52768003Seric 		(void) sprintf(buf,
52868028Seric #ifdef DSN
52968085Seric 			"multipart/report; report-type=X-delivery-status-1; boundary=\"%s\"",
53068028Seric #else
53168028Seric 			"multipart/mixed; boundary=\"%s\"",
53268028Seric #endif
53368003Seric 			ee->e_msgboundary);
53468003Seric 		addheader("Content-Type", buf, &ee->e_header);
53568003Seric 	}
53668228Seric 	if (strncmp(msg, "Warning:", 8) == 0)
53768003Seric 	{
53867261Seric 		addheader("Subject", msg, ee);
53968228Seric 		p = "warning-timeout";
54067261Seric 	}
54167429Seric 	else if (strcmp(msg, "Return receipt") == 0)
54267429Seric 	{
54367429Seric 		addheader("Subject", msg, ee);
54468228Seric 		p = "return-receipt";
54567429Seric 	}
54667261Seric 	else
54767261Seric 	{
54867261Seric 		sprintf(buf, "Returned mail: %.*s", sizeof buf - 20, msg);
54967546Seric 		addheader("Subject", buf, &ee->e_header);
55068228Seric 		p = "failure";
55167261Seric 	}
55268228Seric 	(void) sprintf(buf, "auto-generated (%s)", p);
55368228Seric 	addheader("Auto-Submitted", buf, &ee->e_header);
5544633Seric 
5554633Seric 	/* fake up an address header for the from person */
55668529Seric 	expand("\201n", buf, sizeof buf, e);
55764284Seric 	if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL)
5584633Seric 	{
55958151Seric 		syserr("553 Can't parse myself!");
5604633Seric 		ExitStat = EX_SOFTWARE;
5617045Seric 		returndepth--;
5624633Seric 		return (-1);
5634633Seric 	}
56458704Seric 	ee->e_sender = ee->e_from.q_paddr;
5655984Seric 
5666978Seric 	/* push state into submessage */
5676978Seric 	CurEnv = ee;
56858050Seric 	define('f', "\201n", ee);
5699375Seric 	define('x', "Mail Delivery Subsystem", ee);
57058929Seric 	eatheader(ee, TRUE);
5715984Seric 
57263753Seric 	/* mark statistics */
57364284Seric 	markstats(ee, NULLADDR);
57463753Seric 
5756978Seric 	/* actually deliver the error message */
57614876Seric 	sendall(ee, SM_DEFAULT);
5776978Seric 
5786978Seric 	/* restore state */
5797811Seric 	dropenvelope(ee);
58058680Seric 	CurEnv = oldcur;
5817045Seric 	returndepth--;
5826978Seric 
5837045Seric 	/* should check for delivery errors here */
5844633Seric 	return (0);
5854633Seric }
5864633Seric /*
5876978Seric **  ERRBODY -- output the body of an error message.
5886978Seric **
5896978Seric **	Typically this is a copy of the transcript plus a copy of the
5906978Seric **	original offending message.
5916978Seric **
592297Seric **	Parameters:
59365870Seric **		mci -- the mailer connection information.
5949542Seric **		e -- the envelope we are working in.
59567546Seric **		separator -- any possible MIME separator.
59667936Seric **		flags -- to modify the behaviour.
597297Seric **
598297Seric **	Returns:
599297Seric **		none
600297Seric **
601297Seric **	Side Effects:
6026978Seric **		Outputs the body of an error message.
603297Seric */
604297Seric 
60568228Seric errbody(mci, e, separator)
60665870Seric 	register MCI *mci;
6079542Seric 	register ENVELOPE *e;
60867546Seric 	char *separator;
609297Seric {
6106978Seric 	register FILE *xfile;
61159082Seric 	char *p;
61259082Seric 	register ADDRESS *q;
61359082Seric 	bool printheader;
61468559Seric 	bool sendbody;
6153189Seric 	char buf[MAXLINE];
61668228Seric 	extern char *xtextify();
617297Seric 
61867546Seric 	if (bitset(MCIF_INHEADER, mci->mci_flags))
61967546Seric 	{
62067546Seric 		putline("", mci);
62167546Seric 		mci->mci_flags &= ~MCIF_INHEADER;
62267546Seric 	}
62358680Seric 	if (e->e_parent == NULL)
62458680Seric 	{
62558680Seric 		syserr("errbody: null parent");
62665870Seric 		putline("   ----- Original message lost -----\n", mci);
62758680Seric 		return;
62858680Seric 	}
62958680Seric 
6309057Seric 	/*
63159730Seric 	**  Output MIME header.
63259730Seric 	*/
63359730Seric 
63459730Seric 	if (e->e_msgboundary != NULL)
63559730Seric 	{
63665870Seric 		putline("This is a MIME-encapsulated message", mci);
63765870Seric 		putline("", mci);
63859730Seric 		(void) sprintf(buf, "--%s", e->e_msgboundary);
63965870Seric 		putline(buf, mci);
64065870Seric 		putline("", mci);
64159730Seric 	}
64259730Seric 
64359730Seric 	/*
64463852Seric 	**  Output introductory information.
64563852Seric 	*/
64663852Seric 
64764718Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
64864718Seric 		if (bitset(QBADADDR, q->q_flags))
64964718Seric 			break;
65065054Seric 	if (q == NULL &&
65165054Seric 	    !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags))
65264718Seric 	{
65364718Seric 		putline("    **********************************************",
65465870Seric 			mci);
65564718Seric 		putline("    **      THIS IS A WARNING MESSAGE ONLY      **",
65665870Seric 			mci);
65764718Seric 		putline("    **  YOU DO NOT NEED TO RESEND YOUR MESSAGE  **",
65865870Seric 			mci);
65964718Seric 		putline("    **********************************************",
66065870Seric 			mci);
66165870Seric 		putline("", mci);
66264718Seric 	}
66364718Seric 	sprintf(buf, "The original message was received at %s",
66464718Seric 		arpadate(ctime(&e->e_parent->e_ctime)));
66565870Seric 	putline(buf, mci);
66668529Seric 	expand("from \201_", buf, sizeof buf, e->e_parent);
66765870Seric 	putline(buf, mci);
66865870Seric 	putline("", mci);
66963852Seric 
67063852Seric 	/*
67155372Seric 	**  Output error message header (if specified and available).
67255372Seric 	*/
67355372Seric 
67467682Seric 	if (ErrMsgFile != NULL && !bitset(EF_SENDRECEIPT, e->e_parent->e_flags))
67555372Seric 	{
67655372Seric 		if (*ErrMsgFile == '/')
67755372Seric 		{
67855372Seric 			xfile = fopen(ErrMsgFile, "r");
67955372Seric 			if (xfile != NULL)
68055372Seric 			{
68155372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
68255425Seric 				{
68368529Seric 					expand(buf, buf, sizeof buf, e);
68465870Seric 					putline(buf, mci);
68555425Seric 				}
68655372Seric 				(void) fclose(xfile);
68765870Seric 				putline("\n", mci);
68855372Seric 			}
68955372Seric 		}
69055372Seric 		else
69155372Seric 		{
69268529Seric 			expand(ErrMsgFile, buf, sizeof buf, e);
69365870Seric 			putline(buf, mci);
69465870Seric 			putline("", mci);
69555372Seric 		}
69655372Seric 	}
69755372Seric 
69855372Seric 	/*
69959082Seric 	**  Output message introduction
70059082Seric 	*/
70159082Seric 
70259082Seric 	printheader = TRUE;
70359082Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
70459082Seric 	{
705*68603Seric 		if (bitset(QBADADDR|QREPORT|QRELAYED|QEXPLODED, q->q_flags))
70659082Seric 		{
70763849Seric 			strcpy(buf, q->q_paddr);
70863787Seric 			if (bitset(QBADADDR, q->q_flags))
70963849Seric 				strcat(buf, "  (unrecoverable error)");
710*68603Seric 			else if (!bitset(QPRIMARY, q->q_flags))
711*68603Seric 				continue;
71267981Seric 			else if (bitset(QRELAYED, q->q_flags))
71367981Seric 				strcat(buf, "  (relayed to non-DSN-aware mailer)");
71467880Seric 			else if (bitset(QSENT, q->q_flags))
71567880Seric 				strcat(buf, "  (successfully delivered)");
716*68603Seric 			else if (bitset(QEXPLODED, q->q_flags))
717*68603Seric 				strcat(buf, "  (expanded by mailing list)");
71863787Seric 			else
71963849Seric 				strcat(buf, "  (transient failure)");
720*68603Seric 			if (printheader)
721*68603Seric 			{
722*68603Seric 				putline("   ----- The following addresses have delivery notifications -----",
723*68603Seric 					mci);
724*68603Seric 				printheader = FALSE;
725*68603Seric 			}
72665870Seric 			putline(buf, mci);
72763849Seric 			if (q->q_alias != NULL)
72863849Seric 			{
72963849Seric 				strcpy(buf, "    (expanded from: ");
73063849Seric 				strcat(buf, q->q_alias->q_paddr);
73163849Seric 				strcat(buf, ")");
73265870Seric 				putline(buf, mci);
73363849Seric 			}
73459082Seric 		}
73559082Seric 	}
73659082Seric 	if (!printheader)
73765870Seric 		putline("\n", mci);
73859082Seric 
73959082Seric 	/*
7409057Seric 	**  Output transcript of errors
7419057Seric 	*/
7429057Seric 
7434086Seric 	(void) fflush(stdout);
7449542Seric 	p = queuename(e->e_parent, 'x');
7459337Seric 	if ((xfile = fopen(p, "r")) == NULL)
7469057Seric 	{
7479337Seric 		syserr("Cannot open %s", p);
74865870Seric 		putline("   ----- Transcript of session is unavailable -----\n", mci);
7499057Seric 	}
7509057Seric 	else
7519057Seric 	{
75265870Seric 		putline("   ----- Transcript of session follows -----\n", mci);
7539542Seric 		if (e->e_xfp != NULL)
7549542Seric 			(void) fflush(e->e_xfp);
7559057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
75665870Seric 			putline(buf, mci);
75758680Seric 		(void) xfclose(xfile, "errbody xscript", p);
7589057Seric 	}
759297Seric 	errno = 0;
7604318Seric 
76168028Seric #ifdef DSN
7624318Seric 	/*
76367880Seric 	**  Output machine-readable version.
76467880Seric 	*/
76567880Seric 
76667880Seric 	if (e->e_msgboundary != NULL)
76767880Seric 	{
76867880Seric 		putline("", mci);
76967880Seric 		(void) sprintf(buf, "--%s", e->e_msgboundary);
77067880Seric 		putline(buf, mci);
77168228Seric 		putline("Content-Type: message/X-delivery-status-2 (Draft of 20 January 1995)", mci);
77267880Seric 		putline("", mci);
77367880Seric 
77467880Seric 		/*
77567880Seric 		**  Output per-message information.
77667880Seric 		*/
77767880Seric 
77867880Seric 		/* original envelope id from MAIL FROM: line */
77967880Seric 		if (e->e_parent->e_envid != NULL)
78067880Seric 		{
78167880Seric 			(void) sprintf(buf, "Original-Envelope-Id: %s",
78268228Seric 				xtextify(e->e_parent->e_envid));
78367880Seric 			putline(buf, mci);
78467880Seric 		}
78567880Seric 
78668228Seric 		/* Reporting-MTA: is us (required) */
78768228Seric 		p = e->e_parent->e_from.q_mailer->m_mtatype;
78868228Seric 		if (p == NULL)
78968228Seric 			p = "dns";
79068583Seric 		(void) sprintf(buf, "Reporting-MTA: %s; %s", p,
79168583Seric 			xtextify(MyHostName));
79267880Seric 		putline(buf, mci);
79367880Seric 
79468228Seric 		/* Received-From-MTA: shows where we got this message from */
79567990Seric 		if (RealHostName != NULL)
79667990Seric 		{
79768228Seric 			/* XXX use $s for type? */
79868228Seric 			p = e->e_parent->e_from.q_mailer->m_mtatype;
79968228Seric 			if (p == NULL)
80068228Seric 				p = "dns";
80168228Seric 			(void) sprintf(buf, "Received-From-MTA: %s; %s",
80268583Seric 				p, xtextify(RealHostName));
80367990Seric 			putline(buf, mci);
80467990Seric 		}
80567963Seric 
80667963Seric 		/* Arrival-Date: -- when it arrived here */
80767963Seric 		(void) sprintf(buf, "Arrival-Date: %s",
80867963Seric 			arpadate(ctime(&e->e_parent->e_ctime)));
80967963Seric 		putline(buf, mci);
81067963Seric 
81167880Seric 		/*
81267880Seric 		**  Output per-address information.
81367880Seric 		*/
81467880Seric 
81567880Seric 		for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
81667880Seric 		{
81767880Seric 			register ADDRESS *r;
818*68603Seric 			char *action;
81967880Seric 
820*68603Seric 			if (bitset(QBADADDR, q->q_flags))
821*68603Seric 				action = "failure";
822*68603Seric 			else if (!bitset(QPRIMARY, q->q_flags))
82367880Seric 				continue;
824*68603Seric 			else if (bitset(QRELAYED, q->q_flags))
825*68603Seric 				action = "relayed";
826*68603Seric 			else if (bitset(QEXPLODED, q->q_flags))
827*68603Seric 				action = "delivered (to mailing list)";
828*68603Seric 			else if (bitset(QSENT, q->q_flags) &&
829*68603Seric 				 bitnset(M_LOCALMAILER, q->q_mailer->m_flags))
830*68603Seric 				action = "delivered (final delivery)";
831*68603Seric 			else if (bitset(QREPORT, q->q_flags))
832*68603Seric 				action = "delayed";
833*68603Seric 			else
834*68603Seric 				continue;
835*68603Seric 
83667880Seric 			putline("", mci);
83767880Seric 
83868228Seric 			/* Original-Recipient: -- passed from on high */
83968228Seric 			if (q->q_orcpt != NULL)
84068228Seric 			{
84168228Seric 				(void) sprintf(buf, "Original-Recipient: %s",
84268228Seric 					xtextify(q->q_orcpt));
84368228Seric 				putline(buf, mci);
84468228Seric 			}
84568228Seric 
84668228Seric 			/* Final-Recipient: -- the name from the RCPT command */
84768228Seric 			p = e->e_parent->e_from.q_mailer->m_addrtype;
84868228Seric 			if (p == NULL)
84968228Seric 				p = "rfc822";
85068228Seric 			for (r = q; r->q_alias != NULL; r = r->q_alias)
85168228Seric 				continue;
85268228Seric 			if (strchr(r->q_user, '@') == NULL)
85368583Seric 			{
85468583Seric 				(void) sprintf(buf, "Final-Recipient: %s; %s@",
85568583Seric 					p, xtextify(r->q_user));
85668583Seric 				strcat(buf, xtextify(MyHostName));
85768583Seric 			}
85867998Seric 			else
85968583Seric 			{
86068228Seric 				(void) sprintf(buf, "Final-Recipient: %s; %s",
86168228Seric 					p, xtextify(r->q_user));
86268583Seric 			}
86367880Seric 			putline(buf, mci);
86467880Seric 
865*68603Seric 			/* X-Actual-Recipient: -- the real problem address */
866*68603Seric 			if (r != q)
867*68603Seric 			{
868*68603Seric 				if (strchr(q->q_user, '@') == NULL)
869*68603Seric 				{
870*68603Seric 					(void) sprintf(buf, "X-Actual-Recipient: %s; %s@",
871*68603Seric 						p, xtextify(q->q_user));
872*68603Seric 					strcat(buf, xtextify(MyHostName));
873*68603Seric 				}
874*68603Seric 				else
875*68603Seric 				{
876*68603Seric 					(void) sprintf(buf, "X-Actual-Recipient: %s; %s",
877*68603Seric 						p, xtextify(q->q_user));
878*68603Seric 				}
879*68603Seric 				putline(buf, mci);
880*68603Seric 			}
881*68603Seric 
88267880Seric 			/* Action: -- what happened? */
883*68603Seric 			sprintf(buf, "Action: %s", action);
884*68603Seric 			putline(buf, mci);
88567880Seric 
88667880Seric 			/* Status: -- what _really_ happened? */
88767880Seric 			strcpy(buf, "Status: ");
88867880Seric 			if (q->q_status != NULL)
88967880Seric 				strcat(buf, q->q_status);
89067880Seric 			else if (bitset(QBADADDR, q->q_flags))
89168228Seric 				strcat(buf, "5.0.0");
89267880Seric 			else if (bitset(QQUEUEUP, q->q_flags))
89368228Seric 				strcat(buf, "4.0.0");
89467880Seric 			else
89568228Seric 				strcat(buf, "2.0.0");
89667880Seric 			putline(buf, mci);
89767880Seric 
89868228Seric 			/* Remote-MTA: -- who was I talking to? */
89968228Seric 			p = q->q_mailer->m_mtatype;
90068228Seric 			if (p == NULL)
90168228Seric 				p = "dns";
90268228Seric 			(void) sprintf(buf, "Remote-MTA: %s; ", p);
90368228Seric 			if (q->q_statmta != NULL)
90468228Seric 				p = q->q_statmta;
905*68603Seric 			else if (q->q_host != NULL && q->q_host[0] != '\0')
90668228Seric 				p = q->q_host;
90768228Seric 			else
90868228Seric 				p = NULL;
90968228Seric 			if (p != NULL)
91068228Seric 			{
91168228Seric 				strcat(buf, p);
91268228Seric 				p = &buf[strlen(buf) - 1];
91368228Seric 				if (*p == '.')
91468228Seric 					*p = '\0';
91568228Seric 				putline(buf, mci);
91668228Seric 			}
91768228Seric 
91868228Seric 			/* Diagnostic-Code: -- actual result from other end */
91968228Seric 			if (q->q_rstatus != NULL)
92068228Seric 			{
92168228Seric 				p = q->q_mailer->m_diagtype;
92268228Seric 				if (p == NULL)
92368228Seric 					p = "smtp";
92468228Seric 				(void) sprintf(buf, "Diagnostic-Code: %s; %s",
925*68603Seric 					p, q->q_rstatus);
92668228Seric 				putline(buf, mci);
92768228Seric 			}
92868228Seric 
92968228Seric 			/* Last-Attempt-Date: -- fine granularity */
93067880Seric 			if (q->q_statdate == (time_t) 0L)
93167880Seric 				q->q_statdate = curtime();
93268228Seric 			(void) sprintf(buf, "Last-Attempt-Date: %s",
93367880Seric 				arpadate(ctime(&q->q_statdate)));
93467880Seric 			putline(buf, mci);
93567880Seric 
93667963Seric 			/* Expiry-Date: -- for delayed messages only */
93767963Seric 			if (bitset(QQUEUEUP, q->q_flags) &&
93867963Seric 			    !bitset(QBADADDR, q->q_flags))
93967963Seric 			{
94067963Seric 				time_t xdate;
94167963Seric 
94267963Seric 				xdate = e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass];
94367963Seric 				sprintf(buf, "Expiry-Date: %s",
94467963Seric 					arpadate(ctime(&xdate)));
94567963Seric 				putline(buf, mci);
94667963Seric 			}
94767880Seric 		}
94867880Seric 	}
94968028Seric #endif
95067880Seric 
95167880Seric 	/*
9524318Seric 	**  Output text of original message
9534318Seric 	*/
9544318Seric 
95565870Seric 	putline("", mci);
95668564Seric 	if (bitset(EF_HAS_DF, e->e_parent->e_flags))
9574199Seric 	{
95868559Seric 		sendbody = !bitset(EF_NO_BODY_RETN, e->e_parent->e_flags);
95968559Seric 
96067880Seric 		if (e->e_msgboundary == NULL)
96167880Seric 		{
96268559Seric 			if (sendbody)
96367880Seric 				putline("   ----- Original message follows -----\n", mci);
96467880Seric 			else
96567880Seric 				putline("   ----- Message header follows -----\n", mci);
96667880Seric 			(void) fflush(mci->mci_out);
96767880Seric 		}
9685984Seric 		else
9695984Seric 		{
97059730Seric 			(void) sprintf(buf, "--%s", e->e_msgboundary);
97165870Seric 			putline(buf, mci);
97268228Seric 			(void) sprintf(buf, "Content-Type: message/rfc822%s",
97368559Seric 				mci, sendbody ? "" : "-headers");
97468228Seric 			putline(buf, mci);
9755984Seric 		}
97667981Seric 		putline("", mci);
97768228Seric 		putheader(mci, e->e_parent->e_header, e->e_parent);
97868559Seric 		if (sendbody)
97968228Seric 			putbody(mci, e->e_parent, e->e_msgboundary);
98068228Seric 		else if (e->e_msgboundary == NULL)
98167546Seric 		{
98267546Seric 			putline("", mci);
98365870Seric 			putline("   ----- Message body suppressed -----", mci);
98467546Seric 		}
9854199Seric 	}
98668228Seric 	else if (e->e_msgboundary == NULL)
98710170Seric 	{
98865870Seric 		putline("  ----- No message was collected -----\n", mci);
98910170Seric 	}
9904318Seric 
99160010Seric 	if (e->e_msgboundary != NULL)
99260010Seric 	{
99365870Seric 		putline("", mci);
99460010Seric 		(void) sprintf(buf, "--%s--", e->e_msgboundary);
99565870Seric 		putline(buf, mci);
99660010Seric 	}
99765870Seric 	putline("", mci);
99859730Seric 
9994318Seric 	/*
10004318Seric 	**  Cleanup and exit
10014318Seric 	*/
10024318Seric 
1003297Seric 	if (errno != 0)
10046978Seric 		syserr("errbody: I/O error");
1005297Seric }
100658144Seric /*
100768228Seric **  SMTPTODSN -- convert SMTP to DSN status code
100868228Seric **
100968228Seric **	Parameters:
101068228Seric **		smtpstat -- the smtp status code (e.g., 550).
101168228Seric **
101268228Seric **	Returns:
101368228Seric **		The DSN version of the status code.
101468228Seric */
101568228Seric 
101668228Seric char *
101768228Seric smtptodsn(smtpstat)
101868228Seric 	int smtpstat;
101968228Seric {
102068228Seric 	switch (smtpstat)
102168228Seric 	{
102268228Seric 	  case 450:	/* Req mail action not taken: mailbox unavailable */
102368228Seric 		return "4.2.0";
102468228Seric 
102568228Seric 	  case 451:	/* Req action aborted: local error in processing */
102668228Seric 		return "4.3.0";
102768228Seric 
102868228Seric 	  case 452:	/* Req action not taken: insufficient sys storage */
102968228Seric 		return "4.3.1";
103068228Seric 
103168228Seric 	  case 500:	/* Syntax error, command unrecognized */
103268228Seric 		return "5.5.2";
103368228Seric 
103468228Seric 	  case 501:	/* Syntax error in parameters or arguments */
103568228Seric 		return "5.5.4";
103668228Seric 
103768228Seric 	  case 502:	/* Command not implemented */
103868228Seric 		return "5.5.1";
103968228Seric 
104068228Seric 	  case 503:	/* Bad sequence of commands */
104168228Seric 		return "5.5.1";
104268228Seric 
104368228Seric 	  case 504:	/* Command parameter not implemented */
104468228Seric 		return "5.5.4";
104568228Seric 
104668228Seric 	  case 550:	/* Req mail action not taken: mailbox unavailable */
104768228Seric 		return "5.2.0";
104868228Seric 
104968228Seric 	  case 551:	/* User not local; please try <...> */
105068228Seric 		return "5.1.6";
105168228Seric 
105268228Seric 	  case 552:	/* Req mail action aborted: exceeded storage alloc */
105368228Seric 		return "5.2.2";
105468228Seric 
105568228Seric 	  case 553:	/* Req action not taken: mailbox name not allowed */
105668228Seric 		return "5.1.3";
105768228Seric 
105868228Seric 	  case 554:	/* Transaction failed */
105968228Seric 		return "5.0.0";
106068228Seric 	}
106168228Seric 
106268228Seric 	if ((smtpstat / 100) == 2)
106368228Seric 		return "2.0.0";
106468228Seric 	if ((smtpstat / 100) == 4)
106568228Seric 		return "4.0.0";
106668228Seric 	return "5.0.0";
106768228Seric }
106868228Seric /*
106968228Seric **  XTEXTIFY -- take regular text and turn it into DSN-style xtext
107068228Seric **
107168228Seric **	Parameters:
107268228Seric **		t -- the text to convert.
107368228Seric **
107468228Seric **	Returns:
107568228Seric **		The xtext-ified version of the same string.
107668228Seric */
107768228Seric 
107868228Seric char *
107968228Seric xtextify(t)
108068228Seric 	register char *t;
108168228Seric {
108268228Seric 	register char *p;
108368228Seric 	int l;
108468228Seric 	int nbogus;
108568228Seric 	static char *bp = NULL;
108668228Seric 	static int bplen = 0;
108768228Seric 
108868228Seric 	/* figure out how long this xtext will have to be */
108968228Seric 	nbogus = l = 0;
109068228Seric 	for (p = t; *p != '\0'; p++)
109168228Seric 	{
109268228Seric 		register int c = (*p & 0xff);
109368228Seric 
109468228Seric 		/* ASCII dependence here -- this is the way the spec words it */
1095*68603Seric 		if ((c < ' ' || c > '~' || c == '+' || c == '\\' || c == '(') &&
1096*68603Seric 		    c != '\t')
109768228Seric 			nbogus++;
109868228Seric 		l++;
109968228Seric 	}
110068228Seric 	if (nbogus == 0)
110168228Seric 		return t;
110268228Seric 	l += nbogus * 2 + 1;
110368228Seric 
110468228Seric 	/* now allocate space if necessary for the new string */
110568228Seric 	if (l > bplen)
110668228Seric 	{
110768228Seric 		if (bp != NULL)
110868228Seric 			free(bp);
110968228Seric 		bp = xalloc(l);
111068228Seric 		bplen = l;
111168228Seric 	}
111268228Seric 
111368228Seric 	/* ok, copy the text with byte expansion */
111468228Seric 	for (p = bp; *t != '\0'; )
111568228Seric 	{
111668228Seric 		register int c = (*t++ & 0xff);
111768228Seric 
111868228Seric 		/* ASCII dependence here -- this is the way the spec words it */
111968228Seric 		if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(')
112068228Seric 		{
112168228Seric 			*p++ = '+';
112268228Seric 			*p++ = "0123456789abcdef"[c >> 4];
112368228Seric 			*p++ = "0123456789abcdef"[c & 0xf];
112468228Seric 		}
112568228Seric 		else
112668228Seric 			*p++ = c;
112768228Seric 	}
112868228Seric 	*p = '\0';
112968228Seric 	return bp;
113068228Seric }
113168228Seric /*
113268583Seric **  XTEXTOK -- check if a string is legal xtext
113368583Seric **
113468583Seric **	Xtext is used in Delivery Status Notifications.  The spec was
113568583Seric **	taken from draft-ietf-notary-mime-delivery-04.txt.
113668583Seric **
113768583Seric **	Parameters:
113868583Seric **		s -- the string to check.
113968583Seric **
114068583Seric **	Returns:
114168583Seric **		TRUE -- if 's' is legal xtext.
114268583Seric **		FALSE -- if it has any illegal characters in it.
114368583Seric */
114468583Seric 
114568583Seric bool
114668583Seric xtextok(s)
114768583Seric 	char *s;
114868583Seric {
114968583Seric 	int c;
115068583Seric 
115168583Seric 	while ((c = *s++) != '\0')
115268583Seric 	{
115368583Seric 		if (c == '+')
115468583Seric 		{
115568583Seric 			c = *s++;
115668583Seric 			if (!isascii(c) || !isxdigit(c))
115768583Seric 				return FALSE;
115868583Seric 			c = *s++;
115968583Seric 			if (!isascii(c) || !isxdigit(c))
116068583Seric 				return FALSE;
116168583Seric 		}
116268583Seric 		else if (c < '!' || c > '~' || c == '\\' || c == '(')
116368583Seric 			return FALSE;
116468583Seric 	}
116568583Seric 	return TRUE;
116668583Seric }
116768583Seric /*
116858144Seric **  PRUNEROUTE -- prune an RFC-822 source route
116958144Seric **
117058144Seric **	Trims down a source route to the last internet-registered hop.
117158144Seric **	This is encouraged by RFC 1123 section 5.3.3.
117258144Seric **
117358144Seric **	Parameters:
117458144Seric **		addr -- the address
117558144Seric **
117658144Seric **	Returns:
117758144Seric **		TRUE -- address was modified
117858144Seric **		FALSE -- address could not be pruned
117958144Seric **
118058144Seric **	Side Effects:
118158144Seric **		modifies addr in-place
118258144Seric */
118358144Seric 
118458144Seric pruneroute(addr)
118558144Seric 	char *addr;
118658144Seric {
118766334Seric #if NAMED_BIND
118858144Seric 	char *start, *at, *comma;
118958144Seric 	char c;
119058144Seric 	int rcode;
119158144Seric 	char hostbuf[BUFSIZ];
119258144Seric 	char *mxhosts[MAXMXHOSTS + 1];
119358144Seric 
119458144Seric 	/* check to see if this is really a route-addr */
119558144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
119658144Seric 		return FALSE;
119758144Seric 	start = strchr(addr, ':');
119858144Seric 	at = strrchr(addr, '@');
119958144Seric 	if (start == NULL || at == NULL || at < start)
120058144Seric 		return FALSE;
120158144Seric 
120258144Seric 	/* slice off the angle brackets */
120358144Seric 	strcpy(hostbuf, at + 1);
120458144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
120558144Seric 
120658144Seric 	while (start)
120758144Seric 	{
120859273Seric 		if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0)
120958144Seric 		{
121058144Seric 			strcpy(addr + 1, start + 1);
121158144Seric 			return TRUE;
121258144Seric 		}
121358144Seric 		c = *start;
121458144Seric 		*start = '\0';
121558144Seric 		comma = strrchr(addr, ',');
121658144Seric 		if (comma && comma[1] == '@')
121758144Seric 			strcpy(hostbuf, comma + 2);
121858144Seric 		else
121958144Seric 			comma = 0;
122058144Seric 		*start = c;
122158144Seric 		start = comma;
122258144Seric 	}
122358144Seric #endif
122458144Seric 	return FALSE;
122558144Seric }
1226