1 # include <pwd.h> 2 # include "sendmail.h" 3 4 SCCSID(@(#)savemail.c 3.44 09/24/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 addheader("date", "$b", ee); 247 addheader("from", "$g (Mail Delivery Subsystem)", ee); 248 addheader("to", returnto->q_paddr, ee); 249 addheader("subject", msg, ee); 250 251 /* fake up an address header for the from person */ 252 expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 253 if (parse(buf, &ee->e_from, -1) == NULL) 254 { 255 syserr("Can't parse myself!"); 256 ExitStat = EX_SOFTWARE; 257 returndepth--; 258 return (-1); 259 } 260 ee->e_sendqueue = returnto; 261 262 /* push state into submessage */ 263 CurEnv = ee; 264 define('f', "$n"); 265 define('x', "Mail Delivery Subsystem"); 266 267 /* actually deliver the error message */ 268 sendall(ee, FALSE); 269 270 /* do any closing error processing */ 271 checkerrors(ee); 272 273 /* restore state */ 274 dropenvelope(ee); 275 CurEnv = CurEnv->e_parent; 276 returndepth--; 277 278 /* should check for delivery errors here */ 279 return (0); 280 } 281 /* 282 ** ERRBODY -- output the body of an error message. 283 ** 284 ** Typically this is a copy of the transcript plus a copy of the 285 ** original offending message. 286 ** 287 ** Parameters: 288 ** xfile -- the transcript file. 289 ** fp -- the output file. 290 ** xdot -- if set, use the SMTP hidden dot algorithm. 291 ** 292 ** Returns: 293 ** none 294 ** 295 ** Side Effects: 296 ** Outputs the body of an error message. 297 */ 298 299 errbody(fp, m, xdot) 300 register FILE *fp; 301 register struct mailer *m; 302 bool xdot; 303 { 304 register FILE *xfile; 305 char buf[MAXLINE]; 306 bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 307 308 (void) fflush(stdout); 309 if ((xfile = fopen(Transcript, "r")) == NULL) 310 syserr("Cannot open %s", Transcript); 311 errno = 0; 312 313 /* 314 ** Output transcript of errors 315 */ 316 317 fprintf(fp, " ----- Transcript of session follows -----\n"); 318 (void) fflush(Xscript); 319 while (fgets(buf, sizeof buf, xfile) != NULL) 320 putline(buf, fp, fullsmtp); 321 322 /* 323 ** Output text of original message 324 */ 325 326 if (NoReturn) 327 fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 328 else if (TempFile != NULL) 329 { 330 if (SendBody) 331 { 332 fprintf(fp, "\n ----- Unsent message follows -----\n"); 333 (void) fflush(fp); 334 putheader(fp, m, CurEnv->e_parent); 335 fprintf(fp, "\n"); 336 putbody(fp, m, xdot); 337 } 338 else 339 { 340 fprintf(fp, "\n ----- Message header follows -----\n"); 341 (void) fflush(fp); 342 putheader(fp, m, CurEnv); 343 } 344 } 345 else 346 fprintf(fp, "\n ----- No message was collected -----\n\n"); 347 348 /* 349 ** Cleanup and exit 350 */ 351 352 (void) fclose(xfile); 353 if (errno != 0) 354 syserr("errbody: I/O error"); 355 } 356