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.4 (Berkeley) 09/19/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\n"); 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 state = ESM_QUIET; 126 break; 127 128 case EM_QUIET: 129 /* no need to return anything at all */ 130 return; 131 } 132 133 while (state != ESM_DONE) 134 { 135 switch (state) 136 { 137 case ESM_REPORT: 138 139 /* 140 ** If the user is still logged in on the same terminal, 141 ** then write the error messages back to hir (sic). 142 */ 143 144 p = ttypath(); 145 if (p == NULL || freopen(p, "w", stdout) == NULL) 146 { 147 state = ESM_MAIL; 148 break; 149 } 150 151 expand("\001n", buf, &buf[sizeof buf - 1], e); 152 printf("\r\nMessage from %s...\r\n", buf); 153 printf("Errors occurred while sending mail.\r\n"); 154 if (e->e_xfp != NULL) 155 { 156 (void) fflush(e->e_xfp); 157 fp = fopen(queuename(e, 'x'), "r"); 158 } 159 else 160 fp = NULL; 161 if (fp == NULL) 162 { 163 syserr("Cannot open %s", queuename(e, 'x')); 164 printf("Transcript of session is unavailable.\r\n"); 165 } 166 else 167 { 168 printf("Transcript follows:\r\n"); 169 while (fgets(buf, sizeof buf, fp) != NULL && 170 !ferror(stdout)) 171 fputs(buf, stdout); 172 (void) fclose(fp); 173 } 174 printf("Original message will be saved in dead.letter.\r\n"); 175 if (ferror(stdout)) 176 (void) syserr("savemail: stdout: write err"); 177 state = ESM_DEADLETTER; 178 break; 179 180 case ESM_MAIL: 181 case ESM_POSTMASTER: 182 /* 183 ** If mailing back, do it. 184 ** Throw away all further output. Don't alias, 185 ** since this could cause loops, e.g., if joe 186 ** mails to joe@x, and for some reason the network 187 ** for @x is down, then the response gets sent to 188 ** joe@x, which gives a response, etc. Also force 189 ** the mail to be delivered even if a version of 190 ** it has already been sent to the sender. 191 */ 192 193 if (state == ESM_MAIL) 194 { 195 if (e->e_errorqueue == NULL) 196 sendtolist(e->e_from.q_paddr, 197 (ADDRESS *) NULL, 198 &e->e_errorqueue); 199 q = e->e_errorqueue; 200 } 201 else 202 { 203 if (parseaddr("postmaster", q, 0, '\0') == NULL) 204 { 205 syserr("cannot parse postmaster!"); 206 ExitStat = EX_SOFTWARE; 207 state = ESM_USRTMP; 208 break; 209 } 210 } 211 if (returntosender(e->e_message != NULL ? e->e_message : 212 "Unable to deliver mail", 213 q, TRUE) == 0) 214 { 215 state = ESM_DONE; 216 break; 217 } 218 219 state = state == ESM_MAIL ? ESM_POSTMASTER : ESM_USRTMP; 220 break; 221 222 case ESM_DEADLETTER: 223 /* 224 ** Save the message in dead.letter. 225 ** If we weren't mailing back, and the user is 226 ** local, we should save the message in 227 ** ~/dead.letter so that the poor person doesn't 228 ** have to type it over again -- and we all know 229 ** what poor typists UNIX users are. 230 */ 231 232 p = NULL; 233 if (e->e_from.q_mailer == LocalMailer) 234 { 235 if (e->e_from.q_home != NULL) 236 p = e->e_from.q_home; 237 else if ((pw = getpwnam(e->e_from.q_user)) != NULL) 238 p = pw->pw_dir; 239 } 240 if (p == NULL) 241 { 242 syserr("Can't return mail to %s", e->e_from.q_paddr); 243 state = ESM_MAIL; 244 break; 245 } 246 if (e->e_dfp != NULL) 247 { 248 auto ADDRESS *q; 249 bool oldverb = Verbose; 250 251 /* we have a home directory; open dead.letter */ 252 define('z', p, e); 253 expand("\001z/dead.letter", buf, &buf[sizeof buf - 1], e); 254 Verbose = TRUE; 255 message(Arpa_Info, "Saving message in %s", buf); 256 Verbose = oldverb; 257 e->e_to = buf; 258 q = NULL; 259 sendtolist(buf, (ADDRESS *) NULL, &q); 260 if (deliver(e, q) == 0) 261 state = ESM_DONE; 262 else 263 state = ESM_MAIL; 264 } 265 break; 266 267 case ESM_USRTMP: 268 /* 269 ** Log the mail in /usr/tmp/dead.letter. 270 */ 271 272 fp = dfopen("/usr/tmp/dead.letter", "a"); 273 if (fp == NULL) 274 { 275 state = ESM_PANIC; 276 break; 277 } 278 279 putfromline(fp, ProgMailer); 280 (*e->e_puthdr)(fp, ProgMailer, e); 281 putline("\n", fp, ProgMailer); 282 (*e->e_putbody)(fp, ProgMailer, e); 283 putline("\n", fp, ProgMailer); 284 (void) fflush(fp); 285 state = ferror(fp) ? ESM_PANIC : ESM_DONE; 286 (void) fclose(fp); 287 break; 288 289 default: 290 syserr("savemail: unknown state %d", state); 291 292 /* fall through ... */ 293 294 case ESM_PANIC: 295 syserr("savemail: HELP!!!!"); 296 # ifdef LOG 297 if (LogLevel >= 1) 298 syslog(LOG_ALERT, "savemail: HELP!!!!"); 299 # endif LOG 300 301 /* leave the locked queue & transcript files around */ 302 exit(EX_SOFTWARE); 303 } 304 } 305 } 306 /* 307 ** RETURNTOSENDER -- return a message to the sender with an error. 308 ** 309 ** Parameters: 310 ** msg -- the explanatory message. 311 ** returnq -- the queue of people to send the message to. 312 ** sendbody -- if TRUE, also send back the body of the 313 ** message; otherwise just send the header. 314 ** 315 ** Returns: 316 ** zero -- if everything went ok. 317 ** else -- some error. 318 ** 319 ** Side Effects: 320 ** Returns the current message to the sender via 321 ** mail. 322 */ 323 324 static bool SendBody; 325 326 #define MAXRETURNS 6 /* max depth of returning messages */ 327 328 returntosender(msg, returnq, sendbody) 329 char *msg; 330 ADDRESS *returnq; 331 bool sendbody; 332 { 333 char buf[MAXNAME]; 334 extern putheader(), errbody(); 335 register ENVELOPE *ee; 336 extern ENVELOPE *newenvelope(); 337 ENVELOPE errenvelope; 338 static int returndepth; 339 register ADDRESS *q; 340 341 # ifdef DEBUG 342 if (tTd(6, 1)) 343 { 344 printf("Return To Sender: msg=\"%s\", depth=%d, CurEnv=%x,\n", 345 msg, returndepth, CurEnv); 346 printf("\treturnq="); 347 printaddr(returnq, TRUE); 348 } 349 # endif DEBUG 350 351 if (++returndepth >= MAXRETURNS) 352 { 353 if (returndepth != MAXRETURNS) 354 syserr("returntosender: infinite recursion on %s", returnq->q_paddr); 355 /* don't "unrecurse" and fake a clean exit */ 356 /* returndepth--; */ 357 return (0); 358 } 359 360 SendBody = sendbody; 361 define('g', "\001f", CurEnv); 362 ee = newenvelope(&errenvelope); 363 define('a', "\001b", ee); 364 ee->e_puthdr = putheader; 365 ee->e_putbody = errbody; 366 ee->e_flags |= EF_RESPONSE; 367 ee->e_sendqueue = returnq; 368 openxscript(ee); 369 for (q = returnq; q != NULL; q = q->q_next) 370 { 371 if (q->q_alias == NULL) 372 addheader("to", q->q_paddr, ee); 373 } 374 375 (void) sprintf(buf, "Returned mail: %s", msg); 376 addheader("subject", buf, ee); 377 378 /* fake up an address header for the from person */ 379 expand("\001n", buf, &buf[sizeof buf - 1], CurEnv); 380 if (parseaddr(buf, &ee->e_from, -1, '\0') == NULL) 381 { 382 syserr("Can't parse myself!"); 383 ExitStat = EX_SOFTWARE; 384 returndepth--; 385 return (-1); 386 } 387 loweraddr(&ee->e_from); 388 389 /* push state into submessage */ 390 CurEnv = ee; 391 define('f', "\001n", ee); 392 define('x', "Mail Delivery Subsystem", ee); 393 eatheader(ee); 394 395 /* actually deliver the error message */ 396 sendall(ee, SM_DEFAULT); 397 398 /* restore state */ 399 dropenvelope(ee); 400 CurEnv = CurEnv->e_parent; 401 returndepth--; 402 403 /* should check for delivery errors here */ 404 return (0); 405 } 406 /* 407 ** ERRBODY -- output the body of an error message. 408 ** 409 ** Typically this is a copy of the transcript plus a copy of the 410 ** original offending message. 411 ** 412 ** Parameters: 413 ** fp -- the output file. 414 ** m -- the mailer to output to. 415 ** e -- the envelope we are working in. 416 ** 417 ** Returns: 418 ** none 419 ** 420 ** Side Effects: 421 ** Outputs the body of an error message. 422 */ 423 424 errbody(fp, m, e) 425 register FILE *fp; 426 register struct mailer *m; 427 register ENVELOPE *e; 428 { 429 register FILE *xfile; 430 char buf[MAXLINE]; 431 char *p; 432 433 /* 434 ** Output transcript of errors 435 */ 436 437 (void) fflush(stdout); 438 p = queuename(e->e_parent, 'x'); 439 if ((xfile = fopen(p, "r")) == NULL) 440 { 441 syserr("Cannot open %s", p); 442 fprintf(fp, " ----- Transcript of session is unavailable -----\n"); 443 } 444 else 445 { 446 fprintf(fp, " ----- Transcript of session follows -----\n"); 447 if (e->e_xfp != NULL) 448 (void) fflush(e->e_xfp); 449 while (fgets(buf, sizeof buf, xfile) != NULL) 450 putline(buf, fp, m); 451 (void) fclose(xfile); 452 } 453 errno = 0; 454 455 /* 456 ** Output text of original message 457 */ 458 459 if (NoReturn) 460 fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 461 else if (e->e_parent->e_dfp != NULL) 462 { 463 if (SendBody) 464 { 465 putline("\n", fp, m); 466 putline(" ----- Unsent message follows -----\n", fp, m); 467 (void) fflush(fp); 468 putheader(fp, m, e->e_parent); 469 putline("\n", fp, m); 470 putbody(fp, m, e->e_parent); 471 } 472 else 473 { 474 putline("\n", fp, m); 475 putline(" ----- Message header follows -----\n", fp, m); 476 (void) fflush(fp); 477 putheader(fp, m, e->e_parent); 478 } 479 } 480 else 481 { 482 putline("\n", fp, m); 483 putline(" ----- No message was collected -----\n", fp, m); 484 putline("\n", fp, m); 485 } 486 487 /* 488 ** Cleanup and exit 489 */ 490 491 if (errno != 0) 492 syserr("errbody: I/O error"); 493 } 494