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