1 # include <pwd.h> 2 # include "sendmail.h" 3 4 SCCSID(@(#)savemail.c 3.31 05/31/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_from, 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 ** sendbody -- if TRUE, also send back the body of the 181 ** message; otherwise just send the header. 182 ** 183 ** Returns: 184 ** zero -- if everything went ok. 185 ** else -- some error. 186 ** 187 ** Side Effects: 188 ** Returns the current message to the sender via 189 ** mail. 190 */ 191 192 static bool SendBody; 193 194 returntosender(msg, returnto, sendbody) 195 char *msg; 196 ADDRESS *returnto; 197 bool sendbody; 198 { 199 ADDRESS to_addr; 200 char buf[MAXNAME]; 201 register int i; 202 extern putheader(), errbody(); 203 register ENVELOPE *ee; 204 extern ENVELOPE *newenvelope(); 205 ENVELOPE errenvelope; 206 207 NoAlias = TRUE; 208 SendBody = sendbody; 209 ee = newenvelope(&errenvelope); 210 ee->e_puthdr = putheader; 211 ee->e_putbody = errbody; 212 addheader("date", "$b", ee); 213 addheader("from", "$g (Mail Delivery Subsystem)", ee); 214 addheader("to", returnto->q_paddr, ee); 215 addheader("subject", msg, ee); 216 217 /* fake up an address header for the from person */ 218 bmove((char *) returnto, (char *) &to_addr, sizeof to_addr); 219 expand("$n", buf, &buf[sizeof buf - 1], CurEnv); 220 if (parse(buf, &ee->e_from, -1) == NULL) 221 { 222 syserr("Can't parse myself!"); 223 ExitStat = EX_SOFTWARE; 224 return (-1); 225 } 226 to_addr.q_next = NULL; 227 to_addr.q_flags &= ~QDONTSEND; 228 ee->e_sendqueue = &to_addr; 229 230 /* push state into submessage */ 231 CurEnv = ee; 232 define('f', "$n"); 233 define('x', "Mail Delivery Subsystem"); 234 235 /* actually deliver the error message */ 236 i = deliver(&to_addr); 237 238 /* if the error message was "queued", make that happen */ 239 if (bitset(QQUEUEUP, to_addr.q_flags)) 240 queueup(ee, FALSE); 241 242 /* restore state */ 243 CurEnv = CurEnv->e_parent; 244 245 if (i != 0) 246 { 247 syserr("Can't return mail to %s", to_addr.q_paddr); 248 return (-1); 249 } 250 return (0); 251 } 252 /* 253 ** ERRBODY -- output the body of an error message. 254 ** 255 ** Typically this is a copy of the transcript plus a copy of the 256 ** original offending message. 257 ** 258 ** Parameters: 259 ** xfile -- the transcript file. 260 ** fp -- the output file. 261 ** xdot -- if set, use the SMTP hidden dot algorithm. 262 ** 263 ** Returns: 264 ** none 265 ** 266 ** Side Effects: 267 ** Outputs the body of an error message. 268 */ 269 270 errbody(fp, m, xdot) 271 register FILE *fp; 272 register struct mailer *m; 273 bool xdot; 274 { 275 register FILE *xfile; 276 char buf[MAXLINE]; 277 278 (void) fflush(stdout); 279 if ((xfile = fopen(Transcript, "r")) == NULL) 280 syserr("Cannot open %s", Transcript); 281 errno = 0; 282 283 /* 284 ** Output transcript of errors 285 */ 286 287 fprintf(fp, " ----- Transcript of session follows -----\n"); 288 (void) fflush(Xscript); 289 while (fgets(buf, sizeof buf, xfile) != NULL) 290 fputs(buf, fp); 291 292 /* 293 ** Output text of original message 294 */ 295 296 if (NoReturn) 297 fprintf(fp, "\n ----- Return message suppressed -----\n\n"); 298 else if (TempFile != NULL) 299 { 300 if (SendBody) 301 { 302 fprintf(fp, "\n ----- Unsent message follows -----\n"); 303 (void) fflush(fp); 304 putheader(fp, m, CurEnv->e_parent); 305 fprintf(fp, "\n"); 306 putbody(fp, m, xdot); 307 } 308 else 309 { 310 fprintf(fp, "\n ----- Message header follows -----\n"); 311 (void) fflush(fp); 312 putheader(fp, m, CurEnv); 313 } 314 } 315 else 316 fprintf(fp, "\n ----- No message was collected -----\n\n"); 317 318 /* 319 ** Cleanup and exit 320 */ 321 322 (void) fclose(xfile); 323 if (errno != 0) 324 syserr("errbody: I/O error"); 325 } 326