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