1297Seric # include <stdio.h>
2297Seric # include <pwd.h>
32973Seric # include "postbox.h"
4297Seric 
5*2991Seric static char	SccsId[] = "@(#)savemail.c	3.2	03/07/81";
6408Seric 
7297Seric /*
8297Seric **  SAVEMAIL -- Save mail on error
9297Seric **
10297Seric **	If the MailBack flag is set, mail it back to the originator
11297Seric **	together with an error message; otherwise, just put it in
12297Seric **	dead.letter in the user's home directory (if he exists on
13297Seric **	this machine).
14297Seric **
15297Seric **	Parameters:
16297Seric **		none
17297Seric **
18297Seric **	Returns:
19297Seric **		none
20297Seric **
21297Seric **	Side Effects:
22297Seric **		Saves the letter, by writing or mailing it back to the
23297Seric **		sender, or by putting it in dead.letter in her home
24297Seric **		directory.
25297Seric **
26297Seric **		WARNING: the user id is reset to the original user.
27297Seric */
28297Seric 
29297Seric savemail()
30297Seric {
31297Seric 	register struct passwd *pw;
32297Seric 	register FILE *xfile;
33297Seric 	char buf[MAXLINE+1];
34297Seric 	extern errhdr();
352973Seric 	auto ADDRESS to_addr;
36297Seric 	extern struct passwd *getpwnam();
37297Seric 	register char *p;
38297Seric 	register int i;
39297Seric 	auto long tim;
40297Seric 	extern int errno;
41297Seric 	extern char *ttypath();
42297Seric 	extern char *ctime();
432973Seric 	extern ADDRESS *parse();
44297Seric 	static int exclusive;
452098Seric 	extern char *DaemonName;
46*2991Seric 	extern char *strcpy(), *strcat();
47*2991Seric 	extern long time();
48297Seric 
49297Seric 	if (exclusive++)
50297Seric 		return;
51297Seric 
52297Seric 	/*
53297Seric 	**  In the unhappy event we don't know who to return the mail
54297Seric 	**  to, make someone up.
55297Seric 	*/
56297Seric 
57297Seric 	if (From.q_paddr == NULL)
58297Seric 	{
59297Seric 		if (parse("root", &From, 0) == NULL)
60297Seric 		{
61297Seric 			syserr("Cannot parse root!");
62297Seric 			ExitStat = EX_SOFTWARE;
63297Seric 			finis();
64297Seric 		}
65297Seric 	}
66297Seric 
67297Seric 	/*
68401Seric 	**  If called from Eric Schmidt's network, do special mailback.
69401Seric 	**	Fundamentally, this is the mailback case except that
70401Seric 	**	it returns an OK exit status (assuming the return
71401Seric 	**	worked).
72297Seric 	*/
73297Seric 
74401Seric 	if (BerkNet)
75297Seric 	{
76401Seric 		ExitStat = EX_OK;
77297Seric 		MailBack++;
78297Seric 	}
79297Seric 
80297Seric 	/*
81297Seric 	**  If writing back, do it.
82297Seric 	**	If the user is still logged in on the same terminal,
83297Seric 	**	then write the error messages back to hir (sic).
84297Seric 	**	If not, set the MailBack flag so that it will get
85297Seric 	**	mailed back instead.
86297Seric 	*/
87297Seric 
88297Seric 	if (WriteBack)
89297Seric 	{
90297Seric 		p = ttypath();
91297Seric 		if (p == NULL || freopen(p, "w", stdout) == NULL)
92297Seric 		{
93297Seric 			MailBack++;
94297Seric 			errno = 0;
95297Seric 		}
96297Seric 		else
97297Seric 		{
98401Seric 			xfile = fopen(Transcript, "r");
99401Seric 			if (xfile == NULL)
100401Seric 				syserr("Cannot open %s", Transcript);
1012098Seric 			printf("\r\nMessage from %s\r\n", DaemonName);
102297Seric 			printf("Errors occurred while sending mail, transcript follows:\r\n");
103297Seric 			while (fgets(buf, sizeof buf, xfile) && !ferror(stdout))
104297Seric 				fputs(buf, stdout);
105297Seric 			if (ferror(stdout))
106297Seric 				syserr("savemail: stdout: write err");
107401Seric 			fclose(xfile);
108297Seric 		}
109297Seric 	}
110297Seric 
111297Seric 	/*
112297Seric 	**  If mailing back, do it.
113297Seric 	**	Throw away all further output.  Don't do aliases, since
114297Seric 	**	this could cause loops, e.g., if joe mails to x:joe,
115297Seric 	**	and for some reason the network for x: is down, then
116297Seric 	**	the response gets sent to x:joe, which gives a
117297Seric 	**	response, etc.  Also force the mail to be delivered
118297Seric 	**	even if a version of it has already been sent to the
119297Seric 	**	sender.
120297Seric 	*/
121297Seric 
122297Seric 	if (MailBack || From.q_mailer != &Mailer[0])
123297Seric 	{
124297Seric 		freopen("/dev/null", "w", stdout);
125297Seric 		NoAlias++;
126297Seric 		ForceMail++;
127297Seric 
128297Seric 		/* fake up an address header for the from person */
129297Seric 		bmove((char *) &From, (char *) &to_addr, sizeof to_addr);
1302098Seric 		if (parse(DaemonName, &From, -1) == NULL)
131297Seric 		{
132297Seric 			syserr("Can't parse myself!");
133297Seric 			ExitStat = EX_SOFTWARE;
134297Seric 			finis();
135297Seric 		}
136297Seric 		i = deliver(&to_addr, errhdr);
137297Seric 		bmove((char *) &to_addr, (char *) &From, sizeof From);
138297Seric 		if (i != 0)
139297Seric 			syserr("Can't return mail to %s", p);
140297Seric 		else
141297Seric 			return;
142297Seric 	}
143297Seric 
144297Seric 	/*
145297Seric 	**  Save the message in dead.letter.
146297Seric 	**	If we weren't mailing back, and the user is local, we
147297Seric 	**	should save the message in dead.letter so that the
148297Seric 	**	poor person doesn't have to type it over again --
149297Seric 	**	and we all know what poor typists programmers are.
150297Seric 	*/
151297Seric 
152297Seric 	setuid(getuid());
153297Seric 	setgid(getgid());
154297Seric 	setpwent();
155297Seric 	if (From.q_mailer == &Mailer[0] && (pw = getpwnam(From.q_user)) != NULL)
156297Seric 	{
157297Seric 		/* user has a home directory */
158297Seric 		p = pw->pw_dir;
159297Seric 	}
160297Seric 	else
161297Seric 	{
162297Seric 		syserr("Can't return mail to %s (pw=%u)", From.q_paddr, pw);
163297Seric # ifdef DEBUG
164297Seric 		p = "/usr/tmp";
165297Seric # else
166297Seric 		p = NULL;
167297Seric # endif
168297Seric 	}
169297Seric 	if (p != NULL)
170297Seric 	{
171297Seric 		/* we have a home directory; open dead.letter */
172297Seric 		strcpy(buf, p);
173297Seric 		strcat(buf, "/dead.letter");
174297Seric 		xfile = fopen(buf, "a");
175297Seric 		if (xfile == NULL)
176297Seric 			printf("Cannot save mail, sorry\n");
177297Seric 		else
178297Seric 		{
179297Seric 			rewind(stdin);
180297Seric 			errno = 0;
181297Seric 			time(&tim);
182297Seric 			fprintf(xfile, "----- Mail saved at %s", ctime(&tim));
183297Seric 			while (fgets(buf, sizeof buf, stdin) && !ferror(xfile))
184297Seric 				fputs(buf, xfile);
185297Seric 			fputs("\n", xfile);
186297Seric 			if (ferror(xfile))
187297Seric 				syserr("savemail: dead.letter: write err");
188297Seric 			fclose(xfile);
189297Seric 			printf("Letter saved in dead.letter\n");
190297Seric 		}
191297Seric 	}
192297Seric 	else
193297Seric 
194297Seric 	/* add terminator to writeback message */
195297Seric 	if (WriteBack)
196297Seric 		printf("-----\r\n");
197297Seric }
198297Seric /*
199297Seric **  ERRHDR -- Output the header for error mail.
200297Seric **
201297Seric **	This is the edit filter to error mailbacks.
202297Seric **
203297Seric **	Algorithm:
204297Seric **		Output fixed header.
205297Seric **		Output the transcript part.
206297Seric **		Output the original message.
207297Seric **
208297Seric **	Parameters:
209297Seric **		xfile -- the transcript file.
210297Seric **		fp -- the output file.
211297Seric **
212297Seric **	Returns:
213297Seric **		none
214297Seric **
215297Seric **	Side Effects:
216297Seric **		input from xfile
217297Seric **		output to fp
218297Seric **
219297Seric **	Called By:
220297Seric **		deliver
221297Seric */
222297Seric 
223297Seric 
224297Seric errhdr(fp)
225297Seric 	register FILE *fp;
226297Seric {
227297Seric 	char copybuf[512];
228297Seric 	register int i;
229297Seric 	register int xfile;
230297Seric 	extern int errno;
231297Seric 
232297Seric 	if ((xfile = open(Transcript, 0)) < 0)
233297Seric 		syserr("Cannot open %s", Transcript);
234297Seric 	fflush(stdout);
235297Seric 	errno = 0;
236297Seric 	fprintf(fp, "To: %s\n", To);
237297Seric 	fprintf(fp, "Subject: Unable to deliver mail\n");
238297Seric 	fprintf(fp, "\n   ----- Transcript of session follows -----\n");
239297Seric 	fflush(fp);
240297Seric 	while ((i = read(xfile, copybuf, sizeof copybuf)) > 0)
241297Seric 		write(fileno(fp), copybuf, i);
242297Seric 	fprintf(fp, "\n   ----- Unsent message follows -----\n");
243297Seric 	fflush(fp);
244297Seric 	rewind(stdin);
245297Seric 	while ((i = read(fileno(stdin), copybuf, sizeof copybuf)) > 0)
246297Seric 		write(fileno(fp), copybuf, i);
247297Seric 	close(xfile);
248297Seric 	if (errno != 0)
249297Seric 		syserr("errhdr: I/O error");
250297Seric }
251