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*68060Seric static char sccsid[] = "@(#)mail.local.c	8.11 (Berkeley) 12/07/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;
272*68060Seric 	} else if (sb.st_uid != pw->pw_uid) {
273*68060Seric 		warn("%s: wrong ownership (%d)", path, sb.st_uid);
274*68060Seric 		unlockmbox();
275*68060Seric 		return;
27666733Sbostic 	} else {
27759601Sbostic 		mbfd = open(path, O_APPEND|O_WRONLY, 0);
27866735Sbostic 		if (mbfd != -1 &&
27966735Sbostic 		    (fstat(mbfd, &fsb) || fsb.st_nlink != 1 ||
28066735Sbostic 		    S_ISLNK(fsb.st_mode) || sb.st_dev != fsb.st_dev ||
281*68060Seric 		    sb.st_ino != fsb.st_ino) || sb.st_uid != fsb.st_uid) {
28266735Sbostic 			warn("%s: file changed after open", path);
28366733Sbostic 			(void)close(mbfd);
28468005Seric 			unlockmbox();
28566733Sbostic 			return;
28666733Sbostic 		}
28766733Sbostic 	}
28859601Sbostic 
28959601Sbostic 	if (mbfd == -1) {
29059601Sbostic 		e_to_sys(errno);
29159601Sbostic 		warn("%s: %s", path, strerror(errno));
29268005Seric 		unlockmbox();
29359601Sbostic 		return;
29445976Sbostic 	}
295579Sroot 
29659601Sbostic 	/* Wait until we can get a lock on the file. */
29745976Sbostic 	if (flock(mbfd, LOCK_EX)) {
29859601Sbostic 		e_to_sys(errno);
29959601Sbostic 		warn("%s: %s", path, strerror(errno));
30068005Seric 		unlockmbox();
30159601Sbostic 		goto err1;
30245976Sbostic 	}
303579Sroot 
30459601Sbostic 	/* Get the starting offset of the new message for biff. */
30554203Sbostic 	curoff = lseek(mbfd, (off_t)0, SEEK_END);
30668021Seric 	(void)snprintf(biffmsg, sizeof(biffmsg),
30768021Seric 		sizeof curoff > sizeof(long) ? "%s@%qd\n" : "%s@%ld\n",
30868021Seric 		name, curoff);
30959601Sbostic 
31059601Sbostic 	/* Copy the message into the file. */
31154203Sbostic 	if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
31259601Sbostic 		e_to_sys(errno);
31359601Sbostic 		warn("temporary file: %s", strerror(errno));
31459601Sbostic 		goto err1;
31545976Sbostic 	}
31645976Sbostic 	while ((nr = read(fd, buf, sizeof(buf))) > 0)
31745976Sbostic 		for (off = 0; off < nr; nr -= nw, off += nw)
31845976Sbostic 			if ((nw = write(mbfd, buf + off, nr)) < 0) {
31959601Sbostic 				e_to_sys(errno);
32059601Sbostic 				warn("%s: %s", path, strerror(errno));
32159601Sbostic 				goto err2;;
32245976Sbostic 			}
32345976Sbostic 	if (nr < 0) {
32459601Sbostic 		e_to_sys(errno);
32559601Sbostic 		warn("temporary file: %s", strerror(errno));
32659601Sbostic 		goto err2;;
327579Sroot 	}
328579Sroot 
32959601Sbostic 	/* Flush to disk, don't wait for update. */
33059601Sbostic 	if (fsync(mbfd)) {
33159601Sbostic 		e_to_sys(errno);
33259601Sbostic 		warn("%s: %s", path, strerror(errno));
33359601Sbostic err2:		(void)ftruncate(mbfd, curoff);
33459601Sbostic err1:		(void)close(mbfd);
33568005Seric 		unlockmbox();
33659601Sbostic 		return;
33759601Sbostic 	}
33859601Sbostic 
33959601Sbostic 	/* Close and check -- NFS doesn't write until the close. */
34059601Sbostic 	if (close(mbfd)) {
34159601Sbostic 		e_to_sys(errno);
34259601Sbostic 		warn("%s: %s", path, strerror(errno));
34368005Seric 		unlockmbox();
34459601Sbostic 		return;
34559601Sbostic 	}
346579Sroot 
34768005Seric 	unlockmbox();
34859601Sbostic 	notifybiff(biffmsg);
349579Sroot }
350579Sroot 
35168005Seric /*
35268005Seric  * user.lock files are necessary for compatibility with other
35368005Seric  * systems, e.g., when the mail spool file is NFS exported.
35468005Seric  * Alas, mailbox locking is more than just a local matter.
35568005Seric  * EPA 11/94.
35668005Seric  */
35768005Seric 
35868005Seric char	lockname[50];
35968005Seric int	locked = 0;
36068005Seric 
36168005Seric lockmbox(path)
36268005Seric 	char *path;
36368005Seric {
36468005Seric 	char locktmp[50];
36568005Seric 	int statfailed = 0;
36668005Seric 
36768005Seric 	if (locked)
36868005Seric 		return;
36968005Seric 	sprintf(lockname, "%s.lock", path);
37068005Seric 	sprintf(locktmp, "%s/tmXXXXXX", _PATH_MAILDIR);
37168005Seric 	mktemp(locktmp);
37268005Seric 	unlink(locktmp);
37368005Seric 	for (;; sleep(5)) {
37468005Seric 		int fd;
37568005Seric 		struct stat st;
37668005Seric 		time_t now;
37768005Seric 
37868006Seric 		fd = open(locktmp, O_WRONLY|O_EXCL|O_CREAT, 0);
37968005Seric 		if (fd < 0)
38068005Seric 			continue;
38168005Seric 		close(fd);
38268005Seric 		if (link(locktmp, lockname) >= 0) {
38368005Seric 			unlink(locktmp);
38468005Seric 			locked = 1;
38568005Seric 			return;
38668005Seric 		}
38768005Seric 		unlink(locktmp);
38868005Seric 		if (stat(lockname, &st) < 0) {
38968005Seric 			if (statfailed++ > 5)
39068005Seric 				return;
39168005Seric 			continue;
39268005Seric 		}
39368005Seric 		statfailed = 0;
39468005Seric 		time(&now);
39568005Seric 		if (now < st.st_ctime + 300)
39668005Seric 			continue;
39768005Seric 		unlink(lockname);
39868005Seric 	}
39968005Seric }
40068005Seric 
40168005Seric unlockmbox()
40268005Seric {
40368005Seric 	if (!locked)
40468005Seric 		return;
40568005Seric 	unlink(lockname);
40668005Seric 	locked = 0;
40768005Seric }
40868005Seric 
40950092Sbostic void
41016731Sralph notifybiff(msg)
41116731Sralph 	char *msg;
41216731Sralph {
41316731Sralph 	static struct sockaddr_in addr;
41416731Sralph 	static int f = -1;
41545976Sbostic 	struct hostent *hp;
41645976Sbostic 	struct servent *sp;
41745976Sbostic 	int len;
41816731Sralph 
41945976Sbostic 	if (!addr.sin_family) {
42045976Sbostic 		/* Be silent if biff service not available. */
42145976Sbostic 		if (!(sp = getservbyname("biff", "udp")))
42245976Sbostic 			return;
42345976Sbostic 		if (!(hp = gethostbyname("localhost"))) {
42459601Sbostic 			warn("localhost: %s", strerror(errno));
42545976Sbostic 			return;
42616731Sralph 		}
42745976Sbostic 		addr.sin_family = hp->h_addrtype;
42867830Seric 		memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
42945976Sbostic 		addr.sin_port = sp->s_port;
43016731Sralph 	}
43145976Sbostic 	if (f < 0 && (f = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
43259601Sbostic 		warn("socket: %s", strerror(errno));
43345976Sbostic 		return;
43416731Sralph 	}
43545976Sbostic 	len = strlen(msg) + 1;
43646672Sbostic 	if (sendto(f, msg, len, 0, (struct sockaddr *)&addr, sizeof(addr))
43746672Sbostic 	    != len)
43859601Sbostic 		warn("sendto biff: %s", strerror(errno));
43916731Sralph }
44016731Sralph 
44150092Sbostic void
44245976Sbostic usage()
443579Sroot {
44459601Sbostic 	eval = EX_USAGE;
44559601Sbostic 	err("usage: mail.local [-f from] user ...");
446579Sroot }
447579Sroot 
44850092Sbostic #if __STDC__
44965558Sbostic void
45059601Sbostic err(const char *fmt, ...)
45150092Sbostic #else
45264993Smckusick void
45359601Sbostic err(fmt, va_alist)
45459601Sbostic 	const char *fmt;
45559601Sbostic 	va_dcl
45650092Sbostic #endif
45759601Sbostic {
45859601Sbostic 	va_list ap;
45950092Sbostic 
46059601Sbostic #if __STDC__
46159601Sbostic 	va_start(ap, fmt);
46259601Sbostic #else
46359601Sbostic 	va_start(ap);
46459601Sbostic #endif
46559601Sbostic 	vwarn(fmt, ap);
46659601Sbostic 	va_end(ap);
46759601Sbostic 
46859601Sbostic 	exit(eval);
46959601Sbostic }
47059601Sbostic 
47150092Sbostic void
47250092Sbostic #if __STDC__
47359601Sbostic warn(const char *fmt, ...)
47450092Sbostic #else
47559601Sbostic warn(fmt, va_alist)
47659601Sbostic 	const char *fmt;
47750092Sbostic 	va_dcl
47850092Sbostic #endif
479579Sroot {
48045976Sbostic 	va_list ap;
48159601Sbostic 
48250092Sbostic #if __STDC__
48346554Sbostic 	va_start(ap, fmt);
48450092Sbostic #else
48550092Sbostic 	va_start(ap);
48650092Sbostic #endif
48759601Sbostic 	vwarn(fmt, ap);
48859601Sbostic 	va_end(ap);
48959601Sbostic }
49059601Sbostic 
49159601Sbostic void
49259601Sbostic vwarn(fmt, ap)
49359601Sbostic 	const char *fmt;
49459601Sbostic 	_BSD_VA_LIST_ ap;
49559601Sbostic {
49658400Sbostic 	/*
49759601Sbostic 	 * Log the message to stderr.
49859601Sbostic 	 *
49959601Sbostic 	 * Don't use LOG_PERROR as an openlog() flag to do this,
50059601Sbostic 	 * it's not portable enough.
50158400Sbostic 	 */
50259601Sbostic 	if (eval != EX_USAGE)
50359601Sbostic 		(void)fprintf(stderr, "mail.local: ");
50458396Sbostic 	(void)vfprintf(stderr, fmt, ap);
50558400Sbostic 	(void)fprintf(stderr, "\n");
50659601Sbostic 
50767830Seric #ifndef ultrix
50859601Sbostic 	/* Log the message to syslog. */
50945976Sbostic 	vsyslog(LOG_ERR, fmt, ap);
51067830Seric #else
51167830Seric 	{
51267830Seric 		char fmtbuf[10240];
51367830Seric 
51467830Seric 		(void) sprintf(fmtbuf, fmt, ap);
51567830Seric 		syslog(LOG_ERR, "%s", fmtbuf);
51667830Seric 	}
51767830Seric #endif
518579Sroot }
51959601Sbostic 
52059601Sbostic /*
52159601Sbostic  * e_to_sys --
52259601Sbostic  *	Guess which errno's are temporary.  Gag me.
52359601Sbostic  */
52459601Sbostic void
52559601Sbostic e_to_sys(num)
52659601Sbostic 	int num;
52759601Sbostic {
52859601Sbostic 	/* Temporary failures override hard errors. */
52959601Sbostic 	if (eval == EX_TEMPFAIL)
53059601Sbostic 		return;
53159601Sbostic 
53259601Sbostic 	switch(num) {		/* Hopefully temporary errors. */
53359601Sbostic #ifdef EAGAIN
53459601Sbostic 	case EAGAIN:		/* Resource temporarily unavailable */
53559601Sbostic #endif
53659601Sbostic #ifdef EDQUOT
53759601Sbostic 	case EDQUOT:		/* Disc quota exceeded */
53859601Sbostic #endif
53959601Sbostic #ifdef EBUSY
54059601Sbostic 	case EBUSY:		/* Device busy */
54159601Sbostic #endif
54259601Sbostic #ifdef EPROCLIM
54359601Sbostic 	case EPROCLIM:		/* Too many processes */
54459601Sbostic #endif
54559601Sbostic #ifdef EUSERS
54659601Sbostic 	case EUSERS:		/* Too many users */
54759601Sbostic #endif
54859601Sbostic #ifdef ECONNABORTED
54959601Sbostic 	case ECONNABORTED:	/* Software caused connection abort */
55059601Sbostic #endif
55159601Sbostic #ifdef ECONNREFUSED
55259601Sbostic 	case ECONNREFUSED:	/* Connection refused */
55359601Sbostic #endif
55459601Sbostic #ifdef ECONNRESET
55559601Sbostic 	case ECONNRESET:	/* Connection reset by peer */
55659601Sbostic #endif
55759601Sbostic #ifdef EDEADLK
55859601Sbostic 	case EDEADLK:		/* Resource deadlock avoided */
55959601Sbostic #endif
56059601Sbostic #ifdef EFBIG
56159601Sbostic 	case EFBIG:		/* File too large */
56259601Sbostic #endif
56359601Sbostic #ifdef EHOSTDOWN
56459601Sbostic 	case EHOSTDOWN:		/* Host is down */
56559601Sbostic #endif
56659601Sbostic #ifdef EHOSTUNREACH
56759601Sbostic 	case EHOSTUNREACH:	/* No route to host */
56859601Sbostic #endif
56959601Sbostic #ifdef EMFILE
57059601Sbostic 	case EMFILE:		/* Too many open files */
57159601Sbostic #endif
57259601Sbostic #ifdef ENETDOWN
57359601Sbostic 	case ENETDOWN:		/* Network is down */
57459601Sbostic #endif
57559601Sbostic #ifdef ENETRESET
57659601Sbostic 	case ENETRESET:		/* Network dropped connection on reset */
57759601Sbostic #endif
57859601Sbostic #ifdef ENETUNREACH
57959601Sbostic 	case ENETUNREACH:	/* Network is unreachable */
58059601Sbostic #endif
58159601Sbostic #ifdef ENFILE
58259601Sbostic 	case ENFILE:		/* Too many open files in system */
58359601Sbostic #endif
58459601Sbostic #ifdef ENOBUFS
58559601Sbostic 	case ENOBUFS:		/* No buffer space available */
58659601Sbostic #endif
58759601Sbostic #ifdef ENOMEM
58859601Sbostic 	case ENOMEM:		/* Cannot allocate memory */
58959601Sbostic #endif
59059601Sbostic #ifdef ENOSPC
59159601Sbostic 	case ENOSPC:		/* No space left on device */
59259601Sbostic #endif
59359601Sbostic #ifdef EROFS
59459601Sbostic 	case EROFS:		/* Read-only file system */
59559601Sbostic #endif
59659601Sbostic #ifdef ESTALE
59759601Sbostic 	case ESTALE:		/* Stale NFS file handle */
59859601Sbostic #endif
59959601Sbostic #ifdef ETIMEDOUT
60059601Sbostic 	case ETIMEDOUT:		/* Connection timed out */
60159601Sbostic #endif
60267830Seric #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN && EWOULDBLOCK != EDEADLK
60359601Sbostic 	case EWOULDBLOCK:	/* Operation would block. */
60459601Sbostic #endif
60559601Sbostic 		eval = EX_TEMPFAIL;
60659601Sbostic 		break;
60759601Sbostic 	default:
60859601Sbostic 		eval = EX_UNAVAILABLE;
60959601Sbostic 		break;
61059601Sbostic 	}
61159601Sbostic }
61267830Seric 
61367830Seric #ifndef BSD4_4
61467830Seric 
61567830Seric char *
61667830Seric strerror(eno)
61767830Seric 	int eno;
61867830Seric {
61967830Seric 	extern int sys_nerr;
62067830Seric 	extern char *sys_errlist[];
62167830Seric 	static char ebuf[60];
62267830Seric 
62367830Seric 	if (eno >= 0 && eno <= sys_nerr)
62467830Seric 		return sys_errlist[eno];
62567830Seric 	(void) sprintf(ebuf, "Error %d", eno);
62667830Seric 	return ebuf;
62767830Seric }
62867830Seric 
62967830Seric #if __STDC__
63067830Seric snprintf(char *buf, int bufsiz, const char *fmt, ...)
63167830Seric #else
63267830Seric snprintf(buf, bufsiz, fmt, va_alist)
63367830Seric 	char *buf;
63467830Seric 	int bufsiz;
63567830Seric 	const char *fmt;
63667830Seric 	va_dcl
63767830Seric #endif
63867830Seric {
63967830Seric 	va_list ap;
64067830Seric 
64167830Seric #if __STDC__
64267830Seric 	va_start(ap, fmt);
64367830Seric #else
64467830Seric 	va_start(ap);
64567830Seric #endif
64667830Seric 	vsprintf(buf, fmt, ap);
64767830Seric 	va_end(ap);
64867830Seric }
64967830Seric 
65067830Seric #endif
65167830Seric 
65267830Seric #ifdef ultrix
65367830Seric 
65467830Seric int
65567830Seric mkstemp(template)
65667830Seric 	char *template;
65767830Seric {
65867830Seric 	int fd;
65967830Seric 
66067830Seric 	return open(mktemp(template), O_RDWR|O_CREAT|O_EXCL, 0600);
66167830Seric }
66267830Seric 
66367830Seric #endif
664