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*55425Seric static char sccsid[] = "@(#)savemail.c 5.20 (Berkeley) 07/20/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 { 8155012Seric if (parseaddr("root", &e->e_from, 0, '\0', e) == 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, 20955012Seric &e->e_errorqueue, e); 21024981Seric 21124981Seric /* deliver a cc: to the postmaster if desired */ 21224981Seric if (PostMasterCopy != NULL) 21324981Seric sendtolist(PostMasterCopy, 21424981Seric (ADDRESS *) NULL, 21555012Seric &e->e_errorqueue, e); 21624942Seric q = e->e_errorqueue; 21724942Seric } 21824942Seric else 21924942Seric { 22055012Seric if (parseaddr("postmaster", q, 0, '\0', e) == 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", 23055012Seric q, TRUE, e) == 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; 27655012Seric sendtolist(buf, (ADDRESS *) NULL, &q, e); 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 30154967Seric putfromline(fp, ProgMailer, e); 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. 33655012Seric ** e -- the current envelope. 3374633Seric ** 3384633Seric ** Returns: 3394633Seric ** zero -- if everything went ok. 3404633Seric ** else -- some error. 3414633Seric ** 3424633Seric ** Side Effects: 3434633Seric ** Returns the current message to the sender via 3444633Seric ** mail. 3454633Seric */ 3464633Seric 3475984Seric static bool SendBody; 3484633Seric 3497045Seric #define MAXRETURNS 6 /* max depth of returning messages */ 3507045Seric 35155012Seric returntosender(msg, returnq, sendbody, e) 3524633Seric char *msg; 35316479Seric ADDRESS *returnq; 3545984Seric bool sendbody; 35555012Seric register ENVELOPE *e; 3564633Seric { 3574633Seric char buf[MAXNAME]; 3586978Seric extern putheader(), errbody(); 3596978Seric register ENVELOPE *ee; 3606978Seric extern ENVELOPE *newenvelope(); 3616978Seric ENVELOPE errenvelope; 3627045Seric static int returndepth; 3639375Seric register ADDRESS *q; 3644633Seric 3657676Seric if (tTd(6, 1)) 3667287Seric { 36755012Seric printf("Return To Sender: msg=\"%s\", depth=%d, e=%x,\n", 36855012Seric msg, returndepth, e); 36924942Seric printf("\treturnq="); 37016479Seric printaddr(returnq, TRUE); 3717287Seric } 3727287Seric 3737045Seric if (++returndepth >= MAXRETURNS) 3747045Seric { 3757045Seric if (returndepth != MAXRETURNS) 37616479Seric syserr("returntosender: infinite recursion on %s", returnq->q_paddr); 3777045Seric /* don't "unrecurse" and fake a clean exit */ 3787045Seric /* returndepth--; */ 3797045Seric return (0); 3807045Seric } 3817045Seric 3825984Seric SendBody = sendbody; 38355012Seric define('g', "\001f", e); 38455012Seric define('<', "\001f", e); 3856978Seric ee = newenvelope(&errenvelope); 38624942Seric define('a', "\001b", ee); 3876978Seric ee->e_puthdr = putheader; 3886978Seric ee->e_putbody = errbody; 3899375Seric ee->e_flags |= EF_RESPONSE; 39055012Seric if (!bitset(EF_OLDSTYLE, e->e_flags)) 39145155Seric ee->e_flags &= ~EF_OLDSTYLE; 39216479Seric ee->e_sendqueue = returnq; 3939542Seric openxscript(ee); 39416479Seric for (q = returnq; q != NULL; q = q->q_next) 3959375Seric { 3969375Seric if (q->q_alias == NULL) 3979375Seric addheader("to", q->q_paddr, ee); 3989375Seric } 39924942Seric 40010845Seric (void) sprintf(buf, "Returned mail: %s", msg); 40110106Seric addheader("subject", buf, ee); 4024633Seric 4034633Seric /* fake up an address header for the from person */ 40455012Seric expand("\001n", buf, &buf[sizeof buf - 1], e); 40553181Seric ee->e_sender = ee->e_returnpath = newstr(buf); 40655012Seric if (parseaddr(buf, &ee->e_from, -1, '\0', e) == NULL) 4074633Seric { 4084633Seric syserr("Can't parse myself!"); 4094633Seric ExitStat = EX_SOFTWARE; 4107045Seric returndepth--; 4114633Seric return (-1); 4124633Seric } 41316159Seric loweraddr(&ee->e_from); 4145984Seric 4156978Seric /* push state into submessage */ 4166978Seric CurEnv = ee; 41716152Seric define('f', "\001n", ee); 4189375Seric define('x', "Mail Delivery Subsystem", ee); 41911291Seric eatheader(ee); 4205984Seric 4216978Seric /* actually deliver the error message */ 42214876Seric sendall(ee, SM_DEFAULT); 4236978Seric 4246978Seric /* restore state */ 4257811Seric dropenvelope(ee); 4266978Seric CurEnv = CurEnv->e_parent; 4277045Seric returndepth--; 4286978Seric 4297045Seric /* should check for delivery errors here */ 4304633Seric return (0); 4314633Seric } 4324633Seric /* 4336978Seric ** ERRBODY -- output the body of an error message. 4346978Seric ** 4356978Seric ** Typically this is a copy of the transcript plus a copy of the 4366978Seric ** original offending message. 4376978Seric ** 438297Seric ** Parameters: 439297Seric ** fp -- the output file. 44010170Seric ** m -- the mailer to output to. 4419542Seric ** e -- the envelope we are working in. 442297Seric ** 443297Seric ** Returns: 444297Seric ** none 445297Seric ** 446297Seric ** Side Effects: 4476978Seric ** Outputs the body of an error message. 448297Seric */ 449297Seric 45010170Seric errbody(fp, m, e) 451297Seric register FILE *fp; 4524318Seric register struct mailer *m; 4539542Seric register ENVELOPE *e; 454297Seric { 4556978Seric register FILE *xfile; 4563189Seric char buf[MAXLINE]; 4579337Seric char *p; 458297Seric 4599057Seric /* 46055372Seric ** Output error message header (if specified and available). 46155372Seric */ 46255372Seric 46355372Seric if (ErrMsgFile != NULL) 46455372Seric { 46555372Seric if (*ErrMsgFile == '/') 46655372Seric { 46755372Seric xfile = fopen(ErrMsgFile, "r"); 46855372Seric if (xfile != NULL) 46955372Seric { 47055372Seric while (fgets(buf, sizeof buf, xfile) != NULL) 471*55425Seric { 472*55425Seric expand(buf, buf, &buf[sizeof buf - 1], e); 47355372Seric putline(buf, fp, m); 474*55425Seric } 47555372Seric (void) fclose(xfile); 47655372Seric fprintf(fp, "\n"); 47755372Seric } 47855372Seric } 47955372Seric else 48055372Seric { 481*55425Seric expand(ErrMsgFile, buf, &buf[sizeof buf - 1], e); 482*55425Seric putline(buf, fp, m); 48355372Seric fprintf(fp, "\n"); 48455372Seric } 48555372Seric } 48655372Seric 48755372Seric /* 4889057Seric ** Output transcript of errors 4899057Seric */ 4909057Seric 4914086Seric (void) fflush(stdout); 4929542Seric p = queuename(e->e_parent, 'x'); 4939337Seric if ((xfile = fopen(p, "r")) == NULL) 4949057Seric { 4959337Seric syserr("Cannot open %s", p); 4969057Seric fprintf(fp, " ----- Transcript of session is unavailable -----\n"); 4979057Seric } 4989057Seric else 4999057Seric { 5009057Seric fprintf(fp, " ----- Transcript of session follows -----\n"); 5019542Seric if (e->e_xfp != NULL) 5029542Seric (void) fflush(e->e_xfp); 5039057Seric while (fgets(buf, sizeof buf, xfile) != NULL) 50410170Seric putline(buf, fp, m); 5059057Seric (void) fclose(xfile); 5069057Seric } 507297Seric errno = 0; 5084318Seric 5094318Seric /* 5104318Seric ** Output text of original message 5114318Seric */ 5124318Seric 5134289Seric if (NoReturn) 5144289Seric fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 5159542Seric else if (e->e_parent->e_dfp != NULL) 5164199Seric { 5175984Seric if (SendBody) 5185984Seric { 51910170Seric putline("\n", fp, m); 52010170Seric putline(" ----- Unsent message follows -----\n", fp, m); 5215984Seric (void) fflush(fp); 52210170Seric putheader(fp, m, e->e_parent); 52310170Seric putline("\n", fp, m); 52410170Seric putbody(fp, m, e->e_parent); 5255984Seric } 5265984Seric else 5275984Seric { 52810170Seric putline("\n", fp, m); 52910170Seric putline(" ----- Message header follows -----\n", fp, m); 5305984Seric (void) fflush(fp); 53110170Seric putheader(fp, m, e->e_parent); 5325984Seric } 5334199Seric } 5344199Seric else 53510170Seric { 53610170Seric putline("\n", fp, m); 53710170Seric putline(" ----- No message was collected -----\n", fp, m); 53810170Seric putline("\n", fp, m); 53910170Seric } 5404318Seric 5414318Seric /* 5424318Seric ** Cleanup and exit 5434318Seric */ 5444318Seric 545297Seric if (errno != 0) 5466978Seric syserr("errbody: I/O error"); 547297Seric } 548