1 # include <pwd.h>
2 # include "sendmail.h"
3 
4 SCCSID(@(#)savemail.c	3.39		07/05/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 	extern ENVELOPE *newenvelope();
37 
38 # ifdef DEBUG
39 	if (Debug)
40 		printf("\nsavemail: exclusive %d\n", exclusive);
41 # endif DEBUG
42 
43 	if (exclusive++)
44 		return;
45 	if (CurEnv->e_class <= PRI_JUNK)
46 	{
47 		message(Arpa_Info, "Dumping junk mail");
48 		return;
49 	}
50 	ForceMail = TRUE;
51 	FatalErrors = FALSE;
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 (CurEnv->e_from.q_paddr == NULL)
59 	{
60 		if (parse("root", &CurEnv->e_from, 0) == NULL)
61 		{
62 			syserr("Cannot parse root!");
63 			ExitStat = EX_SOFTWARE;
64 			finis();
65 		}
66 	}
67 	CurEnv->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, CurEnv->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 			(void) fflush(Xscript);
104 			xfile = fopen(Transcript, "r");
105 			if (xfile == NULL)
106 				syserr("Cannot open %s", Transcript);
107 			expand("$n", buf, &buf[sizeof buf - 1], CurEnv);
108 			printf("\r\nMessage from %s...\r\n", buf);
109 			printf("Errors occurred while sending mail; transcript follows:\r\n");
110 			while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout))
111 				fputs(buf, stdout);
112 			if (ferror(stdout))
113 				(void) syserr("savemail: stdout: write err");
114 			(void) fclose(xfile);
115 		}
116 	}
117 
118 	/*
119 	**  If mailing back, do it.
120 	**	Throw away all further output.  Don't do aliases, since
121 	**	this could cause loops, e.g., if joe mails to x:joe,
122 	**	and for some reason the network for x: is down, then
123 	**	the response gets sent to x:joe, which gives a
124 	**	response, etc.  Also force the mail to be delivered
125 	**	even if a version of it has already been sent to the
126 	**	sender.
127 	*/
128 
129 	if (MailBack)
130 	{
131 		if (CurEnv->e_errorqueue == NULL)
132 			sendto(CurEnv->e_from.q_paddr, 1, (ADDRESS *) NULL, &CurEnv->e_errorqueue);
133 		if (returntosender("Unable to deliver mail", CurEnv->e_errorqueue, TRUE) == 0)
134 			return;
135 	}
136 
137 	/*
138 	**  Save the message in dead.letter.
139 	**	If we weren't mailing back, and the user is local, we
140 	**	should save the message in dead.letter so that the
141 	**	poor person doesn't have to type it over again --
142 	**	and we all know what poor typists programmers are.
143 	**	However, if we are running a "smart" protocol, we don't
144 	**	bother to return the message, since the other end is
145 	**	expected to handle that.
146 	*/
147 
148 	if (ArpaMode)
149 		return;
150 	p = NULL;
151 	if (CurEnv->e_from.q_mailer == LocalMailer)
152 	{
153 		if (CurEnv->e_from.q_home != NULL)
154 			p = CurEnv->e_from.q_home;
155 		else if ((pw = getpwnam(CurEnv->e_from.q_user)) != NULL)
156 			p = pw->pw_dir;
157 	}
158 	if (p == NULL)
159 	{
160 		syserr("Can't return mail to %s", CurEnv->e_from.q_paddr);
161 # ifdef DEBUG
162 		p = "/usr/tmp";
163 # endif
164 	}
165 	if (p != NULL && TempFile != NULL)
166 	{
167 		auto ADDRESS *q;
168 		bool oldverb = Verbose;
169 
170 		/* we have a home directory; open dead.letter */
171 		Verbose = TRUE;
172 		message(Arpa_Info, "Saving message in dead.letter");
173 		Verbose = oldverb;
174 		define('z', p);
175 		expand("$z/dead.letter", buf, &buf[sizeof buf - 1], CurEnv);
176 		CurEnv->e_to = buf;
177 		q = NULL;
178 		sendto(buf, -1, (ADDRESS *) NULL, &q);
179 		(void) deliver(q);
180 	}
181 
182 	/* add terminator to writeback message */
183 	if (WriteBack)
184 		printf("-----\r\n");
185 }
186 /*
187 **  RETURNTOSENDER -- return a message to the sender with an error.
188 **
189 **	Parameters:
190 **		msg -- the explanatory message.
191 **		returnto -- the queue of people to send the message to.
192 **		sendbody -- if TRUE, also send back the body of the
193 **			message; otherwise just send the header.
194 **
195 **	Returns:
196 **		zero -- if everything went ok.
197 **		else -- some error.
198 **
199 **	Side Effects:
200 **		Returns the current message to the sender via
201 **		mail.
202 */
203 
204 static bool	SendBody;
205 
206 #define MAXRETURNS	6	/* max depth of returning messages */
207 
208 returntosender(msg, returnto, sendbody)
209 	char *msg;
210 	ADDRESS *returnto;
211 	bool sendbody;
212 {
213 	char buf[MAXNAME];
214 	extern putheader(), errbody();
215 	register ENVELOPE *ee;
216 	extern ENVELOPE *newenvelope();
217 	ENVELOPE errenvelope;
218 	static int returndepth;
219 
220 # ifdef DEBUG
221 	if (Debug > 0)
222 	{
223 		printf("Return To Sender: msg=\"%s\", depth=%d, CurEnv=%x,\n",
224 		       msg, returndepth, CurEnv);
225 		printf("\treturnto=");
226 		printaddr(returnto, FALSE);
227 	}
228 # endif DEBUG
229 
230 	if (++returndepth >= MAXRETURNS)
231 	{
232 		if (returndepth != MAXRETURNS)
233 			syserr("returntosender: infinite recursion on %s", returnto->q_paddr);
234 		/* don't "unrecurse" and fake a clean exit */
235 		/* returndepth--; */
236 		return (0);
237 	}
238 
239 	NoAlias = TRUE;
240 	SendBody = sendbody;
241 	ee = newenvelope(&errenvelope);
242 	ee->e_puthdr = putheader;
243 	ee->e_putbody = errbody;
244 	addheader("date", "$b", ee);
245 	addheader("from", "$g (Mail Delivery Subsystem)", ee);
246 	addheader("to", returnto->q_paddr, ee);
247 	addheader("subject", msg, ee);
248 
249 	/* fake up an address header for the from person */
250 	expand("$n", buf, &buf[sizeof buf - 1], CurEnv);
251 	if (parse(buf, &ee->e_from, -1) == NULL)
252 	{
253 		syserr("Can't parse myself!");
254 		ExitStat = EX_SOFTWARE;
255 		returndepth--;
256 		return (-1);
257 	}
258 	ee->e_sendqueue = returnto;
259 
260 	/* push state into submessage */
261 	CurEnv = ee;
262 	define('f', "$n");
263 	define('x', "Mail Delivery Subsystem");
264 
265 	/* actually deliver the error message */
266 	sendall(ee, FALSE);
267 
268 	/* do any closing error processing */
269 	checkerrors(ee);
270 
271 	/* restore state */
272 	CurEnv = CurEnv->e_parent;
273 	returndepth--;
274 
275 	/* should check for delivery errors here */
276 	return (0);
277 }
278 /*
279 **  ERRBODY -- output the body of an error message.
280 **
281 **	Typically this is a copy of the transcript plus a copy of the
282 **	original offending message.
283 **
284 **	Parameters:
285 **		xfile -- the transcript file.
286 **		fp -- the output file.
287 **		xdot -- if set, use the SMTP hidden dot algorithm.
288 **
289 **	Returns:
290 **		none
291 **
292 **	Side Effects:
293 **		Outputs the body of an error message.
294 */
295 
296 errbody(fp, m, xdot)
297 	register FILE *fp;
298 	register struct mailer *m;
299 	bool xdot;
300 {
301 	register FILE *xfile;
302 	char buf[MAXLINE];
303 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
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 		putline(buf, fp, fullsmtp);
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, m, CurEnv->e_parent);
332 			fprintf(fp, "\n");
333 			putbody(fp, m, xdot);
334 		}
335 		else
336 		{
337 			fprintf(fp, "\n  ----- Message header follows -----\n");
338 			(void) fflush(fp);
339 			putheader(fp, m, CurEnv);
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