undo RESPONSE hack -- is it worthwhile?
[unix-history] / usr / src / usr.sbin / sendmail / src / savemail.c
CommitLineData
b2a81223 1/*
dc45ba8c 2 * Copyright (c) 1983 Eric P. Allman
bee79b64
KB
3 * Copyright (c) 1988 Regents of the University of California.
4 * All rights reserved.
5 *
417f7a11 6 * %sccs.include.redist.c%
bee79b64 7 */
b2a81223
DF
8
9#ifndef lint
9e2cf26f 10static char sccsid[] = "@(#)savemail.c 6.18 (Berkeley) %G%";
bee79b64 11#endif /* not lint */
b2a81223 12
b3cbe40f 13# include <pwd.h>
96faada8 14# include "sendmail.h"
b3cbe40f
EA
15
16/*
17** SAVEMAIL -- Save mail on error
18**
7338e3d4 19** If mailing back errors, mail it back to the originator
b3cbe40f
EA
20** together with an error message; otherwise, just put it in
21** dead.letter in the user's home directory (if he exists on
22** this machine).
23**
24** Parameters:
e6f08ab1 25** e -- the envelope containing the message in error.
b3cbe40f
EA
26**
27** Returns:
28** none
29**
30** Side Effects:
31** Saves the letter, by writing or mailing it back to the
32** sender, or by putting it in dead.letter in her home
33** directory.
b3cbe40f
EA
34*/
35
2e3062fe
EA
36/* defines for state machine */
37# define ESM_REPORT 0 /* report to sender's terminal */
38# define ESM_MAIL 1 /* mail back to sender */
39# define ESM_QUIET 2 /* messages have already been returned */
40# define ESM_DEADLETTER 3 /* save in ~/dead.letter */
41# define ESM_POSTMASTER 4 /* return to postmaster */
42# define ESM_USRTMP 5 /* save in /usr/tmp/dead.letter */
43# define ESM_PANIC 6 /* leave the locked queue/transcript files */
44# define ESM_DONE 7 /* the message is successfully delivered */
45
46
e6f08ab1
EA
47savemail(e)
48 register ENVELOPE *e;
b3cbe40f
EA
49{
50 register struct passwd *pw;
2e3062fe
EA
51 register FILE *fp;
52 int state;
53 auto ADDRESS *q;
b3cbe40f 54 char buf[MAXLINE+1];
b3cbe40f
EA
55 extern struct passwd *getpwnam();
56 register char *p;
b3cbe40f 57 extern char *ttypath();
40e27d12 58 typedef int (*fnptr)();
b3cbe40f 59
9678c96d 60 if (tTd(6, 1))
f4b05990 61 printf("\nsavemail, ErrorMode = %c\n", ErrorMode);
ae945718 62
7338e3d4 63 if (bitset(EF_RESPONSE, e->e_flags))
b3cbe40f 64 return;
e6f08ab1 65 e->e_flags &= ~EF_FATALERRS;
b3cbe40f
EA
66
67 /*
68 ** In the unhappy event we don't know who to return the mail
69 ** to, make someone up.
70 */
71
1bcdf0a2 72 if (CurEnv->e_returnto == NULL)
b3cbe40f 73 {
1bcdf0a2
EA
74 CurEnv->e_returnto = parse("root", (ADDRESS *) NULL, 0);
75 if (CurEnv->e_returnto == NULL)
b3cbe40f 76 {
b6edea3d 77 syserr("553 Cannot parse root!");
b3cbe40f
EA
78 ExitStat = EX_SOFTWARE;
79 finis();
80 }
81 }
e6f08ab1 82 e->e_to = NULL;
b3cbe40f
EA
83
84 /*
2e3062fe
EA
85 ** Basic state machine.
86 **
87 ** This machine runs through the following states:
88 **
89 ** ESM_QUIET Errors have already been printed iff the
90 ** sender is local.
91 ** ESM_REPORT Report directly to the sender's terminal.
92 ** ESM_MAIL Mail response to the sender.
93 ** ESM_DEADLETTER Save response in ~/dead.letter.
94 ** ESM_POSTMASTER Mail response to the postmaster.
95 ** ESM_PANIC Save response anywhere possible.
b3cbe40f
EA
96 */
97
2e3062fe
EA
98 /* determine starting state */
99 switch (ErrorMode)
b3cbe40f 100 {
2e3062fe
EA
101 case EM_WRITE:
102 state = ESM_REPORT;
103 break;
104
105 case EM_BERKNET:
106 /* mail back, but return o.k. exit status */
3b661a43 107 ExitStat = EX_OK;
b3cbe40f 108
2e3062fe
EA
109 /* fall through.... */
110
111 case EM_MAIL:
112 state = ESM_MAIL;
113 break;
114
115 case EM_PRINT:
f4b05990 116 case '\0':
2e3062fe
EA
117 state = ESM_QUIET;
118 break;
b3cbe40f 119
2e3062fe
EA
120 case EM_QUIET:
121 /* no need to return anything at all */
122 return;
f4b05990
EA
123
124 default:
b6edea3d 125 syserr("554 savemail: ErrorMode x%x\n");
f4b05990
EA
126 state = ESM_MAIL;
127 break;
2e3062fe
EA
128 }
129
130 while (state != ESM_DONE)
b3cbe40f 131 {
f4b05990
EA
132 if (tTd(6, 5))
133 printf(" state %d\n", state);
f4b05990 134
2e3062fe 135 switch (state)
b3cbe40f 136 {
f4b05990
EA
137 case ESM_QUIET:
138 if (e->e_from.q_mailer == LocalMailer)
139 state = ESM_DEADLETTER;
140 else
141 state = ESM_MAIL;
142 break;
143
2e3062fe
EA
144 case ESM_REPORT:
145
146 /*
147 ** If the user is still logged in on the same terminal,
148 ** then write the error messages back to hir (sic).
149 */
150
151 p = ttypath();
152 if (p == NULL || freopen(p, "w", stdout) == NULL)
153 {
154 state = ESM_MAIL;
155 break;
156 }
157
2bee003d 158 expand("\201n", buf, &buf[sizeof buf - 1], e);
7338e3d4
EA
159 printf("\r\nMessage from %s...\r\n", buf);
160 printf("Errors occurred while sending mail.\r\n");
912acb74 161 if (e->e_xfp != NULL)
7338e3d4 162 {
912acb74 163 (void) fflush(e->e_xfp);
2e3062fe 164 fp = fopen(queuename(e, 'x'), "r");
7338e3d4
EA
165 }
166 else
2e3062fe
EA
167 fp = NULL;
168 if (fp == NULL)
7338e3d4 169 {
e6f08ab1 170 syserr("Cannot open %s", queuename(e, 'x'));
7338e3d4
EA
171 printf("Transcript of session is unavailable.\r\n");
172 }
173 else
174 {
175 printf("Transcript follows:\r\n");
2e3062fe 176 while (fgets(buf, sizeof buf, fp) != NULL &&
7338e3d4
EA
177 !ferror(stdout))
178 fputs(buf, stdout);
2e3062fe 179 (void) fclose(fp);
7338e3d4 180 }
2e3062fe 181 printf("Original message will be saved in dead.letter.\r\n");
2e3062fe
EA
182 state = ESM_DEADLETTER;
183 break;
b3cbe40f 184
2e3062fe
EA
185 case ESM_MAIL:
186 case ESM_POSTMASTER:
187 /*
188 ** If mailing back, do it.
189 ** Throw away all further output. Don't alias,
190 ** since this could cause loops, e.g., if joe
191 ** mails to joe@x, and for some reason the network
192 ** for @x is down, then the response gets sent to
193 ** joe@x, which gives a response, etc. Also force
194 ** the mail to be delivered even if a version of
195 ** it has already been sent to the sender.
4fbe2304
EA
196 **
197 ** Clever technique for computing rpath from
198 ** Eric Wassenaar <e07@nikhef.nl>.
2e3062fe 199 */
b3cbe40f 200
2e3062fe
EA
201 if (state == ESM_MAIL)
202 {
bb9f418b
EA
203 char *rpath;
204
205 if (e->e_returnpath != e->e_sender)
206 rpath = e->e_returnpath;
207 else
208 rpath = e->e_from.q_paddr;
209 if (strcmp(rpath, "<>") != 0)
210 (void) sendtolist(rpath,
211 (ADDRESS *) NULL,
212 &e->e_errorqueue, e);
a0225d08
EA
213
214 /* deliver a cc: to the postmaster if desired */
215 if (PostMasterCopy != NULL)
ab0b3f7d
EA
216 {
217 auto ADDRESS *rlist = NULL;
218
1c7897ef
EA
219 (void) sendtolist(PostMasterCopy,
220 (ADDRESS *) NULL,
ab0b3f7d
EA
221 &rlist, e);
222 (void) returntosender(e->e_message,
223 rlist, FALSE, e);
224 }
2e3062fe 225 q = e->e_errorqueue;
2667d4e4
EA
226 if (q == NULL)
227 {
228 /* this is an error-error */
229 state = ESM_USRTMP;
230 break;
231 }
2e3062fe
EA
232 }
233 else
234 {
9e2cf26f 235 if (parseaddr("postmaster", q, 0, '\0', NULL, e) == NULL)
2e3062fe 236 {
b6edea3d 237 syserr("553 cannot parse postmaster!");
2e3062fe
EA
238 ExitStat = EX_SOFTWARE;
239 state = ESM_USRTMP;
240 break;
241 }
242 }
243 if (returntosender(e->e_message != NULL ? e->e_message :
244 "Unable to deliver mail",
abccefc9 245 q, (e->e_class >= 0), e) == 0)
2e3062fe
EA
246 {
247 state = ESM_DONE;
248 break;
249 }
b3cbe40f 250
2e3062fe
EA
251 state = state == ESM_MAIL ? ESM_POSTMASTER : ESM_USRTMP;
252 break;
b3cbe40f 253
2e3062fe
EA
254 case ESM_DEADLETTER:
255 /*
256 ** Save the message in dead.letter.
257 ** If we weren't mailing back, and the user is
258 ** local, we should save the message in
259 ** ~/dead.letter so that the poor person doesn't
260 ** have to type it over again -- and we all know
261 ** what poor typists UNIX users are.
262 */
b3cbe40f 263
2e3062fe
EA
264 p = NULL;
265 if (e->e_from.q_mailer == LocalMailer)
266 {
267 if (e->e_from.q_home != NULL)
268 p = e->e_from.q_home;
269 else if ((pw = getpwnam(e->e_from.q_user)) != NULL)
270 p = pw->pw_dir;
271 }
272 if (p == NULL)
273 {
b6edea3d 274 syserr("554 Can't return mail to %s", e->e_from.q_paddr);
2e3062fe
EA
275 state = ESM_MAIL;
276 break;
277 }
278 if (e->e_dfp != NULL)
279 {
280 auto ADDRESS *q;
281 bool oldverb = Verbose;
282
283 /* we have a home directory; open dead.letter */
284 define('z', p, e);
2bee003d 285 expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e);
2e3062fe 286 Verbose = TRUE;
b6edea3d 287 message("Saving message in %s", buf);
2e3062fe
EA
288 Verbose = oldverb;
289 e->e_to = buf;
290 q = NULL;
1c7897ef 291 (void) sendtolist(buf, &e->e_from, &q, e);
2e3062fe
EA
292 if (deliver(e, q) == 0)
293 state = ESM_DONE;
294 else
295 state = ESM_MAIL;
296 }
577bce94
EA
297 else
298 {
299 /* no data file -- try mailing back */
300 state = ESM_MAIL;
301 }
2e3062fe
EA
302 break;
303
304 case ESM_USRTMP:
305 /*
306 ** Log the mail in /usr/tmp/dead.letter.
307 */
308
abccefc9
EA
309 if (e->e_class < 0)
310 {
311 state = ESM_DONE;
312 break;
313 }
314
2e3062fe
EA
315 fp = dfopen("/usr/tmp/dead.letter", "a");
316 if (fp == NULL)
317 {
318 state = ESM_PANIC;
319 break;
320 }
321
49a282c6
EA
322 putfromline(fp, FileMailer, e);
323 (*e->e_puthdr)(fp, FileMailer, e);
324 putline("\n", fp, FileMailer);
325 (*e->e_putbody)(fp, FileMailer, e);
326 putline("\n", fp, FileMailer);
2e3062fe
EA
327 (void) fflush(fp);
328 state = ferror(fp) ? ESM_PANIC : ESM_DONE;
329 (void) fclose(fp);
330 break;
331
332 default:
b6edea3d 333 syserr("554 savemail: unknown state %d", state);
2e3062fe
EA
334
335 /* fall through ... */
336
337 case ESM_PANIC:
2e3062fe 338 /* leave the locked queue & transcript files around */
b6edea3d 339 syserr("554 savemail: cannot save rejected email anywhere");
2e3062fe
EA
340 exit(EX_SOFTWARE);
341 }
342 }
b3cbe40f
EA
343}
344\f/*
aba51985
EA
345** RETURNTOSENDER -- return a message to the sender with an error.
346**
347** Parameters:
348** msg -- the explanatory message.
7a079701 349** returnq -- the queue of people to send the message to.
79bd7c07
EA
350** sendbody -- if TRUE, also send back the body of the
351** message; otherwise just send the header.
a4076aed 352** e -- the current envelope.
aba51985
EA
353**
354** Returns:
355** zero -- if everything went ok.
356** else -- some error.
357**
358** Side Effects:
359** Returns the current message to the sender via
360** mail.
361*/
362
79bd7c07 363static bool SendBody;
aba51985 364
3c7fe765
EA
365#define MAXRETURNS 6 /* max depth of returning messages */
366
a4076aed 367returntosender(msg, returnq, sendbody, e)
aba51985 368 char *msg;
7a079701 369 ADDRESS *returnq;
79bd7c07 370 bool sendbody;
a4076aed 371 register ENVELOPE *e;
aba51985 372{
aba51985 373 char buf[MAXNAME];
dd1fe05b
EA
374 extern putheader(), errbody();
375 register ENVELOPE *ee;
376 extern ENVELOPE *newenvelope();
377 ENVELOPE errenvelope;
3c7fe765 378 static int returndepth;
7338e3d4 379 register ADDRESS *q;
3c7fe765 380
9678c96d 381 if (tTd(6, 1))
e4d966b5 382 {
a4076aed
EA
383 printf("Return To Sender: msg=\"%s\", depth=%d, e=%x,\n",
384 msg, returndepth, e);
2e3062fe 385 printf("\treturnq=");
7a079701 386 printaddr(returnq, TRUE);
e4d966b5 387 }
e4d966b5 388
3c7fe765
EA
389 if (++returndepth >= MAXRETURNS)
390 {
391 if (returndepth != MAXRETURNS)
b6edea3d 392 syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr);
3c7fe765
EA
393 /* don't "unrecurse" and fake a clean exit */
394 /* returndepth--; */
395 return (0);
396 }
aba51985 397
79bd7c07 398 SendBody = sendbody;
fda58daa
EA
399 define('g', e->e_sender, e);
400 define('<', e->e_returnpath, e);
401 ee = newenvelope(&errenvelope, e);
2bee003d 402 define('a', "\201b", ee);
dd1fe05b
EA
403 ee->e_puthdr = putheader;
404 ee->e_putbody = errbody;
7338e3d4 405 ee->e_flags |= EF_RESPONSE;
a4076aed 406 if (!bitset(EF_OLDSTYLE, e->e_flags))
839ae7fa 407 ee->e_flags &= ~EF_OLDSTYLE;
7a079701 408 ee->e_sendqueue = returnq;
912acb74 409 openxscript(ee);
7a079701 410 for (q = returnq; q != NULL; q = q->q_next)
7338e3d4 411 {
63d473ff 412 if (!DontPruneRoutes && pruneroute(q->q_paddr))
9e2cf26f 413 parseaddr(q->q_paddr, q, 0, '\0', NULL, e);
63d473ff 414
7338e3d4
EA
415 if (q->q_alias == NULL)
416 addheader("to", q->q_paddr, ee);
417 }
2e3062fe 418
6e99f903 419# ifdef LOG
68f7099c 420 if (LogLevel > 5)
6e99f903
EA
421 syslog(LOG_INFO, "%s: %s: return to sender: %s",
422 e->e_id, ee->e_id, msg);
423# endif
424
e7d41cfe 425 (void) sprintf(buf, "Returned mail: %s", msg);
baa0c390 426 addheader("subject", buf, ee);
aba51985
EA
427
428 /* fake up an address header for the from person */
2bee003d 429 expand("\201n", buf, &buf[sizeof buf - 1], e);
2667d4e4 430 ee->e_sender = newstr(buf);
a5bf555b
EA
431 if (ConfigLevel >= 4)
432 ee->e_returnpath = "<>";
433 else
434 ee->e_returnpath = ee->e_sender;
9e2cf26f 435 if (parseaddr(buf, &ee->e_from, -1, '\0', NULL, e) == NULL)
aba51985 436 {
b6edea3d 437 syserr("553 Can't parse myself!");
aba51985 438 ExitStat = EX_SOFTWARE;
3c7fe765 439 returndepth--;
aba51985
EA
440 return (-1);
441 }
0fe3917f 442 loweraddr(&ee->e_from);
79bd7c07 443
dd1fe05b
EA
444 /* push state into submessage */
445 CurEnv = ee;
2bee003d 446 define('f', "\201n", ee);
7338e3d4 447 define('x', "Mail Delivery Subsystem", ee);
6e99f903 448 eatheader(ee, FALSE);
dd1fe05b
EA
449
450 /* actually deliver the error message */
f7e74083 451 sendall(ee, SM_DEFAULT);
dd1fe05b 452
dd1fe05b 453 /* restore state */
2cce0c26 454 dropenvelope(ee);
dd1fe05b 455 CurEnv = CurEnv->e_parent;
3c7fe765 456 returndepth--;
79bd7c07 457
3c7fe765 458 /* should check for delivery errors here */
aba51985
EA
459 return (0);
460}
461\f/*
dd1fe05b 462** ERRBODY -- output the body of an error message.
b3cbe40f 463**
dd1fe05b
EA
464** Typically this is a copy of the transcript plus a copy of the
465** original offending message.
b3cbe40f 466**
b3cbe40f 467** Parameters:
b3cbe40f 468** fp -- the output file.
4db45bf1 469** m -- the mailer to output to.
912acb74 470** e -- the envelope we are working in.
b3cbe40f
EA
471**
472** Returns:
473** none
474**
475** Side Effects:
dd1fe05b 476** Outputs the body of an error message.
b3cbe40f
EA
477*/
478
4db45bf1 479errbody(fp, m, e)
b3cbe40f 480 register FILE *fp;
74c5fe7c 481 register struct mailer *m;
912acb74 482 register ENVELOPE *e;
b3cbe40f 483{
9e3c0a28 484 register FILE *xfile;
dd1fe05b 485 char buf[MAXLINE];
e6f08ab1 486 char *p;
b3cbe40f 487
44967af8
EA
488 /*
489 ** Output error message header (if specified and available).
490 */
491
492 if (ErrMsgFile != NULL)
493 {
494 if (*ErrMsgFile == '/')
495 {
496 xfile = fopen(ErrMsgFile, "r");
497 if (xfile != NULL)
498 {
499 while (fgets(buf, sizeof buf, xfile) != NULL)
5d99721e
EA
500 {
501 expand(buf, buf, &buf[sizeof buf - 1], e);
44967af8 502 putline(buf, fp, m);
5d99721e 503 }
44967af8
EA
504 (void) fclose(xfile);
505 fprintf(fp, "\n");
506 }
507 }
508 else
509 {
5d99721e
EA
510 expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e);
511 putline(buf, fp, m);
44967af8
EA
512 fprintf(fp, "\n");
513 }
514 }
515
74c5fe7c
EA
516 /*
517 ** Output transcript of errors
518 */
519
be2fcca9 520 (void) fflush(stdout);
912acb74 521 p = queuename(e->e_parent, 'x');
e6f08ab1 522 if ((xfile = fopen(p, "r")) == NULL)
be2fcca9 523 {
e6f08ab1 524 syserr("Cannot open %s", p);
be2fcca9
EA
525 fprintf(fp, " ----- Transcript of session is unavailable -----\n");
526 }
527 else
528 {
529 fprintf(fp, " ----- Transcript of session follows -----\n");
912acb74
EA
530 if (e->e_xfp != NULL)
531 (void) fflush(e->e_xfp);
be2fcca9 532 while (fgets(buf, sizeof buf, xfile) != NULL)
4db45bf1 533 putline(buf, fp, m);
be2fcca9
EA
534 (void) fclose(xfile);
535 }
536 errno = 0;
74c5fe7c
EA
537
538 /*
539 ** Output text of original message
540 */
541
46b14b48
EA
542 if (NoReturn)
543 fprintf(fp, "\n ----- Return message suppressed -----\n\n");
912acb74 544 else if (e->e_parent->e_dfp != NULL)
738043e8 545 {
79bd7c07
EA
546 if (SendBody)
547 {
4db45bf1
EA
548 putline("\n", fp, m);
549 putline(" ----- Unsent message follows -----\n", fp, m);
79bd7c07 550 (void) fflush(fp);
4db45bf1
EA
551 putheader(fp, m, e->e_parent);
552 putline("\n", fp, m);
553 putbody(fp, m, e->e_parent);
79bd7c07
EA
554 }
555 else
556 {
4db45bf1
EA
557 putline("\n", fp, m);
558 putline(" ----- Message header follows -----\n", fp, m);
79bd7c07 559 (void) fflush(fp);
4db45bf1 560 putheader(fp, m, e->e_parent);
79bd7c07 561 }
738043e8
EA
562 }
563 else
4db45bf1
EA
564 {
565 putline("\n", fp, m);
566 putline(" ----- No message was collected -----\n", fp, m);
567 putline("\n", fp, m);
568 }
74c5fe7c
EA
569
570 /*
571 ** Cleanup and exit
572 */
573
b3cbe40f 574 if (errno != 0)
dd1fe05b 575 syserr("errbody: I/O error");
b3cbe40f 576}
63d473ff
EA
577\f/*
578** PRUNEROUTE -- prune an RFC-822 source route
579**
580** Trims down a source route to the last internet-registered hop.
581** This is encouraged by RFC 1123 section 5.3.3.
582**
583** Parameters:
584** addr -- the address
585**
586** Returns:
587** TRUE -- address was modified
588** FALSE -- address could not be pruned
589**
590** Side Effects:
591** modifies addr in-place
592*/
593
594pruneroute(addr)
595 char *addr;
596{
597#ifdef NAMED_BIND
598 char *start, *at, *comma;
599 char c;
600 int rcode;
601 char hostbuf[BUFSIZ];
602 char *mxhosts[MAXMXHOSTS + 1];
603
604 /* check to see if this is really a route-addr */
605 if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>')
606 return FALSE;
607 start = strchr(addr, ':');
608 at = strrchr(addr, '@');
609 if (start == NULL || at == NULL || at < start)
610 return FALSE;
611
612 /* slice off the angle brackets */
613 strcpy(hostbuf, at + 1);
614 hostbuf[strlen(hostbuf) - 1] = '\0';
615
616 while (start)
617 {
618 if (getmxrr(hostbuf, mxhosts, "", &rcode) > 0)
619 {
620 strcpy(addr + 1, start + 1);
621 return TRUE;
622 }
623 c = *start;
624 *start = '\0';
625 comma = strrchr(addr, ',');
626 if (comma && comma[1] == '@')
627 strcpy(hostbuf, comma + 2);
628 else
629 comma = 0;
630 *start = c;
631 start = comma;
632 }
633#endif
634 return FALSE;
635}