1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 #ifndef lint 10 static char sccsid[] = "@(#)savemail.c 8.15 (Berkeley) 10/17/93"; 11 #endif /* not lint */ 12 13 # include "sendmail.h" 14 # include <pwd.h> 15 16 /* 17 ** SAVEMAIL -- Save mail on error 18 ** 19 ** If mailing back errors, mail it back to the originator 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: 25 ** e -- the envelope containing the message in error. 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. 34 */ 35 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 47 savemail(e) 48 register ENVELOPE *e; 49 { 50 register struct passwd *pw; 51 register FILE *fp; 52 int state; 53 auto ADDRESS *q = NULL; 54 char buf[MAXLINE+1]; 55 extern struct passwd *getpwnam(); 56 register char *p; 57 extern char *ttypath(); 58 typedef int (*fnptr)(); 59 60 if (tTd(6, 1)) 61 { 62 printf("\nsavemail, errormode = %c, id = %s\n e_from=", 63 e->e_errormode, e->e_id == NULL ? "NONE" : e->e_id); 64 printaddr(&e->e_from, FALSE); 65 } 66 67 if (e->e_id == NULL) 68 { 69 /* can't return a message with no id */ 70 return; 71 } 72 73 /* 74 ** In the unhappy event we don't know who to return the mail 75 ** to, make someone up. 76 */ 77 78 if (e->e_from.q_paddr == NULL) 79 { 80 e->e_sender = "Postmaster"; 81 if (parseaddr(e->e_sender, &e->e_from, 82 RF_COPYPARSE|RF_SENDERADDR, '\0', NULL, e) == NULL) 83 { 84 syserr("553 Cannot parse Postmaster!"); 85 ExitStat = EX_SOFTWARE; 86 finis(); 87 } 88 } 89 e->e_to = NULL; 90 91 /* 92 ** Basic state machine. 93 ** 94 ** This machine runs through the following states: 95 ** 96 ** ESM_QUIET Errors have already been printed iff the 97 ** sender is local. 98 ** ESM_REPORT Report directly to the sender's terminal. 99 ** ESM_MAIL Mail response to the sender. 100 ** ESM_DEADLETTER Save response in ~/dead.letter. 101 ** ESM_POSTMASTER Mail response to the postmaster. 102 ** ESM_PANIC Save response anywhere possible. 103 */ 104 105 /* determine starting state */ 106 switch (e->e_errormode) 107 { 108 case EM_WRITE: 109 state = ESM_REPORT; 110 break; 111 112 case EM_BERKNET: 113 /* mail back, but return o.k. exit status */ 114 ExitStat = EX_OK; 115 116 /* fall through.... */ 117 118 case EM_MAIL: 119 state = ESM_MAIL; 120 break; 121 122 case EM_PRINT: 123 case '\0': 124 state = ESM_QUIET; 125 break; 126 127 case EM_QUIET: 128 /* no need to return anything at all */ 129 return; 130 131 default: 132 syserr("554 savemail: bogus errormode x%x\n", e->e_errormode); 133 state = ESM_MAIL; 134 break; 135 } 136 137 /* if this is already an error response, send to postmaster */ 138 if (bitset(EF_RESPONSE, e->e_flags)) 139 { 140 if (e->e_parent != NULL && 141 bitset(EF_RESPONSE, e->e_parent->e_flags)) 142 { 143 /* got an error sending a response -- can it */ 144 return; 145 } 146 state = ESM_POSTMASTER; 147 } 148 149 while (state != ESM_DONE) 150 { 151 if (tTd(6, 5)) 152 printf(" state %d\n", state); 153 154 switch (state) 155 { 156 case ESM_QUIET: 157 if (e->e_from.q_mailer == LocalMailer) 158 state = ESM_DEADLETTER; 159 else 160 state = ESM_MAIL; 161 break; 162 163 case ESM_REPORT: 164 165 /* 166 ** If the user is still logged in on the same terminal, 167 ** then write the error messages back to hir (sic). 168 */ 169 170 p = ttypath(); 171 if (p == NULL || freopen(p, "w", stdout) == NULL) 172 { 173 state = ESM_MAIL; 174 break; 175 } 176 177 expand("\201n", buf, &buf[sizeof buf - 1], e); 178 printf("\r\nMessage from %s...\r\n", buf); 179 printf("Errors occurred while sending mail.\r\n"); 180 if (e->e_xfp != NULL) 181 { 182 (void) fflush(e->e_xfp); 183 fp = fopen(queuename(e, 'x'), "r"); 184 } 185 else 186 fp = NULL; 187 if (fp == NULL) 188 { 189 syserr("Cannot open %s", queuename(e, 'x')); 190 printf("Transcript of session is unavailable.\r\n"); 191 } 192 else 193 { 194 printf("Transcript follows:\r\n"); 195 while (fgets(buf, sizeof buf, fp) != NULL && 196 !ferror(stdout)) 197 fputs(buf, stdout); 198 (void) xfclose(fp, "savemail transcript", e->e_id); 199 } 200 printf("Original message will be saved in dead.letter.\r\n"); 201 state = ESM_DEADLETTER; 202 break; 203 204 case ESM_MAIL: 205 /* 206 ** If mailing back, do it. 207 ** Throw away all further output. Don't alias, 208 ** since this could cause loops, e.g., if joe 209 ** mails to joe@x, and for some reason the network 210 ** for @x is down, then the response gets sent to 211 ** joe@x, which gives a response, etc. Also force 212 ** the mail to be delivered even if a version of 213 ** it has already been sent to the sender. 214 ** 215 ** If this is a configuration or local software 216 ** error, send to the local postmaster as well, 217 ** since the originator can't do anything 218 ** about it anyway. Note that this is a full 219 ** copy of the message (intentionally) so that 220 ** the Postmaster can forward things along. 221 */ 222 223 if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE) 224 { 225 (void) sendtolist("postmaster", 226 NULLADDR, &e->e_errorqueue, e); 227 } 228 if (strcmp(e->e_from.q_paddr, "<>") != 0) 229 { 230 (void) sendtolist(e->e_from.q_paddr, 231 NULLADDR, &e->e_errorqueue, e); 232 } 233 234 /* 235 ** Deliver a non-delivery report to the 236 ** Postmaster-designate (not necessarily 237 ** Postmaster). This does not include the 238 ** body of the message, for privacy reasons. 239 ** You really shouldn't need this. 240 */ 241 242 e->e_flags |= EF_PM_NOTIFY; 243 244 q = e->e_errorqueue; 245 if (q == NULL) 246 { 247 /* this is an error-error */ 248 state = ESM_POSTMASTER; 249 break; 250 } 251 if (returntosender(e->e_message, 252 q, (e->e_class >= 0), e) == 0) 253 { 254 state = ESM_DONE; 255 break; 256 } 257 258 /* didn't work -- return to postmaster */ 259 state = ESM_POSTMASTER; 260 break; 261 262 case ESM_POSTMASTER: 263 /* 264 ** Similar to previous case, but to system postmaster. 265 */ 266 267 q = NULL; 268 if (sendtolist("postmaster", NULL, &q, e) <= 0) 269 { 270 syserr("553 cannot parse postmaster!"); 271 ExitStat = EX_SOFTWARE; 272 state = ESM_USRTMP; 273 break; 274 } 275 if (returntosender(e->e_message, 276 q, (e->e_class >= 0), e) == 0) 277 { 278 state = ESM_DONE; 279 break; 280 } 281 282 /* didn't work -- last resort */ 283 state = ESM_USRTMP; 284 break; 285 286 case ESM_DEADLETTER: 287 /* 288 ** Save the message in dead.letter. 289 ** If we weren't mailing back, and the user is 290 ** local, we should save the message in 291 ** ~/dead.letter so that the poor person doesn't 292 ** have to type it over again -- and we all know 293 ** what poor typists UNIX users are. 294 */ 295 296 p = NULL; 297 if (e->e_from.q_mailer == LocalMailer) 298 { 299 if (e->e_from.q_home != NULL) 300 p = e->e_from.q_home; 301 else if ((pw = getpwnam(e->e_from.q_user)) != NULL) 302 p = pw->pw_dir; 303 } 304 if (p == NULL) 305 { 306 /* no local directory */ 307 state = ESM_MAIL; 308 break; 309 } 310 if (e->e_dfp != NULL) 311 { 312 bool oldverb = Verbose; 313 314 /* we have a home directory; open dead.letter */ 315 define('z', p, e); 316 expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e); 317 Verbose = TRUE; 318 message("Saving message in %s", buf); 319 Verbose = oldverb; 320 e->e_to = buf; 321 q = NULL; 322 (void) sendtolist(buf, &e->e_from, &q, e); 323 if (q != NULL && 324 !bitset(QBADADDR, q->q_flags) && 325 deliver(e, q) == 0) 326 state = ESM_DONE; 327 else 328 state = ESM_MAIL; 329 } 330 else 331 { 332 /* no data file -- try mailing back */ 333 state = ESM_MAIL; 334 } 335 break; 336 337 case ESM_USRTMP: 338 /* 339 ** Log the mail in /usr/tmp/dead.letter. 340 */ 341 342 if (e->e_class < 0) 343 { 344 state = ESM_DONE; 345 break; 346 } 347 348 fp = dfopen("/usr/tmp/dead.letter", 349 O_WRONLY|O_CREAT|O_APPEND, FileMode); 350 if (fp == NULL) 351 { 352 state = ESM_PANIC; 353 break; 354 } 355 356 putfromline(fp, FileMailer, e); 357 (*e->e_puthdr)(fp, FileMailer, e); 358 putline("\n", fp, FileMailer); 359 (*e->e_putbody)(fp, FileMailer, e, NULL); 360 putline("\n", fp, FileMailer); 361 (void) fflush(fp); 362 state = ferror(fp) ? ESM_PANIC : ESM_DONE; 363 (void) xfclose(fp, "savemail", "/usr/tmp/dead.letter"); 364 break; 365 366 default: 367 syserr("554 savemail: unknown state %d", state); 368 369 /* fall through ... */ 370 371 case ESM_PANIC: 372 /* leave the locked queue & transcript files around */ 373 syserr("554 savemail: cannot save rejected email anywhere"); 374 exit(EX_SOFTWARE); 375 } 376 } 377 } 378 /* 379 ** RETURNTOSENDER -- return a message to the sender with an error. 380 ** 381 ** Parameters: 382 ** msg -- the explanatory message. 383 ** returnq -- the queue of people to send the message to. 384 ** sendbody -- if TRUE, also send back the body of the 385 ** message; otherwise just send the header. 386 ** e -- the current envelope. 387 ** 388 ** Returns: 389 ** zero -- if everything went ok. 390 ** else -- some error. 391 ** 392 ** Side Effects: 393 ** Returns the current message to the sender via 394 ** mail. 395 */ 396 397 static bool SendBody; 398 399 #define MAXRETURNS 6 /* max depth of returning messages */ 400 #define ERRORFUDGE 100 /* nominal size of error message text */ 401 402 returntosender(msg, returnq, sendbody, e) 403 char *msg; 404 ADDRESS *returnq; 405 bool sendbody; 406 register ENVELOPE *e; 407 { 408 char buf[MAXNAME]; 409 extern putheader(), errbody(); 410 register ENVELOPE *ee; 411 ENVELOPE *oldcur = CurEnv; 412 ENVELOPE errenvelope; 413 static int returndepth; 414 register ADDRESS *q; 415 416 if (returnq == NULL) 417 return (-1); 418 419 if (msg == NULL) 420 msg = "Unable to deliver mail"; 421 422 if (tTd(6, 1)) 423 { 424 printf("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=", 425 msg, returndepth, e); 426 printaddr(returnq, TRUE); 427 } 428 429 if (++returndepth >= MAXRETURNS) 430 { 431 if (returndepth != MAXRETURNS) 432 syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr); 433 /* don't "unrecurse" and fake a clean exit */ 434 /* returndepth--; */ 435 return (0); 436 } 437 438 SendBody = sendbody; 439 define('g', e->e_from.q_paddr, e); 440 ee = newenvelope(&errenvelope, e); 441 define('a', "\201b", ee); 442 define('r', "internal", ee); 443 define('s', "localhost", ee); 444 define('_', "localhost", ee); 445 ee->e_puthdr = putheader; 446 ee->e_putbody = errbody; 447 ee->e_flags |= EF_RESPONSE|EF_METOO; 448 if (!bitset(EF_OLDSTYLE, e->e_flags)) 449 ee->e_flags &= ~EF_OLDSTYLE; 450 ee->e_sendqueue = returnq; 451 ee->e_msgsize = ERRORFUDGE; 452 if (!NoReturn) 453 ee->e_msgsize += e->e_msgsize; 454 openxscript(ee); 455 for (q = returnq; q != NULL; q = q->q_next) 456 { 457 if (bitset(QBADADDR, q->q_flags)) 458 continue; 459 460 if (!bitset(QDONTSEND, q->q_flags)) 461 ee->e_nrcpts++; 462 463 if (!DontPruneRoutes && pruneroute(q->q_paddr)) 464 parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e); 465 466 if (q->q_alias == NULL) 467 addheader("To", q->q_paddr, ee); 468 } 469 470 # ifdef LOG 471 if (LogLevel > 5) 472 syslog(LOG_INFO, "%s: %s: return to sender: %s", 473 e->e_id, ee->e_id, msg); 474 # endif 475 476 (void) sprintf(buf, "Returned mail: %s", msg); 477 addheader("Subject", buf, ee); 478 if (SendMIMEErrors) 479 { 480 addheader("MIME-Version", "1.0", ee); 481 (void) sprintf(buf, "%s.%ld/%s", 482 ee->e_id, curtime(), MyHostName); 483 ee->e_msgboundary = newstr(buf); 484 (void) sprintf(buf, "multipart/mixed; boundary=\"%s\"", 485 ee->e_msgboundary); 486 addheader("Content-Type", buf, ee); 487 } 488 489 /* fake up an address header for the from person */ 490 expand("\201n", buf, &buf[sizeof buf - 1], e); 491 if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL) 492 { 493 syserr("553 Can't parse myself!"); 494 ExitStat = EX_SOFTWARE; 495 returndepth--; 496 return (-1); 497 } 498 ee->e_sender = ee->e_from.q_paddr; 499 500 /* push state into submessage */ 501 CurEnv = ee; 502 define('f', "\201n", ee); 503 define('x', "Mail Delivery Subsystem", ee); 504 eatheader(ee, TRUE); 505 506 /* mark statistics */ 507 markstats(ee, NULLADDR); 508 509 /* actually deliver the error message */ 510 sendall(ee, SM_DEFAULT); 511 512 /* restore state */ 513 dropenvelope(ee); 514 CurEnv = oldcur; 515 returndepth--; 516 517 /* should check for delivery errors here */ 518 return (0); 519 } 520 /* 521 ** ERRBODY -- output the body of an error message. 522 ** 523 ** Typically this is a copy of the transcript plus a copy of the 524 ** original offending message. 525 ** 526 ** Parameters: 527 ** fp -- the output file. 528 ** m -- the mailer to output to. 529 ** e -- the envelope we are working in. 530 ** 531 ** Returns: 532 ** none 533 ** 534 ** Side Effects: 535 ** Outputs the body of an error message. 536 */ 537 538 errbody(fp, m, e) 539 register FILE *fp; 540 register struct mailer *m; 541 register ENVELOPE *e; 542 { 543 register FILE *xfile; 544 char *p; 545 register ADDRESS *q; 546 bool printheader; 547 char buf[MAXLINE]; 548 549 if (e->e_parent == NULL) 550 { 551 syserr("errbody: null parent"); 552 putline(" ----- Original message lost -----\n", fp, m); 553 return; 554 } 555 556 /* 557 ** Output MIME header. 558 */ 559 560 if (e->e_msgboundary != NULL) 561 { 562 putline("This is a MIME-encapsulated message", fp, m); 563 putline("", fp, m); 564 (void) sprintf(buf, "--%s", e->e_msgboundary); 565 putline(buf, fp, m); 566 putline("", fp, m); 567 } 568 569 /* 570 ** Output introductory information. 571 */ 572 573 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 574 if (bitset(QBADADDR, q->q_flags)) 575 break; 576 if (q == NULL && !bitset(EF_FATALERRS, e->e_parent->e_flags)) 577 { 578 putline(" **********************************************", 579 fp, m); 580 putline(" ** THIS IS A WARNING MESSAGE ONLY **", 581 fp, m); 582 putline(" ** YOU DO NOT NEED TO RESEND YOUR MESSAGE **", 583 fp, m); 584 putline(" **********************************************", 585 fp, m); 586 putline("", fp, m); 587 } 588 sprintf(buf, "The original message was received at %s", 589 arpadate(ctime(&e->e_parent->e_ctime))); 590 putline(buf, fp, m); 591 expand("from \201_", buf, &buf[sizeof buf - 1], e->e_parent); 592 putline(buf, fp, m); 593 putline("", fp, m); 594 595 /* 596 ** Output error message header (if specified and available). 597 */ 598 599 if (ErrMsgFile != NULL) 600 { 601 if (*ErrMsgFile == '/') 602 { 603 xfile = fopen(ErrMsgFile, "r"); 604 if (xfile != NULL) 605 { 606 while (fgets(buf, sizeof buf, xfile) != NULL) 607 { 608 expand(buf, buf, &buf[sizeof buf - 1], e); 609 putline(buf, fp, m); 610 } 611 (void) fclose(xfile); 612 putline("\n", fp, m); 613 } 614 } 615 else 616 { 617 expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e); 618 putline(buf, fp, m); 619 putline("", fp, m); 620 } 621 } 622 623 /* 624 ** Output message introduction 625 */ 626 627 printheader = TRUE; 628 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 629 { 630 if (bitset(QBADADDR|QREPORT, q->q_flags)) 631 { 632 if (printheader) 633 { 634 putline(" ----- The following addresses had delivery problems -----", 635 fp, m); 636 printheader = FALSE; 637 } 638 strcpy(buf, q->q_paddr); 639 if (bitset(QBADADDR, q->q_flags)) 640 strcat(buf, " (unrecoverable error)"); 641 else 642 strcat(buf, " (transient failure)"); 643 putline(buf, fp, m); 644 if (q->q_alias != NULL) 645 { 646 strcpy(buf, " (expanded from: "); 647 strcat(buf, q->q_alias->q_paddr); 648 strcat(buf, ")"); 649 putline(buf, fp, m); 650 } 651 } 652 } 653 if (!printheader) 654 putline("\n", fp, m); 655 656 /* 657 ** Output transcript of errors 658 */ 659 660 (void) fflush(stdout); 661 p = queuename(e->e_parent, 'x'); 662 if ((xfile = fopen(p, "r")) == NULL) 663 { 664 syserr("Cannot open %s", p); 665 putline(" ----- Transcript of session is unavailable -----\n", fp, m); 666 } 667 else 668 { 669 putline(" ----- Transcript of session follows -----\n", fp, m); 670 if (e->e_xfp != NULL) 671 (void) fflush(e->e_xfp); 672 while (fgets(buf, sizeof buf, xfile) != NULL) 673 putline(buf, fp, m); 674 (void) xfclose(xfile, "errbody xscript", p); 675 } 676 errno = 0; 677 678 /* 679 ** Output text of original message 680 */ 681 682 if (NoReturn) 683 SendBody = FALSE; 684 putline("", fp, m); 685 if (e->e_parent->e_df != NULL) 686 { 687 if (SendBody) 688 putline(" ----- Original message follows -----\n", fp, m); 689 else 690 putline(" ----- Message header follows -----\n", fp, m); 691 (void) fflush(fp); 692 693 if (e->e_msgboundary != NULL) 694 { 695 putline("", fp, m); 696 (void) sprintf(buf, "--%s", e->e_msgboundary); 697 putline(buf, fp, m); 698 putline("Content-Type: message/rfc822", fp, m); 699 putline("", fp, m); 700 } 701 putheader(fp, m, e->e_parent); 702 putline("", fp, m); 703 if (SendBody) 704 putbody(fp, m, e->e_parent, e->e_msgboundary); 705 else 706 putline(" ----- Message body suppressed -----", fp, m); 707 } 708 else 709 { 710 putline(" ----- No message was collected -----\n", fp, m); 711 } 712 713 if (e->e_msgboundary != NULL) 714 { 715 putline("", fp, m); 716 (void) sprintf(buf, "--%s--", e->e_msgboundary); 717 putline(buf, fp, m); 718 } 719 putline("", fp, m); 720 721 /* 722 ** Cleanup and exit 723 */ 724 725 if (errno != 0) 726 syserr("errbody: I/O error"); 727 } 728 /* 729 ** PRUNEROUTE -- prune an RFC-822 source route 730 ** 731 ** Trims down a source route to the last internet-registered hop. 732 ** This is encouraged by RFC 1123 section 5.3.3. 733 ** 734 ** Parameters: 735 ** addr -- the address 736 ** 737 ** Returns: 738 ** TRUE -- address was modified 739 ** FALSE -- address could not be pruned 740 ** 741 ** Side Effects: 742 ** modifies addr in-place 743 */ 744 745 pruneroute(addr) 746 char *addr; 747 { 748 #ifdef NAMED_BIND 749 char *start, *at, *comma; 750 char c; 751 int rcode; 752 char hostbuf[BUFSIZ]; 753 char *mxhosts[MAXMXHOSTS + 1]; 754 755 /* check to see if this is really a route-addr */ 756 if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>') 757 return FALSE; 758 start = strchr(addr, ':'); 759 at = strrchr(addr, '@'); 760 if (start == NULL || at == NULL || at < start) 761 return FALSE; 762 763 /* slice off the angle brackets */ 764 strcpy(hostbuf, at + 1); 765 hostbuf[strlen(hostbuf) - 1] = '\0'; 766 767 while (start) 768 { 769 if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0) 770 { 771 strcpy(addr + 1, start + 1); 772 return TRUE; 773 } 774 c = *start; 775 *start = '\0'; 776 comma = strrchr(addr, ','); 777 if (comma && comma[1] == '@') 778 strcpy(hostbuf, comma + 2); 779 else 780 comma = 0; 781 *start = c; 782 start = comma; 783 } 784 #endif 785 return FALSE; 786 } 787