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*69748Seric static char sccsid[] = "@(#)savemail.c	8.71 (Berkeley) 05/28/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 
52*69748Seric void
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;
6368802Seric 	int sfflags;
64297Seric 	char buf[MAXLINE+1];
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;
31368693Seric 				else if ((pw = sm_getpwnam(e->e_from.q_user)) != NULL)
31424942Seric 					p = pw->pw_dir;
31524942Seric 			}
31668802Seric 			if (p == NULL || e->e_dfp == NULL)
31724942Seric 			{
31868802Seric 				/* no local directory or no data file */
31924942Seric 				state = ESM_MAIL;
32024942Seric 				break;
32124942Seric 			}
32224942Seric 
32368802Seric 			/* we have a home directory; open dead.letter */
32468802Seric 			define('z', p, e);
32568802Seric 			expand("\201z/dead.letter", buf, sizeof buf, e);
32668802Seric 			sfflags = SFF_NOSLINK|SFF_CREAT|SFF_REGONLY|SFF_RUNASREALUID;
32768802Seric 			e->e_to = buf;
32868802Seric 			goto writefile;
32924942Seric 
33024942Seric 		  case ESM_USRTMP:
33124942Seric 			/*
33224942Seric 			**  Log the mail in /usr/tmp/dead.letter.
33324942Seric 			*/
33424942Seric 
33557438Seric 			if (e->e_class < 0)
33657438Seric 			{
33757438Seric 				state = ESM_DONE;
33857438Seric 				break;
33957438Seric 			}
34057438Seric 
34168490Seric 			if (SafeFileEnv != NULL && SafeFileEnv[0] != '\0')
34268490Seric 			{
34368490Seric 				state = ESM_PANIC;
34468490Seric 				break;
34568490Seric 			}
34668490Seric 
34765174Seric 			strcpy(buf, _PATH_VARTMP);
34865174Seric 			strcat(buf, "dead.letter");
34968802Seric 			sfflags = SFF_NOSLINK|SFF_CREAT|SFF_REGONLY;
35068802Seric 
35168802Seric   writefile:
35268802Seric 			if (!writable(buf, q, sfflags) ||
35368802Seric 			    (fp = safefopen(buf, O_WRONLY|O_CREAT|O_APPEND,
35468802Seric 					    FileMode, sfflags)) == NULL)
35564945Seric 			{
35668802Seric 				if (state == ESM_USRTMP)
35768802Seric 					state = ESM_PANIC;
35868802Seric 				else
35968802Seric 					state = ESM_MAIL;
36064945Seric 				break;
36164945Seric 			}
36224942Seric 
36365870Seric 			bzero(&mcibuf, sizeof mcibuf);
36465870Seric 			mcibuf.mci_out = fp;
36565870Seric 			mcibuf.mci_mailer = FileMailer;
36665870Seric 			if (bitnset(M_7BITS, FileMailer->m_flags))
36765870Seric 				mcibuf.mci_flags |= MCIF_7BIT;
36865870Seric 
36965870Seric 			putfromline(&mcibuf, e);
37068228Seric 			(*e->e_puthdr)(&mcibuf, e->e_header, e);
37168228Seric 			(*e->e_putbody)(&mcibuf, e, NULL);
37265870Seric 			putline("\n", &mcibuf);
37324942Seric 			(void) fflush(fp);
37468802Seric 			if (!ferror(fp))
37568802Seric 			{
37668802Seric 				bool oldverb = Verbose;
37768802Seric 
37868802Seric 				Verbose = TRUE;
37968802Seric 				message("Saved message in %s", buf);
38068802Seric 				Verbose = oldverb;
38168802Seric 				state = ESM_DONE;
38268802Seric 			}
38368802Seric 			else if (state == ESM_USRTMP)
38468802Seric 				state = ESM_PANIC;
38568802Seric 			else
38668802Seric 				state = ESM_MAIL;
38767809Seric 			(void) xfclose(fp, "savemail", buf);
38824942Seric 			break;
38924942Seric 
39024942Seric 		  default:
39158151Seric 			syserr("554 savemail: unknown state %d", state);
39224942Seric 
39324942Seric 			/* fall through ... */
39424942Seric 
39524942Seric 		  case ESM_PANIC:
39624942Seric 			/* leave the locked queue & transcript files around */
39768490Seric 			loseqfile(e, "savemail panic");
39864949Seric 			syserr("!554 savemail: cannot save rejected email anywhere");
39924942Seric 		}
400297Seric 	}
401297Seric }
402297Seric /*
4034633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
4044633Seric **
4054633Seric **	Parameters:
4064633Seric **		msg -- the explanatory message.
40716479Seric **		returnq -- the queue of people to send the message to.
4085984Seric **		sendbody -- if TRUE, also send back the body of the
4095984Seric **			message; otherwise just send the header.
41055012Seric **		e -- the current envelope.
4114633Seric **
4124633Seric **	Returns:
4134633Seric **		zero -- if everything went ok.
4144633Seric **		else -- some error.
4154633Seric **
4164633Seric **	Side Effects:
4174633Seric **		Returns the current message to the sender via
4184633Seric **		mail.
4194633Seric */
4204633Seric 
4217045Seric #define MAXRETURNS	6	/* max depth of returning messages */
42258559Seric #define ERRORFUDGE	100	/* nominal size of error message text */
4237045Seric 
424*69748Seric int
42555012Seric returntosender(msg, returnq, sendbody, e)
4264633Seric 	char *msg;
42716479Seric 	ADDRESS *returnq;
4285984Seric 	bool sendbody;
42955012Seric 	register ENVELOPE *e;
4304633Seric {
4316978Seric 	register ENVELOPE *ee;
43258680Seric 	ENVELOPE *oldcur = CurEnv;
4336978Seric 	ENVELOPE errenvelope;
4347045Seric 	static int returndepth;
4359375Seric 	register ADDRESS *q;
43668228Seric 	char *p;
437*69748Seric 	char buf[MAXNAME + 1];
438*69748Seric 	extern void errbody __P((MCI *, ENVELOPE *, char *));
4394633Seric 
44060010Seric 	if (returnq == NULL)
44160010Seric 		return (-1);
44260010Seric 
44358966Seric 	if (msg == NULL)
44458966Seric 		msg = "Unable to deliver mail";
44558966Seric 
4467676Seric 	if (tTd(6, 1))
4477287Seric 	{
44867987Seric 		printf("\n*** Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
44955012Seric 		       msg, returndepth, e);
45016479Seric 		printaddr(returnq, TRUE);
45167987Seric 		if (tTd(6, 20))
45267987Seric 		{
45367987Seric 			printf("Sendq=");
45467987Seric 			printaddr(e->e_sendqueue, TRUE);
45567987Seric 		}
4567287Seric 	}
4577287Seric 
4587045Seric 	if (++returndepth >= MAXRETURNS)
4597045Seric 	{
4607045Seric 		if (returndepth != MAXRETURNS)
46158151Seric 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
4627045Seric 		/* don't "unrecurse" and fake a clean exit */
4637045Seric 		/* returndepth--; */
4647045Seric 		return (0);
4657045Seric 	}
4667045Seric 
46758680Seric 	define('g', e->e_from.q_paddr, e);
46864940Seric 	define('u', NULL, e);
46967880Seric 
47067880Seric 	/* initialize error envelope */
47158179Seric 	ee = newenvelope(&errenvelope, e);
47258050Seric 	define('a', "\201b", ee);
47359057Seric 	define('r', "internal", ee);
47459057Seric 	define('s', "localhost", ee);
47559057Seric 	define('_', "localhost", ee);
4766978Seric 	ee->e_puthdr = putheader;
4776978Seric 	ee->e_putbody = errbody;
47864120Seric 	ee->e_flags |= EF_RESPONSE|EF_METOO;
47955012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
48045155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
48116479Seric 	ee->e_sendqueue = returnq;
48264655Seric 	ee->e_msgsize = ERRORFUDGE;
48368559Seric 	if (sendbody)
48464655Seric 		ee->e_msgsize += e->e_msgsize;
48568802Seric 	else
48668802Seric 		ee->e_flags |= EF_NO_BODY_RETN;
48764737Seric 	initsys(ee);
48816479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4899375Seric 	{
49059989Seric 		if (bitset(QBADADDR, q->q_flags))
49158559Seric 			continue;
49258559Seric 
49368141Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
49468141Seric 		{
49568141Seric 			register ADDRESS *p;
49668141Seric 
49768141Seric 			parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e);
49868141Seric 			for (p = returnq; p != NULL; p = p->q_next)
49968141Seric 			{
50068141Seric 				if (p != q && sameaddr(p, q))
50168141Seric 					q->q_flags |= QDONTSEND;
50268141Seric 			}
50368141Seric 		}
50468141Seric 
50559989Seric 		if (!bitset(QDONTSEND, q->q_flags))
50659989Seric 			ee->e_nrcpts++;
50758559Seric 
5089375Seric 		if (q->q_alias == NULL)
50967546Seric 			addheader("To", q->q_paddr, &ee->e_header);
5109375Seric 	}
51124942Seric 
51257642Seric # ifdef LOG
51358020Seric 	if (LogLevel > 5)
51465054Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
51557642Seric 			e->e_id, ee->e_id, msg);
51657642Seric # endif
51757642Seric 
51868003Seric 	if (SendMIMEErrors)
51967261Seric 	{
52068003Seric 		addheader("MIME-Version", "1.0", &ee->e_header);
52168003Seric 		(void) sprintf(buf, "%s.%ld/%s",
52268003Seric 			ee->e_id, curtime(), MyHostName);
52368003Seric 		ee->e_msgboundary = newstr(buf);
52468003Seric 		(void) sprintf(buf,
52568848Seric #if DSN
52669545Seric 			"multipart/report; report-type=X-delivery-status-3 (Draft of May 5, 1995);\n\tboundary=\"%s\"",
52768028Seric #else
52868028Seric 			"multipart/mixed; boundary=\"%s\"",
52968028Seric #endif
53068003Seric 			ee->e_msgboundary);
53168003Seric 		addheader("Content-Type", buf, &ee->e_header);
53268003Seric 	}
53368228Seric 	if (strncmp(msg, "Warning:", 8) == 0)
53468003Seric 	{
53568879Seric 		addheader("Subject", msg, &ee->e_header);
53668228Seric 		p = "warning-timeout";
53767261Seric 	}
53867429Seric 	else if (strcmp(msg, "Return receipt") == 0)
53967429Seric 	{
54068879Seric 		addheader("Subject", msg, &ee->e_header);
54168228Seric 		p = "return-receipt";
54267429Seric 	}
54367261Seric 	else
54467261Seric 	{
54567261Seric 		sprintf(buf, "Returned mail: %.*s", sizeof buf - 20, msg);
54667546Seric 		addheader("Subject", buf, &ee->e_header);
54768228Seric 		p = "failure";
54867261Seric 	}
54968228Seric 	(void) sprintf(buf, "auto-generated (%s)", p);
55068228Seric 	addheader("Auto-Submitted", buf, &ee->e_header);
5514633Seric 
5524633Seric 	/* fake up an address header for the from person */
55368529Seric 	expand("\201n", buf, sizeof buf, e);
55464284Seric 	if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL)
5554633Seric 	{
55658151Seric 		syserr("553 Can't parse myself!");
5574633Seric 		ExitStat = EX_SOFTWARE;
5587045Seric 		returndepth--;
5594633Seric 		return (-1);
5604633Seric 	}
56158704Seric 	ee->e_sender = ee->e_from.q_paddr;
5625984Seric 
5636978Seric 	/* push state into submessage */
5646978Seric 	CurEnv = ee;
56558050Seric 	define('f', "\201n", ee);
5669375Seric 	define('x', "Mail Delivery Subsystem", ee);
56758929Seric 	eatheader(ee, TRUE);
5685984Seric 
56963753Seric 	/* mark statistics */
57064284Seric 	markstats(ee, NULLADDR);
57163753Seric 
5726978Seric 	/* actually deliver the error message */
57314876Seric 	sendall(ee, SM_DEFAULT);
5746978Seric 
5756978Seric 	/* restore state */
5767811Seric 	dropenvelope(ee);
57758680Seric 	CurEnv = oldcur;
5787045Seric 	returndepth--;
5796978Seric 
5807045Seric 	/* should check for delivery errors here */
5814633Seric 	return (0);
5824633Seric }
5834633Seric /*
5846978Seric **  ERRBODY -- output the body of an error message.
5856978Seric **
5866978Seric **	Typically this is a copy of the transcript plus a copy of the
5876978Seric **	original offending message.
5886978Seric **
589297Seric **	Parameters:
59065870Seric **		mci -- the mailer connection information.
5919542Seric **		e -- the envelope we are working in.
59267546Seric **		separator -- any possible MIME separator.
59367936Seric **		flags -- to modify the behaviour.
594297Seric **
595297Seric **	Returns:
596297Seric **		none
597297Seric **
598297Seric **	Side Effects:
5996978Seric **		Outputs the body of an error message.
600297Seric */
601297Seric 
602*69748Seric void
60368228Seric errbody(mci, e, separator)
60465870Seric 	register MCI *mci;
6059542Seric 	register ENVELOPE *e;
60667546Seric 	char *separator;
607297Seric {
6086978Seric 	register FILE *xfile;
60959082Seric 	char *p;
61059082Seric 	register ADDRESS *q;
61159082Seric 	bool printheader;
61268559Seric 	bool sendbody;
6133189Seric 	char buf[MAXLINE];
61468228Seric 	extern char *xtextify();
615297Seric 
61667546Seric 	if (bitset(MCIF_INHEADER, mci->mci_flags))
61767546Seric 	{
61867546Seric 		putline("", mci);
61967546Seric 		mci->mci_flags &= ~MCIF_INHEADER;
62067546Seric 	}
62158680Seric 	if (e->e_parent == NULL)
62258680Seric 	{
62358680Seric 		syserr("errbody: null parent");
62465870Seric 		putline("   ----- Original message lost -----\n", mci);
62558680Seric 		return;
62658680Seric 	}
62758680Seric 
6289057Seric 	/*
62959730Seric 	**  Output MIME header.
63059730Seric 	*/
63159730Seric 
63259730Seric 	if (e->e_msgboundary != NULL)
63359730Seric 	{
63465870Seric 		putline("This is a MIME-encapsulated message", mci);
63565870Seric 		putline("", mci);
63659730Seric 		(void) sprintf(buf, "--%s", e->e_msgboundary);
63765870Seric 		putline(buf, mci);
63865870Seric 		putline("", mci);
63959730Seric 	}
64059730Seric 
64159730Seric 	/*
64263852Seric 	**  Output introductory information.
64363852Seric 	*/
64463852Seric 
64564718Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
64664718Seric 		if (bitset(QBADADDR, q->q_flags))
64764718Seric 			break;
64865054Seric 	if (q == NULL &&
64965054Seric 	    !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags))
65064718Seric 	{
65164718Seric 		putline("    **********************************************",
65265870Seric 			mci);
65364718Seric 		putline("    **      THIS IS A WARNING MESSAGE ONLY      **",
65465870Seric 			mci);
65564718Seric 		putline("    **  YOU DO NOT NEED TO RESEND YOUR MESSAGE  **",
65665870Seric 			mci);
65764718Seric 		putline("    **********************************************",
65865870Seric 			mci);
65965870Seric 		putline("", mci);
66064718Seric 	}
66164718Seric 	sprintf(buf, "The original message was received at %s",
66264718Seric 		arpadate(ctime(&e->e_parent->e_ctime)));
66365870Seric 	putline(buf, mci);
66468529Seric 	expand("from \201_", buf, sizeof buf, e->e_parent);
66565870Seric 	putline(buf, mci);
66665870Seric 	putline("", mci);
66763852Seric 
66863852Seric 	/*
66955372Seric 	**  Output error message header (if specified and available).
67055372Seric 	*/
67155372Seric 
67267682Seric 	if (ErrMsgFile != NULL && !bitset(EF_SENDRECEIPT, e->e_parent->e_flags))
67355372Seric 	{
67455372Seric 		if (*ErrMsgFile == '/')
67555372Seric 		{
67655372Seric 			xfile = fopen(ErrMsgFile, "r");
67755372Seric 			if (xfile != NULL)
67855372Seric 			{
67955372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
68055425Seric 				{
68168529Seric 					expand(buf, buf, sizeof buf, e);
68265870Seric 					putline(buf, mci);
68355425Seric 				}
68455372Seric 				(void) fclose(xfile);
68565870Seric 				putline("\n", mci);
68655372Seric 			}
68755372Seric 		}
68855372Seric 		else
68955372Seric 		{
69068529Seric 			expand(ErrMsgFile, buf, sizeof buf, e);
69165870Seric 			putline(buf, mci);
69265870Seric 			putline("", mci);
69355372Seric 		}
69455372Seric 	}
69555372Seric 
69655372Seric 	/*
69759082Seric 	**  Output message introduction
69859082Seric 	*/
69959082Seric 
70059082Seric 	printheader = TRUE;
70159082Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
70259082Seric 	{
70368868Seric 		if (bitset(QBADADDR, q->q_flags))
70459082Seric 		{
70568868Seric 			if (!bitset(QPINGONFAILURE, q->q_flags))
70668603Seric 				continue;
70768868Seric 			p = "unrecoverable error";
70868868Seric 		}
70968868Seric 		else if (!bitset(QPRIMARY, q->q_flags))
71068868Seric 			continue;
71168868Seric 		else if (bitset(QRELAYED, q->q_flags))
71268868Seric 			p = "relayed to non-DSN-aware mailer";
71368868Seric 		else if (bitset(QDELIVERED, q->q_flags))
71468868Seric 		{
71568868Seric 			if (bitset(QEXPANDED, q->q_flags))
71668868Seric 				p = "successfully delivered to mailing list";
71763787Seric 			else
71868868Seric 				p = "successfully delivered to mailbox";
71968868Seric 		}
72068868Seric 		else if (bitset(QEXPANDED, q->q_flags))
72168868Seric 			p = "expanded by alias";
72268868Seric 		else if (bitset(QDELAYED, q->q_flags))
72368868Seric 			p = "transient failure";
72468868Seric 		else
72568868Seric 			continue;
72668868Seric 
72768868Seric 		if (printheader)
72868868Seric 		{
72968868Seric 			putline("   ----- The following addresses have delivery notifications -----",
73068868Seric 				mci);
73168868Seric 			printheader = FALSE;
73268868Seric 		}
73368868Seric 
73468868Seric 		sprintf(buf, "%s  (%s)", q->q_paddr, p);
73568868Seric 		putline(buf, mci);
73668868Seric 		if (q->q_alias != NULL)
73768868Seric 		{
73868868Seric 			strcpy(buf, "    (expanded from: ");
73968868Seric 			strcat(buf, q->q_alias->q_paddr);
74068868Seric 			strcat(buf, ")");
74165870Seric 			putline(buf, mci);
74259082Seric 		}
74359082Seric 	}
74459082Seric 	if (!printheader)
74565870Seric 		putline("\n", mci);
74659082Seric 
74759082Seric 	/*
7489057Seric 	**  Output transcript of errors
7499057Seric 	*/
7509057Seric 
7514086Seric 	(void) fflush(stdout);
7529542Seric 	p = queuename(e->e_parent, 'x');
7539337Seric 	if ((xfile = fopen(p, "r")) == NULL)
7549057Seric 	{
7559337Seric 		syserr("Cannot open %s", p);
75665870Seric 		putline("   ----- Transcript of session is unavailable -----\n", mci);
7579057Seric 	}
7589057Seric 	else
7599057Seric 	{
76068868Seric 		printheader = TRUE;
7619542Seric 		if (e->e_xfp != NULL)
7629542Seric 			(void) fflush(e->e_xfp);
7639057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
76468868Seric 		{
76568868Seric 			if (printheader)
76668868Seric 				putline("   ----- Transcript of session follows -----\n", mci);
76768868Seric 			printheader = FALSE;
76865870Seric 			putline(buf, mci);
76968868Seric 		}
77058680Seric 		(void) xfclose(xfile, "errbody xscript", p);
7719057Seric 	}
772297Seric 	errno = 0;
7734318Seric 
77468848Seric #if DSN
7754318Seric 	/*
77667880Seric 	**  Output machine-readable version.
77767880Seric 	*/
77867880Seric 
77967880Seric 	if (e->e_msgboundary != NULL)
78067880Seric 	{
78167880Seric 		putline("", mci);
78267880Seric 		(void) sprintf(buf, "--%s", e->e_msgboundary);
78367880Seric 		putline(buf, mci);
78469545Seric 		putline("Content-Type: message/X-delivery-status-04a (Draft of April 4, 1995)", mci);
78567880Seric 		putline("", mci);
78667880Seric 
78767880Seric 		/*
78867880Seric 		**  Output per-message information.
78967880Seric 		*/
79067880Seric 
79167880Seric 		/* original envelope id from MAIL FROM: line */
79267880Seric 		if (e->e_parent->e_envid != NULL)
79367880Seric 		{
79467880Seric 			(void) sprintf(buf, "Original-Envelope-Id: %s",
79568228Seric 				xtextify(e->e_parent->e_envid));
79667880Seric 			putline(buf, mci);
79767880Seric 		}
79867880Seric 
79968228Seric 		/* Reporting-MTA: is us (required) */
80068868Seric 		(void) sprintf(buf, "Reporting-MTA: dns; %s",
80168583Seric 			xtextify(MyHostName));
80267880Seric 		putline(buf, mci);
80367880Seric 
80468868Seric 		/* DSN-Gateway: not relevant since we are not translating */
80568868Seric 
80668228Seric 		/* Received-From-MTA: shows where we got this message from */
80767990Seric 		if (RealHostName != NULL)
80867990Seric 		{
80968228Seric 			/* XXX use $s for type? */
81068228Seric 			p = e->e_parent->e_from.q_mailer->m_mtatype;
81168228Seric 			if (p == NULL)
81268228Seric 				p = "dns";
81368228Seric 			(void) sprintf(buf, "Received-From-MTA: %s; %s",
81468583Seric 				p, xtextify(RealHostName));
81567990Seric 			putline(buf, mci);
81667990Seric 		}
81767963Seric 
81867963Seric 		/* Arrival-Date: -- when it arrived here */
81967963Seric 		(void) sprintf(buf, "Arrival-Date: %s",
82067963Seric 			arpadate(ctime(&e->e_parent->e_ctime)));
82167963Seric 		putline(buf, mci);
82267963Seric 
82367880Seric 		/*
82467880Seric 		**  Output per-address information.
82567880Seric 		*/
82667880Seric 
82767880Seric 		for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
82867880Seric 		{
82967880Seric 			register ADDRESS *r;
83068603Seric 			char *action;
83167880Seric 
83268603Seric 			if (bitset(QBADADDR, q->q_flags))
83368868Seric 				action = "failed";
83468603Seric 			else if (!bitset(QPRIMARY, q->q_flags))
83567880Seric 				continue;
83668868Seric 			else if (bitset(QDELIVERED, q->q_flags))
83768868Seric 			{
83868868Seric 				if (bitset(QEXPANDED, q->q_flags))
83968868Seric 					action = "delivered (to mailing list)";
84068868Seric 				else
84168868Seric 					action = "delivered (to mailbox)";
84268868Seric 			}
84368603Seric 			else if (bitset(QRELAYED, q->q_flags))
84468868Seric 				action = "relayed (to non-DSN-aware mailer)";
84568868Seric 			else if (bitset(QEXPANDED, q->q_flags))
84668868Seric 				action = "expanded (to multi-recipient alias)";
84768868Seric 			else if (bitset(QDELAYED, q->q_flags))
84868603Seric 				action = "delayed";
84968603Seric 			else
85068603Seric 				continue;
85168603Seric 
85267880Seric 			putline("", mci);
85367880Seric 
85468228Seric 			/* Original-Recipient: -- passed from on high */
85568228Seric 			if (q->q_orcpt != NULL)
85668228Seric 			{
85768228Seric 				(void) sprintf(buf, "Original-Recipient: %s",
85868228Seric 					xtextify(q->q_orcpt));
85968228Seric 				putline(buf, mci);
86068228Seric 			}
86168228Seric 
86268228Seric 			/* Final-Recipient: -- the name from the RCPT command */
86368228Seric 			p = e->e_parent->e_from.q_mailer->m_addrtype;
86468228Seric 			if (p == NULL)
86568228Seric 				p = "rfc822";
86668228Seric 			for (r = q; r->q_alias != NULL; r = r->q_alias)
86768228Seric 				continue;
86868228Seric 			if (strchr(r->q_user, '@') == NULL)
86968583Seric 			{
87068583Seric 				(void) sprintf(buf, "Final-Recipient: %s; %s@",
87168583Seric 					p, xtextify(r->q_user));
87268583Seric 				strcat(buf, xtextify(MyHostName));
87368583Seric 			}
87467998Seric 			else
87568583Seric 			{
87668228Seric 				(void) sprintf(buf, "Final-Recipient: %s; %s",
87768228Seric 					p, xtextify(r->q_user));
87868583Seric 			}
87967880Seric 			putline(buf, mci);
88067880Seric 
88168603Seric 			/* X-Actual-Recipient: -- the real problem address */
88268603Seric 			if (r != q)
88368603Seric 			{
88468603Seric 				if (strchr(q->q_user, '@') == NULL)
88568603Seric 				{
88668603Seric 					(void) sprintf(buf, "X-Actual-Recipient: %s; %s@",
88768603Seric 						p, xtextify(q->q_user));
88868603Seric 					strcat(buf, xtextify(MyHostName));
88968603Seric 				}
89068603Seric 				else
89168603Seric 				{
89268603Seric 					(void) sprintf(buf, "X-Actual-Recipient: %s; %s",
89368603Seric 						p, xtextify(q->q_user));
89468603Seric 				}
89568603Seric 				putline(buf, mci);
89668603Seric 			}
89768603Seric 
89867880Seric 			/* Action: -- what happened? */
89968603Seric 			sprintf(buf, "Action: %s", action);
90068603Seric 			putline(buf, mci);
90167880Seric 
90267880Seric 			/* Status: -- what _really_ happened? */
90367880Seric 			strcpy(buf, "Status: ");
90467880Seric 			if (q->q_status != NULL)
90567880Seric 				strcat(buf, q->q_status);
90667880Seric 			else if (bitset(QBADADDR, q->q_flags))
90768228Seric 				strcat(buf, "5.0.0");
90867880Seric 			else if (bitset(QQUEUEUP, q->q_flags))
90968228Seric 				strcat(buf, "4.0.0");
91067880Seric 			else
91168228Seric 				strcat(buf, "2.0.0");
91267880Seric 			putline(buf, mci);
91367880Seric 
91468228Seric 			/* Remote-MTA: -- who was I talking to? */
91568228Seric 			p = q->q_mailer->m_mtatype;
91668228Seric 			if (p == NULL)
91768228Seric 				p = "dns";
91868228Seric 			(void) sprintf(buf, "Remote-MTA: %s; ", p);
91968228Seric 			if (q->q_statmta != NULL)
92068228Seric 				p = q->q_statmta;
92168603Seric 			else if (q->q_host != NULL && q->q_host[0] != '\0')
92268228Seric 				p = q->q_host;
92368228Seric 			else
92468228Seric 				p = NULL;
92568228Seric 			if (p != NULL)
92668228Seric 			{
92768228Seric 				strcat(buf, p);
92868228Seric 				p = &buf[strlen(buf) - 1];
92968228Seric 				if (*p == '.')
93068228Seric 					*p = '\0';
93168228Seric 				putline(buf, mci);
93268228Seric 			}
93368228Seric 
93468228Seric 			/* Diagnostic-Code: -- actual result from other end */
93568228Seric 			if (q->q_rstatus != NULL)
93668228Seric 			{
93768228Seric 				p = q->q_mailer->m_diagtype;
93868228Seric 				if (p == NULL)
93968228Seric 					p = "smtp";
94068228Seric 				(void) sprintf(buf, "Diagnostic-Code: %s; %s",
94168868Seric 					p, xtextify(q->q_rstatus));
94268228Seric 				putline(buf, mci);
94368228Seric 			}
94468228Seric 
94568228Seric 			/* Last-Attempt-Date: -- fine granularity */
94667880Seric 			if (q->q_statdate == (time_t) 0L)
94767880Seric 				q->q_statdate = curtime();
94868228Seric 			(void) sprintf(buf, "Last-Attempt-Date: %s",
94967880Seric 				arpadate(ctime(&q->q_statdate)));
95067880Seric 			putline(buf, mci);
95167880Seric 
95268868Seric 			/* Will-Retry-Until: -- for delayed messages only */
95367963Seric 			if (bitset(QQUEUEUP, q->q_flags) &&
95467963Seric 			    !bitset(QBADADDR, q->q_flags))
95567963Seric 			{
95667963Seric 				time_t xdate;
95767963Seric 
95867963Seric 				xdate = e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass];
95968868Seric 				sprintf(buf, "Will-Retry-Until: %s",
96067963Seric 					arpadate(ctime(&xdate)));
96167963Seric 				putline(buf, mci);
96267963Seric 			}
96367880Seric 		}
96467880Seric 	}
96568028Seric #endif
96667880Seric 
96767880Seric 	/*
9684318Seric 	**  Output text of original message
9694318Seric 	*/
9704318Seric 
97165870Seric 	putline("", mci);
97268564Seric 	if (bitset(EF_HAS_DF, e->e_parent->e_flags))
9734199Seric 	{
97468802Seric 		sendbody = !bitset(EF_NO_BODY_RETN, e->e_parent->e_flags) &&
97568802Seric 			   !bitset(EF_NO_BODY_RETN, e->e_flags);
97668559Seric 
97767880Seric 		if (e->e_msgboundary == NULL)
97867880Seric 		{
97968559Seric 			if (sendbody)
98067880Seric 				putline("   ----- Original message follows -----\n", mci);
98167880Seric 			else
98267880Seric 				putline("   ----- Message header follows -----\n", mci);
98367880Seric 			(void) fflush(mci->mci_out);
98467880Seric 		}
9855984Seric 		else
9865984Seric 		{
98759730Seric 			(void) sprintf(buf, "--%s", e->e_msgboundary);
98865870Seric 			putline(buf, mci);
98968859Seric 			(void) sprintf(buf, "Content-Type: %s",
99068859Seric 				sendbody ? "message/rfc822"
99168859Seric 					 : "text/rfc822-headers");
99268228Seric 			putline(buf, mci);
9935984Seric 		}
99467981Seric 		putline("", mci);
99568228Seric 		putheader(mci, e->e_parent->e_header, e->e_parent);
99668559Seric 		if (sendbody)
99768228Seric 			putbody(mci, e->e_parent, e->e_msgboundary);
99868228Seric 		else if (e->e_msgboundary == NULL)
99967546Seric 		{
100067546Seric 			putline("", mci);
100165870Seric 			putline("   ----- Message body suppressed -----", mci);
100267546Seric 		}
10034199Seric 	}
100468228Seric 	else if (e->e_msgboundary == NULL)
100510170Seric 	{
100665870Seric 		putline("  ----- No message was collected -----\n", mci);
100710170Seric 	}
10084318Seric 
100960010Seric 	if (e->e_msgboundary != NULL)
101060010Seric 	{
101165870Seric 		putline("", mci);
101260010Seric 		(void) sprintf(buf, "--%s--", e->e_msgboundary);
101365870Seric 		putline(buf, mci);
101460010Seric 	}
101565870Seric 	putline("", mci);
101659730Seric 
10174318Seric 	/*
10184318Seric 	**  Cleanup and exit
10194318Seric 	*/
10204318Seric 
1021297Seric 	if (errno != 0)
10226978Seric 		syserr("errbody: I/O error");
1023297Seric }
102458144Seric /*
102568228Seric **  SMTPTODSN -- convert SMTP to DSN status code
102668228Seric **
102768228Seric **	Parameters:
102868228Seric **		smtpstat -- the smtp status code (e.g., 550).
102968228Seric **
103068228Seric **	Returns:
103168228Seric **		The DSN version of the status code.
103268228Seric */
103368228Seric 
103468228Seric char *
103568228Seric smtptodsn(smtpstat)
103668228Seric 	int smtpstat;
103768228Seric {
103868857Seric 	if (smtpstat < 0)
103968857Seric 		return "4.4.2";
104068857Seric 
104168228Seric 	switch (smtpstat)
104268228Seric 	{
104368228Seric 	  case 450:	/* Req mail action not taken: mailbox unavailable */
104468228Seric 		return "4.2.0";
104568228Seric 
104668228Seric 	  case 451:	/* Req action aborted: local error in processing */
104768228Seric 		return "4.3.0";
104868228Seric 
104968228Seric 	  case 452:	/* Req action not taken: insufficient sys storage */
105068228Seric 		return "4.3.1";
105168228Seric 
105268228Seric 	  case 500:	/* Syntax error, command unrecognized */
105368228Seric 		return "5.5.2";
105468228Seric 
105568228Seric 	  case 501:	/* Syntax error in parameters or arguments */
105668228Seric 		return "5.5.4";
105768228Seric 
105868228Seric 	  case 502:	/* Command not implemented */
105968228Seric 		return "5.5.1";
106068228Seric 
106168228Seric 	  case 503:	/* Bad sequence of commands */
106268228Seric 		return "5.5.1";
106368228Seric 
106468228Seric 	  case 504:	/* Command parameter not implemented */
106568228Seric 		return "5.5.4";
106668228Seric 
106768228Seric 	  case 550:	/* Req mail action not taken: mailbox unavailable */
106868228Seric 		return "5.2.0";
106968228Seric 
107068228Seric 	  case 551:	/* User not local; please try <...> */
107168228Seric 		return "5.1.6";
107268228Seric 
107368228Seric 	  case 552:	/* Req mail action aborted: exceeded storage alloc */
107468228Seric 		return "5.2.2";
107568228Seric 
107668228Seric 	  case 553:	/* Req action not taken: mailbox name not allowed */
107768228Seric 		return "5.1.3";
107868228Seric 
107968228Seric 	  case 554:	/* Transaction failed */
108068228Seric 		return "5.0.0";
108168228Seric 	}
108268228Seric 
108368228Seric 	if ((smtpstat / 100) == 2)
108468228Seric 		return "2.0.0";
108568228Seric 	if ((smtpstat / 100) == 4)
108668228Seric 		return "4.0.0";
108768228Seric 	return "5.0.0";
108868228Seric }
108968228Seric /*
109068228Seric **  XTEXTIFY -- take regular text and turn it into DSN-style xtext
109168228Seric **
109268228Seric **	Parameters:
109368228Seric **		t -- the text to convert.
109468228Seric **
109568228Seric **	Returns:
109668228Seric **		The xtext-ified version of the same string.
109768228Seric */
109868228Seric 
109968228Seric char *
110068228Seric xtextify(t)
110168228Seric 	register char *t;
110268228Seric {
110368228Seric 	register char *p;
110468228Seric 	int l;
110568228Seric 	int nbogus;
110668228Seric 	static char *bp = NULL;
110768228Seric 	static int bplen = 0;
110868228Seric 
110968228Seric 	/* figure out how long this xtext will have to be */
111068228Seric 	nbogus = l = 0;
111168228Seric 	for (p = t; *p != '\0'; p++)
111268228Seric 	{
111368228Seric 		register int c = (*p & 0xff);
111468228Seric 
111568228Seric 		/* ASCII dependence here -- this is the way the spec words it */
111668868Seric 		if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(')
111768228Seric 			nbogus++;
111868228Seric 		l++;
111968228Seric 	}
112068228Seric 	if (nbogus == 0)
112168228Seric 		return t;
112268228Seric 	l += nbogus * 2 + 1;
112368228Seric 
112468228Seric 	/* now allocate space if necessary for the new string */
112568228Seric 	if (l > bplen)
112668228Seric 	{
112768228Seric 		if (bp != NULL)
112868228Seric 			free(bp);
112968228Seric 		bp = xalloc(l);
113068228Seric 		bplen = l;
113168228Seric 	}
113268228Seric 
113368228Seric 	/* ok, copy the text with byte expansion */
113468228Seric 	for (p = bp; *t != '\0'; )
113568228Seric 	{
113668228Seric 		register int c = (*t++ & 0xff);
113768228Seric 
113868228Seric 		/* ASCII dependence here -- this is the way the spec words it */
113968228Seric 		if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(')
114068228Seric 		{
114168228Seric 			*p++ = '+';
114268228Seric 			*p++ = "0123456789abcdef"[c >> 4];
114368228Seric 			*p++ = "0123456789abcdef"[c & 0xf];
114468228Seric 		}
114568228Seric 		else
114668228Seric 			*p++ = c;
114768228Seric 	}
114868228Seric 	*p = '\0';
114968228Seric 	return bp;
115068228Seric }
115168228Seric /*
115268583Seric **  XTEXTOK -- check if a string is legal xtext
115368583Seric **
115468583Seric **	Xtext is used in Delivery Status Notifications.  The spec was
115568583Seric **	taken from draft-ietf-notary-mime-delivery-04.txt.
115668583Seric **
115768583Seric **	Parameters:
115868583Seric **		s -- the string to check.
115968583Seric **
116068583Seric **	Returns:
116168583Seric **		TRUE -- if 's' is legal xtext.
116268583Seric **		FALSE -- if it has any illegal characters in it.
116368583Seric */
116468583Seric 
116568583Seric bool
116668583Seric xtextok(s)
116768583Seric 	char *s;
116868583Seric {
116968583Seric 	int c;
117068583Seric 
117168583Seric 	while ((c = *s++) != '\0')
117268583Seric 	{
117368583Seric 		if (c == '+')
117468583Seric 		{
117568583Seric 			c = *s++;
117668583Seric 			if (!isascii(c) || !isxdigit(c))
117768583Seric 				return FALSE;
117868583Seric 			c = *s++;
117968583Seric 			if (!isascii(c) || !isxdigit(c))
118068583Seric 				return FALSE;
118168583Seric 		}
118268583Seric 		else if (c < '!' || c > '~' || c == '\\' || c == '(')
118368583Seric 			return FALSE;
118468583Seric 	}
118568583Seric 	return TRUE;
118668583Seric }
118768583Seric /*
118858144Seric **  PRUNEROUTE -- prune an RFC-822 source route
118958144Seric **
119058144Seric **	Trims down a source route to the last internet-registered hop.
119158144Seric **	This is encouraged by RFC 1123 section 5.3.3.
119258144Seric **
119358144Seric **	Parameters:
119458144Seric **		addr -- the address
119558144Seric **
119658144Seric **	Returns:
119758144Seric **		TRUE -- address was modified
119858144Seric **		FALSE -- address could not be pruned
119958144Seric **
120058144Seric **	Side Effects:
120158144Seric **		modifies addr in-place
120258144Seric */
120358144Seric 
1204*69748Seric bool
120558144Seric pruneroute(addr)
120658144Seric 	char *addr;
120758144Seric {
120866334Seric #if NAMED_BIND
120958144Seric 	char *start, *at, *comma;
121058144Seric 	char c;
121158144Seric 	int rcode;
121258144Seric 	char hostbuf[BUFSIZ];
121358144Seric 	char *mxhosts[MAXMXHOSTS + 1];
121458144Seric 
121558144Seric 	/* check to see if this is really a route-addr */
121658144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
121758144Seric 		return FALSE;
121858144Seric 	start = strchr(addr, ':');
121958144Seric 	at = strrchr(addr, '@');
122058144Seric 	if (start == NULL || at == NULL || at < start)
122158144Seric 		return FALSE;
122258144Seric 
122358144Seric 	/* slice off the angle brackets */
122458144Seric 	strcpy(hostbuf, at + 1);
122558144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
122658144Seric 
122758144Seric 	while (start)
122858144Seric 	{
122959273Seric 		if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0)
123058144Seric 		{
123158144Seric 			strcpy(addr + 1, start + 1);
123258144Seric 			return TRUE;
123358144Seric 		}
123458144Seric 		c = *start;
123558144Seric 		*start = '\0';
123658144Seric 		comma = strrchr(addr, ',');
123758144Seric 		if (comma && comma[1] == '@')
123858144Seric 			strcpy(hostbuf, comma + 2);
123958144Seric 		else
124058144Seric 			comma = 0;
124158144Seric 		*start = c;
124258144Seric 		start = comma;
124358144Seric 	}
124458144Seric #endif
124558144Seric 	return FALSE;
124658144Seric }
1247