1297Seric # include <stdio.h> 2297Seric # include <pwd.h> 3*3313Seric # include "sendmail.h" 4297Seric 5*3313Seric static char SccsId[] = "@(#)savemail.c 3.6 03/20/81"; 6408Seric 7297Seric /* 8297Seric ** SAVEMAIL -- Save mail on error 9297Seric ** 10297Seric ** If the MailBack flag is set, mail it back to the originator 11297Seric ** together with an error message; otherwise, just put it in 12297Seric ** dead.letter in the user's home directory (if he exists on 13297Seric ** this machine). 14297Seric ** 15297Seric ** Parameters: 16297Seric ** none 17297Seric ** 18297Seric ** Returns: 19297Seric ** none 20297Seric ** 21297Seric ** Side Effects: 22297Seric ** Saves the letter, by writing or mailing it back to the 23297Seric ** sender, or by putting it in dead.letter in her home 24297Seric ** directory. 25297Seric ** 26297Seric ** WARNING: the user id is reset to the original user. 27297Seric */ 28297Seric 29297Seric savemail() 30297Seric { 31297Seric register struct passwd *pw; 32297Seric register FILE *xfile; 33297Seric char buf[MAXLINE+1]; 34297Seric extern errhdr(); 352973Seric auto ADDRESS to_addr; 36297Seric extern struct passwd *getpwnam(); 37297Seric register char *p; 38297Seric register int i; 39297Seric auto long tim; 40297Seric extern int errno; 41297Seric extern char *ttypath(); 422973Seric extern ADDRESS *parse(); 43297Seric static int exclusive; 442991Seric extern char *strcpy(), *strcat(); 453189Seric extern char *expand(); 46297Seric 47297Seric if (exclusive++) 48297Seric return; 49297Seric 50297Seric /* 51297Seric ** In the unhappy event we don't know who to return the mail 52297Seric ** to, make someone up. 53297Seric */ 54297Seric 55297Seric if (From.q_paddr == NULL) 56297Seric { 57297Seric if (parse("root", &From, 0) == NULL) 58297Seric { 59297Seric syserr("Cannot parse root!"); 60297Seric ExitStat = EX_SOFTWARE; 61297Seric finis(); 62297Seric } 63297Seric } 64297Seric 65297Seric /* 66401Seric ** If called from Eric Schmidt's network, do special mailback. 67401Seric ** Fundamentally, this is the mailback case except that 68401Seric ** it returns an OK exit status (assuming the return 69401Seric ** worked). 70297Seric */ 71297Seric 72401Seric if (BerkNet) 73297Seric { 74401Seric ExitStat = EX_OK; 75297Seric MailBack++; 76297Seric } 77297Seric 78297Seric /* 79297Seric ** If writing back, do it. 80297Seric ** If the user is still logged in on the same terminal, 81297Seric ** then write the error messages back to hir (sic). 82297Seric ** If not, set the MailBack flag so that it will get 83297Seric ** mailed back instead. 84297Seric */ 85297Seric 86297Seric if (WriteBack) 87297Seric { 88297Seric p = ttypath(); 89297Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 90297Seric { 91297Seric MailBack++; 92297Seric errno = 0; 93297Seric } 94297Seric else 95297Seric { 96401Seric xfile = fopen(Transcript, "r"); 97401Seric if (xfile == NULL) 98401Seric syserr("Cannot open %s", Transcript); 993189Seric printf("\r\nMessage from %s\r\n", expand("$n", buf, &buf[sizeof buf - 1])); 100297Seric printf("Errors occurred while sending mail, transcript follows:\r\n"); 101297Seric while (fgets(buf, sizeof buf, xfile) && !ferror(stdout)) 102297Seric fputs(buf, stdout); 103297Seric if (ferror(stdout)) 104297Seric syserr("savemail: stdout: write err"); 105401Seric fclose(xfile); 106297Seric } 107297Seric } 108297Seric 109297Seric /* 110297Seric ** If mailing back, do it. 111297Seric ** Throw away all further output. Don't do aliases, since 112297Seric ** this could cause loops, e.g., if joe mails to x:joe, 113297Seric ** and for some reason the network for x: is down, then 114297Seric ** the response gets sent to x:joe, which gives a 115297Seric ** response, etc. Also force the mail to be delivered 116297Seric ** even if a version of it has already been sent to the 117297Seric ** sender. 118297Seric */ 119297Seric 1203048Seric if (MailBack) 121297Seric { 122297Seric freopen("/dev/null", "w", stdout); 123297Seric NoAlias++; 124297Seric ForceMail++; 125297Seric 126297Seric /* fake up an address header for the from person */ 127297Seric bmove((char *) &From, (char *) &to_addr, sizeof to_addr); 1283189Seric if (parse(expand("$n", buf, &buf[sizeof buf - 1]), &From, -1) == NULL) 129297Seric { 130297Seric syserr("Can't parse myself!"); 131297Seric ExitStat = EX_SOFTWARE; 132297Seric finis(); 133297Seric } 134297Seric i = deliver(&to_addr, errhdr); 135297Seric bmove((char *) &to_addr, (char *) &From, sizeof From); 136297Seric if (i != 0) 137297Seric syserr("Can't return mail to %s", p); 138297Seric else 139297Seric return; 140297Seric } 141297Seric 142297Seric /* 143297Seric ** Save the message in dead.letter. 144297Seric ** If we weren't mailing back, and the user is local, we 145297Seric ** should save the message in dead.letter so that the 146297Seric ** poor person doesn't have to type it over again -- 147297Seric ** and we all know what poor typists programmers are. 148297Seric */ 149297Seric 150297Seric setuid(getuid()); 151297Seric setgid(getgid()); 152297Seric setpwent(); 1533048Seric if (From.q_mailer == 0 && (pw = getpwnam(From.q_user)) != NULL) 154297Seric { 155297Seric /* user has a home directory */ 156297Seric p = pw->pw_dir; 157297Seric } 158297Seric else 159297Seric { 160297Seric syserr("Can't return mail to %s (pw=%u)", From.q_paddr, pw); 161297Seric # ifdef DEBUG 162297Seric p = "/usr/tmp"; 163297Seric # else 164297Seric p = NULL; 165297Seric # endif 166297Seric } 167297Seric if (p != NULL) 168297Seric { 169297Seric /* we have a home directory; open dead.letter */ 170297Seric strcpy(buf, p); 171297Seric strcat(buf, "/dead.letter"); 1723189Seric if (mailfile(buf) != EX_OK) 173297Seric printf("Cannot save mail, sorry\n"); 174297Seric else 175297Seric printf("Letter saved in dead.letter\n"); 176297Seric } 177297Seric else 178297Seric 179297Seric /* add terminator to writeback message */ 180297Seric if (WriteBack) 181297Seric printf("-----\r\n"); 182297Seric } 183297Seric /* 184297Seric ** ERRHDR -- Output the header for error mail. 185297Seric ** 186297Seric ** This is the edit filter to error mailbacks. 187297Seric ** 188297Seric ** Algorithm: 189297Seric ** Output fixed header. 190297Seric ** Output the transcript part. 191297Seric ** Output the original message. 192297Seric ** 193297Seric ** Parameters: 194297Seric ** xfile -- the transcript file. 195297Seric ** fp -- the output file. 196297Seric ** 197297Seric ** Returns: 198297Seric ** none 199297Seric ** 200297Seric ** Side Effects: 201297Seric ** input from xfile 202297Seric ** output to fp 203297Seric ** 204297Seric ** Called By: 205297Seric ** deliver 206297Seric */ 207297Seric 208297Seric 209297Seric errhdr(fp) 210297Seric register FILE *fp; 211297Seric { 2123189Seric char buf[MAXLINE]; 2133189Seric register FILE *xfile; 214297Seric extern int errno; 215297Seric 2163189Seric fflush(stdout); 2173189Seric if ((xfile = fopen(Transcript, "r")) == NULL) 218297Seric syserr("Cannot open %s", Transcript); 219297Seric errno = 0; 220297Seric fprintf(fp, "To: %s\n", To); 221297Seric fprintf(fp, "Subject: Unable to deliver mail\n"); 222297Seric fprintf(fp, "\n ----- Transcript of session follows -----\n"); 2233189Seric while (fgets(xfile, buf, sizeof buf) != NULL) 2243189Seric fputs(buf, fp); 225297Seric fprintf(fp, "\n ----- Unsent message follows -----\n"); 226297Seric fflush(fp); 2273189Seric putmessage(fp, Mailer[1]); 228297Seric close(xfile); 229297Seric if (errno != 0) 230297Seric syserr("errhdr: I/O error"); 231297Seric } 232