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*68021Seric static char sccsid[] = "@(#)mail.local.c 8.10 (Berkeley) 12/01/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 4267830Seric #ifndef LOCK_EX 4367830Seric # include <sys/file.h> 4467830Seric #endif 45579Sroot 4667830Seric #ifdef BSD4_4 4767830Seric # include "pathnames.h" 4867830Seric #endif 4967830Seric 5067830Seric #ifndef __P 5167830Seric # ifdef __STDC__ 5267830Seric # define __P(protos) protos 5367830Seric # else 5467830Seric # define __P(protos) () 5567830Seric # define const 5667830Seric # endif 5767830Seric #endif 5867830Seric #ifndef __dead 5967830Seric # if defined(__GNUC__) && (__GNUC__ < 2 || __GNUC_MINOR__ < 5) && !defined(__STRICT_ANSI__) 6067830Seric # define __dead __volatile 6167830Seric # else 6267830Seric # define __dead 6367830Seric # endif 6467830Seric #endif 6567830Seric 6667830Seric #ifndef BSD4_4 6767830Seric # define _BSD_VA_LIST_ va_list 6867830Seric extern char *strerror __P((int)); 6967830Seric #endif 7067830Seric 7167830Seric #ifndef _PATH_LOCTMP 7267830Seric # define _PATH_LOCTMP "/tmp/local.XXXXXX" 7367830Seric #endif 7467830Seric #ifndef _PATH_MAILDIR 7567830Seric # define _PATH_MAILDIR "/var/spool/mail" 7667830Seric #endif 7767830Seric 7867830Seric #ifndef S_ISLNK 7967830Seric # define S_ISLNK(mode) (((mode) & _S_IFMT) == S_IFLNK) 8067830Seric #endif 8167830Seric 8259601Sbostic int eval = EX_OK; /* sysexits.h error value. */ 83579Sroot 8459601Sbostic void deliver __P((int, char *)); 8559601Sbostic void e_to_sys __P((int)); 8659601Sbostic __dead void err __P((const char *, ...)); 8759601Sbostic void notifybiff __P((char *)); 8859601Sbostic int store __P((char *)); 8959601Sbostic void usage __P((void)); 9059601Sbostic void vwarn __P((const char *, _BSD_VA_LIST_)); 9159601Sbostic void warn __P((const char *, ...)); 9250092Sbostic 9357648Sbostic int 94579Sroot main(argc, argv) 9545976Sbostic int argc; 9659601Sbostic char *argv[]; 97579Sroot { 9845976Sbostic struct passwd *pw; 9959601Sbostic int ch, fd; 10045976Sbostic uid_t uid; 10145976Sbostic char *from; 10267830Seric extern char *optarg; 10367830Seric extern int optind; 104579Sroot 10567830Seric #ifdef LOG_MAIL 10658400Sbostic openlog("mail.local", 0, LOG_MAIL); 10767830Seric #else 10867830Seric openlog("mail.local", 0); 10967830Seric #endif 11016731Sralph 11145976Sbostic from = NULL; 11245976Sbostic while ((ch = getopt(argc, argv, "df:r:")) != EOF) 11345976Sbostic switch(ch) { 11459601Sbostic case 'd': /* Backward compatible. */ 11516731Sralph break; 11616731Sralph case 'f': 11759601Sbostic case 'r': /* Backward compatible. */ 11859601Sbostic if (from != NULL) { 11959601Sbostic warn("multiple -f options"); 12059601Sbostic usage(); 12159601Sbostic } 12245976Sbostic from = optarg; 123579Sroot break; 12445976Sbostic case '?': 12516731Sralph default: 12645976Sbostic usage(); 12716731Sralph } 12845976Sbostic argc -= optind; 12945976Sbostic argv += optind; 130579Sroot 13145976Sbostic if (!*argv) 13245976Sbostic usage(); 133579Sroot 13445976Sbostic /* 13545976Sbostic * If from not specified, use the name from getlogin() if the 13645976Sbostic * uid matches, otherwise, use the name from the password file 13745976Sbostic * corresponding to the uid. 13845976Sbostic */ 13945976Sbostic uid = getuid(); 14046034Sbostic if (!from && (!(from = getlogin()) || 14146034Sbostic !(pw = getpwnam(from)) || pw->pw_uid != uid)) 14245976Sbostic from = (pw = getpwuid(uid)) ? pw->pw_name : "???"; 143579Sroot 14459601Sbostic /* 14559601Sbostic * There is no way to distinguish the error status of one delivery 14659601Sbostic * from the rest of the deliveries. So, if we failed hard on one 14759601Sbostic * or more deliveries, but had no failures on any of the others, we 14859601Sbostic * return a hard failure. If we failed temporarily on one or more 14959601Sbostic * deliveries, we return a temporary failure regardless of the other 15059601Sbostic * failures. This results in the delivery being reattempted later 15159601Sbostic * at the expense of repeated failures and multiple deliveries. 15259601Sbostic */ 15359601Sbostic for (fd = store(from); *argv; ++argv) 15459601Sbostic deliver(fd, *argv); 15545976Sbostic exit(eval); 156579Sroot } 157579Sroot 15857648Sbostic int 15945976Sbostic store(from) 16045976Sbostic char *from; 161579Sroot { 16245976Sbostic FILE *fp; 16345976Sbostic time_t tval; 16445976Sbostic int fd, eline; 16567830Seric char line[2048]; 16667830Seric char tmpbuf[sizeof _PATH_LOCTMP + 1]; 167579Sroot 16867830Seric strcpy(tmpbuf, _PATH_LOCTMP); 16967830Seric if ((fd = mkstemp(tmpbuf)) == -1 || (fp = fdopen(fd, "w+")) == NULL) { 17059601Sbostic e_to_sys(errno); 17159601Sbostic err("unable to open temporary file"); 17259601Sbostic } 17367830Seric (void)unlink(tmpbuf); 174579Sroot 17545976Sbostic (void)time(&tval); 17645976Sbostic (void)fprintf(fp, "From %s %s", from, ctime(&tval)); 177579Sroot 17845976Sbostic line[0] = '\0'; 17945976Sbostic for (eline = 1; fgets(line, sizeof(line), stdin);) { 18045976Sbostic if (line[0] == '\n') 18145976Sbostic eline = 1; 18245976Sbostic else { 18360097Sbostic if (eline && line[0] == 'F' && 18460097Sbostic !memcmp(line, "From ", 5)) 18545976Sbostic (void)putc('>', fp); 18645976Sbostic eline = 0; 18745976Sbostic } 18845976Sbostic (void)fprintf(fp, "%s", line); 18959601Sbostic if (ferror(fp)) { 19059601Sbostic e_to_sys(errno); 19159601Sbostic err("temporary file write error"); 19259601Sbostic } 193579Sroot } 194579Sroot 19545976Sbostic /* If message not newline terminated, need an extra. */ 19659601Sbostic if (!strchr(line, '\n')) 19745976Sbostic (void)putc('\n', fp); 19845976Sbostic /* Output a newline; note, empty messages are allowed. */ 19945976Sbostic (void)putc('\n', fp); 20011893Seric 20159601Sbostic if (fflush(fp) == EOF || ferror(fp)) { 20259601Sbostic e_to_sys(errno); 20359601Sbostic err("temporary file write error"); 20459601Sbostic } 20559601Sbostic return (fd); 206579Sroot } 207579Sroot 20859601Sbostic void 20945976Sbostic deliver(fd, name) 21045976Sbostic int fd; 21145976Sbostic char *name; 212579Sroot { 21366735Sbostic struct stat fsb, sb; 21445976Sbostic struct passwd *pw; 21559601Sbostic int mbfd, nr, nw, off; 21645976Sbostic char biffmsg[100], buf[8*1024], path[MAXPATHLEN]; 21754203Sbostic off_t curoff; 218579Sroot 21945976Sbostic /* 22045976Sbostic * Disallow delivery to unknown names -- special mailboxes can be 22145976Sbostic * handled in the sendmail aliases file. 22245976Sbostic */ 22345976Sbostic if (!(pw = getpwnam(name))) { 22459601Sbostic if (eval != EX_TEMPFAIL) 22559601Sbostic eval = EX_UNAVAILABLE; 22659601Sbostic warn("unknown name: %s", name); 22759601Sbostic return; 22845976Sbostic } 229579Sroot 23057648Sbostic (void)snprintf(path, sizeof(path), "%s/%s", _PATH_MAILDIR, name); 231579Sroot 232579Sroot /* 23366733Sbostic * If the mailbox is linked or a symlink, fail. There's an obvious 23466733Sbostic * race here, that the file was replaced with a symbolic link after 23566735Sbostic * the lstat returned, but before the open. We attempt to detect 23666735Sbostic * this by comparing the original stat information and information 23766735Sbostic * returned by an fstat of the file descriptor returned by the open. 23859601Sbostic * 23966735Sbostic * NB: this is a symptom of a larger problem, that the mail spooling 24066735Sbostic * directory is writeable by the wrong users. If that directory is 24166735Sbostic * writeable, system security is compromised for other reasons, and 24266735Sbostic * it cannot be fixed here. 24366735Sbostic * 24459601Sbostic * If we created the mailbox, set the owner/group. If that fails, 24559601Sbostic * just return. Another process may have already opened it, so we 24659601Sbostic * can't unlink it. Historically, binmail set the owner/group at 24759601Sbostic * each mail delivery. We no longer do this, assuming that if the 24859601Sbostic * ownership or permissions were changed there was a reason. 24959601Sbostic * 25059601Sbostic * XXX 25159601Sbostic * open(2) should support flock'ing the file. 252579Sroot */ 25366736Sbostic tryagain: 25468005Seric lockmbox(path); 25559601Sbostic if (lstat(path, &sb)) { 25666736Sbostic mbfd = open(path, 25766736Sbostic O_APPEND|O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR); 25866736Sbostic if (mbfd == -1) { 25966736Sbostic if (errno == EEXIST) 26066736Sbostic goto tryagain; 26166736Sbostic } else if (fchown(mbfd, pw->pw_uid, pw->pw_gid)) { 26259601Sbostic e_to_sys(errno); 26359601Sbostic warn("chown %u.%u: %s", pw->pw_uid, pw->pw_gid, name); 26468005Seric unlockmbox(); 26559601Sbostic return; 26659601Sbostic } 26759601Sbostic } else if (sb.st_nlink != 1 || S_ISLNK(sb.st_mode)) { 26859601Sbostic e_to_sys(errno); 26959601Sbostic warn("%s: linked file", path); 27068005Seric unlockmbox(); 27159601Sbostic return; 27266733Sbostic } else { 27359601Sbostic mbfd = open(path, O_APPEND|O_WRONLY, 0); 27466735Sbostic if (mbfd != -1 && 27566735Sbostic (fstat(mbfd, &fsb) || fsb.st_nlink != 1 || 27666735Sbostic S_ISLNK(fsb.st_mode) || sb.st_dev != fsb.st_dev || 27766735Sbostic sb.st_ino != fsb.st_ino)) { 27866735Sbostic warn("%s: file changed after open", path); 27966733Sbostic (void)close(mbfd); 28068005Seric unlockmbox(); 28166733Sbostic return; 28266733Sbostic } 28366733Sbostic } 28459601Sbostic 28559601Sbostic if (mbfd == -1) { 28659601Sbostic e_to_sys(errno); 28759601Sbostic warn("%s: %s", path, strerror(errno)); 28868005Seric unlockmbox(); 28959601Sbostic return; 29045976Sbostic } 291579Sroot 29259601Sbostic /* Wait until we can get a lock on the file. */ 29345976Sbostic if (flock(mbfd, LOCK_EX)) { 29459601Sbostic e_to_sys(errno); 29559601Sbostic warn("%s: %s", path, strerror(errno)); 29668005Seric unlockmbox(); 29759601Sbostic goto err1; 29845976Sbostic } 299579Sroot 30059601Sbostic /* Get the starting offset of the new message for biff. */ 30154203Sbostic curoff = lseek(mbfd, (off_t)0, SEEK_END); 302*68021Seric (void)snprintf(biffmsg, sizeof(biffmsg), 303*68021Seric sizeof curoff > sizeof(long) ? "%s@%qd\n" : "%s@%ld\n", 304*68021Seric name, curoff); 30559601Sbostic 30659601Sbostic /* Copy the message into the file. */ 30754203Sbostic if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) { 30859601Sbostic e_to_sys(errno); 30959601Sbostic warn("temporary file: %s", strerror(errno)); 31059601Sbostic goto err1; 31145976Sbostic } 31245976Sbostic while ((nr = read(fd, buf, sizeof(buf))) > 0) 31345976Sbostic for (off = 0; off < nr; nr -= nw, off += nw) 31445976Sbostic if ((nw = write(mbfd, buf + off, nr)) < 0) { 31559601Sbostic e_to_sys(errno); 31659601Sbostic warn("%s: %s", path, strerror(errno)); 31759601Sbostic goto err2;; 31845976Sbostic } 31945976Sbostic if (nr < 0) { 32059601Sbostic e_to_sys(errno); 32159601Sbostic warn("temporary file: %s", strerror(errno)); 32259601Sbostic goto err2;; 323579Sroot } 324579Sroot 32559601Sbostic /* Flush to disk, don't wait for update. */ 32659601Sbostic if (fsync(mbfd)) { 32759601Sbostic e_to_sys(errno); 32859601Sbostic warn("%s: %s", path, strerror(errno)); 32959601Sbostic err2: (void)ftruncate(mbfd, curoff); 33059601Sbostic err1: (void)close(mbfd); 33168005Seric unlockmbox(); 33259601Sbostic return; 33359601Sbostic } 33459601Sbostic 33559601Sbostic /* Close and check -- NFS doesn't write until the close. */ 33659601Sbostic if (close(mbfd)) { 33759601Sbostic e_to_sys(errno); 33859601Sbostic warn("%s: %s", path, strerror(errno)); 33968005Seric unlockmbox(); 34059601Sbostic return; 34159601Sbostic } 342579Sroot 34368005Seric unlockmbox(); 34459601Sbostic notifybiff(biffmsg); 345579Sroot } 346579Sroot 34768005Seric /* 34868005Seric * user.lock files are necessary for compatibility with other 34968005Seric * systems, e.g., when the mail spool file is NFS exported. 35068005Seric * Alas, mailbox locking is more than just a local matter. 35168005Seric * EPA 11/94. 35268005Seric */ 35368005Seric 35468005Seric char lockname[50]; 35568005Seric int locked = 0; 35668005Seric 35768005Seric lockmbox(path) 35868005Seric char *path; 35968005Seric { 36068005Seric char locktmp[50]; 36168005Seric int statfailed = 0; 36268005Seric 36368005Seric if (locked) 36468005Seric return; 36568005Seric sprintf(lockname, "%s.lock", path); 36668005Seric sprintf(locktmp, "%s/tmXXXXXX", _PATH_MAILDIR); 36768005Seric mktemp(locktmp); 36868005Seric unlink(locktmp); 36968005Seric for (;; sleep(5)) { 37068005Seric int fd; 37168005Seric struct stat st; 37268005Seric time_t now; 37368005Seric 37468006Seric fd = open(locktmp, O_WRONLY|O_EXCL|O_CREAT, 0); 37568005Seric if (fd < 0) 37668005Seric continue; 37768005Seric close(fd); 37868005Seric if (link(locktmp, lockname) >= 0) { 37968005Seric unlink(locktmp); 38068005Seric locked = 1; 38168005Seric return; 38268005Seric } 38368005Seric unlink(locktmp); 38468005Seric if (stat(lockname, &st) < 0) { 38568005Seric if (statfailed++ > 5) 38668005Seric return; 38768005Seric continue; 38868005Seric } 38968005Seric statfailed = 0; 39068005Seric time(&now); 39168005Seric if (now < st.st_ctime + 300) 39268005Seric continue; 39368005Seric unlink(lockname); 39468005Seric } 39568005Seric } 39668005Seric 39768005Seric unlockmbox() 39868005Seric { 39968005Seric if (!locked) 40068005Seric return; 40168005Seric unlink(lockname); 40268005Seric locked = 0; 40368005Seric } 40468005Seric 40550092Sbostic void 40616731Sralph notifybiff(msg) 40716731Sralph char *msg; 40816731Sralph { 40916731Sralph static struct sockaddr_in addr; 41016731Sralph static int f = -1; 41145976Sbostic struct hostent *hp; 41245976Sbostic struct servent *sp; 41345976Sbostic int len; 41416731Sralph 41545976Sbostic if (!addr.sin_family) { 41645976Sbostic /* Be silent if biff service not available. */ 41745976Sbostic if (!(sp = getservbyname("biff", "udp"))) 41845976Sbostic return; 41945976Sbostic if (!(hp = gethostbyname("localhost"))) { 42059601Sbostic warn("localhost: %s", strerror(errno)); 42145976Sbostic return; 42216731Sralph } 42345976Sbostic addr.sin_family = hp->h_addrtype; 42467830Seric memcpy(&addr.sin_addr, hp->h_addr, hp->h_length); 42545976Sbostic addr.sin_port = sp->s_port; 42616731Sralph } 42745976Sbostic if (f < 0 && (f = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 42859601Sbostic warn("socket: %s", strerror(errno)); 42945976Sbostic return; 43016731Sralph } 43145976Sbostic len = strlen(msg) + 1; 43246672Sbostic if (sendto(f, msg, len, 0, (struct sockaddr *)&addr, sizeof(addr)) 43346672Sbostic != len) 43459601Sbostic warn("sendto biff: %s", strerror(errno)); 43516731Sralph } 43616731Sralph 43750092Sbostic void 43845976Sbostic usage() 439579Sroot { 44059601Sbostic eval = EX_USAGE; 44159601Sbostic err("usage: mail.local [-f from] user ..."); 442579Sroot } 443579Sroot 44450092Sbostic #if __STDC__ 44565558Sbostic void 44659601Sbostic err(const char *fmt, ...) 44750092Sbostic #else 44864993Smckusick void 44959601Sbostic err(fmt, va_alist) 45059601Sbostic const char *fmt; 45159601Sbostic va_dcl 45250092Sbostic #endif 45359601Sbostic { 45459601Sbostic va_list ap; 45550092Sbostic 45659601Sbostic #if __STDC__ 45759601Sbostic va_start(ap, fmt); 45859601Sbostic #else 45959601Sbostic va_start(ap); 46059601Sbostic #endif 46159601Sbostic vwarn(fmt, ap); 46259601Sbostic va_end(ap); 46359601Sbostic 46459601Sbostic exit(eval); 46559601Sbostic } 46659601Sbostic 46750092Sbostic void 46850092Sbostic #if __STDC__ 46959601Sbostic warn(const char *fmt, ...) 47050092Sbostic #else 47159601Sbostic warn(fmt, va_alist) 47259601Sbostic const char *fmt; 47350092Sbostic va_dcl 47450092Sbostic #endif 475579Sroot { 47645976Sbostic va_list ap; 47759601Sbostic 47850092Sbostic #if __STDC__ 47946554Sbostic va_start(ap, fmt); 48050092Sbostic #else 48150092Sbostic va_start(ap); 48250092Sbostic #endif 48359601Sbostic vwarn(fmt, ap); 48459601Sbostic va_end(ap); 48559601Sbostic } 48659601Sbostic 48759601Sbostic void 48859601Sbostic vwarn(fmt, ap) 48959601Sbostic const char *fmt; 49059601Sbostic _BSD_VA_LIST_ ap; 49159601Sbostic { 49258400Sbostic /* 49359601Sbostic * Log the message to stderr. 49459601Sbostic * 49559601Sbostic * Don't use LOG_PERROR as an openlog() flag to do this, 49659601Sbostic * it's not portable enough. 49758400Sbostic */ 49859601Sbostic if (eval != EX_USAGE) 49959601Sbostic (void)fprintf(stderr, "mail.local: "); 50058396Sbostic (void)vfprintf(stderr, fmt, ap); 50158400Sbostic (void)fprintf(stderr, "\n"); 50259601Sbostic 50367830Seric #ifndef ultrix 50459601Sbostic /* Log the message to syslog. */ 50545976Sbostic vsyslog(LOG_ERR, fmt, ap); 50667830Seric #else 50767830Seric { 50867830Seric char fmtbuf[10240]; 50967830Seric 51067830Seric (void) sprintf(fmtbuf, fmt, ap); 51167830Seric syslog(LOG_ERR, "%s", fmtbuf); 51267830Seric } 51367830Seric #endif 514579Sroot } 51559601Sbostic 51659601Sbostic /* 51759601Sbostic * e_to_sys -- 51859601Sbostic * Guess which errno's are temporary. Gag me. 51959601Sbostic */ 52059601Sbostic void 52159601Sbostic e_to_sys(num) 52259601Sbostic int num; 52359601Sbostic { 52459601Sbostic /* Temporary failures override hard errors. */ 52559601Sbostic if (eval == EX_TEMPFAIL) 52659601Sbostic return; 52759601Sbostic 52859601Sbostic switch(num) { /* Hopefully temporary errors. */ 52959601Sbostic #ifdef EAGAIN 53059601Sbostic case EAGAIN: /* Resource temporarily unavailable */ 53159601Sbostic #endif 53259601Sbostic #ifdef EDQUOT 53359601Sbostic case EDQUOT: /* Disc quota exceeded */ 53459601Sbostic #endif 53559601Sbostic #ifdef EBUSY 53659601Sbostic case EBUSY: /* Device busy */ 53759601Sbostic #endif 53859601Sbostic #ifdef EPROCLIM 53959601Sbostic case EPROCLIM: /* Too many processes */ 54059601Sbostic #endif 54159601Sbostic #ifdef EUSERS 54259601Sbostic case EUSERS: /* Too many users */ 54359601Sbostic #endif 54459601Sbostic #ifdef ECONNABORTED 54559601Sbostic case ECONNABORTED: /* Software caused connection abort */ 54659601Sbostic #endif 54759601Sbostic #ifdef ECONNREFUSED 54859601Sbostic case ECONNREFUSED: /* Connection refused */ 54959601Sbostic #endif 55059601Sbostic #ifdef ECONNRESET 55159601Sbostic case ECONNRESET: /* Connection reset by peer */ 55259601Sbostic #endif 55359601Sbostic #ifdef EDEADLK 55459601Sbostic case EDEADLK: /* Resource deadlock avoided */ 55559601Sbostic #endif 55659601Sbostic #ifdef EFBIG 55759601Sbostic case EFBIG: /* File too large */ 55859601Sbostic #endif 55959601Sbostic #ifdef EHOSTDOWN 56059601Sbostic case EHOSTDOWN: /* Host is down */ 56159601Sbostic #endif 56259601Sbostic #ifdef EHOSTUNREACH 56359601Sbostic case EHOSTUNREACH: /* No route to host */ 56459601Sbostic #endif 56559601Sbostic #ifdef EMFILE 56659601Sbostic case EMFILE: /* Too many open files */ 56759601Sbostic #endif 56859601Sbostic #ifdef ENETDOWN 56959601Sbostic case ENETDOWN: /* Network is down */ 57059601Sbostic #endif 57159601Sbostic #ifdef ENETRESET 57259601Sbostic case ENETRESET: /* Network dropped connection on reset */ 57359601Sbostic #endif 57459601Sbostic #ifdef ENETUNREACH 57559601Sbostic case ENETUNREACH: /* Network is unreachable */ 57659601Sbostic #endif 57759601Sbostic #ifdef ENFILE 57859601Sbostic case ENFILE: /* Too many open files in system */ 57959601Sbostic #endif 58059601Sbostic #ifdef ENOBUFS 58159601Sbostic case ENOBUFS: /* No buffer space available */ 58259601Sbostic #endif 58359601Sbostic #ifdef ENOMEM 58459601Sbostic case ENOMEM: /* Cannot allocate memory */ 58559601Sbostic #endif 58659601Sbostic #ifdef ENOSPC 58759601Sbostic case ENOSPC: /* No space left on device */ 58859601Sbostic #endif 58959601Sbostic #ifdef EROFS 59059601Sbostic case EROFS: /* Read-only file system */ 59159601Sbostic #endif 59259601Sbostic #ifdef ESTALE 59359601Sbostic case ESTALE: /* Stale NFS file handle */ 59459601Sbostic #endif 59559601Sbostic #ifdef ETIMEDOUT 59659601Sbostic case ETIMEDOUT: /* Connection timed out */ 59759601Sbostic #endif 59867830Seric #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN && EWOULDBLOCK != EDEADLK 59959601Sbostic case EWOULDBLOCK: /* Operation would block. */ 60059601Sbostic #endif 60159601Sbostic eval = EX_TEMPFAIL; 60259601Sbostic break; 60359601Sbostic default: 60459601Sbostic eval = EX_UNAVAILABLE; 60559601Sbostic break; 60659601Sbostic } 60759601Sbostic } 60867830Seric 60967830Seric #ifndef BSD4_4 61067830Seric 61167830Seric char * 61267830Seric strerror(eno) 61367830Seric int eno; 61467830Seric { 61567830Seric extern int sys_nerr; 61667830Seric extern char *sys_errlist[]; 61767830Seric static char ebuf[60]; 61867830Seric 61967830Seric if (eno >= 0 && eno <= sys_nerr) 62067830Seric return sys_errlist[eno]; 62167830Seric (void) sprintf(ebuf, "Error %d", eno); 62267830Seric return ebuf; 62367830Seric } 62467830Seric 62567830Seric #if __STDC__ 62667830Seric snprintf(char *buf, int bufsiz, const char *fmt, ...) 62767830Seric #else 62867830Seric snprintf(buf, bufsiz, fmt, va_alist) 62967830Seric char *buf; 63067830Seric int bufsiz; 63167830Seric const char *fmt; 63267830Seric va_dcl 63367830Seric #endif 63467830Seric { 63567830Seric va_list ap; 63667830Seric 63767830Seric #if __STDC__ 63867830Seric va_start(ap, fmt); 63967830Seric #else 64067830Seric va_start(ap); 64167830Seric #endif 64267830Seric vsprintf(buf, fmt, ap); 64367830Seric va_end(ap); 64467830Seric } 64567830Seric 64667830Seric #endif 64767830Seric 64867830Seric #ifdef ultrix 64967830Seric 65067830Seric int 65167830Seric mkstemp(template) 65267830Seric char *template; 65367830Seric { 65467830Seric int fd; 65567830Seric 65667830Seric return open(mktemp(template), O_RDWR|O_CREAT|O_EXCL, 0600); 65767830Seric } 65867830Seric 65967830Seric #endif 660