1 # include <pwd.h> 2 # include "sendmail.h" 3 4 SCCSID(@(#)savemail.c 3.34 06/07/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 extern ENVELOPE *newenvelope(); 37 38 if (exclusive++) 39 return; 40 if (CurEnv->e_class <= PRI_JUNK) 41 { 42 message(Arpa_Info, "Dumping junk mail"); 43 return; 44 } 45 ForceMail = TRUE; 46 47 /* 48 ** In the unhappy event we don't know who to return the mail 49 ** to, make someone up. 50 */ 51 52 if (CurEnv->e_from.q_paddr == NULL) 53 { 54 if (parse("root", &CurEnv->e_from, 0) == NULL) 55 { 56 syserr("Cannot parse root!"); 57 ExitStat = EX_SOFTWARE; 58 finis(); 59 } 60 } 61 CurEnv->e_to = NULL; 62 63 /* 64 ** If called from Eric Schmidt's network, do special mailback. 65 ** Fundamentally, this is the mailback case except that 66 ** it returns an OK exit status (assuming the return 67 ** worked). 68 ** Also, if the from address is not local, mail it back. 69 */ 70 71 if (BerkNet) 72 { 73 ExitStat = EX_OK; 74 MailBack = TRUE; 75 } 76 if (!bitset(M_LOCAL, CurEnv->e_from.q_mailer->m_flags)) 77 MailBack = TRUE; 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 = TRUE; 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_errorqueue, 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 bool oldverb = Verbose; 160 161 /* we have a home directory; open dead.letter */ 162 Verbose = TRUE; 163 message(Arpa_Info, "Saving message in dead.letter"); 164 Verbose = oldverb; 165 define('z', p); 166 expand("$z/dead.letter", buf, &buf[sizeof buf - 1], CurEnv); 167 CurEnv->e_to = buf; 168 q = NULL; 169 sendto(buf, -1, (ADDRESS *) NULL, &q); 170 (void) deliver(q); 171 } 172 173 /* add terminator to writeback message */ 174 if (WriteBack) 175 printf("-----\r\n"); 176 } 177 /* 178 ** RETURNTOSENDER -- return a message to the sender with an error. 179 ** 180 ** Parameters: 181 ** msg -- the explanatory message. 182 ** returnto -- the queue of people to send the message to. 183 ** sendbody -- if TRUE, also send back the body of the 184 ** message; otherwise just send the header. 185 ** 186 ** Returns: 187 ** zero -- if everything went ok. 188 ** else -- some error. 189 ** 190 ** Side Effects: 191 ** Returns the current message to the sender via 192 ** mail. 193 */ 194 195 static bool SendBody; 196 197 #define MAXRETURNS 6 /* max depth of returning messages */ 198 199 returntosender(msg, returnto, sendbody) 200 char *msg; 201 ADDRESS *returnto; 202 bool sendbody; 203 { 204 char buf[MAXNAME]; 205 register int i; 206 extern putheader(), errbody(); 207 register ENVELOPE *ee; 208 extern ENVELOPE *newenvelope(); 209 ENVELOPE errenvelope; 210 static int returndepth; 211 212 if (++returndepth >= MAXRETURNS) 213 { 214 if (returndepth != MAXRETURNS) 215 syserr("returntosender: infinite recursion on %s", returnto->q_paddr); 216 /* don't "unrecurse" and fake a clean exit */ 217 /* returndepth--; */ 218 return (0); 219 } 220 221 NoAlias = TRUE; 222 SendBody = sendbody; 223 ee = newenvelope(&errenvelope); 224 ee->e_puthdr = putheader; 225 ee->e_putbody = errbody; 226 addheader("date", "$b", ee); 227 addheader("from", "$g (Mail Delivery Subsystem)", ee); 228 addheader("to", returnto->q_paddr, ee); 229 addheader("subject", msg, ee); 230 231 /* fake up an address header for the from person */ 232 expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 233 if (parse(buf, &ee->e_from, -1) == NULL) 234 { 235 syserr("Can't parse myself!"); 236 ExitStat = EX_SOFTWARE; 237 returndepth--; 238 return (-1); 239 } 240 ee->e_sendqueue = returnto; 241 242 /* push state into submessage */ 243 CurEnv = ee; 244 define('f', "$n"); 245 define('x', "Mail Delivery Subsystem"); 246 247 /* actually deliver the error message */ 248 sendall(ee, FALSE); 249 250 /* do any closing error processing */ 251 checkerrors(ee); 252 253 /* restore state */ 254 CurEnv = CurEnv->e_parent; 255 returndepth--; 256 257 /* should check for delivery errors here */ 258 return (0); 259 } 260 /* 261 ** ERRBODY -- output the body of an error message. 262 ** 263 ** Typically this is a copy of the transcript plus a copy of the 264 ** original offending message. 265 ** 266 ** Parameters: 267 ** xfile -- the transcript file. 268 ** fp -- the output file. 269 ** xdot -- if set, use the SMTP hidden dot algorithm. 270 ** 271 ** Returns: 272 ** none 273 ** 274 ** Side Effects: 275 ** Outputs the body of an error message. 276 */ 277 278 errbody(fp, m, xdot) 279 register FILE *fp; 280 register struct mailer *m; 281 bool xdot; 282 { 283 register FILE *xfile; 284 char buf[MAXLINE]; 285 bool fullsmtp = bitset(M_FULLSMTP, m->m_flags); 286 287 (void) fflush(stdout); 288 if ((xfile = fopen(Transcript, "r")) == NULL) 289 syserr("Cannot open %s", Transcript); 290 errno = 0; 291 292 /* 293 ** Output transcript of errors 294 */ 295 296 fprintf(fp, " ----- Transcript of session follows -----\n"); 297 (void) fflush(Xscript); 298 while (fgets(buf, sizeof buf, xfile) != NULL) 299 putline(buf, fp, fullsmtp); 300 301 /* 302 ** Output text of original message 303 */ 304 305 if (NoReturn) 306 fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 307 else if (TempFile != NULL) 308 { 309 if (SendBody) 310 { 311 fprintf(fp, "\n ----- Unsent message follows -----\n"); 312 (void) fflush(fp); 313 putheader(fp, m, CurEnv->e_parent); 314 fprintf(fp, "\n"); 315 putbody(fp, m, xdot); 316 } 317 else 318 { 319 fprintf(fp, "\n ----- Message header follows -----\n"); 320 (void) fflush(fp); 321 putheader(fp, m, CurEnv); 322 } 323 } 324 else 325 fprintf(fp, "\n ----- No message was collected -----\n\n"); 326 327 /* 328 ** Cleanup and exit 329 */ 330 331 (void) fclose(xfile); 332 if (errno != 0) 333 syserr("errbody: I/O error"); 334 } 335