1 # include <stdio.h>
2 # include <pwd.h>
3 # include "sendmail.h"
4 
5 static char	SccsId[] = "@(#)savemail.c	3.9	08/09/81";
6 
7 /*
8 **  SAVEMAIL -- Save mail on error
9 **
10 **	If the MailBack flag is set, mail it back to the originator
11 **	together with an error message; otherwise, just put it in
12 **	dead.letter in the user's home directory (if he exists on
13 **	this machine).
14 **
15 **	Parameters:
16 **		none
17 **
18 **	Returns:
19 **		none
20 **
21 **	Side Effects:
22 **		Saves the letter, by writing or mailing it back to the
23 **		sender, or by putting it in dead.letter in her home
24 **		directory.
25 **
26 **		WARNING: the user id is reset to the original user.
27 */
28 
29 savemail()
30 {
31 	register struct passwd *pw;
32 	register FILE *xfile;
33 	char buf[MAXLINE+1];
34 	extern errhdr();
35 	auto ADDRESS to_addr;
36 	extern struct passwd *getpwnam();
37 	register char *p;
38 	register int i;
39 	auto long tim;
40 	extern int errno;
41 	extern char *ttypath();
42 	extern ADDRESS *parse();
43 	static int exclusive;
44 	extern char *strcpy(), *strcat();
45 	extern char *expand();
46 
47 	if (exclusive++)
48 		return;
49 
50 	/*
51 	**  In the unhappy event we don't know who to return the mail
52 	**  to, make someone up.
53 	*/
54 
55 	if (From.q_paddr == NULL)
56 	{
57 		if (parse("root", &From, 0) == NULL)
58 		{
59 			syserr("Cannot parse root!");
60 			ExitStat = EX_SOFTWARE;
61 			finis();
62 		}
63 	}
64 	To = NULL;
65 
66 	/*
67 	**  If called from Eric Schmidt's network, do special mailback.
68 	**	Fundamentally, this is the mailback case except that
69 	**	it returns an OK exit status (assuming the return
70 	**	worked).
71 	*/
72 
73 	if (BerkNet)
74 	{
75 		ExitStat = EX_OK;
76 		MailBack++;
77 	}
78 
79 	/*
80 	**  If writing back, do it.
81 	**	If the user is still logged in on the same terminal,
82 	**	then write the error messages back to hir (sic).
83 	**	If not, set the MailBack flag so that it will get
84 	**	mailed back instead.
85 	*/
86 
87 	if (WriteBack)
88 	{
89 		p = ttypath();
90 		if (p == NULL || freopen(p, "w", stdout) == NULL)
91 		{
92 			MailBack++;
93 			errno = 0;
94 		}
95 		else
96 		{
97 			xfile = fopen(Transcript, "r");
98 			if (xfile == NULL)
99 				syserr("Cannot open %s", Transcript);
100 			printf("\r\nMessage from %s\r\n", expand("$n", buf, &buf[sizeof buf - 1]));
101 			printf("Errors occurred while sending mail, transcript follows:\r\n");
102 			while (fgets(buf, sizeof buf, xfile) && !ferror(stdout))
103 				fputs(buf, stdout);
104 			if (ferror(stdout))
105 				syserr("savemail: stdout: write err");
106 			fclose(xfile);
107 		}
108 	}
109 
110 	/*
111 	**  If mailing back, do it.
112 	**	Throw away all further output.  Don't do aliases, since
113 	**	this could cause loops, e.g., if joe mails to x:joe,
114 	**	and for some reason the network for x: is down, then
115 	**	the response gets sent to x:joe, which gives a
116 	**	response, etc.  Also force the mail to be delivered
117 	**	even if a version of it has already been sent to the
118 	**	sender.
119 	*/
120 
121 	if (MailBack)
122 	{
123 		freopen("/dev/null", "w", stdout);
124 		NoAlias++;
125 		ForceMail++;
126 
127 		/* fake up an address header for the from person */
128 		bmove((char *) &From, (char *) &to_addr, sizeof to_addr);
129 		if (parse(expand("$n", buf, &buf[sizeof buf - 1]), &From, -1) == NULL)
130 		{
131 			syserr("Can't parse myself!");
132 			ExitStat = EX_SOFTWARE;
133 			finis();
134 		}
135 		i = deliver(&to_addr, errhdr);
136 		bmove((char *) &to_addr, (char *) &From, sizeof From);
137 		if (i != 0)
138 			syserr("Can't return mail to %s", p);
139 		else
140 			return;
141 	}
142 
143 	/*
144 	**  Save the message in dead.letter.
145 	**	If we weren't mailing back, and the user is local, we
146 	**	should save the message in dead.letter so that the
147 	**	poor person doesn't have to type it over again --
148 	**	and we all know what poor typists programmers are.
149 	*/
150 
151 	setuid(getuid());
152 	setgid(getgid());
153 	setpwent();
154 	p = NULL;
155 	if (From.q_mailer == M_LOCAL)
156 	{
157 		if (From.q_home != NULL)
158 			p = From.q_home;
159 		else if ((pw = getpwnam(From.q_user)) != NULL)
160 			p = pw->pw_dir;
161 	}
162 	if (p == NULL)
163 	{
164 		syserr("Can't return mail to %s", From.q_paddr);
165 # ifdef DEBUG
166 		p = "/usr/tmp";
167 # else
168 		p = NULL;
169 # endif
170 	}
171 	if (p != NULL)
172 	{
173 		/* we have a home directory; open dead.letter */
174 		message("050", "Saving message in dead.letter");
175 		define('z', p);
176 		expand("$z/dead.letter", buf, &buf[sizeof buf - 1]);
177 		To = buf;
178 		i = mailfile(buf);
179 		giveresponse(i, TRUE, Mailer[M_LOCAL]);
180 	}
181 
182 	/* add terminator to writeback message */
183 	if (WriteBack)
184 		printf("-----\r\n");
185 }
186 /*
187 **  ERRHDR -- Output the header for error mail.
188 **
189 **	This is the edit filter to error mailbacks.
190 **
191 **	Algorithm:
192 **		Output fixed header.
193 **		Output the transcript part.
194 **		Output the original message.
195 **
196 **	Parameters:
197 **		xfile -- the transcript file.
198 **		fp -- the output file.
199 **
200 **	Returns:
201 **		none
202 **
203 **	Side Effects:
204 **		input from xfile
205 **		output to fp
206 **
207 **	Called By:
208 **		deliver
209 */
210 
211 
212 errhdr(fp)
213 	register FILE *fp;
214 {
215 	char buf[MAXLINE];
216 	register FILE *xfile;
217 	extern int errno;
218 
219 	fflush(stdout);
220 	if ((xfile = fopen(Transcript, "r")) == NULL)
221 		syserr("Cannot open %s", Transcript);
222 	errno = 0;
223 	fprintf(fp, "To: %s\n", To);
224 	fprintf(fp, "Subject: Unable to deliver mail\n");
225 	fprintf(fp, "\n   ----- Transcript of session follows -----\n");
226 	while (fgets(xfile, buf, sizeof buf) != NULL)
227 		fputs(buf, fp);
228 	fprintf(fp, "\n   ----- Unsent message follows -----\n");
229 	fflush(fp);
230 	putmessage(fp, Mailer[1]);
231 	close(xfile);
232 	if (errno != 0)
233 		syserr("errhdr: I/O error");
234 }
235