1 # include <pwd.h>
2 # include "sendmail.h"
3 
4 static char	SccsId[] = "@(#)savemail.c	3.20	10/23/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 errhdr();
32 	auto ADDRESS to_addr;
33 	extern struct passwd *getpwnam();
34 	register char *p;
35 	register int i;
36 	extern char *ttypath();
37 	static int exclusive;
38 
39 	if (exclusive++)
40 		return;
41 
42 	/*
43 	**  In the unhappy event we don't know who to return the mail
44 	**  to, make someone up.
45 	*/
46 
47 	if (From.q_paddr == NULL)
48 	{
49 		if (parse("root", &From, 0) == NULL)
50 		{
51 			syserr("Cannot parse root!");
52 			ExitStat = EX_SOFTWARE;
53 			finis();
54 		}
55 	}
56 	To = NULL;
57 
58 	/*
59 	**  If called from Eric Schmidt's network, do special mailback.
60 	**	Fundamentally, this is the mailback case except that
61 	**	it returns an OK exit status (assuming the return
62 	**	worked).
63 	*/
64 
65 	if (BerkNet)
66 	{
67 		ExitStat = EX_OK;
68 		MailBack++;
69 	}
70 
71 	/*
72 	**  If writing back, do it.
73 	**	If the user is still logged in on the same terminal,
74 	**	then write the error messages back to hir (sic).
75 	**	If not, set the MailBack flag so that it will get
76 	**	mailed back instead.
77 	*/
78 
79 	if (WriteBack)
80 	{
81 		p = ttypath();
82 		if (p == NULL || freopen(p, "w", stdout) == NULL)
83 		{
84 			MailBack++;
85 			errno = 0;
86 		}
87 		else
88 		{
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 		(void) freopen("/dev/null", "w", stdout);
117 		NoAlias++;
118 		ForceMail++;
119 
120 		/* fake up an address header for the from person */
121 		bmove((char *) &From, (char *) &to_addr, sizeof to_addr);
122 		(void) expand("$n", buf, &buf[sizeof buf - 1]);
123 		if (parse(buf, &From, -1) == NULL)
124 		{
125 			syserr("Can't parse myself!");
126 			ExitStat = EX_SOFTWARE;
127 			finis();
128 		}
129 		to_addr.q_next = NULL;
130 		i = deliver(&to_addr, errhdr);
131 		bmove((char *) &to_addr, (char *) &From, sizeof From);
132 		if (i != 0)
133 			syserr("Can't return mail to %s", p);
134 		else
135 			return;
136 	}
137 
138 	/*
139 	**  Save the message in dead.letter.
140 	**	If we weren't mailing back, and the user is local, we
141 	**	should save the message in dead.letter so that the
142 	**	poor person doesn't have to type it over again --
143 	**	and we all know what poor typists programmers are.
144 	*/
145 
146 	if (ArpaMode != ARPA_NONE)
147 		return;
148 	p = NULL;
149 	if (From.q_mailer == LocalMailer)
150 	{
151 		if (From.q_home != NULL)
152 			p = From.q_home;
153 		else if ((pw = getpwnam(From.q_user)) != NULL)
154 			p = pw->pw_dir;
155 	}
156 	if (p == NULL)
157 	{
158 		syserr("Can't return mail to %s", From.q_paddr);
159 # ifdef DEBUG
160 		p = "/usr/tmp";
161 # else
162 		p = NULL;
163 # endif
164 	}
165 	if (p != NULL && TempFile != NULL)
166 	{
167 		/* we have a home directory; open dead.letter */
168 		message(Arpa_Info, "Saving message in dead.letter");
169 		define('z', p);
170 		(void) expand("$z/dead.letter", buf, &buf[sizeof buf - 1]);
171 		To = buf;
172 		i = mailfile(buf, &From);
173 		giveresponse(i, TRUE, LocalMailer);
174 	}
175 
176 	/* add terminator to writeback message */
177 	if (WriteBack)
178 		printf("-----\r\n");
179 }
180 /*
181 **  ERRHDR -- Output the header for error mail.
182 **
183 **	This is the edit filter to error mailbacks.
184 **
185 **	Algorithm:
186 **		Output fixed header.
187 **		Output the transcript part.
188 **		Output the original message.
189 **
190 **	Parameters:
191 **		xfile -- the transcript file.
192 **		fp -- the output file.
193 **
194 **	Returns:
195 **		none
196 **
197 **	Side Effects:
198 **		input from xfile
199 **		output to fp
200 **
201 **	Called By:
202 **		deliver
203 */
204 
205 
206 errhdr(fp, m)
207 	register FILE *fp;
208 	register struct mailer *m;
209 {
210 	char buf[MAXLINE];
211 	register FILE *xfile;
212 	extern char *macvalue();
213 	char *oldfmac;
214 	char *oldgmac;
215 
216 	oldfmac = macvalue('f');
217 	define('f', "$n");
218 	oldgmac = macvalue('g');
219 	define('g', m->m_from);
220 
221 	(void) fflush(stdout);
222 	if ((xfile = fopen(Transcript, "r")) == NULL)
223 		syserr("Cannot open %s", Transcript);
224 	errno = 0;
225 
226 	/*
227 	**  Output "From" line unless supressed
228 	*/
229 
230 	if (!bitset(M_NHDR, m->m_flags))
231 	{
232 		(void) expand("$l", buf, &buf[sizeof buf - 1]);
233 		fprintf(fp, "%s\n", buf);
234 	}
235 
236 	/*
237 	**  Output header of error message.
238 	*/
239 
240 	if (bitset(M_NEEDDATE, m->m_flags))
241 	{
242 		(void) expand("$b", buf, &buf[sizeof buf - 1]);
243 		fprintf(fp, "Date: %s\n", buf);
244 	}
245 	if (bitset(M_NEEDFROM, m->m_flags))
246 	{
247 		(void) expand("$g", buf, &buf[sizeof buf - 1]);
248 		fprintf(fp, "From: %s (Mail Delivery Subsystem)\n", buf);
249 	}
250 	fprintf(fp, "To: %s\n", To);
251 	fprintf(fp, "Subject: Unable to deliver mail\n");
252 
253 	/*
254 	**  End of error message header
255 	*/
256 
257 	define('f', oldfmac);
258 	define('g', oldgmac);
259 
260 	/*
261 	**  Output transcript of errors
262 	*/
263 
264 	fprintf(fp, "\n   ----- Transcript of session follows -----\n");
265 	while (fgets(buf, sizeof buf, xfile) != NULL)
266 		fputs(buf, fp);
267 
268 	/*
269 	**  Output text of original message
270 	*/
271 
272 	if (NoReturn)
273 		fprintf(fp, "\n   ----- Return message suppressed -----\n\n");
274 	else if (TempFile != NULL)
275 	{
276 		fprintf(fp, "\n   ----- Unsent message follows -----\n");
277 		(void) fflush(fp);
278 		putmessage(fp, Mailer[1]);
279 	}
280 	else
281 		fprintf(fp, "\n  ----- No message was collected -----\n\n");
282 
283 	/*
284 	**  Cleanup and exit
285 	*/
286 
287 	(void) fclose(xfile);
288 	if (errno != 0)
289 		syserr("errhdr: I/O error");
290 }
291