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.13 (Berkeley) 02/24/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 ForceMail = TRUE; 67 e->e_flags &= ~EF_FATALERRS; 68 69 /* 70 ** In the unhappy event we don't know who to return the mail 71 ** to, make someone up. 72 */ 73 74 if (e->e_from.q_paddr == NULL) 75 { 76 if (parseaddr("root", &e->e_from, 0, '\0', e) == NULL) 77 { 78 syserr("553 Cannot parse root!"); 79 ExitStat = EX_SOFTWARE; 80 finis(); 81 } 82 } 83 e->e_to = NULL; 84 85 /* 86 ** Basic state machine. 87 ** 88 ** This machine runs through the following states: 89 ** 90 ** ESM_QUIET Errors have already been printed iff the 91 ** sender is local. 92 ** ESM_REPORT Report directly to the sender's terminal. 93 ** ESM_MAIL Mail response to the sender. 94 ** ESM_DEADLETTER Save response in ~/dead.letter. 95 ** ESM_POSTMASTER Mail response to the postmaster. 96 ** ESM_PANIC Save response anywhere possible. 97 */ 98 99 /* determine starting state */ 100 switch (ErrorMode) 101 { 102 case EM_WRITE: 103 state = ESM_REPORT; 104 break; 105 106 case EM_BERKNET: 107 /* mail back, but return o.k. exit status */ 108 ExitStat = EX_OK; 109 110 /* fall through.... */ 111 112 case EM_MAIL: 113 state = ESM_MAIL; 114 break; 115 116 case EM_PRINT: 117 case '\0': 118 state = ESM_QUIET; 119 break; 120 121 case EM_QUIET: 122 /* no need to return anything at all */ 123 return; 124 125 default: 126 syserr("554 savemail: ErrorMode x%x\n"); 127 state = ESM_MAIL; 128 break; 129 } 130 131 while (state != ESM_DONE) 132 { 133 if (tTd(6, 5)) 134 printf(" state %d\n", state); 135 136 switch (state) 137 { 138 case ESM_QUIET: 139 if (e->e_from.q_mailer == LocalMailer) 140 state = ESM_DEADLETTER; 141 else 142 state = ESM_MAIL; 143 break; 144 145 case ESM_REPORT: 146 147 /* 148 ** If the user is still logged in on the same terminal, 149 ** then write the error messages back to hir (sic). 150 */ 151 152 p = ttypath(); 153 if (p == NULL || freopen(p, "w", stdout) == NULL) 154 { 155 state = ESM_MAIL; 156 break; 157 } 158 159 expand("\201n", buf, &buf[sizeof buf - 1], e); 160 printf("\r\nMessage from %s...\r\n", buf); 161 printf("Errors occurred while sending mail.\r\n"); 162 if (e->e_xfp != NULL) 163 { 164 (void) fflush(e->e_xfp); 165 fp = fopen(queuename(e, 'x'), "r"); 166 } 167 else 168 fp = NULL; 169 if (fp == NULL) 170 { 171 syserr("Cannot open %s", queuename(e, 'x')); 172 printf("Transcript of session is unavailable.\r\n"); 173 } 174 else 175 { 176 printf("Transcript follows:\r\n"); 177 while (fgets(buf, sizeof buf, fp) != NULL && 178 !ferror(stdout)) 179 fputs(buf, stdout); 180 (void) fclose(fp); 181 } 182 printf("Original message will be saved in dead.letter.\r\n"); 183 state = ESM_DEADLETTER; 184 break; 185 186 case ESM_MAIL: 187 case ESM_POSTMASTER: 188 /* 189 ** If mailing back, do it. 190 ** Throw away all further output. Don't alias, 191 ** since this could cause loops, e.g., if joe 192 ** mails to joe@x, and for some reason the network 193 ** for @x is down, then the response gets sent to 194 ** joe@x, which gives a response, etc. Also force 195 ** the mail to be delivered even if a version of 196 ** it has already been sent to the sender. 197 */ 198 199 if (state == ESM_MAIL) 200 { 201 if (e->e_errorqueue == NULL) 202 { 203 char *rpath; 204 205 if (strcmp(e->e_returnpath, "<>") != 0) 206 rpath = e->e_returnpath; 207 else if (strcmp(e->e_from.q_paddr, "<>") != 0) 208 rpath = e->e_from.q_paddr; 209 else 210 rpath = NULL; 211 if (rpath != NULL) 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