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