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