1 /* 2 ** Sendmail 3 ** Copyright (c) 1983 Eric P. Allman 4 ** Berkeley, California 5 ** 6 ** Copyright (c) 1983 Regents of the University of California. 7 ** All rights reserved. The Berkeley software License Agreement 8 ** specifies the terms and conditions for redistribution. 9 */ 10 11 #ifndef lint 12 static char SccsId[] = "@(#)savemail.c 5.5 (Berkeley) 09/21/85"; 13 #endif not lint 14 15 # include <pwd.h> 16 # include "sendmail.h" 17 18 /* 19 ** SAVEMAIL -- Save mail on error 20 ** 21 ** If mailing back errors, mail it back to the originator 22 ** together with an error message; otherwise, just put it in 23 ** dead.letter in the user's home directory (if he exists on 24 ** this machine). 25 ** 26 ** Parameters: 27 ** e -- the envelope containing the message in error. 28 ** 29 ** Returns: 30 ** none 31 ** 32 ** Side Effects: 33 ** Saves the letter, by writing or mailing it back to the 34 ** sender, or by putting it in dead.letter in her home 35 ** directory. 36 */ 37 38 /* defines for state machine */ 39 # define ESM_REPORT 0 /* report to sender's terminal */ 40 # define ESM_MAIL 1 /* mail back to sender */ 41 # define ESM_QUIET 2 /* messages have already been returned */ 42 # define ESM_DEADLETTER 3 /* save in ~/dead.letter */ 43 # define ESM_POSTMASTER 4 /* return to postmaster */ 44 # define ESM_USRTMP 5 /* save in /usr/tmp/dead.letter */ 45 # define ESM_PANIC 6 /* leave the locked queue/transcript files */ 46 # define ESM_DONE 7 /* the message is successfully delivered */ 47 48 49 savemail(e) 50 register ENVELOPE *e; 51 { 52 register struct passwd *pw; 53 register FILE *fp; 54 int state; 55 auto ADDRESS *q; 56 char buf[MAXLINE+1]; 57 extern struct passwd *getpwnam(); 58 register char *p; 59 extern char *ttypath(); 60 typedef int (*fnptr)(); 61 62 # ifdef DEBUG 63 if (tTd(6, 1)) 64 printf("\nsavemail, ErrorMode = %c\n", ErrorMode); 65 # endif DEBUG 66 67 if (bitset(EF_RESPONSE, e->e_flags)) 68 return; 69 if (e->e_class < 0) 70 { 71 message(Arpa_Info, "Dumping junk mail"); 72 return; 73 } 74 ForceMail = TRUE; 75 e->e_flags &= ~EF_FATALERRS; 76 77 /* 78 ** In the unhappy event we don't know who to return the mail 79 ** to, make someone up. 80 */ 81 82 if (e->e_from.q_paddr == NULL) 83 { 84 if (parseaddr("root", &e->e_from, 0, '\0') == NULL) 85 { 86 syserr("Cannot parse root!"); 87 ExitStat = EX_SOFTWARE; 88 finis(); 89 } 90 } 91 e->e_to = NULL; 92 93 /* 94 ** Basic state machine. 95 ** 96 ** This machine runs through the following states: 97 ** 98 ** ESM_QUIET Errors have already been printed iff the 99 ** sender is local. 100 ** ESM_REPORT Report directly to the sender's terminal. 101 ** ESM_MAIL Mail response to the sender. 102 ** ESM_DEADLETTER Save response in ~/dead.letter. 103 ** ESM_POSTMASTER Mail response to the postmaster. 104 ** ESM_PANIC Save response anywhere possible. 105 */ 106 107 /* determine starting state */ 108 switch (ErrorMode) 109 { 110 case EM_WRITE: 111 state = ESM_REPORT; 112 break; 113 114 case EM_BERKNET: 115 /* mail back, but return o.k. exit status */ 116 ExitStat = EX_OK; 117 118 /* fall through.... */ 119 120 case EM_MAIL: 121 state = ESM_MAIL; 122 break; 123 124 case EM_PRINT: 125 case '\0': 126 state = ESM_QUIET; 127 break; 128 129 case EM_QUIET: 130 /* no need to return anything at all */ 131 return; 132 133 default: 134 syserr("savemail: ErrorMode x%x\n"); 135 state = ESM_MAIL; 136 break; 137 } 138 139 while (state != ESM_DONE) 140 { 141 # ifdef DEBUG 142 if (tTd(6, 5)) 143 printf(" state %d\n", state); 144 # endif DEBUG 145 146 switch (state) 147 { 148 case ESM_QUIET: 149 if (e->e_from.q_mailer == LocalMailer) 150 state = ESM_DEADLETTER; 151 else 152 state = ESM_MAIL; 153 break; 154 155 case ESM_REPORT: 156 157 /* 158 ** If the user is still logged in on the same terminal, 159 ** then write the error messages back to hir (sic). 160 */ 161 162 p = ttypath(); 163 if (p == NULL || freopen(p, "w", stdout) == NULL) 164 { 165 state = ESM_MAIL; 166 break; 167 } 168 169 expand("\001n", buf, &buf[sizeof buf - 1], e); 170 printf("\r\nMessage from %s...\r\n", buf); 171 printf("Errors occurred while sending mail.\r\n"); 172 if (e->e_xfp != NULL) 173 { 174 (void) fflush(e->e_xfp); 175 fp = fopen(queuename(e, 'x'), "r"); 176 } 177 else 178 fp = NULL; 179 if (fp == NULL) 180 { 181 syserr("Cannot open %s", queuename(e, 'x')); 182 printf("Transcript of session is unavailable.\r\n"); 183 } 184 else 185 { 186 printf("Transcript follows:\r\n"); 187 while (fgets(buf, sizeof buf, fp) != NULL && 188 !ferror(stdout)) 189 fputs(buf, stdout); 190 (void) fclose(fp); 191 } 192 printf("Original message will be saved in dead.letter.\r\n"); 193 if (ferror(stdout)) 194 (void) syserr("savemail: stdout: write err"); 195 state = ESM_DEADLETTER; 196 break; 197 198 case ESM_MAIL: 199 case ESM_POSTMASTER: 200 /* 201 ** If mailing back, do it. 202 ** Throw away all further output. Don't alias, 203 ** since this could cause loops, e.g., if joe 204 ** mails to joe@x, and for some reason the network 205 ** for @x is down, then the response gets sent to 206 ** joe@x, which gives a response, etc. Also force 207 ** the mail to be delivered even if a version of 208 ** it has already been sent to the sender. 209 */ 210 211 if (state == ESM_MAIL) 212 { 213 if (e->e_errorqueue == NULL) 214 sendtolist(e->e_from.q_paddr, 215 (ADDRESS *) NULL, 216 &e->e_errorqueue); 217 q = e->e_errorqueue; 218 } 219 else 220 { 221 if (parseaddr("postmaster", q, 0, '\0') == NULL) 222 { 223 syserr("cannot parse postmaster!"); 224 ExitStat = EX_SOFTWARE; 225 state = ESM_USRTMP; 226 break; 227 } 228 } 229 if (returntosender(e->e_message != NULL ? e->e_message : 230 "Unable to deliver mail", 231 q, TRUE) == 0) 232 { 233 state = ESM_DONE; 234 break; 235 } 236 237 state = state == ESM_MAIL ? ESM_POSTMASTER : ESM_USRTMP; 238 break; 239 240 case ESM_DEADLETTER: 241 /* 242 ** Save the message in dead.letter. 243 ** If we weren't mailing back, and the user is 244 ** local, we should save the message in 245 ** ~/dead.letter so that the poor person doesn't 246 ** have to type it over again -- and we all know 247 ** what poor typists UNIX users are. 248 */ 249 250 p = NULL; 251 if (e->e_from.q_mailer == LocalMailer) 252 { 253 if (e->e_from.q_home != NULL) 254 p = e->e_from.q_home; 255 else if ((pw = getpwnam(e->e_from.q_user)) != NULL) 256 p = pw->pw_dir; 257 } 258 if (p == NULL) 259 { 260 syserr("Can't return mail to %s", e->e_from.q_paddr); 261 state = ESM_MAIL; 262 break; 263 } 264 if (e->e_dfp != NULL) 265 { 266 auto ADDRESS *q; 267 bool oldverb = Verbose; 268 269 /* we have a home directory; open dead.letter */ 270 define('z', p, e); 271 expand("\001z/dead.letter", buf, &buf[sizeof buf - 1], e); 272 Verbose = TRUE; 273 message(Arpa_Info, "Saving message in %s", buf); 274 Verbose = oldverb; 275 e->e_to = buf; 276 q = NULL; 277 sendtolist(buf, (ADDRESS *) NULL, &q); 278 if (deliver(e, q) == 0) 279 state = ESM_DONE; 280 else 281 state = ESM_MAIL; 282 } 283 break; 284 285 case ESM_USRTMP: 286 /* 287 ** Log the mail in /usr/tmp/dead.letter. 288 */ 289 290 fp = dfopen("/usr/tmp/dead.letter", "a"); 291 if (fp == NULL) 292 { 293 state = ESM_PANIC; 294 break; 295 } 296 297 putfromline(fp, ProgMailer); 298 (*e->e_puthdr)(fp, ProgMailer, e); 299 putline("\n", fp, ProgMailer); 300 (*e->e_putbody)(fp, ProgMailer, e); 301 putline("\n", fp, ProgMailer); 302 (void) fflush(fp); 303 state = ferror(fp) ? ESM_PANIC : ESM_DONE; 304 (void) fclose(fp); 305 break; 306 307 default: 308 syserr("savemail: unknown state %d", state); 309 310 /* fall through ... */ 311 312 case ESM_PANIC: 313 syserr("savemail: HELP!!!!"); 314 # ifdef LOG 315 if (LogLevel >= 1) 316 syslog(LOG_ALERT, "savemail: HELP!!!!"); 317 # endif LOG 318 319 /* leave the locked queue & transcript files around */ 320 exit(EX_SOFTWARE); 321 } 322 } 323 } 324 /* 325 ** RETURNTOSENDER -- return a message to the sender with an error. 326 ** 327 ** Parameters: 328 ** msg -- the explanatory message. 329 ** returnq -- the queue of people to send the message to. 330 ** sendbody -- if TRUE, also send back the body of the 331 ** message; otherwise just send the header. 332 ** 333 ** Returns: 334 ** zero -- if everything went ok. 335 ** else -- some error. 336 ** 337 ** Side Effects: 338 ** Returns the current message to the sender via 339 ** mail. 340 */ 341 342 static bool SendBody; 343 344 #define MAXRETURNS 6 /* max depth of returning messages */ 345 346 returntosender(msg, returnq, sendbody) 347 char *msg; 348 ADDRESS *returnq; 349 bool sendbody; 350 { 351 char buf[MAXNAME]; 352 extern putheader(), errbody(); 353 register ENVELOPE *ee; 354 extern ENVELOPE *newenvelope(); 355 ENVELOPE errenvelope; 356 static int returndepth; 357 register ADDRESS *q; 358 359 # ifdef DEBUG 360 if (tTd(6, 1)) 361 { 362 printf("Return To Sender: msg=\"%s\", depth=%d, CurEnv=%x,\n", 363 msg, returndepth, CurEnv); 364 printf("\treturnq="); 365 printaddr(returnq, TRUE); 366 } 367 # endif DEBUG 368 369 if (++returndepth >= MAXRETURNS) 370 { 371 if (returndepth != MAXRETURNS) 372 syserr("returntosender: infinite recursion on %s", returnq->q_paddr); 373 /* don't "unrecurse" and fake a clean exit */ 374 /* returndepth--; */ 375 return (0); 376 } 377 378 SendBody = sendbody; 379 define('g', "\001f", CurEnv); 380 ee = newenvelope(&errenvelope); 381 define('a', "\001b", ee); 382 ee->e_puthdr = putheader; 383 ee->e_putbody = errbody; 384 ee->e_flags |= EF_RESPONSE; 385 ee->e_sendqueue = returnq; 386 openxscript(ee); 387 for (q = returnq; q != NULL; q = q->q_next) 388 { 389 if (q->q_alias == NULL) 390 addheader("to", q->q_paddr, ee); 391 } 392 393 (void) sprintf(buf, "Returned mail: %s", msg); 394 addheader("subject", buf, ee); 395 396 /* fake up an address header for the from person */ 397 expand("\001n", buf, &buf[sizeof buf - 1], CurEnv); 398 if (parseaddr(buf, &ee->e_from, -1, '\0') == NULL) 399 { 400 syserr("Can't parse myself!"); 401 ExitStat = EX_SOFTWARE; 402 returndepth--; 403 return (-1); 404 } 405 loweraddr(&ee->e_from); 406 407 /* push state into submessage */ 408 CurEnv = ee; 409 define('f', "\001n", ee); 410 define('x', "Mail Delivery Subsystem", ee); 411 eatheader(ee); 412 413 /* actually deliver the error message */ 414 sendall(ee, SM_DEFAULT); 415 416 /* restore state */ 417 dropenvelope(ee); 418 CurEnv = CurEnv->e_parent; 419 returndepth--; 420 421 /* should check for delivery errors here */ 422 return (0); 423 } 424 /* 425 ** ERRBODY -- output the body of an error message. 426 ** 427 ** Typically this is a copy of the transcript plus a copy of the 428 ** original offending message. 429 ** 430 ** Parameters: 431 ** fp -- the output file. 432 ** m -- the mailer to output to. 433 ** e -- the envelope we are working in. 434 ** 435 ** Returns: 436 ** none 437 ** 438 ** Side Effects: 439 ** Outputs the body of an error message. 440 */ 441 442 errbody(fp, m, e) 443 register FILE *fp; 444 register struct mailer *m; 445 register ENVELOPE *e; 446 { 447 register FILE *xfile; 448 char buf[MAXLINE]; 449 char *p; 450 451 /* 452 ** Output transcript of errors 453 */ 454 455 (void) fflush(stdout); 456 p = queuename(e->e_parent, 'x'); 457 if ((xfile = fopen(p, "r")) == NULL) 458 { 459 syserr("Cannot open %s", p); 460 fprintf(fp, " ----- Transcript of session is unavailable -----\n"); 461 } 462 else 463 { 464 fprintf(fp, " ----- Transcript of session follows -----\n"); 465 if (e->e_xfp != NULL) 466 (void) fflush(e->e_xfp); 467 while (fgets(buf, sizeof buf, xfile) != NULL) 468 putline(buf, fp, m); 469 (void) fclose(xfile); 470 } 471 errno = 0; 472 473 /* 474 ** Output text of original message 475 */ 476 477 if (NoReturn) 478 fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 479 else if (e->e_parent->e_dfp != NULL) 480 { 481 if (SendBody) 482 { 483 putline("\n", fp, m); 484 putline(" ----- Unsent message follows -----\n", fp, m); 485 (void) fflush(fp); 486 putheader(fp, m, e->e_parent); 487 putline("\n", fp, m); 488 putbody(fp, m, e->e_parent); 489 } 490 else 491 { 492 putline("\n", fp, m); 493 putline(" ----- Message header follows -----\n", fp, m); 494 (void) fflush(fp); 495 putheader(fp, m, e->e_parent); 496 } 497 } 498 else 499 { 500 putline("\n", fp, m); 501 putline(" ----- No message was collected -----\n", fp, m); 502 putline("\n", fp, m); 503 } 504 505 /* 506 ** Cleanup and exit 507 */ 508 509 if (errno != 0) 510 syserr("errbody: I/O error"); 511 } 512