1 # include <pwd.h> 2 # include "sendmail.h" 3 4 SCCSID(@(#)savemail.c 3.29.1.1 05/29/82); 5 6 /* 7 ** SAVEMAIL -- Save mail on error 8 ** 9 ** If the MailBack flag is set, mail it back to the originator 10 ** together with an error message; otherwise, just put it in 11 ** dead.letter in the user's home directory (if he exists on 12 ** this machine). 13 ** 14 ** Parameters: 15 ** none 16 ** 17 ** Returns: 18 ** none 19 ** 20 ** Side Effects: 21 ** Saves the letter, by writing or mailing it back to the 22 ** sender, or by putting it in dead.letter in her home 23 ** directory. 24 */ 25 26 savemail() 27 { 28 register struct passwd *pw; 29 register FILE *xfile; 30 char buf[MAXLINE+1]; 31 extern struct passwd *getpwnam(); 32 register char *p; 33 extern char *ttypath(); 34 static int exclusive; 35 typedef int (*fnptr)(); 36 ENVELOPE errenvelope; 37 register ENVELOPE *ee; 38 extern ENVELOPE *newenvelope(); 39 40 if (exclusive++) 41 return; 42 if (CurEnv->e_class <= PRI_JUNK) 43 { 44 if (Verbose) 45 message(Arpa_Info, "Dumping junk mail"); 46 return; 47 } 48 ForceMail = TRUE; 49 50 /* 51 ** In the unhappy event we don't know who to return the mail 52 ** to, make someone up. 53 */ 54 55 if (CurEnv->e_from.q_paddr == NULL) 56 { 57 if (parse("root", &CurEnv->e_from, 0) == NULL) 58 { 59 syserr("Cannot parse root!"); 60 ExitStat = EX_SOFTWARE; 61 finis(); 62 } 63 } 64 CurEnv->e_to = NULL; 65 66 /* 67 ** If called from Eric Schmidt's network, do special mailback. 68 ** Fundamentally, this is the mailback case except that 69 ** it returns an OK exit status (assuming the return 70 ** worked). 71 */ 72 73 if (BerkNet) 74 { 75 ExitStat = EX_OK; 76 MailBack++; 77 } 78 79 /* 80 ** If writing back, do it. 81 ** If the user is still logged in on the same terminal, 82 ** then write the error messages back to hir (sic). 83 ** If not, set the MailBack flag so that it will get 84 ** mailed back instead. 85 */ 86 87 if (WriteBack) 88 { 89 p = ttypath(); 90 if (p == NULL || freopen(p, "w", stdout) == NULL) 91 { 92 MailBack++; 93 errno = 0; 94 } 95 else 96 { 97 (void) fflush(Xscript); 98 xfile = fopen(Transcript, "r"); 99 if (xfile == NULL) 100 syserr("Cannot open %s", Transcript); 101 expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 102 printf("\r\nMessage from %s...\r\n", buf); 103 printf("Errors occurred while sending mail; transcript follows:\r\n"); 104 while (fgets(buf, sizeof buf, xfile) != NULL && !ferror(stdout)) 105 fputs(buf, stdout); 106 if (ferror(stdout)) 107 (void) syserr("savemail: stdout: write err"); 108 (void) fclose(xfile); 109 } 110 } 111 112 /* 113 ** If mailing back, do it. 114 ** Throw away all further output. Don't do aliases, since 115 ** this could cause loops, e.g., if joe mails to x:joe, 116 ** and for some reason the network for x: is down, then 117 ** the response gets sent to x:joe, which gives a 118 ** response, etc. Also force the mail to be delivered 119 ** even if a version of it has already been sent to the 120 ** sender. 121 */ 122 123 if (MailBack) 124 { 125 if (returntosender("Unable to deliver mail", &CurEnv->e_from, TRUE) == 0) 126 return; 127 } 128 129 /* 130 ** Save the message in dead.letter. 131 ** If we weren't mailing back, and the user is local, we 132 ** should save the message in dead.letter so that the 133 ** poor person doesn't have to type it over again -- 134 ** and we all know what poor typists programmers are. 135 */ 136 137 if (ArpaMode) 138 return; 139 p = NULL; 140 if (CurEnv->e_from.q_mailer == LocalMailer) 141 { 142 if (CurEnv->e_from.q_home != NULL) 143 p = CurEnv->e_from.q_home; 144 else if ((pw = getpwnam(CurEnv->e_from.q_user)) != NULL) 145 p = pw->pw_dir; 146 } 147 if (p == NULL) 148 { 149 syserr("Can't return mail to %s", CurEnv->e_from.q_paddr); 150 # ifdef DEBUG 151 p = "/usr/tmp"; 152 # else 153 p = NULL; 154 # endif 155 } 156 if (p != NULL && TempFile != NULL) 157 { 158 auto ADDRESS *q; 159 160 /* we have a home directory; open dead.letter */ 161 message(Arpa_Info, "Saving message in dead.letter"); 162 define('z', p); 163 expand("$z/dead.letter", buf, &buf[sizeof buf - 1], CurEnv); 164 CurEnv->e_to = buf; 165 q = NULL; 166 sendto(buf, -1, (ADDRESS *) NULL, &q); 167 (void) deliver(q); 168 } 169 170 /* add terminator to writeback message */ 171 if (WriteBack) 172 printf("-----\r\n"); 173 } 174 /* 175 ** RETURNTOSENDER -- return a message to the sender with an error. 176 ** 177 ** Parameters: 178 ** msg -- the explanatory message. 179 ** sendbody -- if TRUE, also send back the body of the 180 ** message; otherwise just send the header. 181 ** 182 ** Returns: 183 ** zero -- if everything went ok. 184 ** else -- some error. 185 ** 186 ** Side Effects: 187 ** Returns the current message to the sender via 188 ** mail. 189 */ 190 191 static char *ErrorMessage; 192 static bool SendBody; 193 194 returntosender(msg, returnto, sendbody) 195 char *msg; 196 ADDRESS *returnto; 197 bool sendbody; 198 { 199 ADDRESS to_addr; 200 char buf[MAXNAME]; 201 register int i; 202 extern putheader(), errbody(); 203 register ENVELOPE *ee; 204 extern ENVELOPE *newenvelope(); 205 ENVELOPE errenvelope; 206 207 NoAlias++; 208 SendBody = sendbody; 209 ee = newenvelope(&errenvelope); 210 ee->e_puthdr = putheader; 211 ee->e_putbody = errbody; 212 addheader("date", "$b", ee); 213 addheader("from", "$g (Mail Delivery Subsystem)", ee); 214 addheader("to", returnto->q_paddr, ee); 215 addheader("subject", msg, ee); 216 217 /* fake up an address header for the from person */ 218 bmove((char *) returnto, (char *) &to_addr, sizeof to_addr); 219 expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 220 if (parse(buf, &ee->e_from, -1) == NULL) 221 { 222 syserr("Can't parse myself!"); 223 ExitStat = EX_SOFTWARE; 224 return (-1); 225 } 226 to_addr.q_next = NULL; 227 to_addr.q_flags &= ~QDONTSEND; 228 ee->e_sendqueue = &to_addr; 229 230 /* push state into submessage */ 231 CurEnv = ee; 232 define('f', "$n"); 233 define('x', "Mail Delivery Subsystem"); 234 235 /* actually deliver the error message */ 236 i = deliver(&to_addr); 237 238 /* if the error message was "queued", make that happen */ 239 if (bitset(QQUEUEUP, to_addr.q_flags)) 240 queueup(ee); 241 242 /* restore state */ 243 CurEnv = CurEnv->e_parent; 244 245 if (i != 0) 246 { 247 syserr("Can't return mail to %s", to_addr.q_paddr); 248 return (-1); 249 } 250 return (0); 251 } 252 /* 253 ** ERRHEADER -- Output the header for error mail. 254 ** 255 ** Parameters: 256 ** xfile -- the transcript file. 257 ** fp -- the output file. 258 ** 259 ** Returns: 260 ** none 261 ** 262 ** Side Effects: 263 ** Outputs the header for an error message. 264 */ 265 266 errheader(fp, m) 267 register FILE *fp; 268 register struct mailer *m; 269 { 270 /* 271 ** Output header of error message. 272 */ 273 274 putheader(fp, m); 275 } 276 /* 277 ** ERRBODY -- output the body of an error message. 278 ** 279 ** Typically this is a copy of the transcript plus a copy of the 280 ** original offending message. 281 ** 282 ** Parameters: 283 ** xfile -- the transcript file. 284 ** fp -- the output file. 285 ** xdot -- if set, use the SMTP hidden dot algorithm. 286 ** 287 ** Returns: 288 ** none 289 ** 290 ** Side Effects: 291 ** Outputs the body of an error message. 292 */ 293 294 errbody(fp, m, xdot) 295 register FILE *fp; 296 register struct mailer *m; 297 bool xdot; 298 { 299 register FILE *xfile; 300 char buf[MAXLINE]; 301 302 (void) fflush(stdout); 303 if ((xfile = fopen(Transcript, "r")) == NULL) 304 syserr("Cannot open %s", Transcript); 305 errno = 0; 306 307 /* 308 ** Output transcript of errors 309 */ 310 311 fprintf(fp, " ----- Transcript of session follows -----\n"); 312 (void) fflush(Xscript); 313 while (fgets(buf, sizeof buf, xfile) != NULL) 314 fputs(buf, fp); 315 316 /* 317 ** Output text of original message 318 */ 319 320 if (NoReturn) 321 fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 322 else if (TempFile != NULL) 323 { 324 if (SendBody) 325 { 326 fprintf(fp, "\n ----- Unsent message follows -----\n"); 327 (void) fflush(fp); 328 putheader(fp, Mailer[1], CurEnv->e_parent); 329 fprintf(fp, "\n"); 330 putbody(fp, Mailer[1], xdot); 331 } 332 else 333 { 334 fprintf(fp, "\n ----- Message header follows -----\n"); 335 (void) fflush(fp); 336 putheader(fp, Mailer[1]); 337 } 338 } 339 else 340 fprintf(fp, "\n ----- No message was collected -----\n\n"); 341 342 /* 343 ** Cleanup and exit 344 */ 345 346 (void) fclose(xfile); 347 if (errno != 0) 348 syserr("errbody: I/O error"); 349 } 350