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*69918Seric static char sccsid[] = "@(#)savemail.c	8.78 (Berkeley) 06/17/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 
5269748Seric 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 	  case EM_MAIL:
12424942Seric 		state = ESM_MAIL;
12524942Seric 		break;
12624942Seric 
12724942Seric 	  case EM_PRINT:
12824979Seric 	  case '\0':
12924942Seric 		state = ESM_QUIET;
13024942Seric 		break;
13124942Seric 
13224942Seric 	  case EM_QUIET:
13324942Seric 		/* no need to return anything at all */
13424942Seric 		return;
13524979Seric 
13624979Seric 	  default:
13758734Seric 		syserr("554 savemail: bogus errormode x%x\n", e->e_errormode);
13824979Seric 		state = ESM_MAIL;
13924979Seric 		break;
140297Seric 	}
141297Seric 
14259094Seric 	/* if this is already an error response, send to postmaster */
14359094Seric 	if (bitset(EF_RESPONSE, e->e_flags))
14459094Seric 	{
14559094Seric 		if (e->e_parent != NULL &&
14659094Seric 		    bitset(EF_RESPONSE, e->e_parent->e_flags))
14759094Seric 		{
14859094Seric 			/* got an error sending a response -- can it */
14959094Seric 			return;
15059094Seric 		}
15159094Seric 		state = ESM_POSTMASTER;
15259094Seric 	}
15359094Seric 
15424942Seric 	while (state != ESM_DONE)
155297Seric 	{
15624979Seric 		if (tTd(6, 5))
15724979Seric 			printf("  state %d\n", state);
15824979Seric 
15924942Seric 		switch (state)
160297Seric 		{
16124979Seric 		  case ESM_QUIET:
16267473Seric 			if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags))
16324979Seric 				state = ESM_DEADLETTER;
16424979Seric 			else
16524979Seric 				state = ESM_MAIL;
16624979Seric 			break;
16724979Seric 
16824942Seric 		  case ESM_REPORT:
16924942Seric 
17024942Seric 			/*
17124942Seric 			**  If the user is still logged in on the same terminal,
17224942Seric 			**  then write the error messages back to hir (sic).
17324942Seric 			*/
17424942Seric 
17524942Seric 			p = ttypath();
17624942Seric 			if (p == NULL || freopen(p, "w", stdout) == NULL)
17724942Seric 			{
17824942Seric 				state = ESM_MAIL;
17924942Seric 				break;
18024942Seric 			}
18124942Seric 
18268529Seric 			expand("\201n", buf, sizeof buf, e);
1839375Seric 			printf("\r\nMessage from %s...\r\n", buf);
1849375Seric 			printf("Errors occurred while sending mail.\r\n");
1859542Seric 			if (e->e_xfp != NULL)
1869375Seric 			{
1879542Seric 				(void) fflush(e->e_xfp);
18824942Seric 				fp = fopen(queuename(e, 'x'), "r");
1899375Seric 			}
1909375Seric 			else
19124942Seric 				fp = NULL;
19224942Seric 			if (fp == NULL)
1939375Seric 			{
1949337Seric 				syserr("Cannot open %s", queuename(e, 'x'));
1959375Seric 				printf("Transcript of session is unavailable.\r\n");
1969375Seric 			}
1979375Seric 			else
1989375Seric 			{
1999375Seric 				printf("Transcript follows:\r\n");
20024942Seric 				while (fgets(buf, sizeof buf, fp) != NULL &&
2019375Seric 				       !ferror(stdout))
2029375Seric 					fputs(buf, stdout);
20358680Seric 				(void) xfclose(fp, "savemail transcript", e->e_id);
2049375Seric 			}
20524942Seric 			printf("Original message will be saved in dead.letter.\r\n");
20624942Seric 			state = ESM_DEADLETTER;
20724942Seric 			break;
208297Seric 
20924942Seric 		  case ESM_MAIL:
21024942Seric 			/*
21124942Seric 			**  If mailing back, do it.
21224942Seric 			**	Throw away all further output.  Don't alias,
21324942Seric 			**	since this could cause loops, e.g., if joe
21424942Seric 			**	mails to joe@x, and for some reason the network
21524942Seric 			**	for @x is down, then the response gets sent to
21624942Seric 			**	joe@x, which gives a response, etc.  Also force
21724942Seric 			**	the mail to be delivered even if a version of
21824942Seric 			**	it has already been sent to the sender.
21963841Seric 			**
22063841Seric 			**  If this is a configuration or local software
22163841Seric 			**	error, send to the local postmaster as well,
22263841Seric 			**	since the originator can't do anything
22363841Seric 			**	about it anyway.  Note that this is a full
22463841Seric 			**	copy of the message (intentionally) so that
22563841Seric 			**	the Postmaster can forward things along.
22624942Seric 			*/
227297Seric 
22863841Seric 			if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE)
22963841Seric 			{
23063841Seric 				(void) sendtolist("postmaster",
23167982Seric 					  NULLADDR, &e->e_errorqueue, 0, e);
23263841Seric 			}
23367939Seric 			if (!emptyaddr(&e->e_from))
23463841Seric 			{
23558680Seric 				(void) sendtolist(e->e_from.q_paddr,
23667982Seric 					  NULLADDR, &e->e_errorqueue, 0, e);
23763841Seric 			}
23858680Seric 
23963841Seric 			/*
24063841Seric 			**  Deliver a non-delivery report to the
24163841Seric 			**  Postmaster-designate (not necessarily
24263841Seric 			**  Postmaster).  This does not include the
24363841Seric 			**  body of the message, for privacy reasons.
24463841Seric 			**  You really shouldn't need this.
24563841Seric 			*/
24663841Seric 
24763849Seric 			e->e_flags |= EF_PM_NOTIFY;
24858178Seric 
24964792Seric 			/* check to see if there are any good addresses */
25064792Seric 			for (q = e->e_errorqueue; q != NULL; q = q->q_next)
25164792Seric 				if (!bitset(QBADADDR|QDONTSEND, q->q_flags))
25264792Seric 					break;
25358680Seric 			if (q == NULL)
25458680Seric 			{
25558680Seric 				/* this is an error-error */
25658680Seric 				state = ESM_POSTMASTER;
25758680Seric 				break;
25858680Seric 			}
25966303Seric 			if (returntosender(e->e_message, e->e_errorqueue,
26067981Seric 					   sendbody, e) == 0)
26158680Seric 			{
26258680Seric 				state = ESM_DONE;
26358680Seric 				break;
26458680Seric 			}
26524981Seric 
26658680Seric 			/* didn't work -- return to postmaster */
26758680Seric 			state = ESM_POSTMASTER;
26858680Seric 			break;
26958306Seric 
27058680Seric 		  case ESM_POSTMASTER:
27158680Seric 			/*
27258680Seric 			**  Similar to previous case, but to system postmaster.
27358680Seric 			*/
27458680Seric 
27559432Seric 			q = NULL;
27667982Seric 			if (sendtolist("postmaster", NULL, &q, 0, e) <= 0)
27724942Seric 			{
27858680Seric 				syserr("553 cannot parse postmaster!");
27958680Seric 				ExitStat = EX_SOFTWARE;
28058680Seric 				state = ESM_USRTMP;
28158680Seric 				break;
28224942Seric 			}
28367981Seric 			if (returntosender(e->e_message, q, sendbody, e) == 0)
28424942Seric 			{
28524942Seric 				state = ESM_DONE;
28624942Seric 				break;
28724942Seric 			}
288297Seric 
28958680Seric 			/* didn't work -- last resort */
29058680Seric 			state = ESM_USRTMP;
29124942Seric 			break;
292297Seric 
29324942Seric 		  case ESM_DEADLETTER:
29424942Seric 			/*
29524942Seric 			**  Save the message in dead.letter.
29624942Seric 			**	If we weren't mailing back, and the user is
29724942Seric 			**	local, we should save the message in
29824942Seric 			**	~/dead.letter so that the poor person doesn't
29924942Seric 			**	have to type it over again -- and we all know
30024942Seric 			**	what poor typists UNIX users are.
30124942Seric 			*/
3025315Seric 
30324942Seric 			p = NULL;
30467473Seric 			if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
30524942Seric 			{
30624942Seric 				if (e->e_from.q_home != NULL)
30724942Seric 					p = e->e_from.q_home;
30868693Seric 				else if ((pw = sm_getpwnam(e->e_from.q_user)) != NULL)
30924942Seric 					p = pw->pw_dir;
31024942Seric 			}
31168802Seric 			if (p == NULL || e->e_dfp == NULL)
31224942Seric 			{
31368802Seric 				/* no local directory or no data file */
31424942Seric 				state = ESM_MAIL;
31524942Seric 				break;
31624942Seric 			}
31724942Seric 
31869787Seric 			/* we have a home directory; write dead.letter */
31968802Seric 			define('z', p, e);
32068802Seric 			expand("\201z/dead.letter", buf, sizeof buf, e);
32168802Seric 			sfflags = SFF_NOSLINK|SFF_CREAT|SFF_REGONLY|SFF_RUNASREALUID;
32268802Seric 			e->e_to = buf;
32369787Seric 			if (mailfile(buf, NULL, sfflags, e) == EX_OK)
32469787Seric 			{
32569787Seric 				bool oldverb = Verbose;
32624942Seric 
32769787Seric 				Verbose = TRUE;
32869787Seric 				message("Saved message in %s", buf);
32969787Seric 				Verbose = oldverb;
33069787Seric 				state = ESM_DONE;
33169787Seric 				break;
33269787Seric 			}
33369787Seric 			state = ESM_MAIL;
33469787Seric 			break;
33569787Seric 
33624942Seric 		  case ESM_USRTMP:
33724942Seric 			/*
33824942Seric 			**  Log the mail in /usr/tmp/dead.letter.
33924942Seric 			*/
34024942Seric 
34157438Seric 			if (e->e_class < 0)
34257438Seric 			{
34357438Seric 				state = ESM_DONE;
34457438Seric 				break;
34557438Seric 			}
34657438Seric 
34768490Seric 			if (SafeFileEnv != NULL && SafeFileEnv[0] != '\0')
34868490Seric 			{
34968490Seric 				state = ESM_PANIC;
35068490Seric 				break;
35168490Seric 			}
35268490Seric 
35365174Seric 			strcpy(buf, _PATH_VARTMP);
35465174Seric 			strcat(buf, "dead.letter");
35568802Seric 			sfflags = SFF_NOSLINK|SFF_CREAT|SFF_REGONLY;
35668802Seric 
35769787Seric 			if (!writable(buf, NULL, sfflags) ||
35868802Seric 			    (fp = safefopen(buf, O_WRONLY|O_CREAT|O_APPEND,
35968802Seric 					    FileMode, sfflags)) == NULL)
36064945Seric 			{
36169787Seric 				state = ESM_PANIC;
36264945Seric 				break;
36364945Seric 			}
36424942Seric 
36565870Seric 			bzero(&mcibuf, sizeof mcibuf);
36665870Seric 			mcibuf.mci_out = fp;
36765870Seric 			mcibuf.mci_mailer = FileMailer;
36865870Seric 			if (bitnset(M_7BITS, FileMailer->m_flags))
36965870Seric 				mcibuf.mci_flags |= MCIF_7BIT;
37065870Seric 
37165870Seric 			putfromline(&mcibuf, e);
37268228Seric 			(*e->e_puthdr)(&mcibuf, e->e_header, e);
37368228Seric 			(*e->e_putbody)(&mcibuf, e, NULL);
37465870Seric 			putline("\n", &mcibuf);
37524942Seric 			(void) fflush(fp);
37668802Seric 			if (!ferror(fp))
37768802Seric 			{
37868802Seric 				bool oldverb = Verbose;
37968802Seric 
38068802Seric 				Verbose = TRUE;
38168802Seric 				message("Saved message in %s", buf);
38268802Seric 				Verbose = oldverb;
38368802Seric 				state = ESM_DONE;
38468802Seric 			}
38569787Seric 			state = ESM_PANIC;
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 
42369748Seric int
42455012Seric returntosender(msg, returnq, sendbody, e)
4254633Seric 	char *msg;
42616479Seric 	ADDRESS *returnq;
4275984Seric 	bool sendbody;
42855012Seric 	register ENVELOPE *e;
4294633Seric {
4306978Seric 	register ENVELOPE *ee;
43158680Seric 	ENVELOPE *oldcur = CurEnv;
4326978Seric 	ENVELOPE errenvelope;
4337045Seric 	static int returndepth;
4349375Seric 	register ADDRESS *q;
43568228Seric 	char *p;
43669748Seric 	char buf[MAXNAME + 1];
43769748Seric 	extern void errbody __P((MCI *, ENVELOPE *, char *));
4384633Seric 
43960010Seric 	if (returnq == NULL)
44060010Seric 		return (-1);
44160010Seric 
44258966Seric 	if (msg == NULL)
44358966Seric 		msg = "Unable to deliver mail";
44458966Seric 
4457676Seric 	if (tTd(6, 1))
4467287Seric 	{
44767987Seric 		printf("\n*** Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
44855012Seric 		       msg, returndepth, e);
44916479Seric 		printaddr(returnq, TRUE);
45067987Seric 		if (tTd(6, 20))
45167987Seric 		{
45267987Seric 			printf("Sendq=");
45367987Seric 			printaddr(e->e_sendqueue, TRUE);
45467987Seric 		}
4557287Seric 	}
4567287Seric 
4577045Seric 	if (++returndepth >= MAXRETURNS)
4587045Seric 	{
4597045Seric 		if (returndepth != MAXRETURNS)
46058151Seric 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
4617045Seric 		/* don't "unrecurse" and fake a clean exit */
4627045Seric 		/* returndepth--; */
4637045Seric 		return (0);
4647045Seric 	}
4657045Seric 
46658680Seric 	define('g', e->e_from.q_paddr, e);
46764940Seric 	define('u', NULL, e);
46867880Seric 
46967880Seric 	/* initialize error envelope */
47058179Seric 	ee = newenvelope(&errenvelope, e);
47158050Seric 	define('a', "\201b", ee);
47259057Seric 	define('r', "internal", ee);
47359057Seric 	define('s', "localhost", ee);
47459057Seric 	define('_', "localhost", ee);
4756978Seric 	ee->e_puthdr = putheader;
4766978Seric 	ee->e_putbody = errbody;
47764120Seric 	ee->e_flags |= EF_RESPONSE|EF_METOO;
47855012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
47945155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
48016479Seric 	ee->e_sendqueue = returnq;
48164655Seric 	ee->e_msgsize = ERRORFUDGE;
48268559Seric 	if (sendbody)
48364655Seric 		ee->e_msgsize += e->e_msgsize;
48468802Seric 	else
48568802Seric 		ee->e_flags |= EF_NO_BODY_RETN;
48664737Seric 	initsys(ee);
48716479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4889375Seric 	{
48959989Seric 		if (bitset(QBADADDR, q->q_flags))
49058559Seric 			continue;
49158559Seric 
49268141Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
49368141Seric 		{
49468141Seric 			register ADDRESS *p;
49568141Seric 
49668141Seric 			parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e);
49768141Seric 			for (p = returnq; p != NULL; p = p->q_next)
49868141Seric 			{
49968141Seric 				if (p != q && sameaddr(p, q))
50068141Seric 					q->q_flags |= QDONTSEND;
50168141Seric 			}
50268141Seric 		}
50368141Seric 
50459989Seric 		if (!bitset(QDONTSEND, q->q_flags))
50559989Seric 			ee->e_nrcpts++;
50658559Seric 
5079375Seric 		if (q->q_alias == NULL)
50867546Seric 			addheader("To", q->q_paddr, &ee->e_header);
5099375Seric 	}
51024942Seric 
51157642Seric # ifdef LOG
51258020Seric 	if (LogLevel > 5)
51365054Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
51457642Seric 			e->e_id, ee->e_id, msg);
51557642Seric # endif
51657642Seric 
51768003Seric 	if (SendMIMEErrors)
51867261Seric 	{
51968003Seric 		addheader("MIME-Version", "1.0", &ee->e_header);
52069908Seric 
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
52669858Seric 			"multipart/report; report-type=X-delivery-status-03 (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);
53269908Seric 
53369908Seric 		p = hvalue("Content-Transfer-Encoding", e->e_header);
53469908Seric 		if (p != NULL && strcasecmp(p, "binary") != 0)
53569908Seric 			p = NULL;
53669908Seric 		if (p == NULL && bitset(EF_HAS8BIT, e->e_flags))
53769908Seric 			p = "8bit";
53869908Seric 		if (p != NULL)
53969908Seric 			addheader("Content-Transfer-Encoding", p, &ee->e_header);
54068003Seric 	}
54168228Seric 	if (strncmp(msg, "Warning:", 8) == 0)
54268003Seric 	{
54368879Seric 		addheader("Subject", msg, &ee->e_header);
54468228Seric 		p = "warning-timeout";
54567261Seric 	}
54667429Seric 	else if (strcmp(msg, "Return receipt") == 0)
54767429Seric 	{
54868879Seric 		addheader("Subject", msg, &ee->e_header);
54968228Seric 		p = "return-receipt";
55067429Seric 	}
55167261Seric 	else
55267261Seric 	{
55367261Seric 		sprintf(buf, "Returned mail: %.*s", sizeof buf - 20, msg);
55467546Seric 		addheader("Subject", buf, &ee->e_header);
55568228Seric 		p = "failure";
55667261Seric 	}
55768228Seric 	(void) sprintf(buf, "auto-generated (%s)", p);
55868228Seric 	addheader("Auto-Submitted", buf, &ee->e_header);
5594633Seric 
5604633Seric 	/* fake up an address header for the from person */
56168529Seric 	expand("\201n", buf, sizeof buf, e);
56264284Seric 	if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL)
5634633Seric 	{
56458151Seric 		syserr("553 Can't parse myself!");
5654633Seric 		ExitStat = EX_SOFTWARE;
5667045Seric 		returndepth--;
5674633Seric 		return (-1);
5684633Seric 	}
56958704Seric 	ee->e_sender = ee->e_from.q_paddr;
5705984Seric 
5716978Seric 	/* push state into submessage */
5726978Seric 	CurEnv = ee;
57358050Seric 	define('f', "\201n", ee);
5749375Seric 	define('x', "Mail Delivery Subsystem", ee);
57558929Seric 	eatheader(ee, TRUE);
5765984Seric 
57763753Seric 	/* mark statistics */
57864284Seric 	markstats(ee, NULLADDR);
57963753Seric 
5806978Seric 	/* actually deliver the error message */
58114876Seric 	sendall(ee, SM_DEFAULT);
5826978Seric 
5836978Seric 	/* restore state */
5847811Seric 	dropenvelope(ee);
58558680Seric 	CurEnv = oldcur;
5867045Seric 	returndepth--;
5876978Seric 
5887045Seric 	/* should check for delivery errors here */
5894633Seric 	return (0);
5904633Seric }
5914633Seric /*
5926978Seric **  ERRBODY -- output the body of an error message.
5936978Seric **
5946978Seric **	Typically this is a copy of the transcript plus a copy of the
5956978Seric **	original offending message.
5966978Seric **
597297Seric **	Parameters:
59865870Seric **		mci -- the mailer connection information.
5999542Seric **		e -- the envelope we are working in.
60067546Seric **		separator -- any possible MIME separator.
60167936Seric **		flags -- to modify the behaviour.
602297Seric **
603297Seric **	Returns:
604297Seric **		none
605297Seric **
606297Seric **	Side Effects:
6076978Seric **		Outputs the body of an error message.
608297Seric */
609297Seric 
61069748Seric void
61168228Seric errbody(mci, e, separator)
61265870Seric 	register MCI *mci;
6139542Seric 	register ENVELOPE *e;
61467546Seric 	char *separator;
615297Seric {
6166978Seric 	register FILE *xfile;
61759082Seric 	char *p;
61859082Seric 	register ADDRESS *q;
61959082Seric 	bool printheader;
62068559Seric 	bool sendbody;
6213189Seric 	char buf[MAXLINE];
62269858Seric 	extern char *xuntextify();
623297Seric 
62467546Seric 	if (bitset(MCIF_INHEADER, mci->mci_flags))
62567546Seric 	{
62667546Seric 		putline("", mci);
62767546Seric 		mci->mci_flags &= ~MCIF_INHEADER;
62867546Seric 	}
62958680Seric 	if (e->e_parent == NULL)
63058680Seric 	{
63158680Seric 		syserr("errbody: null parent");
63265870Seric 		putline("   ----- Original message lost -----\n", mci);
63358680Seric 		return;
63458680Seric 	}
63558680Seric 
6369057Seric 	/*
63759730Seric 	**  Output MIME header.
63859730Seric 	*/
63959730Seric 
64059730Seric 	if (e->e_msgboundary != NULL)
64159730Seric 	{
64265870Seric 		putline("This is a MIME-encapsulated message", mci);
64365870Seric 		putline("", mci);
64459730Seric 		(void) sprintf(buf, "--%s", e->e_msgboundary);
64565870Seric 		putline(buf, mci);
64665870Seric 		putline("", mci);
64759730Seric 	}
64859730Seric 
64959730Seric 	/*
65063852Seric 	**  Output introductory information.
65163852Seric 	*/
65263852Seric 
65364718Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
65464718Seric 		if (bitset(QBADADDR, q->q_flags))
65564718Seric 			break;
65665054Seric 	if (q == NULL &&
65765054Seric 	    !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags))
65864718Seric 	{
65964718Seric 		putline("    **********************************************",
66065870Seric 			mci);
66164718Seric 		putline("    **      THIS IS A WARNING MESSAGE ONLY      **",
66265870Seric 			mci);
66364718Seric 		putline("    **  YOU DO NOT NEED TO RESEND YOUR MESSAGE  **",
66465870Seric 			mci);
66564718Seric 		putline("    **********************************************",
66665870Seric 			mci);
66765870Seric 		putline("", mci);
66864718Seric 	}
66964718Seric 	sprintf(buf, "The original message was received at %s",
67064718Seric 		arpadate(ctime(&e->e_parent->e_ctime)));
67165870Seric 	putline(buf, mci);
67268529Seric 	expand("from \201_", buf, sizeof buf, e->e_parent);
67365870Seric 	putline(buf, mci);
67465870Seric 	putline("", mci);
67563852Seric 
67663852Seric 	/*
67755372Seric 	**  Output error message header (if specified and available).
67855372Seric 	*/
67955372Seric 
68067682Seric 	if (ErrMsgFile != NULL && !bitset(EF_SENDRECEIPT, e->e_parent->e_flags))
68155372Seric 	{
68255372Seric 		if (*ErrMsgFile == '/')
68355372Seric 		{
68455372Seric 			xfile = fopen(ErrMsgFile, "r");
68555372Seric 			if (xfile != NULL)
68655372Seric 			{
68755372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
68855425Seric 				{
68968529Seric 					expand(buf, buf, sizeof buf, e);
69065870Seric 					putline(buf, mci);
69155425Seric 				}
69255372Seric 				(void) fclose(xfile);
69365870Seric 				putline("\n", mci);
69455372Seric 			}
69555372Seric 		}
69655372Seric 		else
69755372Seric 		{
69868529Seric 			expand(ErrMsgFile, buf, sizeof buf, e);
69965870Seric 			putline(buf, mci);
70065870Seric 			putline("", mci);
70155372Seric 		}
70255372Seric 	}
70355372Seric 
70455372Seric 	/*
70559082Seric 	**  Output message introduction
70659082Seric 	*/
70759082Seric 
70859082Seric 	printheader = TRUE;
70959082Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
71059082Seric 	{
71168868Seric 		if (bitset(QBADADDR, q->q_flags))
71259082Seric 		{
71368868Seric 			if (!bitset(QPINGONFAILURE, q->q_flags))
71468603Seric 				continue;
71568868Seric 			p = "unrecoverable error";
71668868Seric 		}
71768868Seric 		else if (!bitset(QPRIMARY, q->q_flags))
71868868Seric 			continue;
71968868Seric 		else if (bitset(QRELAYED, q->q_flags))
72068868Seric 			p = "relayed to non-DSN-aware mailer";
72168868Seric 		else if (bitset(QDELIVERED, q->q_flags))
72268868Seric 		{
72368868Seric 			if (bitset(QEXPANDED, q->q_flags))
72468868Seric 				p = "successfully delivered to mailing list";
72563787Seric 			else
72668868Seric 				p = "successfully delivered to mailbox";
72768868Seric 		}
72868868Seric 		else if (bitset(QEXPANDED, q->q_flags))
72968868Seric 			p = "expanded by alias";
73068868Seric 		else if (bitset(QDELAYED, q->q_flags))
73168868Seric 			p = "transient failure";
73268868Seric 		else
73368868Seric 			continue;
73468868Seric 
73568868Seric 		if (printheader)
73668868Seric 		{
73768868Seric 			putline("   ----- The following addresses have delivery notifications -----",
73868868Seric 				mci);
73968868Seric 			printheader = FALSE;
74068868Seric 		}
74168868Seric 
74268868Seric 		sprintf(buf, "%s  (%s)", q->q_paddr, p);
74368868Seric 		putline(buf, mci);
74468868Seric 		if (q->q_alias != NULL)
74568868Seric 		{
74668868Seric 			strcpy(buf, "    (expanded from: ");
74768868Seric 			strcat(buf, q->q_alias->q_paddr);
74868868Seric 			strcat(buf, ")");
74965870Seric 			putline(buf, mci);
75059082Seric 		}
75159082Seric 	}
75259082Seric 	if (!printheader)
75365870Seric 		putline("\n", mci);
75459082Seric 
75559082Seric 	/*
7569057Seric 	**  Output transcript of errors
7579057Seric 	*/
7589057Seric 
7594086Seric 	(void) fflush(stdout);
7609542Seric 	p = queuename(e->e_parent, 'x');
7619337Seric 	if ((xfile = fopen(p, "r")) == NULL)
7629057Seric 	{
7639337Seric 		syserr("Cannot open %s", p);
76465870Seric 		putline("   ----- Transcript of session is unavailable -----\n", mci);
7659057Seric 	}
7669057Seric 	else
7679057Seric 	{
76868868Seric 		printheader = TRUE;
7699542Seric 		if (e->e_xfp != NULL)
7709542Seric 			(void) fflush(e->e_xfp);
7719057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
77268868Seric 		{
77368868Seric 			if (printheader)
77468868Seric 				putline("   ----- Transcript of session follows -----\n", mci);
77568868Seric 			printheader = FALSE;
77665870Seric 			putline(buf, mci);
77768868Seric 		}
77858680Seric 		(void) xfclose(xfile, "errbody xscript", p);
7799057Seric 	}
780297Seric 	errno = 0;
7814318Seric 
78268848Seric #if DSN
7834318Seric 	/*
78467880Seric 	**  Output machine-readable version.
78567880Seric 	*/
78667880Seric 
78767880Seric 	if (e->e_msgboundary != NULL)
78867880Seric 	{
78967880Seric 		putline("", mci);
79067880Seric 		(void) sprintf(buf, "--%s", e->e_msgboundary);
79167880Seric 		putline(buf, mci);
79269858Seric 		putline("Content-Type: message/X-delivery-status-05 (Draft of May 29, 1995)", mci);
79367880Seric 		putline("", mci);
79467880Seric 
79567880Seric 		/*
79667880Seric 		**  Output per-message information.
79767880Seric 		*/
79867880Seric 
79967880Seric 		/* original envelope id from MAIL FROM: line */
80067880Seric 		if (e->e_parent->e_envid != NULL)
80167880Seric 		{
80267880Seric 			(void) sprintf(buf, "Original-Envelope-Id: %s",
80369858Seric 				xuntextify(e->e_parent->e_envid));
80467880Seric 			putline(buf, mci);
80567880Seric 		}
80667880Seric 
80768228Seric 		/* Reporting-MTA: is us (required) */
80869858Seric 		(void) sprintf(buf, "Reporting-MTA: dns; %s", MyHostName);
80967880Seric 		putline(buf, mci);
81067880Seric 
81168868Seric 		/* DSN-Gateway: not relevant since we are not translating */
81268868Seric 
81368228Seric 		/* Received-From-MTA: shows where we got this message from */
81467990Seric 		if (RealHostName != NULL)
81567990Seric 		{
81668228Seric 			/* XXX use $s for type? */
81768228Seric 			p = e->e_parent->e_from.q_mailer->m_mtatype;
81868228Seric 			if (p == NULL)
81968228Seric 				p = "dns";
82068228Seric 			(void) sprintf(buf, "Received-From-MTA: %s; %s",
82169858Seric 				p, RealHostName);
82267990Seric 			putline(buf, mci);
82367990Seric 		}
82467963Seric 
82567963Seric 		/* Arrival-Date: -- when it arrived here */
82667963Seric 		(void) sprintf(buf, "Arrival-Date: %s",
82767963Seric 			arpadate(ctime(&e->e_parent->e_ctime)));
82867963Seric 		putline(buf, mci);
82967963Seric 
83067880Seric 		/*
83167880Seric 		**  Output per-address information.
83267880Seric 		*/
83367880Seric 
83467880Seric 		for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
83567880Seric 		{
83667880Seric 			register ADDRESS *r;
83768603Seric 			char *action;
83867880Seric 
83968603Seric 			if (bitset(QBADADDR, q->q_flags))
84068868Seric 				action = "failed";
84168603Seric 			else if (!bitset(QPRIMARY, q->q_flags))
84267880Seric 				continue;
84368868Seric 			else if (bitset(QDELIVERED, q->q_flags))
84468868Seric 			{
84568868Seric 				if (bitset(QEXPANDED, q->q_flags))
84668868Seric 					action = "delivered (to mailing list)";
84768868Seric 				else
84868868Seric 					action = "delivered (to mailbox)";
84968868Seric 			}
85068603Seric 			else if (bitset(QRELAYED, q->q_flags))
85168868Seric 				action = "relayed (to non-DSN-aware mailer)";
85268868Seric 			else if (bitset(QEXPANDED, q->q_flags))
85368868Seric 				action = "expanded (to multi-recipient alias)";
85468868Seric 			else if (bitset(QDELAYED, q->q_flags))
85568603Seric 				action = "delayed";
85668603Seric 			else
85768603Seric 				continue;
85868603Seric 
85967880Seric 			putline("", mci);
86067880Seric 
86168228Seric 			/* Original-Recipient: -- passed from on high */
86268228Seric 			if (q->q_orcpt != NULL)
86368228Seric 			{
86468228Seric 				(void) sprintf(buf, "Original-Recipient: %s",
86569858Seric 					q->q_orcpt);
86668228Seric 				putline(buf, mci);
86768228Seric 			}
86868228Seric 
86968228Seric 			/* Final-Recipient: -- the name from the RCPT command */
87068228Seric 			p = e->e_parent->e_from.q_mailer->m_addrtype;
87168228Seric 			if (p == NULL)
87268228Seric 				p = "rfc822";
87368228Seric 			for (r = q; r->q_alias != NULL; r = r->q_alias)
87468228Seric 				continue;
87568228Seric 			if (strchr(r->q_user, '@') == NULL)
87668583Seric 			{
87768583Seric 				(void) sprintf(buf, "Final-Recipient: %s; %s@",
87869858Seric 					p, r->q_user);
87969858Seric 				strcat(buf, MyHostName);
88068583Seric 			}
88167998Seric 			else
88268583Seric 			{
88368228Seric 				(void) sprintf(buf, "Final-Recipient: %s; %s",
88469858Seric 					p, r->q_user);
88568583Seric 			}
88667880Seric 			putline(buf, mci);
88767880Seric 
88868603Seric 			/* X-Actual-Recipient: -- the real problem address */
88969897Seric 			if (r != q && q->q_user[0] != '\0')
89068603Seric 			{
89168603Seric 				if (strchr(q->q_user, '@') == NULL)
89268603Seric 				{
89368603Seric 					(void) sprintf(buf, "X-Actual-Recipient: %s; %s@",
89469858Seric 						p, q->q_user);
89569858Seric 					strcat(buf, MyHostName);
89668603Seric 				}
89768603Seric 				else
89868603Seric 				{
89968603Seric 					(void) sprintf(buf, "X-Actual-Recipient: %s; %s",
90069858Seric 						p, q->q_user);
90168603Seric 				}
90268603Seric 				putline(buf, mci);
90368603Seric 			}
90468603Seric 
90567880Seric 			/* Action: -- what happened? */
90668603Seric 			sprintf(buf, "Action: %s", action);
90768603Seric 			putline(buf, mci);
90867880Seric 
90967880Seric 			/* Status: -- what _really_ happened? */
91067880Seric 			strcpy(buf, "Status: ");
91167880Seric 			if (q->q_status != NULL)
91267880Seric 				strcat(buf, q->q_status);
91367880Seric 			else if (bitset(QBADADDR, q->q_flags))
91468228Seric 				strcat(buf, "5.0.0");
91567880Seric 			else if (bitset(QQUEUEUP, q->q_flags))
91668228Seric 				strcat(buf, "4.0.0");
91767880Seric 			else
91868228Seric 				strcat(buf, "2.0.0");
91967880Seric 			putline(buf, mci);
92067880Seric 
92168228Seric 			/* Remote-MTA: -- who was I talking to? */
92268228Seric 			p = q->q_mailer->m_mtatype;
92368228Seric 			if (p == NULL)
92468228Seric 				p = "dns";
92568228Seric 			(void) sprintf(buf, "Remote-MTA: %s; ", p);
92668228Seric 			if (q->q_statmta != NULL)
92768228Seric 				p = q->q_statmta;
92868603Seric 			else if (q->q_host != NULL && q->q_host[0] != '\0')
92968228Seric 				p = q->q_host;
93068228Seric 			else
93168228Seric 				p = NULL;
93268228Seric 			if (p != NULL)
93368228Seric 			{
93468228Seric 				strcat(buf, p);
93568228Seric 				p = &buf[strlen(buf) - 1];
93668228Seric 				if (*p == '.')
93768228Seric 					*p = '\0';
93868228Seric 				putline(buf, mci);
93968228Seric 			}
94068228Seric 
94168228Seric 			/* Diagnostic-Code: -- actual result from other end */
94268228Seric 			if (q->q_rstatus != NULL)
94368228Seric 			{
94468228Seric 				p = q->q_mailer->m_diagtype;
94568228Seric 				if (p == NULL)
94668228Seric 					p = "smtp";
94768228Seric 				(void) sprintf(buf, "Diagnostic-Code: %s; %s",
94869858Seric 					p, q->q_rstatus);
94968228Seric 				putline(buf, mci);
95068228Seric 			}
95168228Seric 
95268228Seric 			/* Last-Attempt-Date: -- fine granularity */
95367880Seric 			if (q->q_statdate == (time_t) 0L)
95467880Seric 				q->q_statdate = curtime();
95568228Seric 			(void) sprintf(buf, "Last-Attempt-Date: %s",
95667880Seric 				arpadate(ctime(&q->q_statdate)));
95767880Seric 			putline(buf, mci);
95867880Seric 
95968868Seric 			/* Will-Retry-Until: -- for delayed messages only */
96067963Seric 			if (bitset(QQUEUEUP, q->q_flags) &&
96167963Seric 			    !bitset(QBADADDR, q->q_flags))
96267963Seric 			{
96367963Seric 				time_t xdate;
96467963Seric 
96567963Seric 				xdate = e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass];
96668868Seric 				sprintf(buf, "Will-Retry-Until: %s",
96767963Seric 					arpadate(ctime(&xdate)));
96867963Seric 				putline(buf, mci);
96967963Seric 			}
97067880Seric 		}
97167880Seric 	}
97268028Seric #endif
97367880Seric 
97467880Seric 	/*
9754318Seric 	**  Output text of original message
9764318Seric 	*/
9774318Seric 
97865870Seric 	putline("", mci);
97968564Seric 	if (bitset(EF_HAS_DF, e->e_parent->e_flags))
9804199Seric 	{
98168802Seric 		sendbody = !bitset(EF_NO_BODY_RETN, e->e_parent->e_flags) &&
98268802Seric 			   !bitset(EF_NO_BODY_RETN, e->e_flags);
98368559Seric 
98467880Seric 		if (e->e_msgboundary == NULL)
98567880Seric 		{
98668559Seric 			if (sendbody)
98767880Seric 				putline("   ----- Original message follows -----\n", mci);
98867880Seric 			else
98967880Seric 				putline("   ----- Message header follows -----\n", mci);
99067880Seric 			(void) fflush(mci->mci_out);
99167880Seric 		}
9925984Seric 		else
9935984Seric 		{
99459730Seric 			(void) sprintf(buf, "--%s", e->e_msgboundary);
995*69918Seric 
99665870Seric 			putline(buf, mci);
99768859Seric 			(void) sprintf(buf, "Content-Type: %s",
99868859Seric 				sendbody ? "message/rfc822"
99968859Seric 					 : "text/rfc822-headers");
100068228Seric 			putline(buf, mci);
1001*69918Seric 
1002*69918Seric 			p = hvalue("Content-Transfer-Encoding", e->e_parent->e_header);
1003*69918Seric 			if (p != NULL && strcasecmp(p, "binary") != 0)
1004*69918Seric 				p = NULL;
1005*69918Seric 			if (p == NULL && bitset(EF_HAS8BIT, e->e_parent->e_flags))
1006*69918Seric 				p = "8bit";
1007*69918Seric 			if (p != NULL)
1008*69918Seric 			{
1009*69918Seric 				(void) sprintf(buf, "Content-Transfer-Encoding: %s",
1010*69918Seric 					p);
1011*69918Seric 				putline(buf, mci);
1012*69918Seric 			}
10135984Seric 		}
101467981Seric 		putline("", mci);
101568228Seric 		putheader(mci, e->e_parent->e_header, e->e_parent);
101668559Seric 		if (sendbody)
101768228Seric 			putbody(mci, e->e_parent, e->e_msgboundary);
101868228Seric 		else if (e->e_msgboundary == NULL)
101967546Seric 		{
102067546Seric 			putline("", mci);
102165870Seric 			putline("   ----- Message body suppressed -----", mci);
102267546Seric 		}
10234199Seric 	}
102468228Seric 	else if (e->e_msgboundary == NULL)
102510170Seric 	{
102665870Seric 		putline("  ----- No message was collected -----\n", mci);
102710170Seric 	}
10284318Seric 
102960010Seric 	if (e->e_msgboundary != NULL)
103060010Seric 	{
103165870Seric 		putline("", mci);
103260010Seric 		(void) sprintf(buf, "--%s--", e->e_msgboundary);
103365870Seric 		putline(buf, mci);
103460010Seric 	}
103565870Seric 	putline("", mci);
103659730Seric 
10374318Seric 	/*
10384318Seric 	**  Cleanup and exit
10394318Seric 	*/
10404318Seric 
1041297Seric 	if (errno != 0)
10426978Seric 		syserr("errbody: I/O error");
1043297Seric }
104458144Seric /*
104568228Seric **  SMTPTODSN -- convert SMTP to DSN status code
104668228Seric **
104768228Seric **	Parameters:
104868228Seric **		smtpstat -- the smtp status code (e.g., 550).
104968228Seric **
105068228Seric **	Returns:
105168228Seric **		The DSN version of the status code.
105268228Seric */
105368228Seric 
105468228Seric char *
105568228Seric smtptodsn(smtpstat)
105668228Seric 	int smtpstat;
105768228Seric {
105868857Seric 	if (smtpstat < 0)
105968857Seric 		return "4.4.2";
106068857Seric 
106168228Seric 	switch (smtpstat)
106268228Seric 	{
106368228Seric 	  case 450:	/* Req mail action not taken: mailbox unavailable */
106468228Seric 		return "4.2.0";
106568228Seric 
106668228Seric 	  case 451:	/* Req action aborted: local error in processing */
106768228Seric 		return "4.3.0";
106868228Seric 
106968228Seric 	  case 452:	/* Req action not taken: insufficient sys storage */
107068228Seric 		return "4.3.1";
107168228Seric 
107268228Seric 	  case 500:	/* Syntax error, command unrecognized */
107368228Seric 		return "5.5.2";
107468228Seric 
107568228Seric 	  case 501:	/* Syntax error in parameters or arguments */
107668228Seric 		return "5.5.4";
107768228Seric 
107868228Seric 	  case 502:	/* Command not implemented */
107968228Seric 		return "5.5.1";
108068228Seric 
108168228Seric 	  case 503:	/* Bad sequence of commands */
108268228Seric 		return "5.5.1";
108368228Seric 
108468228Seric 	  case 504:	/* Command parameter not implemented */
108568228Seric 		return "5.5.4";
108668228Seric 
108768228Seric 	  case 550:	/* Req mail action not taken: mailbox unavailable */
108868228Seric 		return "5.2.0";
108968228Seric 
109068228Seric 	  case 551:	/* User not local; please try <...> */
109168228Seric 		return "5.1.6";
109268228Seric 
109368228Seric 	  case 552:	/* Req mail action aborted: exceeded storage alloc */
109468228Seric 		return "5.2.2";
109568228Seric 
109668228Seric 	  case 553:	/* Req action not taken: mailbox name not allowed */
109768228Seric 		return "5.1.3";
109868228Seric 
109968228Seric 	  case 554:	/* Transaction failed */
110068228Seric 		return "5.0.0";
110168228Seric 	}
110268228Seric 
110368228Seric 	if ((smtpstat / 100) == 2)
110468228Seric 		return "2.0.0";
110568228Seric 	if ((smtpstat / 100) == 4)
110668228Seric 		return "4.0.0";
110768228Seric 	return "5.0.0";
110868228Seric }
110968228Seric /*
111068228Seric **  XTEXTIFY -- take regular text and turn it into DSN-style xtext
111168228Seric **
111268228Seric **	Parameters:
111368228Seric **		t -- the text to convert.
111468228Seric **
111568228Seric **	Returns:
111668228Seric **		The xtext-ified version of the same string.
111768228Seric */
111868228Seric 
111968228Seric char *
112068228Seric xtextify(t)
112168228Seric 	register char *t;
112268228Seric {
112368228Seric 	register char *p;
112468228Seric 	int l;
112568228Seric 	int nbogus;
112668228Seric 	static char *bp = NULL;
112768228Seric 	static int bplen = 0;
112868228Seric 
112968228Seric 	/* figure out how long this xtext will have to be */
113068228Seric 	nbogus = l = 0;
113168228Seric 	for (p = t; *p != '\0'; p++)
113268228Seric 	{
113368228Seric 		register int c = (*p & 0xff);
113468228Seric 
113568228Seric 		/* ASCII dependence here -- this is the way the spec words it */
113668868Seric 		if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(')
113768228Seric 			nbogus++;
113868228Seric 		l++;
113968228Seric 	}
114068228Seric 	if (nbogus == 0)
114168228Seric 		return t;
114268228Seric 	l += nbogus * 2 + 1;
114368228Seric 
114468228Seric 	/* now allocate space if necessary for the new string */
114568228Seric 	if (l > bplen)
114668228Seric 	{
114768228Seric 		if (bp != NULL)
114868228Seric 			free(bp);
114968228Seric 		bp = xalloc(l);
115068228Seric 		bplen = l;
115168228Seric 	}
115268228Seric 
115368228Seric 	/* ok, copy the text with byte expansion */
115468228Seric 	for (p = bp; *t != '\0'; )
115568228Seric 	{
115668228Seric 		register int c = (*t++ & 0xff);
115768228Seric 
115868228Seric 		/* ASCII dependence here -- this is the way the spec words it */
115968228Seric 		if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(')
116068228Seric 		{
116168228Seric 			*p++ = '+';
116268228Seric 			*p++ = "0123456789abcdef"[c >> 4];
116368228Seric 			*p++ = "0123456789abcdef"[c & 0xf];
116468228Seric 		}
116568228Seric 		else
116668228Seric 			*p++ = c;
116768228Seric 	}
116868228Seric 	*p = '\0';
116968228Seric 	return bp;
117068228Seric }
117168228Seric /*
117269858Seric **  XUNTEXTIFY -- take xtext and turn it into plain text
117369858Seric **
117469858Seric **	Parameters:
117569858Seric **		t -- the xtextified text.
117669858Seric **
117769858Seric **	Returns:
117869858Seric **		The decoded text.  No attempt is made to deal with
117969858Seric **		null strings in the resulting text.
118069858Seric */
118169858Seric 
118269858Seric char *
118369858Seric xuntextify(t)
118469858Seric 	register char *t;
118569858Seric {
118669858Seric 	register char *p;
118769858Seric 	int l;
118869858Seric 	static char *bp = NULL;
118969858Seric 	static int bplen = 0;
119069858Seric 
119169858Seric 	/* heuristic -- if no plus sign, just return the input */
119269858Seric 	if (strchr(t, '+') == NULL)
119369858Seric 		return t;
119469858Seric 
119569858Seric 	/* xtext is always longer than decoded text */
119669858Seric 	l = strlen(t);
119769858Seric 	if (l > bplen)
119869858Seric 	{
119969858Seric 		if (bp != NULL)
120069858Seric 			free(bp);
120169858Seric 		bp = xalloc(l);
120269858Seric 		bplen = l;
120369858Seric 	}
120469858Seric 
120569858Seric 	/* ok, copy the text with byte compression */
120669858Seric 	for (p = bp; *t != '\0'; t++)
120769858Seric 	{
120869858Seric 		register int c = *t & 0xff;
120969858Seric 
121069858Seric 		if (c != '+')
121169858Seric 		{
121269858Seric 			*p++ = c;
121369858Seric 			continue;
121469858Seric 		}
121569858Seric 
121669858Seric 		c = *++t & 0xff;
121769858Seric 		if (!isascii(c) || !isxdigit(c))
121869858Seric 		{
121969858Seric 			/* error -- first digit is not hex */
122069858Seric 			usrerr("bogus xtext: +%c", c);
122169858Seric 			t--;
122269858Seric 			continue;
122369858Seric 		}
122469858Seric 		if (isdigit(c))
122569858Seric 			c -= '0';
122669858Seric 		else if (isupper(c))
122769858Seric 			c -= 'A' - 10;
122869858Seric 		else
122969858Seric 			c -= 'a' - 10;
123069858Seric 		*p = c << 4;
123169858Seric 
123269858Seric 		c = *++t & 0xff;
123369858Seric 		if (!isascii(c) || !isxdigit(c))
123469858Seric 		{
123569858Seric 			/* error -- second digit is not hex */
123669858Seric 			usrerr("bogus xtext: +%x%c", *p >> 4, c);
123769858Seric 			t--;
123869858Seric 			continue;
123969858Seric 		}
124069858Seric 		if (isdigit(c))
124169858Seric 			c -= '0';
124269858Seric 		else if (isupper(c))
124369858Seric 			c -= 'A' - 10;
124469858Seric 		else
124569858Seric 			c -= 'a' - 10;
124669858Seric 		*p++ |= c;
124769858Seric 	}
124869858Seric 	return bp;
124969858Seric }
125069858Seric /*
125168583Seric **  XTEXTOK -- check if a string is legal xtext
125268583Seric **
125368583Seric **	Xtext is used in Delivery Status Notifications.  The spec was
125468583Seric **	taken from draft-ietf-notary-mime-delivery-04.txt.
125568583Seric **
125668583Seric **	Parameters:
125768583Seric **		s -- the string to check.
125868583Seric **
125968583Seric **	Returns:
126068583Seric **		TRUE -- if 's' is legal xtext.
126168583Seric **		FALSE -- if it has any illegal characters in it.
126268583Seric */
126368583Seric 
126468583Seric bool
126568583Seric xtextok(s)
126668583Seric 	char *s;
126768583Seric {
126868583Seric 	int c;
126968583Seric 
127068583Seric 	while ((c = *s++) != '\0')
127168583Seric 	{
127268583Seric 		if (c == '+')
127368583Seric 		{
127468583Seric 			c = *s++;
127568583Seric 			if (!isascii(c) || !isxdigit(c))
127668583Seric 				return FALSE;
127768583Seric 			c = *s++;
127868583Seric 			if (!isascii(c) || !isxdigit(c))
127968583Seric 				return FALSE;
128068583Seric 		}
128169888Seric 		else if (c < '!' || c > '~' || c == '=')
128268583Seric 			return FALSE;
128368583Seric 	}
128468583Seric 	return TRUE;
128568583Seric }
128668583Seric /*
128758144Seric **  PRUNEROUTE -- prune an RFC-822 source route
128858144Seric **
128958144Seric **	Trims down a source route to the last internet-registered hop.
129058144Seric **	This is encouraged by RFC 1123 section 5.3.3.
129158144Seric **
129258144Seric **	Parameters:
129358144Seric **		addr -- the address
129458144Seric **
129558144Seric **	Returns:
129658144Seric **		TRUE -- address was modified
129758144Seric **		FALSE -- address could not be pruned
129858144Seric **
129958144Seric **	Side Effects:
130058144Seric **		modifies addr in-place
130158144Seric */
130258144Seric 
130369748Seric bool
130458144Seric pruneroute(addr)
130558144Seric 	char *addr;
130658144Seric {
130766334Seric #if NAMED_BIND
130858144Seric 	char *start, *at, *comma;
130958144Seric 	char c;
131058144Seric 	int rcode;
131158144Seric 	char hostbuf[BUFSIZ];
131258144Seric 	char *mxhosts[MAXMXHOSTS + 1];
131358144Seric 
131458144Seric 	/* check to see if this is really a route-addr */
131558144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
131658144Seric 		return FALSE;
131758144Seric 	start = strchr(addr, ':');
131858144Seric 	at = strrchr(addr, '@');
131958144Seric 	if (start == NULL || at == NULL || at < start)
132058144Seric 		return FALSE;
132158144Seric 
132258144Seric 	/* slice off the angle brackets */
132358144Seric 	strcpy(hostbuf, at + 1);
132458144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
132558144Seric 
132658144Seric 	while (start)
132758144Seric 	{
132859273Seric 		if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0)
132958144Seric 		{
133058144Seric 			strcpy(addr + 1, start + 1);
133158144Seric 			return TRUE;
133258144Seric 		}
133358144Seric 		c = *start;
133458144Seric 		*start = '\0';
133558144Seric 		comma = strrchr(addr, ',');
133658144Seric 		if (comma && comma[1] == '@')
133758144Seric 			strcpy(hostbuf, comma + 2);
133858144Seric 		else
133958144Seric 			comma = 0;
134058144Seric 		*start = c;
134158144Seric 		start = comma;
134258144Seric 	}
134358144Seric #endif
134458144Seric 	return FALSE;
134558144Seric }
1346