1 # include <pwd.h>
2 # include "sendmail.h"
3 
4 SCCSID(@(#)savemail.c	4.5		05/13/84);
5 
6 /*
7 **  SAVEMAIL -- Save mail on error
8 **
9 **	If mailing back errors, 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 **		e -- the envelope containing the message in error.
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(e)
27 	register ENVELOPE *e;
28 {
29 	register struct passwd *pw;
30 	register FILE *xfile;
31 	char buf[MAXLINE+1];
32 	extern struct passwd *getpwnam();
33 	register char *p;
34 	extern char *ttypath();
35 	typedef int (*fnptr)();
36 
37 # ifdef DEBUG
38 	if (tTd(6, 1))
39 		printf("\nsavemail\n");
40 # endif DEBUG
41 
42 	if (bitset(EF_RESPONSE, e->e_flags))
43 		return;
44 	if (e->e_class < 0)
45 	{
46 		message(Arpa_Info, "Dumping junk mail");
47 		return;
48 	}
49 	ForceMail = TRUE;
50 	e->e_flags &= ~EF_FATALERRS;
51 
52 	/*
53 	**  In the unhappy event we don't know who to return the mail
54 	**  to, make someone up.
55 	*/
56 
57 	if (e->e_from.q_paddr == NULL)
58 	{
59 		if (parseaddr("root", &e->e_from, 0, '\0') == NULL)
60 		{
61 			syserr("Cannot parse root!");
62 			ExitStat = EX_SOFTWARE;
63 			finis();
64 		}
65 	}
66 	e->e_to = NULL;
67 
68 	/*
69 	**  If called from Eric Schmidt's network, do special mailback.
70 	**	Fundamentally, this is the mailback case except that
71 	**	it returns an OK exit status (assuming the return
72 	**	worked).
73 	**  Also, if the from address is not local, mail it back.
74 	*/
75 
76 	if (ErrorMode == EM_BERKNET)
77 	{
78 		ExitStat = EX_OK;
79 		ErrorMode = EM_MAIL;
80 	}
81 	if (!bitnset(M_LOCAL, e->e_from.q_mailer->m_flags))
82 		ErrorMode = EM_MAIL;
83 
84 	/*
85 	**  If writing back, do it.
86 	**	If the user is still logged in on the same terminal,
87 	**	then write the error messages back to hir (sic).
88 	**	If not, mail back instead.
89 	*/
90 
91 	if (ErrorMode == EM_WRITE)
92 	{
93 		p = ttypath();
94 		if (p == NULL || freopen(p, "w", stdout) == NULL)
95 		{
96 			ErrorMode = EM_MAIL;
97 			errno = 0;
98 		}
99 		else
100 		{
101 			expand("\001n", buf, &buf[sizeof buf - 1], e);
102 			printf("\r\nMessage from %s...\r\n", buf);
103 			printf("Errors occurred while sending mail.\r\n");
104 			if (e->e_xfp != NULL)
105 			{
106 				(void) fflush(e->e_xfp);
107 				xfile = fopen(queuename(e, 'x'), "r");
108 			}
109 			else
110 				xfile = NULL;
111 			if (xfile == NULL)
112 			{
113 				syserr("Cannot open %s", queuename(e, 'x'));
114 				printf("Transcript of session is unavailable.\r\n");
115 			}
116 			else
117 			{
118 				printf("Transcript follows:\r\n");
119 				while (fgets(buf, sizeof buf, xfile) != NULL &&
120 				       !ferror(stdout))
121 					fputs(buf, stdout);
122 				(void) fclose(xfile);
123 			}
124 			if (ferror(stdout))
125 				(void) syserr("savemail: stdout: write err");
126 		}
127 	}
128 
129 	/*
130 	**  If mailing back, do it.
131 	**	Throw away all further output.  Don't do aliases, since
132 	**	this could cause loops, e.g., if joe mails to x:joe,
133 	**	and for some reason the network for x: is down, then
134 	**	the response gets sent to x:joe, which gives a
135 	**	response, etc.  Also force the mail to be delivered
136 	**	even if a version of it has already been sent to the
137 	**	sender.
138 	*/
139 
140 	if (ErrorMode == EM_MAIL)
141 	{
142 		if (e->e_errorqueue == NULL)
143 			sendtolist(e->e_from.q_paddr, (ADDRESS *) NULL,
144 			       &e->e_errorqueue);
145 		if (returntosender(e->e_message != NULL ? e->e_message :
146 				   "Unable to deliver mail",
147 				   e->e_errorqueue, TRUE) == 0)
148 			return;
149 	}
150 
151 	/*
152 	**  Save the message in dead.letter.
153 	**	If we weren't mailing back, and the user is local, we
154 	**	should save the message in dead.letter so that the
155 	**	poor person doesn't have to type it over again --
156 	**	and we all know what poor typists programmers are.
157 	*/
158 
159 	p = NULL;
160 	if (e->e_from.q_mailer == LocalMailer)
161 	{
162 		if (e->e_from.q_home != NULL)
163 			p = e->e_from.q_home;
164 		else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
165 			p = pw->pw_dir;
166 	}
167 	if (p == NULL)
168 	{
169 		syserr("Can't return mail to %s", e->e_from.q_paddr);
170 # ifdef DEBUG
171 		p = "/usr/tmp";
172 # endif
173 	}
174 	if (p != NULL && e->e_dfp != NULL)
175 	{
176 		auto ADDRESS *q;
177 		bool oldverb = Verbose;
178 
179 		/* we have a home directory; open dead.letter */
180 		define('z', p, e);
181 		expand("\001z/dead.letter", buf, &buf[sizeof buf - 1], e);
182 		Verbose = TRUE;
183 		message(Arpa_Info, "Saving message in %s", buf);
184 		Verbose = oldverb;
185 		e->e_to = buf;
186 		q = NULL;
187 		sendtolist(buf, (ADDRESS *) NULL, &q);
188 		(void) deliver(e, q);
189 	}
190 
191 	/* add terminator to writeback message */
192 	if (ErrorMode == EM_WRITE)
193 		printf("-----\r\n");
194 }
195 /*
196 **  RETURNTOSENDER -- return a message to the sender with an error.
197 **
198 **	Parameters:
199 **		msg -- the explanatory message.
200 **		returnq -- the queue of people to send the message to.
201 **		sendbody -- if TRUE, also send back the body of the
202 **			message; otherwise just send the header.
203 **
204 **	Returns:
205 **		zero -- if everything went ok.
206 **		else -- some error.
207 **
208 **	Side Effects:
209 **		Returns the current message to the sender via
210 **		mail.
211 */
212 
213 static bool	SendBody;
214 
215 #define MAXRETURNS	6	/* max depth of returning messages */
216 
217 returntosender(msg, returnq, sendbody)
218 	char *msg;
219 	ADDRESS *returnq;
220 	bool sendbody;
221 {
222 	char buf[MAXNAME];
223 	extern putheader(), errbody();
224 	register ENVELOPE *ee;
225 	extern ENVELOPE *newenvelope();
226 	ENVELOPE errenvelope;
227 	static int returndepth;
228 	register ADDRESS *q;
229 
230 # ifdef DEBUG
231 	if (tTd(6, 1))
232 	{
233 		printf("Return To Sender: msg=\"%s\", depth=%d, CurEnv=%x,\n",
234 		       msg, returndepth, CurEnv);
235 		printf("\treturnto=");
236 		printaddr(returnq, TRUE);
237 	}
238 # endif DEBUG
239 
240 	if (++returndepth >= MAXRETURNS)
241 	{
242 		if (returndepth != MAXRETURNS)
243 			syserr("returntosender: infinite recursion on %s", returnq->q_paddr);
244 		/* don't "unrecurse" and fake a clean exit */
245 		/* returndepth--; */
246 		return (0);
247 	}
248 
249 	SendBody = sendbody;
250 	define('g', "\001f", CurEnv);
251 	ee = newenvelope(&errenvelope);
252 	ee->e_puthdr = putheader;
253 	ee->e_putbody = errbody;
254 	ee->e_flags |= EF_RESPONSE;
255 	ee->e_sendqueue = returnq;
256 	openxscript(ee);
257 	for (q = returnq; q != NULL; q = q->q_next)
258 	{
259 		if (q->q_alias == NULL)
260 			addheader("to", q->q_paddr, ee);
261 	}
262 	(void) sprintf(buf, "Returned mail: %s", msg);
263 	addheader("subject", buf, ee);
264 
265 	/* fake up an address header for the from person */
266 	expand("\001n", buf, &buf[sizeof buf - 1], CurEnv);
267 	if (parseaddr(buf, &ee->e_from, -1, '\0') == NULL)
268 	{
269 		syserr("Can't parse myself!");
270 		ExitStat = EX_SOFTWARE;
271 		returndepth--;
272 		return (-1);
273 	}
274 	loweraddr(&ee->e_from);
275 
276 	/* push state into submessage */
277 	CurEnv = ee;
278 	define('f', "\001n", ee);
279 	define('x', "Mail Delivery Subsystem", ee);
280 	eatheader(ee);
281 
282 	/* actually deliver the error message */
283 	sendall(ee, SM_DEFAULT);
284 
285 	/* restore state */
286 	dropenvelope(ee);
287 	CurEnv = CurEnv->e_parent;
288 	returndepth--;
289 
290 	/* should check for delivery errors here */
291 	return (0);
292 }
293 /*
294 **  ERRBODY -- output the body of an error message.
295 **
296 **	Typically this is a copy of the transcript plus a copy of the
297 **	original offending message.
298 **
299 **	Parameters:
300 **		fp -- the output file.
301 **		m -- the mailer to output to.
302 **		e -- the envelope we are working in.
303 **
304 **	Returns:
305 **		none
306 **
307 **	Side Effects:
308 **		Outputs the body of an error message.
309 */
310 
311 errbody(fp, m, e)
312 	register FILE *fp;
313 	register struct mailer *m;
314 	register ENVELOPE *e;
315 {
316 	register FILE *xfile;
317 	char buf[MAXLINE];
318 	char *p;
319 
320 	/*
321 	**  Output transcript of errors
322 	*/
323 
324 	(void) fflush(stdout);
325 	p = queuename(e->e_parent, 'x');
326 	if ((xfile = fopen(p, "r")) == NULL)
327 	{
328 		syserr("Cannot open %s", p);
329 		fprintf(fp, "  ----- Transcript of session is unavailable -----\n");
330 	}
331 	else
332 	{
333 		fprintf(fp, "   ----- Transcript of session follows -----\n");
334 		if (e->e_xfp != NULL)
335 			(void) fflush(e->e_xfp);
336 		while (fgets(buf, sizeof buf, xfile) != NULL)
337 			putline(buf, fp, m);
338 		(void) fclose(xfile);
339 	}
340 	errno = 0;
341 
342 	/*
343 	**  Output text of original message
344 	*/
345 
346 	if (NoReturn)
347 		fprintf(fp, "\n   ----- Return message suppressed -----\n\n");
348 	else if (e->e_parent->e_dfp != NULL)
349 	{
350 		if (SendBody)
351 		{
352 			putline("\n", fp, m);
353 			putline("   ----- Unsent message follows -----\n", fp, m);
354 			(void) fflush(fp);
355 			putheader(fp, m, e->e_parent);
356 			putline("\n", fp, m);
357 			putbody(fp, m, e->e_parent);
358 		}
359 		else
360 		{
361 			putline("\n", fp, m);
362 			putline("  ----- Message header follows -----\n", fp, m);
363 			(void) fflush(fp);
364 			putheader(fp, m, e->e_parent);
365 		}
366 	}
367 	else
368 	{
369 		putline("\n", fp, m);
370 		putline("  ----- No message was collected -----\n", fp, m);
371 		putline("\n", fp, m);
372 	}
373 
374 	/*
375 	**  Cleanup and exit
376 	*/
377 
378 	if (errno != 0)
379 		syserr("errbody: I/O error");
380 }
381