1297Seric # include <pwd.h> 23313Seric # include "sendmail.h" 3297Seric 4*9375Seric SCCSID(@(#)savemail.c 3.50 11/28/82); 5408Seric 6297Seric /* 7297Seric ** SAVEMAIL -- Save mail on error 8297Seric ** 9*9375Seric ** 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)) 39*9375Seric printf("\nsavemail\n"); 407361Seric # endif DEBUG 417361Seric 42*9375Seric 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 { 599337Seric if (parse("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 76*9375Seric if (ErrorMode == EM_BERKNET) 77297Seric { 78401Seric ExitStat = EX_OK; 79*9375Seric ErrorMode = EM_MAIL; 80297Seric } 819337Seric if (!bitset(M_LOCAL, e->e_from.q_mailer->m_flags)) 82*9375Seric 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). 88*9375Seric ** If not, mail back instead. 89297Seric */ 90297Seric 91*9375Seric if (ErrorMode == EM_WRITE) 92297Seric { 93297Seric p = ttypath(); 94297Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 95297Seric { 96*9375Seric ErrorMode = EM_MAIL; 97297Seric errno = 0; 98297Seric } 99297Seric else 100297Seric { 101*9375Seric expand("$n", buf, &buf[sizeof buf - 1], e); 102*9375Seric printf("\r\nMessage from %s...\r\n", buf); 103*9375Seric printf("Errors occurred while sending mail.\r\n"); 1049337Seric if (Xscript != NULL) 105*9375Seric { 1069337Seric (void) fflush(Xscript); 107*9375Seric xfile = fopen(queuename(e, 'x'), "r"); 108*9375Seric } 109*9375Seric else 110*9375Seric xfile = NULL; 111401Seric if (xfile == NULL) 112*9375Seric { 1139337Seric syserr("Cannot open %s", queuename(e, 'x')); 114*9375Seric printf("Transcript of session is unavailable.\r\n"); 115*9375Seric } 116*9375Seric else 117*9375Seric { 118*9375Seric printf("Transcript follows:\r\n"); 119*9375Seric while (fgets(buf, sizeof buf, xfile) != NULL && 120*9375Seric !ferror(stdout)) 121*9375Seric fputs(buf, stdout); 122*9375Seric (void) fclose(xfile); 123*9375Seric } 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 140*9375Seric if (ErrorMode == EM_MAIL) 141297Seric { 1429337Seric if (e->e_errorqueue == NULL) 1439337Seric sendto(e->e_from.q_paddr, (ADDRESS *) NULL, 1449337Seric &e->e_errorqueue); 1458079Seric if (returntosender("Unable to deliver mail", 1469337Seric e->e_errorqueue, TRUE) == 0) 147297Seric return; 148297Seric } 149297Seric 150297Seric /* 151297Seric ** Save the message in dead.letter. 152297Seric ** If we weren't mailing back, and the user is local, we 153297Seric ** should save the message in dead.letter so that the 154297Seric ** poor person doesn't have to type it over again -- 155297Seric ** and we all know what poor typists programmers are. 156297Seric */ 157297Seric 1584079Seric p = NULL; 1599337Seric if (e->e_from.q_mailer == LocalMailer) 160297Seric { 1619337Seric if (e->e_from.q_home != NULL) 1629337Seric p = e->e_from.q_home; 1639337Seric else if ((pw = getpwnam(e->e_from.q_user)) != NULL) 1644079Seric p = pw->pw_dir; 165297Seric } 1664079Seric if (p == NULL) 167297Seric { 1689337Seric syserr("Can't return mail to %s", e->e_from.q_paddr); 169297Seric # ifdef DEBUG 170297Seric p = "/usr/tmp"; 171297Seric # endif 172297Seric } 1734199Seric if (p != NULL && TempFile != NULL) 174297Seric { 1755315Seric auto ADDRESS *q; 1767053Seric bool oldverb = Verbose; 1775315Seric 178297Seric /* we have a home directory; open dead.letter */ 179*9375Seric define('z', p, e); 180*9375Seric expand("$z/dead.letter", buf, &buf[sizeof buf - 1], e); 1817053Seric Verbose = TRUE; 182*9375Seric message(Arpa_Info, "Saving message in %s", buf); 1837053Seric Verbose = oldverb; 1849337Seric e->e_to = buf; 1855315Seric q = NULL; 1868079Seric sendto(buf, (ADDRESS *) NULL, &q); 187*9375Seric (void) deliver(e, q); 188297Seric } 189297Seric 190297Seric /* add terminator to writeback message */ 191*9375Seric if (ErrorMode == EM_WRITE) 192297Seric printf("-----\r\n"); 193297Seric } 194297Seric /* 1954633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 1964633Seric ** 1974633Seric ** Parameters: 1984633Seric ** msg -- the explanatory message. 1997045Seric ** returnto -- the queue of people to send the message to. 2005984Seric ** sendbody -- if TRUE, also send back the body of the 2015984Seric ** message; otherwise just send the header. 2024633Seric ** 2034633Seric ** Returns: 2044633Seric ** zero -- if everything went ok. 2054633Seric ** else -- some error. 2064633Seric ** 2074633Seric ** Side Effects: 2084633Seric ** Returns the current message to the sender via 2094633Seric ** mail. 2104633Seric */ 2114633Seric 2125984Seric static bool SendBody; 2134633Seric 2147045Seric #define MAXRETURNS 6 /* max depth of returning messages */ 2157045Seric 2166978Seric returntosender(msg, returnto, sendbody) 2174633Seric char *msg; 2186978Seric ADDRESS *returnto; 2195984Seric bool sendbody; 2204633Seric { 2214633Seric char buf[MAXNAME]; 2226978Seric extern putheader(), errbody(); 2236978Seric register ENVELOPE *ee; 2246978Seric extern ENVELOPE *newenvelope(); 2256978Seric ENVELOPE errenvelope; 2267045Seric static int returndepth; 227*9375Seric register ADDRESS *q; 2284633Seric 2297287Seric # ifdef DEBUG 2307676Seric if (tTd(6, 1)) 2317287Seric { 2327287Seric printf("Return To Sender: msg=\"%s\", depth=%d, CurEnv=%x,\n", 2337287Seric msg, returndepth, CurEnv); 2347287Seric printf("\treturnto="); 235*9375Seric printaddr(returnto, TRUE); 2367287Seric } 2377287Seric # endif DEBUG 2387287Seric 2397045Seric if (++returndepth >= MAXRETURNS) 2407045Seric { 2417045Seric if (returndepth != MAXRETURNS) 2427045Seric syserr("returntosender: infinite recursion on %s", returnto->q_paddr); 2437045Seric /* don't "unrecurse" and fake a clean exit */ 2447045Seric /* returndepth--; */ 2457045Seric return (0); 2467045Seric } 2477045Seric 2485984Seric SendBody = sendbody; 249*9375Seric define('g', "$f", CurEnv); 2506978Seric ee = newenvelope(&errenvelope); 2516978Seric ee->e_puthdr = putheader; 2526978Seric ee->e_putbody = errbody; 253*9375Seric ee->e_flags |= EF_RESPONSE; 254*9375Seric ee->e_sendqueue = returnto; 2559347Seric (void) queuename(ee, '\0'); 256*9375Seric for (q = returnto; q != NULL; q = q->q_next) 257*9375Seric { 258*9375Seric if (q->q_alias == NULL) 259*9375Seric addheader("to", q->q_paddr, ee); 260*9375Seric } 2616978Seric addheader("subject", msg, ee); 2624633Seric 2634633Seric /* fake up an address header for the from person */ 2646978Seric expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 2656978Seric if (parse(buf, &ee->e_from, -1) == NULL) 2664633Seric { 2674633Seric syserr("Can't parse myself!"); 2684633Seric ExitStat = EX_SOFTWARE; 2697045Seric returndepth--; 2704633Seric return (-1); 2714633Seric } 2725984Seric 2736978Seric /* push state into submessage */ 2746978Seric CurEnv = ee; 275*9375Seric define('f', "$n", ee); 276*9375Seric define('x', "Mail Delivery Subsystem", ee); 2775984Seric 2786978Seric /* actually deliver the error message */ 2799280Seric sendall(ee, SendMode); 2806978Seric 2816978Seric /* restore state */ 2827811Seric dropenvelope(ee); 2836978Seric CurEnv = CurEnv->e_parent; 2847045Seric returndepth--; 2856978Seric 2867045Seric /* should check for delivery errors here */ 2874633Seric return (0); 2884633Seric } 2894633Seric /* 2906978Seric ** ERRBODY -- output the body of an error message. 2916978Seric ** 2926978Seric ** Typically this is a copy of the transcript plus a copy of the 2936978Seric ** original offending message. 2946978Seric ** 295297Seric ** Parameters: 296297Seric ** xfile -- the transcript file. 297297Seric ** fp -- the output file. 2986978Seric ** xdot -- if set, use the SMTP hidden dot algorithm. 299297Seric ** 300297Seric ** Returns: 301297Seric ** none 302297Seric ** 303297Seric ** Side Effects: 3046978Seric ** Outputs the body of an error message. 305297Seric */ 306297Seric 3076978Seric errbody(fp, m, xdot) 308297Seric register FILE *fp; 3094318Seric register struct mailer *m; 3104864Seric bool xdot; 311297Seric { 3126978Seric register FILE *xfile; 3133189Seric char buf[MAXLINE]; 3147124Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 3159337Seric char *p; 316297Seric 3179057Seric /* 3189057Seric ** Output transcript of errors 3199057Seric */ 3209057Seric 3214086Seric (void) fflush(stdout); 3229337Seric p = queuename(CurEnv->e_parent, 'x'); 3239337Seric if ((xfile = fopen(p, "r")) == NULL) 3249057Seric { 3259337Seric syserr("Cannot open %s", p); 3269057Seric fprintf(fp, " ----- Transcript of session is unavailable -----\n"); 3279057Seric } 3289057Seric else 3299057Seric { 3309057Seric fprintf(fp, " ----- Transcript of session follows -----\n"); 3319337Seric if (Xscript != NULL) 3329337Seric (void) fflush(Xscript); 3339057Seric while (fgets(buf, sizeof buf, xfile) != NULL) 3349057Seric putline(buf, fp, fullsmtp); 3359057Seric (void) fclose(xfile); 3369057Seric } 337297Seric errno = 0; 3384318Seric 3394318Seric /* 3404318Seric ** Output text of original message 3414318Seric */ 3424318Seric 3434289Seric if (NoReturn) 3444289Seric fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 3454289Seric else if (TempFile != NULL) 3464199Seric { 3475984Seric if (SendBody) 3485984Seric { 3495984Seric fprintf(fp, "\n ----- Unsent message follows -----\n"); 3505984Seric (void) fflush(fp); 3517006Seric putheader(fp, m, CurEnv->e_parent); 3526978Seric fprintf(fp, "\n"); 3537006Seric putbody(fp, m, xdot); 3545984Seric } 3555984Seric else 3565984Seric { 3575984Seric fprintf(fp, "\n ----- Message header follows -----\n"); 3585984Seric (void) fflush(fp); 359*9375Seric putheader(fp, m, CurEnv->e_parent); 3605984Seric } 3614199Seric } 3624199Seric else 3634199Seric fprintf(fp, "\n ----- No message was collected -----\n\n"); 3644318Seric 3654318Seric /* 3664318Seric ** Cleanup and exit 3674318Seric */ 3684318Seric 369297Seric if (errno != 0) 3706978Seric syserr("errbody: I/O error"); 371297Seric } 372