145976Sbostic /*- 266733Sbostic * Copyright (c) 1990, 1993, 1994 361434Sbostic * The Regents of the University of California. All rights reserved. 445976Sbostic * 545976Sbostic * %sccs.include.redist.c% 645976Sbostic */ 745976Sbostic 814465Ssam #ifndef lint 961434Sbostic static char copyright[] = 1066733Sbostic "@(#) Copyright (c) 1990, 1993, 1994\n\ 1161434Sbostic The Regents of the University of California. All rights reserved.\n"; 1245976Sbostic #endif /* not lint */ 1314465Ssam 1445976Sbostic #ifndef lint 15*66735Sbostic static char sccsid[] = "@(#)mail.local.c 8.5 (Berkeley) 04/08/94"; 1645976Sbostic #endif /* not lint */ 1745976Sbostic 1829763Skarels #include <sys/param.h> 1916731Sralph #include <sys/stat.h> 2045976Sbostic #include <sys/socket.h> 2157648Sbostic 2245976Sbostic #include <netinet/in.h> 2357648Sbostic 2457648Sbostic #include <errno.h> 2546672Sbostic #include <fcntl.h> 2645976Sbostic #include <netdb.h> 2745976Sbostic #include <pwd.h> 28579Sroot #include <stdio.h> 2946672Sbostic #include <stdlib.h> 3045976Sbostic #include <string.h> 3159601Sbostic #include <sysexits.h> 3257648Sbostic #include <syslog.h> 3357648Sbostic #include <time.h> 3457648Sbostic #include <unistd.h> 3557648Sbostic 3659601Sbostic #if __STDC__ 3759601Sbostic #include <stdarg.h> 3859601Sbostic #else 3959601Sbostic #include <varargs.h> 4059601Sbostic #endif 4159601Sbostic 4237465Sbostic #include "pathnames.h" 43579Sroot 4459601Sbostic int eval = EX_OK; /* sysexits.h error value. */ 45579Sroot 4659601Sbostic void deliver __P((int, char *)); 4759601Sbostic void e_to_sys __P((int)); 4859601Sbostic __dead void err __P((const char *, ...)); 4959601Sbostic void notifybiff __P((char *)); 5059601Sbostic int store __P((char *)); 5159601Sbostic void usage __P((void)); 5259601Sbostic void vwarn __P((const char *, _BSD_VA_LIST_)); 5359601Sbostic void warn __P((const char *, ...)); 5450092Sbostic 5557648Sbostic int 56579Sroot main(argc, argv) 5745976Sbostic int argc; 5859601Sbostic char *argv[]; 59579Sroot { 6045976Sbostic struct passwd *pw; 6159601Sbostic int ch, fd; 6245976Sbostic uid_t uid; 6345976Sbostic char *from; 64579Sroot 6558400Sbostic openlog("mail.local", 0, LOG_MAIL); 6616731Sralph 6745976Sbostic from = NULL; 6845976Sbostic while ((ch = getopt(argc, argv, "df:r:")) != EOF) 6945976Sbostic switch(ch) { 7059601Sbostic case 'd': /* Backward compatible. */ 7116731Sralph break; 7216731Sralph case 'f': 7359601Sbostic case 'r': /* Backward compatible. */ 7459601Sbostic if (from != NULL) { 7559601Sbostic warn("multiple -f options"); 7659601Sbostic usage(); 7759601Sbostic } 7845976Sbostic from = optarg; 79579Sroot break; 8045976Sbostic case '?': 8116731Sralph default: 8245976Sbostic usage(); 8316731Sralph } 8445976Sbostic argc -= optind; 8545976Sbostic argv += optind; 86579Sroot 8745976Sbostic if (!*argv) 8845976Sbostic usage(); 89579Sroot 9045976Sbostic /* 9145976Sbostic * If from not specified, use the name from getlogin() if the 9245976Sbostic * uid matches, otherwise, use the name from the password file 9345976Sbostic * corresponding to the uid. 9445976Sbostic */ 9545976Sbostic uid = getuid(); 9646034Sbostic if (!from && (!(from = getlogin()) || 9746034Sbostic !(pw = getpwnam(from)) || pw->pw_uid != uid)) 9845976Sbostic from = (pw = getpwuid(uid)) ? pw->pw_name : "???"; 99579Sroot 10059601Sbostic /* 10159601Sbostic * There is no way to distinguish the error status of one delivery 10259601Sbostic * from the rest of the deliveries. So, if we failed hard on one 10359601Sbostic * or more deliveries, but had no failures on any of the others, we 10459601Sbostic * return a hard failure. If we failed temporarily on one or more 10559601Sbostic * deliveries, we return a temporary failure regardless of the other 10659601Sbostic * failures. This results in the delivery being reattempted later 10759601Sbostic * at the expense of repeated failures and multiple deliveries. 10859601Sbostic */ 10959601Sbostic for (fd = store(from); *argv; ++argv) 11059601Sbostic deliver(fd, *argv); 11145976Sbostic exit(eval); 112579Sroot } 113579Sroot 11457648Sbostic int 11545976Sbostic store(from) 11645976Sbostic char *from; 117579Sroot { 11845976Sbostic FILE *fp; 11945976Sbostic time_t tval; 12045976Sbostic int fd, eline; 12145976Sbostic char *tn, line[2048]; 122579Sroot 12347075Sdonn tn = strdup(_PATH_LOCTMP); 12459601Sbostic if ((fd = mkstemp(tn)) == -1 || (fp = fdopen(fd, "w+")) == NULL) { 12559601Sbostic e_to_sys(errno); 12659601Sbostic err("unable to open temporary file"); 12759601Sbostic } 12845976Sbostic (void)unlink(tn); 12947075Sdonn free(tn); 130579Sroot 13145976Sbostic (void)time(&tval); 13245976Sbostic (void)fprintf(fp, "From %s %s", from, ctime(&tval)); 133579Sroot 13445976Sbostic line[0] = '\0'; 13545976Sbostic for (eline = 1; fgets(line, sizeof(line), stdin);) { 13645976Sbostic if (line[0] == '\n') 13745976Sbostic eline = 1; 13845976Sbostic else { 13960097Sbostic if (eline && line[0] == 'F' && 14060097Sbostic !memcmp(line, "From ", 5)) 14145976Sbostic (void)putc('>', fp); 14245976Sbostic eline = 0; 14345976Sbostic } 14445976Sbostic (void)fprintf(fp, "%s", line); 14559601Sbostic if (ferror(fp)) { 14659601Sbostic e_to_sys(errno); 14759601Sbostic err("temporary file write error"); 14859601Sbostic } 149579Sroot } 150579Sroot 15145976Sbostic /* If message not newline terminated, need an extra. */ 15259601Sbostic if (!strchr(line, '\n')) 15345976Sbostic (void)putc('\n', fp); 15445976Sbostic /* Output a newline; note, empty messages are allowed. */ 15545976Sbostic (void)putc('\n', fp); 15611893Seric 15759601Sbostic if (fflush(fp) == EOF || ferror(fp)) { 15859601Sbostic e_to_sys(errno); 15959601Sbostic err("temporary file write error"); 16059601Sbostic } 16159601Sbostic return (fd); 162579Sroot } 163579Sroot 16459601Sbostic void 16545976Sbostic deliver(fd, name) 16645976Sbostic int fd; 16745976Sbostic char *name; 168579Sroot { 169*66735Sbostic struct stat fsb, sb; 17045976Sbostic struct passwd *pw; 17159601Sbostic int mbfd, nr, nw, off; 17245976Sbostic char biffmsg[100], buf[8*1024], path[MAXPATHLEN]; 17354203Sbostic off_t curoff; 174579Sroot 17545976Sbostic /* 17645976Sbostic * Disallow delivery to unknown names -- special mailboxes can be 17745976Sbostic * handled in the sendmail aliases file. 17845976Sbostic */ 17945976Sbostic if (!(pw = getpwnam(name))) { 18059601Sbostic if (eval != EX_TEMPFAIL) 18159601Sbostic eval = EX_UNAVAILABLE; 18259601Sbostic warn("unknown name: %s", name); 18359601Sbostic return; 18445976Sbostic } 185579Sroot 18657648Sbostic (void)snprintf(path, sizeof(path), "%s/%s", _PATH_MAILDIR, name); 187579Sroot 188579Sroot /* 18966733Sbostic * If the mailbox is linked or a symlink, fail. There's an obvious 19066733Sbostic * race here, that the file was replaced with a symbolic link after 191*66735Sbostic * the lstat returned, but before the open. We attempt to detect 192*66735Sbostic * this by comparing the original stat information and information 193*66735Sbostic * returned by an fstat of the file descriptor returned by the open. 19459601Sbostic * 195*66735Sbostic * NB: this is a symptom of a larger problem, that the mail spooling 196*66735Sbostic * directory is writeable by the wrong users. If that directory is 197*66735Sbostic * writeable, system security is compromised for other reasons, and 198*66735Sbostic * it cannot be fixed here. 199*66735Sbostic * 20059601Sbostic * If we created the mailbox, set the owner/group. If that fails, 20159601Sbostic * just return. Another process may have already opened it, so we 20259601Sbostic * can't unlink it. Historically, binmail set the owner/group at 20359601Sbostic * each mail delivery. We no longer do this, assuming that if the 20459601Sbostic * ownership or permissions were changed there was a reason. 20559601Sbostic * 20659601Sbostic * XXX 20759601Sbostic * open(2) should support flock'ing the file. 208579Sroot */ 20959601Sbostic if (lstat(path, &sb)) { 21059601Sbostic if ((mbfd = open(path, 21159601Sbostic O_APPEND|O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) 21259601Sbostic mbfd = open(path, O_APPEND|O_WRONLY, 0); 21359601Sbostic else if (fchown(mbfd, pw->pw_uid, pw->pw_gid)) { 21459601Sbostic e_to_sys(errno); 21559601Sbostic warn("chown %u.%u: %s", pw->pw_uid, pw->pw_gid, name); 21659601Sbostic return; 21759601Sbostic } 21859601Sbostic } else if (sb.st_nlink != 1 || S_ISLNK(sb.st_mode)) { 21959601Sbostic e_to_sys(errno); 22059601Sbostic warn("%s: linked file", path); 22159601Sbostic return; 22266733Sbostic } else { 22359601Sbostic mbfd = open(path, O_APPEND|O_WRONLY, 0); 224*66735Sbostic if (mbfd != -1 && 225*66735Sbostic (fstat(mbfd, &fsb) || fsb.st_nlink != 1 || 226*66735Sbostic S_ISLNK(fsb.st_mode) || sb.st_dev != fsb.st_dev || 227*66735Sbostic sb.st_ino != fsb.st_ino)) { 228*66735Sbostic warn("%s: file changed after open", path); 22966733Sbostic (void)close(mbfd); 23066733Sbostic return; 23166733Sbostic } 23266733Sbostic } 23359601Sbostic 23459601Sbostic if (mbfd == -1) { 23559601Sbostic e_to_sys(errno); 23659601Sbostic warn("%s: %s", path, strerror(errno)); 23759601Sbostic return; 23845976Sbostic } 239579Sroot 24059601Sbostic /* Wait until we can get a lock on the file. */ 24145976Sbostic if (flock(mbfd, LOCK_EX)) { 24259601Sbostic e_to_sys(errno); 24359601Sbostic warn("%s: %s", path, strerror(errno)); 24459601Sbostic goto err1; 24545976Sbostic } 246579Sroot 24759601Sbostic /* Get the starting offset of the new message for biff. */ 24854203Sbostic curoff = lseek(mbfd, (off_t)0, SEEK_END); 24957648Sbostic (void)snprintf(biffmsg, sizeof(biffmsg), "%s@%qd\n", name, curoff); 25059601Sbostic 25159601Sbostic /* Copy the message into the file. */ 25254203Sbostic if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) { 25359601Sbostic e_to_sys(errno); 25459601Sbostic warn("temporary file: %s", strerror(errno)); 25559601Sbostic goto err1; 25645976Sbostic } 25745976Sbostic while ((nr = read(fd, buf, sizeof(buf))) > 0) 25845976Sbostic for (off = 0; off < nr; nr -= nw, off += nw) 25945976Sbostic if ((nw = write(mbfd, buf + off, nr)) < 0) { 26059601Sbostic e_to_sys(errno); 26159601Sbostic warn("%s: %s", path, strerror(errno)); 26259601Sbostic goto err2;; 26345976Sbostic } 26445976Sbostic if (nr < 0) { 26559601Sbostic e_to_sys(errno); 26659601Sbostic warn("temporary file: %s", strerror(errno)); 26759601Sbostic goto err2;; 268579Sroot } 269579Sroot 27059601Sbostic /* Flush to disk, don't wait for update. */ 27159601Sbostic if (fsync(mbfd)) { 27259601Sbostic e_to_sys(errno); 27359601Sbostic warn("%s: %s", path, strerror(errno)); 27459601Sbostic err2: (void)ftruncate(mbfd, curoff); 27559601Sbostic err1: (void)close(mbfd); 27659601Sbostic return; 27759601Sbostic } 27859601Sbostic 27959601Sbostic /* Close and check -- NFS doesn't write until the close. */ 28059601Sbostic if (close(mbfd)) { 28159601Sbostic e_to_sys(errno); 28259601Sbostic warn("%s: %s", path, strerror(errno)); 28359601Sbostic return; 28459601Sbostic } 285579Sroot 28659601Sbostic notifybiff(biffmsg); 287579Sroot } 288579Sroot 28950092Sbostic void 29016731Sralph notifybiff(msg) 29116731Sralph char *msg; 29216731Sralph { 29316731Sralph static struct sockaddr_in addr; 29416731Sralph static int f = -1; 29545976Sbostic struct hostent *hp; 29645976Sbostic struct servent *sp; 29745976Sbostic int len; 29816731Sralph 29945976Sbostic if (!addr.sin_family) { 30045976Sbostic /* Be silent if biff service not available. */ 30145976Sbostic if (!(sp = getservbyname("biff", "udp"))) 30245976Sbostic return; 30345976Sbostic if (!(hp = gethostbyname("localhost"))) { 30459601Sbostic warn("localhost: %s", strerror(errno)); 30545976Sbostic return; 30616731Sralph } 30745976Sbostic addr.sin_family = hp->h_addrtype; 30860097Sbostic memmove(&addr.sin_addr, hp->h_addr, hp->h_length); 30945976Sbostic addr.sin_port = sp->s_port; 31016731Sralph } 31145976Sbostic if (f < 0 && (f = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 31259601Sbostic warn("socket: %s", strerror(errno)); 31345976Sbostic return; 31416731Sralph } 31545976Sbostic len = strlen(msg) + 1; 31646672Sbostic if (sendto(f, msg, len, 0, (struct sockaddr *)&addr, sizeof(addr)) 31746672Sbostic != len) 31859601Sbostic warn("sendto biff: %s", strerror(errno)); 31916731Sralph } 32016731Sralph 32150092Sbostic void 32245976Sbostic usage() 323579Sroot { 32459601Sbostic eval = EX_USAGE; 32559601Sbostic err("usage: mail.local [-f from] user ..."); 326579Sroot } 327579Sroot 32850092Sbostic #if __STDC__ 32965558Sbostic void 33059601Sbostic err(const char *fmt, ...) 33150092Sbostic #else 33264993Smckusick void 33359601Sbostic err(fmt, va_alist) 33459601Sbostic const char *fmt; 33559601Sbostic va_dcl 33650092Sbostic #endif 33759601Sbostic { 33859601Sbostic va_list ap; 33950092Sbostic 34059601Sbostic #if __STDC__ 34159601Sbostic va_start(ap, fmt); 34259601Sbostic #else 34359601Sbostic va_start(ap); 34459601Sbostic #endif 34559601Sbostic vwarn(fmt, ap); 34659601Sbostic va_end(ap); 34759601Sbostic 34859601Sbostic exit(eval); 34959601Sbostic } 35059601Sbostic 35150092Sbostic void 35250092Sbostic #if __STDC__ 35359601Sbostic warn(const char *fmt, ...) 35450092Sbostic #else 35559601Sbostic warn(fmt, va_alist) 35659601Sbostic const char *fmt; 35750092Sbostic va_dcl 35850092Sbostic #endif 359579Sroot { 36045976Sbostic va_list ap; 36159601Sbostic 36250092Sbostic #if __STDC__ 36346554Sbostic va_start(ap, fmt); 36450092Sbostic #else 36550092Sbostic va_start(ap); 36650092Sbostic #endif 36759601Sbostic vwarn(fmt, ap); 36859601Sbostic va_end(ap); 36959601Sbostic } 37059601Sbostic 37159601Sbostic void 37259601Sbostic vwarn(fmt, ap) 37359601Sbostic const char *fmt; 37459601Sbostic _BSD_VA_LIST_ ap; 37559601Sbostic { 37658400Sbostic /* 37759601Sbostic * Log the message to stderr. 37859601Sbostic * 37959601Sbostic * Don't use LOG_PERROR as an openlog() flag to do this, 38059601Sbostic * it's not portable enough. 38158400Sbostic */ 38259601Sbostic if (eval != EX_USAGE) 38359601Sbostic (void)fprintf(stderr, "mail.local: "); 38458396Sbostic (void)vfprintf(stderr, fmt, ap); 38558400Sbostic (void)fprintf(stderr, "\n"); 38659601Sbostic 38759601Sbostic /* Log the message to syslog. */ 38845976Sbostic vsyslog(LOG_ERR, fmt, ap); 389579Sroot } 39059601Sbostic 39159601Sbostic /* 39259601Sbostic * e_to_sys -- 39359601Sbostic * Guess which errno's are temporary. Gag me. 39459601Sbostic */ 39559601Sbostic void 39659601Sbostic e_to_sys(num) 39759601Sbostic int num; 39859601Sbostic { 39959601Sbostic /* Temporary failures override hard errors. */ 40059601Sbostic if (eval == EX_TEMPFAIL) 40159601Sbostic return; 40259601Sbostic 40359601Sbostic switch(num) { /* Hopefully temporary errors. */ 40459601Sbostic #ifdef EAGAIN 40559601Sbostic case EAGAIN: /* Resource temporarily unavailable */ 40659601Sbostic #endif 40759601Sbostic #ifdef EDQUOT 40859601Sbostic case EDQUOT: /* Disc quota exceeded */ 40959601Sbostic #endif 41059601Sbostic #ifdef EBUSY 41159601Sbostic case EBUSY: /* Device busy */ 41259601Sbostic #endif 41359601Sbostic #ifdef EPROCLIM 41459601Sbostic case EPROCLIM: /* Too many processes */ 41559601Sbostic #endif 41659601Sbostic #ifdef EUSERS 41759601Sbostic case EUSERS: /* Too many users */ 41859601Sbostic #endif 41959601Sbostic #ifdef ECONNABORTED 42059601Sbostic case ECONNABORTED: /* Software caused connection abort */ 42159601Sbostic #endif 42259601Sbostic #ifdef ECONNREFUSED 42359601Sbostic case ECONNREFUSED: /* Connection refused */ 42459601Sbostic #endif 42559601Sbostic #ifdef ECONNRESET 42659601Sbostic case ECONNRESET: /* Connection reset by peer */ 42759601Sbostic #endif 42859601Sbostic #ifdef EDEADLK 42959601Sbostic case EDEADLK: /* Resource deadlock avoided */ 43059601Sbostic #endif 43159601Sbostic #ifdef EFBIG 43259601Sbostic case EFBIG: /* File too large */ 43359601Sbostic #endif 43459601Sbostic #ifdef EHOSTDOWN 43559601Sbostic case EHOSTDOWN: /* Host is down */ 43659601Sbostic #endif 43759601Sbostic #ifdef EHOSTUNREACH 43859601Sbostic case EHOSTUNREACH: /* No route to host */ 43959601Sbostic #endif 44059601Sbostic #ifdef EMFILE 44159601Sbostic case EMFILE: /* Too many open files */ 44259601Sbostic #endif 44359601Sbostic #ifdef ENETDOWN 44459601Sbostic case ENETDOWN: /* Network is down */ 44559601Sbostic #endif 44659601Sbostic #ifdef ENETRESET 44759601Sbostic case ENETRESET: /* Network dropped connection on reset */ 44859601Sbostic #endif 44959601Sbostic #ifdef ENETUNREACH 45059601Sbostic case ENETUNREACH: /* Network is unreachable */ 45159601Sbostic #endif 45259601Sbostic #ifdef ENFILE 45359601Sbostic case ENFILE: /* Too many open files in system */ 45459601Sbostic #endif 45559601Sbostic #ifdef ENOBUFS 45659601Sbostic case ENOBUFS: /* No buffer space available */ 45759601Sbostic #endif 45859601Sbostic #ifdef ENOMEM 45959601Sbostic case ENOMEM: /* Cannot allocate memory */ 46059601Sbostic #endif 46159601Sbostic #ifdef ENOSPC 46259601Sbostic case ENOSPC: /* No space left on device */ 46359601Sbostic #endif 46459601Sbostic #ifdef EROFS 46559601Sbostic case EROFS: /* Read-only file system */ 46659601Sbostic #endif 46759601Sbostic #ifdef ESTALE 46859601Sbostic case ESTALE: /* Stale NFS file handle */ 46959601Sbostic #endif 47059601Sbostic #ifdef ETIMEDOUT 47159601Sbostic case ETIMEDOUT: /* Connection timed out */ 47259601Sbostic #endif 47359601Sbostic #if defined(EWOULDBLOCK) && (EWOULDBLOCK != EAGAIN) 47459601Sbostic case EWOULDBLOCK: /* Operation would block. */ 47559601Sbostic #endif 47659601Sbostic eval = EX_TEMPFAIL; 47759601Sbostic break; 47859601Sbostic default: 47959601Sbostic eval = EX_UNAVAILABLE; 48059601Sbostic break; 48159601Sbostic } 48259601Sbostic } 483