1 # include <pwd.h>
2 # include "sendmail.h"
3 
4 SCCSID(@(#)savemail.c	3.26		02/15/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 
37 	if (exclusive++)
38 		return;
39 	ForceMail = TRUE;
40 
41 	/*
42 	**  In the unhappy event we don't know who to return the mail
43 	**  to, make someone up.
44 	*/
45 
46 	if (From.q_paddr == NULL)
47 	{
48 		if (parse("root", &From, 0) == NULL)
49 		{
50 			syserr("Cannot parse root!");
51 			ExitStat = EX_SOFTWARE;
52 			finis();
53 		}
54 	}
55 	To = NULL;
56 
57 	/*
58 	**  If called from Eric Schmidt's network, do special mailback.
59 	**	Fundamentally, this is the mailback case except that
60 	**	it returns an OK exit status (assuming the return
61 	**	worked).
62 	*/
63 
64 	if (BerkNet)
65 	{
66 		ExitStat = EX_OK;
67 		MailBack++;
68 	}
69 
70 	/*
71 	**  If writing back, do it.
72 	**	If the user is still logged in on the same terminal,
73 	**	then write the error messages back to hir (sic).
74 	**	If not, set the MailBack flag so that it will get
75 	**	mailed back instead.
76 	*/
77 
78 	if (WriteBack)
79 	{
80 		p = ttypath();
81 		if (p == NULL || freopen(p, "w", stdout) == NULL)
82 		{
83 			MailBack++;
84 			errno = 0;
85 		}
86 		else
87 		{
88 			(void) fflush(Xscript);
89 			xfile = fopen(Transcript, "r");
90 			if (xfile == NULL)
91 				syserr("Cannot open %s", Transcript);
92 			(void) expand("$n", buf, &buf[sizeof buf - 1]);
93 			printf("\r\nMessage from %s...\r\n", buf);
94 			printf("Errors occurred while sending mail; transcript follows:\r\n");
95 			while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout))
96 				fputs(buf, stdout);
97 			if (ferror(stdout))
98 				(void) syserr("savemail: stdout: write err");
99 			(void) fclose(xfile);
100 		}
101 	}
102 
103 	/*
104 	**  If mailing back, do it.
105 	**	Throw away all further output.  Don't do aliases, since
106 	**	this could cause loops, e.g., if joe mails to x:joe,
107 	**	and for some reason the network for x: is down, then
108 	**	the response gets sent to x:joe, which gives a
109 	**	response, etc.  Also force the mail to be delivered
110 	**	even if a version of it has already been sent to the
111 	**	sender.
112 	*/
113 
114 	if (MailBack)
115 	{
116 		if (returntosender("Unable to deliver mail") == 0)
117 			return;
118 	}
119 
120 	/*
121 	**  Save the message in dead.letter.
122 	**	If we weren't mailing back, and the user is local, we
123 	**	should save the message in dead.letter so that the
124 	**	poor person doesn't have to type it over again --
125 	**	and we all know what poor typists programmers are.
126 	*/
127 
128 	if (ArpaMode)
129 		return;
130 	p = NULL;
131 	if (From.q_mailer == LocalMailer)
132 	{
133 		if (From.q_home != NULL)
134 			p = From.q_home;
135 		else if ((pw = getpwnam(From.q_user)) != NULL)
136 			p = pw->pw_dir;
137 	}
138 	if (p == NULL)
139 	{
140 		syserr("Can't return mail to %s", From.q_paddr);
141 # ifdef DEBUG
142 		p = "/usr/tmp";
143 # else
144 		p = NULL;
145 # endif
146 	}
147 	if (p != NULL && TempFile != NULL)
148 	{
149 		auto ADDRESS *q;
150 
151 		/* we have a home directory; open dead.letter */
152 		message(Arpa_Info, "Saving message in dead.letter");
153 		define('z', p);
154 		(void) expand("$z/dead.letter", buf, &buf[sizeof buf - 1]);
155 		To = buf;
156 		q = NULL;
157 		sendto(buf, -1, (ADDRESS *) NULL, &q);
158 		(void) deliver(q, (fnptr) NULL);
159 	}
160 
161 	/* add terminator to writeback message */
162 	if (WriteBack)
163 		printf("-----\r\n");
164 }
165 /*
166 **  RETURNTOSENDER -- return a message to the sender with an error.
167 **
168 **	Parameters:
169 **		msg -- the explanatory message.
170 **
171 **	Returns:
172 **		zero -- if everything went ok.
173 **		else -- some error.
174 **
175 **	Side Effects:
176 **		Returns the current message to the sender via
177 **		mail.
178 */
179 
180 static char	*ErrorMessage;
181 
182 returntosender(msg)
183 	char *msg;
184 {
185 	ADDRESS to_addr;
186 	char buf[MAXNAME];
187 	register int i;
188 	extern errhdr();
189 
190 	(void) freopen("/dev/null", "w", stdout);
191 	NoAlias++;
192 	ErrorMessage = msg;
193 
194 	/* fake up an address header for the from person */
195 	bmove((char *) &From, (char *) &to_addr, sizeof to_addr);
196 	(void) expand("$n", buf, &buf[sizeof buf - 1]);
197 	if (parse(buf, &From, -1) == NULL)
198 	{
199 		syserr("Can't parse myself!");
200 		ExitStat = EX_SOFTWARE;
201 		return (-1);
202 	}
203 	to_addr.q_next = NULL;
204 	i = deliver(&to_addr, errhdr);
205 	bmove((char *) &to_addr, (char *) &From, sizeof From);
206 	if (i != 0)
207 	{
208 		syserr("Can't return mail to %s", From.q_paddr);
209 		return (-1);
210 	}
211 	return (0);
212 }
213 /*
214 **  ERRHDR -- Output the header for error mail.
215 **
216 **	This is the edit filter to error mailbacks.
217 **
218 **	Parameters:
219 **		xfile -- the transcript file.
220 **		fp -- the output file.
221 **		xdot -- if set, use smtp hidden dot algorithm.
222 **
223 **	Returns:
224 **		none
225 **
226 **	Side Effects:
227 **		Outputs the current message with an appropriate
228 **		error header.
229 */
230 
231 errhdr(fp, m, xdot)
232 	register FILE *fp;
233 	register struct mailer *m;
234 	bool xdot;
235 {
236 	char buf[MAXLINE];
237 	register FILE *xfile;
238 	extern char *macvalue();
239 	char *oldfmac;
240 	char *oldgmac;
241 
242 	oldfmac = macvalue('f');
243 	define('f', "$n");
244 	oldgmac = macvalue('g');
245 	define('g', m->m_from);
246 
247 	(void) fflush(stdout);
248 	(void) fflush(Xscript);
249 	if ((xfile = fopen(Transcript, "r")) == NULL)
250 		syserr("Cannot open %s", Transcript);
251 	errno = 0;
252 
253 	/*
254 	**  Output "From" line unless supressed
255 	*/
256 
257 	if (!bitset(M_NHDR, m->m_flags))
258 	{
259 		(void) expand("$l", buf, &buf[sizeof buf - 1]);
260 		fprintf(fp, "%s\n", buf);
261 	}
262 
263 	/*
264 	**  Output header of error message.
265 	*/
266 
267 	if (bitset(M_NEEDDATE, m->m_flags))
268 	{
269 		(void) expand("$b", buf, &buf[sizeof buf - 1]);
270 		fprintf(fp, "Date: %s\n", buf);
271 	}
272 	if (bitset(M_NEEDFROM, m->m_flags))
273 	{
274 		(void) expand("$g", buf, &buf[sizeof buf - 1]);
275 		fprintf(fp, "From: %s (Mail Delivery Subsystem)\n", buf);
276 	}
277 	fprintf(fp, "To: %s\n", To);
278 	fprintf(fp, "Subject: %s\n", ErrorMessage);
279 
280 	/*
281 	**  End of error message header
282 	*/
283 
284 	define('f', oldfmac);
285 	define('g', oldgmac);
286 
287 	/*
288 	**  Output transcript of errors
289 	*/
290 
291 	fprintf(fp, "\n   ----- Transcript of session follows -----\n");
292 	(void) fflush(Xscript);
293 	while (fgets(buf, sizeof buf, xfile) != NULL)
294 		fputs(buf, fp);
295 
296 	/*
297 	**  Output text of original message
298 	*/
299 
300 	if (NoReturn)
301 		fprintf(fp, "\n   ----- Return message suppressed -----\n\n");
302 	else if (TempFile != NULL)
303 	{
304 		fprintf(fp, "\n   ----- Unsent message follows -----\n");
305 		(void) fflush(fp);
306 		putmessage(fp, Mailer[1], xdot);
307 	}
308 	else
309 		fprintf(fp, "\n  ----- No message was collected -----\n\n");
310 
311 	/*
312 	**  Cleanup and exit
313 	*/
314 
315 	(void) fclose(xfile);
316 	if (errno != 0)
317 		syserr("errhdr: I/O error");
318 }
319