1 /*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char copyright[] = 10 "@(#) Copyright (c) 1990, 1993\n\ 11 The Regents of the University of California. All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)mail.local.c 8.3 (Berkeley) 01/07/94"; 16 #endif /* not lint */ 17 18 #include <sys/param.h> 19 #include <sys/stat.h> 20 #include <sys/socket.h> 21 22 #include <netinet/in.h> 23 24 #include <errno.h> 25 #include <fcntl.h> 26 #include <netdb.h> 27 #include <pwd.h> 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include <string.h> 31 #include <sysexits.h> 32 #include <syslog.h> 33 #include <time.h> 34 #include <unistd.h> 35 36 #if __STDC__ 37 #include <stdarg.h> 38 #else 39 #include <varargs.h> 40 #endif 41 42 #include "pathnames.h" 43 44 int eval = EX_OK; /* sysexits.h error value. */ 45 46 void deliver __P((int, char *)); 47 void e_to_sys __P((int)); 48 __dead void err __P((const char *, ...)); 49 void notifybiff __P((char *)); 50 int store __P((char *)); 51 void usage __P((void)); 52 void vwarn __P((const char *, _BSD_VA_LIST_)); 53 void warn __P((const char *, ...)); 54 55 int 56 main(argc, argv) 57 int argc; 58 char *argv[]; 59 { 60 struct passwd *pw; 61 int ch, fd; 62 uid_t uid; 63 char *from; 64 65 openlog("mail.local", 0, LOG_MAIL); 66 67 from = NULL; 68 while ((ch = getopt(argc, argv, "df:r:")) != EOF) 69 switch(ch) { 70 case 'd': /* Backward compatible. */ 71 break; 72 case 'f': 73 case 'r': /* Backward compatible. */ 74 if (from != NULL) { 75 warn("multiple -f options"); 76 usage(); 77 } 78 from = optarg; 79 break; 80 case '?': 81 default: 82 usage(); 83 } 84 argc -= optind; 85 argv += optind; 86 87 if (!*argv) 88 usage(); 89 90 /* 91 * If from not specified, use the name from getlogin() if the 92 * uid matches, otherwise, use the name from the password file 93 * corresponding to the uid. 94 */ 95 uid = getuid(); 96 if (!from && (!(from = getlogin()) || 97 !(pw = getpwnam(from)) || pw->pw_uid != uid)) 98 from = (pw = getpwuid(uid)) ? pw->pw_name : "???"; 99 100 /* 101 * There is no way to distinguish the error status of one delivery 102 * from the rest of the deliveries. So, if we failed hard on one 103 * or more deliveries, but had no failures on any of the others, we 104 * return a hard failure. If we failed temporarily on one or more 105 * deliveries, we return a temporary failure regardless of the other 106 * failures. This results in the delivery being reattempted later 107 * at the expense of repeated failures and multiple deliveries. 108 */ 109 for (fd = store(from); *argv; ++argv) 110 deliver(fd, *argv); 111 exit(eval); 112 } 113 114 int 115 store(from) 116 char *from; 117 { 118 FILE *fp; 119 time_t tval; 120 int fd, eline; 121 char *tn, line[2048]; 122 123 tn = strdup(_PATH_LOCTMP); 124 if ((fd = mkstemp(tn)) == -1 || (fp = fdopen(fd, "w+")) == NULL) { 125 e_to_sys(errno); 126 err("unable to open temporary file"); 127 } 128 (void)unlink(tn); 129 free(tn); 130 131 (void)time(&tval); 132 (void)fprintf(fp, "From %s %s", from, ctime(&tval)); 133 134 line[0] = '\0'; 135 for (eline = 1; fgets(line, sizeof(line), stdin);) { 136 if (line[0] == '\n') 137 eline = 1; 138 else { 139 if (eline && line[0] == 'F' && 140 !memcmp(line, "From ", 5)) 141 (void)putc('>', fp); 142 eline = 0; 143 } 144 (void)fprintf(fp, "%s", line); 145 if (ferror(fp)) { 146 e_to_sys(errno); 147 err("temporary file write error"); 148 } 149 } 150 151 /* If message not newline terminated, need an extra. */ 152 if (!strchr(line, '\n')) 153 (void)putc('\n', fp); 154 /* Output a newline; note, empty messages are allowed. */ 155 (void)putc('\n', fp); 156 157 if (fflush(fp) == EOF || ferror(fp)) { 158 e_to_sys(errno); 159 err("temporary file write error"); 160 } 161 return (fd); 162 } 163 164 void 165 deliver(fd, name) 166 int fd; 167 char *name; 168 { 169 struct stat sb; 170 struct passwd *pw; 171 int mbfd, nr, nw, off; 172 char biffmsg[100], buf[8*1024], path[MAXPATHLEN]; 173 off_t curoff; 174 175 /* 176 * Disallow delivery to unknown names -- special mailboxes can be 177 * handled in the sendmail aliases file. 178 */ 179 if (!(pw = getpwnam(name))) { 180 if (eval != EX_TEMPFAIL) 181 eval = EX_UNAVAILABLE; 182 warn("unknown name: %s", name); 183 return; 184 } 185 186 (void)snprintf(path, sizeof(path), "%s/%s", _PATH_MAILDIR, name); 187 188 /* 189 * If the mailbox is a linked or a symlink, fail. 190 * 191 * If we created the mailbox, set the owner/group. If that fails, 192 * just return. Another process may have already opened it, so we 193 * can't unlink it. Historically, binmail set the owner/group at 194 * each mail delivery. We no longer do this, assuming that if the 195 * ownership or permissions were changed there was a reason. 196 * 197 * XXX 198 * open(2) should support flock'ing the file. 199 */ 200 if (lstat(path, &sb)) { 201 if ((mbfd = open(path, 202 O_APPEND|O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) 203 mbfd = open(path, O_APPEND|O_WRONLY, 0); 204 else if (fchown(mbfd, pw->pw_uid, pw->pw_gid)) { 205 e_to_sys(errno); 206 warn("chown %u.%u: %s", pw->pw_uid, pw->pw_gid, name); 207 return; 208 } 209 } else if (sb.st_nlink != 1 || S_ISLNK(sb.st_mode)) { 210 e_to_sys(errno); 211 warn("%s: linked file", path); 212 return; 213 } else 214 mbfd = open(path, O_APPEND|O_WRONLY, 0); 215 216 if (mbfd == -1) { 217 e_to_sys(errno); 218 warn("%s: %s", path, strerror(errno)); 219 return; 220 } 221 222 /* Wait until we can get a lock on the file. */ 223 if (flock(mbfd, LOCK_EX)) { 224 e_to_sys(errno); 225 warn("%s: %s", path, strerror(errno)); 226 goto err1; 227 } 228 229 /* Get the starting offset of the new message for biff. */ 230 curoff = lseek(mbfd, (off_t)0, SEEK_END); 231 (void)snprintf(biffmsg, sizeof(biffmsg), "%s@%qd\n", name, curoff); 232 233 /* Copy the message into the file. */ 234 if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) { 235 e_to_sys(errno); 236 warn("temporary file: %s", strerror(errno)); 237 goto err1; 238 } 239 while ((nr = read(fd, buf, sizeof(buf))) > 0) 240 for (off = 0; off < nr; nr -= nw, off += nw) 241 if ((nw = write(mbfd, buf + off, nr)) < 0) { 242 e_to_sys(errno); 243 warn("%s: %s", path, strerror(errno)); 244 goto err2;; 245 } 246 if (nr < 0) { 247 e_to_sys(errno); 248 warn("temporary file: %s", strerror(errno)); 249 goto err2;; 250 } 251 252 /* Flush to disk, don't wait for update. */ 253 if (fsync(mbfd)) { 254 e_to_sys(errno); 255 warn("%s: %s", path, strerror(errno)); 256 err2: (void)ftruncate(mbfd, curoff); 257 err1: (void)close(mbfd); 258 return; 259 } 260 261 /* Close and check -- NFS doesn't write until the close. */ 262 if (close(mbfd)) { 263 e_to_sys(errno); 264 warn("%s: %s", path, strerror(errno)); 265 return; 266 } 267 268 notifybiff(biffmsg); 269 } 270 271 void 272 notifybiff(msg) 273 char *msg; 274 { 275 static struct sockaddr_in addr; 276 static int f = -1; 277 struct hostent *hp; 278 struct servent *sp; 279 int len; 280 281 if (!addr.sin_family) { 282 /* Be silent if biff service not available. */ 283 if (!(sp = getservbyname("biff", "udp"))) 284 return; 285 if (!(hp = gethostbyname("localhost"))) { 286 warn("localhost: %s", strerror(errno)); 287 return; 288 } 289 addr.sin_family = hp->h_addrtype; 290 memmove(&addr.sin_addr, hp->h_addr, hp->h_length); 291 addr.sin_port = sp->s_port; 292 } 293 if (f < 0 && (f = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 294 warn("socket: %s", strerror(errno)); 295 return; 296 } 297 len = strlen(msg) + 1; 298 if (sendto(f, msg, len, 0, (struct sockaddr *)&addr, sizeof(addr)) 299 != len) 300 warn("sendto biff: %s", strerror(errno)); 301 } 302 303 void 304 usage() 305 { 306 eval = EX_USAGE; 307 err("usage: mail.local [-f from] user ..."); 308 } 309 310 #if __STDC__ 311 void 312 err(const char *fmt, ...) 313 #else 314 void 315 err(fmt, va_alist) 316 const char *fmt; 317 va_dcl 318 #endif 319 { 320 va_list ap; 321 322 #if __STDC__ 323 va_start(ap, fmt); 324 #else 325 va_start(ap); 326 #endif 327 vwarn(fmt, ap); 328 va_end(ap); 329 330 exit(eval); 331 } 332 333 void 334 #if __STDC__ 335 warn(const char *fmt, ...) 336 #else 337 warn(fmt, va_alist) 338 const char *fmt; 339 va_dcl 340 #endif 341 { 342 va_list ap; 343 344 #if __STDC__ 345 va_start(ap, fmt); 346 #else 347 va_start(ap); 348 #endif 349 vwarn(fmt, ap); 350 va_end(ap); 351 } 352 353 void 354 vwarn(fmt, ap) 355 const char *fmt; 356 _BSD_VA_LIST_ ap; 357 { 358 /* 359 * Log the message to stderr. 360 * 361 * Don't use LOG_PERROR as an openlog() flag to do this, 362 * it's not portable enough. 363 */ 364 if (eval != EX_USAGE) 365 (void)fprintf(stderr, "mail.local: "); 366 (void)vfprintf(stderr, fmt, ap); 367 (void)fprintf(stderr, "\n"); 368 369 /* Log the message to syslog. */ 370 vsyslog(LOG_ERR, fmt, ap); 371 } 372 373 /* 374 * e_to_sys -- 375 * Guess which errno's are temporary. Gag me. 376 */ 377 void 378 e_to_sys(num) 379 int num; 380 { 381 /* Temporary failures override hard errors. */ 382 if (eval == EX_TEMPFAIL) 383 return; 384 385 switch(num) { /* Hopefully temporary errors. */ 386 #ifdef EAGAIN 387 case EAGAIN: /* Resource temporarily unavailable */ 388 #endif 389 #ifdef EDQUOT 390 case EDQUOT: /* Disc quota exceeded */ 391 #endif 392 #ifdef EBUSY 393 case EBUSY: /* Device busy */ 394 #endif 395 #ifdef EPROCLIM 396 case EPROCLIM: /* Too many processes */ 397 #endif 398 #ifdef EUSERS 399 case EUSERS: /* Too many users */ 400 #endif 401 #ifdef ECONNABORTED 402 case ECONNABORTED: /* Software caused connection abort */ 403 #endif 404 #ifdef ECONNREFUSED 405 case ECONNREFUSED: /* Connection refused */ 406 #endif 407 #ifdef ECONNRESET 408 case ECONNRESET: /* Connection reset by peer */ 409 #endif 410 #ifdef EDEADLK 411 case EDEADLK: /* Resource deadlock avoided */ 412 #endif 413 #ifdef EFBIG 414 case EFBIG: /* File too large */ 415 #endif 416 #ifdef EHOSTDOWN 417 case EHOSTDOWN: /* Host is down */ 418 #endif 419 #ifdef EHOSTUNREACH 420 case EHOSTUNREACH: /* No route to host */ 421 #endif 422 #ifdef EMFILE 423 case EMFILE: /* Too many open files */ 424 #endif 425 #ifdef ENETDOWN 426 case ENETDOWN: /* Network is down */ 427 #endif 428 #ifdef ENETRESET 429 case ENETRESET: /* Network dropped connection on reset */ 430 #endif 431 #ifdef ENETUNREACH 432 case ENETUNREACH: /* Network is unreachable */ 433 #endif 434 #ifdef ENFILE 435 case ENFILE: /* Too many open files in system */ 436 #endif 437 #ifdef ENOBUFS 438 case ENOBUFS: /* No buffer space available */ 439 #endif 440 #ifdef ENOMEM 441 case ENOMEM: /* Cannot allocate memory */ 442 #endif 443 #ifdef ENOSPC 444 case ENOSPC: /* No space left on device */ 445 #endif 446 #ifdef EROFS 447 case EROFS: /* Read-only file system */ 448 #endif 449 #ifdef ESTALE 450 case ESTALE: /* Stale NFS file handle */ 451 #endif 452 #ifdef ETIMEDOUT 453 case ETIMEDOUT: /* Connection timed out */ 454 #endif 455 #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN) 456 case EWOULDBLOCK: /* Operation would block. */ 457 #endif 458 eval = EX_TEMPFAIL; 459 break; 460 default: 461 eval = EX_UNAVAILABLE; 462 break; 463 } 464 } 465