1297Seric # include <pwd.h> 23313Seric # include "sendmail.h" 3297Seric 4*4633Seric static char SccsId[] = "@(#)savemail.c 3.21 10/27/81"; 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 register int i; 34297Seric extern char *ttypath(); 35297Seric static int exclusive; 36297Seric 374199Seric if (exclusive++) 38297Seric return; 39297Seric 40297Seric /* 41297Seric ** In the unhappy event we don't know who to return the mail 42297Seric ** to, make someone up. 43297Seric */ 44297Seric 45297Seric if (From.q_paddr == NULL) 46297Seric { 47297Seric if (parse("root", &From, 0) == NULL) 48297Seric { 49297Seric syserr("Cannot parse root!"); 50297Seric ExitStat = EX_SOFTWARE; 51297Seric finis(); 52297Seric } 53297Seric } 544079Seric To = NULL; 55297Seric 56297Seric /* 57401Seric ** If called from Eric Schmidt's network, do special mailback. 58401Seric ** Fundamentally, this is the mailback case except that 59401Seric ** it returns an OK exit status (assuming the return 60401Seric ** worked). 61297Seric */ 62297Seric 63401Seric if (BerkNet) 64297Seric { 65401Seric ExitStat = EX_OK; 66297Seric MailBack++; 67297Seric } 68297Seric 69297Seric /* 70297Seric ** If writing back, do it. 71297Seric ** If the user is still logged in on the same terminal, 72297Seric ** then write the error messages back to hir (sic). 73297Seric ** If not, set the MailBack flag so that it will get 74297Seric ** mailed back instead. 75297Seric */ 76297Seric 77297Seric if (WriteBack) 78297Seric { 79297Seric p = ttypath(); 80297Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 81297Seric { 82297Seric MailBack++; 83297Seric errno = 0; 84297Seric } 85297Seric else 86297Seric { 87401Seric xfile = fopen(Transcript, "r"); 88401Seric if (xfile == NULL) 89401Seric syserr("Cannot open %s", Transcript); 904086Seric (void) expand("$n", buf, &buf[sizeof buf - 1]); 914162Seric printf("\r\nMessage from %s...\r\n", buf); 924318Seric printf("Errors occurred while sending mail; transcript follows:\r\n"); 934086Seric while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout)) 94297Seric fputs(buf, stdout); 95297Seric if (ferror(stdout)) 964086Seric (void) syserr("savemail: stdout: write err"); 974086Seric (void) fclose(xfile); 98297Seric } 99297Seric } 100297Seric 101297Seric /* 102297Seric ** If mailing back, do it. 103297Seric ** Throw away all further output. Don't do aliases, since 104297Seric ** this could cause loops, e.g., if joe mails to x:joe, 105297Seric ** and for some reason the network for x: is down, then 106297Seric ** the response gets sent to x:joe, which gives a 107297Seric ** response, etc. Also force the mail to be delivered 108297Seric ** even if a version of it has already been sent to the 109297Seric ** sender. 110297Seric */ 111297Seric 1123048Seric if (MailBack) 113297Seric { 114*4633Seric if (returntosender("Unable to deliver mail") == 0) 115297Seric return; 116297Seric } 117297Seric 118297Seric /* 119297Seric ** Save the message in dead.letter. 120297Seric ** If we weren't mailing back, and the user is local, we 121297Seric ** should save the message in dead.letter so that the 122297Seric ** poor person doesn't have to type it over again -- 123297Seric ** and we all know what poor typists programmers are. 124297Seric */ 125297Seric 1264162Seric if (ArpaMode != ARPA_NONE) 1274162Seric return; 1284079Seric p = NULL; 1294599Seric if (From.q_mailer == LocalMailer) 130297Seric { 1314079Seric if (From.q_home != NULL) 1324079Seric p = From.q_home; 1334079Seric else if ((pw = getpwnam(From.q_user)) != NULL) 1344079Seric p = pw->pw_dir; 135297Seric } 1364079Seric if (p == NULL) 137297Seric { 1384079Seric syserr("Can't return mail to %s", From.q_paddr); 139297Seric # ifdef DEBUG 140297Seric p = "/usr/tmp"; 141297Seric # else 142297Seric p = NULL; 143297Seric # endif 144297Seric } 1454199Seric if (p != NULL && TempFile != NULL) 146297Seric { 147297Seric /* we have a home directory; open dead.letter */ 1484167Seric message(Arpa_Info, "Saving message in dead.letter"); 1494079Seric define('z', p); 1504086Seric (void) expand("$z/dead.letter", buf, &buf[sizeof buf - 1]); 1514066Seric To = buf; 1524398Seric i = mailfile(buf, &From); 1534599Seric giveresponse(i, TRUE, LocalMailer); 154297Seric } 155297Seric 156297Seric /* add terminator to writeback message */ 157297Seric if (WriteBack) 158297Seric printf("-----\r\n"); 159297Seric } 160297Seric /* 161*4633Seric ** RETURNTOSENDER -- return a message to the sender with an error. 162*4633Seric ** 163*4633Seric ** Parameters: 164*4633Seric ** msg -- the explanatory message. 165*4633Seric ** 166*4633Seric ** Returns: 167*4633Seric ** zero -- if everything went ok. 168*4633Seric ** else -- some error. 169*4633Seric ** 170*4633Seric ** Side Effects: 171*4633Seric ** Returns the current message to the sender via 172*4633Seric ** mail. 173*4633Seric */ 174*4633Seric 175*4633Seric static char *ErrorMessage; 176*4633Seric 177*4633Seric returntosender(msg) 178*4633Seric char *msg; 179*4633Seric { 180*4633Seric ADDRESS to_addr; 181*4633Seric char buf[MAXNAME]; 182*4633Seric register int i; 183*4633Seric extern errhdr(); 184*4633Seric 185*4633Seric (void) freopen("/dev/null", "w", stdout); 186*4633Seric NoAlias++; 187*4633Seric ForceMail++; 188*4633Seric ErrorMessage = msg; 189*4633Seric 190*4633Seric /* fake up an address header for the from person */ 191*4633Seric bmove((char *) &From, (char *) &to_addr, sizeof to_addr); 192*4633Seric (void) expand("$n", buf, &buf[sizeof buf - 1]); 193*4633Seric if (parse(buf, &From, -1) == NULL) 194*4633Seric { 195*4633Seric syserr("Can't parse myself!"); 196*4633Seric ExitStat = EX_SOFTWARE; 197*4633Seric return (-1); 198*4633Seric } 199*4633Seric to_addr.q_next = NULL; 200*4633Seric i = deliver(&to_addr, errhdr); 201*4633Seric bmove((char *) &to_addr, (char *) &From, sizeof From); 202*4633Seric if (i != 0) 203*4633Seric { 204*4633Seric syserr("Can't return mail to %s", From.q_paddr); 205*4633Seric return (-1); 206*4633Seric } 207*4633Seric return (0); 208*4633Seric } 209*4633Seric /* 210297Seric ** ERRHDR -- Output the header for error mail. 211297Seric ** 212297Seric ** This is the edit filter to error mailbacks. 213297Seric ** 214297Seric ** Parameters: 215297Seric ** xfile -- the transcript file. 216297Seric ** fp -- the output file. 217297Seric ** 218297Seric ** Returns: 219297Seric ** none 220297Seric ** 221297Seric ** Side Effects: 222*4633Seric ** Outputs the current message with an appropriate 223*4633Seric ** error header. 224297Seric */ 225297Seric 2264318Seric errhdr(fp, m) 227297Seric register FILE *fp; 2284318Seric register struct mailer *m; 229297Seric { 2303189Seric char buf[MAXLINE]; 2313189Seric register FILE *xfile; 2324454Seric extern char *macvalue(); 2334454Seric char *oldfmac; 2344454Seric char *oldgmac; 235297Seric 2364454Seric oldfmac = macvalue('f'); 2374454Seric define('f', "$n"); 2384454Seric oldgmac = macvalue('g'); 2394454Seric define('g', m->m_from); 2404454Seric 2414086Seric (void) fflush(stdout); 2423189Seric if ((xfile = fopen(Transcript, "r")) == NULL) 243297Seric syserr("Cannot open %s", Transcript); 244297Seric errno = 0; 2454318Seric 2464318Seric /* 2474454Seric ** Output "From" line unless supressed 2484454Seric */ 2494454Seric 2504454Seric if (!bitset(M_NHDR, m->m_flags)) 2514454Seric { 2524454Seric (void) expand("$l", buf, &buf[sizeof buf - 1]); 2534454Seric fprintf(fp, "%s\n", buf); 2544454Seric } 2554454Seric 2564454Seric /* 2574318Seric ** Output header of error message. 2584318Seric */ 2594318Seric 2604318Seric if (bitset(M_NEEDDATE, m->m_flags)) 2614318Seric { 2624318Seric (void) expand("$b", buf, &buf[sizeof buf - 1]); 2634318Seric fprintf(fp, "Date: %s\n", buf); 2644318Seric } 2654318Seric if (bitset(M_NEEDFROM, m->m_flags)) 2664318Seric { 2674318Seric (void) expand("$g", buf, &buf[sizeof buf - 1]); 2684318Seric fprintf(fp, "From: %s (Mail Delivery Subsystem)\n", buf); 2694318Seric } 270297Seric fprintf(fp, "To: %s\n", To); 271*4633Seric fprintf(fp, "Subject: %s\n", ErrorMessage); 2724318Seric 2734318Seric /* 2744454Seric ** End of error message header 2754454Seric */ 2764454Seric 2774454Seric define('f', oldfmac); 2784454Seric define('g', oldgmac); 2794454Seric 2804454Seric /* 2814318Seric ** Output transcript of errors 2824318Seric */ 2834318Seric 284297Seric fprintf(fp, "\n ----- Transcript of session follows -----\n"); 2854086Seric while (fgets(buf, sizeof buf, xfile) != NULL) 2863189Seric fputs(buf, fp); 2874318Seric 2884318Seric /* 2894318Seric ** Output text of original message 2904318Seric */ 2914318Seric 2924289Seric if (NoReturn) 2934289Seric fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 2944289Seric else if (TempFile != NULL) 2954199Seric { 2964199Seric fprintf(fp, "\n ----- Unsent message follows -----\n"); 2974199Seric (void) fflush(fp); 2984199Seric putmessage(fp, Mailer[1]); 2994199Seric } 3004199Seric else 3014199Seric fprintf(fp, "\n ----- No message was collected -----\n\n"); 3024318Seric 3034318Seric /* 3044318Seric ** Cleanup and exit 3054318Seric */ 3064318Seric 3074086Seric (void) fclose(xfile); 308297Seric if (errno != 0) 309297Seric syserr("errhdr: I/O error"); 310297Seric } 311