1297Seric # include <pwd.h>
23313Seric # include "sendmail.h"
3297Seric 
4*7053Seric SCCSID(@(#)savemail.c	3.33		06/07/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 	{
42*7053Seric 		message(Arpa_Info, "Dumping junk mail");
436978Seric 		return;
446978Seric 	}
45*7053Seric 	ForceMail = TRUE;
46297Seric 
47297Seric 	/*
48297Seric 	**  In the unhappy event we don't know who to return the mail
49297Seric 	**  to, make someone up.
50297Seric 	*/
51297Seric 
527045Seric 	if (CurEnv->e_from.q_paddr == NULL)
53297Seric 	{
547045Seric 		if (parse("root", &CurEnv->e_from, 0) == NULL)
55297Seric 		{
56297Seric 			syserr("Cannot parse root!");
57297Seric 			ExitStat = EX_SOFTWARE;
58297Seric 			finis();
59297Seric 		}
60297Seric 	}
616904Seric 	CurEnv->e_to = NULL;
62297Seric 
63297Seric 	/*
64401Seric 	**  If called from Eric Schmidt's network, do special mailback.
65401Seric 	**	Fundamentally, this is the mailback case except that
66401Seric 	**	it returns an OK exit status (assuming the return
67401Seric 	**	worked).
686989Seric 	**  Also, if the from address is not local, mail it back.
69297Seric 	*/
70297Seric 
71401Seric 	if (BerkNet)
72297Seric 	{
73401Seric 		ExitStat = EX_OK;
746989Seric 		MailBack = TRUE;
75297Seric 	}
767045Seric 	if (!bitset(M_LOCAL, CurEnv->e_from.q_mailer->m_flags))
776989Seric 		MailBack = TRUE;
78297Seric 
79297Seric 	/*
80297Seric 	**  If writing back, do it.
81297Seric 	**	If the user is still logged in on the same terminal,
82297Seric 	**	then write the error messages back to hir (sic).
83297Seric 	**	If not, set the MailBack flag so that it will get
84297Seric 	**	mailed back instead.
85297Seric 	*/
86297Seric 
87297Seric 	if (WriteBack)
88297Seric 	{
89297Seric 		p = ttypath();
90297Seric 		if (p == NULL || freopen(p, "w", stdout) == NULL)
91297Seric 		{
926989Seric 			MailBack = TRUE;
93297Seric 			errno = 0;
94297Seric 		}
95297Seric 		else
96297Seric 		{
974712Seric 			(void) fflush(Xscript);
98401Seric 			xfile = fopen(Transcript, "r");
99401Seric 			if (xfile == NULL)
100401Seric 				syserr("Cannot open %s", Transcript);
1016978Seric 			expand("$n", buf, &buf[sizeof buf - 1], CurEnv);
1024162Seric 			printf("\r\nMessage from %s...\r\n", buf);
1034318Seric 			printf("Errors occurred while sending mail; transcript follows:\r\n");
1044086Seric 			while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout))
105297Seric 				fputs(buf, stdout);
106297Seric 			if (ferror(stdout))
1074086Seric 				(void) syserr("savemail: stdout: write err");
1084086Seric 			(void) fclose(xfile);
109297Seric 		}
110297Seric 	}
111297Seric 
112297Seric 	/*
113297Seric 	**  If mailing back, do it.
114297Seric 	**	Throw away all further output.  Don't do aliases, since
115297Seric 	**	this could cause loops, e.g., if joe mails to x:joe,
116297Seric 	**	and for some reason the network for x: is down, then
117297Seric 	**	the response gets sent to x:joe, which gives a
118297Seric 	**	response, etc.  Also force the mail to be delivered
119297Seric 	**	even if a version of it has already been sent to the
120297Seric 	**	sender.
121297Seric 	*/
122297Seric 
1233048Seric 	if (MailBack)
124297Seric 	{
1257045Seric 		if (returntosender("Unable to deliver mail", CurEnv->e_errorqueue, TRUE) == 0)
126297Seric 			return;
127297Seric 	}
128297Seric 
129297Seric 	/*
130297Seric 	**  Save the message in dead.letter.
131297Seric 	**	If we weren't mailing back, and the user is local, we
132297Seric 	**	should save the message in dead.letter so that the
133297Seric 	**	poor person doesn't have to type it over again --
134297Seric 	**	and we all know what poor typists programmers are.
135297Seric 	*/
136297Seric 
1374712Seric 	if (ArpaMode)
1384162Seric 		return;
1394079Seric 	p = NULL;
1407045Seric 	if (CurEnv->e_from.q_mailer == LocalMailer)
141297Seric 	{
1427045Seric 		if (CurEnv->e_from.q_home != NULL)
1437045Seric 			p = CurEnv->e_from.q_home;
1447045Seric 		else if ((pw = getpwnam(CurEnv->e_from.q_user)) != NULL)
1454079Seric 			p = pw->pw_dir;
146297Seric 	}
1474079Seric 	if (p == NULL)
148297Seric 	{
1497045Seric 		syserr("Can't return mail to %s", CurEnv->e_from.q_paddr);
150297Seric # ifdef DEBUG
151297Seric 		p = "/usr/tmp";
152297Seric # else
153297Seric 		p = NULL;
154297Seric # endif
155297Seric 	}
1564199Seric 	if (p != NULL && TempFile != NULL)
157297Seric 	{
1585315Seric 		auto ADDRESS *q;
159*7053Seric 		bool oldverb = Verbose;
1605315Seric 
161297Seric 		/* we have a home directory; open dead.letter */
162*7053Seric 		Verbose = TRUE;
1634167Seric 		message(Arpa_Info, "Saving message in dead.letter");
164*7053Seric 		Verbose = oldverb;
1654079Seric 		define('z', p);
1666978Seric 		expand("$z/dead.letter", buf, &buf[sizeof buf - 1], CurEnv);
1676904Seric 		CurEnv->e_to = buf;
1685315Seric 		q = NULL;
1695846Seric 		sendto(buf, -1, (ADDRESS *) NULL, &q);
1706978Seric 		(void) deliver(q);
171297Seric 	}
172297Seric 
173297Seric 	/* add terminator to writeback message */
174297Seric 	if (WriteBack)
175297Seric 		printf("-----\r\n");
176297Seric }
177297Seric /*
1784633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
1794633Seric **
1804633Seric **	Parameters:
1814633Seric **		msg -- the explanatory message.
1827045Seric **		returnto -- the queue of people to send the message to.
1835984Seric **		sendbody -- if TRUE, also send back the body of the
1845984Seric **			message; otherwise just send the header.
1854633Seric **
1864633Seric **	Returns:
1874633Seric **		zero -- if everything went ok.
1884633Seric **		else -- some error.
1894633Seric **
1904633Seric **	Side Effects:
1914633Seric **		Returns the current message to the sender via
1924633Seric **		mail.
1934633Seric */
1944633Seric 
1955984Seric static bool	SendBody;
1964633Seric 
1977045Seric #define MAXRETURNS	6	/* max depth of returning messages */
1987045Seric 
1996978Seric returntosender(msg, returnto, sendbody)
2004633Seric 	char *msg;
2016978Seric 	ADDRESS *returnto;
2025984Seric 	bool sendbody;
2034633Seric {
2044633Seric 	char buf[MAXNAME];
2054633Seric 	register int i;
2066978Seric 	extern putheader(), errbody();
2076978Seric 	register ENVELOPE *ee;
2086978Seric 	extern ENVELOPE *newenvelope();
2096978Seric 	ENVELOPE errenvelope;
2107045Seric 	static int returndepth;
2114633Seric 
2127045Seric 	if (++returndepth >= MAXRETURNS)
2137045Seric 	{
2147045Seric 		if (returndepth != MAXRETURNS)
2157045Seric 			syserr("returntosender: infinite recursion on %s", returnto->q_paddr);
2167045Seric 		/* don't "unrecurse" and fake a clean exit */
2177045Seric 		/* returndepth--; */
2187045Seric 		return (0);
2197045Seric 	}
2207045Seric 
2216989Seric 	NoAlias = TRUE;
2225984Seric 	SendBody = sendbody;
2236978Seric 	ee = newenvelope(&errenvelope);
2246978Seric 	ee->e_puthdr = putheader;
2256978Seric 	ee->e_putbody = errbody;
2266978Seric 	addheader("date", "$b", ee);
2276978Seric 	addheader("from", "$g (Mail Delivery Subsystem)", ee);
2286978Seric 	addheader("to", returnto->q_paddr, ee);
2296978Seric 	addheader("subject", msg, ee);
2304633Seric 
2314633Seric 	/* fake up an address header for the from person */
2326978Seric 	expand("$n", buf, &buf[sizeof buf - 1], CurEnv);
2336978Seric 	if (parse(buf, &ee->e_from, -1) == NULL)
2344633Seric 	{
2354633Seric 		syserr("Can't parse myself!");
2364633Seric 		ExitStat = EX_SOFTWARE;
2377045Seric 		returndepth--;
2384633Seric 		return (-1);
2394633Seric 	}
2407045Seric 	ee->e_sendqueue = returnto;
2415984Seric 
2426978Seric 	/* push state into submessage */
2436978Seric 	CurEnv = ee;
2446978Seric 	define('f', "$n");
2456978Seric 	define('x', "Mail Delivery Subsystem");
2465984Seric 
2476978Seric 	/* actually deliver the error message */
2487045Seric 	sendall(ee, FALSE);
2496978Seric 
2507045Seric 	/* do any closing error processing */
2517045Seric 	checkerrors(ee);
2526978Seric 
2536978Seric 	/* restore state */
2546978Seric 	CurEnv = CurEnv->e_parent;
2557045Seric 	returndepth--;
2566978Seric 
2577045Seric 	/* should check for delivery errors here */
2584633Seric 	return (0);
2594633Seric }
2604633Seric /*
2616978Seric **  ERRBODY -- output the body of an error message.
2626978Seric **
2636978Seric **	Typically this is a copy of the transcript plus a copy of the
2646978Seric **	original offending message.
2656978Seric **
266297Seric **	Parameters:
267297Seric **		xfile -- the transcript file.
268297Seric **		fp -- the output file.
2696978Seric **		xdot -- if set, use the SMTP hidden dot algorithm.
270297Seric **
271297Seric **	Returns:
272297Seric **		none
273297Seric **
274297Seric **	Side Effects:
2756978Seric **		Outputs the body of an error message.
276297Seric */
277297Seric 
2786978Seric errbody(fp, m, xdot)
279297Seric 	register FILE *fp;
2804318Seric 	register struct mailer *m;
2814864Seric 	bool xdot;
282297Seric {
2836978Seric 	register FILE *xfile;
2843189Seric 	char buf[MAXLINE];
285297Seric 
2864086Seric 	(void) fflush(stdout);
2873189Seric 	if ((xfile = fopen(Transcript, "r")) == NULL)
288297Seric 		syserr("Cannot open %s", Transcript);
289297Seric 	errno = 0;
2904318Seric 
2914318Seric 	/*
2924318Seric 	**  Output transcript of errors
2934318Seric 	*/
2944318Seric 
2956978Seric 	fprintf(fp, "   ----- Transcript of session follows -----\n");
2964712Seric 	(void) fflush(Xscript);
2974086Seric 	while (fgets(buf, sizeof buf, xfile) != NULL)
2983189Seric 		fputs(buf, fp);
2994318Seric 
3004318Seric 	/*
3014318Seric 	**  Output text of original message
3024318Seric 	*/
3034318Seric 
3044289Seric 	if (NoReturn)
3054289Seric 		fprintf(fp, "\n   ----- Return message suppressed -----\n\n");
3064289Seric 	else if (TempFile != NULL)
3074199Seric 	{
3085984Seric 		if (SendBody)
3095984Seric 		{
3105984Seric 			fprintf(fp, "\n   ----- Unsent message follows -----\n");
3115984Seric 			(void) fflush(fp);
3127006Seric 			putheader(fp, m, CurEnv->e_parent);
3136978Seric 			fprintf(fp, "\n");
3147006Seric 			putbody(fp, m, xdot);
3155984Seric 		}
3165984Seric 		else
3175984Seric 		{
3185984Seric 			fprintf(fp, "\n  ----- Message header follows -----\n");
3195984Seric 			(void) fflush(fp);
3207006Seric 			putheader(fp, m, CurEnv);
3215984Seric 		}
3224199Seric 	}
3234199Seric 	else
3244199Seric 		fprintf(fp, "\n  ----- No message was collected -----\n\n");
3254318Seric 
3264318Seric 	/*
3274318Seric 	**  Cleanup and exit
3284318Seric 	*/
3294318Seric 
3304086Seric 	(void) fclose(xfile);
331297Seric 	if (errno != 0)
3326978Seric 		syserr("errbody: I/O error");
333297Seric }
334