1297Seric # include <pwd.h> 23313Seric # include "sendmail.h" 3297Seric 4*7286Seric SCCSID(@(#)savemail.c 3.36 06/26/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 { 1257203Seric if (CurEnv->e_errorqueue == NULL) 126*7286Seric sendto(CurEnv->e_from.q_paddr, 1, (ADDRESS *) 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]; 2076978Seric extern putheader(), errbody(); 2086978Seric register ENVELOPE *ee; 2096978Seric extern ENVELOPE *newenvelope(); 2106978Seric ENVELOPE errenvelope; 2117045Seric static int returndepth; 2124633Seric 2137045Seric if (++returndepth >= MAXRETURNS) 2147045Seric { 2157045Seric if (returndepth != MAXRETURNS) 2167045Seric syserr("returntosender: infinite recursion on %s", returnto->q_paddr); 2177045Seric /* don't "unrecurse" and fake a clean exit */ 2187045Seric /* returndepth--; */ 2197045Seric return (0); 2207045Seric } 2217045Seric 2226989Seric NoAlias = TRUE; 2235984Seric SendBody = sendbody; 2246978Seric ee = newenvelope(&errenvelope); 2256978Seric ee->e_puthdr = putheader; 2266978Seric ee->e_putbody = errbody; 2276978Seric addheader("date", "$b", ee); 2286978Seric addheader("from", "$g (Mail Delivery Subsystem)", ee); 2296978Seric addheader("to", returnto->q_paddr, ee); 2306978Seric addheader("subject", msg, ee); 2314633Seric 2324633Seric /* fake up an address header for the from person */ 2336978Seric expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 2346978Seric if (parse(buf, &ee->e_from, -1) == NULL) 2354633Seric { 2364633Seric syserr("Can't parse myself!"); 2374633Seric ExitStat = EX_SOFTWARE; 2387045Seric returndepth--; 2394633Seric return (-1); 2404633Seric } 2417045Seric ee->e_sendqueue = returnto; 2425984Seric 2436978Seric /* push state into submessage */ 2446978Seric CurEnv = ee; 2456978Seric define('f', "$n"); 2466978Seric define('x', "Mail Delivery Subsystem"); 2475984Seric 2486978Seric /* actually deliver the error message */ 2497045Seric sendall(ee, FALSE); 2506978Seric 2517045Seric /* do any closing error processing */ 2527045Seric checkerrors(ee); 2536978Seric 2546978Seric /* restore state */ 2556978Seric CurEnv = CurEnv->e_parent; 2567045Seric returndepth--; 2576978Seric 2587045Seric /* should check for delivery errors here */ 2594633Seric return (0); 2604633Seric } 2614633Seric /* 2626978Seric ** ERRBODY -- output the body of an error message. 2636978Seric ** 2646978Seric ** Typically this is a copy of the transcript plus a copy of the 2656978Seric ** original offending message. 2666978Seric ** 267297Seric ** Parameters: 268297Seric ** xfile -- the transcript file. 269297Seric ** fp -- the output file. 2706978Seric ** xdot -- if set, use the SMTP hidden dot algorithm. 271297Seric ** 272297Seric ** Returns: 273297Seric ** none 274297Seric ** 275297Seric ** Side Effects: 2766978Seric ** Outputs the body of an error message. 277297Seric */ 278297Seric 2796978Seric errbody(fp, m, xdot) 280297Seric register FILE *fp; 2814318Seric register struct mailer *m; 2824864Seric bool xdot; 283297Seric { 2846978Seric register FILE *xfile; 2853189Seric char buf[MAXLINE]; 2867124Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 287297Seric 2884086Seric (void) fflush(stdout); 2893189Seric if ((xfile = fopen(Transcript, "r")) == NULL) 290297Seric syserr("Cannot open %s", Transcript); 291297Seric errno = 0; 2924318Seric 2934318Seric /* 2944318Seric ** Output transcript of errors 2954318Seric */ 2964318Seric 2976978Seric fprintf(fp, " ----- Transcript of session follows -----\n"); 2984712Seric (void) fflush(Xscript); 2994086Seric while (fgets(buf, sizeof buf, xfile) != NULL) 3007124Seric putline(buf, fp, fullsmtp); 3014318Seric 3024318Seric /* 3034318Seric ** Output text of original message 3044318Seric */ 3054318Seric 3064289Seric if (NoReturn) 3074289Seric fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 3084289Seric else if (TempFile != NULL) 3094199Seric { 3105984Seric if (SendBody) 3115984Seric { 3125984Seric fprintf(fp, "\n ----- Unsent message follows -----\n"); 3135984Seric (void) fflush(fp); 3147006Seric putheader(fp, m, CurEnv->e_parent); 3156978Seric fprintf(fp, "\n"); 3167006Seric putbody(fp, m, xdot); 3175984Seric } 3185984Seric else 3195984Seric { 3205984Seric fprintf(fp, "\n ----- Message header follows -----\n"); 3215984Seric (void) fflush(fp); 3227006Seric putheader(fp, m, CurEnv); 3235984Seric } 3244199Seric } 3254199Seric else 3264199Seric fprintf(fp, "\n ----- No message was collected -----\n\n"); 3274318Seric 3284318Seric /* 3294318Seric ** Cleanup and exit 3304318Seric */ 3314318Seric 3324086Seric (void) fclose(xfile); 333297Seric if (errno != 0) 3346978Seric syserr("errbody: I/O error"); 335297Seric } 336