1297Seric # include <pwd.h> 23313Seric # include "sendmail.h" 3297Seric 4*9347Seric SCCSID(@(#)savemail.c 3.49 11/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: 159337Seric ** e -- the envelope containing the message in error. 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 269337Seric savemail(e) 279337Seric register ENVELOPE *e; 28297Seric { 29297Seric register struct passwd *pw; 30297Seric register FILE *xfile; 31297Seric char buf[MAXLINE+1]; 32297Seric extern struct passwd *getpwnam(); 33297Seric register char *p; 34297Seric extern char *ttypath(); 35297Seric static int exclusive; 365846Seric typedef int (*fnptr)(); 37297Seric 387361Seric # ifdef DEBUG 397676Seric if (tTd(6, 1)) 407361Seric printf("\nsavemail: exclusive %d\n", exclusive); 417361Seric # endif DEBUG 427361Seric 436978Seric if (exclusive++) 44297Seric return; 459337Seric if (e->e_class < 0) 466978Seric { 477053Seric message(Arpa_Info, "Dumping junk mail"); 486978Seric return; 496978Seric } 507053Seric ForceMail = TRUE; 519337Seric e->e_flags &= ~EF_FATALERRS; 52297Seric 53297Seric /* 54297Seric ** In the unhappy event we don't know who to return the mail 55297Seric ** to, make someone up. 56297Seric */ 57297Seric 589337Seric if (e->e_from.q_paddr == NULL) 59297Seric { 609337Seric if (parse("root", &e->e_from, 0) == NULL) 61297Seric { 62297Seric syserr("Cannot parse root!"); 63297Seric ExitStat = EX_SOFTWARE; 64297Seric finis(); 65297Seric } 66297Seric } 679337Seric e->e_to = NULL; 68297Seric 69297Seric /* 70401Seric ** If called from Eric Schmidt's network, do special mailback. 71401Seric ** Fundamentally, this is the mailback case except that 72401Seric ** it returns an OK exit status (assuming the return 73401Seric ** worked). 746989Seric ** Also, if the from address is not local, mail it back. 75297Seric */ 76297Seric 77401Seric if (BerkNet) 78297Seric { 79401Seric ExitStat = EX_OK; 806989Seric MailBack = TRUE; 81297Seric } 829337Seric if (!bitset(M_LOCAL, e->e_from.q_mailer->m_flags)) 836989Seric MailBack = TRUE; 84297Seric 85297Seric /* 86297Seric ** If writing back, do it. 87297Seric ** If the user is still logged in on the same terminal, 88297Seric ** then write the error messages back to hir (sic). 89297Seric ** If not, set the MailBack flag so that it will get 90297Seric ** mailed back instead. 91297Seric */ 92297Seric 93297Seric if (WriteBack) 94297Seric { 95297Seric p = ttypath(); 96297Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 97297Seric { 986989Seric MailBack = TRUE; 99297Seric errno = 0; 100297Seric } 101297Seric else 102297Seric { 1039337Seric if (Xscript != NULL) 1049337Seric (void) fflush(Xscript); 1059337Seric xfile = fopen(queuename(e, 'x'), "r"); 106401Seric if (xfile == NULL) 1079337Seric syserr("Cannot open %s", queuename(e, 'x')); 1089337Seric expand("$n", buf, &buf[sizeof buf - 1], e); 1094162Seric printf("\r\nMessage from %s...\r\n", buf); 1104318Seric printf("Errors occurred while sending mail; transcript follows:\r\n"); 1114086Seric while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout)) 112297Seric fputs(buf, stdout); 113297Seric if (ferror(stdout)) 1144086Seric (void) syserr("savemail: stdout: write err"); 1154086Seric (void) fclose(xfile); 116297Seric } 117297Seric } 118297Seric 119297Seric /* 120297Seric ** If mailing back, do it. 121297Seric ** Throw away all further output. Don't do aliases, since 122297Seric ** this could cause loops, e.g., if joe mails to x:joe, 123297Seric ** and for some reason the network for x: is down, then 124297Seric ** the response gets sent to x:joe, which gives a 125297Seric ** response, etc. Also force the mail to be delivered 126297Seric ** even if a version of it has already been sent to the 127297Seric ** sender. 128297Seric */ 129297Seric 1303048Seric if (MailBack) 131297Seric { 1329337Seric if (e->e_errorqueue == NULL) 1339337Seric sendto(e->e_from.q_paddr, (ADDRESS *) NULL, 1349337Seric &e->e_errorqueue); 1358079Seric if (returntosender("Unable to deliver mail", 1369337Seric e->e_errorqueue, TRUE) == 0) 137297Seric return; 138297Seric } 139297Seric 140297Seric /* 141297Seric ** Save the message in dead.letter. 142297Seric ** If we weren't mailing back, and the user is local, we 143297Seric ** should save the message in dead.letter so that the 144297Seric ** poor person doesn't have to type it over again -- 145297Seric ** and we all know what poor typists programmers are. 1467361Seric ** However, if we are running a "smart" protocol, we don't 1477361Seric ** bother to return the message, since the other end is 1487361Seric ** expected to handle that. 149297Seric */ 150297Seric 1519280Seric if (OpMode == MD_ARPAFTP || OpMode == MD_SMTP) 1524162Seric return; 1534079Seric p = NULL; 1549337Seric if (e->e_from.q_mailer == LocalMailer) 155297Seric { 1569337Seric if (e->e_from.q_home != NULL) 1579337Seric p = e->e_from.q_home; 1589337Seric else if ((pw = getpwnam(e->e_from.q_user)) != NULL) 1594079Seric p = pw->pw_dir; 160297Seric } 1614079Seric if (p == NULL) 162297Seric { 1639337Seric syserr("Can't return mail to %s", e->e_from.q_paddr); 164297Seric # ifdef DEBUG 165297Seric p = "/usr/tmp"; 166297Seric # endif 167297Seric } 1684199Seric if (p != NULL && TempFile != NULL) 169297Seric { 1705315Seric auto ADDRESS *q; 1717053Seric bool oldverb = Verbose; 1725315Seric 173297Seric /* we have a home directory; open dead.letter */ 1747053Seric Verbose = TRUE; 1754167Seric message(Arpa_Info, "Saving message in dead.letter"); 1767053Seric Verbose = oldverb; 1774079Seric define('z', p); 1789337Seric expand("$z/dead.letter", buf, &buf[sizeof buf - 1], e); 1799337Seric e->e_to = buf; 1805315Seric q = NULL; 1818079Seric sendto(buf, (ADDRESS *) NULL, &q); 1826978Seric (void) deliver(q); 183297Seric } 184297Seric 185297Seric /* add terminator to writeback message */ 186297Seric if (WriteBack) 187297Seric printf("-----\r\n"); 188297Seric } 189297Seric /* 1904633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 1914633Seric ** 1924633Seric ** Parameters: 1934633Seric ** msg -- the explanatory message. 1947045Seric ** returnto -- the queue of people to send the message to. 1955984Seric ** sendbody -- if TRUE, also send back the body of the 1965984Seric ** message; otherwise just send the header. 1974633Seric ** 1984633Seric ** Returns: 1994633Seric ** zero -- if everything went ok. 2004633Seric ** else -- some error. 2014633Seric ** 2024633Seric ** Side Effects: 2034633Seric ** Returns the current message to the sender via 2044633Seric ** mail. 2054633Seric */ 2064633Seric 2075984Seric static bool SendBody; 2084633Seric 2097045Seric #define MAXRETURNS 6 /* max depth of returning messages */ 2107045Seric 2116978Seric returntosender(msg, returnto, sendbody) 2124633Seric char *msg; 2136978Seric ADDRESS *returnto; 2145984Seric bool sendbody; 2154633Seric { 2164633Seric char buf[MAXNAME]; 2176978Seric extern putheader(), errbody(); 2186978Seric register ENVELOPE *ee; 2196978Seric extern ENVELOPE *newenvelope(); 2206978Seric ENVELOPE errenvelope; 2217045Seric static int returndepth; 2224633Seric 2237287Seric # ifdef DEBUG 2247676Seric if (tTd(6, 1)) 2257287Seric { 2267287Seric printf("Return To Sender: msg=\"%s\", depth=%d, CurEnv=%x,\n", 2277287Seric msg, returndepth, CurEnv); 2287287Seric printf("\treturnto="); 2297287Seric printaddr(returnto, FALSE); 2307287Seric } 2317287Seric # endif DEBUG 2327287Seric 2337045Seric if (++returndepth >= MAXRETURNS) 2347045Seric { 2357045Seric if (returndepth != MAXRETURNS) 2367045Seric syserr("returntosender: infinite recursion on %s", returnto->q_paddr); 2377045Seric /* don't "unrecurse" and fake a clean exit */ 2387045Seric /* returndepth--; */ 2397045Seric return (0); 2407045Seric } 2417045Seric 2426989Seric NoAlias = TRUE; 2435984Seric SendBody = sendbody; 2448129Seric define('g', "$f"); 2456978Seric ee = newenvelope(&errenvelope); 2466978Seric ee->e_puthdr = putheader; 2476978Seric ee->e_putbody = errbody; 248*9347Seric (void) queuename(ee, '\0'); 2496978Seric addheader("to", returnto->q_paddr, ee); 2506978Seric addheader("subject", msg, ee); 2514633Seric 2524633Seric /* fake up an address header for the from person */ 2536978Seric expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 2546978Seric if (parse(buf, &ee->e_from, -1) == NULL) 2554633Seric { 2564633Seric syserr("Can't parse myself!"); 2574633Seric ExitStat = EX_SOFTWARE; 2587045Seric returndepth--; 2594633Seric return (-1); 2604633Seric } 2617045Seric ee->e_sendqueue = returnto; 2625984Seric 2636978Seric /* push state into submessage */ 2646978Seric CurEnv = ee; 2656978Seric define('f', "$n"); 2666978Seric define('x', "Mail Delivery Subsystem"); 2675984Seric 2686978Seric /* actually deliver the error message */ 2699280Seric sendall(ee, SendMode); 2706978Seric 2717045Seric /* do any closing error processing */ 2727045Seric checkerrors(ee); 2736978Seric 2746978Seric /* restore state */ 2757811Seric dropenvelope(ee); 2766978Seric CurEnv = CurEnv->e_parent; 2777045Seric returndepth--; 2786978Seric 2797045Seric /* should check for delivery errors here */ 2804633Seric return (0); 2814633Seric } 2824633Seric /* 2836978Seric ** ERRBODY -- output the body of an error message. 2846978Seric ** 2856978Seric ** Typically this is a copy of the transcript plus a copy of the 2866978Seric ** original offending message. 2876978Seric ** 288297Seric ** Parameters: 289297Seric ** xfile -- the transcript file. 290297Seric ** fp -- the output file. 2916978Seric ** xdot -- if set, use the SMTP hidden dot algorithm. 292297Seric ** 293297Seric ** Returns: 294297Seric ** none 295297Seric ** 296297Seric ** Side Effects: 2976978Seric ** Outputs the body of an error message. 298297Seric */ 299297Seric 3006978Seric errbody(fp, m, xdot) 301297Seric register FILE *fp; 3024318Seric register struct mailer *m; 3034864Seric bool xdot; 304297Seric { 3056978Seric register FILE *xfile; 3063189Seric char buf[MAXLINE]; 3077124Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 3089337Seric char *p; 309297Seric 3109057Seric /* 3119057Seric ** Output transcript of errors 3129057Seric */ 3139057Seric 3144086Seric (void) fflush(stdout); 3159337Seric p = queuename(CurEnv->e_parent, 'x'); 3169337Seric if ((xfile = fopen(p, "r")) == NULL) 3179057Seric { 3189337Seric syserr("Cannot open %s", p); 3199057Seric fprintf(fp, " ----- Transcript of session is unavailable -----\n"); 3209057Seric } 3219057Seric else 3229057Seric { 3239057Seric fprintf(fp, " ----- Transcript of session follows -----\n"); 3249337Seric if (Xscript != NULL) 3259337Seric (void) fflush(Xscript); 3269057Seric while (fgets(buf, sizeof buf, xfile) != NULL) 3279057Seric putline(buf, fp, fullsmtp); 3289057Seric (void) fclose(xfile); 3299057Seric } 330297Seric errno = 0; 3314318Seric 3324318Seric /* 3334318Seric ** Output text of original message 3344318Seric */ 3354318Seric 3364289Seric if (NoReturn) 3374289Seric fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 3384289Seric else if (TempFile != NULL) 3394199Seric { 3405984Seric if (SendBody) 3415984Seric { 3425984Seric fprintf(fp, "\n ----- Unsent message follows -----\n"); 3435984Seric (void) fflush(fp); 3447006Seric putheader(fp, m, CurEnv->e_parent); 3456978Seric fprintf(fp, "\n"); 3467006Seric putbody(fp, m, xdot); 3475984Seric } 3485984Seric else 3495984Seric { 3505984Seric fprintf(fp, "\n ----- Message header follows -----\n"); 3515984Seric (void) fflush(fp); 3527006Seric putheader(fp, m, CurEnv); 3535984Seric } 3544199Seric } 3554199Seric else 3564199Seric fprintf(fp, "\n ----- No message was collected -----\n\n"); 3574318Seric 3584318Seric /* 3594318Seric ** Cleanup and exit 3604318Seric */ 3614318Seric 362297Seric if (errno != 0) 3636978Seric syserr("errbody: I/O error"); 364297Seric } 365