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