1297Seric # include <pwd.h> 23313Seric # include "sendmail.h" 3297Seric 4*10106Seric SCCSID(@(#)savemail.c 3.55 01/03/83); 5408Seric 6297Seric /* 7297Seric ** SAVEMAIL -- Save mail on error 8297Seric ** 99375Seric ** If mailing back errors, 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(); 355846Seric typedef int (*fnptr)(); 36297Seric 377361Seric # ifdef DEBUG 387676Seric if (tTd(6, 1)) 399375Seric printf("\nsavemail\n"); 407361Seric # endif DEBUG 417361Seric 429375Seric if (bitset(EF_RESPONSE, e->e_flags)) 43297Seric return; 449337Seric if (e->e_class < 0) 456978Seric { 467053Seric message(Arpa_Info, "Dumping junk mail"); 476978Seric return; 486978Seric } 497053Seric ForceMail = TRUE; 509337Seric e->e_flags &= ~EF_FATALERRS; 51297Seric 52297Seric /* 53297Seric ** In the unhappy event we don't know who to return the mail 54297Seric ** to, make someone up. 55297Seric */ 56297Seric 579337Seric if (e->e_from.q_paddr == NULL) 58297Seric { 599884Seric if (parseaddr("root", &e->e_from, 0) == NULL) 60297Seric { 61297Seric syserr("Cannot parse root!"); 62297Seric ExitStat = EX_SOFTWARE; 63297Seric finis(); 64297Seric } 65297Seric } 669337Seric e->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 769375Seric if (ErrorMode == EM_BERKNET) 77297Seric { 78401Seric ExitStat = EX_OK; 799375Seric ErrorMode = EM_MAIL; 80297Seric } 819337Seric if (!bitset(M_LOCAL, e->e_from.q_mailer->m_flags)) 829375Seric ErrorMode = EM_MAIL; 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). 889375Seric ** If not, mail back instead. 89297Seric */ 90297Seric 919375Seric if (ErrorMode == EM_WRITE) 92297Seric { 93297Seric p = ttypath(); 94297Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 95297Seric { 969375Seric ErrorMode = EM_MAIL; 97297Seric errno = 0; 98297Seric } 99297Seric else 100297Seric { 1019375Seric expand("$n", buf, &buf[sizeof buf - 1], e); 1029375Seric printf("\r\nMessage from %s...\r\n", buf); 1039375Seric printf("Errors occurred while sending mail.\r\n"); 1049542Seric if (e->e_xfp != NULL) 1059375Seric { 1069542Seric (void) fflush(e->e_xfp); 1079375Seric xfile = fopen(queuename(e, 'x'), "r"); 1089375Seric } 1099375Seric else 1109375Seric xfile = NULL; 111401Seric if (xfile == NULL) 1129375Seric { 1139337Seric syserr("Cannot open %s", queuename(e, 'x')); 1149375Seric printf("Transcript of session is unavailable.\r\n"); 1159375Seric } 1169375Seric else 1179375Seric { 1189375Seric printf("Transcript follows:\r\n"); 1199375Seric while (fgets(buf, sizeof buf, xfile) != NULL && 1209375Seric !ferror(stdout)) 1219375Seric fputs(buf, stdout); 1229375Seric (void) fclose(xfile); 1239375Seric } 124297Seric if (ferror(stdout)) 1254086Seric (void) syserr("savemail: stdout: write err"); 126297Seric } 127297Seric } 128297Seric 129297Seric /* 130297Seric ** If mailing back, do it. 131297Seric ** Throw away all further output. Don't do aliases, since 132297Seric ** this could cause loops, e.g., if joe mails to x:joe, 133297Seric ** and for some reason the network for x: is down, then 134297Seric ** the response gets sent to x:joe, which gives a 135297Seric ** response, etc. Also force the mail to be delivered 136297Seric ** even if a version of it has already been sent to the 137297Seric ** sender. 138297Seric */ 139297Seric 1409375Seric if (ErrorMode == EM_MAIL) 141297Seric { 1429337Seric if (e->e_errorqueue == NULL) 1439616Seric sendtolist(e->e_from.q_paddr, (ADDRESS *) NULL, 1449337Seric &e->e_errorqueue); 145*10106Seric if (returntosender(e->e_message != NULL ? e->e_message : 146*10106Seric "Unable to deliver mail", 1479337Seric e->e_errorqueue, TRUE) == 0) 148297Seric return; 149297Seric } 150297Seric 151297Seric /* 152297Seric ** Save the message in dead.letter. 153297Seric ** If we weren't mailing back, and the user is local, we 154297Seric ** should save the message in dead.letter so that the 155297Seric ** poor person doesn't have to type it over again -- 156297Seric ** and we all know what poor typists programmers are. 157297Seric */ 158297Seric 1594079Seric p = NULL; 1609337Seric if (e->e_from.q_mailer == LocalMailer) 161297Seric { 1629337Seric if (e->e_from.q_home != NULL) 1639337Seric p = e->e_from.q_home; 1649337Seric else if ((pw = getpwnam(e->e_from.q_user)) != NULL) 1654079Seric p = pw->pw_dir; 166297Seric } 1674079Seric if (p == NULL) 168297Seric { 1699337Seric syserr("Can't return mail to %s", e->e_from.q_paddr); 170297Seric # ifdef DEBUG 171297Seric p = "/usr/tmp"; 172297Seric # endif 173297Seric } 1749542Seric if (p != NULL && e->e_dfp != NULL) 175297Seric { 1765315Seric auto ADDRESS *q; 1777053Seric bool oldverb = Verbose; 1785315Seric 179297Seric /* we have a home directory; open dead.letter */ 1809375Seric define('z', p, e); 1819375Seric expand("$z/dead.letter", buf, &buf[sizeof buf - 1], e); 1827053Seric Verbose = TRUE; 1839375Seric message(Arpa_Info, "Saving message in %s", buf); 1847053Seric Verbose = oldverb; 1859337Seric e->e_to = buf; 1865315Seric q = NULL; 1879616Seric sendtolist(buf, (ADDRESS *) NULL, &q); 1889375Seric (void) deliver(e, q); 189297Seric } 190297Seric 191297Seric /* add terminator to writeback message */ 1929375Seric if (ErrorMode == EM_WRITE) 193297Seric printf("-----\r\n"); 194297Seric } 195297Seric /* 1964633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 1974633Seric ** 1984633Seric ** Parameters: 1994633Seric ** msg -- the explanatory message. 2007045Seric ** returnto -- the queue of people to send the message to. 2015984Seric ** sendbody -- if TRUE, also send back the body of the 2025984Seric ** message; otherwise just send the header. 2034633Seric ** 2044633Seric ** Returns: 2054633Seric ** zero -- if everything went ok. 2064633Seric ** else -- some error. 2074633Seric ** 2084633Seric ** Side Effects: 2094633Seric ** Returns the current message to the sender via 2104633Seric ** mail. 2114633Seric */ 2124633Seric 2135984Seric static bool SendBody; 2144633Seric 2157045Seric #define MAXRETURNS 6 /* max depth of returning messages */ 2167045Seric 2176978Seric returntosender(msg, returnto, sendbody) 2184633Seric char *msg; 2196978Seric ADDRESS *returnto; 2205984Seric bool sendbody; 2214633Seric { 2224633Seric char buf[MAXNAME]; 2236978Seric extern putheader(), errbody(); 2246978Seric register ENVELOPE *ee; 2256978Seric extern ENVELOPE *newenvelope(); 2266978Seric ENVELOPE errenvelope; 2277045Seric static int returndepth; 2289375Seric register ADDRESS *q; 2294633Seric 2307287Seric # ifdef DEBUG 2317676Seric if (tTd(6, 1)) 2327287Seric { 2337287Seric printf("Return To Sender: msg=\"%s\", depth=%d, CurEnv=%x,\n", 2347287Seric msg, returndepth, CurEnv); 2357287Seric printf("\treturnto="); 2369375Seric printaddr(returnto, TRUE); 2377287Seric } 2387287Seric # endif DEBUG 2397287Seric 2407045Seric if (++returndepth >= MAXRETURNS) 2417045Seric { 2427045Seric if (returndepth != MAXRETURNS) 2437045Seric syserr("returntosender: infinite recursion on %s", returnto->q_paddr); 2447045Seric /* don't "unrecurse" and fake a clean exit */ 2457045Seric /* returndepth--; */ 2467045Seric return (0); 2477045Seric } 2487045Seric 2495984Seric SendBody = sendbody; 2509375Seric define('g', "$f", CurEnv); 2516978Seric ee = newenvelope(&errenvelope); 2526978Seric ee->e_puthdr = putheader; 2536978Seric ee->e_putbody = errbody; 2549375Seric ee->e_flags |= EF_RESPONSE; 2559375Seric ee->e_sendqueue = returnto; 2569542Seric openxscript(ee); 2579375Seric for (q = returnto; q != NULL; q = q->q_next) 2589375Seric { 2599375Seric if (q->q_alias == NULL) 2609375Seric addheader("to", q->q_paddr, ee); 2619375Seric } 262*10106Seric (void) sprintf(buf, "MAIL FAILURE: %s", msg); 263*10106Seric addheader("subject", buf, ee); 2644633Seric 2654633Seric /* fake up an address header for the from person */ 2666978Seric expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 2679884Seric if (parseaddr(buf, &ee->e_from, -1) == NULL) 2684633Seric { 2694633Seric syserr("Can't parse myself!"); 2704633Seric ExitStat = EX_SOFTWARE; 2717045Seric returndepth--; 2724633Seric return (-1); 2734633Seric } 2745984Seric 2756978Seric /* push state into submessage */ 2766978Seric CurEnv = ee; 2779375Seric define('f', "$n", ee); 2789375Seric define('x', "Mail Delivery Subsystem", ee); 2795984Seric 2806978Seric /* actually deliver the error message */ 2819280Seric sendall(ee, SendMode); 2826978Seric 2836978Seric /* restore state */ 2847811Seric dropenvelope(ee); 2856978Seric CurEnv = CurEnv->e_parent; 2867045Seric returndepth--; 2876978Seric 2887045Seric /* should check for delivery errors here */ 2894633Seric return (0); 2904633Seric } 2914633Seric /* 2926978Seric ** ERRBODY -- output the body of an error message. 2936978Seric ** 2946978Seric ** Typically this is a copy of the transcript plus a copy of the 2956978Seric ** original offending message. 2966978Seric ** 297297Seric ** Parameters: 298297Seric ** xfile -- the transcript file. 299297Seric ** fp -- the output file. 3006978Seric ** xdot -- if set, use the SMTP hidden dot algorithm. 3019542Seric ** e -- the envelope we are working in. 30210065Seric ** crlf -- set if we want CRLF's at the end of lines. 303297Seric ** 304297Seric ** Returns: 305297Seric ** none 306297Seric ** 307297Seric ** Side Effects: 3086978Seric ** Outputs the body of an error message. 309297Seric */ 310297Seric 31110065Seric errbody(fp, m, xdot, e, crlf) 312297Seric register FILE *fp; 3134318Seric register struct mailer *m; 3144864Seric bool xdot; 3159542Seric register ENVELOPE *e; 31610065Seric bool crlf; 317297Seric { 3186978Seric register FILE *xfile; 3193189Seric char buf[MAXLINE]; 3207124Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 3219337Seric char *p; 322297Seric 3239057Seric /* 3249057Seric ** Output transcript of errors 3259057Seric */ 3269057Seric 3274086Seric (void) fflush(stdout); 3289542Seric p = queuename(e->e_parent, 'x'); 3299337Seric if ((xfile = fopen(p, "r")) == NULL) 3309057Seric { 3319337Seric syserr("Cannot open %s", p); 3329057Seric fprintf(fp, " ----- Transcript of session is unavailable -----\n"); 3339057Seric } 3349057Seric else 3359057Seric { 3369057Seric fprintf(fp, " ----- Transcript of session follows -----\n"); 3379542Seric if (e->e_xfp != NULL) 3389542Seric (void) fflush(e->e_xfp); 3399057Seric while (fgets(buf, sizeof buf, xfile) != NULL) 34010065Seric putline(buf, fp, crlf, fullsmtp); 3419057Seric (void) fclose(xfile); 3429057Seric } 343297Seric errno = 0; 3444318Seric 3454318Seric /* 3464318Seric ** Output text of original message 3474318Seric */ 3484318Seric 3494289Seric if (NoReturn) 3504289Seric fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 3519542Seric else if (e->e_parent->e_dfp != NULL) 3524199Seric { 3535984Seric if (SendBody) 3545984Seric { 3555984Seric fprintf(fp, "\n ----- Unsent message follows -----\n"); 3565984Seric (void) fflush(fp); 35710065Seric putheader(fp, m, e->e_parent, crlf); 3586978Seric fprintf(fp, "\n"); 35910065Seric putbody(fp, m, xdot, e->e_parent, crlf); 3605984Seric } 3615984Seric else 3625984Seric { 3635984Seric fprintf(fp, "\n ----- Message header follows -----\n"); 3645984Seric (void) fflush(fp); 36510065Seric putheader(fp, m, e->e_parent, crlf); 3665984Seric } 3674199Seric } 3684199Seric else 3694199Seric fprintf(fp, "\n ----- No message was collected -----\n\n"); 3704318Seric 3714318Seric /* 3724318Seric ** Cleanup and exit 3734318Seric */ 3744318Seric 375297Seric if (errno != 0) 3766978Seric syserr("errbody: I/O error"); 377297Seric } 378