1 # include <stdio.h>
2 # include <pwd.h>
3 # include "sendmail.h"
4 
5 static char	SccsId[] = "@(#)savemail.c	3.10	08/09/81";
6 
7 /*
8 **  SAVEMAIL -- Save mail on error
9 **
10 **	If the MailBack flag is set, mail it back to the originator
11 **	together with an error message; otherwise, just put it in
12 **	dead.letter in the user's home directory (if he exists on
13 **	this machine).
14 **
15 **	Parameters:
16 **		none
17 **
18 **	Returns:
19 **		none
20 **
21 **	Side Effects:
22 **		Saves the letter, by writing or mailing it back to the
23 **		sender, or by putting it in dead.letter in her home
24 **		directory.
25 **
26 **		WARNING: the user id is reset to the original user.
27 */
28 
29 savemail()
30 {
31 	register struct passwd *pw;
32 	register FILE *xfile;
33 	char buf[MAXLINE+1];
34 	extern errhdr();
35 	auto ADDRESS to_addr;
36 	extern struct passwd *getpwnam();
37 	register char *p;
38 	register int i;
39 	extern char *ttypath();
40 	static int exclusive;
41 
42 	if (exclusive++)
43 		return;
44 
45 	/*
46 	**  In the unhappy event we don't know who to return the mail
47 	**  to, make someone up.
48 	*/
49 
50 	if (From.q_paddr == NULL)
51 	{
52 		if (parse("root", &From, 0) == NULL)
53 		{
54 			syserr("Cannot parse root!");
55 			ExitStat = EX_SOFTWARE;
56 			finis();
57 		}
58 	}
59 	To = NULL;
60 
61 	/*
62 	**  If called from Eric Schmidt's network, do special mailback.
63 	**	Fundamentally, this is the mailback case except that
64 	**	it returns an OK exit status (assuming the return
65 	**	worked).
66 	*/
67 
68 	if (BerkNet)
69 	{
70 		ExitStat = EX_OK;
71 		MailBack++;
72 	}
73 
74 	/*
75 	**  If writing back, do it.
76 	**	If the user is still logged in on the same terminal,
77 	**	then write the error messages back to hir (sic).
78 	**	If not, set the MailBack flag so that it will get
79 	**	mailed back instead.
80 	*/
81 
82 	if (WriteBack)
83 	{
84 		p = ttypath();
85 		if (p == NULL || freopen(p, "w", stdout) == NULL)
86 		{
87 			MailBack++;
88 			errno = 0;
89 		}
90 		else
91 		{
92 			xfile = fopen(Transcript, "r");
93 			if (xfile == NULL)
94 				syserr("Cannot open %s", Transcript);
95 			(void) expand("$n", buf, &buf[sizeof buf - 1]);
96 			printf("\r\nMessage from %s\r\n", buf);
97 			printf("Errors occurred while sending mail, transcript follows:\r\n");
98 			while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout))
99 				fputs(buf, stdout);
100 			if (ferror(stdout))
101 				(void) syserr("savemail: stdout: write err");
102 			(void) fclose(xfile);
103 		}
104 	}
105 
106 	/*
107 	**  If mailing back, do it.
108 	**	Throw away all further output.  Don't do aliases, since
109 	**	this could cause loops, e.g., if joe mails to x:joe,
110 	**	and for some reason the network for x: is down, then
111 	**	the response gets sent to x:joe, which gives a
112 	**	response, etc.  Also force the mail to be delivered
113 	**	even if a version of it has already been sent to the
114 	**	sender.
115 	*/
116 
117 	if (MailBack)
118 	{
119 		(void) freopen("/dev/null", "w", stdout);
120 		NoAlias++;
121 		ForceMail++;
122 
123 		/* fake up an address header for the from person */
124 		bmove((char *) &From, (char *) &to_addr, sizeof to_addr);
125 		(void) expand("$n", buf, &buf[sizeof buf - 1]);
126 		if (parse(buf, &From, -1) == NULL)
127 		{
128 			syserr("Can't parse myself!");
129 			ExitStat = EX_SOFTWARE;
130 			finis();
131 		}
132 		i = deliver(&to_addr, errhdr);
133 		bmove((char *) &to_addr, (char *) &From, sizeof From);
134 		if (i != 0)
135 			syserr("Can't return mail to %s", p);
136 		else
137 			return;
138 	}
139 
140 	/*
141 	**  Save the message in dead.letter.
142 	**	If we weren't mailing back, and the user is local, we
143 	**	should save the message in dead.letter so that the
144 	**	poor person doesn't have to type it over again --
145 	**	and we all know what poor typists programmers are.
146 	*/
147 
148 	(void) setuid(getuid());
149 	(void) setgid(getgid());
150 	setpwent();
151 	p = NULL;
152 	if (From.q_mailer == M_LOCAL)
153 	{
154 		if (From.q_home != NULL)
155 			p = From.q_home;
156 		else if ((pw = getpwnam(From.q_user)) != NULL)
157 			p = pw->pw_dir;
158 	}
159 	if (p == NULL)
160 	{
161 		syserr("Can't return mail to %s", From.q_paddr);
162 # ifdef DEBUG
163 		p = "/usr/tmp";
164 # else
165 		p = NULL;
166 # endif
167 	}
168 	if (p != NULL)
169 	{
170 		/* we have a home directory; open dead.letter */
171 		message("050", "Saving message in dead.letter");
172 		define('z', p);
173 		(void) expand("$z/dead.letter", buf, &buf[sizeof buf - 1]);
174 		To = buf;
175 		i = mailfile(buf);
176 		giveresponse(i, TRUE, Mailer[M_LOCAL]);
177 	}
178 
179 	/* add terminator to writeback message */
180 	if (WriteBack)
181 		printf("-----\r\n");
182 }
183 /*
184 **  ERRHDR -- Output the header for error mail.
185 **
186 **	This is the edit filter to error mailbacks.
187 **
188 **	Algorithm:
189 **		Output fixed header.
190 **		Output the transcript part.
191 **		Output the original message.
192 **
193 **	Parameters:
194 **		xfile -- the transcript file.
195 **		fp -- the output file.
196 **
197 **	Returns:
198 **		none
199 **
200 **	Side Effects:
201 **		input from xfile
202 **		output to fp
203 **
204 **	Called By:
205 **		deliver
206 */
207 
208 
209 errhdr(fp)
210 	register FILE *fp;
211 {
212 	char buf[MAXLINE];
213 	register FILE *xfile;
214 
215 	(void) fflush(stdout);
216 	if ((xfile = fopen(Transcript, "r")) == NULL)
217 		syserr("Cannot open %s", Transcript);
218 	errno = 0;
219 	fprintf(fp, "To: %s\n", To);
220 	fprintf(fp, "Subject: Unable to deliver mail\n");
221 	fprintf(fp, "\n   ----- Transcript of session follows -----\n");
222 	while (fgets(buf, sizeof buf, xfile) != NULL)
223 		fputs(buf, fp);
224 	fprintf(fp, "\n   ----- Unsent message follows -----\n");
225 	(void) fflush(fp);
226 	putmessage(fp, Mailer[1]);
227 	(void) fclose(xfile);
228 	if (errno != 0)
229 		syserr("errhdr: I/O error");
230 }
231