1297Seric # include <pwd.h> 23313Seric # include "sendmail.h" 3297Seric 4*6904Seric SCCSID(@(#)savemail.c 3.28 05/22/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)(); 36297Seric 374199Seric if (exclusive++) 38297Seric return; 395315Seric ForceMail = TRUE; 40297Seric 41297Seric /* 42297Seric ** In the unhappy event we don't know who to return the mail 43297Seric ** to, make someone up. 44297Seric */ 45297Seric 46*6904Seric if (CurEnv->e_from.q_paddr == NULL) 47297Seric { 48*6904Seric if (parse("root", &CurEnv->e_from, 0) == NULL) 49297Seric { 50297Seric syserr("Cannot parse root!"); 51297Seric ExitStat = EX_SOFTWARE; 52297Seric finis(); 53297Seric } 54297Seric } 55*6904Seric CurEnv->e_to = NULL; 56297Seric 57297Seric /* 58401Seric ** If called from Eric Schmidt's network, do special mailback. 59401Seric ** Fundamentally, this is the mailback case except that 60401Seric ** it returns an OK exit status (assuming the return 61401Seric ** worked). 62297Seric */ 63297Seric 64401Seric if (BerkNet) 65297Seric { 66401Seric ExitStat = EX_OK; 67297Seric MailBack++; 68297Seric } 69297Seric 70297Seric /* 71297Seric ** If writing back, do it. 72297Seric ** If the user is still logged in on the same terminal, 73297Seric ** then write the error messages back to hir (sic). 74297Seric ** If not, set the MailBack flag so that it will get 75297Seric ** mailed back instead. 76297Seric */ 77297Seric 78297Seric if (WriteBack) 79297Seric { 80297Seric p = ttypath(); 81297Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 82297Seric { 83297Seric MailBack++; 84297Seric errno = 0; 85297Seric } 86297Seric else 87297Seric { 884712Seric (void) fflush(Xscript); 89401Seric xfile = fopen(Transcript, "r"); 90401Seric if (xfile == NULL) 91401Seric syserr("Cannot open %s", Transcript); 924086Seric (void) expand("$n", buf, &buf[sizeof buf - 1]); 934162Seric printf("\r\nMessage from %s...\r\n", buf); 944318Seric printf("Errors occurred while sending mail; transcript follows:\r\n"); 954086Seric while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout)) 96297Seric fputs(buf, stdout); 97297Seric if (ferror(stdout)) 984086Seric (void) syserr("savemail: stdout: write err"); 994086Seric (void) fclose(xfile); 100297Seric } 101297Seric } 102297Seric 103297Seric /* 104297Seric ** If mailing back, do it. 105297Seric ** Throw away all further output. Don't do aliases, since 106297Seric ** this could cause loops, e.g., if joe mails to x:joe, 107297Seric ** and for some reason the network for x: is down, then 108297Seric ** the response gets sent to x:joe, which gives a 109297Seric ** response, etc. Also force the mail to be delivered 110297Seric ** even if a version of it has already been sent to the 111297Seric ** sender. 112297Seric */ 113297Seric 1143048Seric if (MailBack) 115297Seric { 1165984Seric if (returntosender("Unable to deliver mail", TRUE) == 0) 117297Seric return; 118297Seric } 119297Seric 120297Seric /* 121297Seric ** Save the message in dead.letter. 122297Seric ** If we weren't mailing back, and the user is local, we 123297Seric ** should save the message in dead.letter so that the 124297Seric ** poor person doesn't have to type it over again -- 125297Seric ** and we all know what poor typists programmers are. 126297Seric */ 127297Seric 1284712Seric if (ArpaMode) 1294162Seric return; 1304079Seric p = NULL; 131*6904Seric if (CurEnv->e_from.q_mailer == LocalMailer) 132297Seric { 133*6904Seric if (CurEnv->e_from.q_home != NULL) 134*6904Seric p = CurEnv->e_from.q_home; 135*6904Seric else if ((pw = getpwnam(CurEnv->e_from.q_user)) != NULL) 1364079Seric p = pw->pw_dir; 137297Seric } 1384079Seric if (p == NULL) 139297Seric { 140*6904Seric syserr("Can't return mail to %s", CurEnv->e_from.q_paddr); 141297Seric # ifdef DEBUG 142297Seric p = "/usr/tmp"; 143297Seric # else 144297Seric p = NULL; 145297Seric # endif 146297Seric } 1474199Seric if (p != NULL && TempFile != NULL) 148297Seric { 1495315Seric auto ADDRESS *q; 1505315Seric 151297Seric /* we have a home directory; open dead.letter */ 1524167Seric message(Arpa_Info, "Saving message in dead.letter"); 1534079Seric define('z', p); 1544086Seric (void) expand("$z/dead.letter", buf, &buf[sizeof buf - 1]); 155*6904Seric CurEnv->e_to = buf; 1565315Seric q = NULL; 1575846Seric sendto(buf, -1, (ADDRESS *) NULL, &q); 1585846Seric (void) deliver(q, (fnptr) NULL); 159297Seric } 160297Seric 161297Seric /* add terminator to writeback message */ 162297Seric if (WriteBack) 163297Seric printf("-----\r\n"); 164297Seric } 165297Seric /* 1664633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 1674633Seric ** 1684633Seric ** Parameters: 1694633Seric ** msg -- the explanatory message. 1705984Seric ** sendbody -- if TRUE, also send back the body of the 1715984Seric ** message; otherwise just send the header. 1724633Seric ** 1734633Seric ** Returns: 1744633Seric ** zero -- if everything went ok. 1754633Seric ** else -- some error. 1764633Seric ** 1774633Seric ** Side Effects: 1784633Seric ** Returns the current message to the sender via 1794633Seric ** mail. 1804633Seric */ 1814633Seric 1824633Seric static char *ErrorMessage; 1835984Seric static bool SendBody; 1844633Seric 1855984Seric returntosender(msg, sendbody) 1864633Seric char *msg; 1875984Seric bool sendbody; 1884633Seric { 1894633Seric ADDRESS to_addr; 1904633Seric char buf[MAXNAME]; 1914633Seric register int i; 1924633Seric extern errhdr(); 1934633Seric 1944633Seric NoAlias++; 1954633Seric ErrorMessage = msg; 1965984Seric SendBody = sendbody; 1974633Seric 1984633Seric /* fake up an address header for the from person */ 199*6904Seric bmove((char *) &CurEnv->e_from, (char *) &to_addr, sizeof to_addr); 2004633Seric (void) expand("$n", buf, &buf[sizeof buf - 1]); 201*6904Seric if (parse(buf, &CurEnv->e_from, -1) == NULL) 2024633Seric { 2034633Seric syserr("Can't parse myself!"); 2044633Seric ExitStat = EX_SOFTWARE; 2054633Seric return (-1); 2064633Seric } 2074633Seric to_addr.q_next = NULL; 2085984Seric to_addr.q_flags &= ~QDONTSEND; 2094633Seric i = deliver(&to_addr, errhdr); 210*6904Seric bmove((char *) &to_addr, (char *) &CurEnv->e_from, sizeof CurEnv->e_from); 2115984Seric 212*6904Seric /* if CurEnv->e_from was queued up, put in on CurEnv->e_sendqueue */ 213*6904Seric if (bitset(QQUEUEUP, CurEnv->e_from.q_flags)) 2145984Seric { 215*6904Seric CurEnv->e_from.q_next = CurEnv->e_sendqueue; 216*6904Seric CurEnv->e_sendqueue = &CurEnv->e_from; 2175984Seric } 2185984Seric 2194633Seric if (i != 0) 2204633Seric { 221*6904Seric syserr("Can't return mail to %s", CurEnv->e_from.q_paddr); 2224633Seric return (-1); 2234633Seric } 2244633Seric return (0); 2254633Seric } 2264633Seric /* 227297Seric ** ERRHDR -- Output the header for error mail. 228297Seric ** 229297Seric ** This is the edit filter to error mailbacks. 230297Seric ** 231297Seric ** Parameters: 232297Seric ** xfile -- the transcript file. 233297Seric ** fp -- the output file. 2344864Seric ** xdot -- if set, use smtp hidden dot algorithm. 235297Seric ** 236297Seric ** Returns: 237297Seric ** none 238297Seric ** 239297Seric ** Side Effects: 2404633Seric ** Outputs the current message with an appropriate 2414633Seric ** error header. 242297Seric */ 243297Seric 2444864Seric errhdr(fp, m, xdot) 245297Seric register FILE *fp; 2464318Seric register struct mailer *m; 2474864Seric bool xdot; 248297Seric { 2493189Seric char buf[MAXLINE]; 2503189Seric register FILE *xfile; 2514454Seric extern char *macvalue(); 2524454Seric char *oldfmac; 2534454Seric char *oldgmac; 254297Seric 2554454Seric oldfmac = macvalue('f'); 2564454Seric define('f', "$n"); 2574454Seric oldgmac = macvalue('g'); 2584454Seric define('g', m->m_from); 2594454Seric 2604086Seric (void) fflush(stdout); 2614712Seric (void) fflush(Xscript); 2623189Seric if ((xfile = fopen(Transcript, "r")) == NULL) 263297Seric syserr("Cannot open %s", Transcript); 264297Seric errno = 0; 2654318Seric 2664318Seric /* 2674454Seric ** Output "From" line unless supressed 2684454Seric */ 2694454Seric 2704454Seric if (!bitset(M_NHDR, m->m_flags)) 2714454Seric { 2724454Seric (void) expand("$l", buf, &buf[sizeof buf - 1]); 2734454Seric fprintf(fp, "%s\n", buf); 2744454Seric } 2754454Seric 2764454Seric /* 2774318Seric ** Output header of error message. 2784318Seric */ 2794318Seric 2804318Seric if (bitset(M_NEEDDATE, m->m_flags)) 2814318Seric { 2824318Seric (void) expand("$b", buf, &buf[sizeof buf - 1]); 2834318Seric fprintf(fp, "Date: %s\n", buf); 2844318Seric } 2854318Seric if (bitset(M_NEEDFROM, m->m_flags)) 2864318Seric { 2874318Seric (void) expand("$g", buf, &buf[sizeof buf - 1]); 2884318Seric fprintf(fp, "From: %s (Mail Delivery Subsystem)\n", buf); 2894318Seric } 290*6904Seric fprintf(fp, "To: %s\n", CurEnv->e_to); 2914633Seric fprintf(fp, "Subject: %s\n", ErrorMessage); 2924318Seric 2934318Seric /* 2944454Seric ** End of error message header 2954454Seric */ 2964454Seric 2974454Seric define('f', oldfmac); 2984454Seric define('g', oldgmac); 2994454Seric 3004454Seric /* 3014318Seric ** Output transcript of errors 3024318Seric */ 3034318Seric 304297Seric fprintf(fp, "\n ----- Transcript of session follows -----\n"); 3054712Seric (void) fflush(Xscript); 3064086Seric while (fgets(buf, sizeof buf, xfile) != NULL) 3073189Seric fputs(buf, fp); 3084318Seric 3094318Seric /* 3104318Seric ** Output text of original message 3114318Seric */ 3124318Seric 3134289Seric if (NoReturn) 3144289Seric fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 3154289Seric else if (TempFile != NULL) 3164199Seric { 3175984Seric if (SendBody) 3185984Seric { 3195984Seric fprintf(fp, "\n ----- Unsent message follows -----\n"); 3205984Seric (void) fflush(fp); 3215984Seric putmessage(fp, Mailer[1], xdot); 3225984Seric } 3235984Seric else 3245984Seric { 3255984Seric fprintf(fp, "\n ----- Message header follows -----\n"); 3265984Seric (void) fflush(fp); 3275984Seric putheader(fp, Mailer[1]); 3285984Seric } 3294199Seric } 3304199Seric else 3314199Seric fprintf(fp, "\n ----- No message was collected -----\n\n"); 3324318Seric 3334318Seric /* 3344318Seric ** Cleanup and exit 3354318Seric */ 3364318Seric 3374086Seric (void) fclose(xfile); 338297Seric if (errno != 0) 339297Seric syserr("errhdr: I/O error"); 340297Seric } 341