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