10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
50Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
60Sstevel@tonic-gate  * are met:
70Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
80Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
90Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
100Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
110Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
140Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
150Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
160Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
170Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
180Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
190Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
200Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
210Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
220Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate /*
253946Sjp161948  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
260Sstevel@tonic-gate  * Use is subject to license terms.
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include "includes.h"
300Sstevel@tonic-gate RCSID("$OpenBSD: misc.c,v 1.19 2002/03/04 17:27:39 stevesk Exp $");
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include "misc.h"
350Sstevel@tonic-gate #include "log.h"
360Sstevel@tonic-gate #include "xmalloc.h"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /* remove newline at end of string */
390Sstevel@tonic-gate char *
400Sstevel@tonic-gate chop(char *s)
410Sstevel@tonic-gate {
420Sstevel@tonic-gate 	char *t = s;
430Sstevel@tonic-gate 	while (*t) {
440Sstevel@tonic-gate 		if (*t == '\n' || *t == '\r') {
450Sstevel@tonic-gate 			*t = '\0';
460Sstevel@tonic-gate 			return s;
470Sstevel@tonic-gate 		}
480Sstevel@tonic-gate 		t++;
490Sstevel@tonic-gate 	}
500Sstevel@tonic-gate 	return s;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate }
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /* set/unset filedescriptor to non-blocking */
550Sstevel@tonic-gate void
560Sstevel@tonic-gate set_nonblock(int fd)
570Sstevel@tonic-gate {
580Sstevel@tonic-gate 	int val;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	val = fcntl(fd, F_GETFL, 0);
610Sstevel@tonic-gate 	if (val < 0) {
620Sstevel@tonic-gate 		error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
630Sstevel@tonic-gate 		return;
640Sstevel@tonic-gate 	}
650Sstevel@tonic-gate 	if (val & O_NONBLOCK) {
660Sstevel@tonic-gate 		debug2("fd %d is O_NONBLOCK", fd);
670Sstevel@tonic-gate 		return;
680Sstevel@tonic-gate 	}
690Sstevel@tonic-gate 	debug("fd %d setting O_NONBLOCK", fd);
700Sstevel@tonic-gate 	val |= O_NONBLOCK;
710Sstevel@tonic-gate 	if (fcntl(fd, F_SETFL, val) == -1)
720Sstevel@tonic-gate 		debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
730Sstevel@tonic-gate 		    fd, strerror(errno));
740Sstevel@tonic-gate }
750Sstevel@tonic-gate 
760Sstevel@tonic-gate void
770Sstevel@tonic-gate unset_nonblock(int fd)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	int val;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	val = fcntl(fd, F_GETFL, 0);
820Sstevel@tonic-gate 	if (val < 0) {
830Sstevel@tonic-gate 		error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
840Sstevel@tonic-gate 		return;
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate 	if (!(val & O_NONBLOCK)) {
870Sstevel@tonic-gate 		debug2("fd %d is not O_NONBLOCK", fd);
880Sstevel@tonic-gate 		return;
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 	debug("fd %d clearing O_NONBLOCK", fd);
910Sstevel@tonic-gate 	val &= ~O_NONBLOCK;
920Sstevel@tonic-gate 	if (fcntl(fd, F_SETFL, val) == -1)
930Sstevel@tonic-gate 		debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
940Sstevel@tonic-gate 		    fd, strerror(errno));
950Sstevel@tonic-gate }
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /* disable nagle on socket */
980Sstevel@tonic-gate void
990Sstevel@tonic-gate set_nodelay(int fd)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	int opt;
1020Sstevel@tonic-gate 	socklen_t optlen;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	optlen = sizeof opt;
1050Sstevel@tonic-gate 	if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
1060Sstevel@tonic-gate 		error("getsockopt TCP_NODELAY: %.100s", strerror(errno));
1070Sstevel@tonic-gate 		return;
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate 	if (opt == 1) {
1100Sstevel@tonic-gate 		debug2("fd %d is TCP_NODELAY", fd);
1110Sstevel@tonic-gate 		return;
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 	opt = 1;
1140Sstevel@tonic-gate 	debug("fd %d setting TCP_NODELAY", fd);
1150Sstevel@tonic-gate 	if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
1160Sstevel@tonic-gate 		error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /* Characters considered whitespace in strsep calls. */
1200Sstevel@tonic-gate #define WHITESPACE " \t\r\n"
1210Sstevel@tonic-gate 
1223946Sjp161948 /*
1233946Sjp161948  * Function returns a pointer to the 1st token on the line. Such a token can
1243946Sjp161948  * be an empty string in the case of '*s' equal to " value". It changes the
1253946Sjp161948  * first whitespace token or '=' character after the 1st token to '\0'. Upon
1263946Sjp161948  * return it changes '*s' to point to the first character of the next token.
1273946Sjp161948  * That token may be an empty string if the 1st token was followed only by
1283946Sjp161948  * whitespace or it could be a NULL pointer if the line contained one token
1293946Sjp161948  * only.
1303946Sjp161948  */
1310Sstevel@tonic-gate char *
1320Sstevel@tonic-gate strdelim(char **s)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	char *old;
1350Sstevel@tonic-gate 	int wspace = 0;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	if (*s == NULL)
1380Sstevel@tonic-gate 		return NULL;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	old = *s;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	*s = strpbrk(*s, WHITESPACE "=");
1430Sstevel@tonic-gate 	if (*s == NULL)
1440Sstevel@tonic-gate 		return (old);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	/* Allow only one '=' to be skipped */
1470Sstevel@tonic-gate 	if (*s[0] == '=')
1480Sstevel@tonic-gate 		wspace = 1;
1490Sstevel@tonic-gate 	*s[0] = '\0';
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	*s += strspn(*s + 1, WHITESPACE) + 1;
1520Sstevel@tonic-gate 	if (*s[0] == '=' && !wspace)
1530Sstevel@tonic-gate 		*s += strspn(*s + 1, WHITESPACE) + 1;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	return (old);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate struct passwd *
1590Sstevel@tonic-gate pwcopy(struct passwd *pw)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	struct passwd *copy = xmalloc(sizeof(*copy));
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	memset(copy, 0, sizeof(*copy));
1640Sstevel@tonic-gate 	copy->pw_name = xstrdup(pw->pw_name);
1650Sstevel@tonic-gate 	copy->pw_passwd = xstrdup(pw->pw_passwd);
1660Sstevel@tonic-gate 	copy->pw_gecos = xstrdup(pw->pw_gecos);
1670Sstevel@tonic-gate 	copy->pw_uid = pw->pw_uid;
1680Sstevel@tonic-gate 	copy->pw_gid = pw->pw_gid;
1690Sstevel@tonic-gate #ifdef HAVE_PW_EXPIRE_IN_PASSWD
1700Sstevel@tonic-gate 	copy->pw_expire = pw->pw_expire;
1710Sstevel@tonic-gate #endif
1720Sstevel@tonic-gate #ifdef HAVE_PW_CHANGE_IN_PASSWD
1730Sstevel@tonic-gate 	copy->pw_change = pw->pw_change;
1740Sstevel@tonic-gate #endif
1750Sstevel@tonic-gate #ifdef HAVE_PW_CLASS_IN_PASSWD
1760Sstevel@tonic-gate 	copy->pw_class = xstrdup(pw->pw_class);
1770Sstevel@tonic-gate #endif
1780Sstevel@tonic-gate 	copy->pw_dir = xstrdup(pw->pw_dir);
1790Sstevel@tonic-gate 	copy->pw_shell = xstrdup(pw->pw_shell);
1800Sstevel@tonic-gate 	return copy;
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate void
1840Sstevel@tonic-gate pwfree(struct passwd **pw)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate 	struct passwd *p;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (pw == NULL || *pw == NULL)
1890Sstevel@tonic-gate 		return;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	p = *pw;
1920Sstevel@tonic-gate 	*pw = NULL;
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	xfree(p->pw_name);
1950Sstevel@tonic-gate 	xfree(p->pw_passwd);
1960Sstevel@tonic-gate 	xfree(p->pw_gecos);
1970Sstevel@tonic-gate #ifdef HAVE_PW_CLASS_IN_PASSWD
1980Sstevel@tonic-gate 	xfree(p->pw_class);
1990Sstevel@tonic-gate #endif
2000Sstevel@tonic-gate 	xfree(p->pw_dir);
2010Sstevel@tonic-gate 	xfree(p->pw_shell);
2020Sstevel@tonic-gate 	xfree(p);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate  * Convert ASCII string to TCP/IP port number.
2070Sstevel@tonic-gate  * Port must be >0 and <=65535.
2080Sstevel@tonic-gate  * Return 0 if invalid.
2090Sstevel@tonic-gate  */
2100Sstevel@tonic-gate int
2110Sstevel@tonic-gate a2port(const char *s)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate 	long port;
2140Sstevel@tonic-gate 	char *endp;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	errno = 0;
2170Sstevel@tonic-gate 	port = strtol(s, &endp, 0);
2180Sstevel@tonic-gate 	if (s == endp || *endp != '\0' ||
2190Sstevel@tonic-gate 	    (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
2200Sstevel@tonic-gate 	    port <= 0 || port > 65535)
2210Sstevel@tonic-gate 		return 0;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	return port;
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate #define SECONDS		1
2270Sstevel@tonic-gate #define MINUTES		(SECONDS * 60)
2280Sstevel@tonic-gate #define HOURS		(MINUTES * 60)
2290Sstevel@tonic-gate #define DAYS		(HOURS * 24)
2300Sstevel@tonic-gate #define WEEKS		(DAYS * 7)
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate /*
2330Sstevel@tonic-gate  * Convert a time string into seconds; format is
2340Sstevel@tonic-gate  * a sequence of:
2350Sstevel@tonic-gate  *      time[qualifier]
2360Sstevel@tonic-gate  *
2370Sstevel@tonic-gate  * Valid time qualifiers are:
2380Sstevel@tonic-gate  *      <none>  seconds
2390Sstevel@tonic-gate  *      s|S     seconds
2400Sstevel@tonic-gate  *      m|M     minutes
2410Sstevel@tonic-gate  *      h|H     hours
2420Sstevel@tonic-gate  *      d|D     days
2430Sstevel@tonic-gate  *      w|W     weeks
2440Sstevel@tonic-gate  *
2450Sstevel@tonic-gate  * Examples:
2460Sstevel@tonic-gate  *      90m     90 minutes
2470Sstevel@tonic-gate  *      1h30m   90 minutes
2480Sstevel@tonic-gate  *      2d      2 days
2490Sstevel@tonic-gate  *      1w      1 week
2500Sstevel@tonic-gate  *
2510Sstevel@tonic-gate  * Return -1 if time string is invalid.
2520Sstevel@tonic-gate  */
2530Sstevel@tonic-gate long
2540Sstevel@tonic-gate convtime(const char *s)
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate 	long total, secs;
2570Sstevel@tonic-gate 	const char *p;
2580Sstevel@tonic-gate 	char *endp;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	errno = 0;
2610Sstevel@tonic-gate 	total = 0;
2620Sstevel@tonic-gate 	p = s;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	if (p == NULL || *p == '\0')
2650Sstevel@tonic-gate 		return -1;
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	while (*p) {
2680Sstevel@tonic-gate 		secs = strtol(p, &endp, 10);
2690Sstevel@tonic-gate 		if (p == endp ||
2700Sstevel@tonic-gate 		    (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
2710Sstevel@tonic-gate 		    secs < 0)
2720Sstevel@tonic-gate 			return -1;
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 		switch (*endp++) {
2750Sstevel@tonic-gate 		case '\0':
2760Sstevel@tonic-gate 			endp--;
2770Sstevel@tonic-gate 			break;
2780Sstevel@tonic-gate 		case 's':
2790Sstevel@tonic-gate 		case 'S':
2800Sstevel@tonic-gate 			break;
2810Sstevel@tonic-gate 		case 'm':
2820Sstevel@tonic-gate 		case 'M':
2830Sstevel@tonic-gate 			secs *= MINUTES;
2840Sstevel@tonic-gate 			break;
2850Sstevel@tonic-gate 		case 'h':
2860Sstevel@tonic-gate 		case 'H':
2870Sstevel@tonic-gate 			secs *= HOURS;
2880Sstevel@tonic-gate 			break;
2890Sstevel@tonic-gate 		case 'd':
2900Sstevel@tonic-gate 		case 'D':
2910Sstevel@tonic-gate 			secs *= DAYS;
2920Sstevel@tonic-gate 			break;
2930Sstevel@tonic-gate 		case 'w':
2940Sstevel@tonic-gate 		case 'W':
2950Sstevel@tonic-gate 			secs *= WEEKS;
2960Sstevel@tonic-gate 			break;
2970Sstevel@tonic-gate 		default:
2980Sstevel@tonic-gate 			return -1;
2990Sstevel@tonic-gate 		}
3000Sstevel@tonic-gate 		total += secs;
3010Sstevel@tonic-gate 		if (total < 0)
3020Sstevel@tonic-gate 			return -1;
3030Sstevel@tonic-gate 		p = endp;
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	return total;
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate char *
3100Sstevel@tonic-gate cleanhostname(char *host)
3110Sstevel@tonic-gate {
3120Sstevel@tonic-gate 	if (*host == '[' && host[strlen(host) - 1] == ']') {
3130Sstevel@tonic-gate 		host[strlen(host) - 1] = '\0';
3140Sstevel@tonic-gate 		return (host + 1);
3150Sstevel@tonic-gate 	} else
3160Sstevel@tonic-gate 		return host;
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate char *
3200Sstevel@tonic-gate colon(char *cp)
3210Sstevel@tonic-gate {
3220Sstevel@tonic-gate 	int flag = 0;
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	if (*cp == ':')		/* Leading colon is part of file name. */
3250Sstevel@tonic-gate 		return (0);
3260Sstevel@tonic-gate 	if (*cp == '[')
3270Sstevel@tonic-gate 		flag = 1;
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	for (; *cp; ++cp) {
3300Sstevel@tonic-gate 		if (*cp == '@' && *(cp+1) == '[')
3310Sstevel@tonic-gate 			flag = 1;
3320Sstevel@tonic-gate 		if (*cp == ']' && *(cp+1) == ':' && flag)
3330Sstevel@tonic-gate 			return (cp+1);
3340Sstevel@tonic-gate 		if (*cp == ':' && !flag)
3350Sstevel@tonic-gate 			return (cp);
3360Sstevel@tonic-gate 		if (*cp == '/')
3370Sstevel@tonic-gate 			return (0);
3380Sstevel@tonic-gate 	}
3390Sstevel@tonic-gate 	return (0);
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate /* function to assist building execv() arguments */
3430Sstevel@tonic-gate /* PRINTFLIKE2 */
3440Sstevel@tonic-gate void
3450Sstevel@tonic-gate addargs(arglist *args, char *fmt, ...)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	va_list ap;
3480Sstevel@tonic-gate 	char buf[1024];
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	va_start(ap, fmt);
3510Sstevel@tonic-gate 	vsnprintf(buf, sizeof(buf), fmt, ap);
3520Sstevel@tonic-gate 	va_end(ap);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	if (args->list == NULL) {
3550Sstevel@tonic-gate 		args->nalloc = 32;
3560Sstevel@tonic-gate 		args->num = 0;
3570Sstevel@tonic-gate 	} else if (args->num+2 >= args->nalloc)
3580Sstevel@tonic-gate 		args->nalloc *= 2;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	args->list = xrealloc(args->list, args->nalloc * sizeof(char *));
3610Sstevel@tonic-gate 	args->list[args->num++] = xstrdup(buf);
3620Sstevel@tonic-gate 	args->list[args->num] = NULL;
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate 
3652780Sjp161948 void
3665087Sjp161948 replacearg(arglist *args, u_int which, char *fmt, ...)
3675087Sjp161948 {
3685087Sjp161948 	va_list ap;
3695087Sjp161948 	char *cp;
3705087Sjp161948 	int r;
3715087Sjp161948 
3725087Sjp161948 	va_start(ap, fmt);
3735087Sjp161948 	r = vasprintf(&cp, fmt, ap);
3745087Sjp161948 	va_end(ap);
3755087Sjp161948 	if (r == -1)
3765087Sjp161948 		fatal("replacearg: argument too long");
3775087Sjp161948 
3785087Sjp161948 	if (which >= args->num)
3795087Sjp161948 		fatal("replacearg: tried to replace invalid arg %d >= %d",
3805087Sjp161948 		    which, args->num);
3815087Sjp161948 	xfree(args->list[which]);
3825087Sjp161948 	args->list[which] = cp;
3835087Sjp161948 }
3845087Sjp161948 
3855087Sjp161948 void
3862780Sjp161948 freeargs(arglist *args)
3872780Sjp161948 {
3882780Sjp161948 	u_int i;
3892780Sjp161948 
3902780Sjp161948 	if (args->list != NULL) {
3912780Sjp161948 		for (i = 0; i < args->num; i++)
3922780Sjp161948 			xfree(args->list[i]);
3932780Sjp161948 		xfree(args->list);
3942780Sjp161948 		args->nalloc = args->num = 0;
3952780Sjp161948 		args->list = NULL;
3962780Sjp161948 	}
3972780Sjp161948 }
3982780Sjp161948 
3995087Sjp161948 /*
4005087Sjp161948  * Ensure that file descriptors 0, 1 and 2 are open or directed to /dev/null,
4015087Sjp161948  * do not touch those that are already open.
4025087Sjp161948  */
4035087Sjp161948 void
4045087Sjp161948 sanitise_stdfd(void)
4055087Sjp161948 {
4065087Sjp161948 	int nullfd, dupfd;
4075087Sjp161948 
4085087Sjp161948 	if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
4095087Sjp161948 		fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno));
4105087Sjp161948 		exit(1);
4115087Sjp161948 	}
4125087Sjp161948 	while (++dupfd <= 2) {
4135087Sjp161948 		/* Only clobber closed fds */
4145087Sjp161948 		if (fcntl(dupfd, F_GETFL, 0) >= 0)
4155087Sjp161948 			continue;
4165087Sjp161948 		if (dup2(nullfd, dupfd) == -1) {
4175087Sjp161948 			fprintf(stderr, "dup2: %s", strerror(errno));
4185087Sjp161948 			exit(1);
4195087Sjp161948 		}
4205087Sjp161948 	}
4215087Sjp161948 	if (nullfd > 2)
4225087Sjp161948 		close(nullfd);
4235087Sjp161948 }
4245087Sjp161948 
4254907Sjp161948 char *
4264907Sjp161948 tohex(const void *vp, size_t l)
4274907Sjp161948 {
4284907Sjp161948 	const u_char *p = (const u_char *)vp;
4294907Sjp161948 	char b[3], *r;
4304907Sjp161948 	size_t i, hl;
4314907Sjp161948 
4324907Sjp161948 	if (l > 65536)
4334907Sjp161948 		return xstrdup("tohex: length > 65536");
4344907Sjp161948 
4354907Sjp161948 	hl = l * 2 + 1;
4364907Sjp161948 	r = xcalloc(1, hl);
4374907Sjp161948 	for (i = 0; i < l; i++) {
4384907Sjp161948 		snprintf(b, sizeof(b), "%02x", p[i]);
4394907Sjp161948 		strlcat(r, b, hl);
4404907Sjp161948 	}
4414907Sjp161948 	return (r);
4424907Sjp161948 }
4434907Sjp161948 
4445087Sjp161948 u_int64_t
4455087Sjp161948 get_u64(const void *vp)
4465087Sjp161948 {
4475087Sjp161948 	const u_char *p = (const u_char *)vp;
4485087Sjp161948 	u_int64_t v;
4495087Sjp161948 
4505087Sjp161948 	v  = (u_int64_t)p[0] << 56;
4515087Sjp161948 	v |= (u_int64_t)p[1] << 48;
4525087Sjp161948 	v |= (u_int64_t)p[2] << 40;
4535087Sjp161948 	v |= (u_int64_t)p[3] << 32;
4545087Sjp161948 	v |= (u_int64_t)p[4] << 24;
4555087Sjp161948 	v |= (u_int64_t)p[5] << 16;
4565087Sjp161948 	v |= (u_int64_t)p[6] << 8;
4575087Sjp161948 	v |= (u_int64_t)p[7];
4585087Sjp161948 
4595087Sjp161948 	return (v);
4605087Sjp161948 }
4615087Sjp161948 
4625087Sjp161948 u_int32_t
4635087Sjp161948 get_u32(const void *vp)
4645087Sjp161948 {
4655087Sjp161948 	const u_char *p = (const u_char *)vp;
4665087Sjp161948 	u_int32_t v;
4675087Sjp161948 
4685087Sjp161948 	v  = (u_int32_t)p[0] << 24;
4695087Sjp161948 	v |= (u_int32_t)p[1] << 16;
4705087Sjp161948 	v |= (u_int32_t)p[2] << 8;
4715087Sjp161948 	v |= (u_int32_t)p[3];
4725087Sjp161948 
4735087Sjp161948 	return (v);
4745087Sjp161948 }
4755087Sjp161948 
4765087Sjp161948 u_int16_t
4775087Sjp161948 get_u16(const void *vp)
4785087Sjp161948 {
4795087Sjp161948 	const u_char *p = (const u_char *)vp;
4805087Sjp161948 	u_int16_t v;
4815087Sjp161948 
4825087Sjp161948 	v  = (u_int16_t)p[0] << 8;
4835087Sjp161948 	v |= (u_int16_t)p[1];
4845087Sjp161948 
4855087Sjp161948 	return (v);
4865087Sjp161948 }
4875087Sjp161948 
4885087Sjp161948 void
4895087Sjp161948 put_u64(void *vp, u_int64_t v)
4905087Sjp161948 {
4915087Sjp161948 	u_char *p = (u_char *)vp;
4925087Sjp161948 
4935087Sjp161948 	p[0] = (u_char)(v >> 56) & 0xff;
4945087Sjp161948 	p[1] = (u_char)(v >> 48) & 0xff;
4955087Sjp161948 	p[2] = (u_char)(v >> 40) & 0xff;
4965087Sjp161948 	p[3] = (u_char)(v >> 32) & 0xff;
4975087Sjp161948 	p[4] = (u_char)(v >> 24) & 0xff;
4985087Sjp161948 	p[5] = (u_char)(v >> 16) & 0xff;
4995087Sjp161948 	p[6] = (u_char)(v >> 8) & 0xff;
5005087Sjp161948 	p[7] = (u_char)v & 0xff;
5015087Sjp161948 }
5025087Sjp161948 
5035087Sjp161948 void
5045087Sjp161948 put_u32(void *vp, u_int32_t v)
5055087Sjp161948 {
5065087Sjp161948 	u_char *p = (u_char *)vp;
5075087Sjp161948 
5085087Sjp161948 	p[0] = (u_char)(v >> 24) & 0xff;
5095087Sjp161948 	p[1] = (u_char)(v >> 16) & 0xff;
5105087Sjp161948 	p[2] = (u_char)(v >> 8) & 0xff;
5115087Sjp161948 	p[3] = (u_char)v & 0xff;
5125087Sjp161948 }
5135087Sjp161948 
5145087Sjp161948 
5155087Sjp161948 void
5165087Sjp161948 put_u16(void *vp, u_int16_t v)
5175087Sjp161948 {
5185087Sjp161948 	u_char *p = (u_char *)vp;
5195087Sjp161948 
5205087Sjp161948 	p[0] = (u_char)(v >> 8) & 0xff;
5215087Sjp161948 	p[1] = (u_char)v & 0xff;
5225087Sjp161948 }
5235087Sjp161948 
5240Sstevel@tonic-gate mysig_t
5250Sstevel@tonic-gate mysignal(int sig, mysig_t act)
5260Sstevel@tonic-gate {
5270Sstevel@tonic-gate #ifdef HAVE_SIGACTION
5280Sstevel@tonic-gate 	struct sigaction sa, osa;
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	if (sigaction(sig, NULL, &osa) == -1)
5310Sstevel@tonic-gate 		return (mysig_t) -1;
5320Sstevel@tonic-gate 	if (osa.sa_handler != act) {
5330Sstevel@tonic-gate 		memset(&sa, 0, sizeof(sa));
5340Sstevel@tonic-gate 		sigemptyset(&sa.sa_mask);
5350Sstevel@tonic-gate 		sa.sa_flags = 0;
5360Sstevel@tonic-gate #if defined(SA_INTERRUPT)
5370Sstevel@tonic-gate 		if (sig == SIGALRM)
5380Sstevel@tonic-gate 			sa.sa_flags |= SA_INTERRUPT;
5390Sstevel@tonic-gate #endif
5400Sstevel@tonic-gate 		sa.sa_handler = act;
5410Sstevel@tonic-gate 		if (sigaction(sig, &sa, NULL) == -1)
5420Sstevel@tonic-gate 			return (mysig_t) -1;
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 	return (osa.sa_handler);
5450Sstevel@tonic-gate #else
5460Sstevel@tonic-gate 	return (signal(sig, act));
5470Sstevel@tonic-gate #endif
5480Sstevel@tonic-gate }
5494958Sjp161948 
5504958Sjp161948 /*
5514958Sjp161948  * Return true if argument is one of "yes", "true", "no" or "false". If
5524958Sjp161948  * 'active' is 0 than we are in a non-matching Host section of the
5534958Sjp161948  * configuration file so we check the syntax but will not set the value of
5544958Sjp161948  * '*option'. Otherwise we set its value if not already set.
5554958Sjp161948  */
5564958Sjp161948 int
5574958Sjp161948 get_yes_no_flag(int *option, const char *arg, const char *filename, int linenum,
5584958Sjp161948     int active)
5594958Sjp161948 {
5604958Sjp161948 	int value = -1;
5614958Sjp161948 
5624958Sjp161948 	if (arg == NULL || *arg == '\0')
5634958Sjp161948 		fatal("%.200s line %d: Missing argument.", filename, linenum);
5644958Sjp161948 	if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
5654958Sjp161948 		value = 1;
5664958Sjp161948 	else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
5674958Sjp161948 		value = 0;
5684958Sjp161948 
5694958Sjp161948 	if (active && *option == -1 && value != -1)
5704958Sjp161948 		*option = value;
5714958Sjp161948 
5724958Sjp161948 	return (value != -1);
5734958Sjp161948 }
574*5266Sjp161948 
575*5266Sjp161948 /*
576*5266Sjp161948  * Convert a string to lowercase. The string returned is an internally allocated
577*5266Sjp161948  * one so the consumer of this function is not expected to change it or free it.
578*5266Sjp161948  */
579*5266Sjp161948 char *
580*5266Sjp161948 tolowercase(const char *s)
581*5266Sjp161948 {
582*5266Sjp161948 	int i, len;
583*5266Sjp161948 	static int lenret = 0;
584*5266Sjp161948 	static char *ret = NULL;
585*5266Sjp161948 
586*5266Sjp161948 	/* allocate a new string if the old one it not long enough to store s */
587*5266Sjp161948 	len = strlen(s) + 1;
588*5266Sjp161948 	if (len > lenret) {
589*5266Sjp161948 		if (ret != NULL)
590*5266Sjp161948 			xfree(ret);
591*5266Sjp161948 		ret = xmalloc(len);
592*5266Sjp161948 		lenret = len;
593*5266Sjp161948 	}
594*5266Sjp161948 
595*5266Sjp161948 	/* process the string including the ending '\0' */
596*5266Sjp161948 	for (i = 0; i < len; ++i)
597*5266Sjp161948 		ret[i] = tolower(s[i]);
598*5266Sjp161948 
599*5266Sjp161948 	return (ret);
600*5266Sjp161948 }
601