1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988 Regents of the University of California. 4 * All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 #ifndef lint 10 static char sccsid[] = "@(#)savemail.c 6.29 (Berkeley) 04/05/93"; 11 #endif /* not lint */ 12 13 # include <pwd.h> 14 # include "sendmail.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 ** 27 ** Returns: 28 ** none 29 ** 30 ** Side Effects: 31 ** Saves the letter, by writing or mailing it back to the 32 ** sender, or by putting it in dead.letter in her home 33 ** directory. 34 */ 35 36 /* defines for state machine */ 37 # define ESM_REPORT 0 /* report to sender's terminal */ 38 # define ESM_MAIL 1 /* mail back to sender */ 39 # define ESM_QUIET 2 /* messages have already been returned */ 40 # define ESM_DEADLETTER 3 /* save in ~/dead.letter */ 41 # define ESM_POSTMASTER 4 /* return to postmaster */ 42 # define ESM_USRTMP 5 /* save in /usr/tmp/dead.letter */ 43 # define ESM_PANIC 6 /* leave the locked queue/transcript files */ 44 # define ESM_DONE 7 /* the message is successfully delivered */ 45 46 47 savemail(e) 48 register ENVELOPE *e; 49 { 50 register struct passwd *pw; 51 register FILE *fp; 52 int state; 53 auto ADDRESS *q; 54 char buf[MAXLINE+1]; 55 extern struct passwd *getpwnam(); 56 register char *p; 57 extern char *ttypath(); 58 typedef int (*fnptr)(); 59 60 if (tTd(6, 1)) 61 { 62 printf("\nsavemail, errormode = %c\n e_from=", e->e_errormode); 63 printaddr(&e->e_from, FALSE); 64 } 65 66 if (bitset(EF_RESPONSE, e->e_flags)) 67 return; 68 e->e_flags &= ~EF_FATALERRS; 69 70 /* 71 ** In the unhappy event we don't know who to return the mail 72 ** to, make someone up. 73 */ 74 75 if (e->e_from.q_paddr == NULL) 76 { 77 e->e_sender = "Postmaster"; 78 if (parseaddr(e->e_sender, &e->e_from, 0, '\0', NULL, e) == NULL) 79 { 80 syserr("553 Cannot parse Postmaster!"); 81 ExitStat = EX_SOFTWARE; 82 finis(); 83 } 84 } 85 e->e_to = NULL; 86 87 /* 88 ** Basic state machine. 89 ** 90 ** This machine runs through the following states: 91 ** 92 ** ESM_QUIET Errors have already been printed iff the 93 ** sender is local. 94 ** ESM_REPORT Report directly to the sender's terminal. 95 ** ESM_MAIL Mail response to the sender. 96 ** ESM_DEADLETTER Save response in ~/dead.letter. 97 ** ESM_POSTMASTER Mail response to the postmaster. 98 ** ESM_PANIC Save response anywhere possible. 99 */ 100 101 /* determine starting state */ 102 switch (e->e_errormode) 103 { 104 case EM_WRITE: 105 state = ESM_REPORT; 106 break; 107 108 case EM_BERKNET: 109 /* mail back, but return o.k. exit status */ 110 ExitStat = EX_OK; 111 112 /* fall through.... */ 113 114 case EM_MAIL: 115 state = ESM_MAIL; 116 break; 117 118 case EM_PRINT: 119 case '\0': 120 state = ESM_QUIET; 121 break; 122 123 case EM_QUIET: 124 /* no need to return anything at all */ 125 return; 126 127 default: 128 syserr("554 savemail: bogus errormode x%x\n", e->e_errormode); 129 state = ESM_MAIL; 130 break; 131 } 132 133 while (state != ESM_DONE) 134 { 135 if (tTd(6, 5)) 136 printf(" state %d\n", state); 137 138 switch (state) 139 { 140 case ESM_QUIET: 141 if (e->e_from.q_mailer == LocalMailer) 142 state = ESM_DEADLETTER; 143 else 144 state = ESM_MAIL; 145 break; 146 147 case ESM_REPORT: 148 149 /* 150 ** If the user is still logged in on the same terminal, 151 ** then write the error messages back to hir (sic). 152 */ 153 154 p = ttypath(); 155 if (p == NULL || freopen(p, "w", stdout) == NULL) 156 { 157 state = ESM_MAIL; 158 break; 159 } 160 161 expand("\201n", buf, &buf[sizeof buf - 1], e); 162 printf("\r\nMessage from %s...\r\n", buf); 163 printf("Errors occurred while sending mail.\r\n"); 164 if (e->e_xfp != NULL) 165 { 166 (void) fflush(e->e_xfp); 167 fp = fopen(queuename(e, 'x'), "r"); 168 } 169 else 170 fp = NULL; 171 if (fp == NULL) 172 { 173 syserr("Cannot open %s", queuename(e, 'x')); 174 printf("Transcript of session is unavailable.\r\n"); 175 } 176 else 177 { 178 printf("Transcript follows:\r\n"); 179 while (fgets(buf, sizeof buf, fp) != NULL && 180 !ferror(stdout)) 181 fputs(buf, stdout); 182 (void) xfclose(fp, "savemail transcript", e->e_id); 183 } 184 printf("Original message will be saved in dead.letter.\r\n"); 185 state = ESM_DEADLETTER; 186 break; 187 188 case ESM_MAIL: 189 /* 190 ** If mailing back, do it. 191 ** Throw away all further output. Don't alias, 192 ** since this could cause loops, e.g., if joe 193 ** mails to joe@x, and for some reason the network 194 ** for @x is down, then the response gets sent to 195 ** joe@x, which gives a response, etc. Also force 196 ** the mail to be delivered even if a version of 197 ** it has already been sent to the sender. 198 */ 199 200 if (strcmp(e->e_from.q_paddr, "<>") != 0) 201 (void) sendtolist(e->e_from.q_paddr, 202 (ADDRESS *) NULL, 203 &e->e_errorqueue, e); 204 205 /* deliver a cc: to the postmaster if desired */ 206 if (PostMasterCopy != NULL) 207 { 208 auto ADDRESS *rlist = NULL; 209 210 (void) sendtolist(PostMasterCopy, 211 (ADDRESS *) NULL, 212 &rlist, e); 213 (void) returntosender(e->e_message, 214 rlist, FALSE, e); 215 } 216 q = e->e_errorqueue; 217 if (q == NULL) 218 { 219 /* this is an error-error */ 220 state = ESM_POSTMASTER; 221 break; 222 } 223 if (returntosender(e->e_message, 224 q, (e->e_class >= 0), e) == 0) 225 { 226 state = ESM_DONE; 227 break; 228 } 229 230 /* didn't work -- return to postmaster */ 231 state = ESM_POSTMASTER; 232 break; 233 234 case ESM_POSTMASTER: 235 /* 236 ** Similar to previous case, but to system postmaster. 237 */ 238 239 if (parseaddr("postmaster", q, 0, '\0', NULL, e) == NULL) 240 { 241 syserr("553 cannot parse postmaster!"); 242 ExitStat = EX_SOFTWARE; 243 state = ESM_USRTMP; 244 break; 245 } 246 if (returntosender(e->e_message, 247 q, (e->e_class >= 0), e) == 0) 248 { 249 state = ESM_DONE; 250 break; 251 } 252 253 /* didn't work -- last resort */ 254 state = ESM_USRTMP; 255 break; 256 257 case ESM_DEADLETTER: 258 /* 259 ** Save the message in dead.letter. 260 ** If we weren't mailing back, and the user is 261 ** local, we should save the message in 262 ** ~/dead.letter so that the poor person doesn't 263 ** have to type it over again -- and we all know 264 ** what poor typists UNIX users are. 265 */ 266 267 p = NULL; 268 if (e->e_from.q_mailer == LocalMailer) 269 { 270 if (e->e_from.q_home != NULL) 271 p = e->e_from.q_home; 272 else if ((pw = getpwnam(e->e_from.q_user)) != NULL) 273 p = pw->pw_dir; 274 } 275 if (p == NULL) 276 { 277 /* no local directory */ 278 state = ESM_MAIL; 279 break; 280 } 281 if (e->e_dfp != NULL) 282 { 283 auto ADDRESS *q; 284 bool oldverb = Verbose; 285 286 /* we have a home directory; open dead.letter */ 287 define('z', p, e); 288 expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e); 289 Verbose = TRUE; 290 message("Saving message in %s", buf); 291 Verbose = oldverb; 292 e->e_to = buf; 293 q = NULL; 294 (void) sendtolist(buf, &e->e_from, &q, e); 295 if (deliver(e, q) == 0) 296 state = ESM_DONE; 297 else 298 state = ESM_MAIL; 299 } 300 else 301 { 302 /* no data file -- try mailing back */ 303 state = ESM_MAIL; 304 } 305 break; 306 307 case ESM_USRTMP: 308 /* 309 ** Log the mail in /usr/tmp/dead.letter. 310 */ 311 312 if (e->e_class < 0) 313 { 314 state = ESM_DONE; 315 break; 316 } 317 318 fp = dfopen("/usr/tmp/dead.letter", "a"); 319 if (fp == NULL) 320 { 321 state = ESM_PANIC; 322 break; 323 } 324 325 putfromline(fp, FileMailer, e); 326 (*e->e_puthdr)(fp, FileMailer, e); 327 putline("\n", fp, FileMailer); 328 (*e->e_putbody)(fp, FileMailer, e); 329 putline("\n", fp, FileMailer); 330 (void) fflush(fp); 331 state = ferror(fp) ? ESM_PANIC : ESM_DONE; 332 (void) xfclose(fp, "savemail", "/usr/tmp/dead.letter"); 333 break; 334 335 default: 336 syserr("554 savemail: unknown state %d", state); 337 338 /* fall through ... */ 339 340 case ESM_PANIC: 341 /* leave the locked queue & transcript files around */ 342 syserr("554 savemail: cannot save rejected email anywhere"); 343 exit(EX_SOFTWARE); 344 } 345 } 346 } 347 /* 348 ** RETURNTOSENDER -- return a message to the sender with an error. 349 ** 350 ** Parameters: 351 ** msg -- the explanatory message. 352 ** returnq -- the queue of people to send the message to. 353 ** sendbody -- if TRUE, also send back the body of the 354 ** message; otherwise just send the header. 355 ** e -- the current envelope. 356 ** 357 ** Returns: 358 ** zero -- if everything went ok. 359 ** else -- some error. 360 ** 361 ** Side Effects: 362 ** Returns the current message to the sender via 363 ** mail. 364 */ 365 366 static bool SendBody; 367 368 #define MAXRETURNS 6 /* max depth of returning messages */ 369 #define ERRORFUDGE 100 /* nominal size of error message text */ 370 371 returntosender(msg, returnq, sendbody, e) 372 char *msg; 373 ADDRESS *returnq; 374 bool sendbody; 375 register ENVELOPE *e; 376 { 377 char buf[MAXNAME]; 378 extern putheader(), errbody(); 379 register ENVELOPE *ee; 380 ENVELOPE *oldcur = CurEnv; 381 extern ENVELOPE *newenvelope(); 382 ENVELOPE errenvelope; 383 static int returndepth; 384 register ADDRESS *q; 385 386 if (msg == NULL) 387 msg = "Unable to deliver mail"; 388 389 if (tTd(6, 1)) 390 { 391 printf("Return To Sender: msg=\"%s\", depth=%d, e=%x, returnq=", 392 msg, returndepth, e); 393 printaddr(returnq, TRUE); 394 } 395 396 if (++returndepth >= MAXRETURNS) 397 { 398 if (returndepth != MAXRETURNS) 399 syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr); 400 /* don't "unrecurse" and fake a clean exit */ 401 /* returndepth--; */ 402 return (0); 403 } 404 405 SendBody = sendbody; 406 define('g', e->e_from.q_paddr, e); 407 ee = newenvelope(&errenvelope, e); 408 define('a', "\201b", ee); 409 ee->e_puthdr = putheader; 410 ee->e_putbody = errbody; 411 ee->e_flags |= EF_RESPONSE; 412 if (!bitset(EF_OLDSTYLE, e->e_flags)) 413 ee->e_flags &= ~EF_OLDSTYLE; 414 ee->e_sendqueue = returnq; 415 ee->e_msgsize = e->e_msgsize + ERRORFUDGE; 416 openxscript(ee); 417 for (q = returnq; q != NULL; q = q->q_next) 418 { 419 if (bitset(QDONTSEND, q->q_flags)) 420 continue; 421 422 ee->e_nrcpts++; 423 424 if (!DontPruneRoutes && pruneroute(q->q_paddr)) 425 parseaddr(q->q_paddr, q, 0, '\0', NULL, e); 426 427 if (q->q_alias == NULL) 428 addheader("to", q->q_paddr, ee); 429 } 430 431 # ifdef LOG 432 if (LogLevel > 5) 433 syslog(LOG_INFO, "%s: %s: return to sender: %s", 434 e->e_id, ee->e_id, msg); 435 # endif 436 437 (void) sprintf(buf, "Returned mail: %s", msg); 438 addheader("subject", buf, ee); 439 440 /* fake up an address header for the from person */ 441 expand("\201n", buf, &buf[sizeof buf - 1], e); 442 if (parseaddr(buf, &ee->e_from, 1, '\0', NULL, e) == NULL) 443 { 444 syserr("553 Can't parse myself!"); 445 ExitStat = EX_SOFTWARE; 446 returndepth--; 447 return (-1); 448 } 449 ee->e_sender = ee->e_from.q_paddr; 450 451 /* push state into submessage */ 452 CurEnv = ee; 453 define('f', "\201n", ee); 454 define('x', "Mail Delivery Subsystem", ee); 455 eatheader(ee, TRUE); 456 457 /* actually deliver the error message */ 458 sendall(ee, SM_DEFAULT); 459 460 /* restore state */ 461 dropenvelope(ee); 462 CurEnv = oldcur; 463 returndepth--; 464 465 /* should check for delivery errors here */ 466 return (0); 467 } 468 /* 469 ** ERRBODY -- output the body of an error message. 470 ** 471 ** Typically this is a copy of the transcript plus a copy of the 472 ** original offending message. 473 ** 474 ** Parameters: 475 ** fp -- the output file. 476 ** m -- the mailer to output to. 477 ** e -- the envelope we are working in. 478 ** 479 ** Returns: 480 ** none 481 ** 482 ** Side Effects: 483 ** Outputs the body of an error message. 484 */ 485 486 errbody(fp, m, e) 487 register FILE *fp; 488 register struct mailer *m; 489 register ENVELOPE *e; 490 { 491 register FILE *xfile; 492 char buf[MAXLINE]; 493 char *p; 494 495 if (e->e_parent == NULL) 496 { 497 syserr("errbody: null parent"); 498 putline("\n", fp, m); 499 putline(" ----- Original message lost -----\n", fp, m); 500 return; 501 } 502 503 /* 504 ** Output error message header (if specified and available). 505 */ 506 507 if (ErrMsgFile != NULL) 508 { 509 if (*ErrMsgFile == '/') 510 { 511 xfile = fopen(ErrMsgFile, "r"); 512 if (xfile != NULL) 513 { 514 while (fgets(buf, sizeof buf, xfile) != NULL) 515 { 516 expand(buf, buf, &buf[sizeof buf - 1], e); 517 putline(buf, fp, m); 518 } 519 (void) fclose(xfile); 520 fprintf(fp, "\n"); 521 } 522 } 523 else 524 { 525 expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e); 526 putline(buf, fp, m); 527 fprintf(fp, "\n"); 528 } 529 } 530 531 /* 532 ** Output transcript of errors 533 */ 534 535 (void) fflush(stdout); 536 p = queuename(e->e_parent, 'x'); 537 if ((xfile = fopen(p, "r")) == NULL) 538 { 539 syserr("Cannot open %s", p); 540 putline(" ----- Transcript of session is unavailable -----\n", fp, m); 541 } 542 else 543 { 544 putline(" ----- Transcript of session follows -----\n", fp, m); 545 if (e->e_xfp != NULL) 546 (void) fflush(e->e_xfp); 547 while (fgets(buf, sizeof buf, xfile) != NULL) 548 putline(buf, fp, m); 549 (void) xfclose(xfile, "errbody xscript", p); 550 } 551 errno = 0; 552 553 /* 554 ** Output text of original message 555 */ 556 557 if (NoReturn) 558 SendBody = FALSE; 559 if (e->e_parent->e_df != NULL) 560 { 561 if (SendBody) 562 { 563 putline("\n", fp, m); 564 putline(" ----- Unsent message follows -----\n", fp, m); 565 (void) fflush(fp); 566 putheader(fp, m, e->e_parent); 567 putline("\n", fp, m); 568 putbody(fp, m, e->e_parent); 569 } 570 else 571 { 572 putline("\n", fp, m); 573 putline(" ----- Message header follows -----\n", fp, m); 574 (void) fflush(fp); 575 putheader(fp, m, e->e_parent); 576 } 577 } 578 else 579 { 580 putline("\n", fp, m); 581 putline(" ----- No message was collected -----\n", fp, m); 582 putline("\n", fp, m); 583 } 584 585 /* 586 ** Cleanup and exit 587 */ 588 589 if (errno != 0) 590 syserr("errbody: I/O error"); 591 } 592 /* 593 ** PRUNEROUTE -- prune an RFC-822 source route 594 ** 595 ** Trims down a source route to the last internet-registered hop. 596 ** This is encouraged by RFC 1123 section 5.3.3. 597 ** 598 ** Parameters: 599 ** addr -- the address 600 ** 601 ** Returns: 602 ** TRUE -- address was modified 603 ** FALSE -- address could not be pruned 604 ** 605 ** Side Effects: 606 ** modifies addr in-place 607 */ 608 609 pruneroute(addr) 610 char *addr; 611 { 612 #ifdef NAMED_BIND 613 char *start, *at, *comma; 614 char c; 615 int rcode; 616 char hostbuf[BUFSIZ]; 617 char *mxhosts[MAXMXHOSTS + 1]; 618 619 /* check to see if this is really a route-addr */ 620 if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>') 621 return FALSE; 622 start = strchr(addr, ':'); 623 at = strrchr(addr, '@'); 624 if (start == NULL || at == NULL || at < start) 625 return FALSE; 626 627 /* slice off the angle brackets */ 628 strcpy(hostbuf, at + 1); 629 hostbuf[strlen(hostbuf) - 1] = '\0'; 630 631 while (start) 632 { 633 if (getmxrr(hostbuf, mxhosts, "", &rcode) > 0) 634 { 635 strcpy(addr + 1, start + 1); 636 return TRUE; 637 } 638 c = *start; 639 *start = '\0'; 640 comma = strrchr(addr, ','); 641 if (comma && comma[1] == '@') 642 strcpy(hostbuf, comma + 2); 643 else 644 comma = 0; 645 *start = c; 646 start = comma; 647 } 648 #endif 649 return FALSE; 650 } 651