122711Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
362531Sbostic  * Copyright (c) 1988, 1993
462531Sbostic  *	The Regents of the University of California.  All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822711Sdist 
922711Sdist #ifndef lint
10*66303Seric static char sccsid[] = "@(#)savemail.c	8.26 (Berkeley) 03/04/94";
1133731Sbostic #endif /* not lint */
1222711Sdist 
1363937Seric # include "sendmail.h"
14297Seric # include <pwd.h>
15297Seric 
16297Seric /*
17297Seric **  SAVEMAIL -- Save mail on error
18297Seric **
199375Seric **	If mailing back errors, mail it back to the originator
20297Seric **	together with an error message; otherwise, just put it in
21297Seric **	dead.letter in the user's home directory (if he exists on
22297Seric **	this machine).
23297Seric **
24297Seric **	Parameters:
259337Seric **		e -- the envelope containing the message in error.
26297Seric **
27297Seric **	Returns:
28297Seric **		none
29297Seric **
30297Seric **	Side Effects:
31297Seric **		Saves the letter, by writing or mailing it back to the
32297Seric **		sender, or by putting it in dead.letter in her home
33297Seric **		directory.
34297Seric */
35297Seric 
3624942Seric /* defines for state machine */
3724942Seric # define ESM_REPORT	0	/* report to sender's terminal */
3824942Seric # define ESM_MAIL	1	/* mail back to sender */
3924942Seric # define ESM_QUIET	2	/* messages have already been returned */
4024942Seric # define ESM_DEADLETTER	3	/* save in ~/dead.letter */
4124942Seric # define ESM_POSTMASTER	4	/* return to postmaster */
4224942Seric # define ESM_USRTMP	5	/* save in /usr/tmp/dead.letter */
4324942Seric # define ESM_PANIC	6	/* leave the locked queue/transcript files */
4424942Seric # define ESM_DONE	7	/* the message is successfully delivered */
4524942Seric 
4665174Seric # ifndef _PATH_VARTMP
4765174Seric #  define _PATH_VARTMP	"/usr/tmp/"
4865174Seric # endif
4924942Seric 
5065174Seric 
519337Seric savemail(e)
529337Seric 	register ENVELOPE *e;
53297Seric {
54297Seric 	register struct passwd *pw;
5524942Seric 	register FILE *fp;
5624942Seric 	int state;
5759290Seric 	auto ADDRESS *q = NULL;
5865870Seric 	register char *p;
5965870Seric 	MCI mcibuf;
60297Seric 	char buf[MAXLINE+1];
61297Seric 	extern struct passwd *getpwnam();
62297Seric 	extern char *ttypath();
635846Seric 	typedef int (*fnptr)();
6464945Seric 	extern bool writable();
65297Seric 
667676Seric 	if (tTd(6, 1))
6758680Seric 	{
6859101Seric 		printf("\nsavemail, errormode = %c, id = %s\n  e_from=",
6959101Seric 			e->e_errormode, e->e_id == NULL ? "NONE" : e->e_id);
7058680Seric 		printaddr(&e->e_from, FALSE);
7158680Seric 	}
727361Seric 
7359101Seric 	if (e->e_id == NULL)
7459101Seric 	{
7559101Seric 		/* can't return a message with no id */
7659101Seric 		return;
7759101Seric 	}
7859101Seric 
79297Seric 	/*
80297Seric 	**  In the unhappy event we don't know who to return the mail
81297Seric 	**  to, make someone up.
82297Seric 	*/
83297Seric 
849337Seric 	if (e->e_from.q_paddr == NULL)
85297Seric 	{
8658733Seric 		e->e_sender = "Postmaster";
8764284Seric 		if (parseaddr(e->e_sender, &e->e_from,
8864284Seric 			      RF_COPYPARSE|RF_SENDERADDR, '\0', NULL, e) == NULL)
89297Seric 		{
9058733Seric 			syserr("553 Cannot parse Postmaster!");
91297Seric 			ExitStat = EX_SOFTWARE;
92297Seric 			finis();
93297Seric 		}
94297Seric 	}
959337Seric 	e->e_to = NULL;
96297Seric 
97297Seric 	/*
9824942Seric 	**  Basic state machine.
9924942Seric 	**
10024942Seric 	**	This machine runs through the following states:
10124942Seric 	**
10224942Seric 	**	ESM_QUIET	Errors have already been printed iff the
10324942Seric 	**			sender is local.
10424942Seric 	**	ESM_REPORT	Report directly to the sender's terminal.
10524942Seric 	**	ESM_MAIL	Mail response to the sender.
10624942Seric 	**	ESM_DEADLETTER	Save response in ~/dead.letter.
10724942Seric 	**	ESM_POSTMASTER	Mail response to the postmaster.
10824942Seric 	**	ESM_PANIC	Save response anywhere possible.
109297Seric 	*/
110297Seric 
11124942Seric 	/* determine starting state */
11258734Seric 	switch (e->e_errormode)
113297Seric 	{
11424942Seric 	  case EM_WRITE:
11524942Seric 		state = ESM_REPORT;
11624942Seric 		break;
11724942Seric 
11824942Seric 	  case EM_BERKNET:
11924942Seric 		/* mail back, but return o.k. exit status */
120401Seric 		ExitStat = EX_OK;
12124942Seric 
12224942Seric 		/* fall through.... */
12324942Seric 
12424942Seric 	  case EM_MAIL:
12524942Seric 		state = ESM_MAIL;
12624942Seric 		break;
12724942Seric 
12824942Seric 	  case EM_PRINT:
12924979Seric 	  case '\0':
13024942Seric 		state = ESM_QUIET;
13124942Seric 		break;
13224942Seric 
13324942Seric 	  case EM_QUIET:
13424942Seric 		/* no need to return anything at all */
13524942Seric 		return;
13624979Seric 
13724979Seric 	  default:
13858734Seric 		syserr("554 savemail: bogus errormode x%x\n", e->e_errormode);
13924979Seric 		state = ESM_MAIL;
14024979Seric 		break;
141297Seric 	}
142297Seric 
14359094Seric 	/* if this is already an error response, send to postmaster */
14459094Seric 	if (bitset(EF_RESPONSE, e->e_flags))
14559094Seric 	{
14659094Seric 		if (e->e_parent != NULL &&
14759094Seric 		    bitset(EF_RESPONSE, e->e_parent->e_flags))
14859094Seric 		{
14959094Seric 			/* got an error sending a response -- can it */
15059094Seric 			return;
15159094Seric 		}
15259094Seric 		state = ESM_POSTMASTER;
15359094Seric 	}
15459094Seric 
15524942Seric 	while (state != ESM_DONE)
156297Seric 	{
15724979Seric 		if (tTd(6, 5))
15824979Seric 			printf("  state %d\n", state);
15924979Seric 
16024942Seric 		switch (state)
161297Seric 		{
16224979Seric 		  case ESM_QUIET:
16324979Seric 			if (e->e_from.q_mailer == LocalMailer)
16424979Seric 				state = ESM_DEADLETTER;
16524979Seric 			else
16624979Seric 				state = ESM_MAIL;
16724979Seric 			break;
16824979Seric 
16924942Seric 		  case ESM_REPORT:
17024942Seric 
17124942Seric 			/*
17224942Seric 			**  If the user is still logged in on the same terminal,
17324942Seric 			**  then write the error messages back to hir (sic).
17424942Seric 			*/
17524942Seric 
17624942Seric 			p = ttypath();
17724942Seric 			if (p == NULL || freopen(p, "w", stdout) == NULL)
17824942Seric 			{
17924942Seric 				state = ESM_MAIL;
18024942Seric 				break;
18124942Seric 			}
18224942Seric 
18358050Seric 			expand("\201n", buf, &buf[sizeof buf - 1], e);
1849375Seric 			printf("\r\nMessage from %s...\r\n", buf);
1859375Seric 			printf("Errors occurred while sending mail.\r\n");
1869542Seric 			if (e->e_xfp != NULL)
1879375Seric 			{
1889542Seric 				(void) fflush(e->e_xfp);
18924942Seric 				fp = fopen(queuename(e, 'x'), "r");
1909375Seric 			}
1919375Seric 			else
19224942Seric 				fp = NULL;
19324942Seric 			if (fp == NULL)
1949375Seric 			{
1959337Seric 				syserr("Cannot open %s", queuename(e, 'x'));
1969375Seric 				printf("Transcript of session is unavailable.\r\n");
1979375Seric 			}
1989375Seric 			else
1999375Seric 			{
2009375Seric 				printf("Transcript follows:\r\n");
20124942Seric 				while (fgets(buf, sizeof buf, fp) != NULL &&
2029375Seric 				       !ferror(stdout))
2039375Seric 					fputs(buf, stdout);
20458680Seric 				(void) xfclose(fp, "savemail transcript", e->e_id);
2059375Seric 			}
20624942Seric 			printf("Original message will be saved in dead.letter.\r\n");
20724942Seric 			state = ESM_DEADLETTER;
20824942Seric 			break;
209297Seric 
21024942Seric 		  case ESM_MAIL:
21124942Seric 			/*
21224942Seric 			**  If mailing back, do it.
21324942Seric 			**	Throw away all further output.  Don't alias,
21424942Seric 			**	since this could cause loops, e.g., if joe
21524942Seric 			**	mails to joe@x, and for some reason the network
21624942Seric 			**	for @x is down, then the response gets sent to
21724942Seric 			**	joe@x, which gives a response, etc.  Also force
21824942Seric 			**	the mail to be delivered even if a version of
21924942Seric 			**	it has already been sent to the sender.
22063841Seric 			**
22163841Seric 			**  If this is a configuration or local software
22263841Seric 			**	error, send to the local postmaster as well,
22363841Seric 			**	since the originator can't do anything
22463841Seric 			**	about it anyway.  Note that this is a full
22563841Seric 			**	copy of the message (intentionally) so that
22663841Seric 			**	the Postmaster can forward things along.
22724942Seric 			*/
228297Seric 
22963841Seric 			if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE)
23063841Seric 			{
23163841Seric 				(void) sendtolist("postmaster",
23264284Seric 					  NULLADDR, &e->e_errorqueue, e);
23363841Seric 			}
23458680Seric 			if (strcmp(e->e_from.q_paddr, "<>") != 0)
23563841Seric 			{
23658680Seric 				(void) sendtolist(e->e_from.q_paddr,
23764284Seric 					  NULLADDR, &e->e_errorqueue, e);
23863841Seric 			}
23958680Seric 
24063841Seric 			/*
24163841Seric 			**  Deliver a non-delivery report to the
24263841Seric 			**  Postmaster-designate (not necessarily
24363841Seric 			**  Postmaster).  This does not include the
24463841Seric 			**  body of the message, for privacy reasons.
24563841Seric 			**  You really shouldn't need this.
24663841Seric 			*/
24763841Seric 
24863849Seric 			e->e_flags |= EF_PM_NOTIFY;
24958178Seric 
25064792Seric 			/* check to see if there are any good addresses */
25164792Seric 			for (q = e->e_errorqueue; q != NULL; q = q->q_next)
25264792Seric 				if (!bitset(QBADADDR|QDONTSEND, q->q_flags))
25364792Seric 					break;
25458680Seric 			if (q == NULL)
25558680Seric 			{
25658680Seric 				/* this is an error-error */
25758680Seric 				state = ESM_POSTMASTER;
25858680Seric 				break;
25958680Seric 			}
260*66303Seric 			if (returntosender(e->e_message, e->e_errorqueue,
261*66303Seric 					   (e->e_class >= 0), e) == 0)
26258680Seric 			{
26358680Seric 				state = ESM_DONE;
26458680Seric 				break;
26558680Seric 			}
26624981Seric 
26758680Seric 			/* didn't work -- return to postmaster */
26858680Seric 			state = ESM_POSTMASTER;
26958680Seric 			break;
27058306Seric 
27158680Seric 		  case ESM_POSTMASTER:
27258680Seric 			/*
27358680Seric 			**  Similar to previous case, but to system postmaster.
27458680Seric 			*/
27558680Seric 
27659432Seric 			q = NULL;
27759432Seric 			if (sendtolist("postmaster", NULL, &q, e) <= 0)
27824942Seric 			{
27958680Seric 				syserr("553 cannot parse postmaster!");
28058680Seric 				ExitStat = EX_SOFTWARE;
28158680Seric 				state = ESM_USRTMP;
28258680Seric 				break;
28324942Seric 			}
28458966Seric 			if (returntosender(e->e_message,
28557438Seric 					   q, (e->e_class >= 0), e) == 0)
28624942Seric 			{
28724942Seric 				state = ESM_DONE;
28824942Seric 				break;
28924942Seric 			}
290297Seric 
29158680Seric 			/* didn't work -- last resort */
29258680Seric 			state = ESM_USRTMP;
29324942Seric 			break;
294297Seric 
29524942Seric 		  case ESM_DEADLETTER:
29624942Seric 			/*
29724942Seric 			**  Save the message in dead.letter.
29824942Seric 			**	If we weren't mailing back, and the user is
29924942Seric 			**	local, we should save the message in
30024942Seric 			**	~/dead.letter so that the poor person doesn't
30124942Seric 			**	have to type it over again -- and we all know
30224942Seric 			**	what poor typists UNIX users are.
30324942Seric 			*/
3045315Seric 
30524942Seric 			p = NULL;
30624942Seric 			if (e->e_from.q_mailer == LocalMailer)
30724942Seric 			{
30824942Seric 				if (e->e_from.q_home != NULL)
30924942Seric 					p = e->e_from.q_home;
31024942Seric 				else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
31124942Seric 					p = pw->pw_dir;
31224942Seric 			}
31324942Seric 			if (p == NULL)
31424942Seric 			{
31558865Seric 				/* no local directory */
31624942Seric 				state = ESM_MAIL;
31724942Seric 				break;
31824942Seric 			}
31924942Seric 			if (e->e_dfp != NULL)
32024942Seric 			{
32124942Seric 				bool oldverb = Verbose;
32224942Seric 
32324942Seric 				/* we have a home directory; open dead.letter */
32424942Seric 				define('z', p, e);
32558050Seric 				expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e);
32624942Seric 				Verbose = TRUE;
32758151Seric 				message("Saving message in %s", buf);
32824942Seric 				Verbose = oldverb;
32924942Seric 				e->e_to = buf;
33024942Seric 				q = NULL;
33158082Seric 				(void) sendtolist(buf, &e->e_from, &q, e);
33264354Seric 				if (q != NULL &&
33364354Seric 				    !bitset(QBADADDR, q->q_flags) &&
33464354Seric 				    deliver(e, q) == 0)
33524942Seric 					state = ESM_DONE;
33624942Seric 				else
33724942Seric 					state = ESM_MAIL;
33824942Seric 			}
33925569Seric 			else
34025569Seric 			{
34125569Seric 				/* no data file -- try mailing back */
34225569Seric 				state = ESM_MAIL;
34325569Seric 			}
34424942Seric 			break;
34524942Seric 
34624942Seric 		  case ESM_USRTMP:
34724942Seric 			/*
34824942Seric 			**  Log the mail in /usr/tmp/dead.letter.
34924942Seric 			*/
35024942Seric 
35157438Seric 			if (e->e_class < 0)
35257438Seric 			{
35357438Seric 				state = ESM_DONE;
35457438Seric 				break;
35557438Seric 			}
35657438Seric 
35765174Seric 			strcpy(buf, _PATH_VARTMP);
35865174Seric 			strcat(buf, "dead.letter");
35965112Seric 			if (!writable(buf, NULLADDR, SFF_NOSLINK))
36064945Seric 			{
36164945Seric 				state = ESM_PANIC;
36264945Seric 				break;
36364945Seric 			}
36464945Seric 			fp = dfopen(buf, O_WRONLY|O_CREAT|O_APPEND, FileMode);
36524942Seric 			if (fp == NULL)
36624942Seric 			{
36724942Seric 				state = ESM_PANIC;
36824942Seric 				break;
36924942Seric 			}
37024942Seric 
37165870Seric 			bzero(&mcibuf, sizeof mcibuf);
37265870Seric 			mcibuf.mci_out = fp;
37365870Seric 			mcibuf.mci_mailer = FileMailer;
37465870Seric 			if (bitnset(M_7BITS, FileMailer->m_flags))
37565870Seric 				mcibuf.mci_flags |= MCIF_7BIT;
37665870Seric 
37765870Seric 			putfromline(&mcibuf, e);
37865870Seric 			(*e->e_puthdr)(&mcibuf, e);
37965870Seric 			putline("\n", &mcibuf);
38065870Seric 			(*e->e_putbody)(&mcibuf, e, NULL);
38165870Seric 			putline("\n", &mcibuf);
38224942Seric 			(void) fflush(fp);
38324942Seric 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
38458680Seric 			(void) xfclose(fp, "savemail", "/usr/tmp/dead.letter");
38524942Seric 			break;
38624942Seric 
38724942Seric 		  default:
38858151Seric 			syserr("554 savemail: unknown state %d", state);
38924942Seric 
39024942Seric 			/* fall through ... */
39124942Seric 
39224942Seric 		  case ESM_PANIC:
39324942Seric 			/* leave the locked queue & transcript files around */
39464949Seric 			syserr("!554 savemail: cannot save rejected email anywhere");
39524942Seric 		}
396297Seric 	}
397297Seric }
398297Seric /*
3994633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
4004633Seric **
4014633Seric **	Parameters:
4024633Seric **		msg -- the explanatory message.
40316479Seric **		returnq -- the queue of people to send the message to.
4045984Seric **		sendbody -- if TRUE, also send back the body of the
4055984Seric **			message; otherwise just send the header.
40655012Seric **		e -- the current envelope.
4074633Seric **
4084633Seric **	Returns:
4094633Seric **		zero -- if everything went ok.
4104633Seric **		else -- some error.
4114633Seric **
4124633Seric **	Side Effects:
4134633Seric **		Returns the current message to the sender via
4144633Seric **		mail.
4154633Seric */
4164633Seric 
4175984Seric static bool	SendBody;
4184633Seric 
4197045Seric #define MAXRETURNS	6	/* max depth of returning messages */
42058559Seric #define ERRORFUDGE	100	/* nominal size of error message text */
4217045Seric 
42255012Seric returntosender(msg, returnq, sendbody, e)
4234633Seric 	char *msg;
42416479Seric 	ADDRESS *returnq;
4255984Seric 	bool sendbody;
42655012Seric 	register ENVELOPE *e;
4274633Seric {
4284633Seric 	char buf[MAXNAME];
4296978Seric 	extern putheader(), errbody();
4306978Seric 	register ENVELOPE *ee;
43158680Seric 	ENVELOPE *oldcur = CurEnv;
4326978Seric 	ENVELOPE errenvelope;
4337045Seric 	static int returndepth;
4349375Seric 	register ADDRESS *q;
4354633Seric 
43660010Seric 	if (returnq == NULL)
43760010Seric 		return (-1);
43860010Seric 
43958966Seric 	if (msg == NULL)
44058966Seric 		msg = "Unable to deliver mail";
44158966Seric 
4427676Seric 	if (tTd(6, 1))
4437287Seric 	{
44458680Seric 		printf("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=",
44555012Seric 		       msg, returndepth, e);
44616479Seric 		printaddr(returnq, TRUE);
4477287Seric 	}
4487287Seric 
4497045Seric 	if (++returndepth >= MAXRETURNS)
4507045Seric 	{
4517045Seric 		if (returndepth != MAXRETURNS)
45258151Seric 			syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
4537045Seric 		/* don't "unrecurse" and fake a clean exit */
4547045Seric 		/* returndepth--; */
4557045Seric 		return (0);
4567045Seric 	}
4577045Seric 
4585984Seric 	SendBody = sendbody;
45958680Seric 	define('g', e->e_from.q_paddr, e);
46064940Seric 	define('u', NULL, e);
46158179Seric 	ee = newenvelope(&errenvelope, e);
46258050Seric 	define('a', "\201b", ee);
46359057Seric 	define('r', "internal", ee);
46459057Seric 	define('s', "localhost", ee);
46559057Seric 	define('_', "localhost", ee);
4666978Seric 	ee->e_puthdr = putheader;
4676978Seric 	ee->e_putbody = errbody;
46864120Seric 	ee->e_flags |= EF_RESPONSE|EF_METOO;
46955012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags))
47045155Seric 		ee->e_flags &= ~EF_OLDSTYLE;
47116479Seric 	ee->e_sendqueue = returnq;
47264655Seric 	ee->e_msgsize = ERRORFUDGE;
47364655Seric 	if (!NoReturn)
47464655Seric 		ee->e_msgsize += e->e_msgsize;
47564737Seric 	initsys(ee);
47616479Seric 	for (q = returnq; q != NULL; q = q->q_next)
4779375Seric 	{
47859989Seric 		if (bitset(QBADADDR, q->q_flags))
47958559Seric 			continue;
48058559Seric 
48159989Seric 		if (!bitset(QDONTSEND, q->q_flags))
48259989Seric 			ee->e_nrcpts++;
48358559Seric 
48458144Seric 		if (!DontPruneRoutes && pruneroute(q->q_paddr))
48564284Seric 			parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e);
48658144Seric 
4879375Seric 		if (q->q_alias == NULL)
48859580Seric 			addheader("To", q->q_paddr, ee);
4899375Seric 	}
49024942Seric 
49157642Seric # ifdef LOG
49258020Seric 	if (LogLevel > 5)
49365054Seric 		syslog(LOG_INFO, "%s: %s: return to sender: %s",
49457642Seric 			e->e_id, ee->e_id, msg);
49557642Seric # endif
49657642Seric 
49765054Seric 	(void) sprintf(buf, "Returned mail: %s", msg);
49865054Seric 	addheader("Subject", buf, ee);
49959730Seric 	if (SendMIMEErrors)
50059730Seric 	{
50159987Seric 		addheader("MIME-Version", "1.0", ee);
50259730Seric 		(void) sprintf(buf, "%s.%ld/%s",
50359730Seric 			ee->e_id, curtime(), MyHostName);
50459730Seric 		ee->e_msgboundary = newstr(buf);
50559730Seric 		(void) sprintf(buf, "multipart/mixed; boundary=\"%s\"",
50659730Seric 					ee->e_msgboundary);
50759730Seric 		addheader("Content-Type", buf, ee);
50859730Seric 	}
5094633Seric 
5104633Seric 	/* fake up an address header for the from person */
51158050Seric 	expand("\201n", buf, &buf[sizeof buf - 1], e);
51264284Seric 	if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL)
5134633Seric 	{
51458151Seric 		syserr("553 Can't parse myself!");
5154633Seric 		ExitStat = EX_SOFTWARE;
5167045Seric 		returndepth--;
5174633Seric 		return (-1);
5184633Seric 	}
51958704Seric 	ee->e_sender = ee->e_from.q_paddr;
5205984Seric 
5216978Seric 	/* push state into submessage */
5226978Seric 	CurEnv = ee;
52358050Seric 	define('f', "\201n", ee);
5249375Seric 	define('x', "Mail Delivery Subsystem", ee);
52558929Seric 	eatheader(ee, TRUE);
5265984Seric 
52763753Seric 	/* mark statistics */
52864284Seric 	markstats(ee, NULLADDR);
52963753Seric 
5306978Seric 	/* actually deliver the error message */
53114876Seric 	sendall(ee, SM_DEFAULT);
5326978Seric 
5336978Seric 	/* restore state */
5347811Seric 	dropenvelope(ee);
53558680Seric 	CurEnv = oldcur;
5367045Seric 	returndepth--;
5376978Seric 
5387045Seric 	/* should check for delivery errors here */
5394633Seric 	return (0);
5404633Seric }
5414633Seric /*
5426978Seric **  ERRBODY -- output the body of an error message.
5436978Seric **
5446978Seric **	Typically this is a copy of the transcript plus a copy of the
5456978Seric **	original offending message.
5466978Seric **
547297Seric **	Parameters:
54865870Seric **		mci -- the mailer connection information.
5499542Seric **		e -- the envelope we are working in.
550297Seric **
551297Seric **	Returns:
552297Seric **		none
553297Seric **
554297Seric **	Side Effects:
5556978Seric **		Outputs the body of an error message.
556297Seric */
557297Seric 
55865870Seric errbody(mci, e)
55965870Seric 	register MCI *mci;
5609542Seric 	register ENVELOPE *e;
561297Seric {
5626978Seric 	register FILE *xfile;
56359082Seric 	char *p;
56459082Seric 	register ADDRESS *q;
56559082Seric 	bool printheader;
5663189Seric 	char buf[MAXLINE];
567297Seric 
56858680Seric 	if (e->e_parent == NULL)
56958680Seric 	{
57058680Seric 		syserr("errbody: null parent");
57165870Seric 		putline("   ----- Original message lost -----\n", mci);
57258680Seric 		return;
57358680Seric 	}
57458680Seric 
5759057Seric 	/*
57659730Seric 	**  Output MIME header.
57759730Seric 	*/
57859730Seric 
57959730Seric 	if (e->e_msgboundary != NULL)
58059730Seric 	{
58165870Seric 		putline("This is a MIME-encapsulated message", mci);
58265870Seric 		putline("", mci);
58359730Seric 		(void) sprintf(buf, "--%s", e->e_msgboundary);
58465870Seric 		putline(buf, mci);
58565870Seric 		putline("", mci);
58659730Seric 	}
58759730Seric 
58859730Seric 	/*
58963852Seric 	**  Output introductory information.
59063852Seric 	*/
59163852Seric 
59264718Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
59364718Seric 		if (bitset(QBADADDR, q->q_flags))
59464718Seric 			break;
59565054Seric 	if (q == NULL &&
59665054Seric 	    !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags))
59764718Seric 	{
59864718Seric 		putline("    **********************************************",
59965870Seric 			mci);
60064718Seric 		putline("    **      THIS IS A WARNING MESSAGE ONLY      **",
60165870Seric 			mci);
60264718Seric 		putline("    **  YOU DO NOT NEED TO RESEND YOUR MESSAGE  **",
60365870Seric 			mci);
60464718Seric 		putline("    **********************************************",
60565870Seric 			mci);
60665870Seric 		putline("", mci);
60764718Seric 	}
60864718Seric 	sprintf(buf, "The original message was received at %s",
60964718Seric 		arpadate(ctime(&e->e_parent->e_ctime)));
61065870Seric 	putline(buf, mci);
61164025Seric 	expand("from \201_", buf, &buf[sizeof buf - 1], e->e_parent);
61265870Seric 	putline(buf, mci);
61365870Seric 	putline("", mci);
61463852Seric 
61563852Seric 	/*
61655372Seric 	**  Output error message header (if specified and available).
61755372Seric 	*/
61855372Seric 
61955372Seric 	if (ErrMsgFile != NULL)
62055372Seric 	{
62155372Seric 		if (*ErrMsgFile == '/')
62255372Seric 		{
62355372Seric 			xfile = fopen(ErrMsgFile, "r");
62455372Seric 			if (xfile != NULL)
62555372Seric 			{
62655372Seric 				while (fgets(buf, sizeof buf, xfile) != NULL)
62755425Seric 				{
62855425Seric 					expand(buf, buf, &buf[sizeof buf - 1], e);
62965870Seric 					putline(buf, mci);
63055425Seric 				}
63155372Seric 				(void) fclose(xfile);
63265870Seric 				putline("\n", mci);
63355372Seric 			}
63455372Seric 		}
63555372Seric 		else
63655372Seric 		{
63755425Seric 			expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
63865870Seric 			putline(buf, mci);
63965870Seric 			putline("", mci);
64055372Seric 		}
64155372Seric 	}
64255372Seric 
64355372Seric 	/*
64459082Seric 	**  Output message introduction
64559082Seric 	*/
64659082Seric 
64759082Seric 	printheader = TRUE;
64859082Seric 	for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next)
64959082Seric 	{
65063787Seric 		if (bitset(QBADADDR|QREPORT, q->q_flags))
65159082Seric 		{
65259082Seric 			if (printheader)
65359082Seric 			{
65463787Seric 				putline("   ----- The following addresses had delivery problems -----",
65565870Seric 					mci);
65659082Seric 				printheader = FALSE;
65759082Seric 			}
65863849Seric 			strcpy(buf, q->q_paddr);
65963787Seric 			if (bitset(QBADADDR, q->q_flags))
66063849Seric 				strcat(buf, "  (unrecoverable error)");
66163787Seric 			else
66263849Seric 				strcat(buf, "  (transient failure)");
66365870Seric 			putline(buf, mci);
66463849Seric 			if (q->q_alias != NULL)
66563849Seric 			{
66663849Seric 				strcpy(buf, "    (expanded from: ");
66763849Seric 				strcat(buf, q->q_alias->q_paddr);
66863849Seric 				strcat(buf, ")");
66965870Seric 				putline(buf, mci);
67063849Seric 			}
67159082Seric 		}
67259082Seric 	}
67359082Seric 	if (!printheader)
67465870Seric 		putline("\n", mci);
67559082Seric 
67659082Seric 	/*
6779057Seric 	**  Output transcript of errors
6789057Seric 	*/
6799057Seric 
6804086Seric 	(void) fflush(stdout);
6819542Seric 	p = queuename(e->e_parent, 'x');
6829337Seric 	if ((xfile = fopen(p, "r")) == NULL)
6839057Seric 	{
6849337Seric 		syserr("Cannot open %s", p);
68565870Seric 		putline("   ----- Transcript of session is unavailable -----\n", mci);
6869057Seric 	}
6879057Seric 	else
6889057Seric 	{
68965870Seric 		putline("   ----- Transcript of session follows -----\n", mci);
6909542Seric 		if (e->e_xfp != NULL)
6919542Seric 			(void) fflush(e->e_xfp);
6929057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
69365870Seric 			putline(buf, mci);
69458680Seric 		(void) xfclose(xfile, "errbody xscript", p);
6959057Seric 	}
696297Seric 	errno = 0;
6974318Seric 
6984318Seric 	/*
6994318Seric 	**  Output text of original message
7004318Seric 	*/
7014318Seric 
7024289Seric 	if (NoReturn)
70358665Seric 		SendBody = FALSE;
70465870Seric 	putline("", mci);
70558680Seric 	if (e->e_parent->e_df != NULL)
7064199Seric 	{
7075984Seric 		if (SendBody)
70865870Seric 			putline("   ----- Original message follows -----\n", mci);
7095984Seric 		else
71065870Seric 			putline("   ----- Message header follows -----\n", mci);
71165870Seric 		(void) fflush(mci->mci_out);
71259730Seric 
71359730Seric 		if (e->e_msgboundary != NULL)
7145984Seric 		{
71565870Seric 			putline("", mci);
71659730Seric 			(void) sprintf(buf, "--%s", e->e_msgboundary);
71765870Seric 			putline(buf, mci);
71865870Seric 			putline("Content-Type: message/rfc822", mci);
71965870Seric 			putline("", mci);
7205984Seric 		}
72165870Seric 		putheader(mci, e->e_parent);
72265870Seric 		putline("", mci);
72359730Seric 		if (SendBody)
72465870Seric 			putbody(mci, e->e_parent, e->e_msgboundary);
72559987Seric 		else
72665870Seric 			putline("   ----- Message body suppressed -----", mci);
7274199Seric 	}
7284199Seric 	else
72910170Seric 	{
73065870Seric 		putline("  ----- No message was collected -----\n", mci);
73110170Seric 	}
7324318Seric 
73360010Seric 	if (e->e_msgboundary != NULL)
73460010Seric 	{
73565870Seric 		putline("", mci);
73660010Seric 		(void) sprintf(buf, "--%s--", e->e_msgboundary);
73765870Seric 		putline(buf, mci);
73860010Seric 	}
73965870Seric 	putline("", mci);
74059730Seric 
7414318Seric 	/*
7424318Seric 	**  Cleanup and exit
7434318Seric 	*/
7444318Seric 
745297Seric 	if (errno != 0)
7466978Seric 		syserr("errbody: I/O error");
747297Seric }
74858144Seric /*
74958144Seric **  PRUNEROUTE -- prune an RFC-822 source route
75058144Seric **
75158144Seric **	Trims down a source route to the last internet-registered hop.
75258144Seric **	This is encouraged by RFC 1123 section 5.3.3.
75358144Seric **
75458144Seric **	Parameters:
75558144Seric **		addr -- the address
75658144Seric **
75758144Seric **	Returns:
75858144Seric **		TRUE -- address was modified
75958144Seric **		FALSE -- address could not be pruned
76058144Seric **
76158144Seric **	Side Effects:
76258144Seric **		modifies addr in-place
76358144Seric */
76458144Seric 
76558144Seric pruneroute(addr)
76658144Seric 	char *addr;
76758144Seric {
76858144Seric #ifdef NAMED_BIND
76958144Seric 	char *start, *at, *comma;
77058144Seric 	char c;
77158144Seric 	int rcode;
77258144Seric 	char hostbuf[BUFSIZ];
77358144Seric 	char *mxhosts[MAXMXHOSTS + 1];
77458144Seric 
77558144Seric 	/* check to see if this is really a route-addr */
77658144Seric 	if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
77758144Seric 		return FALSE;
77858144Seric 	start = strchr(addr, ':');
77958144Seric 	at = strrchr(addr, '@');
78058144Seric 	if (start == NULL || at == NULL || at < start)
78158144Seric 		return FALSE;
78258144Seric 
78358144Seric 	/* slice off the angle brackets */
78458144Seric 	strcpy(hostbuf, at + 1);
78558144Seric 	hostbuf[strlen(hostbuf) - 1] = '\0';
78658144Seric 
78758144Seric 	while (start)
78858144Seric 	{
78959273Seric 		if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0)
79058144Seric 		{
79158144Seric 			strcpy(addr + 1, start + 1);
79258144Seric 			return TRUE;
79358144Seric 		}
79458144Seric 		c = *start;
79558144Seric 		*start = '\0';
79658144Seric 		comma = strrchr(addr, ',');
79758144Seric 		if (comma && comma[1] == '@')
79858144Seric 			strcpy(hostbuf, comma + 2);
79958144Seric 		else
80058144Seric 			comma = 0;
80158144Seric 		*start = c;
80258144Seric 		start = comma;
80358144Seric 	}
80458144Seric #endif
80558144Seric 	return FALSE;
80658144Seric }
807