1297Seric # include <pwd.h>
23313Seric # include "sendmail.h"
3297Seric 
4*6989Seric SCCSID(@(#)savemail.c	3.30		05/31/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 	ENVELOPE errenvelope;
376978Seric 	register ENVELOPE *ee;
386978Seric 	extern ENVELOPE *newenvelope();
39297Seric 
406978Seric 	if (exclusive++)
41297Seric 		return;
426978Seric 	if (CurEnv->e_class <= PRI_JUNK)
436978Seric 	{
446978Seric 		if (Verbose)
456978Seric 			message(Arpa_Info, "Dumping junk mail");
466978Seric 		return;
476978Seric 	}
485315Seric 	ForceMail = TRUE;
49297Seric 
50297Seric 	/*
51297Seric 	**  In the unhappy event we don't know who to return the mail
52297Seric 	**  to, make someone up.
53297Seric 	*/
54297Seric 
556904Seric 	if (CurEnv->e_from.q_paddr == NULL)
56297Seric 	{
576904Seric 		if (parse("root", &CurEnv->e_from, 0) == NULL)
58297Seric 		{
59297Seric 			syserr("Cannot parse root!");
60297Seric 			ExitStat = EX_SOFTWARE;
61297Seric 			finis();
62297Seric 		}
63297Seric 	}
646904Seric 	CurEnv->e_to = NULL;
65297Seric 
66297Seric 	/*
67401Seric 	**  If called from Eric Schmidt's network, do special mailback.
68401Seric 	**	Fundamentally, this is the mailback case except that
69401Seric 	**	it returns an OK exit status (assuming the return
70401Seric 	**	worked).
71*6989Seric 	**  Also, if the from address is not local, mail it back.
72297Seric 	*/
73297Seric 
74401Seric 	if (BerkNet)
75297Seric 	{
76401Seric 		ExitStat = EX_OK;
77*6989Seric 		MailBack = TRUE;
78297Seric 	}
79*6989Seric 	if (!bitset(M_LOCAL, CurEnv->e_from.q_mailer->m_flags))
80*6989Seric 		MailBack = TRUE;
81297Seric 
82297Seric 	/*
83297Seric 	**  If writing back, do it.
84297Seric 	**	If the user is still logged in on the same terminal,
85297Seric 	**	then write the error messages back to hir (sic).
86297Seric 	**	If not, set the MailBack flag so that it will get
87297Seric 	**	mailed back instead.
88297Seric 	*/
89297Seric 
90297Seric 	if (WriteBack)
91297Seric 	{
92297Seric 		p = ttypath();
93297Seric 		if (p == NULL || freopen(p, "w", stdout) == NULL)
94297Seric 		{
95*6989Seric 			MailBack = TRUE;
96297Seric 			errno = 0;
97297Seric 		}
98297Seric 		else
99297Seric 		{
1004712Seric 			(void) fflush(Xscript);
101401Seric 			xfile = fopen(Transcript, "r");
102401Seric 			if (xfile == NULL)
103401Seric 				syserr("Cannot open %s", Transcript);
1046978Seric 			expand("$n", buf, &buf[sizeof buf - 1], CurEnv);
1054162Seric 			printf("\r\nMessage from %s...\r\n", buf);
1064318Seric 			printf("Errors occurred while sending mail; transcript follows:\r\n");
1074086Seric 			while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout))
108297Seric 				fputs(buf, stdout);
109297Seric 			if (ferror(stdout))
1104086Seric 				(void) syserr("savemail: stdout: write err");
1114086Seric 			(void) fclose(xfile);
112297Seric 		}
113297Seric 	}
114297Seric 
115297Seric 	/*
116297Seric 	**  If mailing back, do it.
117297Seric 	**	Throw away all further output.  Don't do aliases, since
118297Seric 	**	this could cause loops, e.g., if joe mails to x:joe,
119297Seric 	**	and for some reason the network for x: is down, then
120297Seric 	**	the response gets sent to x:joe, which gives a
121297Seric 	**	response, etc.  Also force the mail to be delivered
122297Seric 	**	even if a version of it has already been sent to the
123297Seric 	**	sender.
124297Seric 	*/
125297Seric 
1263048Seric 	if (MailBack)
127297Seric 	{
1286978Seric 		if (returntosender("Unable to deliver mail", &CurEnv->e_from, TRUE) == 0)
129297Seric 			return;
130297Seric 	}
131297Seric 
132297Seric 	/*
133297Seric 	**  Save the message in dead.letter.
134297Seric 	**	If we weren't mailing back, and the user is local, we
135297Seric 	**	should save the message in dead.letter so that the
136297Seric 	**	poor person doesn't have to type it over again --
137297Seric 	**	and we all know what poor typists programmers are.
138297Seric 	*/
139297Seric 
1404712Seric 	if (ArpaMode)
1414162Seric 		return;
1424079Seric 	p = NULL;
1436904Seric 	if (CurEnv->e_from.q_mailer == LocalMailer)
144297Seric 	{
1456904Seric 		if (CurEnv->e_from.q_home != NULL)
1466904Seric 			p = CurEnv->e_from.q_home;
1476904Seric 		else if ((pw = getpwnam(CurEnv->e_from.q_user)) != NULL)
1484079Seric 			p = pw->pw_dir;
149297Seric 	}
1504079Seric 	if (p == NULL)
151297Seric 	{
1526904Seric 		syserr("Can't return mail to %s", CurEnv->e_from.q_paddr);
153297Seric # ifdef DEBUG
154297Seric 		p = "/usr/tmp";
155297Seric # else
156297Seric 		p = NULL;
157297Seric # endif
158297Seric 	}
1594199Seric 	if (p != NULL && TempFile != NULL)
160297Seric 	{
1615315Seric 		auto ADDRESS *q;
1625315Seric 
163297Seric 		/* we have a home directory; open dead.letter */
1644167Seric 		message(Arpa_Info, "Saving message in dead.letter");
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.
1825984Seric **		sendbody -- if TRUE, also send back the body of the
1835984Seric **			message; otherwise just send the header.
1844633Seric **
1854633Seric **	Returns:
1864633Seric **		zero -- if everything went ok.
1874633Seric **		else -- some error.
1884633Seric **
1894633Seric **	Side Effects:
1904633Seric **		Returns the current message to the sender via
1914633Seric **		mail.
1924633Seric */
1934633Seric 
1944633Seric static char	*ErrorMessage;
1955984Seric static bool	SendBody;
1964633Seric 
1976978Seric returntosender(msg, returnto, sendbody)
1984633Seric 	char *msg;
1996978Seric 	ADDRESS *returnto;
2005984Seric 	bool sendbody;
2014633Seric {
2024633Seric 	ADDRESS to_addr;
2034633Seric 	char buf[MAXNAME];
2044633Seric 	register int i;
2056978Seric 	extern putheader(), errbody();
2066978Seric 	register ENVELOPE *ee;
2076978Seric 	extern ENVELOPE *newenvelope();
2086978Seric 	ENVELOPE errenvelope;
2094633Seric 
210*6989Seric 	NoAlias = TRUE;
2115984Seric 	SendBody = sendbody;
2126978Seric 	ee = newenvelope(&errenvelope);
2136978Seric 	ee->e_puthdr = putheader;
2146978Seric 	ee->e_putbody = errbody;
2156978Seric 	addheader("date", "$b", ee);
2166978Seric 	addheader("from", "$g (Mail Delivery Subsystem)", ee);
2176978Seric 	addheader("to", returnto->q_paddr, ee);
2186978Seric 	addheader("subject", msg, ee);
2194633Seric 
2204633Seric 	/* fake up an address header for the from person */
2216978Seric 	bmove((char *) returnto, (char *) &to_addr, sizeof to_addr);
2226978Seric 	expand("$n", buf, &buf[sizeof buf - 1], CurEnv);
2236978Seric 	if (parse(buf, &ee->e_from, -1) == NULL)
2244633Seric 	{
2254633Seric 		syserr("Can't parse myself!");
2264633Seric 		ExitStat = EX_SOFTWARE;
2274633Seric 		return (-1);
2284633Seric 	}
2294633Seric 	to_addr.q_next = NULL;
2305984Seric 	to_addr.q_flags &= ~QDONTSEND;
2316978Seric 	ee->e_sendqueue = &to_addr;
2325984Seric 
2336978Seric 	/* push state into submessage */
2346978Seric 	CurEnv = ee;
2356978Seric 	define('f', "$n");
2366978Seric 	define('x', "Mail Delivery Subsystem");
2375984Seric 
2386978Seric 	/* actually deliver the error message */
2396978Seric 	i = deliver(&to_addr);
2406978Seric 
2416978Seric 	/* if the error message was "queued", make that happen */
2426978Seric 	if (bitset(QQUEUEUP, to_addr.q_flags))
2436978Seric 		queueup(ee);
2446978Seric 
2456978Seric 	/* restore state */
2466978Seric 	CurEnv = CurEnv->e_parent;
2476978Seric 
2484633Seric 	if (i != 0)
2494633Seric 	{
2506978Seric 		syserr("Can't return mail to %s", to_addr.q_paddr);
2514633Seric 		return (-1);
2524633Seric 	}
2534633Seric 	return (0);
2544633Seric }
2554633Seric /*
2566978Seric **  ERRHEADER -- Output the header for error mail.
257297Seric **
2586978Seric **	Parameters:
2596978Seric **		xfile -- the transcript file.
2606978Seric **		fp -- the output file.
261297Seric **
2626978Seric **	Returns:
2636978Seric **		none
2646978Seric **
2656978Seric **	Side Effects:
2666978Seric **		Outputs the header for an error message.
2676978Seric */
2686978Seric 
2696978Seric errheader(fp, m)
2706978Seric 	register FILE *fp;
2716978Seric 	register struct mailer *m;
2726978Seric {
2736978Seric 	/*
2746978Seric 	**  Output header of error message.
2756978Seric 	*/
2766978Seric 
2776978Seric 	putheader(fp, m);
2786978Seric }
2796978Seric /*
2806978Seric **  ERRBODY -- output the body of an error message.
2816978Seric **
2826978Seric **	Typically this is a copy of the transcript plus a copy of the
2836978Seric **	original offending message.
2846978Seric **
285297Seric **	Parameters:
286297Seric **		xfile -- the transcript file.
287297Seric **		fp -- the output file.
2886978Seric **		xdot -- if set, use the SMTP hidden dot algorithm.
289297Seric **
290297Seric **	Returns:
291297Seric **		none
292297Seric **
293297Seric **	Side Effects:
2946978Seric **		Outputs the body of an error message.
295297Seric */
296297Seric 
2976978Seric errbody(fp, m, xdot)
298297Seric 	register FILE *fp;
2994318Seric 	register struct mailer *m;
3004864Seric 	bool xdot;
301297Seric {
3026978Seric 	register FILE *xfile;
3033189Seric 	char buf[MAXLINE];
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)
3173189Seric 		fputs(buf, fp);
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);
3316978Seric 			putheader(fp, Mailer[1], CurEnv->e_parent);
3326978Seric 			fprintf(fp, "\n");
3336978Seric 			putbody(fp, Mailer[1], xdot);
3345984Seric 		}
3355984Seric 		else
3365984Seric 		{
3375984Seric 			fprintf(fp, "\n  ----- Message header follows -----\n");
3385984Seric 			(void) fflush(fp);
3395984Seric 			putheader(fp, Mailer[1]);
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