1297Seric # include <stdio.h> 2297Seric # include <pwd.h> 33313Seric # include "sendmail.h" 4297Seric 5*4167Seric static char SccsId[] = "@(#)savemail.c 3.12 08/18/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 extern char *ttypath(); 40297Seric static int exclusive; 41297Seric 42297Seric if (exclusive++) 43297Seric return; 44297Seric 45297Seric /* 46297Seric ** In the unhappy event we don't know who to return the mail 47297Seric ** to, make someone up. 48297Seric */ 49297Seric 50297Seric if (From.q_paddr == NULL) 51297Seric { 52297Seric if (parse("root", &From, 0) == NULL) 53297Seric { 54297Seric syserr("Cannot parse root!"); 55297Seric ExitStat = EX_SOFTWARE; 56297Seric finis(); 57297Seric } 58297Seric } 594079Seric To = NULL; 60297Seric 61297Seric /* 62401Seric ** If called from Eric Schmidt's network, do special mailback. 63401Seric ** Fundamentally, this is the mailback case except that 64401Seric ** it returns an OK exit status (assuming the return 65401Seric ** worked). 66297Seric */ 67297Seric 68401Seric if (BerkNet) 69297Seric { 70401Seric ExitStat = EX_OK; 71297Seric MailBack++; 72297Seric } 73297Seric 74297Seric /* 75297Seric ** If writing back, do it. 76297Seric ** If the user is still logged in on the same terminal, 77297Seric ** then write the error messages back to hir (sic). 78297Seric ** If not, set the MailBack flag so that it will get 79297Seric ** mailed back instead. 80297Seric */ 81297Seric 82297Seric if (WriteBack) 83297Seric { 84297Seric p = ttypath(); 85297Seric if (p == NULL || freopen(p, "w", stdout) == NULL) 86297Seric { 87297Seric MailBack++; 88297Seric errno = 0; 89297Seric } 90297Seric else 91297Seric { 92401Seric xfile = fopen(Transcript, "r"); 93401Seric if (xfile == NULL) 94401Seric syserr("Cannot open %s", Transcript); 954086Seric (void) expand("$n", buf, &buf[sizeof buf - 1]); 964162Seric printf("\r\nMessage from %s...\r\n", buf); 97297Seric printf("Errors occurred while sending mail, transcript follows:\r\n"); 984086Seric while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout)) 99297Seric fputs(buf, stdout); 100297Seric if (ferror(stdout)) 1014086Seric (void) syserr("savemail: stdout: write err"); 1024086Seric (void) fclose(xfile); 103297Seric } 104297Seric } 105297Seric 106297Seric /* 107297Seric ** If mailing back, do it. 108297Seric ** Throw away all further output. Don't do aliases, since 109297Seric ** this could cause loops, e.g., if joe mails to x:joe, 110297Seric ** and for some reason the network for x: is down, then 111297Seric ** the response gets sent to x:joe, which gives a 112297Seric ** response, etc. Also force the mail to be delivered 113297Seric ** even if a version of it has already been sent to the 114297Seric ** sender. 115297Seric */ 116297Seric 1173048Seric if (MailBack) 118297Seric { 1194086Seric (void) freopen("/dev/null", "w", stdout); 120297Seric NoAlias++; 121297Seric ForceMail++; 122297Seric 123297Seric /* fake up an address header for the from person */ 124297Seric bmove((char *) &From, (char *) &to_addr, sizeof to_addr); 1254086Seric (void) expand("$n", buf, &buf[sizeof buf - 1]); 1264086Seric if (parse(buf, &From, -1) == NULL) 127297Seric { 128297Seric syserr("Can't parse myself!"); 129297Seric ExitStat = EX_SOFTWARE; 130297Seric finis(); 131297Seric } 132297Seric i = deliver(&to_addr, errhdr); 133297Seric bmove((char *) &to_addr, (char *) &From, sizeof From); 134297Seric if (i != 0) 135297Seric syserr("Can't return mail to %s", p); 136297Seric else 137297Seric return; 138297Seric } 139297Seric 140297Seric /* 141297Seric ** Save the message in dead.letter. 142297Seric ** If we weren't mailing back, and the user is local, we 143297Seric ** should save the message in dead.letter so that the 144297Seric ** poor person doesn't have to type it over again -- 145297Seric ** and we all know what poor typists programmers are. 146297Seric */ 147297Seric 1484162Seric if (ArpaMode != ARPA_NONE) 1494162Seric return; 1504079Seric p = NULL; 1514079Seric if (From.q_mailer == M_LOCAL) 152297Seric { 1534079Seric if (From.q_home != NULL) 1544079Seric p = From.q_home; 1554079Seric else if ((pw = getpwnam(From.q_user)) != NULL) 1564079Seric p = pw->pw_dir; 157297Seric } 1584079Seric if (p == NULL) 159297Seric { 1604079Seric syserr("Can't return mail to %s", From.q_paddr); 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 */ 170*4167Seric message(Arpa_Info, "Saving message in dead.letter"); 1714079Seric define('z', p); 1724086Seric (void) expand("$z/dead.letter", buf, &buf[sizeof buf - 1]); 1734066Seric To = buf; 1744066Seric i = mailfile(buf); 1754079Seric giveresponse(i, TRUE, Mailer[M_LOCAL]); 176297Seric } 177297Seric 178297Seric /* add terminator to writeback message */ 179297Seric if (WriteBack) 180297Seric printf("-----\r\n"); 181297Seric } 182297Seric /* 183297Seric ** ERRHDR -- Output the header for error mail. 184297Seric ** 185297Seric ** This is the edit filter to error mailbacks. 186297Seric ** 187297Seric ** Algorithm: 188297Seric ** Output fixed header. 189297Seric ** Output the transcript part. 190297Seric ** Output the original message. 191297Seric ** 192297Seric ** Parameters: 193297Seric ** xfile -- the transcript file. 194297Seric ** fp -- the output file. 195297Seric ** 196297Seric ** Returns: 197297Seric ** none 198297Seric ** 199297Seric ** Side Effects: 200297Seric ** input from xfile 201297Seric ** output to fp 202297Seric ** 203297Seric ** Called By: 204297Seric ** deliver 205297Seric */ 206297Seric 207297Seric 208297Seric errhdr(fp) 209297Seric register FILE *fp; 210297Seric { 2113189Seric char buf[MAXLINE]; 2123189Seric register FILE *xfile; 213297Seric 2144086Seric (void) fflush(stdout); 2153189Seric if ((xfile = fopen(Transcript, "r")) == NULL) 216297Seric syserr("Cannot open %s", Transcript); 217297Seric errno = 0; 218297Seric fprintf(fp, "To: %s\n", To); 219297Seric fprintf(fp, "Subject: Unable to deliver mail\n"); 220297Seric fprintf(fp, "\n ----- Transcript of session follows -----\n"); 2214086Seric while (fgets(buf, sizeof buf, xfile) != NULL) 2223189Seric fputs(buf, fp); 223297Seric fprintf(fp, "\n ----- Unsent message follows -----\n"); 2244086Seric (void) fflush(fp); 2253189Seric putmessage(fp, Mailer[1]); 2264086Seric (void) fclose(xfile); 227297Seric if (errno != 0) 228297Seric syserr("errhdr: I/O error"); 229297Seric } 230