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