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