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.52 (Berkeley) 03/05/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)) 369 { 370 state = ESM_PANIC; 371 break; 372 } 373 fp = dfopen(buf, O_WRONLY|O_CREAT|O_APPEND, FileMode); 374 if (fp == NULL) 375 { 376 state = ESM_PANIC; 377 break; 378 } 379 380 bzero(&mcibuf, sizeof mcibuf); 381 mcibuf.mci_out = fp; 382 mcibuf.mci_mailer = FileMailer; 383 if (bitnset(M_7BITS, FileMailer->m_flags)) 384 mcibuf.mci_flags |= MCIF_7BIT; 385 386 putfromline(&mcibuf, e); 387 (*e->e_puthdr)(&mcibuf, e->e_header, e); 388 (*e->e_putbody)(&mcibuf, e, NULL); 389 putline("\n", &mcibuf); 390 (void) fflush(fp); 391 state = ferror(fp) ? ESM_PANIC : ESM_DONE; 392 (void) xfclose(fp, "savemail", buf); 393 break; 394 395 default: 396 syserr("554 savemail: unknown state %d", state); 397 398 /* fall through ... */ 399 400 case ESM_PANIC: 401 /* leave the locked queue & transcript files around */ 402 loseqfile(e, "savemail panic"); 403 syserr("!554 savemail: cannot save rejected email anywhere"); 404 } 405 } 406 } 407 /* 408 ** RETURNTOSENDER -- return a message to the sender with an error. 409 ** 410 ** Parameters: 411 ** msg -- the explanatory message. 412 ** returnq -- the queue of people to send the message to. 413 ** sendbody -- if TRUE, also send back the body of the 414 ** message; otherwise just send the header. 415 ** e -- the current envelope. 416 ** 417 ** Returns: 418 ** zero -- if everything went ok. 419 ** else -- some error. 420 ** 421 ** Side Effects: 422 ** Returns the current message to the sender via 423 ** mail. 424 */ 425 426 static bool SendBody; 427 428 #define MAXRETURNS 6 /* max depth of returning messages */ 429 #define ERRORFUDGE 100 /* nominal size of error message text */ 430 431 returntosender(msg, returnq, sendbody, e) 432 char *msg; 433 ADDRESS *returnq; 434 bool sendbody; 435 register ENVELOPE *e; 436 { 437 char buf[MAXNAME]; 438 extern putheader(), errbody(); 439 register ENVELOPE *ee; 440 ENVELOPE *oldcur = CurEnv; 441 ENVELOPE errenvelope; 442 static int returndepth; 443 register ADDRESS *q; 444 char *p; 445 446 if (returnq == NULL) 447 return (-1); 448 449 if (msg == NULL) 450 msg = "Unable to deliver mail"; 451 452 if (tTd(6, 1)) 453 { 454 printf("\n*** Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=", 455 msg, returndepth, e); 456 printaddr(returnq, TRUE); 457 if (tTd(6, 20)) 458 { 459 printf("Sendq="); 460 printaddr(e->e_sendqueue, TRUE); 461 } 462 } 463 464 if (++returndepth >= MAXRETURNS) 465 { 466 if (returndepth != MAXRETURNS) 467 syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr); 468 /* don't "unrecurse" and fake a clean exit */ 469 /* returndepth--; */ 470 return (0); 471 } 472 473 SendBody = sendbody; 474 define('g', e->e_from.q_paddr, e); 475 define('u', NULL, e); 476 477 /* initialize error envelope */ 478 ee = newenvelope(&errenvelope, e); 479 define('a', "\201b", ee); 480 define('r', "internal", ee); 481 define('s', "localhost", ee); 482 define('_', "localhost", ee); 483 ee->e_puthdr = putheader; 484 ee->e_putbody = errbody; 485 ee->e_flags |= EF_RESPONSE|EF_METOO; 486 if (!bitset(EF_OLDSTYLE, e->e_flags)) 487 ee->e_flags &= ~EF_OLDSTYLE; 488 ee->e_sendqueue = returnq; 489 ee->e_msgsize = ERRORFUDGE; 490 if (!bitset(EF_NORETURN, e->e_flags)) 491 ee->e_msgsize += e->e_msgsize; 492 initsys(ee); 493 for (q = returnq; q != NULL; q = q->q_next) 494 { 495 if (bitset(QBADADDR, q->q_flags)) 496 continue; 497 498 if (!DontPruneRoutes && pruneroute(q->q_paddr)) 499 { 500 register ADDRESS *p; 501 502 parseaddr(q->q_paddr, q, RF_COPYPARSE, '\0', NULL, e); 503 for (p = returnq; p != NULL; p = p->q_next) 504 { 505 if (p != q && sameaddr(p, q)) 506 q->q_flags |= QDONTSEND; 507 } 508 } 509 510 if (!bitset(QDONTSEND, q->q_flags)) 511 ee->e_nrcpts++; 512 513 if (q->q_alias == NULL) 514 addheader("To", q->q_paddr, &ee->e_header); 515 } 516 517 # ifdef LOG 518 if (LogLevel > 5) 519 syslog(LOG_INFO, "%s: %s: return to sender: %s", 520 e->e_id, ee->e_id, msg); 521 # endif 522 523 if (SendMIMEErrors) 524 { 525 addheader("MIME-Version", "1.0", &ee->e_header); 526 (void) sprintf(buf, "%s.%ld/%s", 527 ee->e_id, curtime(), MyHostName); 528 ee->e_msgboundary = newstr(buf); 529 (void) sprintf(buf, 530 #ifdef DSN 531 "multipart/report; report-type=X-delivery-status-1; boundary=\"%s\"", 532 #else 533 "multipart/mixed; boundary=\"%s\"", 534 #endif 535 ee->e_msgboundary); 536 addheader("Content-Type", buf, &ee->e_header); 537 } 538 if (strncmp(msg, "Warning:", 8) == 0) 539 { 540 addheader("Subject", msg, ee); 541 p = "warning-timeout"; 542 } 543 else if (strcmp(msg, "Return receipt") == 0) 544 { 545 addheader("Subject", msg, ee); 546 p = "return-receipt"; 547 } 548 else 549 { 550 sprintf(buf, "Returned mail: %.*s", sizeof buf - 20, msg); 551 addheader("Subject", buf, &ee->e_header); 552 p = "failure"; 553 } 554 (void) sprintf(buf, "auto-generated (%s)", p); 555 addheader("Auto-Submitted", buf, &ee->e_header); 556 557 /* fake up an address header for the from person */ 558 expand("\201n", buf, &buf[sizeof buf - 1], e); 559 if (parseaddr(buf, &ee->e_from, RF_COPYALL|RF_SENDERADDR, '\0', NULL, e) == NULL) 560 { 561 syserr("553 Can't parse myself!"); 562 ExitStat = EX_SOFTWARE; 563 returndepth--; 564 return (-1); 565 } 566 ee->e_sender = ee->e_from.q_paddr; 567 568 /* push state into submessage */ 569 CurEnv = ee; 570 define('f', "\201n", ee); 571 define('x', "Mail Delivery Subsystem", ee); 572 eatheader(ee, TRUE); 573 574 /* mark statistics */ 575 markstats(ee, NULLADDR); 576 577 /* actually deliver the error message */ 578 sendall(ee, SM_DEFAULT); 579 580 /* restore state */ 581 dropenvelope(ee); 582 CurEnv = oldcur; 583 returndepth--; 584 585 /* should check for delivery errors here */ 586 return (0); 587 } 588 /* 589 ** ERRBODY -- output the body of an error message. 590 ** 591 ** Typically this is a copy of the transcript plus a copy of the 592 ** original offending message. 593 ** 594 ** Parameters: 595 ** mci -- the mailer connection information. 596 ** e -- the envelope we are working in. 597 ** separator -- any possible MIME separator. 598 ** flags -- to modify the behaviour. 599 ** 600 ** Returns: 601 ** none 602 ** 603 ** Side Effects: 604 ** Outputs the body of an error message. 605 */ 606 607 errbody(mci, e, separator) 608 register MCI *mci; 609 register ENVELOPE *e; 610 char *separator; 611 { 612 register FILE *xfile; 613 char *p; 614 register ADDRESS *q; 615 bool printheader; 616 char buf[MAXLINE]; 617 extern char *xtextify(); 618 619 if (bitset(MCIF_INHEADER, mci->mci_flags)) 620 { 621 putline("", mci); 622 mci->mci_flags &= ~MCIF_INHEADER; 623 } 624 if (e->e_parent == NULL) 625 { 626 syserr("errbody: null parent"); 627 putline(" ----- Original message lost -----\n", mci); 628 return; 629 } 630 631 /* 632 ** Output MIME header. 633 */ 634 635 if (e->e_msgboundary != NULL) 636 { 637 putline("This is a MIME-encapsulated message", mci); 638 putline("", mci); 639 (void) sprintf(buf, "--%s", e->e_msgboundary); 640 putline(buf, mci); 641 putline("", mci); 642 } 643 644 /* 645 ** Output introductory information. 646 */ 647 648 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 649 if (bitset(QBADADDR, q->q_flags)) 650 break; 651 if (q == NULL && 652 !bitset(EF_FATALERRS|EF_SENDRECEIPT, e->e_parent->e_flags)) 653 { 654 putline(" **********************************************", 655 mci); 656 putline(" ** THIS IS A WARNING MESSAGE ONLY **", 657 mci); 658 putline(" ** YOU DO NOT NEED TO RESEND YOUR MESSAGE **", 659 mci); 660 putline(" **********************************************", 661 mci); 662 putline("", mci); 663 } 664 sprintf(buf, "The original message was received at %s", 665 arpadate(ctime(&e->e_parent->e_ctime))); 666 putline(buf, mci); 667 expand("from \201_", buf, &buf[sizeof buf - 1], e->e_parent); 668 putline(buf, mci); 669 putline("", mci); 670 671 /* 672 ** Output error message header (if specified and available). 673 */ 674 675 if (ErrMsgFile != NULL && !bitset(EF_SENDRECEIPT, e->e_parent->e_flags)) 676 { 677 if (*ErrMsgFile == '/') 678 { 679 xfile = fopen(ErrMsgFile, "r"); 680 if (xfile != NULL) 681 { 682 while (fgets(buf, sizeof buf, xfile) != NULL) 683 { 684 expand(buf, buf, &buf[sizeof buf - 1], e); 685 putline(buf, mci); 686 } 687 (void) fclose(xfile); 688 putline("\n", mci); 689 } 690 } 691 else 692 { 693 expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e); 694 putline(buf, mci); 695 putline("", mci); 696 } 697 } 698 699 /* 700 ** Output message introduction 701 */ 702 703 printheader = TRUE; 704 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 705 { 706 if (bitset(QBADADDR|QREPORT|QRELAYED, q->q_flags)) 707 { 708 if (printheader) 709 { 710 putline(" ----- The following addresses have delivery notifications -----", 711 mci); 712 printheader = FALSE; 713 } 714 strcpy(buf, q->q_paddr); 715 if (bitset(QBADADDR, q->q_flags)) 716 strcat(buf, " (unrecoverable error)"); 717 else if (bitset(QRELAYED, q->q_flags)) 718 strcat(buf, " (relayed to non-DSN-aware mailer)"); 719 else if (bitset(QSENT, q->q_flags)) 720 strcat(buf, " (successfully delivered)"); 721 else 722 strcat(buf, " (transient failure)"); 723 putline(buf, mci); 724 if (q->q_alias != NULL) 725 { 726 strcpy(buf, " (expanded from: "); 727 strcat(buf, q->q_alias->q_paddr); 728 strcat(buf, ")"); 729 putline(buf, mci); 730 } 731 } 732 } 733 if (!printheader) 734 putline("\n", mci); 735 736 /* 737 ** Output transcript of errors 738 */ 739 740 (void) fflush(stdout); 741 p = queuename(e->e_parent, 'x'); 742 if ((xfile = fopen(p, "r")) == NULL) 743 { 744 syserr("Cannot open %s", p); 745 putline(" ----- Transcript of session is unavailable -----\n", mci); 746 } 747 else 748 { 749 putline(" ----- Transcript of session follows -----\n", mci); 750 if (e->e_xfp != NULL) 751 (void) fflush(e->e_xfp); 752 while (fgets(buf, sizeof buf, xfile) != NULL) 753 putline(buf, mci); 754 (void) xfclose(xfile, "errbody xscript", p); 755 } 756 errno = 0; 757 758 #ifdef DSN 759 /* 760 ** Output machine-readable version. 761 */ 762 763 if (e->e_msgboundary != NULL) 764 { 765 putline("", mci); 766 (void) sprintf(buf, "--%s", e->e_msgboundary); 767 putline(buf, mci); 768 putline("Content-Type: message/X-delivery-status-2 (Draft of 20 January 1995)", mci); 769 putline("", mci); 770 771 /* 772 ** Output per-message information. 773 */ 774 775 /* original envelope id from MAIL FROM: line */ 776 if (e->e_parent->e_envid != NULL) 777 { 778 (void) sprintf(buf, "Original-Envelope-Id: %s", 779 xtextify(e->e_parent->e_envid)); 780 putline(buf, mci); 781 } 782 783 /* Reporting-MTA: is us (required) */ 784 p = e->e_parent->e_from.q_mailer->m_mtatype; 785 if (p == NULL) 786 p = "dns"; 787 (void) sprintf(buf, "Reporting-MTA: %s; %s", p, MyHostName); 788 putline(buf, mci); 789 790 /* Received-From-MTA: shows where we got this message from */ 791 if (RealHostName != NULL) 792 { 793 /* XXX use $s for type? */ 794 p = e->e_parent->e_from.q_mailer->m_mtatype; 795 if (p == NULL) 796 p = "dns"; 797 (void) sprintf(buf, "Received-From-MTA: %s; %s", 798 p, RealHostName); 799 putline(buf, mci); 800 } 801 802 /* Arrival-Date: -- when it arrived here */ 803 (void) sprintf(buf, "Arrival-Date: %s", 804 arpadate(ctime(&e->e_parent->e_ctime))); 805 putline(buf, mci); 806 807 /* 808 ** Output per-address information. 809 */ 810 811 for (q = e->e_parent->e_sendqueue; q != NULL; q = q->q_next) 812 { 813 register ADDRESS *r; 814 815 if (!bitset(QBADADDR|QREPORT|QRELAYED, q->q_flags)) 816 continue; 817 putline("", mci); 818 819 /* Original-Recipient: -- passed from on high */ 820 if (q->q_orcpt != NULL) 821 { 822 (void) sprintf(buf, "Original-Recipient: %s", 823 xtextify(q->q_orcpt)); 824 putline(buf, mci); 825 } 826 827 /* Final-Recipient: -- the name from the RCPT command */ 828 p = e->e_parent->e_from.q_mailer->m_addrtype; 829 if (p == NULL) 830 p = "rfc822"; 831 for (r = q; r->q_alias != NULL; r = r->q_alias) 832 continue; 833 if (strchr(r->q_user, '@') == NULL) 834 (void) sprintf(buf, "Final-Recipient: %s; %s@%s", 835 p, xtextify(r->q_user), MyHostName); 836 else 837 (void) sprintf(buf, "Final-Recipient: %s; %s", 838 p, xtextify(r->q_user)); 839 putline(buf, mci); 840 841 /* Action: -- what happened? */ 842 if (bitset(QBADADDR, q->q_flags)) 843 putline("Action: failure", mci); 844 else if (bitset(QQUEUEUP, q->q_flags)) 845 putline("Action: delayed", mci); 846 else if (bitset(QRELAYED, q->q_flags)) 847 putline("Action: relayed", mci); 848 else 849 putline("Action: delivered", mci); 850 851 /* Status: -- what _really_ happened? */ 852 strcpy(buf, "Status: "); 853 if (q->q_status != NULL) 854 strcat(buf, q->q_status); 855 else if (bitset(QBADADDR, q->q_flags)) 856 strcat(buf, "5.0.0"); 857 else if (bitset(QQUEUEUP, q->q_flags)) 858 strcat(buf, "4.0.0"); 859 else if (bitset(QRELAYED, q->q_flags)) 860 strcat(buf, "6.0.1"); 861 else 862 strcat(buf, "2.0.0"); 863 putline(buf, mci); 864 865 /* Remote-MTA: -- who was I talking to? */ 866 p = q->q_mailer->m_mtatype; 867 if (p == NULL) 868 p = "dns"; 869 (void) sprintf(buf, "Remote-MTA: %s; ", p); 870 if (q->q_statmta != NULL) 871 p = q->q_statmta; 872 else if (q->q_host != NULL) 873 p = q->q_host; 874 else 875 p = NULL; 876 if (p != NULL) 877 { 878 strcat(buf, p); 879 p = &buf[strlen(buf) - 1]; 880 if (*p == '.') 881 *p = '\0'; 882 putline(buf, mci); 883 } 884 885 /* Diagnostic-Code: -- actual result from other end */ 886 if (q->q_rstatus != NULL) 887 { 888 p = q->q_mailer->m_diagtype; 889 if (p == NULL) 890 p = "smtp"; 891 (void) sprintf(buf, "Diagnostic-Code: %s; %s", 892 p, q->q_rstatus); 893 putline(buf, mci); 894 } 895 896 /* Last-Attempt-Date: -- fine granularity */ 897 if (q->q_statdate == (time_t) 0L) 898 q->q_statdate = curtime(); 899 (void) sprintf(buf, "Last-Attempt-Date: %s", 900 arpadate(ctime(&q->q_statdate))); 901 putline(buf, mci); 902 903 /* Expiry-Date: -- for delayed messages only */ 904 if (bitset(QQUEUEUP, q->q_flags) && 905 !bitset(QBADADDR, q->q_flags)) 906 { 907 time_t xdate; 908 909 xdate = e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass]; 910 sprintf(buf, "Expiry-Date: %s", 911 arpadate(ctime(&xdate))); 912 putline(buf, mci); 913 } 914 } 915 } 916 #endif 917 918 /* 919 ** Output text of original message 920 */ 921 922 if (bitset(EF_NORETURN, e->e_parent->e_flags)) 923 SendBody = FALSE; 924 putline("", mci); 925 if (e->e_parent->e_df != NULL) 926 { 927 if (e->e_msgboundary == NULL) 928 { 929 if (SendBody) 930 putline(" ----- Original message follows -----\n", mci); 931 else 932 putline(" ----- Message header follows -----\n", mci); 933 (void) fflush(mci->mci_out); 934 } 935 else 936 { 937 (void) sprintf(buf, "--%s", e->e_msgboundary); 938 putline(buf, mci); 939 (void) sprintf(buf, "Content-Type: message/rfc822%s", 940 mci, SendBody ? "" : "-headers"); 941 putline(buf, mci); 942 } 943 putline("", mci); 944 putheader(mci, e->e_parent->e_header, e->e_parent); 945 if (SendBody) 946 putbody(mci, e->e_parent, e->e_msgboundary); 947 else if (e->e_msgboundary == NULL) 948 { 949 putline("", mci); 950 putline(" ----- Message body suppressed -----", mci); 951 } 952 } 953 else if (e->e_msgboundary == NULL) 954 { 955 putline(" ----- No message was collected -----\n", mci); 956 } 957 958 if (e->e_msgboundary != NULL) 959 { 960 putline("", mci); 961 (void) sprintf(buf, "--%s--", e->e_msgboundary); 962 putline(buf, mci); 963 } 964 putline("", mci); 965 966 /* 967 ** Cleanup and exit 968 */ 969 970 if (errno != 0) 971 syserr("errbody: I/O error"); 972 } 973 /* 974 ** SMTPTODSN -- convert SMTP to DSN status code 975 ** 976 ** Parameters: 977 ** smtpstat -- the smtp status code (e.g., 550). 978 ** 979 ** Returns: 980 ** The DSN version of the status code. 981 */ 982 983 char * 984 smtptodsn(smtpstat) 985 int smtpstat; 986 { 987 switch (smtpstat) 988 { 989 case 450: /* Req mail action not taken: mailbox unavailable */ 990 return "4.2.0"; 991 992 case 451: /* Req action aborted: local error in processing */ 993 return "4.3.0"; 994 995 case 452: /* Req action not taken: insufficient sys storage */ 996 return "4.3.1"; 997 998 case 500: /* Syntax error, command unrecognized */ 999 return "5.5.2"; 1000 1001 case 501: /* Syntax error in parameters or arguments */ 1002 return "5.5.4"; 1003 1004 case 502: /* Command not implemented */ 1005 return "5.5.1"; 1006 1007 case 503: /* Bad sequence of commands */ 1008 return "5.5.1"; 1009 1010 case 504: /* Command parameter not implemented */ 1011 return "5.5.4"; 1012 1013 case 550: /* Req mail action not taken: mailbox unavailable */ 1014 return "5.2.0"; 1015 1016 case 551: /* User not local; please try <...> */ 1017 return "5.1.6"; 1018 1019 case 552: /* Req mail action aborted: exceeded storage alloc */ 1020 return "5.2.2"; 1021 1022 case 553: /* Req action not taken: mailbox name not allowed */ 1023 return "5.1.3"; 1024 1025 case 554: /* Transaction failed */ 1026 return "5.0.0"; 1027 } 1028 1029 if ((smtpstat / 100) == 2) 1030 return "2.0.0"; 1031 if ((smtpstat / 100) == 4) 1032 return "4.0.0"; 1033 return "5.0.0"; 1034 } 1035 /* 1036 ** XTEXTIFY -- take regular text and turn it into DSN-style xtext 1037 ** 1038 ** Parameters: 1039 ** t -- the text to convert. 1040 ** 1041 ** Returns: 1042 ** The xtext-ified version of the same string. 1043 */ 1044 1045 char * 1046 xtextify(t) 1047 register char *t; 1048 { 1049 register char *p; 1050 int l; 1051 int nbogus; 1052 static char *bp = NULL; 1053 static int bplen = 0; 1054 1055 /* figure out how long this xtext will have to be */ 1056 nbogus = l = 0; 1057 for (p = t; *p != '\0'; p++) 1058 { 1059 register int c = (*p & 0xff); 1060 1061 /* ASCII dependence here -- this is the way the spec words it */ 1062 if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(') 1063 nbogus++; 1064 l++; 1065 } 1066 if (nbogus == 0) 1067 return t; 1068 l += nbogus * 2 + 1; 1069 1070 /* now allocate space if necessary for the new string */ 1071 if (l > bplen) 1072 { 1073 if (bp != NULL) 1074 free(bp); 1075 bp = xalloc(l); 1076 bplen = l; 1077 } 1078 1079 /* ok, copy the text with byte expansion */ 1080 for (p = bp; *t != '\0'; ) 1081 { 1082 register int c = (*t++ & 0xff); 1083 1084 /* ASCII dependence here -- this is the way the spec words it */ 1085 if (c < '!' || c > '~' || c == '+' || c == '\\' || c == '(') 1086 { 1087 *p++ = '+'; 1088 *p++ = "0123456789abcdef"[c >> 4]; 1089 *p++ = "0123456789abcdef"[c & 0xf]; 1090 } 1091 else 1092 *p++ = c; 1093 } 1094 *p = '\0'; 1095 return bp; 1096 } 1097 /* 1098 ** PRUNEROUTE -- prune an RFC-822 source route 1099 ** 1100 ** Trims down a source route to the last internet-registered hop. 1101 ** This is encouraged by RFC 1123 section 5.3.3. 1102 ** 1103 ** Parameters: 1104 ** addr -- the address 1105 ** 1106 ** Returns: 1107 ** TRUE -- address was modified 1108 ** FALSE -- address could not be pruned 1109 ** 1110 ** Side Effects: 1111 ** modifies addr in-place 1112 */ 1113 1114 pruneroute(addr) 1115 char *addr; 1116 { 1117 #if NAMED_BIND 1118 char *start, *at, *comma; 1119 char c; 1120 int rcode; 1121 char hostbuf[BUFSIZ]; 1122 char *mxhosts[MAXMXHOSTS + 1]; 1123 1124 /* check to see if this is really a route-addr */ 1125 if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>') 1126 return FALSE; 1127 start = strchr(addr, ':'); 1128 at = strrchr(addr, '@'); 1129 if (start == NULL || at == NULL || at < start) 1130 return FALSE; 1131 1132 /* slice off the angle brackets */ 1133 strcpy(hostbuf, at + 1); 1134 hostbuf[strlen(hostbuf) - 1] = '\0'; 1135 1136 while (start) 1137 { 1138 if (getmxrr(hostbuf, mxhosts, FALSE, &rcode) > 0) 1139 { 1140 strcpy(addr + 1, start + 1); 1141 return TRUE; 1142 } 1143 c = *start; 1144 *start = '\0'; 1145 comma = strrchr(addr, ','); 1146 if (comma && comma[1] == '@') 1147 strcpy(hostbuf, comma + 2); 1148 else 1149 comma = 0; 1150 *start = c; 1151 start = comma; 1152 } 1153 #endif 1154 return FALSE; 1155 } 1156