1297Seric # include <pwd.h> 23313Seric # include "sendmail.h" 3297Seric 4*16159Seric SCCSID(@(#)savemail.c 4.4 03/11/84); 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 { 5911447Seric if (parseaddr("root", &e->e_from, 0, '\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 } 8110683Seric if (!bitnset(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 { 10116152Seric expand("\001n", 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); 14510106Seric if (returntosender(e->e_message != NULL ? e->e_message : 14610106Seric "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); 18116152Seric expand("\001z/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; 25016152Seric define('g', "\001f", 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 } 26210845Seric (void) sprintf(buf, "Returned mail: %s", msg); 26310106Seric addheader("subject", buf, ee); 2644633Seric 2654633Seric /* fake up an address header for the from person */ 26616152Seric expand("\001n", buf, &buf[sizeof buf - 1], CurEnv); 26711447Seric if (parseaddr(buf, &ee->e_from, -1, '\0') == NULL) 2684633Seric { 2694633Seric syserr("Can't parse myself!"); 2704633Seric ExitStat = EX_SOFTWARE; 2717045Seric returndepth--; 2724633Seric return (-1); 2734633Seric } 274*16159Seric loweraddr(&ee->e_from); 2755984Seric 2766978Seric /* push state into submessage */ 2776978Seric CurEnv = ee; 27816152Seric define('f', "\001n", ee); 2799375Seric define('x', "Mail Delivery Subsystem", ee); 28011291Seric eatheader(ee); 2815984Seric 2826978Seric /* actually deliver the error message */ 28314876Seric sendall(ee, SM_DEFAULT); 2846978Seric 2856978Seric /* restore state */ 2867811Seric dropenvelope(ee); 2876978Seric CurEnv = CurEnv->e_parent; 2887045Seric returndepth--; 2896978Seric 2907045Seric /* should check for delivery errors here */ 2914633Seric return (0); 2924633Seric } 2934633Seric /* 2946978Seric ** ERRBODY -- output the body of an error message. 2956978Seric ** 2966978Seric ** Typically this is a copy of the transcript plus a copy of the 2976978Seric ** original offending message. 2986978Seric ** 299297Seric ** Parameters: 300297Seric ** fp -- the output file. 30110170Seric ** m -- the mailer to output to. 3029542Seric ** e -- the envelope we are working in. 303297Seric ** 304297Seric ** Returns: 305297Seric ** none 306297Seric ** 307297Seric ** Side Effects: 3086978Seric ** Outputs the body of an error message. 309297Seric */ 310297Seric 31110170Seric errbody(fp, m, e) 312297Seric register FILE *fp; 3134318Seric register struct mailer *m; 3149542Seric register ENVELOPE *e; 315297Seric { 3166978Seric register FILE *xfile; 3173189Seric char buf[MAXLINE]; 3189337Seric char *p; 319297Seric 3209057Seric /* 3219057Seric ** Output transcript of errors 3229057Seric */ 3239057Seric 3244086Seric (void) fflush(stdout); 3259542Seric p = queuename(e->e_parent, 'x'); 3269337Seric if ((xfile = fopen(p, "r")) == NULL) 3279057Seric { 3289337Seric syserr("Cannot open %s", p); 3299057Seric fprintf(fp, " ----- Transcript of session is unavailable -----\n"); 3309057Seric } 3319057Seric else 3329057Seric { 3339057Seric fprintf(fp, " ----- Transcript of session follows -----\n"); 3349542Seric if (e->e_xfp != NULL) 3359542Seric (void) fflush(e->e_xfp); 3369057Seric while (fgets(buf, sizeof buf, xfile) != NULL) 33710170Seric putline(buf, fp, m); 3389057Seric (void) fclose(xfile); 3399057Seric } 340297Seric errno = 0; 3414318Seric 3424318Seric /* 3434318Seric ** Output text of original message 3444318Seric */ 3454318Seric 3464289Seric if (NoReturn) 3474289Seric fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 3489542Seric else if (e->e_parent->e_dfp != NULL) 3494199Seric { 3505984Seric if (SendBody) 3515984Seric { 35210170Seric putline("\n", fp, m); 35310170Seric putline(" ----- Unsent message follows -----\n", fp, m); 3545984Seric (void) fflush(fp); 35510170Seric putheader(fp, m, e->e_parent); 35610170Seric putline("\n", fp, m); 35710170Seric putbody(fp, m, e->e_parent); 3585984Seric } 3595984Seric else 3605984Seric { 36110170Seric putline("\n", fp, m); 36210170Seric putline(" ----- Message header follows -----\n", fp, m); 3635984Seric (void) fflush(fp); 36410170Seric putheader(fp, m, e->e_parent); 3655984Seric } 3664199Seric } 3674199Seric else 36810170Seric { 36910170Seric putline("\n", fp, m); 37010170Seric putline(" ----- No message was collected -----\n", fp, m); 37110170Seric putline("\n", fp, m); 37210170Seric } 3734318Seric 3744318Seric /* 3754318Seric ** Cleanup and exit 3764318Seric */ 3774318Seric 378297Seric if (errno != 0) 3796978Seric syserr("errbody: I/O error"); 380297Seric } 381