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*68190Seric static char sccsid[] = "@(#)mail.local.c	8.15 (Berkeley) 01/18/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 
7868188Seric #ifndef S_ISREG
7968188Seric # define S_ISREG(mode)	(((mode) & _S_IFMT) == S_IFREG)
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 
105*68190Seric 	/* make sure we have some open file descriptors */
106*68190Seric 	for (fd = 10; fd < 30; fd++)
107*68190Seric 		(void) close(fd);
108*68190Seric 
109*68190Seric 	/* use a reasonable umask */
110*68190Seric 	(void) umask(0077)
111*68190Seric 
11267830Seric #ifdef LOG_MAIL
11358400Sbostic 	openlog("mail.local", 0, LOG_MAIL);
11467830Seric #else
11567830Seric 	openlog("mail.local", 0);
11667830Seric #endif
11716731Sralph 
11845976Sbostic 	from = NULL;
11945976Sbostic 	while ((ch = getopt(argc, argv, "df:r:")) != EOF)
12045976Sbostic 		switch(ch) {
12159601Sbostic 		case 'd':		/* Backward compatible. */
12216731Sralph 			break;
12316731Sralph 		case 'f':
12459601Sbostic 		case 'r':		/* Backward compatible. */
12559601Sbostic 			if (from != NULL) {
12659601Sbostic 				warn("multiple -f options");
12759601Sbostic 				usage();
12859601Sbostic 			}
12945976Sbostic 			from = optarg;
130579Sroot 			break;
13145976Sbostic 		case '?':
13216731Sralph 		default:
13345976Sbostic 			usage();
13416731Sralph 		}
13545976Sbostic 	argc -= optind;
13645976Sbostic 	argv += optind;
137579Sroot 
13845976Sbostic 	if (!*argv)
13945976Sbostic 		usage();
140579Sroot 
14145976Sbostic 	/*
14245976Sbostic 	 * If from not specified, use the name from getlogin() if the
14345976Sbostic 	 * uid matches, otherwise, use the name from the password file
14445976Sbostic 	 * corresponding to the uid.
14545976Sbostic 	 */
14645976Sbostic 	uid = getuid();
14746034Sbostic 	if (!from && (!(from = getlogin()) ||
14846034Sbostic 	    !(pw = getpwnam(from)) || pw->pw_uid != uid))
14945976Sbostic 		from = (pw = getpwuid(uid)) ? pw->pw_name : "???";
150579Sroot 
15159601Sbostic 	/*
15259601Sbostic 	 * There is no way to distinguish the error status of one delivery
15359601Sbostic 	 * from the rest of the deliveries.  So, if we failed hard on one
15459601Sbostic 	 * or more deliveries, but had no failures on any of the others, we
15559601Sbostic 	 * return a hard failure.  If we failed temporarily on one or more
15659601Sbostic 	 * deliveries, we return a temporary failure regardless of the other
15759601Sbostic 	 * failures.  This results in the delivery being reattempted later
15859601Sbostic 	 * at the expense of repeated failures and multiple deliveries.
15959601Sbostic 	 */
16059601Sbostic 	for (fd = store(from); *argv; ++argv)
16159601Sbostic 		deliver(fd, *argv);
16245976Sbostic 	exit(eval);
163579Sroot }
164579Sroot 
16557648Sbostic int
16645976Sbostic store(from)
16745976Sbostic 	char *from;
168579Sroot {
16945976Sbostic 	FILE *fp;
17045976Sbostic 	time_t tval;
17145976Sbostic 	int fd, eline;
17267830Seric 	char line[2048];
17367830Seric 	char tmpbuf[sizeof _PATH_LOCTMP + 1];
174579Sroot 
17567830Seric 	strcpy(tmpbuf, _PATH_LOCTMP);
17667830Seric 	if ((fd = mkstemp(tmpbuf)) == -1 || (fp = fdopen(fd, "w+")) == NULL) {
17759601Sbostic 		e_to_sys(errno);
17859601Sbostic 		err("unable to open temporary file");
17959601Sbostic 	}
18067830Seric 	(void)unlink(tmpbuf);
181579Sroot 
18245976Sbostic 	(void)time(&tval);
18345976Sbostic 	(void)fprintf(fp, "From %s %s", from, ctime(&tval));
184579Sroot 
18545976Sbostic 	line[0] = '\0';
18645976Sbostic 	for (eline = 1; fgets(line, sizeof(line), stdin);) {
18745976Sbostic 		if (line[0] == '\n')
18845976Sbostic 			eline = 1;
18945976Sbostic 		else {
19060097Sbostic 			if (eline && line[0] == 'F' &&
19160097Sbostic 			    !memcmp(line, "From ", 5))
19245976Sbostic 				(void)putc('>', fp);
19345976Sbostic 			eline = 0;
19445976Sbostic 		}
19545976Sbostic 		(void)fprintf(fp, "%s", line);
19659601Sbostic 		if (ferror(fp)) {
19759601Sbostic 			e_to_sys(errno);
19859601Sbostic 			err("temporary file write error");
19959601Sbostic 		}
200579Sroot 	}
201579Sroot 
20245976Sbostic 	/* If message not newline terminated, need an extra. */
20359601Sbostic 	if (!strchr(line, '\n'))
20445976Sbostic 		(void)putc('\n', fp);
20545976Sbostic 	/* Output a newline; note, empty messages are allowed. */
20645976Sbostic 	(void)putc('\n', fp);
20711893Seric 
20859601Sbostic 	if (fflush(fp) == EOF || ferror(fp)) {
20959601Sbostic 		e_to_sys(errno);
21059601Sbostic 		err("temporary file write error");
21159601Sbostic 	}
21259601Sbostic 	return (fd);
213579Sroot }
214579Sroot 
21559601Sbostic void
21645976Sbostic deliver(fd, name)
21745976Sbostic 	int fd;
21845976Sbostic 	char *name;
219579Sroot {
22066735Sbostic 	struct stat fsb, sb;
22145976Sbostic 	struct passwd *pw;
22259601Sbostic 	int mbfd, nr, nw, off;
22345976Sbostic 	char biffmsg[100], buf[8*1024], path[MAXPATHLEN];
22454203Sbostic 	off_t curoff;
225579Sroot 
22645976Sbostic 	/*
22745976Sbostic 	 * Disallow delivery to unknown names -- special mailboxes can be
22845976Sbostic 	 * handled in the sendmail aliases file.
22945976Sbostic 	 */
23045976Sbostic 	if (!(pw = getpwnam(name))) {
23159601Sbostic 		if (eval != EX_TEMPFAIL)
23259601Sbostic 			eval = EX_UNAVAILABLE;
23359601Sbostic 		warn("unknown name: %s", name);
23459601Sbostic 		return;
23545976Sbostic 	}
236579Sroot 
23757648Sbostic 	(void)snprintf(path, sizeof(path), "%s/%s", _PATH_MAILDIR, name);
238579Sroot 
239579Sroot 	/*
24066733Sbostic 	 * If the mailbox is linked or a symlink, fail.  There's an obvious
24166733Sbostic 	 * race here, that the file was replaced with a symbolic link after
24266735Sbostic 	 * the lstat returned, but before the open.  We attempt to detect
24366735Sbostic 	 * this by comparing the original stat information and information
24466735Sbostic 	 * returned by an fstat of the file descriptor returned by the open.
24559601Sbostic 	 *
24666735Sbostic 	 * NB: this is a symptom of a larger problem, that the mail spooling
24766735Sbostic 	 * directory is writeable by the wrong users.  If that directory is
24866735Sbostic 	 * writeable, system security is compromised for other reasons, and
24966735Sbostic 	 * it cannot be fixed here.
25066735Sbostic 	 *
25159601Sbostic 	 * If we created the mailbox, set the owner/group.  If that fails,
25259601Sbostic 	 * just return.  Another process may have already opened it, so we
25359601Sbostic 	 * can't unlink it.  Historically, binmail set the owner/group at
25459601Sbostic 	 * each mail delivery.  We no longer do this, assuming that if the
25559601Sbostic 	 * ownership or permissions were changed there was a reason.
25659601Sbostic 	 *
25759601Sbostic 	 * XXX
25859601Sbostic 	 * open(2) should support flock'ing the file.
259579Sroot 	 */
26066736Sbostic tryagain:
26168005Seric 	lockmbox(path);
26259601Sbostic 	if (lstat(path, &sb)) {
26366736Sbostic 		mbfd = open(path,
26466736Sbostic 		    O_APPEND|O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR);
26566736Sbostic 		if (mbfd == -1) {
26666736Sbostic 			if (errno == EEXIST)
26766736Sbostic 				goto tryagain;
26866736Sbostic 		} else if (fchown(mbfd, pw->pw_uid, pw->pw_gid)) {
26959601Sbostic 			e_to_sys(errno);
27059601Sbostic 			warn("chown %u.%u: %s", pw->pw_uid, pw->pw_gid, name);
27168005Seric 			unlockmbox();
27259601Sbostic 			return;
27359601Sbostic 		}
27468188Seric 	} else if (sb.st_nlink != 1 || !S_ISREG(sb.st_mode)) {
27559601Sbostic 		e_to_sys(errno);
27668188Seric 		warn("%s: irregular file", path);
27768005Seric 		unlockmbox();
27859601Sbostic 		return;
27968060Seric 	} else if (sb.st_uid != pw->pw_uid) {
28068060Seric 		warn("%s: wrong ownership (%d)", path, sb.st_uid);
28168060Seric 		unlockmbox();
28268060Seric 		return;
28366733Sbostic 	} else {
28459601Sbostic 		mbfd = open(path, O_APPEND|O_WRONLY, 0);
28566735Sbostic 		if (mbfd != -1 &&
28666735Sbostic 		    (fstat(mbfd, &fsb) || fsb.st_nlink != 1 ||
28768188Seric 		    !S_ISREG(fsb.st_mode) || sb.st_dev != fsb.st_dev ||
28868188Seric 		    sb.st_ino != fsb.st_ino || sb.st_uid != fsb.st_uid)) {
28966735Sbostic 			warn("%s: file changed after open", path);
29066733Sbostic 			(void)close(mbfd);
29168005Seric 			unlockmbox();
29266733Sbostic 			return;
29366733Sbostic 		}
29466733Sbostic 	}
29559601Sbostic 
29659601Sbostic 	if (mbfd == -1) {
29759601Sbostic 		e_to_sys(errno);
29859601Sbostic 		warn("%s: %s", path, strerror(errno));
29968005Seric 		unlockmbox();
30059601Sbostic 		return;
30145976Sbostic 	}
302579Sroot 
30359601Sbostic 	/* Wait until we can get a lock on the file. */
30445976Sbostic 	if (flock(mbfd, LOCK_EX)) {
30559601Sbostic 		e_to_sys(errno);
30659601Sbostic 		warn("%s: %s", path, strerror(errno));
30768005Seric 		unlockmbox();
30859601Sbostic 		goto err1;
30945976Sbostic 	}
310579Sroot 
31159601Sbostic 	/* Get the starting offset of the new message for biff. */
31254203Sbostic 	curoff = lseek(mbfd, (off_t)0, SEEK_END);
31368021Seric 	(void)snprintf(biffmsg, sizeof(biffmsg),
31468021Seric 		sizeof curoff > sizeof(long) ? "%s@%qd\n" : "%s@%ld\n",
31568021Seric 		name, curoff);
31659601Sbostic 
31759601Sbostic 	/* Copy the message into the file. */
31854203Sbostic 	if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
31959601Sbostic 		e_to_sys(errno);
32059601Sbostic 		warn("temporary file: %s", strerror(errno));
32159601Sbostic 		goto err1;
32245976Sbostic 	}
32345976Sbostic 	while ((nr = read(fd, buf, sizeof(buf))) > 0)
32445976Sbostic 		for (off = 0; off < nr; nr -= nw, off += nw)
32545976Sbostic 			if ((nw = write(mbfd, buf + off, nr)) < 0) {
32659601Sbostic 				e_to_sys(errno);
32759601Sbostic 				warn("%s: %s", path, strerror(errno));
32859601Sbostic 				goto err2;;
32945976Sbostic 			}
33045976Sbostic 	if (nr < 0) {
33159601Sbostic 		e_to_sys(errno);
33259601Sbostic 		warn("temporary file: %s", strerror(errno));
33359601Sbostic 		goto err2;;
334579Sroot 	}
335579Sroot 
33659601Sbostic 	/* Flush to disk, don't wait for update. */
33759601Sbostic 	if (fsync(mbfd)) {
33859601Sbostic 		e_to_sys(errno);
33959601Sbostic 		warn("%s: %s", path, strerror(errno));
34059601Sbostic err2:		(void)ftruncate(mbfd, curoff);
34159601Sbostic err1:		(void)close(mbfd);
34268005Seric 		unlockmbox();
34359601Sbostic 		return;
34459601Sbostic 	}
34559601Sbostic 
34659601Sbostic 	/* Close and check -- NFS doesn't write until the close. */
34759601Sbostic 	if (close(mbfd)) {
34859601Sbostic 		e_to_sys(errno);
34959601Sbostic 		warn("%s: %s", path, strerror(errno));
35068005Seric 		unlockmbox();
35159601Sbostic 		return;
35259601Sbostic 	}
353579Sroot 
35468005Seric 	unlockmbox();
35559601Sbostic 	notifybiff(biffmsg);
356579Sroot }
357579Sroot 
35868005Seric /*
35968005Seric  * user.lock files are necessary for compatibility with other
36068005Seric  * systems, e.g., when the mail spool file is NFS exported.
36168005Seric  * Alas, mailbox locking is more than just a local matter.
36268005Seric  * EPA 11/94.
36368005Seric  */
36468005Seric 
36568005Seric char	lockname[50];
36668005Seric int	locked = 0;
36768005Seric 
36868005Seric lockmbox(path)
36968005Seric 	char *path;
37068005Seric {
37168005Seric 	int statfailed = 0;
37268005Seric 
37368005Seric 	if (locked)
37468005Seric 		return;
37568005Seric 	sprintf(lockname, "%s.lock", path);
37668005Seric 	for (;; sleep(5)) {
37768005Seric 		int fd;
37868005Seric 		struct stat st;
37968005Seric 		time_t now;
38068005Seric 
38168187Seric 		fd = open(lockname, O_WRONLY|O_EXCL|O_CREAT, 0);
38268187Seric 		if (fd >= 0) {
38368005Seric 			locked = 1;
38468187Seric 			close(fd);
38568005Seric 			return;
38668005Seric 		}
38768005Seric 		if (stat(lockname, &st) < 0) {
38868005Seric 			if (statfailed++ > 5)
38968005Seric 				return;
39068005Seric 			continue;
39168005Seric 		}
39268005Seric 		statfailed = 0;
39368005Seric 		time(&now);
39468005Seric 		if (now < st.st_ctime + 300)
39568005Seric 			continue;
39668005Seric 		unlink(lockname);
39768005Seric 	}
39868005Seric }
39968005Seric 
40068005Seric unlockmbox()
40168005Seric {
40268005Seric 	if (!locked)
40368005Seric 		return;
40468005Seric 	unlink(lockname);
40568005Seric 	locked = 0;
40668005Seric }
40768005Seric 
40850092Sbostic void
40916731Sralph notifybiff(msg)
41016731Sralph 	char *msg;
41116731Sralph {
41216731Sralph 	static struct sockaddr_in addr;
41316731Sralph 	static int f = -1;
41445976Sbostic 	struct hostent *hp;
41545976Sbostic 	struct servent *sp;
41645976Sbostic 	int len;
41716731Sralph 
41845976Sbostic 	if (!addr.sin_family) {
41945976Sbostic 		/* Be silent if biff service not available. */
42045976Sbostic 		if (!(sp = getservbyname("biff", "udp")))
42145976Sbostic 			return;
42245976Sbostic 		if (!(hp = gethostbyname("localhost"))) {
42359601Sbostic 			warn("localhost: %s", strerror(errno));
42445976Sbostic 			return;
42516731Sralph 		}
42645976Sbostic 		addr.sin_family = hp->h_addrtype;
42767830Seric 		memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
42845976Sbostic 		addr.sin_port = sp->s_port;
42916731Sralph 	}
43045976Sbostic 	if (f < 0 && (f = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
43159601Sbostic 		warn("socket: %s", strerror(errno));
43245976Sbostic 		return;
43316731Sralph 	}
43445976Sbostic 	len = strlen(msg) + 1;
43546672Sbostic 	if (sendto(f, msg, len, 0, (struct sockaddr *)&addr, sizeof(addr))
43646672Sbostic 	    != len)
43759601Sbostic 		warn("sendto biff: %s", strerror(errno));
43816731Sralph }
43916731Sralph 
44050092Sbostic void
44145976Sbostic usage()
442579Sroot {
44359601Sbostic 	eval = EX_USAGE;
44459601Sbostic 	err("usage: mail.local [-f from] user ...");
445579Sroot }
446579Sroot 
44750092Sbostic #if __STDC__
44865558Sbostic void
44959601Sbostic err(const char *fmt, ...)
45050092Sbostic #else
45164993Smckusick void
45259601Sbostic err(fmt, va_alist)
45359601Sbostic 	const char *fmt;
45459601Sbostic 	va_dcl
45550092Sbostic #endif
45659601Sbostic {
45759601Sbostic 	va_list ap;
45850092Sbostic 
45959601Sbostic #if __STDC__
46059601Sbostic 	va_start(ap, fmt);
46159601Sbostic #else
46259601Sbostic 	va_start(ap);
46359601Sbostic #endif
46459601Sbostic 	vwarn(fmt, ap);
46559601Sbostic 	va_end(ap);
46659601Sbostic 
46759601Sbostic 	exit(eval);
46859601Sbostic }
46959601Sbostic 
47050092Sbostic void
47150092Sbostic #if __STDC__
47259601Sbostic warn(const char *fmt, ...)
47350092Sbostic #else
47459601Sbostic warn(fmt, va_alist)
47559601Sbostic 	const char *fmt;
47650092Sbostic 	va_dcl
47750092Sbostic #endif
478579Sroot {
47945976Sbostic 	va_list ap;
48059601Sbostic 
48150092Sbostic #if __STDC__
48246554Sbostic 	va_start(ap, fmt);
48350092Sbostic #else
48450092Sbostic 	va_start(ap);
48550092Sbostic #endif
48659601Sbostic 	vwarn(fmt, ap);
48759601Sbostic 	va_end(ap);
48859601Sbostic }
48959601Sbostic 
49059601Sbostic void
49159601Sbostic vwarn(fmt, ap)
49259601Sbostic 	const char *fmt;
49359601Sbostic 	_BSD_VA_LIST_ ap;
49459601Sbostic {
49558400Sbostic 	/*
49659601Sbostic 	 * Log the message to stderr.
49759601Sbostic 	 *
49859601Sbostic 	 * Don't use LOG_PERROR as an openlog() flag to do this,
49959601Sbostic 	 * it's not portable enough.
50058400Sbostic 	 */
50159601Sbostic 	if (eval != EX_USAGE)
50259601Sbostic 		(void)fprintf(stderr, "mail.local: ");
50358396Sbostic 	(void)vfprintf(stderr, fmt, ap);
50458400Sbostic 	(void)fprintf(stderr, "\n");
50559601Sbostic 
50667830Seric #ifndef ultrix
50759601Sbostic 	/* Log the message to syslog. */
50845976Sbostic 	vsyslog(LOG_ERR, fmt, ap);
50967830Seric #else
51067830Seric 	{
51167830Seric 		char fmtbuf[10240];
51267830Seric 
51367830Seric 		(void) sprintf(fmtbuf, fmt, ap);
51467830Seric 		syslog(LOG_ERR, "%s", fmtbuf);
51567830Seric 	}
51667830Seric #endif
517579Sroot }
51859601Sbostic 
51959601Sbostic /*
52059601Sbostic  * e_to_sys --
52159601Sbostic  *	Guess which errno's are temporary.  Gag me.
52259601Sbostic  */
52359601Sbostic void
52459601Sbostic e_to_sys(num)
52559601Sbostic 	int num;
52659601Sbostic {
52759601Sbostic 	/* Temporary failures override hard errors. */
52859601Sbostic 	if (eval == EX_TEMPFAIL)
52959601Sbostic 		return;
53059601Sbostic 
53159601Sbostic 	switch(num) {		/* Hopefully temporary errors. */
53259601Sbostic #ifdef EAGAIN
53359601Sbostic 	case EAGAIN:		/* Resource temporarily unavailable */
53459601Sbostic #endif
53559601Sbostic #ifdef EDQUOT
53659601Sbostic 	case EDQUOT:		/* Disc quota exceeded */
53759601Sbostic #endif
53859601Sbostic #ifdef EBUSY
53959601Sbostic 	case EBUSY:		/* Device busy */
54059601Sbostic #endif
54159601Sbostic #ifdef EPROCLIM
54259601Sbostic 	case EPROCLIM:		/* Too many processes */
54359601Sbostic #endif
54459601Sbostic #ifdef EUSERS
54559601Sbostic 	case EUSERS:		/* Too many users */
54659601Sbostic #endif
54759601Sbostic #ifdef ECONNABORTED
54859601Sbostic 	case ECONNABORTED:	/* Software caused connection abort */
54959601Sbostic #endif
55059601Sbostic #ifdef ECONNREFUSED
55159601Sbostic 	case ECONNREFUSED:	/* Connection refused */
55259601Sbostic #endif
55359601Sbostic #ifdef ECONNRESET
55459601Sbostic 	case ECONNRESET:	/* Connection reset by peer */
55559601Sbostic #endif
55659601Sbostic #ifdef EDEADLK
55759601Sbostic 	case EDEADLK:		/* Resource deadlock avoided */
55859601Sbostic #endif
55959601Sbostic #ifdef EFBIG
56059601Sbostic 	case EFBIG:		/* File too large */
56159601Sbostic #endif
56259601Sbostic #ifdef EHOSTDOWN
56359601Sbostic 	case EHOSTDOWN:		/* Host is down */
56459601Sbostic #endif
56559601Sbostic #ifdef EHOSTUNREACH
56659601Sbostic 	case EHOSTUNREACH:	/* No route to host */
56759601Sbostic #endif
56859601Sbostic #ifdef EMFILE
56959601Sbostic 	case EMFILE:		/* Too many open files */
57059601Sbostic #endif
57159601Sbostic #ifdef ENETDOWN
57259601Sbostic 	case ENETDOWN:		/* Network is down */
57359601Sbostic #endif
57459601Sbostic #ifdef ENETRESET
57559601Sbostic 	case ENETRESET:		/* Network dropped connection on reset */
57659601Sbostic #endif
57759601Sbostic #ifdef ENETUNREACH
57859601Sbostic 	case ENETUNREACH:	/* Network is unreachable */
57959601Sbostic #endif
58059601Sbostic #ifdef ENFILE
58159601Sbostic 	case ENFILE:		/* Too many open files in system */
58259601Sbostic #endif
58359601Sbostic #ifdef ENOBUFS
58459601Sbostic 	case ENOBUFS:		/* No buffer space available */
58559601Sbostic #endif
58659601Sbostic #ifdef ENOMEM
58759601Sbostic 	case ENOMEM:		/* Cannot allocate memory */
58859601Sbostic #endif
58959601Sbostic #ifdef ENOSPC
59059601Sbostic 	case ENOSPC:		/* No space left on device */
59159601Sbostic #endif
59259601Sbostic #ifdef EROFS
59359601Sbostic 	case EROFS:		/* Read-only file system */
59459601Sbostic #endif
59559601Sbostic #ifdef ESTALE
59659601Sbostic 	case ESTALE:		/* Stale NFS file handle */
59759601Sbostic #endif
59859601Sbostic #ifdef ETIMEDOUT
59959601Sbostic 	case ETIMEDOUT:		/* Connection timed out */
60059601Sbostic #endif
60167830Seric #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN && EWOULDBLOCK != EDEADLK
60259601Sbostic 	case EWOULDBLOCK:	/* Operation would block. */
60359601Sbostic #endif
60459601Sbostic 		eval = EX_TEMPFAIL;
60559601Sbostic 		break;
60659601Sbostic 	default:
60759601Sbostic 		eval = EX_UNAVAILABLE;
60859601Sbostic 		break;
60959601Sbostic 	}
61059601Sbostic }
61167830Seric 
61267830Seric #ifndef BSD4_4
61367830Seric 
61467830Seric char *
61567830Seric strerror(eno)
61667830Seric 	int eno;
61767830Seric {
61867830Seric 	extern int sys_nerr;
61967830Seric 	extern char *sys_errlist[];
62067830Seric 	static char ebuf[60];
62167830Seric 
62267830Seric 	if (eno >= 0 && eno <= sys_nerr)
62367830Seric 		return sys_errlist[eno];
62467830Seric 	(void) sprintf(ebuf, "Error %d", eno);
62567830Seric 	return ebuf;
62667830Seric }
62767830Seric 
62867830Seric #if __STDC__
62967830Seric snprintf(char *buf, int bufsiz, const char *fmt, ...)
63067830Seric #else
63167830Seric snprintf(buf, bufsiz, fmt, va_alist)
63267830Seric 	char *buf;
63367830Seric 	int bufsiz;
63467830Seric 	const char *fmt;
63567830Seric 	va_dcl
63667830Seric #endif
63767830Seric {
63867830Seric 	va_list ap;
63967830Seric 
64067830Seric #if __STDC__
64167830Seric 	va_start(ap, fmt);
64267830Seric #else
64367830Seric 	va_start(ap);
64467830Seric #endif
64567830Seric 	vsprintf(buf, fmt, ap);
64667830Seric 	va_end(ap);
64767830Seric }
64867830Seric 
64967830Seric #endif
65067830Seric 
65167830Seric #ifdef ultrix
65267830Seric 
65368189Seric /*
65468189Seric  * Copyright (c) 1987, 1993
65568189Seric  *	The Regents of the University of California.  All rights reserved.
65668189Seric  *
65768189Seric  * Redistribution and use in source and binary forms, with or without
65868189Seric  * modification, are permitted provided that the following conditions
65968189Seric  * are met:
66068189Seric  * 1. Redistributions of source code must retain the above copyright
66168189Seric  *    notice, this list of conditions and the following disclaimer.
66268189Seric  * 2. Redistributions in binary form must reproduce the above copyright
66368189Seric  *    notice, this list of conditions and the following disclaimer in the
66468189Seric  *    documentation and/or other materials provided with the distribution.
66568189Seric  * 3. All advertising materials mentioning features or use of this software
66668189Seric  *    must display the following acknowledgement:
66768189Seric  *	This product includes software developed by the University of
66868189Seric  *	California, Berkeley and its contributors.
66968189Seric  * 4. Neither the name of the University nor the names of its contributors
67068189Seric  *    may be used to endorse or promote products derived from this software
67168189Seric  *    without specific prior written permission.
67268189Seric  *
67368189Seric  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
67468189Seric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67568189Seric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67668189Seric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67768189Seric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67868189Seric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67968189Seric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68068189Seric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68168189Seric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68268189Seric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68368189Seric  * SUCH DAMAGE.
68468189Seric  */
68568189Seric 
68668189Seric #if defined(LIBC_SCCS) && !defined(lint)
68768189Seric static char sccsid[] = "@(#)mktemp.c	8.1 (Berkeley) 6/4/93";
68868189Seric #endif /* LIBC_SCCS and not lint */
68968189Seric 
69068189Seric #include <sys/types.h>
69168189Seric #include <sys/stat.h>
69268189Seric #include <fcntl.h>
69368189Seric #include <errno.h>
69468189Seric #include <stdio.h>
69568189Seric #include <ctype.h>
69668189Seric 
69768189Seric static int _gettemp();
69868189Seric 
69968189Seric mkstemp(path)
70068189Seric 	char *path;
70167830Seric {
70267830Seric 	int fd;
70367830Seric 
70468189Seric 	return (_gettemp(path, &fd) ? fd : -1);
70567830Seric }
70667830Seric 
70768189Seric /*
70868189Seric char *
70968189Seric mktemp(path)
71068189Seric 	char *path;
71168189Seric {
71268189Seric 	return(_gettemp(path, (int *)NULL) ? path : (char *)NULL);
71368189Seric }
71468189Seric */
71568189Seric 
71668189Seric static
71768189Seric _gettemp(path, doopen)
71868189Seric 	char *path;
71968189Seric 	register int *doopen;
72068189Seric {
72168189Seric 	extern int errno;
72268189Seric 	register char *start, *trv;
72368189Seric 	struct stat sbuf;
72468189Seric 	u_int pid;
72568189Seric 
72668189Seric 	pid = getpid();
72768189Seric 	for (trv = path; *trv; ++trv);		/* extra X's get set to 0's */
72868189Seric 	while (*--trv == 'X') {
72968189Seric 		*trv = (pid % 10) + '0';
73068189Seric 		pid /= 10;
73168189Seric 	}
73268189Seric 
73368189Seric 	/*
73468189Seric 	 * check the target directory; if you have six X's and it
73568189Seric 	 * doesn't exist this runs for a *very* long time.
73668189Seric 	 */
73768189Seric 	for (start = trv + 1;; --trv) {
73868189Seric 		if (trv <= path)
73968189Seric 			break;
74068189Seric 		if (*trv == '/') {
74168189Seric 			*trv = '\0';
74268189Seric 			if (stat(path, &sbuf))
74368189Seric 				return(0);
74468189Seric 			if (!S_ISDIR(sbuf.st_mode)) {
74568189Seric 				errno = ENOTDIR;
74668189Seric 				return(0);
74768189Seric 			}
74868189Seric 			*trv = '/';
74968189Seric 			break;
75068189Seric 		}
75168189Seric 	}
75268189Seric 
75368189Seric 	for (;;) {
75468189Seric 		if (doopen) {
75568189Seric 			if ((*doopen =
75668189Seric 			    open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
75768189Seric 				return(1);
75868189Seric 			if (errno != EEXIST)
75968189Seric 				return(0);
76068189Seric 		}
76168189Seric 		else if (stat(path, &sbuf))
76268189Seric 			return(errno == ENOENT ? 1 : 0);
76368189Seric 
76468189Seric 		/* tricky little algorithm for backward compatibility */
76568189Seric 		for (trv = start;;) {
76668189Seric 			if (!*trv)
76768189Seric 				return(0);
76868189Seric 			if (*trv == 'z')
76968189Seric 				*trv++ = 'a';
77068189Seric 			else {
77168189Seric 				if (isdigit(*trv))
77268189Seric 					*trv = 'a';
77368189Seric 				else
77468189Seric 					++*trv;
77568189Seric 				break;
77668189Seric 			}
77768189Seric 		}
77868189Seric 	}
77968189Seric 	/*NOTREACHED*/
78068189Seric }
78168189Seric 
78267830Seric #endif
783