1297Seric # include <pwd.h> 23313Seric # include "sendmail.h" 3297Seric 4*8250Seric SCCSID(@(#)savemail.c 3.44 09/24/82); 5408Seric 6297Seric /* 7297Seric ** SAVEMAIL -- Save mail on error 8297Seric ** 9297Seric ** If the MailBack flag is set, mail it back to the originator 10297Seric ** together with an error message; otherwise, just put it in 11297Seric ** dead.letter in the user's home directory (if he exists on 12297Seric ** this machine). 13297Seric ** 14297Seric ** Parameters: 15297Seric ** none 16297Seric ** 17297Seric ** Returns: 18297Seric ** none 19297Seric ** 20297Seric ** Side Effects: 21297Seric ** Saves the letter, by writing or mailing it back to the 22297Seric ** sender, or by putting it in dead.letter in her home 23297Seric ** directory. 24297Seric */ 25297Seric 26297Seric savemail() 27297Seric { 28297Seric register struct passwd *pw; 29297Seric register FILE *xfile; 30297Seric char buf[MAXLINE+1]; 31297Seric extern struct passwd *getpwnam(); 32297Seric register char *p; 33297Seric extern char *ttypath(); 34297Seric static int exclusive; 355846Seric typedef int (*fnptr)(); 36297Seric 377361Seric # ifdef DEBUG 387676Seric if (tTd(6, 1)) 397361Seric printf("\nsavemail: exclusive %d\n", exclusive); 407361Seric # endif DEBUG 417361Seric 426978Seric if (exclusive++) 43297Seric return; 44*8250Seric if (CurEnv->e_class < 0) 456978Seric { 467053Seric message(Arpa_Info, "Dumping junk mail"); 476978Seric return; 486978Seric } 497053Seric ForceMail = TRUE; 507370Seric FatalErrors = FALSE; 51297Seric 52297Seric /* 53297Seric ** In the unhappy event we don't know who to return the mail 54297Seric ** to, make someone up. 55297Seric */ 56297Seric 577045Seric if (CurEnv->e_from.q_paddr == NULL) 58297Seric { 597045Seric if (parse("root", &CurEnv->e_from, 0) == NULL) 60297Seric { 61297Seric syserr("Cannot parse root!"); 62297Seric ExitStat = EX_SOFTWARE; 63297Seric finis(); 64297Seric } 65297Seric } 666904Seric CurEnv->e_to = NULL; 67297Seric 68297Seric /* 69401Seric ** If called from Eric Schmidt's network, do special mailback. 70401Seric ** Fundamentally, this is the mailback case except that 71401Seric ** it returns an OK exit status (assuming the return 72401Seric ** worked). 736989Seric ** Also, if the from address is not local, mail it back. 74297Seric */ 75297Seric 76401Seric if (BerkNet) 77297Seric { 78401Seric ExitStat = EX_OK; 796989Seric MailBack = TRUE; 80297Seric } 817045Seric if (!bitset(M_LOCAL, CurEnv->e_from.q_mailer->m_flags)) 826989Seric MailBack = TRUE; 83297Seric 84297Seric /* 85297Seric ** If writing back, do it. 86297Seric ** If the user is still logged in on the same terminal, 87297Seric ** then write the error messages back to hir (sic). 88297Seric ** If not, set the MailBack flag so that it will get 89297Seric ** mailed back instead. 90297Seric */ 91297Seric 92297Seric if (WriteBack) 93297Seric { 94297Seric p = ttypath(); 95297Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 96297Seric { 976989Seric MailBack = TRUE; 98297Seric errno = 0; 99297Seric } 100297Seric else 101297Seric { 1024712Seric (void) fflush(Xscript); 103401Seric xfile = fopen(Transcript, "r"); 104401Seric if (xfile == NULL) 105401Seric syserr("Cannot open %s", Transcript); 1066978Seric expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 1074162Seric printf("\r\nMessage from %s...\r\n", buf); 1084318Seric printf("Errors occurred while sending mail; transcript follows:\r\n"); 1094086Seric while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout)) 110297Seric fputs(buf, stdout); 111297Seric if (ferror(stdout)) 1124086Seric (void) syserr("savemail: stdout: write err"); 1134086Seric (void) fclose(xfile); 114297Seric } 115297Seric } 116297Seric 117297Seric /* 118297Seric ** If mailing back, do it. 119297Seric ** Throw away all further output. Don't do aliases, since 120297Seric ** this could cause loops, e.g., if joe mails to x:joe, 121297Seric ** and for some reason the network for x: is down, then 122297Seric ** the response gets sent to x:joe, which gives a 123297Seric ** response, etc. Also force the mail to be delivered 124297Seric ** even if a version of it has already been sent to the 125297Seric ** sender. 126297Seric */ 127297Seric 1283048Seric if (MailBack) 129297Seric { 1307203Seric if (CurEnv->e_errorqueue == NULL) 1318079Seric sendto(CurEnv->e_from.q_paddr, (ADDRESS *) NULL, 1328079Seric &CurEnv->e_errorqueue); 1338079Seric if (returntosender("Unable to deliver mail", 1348079Seric CurEnv->e_errorqueue, TRUE) == 0) 135297Seric return; 136297Seric } 137297Seric 138297Seric /* 139297Seric ** Save the message in dead.letter. 140297Seric ** If we weren't mailing back, and the user is local, we 141297Seric ** should save the message in dead.letter so that the 142297Seric ** poor person doesn't have to type it over again -- 143297Seric ** and we all know what poor typists programmers are. 1447361Seric ** However, if we are running a "smart" protocol, we don't 1457361Seric ** bother to return the message, since the other end is 1467361Seric ** expected to handle that. 147297Seric */ 148297Seric 1494712Seric if (ArpaMode) 1504162Seric return; 1514079Seric p = NULL; 1527045Seric if (CurEnv->e_from.q_mailer == LocalMailer) 153297Seric { 1547045Seric if (CurEnv->e_from.q_home != NULL) 1557045Seric p = CurEnv->e_from.q_home; 1567045Seric else if ((pw = getpwnam(CurEnv->e_from.q_user)) != NULL) 1574079Seric p = pw->pw_dir; 158297Seric } 1594079Seric if (p == NULL) 160297Seric { 1617045Seric syserr("Can't return mail to %s", CurEnv->e_from.q_paddr); 162297Seric # ifdef DEBUG 163297Seric p = "/usr/tmp"; 164297Seric # endif 165297Seric } 1664199Seric if (p != NULL && TempFile != NULL) 167297Seric { 1685315Seric auto ADDRESS *q; 1697053Seric bool oldverb = Verbose; 1705315Seric 171297Seric /* we have a home directory; open dead.letter */ 1727053Seric Verbose = TRUE; 1734167Seric message(Arpa_Info, "Saving message in dead.letter"); 1747053Seric Verbose = oldverb; 1754079Seric define('z', p); 1766978Seric expand("$z/dead.letter", buf, &buf[sizeof buf - 1], CurEnv); 1776904Seric CurEnv->e_to = buf; 1785315Seric q = NULL; 1798079Seric sendto(buf, (ADDRESS *) NULL, &q); 1806978Seric (void) deliver(q); 181297Seric } 182297Seric 183297Seric /* add terminator to writeback message */ 184297Seric if (WriteBack) 185297Seric printf("-----\r\n"); 186297Seric } 187297Seric /* 1884633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 1894633Seric ** 1904633Seric ** Parameters: 1914633Seric ** msg -- the explanatory message. 1927045Seric ** returnto -- the queue of people to send the message to. 1935984Seric ** sendbody -- if TRUE, also send back the body of the 1945984Seric ** message; otherwise just send the header. 1954633Seric ** 1964633Seric ** Returns: 1974633Seric ** zero -- if everything went ok. 1984633Seric ** else -- some error. 1994633Seric ** 2004633Seric ** Side Effects: 2014633Seric ** Returns the current message to the sender via 2024633Seric ** mail. 2034633Seric */ 2044633Seric 2055984Seric static bool SendBody; 2064633Seric 2077045Seric #define MAXRETURNS 6 /* max depth of returning messages */ 2087045Seric 2096978Seric returntosender(msg, returnto, sendbody) 2104633Seric char *msg; 2116978Seric ADDRESS *returnto; 2125984Seric bool sendbody; 2134633Seric { 2144633Seric char buf[MAXNAME]; 2156978Seric extern putheader(), errbody(); 2166978Seric register ENVELOPE *ee; 2176978Seric extern ENVELOPE *newenvelope(); 2186978Seric ENVELOPE errenvelope; 2197045Seric static int returndepth; 2204633Seric 2217287Seric # ifdef DEBUG 2227676Seric if (tTd(6, 1)) 2237287Seric { 2247287Seric printf("Return To Sender: msg=\"%s\", depth=%d, CurEnv=%x,\n", 2257287Seric msg, returndepth, CurEnv); 2267287Seric printf("\treturnto="); 2277287Seric printaddr(returnto, FALSE); 2287287Seric } 2297287Seric # endif DEBUG 2307287Seric 2317045Seric if (++returndepth >= MAXRETURNS) 2327045Seric { 2337045Seric if (returndepth != MAXRETURNS) 2347045Seric syserr("returntosender: infinite recursion on %s", returnto->q_paddr); 2357045Seric /* don't "unrecurse" and fake a clean exit */ 2367045Seric /* returndepth--; */ 2377045Seric return (0); 2387045Seric } 2397045Seric 2406989Seric NoAlias = TRUE; 2415984Seric SendBody = sendbody; 2428129Seric define('g', "$f"); 2436978Seric ee = newenvelope(&errenvelope); 2446978Seric ee->e_puthdr = putheader; 2456978Seric ee->e_putbody = errbody; 2466978Seric addheader("date", "$b", ee); 2476978Seric addheader("from", "$g (Mail Delivery Subsystem)", ee); 2486978Seric addheader("to", returnto->q_paddr, ee); 2496978Seric addheader("subject", msg, ee); 2504633Seric 2514633Seric /* fake up an address header for the from person */ 2526978Seric expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 2536978Seric if (parse(buf, &ee->e_from, -1) == NULL) 2544633Seric { 2554633Seric syserr("Can't parse myself!"); 2564633Seric ExitStat = EX_SOFTWARE; 2577045Seric returndepth--; 2584633Seric return (-1); 2594633Seric } 2607045Seric ee->e_sendqueue = returnto; 2615984Seric 2626978Seric /* push state into submessage */ 2636978Seric CurEnv = ee; 2646978Seric define('f', "$n"); 2656978Seric define('x', "Mail Delivery Subsystem"); 2665984Seric 2676978Seric /* actually deliver the error message */ 2687045Seric sendall(ee, FALSE); 2696978Seric 2707045Seric /* do any closing error processing */ 2717045Seric checkerrors(ee); 2726978Seric 2736978Seric /* restore state */ 2747811Seric dropenvelope(ee); 2756978Seric CurEnv = CurEnv->e_parent; 2767045Seric returndepth--; 2776978Seric 2787045Seric /* should check for delivery errors here */ 2794633Seric return (0); 2804633Seric } 2814633Seric /* 2826978Seric ** ERRBODY -- output the body of an error message. 2836978Seric ** 2846978Seric ** Typically this is a copy of the transcript plus a copy of the 2856978Seric ** original offending message. 2866978Seric ** 287297Seric ** Parameters: 288297Seric ** xfile -- the transcript file. 289297Seric ** fp -- the output file. 2906978Seric ** xdot -- if set, use the SMTP hidden dot algorithm. 291297Seric ** 292297Seric ** Returns: 293297Seric ** none 294297Seric ** 295297Seric ** Side Effects: 2966978Seric ** Outputs the body of an error message. 297297Seric */ 298297Seric 2996978Seric errbody(fp, m, xdot) 300297Seric register FILE *fp; 3014318Seric register struct mailer *m; 3024864Seric bool xdot; 303297Seric { 3046978Seric register FILE *xfile; 3053189Seric char buf[MAXLINE]; 3067124Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 307297Seric 3084086Seric (void) fflush(stdout); 3093189Seric if ((xfile = fopen(Transcript, "r")) == NULL) 310297Seric syserr("Cannot open %s", Transcript); 311297Seric errno = 0; 3124318Seric 3134318Seric /* 3144318Seric ** Output transcript of errors 3154318Seric */ 3164318Seric 3176978Seric fprintf(fp, " ----- Transcript of session follows -----\n"); 3184712Seric (void) fflush(Xscript); 3194086Seric while (fgets(buf, sizeof buf, xfile) != NULL) 3207124Seric putline(buf, fp, fullsmtp); 3214318Seric 3224318Seric /* 3234318Seric ** Output text of original message 3244318Seric */ 3254318Seric 3264289Seric if (NoReturn) 3274289Seric fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 3284289Seric else if (TempFile != NULL) 3294199Seric { 3305984Seric if (SendBody) 3315984Seric { 3325984Seric fprintf(fp, "\n ----- Unsent message follows -----\n"); 3335984Seric (void) fflush(fp); 3347006Seric putheader(fp, m, CurEnv->e_parent); 3356978Seric fprintf(fp, "\n"); 3367006Seric putbody(fp, m, xdot); 3375984Seric } 3385984Seric else 3395984Seric { 3405984Seric fprintf(fp, "\n ----- Message header follows -----\n"); 3415984Seric (void) fflush(fp); 3427006Seric putheader(fp, m, CurEnv); 3435984Seric } 3444199Seric } 3454199Seric else 3464199Seric fprintf(fp, "\n ----- No message was collected -----\n\n"); 3474318Seric 3484318Seric /* 3494318Seric ** Cleanup and exit 3504318Seric */ 3514318Seric 3524086Seric (void) fclose(xfile); 353297Seric if (errno != 0) 3546978Seric syserr("errbody: I/O error"); 355297Seric } 356