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