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