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*68187Seric static char sccsid[] = "@(#)mail.local.c	8.12 (Berkeley) 01/16/95";
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;
27268060Seric 	} else if (sb.st_uid != pw->pw_uid) {
27368060Seric 		warn("%s: wrong ownership (%d)", path, sb.st_uid);
27468060Seric 		unlockmbox();
27568060Seric 		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 ||
28168060Seric 		    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 	int statfailed = 0;
36568005Seric 
36668005Seric 	if (locked)
36768005Seric 		return;
36868005Seric 	sprintf(lockname, "%s.lock", path);
36968005Seric 	for (;; sleep(5)) {
37068005Seric 		int fd;
37168005Seric 		struct stat st;
37268005Seric 		time_t now;
37368005Seric 
374*68187Seric 		fd = open(lockname, O_WRONLY|O_EXCL|O_CREAT, 0);
375*68187Seric 		if (fd >= 0) {
37668005Seric 			locked = 1;
377*68187Seric 			close(fd);
37868005Seric 			return;
37968005Seric 		}
38068005Seric 		if (stat(lockname, &st) < 0) {
38168005Seric 			if (statfailed++ > 5)
38268005Seric 				return;
38368005Seric 			continue;
38468005Seric 		}
38568005Seric 		statfailed = 0;
38668005Seric 		time(&now);
38768005Seric 		if (now < st.st_ctime + 300)
38868005Seric 			continue;
38968005Seric 		unlink(lockname);
39068005Seric 	}
39168005Seric }
39268005Seric 
39368005Seric unlockmbox()
39468005Seric {
39568005Seric 	if (!locked)
39668005Seric 		return;
39768005Seric 	unlink(lockname);
39868005Seric 	locked = 0;
39968005Seric }
40068005Seric 
40150092Sbostic void
40216731Sralph notifybiff(msg)
40316731Sralph 	char *msg;
40416731Sralph {
40516731Sralph 	static struct sockaddr_in addr;
40616731Sralph 	static int f = -1;
40745976Sbostic 	struct hostent *hp;
40845976Sbostic 	struct servent *sp;
40945976Sbostic 	int len;
41016731Sralph 
41145976Sbostic 	if (!addr.sin_family) {
41245976Sbostic 		/* Be silent if biff service not available. */
41345976Sbostic 		if (!(sp = getservbyname("biff", "udp")))
41445976Sbostic 			return;
41545976Sbostic 		if (!(hp = gethostbyname("localhost"))) {
41659601Sbostic 			warn("localhost: %s", strerror(errno));
41745976Sbostic 			return;
41816731Sralph 		}
41945976Sbostic 		addr.sin_family = hp->h_addrtype;
42067830Seric 		memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
42145976Sbostic 		addr.sin_port = sp->s_port;
42216731Sralph 	}
42345976Sbostic 	if (f < 0 && (f = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
42459601Sbostic 		warn("socket: %s", strerror(errno));
42545976Sbostic 		return;
42616731Sralph 	}
42745976Sbostic 	len = strlen(msg) + 1;
42846672Sbostic 	if (sendto(f, msg, len, 0, (struct sockaddr *)&addr, sizeof(addr))
42946672Sbostic 	    != len)
43059601Sbostic 		warn("sendto biff: %s", strerror(errno));
43116731Sralph }
43216731Sralph 
43350092Sbostic void
43445976Sbostic usage()
435579Sroot {
43659601Sbostic 	eval = EX_USAGE;
43759601Sbostic 	err("usage: mail.local [-f from] user ...");
438579Sroot }
439579Sroot 
44050092Sbostic #if __STDC__
44165558Sbostic void
44259601Sbostic err(const char *fmt, ...)
44350092Sbostic #else
44464993Smckusick void
44559601Sbostic err(fmt, va_alist)
44659601Sbostic 	const char *fmt;
44759601Sbostic 	va_dcl
44850092Sbostic #endif
44959601Sbostic {
45059601Sbostic 	va_list ap;
45150092Sbostic 
45259601Sbostic #if __STDC__
45359601Sbostic 	va_start(ap, fmt);
45459601Sbostic #else
45559601Sbostic 	va_start(ap);
45659601Sbostic #endif
45759601Sbostic 	vwarn(fmt, ap);
45859601Sbostic 	va_end(ap);
45959601Sbostic 
46059601Sbostic 	exit(eval);
46159601Sbostic }
46259601Sbostic 
46350092Sbostic void
46450092Sbostic #if __STDC__
46559601Sbostic warn(const char *fmt, ...)
46650092Sbostic #else
46759601Sbostic warn(fmt, va_alist)
46859601Sbostic 	const char *fmt;
46950092Sbostic 	va_dcl
47050092Sbostic #endif
471579Sroot {
47245976Sbostic 	va_list ap;
47359601Sbostic 
47450092Sbostic #if __STDC__
47546554Sbostic 	va_start(ap, fmt);
47650092Sbostic #else
47750092Sbostic 	va_start(ap);
47850092Sbostic #endif
47959601Sbostic 	vwarn(fmt, ap);
48059601Sbostic 	va_end(ap);
48159601Sbostic }
48259601Sbostic 
48359601Sbostic void
48459601Sbostic vwarn(fmt, ap)
48559601Sbostic 	const char *fmt;
48659601Sbostic 	_BSD_VA_LIST_ ap;
48759601Sbostic {
48858400Sbostic 	/*
48959601Sbostic 	 * Log the message to stderr.
49059601Sbostic 	 *
49159601Sbostic 	 * Don't use LOG_PERROR as an openlog() flag to do this,
49259601Sbostic 	 * it's not portable enough.
49358400Sbostic 	 */
49459601Sbostic 	if (eval != EX_USAGE)
49559601Sbostic 		(void)fprintf(stderr, "mail.local: ");
49658396Sbostic 	(void)vfprintf(stderr, fmt, ap);
49758400Sbostic 	(void)fprintf(stderr, "\n");
49859601Sbostic 
49967830Seric #ifndef ultrix
50059601Sbostic 	/* Log the message to syslog. */
50145976Sbostic 	vsyslog(LOG_ERR, fmt, ap);
50267830Seric #else
50367830Seric 	{
50467830Seric 		char fmtbuf[10240];
50567830Seric 
50667830Seric 		(void) sprintf(fmtbuf, fmt, ap);
50767830Seric 		syslog(LOG_ERR, "%s", fmtbuf);
50867830Seric 	}
50967830Seric #endif
510579Sroot }
51159601Sbostic 
51259601Sbostic /*
51359601Sbostic  * e_to_sys --
51459601Sbostic  *	Guess which errno's are temporary.  Gag me.
51559601Sbostic  */
51659601Sbostic void
51759601Sbostic e_to_sys(num)
51859601Sbostic 	int num;
51959601Sbostic {
52059601Sbostic 	/* Temporary failures override hard errors. */
52159601Sbostic 	if (eval == EX_TEMPFAIL)
52259601Sbostic 		return;
52359601Sbostic 
52459601Sbostic 	switch(num) {		/* Hopefully temporary errors. */
52559601Sbostic #ifdef EAGAIN
52659601Sbostic 	case EAGAIN:		/* Resource temporarily unavailable */
52759601Sbostic #endif
52859601Sbostic #ifdef EDQUOT
52959601Sbostic 	case EDQUOT:		/* Disc quota exceeded */
53059601Sbostic #endif
53159601Sbostic #ifdef EBUSY
53259601Sbostic 	case EBUSY:		/* Device busy */
53359601Sbostic #endif
53459601Sbostic #ifdef EPROCLIM
53559601Sbostic 	case EPROCLIM:		/* Too many processes */
53659601Sbostic #endif
53759601Sbostic #ifdef EUSERS
53859601Sbostic 	case EUSERS:		/* Too many users */
53959601Sbostic #endif
54059601Sbostic #ifdef ECONNABORTED
54159601Sbostic 	case ECONNABORTED:	/* Software caused connection abort */
54259601Sbostic #endif
54359601Sbostic #ifdef ECONNREFUSED
54459601Sbostic 	case ECONNREFUSED:	/* Connection refused */
54559601Sbostic #endif
54659601Sbostic #ifdef ECONNRESET
54759601Sbostic 	case ECONNRESET:	/* Connection reset by peer */
54859601Sbostic #endif
54959601Sbostic #ifdef EDEADLK
55059601Sbostic 	case EDEADLK:		/* Resource deadlock avoided */
55159601Sbostic #endif
55259601Sbostic #ifdef EFBIG
55359601Sbostic 	case EFBIG:		/* File too large */
55459601Sbostic #endif
55559601Sbostic #ifdef EHOSTDOWN
55659601Sbostic 	case EHOSTDOWN:		/* Host is down */
55759601Sbostic #endif
55859601Sbostic #ifdef EHOSTUNREACH
55959601Sbostic 	case EHOSTUNREACH:	/* No route to host */
56059601Sbostic #endif
56159601Sbostic #ifdef EMFILE
56259601Sbostic 	case EMFILE:		/* Too many open files */
56359601Sbostic #endif
56459601Sbostic #ifdef ENETDOWN
56559601Sbostic 	case ENETDOWN:		/* Network is down */
56659601Sbostic #endif
56759601Sbostic #ifdef ENETRESET
56859601Sbostic 	case ENETRESET:		/* Network dropped connection on reset */
56959601Sbostic #endif
57059601Sbostic #ifdef ENETUNREACH
57159601Sbostic 	case ENETUNREACH:	/* Network is unreachable */
57259601Sbostic #endif
57359601Sbostic #ifdef ENFILE
57459601Sbostic 	case ENFILE:		/* Too many open files in system */
57559601Sbostic #endif
57659601Sbostic #ifdef ENOBUFS
57759601Sbostic 	case ENOBUFS:		/* No buffer space available */
57859601Sbostic #endif
57959601Sbostic #ifdef ENOMEM
58059601Sbostic 	case ENOMEM:		/* Cannot allocate memory */
58159601Sbostic #endif
58259601Sbostic #ifdef ENOSPC
58359601Sbostic 	case ENOSPC:		/* No space left on device */
58459601Sbostic #endif
58559601Sbostic #ifdef EROFS
58659601Sbostic 	case EROFS:		/* Read-only file system */
58759601Sbostic #endif
58859601Sbostic #ifdef ESTALE
58959601Sbostic 	case ESTALE:		/* Stale NFS file handle */
59059601Sbostic #endif
59159601Sbostic #ifdef ETIMEDOUT
59259601Sbostic 	case ETIMEDOUT:		/* Connection timed out */
59359601Sbostic #endif
59467830Seric #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN && EWOULDBLOCK != EDEADLK
59559601Sbostic 	case EWOULDBLOCK:	/* Operation would block. */
59659601Sbostic #endif
59759601Sbostic 		eval = EX_TEMPFAIL;
59859601Sbostic 		break;
59959601Sbostic 	default:
60059601Sbostic 		eval = EX_UNAVAILABLE;
60159601Sbostic 		break;
60259601Sbostic 	}
60359601Sbostic }
60467830Seric 
60567830Seric #ifndef BSD4_4
60667830Seric 
60767830Seric char *
60867830Seric strerror(eno)
60967830Seric 	int eno;
61067830Seric {
61167830Seric 	extern int sys_nerr;
61267830Seric 	extern char *sys_errlist[];
61367830Seric 	static char ebuf[60];
61467830Seric 
61567830Seric 	if (eno >= 0 && eno <= sys_nerr)
61667830Seric 		return sys_errlist[eno];
61767830Seric 	(void) sprintf(ebuf, "Error %d", eno);
61867830Seric 	return ebuf;
61967830Seric }
62067830Seric 
62167830Seric #if __STDC__
62267830Seric snprintf(char *buf, int bufsiz, const char *fmt, ...)
62367830Seric #else
62467830Seric snprintf(buf, bufsiz, fmt, va_alist)
62567830Seric 	char *buf;
62667830Seric 	int bufsiz;
62767830Seric 	const char *fmt;
62867830Seric 	va_dcl
62967830Seric #endif
63067830Seric {
63167830Seric 	va_list ap;
63267830Seric 
63367830Seric #if __STDC__
63467830Seric 	va_start(ap, fmt);
63567830Seric #else
63667830Seric 	va_start(ap);
63767830Seric #endif
63867830Seric 	vsprintf(buf, fmt, ap);
63967830Seric 	va_end(ap);
64067830Seric }
64167830Seric 
64267830Seric #endif
64367830Seric 
64467830Seric #ifdef ultrix
64567830Seric 
64667830Seric int
64767830Seric mkstemp(template)
64867830Seric 	char *template;
64967830Seric {
65067830Seric 	int fd;
65167830Seric 
65267830Seric 	return open(mktemp(template), O_RDWR|O_CREAT|O_EXCL, 0600);
65367830Seric }
65467830Seric 
65567830Seric #endif
656