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