1297Seric # include <pwd.h> 23313Seric # include "sendmail.h" 3297Seric 4*9057Seric SCCSID(@(#)savemail.c 3.46 11/04/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; 448250Seric 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; 2468439Seric queuename(ee, '\0'); 2476978Seric addheader("to", returnto->q_paddr, ee); 2486978Seric addheader("subject", msg, ee); 2494633Seric 2504633Seric /* fake up an address header for the from person */ 2516978Seric expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 2526978Seric if (parse(buf, &ee->e_from, -1) == NULL) 2534633Seric { 2544633Seric syserr("Can't parse myself!"); 2554633Seric ExitStat = EX_SOFTWARE; 2567045Seric returndepth--; 2574633Seric return (-1); 2584633Seric } 2597045Seric ee->e_sendqueue = returnto; 2605984Seric 2616978Seric /* push state into submessage */ 2626978Seric CurEnv = ee; 2636978Seric define('f', "$n"); 2646978Seric define('x', "Mail Delivery Subsystem"); 2655984Seric 2666978Seric /* actually deliver the error message */ 2677045Seric sendall(ee, FALSE); 2686978Seric 2697045Seric /* do any closing error processing */ 2707045Seric checkerrors(ee); 2716978Seric 2726978Seric /* restore state */ 2737811Seric dropenvelope(ee); 2746978Seric CurEnv = CurEnv->e_parent; 2757045Seric returndepth--; 2766978Seric 2777045Seric /* should check for delivery errors here */ 2784633Seric return (0); 2794633Seric } 2804633Seric /* 2816978Seric ** ERRBODY -- output the body of an error message. 2826978Seric ** 2836978Seric ** Typically this is a copy of the transcript plus a copy of the 2846978Seric ** original offending message. 2856978Seric ** 286297Seric ** Parameters: 287297Seric ** xfile -- the transcript file. 288297Seric ** fp -- the output file. 2896978Seric ** xdot -- if set, use the SMTP hidden dot algorithm. 290297Seric ** 291297Seric ** Returns: 292297Seric ** none 293297Seric ** 294297Seric ** Side Effects: 2956978Seric ** Outputs the body of an error message. 296297Seric */ 297297Seric 2986978Seric errbody(fp, m, xdot) 299297Seric register FILE *fp; 3004318Seric register struct mailer *m; 3014864Seric bool xdot; 302297Seric { 3036978Seric register FILE *xfile; 3043189Seric char buf[MAXLINE]; 3057124Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 306297Seric 307*9057Seric /* 308*9057Seric ** Output transcript of errors 309*9057Seric */ 310*9057Seric 3114086Seric (void) fflush(stdout); 3123189Seric if ((xfile = fopen(Transcript, "r")) == NULL) 313*9057Seric { 314297Seric syserr("Cannot open %s", Transcript); 315*9057Seric fprintf(fp, " ----- Transcript of session is unavailable -----\n"); 316*9057Seric } 317*9057Seric else 318*9057Seric { 319*9057Seric fprintf(fp, " ----- Transcript of session follows -----\n"); 320*9057Seric (void) fflush(Xscript); 321*9057Seric while (fgets(buf, sizeof buf, xfile) != NULL) 322*9057Seric putline(buf, fp, fullsmtp); 323*9057Seric (void) fclose(xfile); 324*9057Seric } 325297Seric errno = 0; 3264318Seric 3274318Seric /* 3284318Seric ** Output text of original message 3294318Seric */ 3304318Seric 3314289Seric if (NoReturn) 3324289Seric fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 3334289Seric else if (TempFile != NULL) 3344199Seric { 3355984Seric if (SendBody) 3365984Seric { 3375984Seric fprintf(fp, "\n ----- Unsent message follows -----\n"); 3385984Seric (void) fflush(fp); 3397006Seric putheader(fp, m, CurEnv->e_parent); 3406978Seric fprintf(fp, "\n"); 3417006Seric putbody(fp, m, xdot); 3425984Seric } 3435984Seric else 3445984Seric { 3455984Seric fprintf(fp, "\n ----- Message header follows -----\n"); 3465984Seric (void) fflush(fp); 3477006Seric putheader(fp, m, CurEnv); 3485984Seric } 3494199Seric } 3504199Seric else 3514199Seric fprintf(fp, "\n ----- No message was collected -----\n\n"); 3524318Seric 3534318Seric /* 3544318Seric ** Cleanup and exit 3554318Seric */ 3564318Seric 357297Seric if (errno != 0) 3586978Seric syserr("errbody: I/O error"); 359297Seric } 360