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