1297Seric # include <pwd.h> 23313Seric # include "sendmail.h" 3297Seric 4*7203Seric SCCSID(@(#)savemail.c 3.35 06/16/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)(); 366978Seric extern ENVELOPE *newenvelope(); 37297Seric 386978Seric if (exclusive++) 39297Seric return; 406978Seric if (CurEnv->e_class <= PRI_JUNK) 416978Seric { 427053Seric message(Arpa_Info, "Dumping junk mail"); 436978Seric return; 446978Seric } 457053Seric ForceMail = TRUE; 46297Seric 47297Seric /* 48297Seric ** In the unhappy event we don't know who to return the mail 49297Seric ** to, make someone up. 50297Seric */ 51297Seric 527045Seric if (CurEnv->e_from.q_paddr == NULL) 53297Seric { 547045Seric if (parse("root", &CurEnv->e_from, 0) == NULL) 55297Seric { 56297Seric syserr("Cannot parse root!"); 57297Seric ExitStat = EX_SOFTWARE; 58297Seric finis(); 59297Seric } 60297Seric } 616904Seric CurEnv->e_to = NULL; 62297Seric 63297Seric /* 64401Seric ** If called from Eric Schmidt's network, do special mailback. 65401Seric ** Fundamentally, this is the mailback case except that 66401Seric ** it returns an OK exit status (assuming the return 67401Seric ** worked). 686989Seric ** Also, if the from address is not local, mail it back. 69297Seric */ 70297Seric 71401Seric if (BerkNet) 72297Seric { 73401Seric ExitStat = EX_OK; 746989Seric MailBack = TRUE; 75297Seric } 767045Seric if (!bitset(M_LOCAL, CurEnv->e_from.q_mailer->m_flags)) 776989Seric MailBack = TRUE; 78297Seric 79297Seric /* 80297Seric ** If writing back, do it. 81297Seric ** If the user is still logged in on the same terminal, 82297Seric ** then write the error messages back to hir (sic). 83297Seric ** If not, set the MailBack flag so that it will get 84297Seric ** mailed back instead. 85297Seric */ 86297Seric 87297Seric if (WriteBack) 88297Seric { 89297Seric p = ttypath(); 90297Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 91297Seric { 926989Seric MailBack = TRUE; 93297Seric errno = 0; 94297Seric } 95297Seric else 96297Seric { 974712Seric (void) fflush(Xscript); 98401Seric xfile = fopen(Transcript, "r"); 99401Seric if (xfile == NULL) 100401Seric syserr("Cannot open %s", Transcript); 1016978Seric expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 1024162Seric printf("\r\nMessage from %s...\r\n", buf); 1034318Seric printf("Errors occurred while sending mail; transcript follows:\r\n"); 1044086Seric while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout)) 105297Seric fputs(buf, stdout); 106297Seric if (ferror(stdout)) 1074086Seric (void) syserr("savemail: stdout: write err"); 1084086Seric (void) fclose(xfile); 109297Seric } 110297Seric } 111297Seric 112297Seric /* 113297Seric ** If mailing back, do it. 114297Seric ** Throw away all further output. Don't do aliases, since 115297Seric ** this could cause loops, e.g., if joe mails to x:joe, 116297Seric ** and for some reason the network for x: is down, then 117297Seric ** the response gets sent to x:joe, which gives a 118297Seric ** response, etc. Also force the mail to be delivered 119297Seric ** even if a version of it has already been sent to the 120297Seric ** sender. 121297Seric */ 122297Seric 1233048Seric if (MailBack) 124297Seric { 125*7203Seric if (CurEnv->e_errorqueue == NULL) 126*7203Seric sendto(CurEnv->e_from.q_paddr, 1, NULL, &CurEnv->e_errorqueue); 1277045Seric if (returntosender("Unable to deliver mail", CurEnv->e_errorqueue, TRUE) == 0) 128297Seric return; 129297Seric } 130297Seric 131297Seric /* 132297Seric ** Save the message in dead.letter. 133297Seric ** If we weren't mailing back, and the user is local, we 134297Seric ** should save the message in dead.letter so that the 135297Seric ** poor person doesn't have to type it over again -- 136297Seric ** and we all know what poor typists programmers are. 137297Seric */ 138297Seric 1394712Seric if (ArpaMode) 1404162Seric return; 1414079Seric p = NULL; 1427045Seric if (CurEnv->e_from.q_mailer == LocalMailer) 143297Seric { 1447045Seric if (CurEnv->e_from.q_home != NULL) 1457045Seric p = CurEnv->e_from.q_home; 1467045Seric else if ((pw = getpwnam(CurEnv->e_from.q_user)) != NULL) 1474079Seric p = pw->pw_dir; 148297Seric } 1494079Seric if (p == NULL) 150297Seric { 1517045Seric syserr("Can't return mail to %s", CurEnv->e_from.q_paddr); 152297Seric # ifdef DEBUG 153297Seric p = "/usr/tmp"; 154297Seric # else 155297Seric p = NULL; 156297Seric # endif 157297Seric } 1584199Seric if (p != NULL && TempFile != NULL) 159297Seric { 1605315Seric auto ADDRESS *q; 1617053Seric bool oldverb = Verbose; 1625315Seric 163297Seric /* we have a home directory; open dead.letter */ 1647053Seric Verbose = TRUE; 1654167Seric message(Arpa_Info, "Saving message in dead.letter"); 1667053Seric Verbose = oldverb; 1674079Seric define('z', p); 1686978Seric expand("$z/dead.letter", buf, &buf[sizeof buf - 1], CurEnv); 1696904Seric CurEnv->e_to = buf; 1705315Seric q = NULL; 1715846Seric sendto(buf, -1, (ADDRESS *) NULL, &q); 1726978Seric (void) deliver(q); 173297Seric } 174297Seric 175297Seric /* add terminator to writeback message */ 176297Seric if (WriteBack) 177297Seric printf("-----\r\n"); 178297Seric } 179297Seric /* 1804633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 1814633Seric ** 1824633Seric ** Parameters: 1834633Seric ** msg -- the explanatory message. 1847045Seric ** returnto -- the queue of people to send the message to. 1855984Seric ** sendbody -- if TRUE, also send back the body of the 1865984Seric ** message; otherwise just send the header. 1874633Seric ** 1884633Seric ** Returns: 1894633Seric ** zero -- if everything went ok. 1904633Seric ** else -- some error. 1914633Seric ** 1924633Seric ** Side Effects: 1934633Seric ** Returns the current message to the sender via 1944633Seric ** mail. 1954633Seric */ 1964633Seric 1975984Seric static bool SendBody; 1984633Seric 1997045Seric #define MAXRETURNS 6 /* max depth of returning messages */ 2007045Seric 2016978Seric returntosender(msg, returnto, sendbody) 2024633Seric char *msg; 2036978Seric ADDRESS *returnto; 2045984Seric bool sendbody; 2054633Seric { 2064633Seric char buf[MAXNAME]; 2074633Seric register int i; 2086978Seric extern putheader(), errbody(); 2096978Seric register ENVELOPE *ee; 2106978Seric extern ENVELOPE *newenvelope(); 2116978Seric ENVELOPE errenvelope; 2127045Seric static int returndepth; 2134633Seric 2147045Seric if (++returndepth >= MAXRETURNS) 2157045Seric { 2167045Seric if (returndepth != MAXRETURNS) 2177045Seric syserr("returntosender: infinite recursion on %s", returnto->q_paddr); 2187045Seric /* don't "unrecurse" and fake a clean exit */ 2197045Seric /* returndepth--; */ 2207045Seric return (0); 2217045Seric } 2227045Seric 2236989Seric NoAlias = TRUE; 2245984Seric SendBody = sendbody; 2256978Seric ee = newenvelope(&errenvelope); 2266978Seric ee->e_puthdr = putheader; 2276978Seric ee->e_putbody = errbody; 2286978Seric addheader("date", "$b", ee); 2296978Seric addheader("from", "$g (Mail Delivery Subsystem)", ee); 2306978Seric addheader("to", returnto->q_paddr, ee); 2316978Seric addheader("subject", msg, ee); 2324633Seric 2334633Seric /* fake up an address header for the from person */ 2346978Seric expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 2356978Seric if (parse(buf, &ee->e_from, -1) == NULL) 2364633Seric { 2374633Seric syserr("Can't parse myself!"); 2384633Seric ExitStat = EX_SOFTWARE; 2397045Seric returndepth--; 2404633Seric return (-1); 2414633Seric } 2427045Seric ee->e_sendqueue = returnto; 2435984Seric 2446978Seric /* push state into submessage */ 2456978Seric CurEnv = ee; 2466978Seric define('f', "$n"); 2476978Seric define('x', "Mail Delivery Subsystem"); 2485984Seric 2496978Seric /* actually deliver the error message */ 2507045Seric sendall(ee, FALSE); 2516978Seric 2527045Seric /* do any closing error processing */ 2537045Seric checkerrors(ee); 2546978Seric 2556978Seric /* restore state */ 2566978Seric CurEnv = CurEnv->e_parent; 2577045Seric returndepth--; 2586978Seric 2597045Seric /* should check for delivery errors here */ 2604633Seric return (0); 2614633Seric } 2624633Seric /* 2636978Seric ** ERRBODY -- output the body of an error message. 2646978Seric ** 2656978Seric ** Typically this is a copy of the transcript plus a copy of the 2666978Seric ** original offending message. 2676978Seric ** 268297Seric ** Parameters: 269297Seric ** xfile -- the transcript file. 270297Seric ** fp -- the output file. 2716978Seric ** xdot -- if set, use the SMTP hidden dot algorithm. 272297Seric ** 273297Seric ** Returns: 274297Seric ** none 275297Seric ** 276297Seric ** Side Effects: 2776978Seric ** Outputs the body of an error message. 278297Seric */ 279297Seric 2806978Seric errbody(fp, m, xdot) 281297Seric register FILE *fp; 2824318Seric register struct mailer *m; 2834864Seric bool xdot; 284297Seric { 2856978Seric register FILE *xfile; 2863189Seric char buf[MAXLINE]; 2877124Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 288297Seric 2894086Seric (void) fflush(stdout); 2903189Seric if ((xfile = fopen(Transcript, "r")) == NULL) 291297Seric syserr("Cannot open %s", Transcript); 292297Seric errno = 0; 2934318Seric 2944318Seric /* 2954318Seric ** Output transcript of errors 2964318Seric */ 2974318Seric 2986978Seric fprintf(fp, " ----- Transcript of session follows -----\n"); 2994712Seric (void) fflush(Xscript); 3004086Seric while (fgets(buf, sizeof buf, xfile) != NULL) 3017124Seric putline(buf, fp, fullsmtp); 3024318Seric 3034318Seric /* 3044318Seric ** Output text of original message 3054318Seric */ 3064318Seric 3074289Seric if (NoReturn) 3084289Seric fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 3094289Seric else if (TempFile != NULL) 3104199Seric { 3115984Seric if (SendBody) 3125984Seric { 3135984Seric fprintf(fp, "\n ----- Unsent message follows -----\n"); 3145984Seric (void) fflush(fp); 3157006Seric putheader(fp, m, CurEnv->e_parent); 3166978Seric fprintf(fp, "\n"); 3177006Seric putbody(fp, m, xdot); 3185984Seric } 3195984Seric else 3205984Seric { 3215984Seric fprintf(fp, "\n ----- Message header follows -----\n"); 3225984Seric (void) fflush(fp); 3237006Seric putheader(fp, m, CurEnv); 3245984Seric } 3254199Seric } 3264199Seric else 3274199Seric fprintf(fp, "\n ----- No message was collected -----\n\n"); 3284318Seric 3294318Seric /* 3304318Seric ** Cleanup and exit 3314318Seric */ 3324318Seric 3334086Seric (void) fclose(xfile); 334297Seric if (errno != 0) 3356978Seric syserr("errbody: I/O error"); 336297Seric } 337