1297Seric # include <pwd.h>
23313Seric # include "sendmail.h"
3297Seric 
4*7811Seric SCCSID(@(#)savemail.c	3.41		08/22/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)();
36297Seric 
377361Seric # ifdef DEBUG
387676Seric 	if (tTd(6, 1))
397361Seric 		printf("\nsavemail: exclusive %d\n", exclusive);
407361Seric # endif DEBUG
417361Seric 
426978Seric 	if (exclusive++)
43297Seric 		return;
446978Seric 	if (CurEnv->e_class <= PRI_JUNK)
456978Seric 	{
467053Seric 		message(Arpa_Info, "Dumping junk mail");
476978Seric 		return;
486978Seric 	}
497053Seric 	ForceMail = TRUE;
507370Seric 	FatalErrors = FALSE;
51297Seric 
52297Seric 	/*
53297Seric 	**  In the unhappy event we don't know who to return the mail
54297Seric 	**  to, make someone up.
55297Seric 	*/
56297Seric 
577045Seric 	if (CurEnv->e_from.q_paddr == NULL)
58297Seric 	{
597045Seric 		if (parse("root", &CurEnv->e_from, 0) == NULL)
60297Seric 		{
61297Seric 			syserr("Cannot parse root!");
62297Seric 			ExitStat = EX_SOFTWARE;
63297Seric 			finis();
64297Seric 		}
65297Seric 	}
666904Seric 	CurEnv->e_to = NULL;
67297Seric 
68297Seric 	/*
69401Seric 	**  If called from Eric Schmidt's network, do special mailback.
70401Seric 	**	Fundamentally, this is the mailback case except that
71401Seric 	**	it returns an OK exit status (assuming the return
72401Seric 	**	worked).
736989Seric 	**  Also, if the from address is not local, mail it back.
74297Seric 	*/
75297Seric 
76401Seric 	if (BerkNet)
77297Seric 	{
78401Seric 		ExitStat = EX_OK;
796989Seric 		MailBack = TRUE;
80297Seric 	}
817045Seric 	if (!bitset(M_LOCAL, CurEnv->e_from.q_mailer->m_flags))
826989Seric 		MailBack = TRUE;
83297Seric 
84297Seric 	/*
85297Seric 	**  If writing back, do it.
86297Seric 	**	If the user is still logged in on the same terminal,
87297Seric 	**	then write the error messages back to hir (sic).
88297Seric 	**	If not, set the MailBack flag so that it will get
89297Seric 	**	mailed back instead.
90297Seric 	*/
91297Seric 
92297Seric 	if (WriteBack)
93297Seric 	{
94297Seric 		p = ttypath();
95297Seric 		if (p == NULL || freopen(p, "w", stdout) == NULL)
96297Seric 		{
976989Seric 			MailBack = TRUE;
98297Seric 			errno = 0;
99297Seric 		}
100297Seric 		else
101297Seric 		{
1024712Seric 			(void) fflush(Xscript);
103401Seric 			xfile = fopen(Transcript, "r");
104401Seric 			if (xfile == NULL)
105401Seric 				syserr("Cannot open %s", Transcript);
1066978Seric 			expand("$n", buf, &buf[sizeof buf - 1], CurEnv);
1074162Seric 			printf("\r\nMessage from %s...\r\n", buf);
1084318Seric 			printf("Errors occurred while sending mail; transcript follows:\r\n");
1094086Seric 			while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout))
110297Seric 				fputs(buf, stdout);
111297Seric 			if (ferror(stdout))
1124086Seric 				(void) syserr("savemail: stdout: write err");
1134086Seric 			(void) fclose(xfile);
114297Seric 		}
115297Seric 	}
116297Seric 
117297Seric 	/*
118297Seric 	**  If mailing back, do it.
119297Seric 	**	Throw away all further output.  Don't do aliases, since
120297Seric 	**	this could cause loops, e.g., if joe mails to x:joe,
121297Seric 	**	and for some reason the network for x: is down, then
122297Seric 	**	the response gets sent to x:joe, which gives a
123297Seric 	**	response, etc.  Also force the mail to be delivered
124297Seric 	**	even if a version of it has already been sent to the
125297Seric 	**	sender.
126297Seric 	*/
127297Seric 
1283048Seric 	if (MailBack)
129297Seric 	{
1307203Seric 		if (CurEnv->e_errorqueue == NULL)
1317286Seric 			sendto(CurEnv->e_from.q_paddr, 1, (ADDRESS *) NULL, &CurEnv->e_errorqueue);
1327045Seric 		if (returntosender("Unable to deliver mail", CurEnv->e_errorqueue, TRUE) == 0)
133297Seric 			return;
134297Seric 	}
135297Seric 
136297Seric 	/*
137297Seric 	**  Save the message in dead.letter.
138297Seric 	**	If we weren't mailing back, and the user is local, we
139297Seric 	**	should save the message in dead.letter so that the
140297Seric 	**	poor person doesn't have to type it over again --
141297Seric 	**	and we all know what poor typists programmers are.
1427361Seric 	**	However, if we are running a "smart" protocol, we don't
1437361Seric 	**	bother to return the message, since the other end is
1447361Seric 	**	expected to handle that.
145297Seric 	*/
146297Seric 
1474712Seric 	if (ArpaMode)
1484162Seric 		return;
1494079Seric 	p = NULL;
1507045Seric 	if (CurEnv->e_from.q_mailer == LocalMailer)
151297Seric 	{
1527045Seric 		if (CurEnv->e_from.q_home != NULL)
1537045Seric 			p = CurEnv->e_from.q_home;
1547045Seric 		else if ((pw = getpwnam(CurEnv->e_from.q_user)) != NULL)
1554079Seric 			p = pw->pw_dir;
156297Seric 	}
1574079Seric 	if (p == NULL)
158297Seric 	{
1597045Seric 		syserr("Can't return mail to %s", CurEnv->e_from.q_paddr);
160297Seric # ifdef DEBUG
161297Seric 		p = "/usr/tmp";
162297Seric # endif
163297Seric 	}
1644199Seric 	if (p != NULL && TempFile != NULL)
165297Seric 	{
1665315Seric 		auto ADDRESS *q;
1677053Seric 		bool oldverb = Verbose;
1685315Seric 
169297Seric 		/* we have a home directory; open dead.letter */
1707053Seric 		Verbose = TRUE;
1714167Seric 		message(Arpa_Info, "Saving message in dead.letter");
1727053Seric 		Verbose = oldverb;
1734079Seric 		define('z', p);
1746978Seric 		expand("$z/dead.letter", buf, &buf[sizeof buf - 1], CurEnv);
1756904Seric 		CurEnv->e_to = buf;
1765315Seric 		q = NULL;
1775846Seric 		sendto(buf, -1, (ADDRESS *) NULL, &q);
1786978Seric 		(void) deliver(q);
179297Seric 	}
180297Seric 
181297Seric 	/* add terminator to writeback message */
182297Seric 	if (WriteBack)
183297Seric 		printf("-----\r\n");
184297Seric }
185297Seric /*
1864633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
1874633Seric **
1884633Seric **	Parameters:
1894633Seric **		msg -- the explanatory message.
1907045Seric **		returnto -- the queue of people to send the message to.
1915984Seric **		sendbody -- if TRUE, also send back the body of the
1925984Seric **			message; otherwise just send the header.
1934633Seric **
1944633Seric **	Returns:
1954633Seric **		zero -- if everything went ok.
1964633Seric **		else -- some error.
1974633Seric **
1984633Seric **	Side Effects:
1994633Seric **		Returns the current message to the sender via
2004633Seric **		mail.
2014633Seric */
2024633Seric 
2035984Seric static bool	SendBody;
2044633Seric 
2057045Seric #define MAXRETURNS	6	/* max depth of returning messages */
2067045Seric 
2076978Seric returntosender(msg, returnto, sendbody)
2084633Seric 	char *msg;
2096978Seric 	ADDRESS *returnto;
2105984Seric 	bool sendbody;
2114633Seric {
2124633Seric 	char buf[MAXNAME];
2136978Seric 	extern putheader(), errbody();
2146978Seric 	register ENVELOPE *ee;
2156978Seric 	extern ENVELOPE *newenvelope();
2166978Seric 	ENVELOPE errenvelope;
2177045Seric 	static int returndepth;
2184633Seric 
2197287Seric # ifdef DEBUG
2207676Seric 	if (tTd(6, 1))
2217287Seric 	{
2227287Seric 		printf("Return To Sender: msg=\"%s\", depth=%d, CurEnv=%x,\n",
2237287Seric 		       msg, returndepth, CurEnv);
2247287Seric 		printf("\treturnto=");
2257287Seric 		printaddr(returnto, FALSE);
2267287Seric 	}
2277287Seric # endif DEBUG
2287287Seric 
2297045Seric 	if (++returndepth >= MAXRETURNS)
2307045Seric 	{
2317045Seric 		if (returndepth != MAXRETURNS)
2327045Seric 			syserr("returntosender: infinite recursion on %s", returnto->q_paddr);
2337045Seric 		/* don't "unrecurse" and fake a clean exit */
2347045Seric 		/* returndepth--; */
2357045Seric 		return (0);
2367045Seric 	}
2377045Seric 
2386989Seric 	NoAlias = TRUE;
2395984Seric 	SendBody = sendbody;
2406978Seric 	ee = newenvelope(&errenvelope);
2416978Seric 	ee->e_puthdr = putheader;
2426978Seric 	ee->e_putbody = errbody;
2436978Seric 	addheader("date", "$b", ee);
2446978Seric 	addheader("from", "$g (Mail Delivery Subsystem)", ee);
2456978Seric 	addheader("to", returnto->q_paddr, ee);
2466978Seric 	addheader("subject", msg, ee);
2474633Seric 
2484633Seric 	/* fake up an address header for the from person */
2496978Seric 	expand("$n", buf, &buf[sizeof buf - 1], CurEnv);
2506978Seric 	if (parse(buf, &ee->e_from, -1) == NULL)
2514633Seric 	{
2524633Seric 		syserr("Can't parse myself!");
2534633Seric 		ExitStat = EX_SOFTWARE;
2547045Seric 		returndepth--;
2554633Seric 		return (-1);
2564633Seric 	}
2577045Seric 	ee->e_sendqueue = returnto;
2585984Seric 
2596978Seric 	/* push state into submessage */
2606978Seric 	CurEnv = ee;
2616978Seric 	define('f', "$n");
2626978Seric 	define('x', "Mail Delivery Subsystem");
2635984Seric 
2646978Seric 	/* actually deliver the error message */
2657045Seric 	sendall(ee, FALSE);
2666978Seric 
2677045Seric 	/* do any closing error processing */
2687045Seric 	checkerrors(ee);
2696978Seric 
2706978Seric 	/* restore state */
271*7811Seric 	dropenvelope(ee);
2726978Seric 	CurEnv = CurEnv->e_parent;
2737045Seric 	returndepth--;
2746978Seric 
2757045Seric 	/* should check for delivery errors here */
2764633Seric 	return (0);
2774633Seric }
2784633Seric /*
2796978Seric **  ERRBODY -- output the body of an error message.
2806978Seric **
2816978Seric **	Typically this is a copy of the transcript plus a copy of the
2826978Seric **	original offending message.
2836978Seric **
284297Seric **	Parameters:
285297Seric **		xfile -- the transcript file.
286297Seric **		fp -- the output file.
2876978Seric **		xdot -- if set, use the SMTP hidden dot algorithm.
288297Seric **
289297Seric **	Returns:
290297Seric **		none
291297Seric **
292297Seric **	Side Effects:
2936978Seric **		Outputs the body of an error message.
294297Seric */
295297Seric 
2966978Seric errbody(fp, m, xdot)
297297Seric 	register FILE *fp;
2984318Seric 	register struct mailer *m;
2994864Seric 	bool xdot;
300297Seric {
3016978Seric 	register FILE *xfile;
3023189Seric 	char buf[MAXLINE];
3037124Seric 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
304297Seric 
3054086Seric 	(void) fflush(stdout);
3063189Seric 	if ((xfile = fopen(Transcript, "r")) == NULL)
307297Seric 		syserr("Cannot open %s", Transcript);
308297Seric 	errno = 0;
3094318Seric 
3104318Seric 	/*
3114318Seric 	**  Output transcript of errors
3124318Seric 	*/
3134318Seric 
3146978Seric 	fprintf(fp, "   ----- Transcript of session follows -----\n");
3154712Seric 	(void) fflush(Xscript);
3164086Seric 	while (fgets(buf, sizeof buf, xfile) != NULL)
3177124Seric 		putline(buf, fp, fullsmtp);
3184318Seric 
3194318Seric 	/*
3204318Seric 	**  Output text of original message
3214318Seric 	*/
3224318Seric 
3234289Seric 	if (NoReturn)
3244289Seric 		fprintf(fp, "\n   ----- Return message suppressed -----\n\n");
3254289Seric 	else if (TempFile != NULL)
3264199Seric 	{
3275984Seric 		if (SendBody)
3285984Seric 		{
3295984Seric 			fprintf(fp, "\n   ----- Unsent message follows -----\n");
3305984Seric 			(void) fflush(fp);
3317006Seric 			putheader(fp, m, CurEnv->e_parent);
3326978Seric 			fprintf(fp, "\n");
3337006Seric 			putbody(fp, m, xdot);
3345984Seric 		}
3355984Seric 		else
3365984Seric 		{
3375984Seric 			fprintf(fp, "\n  ----- Message header follows -----\n");
3385984Seric 			(void) fflush(fp);
3397006Seric 			putheader(fp, m, CurEnv);
3405984Seric 		}
3414199Seric 	}
3424199Seric 	else
3434199Seric 		fprintf(fp, "\n  ----- No message was collected -----\n\n");
3444318Seric 
3454318Seric 	/*
3464318Seric 	**  Cleanup and exit
3474318Seric 	*/
3484318Seric 
3494086Seric 	(void) fclose(xfile);
350297Seric 	if (errno != 0)
3516978Seric 		syserr("errbody: I/O error");
352297Seric }
353