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.15 (Berkeley) 02/28/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 char *rpath; 204 205 if (e->e_returnpath != e->e_sender) 206 rpath = e->e_returnpath; 207 else 208 rpath = e->e_from.q_paddr; 209 if (strcmp(rpath, "<>") != 0) 210 (void) sendtolist(rpath, 211 (ADDRESS *) NULL, 212 &e->e_errorqueue, e); 213 214 /* deliver a cc: to the postmaster if desired */ 215 if (PostMasterCopy != NULL) 216 (void) sendtolist(PostMasterCopy, 217 (ADDRESS *) NULL, 218 &e->e_errorqueue, e); 219 q = e->e_errorqueue; 220 if (q == NULL) 221 { 222 /* this is an error-error */ 223 state = ESM_USRTMP; 224 break; 225 } 226 } 227 else 228 { 229 if (parseaddr("postmaster", q, 0, '\0', e) == NULL) 230 { 231 syserr("553 cannot parse postmaster!"); 232 ExitStat = EX_SOFTWARE; 233 state = ESM_USRTMP; 234 break; 235 } 236 } 237 if (returntosender(e->e_message != NULL ? e->e_message : 238 "Unable to deliver mail", 239 q, (e->e_class >= 0), e) == 0) 240 { 241 state = ESM_DONE; 242 break; 243 } 244 245 state = state == ESM_MAIL ? ESM_POSTMASTER : ESM_USRTMP; 246 break; 247 248 case ESM_DEADLETTER: 249 /* 250 ** Save the message in dead.letter. 251 ** If we weren't mailing back, and the user is 252 ** local, we should save the message in 253 ** ~/dead.letter so that the poor person doesn't 254 ** have to type it over again -- and we all know 255 ** what poor typists UNIX users are. 256 */ 257 258 p = NULL; 259 if (e->e_from.q_mailer == LocalMailer) 260 { 261 if (e->e_from.q_home != NULL) 262 p = e->e_from.q_home; 263 else if ((pw = getpwnam(e->e_from.q_user)) != NULL) 264 p = pw->pw_dir; 265 } 266 if (p == NULL) 267 { 268 syserr("554 Can't return mail to %s", e->e_from.q_paddr); 269 state = ESM_MAIL; 270 break; 271 } 272 if (e->e_dfp != NULL) 273 { 274 auto ADDRESS *q; 275 bool oldverb = Verbose; 276 277 /* we have a home directory; open dead.letter */ 278 define('z', p, e); 279 expand("\201z/dead.letter", buf, &buf[sizeof buf - 1], e); 280 Verbose = TRUE; 281 message("Saving message in %s", buf); 282 Verbose = oldverb; 283 e->e_to = buf; 284 q = NULL; 285 (void) sendtolist(buf, &e->e_from, &q, e); 286 if (deliver(e, q) == 0) 287 state = ESM_DONE; 288 else 289 state = ESM_MAIL; 290 } 291 else 292 { 293 /* no data file -- try mailing back */ 294 state = ESM_MAIL; 295 } 296 break; 297 298 case ESM_USRTMP: 299 /* 300 ** Log the mail in /usr/tmp/dead.letter. 301 */ 302 303 if (e->e_class < 0) 304 { 305 state = ESM_DONE; 306 break; 307 } 308 309 fp = dfopen("/usr/tmp/dead.letter", "a"); 310 if (fp == NULL) 311 { 312 state = ESM_PANIC; 313 break; 314 } 315 316 putfromline(fp, FileMailer, e); 317 (*e->e_puthdr)(fp, FileMailer, e); 318 putline("\n", fp, FileMailer); 319 (*e->e_putbody)(fp, FileMailer, e); 320 putline("\n", fp, FileMailer); 321 (void) fflush(fp); 322 state = ferror(fp) ? ESM_PANIC : ESM_DONE; 323 (void) fclose(fp); 324 break; 325 326 default: 327 syserr("554 savemail: unknown state %d", state); 328 329 /* fall through ... */ 330 331 case ESM_PANIC: 332 /* leave the locked queue & transcript files around */ 333 syserr("554 savemail: cannot save rejected email anywhere"); 334 exit(EX_SOFTWARE); 335 } 336 } 337 } 338 /* 339 ** RETURNTOSENDER -- return a message to the sender with an error. 340 ** 341 ** Parameters: 342 ** msg -- the explanatory message. 343 ** returnq -- the queue of people to send the message to. 344 ** sendbody -- if TRUE, also send back the body of the 345 ** message; otherwise just send the header. 346 ** e -- the current envelope. 347 ** 348 ** Returns: 349 ** zero -- if everything went ok. 350 ** else -- some error. 351 ** 352 ** Side Effects: 353 ** Returns the current message to the sender via 354 ** mail. 355 */ 356 357 static bool SendBody; 358 359 #define MAXRETURNS 6 /* max depth of returning messages */ 360 361 returntosender(msg, returnq, sendbody, e) 362 char *msg; 363 ADDRESS *returnq; 364 bool sendbody; 365 register ENVELOPE *e; 366 { 367 char buf[MAXNAME]; 368 extern putheader(), errbody(); 369 register ENVELOPE *ee; 370 extern ENVELOPE *newenvelope(); 371 ENVELOPE errenvelope; 372 static int returndepth; 373 register ADDRESS *q; 374 375 if (tTd(6, 1)) 376 { 377 printf("Return To Sender: msg=\"%s\", depth=%d, e=%x,\n", 378 msg, returndepth, e); 379 printf("\treturnq="); 380 printaddr(returnq, TRUE); 381 } 382 383 if (++returndepth >= MAXRETURNS) 384 { 385 if (returndepth != MAXRETURNS) 386 syserr("554 returntosender: infinite recursion on %s", returnq->q_paddr); 387 /* don't "unrecurse" and fake a clean exit */ 388 /* returndepth--; */ 389 return (0); 390 } 391 392 SendBody = sendbody; 393 define('g', e->e_sender, e); 394 define('<', e->e_returnpath, e); 395 ee = newenvelope(&errenvelope, e); 396 define('a', "\201b", ee); 397 ee->e_puthdr = putheader; 398 ee->e_putbody = errbody; 399 ee->e_flags |= EF_RESPONSE; 400 if (!bitset(EF_OLDSTYLE, e->e_flags)) 401 ee->e_flags &= ~EF_OLDSTYLE; 402 ee->e_sendqueue = returnq; 403 openxscript(ee); 404 for (q = returnq; q != NULL; q = q->q_next) 405 { 406 if (!DontPruneRoutes && pruneroute(q->q_paddr)) 407 parseaddr(q->q_paddr, q, 0, '\0', e); 408 409 if (q->q_alias == NULL) 410 addheader("to", q->q_paddr, ee); 411 } 412 413 # ifdef LOG 414 if (LogLevel > 5) 415 syslog(LOG_INFO, "%s: %s: return to sender: %s", 416 e->e_id, ee->e_id, msg); 417 # endif 418 419 (void) sprintf(buf, "Returned mail: %s", msg); 420 addheader("subject", buf, ee); 421 422 /* fake up an address header for the from person */ 423 expand("\201n", buf, &buf[sizeof buf - 1], e); 424 ee->e_sender = newstr(buf); 425 if (ConfigLevel >= 4) 426 ee->e_returnpath = "<>"; 427 else 428 ee->e_returnpath = ee->e_sender; 429 if (parseaddr(buf, &ee->e_from, -1, '\0', e) == NULL) 430 { 431 syserr("553 Can't parse myself!"); 432 ExitStat = EX_SOFTWARE; 433 returndepth--; 434 return (-1); 435 } 436 loweraddr(&ee->e_from); 437 438 /* push state into submessage */ 439 CurEnv = ee; 440 define('f', "\201n", ee); 441 define('x', "Mail Delivery Subsystem", ee); 442 eatheader(ee, FALSE); 443 444 /* actually deliver the error message */ 445 sendall(ee, SM_DEFAULT); 446 447 /* restore state */ 448 dropenvelope(ee); 449 CurEnv = CurEnv->e_parent; 450 returndepth--; 451 452 /* should check for delivery errors here */ 453 return (0); 454 } 455 /* 456 ** ERRBODY -- output the body of an error message. 457 ** 458 ** Typically this is a copy of the transcript plus a copy of the 459 ** original offending message. 460 ** 461 ** Parameters: 462 ** fp -- the output file. 463 ** m -- the mailer to output to. 464 ** e -- the envelope we are working in. 465 ** 466 ** Returns: 467 ** none 468 ** 469 ** Side Effects: 470 ** Outputs the body of an error message. 471 */ 472 473 errbody(fp, m, e) 474 register FILE *fp; 475 register struct mailer *m; 476 register ENVELOPE *e; 477 { 478 register FILE *xfile; 479 char buf[MAXLINE]; 480 char *p; 481 482 /* 483 ** Output error message header (if specified and available). 484 */ 485 486 if (ErrMsgFile != NULL) 487 { 488 if (*ErrMsgFile == '/') 489 { 490 xfile = fopen(ErrMsgFile, "r"); 491 if (xfile != NULL) 492 { 493 while (fgets(buf, sizeof buf, xfile) != NULL) 494 { 495 expand(buf, buf, &buf[sizeof buf - 1], e); 496 putline(buf, fp, m); 497 } 498 (void) fclose(xfile); 499 fprintf(fp, "\n"); 500 } 501 } 502 else 503 { 504 expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e); 505 putline(buf, fp, m); 506 fprintf(fp, "\n"); 507 } 508 } 509 510 /* 511 ** Output transcript of errors 512 */ 513 514 (void) fflush(stdout); 515 p = queuename(e->e_parent, 'x'); 516 if ((xfile = fopen(p, "r")) == NULL) 517 { 518 syserr("Cannot open %s", p); 519 fprintf(fp, " ----- Transcript of session is unavailable -----\n"); 520 } 521 else 522 { 523 fprintf(fp, " ----- Transcript of session follows -----\n"); 524 if (e->e_xfp != NULL) 525 (void) fflush(e->e_xfp); 526 while (fgets(buf, sizeof buf, xfile) != NULL) 527 putline(buf, fp, m); 528 (void) fclose(xfile); 529 } 530 errno = 0; 531 532 /* 533 ** Output text of original message 534 */ 535 536 if (NoReturn) 537 fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 538 else if (e->e_parent->e_dfp != NULL) 539 { 540 if (SendBody) 541 { 542 putline("\n", fp, m); 543 putline(" ----- Unsent message follows -----\n", fp, m); 544 (void) fflush(fp); 545 putheader(fp, m, e->e_parent); 546 putline("\n", fp, m); 547 putbody(fp, m, e->e_parent); 548 } 549 else 550 { 551 putline("\n", fp, m); 552 putline(" ----- Message header follows -----\n", fp, m); 553 (void) fflush(fp); 554 putheader(fp, m, e->e_parent); 555 } 556 } 557 else 558 { 559 putline("\n", fp, m); 560 putline(" ----- No message was collected -----\n", fp, m); 561 putline("\n", fp, m); 562 } 563 564 /* 565 ** Cleanup and exit 566 */ 567 568 if (errno != 0) 569 syserr("errbody: I/O error"); 570 } 571 /* 572 ** PRUNEROUTE -- prune an RFC-822 source route 573 ** 574 ** Trims down a source route to the last internet-registered hop. 575 ** This is encouraged by RFC 1123 section 5.3.3. 576 ** 577 ** Parameters: 578 ** addr -- the address 579 ** 580 ** Returns: 581 ** TRUE -- address was modified 582 ** FALSE -- address could not be pruned 583 ** 584 ** Side Effects: 585 ** modifies addr in-place 586 */ 587 588 pruneroute(addr) 589 char *addr; 590 { 591 #ifdef NAMED_BIND 592 char *start, *at, *comma; 593 char c; 594 int rcode; 595 char hostbuf[BUFSIZ]; 596 char *mxhosts[MAXMXHOSTS + 1]; 597 598 /* check to see if this is really a route-addr */ 599 if (*addr != '<' || addr[1] != '@' || addr[strlen(addr) - 1] != '>') 600 return FALSE; 601 start = strchr(addr, ':'); 602 at = strrchr(addr, '@'); 603 if (start == NULL || at == NULL || at < start) 604 return FALSE; 605 606 /* slice off the angle brackets */ 607 strcpy(hostbuf, at + 1); 608 hostbuf[strlen(hostbuf) - 1] = '\0'; 609 610 while (start) 611 { 612 if (getmxrr(hostbuf, mxhosts, "", &rcode) > 0) 613 { 614 strcpy(addr + 1, start + 1); 615 return TRUE; 616 } 617 c = *start; 618 *start = '\0'; 619 comma = strrchr(addr, ','); 620 if (comma && comma[1] == '@') 621 strcpy(hostbuf, comma + 2); 622 else 623 comma = 0; 624 *start = c; 625 start = comma; 626 } 627 #endif 628 return FALSE; 629 } 630