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