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*69950Seric static char sccsid[] = "@(#)mail.local.c 8.21 (Berkeley) 06/20/95"; 1645976Sbostic #endif /* not lint */ 1745976Sbostic 1868202Seric /* 1968202Seric * This is not intended to compile on System V derived systems 2068202Seric * such as Solaris or HP-UX, since they use a totally different 2168202Seric * approach to mailboxes (essentially, they have a setgid program 2268202Seric * rather than setuid, and they rely on the ability to "give away" 2368202Seric * files to do their work). IT IS NOT A BUG that this doesn't 2468202Seric * compile on such architectures. 2568202Seric */ 2668202Seric 2729763Skarels #include <sys/param.h> 2816731Sralph #include <sys/stat.h> 2945976Sbostic #include <sys/socket.h> 3057648Sbostic 3145976Sbostic #include <netinet/in.h> 3257648Sbostic 3357648Sbostic #include <errno.h> 3446672Sbostic #include <fcntl.h> 3545976Sbostic #include <netdb.h> 3645976Sbostic #include <pwd.h> 37579Sroot #include <stdio.h> 3846672Sbostic #include <stdlib.h> 3945976Sbostic #include <string.h> 4059601Sbostic #include <sysexits.h> 4157648Sbostic #include <syslog.h> 4257648Sbostic #include <time.h> 4357648Sbostic #include <unistd.h> 4468201Seric #include <ctype.h> 4557648Sbostic 4659601Sbostic #if __STDC__ 4759601Sbostic #include <stdarg.h> 4859601Sbostic #else 4959601Sbostic #include <varargs.h> 5059601Sbostic #endif 5159601Sbostic 5267830Seric #ifndef LOCK_EX 5367830Seric # include <sys/file.h> 5467830Seric #endif 55579Sroot 5667830Seric #ifdef BSD4_4 5767830Seric # include "pathnames.h" 5867830Seric #endif 5967830Seric 6067830Seric #ifndef __P 6167830Seric # ifdef __STDC__ 6267830Seric # define __P(protos) protos 6367830Seric # else 6467830Seric # define __P(protos) () 6567830Seric # define const 6667830Seric # endif 6767830Seric #endif 6867830Seric #ifndef __dead 6967830Seric # if defined(__GNUC__) && (__GNUC__ < 2 || __GNUC_MINOR__ < 5) && !defined(__STRICT_ANSI__) 7067830Seric # define __dead __volatile 7167830Seric # else 7267830Seric # define __dead 7367830Seric # endif 7467830Seric #endif 7567830Seric 7667830Seric #ifndef BSD4_4 7767830Seric # define _BSD_VA_LIST_ va_list 7867830Seric extern char *strerror __P((int)); 7969603Seric extern int snprintf __P((char *, int, const char *, ...)); 8067830Seric #endif 8167830Seric 8269603Seric /* 8369604Seric * If you don't have setreuid, and you have saved uids, and you have 8469604Seric * a seteuid() call that doesn't try to emulate using setuid(), then 8569604Seric * you can try defining USE_SETEUID. 8669603Seric */ 8769604Seric #ifdef USE_SETEUID 8869604Seric # define setreuid(r, e) seteuid(e) 8969603Seric #endif 9069603Seric 9167830Seric #ifndef _PATH_LOCTMP 9267830Seric # define _PATH_LOCTMP "/tmp/local.XXXXXX" 9367830Seric #endif 9467830Seric #ifndef _PATH_MAILDIR 9567830Seric # define _PATH_MAILDIR "/var/spool/mail" 9667830Seric #endif 9767830Seric 9868188Seric #ifndef S_ISREG 9968188Seric # define S_ISREG(mode) (((mode) & _S_IFMT) == S_IFREG) 10067830Seric #endif 10167830Seric 10259601Sbostic int eval = EX_OK; /* sysexits.h error value. */ 103579Sroot 10459601Sbostic void deliver __P((int, char *)); 10559601Sbostic void e_to_sys __P((int)); 10659601Sbostic __dead void err __P((const char *, ...)); 10759601Sbostic void notifybiff __P((char *)); 10859601Sbostic int store __P((char *)); 10959601Sbostic void usage __P((void)); 11059601Sbostic void vwarn __P((const char *, _BSD_VA_LIST_)); 11159601Sbostic void warn __P((const char *, ...)); 11250092Sbostic 11357648Sbostic int 114579Sroot main(argc, argv) 11545976Sbostic int argc; 11659601Sbostic char *argv[]; 117579Sroot { 11845976Sbostic struct passwd *pw; 11959601Sbostic int ch, fd; 12045976Sbostic uid_t uid; 12145976Sbostic char *from; 12267830Seric extern char *optarg; 12367830Seric extern int optind; 124579Sroot 12568190Seric /* make sure we have some open file descriptors */ 12668190Seric for (fd = 10; fd < 30; fd++) 12768190Seric (void) close(fd); 12868190Seric 12968190Seric /* use a reasonable umask */ 13068191Seric (void) umask(0077); 13168190Seric 13267830Seric #ifdef LOG_MAIL 13358400Sbostic openlog("mail.local", 0, LOG_MAIL); 13467830Seric #else 13567830Seric openlog("mail.local", 0); 13667830Seric #endif 13716731Sralph 13845976Sbostic from = NULL; 13945976Sbostic while ((ch = getopt(argc, argv, "df:r:")) != EOF) 14045976Sbostic switch(ch) { 14159601Sbostic case 'd': /* Backward compatible. */ 14216731Sralph break; 14316731Sralph case 'f': 14459601Sbostic case 'r': /* Backward compatible. */ 14559601Sbostic if (from != NULL) { 14659601Sbostic warn("multiple -f options"); 14759601Sbostic usage(); 14859601Sbostic } 14945976Sbostic from = optarg; 150579Sroot break; 15145976Sbostic case '?': 15216731Sralph default: 15345976Sbostic usage(); 15416731Sralph } 15545976Sbostic argc -= optind; 15645976Sbostic argv += optind; 157579Sroot 15845976Sbostic if (!*argv) 15945976Sbostic usage(); 160579Sroot 16145976Sbostic /* 16245976Sbostic * If from not specified, use the name from getlogin() if the 16345976Sbostic * uid matches, otherwise, use the name from the password file 16445976Sbostic * corresponding to the uid. 16545976Sbostic */ 16645976Sbostic uid = getuid(); 16746034Sbostic if (!from && (!(from = getlogin()) || 16846034Sbostic !(pw = getpwnam(from)) || pw->pw_uid != uid)) 16945976Sbostic from = (pw = getpwuid(uid)) ? pw->pw_name : "???"; 170579Sroot 17159601Sbostic /* 17259601Sbostic * There is no way to distinguish the error status of one delivery 17359601Sbostic * from the rest of the deliveries. So, if we failed hard on one 17459601Sbostic * or more deliveries, but had no failures on any of the others, we 17559601Sbostic * return a hard failure. If we failed temporarily on one or more 17659601Sbostic * deliveries, we return a temporary failure regardless of the other 17759601Sbostic * failures. This results in the delivery being reattempted later 17859601Sbostic * at the expense of repeated failures and multiple deliveries. 17959601Sbostic */ 18059601Sbostic for (fd = store(from); *argv; ++argv) 18159601Sbostic deliver(fd, *argv); 18245976Sbostic exit(eval); 183579Sroot } 184579Sroot 18557648Sbostic int 18645976Sbostic store(from) 18745976Sbostic char *from; 188579Sroot { 18945976Sbostic FILE *fp; 19045976Sbostic time_t tval; 19145976Sbostic int fd, eline; 19267830Seric char line[2048]; 19367830Seric char tmpbuf[sizeof _PATH_LOCTMP + 1]; 194579Sroot 19567830Seric strcpy(tmpbuf, _PATH_LOCTMP); 19667830Seric if ((fd = mkstemp(tmpbuf)) == -1 || (fp = fdopen(fd, "w+")) == NULL) { 19759601Sbostic e_to_sys(errno); 19859601Sbostic err("unable to open temporary file"); 19959601Sbostic } 20067830Seric (void)unlink(tmpbuf); 201579Sroot 20245976Sbostic (void)time(&tval); 20345976Sbostic (void)fprintf(fp, "From %s %s", from, ctime(&tval)); 204579Sroot 20545976Sbostic line[0] = '\0'; 20645976Sbostic for (eline = 1; fgets(line, sizeof(line), stdin);) { 20745976Sbostic if (line[0] == '\n') 20845976Sbostic eline = 1; 20945976Sbostic else { 21060097Sbostic if (eline && line[0] == 'F' && 21160097Sbostic !memcmp(line, "From ", 5)) 21245976Sbostic (void)putc('>', fp); 21345976Sbostic eline = 0; 21445976Sbostic } 21545976Sbostic (void)fprintf(fp, "%s", line); 21659601Sbostic if (ferror(fp)) { 21759601Sbostic e_to_sys(errno); 21859601Sbostic err("temporary file write error"); 21959601Sbostic } 220579Sroot } 221579Sroot 22245976Sbostic /* If message not newline terminated, need an extra. */ 22359601Sbostic if (!strchr(line, '\n')) 22445976Sbostic (void)putc('\n', fp); 22545976Sbostic /* Output a newline; note, empty messages are allowed. */ 22645976Sbostic (void)putc('\n', fp); 22711893Seric 22859601Sbostic if (fflush(fp) == EOF || ferror(fp)) { 22959601Sbostic e_to_sys(errno); 23059601Sbostic err("temporary file write error"); 23159601Sbostic } 23259601Sbostic return (fd); 233579Sroot } 234579Sroot 23559601Sbostic void 23645976Sbostic deliver(fd, name) 23745976Sbostic int fd; 23845976Sbostic char *name; 239579Sroot { 24066735Sbostic struct stat fsb, sb; 24145976Sbostic struct passwd *pw; 24259601Sbostic int mbfd, nr, nw, off; 24368201Seric char *p; 24445976Sbostic char biffmsg[100], buf[8*1024], path[MAXPATHLEN]; 24554203Sbostic off_t curoff; 246579Sroot 24745976Sbostic /* 24845976Sbostic * Disallow delivery to unknown names -- special mailboxes can be 24945976Sbostic * handled in the sendmail aliases file. 25045976Sbostic */ 25145976Sbostic if (!(pw = getpwnam(name))) { 25259601Sbostic if (eval != EX_TEMPFAIL) 25359601Sbostic eval = EX_UNAVAILABLE; 25459601Sbostic warn("unknown name: %s", name); 25559601Sbostic return; 25645976Sbostic } 257579Sroot 25868201Seric /* 25968201Seric * Keep name reasonably short to avoid buffer overruns. 26068201Seric * This isn't necessary on BSD because of the proper 26168201Seric * definition of snprintf(), but it can cause problems 26268201Seric * on other systems. 26368201Seric * Also, clear out any bogus characters. 26468201Seric */ 26568201Seric 26668201Seric if (strlen(name) > 40) 26768201Seric name[40] = '\0'; 26868201Seric for (p = name; *p != '\0'; p++) 26968201Seric { 27068201Seric if (!isascii(*p)) 27168201Seric *p &= 0x7f; 27268201Seric else if (!isprint(*p)) 27368201Seric *p = '.'; 27468201Seric } 27568201Seric 27657648Sbostic (void)snprintf(path, sizeof(path), "%s/%s", _PATH_MAILDIR, name); 277579Sroot 278579Sroot /* 27966733Sbostic * If the mailbox is linked or a symlink, fail. There's an obvious 28066733Sbostic * race here, that the file was replaced with a symbolic link after 28166735Sbostic * the lstat returned, but before the open. We attempt to detect 28266735Sbostic * this by comparing the original stat information and information 28366735Sbostic * returned by an fstat of the file descriptor returned by the open. 28459601Sbostic * 28566735Sbostic * NB: this is a symptom of a larger problem, that the mail spooling 28666735Sbostic * directory is writeable by the wrong users. If that directory is 28766735Sbostic * writeable, system security is compromised for other reasons, and 28866735Sbostic * it cannot be fixed here. 28966735Sbostic * 29059601Sbostic * If we created the mailbox, set the owner/group. If that fails, 29159601Sbostic * just return. Another process may have already opened it, so we 29259601Sbostic * can't unlink it. Historically, binmail set the owner/group at 29359601Sbostic * each mail delivery. We no longer do this, assuming that if the 29459601Sbostic * ownership or permissions were changed there was a reason. 29559601Sbostic * 29659601Sbostic * XXX 29759601Sbostic * open(2) should support flock'ing the file. 298579Sroot */ 29966736Sbostic tryagain: 30068005Seric lockmbox(path); 30159601Sbostic if (lstat(path, &sb)) { 30266736Sbostic mbfd = open(path, 30366736Sbostic O_APPEND|O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR); 30466736Sbostic if (mbfd == -1) { 30566736Sbostic if (errno == EEXIST) 30666736Sbostic goto tryagain; 30766736Sbostic } else if (fchown(mbfd, pw->pw_uid, pw->pw_gid)) { 30859601Sbostic e_to_sys(errno); 30959601Sbostic warn("chown %u.%u: %s", pw->pw_uid, pw->pw_gid, name); 31069603Seric goto err1; 31159601Sbostic } 31268188Seric } else if (sb.st_nlink != 1 || !S_ISREG(sb.st_mode)) { 31359601Sbostic e_to_sys(errno); 31468188Seric warn("%s: irregular file", path); 31569603Seric goto err0; 31668060Seric } else if (sb.st_uid != pw->pw_uid) { 31768060Seric warn("%s: wrong ownership (%d)", path, sb.st_uid); 31868060Seric unlockmbox(); 31968060Seric return; 32066733Sbostic } else { 32159601Sbostic mbfd = open(path, O_APPEND|O_WRONLY, 0); 32266735Sbostic if (mbfd != -1 && 32366735Sbostic (fstat(mbfd, &fsb) || fsb.st_nlink != 1 || 32468188Seric !S_ISREG(fsb.st_mode) || sb.st_dev != fsb.st_dev || 32568188Seric sb.st_ino != fsb.st_ino || sb.st_uid != fsb.st_uid)) { 32666735Sbostic warn("%s: file changed after open", path); 32769603Seric goto err1; 32866733Sbostic } 32966733Sbostic } 33059601Sbostic 33159601Sbostic if (mbfd == -1) { 33259601Sbostic e_to_sys(errno); 33359601Sbostic warn("%s: %s", path, strerror(errno)); 33469603Seric goto err0; 33545976Sbostic } 336579Sroot 33759601Sbostic /* Wait until we can get a lock on the file. */ 33845976Sbostic if (flock(mbfd, LOCK_EX)) { 33959601Sbostic e_to_sys(errno); 34059601Sbostic warn("%s: %s", path, strerror(errno)); 34159601Sbostic goto err1; 34245976Sbostic } 343579Sroot 34459601Sbostic /* Get the starting offset of the new message for biff. */ 34554203Sbostic curoff = lseek(mbfd, (off_t)0, SEEK_END); 34668021Seric (void)snprintf(biffmsg, sizeof(biffmsg), 34768021Seric sizeof curoff > sizeof(long) ? "%s@%qd\n" : "%s@%ld\n", 34868021Seric name, curoff); 34959601Sbostic 35059601Sbostic /* Copy the message into the file. */ 35154203Sbostic if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) { 35259601Sbostic e_to_sys(errno); 35359601Sbostic warn("temporary file: %s", strerror(errno)); 35459601Sbostic goto err1; 35545976Sbostic } 35669603Seric if (setreuid(0, pw->pw_uid) < 0) { 35769603Seric e_to_sys(errno); 35869603Seric warn("setreuid(0, %d): %s (r=%d, e=%d)", 35969603Seric pw->pw_uid, strerror(errno), getuid(), geteuid()); 36069603Seric goto err1; 36169603Seric } 36269603Seric #ifdef DEBUG 36369603Seric printf("new euid = %d\n", geteuid()); 36469603Seric #endif 36545976Sbostic while ((nr = read(fd, buf, sizeof(buf))) > 0) 36668201Seric for (off = 0; off < nr; off += nw) 36768201Seric if ((nw = write(mbfd, buf + off, nr - off)) < 0) { 36859601Sbostic e_to_sys(errno); 36959601Sbostic warn("%s: %s", path, strerror(errno)); 37069603Seric goto err3; 37145976Sbostic } 37245976Sbostic if (nr < 0) { 37359601Sbostic e_to_sys(errno); 37459601Sbostic warn("temporary file: %s", strerror(errno)); 37569603Seric goto err3; 376579Sroot } 377579Sroot 37859601Sbostic /* Flush to disk, don't wait for update. */ 37959601Sbostic if (fsync(mbfd)) { 38059601Sbostic e_to_sys(errno); 38159601Sbostic warn("%s: %s", path, strerror(errno)); 38269603Seric err3: 38369603Seric if (setreuid(0, 0) < 0) { 38469603Seric e_to_sys(errno); 38569603Seric warn("setreuid(0, 0): %s", strerror(errno)); 38669603Seric } 38769604Seric #ifdef DEBUG 38869603Seric printf("reset euid = %d\n", geteuid()); 38969603Seric #endif 39059601Sbostic err2: (void)ftruncate(mbfd, curoff); 39159601Sbostic err1: (void)close(mbfd); 39269603Seric err0: unlockmbox(); 39359601Sbostic return; 39459601Sbostic } 39559601Sbostic 39659601Sbostic /* Close and check -- NFS doesn't write until the close. */ 39759601Sbostic if (close(mbfd)) { 39859601Sbostic e_to_sys(errno); 39959601Sbostic warn("%s: %s", path, strerror(errno)); 40068005Seric unlockmbox(); 40159601Sbostic return; 40259601Sbostic } 403579Sroot 40469603Seric if (setreuid(0, 0) < 0) { 40569603Seric e_to_sys(errno); 40669603Seric warn("setreuid(0, 0): %s", strerror(errno)); 40769603Seric } 40869604Seric #ifdef DEBUG 40969603Seric printf("reset euid = %d\n", geteuid()); 41069603Seric #endif 41168005Seric unlockmbox(); 41259601Sbostic notifybiff(biffmsg); 413579Sroot } 414579Sroot 41568005Seric /* 41668005Seric * user.lock files are necessary for compatibility with other 41768005Seric * systems, e.g., when the mail spool file is NFS exported. 41868005Seric * Alas, mailbox locking is more than just a local matter. 41968005Seric * EPA 11/94. 42068005Seric */ 42168005Seric 42268201Seric char lockname[MAXPATHLEN]; 42368005Seric int locked = 0; 42468005Seric 42568005Seric lockmbox(path) 42668005Seric char *path; 42768005Seric { 42868005Seric int statfailed = 0; 42968005Seric 43068005Seric if (locked) 43168005Seric return; 43268005Seric sprintf(lockname, "%s.lock", path); 43368005Seric for (;; sleep(5)) { 43468005Seric int fd; 43568005Seric struct stat st; 43668005Seric time_t now; 43768005Seric 43868187Seric fd = open(lockname, O_WRONLY|O_EXCL|O_CREAT, 0); 43968187Seric if (fd >= 0) { 44068005Seric locked = 1; 44168187Seric close(fd); 44268005Seric return; 44368005Seric } 44468005Seric if (stat(lockname, &st) < 0) { 44568005Seric if (statfailed++ > 5) 44668005Seric return; 44768005Seric continue; 44868005Seric } 44968005Seric statfailed = 0; 45068005Seric time(&now); 45168005Seric if (now < st.st_ctime + 300) 45268005Seric continue; 45368005Seric unlink(lockname); 45468005Seric } 45568005Seric } 45668005Seric 45768005Seric unlockmbox() 45868005Seric { 45968005Seric if (!locked) 46068005Seric return; 46168005Seric unlink(lockname); 46268005Seric locked = 0; 46368005Seric } 46468005Seric 46550092Sbostic void 46616731Sralph notifybiff(msg) 46716731Sralph char *msg; 46816731Sralph { 46916731Sralph static struct sockaddr_in addr; 47016731Sralph static int f = -1; 47145976Sbostic struct hostent *hp; 47245976Sbostic struct servent *sp; 47345976Sbostic int len; 47416731Sralph 47545976Sbostic if (!addr.sin_family) { 47645976Sbostic /* Be silent if biff service not available. */ 47745976Sbostic if (!(sp = getservbyname("biff", "udp"))) 47845976Sbostic return; 47945976Sbostic if (!(hp = gethostbyname("localhost"))) { 48059601Sbostic warn("localhost: %s", strerror(errno)); 48145976Sbostic return; 48216731Sralph } 48345976Sbostic addr.sin_family = hp->h_addrtype; 48467830Seric memcpy(&addr.sin_addr, hp->h_addr, hp->h_length); 48545976Sbostic addr.sin_port = sp->s_port; 48616731Sralph } 48745976Sbostic if (f < 0 && (f = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 48859601Sbostic warn("socket: %s", strerror(errno)); 48945976Sbostic return; 49016731Sralph } 49145976Sbostic len = strlen(msg) + 1; 49246672Sbostic if (sendto(f, msg, len, 0, (struct sockaddr *)&addr, sizeof(addr)) 49346672Sbostic != len) 49459601Sbostic warn("sendto biff: %s", strerror(errno)); 49516731Sralph } 49616731Sralph 49750092Sbostic void 49845976Sbostic usage() 499579Sroot { 50059601Sbostic eval = EX_USAGE; 50159601Sbostic err("usage: mail.local [-f from] user ..."); 502579Sroot } 503579Sroot 50450092Sbostic #if __STDC__ 50565558Sbostic void 50659601Sbostic err(const char *fmt, ...) 50750092Sbostic #else 50864993Smckusick void 50959601Sbostic err(fmt, va_alist) 51059601Sbostic const char *fmt; 51159601Sbostic va_dcl 51250092Sbostic #endif 51359601Sbostic { 51459601Sbostic va_list ap; 51550092Sbostic 51659601Sbostic #if __STDC__ 51759601Sbostic va_start(ap, fmt); 51859601Sbostic #else 51959601Sbostic va_start(ap); 52059601Sbostic #endif 52159601Sbostic vwarn(fmt, ap); 52259601Sbostic va_end(ap); 52359601Sbostic 52459601Sbostic exit(eval); 52559601Sbostic } 52659601Sbostic 52750092Sbostic void 52850092Sbostic #if __STDC__ 52959601Sbostic warn(const char *fmt, ...) 53050092Sbostic #else 53159601Sbostic warn(fmt, va_alist) 53259601Sbostic const char *fmt; 53350092Sbostic va_dcl 53450092Sbostic #endif 535579Sroot { 53645976Sbostic va_list ap; 53759601Sbostic 53850092Sbostic #if __STDC__ 53946554Sbostic va_start(ap, fmt); 54050092Sbostic #else 54150092Sbostic va_start(ap); 54250092Sbostic #endif 54359601Sbostic vwarn(fmt, ap); 54459601Sbostic va_end(ap); 54559601Sbostic } 54659601Sbostic 54759601Sbostic void 54859601Sbostic vwarn(fmt, ap) 54959601Sbostic const char *fmt; 55059601Sbostic _BSD_VA_LIST_ ap; 55159601Sbostic { 55258400Sbostic /* 55359601Sbostic * Log the message to stderr. 55459601Sbostic * 55559601Sbostic * Don't use LOG_PERROR as an openlog() flag to do this, 55659601Sbostic * it's not portable enough. 55758400Sbostic */ 55859601Sbostic if (eval != EX_USAGE) 55959601Sbostic (void)fprintf(stderr, "mail.local: "); 56058396Sbostic (void)vfprintf(stderr, fmt, ap); 56158400Sbostic (void)fprintf(stderr, "\n"); 56259601Sbostic 563*69950Seric #if !defined(ultrix) && !defined(__osf__) 56459601Sbostic /* Log the message to syslog. */ 56545976Sbostic vsyslog(LOG_ERR, fmt, ap); 56667830Seric #else 56767830Seric { 56867830Seric char fmtbuf[10240]; 56967830Seric 57067830Seric (void) sprintf(fmtbuf, fmt, ap); 57167830Seric syslog(LOG_ERR, "%s", fmtbuf); 57267830Seric } 57367830Seric #endif 574579Sroot } 57559601Sbostic 57659601Sbostic /* 57759601Sbostic * e_to_sys -- 57859601Sbostic * Guess which errno's are temporary. Gag me. 57959601Sbostic */ 58059601Sbostic void 58159601Sbostic e_to_sys(num) 58259601Sbostic int num; 58359601Sbostic { 58459601Sbostic /* Temporary failures override hard errors. */ 58559601Sbostic if (eval == EX_TEMPFAIL) 58659601Sbostic return; 58759601Sbostic 58859601Sbostic switch(num) { /* Hopefully temporary errors. */ 58959601Sbostic #ifdef EAGAIN 59059601Sbostic case EAGAIN: /* Resource temporarily unavailable */ 59159601Sbostic #endif 59259601Sbostic #ifdef EDQUOT 59359601Sbostic case EDQUOT: /* Disc quota exceeded */ 59459601Sbostic #endif 59559601Sbostic #ifdef EBUSY 59659601Sbostic case EBUSY: /* Device busy */ 59759601Sbostic #endif 59859601Sbostic #ifdef EPROCLIM 59959601Sbostic case EPROCLIM: /* Too many processes */ 60059601Sbostic #endif 60159601Sbostic #ifdef EUSERS 60259601Sbostic case EUSERS: /* Too many users */ 60359601Sbostic #endif 60459601Sbostic #ifdef ECONNABORTED 60559601Sbostic case ECONNABORTED: /* Software caused connection abort */ 60659601Sbostic #endif 60759601Sbostic #ifdef ECONNREFUSED 60859601Sbostic case ECONNREFUSED: /* Connection refused */ 60959601Sbostic #endif 61059601Sbostic #ifdef ECONNRESET 61159601Sbostic case ECONNRESET: /* Connection reset by peer */ 61259601Sbostic #endif 61359601Sbostic #ifdef EDEADLK 61459601Sbostic case EDEADLK: /* Resource deadlock avoided */ 61559601Sbostic #endif 61659601Sbostic #ifdef EFBIG 61759601Sbostic case EFBIG: /* File too large */ 61859601Sbostic #endif 61959601Sbostic #ifdef EHOSTDOWN 62059601Sbostic case EHOSTDOWN: /* Host is down */ 62159601Sbostic #endif 62259601Sbostic #ifdef EHOSTUNREACH 62359601Sbostic case EHOSTUNREACH: /* No route to host */ 62459601Sbostic #endif 62559601Sbostic #ifdef EMFILE 62659601Sbostic case EMFILE: /* Too many open files */ 62759601Sbostic #endif 62859601Sbostic #ifdef ENETDOWN 62959601Sbostic case ENETDOWN: /* Network is down */ 63059601Sbostic #endif 63159601Sbostic #ifdef ENETRESET 63259601Sbostic case ENETRESET: /* Network dropped connection on reset */ 63359601Sbostic #endif 63459601Sbostic #ifdef ENETUNREACH 63559601Sbostic case ENETUNREACH: /* Network is unreachable */ 63659601Sbostic #endif 63759601Sbostic #ifdef ENFILE 63859601Sbostic case ENFILE: /* Too many open files in system */ 63959601Sbostic #endif 64059601Sbostic #ifdef ENOBUFS 64159601Sbostic case ENOBUFS: /* No buffer space available */ 64259601Sbostic #endif 64359601Sbostic #ifdef ENOMEM 64459601Sbostic case ENOMEM: /* Cannot allocate memory */ 64559601Sbostic #endif 64659601Sbostic #ifdef ENOSPC 64759601Sbostic case ENOSPC: /* No space left on device */ 64859601Sbostic #endif 64959601Sbostic #ifdef EROFS 65059601Sbostic case EROFS: /* Read-only file system */ 65159601Sbostic #endif 65259601Sbostic #ifdef ESTALE 65359601Sbostic case ESTALE: /* Stale NFS file handle */ 65459601Sbostic #endif 65559601Sbostic #ifdef ETIMEDOUT 65659601Sbostic case ETIMEDOUT: /* Connection timed out */ 65759601Sbostic #endif 65867830Seric #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN && EWOULDBLOCK != EDEADLK 65959601Sbostic case EWOULDBLOCK: /* Operation would block. */ 66059601Sbostic #endif 66159601Sbostic eval = EX_TEMPFAIL; 66259601Sbostic break; 66359601Sbostic default: 66459601Sbostic eval = EX_UNAVAILABLE; 66559601Sbostic break; 66659601Sbostic } 66759601Sbostic } 66867830Seric 66967830Seric #ifndef BSD4_4 67067830Seric 671*69950Seric # ifndef __osf__ 67267830Seric char * 67367830Seric strerror(eno) 67467830Seric int eno; 67567830Seric { 67667830Seric extern int sys_nerr; 67767830Seric extern char *sys_errlist[]; 67867830Seric static char ebuf[60]; 67967830Seric 68067830Seric if (eno >= 0 && eno <= sys_nerr) 68167830Seric return sys_errlist[eno]; 68267830Seric (void) sprintf(ebuf, "Error %d", eno); 68367830Seric return ebuf; 68467830Seric } 685*69950Seric # endif 68667830Seric 687*69950Seric # if __STDC__ 68867830Seric snprintf(char *buf, int bufsiz, const char *fmt, ...) 689*69950Seric # else 69067830Seric snprintf(buf, bufsiz, fmt, va_alist) 69167830Seric char *buf; 69267830Seric int bufsiz; 69367830Seric const char *fmt; 69467830Seric va_dcl 695*69950Seric # endif 69667830Seric { 69767830Seric va_list ap; 69867830Seric 699*69950Seric # if __STDC__ 70067830Seric va_start(ap, fmt); 701*69950Seric # else 70267830Seric va_start(ap); 703*69950Seric # endif 70467830Seric vsprintf(buf, fmt, ap); 70567830Seric va_end(ap); 70667830Seric } 70767830Seric 70867830Seric #endif 70967830Seric 71067830Seric #ifdef ultrix 71167830Seric 71268189Seric /* 71368189Seric * Copyright (c) 1987, 1993 71468189Seric * The Regents of the University of California. All rights reserved. 71568189Seric * 71668189Seric * Redistribution and use in source and binary forms, with or without 71768189Seric * modification, are permitted provided that the following conditions 71868189Seric * are met: 71968189Seric * 1. Redistributions of source code must retain the above copyright 72068189Seric * notice, this list of conditions and the following disclaimer. 72168189Seric * 2. Redistributions in binary form must reproduce the above copyright 72268189Seric * notice, this list of conditions and the following disclaimer in the 72368189Seric * documentation and/or other materials provided with the distribution. 72468189Seric * 3. All advertising materials mentioning features or use of this software 72568189Seric * must display the following acknowledgement: 72668189Seric * This product includes software developed by the University of 72768189Seric * California, Berkeley and its contributors. 72868189Seric * 4. Neither the name of the University nor the names of its contributors 72968189Seric * may be used to endorse or promote products derived from this software 73068189Seric * without specific prior written permission. 73168189Seric * 73268189Seric * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 73368189Seric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 73468189Seric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 73568189Seric * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 73668189Seric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 73768189Seric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 73868189Seric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 73968189Seric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 74068189Seric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 74168189Seric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 74268189Seric * SUCH DAMAGE. 74368189Seric */ 74468189Seric 74568189Seric #if defined(LIBC_SCCS) && !defined(lint) 74668189Seric static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93"; 74768189Seric #endif /* LIBC_SCCS and not lint */ 74868189Seric 74968189Seric #include <sys/types.h> 75068189Seric #include <sys/stat.h> 75168189Seric #include <fcntl.h> 75268189Seric #include <errno.h> 75368189Seric #include <stdio.h> 75468189Seric #include <ctype.h> 75568189Seric 75668189Seric static int _gettemp(); 75768189Seric 75868189Seric mkstemp(path) 75968189Seric char *path; 76067830Seric { 76167830Seric int fd; 76267830Seric 76368189Seric return (_gettemp(path, &fd) ? fd : -1); 76467830Seric } 76567830Seric 76668189Seric /* 76768189Seric char * 76868189Seric mktemp(path) 76968189Seric char *path; 77068189Seric { 77168189Seric return(_gettemp(path, (int *)NULL) ? path : (char *)NULL); 77268189Seric } 77368189Seric */ 77468189Seric 77568189Seric static 77668189Seric _gettemp(path, doopen) 77768189Seric char *path; 77868189Seric register int *doopen; 77968189Seric { 78068189Seric extern int errno; 78168189Seric register char *start, *trv; 78268189Seric struct stat sbuf; 78368189Seric u_int pid; 78468189Seric 78568189Seric pid = getpid(); 78668189Seric for (trv = path; *trv; ++trv); /* extra X's get set to 0's */ 78768189Seric while (*--trv == 'X') { 78868189Seric *trv = (pid % 10) + '0'; 78968189Seric pid /= 10; 79068189Seric } 79168189Seric 79268189Seric /* 79368189Seric * check the target directory; if you have six X's and it 79468189Seric * doesn't exist this runs for a *very* long time. 79568189Seric */ 79668189Seric for (start = trv + 1;; --trv) { 79768189Seric if (trv <= path) 79868189Seric break; 79968189Seric if (*trv == '/') { 80068189Seric *trv = '\0'; 80168189Seric if (stat(path, &sbuf)) 80268189Seric return(0); 80368189Seric if (!S_ISDIR(sbuf.st_mode)) { 80468189Seric errno = ENOTDIR; 80568189Seric return(0); 80668189Seric } 80768189Seric *trv = '/'; 80868189Seric break; 80968189Seric } 81068189Seric } 81168189Seric 81268189Seric for (;;) { 81368189Seric if (doopen) { 81468189Seric if ((*doopen = 81568189Seric open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0) 81668189Seric return(1); 81768189Seric if (errno != EEXIST) 81868189Seric return(0); 81968189Seric } 82068189Seric else if (stat(path, &sbuf)) 82168189Seric return(errno == ENOENT ? 1 : 0); 82268189Seric 82368189Seric /* tricky little algorithm for backward compatibility */ 82468189Seric for (trv = start;;) { 82568189Seric if (!*trv) 82668189Seric return(0); 82768189Seric if (*trv == 'z') 82868189Seric *trv++ = 'a'; 82968189Seric else { 83068189Seric if (isdigit(*trv)) 83168189Seric *trv = 'a'; 83268189Seric else 83368189Seric ++*trv; 83468189Seric break; 83568189Seric } 83668189Seric } 83768189Seric } 83868189Seric /*NOTREACHED*/ 83968189Seric } 84068189Seric 84167830Seric #endif 842