1 # include <pwd.h> 2 # include "sendmail.h" 3 4 SCCSID(@(#)savemail.c 3.41 08/22/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 <= PRI_JUNK) 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, 1, (ADDRESS *) NULL, &CurEnv->e_errorqueue); 132 if (returntosender("Unable to deliver mail", CurEnv->e_errorqueue, TRUE) == 0) 133 return; 134 } 135 136 /* 137 ** Save the message in dead.letter. 138 ** If we weren't mailing back, and the user is local, we 139 ** should save the message in dead.letter so that the 140 ** poor person doesn't have to type it over again -- 141 ** and we all know what poor typists programmers are. 142 ** However, if we are running a "smart" protocol, we don't 143 ** bother to return the message, since the other end is 144 ** expected to handle that. 145 */ 146 147 if (ArpaMode) 148 return; 149 p = NULL; 150 if (CurEnv->e_from.q_mailer == LocalMailer) 151 { 152 if (CurEnv->e_from.q_home != NULL) 153 p = CurEnv->e_from.q_home; 154 else if ((pw = getpwnam(CurEnv->e_from.q_user)) != NULL) 155 p = pw->pw_dir; 156 } 157 if (p == NULL) 158 { 159 syserr("Can't return mail to %s", CurEnv->e_from.q_paddr); 160 # ifdef DEBUG 161 p = "/usr/tmp"; 162 # endif 163 } 164 if (p != NULL && TempFile != NULL) 165 { 166 auto ADDRESS *q; 167 bool oldverb = Verbose; 168 169 /* we have a home directory; open dead.letter */ 170 Verbose = TRUE; 171 message(Arpa_Info, "Saving message in dead.letter"); 172 Verbose = oldverb; 173 define('z', p); 174 expand("$z/dead.letter", buf, &buf[sizeof buf - 1], CurEnv); 175 CurEnv->e_to = buf; 176 q = NULL; 177 sendto(buf, -1, (ADDRESS *) NULL, &q); 178 (void) deliver(q); 179 } 180 181 /* add terminator to writeback message */ 182 if (WriteBack) 183 printf("-----\r\n"); 184 } 185 /* 186 ** RETURNTOSENDER -- return a message to the sender with an error. 187 ** 188 ** Parameters: 189 ** msg -- the explanatory message. 190 ** returnto -- the queue of people to send the message to. 191 ** sendbody -- if TRUE, also send back the body of the 192 ** message; otherwise just send the header. 193 ** 194 ** Returns: 195 ** zero -- if everything went ok. 196 ** else -- some error. 197 ** 198 ** Side Effects: 199 ** Returns the current message to the sender via 200 ** mail. 201 */ 202 203 static bool SendBody; 204 205 #define MAXRETURNS 6 /* max depth of returning messages */ 206 207 returntosender(msg, returnto, sendbody) 208 char *msg; 209 ADDRESS *returnto; 210 bool sendbody; 211 { 212 char buf[MAXNAME]; 213 extern putheader(), errbody(); 214 register ENVELOPE *ee; 215 extern ENVELOPE *newenvelope(); 216 ENVELOPE errenvelope; 217 static int returndepth; 218 219 # ifdef DEBUG 220 if (tTd(6, 1)) 221 { 222 printf("Return To Sender: msg=\"%s\", depth=%d, CurEnv=%x,\n", 223 msg, returndepth, CurEnv); 224 printf("\treturnto="); 225 printaddr(returnto, FALSE); 226 } 227 # endif DEBUG 228 229 if (++returndepth >= MAXRETURNS) 230 { 231 if (returndepth != MAXRETURNS) 232 syserr("returntosender: infinite recursion on %s", returnto->q_paddr); 233 /* don't "unrecurse" and fake a clean exit */ 234 /* returndepth--; */ 235 return (0); 236 } 237 238 NoAlias = TRUE; 239 SendBody = sendbody; 240 ee = newenvelope(&errenvelope); 241 ee->e_puthdr = putheader; 242 ee->e_putbody = errbody; 243 addheader("date", "$b", ee); 244 addheader("from", "$g (Mail Delivery Subsystem)", ee); 245 addheader("to", returnto->q_paddr, ee); 246 addheader("subject", msg, ee); 247 248 /* fake up an address header for the from person */ 249 expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 250 if (parse(buf, &ee->e_from, -1) == NULL) 251 { 252 syserr("Can't parse myself!"); 253 ExitStat = EX_SOFTWARE; 254 returndepth--; 255 return (-1); 256 } 257 ee->e_sendqueue = returnto; 258 259 /* push state into submessage */ 260 CurEnv = ee; 261 define('f', "$n"); 262 define('x', "Mail Delivery Subsystem"); 263 264 /* actually deliver the error message */ 265 sendall(ee, FALSE); 266 267 /* do any closing error processing */ 268 checkerrors(ee); 269 270 /* restore state */ 271 dropenvelope(ee); 272 CurEnv = CurEnv->e_parent; 273 returndepth--; 274 275 /* should check for delivery errors here */ 276 return (0); 277 } 278 /* 279 ** ERRBODY -- output the body of an error message. 280 ** 281 ** Typically this is a copy of the transcript plus a copy of the 282 ** original offending message. 283 ** 284 ** Parameters: 285 ** xfile -- the transcript file. 286 ** fp -- the output file. 287 ** xdot -- if set, use the SMTP hidden dot algorithm. 288 ** 289 ** Returns: 290 ** none 291 ** 292 ** Side Effects: 293 ** Outputs the body of an error message. 294 */ 295 296 errbody(fp, m, xdot) 297 register FILE *fp; 298 register struct mailer *m; 299 bool xdot; 300 { 301 register FILE *xfile; 302 char buf[MAXLINE]; 303 bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 304 305 (void) fflush(stdout); 306 if ((xfile = fopen(Transcript, "r")) == NULL) 307 syserr("Cannot open %s", Transcript); 308 errno = 0; 309 310 /* 311 ** Output transcript of errors 312 */ 313 314 fprintf(fp, " ----- Transcript of session follows -----\n"); 315 (void) fflush(Xscript); 316 while (fgets(buf, sizeof buf, xfile) != NULL) 317 putline(buf, fp, fullsmtp); 318 319 /* 320 ** Output text of original message 321 */ 322 323 if (NoReturn) 324 fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 325 else if (TempFile != NULL) 326 { 327 if (SendBody) 328 { 329 fprintf(fp, "\n ----- Unsent message follows -----\n"); 330 (void) fflush(fp); 331 putheader(fp, m, CurEnv->e_parent); 332 fprintf(fp, "\n"); 333 putbody(fp, m, xdot); 334 } 335 else 336 { 337 fprintf(fp, "\n ----- Message header follows -----\n"); 338 (void) fflush(fp); 339 putheader(fp, m, CurEnv); 340 } 341 } 342 else 343 fprintf(fp, "\n ----- No message was collected -----\n\n"); 344 345 /* 346 ** Cleanup and exit 347 */ 348 349 (void) fclose(xfile); 350 if (errno != 0) 351 syserr("errbody: I/O error"); 352 } 353