1297Seric # include <pwd.h>
23313Seric # include "sendmail.h"
3297Seric 
4*7370Seric SCCSID(@(#)savemail.c	3.39		07/05/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 
387361Seric # ifdef DEBUG
397361Seric 	if (Debug)
407361Seric 		printf("\nsavemail: exclusive %d\n", exclusive);
417361Seric # endif DEBUG
427361Seric 
436978Seric 	if (exclusive++)
44297Seric 		return;
456978Seric 	if (CurEnv->e_class <= PRI_JUNK)
466978Seric 	{
477053Seric 		message(Arpa_Info, "Dumping junk mail");
486978Seric 		return;
496978Seric 	}
507053Seric 	ForceMail = TRUE;
51*7370Seric 	FatalErrors = FALSE;
52297Seric 
53297Seric 	/*
54297Seric 	**  In the unhappy event we don't know who to return the mail
55297Seric 	**  to, make someone up.
56297Seric 	*/
57297Seric 
587045Seric 	if (CurEnv->e_from.q_paddr == NULL)
59297Seric 	{
607045Seric 		if (parse("root", &CurEnv->e_from, 0) == NULL)
61297Seric 		{
62297Seric 			syserr("Cannot parse root!");
63297Seric 			ExitStat = EX_SOFTWARE;
64297Seric 			finis();
65297Seric 		}
66297Seric 	}
676904Seric 	CurEnv->e_to = NULL;
68297Seric 
69297Seric 	/*
70401Seric 	**  If called from Eric Schmidt's network, do special mailback.
71401Seric 	**	Fundamentally, this is the mailback case except that
72401Seric 	**	it returns an OK exit status (assuming the return
73401Seric 	**	worked).
746989Seric 	**  Also, if the from address is not local, mail it back.
75297Seric 	*/
76297Seric 
77401Seric 	if (BerkNet)
78297Seric 	{
79401Seric 		ExitStat = EX_OK;
806989Seric 		MailBack = TRUE;
81297Seric 	}
827045Seric 	if (!bitset(M_LOCAL, CurEnv->e_from.q_mailer->m_flags))
836989Seric 		MailBack = TRUE;
84297Seric 
85297Seric 	/*
86297Seric 	**  If writing back, do it.
87297Seric 	**	If the user is still logged in on the same terminal,
88297Seric 	**	then write the error messages back to hir (sic).
89297Seric 	**	If not, set the MailBack flag so that it will get
90297Seric 	**	mailed back instead.
91297Seric 	*/
92297Seric 
93297Seric 	if (WriteBack)
94297Seric 	{
95297Seric 		p = ttypath();
96297Seric 		if (p == NULL || freopen(p, "w", stdout) == NULL)
97297Seric 		{
986989Seric 			MailBack = TRUE;
99297Seric 			errno = 0;
100297Seric 		}
101297Seric 		else
102297Seric 		{
1034712Seric 			(void) fflush(Xscript);
104401Seric 			xfile = fopen(Transcript, "r");
105401Seric 			if (xfile == NULL)
106401Seric 				syserr("Cannot open %s", Transcript);
1076978Seric 			expand("$n", buf, &buf[sizeof buf - 1], CurEnv);
1084162Seric 			printf("\r\nMessage from %s...\r\n", buf);
1094318Seric 			printf("Errors occurred while sending mail; transcript follows:\r\n");
1104086Seric 			while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout))
111297Seric 				fputs(buf, stdout);
112297Seric 			if (ferror(stdout))
1134086Seric 				(void) syserr("savemail: stdout: write err");
1144086Seric 			(void) fclose(xfile);
115297Seric 		}
116297Seric 	}
117297Seric 
118297Seric 	/*
119297Seric 	**  If mailing back, do it.
120297Seric 	**	Throw away all further output.  Don't do aliases, since
121297Seric 	**	this could cause loops, e.g., if joe mails to x:joe,
122297Seric 	**	and for some reason the network for x: is down, then
123297Seric 	**	the response gets sent to x:joe, which gives a
124297Seric 	**	response, etc.  Also force the mail to be delivered
125297Seric 	**	even if a version of it has already been sent to the
126297Seric 	**	sender.
127297Seric 	*/
128297Seric 
1293048Seric 	if (MailBack)
130297Seric 	{
1317203Seric 		if (CurEnv->e_errorqueue == NULL)
1327286Seric 			sendto(CurEnv->e_from.q_paddr, 1, (ADDRESS *) NULL, &CurEnv->e_errorqueue);
1337045Seric 		if (returntosender("Unable to deliver mail", CurEnv->e_errorqueue, TRUE) == 0)
134297Seric 			return;
135297Seric 	}
136297Seric 
137297Seric 	/*
138297Seric 	**  Save the message in dead.letter.
139297Seric 	**	If we weren't mailing back, and the user is local, we
140297Seric 	**	should save the message in dead.letter so that the
141297Seric 	**	poor person doesn't have to type it over again --
142297Seric 	**	and we all know what poor typists programmers are.
1437361Seric 	**	However, if we are running a "smart" protocol, we don't
1447361Seric 	**	bother to return the message, since the other end is
1457361Seric 	**	expected to handle that.
146297Seric 	*/
147297Seric 
1484712Seric 	if (ArpaMode)
1494162Seric 		return;
1504079Seric 	p = NULL;
1517045Seric 	if (CurEnv->e_from.q_mailer == LocalMailer)
152297Seric 	{
1537045Seric 		if (CurEnv->e_from.q_home != NULL)
1547045Seric 			p = CurEnv->e_from.q_home;
1557045Seric 		else if ((pw = getpwnam(CurEnv->e_from.q_user)) != NULL)
1564079Seric 			p = pw->pw_dir;
157297Seric 	}
1584079Seric 	if (p == NULL)
159297Seric 	{
1607045Seric 		syserr("Can't return mail to %s", CurEnv->e_from.q_paddr);
161297Seric # ifdef DEBUG
162297Seric 		p = "/usr/tmp";
163297Seric # endif
164297Seric 	}
1654199Seric 	if (p != NULL && TempFile != NULL)
166297Seric 	{
1675315Seric 		auto ADDRESS *q;
1687053Seric 		bool oldverb = Verbose;
1695315Seric 
170297Seric 		/* we have a home directory; open dead.letter */
1717053Seric 		Verbose = TRUE;
1724167Seric 		message(Arpa_Info, "Saving message in dead.letter");
1737053Seric 		Verbose = oldverb;
1744079Seric 		define('z', p);
1756978Seric 		expand("$z/dead.letter", buf, &buf[sizeof buf - 1], CurEnv);
1766904Seric 		CurEnv->e_to = buf;
1775315Seric 		q = NULL;
1785846Seric 		sendto(buf, -1, (ADDRESS *) NULL, &q);
1796978Seric 		(void) deliver(q);
180297Seric 	}
181297Seric 
182297Seric 	/* add terminator to writeback message */
183297Seric 	if (WriteBack)
184297Seric 		printf("-----\r\n");
185297Seric }
186297Seric /*
1874633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
1884633Seric **
1894633Seric **	Parameters:
1904633Seric **		msg -- the explanatory message.
1917045Seric **		returnto -- the queue of people to send the message to.
1925984Seric **		sendbody -- if TRUE, also send back the body of the
1935984Seric **			message; otherwise just send the header.
1944633Seric **
1954633Seric **	Returns:
1964633Seric **		zero -- if everything went ok.
1974633Seric **		else -- some error.
1984633Seric **
1994633Seric **	Side Effects:
2004633Seric **		Returns the current message to the sender via
2014633Seric **		mail.
2024633Seric */
2034633Seric 
2045984Seric static bool	SendBody;
2054633Seric 
2067045Seric #define MAXRETURNS	6	/* max depth of returning messages */
2077045Seric 
2086978Seric returntosender(msg, returnto, sendbody)
2094633Seric 	char *msg;
2106978Seric 	ADDRESS *returnto;
2115984Seric 	bool sendbody;
2124633Seric {
2134633Seric 	char buf[MAXNAME];
2146978Seric 	extern putheader(), errbody();
2156978Seric 	register ENVELOPE *ee;
2166978Seric 	extern ENVELOPE *newenvelope();
2176978Seric 	ENVELOPE errenvelope;
2187045Seric 	static int returndepth;
2194633Seric 
2207287Seric # ifdef DEBUG
2217287Seric 	if (Debug > 0)
2227287Seric 	{
2237287Seric 		printf("Return To Sender: msg=\"%s\", depth=%d, CurEnv=%x,\n",
2247287Seric 		       msg, returndepth, CurEnv);
2257287Seric 		printf("\treturnto=");
2267287Seric 		printaddr(returnto, FALSE);
2277287Seric 	}
2287287Seric # endif DEBUG
2297287Seric 
2307045Seric 	if (++returndepth >= MAXRETURNS)
2317045Seric 	{
2327045Seric 		if (returndepth != MAXRETURNS)
2337045Seric 			syserr("returntosender: infinite recursion on %s", returnto->q_paddr);
2347045Seric 		/* don't "unrecurse" and fake a clean exit */
2357045Seric 		/* returndepth--; */
2367045Seric 		return (0);
2377045Seric 	}
2387045Seric 
2396989Seric 	NoAlias = TRUE;
2405984Seric 	SendBody = sendbody;
2416978Seric 	ee = newenvelope(&errenvelope);
2426978Seric 	ee->e_puthdr = putheader;
2436978Seric 	ee->e_putbody = errbody;
2446978Seric 	addheader("date", "$b", ee);
2456978Seric 	addheader("from", "$g (Mail Delivery Subsystem)", ee);
2466978Seric 	addheader("to", returnto->q_paddr, ee);
2476978Seric 	addheader("subject", msg, ee);
2484633Seric 
2494633Seric 	/* fake up an address header for the from person */
2506978Seric 	expand("$n", buf, &buf[sizeof buf - 1], CurEnv);
2516978Seric 	if (parse(buf, &ee->e_from, -1) == NULL)
2524633Seric 	{
2534633Seric 		syserr("Can't parse myself!");
2544633Seric 		ExitStat = EX_SOFTWARE;
2557045Seric 		returndepth--;
2564633Seric 		return (-1);
2574633Seric 	}
2587045Seric 	ee->e_sendqueue = returnto;
2595984Seric 
2606978Seric 	/* push state into submessage */
2616978Seric 	CurEnv = ee;
2626978Seric 	define('f', "$n");
2636978Seric 	define('x', "Mail Delivery Subsystem");
2645984Seric 
2656978Seric 	/* actually deliver the error message */
2667045Seric 	sendall(ee, FALSE);
2676978Seric 
2687045Seric 	/* do any closing error processing */
2697045Seric 	checkerrors(ee);
2706978Seric 
2716978Seric 	/* restore state */
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