1 # include <pwd.h>
2 # include "sendmail.h"
3 
4 SCCSID(@(#)savemail.c	3.35		06/16/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, 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 	register int i;
208 	extern putheader(), errbody();
209 	register ENVELOPE *ee;
210 	extern ENVELOPE *newenvelope();
211 	ENVELOPE errenvelope;
212 	static int returndepth;
213 
214 	if (++returndepth >= MAXRETURNS)
215 	{
216 		if (returndepth != MAXRETURNS)
217 			syserr("returntosender: infinite recursion on %s", returnto->q_paddr);
218 		/* don't "unrecurse" and fake a clean exit */
219 		/* returndepth--; */
220 		return (0);
221 	}
222 
223 	NoAlias = TRUE;
224 	SendBody = sendbody;
225 	ee = newenvelope(&errenvelope);
226 	ee->e_puthdr = putheader;
227 	ee->e_putbody = errbody;
228 	addheader("date", "$b", ee);
229 	addheader("from", "$g (Mail Delivery Subsystem)", ee);
230 	addheader("to", returnto->q_paddr, ee);
231 	addheader("subject", msg, ee);
232 
233 	/* fake up an address header for the from person */
234 	expand("$n", buf, &buf[sizeof buf - 1], CurEnv);
235 	if (parse(buf, &ee->e_from, -1) == NULL)
236 	{
237 		syserr("Can't parse myself!");
238 		ExitStat = EX_SOFTWARE;
239 		returndepth--;
240 		return (-1);
241 	}
242 	ee->e_sendqueue = returnto;
243 
244 	/* push state into submessage */
245 	CurEnv = ee;
246 	define('f', "$n");
247 	define('x', "Mail Delivery Subsystem");
248 
249 	/* actually deliver the error message */
250 	sendall(ee, FALSE);
251 
252 	/* do any closing error processing */
253 	checkerrors(ee);
254 
255 	/* restore state */
256 	CurEnv = CurEnv->e_parent;
257 	returndepth--;
258 
259 	/* should check for delivery errors here */
260 	return (0);
261 }
262 /*
263 **  ERRBODY -- output the body of an error message.
264 **
265 **	Typically this is a copy of the transcript plus a copy of the
266 **	original offending message.
267 **
268 **	Parameters:
269 **		xfile -- the transcript file.
270 **		fp -- the output file.
271 **		xdot -- if set, use the SMTP hidden dot algorithm.
272 **
273 **	Returns:
274 **		none
275 **
276 **	Side Effects:
277 **		Outputs the body of an error message.
278 */
279 
280 errbody(fp, m, xdot)
281 	register FILE *fp;
282 	register struct mailer *m;
283 	bool xdot;
284 {
285 	register FILE *xfile;
286 	char buf[MAXLINE];
287 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
288 
289 	(void) fflush(stdout);
290 	if ((xfile = fopen(Transcript, "r")) == NULL)
291 		syserr("Cannot open %s", Transcript);
292 	errno = 0;
293 
294 	/*
295 	**  Output transcript of errors
296 	*/
297 
298 	fprintf(fp, "   ----- Transcript of session follows -----\n");
299 	(void) fflush(Xscript);
300 	while (fgets(buf, sizeof buf, xfile) != NULL)
301 		putline(buf, fp, fullsmtp);
302 
303 	/*
304 	**  Output text of original message
305 	*/
306 
307 	if (NoReturn)
308 		fprintf(fp, "\n   ----- Return message suppressed -----\n\n");
309 	else if (TempFile != NULL)
310 	{
311 		if (SendBody)
312 		{
313 			fprintf(fp, "\n   ----- Unsent message follows -----\n");
314 			(void) fflush(fp);
315 			putheader(fp, m, CurEnv->e_parent);
316 			fprintf(fp, "\n");
317 			putbody(fp, m, xdot);
318 		}
319 		else
320 		{
321 			fprintf(fp, "\n  ----- Message header follows -----\n");
322 			(void) fflush(fp);
323 			putheader(fp, m, CurEnv);
324 		}
325 	}
326 	else
327 		fprintf(fp, "\n  ----- No message was collected -----\n\n");
328 
329 	/*
330 	**  Cleanup and exit
331 	*/
332 
333 	(void) fclose(xfile);
334 	if (errno != 0)
335 		syserr("errbody: I/O error");
336 }
337