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