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