1 # include <stdio.h> 2 # include <pwd.h> 3 # include "dlvrmail.h" 4 5 /* 6 ** SAVEMAIL -- Save mail on error 7 ** 8 ** If the MailBack flag is set, mail it back to the originator 9 ** together with an error message; otherwise, just put it in 10 ** dead.letter in the user's home directory (if he exists on 11 ** this machine). 12 ** 13 ** Parameters: 14 ** none 15 ** 16 ** Returns: 17 ** none 18 ** 19 ** Side Effects: 20 ** Saves the letter, by writing or mailing it back to the 21 ** sender, or by putting it in dead.letter in her home 22 ** directory. 23 ** 24 ** WARNING: the user id is reset to the original user. 25 ** 26 ** Requires: 27 ** fopen (sys) 28 ** bmove 29 ** parse 30 ** deliver 31 ** strcpy (sys) 32 ** strcat (sys) 33 ** fclose (sys) 34 ** fgets (sys) 35 ** fputs (sys) 36 ** setpwent (sys) 37 ** getuid (sys) 38 ** setuid (sys) 39 ** getgid (sys) 40 ** setgid (sys) 41 ** getpwnam (sys) 42 ** fprintf (sys) 43 ** ttypath 44 ** freopen (sys) 45 ** printf (sys) 46 ** syserr 47 ** rewind (sys) 48 ** time (sys) 49 ** ferror (sys) 50 ** 51 ** Called By: 52 ** finis 53 ** 54 ** History: 55 ** 12/30/79 -- written. 56 */ 57 58 # define MY_NAME "~MAILER~DAEMON~" 59 60 savemail() 61 { 62 register struct passwd *pw; 63 register FILE *xfile; 64 char buf[MAXLINE+1]; 65 extern errhdr(); 66 auto addrq to_addr; 67 extern struct passwd *getpwnam(); 68 register char *p; 69 register int i; 70 auto long tim; 71 extern int errno; 72 extern char *ttypath(); 73 extern char *ctime(); 74 extern addrq *parse(); 75 static int exclusive; 76 77 if (exclusive++) 78 return; 79 80 /* 81 ** In the unhappy event we don't know who to return the mail 82 ** to, make someone up. 83 */ 84 85 if (From.q_paddr == NULL) 86 { 87 if (parse("root", &From, 0) == NULL) 88 { 89 syserr("Cannot parse root!"); 90 ExitStat = EX_SOFTWARE; 91 finis(); 92 } 93 } 94 95 /* 96 ** If called from Eric Schmidt's network, do special mailback. 97 ** Fundamentally, this is the mailback case except that 98 ** it returns an OK exit status (assuming the return 99 ** worked). 100 */ 101 102 if (BerkNet) 103 { 104 ExitStat = EX_OK; 105 MailBack++; 106 } 107 108 /* 109 ** If writing back, do it. 110 ** If the user is still logged in on the same terminal, 111 ** then write the error messages back to hir (sic). 112 ** If not, set the MailBack flag so that it will get 113 ** mailed back instead. 114 */ 115 116 if (WriteBack) 117 { 118 p = ttypath(); 119 if (p == NULL || freopen(p, "w", stdout) == NULL) 120 { 121 MailBack++; 122 errno = 0; 123 } 124 else 125 { 126 xfile = fopen(Transcript, "r"); 127 if (xfile == NULL) 128 syserr("Cannot open %s", Transcript); 129 printf("\r\nMessage from %s\r\n", MY_NAME); 130 printf("Errors occurred while sending mail, transcript follows:\r\n"); 131 while (fgets(buf, sizeof buf, xfile) && !ferror(stdout)) 132 fputs(buf, stdout); 133 if (ferror(stdout)) 134 syserr("savemail: stdout: write err"); 135 fclose(xfile); 136 } 137 } 138 139 /* 140 ** If mailing back, do it. 141 ** Throw away all further output. Don't do aliases, since 142 ** this could cause loops, e.g., if joe mails to x:joe, 143 ** and for some reason the network for x: is down, then 144 ** the response gets sent to x:joe, which gives a 145 ** response, etc. Also force the mail to be delivered 146 ** even if a version of it has already been sent to the 147 ** sender. 148 */ 149 150 if (MailBack || From.q_mailer != &Mailer[0]) 151 { 152 freopen("/dev/null", "w", stdout); 153 NoAlias++; 154 ForceMail++; 155 156 /* fake up an address header for the from person */ 157 bmove((char *) &From, (char *) &to_addr, sizeof to_addr); 158 if (parse(MY_NAME, &From, -1) == NULL) 159 { 160 syserr("Can't parse myself!"); 161 ExitStat = EX_SOFTWARE; 162 finis(); 163 } 164 i = deliver(&to_addr, errhdr); 165 bmove((char *) &to_addr, (char *) &From, sizeof From); 166 if (i != 0) 167 syserr("Can't return mail to %s", p); 168 else 169 return; 170 } 171 172 /* 173 ** Save the message in dead.letter. 174 ** If we weren't mailing back, and the user is local, we 175 ** should save the message in dead.letter so that the 176 ** poor person doesn't have to type it over again -- 177 ** and we all know what poor typists programmers are. 178 */ 179 180 setuid(getuid()); 181 setgid(getgid()); 182 setpwent(); 183 if (From.q_mailer == &Mailer[0] && (pw = getpwnam(From.q_user)) != NULL) 184 { 185 /* user has a home directory */ 186 p = pw->pw_dir; 187 } 188 else 189 { 190 syserr("Can't return mail to %s (pw=%u)", From.q_paddr, pw); 191 # ifdef DEBUG 192 p = "/usr/tmp"; 193 # else 194 p = NULL; 195 # endif 196 } 197 if (p != NULL) 198 { 199 /* we have a home directory; open dead.letter */ 200 strcpy(buf, p); 201 strcat(buf, "/dead.letter"); 202 xfile = fopen(buf, "a"); 203 if (xfile == NULL) 204 printf("Cannot save mail, sorry\n"); 205 else 206 { 207 rewind(stdin); 208 errno = 0; 209 time(&tim); 210 fprintf(xfile, "----- Mail saved at %s", ctime(&tim)); 211 while (fgets(buf, sizeof buf, stdin) && !ferror(xfile)) 212 fputs(buf, xfile); 213 fputs("\n", xfile); 214 if (ferror(xfile)) 215 syserr("savemail: dead.letter: write err"); 216 fclose(xfile); 217 printf("Letter saved in dead.letter\n"); 218 } 219 } 220 else 221 222 /* add terminator to writeback message */ 223 if (WriteBack) 224 printf("-----\r\n"); 225 } 226 /* 227 ** ERRHDR -- Output the header for error mail. 228 ** 229 ** This is the edit filter to error mailbacks. 230 ** 231 ** Algorithm: 232 ** Output fixed header. 233 ** Output the transcript part. 234 ** Output the original message. 235 ** 236 ** Parameters: 237 ** xfile -- the transcript file. 238 ** fp -- the output file. 239 ** 240 ** Returns: 241 ** none 242 ** 243 ** Side Effects: 244 ** input from xfile 245 ** output to fp 246 ** 247 ** Requires: 248 ** read (sys) 249 ** write (sys) 250 ** open (sys) 251 ** close (sys) 252 ** syserr 253 ** rewind (sys) 254 ** fflush (sys) 255 ** fprintf (sys) 256 ** fileno (sys) 257 ** 258 ** Called By: 259 ** deliver 260 ** 261 ** History: 262 ** 12/28/79 -- written. 263 */ 264 265 266 errhdr(fp) 267 register FILE *fp; 268 { 269 char copybuf[512]; 270 register int i; 271 register int xfile; 272 extern int errno; 273 274 if ((xfile = open(Transcript, 0)) < 0) 275 syserr("Cannot open %s", Transcript); 276 fflush(stdout); 277 errno = 0; 278 fprintf(fp, "To: %s\n", To); 279 fprintf(fp, "Subject: Unable to deliver mail\n"); 280 fprintf(fp, "\n ----- Transcript of session follows -----\n"); 281 fflush(fp); 282 while ((i = read(xfile, copybuf, sizeof copybuf)) > 0) 283 write(fileno(fp), copybuf, i); 284 fprintf(fp, "\n ----- Unsent message follows -----\n"); 285 fflush(fp); 286 rewind(stdin); 287 while ((i = read(fileno(stdin), copybuf, sizeof copybuf)) > 0) 288 write(fileno(fp), copybuf, i); 289 close(xfile); 290 if (errno != 0) 291 syserr("errhdr: I/O error"); 292 } 293