1 /* 2 * Copyright (c) 1983, 1995 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.73 (Berkeley) 05/31/95"; 11 #endif /* not lint */ 12 13 # include "sendmail.h" 14 15 /* 16 ** SAVEMAIL -- Save mail on error 17 ** 18 ** If mailing back errors, mail it back to the originator 19 ** together with an error message; otherwise, just put it in 20 ** dead.letter in the user's home directory (if he exists on 21 ** this machine). 22 ** 23 ** Parameters: 24 ** e -- the envelope containing the message in error. 25 ** sendbody -- if TRUE, also send back the body of the 26 ** message; otherwise just send the header. 27 ** 28 ** Returns: 29 ** none 30 ** 31 ** Side Effects: 32 ** Saves the letter, by writing or mailing it back to the 33 ** sender, or by putting it in dead.letter in her home 34 ** directory. 35 */ 36 37 /* defines for state machine */ 38 # define ESM_REPORT 0 /* report to sender's terminal */ 39 # define ESM_MAIL 1 /* mail back to sender */ 40 # define ESM_QUIET 2 /* messages have already been returned */ 41 # define ESM_DEADLETTER 3 /* save in ~/dead.letter */ 42 # define ESM_POSTMASTER 4 /* return to postmaster */ 43 # define ESM_USRTMP 5 /* save in /usr/tmp/dead.letter */ 44 # define ESM_PANIC 6 /* leave the locked queue/transcript files */ 45 # define ESM_DONE 7 /* the message is successfully delivered */ 46 47 # ifndef _PATH_VARTMP 48 # define _PATH_VARTMP "/usr/tmp/" 49 # endif 50 51 52 void 53 savemail(e, sendbody) 54 register ENVELOPE *e; 55 bool sendbody; 56 { 57 register struct passwd *pw; 58 register FILE *fp; 59 int state; 60 auto ADDRESS *q = NULL; 61 register char *p; 62 MCI mcibuf; 63 int sfflags; 64 char buf[MAXLINE+1]; 65 extern char *ttypath(); 66 typedef int (*fnptr)(); 67 extern bool writable(); 68 69 if (tTd(6, 1)) 70 { 71 printf("\nsavemail, errormode = %c, id = %s, ExitStat = %d\n e_from=", 72 e->e_errormode, e->e_id == NULL ? "NONE" : e->e_id, 73 ExitStat); 74 printaddr(&e->e_from, FALSE); 75 } 76 77 if (e->e_id == NULL) 78 { 79 /* can't return a message with no id */ 80 return; 81 } 82 83 /* 84 ** In the unhappy event we don't know who to return the mail 85 ** to, make someone up. 86 */ 87 88 if (e->e_from.q_paddr == NULL) 89 { 90 e->e_sender = "Postmaster"; 91 if (parseaddr(e->e_sender, &e->e_from, 92 RF_COPYPARSE|RF_SENDERADDR, '\0', NULL, e) == NULL) 93 { 94 syserr("553 Cannot parse Postmaster!"); 95 ExitStat = EX_SOFTWARE; 96 finis(); 97 } 98 } 99 e->e_to = NULL; 100 101 /* 102 ** Basic state machine. 103 ** 104 ** This machine runs through the following states: 105 ** 106 ** ESM_QUIET Errors have already been printed iff the 107 ** sender is local. 108 ** ESM_REPORT Report directly to the sender's terminal. 109 ** ESM_MAIL Mail response to the sender. 110 ** ESM_DEADLETTER Save response in ~/dead.letter. 111 ** ESM_POSTMASTER Mail response to the postmaster. 112 ** ESM_PANIC Save response anywhere possible. 113 */ 114 115 /* determine starting state */ 116 switch (e->e_errormode) 117 { 118 case EM_WRITE: 119 state = ESM_REPORT; 120 break; 121 122 case EM_BERKNET: 123 case EM_MAIL: 124 state = ESM_MAIL; 125 break; 126 127 case EM_PRINT: 128 case '\0': 129 state = ESM_QUIET; 130 break; 131 132 case EM_QUIET: 133 /* no need to return anything at all */ 134 return; 135 136 default: 137 syserr("554 savemail: bogus errormode x%x\n", e->e_errormode); 138 state = ESM_MAIL; 139 break; 140 } 141 142 /* if this is already an error response, send to postmaster */ 143 if (bitset(EF_RESPONSE, e->e_flags)) 144 { 145 if (e->e_parent != NULL && 146 bitset(EF_RESPONSE, e->e_parent->e_flags)) 147 { 148 /* got an error sending a response -- can it */ 149 return; 150 } 151 state = ESM_POSTMASTER; 152 } 153 154 while (state != ESM_DONE) 155 { 156 if (tTd(6, 5)) 157 printf(" state %d\n", state); 158 159 switch (state) 160 { 161 case ESM_QUIET: 162 if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags)) 163 state = ESM_DEADLETTER; 164 else 165 state = ESM_MAIL; 166 break; 167 168 case ESM_REPORT: 169 170 /* 171 ** If the user is still logged in on the same terminal, 172 ** then write the error messages back to hir (sic). 173 */ 174 175 p = ttypath(); 176 if (p == NULL || freopen(p, "w", stdout) == NULL) 177 { 178 state = ESM_MAIL; 179 break; 180 } 181 182 expand("\201n", buf, sizeof buf, e); 183 printf("\r\nMessage from %s...\r\n", buf); 184 printf("Errors occurred while sending mail.\r\n"); 185 if (e->e_xfp != NULL) 186 { 187 (void) fflush(e->e_xfp); 188 fp = fopen(queuename(e, 'x'), "r"); 189 } 190 else 191 fp = NULL; 192 if (fp == NULL) 193 { 194 syserr("Cannot open %s", queuename(e, 'x')); 195 printf("Transcript of session is unavailable.\r\n"); 196 } 197 else 198 { 199 printf("Transcript follows:\r\n"); 200 while (fgets(buf, sizeof buf, fp) != NULL && 201 !ferror(stdout)) 202 fputs(buf, stdout); 203 (void) xfclose(fp, "savemail transcript", e->e_id); 204 } 205 printf("Original message will be saved in dead.letter.\r\n"); 206 state = ESM_DEADLETTER; 207 break; 208 209 case ESM_MAIL: 210 /* 211 ** If mailing back, do it. 212 ** Throw away all further output. Don't alias, 213 ** since this could cause loops, e.g., if joe 214 ** mails to joe@x, and for some reason the network 215 ** for @x is down, then the response gets sent to 216 ** joe@x, which gives a response, etc. Also force 217 ** the mail to be delivered even if a version of 218 ** it has already been sent to the sender. 219 ** 220 ** If this is a configuration or local software 221 ** error, send to the local postmaster as well, 222 ** since the originator can't do anything 223 ** about it anyway. Note that this is a full 224 ** copy of the message (intentionally) so that 225 ** the Postmaster can forward things along. 226 */ 227 228 if (ExitStat == EX_CONFIG || ExitStat == EX_SOFTWARE) 229 { 230 (void) sendtolist("postmaster", 231 NULLADDR, &e->e_errorqueue, 0, e); 232 } 233 if (!emptyaddr(&e->e_from)) 234 { 235 (void) sendtolist(e->e_from.q_paddr, 236 NULLADDR, &e->e_errorqueue, 0, e); 237 } 238 239 /* 240 ** Deliver a non-delivery report to the 241 ** Postmaster-designate (not necessarily 242 ** Postmaster). This does not include the 243 ** body of the message, for privacy reasons. 244 ** You really shouldn't need this. 245 */ 246 247 e->e_flags |= EF_PM_NOTIFY; 248 249 /* check to see if there are any good addresses */ 250 for (q = e->e_errorqueue; q != NULL; q = q->q_next) 251 if (!bitset(QBADADDR|QDONTSEND, q->q_flags)) 252 break; 253 if (q == NULL) 254 { 255 /* this is an error-error */ 256 state = ESM_POSTMASTER; 257 break; 258 } 259 if (returntosender(e->e_message, e->e_errorqueue, 260 sendbody, e) == 0) 261 { 262 state = ESM_DONE; 263 break; 264 } 265 266 /* didn't work -- return to postmaster */ 267 state = ESM_POSTMASTER; 268 break; 269 270 case ESM_POSTMASTER: 271 /* 272 ** Similar to previous case, but to system postmaster. 273 */ 274 275 q = NULL; 276 if (sendtolist("postmaster", NULL, &q, 0, e) <= 0) 277 { 278 syserr("553 cannot parse postmaster!"); 279 ExitStat = EX_SOFTWARE; 280 state = ESM_USRTMP; 281 break; 282 } 283 if (returntosender(e->e_message, q, sendbody, e) == 0) 284 { 285 state = ESM_DONE; 286 break; 287 } 288 289 /* didn't work -- last resort */ 290 state = ESM_USRTMP; 291 break; 292 293 case ESM_DEADLETTER: 294 /* 295 ** Save the message in dead.letter. 296 ** If we weren't mailing back, and the user is 297 ** local, we should save the message in 298 ** ~/dead.letter so that the poor person doesn't 299 ** have to type it over again -- and we all know 300 ** what poor typists UNIX users are. 301 */ 302 303 p = NULL; 304 if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags)) 305 { 306 if (e->e_from.q_home != NULL) 307 p = e->e_from.q_home; 308 else if ((pw = sm_getpwnam(e->e_from.q_user)) != NULL) 309 p = pw->pw_dir; 310 } 311 if (p == NULL || e->e_dfp == NULL) 312 { 313 /* no local directory or no data file */ 314 state = ESM_MAIL; 315 break; 316 } 317 318 /* we have a home directory; write dead.letter */ 319 define('z', p, e); 320 expand("\201z/dead.letter", buf, sizeof buf, e); 321 sfflags = SFF_NOSLINK|SFF_CREAT|SFF_REGONLY|SFF_RUNASREALUID; 322 e->e_to = buf; 323 if (mailfile(buf, NULL, sfflags, e) == EX_OK) 324 { 325 bool oldverb = Verbose; 326 327 Verbose = TRUE; 328 message("Saved message in %s", buf); 329 Verbose = oldverb; 330 state = ESM_DONE; 331 break; 332 } 333 state = ESM_MAIL; 334 break; 335 336 case ESM_USRTMP: 337 /* 338 ** Log the mail in /usr/tmp/dead.letter. 339 */ 340 341 if (e->e_class < 0) 342 { 343 state = ESM_DONE; 344 break; 345 } 346 347 if (SafeFileEnv != NULL && SafeFileEnv[0] != '\0') 348 { 349 state = ESM_PANIC; 350 break; 351 } 352 353 strcpy(buf, _PATH_VARTMP); 354 strcat(buf, "dead.letter"); 355 sfflags = SFF_NOSLINK|SFF_CREAT|SFF_REGONLY; 356 357 if (!writable(buf, NULL, sfflags) || 358 (fp = safefopen(buf, O_WRONLY|O_CREAT|O_APPEND, 359 FileMode, sfflags)) == NULL) 360 { 361 state = ESM_PANIC; 362 break; 363 } 364 365 bzero(&mcibuf, sizeof mcibuf); 366 mcibuf.mci_out = fp; 367 mcibuf.mci_mailer = FileMailer; 368 if (bitnset(M_7BITS, FileMailer->m_flags)) 369 mcibuf.mci_flags |= MCIF_7BIT; 370 371 putfromline(&mcibuf, e); 372 (*e->e_puthdr)(&mcibuf, e->e_header, e); 373 (*e->e_putbody)(&mcibuf, e, NULL); 374 putline("\n", &mcibuf); 375 (void) fflush(fp); 376 if (!ferror(fp)) 377 { 378 bool oldverb = Verbose; 379 380 Verbose = TRUE; 381 message("Saved message in %s", buf); 382 Verbose = oldverb; 383 state = ESM_DONE; 384 } 385 state = ESM_PANIC; 386 (void) xfclose(fp, "savemail", buf); 387 break; 388 389 default: 390 syserr("554 savemail: unknown state %d", state); 391 392 /* fall through ... */ 393 394 case ESM_PANIC: 395 /* leave the locked queue & transcript files around */ 396 loseqfile(e, "savemail panic"); 397 syserr("!554 savemail: cannot save rejected email anywhere"); 398 } 399 } 400 } 401 /* 402 ** RETURNTOSENDER -- return a message to the sender with an error. 403 ** 404 ** Parameters: 405 ** msg -- the explanatory message. 406 ** returnq -- the queue of people to send the message to. 407 ** sendbody -- if TRUE, also send back the body of the 408 ** message; otherwise just send the header. 409 ** e -- the current envelope. 410 ** 411 ** Returns: 412 ** zero -- if everything went ok. 413 ** else -- some error. 414 ** 415 ** Side Effects: 416 ** Returns the current message to the sender via 417 ** mail. 418 */ 419 420 #define MAXRETURNS 6 /* max depth of returning messages */ 421 #define ERRORFUDGE 100 /* nominal size of error message text */ 422 423 int 424 returntosender(msg, returnq, sendbody, e) 425 char *msg; 426 ADDRESS *returnq; 427 bool sendbody; 428 register ENVELOPE *e; 429 { 430 register ENVELOPE *ee; 431 ENVELOPE *oldcur = CurEnv; 432 ENVELOPE errenvelope; 433 static int returndepth; 434 register ADDRESS *q; 435 char *p; 436 char buf[MAXNAME + 1]; 437 extern void errbody __P((MCI *, ENVELOPE *, char *)); 438 439 if (returnq == NULL) 440 return (-1); 441 442 if (msg == NULL) 443 msg = "Unable to deliver mail"; 444 445 if (tTd(6, 1)) 446 { 447 printf("\n*** Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=", 448 msg, returndepth, e); 449 printaddr(returnq, TRUE); 450 if (tTd(6, 20)) 451 { 452 printf("Sendq="); 453 printaddr(e->e_sendqueue, TRUE); 454 } 455 } 456 457 if (++returndepth >= MAXRETURNS) 458 { 459 if (returndepth != MAXRETURNS) 460 syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr); 461 /* don't "unrecurse" and fake a clean exit */ 462 /* returndepth--; */ 463 return (0); 464 } 465 466 define('g', e->e_from.q_paddr, e); 467 define('u', NULL, e); 468 469 /* initialize error envelope */ 470 ee = newenvelope(&errenvelope, e); 471 define('a', "\201b", ee); 472 define('r', "internal", ee); 473 define('s', "localhost", ee); 474 define('_', "localhost", ee); 475 ee->e_puthdr = putheader; 476 ee->e_putbody = errbody; 477 ee->e_flags |= EF_RESPONSE|EF_METOO; 478 if (!bitset(EF_OLDSTYLE, e->e_flags)) 479 ee->e_flags &= ~EF_OLDSTYLE; 480 ee->e_sendqueue = returnq; 481 ee->e_msgsize = ERRORFUDGE; 482 if (sendbody) 483 ee->e_msgsize += e->e_msgsize; 484 else 485 ee->e_flags |= EF_NO_BODY_RETN; 486 initsys(ee); 487 for (q = returnq; q != NULL; q = q->q_next) 488 { 489 if (bitset(QBADADDR, q->q_flags)) 490 continue; 491 492 if (!DontPruneRoutes && pruneroute(q->q_paddr)) 493 { 494 register ADDRESS *p; 495 496 parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e); 497 for (p = returnq; p != NULL; p = p->q_next) 498 { 499 if (p != q && sameaddr(p, q)) 500 q->q_flags |= QDONTSEND; 501 } 502 } 503 504 if (!bitset(QDONTSEND, q->q_flags)) 505 ee->e_nrcpts++; 506 507 if (q->q_alias == NULL) 508 addheader("To", q->q_paddr, &ee->e_header); 509 } 510 511 # ifdef LOG 512 if (LogLevel > 5) 513 syslog(LOG_INFO, "%s: %s: return to sender: %s", 514 e->e_id, ee->e_id, msg); 515 # endif 516 517 if (SendMIMEErrors) 518 { 519 addheader("MIME-Version", "1.0", &ee->e_header); 520 (void) sprintf(buf, "%s.%ld/%s", 521 ee->e_id, curtime(), MyHostName); 522 ee->e_msgboundary = newstr(buf); 523 (void) sprintf(buf, 524 #if DSN 525 "multipart/report; report-type=X-delivery-status-3 (Draft of May 5, 1995);\n\tboundary=\"%s\"", 526 #else 527 "multipart/mixed; boundary=\"%s\"", 528 #endif 529 ee->e_msgboundary); 530 addheader("Content-Type", buf, &ee->e_header); 531 } 532 if (strncmp(msg, "Warning:", 8) == 0) 533 { 534 addheader("Subject", msg, &ee->e_header); 535 p = "warning-timeout"; 536 } 537 else if (strcmp(msg, "Return receipt") == 0) 538 { 539 addheader("Subject", msg, &ee->e_header); 540 p = "return-receipt"; 541 } 542 else 543 { 544 sprintf(buf, "Returned mail: %.*s", sizeof buf - 20, msg); 545 addheader("Subject", buf, &ee->e_header); 546 p = "failure"; 547 } 548 (void) sprintf(buf, "auto-generated (%s)", p); 549 addheader("Auto-Submitted", buf, &ee->e_header); 550 551 /* fake up an address header for the from person */ 552 expand("\201n", buf, sizeof buf, e); 553 if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL) 554 { 555 syserr("553 Can't parse myself!"); 556 ExitStat = EX_SOFTWARE; 557 returndepth--; 558 return (-1); 559 } 560 ee->e_sender = ee->e_from.q_paddr; 561 562 /* push state into submessage */ 563 CurEnv = ee; 564 define('f', "\201n", ee); 565 define('x', "Mail Delivery Subsystem", ee); 566 eatheader(ee, TRUE); 567 568 /* mark statistics */ 569 markstats(ee, NULLADDR); 570 571 /* actually deliver the error message */ 572 sendall(ee, SM_DEFAULT); 573 574 /* restore state */ 575 dropenvelope(ee); 576 CurEnv = oldcur; 577 returndepth--; 578 579 /* should check for delivery errors here */ 580 return (0); 581 } 582 /* 583 ** ERRBODY -- output the body of an error message. 584 ** 585 ** Typically this is a copy of the transcript plus a copy of the 586 ** original offending message. 587 ** 588 ** Parameters: 589 ** mci -- the mailer connection information. 590 ** e -- the envelope we are working in. 591 ** separator -- any possible MIME separator. 592 ** flags -- to modify the behaviour. 593 ** 594 ** Returns: 595 ** none 596 ** 597 ** Side Effects: 598 ** Outputs the body of an error message. 599 */ 600 601 void 602 errbody(mci, e, separator) 603 register MCI *mci; 604 register ENVELOPE *e; 605 char *separator; 606 { 607 register FILE *xfile; 608 char *p; 609 register ADDRESS *q; 610 bool printheader; 611 bool sendbody; 612 char buf[MAXLINE]; 613 extern char *xtextify(); 614 615 if (bitset(MCIF_INHEADER, mci->mci_flags)) 616 { 617 putline("", mci); 618 mci->mci_flags &= ~MCIF_INHEADER; 619 } 620 if (e->e_parent == NULL) 621 { 622 syserr("errbody: null parent"); 623 putline(" ----- Original message lost -----\n", mci); 624 return; 625 } 626 627 /* 628 ** Output MIME header. 629 */ 630 631 if (e->e_msgboundary != NULL) 632 { 633 putline("This is a MIME-encapsulated message", mci); 634 putline("", mci); 635 (void) sprintf(buf, "--%s", e->e_msgboundary); 636 putline(buf, mci); 637 putline("", mci); 638 } 639 640 /* 641 ** Output introductory information. 642 */ 643 644 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 645 if (bitset(QBADADDR, q->q_flags)) 646 break; 647 if (q == NULL && 648 !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags)) 649 { 650 putline(" **********************************************", 651 mci); 652 putline(" ** THIS IS A WARNING MESSAGE ONLY **", 653 mci); 654 putline(" ** YOU DO NOT NEED TO RESEND YOUR MESSAGE **", 655 mci); 656 putline(" **********************************************", 657 mci); 658 putline("", mci); 659 } 660 sprintf(buf, "The original message was received at %s", 661 arpadate(ctime(&e->e_parent->e_ctime))); 662 putline(buf, mci); 663 expand("from \201_", buf, sizeof buf, e->e_parent); 664 putline(buf, mci); 665 putline("", mci); 666 667 /* 668 ** Output error message header (if specified and available). 669 */ 670 671 if (ErrMsgFile != NULL && !bitset(EF_SENDRECEIPT, e->e_parent->e_flags)) 672 { 673 if (*ErrMsgFile == '/') 674 { 675 xfile = fopen(ErrMsgFile, "r"); 676 if (xfile != NULL) 677 { 678 while (fgets(buf, sizeof buf, xfile) != NULL) 679 { 680 expand(buf, buf, sizeof buf, e); 681 putline(buf, mci); 682 } 683 (void) fclose(xfile); 684 putline("\n", mci); 685 } 686 } 687 else 688 { 689 expand(ErrMsgFile, buf, sizeof buf, e); 690 putline(buf, mci); 691 putline("", mci); 692 } 693 } 694 695 /* 696 ** Output message introduction 697 */ 698 699 printheader = TRUE; 700 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 701 { 702 if (bitset(QBADADDR, q->q_flags)) 703 { 704 if (!bitset(QPINGONFAILURE, q->q_flags)) 705 continue; 706 p = "unrecoverable error"; 707 } 708 else if (!bitset(QPRIMARY, q->q_flags)) 709 continue; 710 else if (bitset(QRELAYED, q->q_flags)) 711 p = "relayed to non-DSN-aware mailer"; 712 else if (bitset(QDELIVERED, q->q_flags)) 713 { 714 if (bitset(QEXPANDED, q->q_flags)) 715 p = "successfully delivered to mailing list"; 716 else 717 p = "successfully delivered to mailbox"; 718 } 719 else if (bitset(QEXPANDED, q->q_flags)) 720 p = "expanded by alias"; 721 else if (bitset(QDELAYED, q->q_flags)) 722 p = "transient failure"; 723 else 724 continue; 725 726 if (printheader) 727 { 728 putline(" ----- The following addresses have delivery notifications -----", 729 mci); 730 printheader = FALSE; 731 } 732 733 sprintf(buf, "%s (%s)", q->q_paddr, p); 734 putline(buf, mci); 735 if (q->q_alias != NULL) 736 { 737 strcpy(buf, " (expanded from: "); 738 strcat(buf, q->q_alias->q_paddr); 739 strcat(buf, ")"); 740 putline(buf, mci); 741 } 742 } 743 if (!printheader) 744 putline("\n", mci); 745 746 /* 747 ** Output transcript of errors 748 */ 749 750 (void) fflush(stdout); 751 p = queuename(e->e_parent, 'x'); 752 if ((xfile = fopen(p, "r")) == NULL) 753 { 754 syserr("Cannot open %s", p); 755 putline(" ----- Transcript of session is unavailable -----\n", mci); 756 } 757 else 758 { 759 printheader = TRUE; 760 if (e->e_xfp != NULL) 761 (void) fflush(e->e_xfp); 762 while (fgets(buf, sizeof buf, xfile) != NULL) 763 { 764 if (printheader) 765 putline(" ----- Transcript of session follows -----\n", mci); 766 printheader = FALSE; 767 putline(buf, mci); 768 } 769 (void) xfclose(xfile, "errbody xscript", p); 770 } 771 errno = 0; 772 773 #if DSN 774 /* 775 ** Output machine-readable version. 776 */ 777 778 if (e->e_msgboundary != NULL) 779 { 780 putline("", mci); 781 (void) sprintf(buf, "--%s", e->e_msgboundary); 782 putline(buf, mci); 783 putline("Content-Type: message/X-delivery-status-04a (Draft of April 4, 1995)", mci); 784 putline("", mci); 785 786 /* 787 ** Output per-message information. 788 */ 789 790 /* original envelope id from MAIL FROM: line */ 791 if (e->e_parent->e_envid != NULL) 792 { 793 (void) sprintf(buf, "Original-Envelope-Id: %s", 794 xtextify(e->e_parent->e_envid)); 795 putline(buf, mci); 796 } 797 798 /* Reporting-MTA: is us (required) */ 799 (void) sprintf(buf, "Reporting-MTA: dns; %s", 800 xtextify(MyHostName)); 801 putline(buf, mci); 802 803 /* DSN-Gateway: not relevant since we are not translating */ 804 805 /* Received-From-MTA: shows where we got this message from */ 806 if (RealHostName != NULL) 807 { 808 /* XXX use $s for type? */ 809 p = e->e_parent->e_from.q_mailer->m_mtatype; 810 if (p == NULL) 811 p = "dns"; 812 (void) sprintf(buf, "Received-From-MTA: %s; %s", 813 p, xtextify(RealHostName)); 814 putline(buf, mci); 815 } 816 817 /* Arrival-Date: -- when it arrived here */ 818 (void) sprintf(buf, "Arrival-Date: %s", 819 arpadate(ctime(&e->e_parent->e_ctime))); 820 putline(buf, mci); 821 822 /* 823 ** Output per-address information. 824 */ 825 826 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 827 { 828 register ADDRESS *r; 829 char *action; 830 831 if (bitset(QBADADDR, q->q_flags)) 832 action = "failed"; 833 else if (!bitset(QPRIMARY, q->q_flags)) 834 continue; 835 else if (bitset(QDELIVERED, q->q_flags)) 836 { 837 if (bitset(QEXPANDED, q->q_flags)) 838 action = "delivered (to mailing list)"; 839 else 840 action = "delivered (to mailbox)"; 841 } 842 else if (bitset(QRELAYED, q->q_flags)) 843 action = "relayed (to non-DSN-aware mailer)"; 844 else if (bitset(QEXPANDED, q->q_flags)) 845 action = "expanded (to multi-recipient alias)"; 846 else if (bitset(QDELAYED, q->q_flags)) 847 action = "delayed"; 848 else 849 continue; 850 851 putline("", mci); 852 853 /* Original-Recipient: -- passed from on high */ 854 if (q->q_orcpt != NULL) 855 { 856 (void) sprintf(buf, "Original-Recipient: %s", 857 xtextify(q->q_orcpt)); 858 putline(buf, mci); 859 } 860 861 /* Final-Recipient: -- the name from the RCPT command */ 862 p = e->e_parent->e_from.q_mailer->m_addrtype; 863 if (p == NULL) 864 p = "rfc822"; 865 for (r = q; r->q_alias != NULL; r = r->q_alias) 866 continue; 867 if (strchr(r->q_user, '@') == NULL) 868 { 869 (void) sprintf(buf, "Final-Recipient: %s; %s@", 870 p, xtextify(r->q_user)); 871 strcat(buf, xtextify(MyHostName)); 872 } 873 else 874 { 875 (void) sprintf(buf, "Final-Recipient: %s; %s", 876 p, xtextify(r->q_user)); 877 } 878 putline(buf, mci); 879 880 /* X-Actual-Recipient: -- the real problem address */ 881 if (r != q) 882 { 883 if (strchr(q->q_user, '@') == NULL) 884 { 885 (void) sprintf(buf, "X-Actual-Recipient: %s; %s@", 886 p, xtextify(q->q_user)); 887 strcat(buf, xtextify(MyHostName)); 888 } 889 else 890 { 891 (void) sprintf(buf, "X-Actual-Recipient: %s; %s", 892 p, xtextify(q->q_user)); 893 } 894 putline(buf, mci); 895 } 896 897 /* Action: -- what happened? */ 898 sprintf(buf, "Action: %s", action); 899 putline(buf, mci); 900 901 /* Status: -- what _really_ happened? */ 902 strcpy(buf, "Status: "); 903 if (q->q_status != NULL) 904 strcat(buf, q->q_status); 905 else if (bitset(QBADADDR, q->q_flags)) 906 strcat(buf, "5.0.0"); 907 else if (bitset(QQUEUEUP, q->q_flags)) 908 strcat(buf, "4.0.0"); 909 else 910 strcat(buf, "2.0.0"); 911 putline(buf, mci); 912 913 /* Remote-MTA: -- who was I talking to? */ 914 p = q->q_mailer->m_mtatype; 915 if (p == NULL) 916 p = "dns"; 917 (void) sprintf(buf, "Remote-MTA: %s; ", p); 918 if (q->q_statmta != NULL) 919 p = q->q_statmta; 920 else if (q->q_host != NULL && q->q_host[0] != '\0') 921 p = q->q_host; 922 else 923 p = NULL; 924 if (p != NULL) 925 { 926 strcat(buf, p); 927 p = &buf[strlen(buf) - 1]; 928 if (*p == '.') 929 *p = '\0'; 930 putline(buf, mci); 931 } 932 933 /* Diagnostic-Code: -- actual result from other end */ 934 if (q->q_rstatus != NULL) 935 { 936 p = q->q_mailer->m_diagtype; 937 if (p == NULL) 938 p = "smtp"; 939 (void) sprintf(buf, "Diagnostic-Code: %s; %s", 940 p, xtextify(q->q_rstatus)); 941 putline(buf, mci); 942 } 943 944 /* Last-Attempt-Date: -- fine granularity */ 945 if (q->q_statdate == (time_t) 0L) 946 q->q_statdate = curtime(); 947 (void) sprintf(buf, "Last-Attempt-Date: %s", 948 arpadate(ctime(&q->q_statdate))); 949 putline(buf, mci); 950 951 /* Will-Retry-Until: -- for delayed messages only */ 952 if (bitset(QQUEUEUP, q->q_flags) && 953 !bitset(QBADADDR, q->q_flags)) 954 { 955 time_t xdate; 956 957 xdate = e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass]; 958 sprintf(buf, "Will-Retry-Until: %s", 959 arpadate(ctime(&xdate))); 960 putline(buf, mci); 961 } 962 } 963 } 964 #endif 965 966 /* 967 ** Output text of original message 968 */ 969 970 putline("", mci); 971 if (bitset(EF_HAS_DF, e->e_parent->e_flags)) 972 { 973 sendbody = !bitset(EF_NO_BODY_RETN, e->e_parent->e_flags) && 974 !bitset(EF_NO_BODY_RETN, e->e_flags); 975 976 if (e->e_msgboundary == NULL) 977 { 978 if (sendbody) 979 putline(" ----- Original message follows -----\n", mci); 980 else 981 putline(" ----- Message header follows -----\n", mci); 982 (void) fflush(mci->mci_out); 983 } 984 else 985 { 986 (void) sprintf(buf, "--%s", e->e_msgboundary); 987 putline(buf, mci); 988 (void) sprintf(buf, "Content-Type: %s", 989 sendbody ? "message/rfc822" 990 : "text/rfc822-headers"); 991 putline(buf, mci); 992 } 993 putline("", mci); 994 putheader(mci, e->e_parent->e_header, e->e_parent); 995 if (sendbody) 996 putbody(mci, e->e_parent, e->e_msgboundary); 997 else if (e->e_msgboundary == NULL) 998 { 999 putline("", mci); 1000 putline(" ----- Message body suppressed -----", mci); 1001 } 1002 } 1003 else if (e->e_msgboundary == NULL) 1004 { 1005 putline(" ----- No message was collected -----\n", mci); 1006 } 1007 1008 if (e->e_msgboundary != NULL) 1009 { 1010 putline("", mci); 1011 (void) sprintf(buf, "--%s--", e->e_msgboundary); 1012 putline(buf, mci); 1013 } 1014 putline("", mci); 1015 1016 /* 1017 ** Cleanup and exit 1018 */ 1019 1020 if (errno != 0) 1021 syserr("errbody: I/O error"); 1022 } 1023 /* 1024 ** SMTPTODSN -- convert SMTP to DSN status code 1025 ** 1026 ** Parameters: 1027 ** smtpstat -- the smtp status code (e.g., 550). 1028 ** 1029 ** Returns: 1030 ** The DSN version of the status code. 1031 */ 1032 1033 char * 1034 smtptodsn(smtpstat) 1035 int smtpstat; 1036 { 1037 if (smtpstat < 0) 1038 return "4.4.2"; 1039 1040 switch (smtpstat) 1041 { 1042 case 450: /* Req mail action not taken: mailbox unavailable */ 1043 return "4.2.0"; 1044 1045 case 451: /* Req action aborted: local error in processing */ 1046 return "4.3.0"; 1047 1048 case 452: /* Req action not taken: insufficient sys storage */ 1049 return "4.3.1"; 1050 1051 case 500: /* Syntax error, command unrecognized */ 1052 return "5.5.2"; 1053 1054 case 501: /* Syntax error in parameters or arguments */ 1055 return "5.5.4"; 1056 1057 case 502: /* Command not implemented */ 1058 return "5.5.1"; 1059 1060 case 503: /* Bad sequence of commands */ 1061 return "5.5.1"; 1062 1063 case 504: /* Command parameter not implemented */ 1064 return "5.5.4"; 1065 1066 case 550: /* Req mail action not taken: mailbox unavailable */ 1067 return "5.2.0"; 1068 1069 case 551: /* User not local; please try <...> */ 1070 return "5.1.6"; 1071 1072 case 552: /* Req mail action aborted: exceeded storage alloc */ 1073 return "5.2.2"; 1074 1075 case 553: /* Req action not taken: mailbox name not allowed */ 1076 return "5.1.3"; 1077 1078 case 554: /* Transaction failed */ 1079 return "5.0.0"; 1080 } 1081 1082 if ((smtpstat / 100) == 2) 1083 return "2.0.0"; 1084 if ((smtpstat / 100) == 4) 1085 return "4.0.0"; 1086 return "5.0.0"; 1087 } 1088 /* 1089 ** XTEXTIFY -- take regular text and turn it into DSN-style xtext 1090 ** 1091 ** Parameters: 1092 ** t -- the text to convert. 1093 ** 1094 ** Returns: 1095 ** The xtext-ified version of the same string. 1096 */ 1097 1098 char * 1099 xtextify(t) 1100 register char *t; 1101 { 1102 register char *p; 1103 int l; 1104 int nbogus; 1105 static char *bp = NULL; 1106 static int bplen = 0; 1107 1108 /* figure out how long this xtext will have to be */ 1109 nbogus = l = 0; 1110 for (p = t; *p != '\0'; p++) 1111 { 1112 register int c = (*p & 0xff); 1113 1114 /* ASCII dependence here -- this is the way the spec words it */ 1115 if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(') 1116 nbogus++; 1117 l++; 1118 } 1119 if (nbogus == 0) 1120 return t; 1121 l += nbogus * 2 + 1; 1122 1123 /* now allocate space if necessary for the new string */ 1124 if (l > bplen) 1125 { 1126 if (bp != NULL) 1127 free(bp); 1128 bp = xalloc(l); 1129 bplen = l; 1130 } 1131 1132 /* ok, copy the text with byte expansion */ 1133 for (p = bp; *t != '\0'; ) 1134 { 1135 register int c = (*t++ & 0xff); 1136 1137 /* ASCII dependence here -- this is the way the spec words it */ 1138 if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(') 1139 { 1140 *p++ = '+'; 1141 *p++ = "0123456789abcdef"[c >> 4]; 1142 *p++ = "0123456789abcdef"[c & 0xf]; 1143 } 1144 else 1145 *p++ = c; 1146 } 1147 *p = '\0'; 1148 return bp; 1149 } 1150 /* 1151 ** XTEXTOK -- check if a string is legal xtext 1152 ** 1153 ** Xtext is used in Delivery Status Notifications. The spec was 1154 ** taken from draft-ietf-notary-mime-delivery-04.txt. 1155 ** 1156 ** Parameters: 1157 ** s -- the string to check. 1158 ** 1159 ** Returns: 1160 ** TRUE -- if 's' is legal xtext. 1161 ** FALSE -- if it has any illegal characters in it. 1162 */ 1163 1164 bool 1165 xtextok(s) 1166 char *s; 1167 { 1168 int c; 1169 1170 while ((c = *s++) != '\0') 1171 { 1172 if (c == '+') 1173 { 1174 c = *s++; 1175 if (!isascii(c) || !isxdigit(c)) 1176 return FALSE; 1177 c = *s++; 1178 if (!isascii(c) || !isxdigit(c)) 1179 return FALSE; 1180 } 1181 else if (c < '!' || c > '~' || c == '\\' || c == '(') 1182 return FALSE; 1183 } 1184 return TRUE; 1185 } 1186 /* 1187 ** PRUNEROUTE -- prune an RFC-822 source route 1188 ** 1189 ** Trims down a source route to the last internet-registered hop. 1190 ** This is encouraged by RFC 1123 section 5.3.3. 1191 ** 1192 ** Parameters: 1193 ** addr -- the address 1194 ** 1195 ** Returns: 1196 ** TRUE -- address was modified 1197 ** FALSE -- address could not be pruned 1198 ** 1199 ** Side Effects: 1200 ** modifies addr in-place 1201 */ 1202 1203 bool 1204 pruneroute(addr) 1205 char *addr; 1206 { 1207 #if NAMED_BIND 1208 char *start, *at, *comma; 1209 char c; 1210 int rcode; 1211 char hostbuf[BUFSIZ]; 1212 char *mxhosts[MAXMXHOSTS + 1]; 1213 1214 /* check to see if this is really a route-addr */ 1215 if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>') 1216 return FALSE; 1217 start = strchr(addr, ':'); 1218 at = strrchr(addr, '@'); 1219 if (start == NULL || at == NULL || at < start) 1220 return FALSE; 1221 1222 /* slice off the angle brackets */ 1223 strcpy(hostbuf, at + 1); 1224 hostbuf[strlen(hostbuf) - 1] = '\0'; 1225 1226 while (start) 1227 { 1228 if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0) 1229 { 1230 strcpy(addr + 1, start + 1); 1231 return TRUE; 1232 } 1233 c = *start; 1234 *start = '\0'; 1235 comma = strrchr(addr, ','); 1236 if (comma && comma[1] == '@') 1237 strcpy(hostbuf, comma + 2); 1238 else 1239 comma = 0; 1240 *start = c; 1241 start = comma; 1242 } 1243 #endif 1244 return FALSE; 1245 } 1246