1297Seric # include <pwd.h>
23313Seric # include "sendmail.h"
3297Seric 
4*5846Seric SCCSID(@(#)savemail.c	3.26		02/15/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;
35*5846Seric 	typedef int (*fnptr)();
36297Seric 
374199Seric 	if (exclusive++)
38297Seric 		return;
395315Seric 	ForceMail = TRUE;
40297Seric 
41297Seric 	/*
42297Seric 	**  In the unhappy event we don't know who to return the mail
43297Seric 	**  to, make someone up.
44297Seric 	*/
45297Seric 
46297Seric 	if (From.q_paddr == NULL)
47297Seric 	{
48297Seric 		if (parse("root", &From, 0) == NULL)
49297Seric 		{
50297Seric 			syserr("Cannot parse root!");
51297Seric 			ExitStat = EX_SOFTWARE;
52297Seric 			finis();
53297Seric 		}
54297Seric 	}
554079Seric 	To = NULL;
56297Seric 
57297Seric 	/*
58401Seric 	**  If called from Eric Schmidt's network, do special mailback.
59401Seric 	**	Fundamentally, this is the mailback case except that
60401Seric 	**	it returns an OK exit status (assuming the return
61401Seric 	**	worked).
62297Seric 	*/
63297Seric 
64401Seric 	if (BerkNet)
65297Seric 	{
66401Seric 		ExitStat = EX_OK;
67297Seric 		MailBack++;
68297Seric 	}
69297Seric 
70297Seric 	/*
71297Seric 	**  If writing back, do it.
72297Seric 	**	If the user is still logged in on the same terminal,
73297Seric 	**	then write the error messages back to hir (sic).
74297Seric 	**	If not, set the MailBack flag so that it will get
75297Seric 	**	mailed back instead.
76297Seric 	*/
77297Seric 
78297Seric 	if (WriteBack)
79297Seric 	{
80297Seric 		p = ttypath();
81297Seric 		if (p == NULL || freopen(p, "w", stdout) == NULL)
82297Seric 		{
83297Seric 			MailBack++;
84297Seric 			errno = 0;
85297Seric 		}
86297Seric 		else
87297Seric 		{
884712Seric 			(void) fflush(Xscript);
89401Seric 			xfile = fopen(Transcript, "r");
90401Seric 			if (xfile == NULL)
91401Seric 				syserr("Cannot open %s", Transcript);
924086Seric 			(void) expand("$n", buf, &buf[sizeof buf - 1]);
934162Seric 			printf("\r\nMessage from %s...\r\n", buf);
944318Seric 			printf("Errors occurred while sending mail; transcript follows:\r\n");
954086Seric 			while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout))
96297Seric 				fputs(buf, stdout);
97297Seric 			if (ferror(stdout))
984086Seric 				(void) syserr("savemail: stdout: write err");
994086Seric 			(void) fclose(xfile);
100297Seric 		}
101297Seric 	}
102297Seric 
103297Seric 	/*
104297Seric 	**  If mailing back, do it.
105297Seric 	**	Throw away all further output.  Don't do aliases, since
106297Seric 	**	this could cause loops, e.g., if joe mails to x:joe,
107297Seric 	**	and for some reason the network for x: is down, then
108297Seric 	**	the response gets sent to x:joe, which gives a
109297Seric 	**	response, etc.  Also force the mail to be delivered
110297Seric 	**	even if a version of it has already been sent to the
111297Seric 	**	sender.
112297Seric 	*/
113297Seric 
1143048Seric 	if (MailBack)
115297Seric 	{
1164633Seric 		if (returntosender("Unable to deliver mail") == 0)
117297Seric 			return;
118297Seric 	}
119297Seric 
120297Seric 	/*
121297Seric 	**  Save the message in dead.letter.
122297Seric 	**	If we weren't mailing back, and the user is local, we
123297Seric 	**	should save the message in dead.letter so that the
124297Seric 	**	poor person doesn't have to type it over again --
125297Seric 	**	and we all know what poor typists programmers are.
126297Seric 	*/
127297Seric 
1284712Seric 	if (ArpaMode)
1294162Seric 		return;
1304079Seric 	p = NULL;
1314599Seric 	if (From.q_mailer == LocalMailer)
132297Seric 	{
1334079Seric 		if (From.q_home != NULL)
1344079Seric 			p = From.q_home;
1354079Seric 		else if ((pw = getpwnam(From.q_user)) != NULL)
1364079Seric 			p = pw->pw_dir;
137297Seric 	}
1384079Seric 	if (p == NULL)
139297Seric 	{
1404079Seric 		syserr("Can't return mail to %s", From.q_paddr);
141297Seric # ifdef DEBUG
142297Seric 		p = "/usr/tmp";
143297Seric # else
144297Seric 		p = NULL;
145297Seric # endif
146297Seric 	}
1474199Seric 	if (p != NULL && TempFile != NULL)
148297Seric 	{
1495315Seric 		auto ADDRESS *q;
1505315Seric 
151297Seric 		/* we have a home directory; open dead.letter */
1524167Seric 		message(Arpa_Info, "Saving message in dead.letter");
1534079Seric 		define('z', p);
1544086Seric 		(void) expand("$z/dead.letter", buf, &buf[sizeof buf - 1]);
1554066Seric 		To = buf;
1565315Seric 		q = NULL;
157*5846Seric 		sendto(buf, -1, (ADDRESS *) NULL, &q);
158*5846Seric 		(void) deliver(q, (fnptr) NULL);
159297Seric 	}
160297Seric 
161297Seric 	/* add terminator to writeback message */
162297Seric 	if (WriteBack)
163297Seric 		printf("-----\r\n");
164297Seric }
165297Seric /*
1664633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
1674633Seric **
1684633Seric **	Parameters:
1694633Seric **		msg -- the explanatory message.
1704633Seric **
1714633Seric **	Returns:
1724633Seric **		zero -- if everything went ok.
1734633Seric **		else -- some error.
1744633Seric **
1754633Seric **	Side Effects:
1764633Seric **		Returns the current message to the sender via
1774633Seric **		mail.
1784633Seric */
1794633Seric 
1804633Seric static char	*ErrorMessage;
1814633Seric 
1824633Seric returntosender(msg)
1834633Seric 	char *msg;
1844633Seric {
1854633Seric 	ADDRESS to_addr;
1864633Seric 	char buf[MAXNAME];
1874633Seric 	register int i;
1884633Seric 	extern errhdr();
1894633Seric 
1904633Seric 	(void) freopen("/dev/null", "w", stdout);
1914633Seric 	NoAlias++;
1924633Seric 	ErrorMessage = msg;
1934633Seric 
1944633Seric 	/* fake up an address header for the from person */
1954633Seric 	bmove((char *) &From, (char *) &to_addr, sizeof to_addr);
1964633Seric 	(void) expand("$n", buf, &buf[sizeof buf - 1]);
1974633Seric 	if (parse(buf, &From, -1) == NULL)
1984633Seric 	{
1994633Seric 		syserr("Can't parse myself!");
2004633Seric 		ExitStat = EX_SOFTWARE;
2014633Seric 		return (-1);
2024633Seric 	}
2034633Seric 	to_addr.q_next = NULL;
2044633Seric 	i = deliver(&to_addr, errhdr);
2054633Seric 	bmove((char *) &to_addr, (char *) &From, sizeof From);
2064633Seric 	if (i != 0)
2074633Seric 	{
2084633Seric 		syserr("Can't return mail to %s", From.q_paddr);
2094633Seric 		return (-1);
2104633Seric 	}
2114633Seric 	return (0);
2124633Seric }
2134633Seric /*
214297Seric **  ERRHDR -- Output the header for error mail.
215297Seric **
216297Seric **	This is the edit filter to error mailbacks.
217297Seric **
218297Seric **	Parameters:
219297Seric **		xfile -- the transcript file.
220297Seric **		fp -- the output file.
2214864Seric **		xdot -- if set, use smtp hidden dot algorithm.
222297Seric **
223297Seric **	Returns:
224297Seric **		none
225297Seric **
226297Seric **	Side Effects:
2274633Seric **		Outputs the current message with an appropriate
2284633Seric **		error header.
229297Seric */
230297Seric 
2314864Seric errhdr(fp, m, xdot)
232297Seric 	register FILE *fp;
2334318Seric 	register struct mailer *m;
2344864Seric 	bool xdot;
235297Seric {
2363189Seric 	char buf[MAXLINE];
2373189Seric 	register FILE *xfile;
2384454Seric 	extern char *macvalue();
2394454Seric 	char *oldfmac;
2404454Seric 	char *oldgmac;
241297Seric 
2424454Seric 	oldfmac = macvalue('f');
2434454Seric 	define('f', "$n");
2444454Seric 	oldgmac = macvalue('g');
2454454Seric 	define('g', m->m_from);
2464454Seric 
2474086Seric 	(void) fflush(stdout);
2484712Seric 	(void) fflush(Xscript);
2493189Seric 	if ((xfile = fopen(Transcript, "r")) == NULL)
250297Seric 		syserr("Cannot open %s", Transcript);
251297Seric 	errno = 0;
2524318Seric 
2534318Seric 	/*
2544454Seric 	**  Output "From" line unless supressed
2554454Seric 	*/
2564454Seric 
2574454Seric 	if (!bitset(M_NHDR, m->m_flags))
2584454Seric 	{
2594454Seric 		(void) expand("$l", buf, &buf[sizeof buf - 1]);
2604454Seric 		fprintf(fp, "%s\n", buf);
2614454Seric 	}
2624454Seric 
2634454Seric 	/*
2644318Seric 	**  Output header of error message.
2654318Seric 	*/
2664318Seric 
2674318Seric 	if (bitset(M_NEEDDATE, m->m_flags))
2684318Seric 	{
2694318Seric 		(void) expand("$b", buf, &buf[sizeof buf - 1]);
2704318Seric 		fprintf(fp, "Date: %s\n", buf);
2714318Seric 	}
2724318Seric 	if (bitset(M_NEEDFROM, m->m_flags))
2734318Seric 	{
2744318Seric 		(void) expand("$g", buf, &buf[sizeof buf - 1]);
2754318Seric 		fprintf(fp, "From: %s (Mail Delivery Subsystem)\n", buf);
2764318Seric 	}
277297Seric 	fprintf(fp, "To: %s\n", To);
2784633Seric 	fprintf(fp, "Subject: %s\n", ErrorMessage);
2794318Seric 
2804318Seric 	/*
2814454Seric 	**  End of error message header
2824454Seric 	*/
2834454Seric 
2844454Seric 	define('f', oldfmac);
2854454Seric 	define('g', oldgmac);
2864454Seric 
2874454Seric 	/*
2884318Seric 	**  Output transcript of errors
2894318Seric 	*/
2904318Seric 
291297Seric 	fprintf(fp, "\n   ----- Transcript of session follows -----\n");
2924712Seric 	(void) fflush(Xscript);
2934086Seric 	while (fgets(buf, sizeof buf, xfile) != NULL)
2943189Seric 		fputs(buf, fp);
2954318Seric 
2964318Seric 	/*
2974318Seric 	**  Output text of original message
2984318Seric 	*/
2994318Seric 
3004289Seric 	if (NoReturn)
3014289Seric 		fprintf(fp, "\n   ----- Return message suppressed -----\n\n");
3024289Seric 	else if (TempFile != NULL)
3034199Seric 	{
3044199Seric 		fprintf(fp, "\n   ----- Unsent message follows -----\n");
3054199Seric 		(void) fflush(fp);
3064864Seric 		putmessage(fp, Mailer[1], xdot);
3074199Seric 	}
3084199Seric 	else
3094199Seric 		fprintf(fp, "\n  ----- No message was collected -----\n\n");
3104318Seric 
3114318Seric 	/*
3124318Seric 	**  Cleanup and exit
3134318Seric 	*/
3144318Seric 
3154086Seric 	(void) fclose(xfile);
316297Seric 	if (errno != 0)
317297Seric 		syserr("errhdr: I/O error");
318297Seric }
319