1297Seric # include <pwd.h> 23313Seric # include "sendmail.h" 3297Seric 4*7006Seric SCCSID(@(#)savemail.c 3.31 05/31/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: 15297Seric ** none 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 26297Seric savemail() 27297Seric { 28297Seric register struct passwd *pw; 29297Seric register FILE *xfile; 30297Seric char buf[MAXLINE+1]; 31297Seric extern struct passwd *getpwnam(); 32297Seric register char *p; 33297Seric extern char *ttypath(); 34297Seric static int exclusive; 355846Seric typedef int (*fnptr)(); 366978Seric extern ENVELOPE *newenvelope(); 37297Seric 386978Seric if (exclusive++) 39297Seric return; 406978Seric if (CurEnv->e_class <= PRI_JUNK) 416978Seric { 426978Seric if (Verbose) 436978Seric message(Arpa_Info, "Dumping junk mail"); 446978Seric return; 456978Seric } 465315Seric ForceMail = TRUE; 47297Seric 48297Seric /* 49297Seric ** In the unhappy event we don't know who to return the mail 50297Seric ** to, make someone up. 51297Seric */ 52297Seric 536904Seric if (CurEnv->e_from.q_paddr == NULL) 54297Seric { 556904Seric if (parse("root", &CurEnv->e_from, 0) == NULL) 56297Seric { 57297Seric syserr("Cannot parse root!"); 58297Seric ExitStat = EX_SOFTWARE; 59297Seric finis(); 60297Seric } 61297Seric } 626904Seric CurEnv->e_to = NULL; 63297Seric 64297Seric /* 65401Seric ** If called from Eric Schmidt's network, do special mailback. 66401Seric ** Fundamentally, this is the mailback case except that 67401Seric ** it returns an OK exit status (assuming the return 68401Seric ** worked). 696989Seric ** Also, if the from address is not local, mail it back. 70297Seric */ 71297Seric 72401Seric if (BerkNet) 73297Seric { 74401Seric ExitStat = EX_OK; 756989Seric MailBack = TRUE; 76297Seric } 776989Seric if (!bitset(M_LOCAL, CurEnv->e_from.q_mailer->m_flags)) 786989Seric MailBack = TRUE; 79297Seric 80297Seric /* 81297Seric ** If writing back, do it. 82297Seric ** If the user is still logged in on the same terminal, 83297Seric ** then write the error messages back to hir (sic). 84297Seric ** If not, set the MailBack flag so that it will get 85297Seric ** mailed back instead. 86297Seric */ 87297Seric 88297Seric if (WriteBack) 89297Seric { 90297Seric p = ttypath(); 91297Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 92297Seric { 936989Seric MailBack = TRUE; 94297Seric errno = 0; 95297Seric } 96297Seric else 97297Seric { 984712Seric (void) fflush(Xscript); 99401Seric xfile = fopen(Transcript, "r"); 100401Seric if (xfile == NULL) 101401Seric syserr("Cannot open %s", Transcript); 1026978Seric expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 1034162Seric printf("\r\nMessage from %s...\r\n", buf); 1044318Seric printf("Errors occurred while sending mail; transcript follows:\r\n"); 1054086Seric while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout)) 106297Seric fputs(buf, stdout); 107297Seric if (ferror(stdout)) 1084086Seric (void) syserr("savemail: stdout: write err"); 1094086Seric (void) fclose(xfile); 110297Seric } 111297Seric } 112297Seric 113297Seric /* 114297Seric ** If mailing back, do it. 115297Seric ** Throw away all further output. Don't do aliases, since 116297Seric ** this could cause loops, e.g., if joe mails to x:joe, 117297Seric ** and for some reason the network for x: is down, then 118297Seric ** the response gets sent to x:joe, which gives a 119297Seric ** response, etc. Also force the mail to be delivered 120297Seric ** even if a version of it has already been sent to the 121297Seric ** sender. 122297Seric */ 123297Seric 1243048Seric if (MailBack) 125297Seric { 1266978Seric if (returntosender("Unable to deliver mail", &CurEnv->e_from, TRUE) == 0) 127297Seric return; 128297Seric } 129297Seric 130297Seric /* 131297Seric ** Save the message in dead.letter. 132297Seric ** If we weren't mailing back, and the user is local, we 133297Seric ** should save the message in dead.letter so that the 134297Seric ** poor person doesn't have to type it over again -- 135297Seric ** and we all know what poor typists programmers are. 136297Seric */ 137297Seric 1384712Seric if (ArpaMode) 1394162Seric return; 1404079Seric p = NULL; 1416904Seric if (CurEnv->e_from.q_mailer == LocalMailer) 142297Seric { 1436904Seric if (CurEnv->e_from.q_home != NULL) 1446904Seric p = CurEnv->e_from.q_home; 1456904Seric else if ((pw = getpwnam(CurEnv->e_from.q_user)) != NULL) 1464079Seric p = pw->pw_dir; 147297Seric } 1484079Seric if (p == NULL) 149297Seric { 1506904Seric syserr("Can't return mail to %s", CurEnv->e_from.q_paddr); 151297Seric # ifdef DEBUG 152297Seric p = "/usr/tmp"; 153297Seric # else 154297Seric p = NULL; 155297Seric # endif 156297Seric } 1574199Seric if (p != NULL && TempFile != NULL) 158297Seric { 1595315Seric auto ADDRESS *q; 1605315Seric 161297Seric /* we have a home directory; open dead.letter */ 1624167Seric message(Arpa_Info, "Saving message in dead.letter"); 1634079Seric define('z', p); 1646978Seric expand("$z/dead.letter", buf, &buf[sizeof buf - 1], CurEnv); 1656904Seric CurEnv->e_to = buf; 1665315Seric q = NULL; 1675846Seric sendto(buf, -1, (ADDRESS *) NULL, &q); 1686978Seric (void) deliver(q); 169297Seric } 170297Seric 171297Seric /* add terminator to writeback message */ 172297Seric if (WriteBack) 173297Seric printf("-----\r\n"); 174297Seric } 175297Seric /* 1764633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 1774633Seric ** 1784633Seric ** Parameters: 1794633Seric ** msg -- the explanatory message. 1805984Seric ** sendbody -- if TRUE, also send back the body of the 1815984Seric ** message; otherwise just send the header. 1824633Seric ** 1834633Seric ** Returns: 1844633Seric ** zero -- if everything went ok. 1854633Seric ** else -- some error. 1864633Seric ** 1874633Seric ** Side Effects: 1884633Seric ** Returns the current message to the sender via 1894633Seric ** mail. 1904633Seric */ 1914633Seric 1925984Seric static bool SendBody; 1934633Seric 1946978Seric returntosender(msg, returnto, sendbody) 1954633Seric char *msg; 1966978Seric ADDRESS *returnto; 1975984Seric bool sendbody; 1984633Seric { 1994633Seric ADDRESS to_addr; 2004633Seric char buf[MAXNAME]; 2014633Seric register int i; 2026978Seric extern putheader(), errbody(); 2036978Seric register ENVELOPE *ee; 2046978Seric extern ENVELOPE *newenvelope(); 2056978Seric ENVELOPE errenvelope; 2064633Seric 2076989Seric NoAlias = TRUE; 2085984Seric SendBody = sendbody; 2096978Seric ee = newenvelope(&errenvelope); 2106978Seric ee->e_puthdr = putheader; 2116978Seric ee->e_putbody = errbody; 2126978Seric addheader("date", "$b", ee); 2136978Seric addheader("from", "$g (Mail Delivery Subsystem)", ee); 2146978Seric addheader("to", returnto->q_paddr, ee); 2156978Seric addheader("subject", msg, ee); 2164633Seric 2174633Seric /* fake up an address header for the from person */ 2186978Seric bmove((char *) returnto, (char *) &to_addr, sizeof to_addr); 2196978Seric expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 2206978Seric if (parse(buf, &ee->e_from, -1) == NULL) 2214633Seric { 2224633Seric syserr("Can't parse myself!"); 2234633Seric ExitStat = EX_SOFTWARE; 2244633Seric return (-1); 2254633Seric } 2264633Seric to_addr.q_next = NULL; 2275984Seric to_addr.q_flags &= ~QDONTSEND; 2286978Seric ee->e_sendqueue = &to_addr; 2295984Seric 2306978Seric /* push state into submessage */ 2316978Seric CurEnv = ee; 2326978Seric define('f', "$n"); 2336978Seric define('x', "Mail Delivery Subsystem"); 2345984Seric 2356978Seric /* actually deliver the error message */ 2366978Seric i = deliver(&to_addr); 2376978Seric 2386978Seric /* if the error message was "queued", make that happen */ 2396978Seric if (bitset(QQUEUEUP, to_addr.q_flags)) 240*7006Seric queueup(ee, FALSE); 2416978Seric 2426978Seric /* restore state */ 2436978Seric CurEnv = CurEnv->e_parent; 2446978Seric 2454633Seric if (i != 0) 2464633Seric { 2476978Seric syserr("Can't return mail to %s", to_addr.q_paddr); 2484633Seric return (-1); 2494633Seric } 2504633Seric return (0); 2514633Seric } 2524633Seric /* 2536978Seric ** ERRBODY -- output the body of an error message. 2546978Seric ** 2556978Seric ** Typically this is a copy of the transcript plus a copy of the 2566978Seric ** original offending message. 2576978Seric ** 258297Seric ** Parameters: 259297Seric ** xfile -- the transcript file. 260297Seric ** fp -- the output file. 2616978Seric ** xdot -- if set, use the SMTP hidden dot algorithm. 262297Seric ** 263297Seric ** Returns: 264297Seric ** none 265297Seric ** 266297Seric ** Side Effects: 2676978Seric ** Outputs the body of an error message. 268297Seric */ 269297Seric 2706978Seric errbody(fp, m, xdot) 271297Seric register FILE *fp; 2724318Seric register struct mailer *m; 2734864Seric bool xdot; 274297Seric { 2756978Seric register FILE *xfile; 2763189Seric char buf[MAXLINE]; 277297Seric 2784086Seric (void) fflush(stdout); 2793189Seric if ((xfile = fopen(Transcript, "r")) == NULL) 280297Seric syserr("Cannot open %s", Transcript); 281297Seric errno = 0; 2824318Seric 2834318Seric /* 2844318Seric ** Output transcript of errors 2854318Seric */ 2864318Seric 2876978Seric fprintf(fp, " ----- Transcript of session follows -----\n"); 2884712Seric (void) fflush(Xscript); 2894086Seric while (fgets(buf, sizeof buf, xfile) != NULL) 2903189Seric fputs(buf, fp); 2914318Seric 2924318Seric /* 2934318Seric ** Output text of original message 2944318Seric */ 2954318Seric 2964289Seric if (NoReturn) 2974289Seric fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 2984289Seric else if (TempFile != NULL) 2994199Seric { 3005984Seric if (SendBody) 3015984Seric { 3025984Seric fprintf(fp, "\n ----- Unsent message follows -----\n"); 3035984Seric (void) fflush(fp); 304*7006Seric putheader(fp, m, CurEnv->e_parent); 3056978Seric fprintf(fp, "\n"); 306*7006Seric putbody(fp, m, xdot); 3075984Seric } 3085984Seric else 3095984Seric { 3105984Seric fprintf(fp, "\n ----- Message header follows -----\n"); 3115984Seric (void) fflush(fp); 312*7006Seric putheader(fp, m, CurEnv); 3135984Seric } 3144199Seric } 3154199Seric else 3164199Seric fprintf(fp, "\n ----- No message was collected -----\n\n"); 3174318Seric 3184318Seric /* 3194318Seric ** Cleanup and exit 3204318Seric */ 3214318Seric 3224086Seric (void) fclose(xfile); 323297Seric if (errno != 0) 3246978Seric syserr("errbody: I/O error"); 325297Seric } 326