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