Home | History | Annotate | Line # | Download | only in dist
misc.c revision 1.13
      1   1.8  christos /*	$NetBSD: misc.c,v 1.13 2016/08/02 13:45:12 christos Exp $	*/
      2  1.13  christos /* $OpenBSD: misc.c,v 1.105 2016/07/15 00:24:30 djm Exp $ */
      3   1.1  christos /*
      4   1.1  christos  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
      5   1.1  christos  * Copyright (c) 2005,2006 Damien Miller.  All rights reserved.
      6   1.1  christos  *
      7   1.1  christos  * Redistribution and use in source and binary forms, with or without
      8   1.1  christos  * modification, are permitted provided that the following conditions
      9   1.1  christos  * are met:
     10   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  christos  *    documentation and/or other materials provided with the distribution.
     15   1.1  christos  *
     16   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21   1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22   1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23   1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24   1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25   1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26   1.1  christos  */
     27   1.1  christos 
     28   1.2  christos #include "includes.h"
     29   1.8  christos __RCSID("$NetBSD: misc.c,v 1.13 2016/08/02 13:45:12 christos Exp $");
     30   1.1  christos #include <sys/types.h>
     31   1.1  christos #include <sys/ioctl.h>
     32   1.1  christos #include <sys/socket.h>
     33  1.12  christos #include <sys/time.h>
     34   1.9  christos #include <sys/un.h>
     35   1.1  christos 
     36   1.1  christos #include <net/if.h>
     37   1.2  christos #include <net/if_tun.h>
     38   1.1  christos #include <netinet/in.h>
     39   1.5  christos #include <netinet/ip.h>
     40   1.1  christos #include <netinet/tcp.h>
     41   1.1  christos 
     42   1.9  christos #include <ctype.h>
     43   1.1  christos #include <errno.h>
     44   1.1  christos #include <fcntl.h>
     45   1.1  christos #include <netdb.h>
     46   1.1  christos #include <paths.h>
     47   1.1  christos #include <pwd.h>
     48  1.10  christos #include <limits.h>
     49   1.1  christos #include <stdarg.h>
     50   1.1  christos #include <stdio.h>
     51   1.1  christos #include <stdlib.h>
     52   1.1  christos #include <string.h>
     53   1.1  christos #include <unistd.h>
     54   1.1  christos 
     55   1.1  christos #include "xmalloc.h"
     56   1.1  christos #include "misc.h"
     57   1.1  christos #include "log.h"
     58   1.1  christos #include "ssh.h"
     59   1.1  christos 
     60   1.1  christos /* remove newline at end of string */
     61   1.1  christos char *
     62   1.1  christos chop(char *s)
     63   1.1  christos {
     64   1.1  christos 	char *t = s;
     65   1.1  christos 	while (*t) {
     66   1.1  christos 		if (*t == '\n' || *t == '\r') {
     67   1.1  christos 			*t = '\0';
     68   1.1  christos 			return s;
     69   1.1  christos 		}
     70   1.1  christos 		t++;
     71   1.1  christos 	}
     72   1.1  christos 	return s;
     73   1.1  christos 
     74   1.1  christos }
     75   1.1  christos 
     76   1.1  christos /* set/unset filedescriptor to non-blocking */
     77   1.1  christos int
     78   1.1  christos set_nonblock(int fd)
     79   1.1  christos {
     80   1.1  christos 	int val;
     81   1.1  christos 
     82  1.13  christos 	val = fcntl(fd, F_GETFL);
     83   1.1  christos 	if (val < 0) {
     84  1.13  christos 		error("fcntl(%d, F_GETFL): %s", fd, strerror(errno));
     85   1.1  christos 		return (-1);
     86   1.1  christos 	}
     87   1.1  christos 	if (val & O_NONBLOCK) {
     88   1.1  christos 		debug3("fd %d is O_NONBLOCK", fd);
     89   1.1  christos 		return (0);
     90   1.1  christos 	}
     91   1.1  christos 	debug2("fd %d setting O_NONBLOCK", fd);
     92   1.1  christos 	val |= O_NONBLOCK;
     93   1.1  christos 	if (fcntl(fd, F_SETFL, val) == -1) {
     94   1.1  christos 		debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd,
     95   1.1  christos 		    strerror(errno));
     96   1.1  christos 		return (-1);
     97   1.1  christos 	}
     98   1.1  christos 	return (0);
     99   1.1  christos }
    100   1.1  christos 
    101   1.1  christos int
    102   1.1  christos unset_nonblock(int fd)
    103   1.1  christos {
    104   1.1  christos 	int val;
    105   1.1  christos 
    106  1.13  christos 	val = fcntl(fd, F_GETFL);
    107   1.1  christos 	if (val < 0) {
    108  1.13  christos 		error("fcntl(%d, F_GETFL): %s", fd, strerror(errno));
    109   1.1  christos 		return (-1);
    110   1.1  christos 	}
    111   1.1  christos 	if (!(val & O_NONBLOCK)) {
    112   1.1  christos 		debug3("fd %d is not O_NONBLOCK", fd);
    113   1.1  christos 		return (0);
    114   1.1  christos 	}
    115   1.1  christos 	debug("fd %d clearing O_NONBLOCK", fd);
    116   1.1  christos 	val &= ~O_NONBLOCK;
    117   1.1  christos 	if (fcntl(fd, F_SETFL, val) == -1) {
    118   1.1  christos 		debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s",
    119   1.1  christos 		    fd, strerror(errno));
    120   1.1  christos 		return (-1);
    121   1.1  christos 	}
    122   1.1  christos 	return (0);
    123   1.1  christos }
    124   1.1  christos 
    125   1.1  christos const char *
    126   1.1  christos ssh_gai_strerror(int gaierr)
    127   1.1  christos {
    128   1.8  christos 	if (gaierr == EAI_SYSTEM && errno != 0)
    129   1.1  christos 		return strerror(errno);
    130   1.1  christos 	return gai_strerror(gaierr);
    131   1.1  christos }
    132   1.1  christos 
    133   1.1  christos /* disable nagle on socket */
    134   1.1  christos void
    135   1.1  christos set_nodelay(int fd)
    136   1.1  christos {
    137   1.1  christos 	int opt;
    138   1.1  christos 	socklen_t optlen;
    139   1.1  christos 
    140   1.1  christos 	optlen = sizeof opt;
    141   1.1  christos 	if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
    142   1.1  christos 		debug("getsockopt TCP_NODELAY: %.100s", strerror(errno));
    143   1.1  christos 		return;
    144   1.1  christos 	}
    145   1.1  christos 	if (opt == 1) {
    146   1.1  christos 		debug2("fd %d is TCP_NODELAY", fd);
    147   1.1  christos 		return;
    148   1.1  christos 	}
    149   1.1  christos 	opt = 1;
    150   1.1  christos 	debug2("fd %d setting TCP_NODELAY", fd);
    151   1.1  christos 	if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
    152   1.1  christos 		error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
    153   1.1  christos }
    154   1.1  christos 
    155   1.1  christos /* Characters considered whitespace in strsep calls. */
    156   1.1  christos #define WHITESPACE " \t\r\n"
    157   1.1  christos #define QUOTE	"\""
    158   1.1  christos 
    159   1.1  christos /* return next token in configuration line */
    160   1.1  christos char *
    161   1.1  christos strdelim(char **s)
    162   1.1  christos {
    163   1.1  christos 	char *old;
    164   1.1  christos 	int wspace = 0;
    165   1.1  christos 
    166   1.1  christos 	if (*s == NULL)
    167   1.1  christos 		return NULL;
    168   1.1  christos 
    169   1.1  christos 	old = *s;
    170   1.1  christos 
    171   1.1  christos 	*s = strpbrk(*s, WHITESPACE QUOTE "=");
    172   1.1  christos 	if (*s == NULL)
    173   1.1  christos 		return (old);
    174   1.1  christos 
    175   1.1  christos 	if (*s[0] == '\"') {
    176   1.1  christos 		memmove(*s, *s + 1, strlen(*s)); /* move nul too */
    177   1.1  christos 		/* Find matching quote */
    178   1.1  christos 		if ((*s = strpbrk(*s, QUOTE)) == NULL) {
    179   1.1  christos 			return (NULL);		/* no matching quote */
    180   1.1  christos 		} else {
    181   1.1  christos 			*s[0] = '\0';
    182   1.4      adam 			*s += strspn(*s + 1, WHITESPACE) + 1;
    183   1.1  christos 			return (old);
    184   1.1  christos 		}
    185   1.1  christos 	}
    186   1.1  christos 
    187   1.1  christos 	/* Allow only one '=' to be skipped */
    188   1.1  christos 	if (*s[0] == '=')
    189   1.1  christos 		wspace = 1;
    190   1.1  christos 	*s[0] = '\0';
    191   1.1  christos 
    192   1.1  christos 	/* Skip any extra whitespace after first token */
    193   1.1  christos 	*s += strspn(*s + 1, WHITESPACE) + 1;
    194   1.1  christos 	if (*s[0] == '=' && !wspace)
    195   1.1  christos 		*s += strspn(*s + 1, WHITESPACE) + 1;
    196   1.1  christos 
    197   1.1  christos 	return (old);
    198   1.1  christos }
    199   1.1  christos 
    200   1.1  christos struct passwd *
    201   1.1  christos pwcopy(struct passwd *pw)
    202   1.1  christos {
    203   1.1  christos 	struct passwd *copy = xcalloc(1, sizeof(*copy));
    204   1.1  christos 
    205   1.1  christos 	copy->pw_name = xstrdup(pw->pw_name);
    206   1.1  christos 	copy->pw_passwd = xstrdup(pw->pw_passwd);
    207   1.1  christos 	copy->pw_gecos = xstrdup(pw->pw_gecos);
    208   1.1  christos 	copy->pw_uid = pw->pw_uid;
    209   1.1  christos 	copy->pw_gid = pw->pw_gid;
    210   1.1  christos 	copy->pw_expire = pw->pw_expire;
    211   1.1  christos 	copy->pw_change = pw->pw_change;
    212   1.1  christos 	copy->pw_class = xstrdup(pw->pw_class);
    213   1.1  christos 	copy->pw_dir = xstrdup(pw->pw_dir);
    214   1.1  christos 	copy->pw_shell = xstrdup(pw->pw_shell);
    215   1.1  christos 	return copy;
    216   1.1  christos }
    217   1.1  christos 
    218   1.1  christos /*
    219   1.1  christos  * Convert ASCII string to TCP/IP port number.
    220   1.1  christos  * Port must be >=0 and <=65535.
    221   1.1  christos  * Return -1 if invalid.
    222   1.1  christos  */
    223   1.1  christos int
    224   1.1  christos a2port(const char *s)
    225   1.1  christos {
    226   1.1  christos 	long long port;
    227   1.1  christos 	const char *errstr;
    228   1.1  christos 
    229   1.1  christos 	port = strtonum(s, 0, 65535, &errstr);
    230   1.1  christos 	if (errstr != NULL)
    231   1.1  christos 		return -1;
    232   1.1  christos 	return (int)port;
    233   1.1  christos }
    234   1.1  christos 
    235   1.1  christos int
    236   1.1  christos a2tun(const char *s, int *remote)
    237   1.1  christos {
    238   1.1  christos 	const char *errstr = NULL;
    239   1.1  christos 	char *sp, *ep;
    240   1.1  christos 	int tun;
    241   1.1  christos 
    242   1.1  christos 	if (remote != NULL) {
    243   1.1  christos 		*remote = SSH_TUNID_ANY;
    244   1.1  christos 		sp = xstrdup(s);
    245   1.1  christos 		if ((ep = strchr(sp, ':')) == NULL) {
    246   1.8  christos 			free(sp);
    247   1.1  christos 			return (a2tun(s, NULL));
    248   1.1  christos 		}
    249   1.1  christos 		ep[0] = '\0'; ep++;
    250   1.1  christos 		*remote = a2tun(ep, NULL);
    251   1.1  christos 		tun = a2tun(sp, NULL);
    252   1.8  christos 		free(sp);
    253   1.1  christos 		return (*remote == SSH_TUNID_ERR ? *remote : tun);
    254   1.1  christos 	}
    255   1.1  christos 
    256   1.1  christos 	if (strcasecmp(s, "any") == 0)
    257   1.1  christos 		return (SSH_TUNID_ANY);
    258   1.1  christos 
    259   1.1  christos 	tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr);
    260   1.1  christos 	if (errstr != NULL)
    261   1.1  christos 		return (SSH_TUNID_ERR);
    262   1.1  christos 
    263   1.1  christos 	return (tun);
    264   1.1  christos }
    265   1.1  christos 
    266   1.1  christos #define SECONDS		1
    267   1.1  christos #define MINUTES		(SECONDS * 60)
    268   1.1  christos #define HOURS		(MINUTES * 60)
    269   1.1  christos #define DAYS		(HOURS * 24)
    270   1.1  christos #define WEEKS		(DAYS * 7)
    271   1.1  christos 
    272   1.1  christos /*
    273   1.1  christos  * Convert a time string into seconds; format is
    274   1.1  christos  * a sequence of:
    275   1.1  christos  *      time[qualifier]
    276   1.1  christos  *
    277   1.1  christos  * Valid time qualifiers are:
    278   1.1  christos  *      <none>  seconds
    279   1.1  christos  *      s|S     seconds
    280   1.1  christos  *      m|M     minutes
    281   1.1  christos  *      h|H     hours
    282   1.1  christos  *      d|D     days
    283   1.1  christos  *      w|W     weeks
    284   1.1  christos  *
    285   1.1  christos  * Examples:
    286   1.1  christos  *      90m     90 minutes
    287   1.1  christos  *      1h30m   90 minutes
    288   1.1  christos  *      2d      2 days
    289   1.1  christos  *      1w      1 week
    290   1.1  christos  *
    291   1.1  christos  * Return -1 if time string is invalid.
    292   1.1  christos  */
    293   1.1  christos long
    294   1.1  christos convtime(const char *s)
    295   1.1  christos {
    296   1.1  christos 	long total, secs;
    297   1.1  christos 	const char *p;
    298   1.1  christos 	char *endp;
    299   1.1  christos 
    300   1.1  christos 	errno = 0;
    301   1.1  christos 	total = 0;
    302   1.1  christos 	p = s;
    303   1.1  christos 
    304   1.1  christos 	if (p == NULL || *p == '\0')
    305   1.1  christos 		return -1;
    306   1.1  christos 
    307   1.1  christos 	while (*p) {
    308   1.1  christos 		secs = strtol(p, &endp, 10);
    309   1.1  christos 		if (p == endp ||
    310   1.1  christos 		    (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
    311   1.1  christos 		    secs < 0)
    312   1.1  christos 			return -1;
    313   1.1  christos 
    314   1.1  christos 		switch (*endp++) {
    315   1.1  christos 		case '\0':
    316   1.1  christos 			endp--;
    317   1.1  christos 			break;
    318   1.1  christos 		case 's':
    319   1.1  christos 		case 'S':
    320   1.1  christos 			break;
    321   1.1  christos 		case 'm':
    322   1.1  christos 		case 'M':
    323   1.1  christos 			secs *= MINUTES;
    324   1.1  christos 			break;
    325   1.1  christos 		case 'h':
    326   1.1  christos 		case 'H':
    327   1.1  christos 			secs *= HOURS;
    328   1.1  christos 			break;
    329   1.1  christos 		case 'd':
    330   1.1  christos 		case 'D':
    331   1.1  christos 			secs *= DAYS;
    332   1.1  christos 			break;
    333   1.1  christos 		case 'w':
    334   1.1  christos 		case 'W':
    335   1.1  christos 			secs *= WEEKS;
    336   1.1  christos 			break;
    337   1.1  christos 		default:
    338   1.1  christos 			return -1;
    339   1.1  christos 		}
    340   1.1  christos 		total += secs;
    341   1.1  christos 		if (total < 0)
    342   1.1  christos 			return -1;
    343   1.1  christos 		p = endp;
    344   1.1  christos 	}
    345   1.1  christos 
    346   1.1  christos 	return total;
    347   1.1  christos }
    348   1.1  christos 
    349   1.1  christos /*
    350   1.1  christos  * Returns a standardized host+port identifier string.
    351   1.1  christos  * Caller must free returned string.
    352   1.1  christos  */
    353   1.1  christos char *
    354   1.1  christos put_host_port(const char *host, u_short port)
    355   1.1  christos {
    356   1.1  christos 	char *hoststr;
    357   1.1  christos 
    358   1.1  christos 	if (port == 0 || port == SSH_DEFAULT_PORT)
    359   1.1  christos 		return(xstrdup(host));
    360   1.1  christos 	if (asprintf(&hoststr, "[%s]:%d", host, (int)port) < 0)
    361   1.1  christos 		fatal("put_host_port: asprintf: %s", strerror(errno));
    362   1.1  christos 	debug3("put_host_port: %s", hoststr);
    363   1.1  christos 	return hoststr;
    364   1.1  christos }
    365   1.1  christos 
    366   1.1  christos /*
    367   1.1  christos  * Search for next delimiter between hostnames/addresses and ports.
    368   1.1  christos  * Argument may be modified (for termination).
    369   1.1  christos  * Returns *cp if parsing succeeds.
    370   1.1  christos  * *cp is set to the start of the next delimiter, if one was found.
    371   1.1  christos  * If this is the last field, *cp is set to NULL.
    372   1.1  christos  */
    373   1.1  christos char *
    374   1.1  christos hpdelim(char **cp)
    375   1.1  christos {
    376   1.1  christos 	char *s, *old;
    377   1.1  christos 
    378   1.1  christos 	if (cp == NULL || *cp == NULL)
    379   1.1  christos 		return NULL;
    380   1.1  christos 
    381   1.1  christos 	old = s = *cp;
    382   1.1  christos 	if (*s == '[') {
    383   1.1  christos 		if ((s = strchr(s, ']')) == NULL)
    384   1.1  christos 			return NULL;
    385   1.1  christos 		else
    386   1.1  christos 			s++;
    387   1.1  christos 	} else if ((s = strpbrk(s, ":/")) == NULL)
    388   1.1  christos 		s = *cp + strlen(*cp); /* skip to end (see first case below) */
    389   1.1  christos 
    390   1.1  christos 	switch (*s) {
    391   1.1  christos 	case '\0':
    392   1.1  christos 		*cp = NULL;	/* no more fields*/
    393   1.1  christos 		break;
    394   1.1  christos 
    395   1.1  christos 	case ':':
    396   1.1  christos 	case '/':
    397   1.1  christos 		*s = '\0';	/* terminate */
    398   1.1  christos 		*cp = s + 1;
    399   1.1  christos 		break;
    400   1.1  christos 
    401   1.1  christos 	default:
    402   1.1  christos 		return NULL;
    403   1.1  christos 	}
    404   1.1  christos 
    405   1.1  christos 	return old;
    406   1.1  christos }
    407   1.1  christos 
    408   1.1  christos char *
    409   1.1  christos cleanhostname(char *host)
    410   1.1  christos {
    411   1.1  christos 	if (*host == '[' && host[strlen(host) - 1] == ']') {
    412   1.1  christos 		host[strlen(host) - 1] = '\0';
    413   1.1  christos 		return (host + 1);
    414   1.1  christos 	} else
    415   1.1  christos 		return host;
    416   1.1  christos }
    417   1.1  christos 
    418   1.1  christos char *
    419   1.1  christos colon(char *cp)
    420   1.1  christos {
    421   1.1  christos 	int flag = 0;
    422   1.1  christos 
    423   1.1  christos 	if (*cp == ':')		/* Leading colon is part of file name. */
    424   1.4      adam 		return NULL;
    425   1.1  christos 	if (*cp == '[')
    426   1.1  christos 		flag = 1;
    427   1.1  christos 
    428   1.1  christos 	for (; *cp; ++cp) {
    429   1.1  christos 		if (*cp == '@' && *(cp+1) == '[')
    430   1.1  christos 			flag = 1;
    431   1.1  christos 		if (*cp == ']' && *(cp+1) == ':' && flag)
    432   1.1  christos 			return (cp+1);
    433   1.1  christos 		if (*cp == ':' && !flag)
    434   1.1  christos 			return (cp);
    435   1.1  christos 		if (*cp == '/')
    436   1.4      adam 			return NULL;
    437   1.1  christos 	}
    438   1.4      adam 	return NULL;
    439   1.1  christos }
    440   1.1  christos 
    441  1.13  christos /*
    442  1.13  christos  * Parse a [user@]host[:port] string.
    443  1.13  christos  * Caller must free returned user and host.
    444  1.13  christos  * Any of the pointer return arguments may be NULL (useful for syntax checking).
    445  1.13  christos  * If user was not specified then *userp will be set to NULL.
    446  1.13  christos  * If port was not specified then *portp will be -1.
    447  1.13  christos  * Returns 0 on success, -1 on failure.
    448  1.13  christos  */
    449  1.13  christos int
    450  1.13  christos parse_user_host_port(const char *s, char **userp, char **hostp, int *portp)
    451  1.13  christos {
    452  1.13  christos 	char *sdup, *cp, *tmp;
    453  1.13  christos 	char *user = NULL, *host = NULL;
    454  1.13  christos 	int port = -1, ret = -1;
    455  1.13  christos 
    456  1.13  christos 	if (userp != NULL)
    457  1.13  christos 		*userp = NULL;
    458  1.13  christos 	if (hostp != NULL)
    459  1.13  christos 		*hostp = NULL;
    460  1.13  christos 	if (portp != NULL)
    461  1.13  christos 		*portp = -1;
    462  1.13  christos 
    463  1.13  christos 	if ((sdup = tmp = strdup(s)) == NULL)
    464  1.13  christos 		return -1;
    465  1.13  christos 	/* Extract optional username */
    466  1.13  christos 	if ((cp = strchr(tmp, '@')) != NULL) {
    467  1.13  christos 		*cp = '\0';
    468  1.13  christos 		if (*tmp == '\0')
    469  1.13  christos 			goto out;
    470  1.13  christos 		if ((user = strdup(tmp)) == NULL)
    471  1.13  christos 			goto out;
    472  1.13  christos 		tmp = cp + 1;
    473  1.13  christos 	}
    474  1.13  christos 	/* Extract mandatory hostname */
    475  1.13  christos 	if ((cp = hpdelim(&tmp)) == NULL || *cp == '\0')
    476  1.13  christos 		goto out;
    477  1.13  christos 	host = xstrdup(cleanhostname(cp));
    478  1.13  christos 	/* Convert and verify optional port */
    479  1.13  christos 	if (tmp != NULL && *tmp != '\0') {
    480  1.13  christos 		if ((port = a2port(tmp)) <= 0)
    481  1.13  christos 			goto out;
    482  1.13  christos 	}
    483  1.13  christos 	/* Success */
    484  1.13  christos 	if (userp != NULL) {
    485  1.13  christos 		*userp = user;
    486  1.13  christos 		user = NULL;
    487  1.13  christos 	}
    488  1.13  christos 	if (hostp != NULL) {
    489  1.13  christos 		*hostp = host;
    490  1.13  christos 		host = NULL;
    491  1.13  christos 	}
    492  1.13  christos 	if (portp != NULL)
    493  1.13  christos 		*portp = port;
    494  1.13  christos 	ret = 0;
    495  1.13  christos  out:
    496  1.13  christos 	free(sdup);
    497  1.13  christos 	free(user);
    498  1.13  christos 	free(host);
    499  1.13  christos 	return ret;
    500  1.13  christos }
    501  1.13  christos 
    502   1.1  christos /* function to assist building execv() arguments */
    503   1.1  christos void
    504   1.5  christos addargs(arglist *args, const char *fmt, ...)
    505   1.1  christos {
    506   1.1  christos 	va_list ap;
    507   1.1  christos 	char *cp;
    508   1.1  christos 	u_int nalloc;
    509   1.1  christos 	int r;
    510   1.1  christos 
    511   1.1  christos 	va_start(ap, fmt);
    512   1.1  christos 	r = vasprintf(&cp, fmt, ap);
    513   1.1  christos 	va_end(ap);
    514   1.1  christos 	if (r == -1)
    515   1.1  christos 		fatal("addargs: argument too long");
    516   1.1  christos 
    517   1.1  christos 	nalloc = args->nalloc;
    518   1.1  christos 	if (args->list == NULL) {
    519   1.1  christos 		nalloc = 32;
    520   1.1  christos 		args->num = 0;
    521   1.1  christos 	} else if (args->num+2 >= nalloc)
    522   1.1  christos 		nalloc *= 2;
    523   1.1  christos 
    524  1.11  christos 	args->list = xreallocarray(args->list, nalloc, sizeof(char *));
    525   1.1  christos 	args->nalloc = nalloc;
    526   1.1  christos 	args->list[args->num++] = cp;
    527   1.1  christos 	args->list[args->num] = NULL;
    528   1.1  christos }
    529   1.1  christos 
    530   1.1  christos void
    531   1.5  christos replacearg(arglist *args, u_int which, const char *fmt, ...)
    532   1.1  christos {
    533   1.1  christos 	va_list ap;
    534   1.1  christos 	char *cp;
    535   1.1  christos 	int r;
    536   1.1  christos 
    537   1.1  christos 	va_start(ap, fmt);
    538   1.1  christos 	r = vasprintf(&cp, fmt, ap);
    539   1.1  christos 	va_end(ap);
    540   1.1  christos 	if (r == -1)
    541   1.1  christos 		fatal("replacearg: argument too long");
    542   1.1  christos 
    543   1.1  christos 	if (which >= args->num)
    544   1.1  christos 		fatal("replacearg: tried to replace invalid arg %d >= %d",
    545   1.1  christos 		    which, args->num);
    546   1.8  christos 	free(args->list[which]);
    547   1.1  christos 	args->list[which] = cp;
    548   1.1  christos }
    549   1.1  christos 
    550   1.1  christos void
    551   1.1  christos freeargs(arglist *args)
    552   1.1  christos {
    553   1.1  christos 	u_int i;
    554   1.1  christos 
    555   1.1  christos 	if (args->list != NULL) {
    556   1.1  christos 		for (i = 0; i < args->num; i++)
    557   1.8  christos 			free(args->list[i]);
    558   1.8  christos 		free(args->list);
    559   1.1  christos 		args->nalloc = args->num = 0;
    560   1.1  christos 		args->list = NULL;
    561   1.1  christos 	}
    562   1.1  christos }
    563   1.1  christos 
    564   1.1  christos /*
    565   1.1  christos  * Expands tildes in the file name.  Returns data allocated by xmalloc.
    566   1.1  christos  * Warning: this calls getpw*.
    567   1.1  christos  */
    568   1.1  christos char *
    569   1.1  christos tilde_expand_filename(const char *filename, uid_t uid)
    570   1.1  christos {
    571   1.8  christos 	const char *path, *sep;
    572   1.8  christos 	char user[128], *ret, *homedir;
    573   1.1  christos 	struct passwd *pw;
    574   1.1  christos 	u_int len, slash;
    575   1.1  christos 
    576   1.1  christos 	if (*filename != '~')
    577   1.1  christos 		return (xstrdup(filename));
    578   1.1  christos 	filename++;
    579   1.1  christos 
    580   1.1  christos 	path = strchr(filename, '/');
    581   1.1  christos 	if (path != NULL && path > filename) {		/* ~user/path */
    582   1.1  christos 		slash = path - filename;
    583   1.1  christos 		if (slash > sizeof(user) - 1)
    584   1.1  christos 			fatal("tilde_expand_filename: ~username too long");
    585   1.1  christos 		memcpy(user, filename, slash);
    586   1.1  christos 		user[slash] = '\0';
    587   1.1  christos 		if ((pw = getpwnam(user)) == NULL)
    588   1.1  christos 			fatal("tilde_expand_filename: No such user %s", user);
    589   1.2  christos 		homedir = pw->pw_dir;
    590   1.2  christos 	} else {
    591   1.2  christos 		if ((pw = getpwuid(uid)) == NULL)	/* ~/path */
    592   1.2  christos 			fatal("tilde_expand_filename: No such uid %ld",
    593   1.2  christos 			    (long)uid);
    594   1.2  christos 		homedir = pw->pw_dir;
    595   1.2  christos 	}
    596   1.1  christos 
    597   1.1  christos 	/* Make sure directory has a trailing '/' */
    598   1.2  christos 	len = strlen(homedir);
    599   1.8  christos 	if (len == 0 || homedir[len - 1] != '/')
    600   1.8  christos 		sep = "/";
    601   1.8  christos 	else
    602   1.8  christos 		sep = "";
    603   1.1  christos 
    604   1.1  christos 	/* Skip leading '/' from specified path */
    605   1.1  christos 	if (path != NULL)
    606   1.1  christos 		filename = path + 1;
    607   1.8  christos 
    608  1.10  christos 	if (xasprintf(&ret, "%s%s%s", homedir, sep, filename) >= PATH_MAX)
    609   1.1  christos 		fatal("tilde_expand_filename: Path too long");
    610   1.1  christos 
    611   1.8  christos 	return (ret);
    612   1.1  christos }
    613   1.1  christos 
    614   1.1  christos /*
    615   1.1  christos  * Expand a string with a set of %[char] escapes. A number of escapes may be
    616   1.1  christos  * specified as (char *escape_chars, char *replacement) pairs. The list must
    617   1.1  christos  * be terminated by a NULL escape_char. Returns replaced string in memory
    618   1.1  christos  * allocated by xmalloc.
    619   1.1  christos  */
    620   1.1  christos char *
    621   1.1  christos percent_expand(const char *string, ...)
    622   1.1  christos {
    623   1.1  christos #define EXPAND_MAX_KEYS	16
    624   1.4      adam 	u_int num_keys, i, j;
    625   1.1  christos 	struct {
    626   1.1  christos 		const char *key;
    627   1.1  christos 		const char *repl;
    628   1.1  christos 	} keys[EXPAND_MAX_KEYS];
    629   1.1  christos 	char buf[4096];
    630   1.1  christos 	va_list ap;
    631   1.1  christos 
    632   1.1  christos 	/* Gather keys */
    633   1.1  christos 	va_start(ap, string);
    634   1.1  christos 	for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) {
    635   1.1  christos 		keys[num_keys].key = va_arg(ap, char *);
    636   1.1  christos 		if (keys[num_keys].key == NULL)
    637   1.1  christos 			break;
    638   1.1  christos 		keys[num_keys].repl = va_arg(ap, char *);
    639   1.1  christos 		if (keys[num_keys].repl == NULL)
    640   1.4      adam 			fatal("%s: NULL replacement", __func__);
    641   1.1  christos 	}
    642   1.4      adam 	if (num_keys == EXPAND_MAX_KEYS && va_arg(ap, char *) != NULL)
    643   1.4      adam 		fatal("%s: too many keys", __func__);
    644   1.1  christos 	va_end(ap);
    645   1.1  christos 
    646   1.1  christos 	/* Expand string */
    647   1.1  christos 	*buf = '\0';
    648   1.1  christos 	for (i = 0; *string != '\0'; string++) {
    649   1.1  christos 		if (*string != '%') {
    650   1.1  christos  append:
    651   1.1  christos 			buf[i++] = *string;
    652   1.1  christos 			if (i >= sizeof(buf))
    653   1.4      adam 				fatal("%s: string too long", __func__);
    654   1.1  christos 			buf[i] = '\0';
    655   1.1  christos 			continue;
    656   1.1  christos 		}
    657   1.1  christos 		string++;
    658   1.4      adam 		/* %% case */
    659   1.1  christos 		if (*string == '%')
    660   1.1  christos 			goto append;
    661  1.12  christos 		if (*string == '\0')
    662  1.12  christos 			fatal("%s: invalid format", __func__);
    663   1.1  christos 		for (j = 0; j < num_keys; j++) {
    664   1.1  christos 			if (strchr(keys[j].key, *string) != NULL) {
    665   1.1  christos 				i = strlcat(buf, keys[j].repl, sizeof(buf));
    666   1.1  christos 				if (i >= sizeof(buf))
    667   1.4      adam 					fatal("%s: string too long", __func__);
    668   1.1  christos 				break;
    669   1.1  christos 			}
    670   1.1  christos 		}
    671   1.1  christos 		if (j >= num_keys)
    672   1.4      adam 			fatal("%s: unknown key %%%c", __func__, *string);
    673   1.1  christos 	}
    674   1.1  christos 	return (xstrdup(buf));
    675   1.1  christos #undef EXPAND_MAX_KEYS
    676   1.1  christos }
    677   1.1  christos 
    678   1.1  christos /*
    679   1.1  christos  * Read an entire line from a public key file into a static buffer, discarding
    680   1.1  christos  * lines that exceed the buffer size.  Returns 0 on success, -1 on failure.
    681   1.1  christos  */
    682   1.1  christos int
    683   1.1  christos read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
    684   1.1  christos    u_long *lineno)
    685   1.1  christos {
    686   1.1  christos 	while (fgets(buf, bufsz, f) != NULL) {
    687   1.1  christos 		if (buf[0] == '\0')
    688   1.1  christos 			continue;
    689   1.1  christos 		(*lineno)++;
    690   1.1  christos 		if (buf[strlen(buf) - 1] == '\n' || feof(f)) {
    691   1.1  christos 			return 0;
    692   1.1  christos 		} else {
    693   1.1  christos 			debug("%s: %s line %lu exceeds size limit", __func__,
    694   1.1  christos 			    filename, *lineno);
    695   1.1  christos 			/* discard remainder of line */
    696   1.1  christos 			while (fgetc(f) != '\n' && !feof(f))
    697   1.1  christos 				;	/* nothing */
    698   1.1  christos 		}
    699   1.1  christos 	}
    700   1.1  christos 	return -1;
    701   1.1  christos }
    702   1.1  christos 
    703   1.1  christos int
    704   1.1  christos tun_open(int tun, int mode)
    705   1.1  christos {
    706   1.1  christos 	struct ifreq ifr;
    707  1.12  christos 	char name[100];
    708  1.12  christos 	int fd = -1, sock;
    709  1.12  christos 	const char *tunbase = "tun";
    710  1.12  christos 
    711  1.12  christos 	if (mode == SSH_TUNMODE_ETHERNET)
    712  1.12  christos 		tunbase = "tap";
    713   1.1  christos 
    714   1.1  christos 	/* Open the tunnel device */
    715   1.1  christos 	if (tun <= SSH_TUNID_MAX) {
    716  1.12  christos 		snprintf(name, sizeof(name), "/dev/%s%d", tunbase, tun);
    717  1.12  christos 		fd = open(name, O_RDWR);
    718   1.1  christos 	} else if (tun == SSH_TUNID_ANY) {
    719   1.1  christos 		for (tun = 100; tun >= 0; tun--) {
    720  1.12  christos 			snprintf(name, sizeof(name), "/dev/%s%d",
    721  1.12  christos 			    tunbase, tun);
    722  1.12  christos 			if ((fd = open(name, O_RDWR)) >= 0)
    723   1.1  christos 				break;
    724   1.1  christos 		}
    725   1.1  christos 	} else {
    726   1.1  christos 		debug("%s: invalid tunnel %u", __func__, tun);
    727  1.12  christos 		return -1;
    728   1.1  christos 	}
    729   1.1  christos 
    730   1.1  christos 	if (fd < 0) {
    731  1.12  christos 		debug("%s: %s open: %s", __func__, name, strerror(errno));
    732  1.12  christos 		return -1;
    733   1.1  christos 	}
    734   1.1  christos 
    735   1.1  christos 
    736  1.12  christos #ifdef TUNSIFHEAD
    737   1.2  christos 	/* Turn on tunnel headers */
    738  1.12  christos 	int flag = 1;
    739   1.2  christos 	if (mode != SSH_TUNMODE_ETHERNET &&
    740   1.2  christos 	    ioctl(fd, TUNSIFHEAD, &flag) == -1) {
    741   1.2  christos 		debug("%s: ioctl(%d, TUNSIFHEAD, 1): %s", __func__, fd,
    742   1.2  christos 		    strerror(errno));
    743   1.2  christos 		close(fd);
    744   1.2  christos 		return -1;
    745   1.2  christos 	}
    746  1.12  christos #endif
    747   1.2  christos 
    748   1.2  christos 	debug("%s: %s mode %d fd %d", __func__, ifr.ifr_name, mode, fd);
    749  1.12  christos 	/* Bring interface up if it is not already */
    750   1.3   jnemeth 	snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", tunbase, tun);
    751   1.1  christos 	if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
    752   1.1  christos 		goto failed;
    753   1.1  christos 
    754  1.12  christos 	if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) {
    755  1.12  christos 		debug("%s: get interface %s flags: %s", __func__,
    756  1.12  christos 		    ifr.ifr_name, strerror(errno));
    757   1.1  christos 		goto failed;
    758  1.12  christos 	}
    759   1.1  christos 
    760  1.12  christos 	if (!(ifr.ifr_flags & IFF_UP)) {
    761  1.12  christos 		ifr.ifr_flags |= IFF_UP;
    762  1.12  christos 		if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) {
    763  1.12  christos 			debug("%s: activate interface %s: %s", __func__,
    764  1.12  christos 			    ifr.ifr_name, strerror(errno));
    765  1.12  christos 			goto failed;
    766  1.12  christos 		}
    767  1.12  christos 	}
    768   1.1  christos 
    769   1.1  christos 	close(sock);
    770  1.12  christos 	return fd;
    771   1.1  christos 
    772   1.1  christos  failed:
    773   1.1  christos 	if (fd >= 0)
    774   1.1  christos 		close(fd);
    775   1.1  christos 	if (sock >= 0)
    776   1.1  christos 		close(sock);
    777   1.2  christos 	debug("%s: failed to set %s mode %d: %s", __func__, ifr.ifr_name,
    778   1.1  christos 	    mode, strerror(errno));
    779  1.12  christos 	return -1;
    780   1.1  christos }
    781   1.1  christos 
    782   1.1  christos void
    783   1.1  christos sanitise_stdfd(void)
    784   1.1  christos {
    785   1.1  christos 	int nullfd, dupfd;
    786   1.1  christos 
    787   1.1  christos 	if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
    788   1.1  christos 		fprintf(stderr, "Couldn't open /dev/null: %s\n",
    789   1.1  christos 		    strerror(errno));
    790   1.1  christos 		exit(1);
    791   1.1  christos 	}
    792  1.13  christos 	while (++dupfd <= STDERR_FILENO) {
    793  1.13  christos 		/* Only populate closed fds. */
    794  1.13  christos 		if (fcntl(dupfd, F_GETFL) == -1 && errno == EBADF) {
    795  1.13  christos 			if (dup2(nullfd, dupfd) == -1) {
    796  1.13  christos 				fprintf(stderr, "dup2: %s\n", strerror(errno));
    797  1.13  christos 				exit(1);
    798  1.13  christos 			}
    799   1.1  christos 		}
    800   1.1  christos 	}
    801  1.13  christos 	if (nullfd > STDERR_FILENO)
    802   1.1  christos 		close(nullfd);
    803   1.1  christos }
    804   1.1  christos 
    805   1.1  christos char *
    806   1.1  christos tohex(const void *vp, size_t l)
    807   1.1  christos {
    808   1.1  christos 	const u_char *p = (const u_char *)vp;
    809   1.1  christos 	char b[3], *r;
    810   1.1  christos 	size_t i, hl;
    811   1.1  christos 
    812   1.1  christos 	if (l > 65536)
    813   1.1  christos 		return xstrdup("tohex: length > 65536");
    814   1.1  christos 
    815   1.1  christos 	hl = l * 2 + 1;
    816   1.1  christos 	r = xcalloc(1, hl);
    817   1.1  christos 	for (i = 0; i < l; i++) {
    818   1.1  christos 		snprintf(b, sizeof(b), "%02x", p[i]);
    819   1.1  christos 		strlcat(r, b, hl);
    820   1.1  christos 	}
    821   1.1  christos 	return (r);
    822   1.1  christos }
    823   1.1  christos 
    824   1.1  christos u_int64_t
    825   1.1  christos get_u64(const void *vp)
    826   1.1  christos {
    827   1.1  christos 	const u_char *p = (const u_char *)vp;
    828   1.1  christos 	u_int64_t v;
    829   1.1  christos 
    830   1.1  christos 	v  = (u_int64_t)p[0] << 56;
    831   1.1  christos 	v |= (u_int64_t)p[1] << 48;
    832   1.1  christos 	v |= (u_int64_t)p[2] << 40;
    833   1.1  christos 	v |= (u_int64_t)p[3] << 32;
    834   1.1  christos 	v |= (u_int64_t)p[4] << 24;
    835   1.1  christos 	v |= (u_int64_t)p[5] << 16;
    836   1.1  christos 	v |= (u_int64_t)p[6] << 8;
    837   1.1  christos 	v |= (u_int64_t)p[7];
    838   1.1  christos 
    839   1.1  christos 	return (v);
    840   1.1  christos }
    841   1.1  christos 
    842   1.1  christos u_int32_t
    843   1.1  christos get_u32(const void *vp)
    844   1.1  christos {
    845   1.1  christos 	const u_char *p = (const u_char *)vp;
    846   1.1  christos 	u_int32_t v;
    847   1.1  christos 
    848   1.1  christos 	v  = (u_int32_t)p[0] << 24;
    849   1.1  christos 	v |= (u_int32_t)p[1] << 16;
    850   1.1  christos 	v |= (u_int32_t)p[2] << 8;
    851   1.1  christos 	v |= (u_int32_t)p[3];
    852   1.1  christos 
    853   1.1  christos 	return (v);
    854   1.1  christos }
    855   1.1  christos 
    856   1.9  christos u_int32_t
    857   1.9  christos get_u32_le(const void *vp)
    858   1.9  christos {
    859   1.9  christos 	const u_char *p = (const u_char *)vp;
    860   1.9  christos 	u_int32_t v;
    861   1.9  christos 
    862   1.9  christos 	v  = (u_int32_t)p[0];
    863   1.9  christos 	v |= (u_int32_t)p[1] << 8;
    864   1.9  christos 	v |= (u_int32_t)p[2] << 16;
    865   1.9  christos 	v |= (u_int32_t)p[3] << 24;
    866   1.9  christos 
    867   1.9  christos 	return (v);
    868   1.9  christos }
    869   1.9  christos 
    870   1.1  christos u_int16_t
    871   1.1  christos get_u16(const void *vp)
    872   1.1  christos {
    873   1.1  christos 	const u_char *p = (const u_char *)vp;
    874   1.1  christos 	u_int16_t v;
    875   1.1  christos 
    876   1.1  christos 	v  = (u_int16_t)p[0] << 8;
    877   1.1  christos 	v |= (u_int16_t)p[1];
    878   1.1  christos 
    879   1.1  christos 	return (v);
    880   1.1  christos }
    881   1.1  christos 
    882   1.1  christos void
    883   1.1  christos put_u64(void *vp, u_int64_t v)
    884   1.1  christos {
    885   1.1  christos 	u_char *p = (u_char *)vp;
    886   1.1  christos 
    887   1.1  christos 	p[0] = (u_char)(v >> 56) & 0xff;
    888   1.1  christos 	p[1] = (u_char)(v >> 48) & 0xff;
    889   1.1  christos 	p[2] = (u_char)(v >> 40) & 0xff;
    890   1.1  christos 	p[3] = (u_char)(v >> 32) & 0xff;
    891   1.1  christos 	p[4] = (u_char)(v >> 24) & 0xff;
    892   1.1  christos 	p[5] = (u_char)(v >> 16) & 0xff;
    893   1.1  christos 	p[6] = (u_char)(v >> 8) & 0xff;
    894   1.1  christos 	p[7] = (u_char)v & 0xff;
    895   1.1  christos }
    896   1.1  christos 
    897   1.1  christos void
    898   1.1  christos put_u32(void *vp, u_int32_t v)
    899   1.1  christos {
    900   1.1  christos 	u_char *p = (u_char *)vp;
    901   1.1  christos 
    902   1.1  christos 	p[0] = (u_char)(v >> 24) & 0xff;
    903   1.1  christos 	p[1] = (u_char)(v >> 16) & 0xff;
    904   1.1  christos 	p[2] = (u_char)(v >> 8) & 0xff;
    905   1.1  christos 	p[3] = (u_char)v & 0xff;
    906   1.1  christos }
    907   1.1  christos 
    908   1.9  christos void
    909   1.9  christos put_u32_le(void *vp, u_int32_t v)
    910   1.9  christos {
    911   1.9  christos 	u_char *p = (u_char *)vp;
    912   1.9  christos 
    913   1.9  christos 	p[0] = (u_char)v & 0xff;
    914   1.9  christos 	p[1] = (u_char)(v >> 8) & 0xff;
    915   1.9  christos 	p[2] = (u_char)(v >> 16) & 0xff;
    916   1.9  christos 	p[3] = (u_char)(v >> 24) & 0xff;
    917   1.9  christos }
    918   1.1  christos 
    919   1.1  christos void
    920   1.1  christos put_u16(void *vp, u_int16_t v)
    921   1.1  christos {
    922   1.1  christos 	u_char *p = (u_char *)vp;
    923   1.1  christos 
    924   1.1  christos 	p[0] = (u_char)(v >> 8) & 0xff;
    925   1.1  christos 	p[1] = (u_char)v & 0xff;
    926   1.1  christos }
    927   1.1  christos 
    928   1.1  christos void
    929   1.1  christos ms_subtract_diff(struct timeval *start, int *ms)
    930   1.1  christos {
    931   1.1  christos 	struct timeval diff, finish;
    932   1.1  christos 
    933   1.1  christos 	gettimeofday(&finish, NULL);
    934   1.1  christos 	timersub(&finish, start, &diff);
    935   1.1  christos 	*ms -= (diff.tv_sec * 1000) + (diff.tv_usec / 1000);
    936   1.1  christos }
    937   1.1  christos 
    938   1.1  christos void
    939   1.1  christos ms_to_timeval(struct timeval *tv, int ms)
    940   1.1  christos {
    941   1.1  christos 	if (ms < 0)
    942   1.1  christos 		ms = 0;
    943   1.1  christos 	tv->tv_sec = ms / 1000;
    944   1.1  christos 	tv->tv_usec = (ms % 1000) * 1000;
    945   1.1  christos }
    946   1.1  christos 
    947   1.8  christos time_t
    948   1.8  christos monotime(void)
    949   1.8  christos {
    950   1.8  christos 	struct timespec ts;
    951   1.8  christos 
    952   1.8  christos 	if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
    953   1.8  christos 		fatal("clock_gettime: %s", strerror(errno));
    954   1.8  christos 
    955   1.8  christos 	return (ts.tv_sec);
    956   1.8  christos }
    957   1.8  christos 
    958  1.13  christos double
    959  1.13  christos monotime_double(void)
    960  1.13  christos {
    961  1.13  christos 	struct timespec ts;
    962  1.13  christos 
    963  1.13  christos 	if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
    964  1.13  christos 		fatal("clock_gettime: %s", strerror(errno));
    965  1.13  christos 
    966  1.13  christos 	return (ts.tv_sec + (double)ts.tv_nsec / 1000000000);
    967  1.13  christos }
    968  1.13  christos 
    969   1.5  christos void
    970   1.5  christos bandwidth_limit_init(struct bwlimit *bw, u_int64_t kbps, size_t buflen)
    971   1.5  christos {
    972   1.5  christos 	bw->buflen = buflen;
    973   1.5  christos 	bw->rate = kbps;
    974   1.5  christos 	bw->thresh = bw->rate;
    975   1.5  christos 	bw->lamt = 0;
    976   1.5  christos 	timerclear(&bw->bwstart);
    977   1.5  christos 	timerclear(&bw->bwend);
    978   1.5  christos }
    979   1.5  christos 
    980   1.5  christos /* Callback from read/write loop to insert bandwidth-limiting delays */
    981   1.5  christos void
    982   1.5  christos bandwidth_limit(struct bwlimit *bw, size_t read_len)
    983   1.5  christos {
    984   1.5  christos 	u_int64_t waitlen;
    985   1.5  christos 	struct timespec ts, rm;
    986   1.5  christos 
    987   1.5  christos 	if (!timerisset(&bw->bwstart)) {
    988   1.5  christos 		gettimeofday(&bw->bwstart, NULL);
    989   1.5  christos 		return;
    990   1.5  christos 	}
    991   1.5  christos 
    992   1.5  christos 	bw->lamt += read_len;
    993   1.5  christos 	if (bw->lamt < bw->thresh)
    994   1.5  christos 		return;
    995   1.5  christos 
    996   1.5  christos 	gettimeofday(&bw->bwend, NULL);
    997   1.5  christos 	timersub(&bw->bwend, &bw->bwstart, &bw->bwend);
    998   1.5  christos 	if (!timerisset(&bw->bwend))
    999   1.5  christos 		return;
   1000   1.5  christos 
   1001   1.5  christos 	bw->lamt *= 8;
   1002   1.5  christos 	waitlen = (double)1000000L * bw->lamt / bw->rate;
   1003   1.5  christos 
   1004   1.5  christos 	bw->bwstart.tv_sec = waitlen / 1000000L;
   1005   1.5  christos 	bw->bwstart.tv_usec = waitlen % 1000000L;
   1006   1.5  christos 
   1007   1.5  christos 	if (timercmp(&bw->bwstart, &bw->bwend, >)) {
   1008   1.5  christos 		timersub(&bw->bwstart, &bw->bwend, &bw->bwend);
   1009   1.5  christos 
   1010   1.5  christos 		/* Adjust the wait time */
   1011   1.5  christos 		if (bw->bwend.tv_sec) {
   1012   1.5  christos 			bw->thresh /= 2;
   1013   1.5  christos 			if (bw->thresh < bw->buflen / 4)
   1014   1.5  christos 				bw->thresh = bw->buflen / 4;
   1015   1.5  christos 		} else if (bw->bwend.tv_usec < 10000) {
   1016   1.5  christos 			bw->thresh *= 2;
   1017   1.5  christos 			if (bw->thresh > bw->buflen * 8)
   1018   1.5  christos 				bw->thresh = bw->buflen * 8;
   1019   1.5  christos 		}
   1020   1.5  christos 
   1021   1.5  christos 		TIMEVAL_TO_TIMESPEC(&bw->bwend, &ts);
   1022   1.5  christos 		while (nanosleep(&ts, &rm) == -1) {
   1023   1.5  christos 			if (errno != EINTR)
   1024   1.5  christos 				break;
   1025   1.5  christos 			ts = rm;
   1026   1.5  christos 		}
   1027   1.5  christos 	}
   1028   1.5  christos 
   1029   1.5  christos 	bw->lamt = 0;
   1030   1.5  christos 	gettimeofday(&bw->bwstart, NULL);
   1031   1.5  christos }
   1032   1.5  christos 
   1033   1.5  christos /* Make a template filename for mk[sd]temp() */
   1034   1.5  christos void
   1035   1.5  christos mktemp_proto(char *s, size_t len)
   1036   1.5  christos {
   1037   1.5  christos 	const char *tmpdir;
   1038   1.5  christos 	int r;
   1039   1.5  christos 
   1040   1.5  christos 	if ((tmpdir = getenv("TMPDIR")) != NULL) {
   1041   1.5  christos 		r = snprintf(s, len, "%s/ssh-XXXXXXXXXXXX", tmpdir);
   1042   1.5  christos 		if (r > 0 && (size_t)r < len)
   1043   1.5  christos 			return;
   1044   1.5  christos 	}
   1045   1.5  christos 	r = snprintf(s, len, "/tmp/ssh-XXXXXXXXXXXX");
   1046   1.5  christos 	if (r < 0 || (size_t)r >= len)
   1047   1.5  christos 		fatal("%s: template string too short", __func__);
   1048   1.5  christos }
   1049   1.5  christos 
   1050   1.5  christos static const struct {
   1051   1.5  christos 	const char *name;
   1052   1.5  christos 	int value;
   1053   1.5  christos } ipqos[] = {
   1054   1.5  christos 	{ "af11", IPTOS_DSCP_AF11 },
   1055   1.5  christos 	{ "af12", IPTOS_DSCP_AF12 },
   1056   1.5  christos 	{ "af13", IPTOS_DSCP_AF13 },
   1057   1.7  christos 	{ "af21", IPTOS_DSCP_AF21 },
   1058   1.5  christos 	{ "af22", IPTOS_DSCP_AF22 },
   1059   1.5  christos 	{ "af23", IPTOS_DSCP_AF23 },
   1060   1.5  christos 	{ "af31", IPTOS_DSCP_AF31 },
   1061   1.5  christos 	{ "af32", IPTOS_DSCP_AF32 },
   1062   1.5  christos 	{ "af33", IPTOS_DSCP_AF33 },
   1063   1.5  christos 	{ "af41", IPTOS_DSCP_AF41 },
   1064   1.5  christos 	{ "af42", IPTOS_DSCP_AF42 },
   1065   1.5  christos 	{ "af43", IPTOS_DSCP_AF43 },
   1066   1.5  christos 	{ "cs0", IPTOS_DSCP_CS0 },
   1067   1.5  christos 	{ "cs1", IPTOS_DSCP_CS1 },
   1068   1.5  christos 	{ "cs2", IPTOS_DSCP_CS2 },
   1069   1.5  christos 	{ "cs3", IPTOS_DSCP_CS3 },
   1070   1.5  christos 	{ "cs4", IPTOS_DSCP_CS4 },
   1071   1.5  christos 	{ "cs5", IPTOS_DSCP_CS5 },
   1072   1.5  christos 	{ "cs6", IPTOS_DSCP_CS6 },
   1073   1.5  christos 	{ "cs7", IPTOS_DSCP_CS7 },
   1074   1.5  christos 	{ "ef", IPTOS_DSCP_EF },
   1075   1.5  christos 	{ "lowdelay", IPTOS_LOWDELAY },
   1076   1.5  christos 	{ "throughput", IPTOS_THROUGHPUT },
   1077   1.5  christos 	{ "reliability", IPTOS_RELIABILITY },
   1078   1.5  christos 	{ NULL, -1 }
   1079   1.5  christos };
   1080   1.5  christos 
   1081   1.5  christos int
   1082   1.5  christos parse_ipqos(const char *cp)
   1083   1.5  christos {
   1084   1.5  christos 	u_int i;
   1085   1.5  christos 	char *ep;
   1086   1.5  christos 	long val;
   1087   1.5  christos 
   1088   1.5  christos 	if (cp == NULL)
   1089   1.5  christos 		return -1;
   1090   1.5  christos 	for (i = 0; ipqos[i].name != NULL; i++) {
   1091   1.5  christos 		if (strcasecmp(cp, ipqos[i].name) == 0)
   1092   1.5  christos 			return ipqos[i].value;
   1093   1.5  christos 	}
   1094   1.5  christos 	/* Try parsing as an integer */
   1095   1.5  christos 	val = strtol(cp, &ep, 0);
   1096   1.5  christos 	if (*cp == '\0' || *ep != '\0' || val < 0 || val > 255)
   1097   1.5  christos 		return -1;
   1098   1.5  christos 	return val;
   1099   1.5  christos }
   1100   1.5  christos 
   1101   1.6  christos const char *
   1102   1.6  christos iptos2str(int iptos)
   1103   1.6  christos {
   1104   1.6  christos 	int i;
   1105   1.6  christos 	static char iptos_str[sizeof "0xff"];
   1106   1.6  christos 
   1107   1.6  christos 	for (i = 0; ipqos[i].name != NULL; i++) {
   1108   1.6  christos 		if (ipqos[i].value == iptos)
   1109   1.6  christos 			return ipqos[i].name;
   1110   1.6  christos 	}
   1111   1.6  christos 	snprintf(iptos_str, sizeof iptos_str, "0x%02x", iptos);
   1112   1.6  christos 	return iptos_str;
   1113   1.6  christos }
   1114   1.6  christos 
   1115   1.9  christos void
   1116   1.9  christos lowercase(char *s)
   1117   1.9  christos {
   1118   1.9  christos 	for (; *s; s++)
   1119   1.9  christos 		*s = tolower((u_char)*s);
   1120   1.9  christos }
   1121   1.9  christos 
   1122   1.4      adam int
   1123   1.9  christos unix_listener(const char *path, int backlog, int unlink_first)
   1124   1.4      adam {
   1125   1.9  christos 	struct sockaddr_un sunaddr;
   1126   1.9  christos 	int saved_errno, sock;
   1127   1.4      adam 
   1128   1.9  christos 	memset(&sunaddr, 0, sizeof(sunaddr));
   1129   1.9  christos 	sunaddr.sun_family = AF_UNIX;
   1130   1.9  christos 	if (strlcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path)) >= sizeof(sunaddr.sun_path)) {
   1131   1.9  christos 		error("%s: \"%s\" too long for Unix domain socket", __func__,
   1132   1.9  christos 		    path);
   1133   1.9  christos 		errno = ENAMETOOLONG;
   1134   1.9  christos 		return -1;
   1135   1.9  christos 	}
   1136   1.9  christos 
   1137   1.9  christos 	sock = socket(PF_UNIX, SOCK_STREAM, 0);
   1138   1.9  christos 	if (sock < 0) {
   1139   1.9  christos 		saved_errno = errno;
   1140   1.9  christos 		error("socket: %.100s", strerror(errno));
   1141   1.9  christos 		errno = saved_errno;
   1142   1.9  christos 		return -1;
   1143   1.9  christos 	}
   1144   1.9  christos 	if (unlink_first == 1) {
   1145   1.9  christos 		if (unlink(path) != 0 && errno != ENOENT)
   1146   1.9  christos 			error("unlink(%s): %.100s", path, strerror(errno));
   1147   1.9  christos 	}
   1148   1.9  christos 	if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0) {
   1149   1.9  christos 		saved_errno = errno;
   1150   1.9  christos 		error("bind: %.100s", strerror(errno));
   1151   1.9  christos 		close(sock);
   1152   1.9  christos 		error("%s: cannot bind to path: %s", __func__, path);
   1153   1.9  christos 		errno = saved_errno;
   1154   1.9  christos 		return -1;
   1155   1.9  christos 	}
   1156   1.9  christos 	if (listen(sock, backlog) < 0) {
   1157   1.9  christos 		saved_errno = errno;
   1158   1.9  christos 		error("listen: %.100s", strerror(errno));
   1159   1.9  christos 		close(sock);
   1160   1.9  christos 		unlink(path);
   1161   1.9  christos 		error("%s: cannot listen on path: %s", __func__, path);
   1162   1.9  christos 		errno = saved_errno;
   1163   1.9  christos 		return -1;
   1164   1.9  christos 	}
   1165   1.9  christos 	return sock;
   1166   1.4      adam }
   1167  1.13  christos 
   1168  1.13  christos /*
   1169  1.13  christos  * Compares two strings that maybe be NULL. Returns non-zero if strings
   1170  1.13  christos  * are both NULL or are identical, returns zero otherwise.
   1171  1.13  christos  */
   1172  1.13  christos static int
   1173  1.13  christos strcmp_maybe_null(const char *a, const char *b)
   1174  1.13  christos {
   1175  1.13  christos 	if ((a == NULL && b != NULL) || (a != NULL && b == NULL))
   1176  1.13  christos 		return 0;
   1177  1.13  christos 	if (a != NULL && strcmp(a, b) != 0)
   1178  1.13  christos 		return 0;
   1179  1.13  christos 	return 1;
   1180  1.13  christos }
   1181  1.13  christos 
   1182  1.13  christos /*
   1183  1.13  christos  * Compare two forwards, returning non-zero if they are identical or
   1184  1.13  christos  * zero otherwise.
   1185  1.13  christos  */
   1186  1.13  christos int
   1187  1.13  christos forward_equals(const struct Forward *a, const struct Forward *b)
   1188  1.13  christos {
   1189  1.13  christos 	if (strcmp_maybe_null(a->listen_host, b->listen_host) == 0)
   1190  1.13  christos 		return 0;
   1191  1.13  christos 	if (a->listen_port != b->listen_port)
   1192  1.13  christos 		return 0;
   1193  1.13  christos 	if (strcmp_maybe_null(a->listen_path, b->listen_path) == 0)
   1194  1.13  christos 		return 0;
   1195  1.13  christos 	if (strcmp_maybe_null(a->connect_host, b->connect_host) == 0)
   1196  1.13  christos 		return 0;
   1197  1.13  christos 	if (a->connect_port != b->connect_port)
   1198  1.13  christos 		return 0;
   1199  1.13  christos 	if (strcmp_maybe_null(a->connect_path, b->connect_path) == 0)
   1200  1.13  christos 		return 0;
   1201  1.13  christos 	/* allocated_port and handle are not checked */
   1202  1.13  christos 	return 1;
   1203  1.13  christos }
   1204  1.13  christos 
   1205