1297Seric # include <pwd.h> 23313Seric # include "sendmail.h" 3297Seric 4*7287Seric SCCSID(@(#)savemail.c 3.37 06/26/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 { 427053Seric message(Arpa_Info, "Dumping junk mail"); 436978Seric return; 446978Seric } 457053Seric ForceMail = TRUE; 46297Seric 47297Seric /* 48297Seric ** In the unhappy event we don't know who to return the mail 49297Seric ** to, make someone up. 50297Seric */ 51297Seric 527045Seric if (CurEnv->e_from.q_paddr == NULL) 53297Seric { 547045Seric if (parse("root", &CurEnv->e_from, 0) == NULL) 55297Seric { 56297Seric syserr("Cannot parse root!"); 57297Seric ExitStat = EX_SOFTWARE; 58297Seric finis(); 59297Seric } 60297Seric } 616904Seric CurEnv->e_to = NULL; 62297Seric 63297Seric /* 64401Seric ** If called from Eric Schmidt's network, do special mailback. 65401Seric ** Fundamentally, this is the mailback case except that 66401Seric ** it returns an OK exit status (assuming the return 67401Seric ** worked). 686989Seric ** Also, if the from address is not local, mail it back. 69297Seric */ 70297Seric 71401Seric if (BerkNet) 72297Seric { 73401Seric ExitStat = EX_OK; 746989Seric MailBack = TRUE; 75297Seric } 767045Seric if (!bitset(M_LOCAL, CurEnv->e_from.q_mailer->m_flags)) 776989Seric MailBack = TRUE; 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 { 926989Seric MailBack = TRUE; 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); 1016978Seric 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 { 1257203Seric if (CurEnv->e_errorqueue == NULL) 1267286Seric sendto(CurEnv->e_from.q_paddr, 1, (ADDRESS *) NULL, &CurEnv->e_errorqueue); 1277045Seric if (returntosender("Unable to deliver mail", CurEnv->e_errorqueue, TRUE) == 0) 128297Seric return; 129297Seric } 130297Seric 131297Seric /* 132297Seric ** Save the message in dead.letter. 133297Seric ** If we weren't mailing back, and the user is local, we 134297Seric ** should save the message in dead.letter so that the 135297Seric ** poor person doesn't have to type it over again -- 136297Seric ** and we all know what poor typists programmers are. 137297Seric */ 138297Seric 1394712Seric if (ArpaMode) 1404162Seric return; 1414079Seric p = NULL; 1427045Seric if (CurEnv->e_from.q_mailer == LocalMailer) 143297Seric { 1447045Seric if (CurEnv->e_from.q_home != NULL) 1457045Seric p = CurEnv->e_from.q_home; 1467045Seric else if ((pw = getpwnam(CurEnv->e_from.q_user)) != NULL) 1474079Seric p = pw->pw_dir; 148297Seric } 1494079Seric if (p == NULL) 150297Seric { 1517045Seric syserr("Can't return mail to %s", CurEnv->e_from.q_paddr); 152297Seric # ifdef DEBUG 153297Seric p = "/usr/tmp"; 154297Seric # else 155297Seric p = NULL; 156297Seric # endif 157297Seric } 1584199Seric if (p != NULL && TempFile != NULL) 159297Seric { 1605315Seric auto ADDRESS *q; 1617053Seric bool oldverb = Verbose; 1625315Seric 163297Seric /* we have a home directory; open dead.letter */ 1647053Seric Verbose = TRUE; 1654167Seric message(Arpa_Info, "Saving message in dead.letter"); 1667053Seric Verbose = oldverb; 1674079Seric define('z', p); 1686978Seric expand("$z/dead.letter", buf, &buf[sizeof buf - 1], CurEnv); 1696904Seric CurEnv->e_to = buf; 1705315Seric q = NULL; 1715846Seric sendto(buf, -1, (ADDRESS *) NULL, &q); 1726978Seric (void) deliver(q); 173297Seric } 174297Seric 175297Seric /* add terminator to writeback message */ 176297Seric if (WriteBack) 177297Seric printf("-----\r\n"); 178297Seric } 179297Seric /* 1804633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 1814633Seric ** 1824633Seric ** Parameters: 1834633Seric ** msg -- the explanatory message. 1847045Seric ** returnto -- the queue of people to send the message to. 1855984Seric ** sendbody -- if TRUE, also send back the body of the 1865984Seric ** message; otherwise just send the header. 1874633Seric ** 1884633Seric ** Returns: 1894633Seric ** zero -- if everything went ok. 1904633Seric ** else -- some error. 1914633Seric ** 1924633Seric ** Side Effects: 1934633Seric ** Returns the current message to the sender via 1944633Seric ** mail. 1954633Seric */ 1964633Seric 1975984Seric static bool SendBody; 1984633Seric 1997045Seric #define MAXRETURNS 6 /* max depth of returning messages */ 2007045Seric 2016978Seric returntosender(msg, returnto, sendbody) 2024633Seric char *msg; 2036978Seric ADDRESS *returnto; 2045984Seric bool sendbody; 2054633Seric { 2064633Seric char buf[MAXNAME]; 2076978Seric extern putheader(), errbody(); 2086978Seric register ENVELOPE *ee; 2096978Seric extern ENVELOPE *newenvelope(); 2106978Seric ENVELOPE errenvelope; 2117045Seric static int returndepth; 2124633Seric 213*7287Seric # ifdef DEBUG 214*7287Seric if (Debug > 0) 215*7287Seric { 216*7287Seric printf("Return To Sender: msg=\"%s\", depth=%d, CurEnv=%x,\n", 217*7287Seric msg, returndepth, CurEnv); 218*7287Seric printf("\treturnto="); 219*7287Seric printaddr(returnto, FALSE); 220*7287Seric } 221*7287Seric # endif DEBUG 222*7287Seric 2237045Seric if (++returndepth >= MAXRETURNS) 2247045Seric { 2257045Seric if (returndepth != MAXRETURNS) 2267045Seric syserr("returntosender: infinite recursion on %s", returnto->q_paddr); 2277045Seric /* don't "unrecurse" and fake a clean exit */ 2287045Seric /* returndepth--; */ 2297045Seric return (0); 2307045Seric } 2317045Seric 2326989Seric NoAlias = TRUE; 2335984Seric SendBody = sendbody; 2346978Seric ee = newenvelope(&errenvelope); 2356978Seric ee->e_puthdr = putheader; 2366978Seric ee->e_putbody = errbody; 2376978Seric addheader("date", "$b", ee); 2386978Seric addheader("from", "$g (Mail Delivery Subsystem)", ee); 2396978Seric addheader("to", returnto->q_paddr, ee); 2406978Seric addheader("subject", msg, ee); 2414633Seric 2424633Seric /* fake up an address header for the from person */ 2436978Seric expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 2446978Seric if (parse(buf, &ee->e_from, -1) == NULL) 2454633Seric { 2464633Seric syserr("Can't parse myself!"); 2474633Seric ExitStat = EX_SOFTWARE; 2487045Seric returndepth--; 2494633Seric return (-1); 2504633Seric } 2517045Seric ee->e_sendqueue = returnto; 2525984Seric 2536978Seric /* push state into submessage */ 2546978Seric CurEnv = ee; 2556978Seric define('f', "$n"); 2566978Seric define('x', "Mail Delivery Subsystem"); 2575984Seric 2586978Seric /* actually deliver the error message */ 2597045Seric sendall(ee, FALSE); 2606978Seric 2617045Seric /* do any closing error processing */ 2627045Seric checkerrors(ee); 2636978Seric 2646978Seric /* restore state */ 2656978Seric CurEnv = CurEnv->e_parent; 2667045Seric returndepth--; 2676978Seric 2687045Seric /* should check for delivery errors here */ 2694633Seric return (0); 2704633Seric } 2714633Seric /* 2726978Seric ** ERRBODY -- output the body of an error message. 2736978Seric ** 2746978Seric ** Typically this is a copy of the transcript plus a copy of the 2756978Seric ** original offending message. 2766978Seric ** 277297Seric ** Parameters: 278297Seric ** xfile -- the transcript file. 279297Seric ** fp -- the output file. 2806978Seric ** xdot -- if set, use the SMTP hidden dot algorithm. 281297Seric ** 282297Seric ** Returns: 283297Seric ** none 284297Seric ** 285297Seric ** Side Effects: 2866978Seric ** Outputs the body of an error message. 287297Seric */ 288297Seric 2896978Seric errbody(fp, m, xdot) 290297Seric register FILE *fp; 2914318Seric register struct mailer *m; 2924864Seric bool xdot; 293297Seric { 2946978Seric register FILE *xfile; 2953189Seric char buf[MAXLINE]; 2967124Seric bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 297297Seric 2984086Seric (void) fflush(stdout); 2993189Seric if ((xfile = fopen(Transcript, "r")) == NULL) 300297Seric syserr("Cannot open %s", Transcript); 301297Seric errno = 0; 3024318Seric 3034318Seric /* 3044318Seric ** Output transcript of errors 3054318Seric */ 3064318Seric 3076978Seric fprintf(fp, " ----- Transcript of session follows -----\n"); 3084712Seric (void) fflush(Xscript); 3094086Seric while (fgets(buf, sizeof buf, xfile) != NULL) 3107124Seric putline(buf, fp, fullsmtp); 3114318Seric 3124318Seric /* 3134318Seric ** Output text of original message 3144318Seric */ 3154318Seric 3164289Seric if (NoReturn) 3174289Seric fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 3184289Seric else if (TempFile != NULL) 3194199Seric { 3205984Seric if (SendBody) 3215984Seric { 3225984Seric fprintf(fp, "\n ----- Unsent message follows -----\n"); 3235984Seric (void) fflush(fp); 3247006Seric putheader(fp, m, CurEnv->e_parent); 3256978Seric fprintf(fp, "\n"); 3267006Seric putbody(fp, m, xdot); 3275984Seric } 3285984Seric else 3295984Seric { 3305984Seric fprintf(fp, "\n ----- Message header follows -----\n"); 3315984Seric (void) fflush(fp); 3327006Seric putheader(fp, m, CurEnv); 3335984Seric } 3344199Seric } 3354199Seric else 3364199Seric fprintf(fp, "\n ----- No message was collected -----\n\n"); 3374318Seric 3384318Seric /* 3394318Seric ** Cleanup and exit 3404318Seric */ 3414318Seric 3424086Seric (void) fclose(xfile); 343297Seric if (errno != 0) 3446978Seric syserr("errbody: I/O error"); 345297Seric } 346