1297Seric # include <pwd.h>
23313Seric # include "sendmail.h"
3297Seric 
4*4864Seric static char	SccsId[] = "@(#)savemail.c	3.23	11/11/81";
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 	register int i;
34297Seric 	extern char *ttypath();
35297Seric 	static int exclusive;
36297Seric 
374199Seric 	if (exclusive++)
38297Seric 		return;
39297Seric 
40297Seric 	/*
41297Seric 	**  In the unhappy event we don't know who to return the mail
42297Seric 	**  to, make someone up.
43297Seric 	*/
44297Seric 
45297Seric 	if (From.q_paddr == NULL)
46297Seric 	{
47297Seric 		if (parse("root", &From, 0) == NULL)
48297Seric 		{
49297Seric 			syserr("Cannot parse root!");
50297Seric 			ExitStat = EX_SOFTWARE;
51297Seric 			finis();
52297Seric 		}
53297Seric 	}
544079Seric 	To = NULL;
55297Seric 
56297Seric 	/*
57401Seric 	**  If called from Eric Schmidt's network, do special mailback.
58401Seric 	**	Fundamentally, this is the mailback case except that
59401Seric 	**	it returns an OK exit status (assuming the return
60401Seric 	**	worked).
61297Seric 	*/
62297Seric 
63401Seric 	if (BerkNet)
64297Seric 	{
65401Seric 		ExitStat = EX_OK;
66297Seric 		MailBack++;
67297Seric 	}
68297Seric 
69297Seric 	/*
70297Seric 	**  If writing back, do it.
71297Seric 	**	If the user is still logged in on the same terminal,
72297Seric 	**	then write the error messages back to hir (sic).
73297Seric 	**	If not, set the MailBack flag so that it will get
74297Seric 	**	mailed back instead.
75297Seric 	*/
76297Seric 
77297Seric 	if (WriteBack)
78297Seric 	{
79297Seric 		p = ttypath();
80297Seric 		if (p == NULL || freopen(p, "w", stdout) == NULL)
81297Seric 		{
82297Seric 			MailBack++;
83297Seric 			errno = 0;
84297Seric 		}
85297Seric 		else
86297Seric 		{
874712Seric 			(void) fflush(Xscript);
88401Seric 			xfile = fopen(Transcript, "r");
89401Seric 			if (xfile == NULL)
90401Seric 				syserr("Cannot open %s", Transcript);
914086Seric 			(void) expand("$n", buf, &buf[sizeof buf - 1]);
924162Seric 			printf("\r\nMessage from %s...\r\n", buf);
934318Seric 			printf("Errors occurred while sending mail; transcript follows:\r\n");
944086Seric 			while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout))
95297Seric 				fputs(buf, stdout);
96297Seric 			if (ferror(stdout))
974086Seric 				(void) syserr("savemail: stdout: write err");
984086Seric 			(void) fclose(xfile);
99297Seric 		}
100297Seric 	}
101297Seric 
102297Seric 	/*
103297Seric 	**  If mailing back, do it.
104297Seric 	**	Throw away all further output.  Don't do aliases, since
105297Seric 	**	this could cause loops, e.g., if joe mails to x:joe,
106297Seric 	**	and for some reason the network for x: is down, then
107297Seric 	**	the response gets sent to x:joe, which gives a
108297Seric 	**	response, etc.  Also force the mail to be delivered
109297Seric 	**	even if a version of it has already been sent to the
110297Seric 	**	sender.
111297Seric 	*/
112297Seric 
1133048Seric 	if (MailBack)
114297Seric 	{
1154633Seric 		if (returntosender("Unable to deliver mail") == 0)
116297Seric 			return;
117297Seric 	}
118297Seric 
119297Seric 	/*
120297Seric 	**  Save the message in dead.letter.
121297Seric 	**	If we weren't mailing back, and the user is local, we
122297Seric 	**	should save the message in dead.letter so that the
123297Seric 	**	poor person doesn't have to type it over again --
124297Seric 	**	and we all know what poor typists programmers are.
125297Seric 	*/
126297Seric 
1274712Seric 	if (ArpaMode)
1284162Seric 		return;
1294079Seric 	p = NULL;
1304599Seric 	if (From.q_mailer == LocalMailer)
131297Seric 	{
1324079Seric 		if (From.q_home != NULL)
1334079Seric 			p = From.q_home;
1344079Seric 		else if ((pw = getpwnam(From.q_user)) != NULL)
1354079Seric 			p = pw->pw_dir;
136297Seric 	}
1374079Seric 	if (p == NULL)
138297Seric 	{
1394079Seric 		syserr("Can't return mail to %s", From.q_paddr);
140297Seric # ifdef DEBUG
141297Seric 		p = "/usr/tmp";
142297Seric # else
143297Seric 		p = NULL;
144297Seric # endif
145297Seric 	}
1464199Seric 	if (p != NULL && TempFile != NULL)
147297Seric 	{
148297Seric 		/* we have a home directory; open dead.letter */
1494167Seric 		message(Arpa_Info, "Saving message in dead.letter");
1504079Seric 		define('z', p);
1514086Seric 		(void) expand("$z/dead.letter", buf, &buf[sizeof buf - 1]);
1524066Seric 		To = buf;
1534398Seric 		i = mailfile(buf, &From);
1544599Seric 		giveresponse(i, TRUE, LocalMailer);
155297Seric 	}
156297Seric 
157297Seric 	/* add terminator to writeback message */
158297Seric 	if (WriteBack)
159297Seric 		printf("-----\r\n");
160297Seric }
161297Seric /*
1624633Seric **  RETURNTOSENDER -- return a message to the sender with an error.
1634633Seric **
1644633Seric **	Parameters:
1654633Seric **		msg -- the explanatory message.
1664633Seric **
1674633Seric **	Returns:
1684633Seric **		zero -- if everything went ok.
1694633Seric **		else -- some error.
1704633Seric **
1714633Seric **	Side Effects:
1724633Seric **		Returns the current message to the sender via
1734633Seric **		mail.
1744633Seric */
1754633Seric 
1764633Seric static char	*ErrorMessage;
1774633Seric 
1784633Seric returntosender(msg)
1794633Seric 	char *msg;
1804633Seric {
1814633Seric 	ADDRESS to_addr;
1824633Seric 	char buf[MAXNAME];
1834633Seric 	register int i;
1844633Seric 	extern errhdr();
1854633Seric 
1864633Seric 	(void) freopen("/dev/null", "w", stdout);
1874633Seric 	NoAlias++;
1884633Seric 	ForceMail++;
1894633Seric 	ErrorMessage = msg;
1904633Seric 
1914633Seric 	/* fake up an address header for the from person */
1924633Seric 	bmove((char *) &From, (char *) &to_addr, sizeof to_addr);
1934633Seric 	(void) expand("$n", buf, &buf[sizeof buf - 1]);
1944633Seric 	if (parse(buf, &From, -1) == NULL)
1954633Seric 	{
1964633Seric 		syserr("Can't parse myself!");
1974633Seric 		ExitStat = EX_SOFTWARE;
1984633Seric 		return (-1);
1994633Seric 	}
2004633Seric 	to_addr.q_next = NULL;
2014633Seric 	i = deliver(&to_addr, errhdr);
2024633Seric 	bmove((char *) &to_addr, (char *) &From, sizeof From);
2034633Seric 	if (i != 0)
2044633Seric 	{
2054633Seric 		syserr("Can't return mail to %s", From.q_paddr);
2064633Seric 		return (-1);
2074633Seric 	}
2084633Seric 	return (0);
2094633Seric }
2104633Seric /*
211297Seric **  ERRHDR -- Output the header for error mail.
212297Seric **
213297Seric **	This is the edit filter to error mailbacks.
214297Seric **
215297Seric **	Parameters:
216297Seric **		xfile -- the transcript file.
217297Seric **		fp -- the output file.
218*4864Seric **		xdot -- if set, use smtp hidden dot algorithm.
219297Seric **
220297Seric **	Returns:
221297Seric **		none
222297Seric **
223297Seric **	Side Effects:
2244633Seric **		Outputs the current message with an appropriate
2254633Seric **		error header.
226297Seric */
227297Seric 
228*4864Seric errhdr(fp, m, xdot)
229297Seric 	register FILE *fp;
2304318Seric 	register struct mailer *m;
231*4864Seric 	bool xdot;
232297Seric {
2333189Seric 	char buf[MAXLINE];
2343189Seric 	register FILE *xfile;
2354454Seric 	extern char *macvalue();
2364454Seric 	char *oldfmac;
2374454Seric 	char *oldgmac;
238297Seric 
2394454Seric 	oldfmac = macvalue('f');
2404454Seric 	define('f', "$n");
2414454Seric 	oldgmac = macvalue('g');
2424454Seric 	define('g', m->m_from);
2434454Seric 
2444086Seric 	(void) fflush(stdout);
2454712Seric 	(void) fflush(Xscript);
2463189Seric 	if ((xfile = fopen(Transcript, "r")) == NULL)
247297Seric 		syserr("Cannot open %s", Transcript);
248297Seric 	errno = 0;
2494318Seric 
2504318Seric 	/*
2514454Seric 	**  Output "From" line unless supressed
2524454Seric 	*/
2534454Seric 
2544454Seric 	if (!bitset(M_NHDR, m->m_flags))
2554454Seric 	{
2564454Seric 		(void) expand("$l", buf, &buf[sizeof buf - 1]);
2574454Seric 		fprintf(fp, "%s\n", buf);
2584454Seric 	}
2594454Seric 
2604454Seric 	/*
2614318Seric 	**  Output header of error message.
2624318Seric 	*/
2634318Seric 
2644318Seric 	if (bitset(M_NEEDDATE, m->m_flags))
2654318Seric 	{
2664318Seric 		(void) expand("$b", buf, &buf[sizeof buf - 1]);
2674318Seric 		fprintf(fp, "Date: %s\n", buf);
2684318Seric 	}
2694318Seric 	if (bitset(M_NEEDFROM, m->m_flags))
2704318Seric 	{
2714318Seric 		(void) expand("$g", buf, &buf[sizeof buf - 1]);
2724318Seric 		fprintf(fp, "From: %s (Mail Delivery Subsystem)\n", buf);
2734318Seric 	}
274297Seric 	fprintf(fp, "To: %s\n", To);
2754633Seric 	fprintf(fp, "Subject: %s\n", ErrorMessage);
2764318Seric 
2774318Seric 	/*
2784454Seric 	**  End of error message header
2794454Seric 	*/
2804454Seric 
2814454Seric 	define('f', oldfmac);
2824454Seric 	define('g', oldgmac);
2834454Seric 
2844454Seric 	/*
2854318Seric 	**  Output transcript of errors
2864318Seric 	*/
2874318Seric 
288297Seric 	fprintf(fp, "\n   ----- Transcript of session follows -----\n");
2894712Seric 	(void) fflush(Xscript);
2904086Seric 	while (fgets(buf, sizeof buf, xfile) != NULL)
2913189Seric 		fputs(buf, fp);
2924318Seric 
2934318Seric 	/*
2944318Seric 	**  Output text of original message
2954318Seric 	*/
2964318Seric 
2974289Seric 	if (NoReturn)
2984289Seric 		fprintf(fp, "\n   ----- Return message suppressed -----\n\n");
2994289Seric 	else if (TempFile != NULL)
3004199Seric 	{
3014199Seric 		fprintf(fp, "\n   ----- Unsent message follows -----\n");
3024199Seric 		(void) fflush(fp);
303*4864Seric 		putmessage(fp, Mailer[1], xdot);
3044199Seric 	}
3054199Seric 	else
3064199Seric 		fprintf(fp, "\n  ----- No message was collected -----\n\n");
3074318Seric 
3084318Seric 	/*
3094318Seric 	**  Cleanup and exit
3104318Seric 	*/
3114318Seric 
3124086Seric 	(void) fclose(xfile);
313297Seric 	if (errno != 0)
314297Seric 		syserr("errhdr: I/O error");
315297Seric }
316