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