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