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*68005Seric static char sccsid[] = "@(#)mail.local.c 8.8 (Berkeley) 11/29/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: 254*68005Seric 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); 264*68005Seric 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); 270*68005Seric 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); 280*68005Seric unlockmbox(); 28166733Sbostic return; 28266733Sbostic } 28366733Sbostic } 28459601Sbostic 28559601Sbostic if (mbfd == -1) { 28659601Sbostic e_to_sys(errno); 28759601Sbostic warn("%s: %s", path, strerror(errno)); 288*68005Seric 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)); 296*68005Seric 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); 30257648Sbostic (void)snprintf(biffmsg, sizeof(biffmsg), "%s@%qd\n", name, curoff); 30359601Sbostic 30459601Sbostic /* Copy the message into the file. */ 30554203Sbostic if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) { 30659601Sbostic e_to_sys(errno); 30759601Sbostic warn("temporary file: %s", strerror(errno)); 30859601Sbostic goto err1; 30945976Sbostic } 31045976Sbostic while ((nr = read(fd, buf, sizeof(buf))) > 0) 31145976Sbostic for (off = 0; off < nr; nr -= nw, off += nw) 31245976Sbostic if ((nw = write(mbfd, buf + off, nr)) < 0) { 31359601Sbostic e_to_sys(errno); 31459601Sbostic warn("%s: %s", path, strerror(errno)); 31559601Sbostic goto err2;; 31645976Sbostic } 31745976Sbostic if (nr < 0) { 31859601Sbostic e_to_sys(errno); 31959601Sbostic warn("temporary file: %s", strerror(errno)); 32059601Sbostic goto err2;; 321579Sroot } 322579Sroot 32359601Sbostic /* Flush to disk, don't wait for update. */ 32459601Sbostic if (fsync(mbfd)) { 32559601Sbostic e_to_sys(errno); 32659601Sbostic warn("%s: %s", path, strerror(errno)); 32759601Sbostic err2: (void)ftruncate(mbfd, curoff); 32859601Sbostic err1: (void)close(mbfd); 329*68005Seric unlockmbox(); 33059601Sbostic return; 33159601Sbostic } 33259601Sbostic 33359601Sbostic /* Close and check -- NFS doesn't write until the close. */ 33459601Sbostic if (close(mbfd)) { 33559601Sbostic e_to_sys(errno); 33659601Sbostic warn("%s: %s", path, strerror(errno)); 337*68005Seric unlockmbox(); 33859601Sbostic return; 33959601Sbostic } 340579Sroot 341*68005Seric unlockmbox(); 34259601Sbostic notifybiff(biffmsg); 343579Sroot } 344579Sroot 345*68005Seric /* 346*68005Seric * user.lock files are necessary for compatibility with other 347*68005Seric * systems, e.g., when the mail spool file is NFS exported. 348*68005Seric * Alas, mailbox locking is more than just a local matter. 349*68005Seric * EPA 11/94. 350*68005Seric */ 351*68005Seric 352*68005Seric char lockname[50]; 353*68005Seric int locked = 0; 354*68005Seric 355*68005Seric lockmbox(path) 356*68005Seric char *path; 357*68005Seric { 358*68005Seric char locktmp[50]; 359*68005Seric int statfailed = 0; 360*68005Seric 361*68005Seric if (locked) 362*68005Seric return; 363*68005Seric sprintf(lockname, "%s.lock", path); 364*68005Seric sprintf(locktmp, "%s/tmXXXXXX", _PATH_MAILDIR); 365*68005Seric mktemp(locktmp); 366*68005Seric unlink(locktmp); 367*68005Seric for (;; sleep(5)) { 368*68005Seric int fd; 369*68005Seric struct stat st; 370*68005Seric time_t now; 371*68005Seric 372*68005Seric fd = creat(locktmp, 0); 373*68005Seric if (fd < 0) 374*68005Seric continue; 375*68005Seric close(fd); 376*68005Seric if (link(locktmp, lockname) >= 0) { 377*68005Seric unlink(locktmp); 378*68005Seric locked = 1; 379*68005Seric return; 380*68005Seric } 381*68005Seric unlink(locktmp); 382*68005Seric if (stat(lockname, &st) < 0) { 383*68005Seric if (statfailed++ > 5) 384*68005Seric return; 385*68005Seric continue; 386*68005Seric } 387*68005Seric statfailed = 0; 388*68005Seric time(&now); 389*68005Seric if (now < st.st_ctime + 300) 390*68005Seric continue; 391*68005Seric unlink(lockname); 392*68005Seric } 393*68005Seric } 394*68005Seric 395*68005Seric unlockmbox() 396*68005Seric { 397*68005Seric if (!locked) 398*68005Seric return; 399*68005Seric unlink(lockname); 400*68005Seric locked = 0; 401*68005Seric } 402*68005Seric 40350092Sbostic void 40416731Sralph notifybiff(msg) 40516731Sralph char *msg; 40616731Sralph { 40716731Sralph static struct sockaddr_in addr; 40816731Sralph static int f = -1; 40945976Sbostic struct hostent *hp; 41045976Sbostic struct servent *sp; 41145976Sbostic int len; 41216731Sralph 41345976Sbostic if (!addr.sin_family) { 41445976Sbostic /* Be silent if biff service not available. */ 41545976Sbostic if (!(sp = getservbyname("biff", "udp"))) 41645976Sbostic return; 41745976Sbostic if (!(hp = gethostbyname("localhost"))) { 41859601Sbostic warn("localhost: %s", strerror(errno)); 41945976Sbostic return; 42016731Sralph } 42145976Sbostic addr.sin_family = hp->h_addrtype; 42267830Seric memcpy(&addr.sin_addr, hp->h_addr, hp->h_length); 42345976Sbostic addr.sin_port = sp->s_port; 42416731Sralph } 42545976Sbostic if (f < 0 && (f = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 42659601Sbostic warn("socket: %s", strerror(errno)); 42745976Sbostic return; 42816731Sralph } 42945976Sbostic len = strlen(msg) + 1; 43046672Sbostic if (sendto(f, msg, len, 0, (struct sockaddr *)&addr, sizeof(addr)) 43146672Sbostic != len) 43259601Sbostic warn("sendto biff: %s", strerror(errno)); 43316731Sralph } 43416731Sralph 43550092Sbostic void 43645976Sbostic usage() 437579Sroot { 43859601Sbostic eval = EX_USAGE; 43959601Sbostic err("usage: mail.local [-f from] user ..."); 440579Sroot } 441579Sroot 44250092Sbostic #if __STDC__ 44365558Sbostic void 44459601Sbostic err(const char *fmt, ...) 44550092Sbostic #else 44664993Smckusick void 44759601Sbostic err(fmt, va_alist) 44859601Sbostic const char *fmt; 44959601Sbostic va_dcl 45050092Sbostic #endif 45159601Sbostic { 45259601Sbostic va_list ap; 45350092Sbostic 45459601Sbostic #if __STDC__ 45559601Sbostic va_start(ap, fmt); 45659601Sbostic #else 45759601Sbostic va_start(ap); 45859601Sbostic #endif 45959601Sbostic vwarn(fmt, ap); 46059601Sbostic va_end(ap); 46159601Sbostic 46259601Sbostic exit(eval); 46359601Sbostic } 46459601Sbostic 46550092Sbostic void 46650092Sbostic #if __STDC__ 46759601Sbostic warn(const char *fmt, ...) 46850092Sbostic #else 46959601Sbostic warn(fmt, va_alist) 47059601Sbostic const char *fmt; 47150092Sbostic va_dcl 47250092Sbostic #endif 473579Sroot { 47445976Sbostic va_list ap; 47559601Sbostic 47650092Sbostic #if __STDC__ 47746554Sbostic va_start(ap, fmt); 47850092Sbostic #else 47950092Sbostic va_start(ap); 48050092Sbostic #endif 48159601Sbostic vwarn(fmt, ap); 48259601Sbostic va_end(ap); 48359601Sbostic } 48459601Sbostic 48559601Sbostic void 48659601Sbostic vwarn(fmt, ap) 48759601Sbostic const char *fmt; 48859601Sbostic _BSD_VA_LIST_ ap; 48959601Sbostic { 49058400Sbostic /* 49159601Sbostic * Log the message to stderr. 49259601Sbostic * 49359601Sbostic * Don't use LOG_PERROR as an openlog() flag to do this, 49459601Sbostic * it's not portable enough. 49558400Sbostic */ 49659601Sbostic if (eval != EX_USAGE) 49759601Sbostic (void)fprintf(stderr, "mail.local: "); 49858396Sbostic (void)vfprintf(stderr, fmt, ap); 49958400Sbostic (void)fprintf(stderr, "\n"); 50059601Sbostic 50167830Seric #ifndef ultrix 50259601Sbostic /* Log the message to syslog. */ 50345976Sbostic vsyslog(LOG_ERR, fmt, ap); 50467830Seric #else 50567830Seric { 50667830Seric char fmtbuf[10240]; 50767830Seric 50867830Seric (void) sprintf(fmtbuf, fmt, ap); 50967830Seric syslog(LOG_ERR, "%s", fmtbuf); 51067830Seric } 51167830Seric #endif 512579Sroot } 51359601Sbostic 51459601Sbostic /* 51559601Sbostic * e_to_sys -- 51659601Sbostic * Guess which errno's are temporary. Gag me. 51759601Sbostic */ 51859601Sbostic void 51959601Sbostic e_to_sys(num) 52059601Sbostic int num; 52159601Sbostic { 52259601Sbostic /* Temporary failures override hard errors. */ 52359601Sbostic if (eval == EX_TEMPFAIL) 52459601Sbostic return; 52559601Sbostic 52659601Sbostic switch(num) { /* Hopefully temporary errors. */ 52759601Sbostic #ifdef EAGAIN 52859601Sbostic case EAGAIN: /* Resource temporarily unavailable */ 52959601Sbostic #endif 53059601Sbostic #ifdef EDQUOT 53159601Sbostic case EDQUOT: /* Disc quota exceeded */ 53259601Sbostic #endif 53359601Sbostic #ifdef EBUSY 53459601Sbostic case EBUSY: /* Device busy */ 53559601Sbostic #endif 53659601Sbostic #ifdef EPROCLIM 53759601Sbostic case EPROCLIM: /* Too many processes */ 53859601Sbostic #endif 53959601Sbostic #ifdef EUSERS 54059601Sbostic case EUSERS: /* Too many users */ 54159601Sbostic #endif 54259601Sbostic #ifdef ECONNABORTED 54359601Sbostic case ECONNABORTED: /* Software caused connection abort */ 54459601Sbostic #endif 54559601Sbostic #ifdef ECONNREFUSED 54659601Sbostic case ECONNREFUSED: /* Connection refused */ 54759601Sbostic #endif 54859601Sbostic #ifdef ECONNRESET 54959601Sbostic case ECONNRESET: /* Connection reset by peer */ 55059601Sbostic #endif 55159601Sbostic #ifdef EDEADLK 55259601Sbostic case EDEADLK: /* Resource deadlock avoided */ 55359601Sbostic #endif 55459601Sbostic #ifdef EFBIG 55559601Sbostic case EFBIG: /* File too large */ 55659601Sbostic #endif 55759601Sbostic #ifdef EHOSTDOWN 55859601Sbostic case EHOSTDOWN: /* Host is down */ 55959601Sbostic #endif 56059601Sbostic #ifdef EHOSTUNREACH 56159601Sbostic case EHOSTUNREACH: /* No route to host */ 56259601Sbostic #endif 56359601Sbostic #ifdef EMFILE 56459601Sbostic case EMFILE: /* Too many open files */ 56559601Sbostic #endif 56659601Sbostic #ifdef ENETDOWN 56759601Sbostic case ENETDOWN: /* Network is down */ 56859601Sbostic #endif 56959601Sbostic #ifdef ENETRESET 57059601Sbostic case ENETRESET: /* Network dropped connection on reset */ 57159601Sbostic #endif 57259601Sbostic #ifdef ENETUNREACH 57359601Sbostic case ENETUNREACH: /* Network is unreachable */ 57459601Sbostic #endif 57559601Sbostic #ifdef ENFILE 57659601Sbostic case ENFILE: /* Too many open files in system */ 57759601Sbostic #endif 57859601Sbostic #ifdef ENOBUFS 57959601Sbostic case ENOBUFS: /* No buffer space available */ 58059601Sbostic #endif 58159601Sbostic #ifdef ENOMEM 58259601Sbostic case ENOMEM: /* Cannot allocate memory */ 58359601Sbostic #endif 58459601Sbostic #ifdef ENOSPC 58559601Sbostic case ENOSPC: /* No space left on device */ 58659601Sbostic #endif 58759601Sbostic #ifdef EROFS 58859601Sbostic case EROFS: /* Read-only file system */ 58959601Sbostic #endif 59059601Sbostic #ifdef ESTALE 59159601Sbostic case ESTALE: /* Stale NFS file handle */ 59259601Sbostic #endif 59359601Sbostic #ifdef ETIMEDOUT 59459601Sbostic case ETIMEDOUT: /* Connection timed out */ 59559601Sbostic #endif 59667830Seric #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN && EWOULDBLOCK != EDEADLK 59759601Sbostic case EWOULDBLOCK: /* Operation would block. */ 59859601Sbostic #endif 59959601Sbostic eval = EX_TEMPFAIL; 60059601Sbostic break; 60159601Sbostic default: 60259601Sbostic eval = EX_UNAVAILABLE; 60359601Sbostic break; 60459601Sbostic } 60559601Sbostic } 60667830Seric 60767830Seric #ifndef BSD4_4 60867830Seric 60967830Seric char * 61067830Seric strerror(eno) 61167830Seric int eno; 61267830Seric { 61367830Seric extern int sys_nerr; 61467830Seric extern char *sys_errlist[]; 61567830Seric static char ebuf[60]; 61667830Seric 61767830Seric if (eno >= 0 && eno <= sys_nerr) 61867830Seric return sys_errlist[eno]; 61967830Seric (void) sprintf(ebuf, "Error %d", eno); 62067830Seric return ebuf; 62167830Seric } 62267830Seric 62367830Seric #if __STDC__ 62467830Seric snprintf(char *buf, int bufsiz, const char *fmt, ...) 62567830Seric #else 62667830Seric snprintf(buf, bufsiz, fmt, va_alist) 62767830Seric char *buf; 62867830Seric int bufsiz; 62967830Seric const char *fmt; 63067830Seric va_dcl 63167830Seric #endif 63267830Seric { 63367830Seric va_list ap; 63467830Seric 63567830Seric #if __STDC__ 63667830Seric va_start(ap, fmt); 63767830Seric #else 63867830Seric va_start(ap); 63967830Seric #endif 64067830Seric vsprintf(buf, fmt, ap); 64167830Seric va_end(ap); 64267830Seric } 64367830Seric 64467830Seric #endif 64567830Seric 64667830Seric #ifdef ultrix 64767830Seric 64867830Seric int 64967830Seric mkstemp(template) 65067830Seric char *template; 65167830Seric { 65267830Seric int fd; 65367830Seric 65467830Seric return open(mktemp(template), O_RDWR|O_CREAT|O_EXCL, 0600); 65567830Seric } 65667830Seric 65767830Seric #endif 658