122711Sdist /* 222711Sdist ** Sendmail 322711Sdist ** Copyright (c) 1983 Eric P. Allman 422711Sdist ** Berkeley, California 522711Sdist ** 622711Sdist ** Copyright (c) 1983 Regents of the University of California. 722711Sdist ** All rights reserved. The Berkeley software License Agreement 822711Sdist ** specifies the terms and conditions for redistribution. 922711Sdist */ 1022711Sdist 1122711Sdist #ifndef lint 12*24942Seric static char SccsId[] = "@(#)savemail.c 5.3 (Berkeley) 09/19/85"; 1322711Sdist #endif not lint 1422711Sdist 15297Seric # include <pwd.h> 163313Seric # include "sendmail.h" 17297Seric 18297Seric /* 19297Seric ** SAVEMAIL -- Save mail on error 20297Seric ** 219375Seric ** If mailing back errors, mail it back to the originator 22297Seric ** together with an error message; otherwise, just put it in 23297Seric ** dead.letter in the user's home directory (if he exists on 24297Seric ** this machine). 25297Seric ** 26297Seric ** Parameters: 279337Seric ** e -- the envelope containing the message in error. 28297Seric ** 29297Seric ** Returns: 30297Seric ** none 31297Seric ** 32297Seric ** Side Effects: 33297Seric ** Saves the letter, by writing or mailing it back to the 34297Seric ** sender, or by putting it in dead.letter in her home 35297Seric ** directory. 36297Seric */ 37297Seric 38*24942Seric /* defines for state machine */ 39*24942Seric # define ESM_REPORT 0 /* report to sender's terminal */ 40*24942Seric # define ESM_MAIL 1 /* mail back to sender */ 41*24942Seric # define ESM_QUIET 2 /* messages have already been returned */ 42*24942Seric # define ESM_DEADLETTER 3 /* save in ~/dead.letter */ 43*24942Seric # define ESM_POSTMASTER 4 /* return to postmaster */ 44*24942Seric # define ESM_USRTMP 5 /* save in /usr/tmp/dead.letter */ 45*24942Seric # define ESM_PANIC 6 /* leave the locked queue/transcript files */ 46*24942Seric # define ESM_DONE 7 /* the message is successfully delivered */ 47*24942Seric 48*24942Seric 499337Seric savemail(e) 509337Seric register ENVELOPE *e; 51297Seric { 52297Seric register struct passwd *pw; 53*24942Seric register FILE *fp; 54*24942Seric int state; 55*24942Seric auto ADDRESS *q; 56297Seric char buf[MAXLINE+1]; 57297Seric extern struct passwd *getpwnam(); 58297Seric register char *p; 59297Seric extern char *ttypath(); 605846Seric typedef int (*fnptr)(); 61297Seric 627361Seric # ifdef DEBUG 637676Seric if (tTd(6, 1)) 649375Seric printf("\nsavemail\n"); 657361Seric # endif DEBUG 667361Seric 679375Seric if (bitset(EF_RESPONSE, e->e_flags)) 68297Seric return; 699337Seric if (e->e_class < 0) 706978Seric { 717053Seric message(Arpa_Info, "Dumping junk mail"); 726978Seric return; 736978Seric } 747053Seric ForceMail = TRUE; 759337Seric e->e_flags &= ~EF_FATALERRS; 76297Seric 77297Seric /* 78297Seric ** In the unhappy event we don't know who to return the mail 79297Seric ** to, make someone up. 80297Seric */ 81297Seric 829337Seric if (e->e_from.q_paddr == NULL) 83297Seric { 8411447Seric if (parseaddr("root", &e->e_from, 0, '\0') == NULL) 85297Seric { 86297Seric syserr("Cannot parse root!"); 87297Seric ExitStat = EX_SOFTWARE; 88297Seric finis(); 89297Seric } 90297Seric } 919337Seric e->e_to = NULL; 92297Seric 93297Seric /* 94*24942Seric ** Basic state machine. 95*24942Seric ** 96*24942Seric ** This machine runs through the following states: 97*24942Seric ** 98*24942Seric ** ESM_QUIET Errors have already been printed iff the 99*24942Seric ** sender is local. 100*24942Seric ** ESM_REPORT Report directly to the sender's terminal. 101*24942Seric ** ESM_MAIL Mail response to the sender. 102*24942Seric ** ESM_DEADLETTER Save response in ~/dead.letter. 103*24942Seric ** ESM_POSTMASTER Mail response to the postmaster. 104*24942Seric ** ESM_PANIC Save response anywhere possible. 105297Seric */ 106297Seric 107*24942Seric /* determine starting state */ 108*24942Seric switch (ErrorMode) 109297Seric { 110*24942Seric case EM_WRITE: 111*24942Seric state = ESM_REPORT; 112*24942Seric break; 113*24942Seric 114*24942Seric case EM_BERKNET: 115*24942Seric /* mail back, but return o.k. exit status */ 116401Seric ExitStat = EX_OK; 117*24942Seric 118*24942Seric /* fall through.... */ 119*24942Seric 120*24942Seric case EM_MAIL: 121*24942Seric state = ESM_MAIL; 122*24942Seric break; 123*24942Seric 124*24942Seric case EM_PRINT: 125*24942Seric state = ESM_QUIET; 126*24942Seric break; 127*24942Seric 128*24942Seric case EM_QUIET: 129*24942Seric /* no need to return anything at all */ 130*24942Seric return; 131297Seric } 132297Seric 133*24942Seric while (state != ESM_DONE) 134297Seric { 135*24942Seric switch (state) 136297Seric { 137*24942Seric case ESM_REPORT: 138*24942Seric 139*24942Seric /* 140*24942Seric ** If the user is still logged in on the same terminal, 141*24942Seric ** then write the error messages back to hir (sic). 142*24942Seric */ 143*24942Seric 144*24942Seric p = ttypath(); 145*24942Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 146*24942Seric { 147*24942Seric state = ESM_MAIL; 148*24942Seric break; 149*24942Seric } 150*24942Seric 15116152Seric expand("\001n", buf, &buf[sizeof buf - 1], e); 1529375Seric printf("\r\nMessage from %s...\r\n", buf); 1539375Seric printf("Errors occurred while sending mail.\r\n"); 1549542Seric if (e->e_xfp != NULL) 1559375Seric { 1569542Seric (void) fflush(e->e_xfp); 157*24942Seric fp = fopen(queuename(e, 'x'), "r"); 1589375Seric } 1599375Seric else 160*24942Seric fp = NULL; 161*24942Seric if (fp == NULL) 1629375Seric { 1639337Seric syserr("Cannot open %s", queuename(e, 'x')); 1649375Seric printf("Transcript of session is unavailable.\r\n"); 1659375Seric } 1669375Seric else 1679375Seric { 1689375Seric printf("Transcript follows:\r\n"); 169*24942Seric while (fgets(buf, sizeof buf, fp) != NULL && 1709375Seric !ferror(stdout)) 1719375Seric fputs(buf, stdout); 172*24942Seric (void) fclose(fp); 1739375Seric } 174*24942Seric printf("Original message will be saved in dead.letter.\r\n"); 175297Seric if (ferror(stdout)) 1764086Seric (void) syserr("savemail: stdout: write err"); 177*24942Seric state = ESM_DEADLETTER; 178*24942Seric break; 179297Seric 180*24942Seric case ESM_MAIL: 181*24942Seric case ESM_POSTMASTER: 182*24942Seric /* 183*24942Seric ** If mailing back, do it. 184*24942Seric ** Throw away all further output. Don't alias, 185*24942Seric ** since this could cause loops, e.g., if joe 186*24942Seric ** mails to joe@x, and for some reason the network 187*24942Seric ** for @x is down, then the response gets sent to 188*24942Seric ** joe@x, which gives a response, etc. Also force 189*24942Seric ** the mail to be delivered even if a version of 190*24942Seric ** it has already been sent to the sender. 191*24942Seric */ 192297Seric 193*24942Seric if (state == ESM_MAIL) 194*24942Seric { 195*24942Seric if (e->e_errorqueue == NULL) 196*24942Seric sendtolist(e->e_from.q_paddr, 197*24942Seric (ADDRESS *) NULL, 198*24942Seric &e->e_errorqueue); 199*24942Seric q = e->e_errorqueue; 200*24942Seric } 201*24942Seric else 202*24942Seric { 203*24942Seric if (parseaddr("postmaster", &q, 0, '\0') == NULL) 204*24942Seric { 205*24942Seric syserr("cannot parse postmaster!"); 206*24942Seric ExitStat = EX_SOFTWARE; 207*24942Seric state = ESM_USRTMP; 208*24942Seric break; 209*24942Seric } 210*24942Seric } 211*24942Seric if (returntosender(e->e_message != NULL ? e->e_message : 212*24942Seric "Unable to deliver mail", 213*24942Seric q, TRUE) == 0) 214*24942Seric { 215*24942Seric state = ESM_DONE; 216*24942Seric break; 217*24942Seric } 218297Seric 219*24942Seric state = state == ESM_MAIL ? ESM_POSTMASTER : ESM_USRTMP; 220*24942Seric break; 221297Seric 222*24942Seric case ESM_DEADLETTER: 223*24942Seric /* 224*24942Seric ** Save the message in dead.letter. 225*24942Seric ** If we weren't mailing back, and the user is 226*24942Seric ** local, we should save the message in 227*24942Seric ** ~/dead.letter so that the poor person doesn't 228*24942Seric ** have to type it over again -- and we all know 229*24942Seric ** what poor typists UNIX users are. 230*24942Seric */ 2315315Seric 232*24942Seric p = NULL; 233*24942Seric if (e->e_from.q_mailer == LocalMailer) 234*24942Seric { 235*24942Seric if (e->e_from.q_home != NULL) 236*24942Seric p = e->e_from.q_home; 237*24942Seric else if ((pw = getpwnam(e->e_from.q_user)) != NULL) 238*24942Seric p = pw->pw_dir; 239*24942Seric } 240*24942Seric if (p == NULL) 241*24942Seric { 242*24942Seric syserr("Can't return mail to %s", e->e_from.q_paddr); 243*24942Seric state = ESM_MAIL; 244*24942Seric break; 245*24942Seric } 246*24942Seric if (e->e_dfp != NULL) 247*24942Seric { 248*24942Seric auto ADDRESS *q; 249*24942Seric bool oldverb = Verbose; 250*24942Seric 251*24942Seric /* we have a home directory; open dead.letter */ 252*24942Seric define('z', p, e); 253*24942Seric expand("\001z/dead.letter", buf, &buf[sizeof buf - 1], e); 254*24942Seric Verbose = TRUE; 255*24942Seric message(Arpa_Info, "Saving message in %s", buf); 256*24942Seric Verbose = oldverb; 257*24942Seric e->e_to = buf; 258*24942Seric q = NULL; 259*24942Seric sendtolist(buf, (ADDRESS *) NULL, &q); 260*24942Seric if (deliver(e, q) == 0) 261*24942Seric state = ESM_DONE; 262*24942Seric else 263*24942Seric state = ESM_MAIL; 264*24942Seric } 265*24942Seric break; 266*24942Seric 267*24942Seric case ESM_USRTMP: 268*24942Seric /* 269*24942Seric ** Log the mail in /usr/tmp/dead.letter. 270*24942Seric */ 271*24942Seric 272*24942Seric fp = dfopen("/usr/tmp/dead.letter", "a"); 273*24942Seric if (fp == NULL) 274*24942Seric { 275*24942Seric state = ESM_PANIC; 276*24942Seric break; 277*24942Seric } 278*24942Seric 279*24942Seric putfromline(fp, ProgMailer); 280*24942Seric (*e->e_puthdr)(fp, ProgMailer, e); 281*24942Seric putline("\n", fp, ProgMailer); 282*24942Seric (*e->e_putbody)(fp, ProgMailer, e); 283*24942Seric putline("\n", fp, ProgMailer); 284*24942Seric (void) fflush(fp); 285*24942Seric state = ferror(fp) ? ESM_PANIC : ESM_DONE; 286*24942Seric (void) fclose(fp); 287*24942Seric break; 288*24942Seric 289*24942Seric default: 290*24942Seric syserr("savemail: unknown state %d", state); 291*24942Seric 292*24942Seric /* fall through ... */ 293*24942Seric 294*24942Seric case ESM_PANIC: 295*24942Seric syserr("savemail: HELP!!!!"); 296*24942Seric # ifdef LOG 297*24942Seric if (LogLevel >= 1) 298*24942Seric syslog(LOG_ALERT, "savemail: HELP!!!!"); 299*24942Seric # endif LOG 300*24942Seric 301*24942Seric /* leave the locked queue & transcript files around */ 302*24942Seric exit(EX_SOFTWARE); 303*24942Seric } 304297Seric } 305297Seric } 306297Seric /* 3074633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 3084633Seric ** 3094633Seric ** Parameters: 3104633Seric ** msg -- the explanatory message. 31116479Seric ** returnq -- the queue of people to send the message to. 3125984Seric ** sendbody -- if TRUE, also send back the body of the 3135984Seric ** message; otherwise just send the header. 3144633Seric ** 3154633Seric ** Returns: 3164633Seric ** zero -- if everything went ok. 3174633Seric ** else -- some error. 3184633Seric ** 3194633Seric ** Side Effects: 3204633Seric ** Returns the current message to the sender via 3214633Seric ** mail. 3224633Seric */ 3234633Seric 3245984Seric static bool SendBody; 3254633Seric 3267045Seric #define MAXRETURNS 6 /* max depth of returning messages */ 3277045Seric 32816479Seric returntosender(msg, returnq, sendbody) 3294633Seric char *msg; 33016479Seric ADDRESS *returnq; 3315984Seric bool sendbody; 3324633Seric { 3334633Seric char buf[MAXNAME]; 3346978Seric extern putheader(), errbody(); 3356978Seric register ENVELOPE *ee; 3366978Seric extern ENVELOPE *newenvelope(); 3376978Seric ENVELOPE errenvelope; 3387045Seric static int returndepth; 3399375Seric register ADDRESS *q; 3404633Seric 3417287Seric # ifdef DEBUG 3427676Seric if (tTd(6, 1)) 3437287Seric { 3447287Seric printf("Return To Sender: msg=\"%s\", depth=%d, CurEnv=%x,\n", 3457287Seric msg, returndepth, CurEnv); 346*24942Seric printf("\treturnq="); 34716479Seric printaddr(returnq, TRUE); 3487287Seric } 3497287Seric # endif DEBUG 3507287Seric 3517045Seric if (++returndepth >= MAXRETURNS) 3527045Seric { 3537045Seric if (returndepth != MAXRETURNS) 35416479Seric syserr("returntosender: infinite recursion on %s", returnq->q_paddr); 3557045Seric /* don't "unrecurse" and fake a clean exit */ 3567045Seric /* returndepth--; */ 3577045Seric return (0); 3587045Seric } 3597045Seric 3605984Seric SendBody = sendbody; 36116152Seric define('g', "\001f", CurEnv); 3626978Seric ee = newenvelope(&errenvelope); 363*24942Seric define('a', "\001b", ee); 3646978Seric ee->e_puthdr = putheader; 3656978Seric ee->e_putbody = errbody; 3669375Seric ee->e_flags |= EF_RESPONSE; 36716479Seric ee->e_sendqueue = returnq; 3689542Seric openxscript(ee); 36916479Seric for (q = returnq; q != NULL; q = q->q_next) 3709375Seric { 3719375Seric if (q->q_alias == NULL) 3729375Seric addheader("to", q->q_paddr, ee); 3739375Seric } 374*24942Seric 37510845Seric (void) sprintf(buf, "Returned mail: %s", msg); 37610106Seric addheader("subject", buf, ee); 3774633Seric 3784633Seric /* fake up an address header for the from person */ 37916152Seric expand("\001n", buf, &buf[sizeof buf - 1], CurEnv); 38011447Seric if (parseaddr(buf, &ee->e_from, -1, '\0') == NULL) 3814633Seric { 3824633Seric syserr("Can't parse myself!"); 3834633Seric ExitStat = EX_SOFTWARE; 3847045Seric returndepth--; 3854633Seric return (-1); 3864633Seric } 38716159Seric loweraddr(&ee->e_from); 3885984Seric 3896978Seric /* push state into submessage */ 3906978Seric CurEnv = ee; 39116152Seric define('f', "\001n", ee); 3929375Seric define('x', "Mail Delivery Subsystem", ee); 39311291Seric eatheader(ee); 3945984Seric 3956978Seric /* actually deliver the error message */ 39614876Seric sendall(ee, SM_DEFAULT); 3976978Seric 3986978Seric /* restore state */ 3997811Seric dropenvelope(ee); 4006978Seric CurEnv = CurEnv->e_parent; 4017045Seric returndepth--; 4026978Seric 4037045Seric /* should check for delivery errors here */ 4044633Seric return (0); 4054633Seric } 4064633Seric /* 4076978Seric ** ERRBODY -- output the body of an error message. 4086978Seric ** 4096978Seric ** Typically this is a copy of the transcript plus a copy of the 4106978Seric ** original offending message. 4116978Seric ** 412297Seric ** Parameters: 413297Seric ** fp -- the output file. 41410170Seric ** m -- the mailer to output to. 4159542Seric ** e -- the envelope we are working in. 416297Seric ** 417297Seric ** Returns: 418297Seric ** none 419297Seric ** 420297Seric ** Side Effects: 4216978Seric ** Outputs the body of an error message. 422297Seric */ 423297Seric 42410170Seric errbody(fp, m, e) 425297Seric register FILE *fp; 4264318Seric register struct mailer *m; 4279542Seric register ENVELOPE *e; 428297Seric { 4296978Seric register FILE *xfile; 4303189Seric char buf[MAXLINE]; 4319337Seric char *p; 432297Seric 4339057Seric /* 4349057Seric ** Output transcript of errors 4359057Seric */ 4369057Seric 4374086Seric (void) fflush(stdout); 4389542Seric p = queuename(e->e_parent, 'x'); 4399337Seric if ((xfile = fopen(p, "r")) == NULL) 4409057Seric { 4419337Seric syserr("Cannot open %s", p); 4429057Seric fprintf(fp, " ----- Transcript of session is unavailable -----\n"); 4439057Seric } 4449057Seric else 4459057Seric { 4469057Seric fprintf(fp, " ----- Transcript of session follows -----\n"); 4479542Seric if (e->e_xfp != NULL) 4489542Seric (void) fflush(e->e_xfp); 4499057Seric while (fgets(buf, sizeof buf, xfile) != NULL) 45010170Seric putline(buf, fp, m); 4519057Seric (void) fclose(xfile); 4529057Seric } 453297Seric errno = 0; 4544318Seric 4554318Seric /* 4564318Seric ** Output text of original message 4574318Seric */ 4584318Seric 4594289Seric if (NoReturn) 4604289Seric fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 4619542Seric else if (e->e_parent->e_dfp != NULL) 4624199Seric { 4635984Seric if (SendBody) 4645984Seric { 46510170Seric putline("\n", fp, m); 46610170Seric putline(" ----- Unsent message follows -----\n", fp, m); 4675984Seric (void) fflush(fp); 46810170Seric putheader(fp, m, e->e_parent); 46910170Seric putline("\n", fp, m); 47010170Seric putbody(fp, m, e->e_parent); 4715984Seric } 4725984Seric else 4735984Seric { 47410170Seric putline("\n", fp, m); 47510170Seric putline(" ----- Message header follows -----\n", fp, m); 4765984Seric (void) fflush(fp); 47710170Seric putheader(fp, m, e->e_parent); 4785984Seric } 4794199Seric } 4804199Seric else 48110170Seric { 48210170Seric putline("\n", fp, m); 48310170Seric putline(" ----- No message was collected -----\n", fp, m); 48410170Seric putline("\n", fp, m); 48510170Seric } 4864318Seric 4874318Seric /* 4884318Seric ** Cleanup and exit 4894318Seric */ 4904318Seric 491297Seric if (errno != 0) 4926978Seric syserr("errbody: I/O error"); 493297Seric } 494