122711Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 333731Sbostic * Copyright (c) 1988 Regents of the University of California. 433731Sbostic * All rights reserved. 533731Sbostic * 642829Sbostic * %sccs.include.redist.c% 733731Sbostic */ 822711Sdist 922711Sdist #ifndef lint 10*53181Seric static char sccsid[] = "@(#)savemail.c 5.16 (Berkeley) 04/16/92"; 1133731Sbostic #endif /* not lint */ 1222711Sdist 1336928Sbostic # include <sys/types.h> 14297Seric # include <pwd.h> 153313Seric # include "sendmail.h" 16297Seric 17297Seric /* 18297Seric ** SAVEMAIL -- Save mail on error 19297Seric ** 209375Seric ** If mailing back errors, mail it back to the originator 21297Seric ** together with an error message; otherwise, just put it in 22297Seric ** dead.letter in the user's home directory (if he exists on 23297Seric ** this machine). 24297Seric ** 25297Seric ** Parameters: 269337Seric ** e -- the envelope containing the message in error. 27297Seric ** 28297Seric ** Returns: 29297Seric ** none 30297Seric ** 31297Seric ** Side Effects: 32297Seric ** Saves the letter, by writing or mailing it back to the 33297Seric ** sender, or by putting it in dead.letter in her home 34297Seric ** directory. 35297Seric */ 36297Seric 3724942Seric /* defines for state machine */ 3824942Seric # define ESM_REPORT 0 /* report to sender's terminal */ 3924942Seric # define ESM_MAIL 1 /* mail back to sender */ 4024942Seric # define ESM_QUIET 2 /* messages have already been returned */ 4124942Seric # define ESM_DEADLETTER 3 /* save in ~/dead.letter */ 4224942Seric # define ESM_POSTMASTER 4 /* return to postmaster */ 4324942Seric # define ESM_USRTMP 5 /* save in /usr/tmp/dead.letter */ 4424942Seric # define ESM_PANIC 6 /* leave the locked queue/transcript files */ 4524942Seric # define ESM_DONE 7 /* the message is successfully delivered */ 4624942Seric 4724942Seric 489337Seric savemail(e) 499337Seric register ENVELOPE *e; 50297Seric { 51297Seric register struct passwd *pw; 5224942Seric register FILE *fp; 5324942Seric int state; 5424942Seric auto ADDRESS *q; 55297Seric char buf[MAXLINE+1]; 56297Seric extern struct passwd *getpwnam(); 57297Seric register char *p; 58297Seric extern char *ttypath(); 595846Seric typedef int (*fnptr)(); 60297Seric 617676Seric if (tTd(6, 1)) 6224979Seric printf("\nsavemail, ErrorMode = %c\n", ErrorMode); 637361Seric 649375Seric if (bitset(EF_RESPONSE, e->e_flags)) 65297Seric return; 669337Seric if (e->e_class < 0) 676978Seric { 687053Seric message(Arpa_Info, "Dumping junk mail"); 696978Seric return; 706978Seric } 717053Seric ForceMail = TRUE; 729337Seric e->e_flags &= ~EF_FATALERRS; 73297Seric 74297Seric /* 75297Seric ** In the unhappy event we don't know who to return the mail 76297Seric ** to, make someone up. 77297Seric */ 78297Seric 799337Seric if (e->e_from.q_paddr == NULL) 80297Seric { 8111447Seric if (parseaddr("root", &e->e_from, 0, '\0') == NULL) 82297Seric { 83297Seric syserr("Cannot parse root!"); 84297Seric ExitStat = EX_SOFTWARE; 85297Seric finis(); 86297Seric } 87297Seric } 889337Seric e->e_to = NULL; 89297Seric 90297Seric /* 9124942Seric ** Basic state machine. 9224942Seric ** 9324942Seric ** This machine runs through the following states: 9424942Seric ** 9524942Seric ** ESM_QUIET Errors have already been printed iff the 9624942Seric ** sender is local. 9724942Seric ** ESM_REPORT Report directly to the sender's terminal. 9824942Seric ** ESM_MAIL Mail response to the sender. 9924942Seric ** ESM_DEADLETTER Save response in ~/dead.letter. 10024942Seric ** ESM_POSTMASTER Mail response to the postmaster. 10124942Seric ** ESM_PANIC Save response anywhere possible. 102297Seric */ 103297Seric 10424942Seric /* determine starting state */ 10524942Seric switch (ErrorMode) 106297Seric { 10724942Seric case EM_WRITE: 10824942Seric state = ESM_REPORT; 10924942Seric break; 11024942Seric 11124942Seric case EM_BERKNET: 11224942Seric /* mail back, but return o.k. exit status */ 113401Seric ExitStat = EX_OK; 11424942Seric 11524942Seric /* fall through.... */ 11624942Seric 11724942Seric case EM_MAIL: 11824942Seric state = ESM_MAIL; 11924942Seric break; 12024942Seric 12124942Seric case EM_PRINT: 12224979Seric case '\0': 12324942Seric state = ESM_QUIET; 12424942Seric break; 12524942Seric 12624942Seric case EM_QUIET: 12724942Seric /* no need to return anything at all */ 12824942Seric return; 12924979Seric 13024979Seric default: 13124979Seric syserr("savemail: ErrorMode x%x\n"); 13224979Seric state = ESM_MAIL; 13324979Seric break; 134297Seric } 135297Seric 13624942Seric while (state != ESM_DONE) 137297Seric { 13824979Seric if (tTd(6, 5)) 13924979Seric printf(" state %d\n", state); 14024979Seric 14124942Seric switch (state) 142297Seric { 14324979Seric case ESM_QUIET: 14424979Seric if (e->e_from.q_mailer == LocalMailer) 14524979Seric state = ESM_DEADLETTER; 14624979Seric else 14724979Seric state = ESM_MAIL; 14824979Seric break; 14924979Seric 15024942Seric case ESM_REPORT: 15124942Seric 15224942Seric /* 15324942Seric ** If the user is still logged in on the same terminal, 15424942Seric ** then write the error messages back to hir (sic). 15524942Seric */ 15624942Seric 15724942Seric p = ttypath(); 15824942Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 15924942Seric { 16024942Seric state = ESM_MAIL; 16124942Seric break; 16224942Seric } 16324942Seric 16416152Seric expand("\001n", buf, &buf[sizeof buf - 1], e); 1659375Seric printf("\r\nMessage from %s...\r\n", buf); 1669375Seric printf("Errors occurred while sending mail.\r\n"); 1679542Seric if (e->e_xfp != NULL) 1689375Seric { 1699542Seric (void) fflush(e->e_xfp); 17024942Seric fp = fopen(queuename(e, 'x'), "r"); 1719375Seric } 1729375Seric else 17324942Seric fp = NULL; 17424942Seric if (fp == NULL) 1759375Seric { 1769337Seric syserr("Cannot open %s", queuename(e, 'x')); 1779375Seric printf("Transcript of session is unavailable.\r\n"); 1789375Seric } 1799375Seric else 1809375Seric { 1819375Seric printf("Transcript follows:\r\n"); 18224942Seric while (fgets(buf, sizeof buf, fp) != NULL && 1839375Seric !ferror(stdout)) 1849375Seric fputs(buf, stdout); 18524942Seric (void) fclose(fp); 1869375Seric } 18724942Seric printf("Original message will be saved in dead.letter.\r\n"); 18824942Seric state = ESM_DEADLETTER; 18924942Seric break; 190297Seric 19124942Seric case ESM_MAIL: 19224942Seric case ESM_POSTMASTER: 19324942Seric /* 19424942Seric ** If mailing back, do it. 19524942Seric ** Throw away all further output. Don't alias, 19624942Seric ** since this could cause loops, e.g., if joe 19724942Seric ** mails to joe@x, and for some reason the network 19824942Seric ** for @x is down, then the response gets sent to 19924942Seric ** joe@x, which gives a response, etc. Also force 20024942Seric ** the mail to be delivered even if a version of 20124942Seric ** it has already been sent to the sender. 20224942Seric */ 203297Seric 20424942Seric if (state == ESM_MAIL) 20524942Seric { 20624942Seric if (e->e_errorqueue == NULL) 20724942Seric sendtolist(e->e_from.q_paddr, 20824942Seric (ADDRESS *) NULL, 20924942Seric &e->e_errorqueue); 21024981Seric 21124981Seric /* deliver a cc: to the postmaster if desired */ 21224981Seric if (PostMasterCopy != NULL) 21324981Seric sendtolist(PostMasterCopy, 21424981Seric (ADDRESS *) NULL, 21524981Seric &e->e_errorqueue); 21624942Seric q = e->e_errorqueue; 21724942Seric } 21824942Seric else 21924942Seric { 22024955Seric if (parseaddr("postmaster", q, 0, '\0') == NULL) 22124942Seric { 22224942Seric syserr("cannot parse postmaster!"); 22324942Seric ExitStat = EX_SOFTWARE; 22424942Seric state = ESM_USRTMP; 22524942Seric break; 22624942Seric } 22724942Seric } 22824942Seric if (returntosender(e->e_message != NULL ? e->e_message : 22924942Seric "Unable to deliver mail", 23024942Seric q, TRUE) == 0) 23124942Seric { 23224942Seric state = ESM_DONE; 23324942Seric break; 23424942Seric } 235297Seric 23624942Seric state = state == ESM_MAIL ? ESM_POSTMASTER : ESM_USRTMP; 23724942Seric break; 238297Seric 23924942Seric case ESM_DEADLETTER: 24024942Seric /* 24124942Seric ** Save the message in dead.letter. 24224942Seric ** If we weren't mailing back, and the user is 24324942Seric ** local, we should save the message in 24424942Seric ** ~/dead.letter so that the poor person doesn't 24524942Seric ** have to type it over again -- and we all know 24624942Seric ** what poor typists UNIX users are. 24724942Seric */ 2485315Seric 24924942Seric p = NULL; 25024942Seric if (e->e_from.q_mailer == LocalMailer) 25124942Seric { 25224942Seric if (e->e_from.q_home != NULL) 25324942Seric p = e->e_from.q_home; 25424942Seric else if ((pw = getpwnam(e->e_from.q_user)) != NULL) 25524942Seric p = pw->pw_dir; 25624942Seric } 25724942Seric if (p == NULL) 25824942Seric { 25924942Seric syserr("Can't return mail to %s", e->e_from.q_paddr); 26024942Seric state = ESM_MAIL; 26124942Seric break; 26224942Seric } 26324942Seric if (e->e_dfp != NULL) 26424942Seric { 26524942Seric auto ADDRESS *q; 26624942Seric bool oldverb = Verbose; 26724942Seric 26824942Seric /* we have a home directory; open dead.letter */ 26924942Seric define('z', p, e); 27024942Seric expand("\001z/dead.letter", buf, &buf[sizeof buf - 1], e); 27124942Seric Verbose = TRUE; 27224942Seric message(Arpa_Info, "Saving message in %s", buf); 27324942Seric Verbose = oldverb; 27424942Seric e->e_to = buf; 27524942Seric q = NULL; 27624942Seric sendtolist(buf, (ADDRESS *) NULL, &q); 27724942Seric if (deliver(e, q) == 0) 27824942Seric state = ESM_DONE; 27924942Seric else 28024942Seric state = ESM_MAIL; 28124942Seric } 28225569Seric else 28325569Seric { 28425569Seric /* no data file -- try mailing back */ 28525569Seric state = ESM_MAIL; 28625569Seric } 28724942Seric break; 28824942Seric 28924942Seric case ESM_USRTMP: 29024942Seric /* 29124942Seric ** Log the mail in /usr/tmp/dead.letter. 29224942Seric */ 29324942Seric 29424942Seric fp = dfopen("/usr/tmp/dead.letter", "a"); 29524942Seric if (fp == NULL) 29624942Seric { 29724942Seric state = ESM_PANIC; 29824942Seric break; 29924942Seric } 30024942Seric 30124942Seric putfromline(fp, ProgMailer); 30224942Seric (*e->e_puthdr)(fp, ProgMailer, e); 30324942Seric putline("\n", fp, ProgMailer); 30424942Seric (*e->e_putbody)(fp, ProgMailer, e); 30524942Seric putline("\n", fp, ProgMailer); 30624942Seric (void) fflush(fp); 30724942Seric state = ferror(fp) ? ESM_PANIC : ESM_DONE; 30824942Seric (void) fclose(fp); 30924942Seric break; 31024942Seric 31124942Seric default: 31224942Seric syserr("savemail: unknown state %d", state); 31324942Seric 31424942Seric /* fall through ... */ 31524942Seric 31624942Seric case ESM_PANIC: 31724942Seric syserr("savemail: HELP!!!!"); 31824942Seric # ifdef LOG 31924942Seric if (LogLevel >= 1) 32024942Seric syslog(LOG_ALERT, "savemail: HELP!!!!"); 32124942Seric # endif LOG 32224942Seric 32324942Seric /* leave the locked queue & transcript files around */ 32424942Seric exit(EX_SOFTWARE); 32524942Seric } 326297Seric } 327297Seric } 328297Seric /* 3294633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 3304633Seric ** 3314633Seric ** Parameters: 3324633Seric ** msg -- the explanatory message. 33316479Seric ** returnq -- the queue of people to send the message to. 3345984Seric ** sendbody -- if TRUE, also send back the body of the 3355984Seric ** message; otherwise just send the header. 3364633Seric ** 3374633Seric ** Returns: 3384633Seric ** zero -- if everything went ok. 3394633Seric ** else -- some error. 3404633Seric ** 3414633Seric ** Side Effects: 3424633Seric ** Returns the current message to the sender via 3434633Seric ** mail. 3444633Seric */ 3454633Seric 3465984Seric static bool SendBody; 3474633Seric 3487045Seric #define MAXRETURNS 6 /* max depth of returning messages */ 3497045Seric 35016479Seric returntosender(msg, returnq, sendbody) 3514633Seric char *msg; 35216479Seric ADDRESS *returnq; 3535984Seric bool sendbody; 3544633Seric { 3554633Seric char buf[MAXNAME]; 3566978Seric extern putheader(), errbody(); 3576978Seric register ENVELOPE *ee; 3586978Seric extern ENVELOPE *newenvelope(); 3596978Seric ENVELOPE errenvelope; 3607045Seric static int returndepth; 3619375Seric register ADDRESS *q; 3624633Seric 3637676Seric if (tTd(6, 1)) 3647287Seric { 3657287Seric printf("Return To Sender: msg=\"%s\", depth=%d, CurEnv=%x,\n", 3667287Seric msg, returndepth, CurEnv); 36724942Seric printf("\treturnq="); 36816479Seric printaddr(returnq, TRUE); 3697287Seric } 3707287Seric 3717045Seric if (++returndepth >= MAXRETURNS) 3727045Seric { 3737045Seric if (returndepth != MAXRETURNS) 37416479Seric syserr("returntosender: infinite recursion on %s", returnq->q_paddr); 3757045Seric /* don't "unrecurse" and fake a clean exit */ 3767045Seric /* returndepth--; */ 3777045Seric return (0); 3787045Seric } 3797045Seric 3805984Seric SendBody = sendbody; 38116152Seric define('g', "\001f", CurEnv); 38251951Seric define('<', "\001f", CurEnv); 3836978Seric ee = newenvelope(&errenvelope); 38424942Seric define('a', "\001b", ee); 3856978Seric ee->e_puthdr = putheader; 3866978Seric ee->e_putbody = errbody; 3879375Seric ee->e_flags |= EF_RESPONSE; 38845155Seric if (!bitset(EF_OLDSTYLE, CurEnv->e_flags)) 38945155Seric ee->e_flags &= ~EF_OLDSTYLE; 39016479Seric ee->e_sendqueue = returnq; 3919542Seric openxscript(ee); 39216479Seric for (q = returnq; q != NULL; q = q->q_next) 3939375Seric { 3949375Seric if (q->q_alias == NULL) 3959375Seric addheader("to", q->q_paddr, ee); 3969375Seric } 39724942Seric 39810845Seric (void) sprintf(buf, "Returned mail: %s", msg); 39910106Seric addheader("subject", buf, ee); 4004633Seric 4014633Seric /* fake up an address header for the from person */ 40216152Seric expand("\001n", buf, &buf[sizeof buf - 1], CurEnv); 403*53181Seric ee->e_sender = ee->e_returnpath = newstr(buf); 40411447Seric if (parseaddr(buf, &ee->e_from, -1, '\0') == NULL) 4054633Seric { 4064633Seric syserr("Can't parse myself!"); 4074633Seric ExitStat = EX_SOFTWARE; 4087045Seric returndepth--; 4094633Seric return (-1); 4104633Seric } 41116159Seric loweraddr(&ee->e_from); 4125984Seric 4136978Seric /* push state into submessage */ 4146978Seric CurEnv = ee; 41516152Seric define('f', "\001n", ee); 4169375Seric define('x', "Mail Delivery Subsystem", ee); 41711291Seric eatheader(ee); 4185984Seric 4196978Seric /* actually deliver the error message */ 42014876Seric sendall(ee, SM_DEFAULT); 4216978Seric 4226978Seric /* restore state */ 4237811Seric dropenvelope(ee); 4246978Seric CurEnv = CurEnv->e_parent; 4257045Seric returndepth--; 4266978Seric 4277045Seric /* should check for delivery errors here */ 4284633Seric return (0); 4294633Seric } 4304633Seric /* 4316978Seric ** ERRBODY -- output the body of an error message. 4326978Seric ** 4336978Seric ** Typically this is a copy of the transcript plus a copy of the 4346978Seric ** original offending message. 4356978Seric ** 436297Seric ** Parameters: 437297Seric ** fp -- the output file. 43810170Seric ** m -- the mailer to output to. 4399542Seric ** e -- the envelope we are working in. 440297Seric ** 441297Seric ** Returns: 442297Seric ** none 443297Seric ** 444297Seric ** Side Effects: 4456978Seric ** Outputs the body of an error message. 446297Seric */ 447297Seric 44810170Seric errbody(fp, m, e) 449297Seric register FILE *fp; 4504318Seric register struct mailer *m; 4519542Seric register ENVELOPE *e; 452297Seric { 4536978Seric register FILE *xfile; 4543189Seric char buf[MAXLINE]; 4559337Seric char *p; 456297Seric 4579057Seric /* 4589057Seric ** Output transcript of errors 4599057Seric */ 4609057Seric 4614086Seric (void) fflush(stdout); 4629542Seric p = queuename(e->e_parent, 'x'); 4639337Seric if ((xfile = fopen(p, "r")) == NULL) 4649057Seric { 4659337Seric syserr("Cannot open %s", p); 4669057Seric fprintf(fp, " ----- Transcript of session is unavailable -----\n"); 4679057Seric } 4689057Seric else 4699057Seric { 4709057Seric fprintf(fp, " ----- Transcript of session follows -----\n"); 4719542Seric if (e->e_xfp != NULL) 4729542Seric (void) fflush(e->e_xfp); 4739057Seric while (fgets(buf, sizeof buf, xfile) != NULL) 47410170Seric putline(buf, fp, m); 4759057Seric (void) fclose(xfile); 4769057Seric } 477297Seric errno = 0; 4784318Seric 4794318Seric /* 4804318Seric ** Output text of original message 4814318Seric */ 4824318Seric 4834289Seric if (NoReturn) 4844289Seric fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 4859542Seric else if (e->e_parent->e_dfp != NULL) 4864199Seric { 4875984Seric if (SendBody) 4885984Seric { 48910170Seric putline("\n", fp, m); 49010170Seric putline(" ----- Unsent message follows -----\n", fp, m); 4915984Seric (void) fflush(fp); 49210170Seric putheader(fp, m, e->e_parent); 49310170Seric putline("\n", fp, m); 49410170Seric putbody(fp, m, e->e_parent); 4955984Seric } 4965984Seric else 4975984Seric { 49810170Seric putline("\n", fp, m); 49910170Seric putline(" ----- Message header follows -----\n", fp, m); 5005984Seric (void) fflush(fp); 50110170Seric putheader(fp, m, e->e_parent); 5025984Seric } 5034199Seric } 5044199Seric else 50510170Seric { 50610170Seric putline("\n", fp, m); 50710170Seric putline(" ----- No message was collected -----\n", fp, m); 50810170Seric putline("\n", fp, m); 50910170Seric } 5104318Seric 5114318Seric /* 5124318Seric ** Cleanup and exit 5134318Seric */ 5144318Seric 515297Seric if (errno != 0) 5166978Seric syserr("errbody: I/O error"); 517297Seric } 518