1297Seric # include <pwd.h> 23313Seric # include "sendmail.h" 3297Seric 4*6978Seric SCCSID(@(#)savemail.c 3.29.1.1 05/29/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)(); 36*6978Seric ENVELOPE errenvelope; 37*6978Seric register ENVELOPE *ee; 38*6978Seric extern ENVELOPE *newenvelope(); 39297Seric 40*6978Seric if (exclusive++) 41297Seric return; 42*6978Seric if (CurEnv->e_class <= PRI_JUNK) 43*6978Seric { 44*6978Seric if (Verbose) 45*6978Seric message(Arpa_Info, "Dumping junk mail"); 46*6978Seric return; 47*6978Seric } 485315Seric ForceMail = TRUE; 49297Seric 50297Seric /* 51297Seric ** In the unhappy event we don't know who to return the mail 52297Seric ** to, make someone up. 53297Seric */ 54297Seric 556904Seric if (CurEnv->e_from.q_paddr == NULL) 56297Seric { 576904Seric if (parse("root", &CurEnv->e_from, 0) == NULL) 58297Seric { 59297Seric syserr("Cannot parse root!"); 60297Seric ExitStat = EX_SOFTWARE; 61297Seric finis(); 62297Seric } 63297Seric } 646904Seric CurEnv->e_to = NULL; 65297Seric 66297Seric /* 67401Seric ** If called from Eric Schmidt's network, do special mailback. 68401Seric ** Fundamentally, this is the mailback case except that 69401Seric ** it returns an OK exit status (assuming the return 70401Seric ** worked). 71297Seric */ 72297Seric 73401Seric if (BerkNet) 74297Seric { 75401Seric ExitStat = EX_OK; 76297Seric MailBack++; 77297Seric } 78297Seric 79297Seric /* 80297Seric ** If writing back, do it. 81297Seric ** If the user is still logged in on the same terminal, 82297Seric ** then write the error messages back to hir (sic). 83297Seric ** If not, set the MailBack flag so that it will get 84297Seric ** mailed back instead. 85297Seric */ 86297Seric 87297Seric if (WriteBack) 88297Seric { 89297Seric p = ttypath(); 90297Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 91297Seric { 92297Seric MailBack++; 93297Seric errno = 0; 94297Seric } 95297Seric else 96297Seric { 974712Seric (void) fflush(Xscript); 98401Seric xfile = fopen(Transcript, "r"); 99401Seric if (xfile == NULL) 100401Seric syserr("Cannot open %s", Transcript); 101*6978Seric expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 1024162Seric printf("\r\nMessage from %s...\r\n", buf); 1034318Seric printf("Errors occurred while sending mail; transcript follows:\r\n"); 1044086Seric while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout)) 105297Seric fputs(buf, stdout); 106297Seric if (ferror(stdout)) 1074086Seric (void) syserr("savemail: stdout: write err"); 1084086Seric (void) fclose(xfile); 109297Seric } 110297Seric } 111297Seric 112297Seric /* 113297Seric ** If mailing back, do it. 114297Seric ** Throw away all further output. Don't do aliases, since 115297Seric ** this could cause loops, e.g., if joe mails to x:joe, 116297Seric ** and for some reason the network for x: is down, then 117297Seric ** the response gets sent to x:joe, which gives a 118297Seric ** response, etc. Also force the mail to be delivered 119297Seric ** even if a version of it has already been sent to the 120297Seric ** sender. 121297Seric */ 122297Seric 1233048Seric if (MailBack) 124297Seric { 125*6978Seric if (returntosender("Unable to deliver mail", &CurEnv->e_from, TRUE) == 0) 126297Seric return; 127297Seric } 128297Seric 129297Seric /* 130297Seric ** Save the message in dead.letter. 131297Seric ** If we weren't mailing back, and the user is local, we 132297Seric ** should save the message in dead.letter so that the 133297Seric ** poor person doesn't have to type it over again -- 134297Seric ** and we all know what poor typists programmers are. 135297Seric */ 136297Seric 1374712Seric if (ArpaMode) 1384162Seric return; 1394079Seric p = NULL; 1406904Seric if (CurEnv->e_from.q_mailer == LocalMailer) 141297Seric { 1426904Seric if (CurEnv->e_from.q_home != NULL) 1436904Seric p = CurEnv->e_from.q_home; 1446904Seric else if ((pw = getpwnam(CurEnv->e_from.q_user)) != NULL) 1454079Seric p = pw->pw_dir; 146297Seric } 1474079Seric if (p == NULL) 148297Seric { 1496904Seric syserr("Can't return mail to %s", CurEnv->e_from.q_paddr); 150297Seric # ifdef DEBUG 151297Seric p = "/usr/tmp"; 152297Seric # else 153297Seric p = NULL; 154297Seric # endif 155297Seric } 1564199Seric if (p != NULL && TempFile != NULL) 157297Seric { 1585315Seric auto ADDRESS *q; 1595315Seric 160297Seric /* we have a home directory; open dead.letter */ 1614167Seric message(Arpa_Info, "Saving message in dead.letter"); 1624079Seric define('z', p); 163*6978Seric expand("$z/dead.letter", buf, &buf[sizeof buf - 1], CurEnv); 1646904Seric CurEnv->e_to = buf; 1655315Seric q = NULL; 1665846Seric sendto(buf, -1, (ADDRESS *) NULL, &q); 167*6978Seric (void) deliver(q); 168297Seric } 169297Seric 170297Seric /* add terminator to writeback message */ 171297Seric if (WriteBack) 172297Seric printf("-----\r\n"); 173297Seric } 174297Seric /* 1754633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 1764633Seric ** 1774633Seric ** Parameters: 1784633Seric ** msg -- the explanatory message. 1795984Seric ** sendbody -- if TRUE, also send back the body of the 1805984Seric ** message; otherwise just send the header. 1814633Seric ** 1824633Seric ** Returns: 1834633Seric ** zero -- if everything went ok. 1844633Seric ** else -- some error. 1854633Seric ** 1864633Seric ** Side Effects: 1874633Seric ** Returns the current message to the sender via 1884633Seric ** mail. 1894633Seric */ 1904633Seric 1914633Seric static char *ErrorMessage; 1925984Seric static bool SendBody; 1934633Seric 194*6978Seric returntosender(msg, returnto, sendbody) 1954633Seric char *msg; 196*6978Seric ADDRESS *returnto; 1975984Seric bool sendbody; 1984633Seric { 1994633Seric ADDRESS to_addr; 2004633Seric char buf[MAXNAME]; 2014633Seric register int i; 202*6978Seric extern putheader(), errbody(); 203*6978Seric register ENVELOPE *ee; 204*6978Seric extern ENVELOPE *newenvelope(); 205*6978Seric ENVELOPE errenvelope; 2064633Seric 2074633Seric NoAlias++; 2085984Seric SendBody = sendbody; 209*6978Seric ee = newenvelope(&errenvelope); 210*6978Seric ee->e_puthdr = putheader; 211*6978Seric ee->e_putbody = errbody; 212*6978Seric addheader("date", "$b", ee); 213*6978Seric addheader("from", "$g (Mail Delivery Subsystem)", ee); 214*6978Seric addheader("to", returnto->q_paddr, ee); 215*6978Seric addheader("subject", msg, ee); 2164633Seric 2174633Seric /* fake up an address header for the from person */ 218*6978Seric bmove((char *) returnto, (char *) &to_addr, sizeof to_addr); 219*6978Seric expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 220*6978Seric 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; 228*6978Seric ee->e_sendqueue = &to_addr; 2295984Seric 230*6978Seric /* push state into submessage */ 231*6978Seric CurEnv = ee; 232*6978Seric define('f', "$n"); 233*6978Seric define('x', "Mail Delivery Subsystem"); 2345984Seric 235*6978Seric /* actually deliver the error message */ 236*6978Seric i = deliver(&to_addr); 237*6978Seric 238*6978Seric /* if the error message was "queued", make that happen */ 239*6978Seric if (bitset(QQUEUEUP, to_addr.q_flags)) 240*6978Seric queueup(ee); 241*6978Seric 242*6978Seric /* restore state */ 243*6978Seric CurEnv = CurEnv->e_parent; 244*6978Seric 2454633Seric if (i != 0) 2464633Seric { 247*6978Seric syserr("Can't return mail to %s", to_addr.q_paddr); 2484633Seric return (-1); 2494633Seric } 2504633Seric return (0); 2514633Seric } 2524633Seric /* 253*6978Seric ** ERRHEADER -- Output the header for error mail. 254297Seric ** 255*6978Seric ** Parameters: 256*6978Seric ** xfile -- the transcript file. 257*6978Seric ** fp -- the output file. 258297Seric ** 259*6978Seric ** Returns: 260*6978Seric ** none 261*6978Seric ** 262*6978Seric ** Side Effects: 263*6978Seric ** Outputs the header for an error message. 264*6978Seric */ 265*6978Seric 266*6978Seric errheader(fp, m) 267*6978Seric register FILE *fp; 268*6978Seric register struct mailer *m; 269*6978Seric { 270*6978Seric /* 271*6978Seric ** Output header of error message. 272*6978Seric */ 273*6978Seric 274*6978Seric putheader(fp, m); 275*6978Seric } 276*6978Seric /* 277*6978Seric ** ERRBODY -- output the body of an error message. 278*6978Seric ** 279*6978Seric ** Typically this is a copy of the transcript plus a copy of the 280*6978Seric ** original offending message. 281*6978Seric ** 282297Seric ** Parameters: 283297Seric ** xfile -- the transcript file. 284297Seric ** fp -- the output file. 285*6978Seric ** xdot -- if set, use the SMTP hidden dot algorithm. 286297Seric ** 287297Seric ** Returns: 288297Seric ** none 289297Seric ** 290297Seric ** Side Effects: 291*6978Seric ** Outputs the body of an error message. 292297Seric */ 293297Seric 294*6978Seric errbody(fp, m, xdot) 295297Seric register FILE *fp; 2964318Seric register struct mailer *m; 2974864Seric bool xdot; 298297Seric { 299*6978Seric register FILE *xfile; 3003189Seric char buf[MAXLINE]; 301297Seric 3024086Seric (void) fflush(stdout); 3033189Seric if ((xfile = fopen(Transcript, "r")) == NULL) 304297Seric syserr("Cannot open %s", Transcript); 305297Seric errno = 0; 3064318Seric 3074318Seric /* 3084318Seric ** Output transcript of errors 3094318Seric */ 3104318Seric 311*6978Seric fprintf(fp, " ----- Transcript of session follows -----\n"); 3124712Seric (void) fflush(Xscript); 3134086Seric while (fgets(buf, sizeof buf, xfile) != NULL) 3143189Seric fputs(buf, fp); 3154318Seric 3164318Seric /* 3174318Seric ** Output text of original message 3184318Seric */ 3194318Seric 3204289Seric if (NoReturn) 3214289Seric fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 3224289Seric else if (TempFile != NULL) 3234199Seric { 3245984Seric if (SendBody) 3255984Seric { 3265984Seric fprintf(fp, "\n ----- Unsent message follows -----\n"); 3275984Seric (void) fflush(fp); 328*6978Seric putheader(fp, Mailer[1], CurEnv->e_parent); 329*6978Seric fprintf(fp, "\n"); 330*6978Seric putbody(fp, Mailer[1], xdot); 3315984Seric } 3325984Seric else 3335984Seric { 3345984Seric fprintf(fp, "\n ----- Message header follows -----\n"); 3355984Seric (void) fflush(fp); 3365984Seric putheader(fp, Mailer[1]); 3375984Seric } 3384199Seric } 3394199Seric else 3404199Seric fprintf(fp, "\n ----- No message was collected -----\n\n"); 3414318Seric 3424318Seric /* 3434318Seric ** Cleanup and exit 3444318Seric */ 3454318Seric 3464086Seric (void) fclose(xfile); 347297Seric if (errno != 0) 348*6978Seric syserr("errbody: I/O error"); 349297Seric } 350