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*68202Seric static char sccsid[] = "@(#)mail.local.c	8.18 (Berkeley) 01/25/95";
1645976Sbostic #endif /* not lint */
1745976Sbostic 
18*68202Seric /*
19*68202Seric  * This is not intended to compile on System V derived systems
20*68202Seric  * such as Solaris or HP-UX, since they use a totally different
21*68202Seric  * approach to mailboxes (essentially, they have a setgid program
22*68202Seric  * rather than setuid, and they rely on the ability to "give away"
23*68202Seric  * files to do their work).  IT IS NOT A BUG that this doesn't
24*68202Seric  * compile on such architectures.
25*68202Seric  */
26*68202Seric 
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));
7967830Seric #endif
8067830Seric 
8167830Seric #ifndef _PATH_LOCTMP
8267830Seric # define _PATH_LOCTMP	"/tmp/local.XXXXXX"
8367830Seric #endif
8467830Seric #ifndef _PATH_MAILDIR
8567830Seric # define _PATH_MAILDIR	"/var/spool/mail"
8667830Seric #endif
8767830Seric 
8868188Seric #ifndef S_ISREG
8968188Seric # define S_ISREG(mode)	(((mode) & _S_IFMT) == S_IFREG)
9067830Seric #endif
9167830Seric 
9259601Sbostic int eval = EX_OK;			/* sysexits.h error value. */
93579Sroot 
9459601Sbostic void		deliver __P((int, char *));
9559601Sbostic void		e_to_sys __P((int));
9659601Sbostic __dead void	err __P((const char *, ...));
9759601Sbostic void		notifybiff __P((char *));
9859601Sbostic int		store __P((char *));
9959601Sbostic void		usage __P((void));
10059601Sbostic void		vwarn __P((const char *, _BSD_VA_LIST_));
10159601Sbostic void		warn __P((const char *, ...));
10250092Sbostic 
10357648Sbostic int
104579Sroot main(argc, argv)
10545976Sbostic 	int argc;
10659601Sbostic 	char *argv[];
107579Sroot {
10845976Sbostic 	struct passwd *pw;
10959601Sbostic 	int ch, fd;
11045976Sbostic 	uid_t uid;
11145976Sbostic 	char *from;
11267830Seric 	extern char *optarg;
11367830Seric 	extern int optind;
114579Sroot 
11568190Seric 	/* make sure we have some open file descriptors */
11668190Seric 	for (fd = 10; fd < 30; fd++)
11768190Seric 		(void) close(fd);
11868190Seric 
11968190Seric 	/* use a reasonable umask */
12068191Seric 	(void) umask(0077);
12168190Seric 
12267830Seric #ifdef LOG_MAIL
12358400Sbostic 	openlog("mail.local", 0, LOG_MAIL);
12467830Seric #else
12567830Seric 	openlog("mail.local", 0);
12667830Seric #endif
12716731Sralph 
12845976Sbostic 	from = NULL;
12945976Sbostic 	while ((ch = getopt(argc, argv, "df:r:")) != EOF)
13045976Sbostic 		switch(ch) {
13159601Sbostic 		case 'd':		/* Backward compatible. */
13216731Sralph 			break;
13316731Sralph 		case 'f':
13459601Sbostic 		case 'r':		/* Backward compatible. */
13559601Sbostic 			if (from != NULL) {
13659601Sbostic 				warn("multiple -f options");
13759601Sbostic 				usage();
13859601Sbostic 			}
13945976Sbostic 			from = optarg;
140579Sroot 			break;
14145976Sbostic 		case '?':
14216731Sralph 		default:
14345976Sbostic 			usage();
14416731Sralph 		}
14545976Sbostic 	argc -= optind;
14645976Sbostic 	argv += optind;
147579Sroot 
14845976Sbostic 	if (!*argv)
14945976Sbostic 		usage();
150579Sroot 
15145976Sbostic 	/*
15245976Sbostic 	 * If from not specified, use the name from getlogin() if the
15345976Sbostic 	 * uid matches, otherwise, use the name from the password file
15445976Sbostic 	 * corresponding to the uid.
15545976Sbostic 	 */
15645976Sbostic 	uid = getuid();
15746034Sbostic 	if (!from && (!(from = getlogin()) ||
15846034Sbostic 	    !(pw = getpwnam(from)) || pw->pw_uid != uid))
15945976Sbostic 		from = (pw = getpwuid(uid)) ? pw->pw_name : "???";
160579Sroot 
16159601Sbostic 	/*
16259601Sbostic 	 * There is no way to distinguish the error status of one delivery
16359601Sbostic 	 * from the rest of the deliveries.  So, if we failed hard on one
16459601Sbostic 	 * or more deliveries, but had no failures on any of the others, we
16559601Sbostic 	 * return a hard failure.  If we failed temporarily on one or more
16659601Sbostic 	 * deliveries, we return a temporary failure regardless of the other
16759601Sbostic 	 * failures.  This results in the delivery being reattempted later
16859601Sbostic 	 * at the expense of repeated failures and multiple deliveries.
16959601Sbostic 	 */
17059601Sbostic 	for (fd = store(from); *argv; ++argv)
17159601Sbostic 		deliver(fd, *argv);
17245976Sbostic 	exit(eval);
173579Sroot }
174579Sroot 
17557648Sbostic int
17645976Sbostic store(from)
17745976Sbostic 	char *from;
178579Sroot {
17945976Sbostic 	FILE *fp;
18045976Sbostic 	time_t tval;
18145976Sbostic 	int fd, eline;
18267830Seric 	char line[2048];
18367830Seric 	char tmpbuf[sizeof _PATH_LOCTMP + 1];
184579Sroot 
18567830Seric 	strcpy(tmpbuf, _PATH_LOCTMP);
18667830Seric 	if ((fd = mkstemp(tmpbuf)) == -1 || (fp = fdopen(fd, "w+")) == NULL) {
18759601Sbostic 		e_to_sys(errno);
18859601Sbostic 		err("unable to open temporary file");
18959601Sbostic 	}
19067830Seric 	(void)unlink(tmpbuf);
191579Sroot 
19245976Sbostic 	(void)time(&tval);
19345976Sbostic 	(void)fprintf(fp, "From %s %s", from, ctime(&tval));
194579Sroot 
19545976Sbostic 	line[0] = '\0';
19645976Sbostic 	for (eline = 1; fgets(line, sizeof(line), stdin);) {
19745976Sbostic 		if (line[0] == '\n')
19845976Sbostic 			eline = 1;
19945976Sbostic 		else {
20060097Sbostic 			if (eline && line[0] == 'F' &&
20160097Sbostic 			    !memcmp(line, "From ", 5))
20245976Sbostic 				(void)putc('>', fp);
20345976Sbostic 			eline = 0;
20445976Sbostic 		}
20545976Sbostic 		(void)fprintf(fp, "%s", line);
20659601Sbostic 		if (ferror(fp)) {
20759601Sbostic 			e_to_sys(errno);
20859601Sbostic 			err("temporary file write error");
20959601Sbostic 		}
210579Sroot 	}
211579Sroot 
21245976Sbostic 	/* If message not newline terminated, need an extra. */
21359601Sbostic 	if (!strchr(line, '\n'))
21445976Sbostic 		(void)putc('\n', fp);
21545976Sbostic 	/* Output a newline; note, empty messages are allowed. */
21645976Sbostic 	(void)putc('\n', fp);
21711893Seric 
21859601Sbostic 	if (fflush(fp) == EOF || ferror(fp)) {
21959601Sbostic 		e_to_sys(errno);
22059601Sbostic 		err("temporary file write error");
22159601Sbostic 	}
22259601Sbostic 	return (fd);
223579Sroot }
224579Sroot 
22559601Sbostic void
22645976Sbostic deliver(fd, name)
22745976Sbostic 	int fd;
22845976Sbostic 	char *name;
229579Sroot {
23066735Sbostic 	struct stat fsb, sb;
23145976Sbostic 	struct passwd *pw;
23259601Sbostic 	int mbfd, nr, nw, off;
23368201Seric 	char *p;
23445976Sbostic 	char biffmsg[100], buf[8*1024], path[MAXPATHLEN];
23554203Sbostic 	off_t curoff;
236579Sroot 
23745976Sbostic 	/*
23845976Sbostic 	 * Disallow delivery to unknown names -- special mailboxes can be
23945976Sbostic 	 * handled in the sendmail aliases file.
24045976Sbostic 	 */
24145976Sbostic 	if (!(pw = getpwnam(name))) {
24259601Sbostic 		if (eval != EX_TEMPFAIL)
24359601Sbostic 			eval = EX_UNAVAILABLE;
24459601Sbostic 		warn("unknown name: %s", name);
24559601Sbostic 		return;
24645976Sbostic 	}
247579Sroot 
24868201Seric 	/*
24968201Seric 	 * Keep name reasonably short to avoid buffer overruns.
25068201Seric 	 *	This isn't necessary on BSD because of the proper
25168201Seric 	 *	definition of snprintf(), but it can cause problems
25268201Seric 	 *	on other systems.
25368201Seric 	 * Also, clear out any bogus characters.
25468201Seric 	 */
25568201Seric 
25668201Seric 	if (strlen(name) > 40)
25768201Seric 		name[40] = '\0';
25868201Seric 	for (p = name; *p != '\0'; p++)
25968201Seric 	{
26068201Seric 		if (!isascii(*p))
26168201Seric 			*p &= 0x7f;
26268201Seric 		else if (!isprint(*p))
26368201Seric 			*p = '.';
26468201Seric 	}
26568201Seric 
26657648Sbostic 	(void)snprintf(path, sizeof(path), "%s/%s", _PATH_MAILDIR, name);
267579Sroot 
268579Sroot 	/*
26966733Sbostic 	 * If the mailbox is linked or a symlink, fail.  There's an obvious
27066733Sbostic 	 * race here, that the file was replaced with a symbolic link after
27166735Sbostic 	 * the lstat returned, but before the open.  We attempt to detect
27266735Sbostic 	 * this by comparing the original stat information and information
27366735Sbostic 	 * returned by an fstat of the file descriptor returned by the open.
27459601Sbostic 	 *
27566735Sbostic 	 * NB: this is a symptom of a larger problem, that the mail spooling
27666735Sbostic 	 * directory is writeable by the wrong users.  If that directory is
27766735Sbostic 	 * writeable, system security is compromised for other reasons, and
27866735Sbostic 	 * it cannot be fixed here.
27966735Sbostic 	 *
28059601Sbostic 	 * If we created the mailbox, set the owner/group.  If that fails,
28159601Sbostic 	 * just return.  Another process may have already opened it, so we
28259601Sbostic 	 * can't unlink it.  Historically, binmail set the owner/group at
28359601Sbostic 	 * each mail delivery.  We no longer do this, assuming that if the
28459601Sbostic 	 * ownership or permissions were changed there was a reason.
28559601Sbostic 	 *
28659601Sbostic 	 * XXX
28759601Sbostic 	 * open(2) should support flock'ing the file.
288579Sroot 	 */
28966736Sbostic tryagain:
29068005Seric 	lockmbox(path);
29159601Sbostic 	if (lstat(path, &sb)) {
29266736Sbostic 		mbfd = open(path,
29366736Sbostic 		    O_APPEND|O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR);
29466736Sbostic 		if (mbfd == -1) {
29566736Sbostic 			if (errno == EEXIST)
29666736Sbostic 				goto tryagain;
29766736Sbostic 		} else if (fchown(mbfd, pw->pw_uid, pw->pw_gid)) {
29859601Sbostic 			e_to_sys(errno);
29959601Sbostic 			warn("chown %u.%u: %s", pw->pw_uid, pw->pw_gid, name);
30068005Seric 			unlockmbox();
30159601Sbostic 			return;
30259601Sbostic 		}
30368188Seric 	} else if (sb.st_nlink != 1 || !S_ISREG(sb.st_mode)) {
30459601Sbostic 		e_to_sys(errno);
30568188Seric 		warn("%s: irregular file", path);
30668005Seric 		unlockmbox();
30759601Sbostic 		return;
30868060Seric 	} else if (sb.st_uid != pw->pw_uid) {
30968060Seric 		warn("%s: wrong ownership (%d)", path, sb.st_uid);
31068060Seric 		unlockmbox();
31168060Seric 		return;
31266733Sbostic 	} else {
31359601Sbostic 		mbfd = open(path, O_APPEND|O_WRONLY, 0);
31466735Sbostic 		if (mbfd != -1 &&
31566735Sbostic 		    (fstat(mbfd, &fsb) || fsb.st_nlink != 1 ||
31668188Seric 		    !S_ISREG(fsb.st_mode) || sb.st_dev != fsb.st_dev ||
31768188Seric 		    sb.st_ino != fsb.st_ino || sb.st_uid != fsb.st_uid)) {
31866735Sbostic 			warn("%s: file changed after open", path);
31966733Sbostic 			(void)close(mbfd);
32068005Seric 			unlockmbox();
32166733Sbostic 			return;
32266733Sbostic 		}
32366733Sbostic 	}
32459601Sbostic 
32559601Sbostic 	if (mbfd == -1) {
32659601Sbostic 		e_to_sys(errno);
32759601Sbostic 		warn("%s: %s", path, strerror(errno));
32868005Seric 		unlockmbox();
32959601Sbostic 		return;
33045976Sbostic 	}
331579Sroot 
33259601Sbostic 	/* Wait until we can get a lock on the file. */
33345976Sbostic 	if (flock(mbfd, LOCK_EX)) {
33459601Sbostic 		e_to_sys(errno);
33559601Sbostic 		warn("%s: %s", path, strerror(errno));
33668005Seric 		unlockmbox();
33759601Sbostic 		goto err1;
33845976Sbostic 	}
339579Sroot 
34059601Sbostic 	/* Get the starting offset of the new message for biff. */
34154203Sbostic 	curoff = lseek(mbfd, (off_t)0, SEEK_END);
34268021Seric 	(void)snprintf(biffmsg, sizeof(biffmsg),
34368021Seric 		sizeof curoff > sizeof(long) ? "%s@%qd\n" : "%s@%ld\n",
34468021Seric 		name, curoff);
34559601Sbostic 
34659601Sbostic 	/* Copy the message into the file. */
34754203Sbostic 	if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) {
34859601Sbostic 		e_to_sys(errno);
34959601Sbostic 		warn("temporary file: %s", strerror(errno));
35059601Sbostic 		goto err1;
35145976Sbostic 	}
35245976Sbostic 	while ((nr = read(fd, buf, sizeof(buf))) > 0)
35368201Seric 		for (off = 0; off < nr; off += nw)
35468201Seric 			if ((nw = write(mbfd, buf + off, nr - off)) < 0) {
35559601Sbostic 				e_to_sys(errno);
35659601Sbostic 				warn("%s: %s", path, strerror(errno));
35759601Sbostic 				goto err2;;
35845976Sbostic 			}
35945976Sbostic 	if (nr < 0) {
36059601Sbostic 		e_to_sys(errno);
36159601Sbostic 		warn("temporary file: %s", strerror(errno));
36259601Sbostic 		goto err2;;
363579Sroot 	}
364579Sroot 
36559601Sbostic 	/* Flush to disk, don't wait for update. */
36659601Sbostic 	if (fsync(mbfd)) {
36759601Sbostic 		e_to_sys(errno);
36859601Sbostic 		warn("%s: %s", path, strerror(errno));
36959601Sbostic err2:		(void)ftruncate(mbfd, curoff);
37059601Sbostic err1:		(void)close(mbfd);
37168005Seric 		unlockmbox();
37259601Sbostic 		return;
37359601Sbostic 	}
37459601Sbostic 
37559601Sbostic 	/* Close and check -- NFS doesn't write until the close. */
37659601Sbostic 	if (close(mbfd)) {
37759601Sbostic 		e_to_sys(errno);
37859601Sbostic 		warn("%s: %s", path, strerror(errno));
37968005Seric 		unlockmbox();
38059601Sbostic 		return;
38159601Sbostic 	}
382579Sroot 
38368005Seric 	unlockmbox();
38459601Sbostic 	notifybiff(biffmsg);
385579Sroot }
386579Sroot 
38768005Seric /*
38868005Seric  * user.lock files are necessary for compatibility with other
38968005Seric  * systems, e.g., when the mail spool file is NFS exported.
39068005Seric  * Alas, mailbox locking is more than just a local matter.
39168005Seric  * EPA 11/94.
39268005Seric  */
39368005Seric 
39468201Seric char	lockname[MAXPATHLEN];
39568005Seric int	locked = 0;
39668005Seric 
39768005Seric lockmbox(path)
39868005Seric 	char *path;
39968005Seric {
40068005Seric 	int statfailed = 0;
40168005Seric 
40268005Seric 	if (locked)
40368005Seric 		return;
40468005Seric 	sprintf(lockname, "%s.lock", path);
40568005Seric 	for (;; sleep(5)) {
40668005Seric 		int fd;
40768005Seric 		struct stat st;
40868005Seric 		time_t now;
40968005Seric 
41068187Seric 		fd = open(lockname, O_WRONLY|O_EXCL|O_CREAT, 0);
41168187Seric 		if (fd >= 0) {
41268005Seric 			locked = 1;
41368187Seric 			close(fd);
41468005Seric 			return;
41568005Seric 		}
41668005Seric 		if (stat(lockname, &st) < 0) {
41768005Seric 			if (statfailed++ > 5)
41868005Seric 				return;
41968005Seric 			continue;
42068005Seric 		}
42168005Seric 		statfailed = 0;
42268005Seric 		time(&now);
42368005Seric 		if (now < st.st_ctime + 300)
42468005Seric 			continue;
42568005Seric 		unlink(lockname);
42668005Seric 	}
42768005Seric }
42868005Seric 
42968005Seric unlockmbox()
43068005Seric {
43168005Seric 	if (!locked)
43268005Seric 		return;
43368005Seric 	unlink(lockname);
43468005Seric 	locked = 0;
43568005Seric }
43668005Seric 
43750092Sbostic void
43816731Sralph notifybiff(msg)
43916731Sralph 	char *msg;
44016731Sralph {
44116731Sralph 	static struct sockaddr_in addr;
44216731Sralph 	static int f = -1;
44345976Sbostic 	struct hostent *hp;
44445976Sbostic 	struct servent *sp;
44545976Sbostic 	int len;
44616731Sralph 
44745976Sbostic 	if (!addr.sin_family) {
44845976Sbostic 		/* Be silent if biff service not available. */
44945976Sbostic 		if (!(sp = getservbyname("biff", "udp")))
45045976Sbostic 			return;
45145976Sbostic 		if (!(hp = gethostbyname("localhost"))) {
45259601Sbostic 			warn("localhost: %s", strerror(errno));
45345976Sbostic 			return;
45416731Sralph 		}
45545976Sbostic 		addr.sin_family = hp->h_addrtype;
45667830Seric 		memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
45745976Sbostic 		addr.sin_port = sp->s_port;
45816731Sralph 	}
45945976Sbostic 	if (f < 0 && (f = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
46059601Sbostic 		warn("socket: %s", strerror(errno));
46145976Sbostic 		return;
46216731Sralph 	}
46345976Sbostic 	len = strlen(msg) + 1;
46446672Sbostic 	if (sendto(f, msg, len, 0, (struct sockaddr *)&addr, sizeof(addr))
46546672Sbostic 	    != len)
46659601Sbostic 		warn("sendto biff: %s", strerror(errno));
46716731Sralph }
46816731Sralph 
46950092Sbostic void
47045976Sbostic usage()
471579Sroot {
47259601Sbostic 	eval = EX_USAGE;
47359601Sbostic 	err("usage: mail.local [-f from] user ...");
474579Sroot }
475579Sroot 
47650092Sbostic #if __STDC__
47765558Sbostic void
47859601Sbostic err(const char *fmt, ...)
47950092Sbostic #else
48064993Smckusick void
48159601Sbostic err(fmt, va_alist)
48259601Sbostic 	const char *fmt;
48359601Sbostic 	va_dcl
48450092Sbostic #endif
48559601Sbostic {
48659601Sbostic 	va_list ap;
48750092Sbostic 
48859601Sbostic #if __STDC__
48959601Sbostic 	va_start(ap, fmt);
49059601Sbostic #else
49159601Sbostic 	va_start(ap);
49259601Sbostic #endif
49359601Sbostic 	vwarn(fmt, ap);
49459601Sbostic 	va_end(ap);
49559601Sbostic 
49659601Sbostic 	exit(eval);
49759601Sbostic }
49859601Sbostic 
49950092Sbostic void
50050092Sbostic #if __STDC__
50159601Sbostic warn(const char *fmt, ...)
50250092Sbostic #else
50359601Sbostic warn(fmt, va_alist)
50459601Sbostic 	const char *fmt;
50550092Sbostic 	va_dcl
50650092Sbostic #endif
507579Sroot {
50845976Sbostic 	va_list ap;
50959601Sbostic 
51050092Sbostic #if __STDC__
51146554Sbostic 	va_start(ap, fmt);
51250092Sbostic #else
51350092Sbostic 	va_start(ap);
51450092Sbostic #endif
51559601Sbostic 	vwarn(fmt, ap);
51659601Sbostic 	va_end(ap);
51759601Sbostic }
51859601Sbostic 
51959601Sbostic void
52059601Sbostic vwarn(fmt, ap)
52159601Sbostic 	const char *fmt;
52259601Sbostic 	_BSD_VA_LIST_ ap;
52359601Sbostic {
52458400Sbostic 	/*
52559601Sbostic 	 * Log the message to stderr.
52659601Sbostic 	 *
52759601Sbostic 	 * Don't use LOG_PERROR as an openlog() flag to do this,
52859601Sbostic 	 * it's not portable enough.
52958400Sbostic 	 */
53059601Sbostic 	if (eval != EX_USAGE)
53159601Sbostic 		(void)fprintf(stderr, "mail.local: ");
53258396Sbostic 	(void)vfprintf(stderr, fmt, ap);
53358400Sbostic 	(void)fprintf(stderr, "\n");
53459601Sbostic 
53567830Seric #ifndef ultrix
53659601Sbostic 	/* Log the message to syslog. */
53745976Sbostic 	vsyslog(LOG_ERR, fmt, ap);
53867830Seric #else
53967830Seric 	{
54067830Seric 		char fmtbuf[10240];
54167830Seric 
54267830Seric 		(void) sprintf(fmtbuf, fmt, ap);
54367830Seric 		syslog(LOG_ERR, "%s", fmtbuf);
54467830Seric 	}
54567830Seric #endif
546579Sroot }
54759601Sbostic 
54859601Sbostic /*
54959601Sbostic  * e_to_sys --
55059601Sbostic  *	Guess which errno's are temporary.  Gag me.
55159601Sbostic  */
55259601Sbostic void
55359601Sbostic e_to_sys(num)
55459601Sbostic 	int num;
55559601Sbostic {
55659601Sbostic 	/* Temporary failures override hard errors. */
55759601Sbostic 	if (eval == EX_TEMPFAIL)
55859601Sbostic 		return;
55959601Sbostic 
56059601Sbostic 	switch(num) {		/* Hopefully temporary errors. */
56159601Sbostic #ifdef EAGAIN
56259601Sbostic 	case EAGAIN:		/* Resource temporarily unavailable */
56359601Sbostic #endif
56459601Sbostic #ifdef EDQUOT
56559601Sbostic 	case EDQUOT:		/* Disc quota exceeded */
56659601Sbostic #endif
56759601Sbostic #ifdef EBUSY
56859601Sbostic 	case EBUSY:		/* Device busy */
56959601Sbostic #endif
57059601Sbostic #ifdef EPROCLIM
57159601Sbostic 	case EPROCLIM:		/* Too many processes */
57259601Sbostic #endif
57359601Sbostic #ifdef EUSERS
57459601Sbostic 	case EUSERS:		/* Too many users */
57559601Sbostic #endif
57659601Sbostic #ifdef ECONNABORTED
57759601Sbostic 	case ECONNABORTED:	/* Software caused connection abort */
57859601Sbostic #endif
57959601Sbostic #ifdef ECONNREFUSED
58059601Sbostic 	case ECONNREFUSED:	/* Connection refused */
58159601Sbostic #endif
58259601Sbostic #ifdef ECONNRESET
58359601Sbostic 	case ECONNRESET:	/* Connection reset by peer */
58459601Sbostic #endif
58559601Sbostic #ifdef EDEADLK
58659601Sbostic 	case EDEADLK:		/* Resource deadlock avoided */
58759601Sbostic #endif
58859601Sbostic #ifdef EFBIG
58959601Sbostic 	case EFBIG:		/* File too large */
59059601Sbostic #endif
59159601Sbostic #ifdef EHOSTDOWN
59259601Sbostic 	case EHOSTDOWN:		/* Host is down */
59359601Sbostic #endif
59459601Sbostic #ifdef EHOSTUNREACH
59559601Sbostic 	case EHOSTUNREACH:	/* No route to host */
59659601Sbostic #endif
59759601Sbostic #ifdef EMFILE
59859601Sbostic 	case EMFILE:		/* Too many open files */
59959601Sbostic #endif
60059601Sbostic #ifdef ENETDOWN
60159601Sbostic 	case ENETDOWN:		/* Network is down */
60259601Sbostic #endif
60359601Sbostic #ifdef ENETRESET
60459601Sbostic 	case ENETRESET:		/* Network dropped connection on reset */
60559601Sbostic #endif
60659601Sbostic #ifdef ENETUNREACH
60759601Sbostic 	case ENETUNREACH:	/* Network is unreachable */
60859601Sbostic #endif
60959601Sbostic #ifdef ENFILE
61059601Sbostic 	case ENFILE:		/* Too many open files in system */
61159601Sbostic #endif
61259601Sbostic #ifdef ENOBUFS
61359601Sbostic 	case ENOBUFS:		/* No buffer space available */
61459601Sbostic #endif
61559601Sbostic #ifdef ENOMEM
61659601Sbostic 	case ENOMEM:		/* Cannot allocate memory */
61759601Sbostic #endif
61859601Sbostic #ifdef ENOSPC
61959601Sbostic 	case ENOSPC:		/* No space left on device */
62059601Sbostic #endif
62159601Sbostic #ifdef EROFS
62259601Sbostic 	case EROFS:		/* Read-only file system */
62359601Sbostic #endif
62459601Sbostic #ifdef ESTALE
62559601Sbostic 	case ESTALE:		/* Stale NFS file handle */
62659601Sbostic #endif
62759601Sbostic #ifdef ETIMEDOUT
62859601Sbostic 	case ETIMEDOUT:		/* Connection timed out */
62959601Sbostic #endif
63067830Seric #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN && EWOULDBLOCK != EDEADLK
63159601Sbostic 	case EWOULDBLOCK:	/* Operation would block. */
63259601Sbostic #endif
63359601Sbostic 		eval = EX_TEMPFAIL;
63459601Sbostic 		break;
63559601Sbostic 	default:
63659601Sbostic 		eval = EX_UNAVAILABLE;
63759601Sbostic 		break;
63859601Sbostic 	}
63959601Sbostic }
64067830Seric 
64167830Seric #ifndef BSD4_4
64267830Seric 
64367830Seric char *
64467830Seric strerror(eno)
64567830Seric 	int eno;
64667830Seric {
64767830Seric 	extern int sys_nerr;
64867830Seric 	extern char *sys_errlist[];
64967830Seric 	static char ebuf[60];
65067830Seric 
65167830Seric 	if (eno >= 0 && eno <= sys_nerr)
65267830Seric 		return sys_errlist[eno];
65367830Seric 	(void) sprintf(ebuf, "Error %d", eno);
65467830Seric 	return ebuf;
65567830Seric }
65667830Seric 
65767830Seric #if __STDC__
65867830Seric snprintf(char *buf, int bufsiz, const char *fmt, ...)
65967830Seric #else
66067830Seric snprintf(buf, bufsiz, fmt, va_alist)
66167830Seric 	char *buf;
66267830Seric 	int bufsiz;
66367830Seric 	const char *fmt;
66467830Seric 	va_dcl
66567830Seric #endif
66667830Seric {
66767830Seric 	va_list ap;
66867830Seric 
66967830Seric #if __STDC__
67067830Seric 	va_start(ap, fmt);
67167830Seric #else
67267830Seric 	va_start(ap);
67367830Seric #endif
67467830Seric 	vsprintf(buf, fmt, ap);
67567830Seric 	va_end(ap);
67667830Seric }
67767830Seric 
67867830Seric #endif
67967830Seric 
68067830Seric #ifdef ultrix
68167830Seric 
68268189Seric /*
68368189Seric  * Copyright (c) 1987, 1993
68468189Seric  *	The Regents of the University of California.  All rights reserved.
68568189Seric  *
68668189Seric  * Redistribution and use in source and binary forms, with or without
68768189Seric  * modification, are permitted provided that the following conditions
68868189Seric  * are met:
68968189Seric  * 1. Redistributions of source code must retain the above copyright
69068189Seric  *    notice, this list of conditions and the following disclaimer.
69168189Seric  * 2. Redistributions in binary form must reproduce the above copyright
69268189Seric  *    notice, this list of conditions and the following disclaimer in the
69368189Seric  *    documentation and/or other materials provided with the distribution.
69468189Seric  * 3. All advertising materials mentioning features or use of this software
69568189Seric  *    must display the following acknowledgement:
69668189Seric  *	This product includes software developed by the University of
69768189Seric  *	California, Berkeley and its contributors.
69868189Seric  * 4. Neither the name of the University nor the names of its contributors
69968189Seric  *    may be used to endorse or promote products derived from this software
70068189Seric  *    without specific prior written permission.
70168189Seric  *
70268189Seric  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
70368189Seric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70468189Seric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70568189Seric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70668189Seric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70768189Seric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70868189Seric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70968189Seric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71068189Seric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71168189Seric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71268189Seric  * SUCH DAMAGE.
71368189Seric  */
71468189Seric 
71568189Seric #if defined(LIBC_SCCS) && !defined(lint)
71668189Seric static char sccsid[] = "@(#)mktemp.c	8.1 (Berkeley) 6/4/93";
71768189Seric #endif /* LIBC_SCCS and not lint */
71868189Seric 
71968189Seric #include <sys/types.h>
72068189Seric #include <sys/stat.h>
72168189Seric #include <fcntl.h>
72268189Seric #include <errno.h>
72368189Seric #include <stdio.h>
72468189Seric #include <ctype.h>
72568189Seric 
72668189Seric static int _gettemp();
72768189Seric 
72868189Seric mkstemp(path)
72968189Seric 	char *path;
73067830Seric {
73167830Seric 	int fd;
73267830Seric 
73368189Seric 	return (_gettemp(path, &fd) ? fd : -1);
73467830Seric }
73567830Seric 
73668189Seric /*
73768189Seric char *
73868189Seric mktemp(path)
73968189Seric 	char *path;
74068189Seric {
74168189Seric 	return(_gettemp(path, (int *)NULL) ? path : (char *)NULL);
74268189Seric }
74368189Seric */
74468189Seric 
74568189Seric static
74668189Seric _gettemp(path, doopen)
74768189Seric 	char *path;
74868189Seric 	register int *doopen;
74968189Seric {
75068189Seric 	extern int errno;
75168189Seric 	register char *start, *trv;
75268189Seric 	struct stat sbuf;
75368189Seric 	u_int pid;
75468189Seric 
75568189Seric 	pid = getpid();
75668189Seric 	for (trv = path; *trv; ++trv);		/* extra X's get set to 0's */
75768189Seric 	while (*--trv == 'X') {
75868189Seric 		*trv = (pid % 10) + '0';
75968189Seric 		pid /= 10;
76068189Seric 	}
76168189Seric 
76268189Seric 	/*
76368189Seric 	 * check the target directory; if you have six X's and it
76468189Seric 	 * doesn't exist this runs for a *very* long time.
76568189Seric 	 */
76668189Seric 	for (start = trv + 1;; --trv) {
76768189Seric 		if (trv <= path)
76868189Seric 			break;
76968189Seric 		if (*trv == '/') {
77068189Seric 			*trv = '\0';
77168189Seric 			if (stat(path, &sbuf))
77268189Seric 				return(0);
77368189Seric 			if (!S_ISDIR(sbuf.st_mode)) {
77468189Seric 				errno = ENOTDIR;
77568189Seric 				return(0);
77668189Seric 			}
77768189Seric 			*trv = '/';
77868189Seric 			break;
77968189Seric 		}
78068189Seric 	}
78168189Seric 
78268189Seric 	for (;;) {
78368189Seric 		if (doopen) {
78468189Seric 			if ((*doopen =
78568189Seric 			    open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
78668189Seric 				return(1);
78768189Seric 			if (errno != EEXIST)
78868189Seric 				return(0);
78968189Seric 		}
79068189Seric 		else if (stat(path, &sbuf))
79168189Seric 			return(errno == ENOENT ? 1 : 0);
79268189Seric 
79368189Seric 		/* tricky little algorithm for backward compatibility */
79468189Seric 		for (trv = start;;) {
79568189Seric 			if (!*trv)
79668189Seric 				return(0);
79768189Seric 			if (*trv == 'z')
79868189Seric 				*trv++ = 'a';
79968189Seric 			else {
80068189Seric 				if (isdigit(*trv))
80168189Seric 					*trv = 'a';
80268189Seric 				else
80368189Seric 					++*trv;
80468189Seric 				break;
80568189Seric 			}
80668189Seric 		}
80768189Seric 	}
80868189Seric 	/*NOTREACHED*/
80968189Seric }
81068189Seric 
81167830Seric #endif
812