122711Sdist /*
222711Sdist **  Sendmail
322711Sdist **  Copyright (c) 1983  Eric P. Allman
422711Sdist **  Berkeley, California
522711Sdist **
622711Sdist **  Copyright (c) 1983 Regents of the University of California.
722711Sdist **  All rights reserved.  The Berkeley software License Agreement
822711Sdist **  specifies the terms and conditions for redistribution.
922711Sdist */
1022711Sdist 
1122711Sdist #ifndef lint
12*24955Seric static char	SccsId[] = "@(#)savemail.c	5.4 (Berkeley) 09/19/85";
1322711Sdist #endif not lint
1422711Sdist 
15297Seric # include <pwd.h>
163313Seric # include "sendmail.h"
17297Seric 
18297Seric /*
19297Seric **  SAVEMAIL -- Save mail on error
20297Seric **
219375Seric **	If mailing back errors, mail it back to the originator
22297Seric **	together with an error message; otherwise, just put it in
23297Seric **	dead.letter in the user's home directory (if he exists on
24297Seric **	this machine).
25297Seric **
26297Seric **	Parameters:
279337Seric **		e -- the envelope containing the message in error.
28297Seric **
29297Seric **	Returns:
30297Seric **		none
31297Seric **
32297Seric **	Side Effects:
33297Seric **		Saves the letter, by writing or mailing it back to the
34297Seric **		sender, or by putting it in dead.letter in her home
35297Seric **		directory.
36297Seric */
37297Seric 
3824942Seric /* defines for state machine */
3924942Seric # define ESM_REPORT	0	/* report to sender's terminal */
4024942Seric # define ESM_MAIL	1	/* mail back to sender */
4124942Seric # define ESM_QUIET	2	/* messages have already been returned */
4224942Seric # define ESM_DEADLETTER	3	/* save in ~/dead.letter */
4324942Seric # define ESM_POSTMASTER	4	/* return to postmaster */
4424942Seric # define ESM_USRTMP	5	/* save in /usr/tmp/dead.letter */
4524942Seric # define ESM_PANIC	6	/* leave the locked queue/transcript files */
4624942Seric # define ESM_DONE	7	/* the message is successfully delivered */
4724942Seric 
4824942Seric 
499337Seric savemail(e)
509337Seric 	register ENVELOPE *e;
51297Seric {
52297Seric 	register struct passwd *pw;
5324942Seric 	register FILE *fp;
5424942Seric 	int state;
5524942Seric 	auto ADDRESS *q;
56297Seric 	char buf[MAXLINE+1];
57297Seric 	extern struct passwd *getpwnam();
58297Seric 	register char *p;
59297Seric 	extern char *ttypath();
605846Seric 	typedef int (*fnptr)();
61297Seric 
627361Seric # ifdef DEBUG
637676Seric 	if (tTd(6, 1))
649375Seric 		printf("\nsavemail\n");
657361Seric # endif DEBUG
667361Seric 
679375Seric 	if (bitset(EF_RESPONSE, e->e_flags))
68297Seric 		return;
699337Seric 	if (e->e_class < 0)
706978Seric 	{
717053Seric 		message(Arpa_Info, "Dumping junk mail");
726978Seric 		return;
736978Seric 	}
747053Seric 	ForceMail = TRUE;
759337Seric 	e->e_flags &= ~EF_FATALERRS;
76297Seric 
77297Seric 	/*
78297Seric 	**  In the unhappy event we don't know who to return the mail
79297Seric 	**  to, make someone up.
80297Seric 	*/
81297Seric 
829337Seric 	if (e->e_from.q_paddr == NULL)
83297Seric 	{
8411447Seric 		if (parseaddr("root", &e->e_from, 0, '\0') == NULL)
85297Seric 		{
86297Seric 			syserr("Cannot parse root!");
87297Seric 			ExitStat = EX_SOFTWARE;
88297Seric 			finis();
89297Seric 		}
90297Seric 	}
919337Seric 	e->e_to = NULL;
92297Seric 
93297Seric 	/*
9424942Seric 	**  Basic state machine.
9524942Seric 	**
9624942Seric 	**	This machine runs through the following states:
9724942Seric 	**
9824942Seric 	**	ESM_QUIET	Errors have already been printed iff the
9924942Seric 	**			sender is local.
10024942Seric 	**	ESM_REPORT	Report directly to the sender's terminal.
10124942Seric 	**	ESM_MAIL	Mail response to the sender.
10224942Seric 	**	ESM_DEADLETTER	Save response in ~/dead.letter.
10324942Seric 	**	ESM_POSTMASTER	Mail response to the postmaster.
10424942Seric 	**	ESM_PANIC	Save response anywhere possible.
105297Seric 	*/
106297Seric 
10724942Seric 	/* determine starting state */
10824942Seric 	switch (ErrorMode)
109297Seric 	{
11024942Seric 	  case EM_WRITE:
11124942Seric 		state = ESM_REPORT;
11224942Seric 		break;
11324942Seric 
11424942Seric 	  case EM_BERKNET:
11524942Seric 		/* mail back, but return o.k. exit status */
116401Seric 		ExitStat = EX_OK;
11724942Seric 
11824942Seric 		/* fall through.... */
11924942Seric 
12024942Seric 	  case EM_MAIL:
12124942Seric 		state = ESM_MAIL;
12224942Seric 		break;
12324942Seric 
12424942Seric 	  case EM_PRINT:
12524942Seric 		state = ESM_QUIET;
12624942Seric 		break;
12724942Seric 
12824942Seric 	  case EM_QUIET:
12924942Seric 		/* no need to return anything at all */
13024942Seric 		return;
131297Seric 	}
132297Seric 
13324942Seric 	while (state != ESM_DONE)
134297Seric 	{
13524942Seric 		switch (state)
136297Seric 		{
13724942Seric 		  case ESM_REPORT:
13824942Seric 
13924942Seric 			/*
14024942Seric 			**  If the user is still logged in on the same terminal,
14124942Seric 			**  then write the error messages back to hir (sic).
14224942Seric 			*/
14324942Seric 
14424942Seric 			p = ttypath();
14524942Seric 			if (p == NULL || freopen(p, "w", stdout) == NULL)
14624942Seric 			{
14724942Seric 				state = ESM_MAIL;
14824942Seric 				break;
14924942Seric 			}
15024942Seric 
15116152Seric 			expand("\001n", buf, &buf[sizeof buf - 1], e);
1529375Seric 			printf("\r\nMessage from %s...\r\n", buf);
1539375Seric 			printf("Errors occurred while sending mail.\r\n");
1549542Seric 			if (e->e_xfp != NULL)
1559375Seric 			{
1569542Seric 				(void) fflush(e->e_xfp);
15724942Seric 				fp = fopen(queuename(e, 'x'), "r");
1589375Seric 			}
1599375Seric 			else
16024942Seric 				fp = NULL;
16124942Seric 			if (fp == NULL)
1629375Seric 			{
1639337Seric 				syserr("Cannot open %s", queuename(e, 'x'));
1649375Seric 				printf("Transcript of session is unavailable.\r\n");
1659375Seric 			}
1669375Seric 			else
1679375Seric 			{
1689375Seric 				printf("Transcript follows:\r\n");
16924942Seric 				while (fgets(buf, sizeof buf, fp) != NULL &&
1709375Seric 				       !ferror(stdout))
1719375Seric 					fputs(buf, stdout);
17224942Seric 				(void) fclose(fp);
1739375Seric 			}
17424942Seric 			printf("Original message will be saved in dead.letter.\r\n");
175297Seric 			if (ferror(stdout))
1764086Seric 				(void) syserr("savemail: stdout: write err");
17724942Seric 			state = ESM_DEADLETTER;
17824942Seric 			break;
179297Seric 
18024942Seric 		  case ESM_MAIL:
18124942Seric 		  case ESM_POSTMASTER:
18224942Seric 			/*
18324942Seric 			**  If mailing back, do it.
18424942Seric 			**	Throw away all further output.  Don't alias,
18524942Seric 			**	since this could cause loops, e.g., if joe
18624942Seric 			**	mails to joe@x, and for some reason the network
18724942Seric 			**	for @x is down, then the response gets sent to
18824942Seric 			**	joe@x, which gives a response, etc.  Also force
18924942Seric 			**	the mail to be delivered even if a version of
19024942Seric 			**	it has already been sent to the sender.
19124942Seric 			*/
192297Seric 
19324942Seric 			if (state == ESM_MAIL)
19424942Seric 			{
19524942Seric 				if (e->e_errorqueue == NULL)
19624942Seric 					sendtolist(e->e_from.q_paddr,
19724942Seric 						(ADDRESS *) NULL,
19824942Seric 						&e->e_errorqueue);
19924942Seric 				q = e->e_errorqueue;
20024942Seric 			}
20124942Seric 			else
20224942Seric 			{
203*24955Seric 				if (parseaddr("postmaster", q, 0, '\0') == NULL)
20424942Seric 				{
20524942Seric 					syserr("cannot parse postmaster!");
20624942Seric 					ExitStat = EX_SOFTWARE;
20724942Seric 					state = ESM_USRTMP;
20824942Seric 					break;
20924942Seric 				}
21024942Seric 			}
21124942Seric 			if (returntosender(e->e_message != NULL ? e->e_message :
21224942Seric 					   "Unable to deliver mail",
21324942Seric 					   q, TRUE) == 0)
21424942Seric 			{
21524942Seric 				state = ESM_DONE;
21624942Seric 				break;
21724942Seric 			}
218297Seric 
21924942Seric 			state = state == ESM_MAIL ? ESM_POSTMASTER : ESM_USRTMP;
22024942Seric 			break;
221297Seric 
22224942Seric 		  case ESM_DEADLETTER:
22324942Seric 			/*
22424942Seric 			**  Save the message in dead.letter.
22524942Seric 			**	If we weren't mailing back, and the user is
22624942Seric 			**	local, we should save the message in
22724942Seric 			**	~/dead.letter so that the poor person doesn't
22824942Seric 			**	have to type it over again -- and we all know
22924942Seric 			**	what poor typists UNIX users are.
23024942Seric 			*/
2315315Seric 
23224942Seric 			p = NULL;
23324942Seric 			if (e->e_from.q_mailer == LocalMailer)
23424942Seric 			{
23524942Seric 				if (e->e_from.q_home != NULL)
23624942Seric 					p = e->e_from.q_home;
23724942Seric 				else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
23824942Seric 					p = pw->pw_dir;
23924942Seric 			}
24024942Seric 			if (p == NULL)
24124942Seric 			{
24224942Seric 				syserr("Can't return mail to %s", e->e_from.q_paddr);
24324942Seric 				state = ESM_MAIL;
24424942Seric 				break;
24524942Seric 			}
24624942Seric 			if (e->e_dfp != NULL)
24724942Seric 			{
24824942Seric 				auto ADDRESS *q;
24924942Seric 				bool oldverb = Verbose;
25024942Seric 
25124942Seric 				/* we have a home directory; open dead.letter */
25224942Seric 				define('z', p, e);
25324942Seric 				expand("\001z/dead.letter", buf, &buf[sizeof buf - 1], e);
25424942Seric 				Verbose = TRUE;
25524942Seric 				message(Arpa_Info, "Saving message in %s", buf);
25624942Seric 				Verbose = oldverb;
25724942Seric 				e->e_to = buf;
25824942Seric 				q = NULL;
25924942Seric 				sendtolist(buf, (ADDRESS *) NULL, &q);
26024942Seric 				if (deliver(e, q) == 0)
26124942Seric 					state = ESM_DONE;
26224942Seric 				else
26324942Seric 					state = ESM_MAIL;
26424942Seric 			}
26524942Seric 			break;
26624942Seric 
26724942Seric 		  case ESM_USRTMP:
26824942Seric 			/*
26924942Seric 			**  Log the mail in /usr/tmp/dead.letter.
27024942Seric 			*/
27124942Seric 
27224942Seric 			fp = dfopen("/usr/tmp/dead.letter", "a");
27324942Seric 			if (fp == NULL)
27424942Seric 			{
27524942Seric 				state = ESM_PANIC;
27624942Seric 				break;
27724942Seric 			}
27824942Seric 
27924942Seric 			putfromline(fp, ProgMailer);
28024942Seric 			(*e->e_puthdr)(fp, ProgMailer, e);
28124942Seric 			putline("\n", fp, ProgMailer);
28224942Seric 			(*e->e_putbody)(fp, ProgMailer, e);
28324942Seric 			putline("\n", fp, ProgMailer);
28424942Seric 			(void) fflush(fp);
28524942Seric 			state = ferror(fp) ? ESM_PANIC : ESM_DONE;
28624942Seric 			(void) fclose(fp);
28724942Seric 			break;
28824942Seric 
28924942Seric 		  default:
29024942Seric 			syserr("savemail: unknown state %d", state);
29124942Seric 
29224942Seric 			/* fall through ... */
29324942Seric 
29424942Seric 		  case ESM_PANIC:
29524942Seric 			syserr("savemail: HELP!!!!");
29624942Seric # ifdef LOG
29724942Seric 			if (LogLevel >= 1)
29824942Seric 				syslog(LOG_ALERT, "savemail: HELP!!!!");
29924942Seric # endif LOG
30024942Seric 
30124942Seric 			/* leave the locked queue & transcript files around */
30224942Seric 			exit(EX_SOFTWARE);
30324942Seric 		}
304297Seric 	}
305297Seric }
306297Seric /*
3074633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
3084633Seric **
3094633Seric **	Parameters:
3104633Seric **		msg -- the explanatory message.
31116479Seric **		returnq -- the queue of people to send the message to.
3125984Seric **		sendbody -- if TRUE, also send back the body of the
3135984Seric **			message; otherwise just send the header.
3144633Seric **
3154633Seric **	Returns:
3164633Seric **		zero -- if everything went ok.
3174633Seric **		else -- some error.
3184633Seric **
3194633Seric **	Side Effects:
3204633Seric **		Returns the current message to the sender via
3214633Seric **		mail.
3224633Seric */
3234633Seric 
3245984Seric static bool	SendBody;
3254633Seric 
3267045Seric #define MAXRETURNS	6	/* max depth of returning messages */
3277045Seric 
32816479Seric returntosender(msg, returnq, sendbody)
3294633Seric 	char *msg;
33016479Seric 	ADDRESS *returnq;
3315984Seric 	bool sendbody;
3324633Seric {
3334633Seric 	char buf[MAXNAME];
3346978Seric 	extern putheader(), errbody();
3356978Seric 	register ENVELOPE *ee;
3366978Seric 	extern ENVELOPE *newenvelope();
3376978Seric 	ENVELOPE errenvelope;
3387045Seric 	static int returndepth;
3399375Seric 	register ADDRESS *q;
3404633Seric 
3417287Seric # ifdef DEBUG
3427676Seric 	if (tTd(6, 1))
3437287Seric 	{
3447287Seric 		printf("Return To Sender: msg=\"%s\", depth=%d, CurEnv=%x,\n",
3457287Seric 		       msg, returndepth, CurEnv);
34624942Seric 		printf("\treturnq=");
34716479Seric 		printaddr(returnq, TRUE);
3487287Seric 	}
3497287Seric # endif DEBUG
3507287Seric 
3517045Seric 	if (++returndepth >= MAXRETURNS)
3527045Seric 	{
3537045Seric 		if (returndepth != MAXRETURNS)
35416479Seric 			syserr("returntosender: infinite recursion on %s", returnq->q_paddr);
3557045Seric 		/* don't "unrecurse" and fake a clean exit */
3567045Seric 		/* returndepth--; */
3577045Seric 		return (0);
3587045Seric 	}
3597045Seric 
3605984Seric 	SendBody = sendbody;
36116152Seric 	define('g', "\001f", CurEnv);
3626978Seric 	ee = newenvelope(&errenvelope);
36324942Seric 	define('a', "\001b", ee);
3646978Seric 	ee->e_puthdr = putheader;
3656978Seric 	ee->e_putbody = errbody;
3669375Seric 	ee->e_flags |= EF_RESPONSE;
36716479Seric 	ee->e_sendqueue = returnq;
3689542Seric 	openxscript(ee);
36916479Seric 	for (q = returnq; q != NULL; q = q->q_next)
3709375Seric 	{
3719375Seric 		if (q->q_alias == NULL)
3729375Seric 			addheader("to", q->q_paddr, ee);
3739375Seric 	}
37424942Seric 
37510845Seric 	(void) sprintf(buf, "Returned mail: %s", msg);
37610106Seric 	addheader("subject", buf, ee);
3774633Seric 
3784633Seric 	/* fake up an address header for the from person */
37916152Seric 	expand("\001n", buf, &buf[sizeof buf - 1], CurEnv);
38011447Seric 	if (parseaddr(buf, &ee->e_from, -1, '\0') == NULL)
3814633Seric 	{
3824633Seric 		syserr("Can't parse myself!");
3834633Seric 		ExitStat = EX_SOFTWARE;
3847045Seric 		returndepth--;
3854633Seric 		return (-1);
3864633Seric 	}
38716159Seric 	loweraddr(&ee->e_from);
3885984Seric 
3896978Seric 	/* push state into submessage */
3906978Seric 	CurEnv = ee;
39116152Seric 	define('f', "\001n", ee);
3929375Seric 	define('x', "Mail Delivery Subsystem", ee);
39311291Seric 	eatheader(ee);
3945984Seric 
3956978Seric 	/* actually deliver the error message */
39614876Seric 	sendall(ee, SM_DEFAULT);
3976978Seric 
3986978Seric 	/* restore state */
3997811Seric 	dropenvelope(ee);
4006978Seric 	CurEnv = CurEnv->e_parent;
4017045Seric 	returndepth--;
4026978Seric 
4037045Seric 	/* should check for delivery errors here */
4044633Seric 	return (0);
4054633Seric }
4064633Seric /*
4076978Seric **  ERRBODY -- output the body of an error message.
4086978Seric **
4096978Seric **	Typically this is a copy of the transcript plus a copy of the
4106978Seric **	original offending message.
4116978Seric **
412297Seric **	Parameters:
413297Seric **		fp -- the output file.
41410170Seric **		m -- the mailer to output to.
4159542Seric **		e -- the envelope we are working in.
416297Seric **
417297Seric **	Returns:
418297Seric **		none
419297Seric **
420297Seric **	Side Effects:
4216978Seric **		Outputs the body of an error message.
422297Seric */
423297Seric 
42410170Seric errbody(fp, m, e)
425297Seric 	register FILE *fp;
4264318Seric 	register struct mailer *m;
4279542Seric 	register ENVELOPE *e;
428297Seric {
4296978Seric 	register FILE *xfile;
4303189Seric 	char buf[MAXLINE];
4319337Seric 	char *p;
432297Seric 
4339057Seric 	/*
4349057Seric 	**  Output transcript of errors
4359057Seric 	*/
4369057Seric 
4374086Seric 	(void) fflush(stdout);
4389542Seric 	p = queuename(e->e_parent, 'x');
4399337Seric 	if ((xfile = fopen(p, "r")) == NULL)
4409057Seric 	{
4419337Seric 		syserr("Cannot open %s", p);
4429057Seric 		fprintf(fp, "  ----- Transcript of session is unavailable -----\n");
4439057Seric 	}
4449057Seric 	else
4459057Seric 	{
4469057Seric 		fprintf(fp, "   ----- Transcript of session follows -----\n");
4479542Seric 		if (e->e_xfp != NULL)
4489542Seric 			(void) fflush(e->e_xfp);
4499057Seric 		while (fgets(buf, sizeof buf, xfile) != NULL)
45010170Seric 			putline(buf, fp, m);
4519057Seric 		(void) fclose(xfile);
4529057Seric 	}
453297Seric 	errno = 0;
4544318Seric 
4554318Seric 	/*
4564318Seric 	**  Output text of original message
4574318Seric 	*/
4584318Seric 
4594289Seric 	if (NoReturn)
4604289Seric 		fprintf(fp, "\n   ----- Return message suppressed -----\n\n");
4619542Seric 	else if (e->e_parent->e_dfp != NULL)
4624199Seric 	{
4635984Seric 		if (SendBody)
4645984Seric 		{
46510170Seric 			putline("\n", fp, m);
46610170Seric 			putline("   ----- Unsent message follows -----\n", fp, m);
4675984Seric 			(void) fflush(fp);
46810170Seric 			putheader(fp, m, e->e_parent);
46910170Seric 			putline("\n", fp, m);
47010170Seric 			putbody(fp, m, e->e_parent);
4715984Seric 		}
4725984Seric 		else
4735984Seric 		{
47410170Seric 			putline("\n", fp, m);
47510170Seric 			putline("  ----- Message header follows -----\n", fp, m);
4765984Seric 			(void) fflush(fp);
47710170Seric 			putheader(fp, m, e->e_parent);
4785984Seric 		}
4794199Seric 	}
4804199Seric 	else
48110170Seric 	{
48210170Seric 		putline("\n", fp, m);
48310170Seric 		putline("  ----- No message was collected -----\n", fp, m);
48410170Seric 		putline("\n", fp, m);
48510170Seric 	}
4864318Seric 
4874318Seric 	/*
4884318Seric 	**  Cleanup and exit
4894318Seric 	*/
4904318Seric 
491297Seric 	if (errno != 0)
4926978Seric 		syserr("errbody: I/O error");
493297Seric }
494