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*25569Seric static char SccsId[] = "@(#)savemail.c 5.7 (Berkeley) 12/07/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 3824942Seric /* defines for state machine */ 3924942Seric # define ESM_REPORT 0 /* report to sender's terminal */ 4024942Seric # define ESM_MAIL 1 /* mail back to sender */ 4124942Seric # define ESM_QUIET 2 /* messages have already been returned */ 4224942Seric # define ESM_DEADLETTER 3 /* save in ~/dead.letter */ 4324942Seric # define ESM_POSTMASTER 4 /* return to postmaster */ 4424942Seric # define ESM_USRTMP 5 /* save in /usr/tmp/dead.letter */ 4524942Seric # define ESM_PANIC 6 /* leave the locked queue/transcript files */ 4624942Seric # define ESM_DONE 7 /* the message is successfully delivered */ 4724942Seric 4824942Seric 499337Seric savemail(e) 509337Seric register ENVELOPE *e; 51297Seric { 52297Seric register struct passwd *pw; 5324942Seric register FILE *fp; 5424942Seric int state; 5524942Seric 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)) 6424979Seric printf("\nsavemail, ErrorMode = %c\n", ErrorMode); 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 /* 9424942Seric ** Basic state machine. 9524942Seric ** 9624942Seric ** This machine runs through the following states: 9724942Seric ** 9824942Seric ** ESM_QUIET Errors have already been printed iff the 9924942Seric ** sender is local. 10024942Seric ** ESM_REPORT Report directly to the sender's terminal. 10124942Seric ** ESM_MAIL Mail response to the sender. 10224942Seric ** ESM_DEADLETTER Save response in ~/dead.letter. 10324942Seric ** ESM_POSTMASTER Mail response to the postmaster. 10424942Seric ** ESM_PANIC Save response anywhere possible. 105297Seric */ 106297Seric 10724942Seric /* determine starting state */ 10824942Seric switch (ErrorMode) 109297Seric { 11024942Seric case EM_WRITE: 11124942Seric state = ESM_REPORT; 11224942Seric break; 11324942Seric 11424942Seric case EM_BERKNET: 11524942Seric /* mail back, but return o.k. exit status */ 116401Seric ExitStat = EX_OK; 11724942Seric 11824942Seric /* fall through.... */ 11924942Seric 12024942Seric case EM_MAIL: 12124942Seric state = ESM_MAIL; 12224942Seric break; 12324942Seric 12424942Seric case EM_PRINT: 12524979Seric case '\0': 12624942Seric state = ESM_QUIET; 12724942Seric break; 12824942Seric 12924942Seric case EM_QUIET: 13024942Seric /* no need to return anything at all */ 13124942Seric return; 13224979Seric 13324979Seric default: 13424979Seric syserr("savemail: ErrorMode x%x\n"); 13524979Seric state = ESM_MAIL; 13624979Seric break; 137297Seric } 138297Seric 13924942Seric while (state != ESM_DONE) 140297Seric { 14124979Seric # ifdef DEBUG 14224979Seric if (tTd(6, 5)) 14324979Seric printf(" state %d\n", state); 14424979Seric # endif DEBUG 14524979Seric 14624942Seric switch (state) 147297Seric { 14824979Seric case ESM_QUIET: 14924979Seric if (e->e_from.q_mailer == LocalMailer) 15024979Seric state = ESM_DEADLETTER; 15124979Seric else 15224979Seric state = ESM_MAIL; 15324979Seric break; 15424979Seric 15524942Seric case ESM_REPORT: 15624942Seric 15724942Seric /* 15824942Seric ** If the user is still logged in on the same terminal, 15924942Seric ** then write the error messages back to hir (sic). 16024942Seric */ 16124942Seric 16224942Seric p = ttypath(); 16324942Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 16424942Seric { 16524942Seric state = ESM_MAIL; 16624942Seric break; 16724942Seric } 16824942Seric 16916152Seric expand("\001n", buf, &buf[sizeof buf - 1], e); 1709375Seric printf("\r\nMessage from %s...\r\n", buf); 1719375Seric printf("Errors occurred while sending mail.\r\n"); 1729542Seric if (e->e_xfp != NULL) 1739375Seric { 1749542Seric (void) fflush(e->e_xfp); 17524942Seric fp = fopen(queuename(e, 'x'), "r"); 1769375Seric } 1779375Seric else 17824942Seric fp = NULL; 17924942Seric if (fp == NULL) 1809375Seric { 1819337Seric syserr("Cannot open %s", queuename(e, 'x')); 1829375Seric printf("Transcript of session is unavailable.\r\n"); 1839375Seric } 1849375Seric else 1859375Seric { 1869375Seric printf("Transcript follows:\r\n"); 18724942Seric while (fgets(buf, sizeof buf, fp) != NULL && 1889375Seric !ferror(stdout)) 1899375Seric fputs(buf, stdout); 19024942Seric (void) fclose(fp); 1919375Seric } 19224942Seric printf("Original message will be saved in dead.letter.\r\n"); 193297Seric if (ferror(stdout)) 1944086Seric (void) syserr("savemail: stdout: write err"); 19524942Seric state = ESM_DEADLETTER; 19624942Seric break; 197297Seric 19824942Seric case ESM_MAIL: 19924942Seric case ESM_POSTMASTER: 20024942Seric /* 20124942Seric ** If mailing back, do it. 20224942Seric ** Throw away all further output. Don't alias, 20324942Seric ** since this could cause loops, e.g., if joe 20424942Seric ** mails to joe@x, and for some reason the network 20524942Seric ** for @x is down, then the response gets sent to 20624942Seric ** joe@x, which gives a response, etc. Also force 20724942Seric ** the mail to be delivered even if a version of 20824942Seric ** it has already been sent to the sender. 20924942Seric */ 210297Seric 21124942Seric if (state == ESM_MAIL) 21224942Seric { 21324942Seric if (e->e_errorqueue == NULL) 21424942Seric sendtolist(e->e_from.q_paddr, 21524942Seric (ADDRESS *) NULL, 21624942Seric &e->e_errorqueue); 21724981Seric 21824981Seric /* deliver a cc: to the postmaster if desired */ 21924981Seric if (PostMasterCopy != NULL) 22024981Seric sendtolist(PostMasterCopy, 22124981Seric (ADDRESS *) NULL, 22224981Seric &e->e_errorqueue); 22324942Seric q = e->e_errorqueue; 22424942Seric } 22524942Seric else 22624942Seric { 22724955Seric if (parseaddr("postmaster", q, 0, '\0') == NULL) 22824942Seric { 22924942Seric syserr("cannot parse postmaster!"); 23024942Seric ExitStat = EX_SOFTWARE; 23124942Seric state = ESM_USRTMP; 23224942Seric break; 23324942Seric } 23424942Seric } 23524942Seric if (returntosender(e->e_message != NULL ? e->e_message : 23624942Seric "Unable to deliver mail", 23724942Seric q, TRUE) == 0) 23824942Seric { 23924942Seric state = ESM_DONE; 24024942Seric break; 24124942Seric } 242297Seric 24324942Seric state = state == ESM_MAIL ? ESM_POSTMASTER : ESM_USRTMP; 24424942Seric break; 245297Seric 24624942Seric case ESM_DEADLETTER: 24724942Seric /* 24824942Seric ** Save the message in dead.letter. 24924942Seric ** If we weren't mailing back, and the user is 25024942Seric ** local, we should save the message in 25124942Seric ** ~/dead.letter so that the poor person doesn't 25224942Seric ** have to type it over again -- and we all know 25324942Seric ** what poor typists UNIX users are. 25424942Seric */ 2555315Seric 25624942Seric p = NULL; 25724942Seric if (e->e_from.q_mailer == LocalMailer) 25824942Seric { 25924942Seric if (e->e_from.q_home != NULL) 26024942Seric p = e->e_from.q_home; 26124942Seric else if ((pw = getpwnam(e->e_from.q_user)) != NULL) 26224942Seric p = pw->pw_dir; 26324942Seric } 26424942Seric if (p == NULL) 26524942Seric { 26624942Seric syserr("Can't return mail to %s", e->e_from.q_paddr); 26724942Seric state = ESM_MAIL; 26824942Seric break; 26924942Seric } 27024942Seric if (e->e_dfp != NULL) 27124942Seric { 27224942Seric auto ADDRESS *q; 27324942Seric bool oldverb = Verbose; 27424942Seric 27524942Seric /* we have a home directory; open dead.letter */ 27624942Seric define('z', p, e); 27724942Seric expand("\001z/dead.letter", buf, &buf[sizeof buf - 1], e); 27824942Seric Verbose = TRUE; 27924942Seric message(Arpa_Info, "Saving message in %s", buf); 28024942Seric Verbose = oldverb; 28124942Seric e->e_to = buf; 28224942Seric q = NULL; 28324942Seric sendtolist(buf, (ADDRESS *) NULL, &q); 28424942Seric if (deliver(e, q) == 0) 28524942Seric state = ESM_DONE; 28624942Seric else 28724942Seric state = ESM_MAIL; 28824942Seric } 289*25569Seric else 290*25569Seric { 291*25569Seric /* no data file -- try mailing back */ 292*25569Seric state = ESM_MAIL; 293*25569Seric } 29424942Seric break; 29524942Seric 29624942Seric case ESM_USRTMP: 29724942Seric /* 29824942Seric ** Log the mail in /usr/tmp/dead.letter. 29924942Seric */ 30024942Seric 30124942Seric fp = dfopen("/usr/tmp/dead.letter", "a"); 30224942Seric if (fp == NULL) 30324942Seric { 30424942Seric state = ESM_PANIC; 30524942Seric break; 30624942Seric } 30724942Seric 30824942Seric putfromline(fp, ProgMailer); 30924942Seric (*e->e_puthdr)(fp, ProgMailer, e); 31024942Seric putline("\n", fp, ProgMailer); 31124942Seric (*e->e_putbody)(fp, ProgMailer, e); 31224942Seric putline("\n", fp, ProgMailer); 31324942Seric (void) fflush(fp); 31424942Seric state = ferror(fp) ? ESM_PANIC : ESM_DONE; 31524942Seric (void) fclose(fp); 31624942Seric break; 31724942Seric 31824942Seric default: 31924942Seric syserr("savemail: unknown state %d", state); 32024942Seric 32124942Seric /* fall through ... */ 32224942Seric 32324942Seric case ESM_PANIC: 32424942Seric syserr("savemail: HELP!!!!"); 32524942Seric # ifdef LOG 32624942Seric if (LogLevel >= 1) 32724942Seric syslog(LOG_ALERT, "savemail: HELP!!!!"); 32824942Seric # endif LOG 32924942Seric 33024942Seric /* leave the locked queue & transcript files around */ 33124942Seric exit(EX_SOFTWARE); 33224942Seric } 333297Seric } 334297Seric } 335297Seric /* 3364633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 3374633Seric ** 3384633Seric ** Parameters: 3394633Seric ** msg -- the explanatory message. 34016479Seric ** returnq -- the queue of people to send the message to. 3415984Seric ** sendbody -- if TRUE, also send back the body of the 3425984Seric ** message; otherwise just send the header. 3434633Seric ** 3444633Seric ** Returns: 3454633Seric ** zero -- if everything went ok. 3464633Seric ** else -- some error. 3474633Seric ** 3484633Seric ** Side Effects: 3494633Seric ** Returns the current message to the sender via 3504633Seric ** mail. 3514633Seric */ 3524633Seric 3535984Seric static bool SendBody; 3544633Seric 3557045Seric #define MAXRETURNS 6 /* max depth of returning messages */ 3567045Seric 35716479Seric returntosender(msg, returnq, sendbody) 3584633Seric char *msg; 35916479Seric ADDRESS *returnq; 3605984Seric bool sendbody; 3614633Seric { 3624633Seric char buf[MAXNAME]; 3636978Seric extern putheader(), errbody(); 3646978Seric register ENVELOPE *ee; 3656978Seric extern ENVELOPE *newenvelope(); 3666978Seric ENVELOPE errenvelope; 3677045Seric static int returndepth; 3689375Seric register ADDRESS *q; 3694633Seric 3707287Seric # ifdef DEBUG 3717676Seric if (tTd(6, 1)) 3727287Seric { 3737287Seric printf("Return To Sender: msg=\"%s\", depth=%d, CurEnv=%x,\n", 3747287Seric msg, returndepth, CurEnv); 37524942Seric printf("\treturnq="); 37616479Seric printaddr(returnq, TRUE); 3777287Seric } 3787287Seric # endif DEBUG 3797287Seric 3807045Seric if (++returndepth >= MAXRETURNS) 3817045Seric { 3827045Seric if (returndepth != MAXRETURNS) 38316479Seric syserr("returntosender: infinite recursion on %s", returnq->q_paddr); 3847045Seric /* don't "unrecurse" and fake a clean exit */ 3857045Seric /* returndepth--; */ 3867045Seric return (0); 3877045Seric } 3887045Seric 3895984Seric SendBody = sendbody; 39016152Seric define('g', "\001f", CurEnv); 3916978Seric ee = newenvelope(&errenvelope); 39224942Seric define('a', "\001b", ee); 3936978Seric ee->e_puthdr = putheader; 3946978Seric ee->e_putbody = errbody; 3959375Seric ee->e_flags |= EF_RESPONSE; 39616479Seric ee->e_sendqueue = returnq; 3979542Seric openxscript(ee); 39816479Seric for (q = returnq; q != NULL; q = q->q_next) 3999375Seric { 4009375Seric if (q->q_alias == NULL) 4019375Seric addheader("to", q->q_paddr, ee); 4029375Seric } 40324942Seric 40410845Seric (void) sprintf(buf, "Returned mail: %s", msg); 40510106Seric addheader("subject", buf, ee); 4064633Seric 4074633Seric /* fake up an address header for the from person */ 40816152Seric expand("\001n", buf, &buf[sizeof buf - 1], CurEnv); 40911447Seric if (parseaddr(buf, &ee->e_from, -1, '\0') == NULL) 4104633Seric { 4114633Seric syserr("Can't parse myself!"); 4124633Seric ExitStat = EX_SOFTWARE; 4137045Seric returndepth--; 4144633Seric return (-1); 4154633Seric } 41616159Seric loweraddr(&ee->e_from); 4175984Seric 4186978Seric /* push state into submessage */ 4196978Seric CurEnv = ee; 42016152Seric define('f', "\001n", ee); 4219375Seric define('x', "Mail Delivery Subsystem", ee); 42211291Seric eatheader(ee); 4235984Seric 4246978Seric /* actually deliver the error message */ 42514876Seric sendall(ee, SM_DEFAULT); 4266978Seric 4276978Seric /* restore state */ 4287811Seric dropenvelope(ee); 4296978Seric CurEnv = CurEnv->e_parent; 4307045Seric returndepth--; 4316978Seric 4327045Seric /* should check for delivery errors here */ 4334633Seric return (0); 4344633Seric } 4354633Seric /* 4366978Seric ** ERRBODY -- output the body of an error message. 4376978Seric ** 4386978Seric ** Typically this is a copy of the transcript plus a copy of the 4396978Seric ** original offending message. 4406978Seric ** 441297Seric ** Parameters: 442297Seric ** fp -- the output file. 44310170Seric ** m -- the mailer to output to. 4449542Seric ** e -- the envelope we are working in. 445297Seric ** 446297Seric ** Returns: 447297Seric ** none 448297Seric ** 449297Seric ** Side Effects: 4506978Seric ** Outputs the body of an error message. 451297Seric */ 452297Seric 45310170Seric errbody(fp, m, e) 454297Seric register FILE *fp; 4554318Seric register struct mailer *m; 4569542Seric register ENVELOPE *e; 457297Seric { 4586978Seric register FILE *xfile; 4593189Seric char buf[MAXLINE]; 4609337Seric char *p; 461297Seric 4629057Seric /* 4639057Seric ** Output transcript of errors 4649057Seric */ 4659057Seric 4664086Seric (void) fflush(stdout); 4679542Seric p = queuename(e->e_parent, 'x'); 4689337Seric if ((xfile = fopen(p, "r")) == NULL) 4699057Seric { 4709337Seric syserr("Cannot open %s", p); 4719057Seric fprintf(fp, " ----- Transcript of session is unavailable -----\n"); 4729057Seric } 4739057Seric else 4749057Seric { 4759057Seric fprintf(fp, " ----- Transcript of session follows -----\n"); 4769542Seric if (e->e_xfp != NULL) 4779542Seric (void) fflush(e->e_xfp); 4789057Seric while (fgets(buf, sizeof buf, xfile) != NULL) 47910170Seric putline(buf, fp, m); 4809057Seric (void) fclose(xfile); 4819057Seric } 482297Seric errno = 0; 4834318Seric 4844318Seric /* 4854318Seric ** Output text of original message 4864318Seric */ 4874318Seric 4884289Seric if (NoReturn) 4894289Seric fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 4909542Seric else if (e->e_parent->e_dfp != NULL) 4914199Seric { 4925984Seric if (SendBody) 4935984Seric { 49410170Seric putline("\n", fp, m); 49510170Seric putline(" ----- Unsent message follows -----\n", fp, m); 4965984Seric (void) fflush(fp); 49710170Seric putheader(fp, m, e->e_parent); 49810170Seric putline("\n", fp, m); 49910170Seric putbody(fp, m, e->e_parent); 5005984Seric } 5015984Seric else 5025984Seric { 50310170Seric putline("\n", fp, m); 50410170Seric putline(" ----- Message header follows -----\n", fp, m); 5055984Seric (void) fflush(fp); 50610170Seric putheader(fp, m, e->e_parent); 5075984Seric } 5084199Seric } 5094199Seric else 51010170Seric { 51110170Seric putline("\n", fp, m); 51210170Seric putline(" ----- No message was collected -----\n", fp, m); 51310170Seric putline("\n", fp, m); 51410170Seric } 5154318Seric 5164318Seric /* 5174318Seric ** Cleanup and exit 5184318Seric */ 5194318Seric 520297Seric if (errno != 0) 5216978Seric syserr("errbody: I/O error"); 522297Seric } 523