1 # include <pwd.h>
2 # include "sendmail.h"
3 
4 SCCSID(@(#)savemail.c	3.30		05/31/82);
5 
6 /*
7 **  SAVEMAIL -- Save mail on error
8 **
9 **	If the MailBack flag is set, mail it back to the originator
10 **	together with an error message; otherwise, just put it in
11 **	dead.letter in the user's home directory (if he exists on
12 **	this machine).
13 **
14 **	Parameters:
15 **		none
16 **
17 **	Returns:
18 **		none
19 **
20 **	Side Effects:
21 **		Saves the letter, by writing or mailing it back to the
22 **		sender, or by putting it in dead.letter in her home
23 **		directory.
24 */
25 
26 savemail()
27 {
28 	register struct passwd *pw;
29 	register FILE *xfile;
30 	char buf[MAXLINE+1];
31 	extern struct passwd *getpwnam();
32 	register char *p;
33 	extern char *ttypath();
34 	static int exclusive;
35 	typedef int (*fnptr)();
36 	ENVELOPE errenvelope;
37 	register ENVELOPE *ee;
38 	extern ENVELOPE *newenvelope();
39 
40 	if (exclusive++)
41 		return;
42 	if (CurEnv->e_class <= PRI_JUNK)
43 	{
44 		if (Verbose)
45 			message(Arpa_Info, "Dumping junk mail");
46 		return;
47 	}
48 	ForceMail = TRUE;
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 (CurEnv->e_from.q_paddr == NULL)
56 	{
57 		if (parse("root", &CurEnv->e_from, 0) == NULL)
58 		{
59 			syserr("Cannot parse root!");
60 			ExitStat = EX_SOFTWARE;
61 			finis();
62 		}
63 	}
64 	CurEnv->e_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 	**  Also, if the from address is not local, mail it back.
72 	*/
73 
74 	if (BerkNet)
75 	{
76 		ExitStat = EX_OK;
77 		MailBack = TRUE;
78 	}
79 	if (!bitset(M_LOCAL, CurEnv->e_from.q_mailer->m_flags))
80 		MailBack = TRUE;
81 
82 	/*
83 	**  If writing back, do it.
84 	**	If the user is still logged in on the same terminal,
85 	**	then write the error messages back to hir (sic).
86 	**	If not, set the MailBack flag so that it will get
87 	**	mailed back instead.
88 	*/
89 
90 	if (WriteBack)
91 	{
92 		p = ttypath();
93 		if (p == NULL || freopen(p, "w", stdout) == NULL)
94 		{
95 			MailBack = TRUE;
96 			errno = 0;
97 		}
98 		else
99 		{
100 			(void) fflush(Xscript);
101 			xfile = fopen(Transcript, "r");
102 			if (xfile == NULL)
103 				syserr("Cannot open %s", Transcript);
104 			expand("$n", buf, &buf[sizeof buf - 1], CurEnv);
105 			printf("\r\nMessage from %s...\r\n", buf);
106 			printf("Errors occurred while sending mail; transcript follows:\r\n");
107 			while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout))
108 				fputs(buf, stdout);
109 			if (ferror(stdout))
110 				(void) syserr("savemail: stdout: write err");
111 			(void) fclose(xfile);
112 		}
113 	}
114 
115 	/*
116 	**  If mailing back, do it.
117 	**	Throw away all further output.  Don't do aliases, since
118 	**	this could cause loops, e.g., if joe mails to x:joe,
119 	**	and for some reason the network for x: is down, then
120 	**	the response gets sent to x:joe, which gives a
121 	**	response, etc.  Also force the mail to be delivered
122 	**	even if a version of it has already been sent to the
123 	**	sender.
124 	*/
125 
126 	if (MailBack)
127 	{
128 		if (returntosender("Unable to deliver mail", &CurEnv->e_from, TRUE) == 0)
129 			return;
130 	}
131 
132 	/*
133 	**  Save the message in dead.letter.
134 	**	If we weren't mailing back, and the user is local, we
135 	**	should save the message in dead.letter so that the
136 	**	poor person doesn't have to type it over again --
137 	**	and we all know what poor typists programmers are.
138 	*/
139 
140 	if (ArpaMode)
141 		return;
142 	p = NULL;
143 	if (CurEnv->e_from.q_mailer == LocalMailer)
144 	{
145 		if (CurEnv->e_from.q_home != NULL)
146 			p = CurEnv->e_from.q_home;
147 		else if ((pw = getpwnam(CurEnv->e_from.q_user)) != NULL)
148 			p = pw->pw_dir;
149 	}
150 	if (p == NULL)
151 	{
152 		syserr("Can't return mail to %s", CurEnv->e_from.q_paddr);
153 # ifdef DEBUG
154 		p = "/usr/tmp";
155 # else
156 		p = NULL;
157 # endif
158 	}
159 	if (p != NULL && TempFile != NULL)
160 	{
161 		auto ADDRESS *q;
162 
163 		/* we have a home directory; open dead.letter */
164 		message(Arpa_Info, "Saving message in dead.letter");
165 		define('z', p);
166 		expand("$z/dead.letter", buf, &buf[sizeof buf - 1], CurEnv);
167 		CurEnv->e_to = buf;
168 		q = NULL;
169 		sendto(buf, -1, (ADDRESS *) NULL, &q);
170 		(void) deliver(q);
171 	}
172 
173 	/* add terminator to writeback message */
174 	if (WriteBack)
175 		printf("-----\r\n");
176 }
177 /*
178 **  RETURNTOSENDER -- return a message to the sender with an error.
179 **
180 **	Parameters:
181 **		msg -- the explanatory message.
182 **		sendbody -- if TRUE, also send back the body of the
183 **			message; otherwise just send the header.
184 **
185 **	Returns:
186 **		zero -- if everything went ok.
187 **		else -- some error.
188 **
189 **	Side Effects:
190 **		Returns the current message to the sender via
191 **		mail.
192 */
193 
194 static char	*ErrorMessage;
195 static bool	SendBody;
196 
197 returntosender(msg, returnto, sendbody)
198 	char *msg;
199 	ADDRESS *returnto;
200 	bool sendbody;
201 {
202 	ADDRESS to_addr;
203 	char buf[MAXNAME];
204 	register int i;
205 	extern putheader(), errbody();
206 	register ENVELOPE *ee;
207 	extern ENVELOPE *newenvelope();
208 	ENVELOPE errenvelope;
209 
210 	NoAlias = TRUE;
211 	SendBody = sendbody;
212 	ee = newenvelope(&errenvelope);
213 	ee->e_puthdr = putheader;
214 	ee->e_putbody = errbody;
215 	addheader("date", "$b", ee);
216 	addheader("from", "$g (Mail Delivery Subsystem)", ee);
217 	addheader("to", returnto->q_paddr, ee);
218 	addheader("subject", msg, ee);
219 
220 	/* fake up an address header for the from person */
221 	bmove((char *) returnto, (char *) &to_addr, sizeof to_addr);
222 	expand("$n", buf, &buf[sizeof buf - 1], CurEnv);
223 	if (parse(buf, &ee->e_from, -1) == NULL)
224 	{
225 		syserr("Can't parse myself!");
226 		ExitStat = EX_SOFTWARE;
227 		return (-1);
228 	}
229 	to_addr.q_next = NULL;
230 	to_addr.q_flags &= ~QDONTSEND;
231 	ee->e_sendqueue = &to_addr;
232 
233 	/* push state into submessage */
234 	CurEnv = ee;
235 	define('f', "$n");
236 	define('x', "Mail Delivery Subsystem");
237 
238 	/* actually deliver the error message */
239 	i = deliver(&to_addr);
240 
241 	/* if the error message was "queued", make that happen */
242 	if (bitset(QQUEUEUP, to_addr.q_flags))
243 		queueup(ee);
244 
245 	/* restore state */
246 	CurEnv = CurEnv->e_parent;
247 
248 	if (i != 0)
249 	{
250 		syserr("Can't return mail to %s", to_addr.q_paddr);
251 		return (-1);
252 	}
253 	return (0);
254 }
255 /*
256 **  ERRHEADER -- Output the header for error mail.
257 **
258 **	Parameters:
259 **		xfile -- the transcript file.
260 **		fp -- the output file.
261 **
262 **	Returns:
263 **		none
264 **
265 **	Side Effects:
266 **		Outputs the header for an error message.
267 */
268 
269 errheader(fp, m)
270 	register FILE *fp;
271 	register struct mailer *m;
272 {
273 	/*
274 	**  Output header of error message.
275 	*/
276 
277 	putheader(fp, m);
278 }
279 /*
280 **  ERRBODY -- output the body of an error message.
281 **
282 **	Typically this is a copy of the transcript plus a copy of the
283 **	original offending message.
284 **
285 **	Parameters:
286 **		xfile -- the transcript file.
287 **		fp -- the output file.
288 **		xdot -- if set, use the SMTP hidden dot algorithm.
289 **
290 **	Returns:
291 **		none
292 **
293 **	Side Effects:
294 **		Outputs the body of an error message.
295 */
296 
297 errbody(fp, m, xdot)
298 	register FILE *fp;
299 	register struct mailer *m;
300 	bool xdot;
301 {
302 	register FILE *xfile;
303 	char buf[MAXLINE];
304 
305 	(void) fflush(stdout);
306 	if ((xfile = fopen(Transcript, "r")) == NULL)
307 		syserr("Cannot open %s", Transcript);
308 	errno = 0;
309 
310 	/*
311 	**  Output transcript of errors
312 	*/
313 
314 	fprintf(fp, "   ----- Transcript of session follows -----\n");
315 	(void) fflush(Xscript);
316 	while (fgets(buf, sizeof buf, xfile) != NULL)
317 		fputs(buf, fp);
318 
319 	/*
320 	**  Output text of original message
321 	*/
322 
323 	if (NoReturn)
324 		fprintf(fp, "\n   ----- Return message suppressed -----\n\n");
325 	else if (TempFile != NULL)
326 	{
327 		if (SendBody)
328 		{
329 			fprintf(fp, "\n   ----- Unsent message follows -----\n");
330 			(void) fflush(fp);
331 			putheader(fp, Mailer[1], CurEnv->e_parent);
332 			fprintf(fp, "\n");
333 			putbody(fp, Mailer[1], xdot);
334 		}
335 		else
336 		{
337 			fprintf(fp, "\n  ----- Message header follows -----\n");
338 			(void) fflush(fp);
339 			putheader(fp, Mailer[1]);
340 		}
341 	}
342 	else
343 		fprintf(fp, "\n  ----- No message was collected -----\n\n");
344 
345 	/*
346 	**  Cleanup and exit
347 	*/
348 
349 	(void) fclose(xfile);
350 	if (errno != 0)
351 		syserr("errbody: I/O error");
352 }
353