1297Seric # include <pwd.h> 23313Seric # include "sendmail.h" 3297Seric 4*9884Seric SCCSID(@(#)savemail.c 3.53 12/24/82); 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 { 59*9884Seric 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); 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 } 1739542Seric if (p != NULL && e->e_dfp != NULL) 174297Seric { 1755315Seric auto ADDRESS *q; 1767053Seric bool oldverb = Verbose; 1775315Seric 178297Seric /* we have a home directory; open dead.letter */ 1799375Seric define('z', p, e); 1809375Seric expand("$z/dead.letter", buf, &buf[sizeof buf - 1], e); 1817053Seric Verbose = TRUE; 1829375Seric message(Arpa_Info, "Saving message in %s", buf); 1837053Seric Verbose = oldverb; 1849337Seric e->e_to = buf; 1855315Seric q = NULL; 1869616Seric sendtolist(buf, (ADDRESS *) NULL, &q); 1879375Seric (void) deliver(e, q); 188297Seric } 189297Seric 190297Seric /* add terminator to writeback message */ 1919375Seric 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; 2279375Seric 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="); 2359375Seric 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; 2499375Seric define('g', "$f", CurEnv); 2506978Seric ee = newenvelope(&errenvelope); 2516978Seric ee->e_puthdr = putheader; 2526978Seric ee->e_putbody = errbody; 2539375Seric ee->e_flags |= EF_RESPONSE; 2549375Seric ee->e_sendqueue = returnto; 2559542Seric openxscript(ee); 2569375Seric for (q = returnto; q != NULL; q = q->q_next) 2579375Seric { 2589375Seric if (q->q_alias == NULL) 2599375Seric addheader("to", q->q_paddr, ee); 2609375Seric } 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); 265*9884Seric if (parseaddr(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; 2759375Seric define('f', "$n", ee); 2769375Seric 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. 2999542Seric ** e -- the envelope we are working in. 300297Seric ** 301297Seric ** Returns: 302297Seric ** none 303297Seric ** 304297Seric ** Side Effects: 3056978Seric ** Outputs the body of an error message. 306297Seric */ 307297Seric 3089542Seric errbody(fp, m, xdot, e) 309297Seric register FILE *fp; 3104318Seric register struct mailer *m; 3114864Seric bool xdot; 3129542Seric register ENVELOPE *e; 313297Seric { 3146978Seric register FILE *xfile; 3153189Seric char buf[MAXLINE]; 3167124Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 3179337Seric char *p; 318297Seric 3199057Seric /* 3209057Seric ** Output transcript of errors 3219057Seric */ 3229057Seric 3234086Seric (void) fflush(stdout); 3249542Seric p = queuename(e->e_parent, 'x'); 3259337Seric if ((xfile = fopen(p, "r")) == NULL) 3269057Seric { 3279337Seric syserr("Cannot open %s", p); 3289057Seric fprintf(fp, " ----- Transcript of session is unavailable -----\n"); 3299057Seric } 3309057Seric else 3319057Seric { 3329057Seric fprintf(fp, " ----- Transcript of session follows -----\n"); 3339542Seric if (e->e_xfp != NULL) 3349542Seric (void) fflush(e->e_xfp); 3359057Seric while (fgets(buf, sizeof buf, xfile) != NULL) 3369057Seric putline(buf, fp, fullsmtp); 3379057Seric (void) fclose(xfile); 3389057Seric } 339297Seric errno = 0; 3404318Seric 3414318Seric /* 3424318Seric ** Output text of original message 3434318Seric */ 3444318Seric 3454289Seric if (NoReturn) 3464289Seric fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 3479542Seric else if (e->e_parent->e_dfp != NULL) 3484199Seric { 3495984Seric if (SendBody) 3505984Seric { 3515984Seric fprintf(fp, "\n ----- Unsent message follows -----\n"); 3525984Seric (void) fflush(fp); 3539542Seric putheader(fp, m, e->e_parent); 3546978Seric fprintf(fp, "\n"); 3559542Seric putbody(fp, m, xdot, e->e_parent); 3565984Seric } 3575984Seric else 3585984Seric { 3595984Seric fprintf(fp, "\n ----- Message header follows -----\n"); 3605984Seric (void) fflush(fp); 3619542Seric putheader(fp, m, e->e_parent); 3625984Seric } 3634199Seric } 3644199Seric else 3654199Seric fprintf(fp, "\n ----- No message was collected -----\n\n"); 3664318Seric 3674318Seric /* 3684318Seric ** Cleanup and exit 3694318Seric */ 3704318Seric 371297Seric if (errno != 0) 3726978Seric syserr("errbody: I/O error"); 373297Seric } 374