xref: /titanic_44/usr/src/cmd/ssh/libssh/common/misc.c (revision 6f8d59d8fcaf391990ca04c7bdcf65ab23320fe0)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
57c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
67c478bd9Sstevel@tonic-gate  * are met:
77c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
87c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
97c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
107c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
117c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
147c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
157c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
167c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
177c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
187c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
197c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
207c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
217c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
227c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate /*
25*6f8d59d8SJan Pechanec  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
267c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include "includes.h"
307c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: misc.c,v 1.19 2002/03/04 17:27:39 stevesk Exp $");
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include "misc.h"
337c478bd9Sstevel@tonic-gate #include "log.h"
347c478bd9Sstevel@tonic-gate #include "xmalloc.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /* remove newline at end of string */
377c478bd9Sstevel@tonic-gate char *
chop(char * s)387c478bd9Sstevel@tonic-gate chop(char *s)
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate 	char *t = s;
417c478bd9Sstevel@tonic-gate 	while (*t) {
427c478bd9Sstevel@tonic-gate 		if (*t == '\n' || *t == '\r') {
437c478bd9Sstevel@tonic-gate 			*t = '\0';
447c478bd9Sstevel@tonic-gate 			return s;
457c478bd9Sstevel@tonic-gate 		}
467c478bd9Sstevel@tonic-gate 		t++;
477c478bd9Sstevel@tonic-gate 	}
487c478bd9Sstevel@tonic-gate 	return s;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate }
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /* set/unset filedescriptor to non-blocking */
537c478bd9Sstevel@tonic-gate void
set_nonblock(int fd)547c478bd9Sstevel@tonic-gate set_nonblock(int fd)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	int val;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	val = fcntl(fd, F_GETFL, 0);
597c478bd9Sstevel@tonic-gate 	if (val < 0) {
607c478bd9Sstevel@tonic-gate 		error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
617c478bd9Sstevel@tonic-gate 		return;
627c478bd9Sstevel@tonic-gate 	}
637c478bd9Sstevel@tonic-gate 	if (val & O_NONBLOCK) {
647c478bd9Sstevel@tonic-gate 		debug2("fd %d is O_NONBLOCK", fd);
657c478bd9Sstevel@tonic-gate 		return;
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate 	debug("fd %d setting O_NONBLOCK", fd);
687c478bd9Sstevel@tonic-gate 	val |= O_NONBLOCK;
697c478bd9Sstevel@tonic-gate 	if (fcntl(fd, F_SETFL, val) == -1)
707c478bd9Sstevel@tonic-gate 		debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
717c478bd9Sstevel@tonic-gate 		    fd, strerror(errno));
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate void
unset_nonblock(int fd)757c478bd9Sstevel@tonic-gate unset_nonblock(int fd)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	int val;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	val = fcntl(fd, F_GETFL, 0);
807c478bd9Sstevel@tonic-gate 	if (val < 0) {
817c478bd9Sstevel@tonic-gate 		error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
827c478bd9Sstevel@tonic-gate 		return;
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 	if (!(val & O_NONBLOCK)) {
857c478bd9Sstevel@tonic-gate 		debug2("fd %d is not O_NONBLOCK", fd);
867c478bd9Sstevel@tonic-gate 		return;
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 	debug("fd %d clearing O_NONBLOCK", fd);
897c478bd9Sstevel@tonic-gate 	val &= ~O_NONBLOCK;
907c478bd9Sstevel@tonic-gate 	if (fcntl(fd, F_SETFL, val) == -1)
917c478bd9Sstevel@tonic-gate 		debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
927c478bd9Sstevel@tonic-gate 		    fd, strerror(errno));
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /* disable nagle on socket */
967c478bd9Sstevel@tonic-gate void
set_nodelay(int fd)977c478bd9Sstevel@tonic-gate set_nodelay(int fd)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	int opt;
1007c478bd9Sstevel@tonic-gate 	socklen_t optlen;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	optlen = sizeof opt;
1037c478bd9Sstevel@tonic-gate 	if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
1047c478bd9Sstevel@tonic-gate 		error("getsockopt TCP_NODELAY: %.100s", strerror(errno));
1057c478bd9Sstevel@tonic-gate 		return;
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 	if (opt == 1) {
1087c478bd9Sstevel@tonic-gate 		debug2("fd %d is TCP_NODELAY", fd);
1097c478bd9Sstevel@tonic-gate 		return;
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 	opt = 1;
1127c478bd9Sstevel@tonic-gate 	debug("fd %d setting TCP_NODELAY", fd);
1137c478bd9Sstevel@tonic-gate 	if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
1147c478bd9Sstevel@tonic-gate 		error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /* Characters considered whitespace in strsep calls. */
1187c478bd9Sstevel@tonic-gate #define WHITESPACE " \t\r\n"
1197c478bd9Sstevel@tonic-gate 
12047b374ffSjp161948 /*
12147b374ffSjp161948  * Function returns a pointer to the 1st token on the line. Such a token can
12247b374ffSjp161948  * be an empty string in the case of '*s' equal to " value". It changes the
12347b374ffSjp161948  * first whitespace token or '=' character after the 1st token to '\0'. Upon
12447b374ffSjp161948  * return it changes '*s' to point to the first character of the next token.
12547b374ffSjp161948  * That token may be an empty string if the 1st token was followed only by
12647b374ffSjp161948  * whitespace or it could be a NULL pointer if the line contained one token
12747b374ffSjp161948  * only.
12847b374ffSjp161948  */
1297c478bd9Sstevel@tonic-gate char *
strdelim(char ** s)1307c478bd9Sstevel@tonic-gate strdelim(char **s)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	char *old;
1337c478bd9Sstevel@tonic-gate 	int wspace = 0;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	if (*s == NULL)
1367c478bd9Sstevel@tonic-gate 		return NULL;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	old = *s;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	*s = strpbrk(*s, WHITESPACE "=");
1417c478bd9Sstevel@tonic-gate 	if (*s == NULL)
1427c478bd9Sstevel@tonic-gate 		return (old);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	/* Allow only one '=' to be skipped */
1457c478bd9Sstevel@tonic-gate 	if (*s[0] == '=')
1467c478bd9Sstevel@tonic-gate 		wspace = 1;
1477c478bd9Sstevel@tonic-gate 	*s[0] = '\0';
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	*s += strspn(*s + 1, WHITESPACE) + 1;
1507c478bd9Sstevel@tonic-gate 	if (*s[0] == '=' && !wspace)
1517c478bd9Sstevel@tonic-gate 		*s += strspn(*s + 1, WHITESPACE) + 1;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	return (old);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate struct passwd *
pwcopy(struct passwd * pw)1577c478bd9Sstevel@tonic-gate pwcopy(struct passwd *pw)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	struct passwd *copy = xmalloc(sizeof(*copy));
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	memset(copy, 0, sizeof(*copy));
1627c478bd9Sstevel@tonic-gate 	copy->pw_name = xstrdup(pw->pw_name);
1637c478bd9Sstevel@tonic-gate 	copy->pw_passwd = xstrdup(pw->pw_passwd);
1647c478bd9Sstevel@tonic-gate 	copy->pw_gecos = xstrdup(pw->pw_gecos);
1657c478bd9Sstevel@tonic-gate 	copy->pw_uid = pw->pw_uid;
1667c478bd9Sstevel@tonic-gate 	copy->pw_gid = pw->pw_gid;
1677c478bd9Sstevel@tonic-gate #ifdef HAVE_PW_EXPIRE_IN_PASSWD
1687c478bd9Sstevel@tonic-gate 	copy->pw_expire = pw->pw_expire;
1697c478bd9Sstevel@tonic-gate #endif
1707c478bd9Sstevel@tonic-gate #ifdef HAVE_PW_CHANGE_IN_PASSWD
1717c478bd9Sstevel@tonic-gate 	copy->pw_change = pw->pw_change;
1727c478bd9Sstevel@tonic-gate #endif
1737c478bd9Sstevel@tonic-gate #ifdef HAVE_PW_CLASS_IN_PASSWD
1747c478bd9Sstevel@tonic-gate 	copy->pw_class = xstrdup(pw->pw_class);
1757c478bd9Sstevel@tonic-gate #endif
1767c478bd9Sstevel@tonic-gate 	copy->pw_dir = xstrdup(pw->pw_dir);
1777c478bd9Sstevel@tonic-gate 	copy->pw_shell = xstrdup(pw->pw_shell);
1787c478bd9Sstevel@tonic-gate 	return copy;
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate void
pwfree(struct passwd ** pw)1827c478bd9Sstevel@tonic-gate pwfree(struct passwd **pw)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	struct passwd *p;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (pw == NULL || *pw == NULL)
1877c478bd9Sstevel@tonic-gate 		return;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	p = *pw;
1907c478bd9Sstevel@tonic-gate 	*pw = NULL;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	xfree(p->pw_name);
1937c478bd9Sstevel@tonic-gate 	xfree(p->pw_passwd);
1947c478bd9Sstevel@tonic-gate 	xfree(p->pw_gecos);
1957c478bd9Sstevel@tonic-gate #ifdef HAVE_PW_CLASS_IN_PASSWD
1967c478bd9Sstevel@tonic-gate 	xfree(p->pw_class);
1977c478bd9Sstevel@tonic-gate #endif
1987c478bd9Sstevel@tonic-gate 	xfree(p->pw_dir);
1997c478bd9Sstevel@tonic-gate 	xfree(p->pw_shell);
2007c478bd9Sstevel@tonic-gate 	xfree(p);
2017c478bd9Sstevel@tonic-gate }
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate /*
2047c478bd9Sstevel@tonic-gate  * Convert ASCII string to TCP/IP port number.
2057c478bd9Sstevel@tonic-gate  * Port must be >0 and <=65535.
2067c478bd9Sstevel@tonic-gate  * Return 0 if invalid.
2077c478bd9Sstevel@tonic-gate  */
2087c478bd9Sstevel@tonic-gate int
a2port(const char * s)2097c478bd9Sstevel@tonic-gate a2port(const char *s)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	long port;
2127c478bd9Sstevel@tonic-gate 	char *endp;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	errno = 0;
2157c478bd9Sstevel@tonic-gate 	port = strtol(s, &endp, 0);
2167c478bd9Sstevel@tonic-gate 	if (s == endp || *endp != '\0' ||
2177c478bd9Sstevel@tonic-gate 	    (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
2187c478bd9Sstevel@tonic-gate 	    port <= 0 || port > 65535)
2197c478bd9Sstevel@tonic-gate 		return 0;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	return port;
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate #define SECONDS		1
2257c478bd9Sstevel@tonic-gate #define MINUTES		(SECONDS * 60)
2267c478bd9Sstevel@tonic-gate #define HOURS		(MINUTES * 60)
2277c478bd9Sstevel@tonic-gate #define DAYS		(HOURS * 24)
2287c478bd9Sstevel@tonic-gate #define WEEKS		(DAYS * 7)
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate /*
2317c478bd9Sstevel@tonic-gate  * Convert a time string into seconds; format is
2327c478bd9Sstevel@tonic-gate  * a sequence of:
2337c478bd9Sstevel@tonic-gate  *      time[qualifier]
2347c478bd9Sstevel@tonic-gate  *
2357c478bd9Sstevel@tonic-gate  * Valid time qualifiers are:
2367c478bd9Sstevel@tonic-gate  *      <none>  seconds
2377c478bd9Sstevel@tonic-gate  *      s|S     seconds
2387c478bd9Sstevel@tonic-gate  *      m|M     minutes
2397c478bd9Sstevel@tonic-gate  *      h|H     hours
2407c478bd9Sstevel@tonic-gate  *      d|D     days
2417c478bd9Sstevel@tonic-gate  *      w|W     weeks
2427c478bd9Sstevel@tonic-gate  *
2437c478bd9Sstevel@tonic-gate  * Examples:
2447c478bd9Sstevel@tonic-gate  *      90m     90 minutes
2457c478bd9Sstevel@tonic-gate  *      1h30m   90 minutes
2467c478bd9Sstevel@tonic-gate  *      2d      2 days
2477c478bd9Sstevel@tonic-gate  *      1w      1 week
2487c478bd9Sstevel@tonic-gate  *
2497c478bd9Sstevel@tonic-gate  * Return -1 if time string is invalid.
2507c478bd9Sstevel@tonic-gate  */
2517c478bd9Sstevel@tonic-gate long
convtime(const char * s)2527c478bd9Sstevel@tonic-gate convtime(const char *s)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	long total, secs;
2557c478bd9Sstevel@tonic-gate 	const char *p;
2567c478bd9Sstevel@tonic-gate 	char *endp;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	errno = 0;
2597c478bd9Sstevel@tonic-gate 	total = 0;
2607c478bd9Sstevel@tonic-gate 	p = s;
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	if (p == NULL || *p == '\0')
2637c478bd9Sstevel@tonic-gate 		return -1;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	while (*p) {
2667c478bd9Sstevel@tonic-gate 		secs = strtol(p, &endp, 10);
2677c478bd9Sstevel@tonic-gate 		if (p == endp ||
2687c478bd9Sstevel@tonic-gate 		    (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
2697c478bd9Sstevel@tonic-gate 		    secs < 0)
2707c478bd9Sstevel@tonic-gate 			return -1;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 		switch (*endp++) {
2737c478bd9Sstevel@tonic-gate 		case '\0':
2747c478bd9Sstevel@tonic-gate 			endp--;
2757c478bd9Sstevel@tonic-gate 			break;
2767c478bd9Sstevel@tonic-gate 		case 's':
2777c478bd9Sstevel@tonic-gate 		case 'S':
2787c478bd9Sstevel@tonic-gate 			break;
2797c478bd9Sstevel@tonic-gate 		case 'm':
2807c478bd9Sstevel@tonic-gate 		case 'M':
2817c478bd9Sstevel@tonic-gate 			secs *= MINUTES;
2827c478bd9Sstevel@tonic-gate 			break;
2837c478bd9Sstevel@tonic-gate 		case 'h':
2847c478bd9Sstevel@tonic-gate 		case 'H':
2857c478bd9Sstevel@tonic-gate 			secs *= HOURS;
2867c478bd9Sstevel@tonic-gate 			break;
2877c478bd9Sstevel@tonic-gate 		case 'd':
2887c478bd9Sstevel@tonic-gate 		case 'D':
2897c478bd9Sstevel@tonic-gate 			secs *= DAYS;
2907c478bd9Sstevel@tonic-gate 			break;
2917c478bd9Sstevel@tonic-gate 		case 'w':
2927c478bd9Sstevel@tonic-gate 		case 'W':
2937c478bd9Sstevel@tonic-gate 			secs *= WEEKS;
2947c478bd9Sstevel@tonic-gate 			break;
2957c478bd9Sstevel@tonic-gate 		default:
2967c478bd9Sstevel@tonic-gate 			return -1;
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 		total += secs;
2997c478bd9Sstevel@tonic-gate 		if (total < 0)
3007c478bd9Sstevel@tonic-gate 			return -1;
3017c478bd9Sstevel@tonic-gate 		p = endp;
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	return total;
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3079b03ea0fSjp161948 /*
3089b03ea0fSjp161948  * Search for next delimiter between hostnames/addresses and ports.
3099b03ea0fSjp161948  * Argument may be modified (for termination).
3109b03ea0fSjp161948  * Returns *cp if parsing succeeds.
3119b03ea0fSjp161948  * *cp is set to the start of the next delimiter, if one was found.
3129b03ea0fSjp161948  * If this is the last field, *cp is set to NULL.
3139b03ea0fSjp161948  */
3149b03ea0fSjp161948 char *
hpdelim(char ** cp)3159b03ea0fSjp161948 hpdelim(char **cp)
3169b03ea0fSjp161948 {
3179b03ea0fSjp161948 	char *s, *old;
3189b03ea0fSjp161948 
3199b03ea0fSjp161948 	if (cp == NULL || *cp == NULL)
3209b03ea0fSjp161948 		return NULL;
3219b03ea0fSjp161948 
3229b03ea0fSjp161948 	old = s = *cp;
3239b03ea0fSjp161948 	if (*s == '[') {
3249b03ea0fSjp161948 		if ((s = strchr(s, ']')) == NULL)
3259b03ea0fSjp161948 			return NULL;
3269b03ea0fSjp161948 		else
3279b03ea0fSjp161948 			s++;
3289b03ea0fSjp161948 	} else if ((s = strpbrk(s, ":/")) == NULL)
3299b03ea0fSjp161948 		s = *cp + strlen(*cp); /* skip to end (see first case below) */
3309b03ea0fSjp161948 
3319b03ea0fSjp161948 	switch (*s) {
3329b03ea0fSjp161948 	case '\0':
3339b03ea0fSjp161948 		*cp = NULL;	/* no more fields*/
3349b03ea0fSjp161948 		break;
3359b03ea0fSjp161948 
3369b03ea0fSjp161948 	case ':':
3379b03ea0fSjp161948 	case '/':
3389b03ea0fSjp161948 		*s = '\0';	/* terminate */
3399b03ea0fSjp161948 		*cp = s + 1;
3409b03ea0fSjp161948 		break;
3419b03ea0fSjp161948 
3429b03ea0fSjp161948 	default:
3439b03ea0fSjp161948 		return NULL;
3449b03ea0fSjp161948 	}
3459b03ea0fSjp161948 
3469b03ea0fSjp161948 	return old;
3479b03ea0fSjp161948 }
3489b03ea0fSjp161948 
3497c478bd9Sstevel@tonic-gate char *
cleanhostname(char * host)3507c478bd9Sstevel@tonic-gate cleanhostname(char *host)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	if (*host == '[' && host[strlen(host) - 1] == ']') {
3537c478bd9Sstevel@tonic-gate 		host[strlen(host) - 1] = '\0';
3547c478bd9Sstevel@tonic-gate 		return (host + 1);
3557c478bd9Sstevel@tonic-gate 	} else
3567c478bd9Sstevel@tonic-gate 		return host;
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate char *
colon(char * cp)3607c478bd9Sstevel@tonic-gate colon(char *cp)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate 	int flag = 0;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	if (*cp == ':')		/* Leading colon is part of file name. */
3657c478bd9Sstevel@tonic-gate 		return (0);
3667c478bd9Sstevel@tonic-gate 	if (*cp == '[')
3677c478bd9Sstevel@tonic-gate 		flag = 1;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	for (; *cp; ++cp) {
3707c478bd9Sstevel@tonic-gate 		if (*cp == '@' && *(cp+1) == '[')
3717c478bd9Sstevel@tonic-gate 			flag = 1;
3727c478bd9Sstevel@tonic-gate 		if (*cp == ']' && *(cp+1) == ':' && flag)
3737c478bd9Sstevel@tonic-gate 			return (cp+1);
3747c478bd9Sstevel@tonic-gate 		if (*cp == ':' && !flag)
3757c478bd9Sstevel@tonic-gate 			return (cp);
3767c478bd9Sstevel@tonic-gate 		if (*cp == '/')
3777c478bd9Sstevel@tonic-gate 			return (0);
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 	return (0);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate /* function to assist building execv() arguments */
3837c478bd9Sstevel@tonic-gate /* PRINTFLIKE2 */
3847c478bd9Sstevel@tonic-gate void
addargs(arglist * args,char * fmt,...)3857c478bd9Sstevel@tonic-gate addargs(arglist *args, char *fmt, ...)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 	va_list ap;
3887c478bd9Sstevel@tonic-gate 	char buf[1024];
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
3917c478bd9Sstevel@tonic-gate 	vsnprintf(buf, sizeof(buf), fmt, ap);
3927c478bd9Sstevel@tonic-gate 	va_end(ap);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	if (args->list == NULL) {
3957c478bd9Sstevel@tonic-gate 		args->nalloc = 32;
3967c478bd9Sstevel@tonic-gate 		args->num = 0;
3977c478bd9Sstevel@tonic-gate 	} else if (args->num+2 >= args->nalloc)
3987c478bd9Sstevel@tonic-gate 		args->nalloc *= 2;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	args->list = xrealloc(args->list, args->nalloc * sizeof(char *));
4017c478bd9Sstevel@tonic-gate 	args->list[args->num++] = xstrdup(buf);
4027c478bd9Sstevel@tonic-gate 	args->list[args->num] = NULL;
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate 
405a7e50fb1Sjp161948 void
replacearg(arglist * args,u_int which,char * fmt,...)40690685d2cSjp161948 replacearg(arglist *args, u_int which, char *fmt, ...)
40790685d2cSjp161948 {
40890685d2cSjp161948 	va_list ap;
40990685d2cSjp161948 	char *cp;
41090685d2cSjp161948 	int r;
41190685d2cSjp161948 
41290685d2cSjp161948 	va_start(ap, fmt);
41390685d2cSjp161948 	r = vasprintf(&cp, fmt, ap);
41490685d2cSjp161948 	va_end(ap);
41590685d2cSjp161948 	if (r == -1)
41690685d2cSjp161948 		fatal("replacearg: argument too long");
41790685d2cSjp161948 
41890685d2cSjp161948 	if (which >= args->num)
41990685d2cSjp161948 		fatal("replacearg: tried to replace invalid arg %d >= %d",
42090685d2cSjp161948 		    which, args->num);
42190685d2cSjp161948 	xfree(args->list[which]);
42290685d2cSjp161948 	args->list[which] = cp;
42390685d2cSjp161948 }
42490685d2cSjp161948 
42590685d2cSjp161948 void
freeargs(arglist * args)426a7e50fb1Sjp161948 freeargs(arglist *args)
427a7e50fb1Sjp161948 {
428a7e50fb1Sjp161948 	u_int i;
429a7e50fb1Sjp161948 
430a7e50fb1Sjp161948 	if (args->list != NULL) {
431a7e50fb1Sjp161948 		for (i = 0; i < args->num; i++)
432a7e50fb1Sjp161948 			xfree(args->list[i]);
433a7e50fb1Sjp161948 		xfree(args->list);
434a7e50fb1Sjp161948 		args->nalloc = args->num = 0;
435a7e50fb1Sjp161948 		args->list = NULL;
436a7e50fb1Sjp161948 	}
437a7e50fb1Sjp161948 }
438a7e50fb1Sjp161948 
43990685d2cSjp161948 /*
440*6f8d59d8SJan Pechanec  * Expand a string with a set of %[char] escapes. A number of escapes may be
441*6f8d59d8SJan Pechanec  * specified as (char *escape_chars, char *replacement) pairs. The list must
442*6f8d59d8SJan Pechanec  * be terminated by a NULL escape_char. Returns replaced string in memory
443*6f8d59d8SJan Pechanec  * allocated by xmalloc.
444*6f8d59d8SJan Pechanec  */
445*6f8d59d8SJan Pechanec char *
percent_expand(const char * string,...)446*6f8d59d8SJan Pechanec percent_expand(const char *string, ...)
447*6f8d59d8SJan Pechanec {
448*6f8d59d8SJan Pechanec #define EXPAND_MAX_KEYS	16
449*6f8d59d8SJan Pechanec 	struct {
450*6f8d59d8SJan Pechanec 		const char *key;
451*6f8d59d8SJan Pechanec 		const char *repl;
452*6f8d59d8SJan Pechanec 	} keys[EXPAND_MAX_KEYS];
453*6f8d59d8SJan Pechanec 	u_int num_keys, i, j;
454*6f8d59d8SJan Pechanec 	char buf[4096];
455*6f8d59d8SJan Pechanec 	va_list ap;
456*6f8d59d8SJan Pechanec 
457*6f8d59d8SJan Pechanec 	/* Gather keys */
458*6f8d59d8SJan Pechanec 	va_start(ap, string);
459*6f8d59d8SJan Pechanec 	for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) {
460*6f8d59d8SJan Pechanec 		keys[num_keys].key = va_arg(ap, char *);
461*6f8d59d8SJan Pechanec 		if (keys[num_keys].key == NULL)
462*6f8d59d8SJan Pechanec 			break;
463*6f8d59d8SJan Pechanec 		keys[num_keys].repl = va_arg(ap, char *);
464*6f8d59d8SJan Pechanec 		if (keys[num_keys].repl == NULL)
465*6f8d59d8SJan Pechanec 			fatal("percent_expand: NULL replacement");
466*6f8d59d8SJan Pechanec 	}
467*6f8d59d8SJan Pechanec 	va_end(ap);
468*6f8d59d8SJan Pechanec 
469*6f8d59d8SJan Pechanec 	if (num_keys >= EXPAND_MAX_KEYS)
470*6f8d59d8SJan Pechanec 		fatal("percent_expand: too many keys");
471*6f8d59d8SJan Pechanec 
472*6f8d59d8SJan Pechanec 	/* Expand string */
473*6f8d59d8SJan Pechanec 	*buf = '\0';
474*6f8d59d8SJan Pechanec 	for (i = 0; *string != '\0'; string++) {
475*6f8d59d8SJan Pechanec 		if (*string != '%') {
476*6f8d59d8SJan Pechanec  append:
477*6f8d59d8SJan Pechanec 			buf[i++] = *string;
478*6f8d59d8SJan Pechanec 			if (i >= sizeof(buf))
479*6f8d59d8SJan Pechanec 				fatal("percent_expand: string too long");
480*6f8d59d8SJan Pechanec 			buf[i] = '\0';
481*6f8d59d8SJan Pechanec 			continue;
482*6f8d59d8SJan Pechanec 		}
483*6f8d59d8SJan Pechanec 		string++;
484*6f8d59d8SJan Pechanec 		if (*string == '%')
485*6f8d59d8SJan Pechanec 			goto append;
486*6f8d59d8SJan Pechanec 		for (j = 0; j < num_keys; j++) {
487*6f8d59d8SJan Pechanec 			if (strchr(keys[j].key, *string) != NULL) {
488*6f8d59d8SJan Pechanec 				i = strlcat(buf, keys[j].repl, sizeof(buf));
489*6f8d59d8SJan Pechanec 				if (i >= sizeof(buf))
490*6f8d59d8SJan Pechanec 					fatal("percent_expand: string too long");
491*6f8d59d8SJan Pechanec 				break;
492*6f8d59d8SJan Pechanec 			}
493*6f8d59d8SJan Pechanec 		}
494*6f8d59d8SJan Pechanec 		if (j >= num_keys)
495*6f8d59d8SJan Pechanec 			fatal("percent_expand: unknown key %%%c", *string);
496*6f8d59d8SJan Pechanec 	}
497*6f8d59d8SJan Pechanec 	return (xstrdup(buf));
498*6f8d59d8SJan Pechanec #undef EXPAND_MAX_KEYS
499*6f8d59d8SJan Pechanec }
500*6f8d59d8SJan Pechanec 
501*6f8d59d8SJan Pechanec /*
50290685d2cSjp161948  * Ensure that file descriptors 0, 1 and 2 are open or directed to /dev/null,
50390685d2cSjp161948  * do not touch those that are already open.
50490685d2cSjp161948  */
50590685d2cSjp161948 void
sanitise_stdfd(void)50690685d2cSjp161948 sanitise_stdfd(void)
50790685d2cSjp161948 {
50890685d2cSjp161948 	int nullfd, dupfd;
50990685d2cSjp161948 
51090685d2cSjp161948 	if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
51190685d2cSjp161948 		fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno));
51290685d2cSjp161948 		exit(1);
51390685d2cSjp161948 	}
51490685d2cSjp161948 	while (++dupfd <= 2) {
51590685d2cSjp161948 		/* Only clobber closed fds */
51690685d2cSjp161948 		if (fcntl(dupfd, F_GETFL, 0) >= 0)
51790685d2cSjp161948 			continue;
51890685d2cSjp161948 		if (dup2(nullfd, dupfd) == -1) {
51990685d2cSjp161948 			fprintf(stderr, "dup2: %s", strerror(errno));
52090685d2cSjp161948 			exit(1);
52190685d2cSjp161948 		}
52290685d2cSjp161948 	}
52390685d2cSjp161948 	if (nullfd > 2)
52490685d2cSjp161948 		close(nullfd);
52590685d2cSjp161948 }
52690685d2cSjp161948 
527383a1232Sjp161948 char *
tohex(const void * vp,size_t l)528383a1232Sjp161948 tohex(const void *vp, size_t l)
529383a1232Sjp161948 {
530383a1232Sjp161948 	const u_char *p = (const u_char *)vp;
531383a1232Sjp161948 	char b[3], *r;
532383a1232Sjp161948 	size_t i, hl;
533383a1232Sjp161948 
534383a1232Sjp161948 	if (l > 65536)
535383a1232Sjp161948 		return xstrdup("tohex: length > 65536");
536383a1232Sjp161948 
537383a1232Sjp161948 	hl = l * 2 + 1;
538383a1232Sjp161948 	r = xcalloc(1, hl);
539383a1232Sjp161948 	for (i = 0; i < l; i++) {
540383a1232Sjp161948 		snprintf(b, sizeof(b), "%02x", p[i]);
541383a1232Sjp161948 		strlcat(r, b, hl);
542383a1232Sjp161948 	}
543383a1232Sjp161948 	return (r);
544383a1232Sjp161948 }
545383a1232Sjp161948 
54690685d2cSjp161948 u_int64_t
get_u64(const void * vp)54790685d2cSjp161948 get_u64(const void *vp)
54890685d2cSjp161948 {
54990685d2cSjp161948 	const u_char *p = (const u_char *)vp;
55090685d2cSjp161948 	u_int64_t v;
55190685d2cSjp161948 
55290685d2cSjp161948 	v  = (u_int64_t)p[0] << 56;
55390685d2cSjp161948 	v |= (u_int64_t)p[1] << 48;
55490685d2cSjp161948 	v |= (u_int64_t)p[2] << 40;
55590685d2cSjp161948 	v |= (u_int64_t)p[3] << 32;
55690685d2cSjp161948 	v |= (u_int64_t)p[4] << 24;
55790685d2cSjp161948 	v |= (u_int64_t)p[5] << 16;
55890685d2cSjp161948 	v |= (u_int64_t)p[6] << 8;
55990685d2cSjp161948 	v |= (u_int64_t)p[7];
56090685d2cSjp161948 
56190685d2cSjp161948 	return (v);
56290685d2cSjp161948 }
56390685d2cSjp161948 
56490685d2cSjp161948 u_int32_t
get_u32(const void * vp)56590685d2cSjp161948 get_u32(const void *vp)
56690685d2cSjp161948 {
56790685d2cSjp161948 	const u_char *p = (const u_char *)vp;
56890685d2cSjp161948 	u_int32_t v;
56990685d2cSjp161948 
57090685d2cSjp161948 	v  = (u_int32_t)p[0] << 24;
57190685d2cSjp161948 	v |= (u_int32_t)p[1] << 16;
57290685d2cSjp161948 	v |= (u_int32_t)p[2] << 8;
57390685d2cSjp161948 	v |= (u_int32_t)p[3];
57490685d2cSjp161948 
57590685d2cSjp161948 	return (v);
57690685d2cSjp161948 }
57790685d2cSjp161948 
57890685d2cSjp161948 u_int16_t
get_u16(const void * vp)57990685d2cSjp161948 get_u16(const void *vp)
58090685d2cSjp161948 {
58190685d2cSjp161948 	const u_char *p = (const u_char *)vp;
58290685d2cSjp161948 	u_int16_t v;
58390685d2cSjp161948 
58490685d2cSjp161948 	v  = (u_int16_t)p[0] << 8;
58590685d2cSjp161948 	v |= (u_int16_t)p[1];
58690685d2cSjp161948 
58790685d2cSjp161948 	return (v);
58890685d2cSjp161948 }
58990685d2cSjp161948 
59090685d2cSjp161948 void
put_u64(void * vp,u_int64_t v)59190685d2cSjp161948 put_u64(void *vp, u_int64_t v)
59290685d2cSjp161948 {
59390685d2cSjp161948 	u_char *p = (u_char *)vp;
59490685d2cSjp161948 
59590685d2cSjp161948 	p[0] = (u_char)(v >> 56) & 0xff;
59690685d2cSjp161948 	p[1] = (u_char)(v >> 48) & 0xff;
59790685d2cSjp161948 	p[2] = (u_char)(v >> 40) & 0xff;
59890685d2cSjp161948 	p[3] = (u_char)(v >> 32) & 0xff;
59990685d2cSjp161948 	p[4] = (u_char)(v >> 24) & 0xff;
60090685d2cSjp161948 	p[5] = (u_char)(v >> 16) & 0xff;
60190685d2cSjp161948 	p[6] = (u_char)(v >> 8) & 0xff;
60290685d2cSjp161948 	p[7] = (u_char)v & 0xff;
60390685d2cSjp161948 }
60490685d2cSjp161948 
60590685d2cSjp161948 void
put_u32(void * vp,u_int32_t v)60690685d2cSjp161948 put_u32(void *vp, u_int32_t v)
60790685d2cSjp161948 {
60890685d2cSjp161948 	u_char *p = (u_char *)vp;
60990685d2cSjp161948 
61090685d2cSjp161948 	p[0] = (u_char)(v >> 24) & 0xff;
61190685d2cSjp161948 	p[1] = (u_char)(v >> 16) & 0xff;
61290685d2cSjp161948 	p[2] = (u_char)(v >> 8) & 0xff;
61390685d2cSjp161948 	p[3] = (u_char)v & 0xff;
61490685d2cSjp161948 }
61590685d2cSjp161948 
61690685d2cSjp161948 
61790685d2cSjp161948 void
put_u16(void * vp,u_int16_t v)61890685d2cSjp161948 put_u16(void *vp, u_int16_t v)
61990685d2cSjp161948 {
62090685d2cSjp161948 	u_char *p = (u_char *)vp;
62190685d2cSjp161948 
62290685d2cSjp161948 	p[0] = (u_char)(v >> 8) & 0xff;
62390685d2cSjp161948 	p[1] = (u_char)v & 0xff;
62490685d2cSjp161948 }
62590685d2cSjp161948 
6267c478bd9Sstevel@tonic-gate mysig_t
mysignal(int sig,mysig_t act)6277c478bd9Sstevel@tonic-gate mysignal(int sig, mysig_t act)
6287c478bd9Sstevel@tonic-gate {
6297c478bd9Sstevel@tonic-gate #ifdef HAVE_SIGACTION
6307c478bd9Sstevel@tonic-gate 	struct sigaction sa, osa;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	if (sigaction(sig, NULL, &osa) == -1)
6337c478bd9Sstevel@tonic-gate 		return (mysig_t) -1;
6347c478bd9Sstevel@tonic-gate 	if (osa.sa_handler != act) {
6357c478bd9Sstevel@tonic-gate 		memset(&sa, 0, sizeof(sa));
6367c478bd9Sstevel@tonic-gate 		sigemptyset(&sa.sa_mask);
6377c478bd9Sstevel@tonic-gate 		sa.sa_flags = 0;
6387c478bd9Sstevel@tonic-gate #if defined(SA_INTERRUPT)
6397c478bd9Sstevel@tonic-gate 		if (sig == SIGALRM)
6407c478bd9Sstevel@tonic-gate 			sa.sa_flags |= SA_INTERRUPT;
6417c478bd9Sstevel@tonic-gate #endif
6427c478bd9Sstevel@tonic-gate 		sa.sa_handler = act;
6437c478bd9Sstevel@tonic-gate 		if (sigaction(sig, &sa, NULL) == -1)
6447c478bd9Sstevel@tonic-gate 			return (mysig_t) -1;
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate 	return (osa.sa_handler);
6477c478bd9Sstevel@tonic-gate #else
6487c478bd9Sstevel@tonic-gate 	return (signal(sig, act));
6497c478bd9Sstevel@tonic-gate #endif
6507c478bd9Sstevel@tonic-gate }
651e5e9dedaSjp161948 
652e5e9dedaSjp161948 /*
653e5e9dedaSjp161948  * Return true if argument is one of "yes", "true", "no" or "false". If
654e5e9dedaSjp161948  * 'active' is 0 than we are in a non-matching Host section of the
655e5e9dedaSjp161948  * configuration file so we check the syntax but will not set the value of
656e5e9dedaSjp161948  * '*option'. Otherwise we set its value if not already set.
657e5e9dedaSjp161948  */
658e5e9dedaSjp161948 int
get_yes_no_flag(int * option,const char * arg,const char * filename,int linenum,int active)659e5e9dedaSjp161948 get_yes_no_flag(int *option, const char *arg, const char *filename, int linenum,
660e5e9dedaSjp161948     int active)
661e5e9dedaSjp161948 {
662e5e9dedaSjp161948 	int value = -1;
663e5e9dedaSjp161948 
664e5e9dedaSjp161948 	if (arg == NULL || *arg == '\0')
665e5e9dedaSjp161948 		fatal("%.200s line %d: Missing argument.", filename, linenum);
666e5e9dedaSjp161948 	if (strcmp(arg, "yes") == 0 || strcmp(arg, "true") == 0)
667e5e9dedaSjp161948 		value = 1;
668e5e9dedaSjp161948 	else if (strcmp(arg, "no") == 0 || strcmp(arg, "false") == 0)
669e5e9dedaSjp161948 		value = 0;
670e5e9dedaSjp161948 
671e5e9dedaSjp161948 	if (active && *option == -1 && value != -1)
672e5e9dedaSjp161948 		*option = value;
673e5e9dedaSjp161948 
674e5e9dedaSjp161948 	return (value != -1);
675e5e9dedaSjp161948 }
6765d7f3ec0Sjp161948 
6775d7f3ec0Sjp161948 /*
6785d7f3ec0Sjp161948  * Convert a string to lowercase. The string returned is an internally allocated
6795d7f3ec0Sjp161948  * one so the consumer of this function is not expected to change it or free it.
6805d7f3ec0Sjp161948  */
6815d7f3ec0Sjp161948 char *
tolowercase(const char * s)6825d7f3ec0Sjp161948 tolowercase(const char *s)
6835d7f3ec0Sjp161948 {
6845d7f3ec0Sjp161948 	int i, len;
6855d7f3ec0Sjp161948 	static int lenret = 0;
6865d7f3ec0Sjp161948 	static char *ret = NULL;
6875d7f3ec0Sjp161948 
6885d7f3ec0Sjp161948 	/* allocate a new string if the old one it not long enough to store s */
6895d7f3ec0Sjp161948 	len = strlen(s) + 1;
6905d7f3ec0Sjp161948 	if (len > lenret) {
6915d7f3ec0Sjp161948 		if (ret != NULL)
6925d7f3ec0Sjp161948 			xfree(ret);
6935d7f3ec0Sjp161948 		ret = xmalloc(len);
6945d7f3ec0Sjp161948 		lenret = len;
6955d7f3ec0Sjp161948 	}
6965d7f3ec0Sjp161948 
6975d7f3ec0Sjp161948 	/* process the string including the ending '\0' */
6985d7f3ec0Sjp161948 	for (i = 0; i < len; ++i)
6995d7f3ec0Sjp161948 		ret[i] = tolower(s[i]);
7005d7f3ec0Sjp161948 
7015d7f3ec0Sjp161948 	return (ret);
7025d7f3ec0Sjp161948 }
703