1297Seric # include <pwd.h>
23313Seric # include "sendmail.h"
3297Seric 
4*7045Seric SCCSID(@(#)savemail.c	3.32		06/06/82);
5408Seric 
6297Seric /*
7297Seric **  SAVEMAIL -- Save mail on error
8297Seric **
9297Seric **	If the MailBack flag is set, mail it back to the originator
10297Seric **	together with an error message; otherwise, just put it in
11297Seric **	dead.letter in the user's home directory (if he exists on
12297Seric **	this machine).
13297Seric **
14297Seric **	Parameters:
15297Seric **		none
16297Seric **
17297Seric **	Returns:
18297Seric **		none
19297Seric **
20297Seric **	Side Effects:
21297Seric **		Saves the letter, by writing or mailing it back to the
22297Seric **		sender, or by putting it in dead.letter in her home
23297Seric **		directory.
24297Seric */
25297Seric 
26297Seric savemail()
27297Seric {
28297Seric 	register struct passwd *pw;
29297Seric 	register FILE *xfile;
30297Seric 	char buf[MAXLINE+1];
31297Seric 	extern struct passwd *getpwnam();
32297Seric 	register char *p;
33297Seric 	extern char *ttypath();
34297Seric 	static int exclusive;
355846Seric 	typedef int (*fnptr)();
366978Seric 	extern ENVELOPE *newenvelope();
37297Seric 
386978Seric 	if (exclusive++)
39297Seric 		return;
406978Seric 	if (CurEnv->e_class <= PRI_JUNK)
416978Seric 	{
426978Seric 		if (Verbose)
436978Seric 			message(Arpa_Info, "Dumping junk mail");
446978Seric 		return;
456978Seric 	}
46*7045Seric 	/* ForceMail = TRUE; */
47297Seric 
48297Seric 	/*
49297Seric 	**  In the unhappy event we don't know who to return the mail
50297Seric 	**  to, make someone up.
51297Seric 	*/
52297Seric 
53*7045Seric 	if (CurEnv->e_from.q_paddr == NULL)
54297Seric 	{
55*7045Seric 		if (parse("root", &CurEnv->e_from, 0) == NULL)
56297Seric 		{
57297Seric 			syserr("Cannot parse root!");
58297Seric 			ExitStat = EX_SOFTWARE;
59297Seric 			finis();
60297Seric 		}
61297Seric 	}
626904Seric 	CurEnv->e_to = NULL;
63297Seric 
64297Seric 	/*
65401Seric 	**  If called from Eric Schmidt's network, do special mailback.
66401Seric 	**	Fundamentally, this is the mailback case except that
67401Seric 	**	it returns an OK exit status (assuming the return
68401Seric 	**	worked).
696989Seric 	**  Also, if the from address is not local, mail it back.
70297Seric 	*/
71297Seric 
72401Seric 	if (BerkNet)
73297Seric 	{
74401Seric 		ExitStat = EX_OK;
756989Seric 		MailBack = TRUE;
76297Seric 	}
77*7045Seric 	if (!bitset(M_LOCAL, CurEnv->e_from.q_mailer->m_flags))
786989Seric 		MailBack = TRUE;
79297Seric 
80297Seric 	/*
81297Seric 	**  If writing back, do it.
82297Seric 	**	If the user is still logged in on the same terminal,
83297Seric 	**	then write the error messages back to hir (sic).
84297Seric 	**	If not, set the MailBack flag so that it will get
85297Seric 	**	mailed back instead.
86297Seric 	*/
87297Seric 
88297Seric 	if (WriteBack)
89297Seric 	{
90297Seric 		p = ttypath();
91297Seric 		if (p == NULL || freopen(p, "w", stdout) == NULL)
92297Seric 		{
936989Seric 			MailBack = TRUE;
94297Seric 			errno = 0;
95297Seric 		}
96297Seric 		else
97297Seric 		{
984712Seric 			(void) fflush(Xscript);
99401Seric 			xfile = fopen(Transcript, "r");
100401Seric 			if (xfile == NULL)
101401Seric 				syserr("Cannot open %s", Transcript);
1026978Seric 			expand("$n", buf, &buf[sizeof buf - 1], CurEnv);
1034162Seric 			printf("\r\nMessage from %s...\r\n", buf);
1044318Seric 			printf("Errors occurred while sending mail; transcript follows:\r\n");
1054086Seric 			while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout))
106297Seric 				fputs(buf, stdout);
107297Seric 			if (ferror(stdout))
1084086Seric 				(void) syserr("savemail: stdout: write err");
1094086Seric 			(void) fclose(xfile);
110297Seric 		}
111297Seric 	}
112297Seric 
113297Seric 	/*
114297Seric 	**  If mailing back, do it.
115297Seric 	**	Throw away all further output.  Don't do aliases, since
116297Seric 	**	this could cause loops, e.g., if joe mails to x:joe,
117297Seric 	**	and for some reason the network for x: is down, then
118297Seric 	**	the response gets sent to x:joe, which gives a
119297Seric 	**	response, etc.  Also force the mail to be delivered
120297Seric 	**	even if a version of it has already been sent to the
121297Seric 	**	sender.
122297Seric 	*/
123297Seric 
1243048Seric 	if (MailBack)
125297Seric 	{
126*7045Seric 		if (returntosender("Unable to deliver mail", CurEnv->e_errorqueue, TRUE) == 0)
127297Seric 			return;
128297Seric 	}
129297Seric 
130297Seric 	/*
131297Seric 	**  Save the message in dead.letter.
132297Seric 	**	If we weren't mailing back, and the user is local, we
133297Seric 	**	should save the message in dead.letter so that the
134297Seric 	**	poor person doesn't have to type it over again --
135297Seric 	**	and we all know what poor typists programmers are.
136297Seric 	*/
137297Seric 
1384712Seric 	if (ArpaMode)
1394162Seric 		return;
1404079Seric 	p = NULL;
141*7045Seric 	if (CurEnv->e_from.q_mailer == LocalMailer)
142297Seric 	{
143*7045Seric 		if (CurEnv->e_from.q_home != NULL)
144*7045Seric 			p = CurEnv->e_from.q_home;
145*7045Seric 		else if ((pw = getpwnam(CurEnv->e_from.q_user)) != NULL)
1464079Seric 			p = pw->pw_dir;
147297Seric 	}
1484079Seric 	if (p == NULL)
149297Seric 	{
150*7045Seric 		syserr("Can't return mail to %s", CurEnv->e_from.q_paddr);
151297Seric # ifdef DEBUG
152297Seric 		p = "/usr/tmp";
153297Seric # else
154297Seric 		p = NULL;
155297Seric # endif
156297Seric 	}
1574199Seric 	if (p != NULL && TempFile != NULL)
158297Seric 	{
1595315Seric 		auto ADDRESS *q;
1605315Seric 
161297Seric 		/* we have a home directory; open dead.letter */
1624167Seric 		message(Arpa_Info, "Saving message in dead.letter");
1634079Seric 		define('z', p);
1646978Seric 		expand("$z/dead.letter", buf, &buf[sizeof buf - 1], CurEnv);
1656904Seric 		CurEnv->e_to = buf;
1665315Seric 		q = NULL;
1675846Seric 		sendto(buf, -1, (ADDRESS *) NULL, &q);
1686978Seric 		(void) deliver(q);
169297Seric 	}
170297Seric 
171297Seric 	/* add terminator to writeback message */
172297Seric 	if (WriteBack)
173297Seric 		printf("-----\r\n");
174297Seric }
175297Seric /*
1764633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
1774633Seric **
1784633Seric **	Parameters:
1794633Seric **		msg -- the explanatory message.
180*7045Seric **		returnto -- the queue of people to send the message to.
1815984Seric **		sendbody -- if TRUE, also send back the body of the
1825984Seric **			message; otherwise just send the header.
1834633Seric **
1844633Seric **	Returns:
1854633Seric **		zero -- if everything went ok.
1864633Seric **		else -- some error.
1874633Seric **
1884633Seric **	Side Effects:
1894633Seric **		Returns the current message to the sender via
1904633Seric **		mail.
1914633Seric */
1924633Seric 
1935984Seric static bool	SendBody;
1944633Seric 
195*7045Seric #define MAXRETURNS	6	/* max depth of returning messages */
196*7045Seric 
1976978Seric returntosender(msg, returnto, sendbody)
1984633Seric 	char *msg;
1996978Seric 	ADDRESS *returnto;
2005984Seric 	bool sendbody;
2014633Seric {
2024633Seric 	char buf[MAXNAME];
2034633Seric 	register int i;
2046978Seric 	extern putheader(), errbody();
2056978Seric 	register ENVELOPE *ee;
2066978Seric 	extern ENVELOPE *newenvelope();
2076978Seric 	ENVELOPE errenvelope;
208*7045Seric 	static int returndepth;
2094633Seric 
210*7045Seric 	if (++returndepth >= MAXRETURNS)
211*7045Seric 	{
212*7045Seric 		if (returndepth != MAXRETURNS)
213*7045Seric 			syserr("returntosender: infinite recursion on %s", returnto->q_paddr);
214*7045Seric 		/* don't "unrecurse" and fake a clean exit */
215*7045Seric 		/* returndepth--; */
216*7045Seric 		return (0);
217*7045Seric 	}
218*7045Seric 
2196989Seric 	NoAlias = TRUE;
2205984Seric 	SendBody = sendbody;
2216978Seric 	ee = newenvelope(&errenvelope);
2226978Seric 	ee->e_puthdr = putheader;
2236978Seric 	ee->e_putbody = errbody;
2246978Seric 	addheader("date", "$b", ee);
2256978Seric 	addheader("from", "$g (Mail Delivery Subsystem)", ee);
2266978Seric 	addheader("to", returnto->q_paddr, ee);
2276978Seric 	addheader("subject", msg, ee);
2284633Seric 
2294633Seric 	/* fake up an address header for the from person */
2306978Seric 	expand("$n", buf, &buf[sizeof buf - 1], CurEnv);
2316978Seric 	if (parse(buf, &ee->e_from, -1) == NULL)
2324633Seric 	{
2334633Seric 		syserr("Can't parse myself!");
2344633Seric 		ExitStat = EX_SOFTWARE;
235*7045Seric 		returndepth--;
2364633Seric 		return (-1);
2374633Seric 	}
238*7045Seric 	ee->e_sendqueue = returnto;
2395984Seric 
2406978Seric 	/* push state into submessage */
2416978Seric 	CurEnv = ee;
2426978Seric 	define('f', "$n");
2436978Seric 	define('x', "Mail Delivery Subsystem");
2445984Seric 
2456978Seric 	/* actually deliver the error message */
246*7045Seric 	sendall(ee, FALSE);
2476978Seric 
248*7045Seric 	/* do any closing error processing */
249*7045Seric 	checkerrors(ee);
2506978Seric 
2516978Seric 	/* restore state */
2526978Seric 	CurEnv = CurEnv->e_parent;
253*7045Seric 	returndepth--;
2546978Seric 
255*7045Seric 	/* should check for delivery errors here */
2564633Seric 	return (0);
2574633Seric }
2584633Seric /*
2596978Seric **  ERRBODY -- output the body of an error message.
2606978Seric **
2616978Seric **	Typically this is a copy of the transcript plus a copy of the
2626978Seric **	original offending message.
2636978Seric **
264297Seric **	Parameters:
265297Seric **		xfile -- the transcript file.
266297Seric **		fp -- the output file.
2676978Seric **		xdot -- if set, use the SMTP hidden dot algorithm.
268297Seric **
269297Seric **	Returns:
270297Seric **		none
271297Seric **
272297Seric **	Side Effects:
2736978Seric **		Outputs the body of an error message.
274297Seric */
275297Seric 
2766978Seric errbody(fp, m, xdot)
277297Seric 	register FILE *fp;
2784318Seric 	register struct mailer *m;
2794864Seric 	bool xdot;
280297Seric {
2816978Seric 	register FILE *xfile;
2823189Seric 	char buf[MAXLINE];
283297Seric 
2844086Seric 	(void) fflush(stdout);
2853189Seric 	if ((xfile = fopen(Transcript, "r")) == NULL)
286297Seric 		syserr("Cannot open %s", Transcript);
287297Seric 	errno = 0;
2884318Seric 
2894318Seric 	/*
2904318Seric 	**  Output transcript of errors
2914318Seric 	*/
2924318Seric 
2936978Seric 	fprintf(fp, "   ----- Transcript of session follows -----\n");
2944712Seric 	(void) fflush(Xscript);
2954086Seric 	while (fgets(buf, sizeof buf, xfile) != NULL)
2963189Seric 		fputs(buf, fp);
2974318Seric 
2984318Seric 	/*
2994318Seric 	**  Output text of original message
3004318Seric 	*/
3014318Seric 
3024289Seric 	if (NoReturn)
3034289Seric 		fprintf(fp, "\n   ----- Return message suppressed -----\n\n");
3044289Seric 	else if (TempFile != NULL)
3054199Seric 	{
3065984Seric 		if (SendBody)
3075984Seric 		{
3085984Seric 			fprintf(fp, "\n   ----- Unsent message follows -----\n");
3095984Seric 			(void) fflush(fp);
3107006Seric 			putheader(fp, m, CurEnv->e_parent);
3116978Seric 			fprintf(fp, "\n");
3127006Seric 			putbody(fp, m, xdot);
3135984Seric 		}
3145984Seric 		else
3155984Seric 		{
3165984Seric 			fprintf(fp, "\n  ----- Message header follows -----\n");
3175984Seric 			(void) fflush(fp);
3187006Seric 			putheader(fp, m, CurEnv);
3195984Seric 		}
3204199Seric 	}
3214199Seric 	else
3224199Seric 		fprintf(fp, "\n  ----- No message was collected -----\n\n");
3234318Seric 
3244318Seric 	/*
3254318Seric 	**  Cleanup and exit
3264318Seric 	*/
3274318Seric 
3284086Seric 	(void) fclose(xfile);
329297Seric 	if (errno != 0)
3306978Seric 		syserr("errbody: I/O error");
331297Seric }
332