blob: 9d59ca6b2c88bd5f71df9521f04a8373b27589d7 [file] [log] [blame]
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +00001/* $OpenBSD: misc.c,v 1.104 2016/04/06 06:42:17 djm Exp $ */
Damien Millere4340be2000-09-16 13:29:08 +11002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Miller3f941882006-03-31 23:13:02 +11004 * Copyright (c) 2005,2006 Damien Miller. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +11005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
Damien Miller61c51502000-08-18 14:01:04 +100027#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110028
Damien Miller9f2abc42006-07-10 20:53:08 +100029#include <sys/types.h>
Damien Millerd7834352006-08-05 12:39:39 +100030#include <sys/ioctl.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100031#include <sys/socket.h>
dtucker@openbsd.org65a3c0d2016-01-20 09:22:39 +000032#include <sys/time.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100033#include <sys/un.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100034
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +000035#include <limits.h>
Darren Tucker5d196262006-07-12 22:15:16 +100036#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100037#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100038#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100039#include <string.h>
Damien Miller1708cb72011-01-13 12:21:34 +110040#include <time.h>
Damien Millere6b3b612006-07-24 14:01:23 +100041#include <unistd.h>
Darren Tucker5d196262006-07-12 22:15:16 +100042
Damien Miller8ec8c3e2006-07-10 20:35:38 +100043#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110044#include <netinet/in_systm.h>
45#include <netinet/ip.h>
Damien Miller3a4051e2006-03-15 11:19:42 +110046#include <netinet/tcp.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100047
Damien Millere9fc72e2013-10-15 12:14:12 +110048#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100049#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100050#include <fcntl.h>
Darren Tucker4abde772007-12-29 02:43:51 +110051#include <netdb.h>
Damien Miller03e20032006-03-15 11:16:59 +110052#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110053# include <paths.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100054#include <pwd.h>
Damien Miller03e20032006-03-15 11:16:59 +110055#endif
Damien Miller3beb8522006-01-02 23:40:10 +110056#ifdef SSH_TUN_OPENBSD
57#include <net/if.h>
58#endif
Damien Miller61c51502000-08-18 14:01:04 +100059
Damien Millerd7834352006-08-05 12:39:39 +100060#include "xmalloc.h"
Kevin Stevesb6e773a2001-02-04 13:20:36 +000061#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000062#include "log.h"
Darren Tuckerda345532006-07-10 23:04:19 +100063#include "ssh.h"
Damien Miller61c51502000-08-18 14:01:04 +100064
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000065/* remove newline at end of string */
Damien Miller61c51502000-08-18 14:01:04 +100066char *
67chop(char *s)
68{
69 char *t = s;
70 while (*t) {
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +000071 if (*t == '\n' || *t == '\r') {
Damien Miller61c51502000-08-18 14:01:04 +100072 *t = '\0';
73 return s;
74 }
75 t++;
76 }
77 return s;
78
79}
80
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000081/* set/unset filedescriptor to non-blocking */
Damien Miller232711f2004-06-15 10:35:30 +100082int
Damien Miller61c51502000-08-18 14:01:04 +100083set_nonblock(int fd)
84{
85 int val;
Ben Lindstromc93e84c2001-05-12 00:08:37 +000086
krw@openbsd.org574def02016-04-02 14:37:42 +000087 val = fcntl(fd, F_GETFL);
Damien Miller61c51502000-08-18 14:01:04 +100088 if (val < 0) {
krw@openbsd.org574def02016-04-02 14:37:42 +000089 error("fcntl(%d, F_GETFL): %s", fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +100090 return (-1);
Damien Miller61c51502000-08-18 14:01:04 +100091 }
Damien Miller69b69aa2000-10-28 14:19:58 +110092 if (val & O_NONBLOCK) {
Damien Miller232711f2004-06-15 10:35:30 +100093 debug3("fd %d is O_NONBLOCK", fd);
94 return (0);
Damien Miller69b69aa2000-10-28 14:19:58 +110095 }
Damien Milleref095ce2003-05-14 13:41:39 +100096 debug2("fd %d setting O_NONBLOCK", fd);
Damien Miller61c51502000-08-18 14:01:04 +100097 val |= O_NONBLOCK;
Damien Miller232711f2004-06-15 10:35:30 +100098 if (fcntl(fd, F_SETFL, val) == -1) {
99 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd,
100 strerror(errno));
101 return (-1);
102 }
103 return (0);
Damien Miller61c51502000-08-18 14:01:04 +1000104}
105
Damien Miller232711f2004-06-15 10:35:30 +1000106int
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000107unset_nonblock(int fd)
108{
109 int val;
110
krw@openbsd.org574def02016-04-02 14:37:42 +0000111 val = fcntl(fd, F_GETFL);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000112 if (val < 0) {
krw@openbsd.org574def02016-04-02 14:37:42 +0000113 error("fcntl(%d, F_GETFL): %s", fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +1000114 return (-1);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000115 }
116 if (!(val & O_NONBLOCK)) {
Damien Miller232711f2004-06-15 10:35:30 +1000117 debug3("fd %d is not O_NONBLOCK", fd);
118 return (0);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000119 }
Ben Lindstrom352b1c22001-06-21 03:04:37 +0000120 debug("fd %d clearing O_NONBLOCK", fd);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000121 val &= ~O_NONBLOCK;
Damien Miller232711f2004-06-15 10:35:30 +1000122 if (fcntl(fd, F_SETFL, val) == -1) {
123 debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s",
Ben Lindstrom84fcb312002-03-05 01:48:09 +0000124 fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +1000125 return (-1);
126 }
127 return (0);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000128}
129
Darren Tucker4abde772007-12-29 02:43:51 +1100130const char *
131ssh_gai_strerror(int gaierr)
132{
Damien Miller7313fc92013-07-18 16:13:19 +1000133 if (gaierr == EAI_SYSTEM && errno != 0)
Darren Tucker912428c2008-01-01 20:33:35 +1100134 return strerror(errno);
135 return gai_strerror(gaierr);
Darren Tucker4abde772007-12-29 02:43:51 +1100136}
137
Damien Miller398e1cf2002-02-05 11:52:13 +1100138/* disable nagle on socket */
139void
140set_nodelay(int fd)
141{
Ben Lindstrome86de512002-03-05 01:28:14 +0000142 int opt;
143 socklen_t optlen;
Damien Miller398e1cf2002-02-05 11:52:13 +1100144
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000145 optlen = sizeof opt;
146 if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
Darren Tucker6db8f932003-11-03 20:07:14 +1100147 debug("getsockopt TCP_NODELAY: %.100s", strerror(errno));
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000148 return;
149 }
150 if (opt == 1) {
151 debug2("fd %d is TCP_NODELAY", fd);
152 return;
153 }
154 opt = 1;
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000155 debug2("fd %d setting TCP_NODELAY", fd);
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000156 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
Damien Miller398e1cf2002-02-05 11:52:13 +1100157 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
158}
159
Damien Miller61c51502000-08-18 14:01:04 +1000160/* Characters considered whitespace in strsep calls. */
161#define WHITESPACE " \t\r\n"
Damien Miller306d1182006-03-15 12:05:59 +1100162#define QUOTE "\""
Damien Miller61c51502000-08-18 14:01:04 +1000163
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000164/* return next token in configuration line */
Damien Miller61c51502000-08-18 14:01:04 +1000165char *
166strdelim(char **s)
167{
168 char *old;
169 int wspace = 0;
170
171 if (*s == NULL)
172 return NULL;
173
174 old = *s;
175
Damien Miller306d1182006-03-15 12:05:59 +1100176 *s = strpbrk(*s, WHITESPACE QUOTE "=");
Damien Miller61c51502000-08-18 14:01:04 +1000177 if (*s == NULL)
178 return (old);
179
Damien Miller306d1182006-03-15 12:05:59 +1100180 if (*s[0] == '\"') {
181 memmove(*s, *s + 1, strlen(*s)); /* move nul too */
182 /* Find matching quote */
183 if ((*s = strpbrk(*s, QUOTE)) == NULL) {
184 return (NULL); /* no matching quote */
185 } else {
186 *s[0] = '\0';
Damien Miller9308fc72010-07-16 13:56:01 +1000187 *s += strspn(*s + 1, WHITESPACE) + 1;
Damien Miller306d1182006-03-15 12:05:59 +1100188 return (old);
189 }
190 }
191
Damien Miller61c51502000-08-18 14:01:04 +1000192 /* Allow only one '=' to be skipped */
193 if (*s[0] == '=')
194 wspace = 1;
195 *s[0] = '\0';
196
Damien Miller306d1182006-03-15 12:05:59 +1100197 /* Skip any extra whitespace after first token */
Damien Miller61c51502000-08-18 14:01:04 +1000198 *s += strspn(*s + 1, WHITESPACE) + 1;
199 if (*s[0] == '=' && !wspace)
200 *s += strspn(*s + 1, WHITESPACE) + 1;
201
202 return (old);
203}
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000204
Ben Lindstrom086cf212001-03-05 05:56:40 +0000205struct passwd *
206pwcopy(struct passwd *pw)
207{
Damien Miller07d86be2006-03-26 14:19:21 +1100208 struct passwd *copy = xcalloc(1, sizeof(*copy));
Ben Lindstrom40304422001-03-05 06:22:01 +0000209
Ben Lindstrom086cf212001-03-05 05:56:40 +0000210 copy->pw_name = xstrdup(pw->pw_name);
211 copy->pw_passwd = xstrdup(pw->pw_passwd);
Damien Miller6332da22013-04-23 14:25:52 +1000212#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom40304422001-03-05 06:22:01 +0000213 copy->pw_gecos = xstrdup(pw->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000214#endif
Ben Lindstrom086cf212001-03-05 05:56:40 +0000215 copy->pw_uid = pw->pw_uid;
216 copy->pw_gid = pw->pw_gid;
Damien Miller6332da22013-04-23 14:25:52 +1000217#ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000218 copy->pw_expire = pw->pw_expire;
Kevin Steves82456952001-06-22 21:14:18 +0000219#endif
Damien Miller6332da22013-04-23 14:25:52 +1000220#ifdef HAVE_STRUCT_PASSWD_PW_CHANGE
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000221 copy->pw_change = pw->pw_change;
Kevin Steves82456952001-06-22 21:14:18 +0000222#endif
Damien Miller6332da22013-04-23 14:25:52 +1000223#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom086cf212001-03-05 05:56:40 +0000224 copy->pw_class = xstrdup(pw->pw_class);
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000225#endif
Ben Lindstrom086cf212001-03-05 05:56:40 +0000226 copy->pw_dir = xstrdup(pw->pw_dir);
227 copy->pw_shell = xstrdup(pw->pw_shell);
228 return copy;
229}
230
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000231/*
232 * Convert ASCII string to TCP/IP port number.
Damien Miller3dc71ad2009-01-28 16:31:22 +1100233 * Port must be >=0 and <=65535.
234 * Return -1 if invalid.
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000235 */
236int
237a2port(const char *s)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000238{
Damien Miller3dc71ad2009-01-28 16:31:22 +1100239 long long port;
240 const char *errstr;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000241
Damien Miller3dc71ad2009-01-28 16:31:22 +1100242 port = strtonum(s, 0, 65535, &errstr);
243 if (errstr != NULL)
244 return -1;
245 return (int)port;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000246}
247
Damien Millerd27b9472005-12-13 19:29:02 +1100248int
249a2tun(const char *s, int *remote)
250{
251 const char *errstr = NULL;
252 char *sp, *ep;
253 int tun;
254
255 if (remote != NULL) {
Damien Miller7b58e802005-12-13 19:33:19 +1100256 *remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +1100257 sp = xstrdup(s);
258 if ((ep = strchr(sp, ':')) == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +1000259 free(sp);
Damien Millerd27b9472005-12-13 19:29:02 +1100260 return (a2tun(s, NULL));
261 }
262 ep[0] = '\0'; ep++;
263 *remote = a2tun(ep, NULL);
264 tun = a2tun(sp, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +1000265 free(sp);
Damien Miller7b58e802005-12-13 19:33:19 +1100266 return (*remote == SSH_TUNID_ERR ? *remote : tun);
Damien Millerd27b9472005-12-13 19:29:02 +1100267 }
268
269 if (strcasecmp(s, "any") == 0)
Damien Miller7b58e802005-12-13 19:33:19 +1100270 return (SSH_TUNID_ANY);
Damien Millerd27b9472005-12-13 19:29:02 +1100271
Damien Miller7b58e802005-12-13 19:33:19 +1100272 tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr);
273 if (errstr != NULL)
274 return (SSH_TUNID_ERR);
Damien Millerd27b9472005-12-13 19:29:02 +1100275
276 return (tun);
277}
278
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000279#define SECONDS 1
280#define MINUTES (SECONDS * 60)
281#define HOURS (MINUTES * 60)
282#define DAYS (HOURS * 24)
283#define WEEKS (DAYS * 7)
284
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000285/*
286 * Convert a time string into seconds; format is
287 * a sequence of:
288 * time[qualifier]
289 *
290 * Valid time qualifiers are:
291 * <none> seconds
292 * s|S seconds
293 * m|M minutes
294 * h|H hours
295 * d|D days
296 * w|W weeks
297 *
298 * Examples:
299 * 90m 90 minutes
300 * 1h30m 90 minutes
301 * 2d 2 days
302 * 1w 1 week
303 *
304 * Return -1 if time string is invalid.
305 */
306long
307convtime(const char *s)
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000308{
309 long total, secs;
310 const char *p;
311 char *endp;
312
313 errno = 0;
314 total = 0;
315 p = s;
316
317 if (p == NULL || *p == '\0')
318 return -1;
319
320 while (*p) {
321 secs = strtol(p, &endp, 10);
322 if (p == endp ||
323 (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
324 secs < 0)
325 return -1;
326
327 switch (*endp++) {
328 case '\0':
329 endp--;
Damien Miller69b72032006-03-26 14:02:35 +1100330 break;
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000331 case 's':
332 case 'S':
333 break;
334 case 'm':
335 case 'M':
336 secs *= MINUTES;
337 break;
338 case 'h':
339 case 'H':
340 secs *= HOURS;
341 break;
342 case 'd':
343 case 'D':
344 secs *= DAYS;
345 break;
346 case 'w':
347 case 'W':
348 secs *= WEEKS;
349 break;
350 default:
351 return -1;
352 }
353 total += secs;
354 if (total < 0)
355 return -1;
356 p = endp;
357 }
358
359 return total;
360}
361
Damien Millerf91ee4c2005-03-01 21:24:33 +1100362/*
Darren Tuckerda345532006-07-10 23:04:19 +1000363 * Returns a standardized host+port identifier string.
364 * Caller must free returned string.
365 */
366char *
367put_host_port(const char *host, u_short port)
368{
369 char *hoststr;
370
371 if (port == 0 || port == SSH_DEFAULT_PORT)
372 return(xstrdup(host));
373 if (asprintf(&hoststr, "[%s]:%d", host, (int)port) < 0)
374 fatal("put_host_port: asprintf: %s", strerror(errno));
375 debug3("put_host_port: %s", hoststr);
376 return hoststr;
377}
378
379/*
Damien Millerf91ee4c2005-03-01 21:24:33 +1100380 * Search for next delimiter between hostnames/addresses and ports.
381 * Argument may be modified (for termination).
382 * Returns *cp if parsing succeeds.
383 * *cp is set to the start of the next delimiter, if one was found.
384 * If this is the last field, *cp is set to NULL.
385 */
386char *
387hpdelim(char **cp)
388{
389 char *s, *old;
390
391 if (cp == NULL || *cp == NULL)
392 return NULL;
393
394 old = s = *cp;
395 if (*s == '[') {
396 if ((s = strchr(s, ']')) == NULL)
397 return NULL;
398 else
399 s++;
400 } else if ((s = strpbrk(s, ":/")) == NULL)
401 s = *cp + strlen(*cp); /* skip to end (see first case below) */
402
403 switch (*s) {
404 case '\0':
405 *cp = NULL; /* no more fields*/
406 break;
Darren Tucker47eede72005-03-14 23:08:12 +1100407
Damien Millerf91ee4c2005-03-01 21:24:33 +1100408 case ':':
409 case '/':
410 *s = '\0'; /* terminate */
411 *cp = s + 1;
412 break;
Darren Tucker47eede72005-03-14 23:08:12 +1100413
Damien Millerf91ee4c2005-03-01 21:24:33 +1100414 default:
415 return NULL;
416 }
417
418 return old;
419}
420
Ben Lindstrom4529b702001-05-03 23:39:53 +0000421char *
422cleanhostname(char *host)
423{
424 if (*host == '[' && host[strlen(host) - 1] == ']') {
425 host[strlen(host) - 1] = '\0';
426 return (host + 1);
427 } else
428 return host;
429}
430
431char *
432colon(char *cp)
433{
434 int flag = 0;
435
436 if (*cp == ':') /* Leading colon is part of file name. */
Damien Miller2e774462010-06-26 09:30:47 +1000437 return NULL;
Ben Lindstrom4529b702001-05-03 23:39:53 +0000438 if (*cp == '[')
439 flag = 1;
440
441 for (; *cp; ++cp) {
442 if (*cp == '@' && *(cp+1) == '[')
443 flag = 1;
444 if (*cp == ']' && *(cp+1) == ':' && flag)
445 return (cp+1);
446 if (*cp == ':' && !flag)
447 return (cp);
448 if (*cp == '/')
Damien Miller2e774462010-06-26 09:30:47 +1000449 return NULL;
Ben Lindstrom4529b702001-05-03 23:39:53 +0000450 }
Damien Miller2e774462010-06-26 09:30:47 +1000451 return NULL;
Ben Lindstrom4529b702001-05-03 23:39:53 +0000452}
453
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000454/* function to assist building execv() arguments */
Ben Lindstrom387c4722001-05-08 20:27:25 +0000455void
456addargs(arglist *args, char *fmt, ...)
457{
458 va_list ap;
Damien Miller3eec6b72006-01-31 21:49:27 +1100459 char *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000460 u_int nalloc;
Damien Miller3eec6b72006-01-31 21:49:27 +1100461 int r;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000462
463 va_start(ap, fmt);
Damien Miller3eec6b72006-01-31 21:49:27 +1100464 r = vasprintf(&cp, fmt, ap);
Ben Lindstrom387c4722001-05-08 20:27:25 +0000465 va_end(ap);
Damien Miller3eec6b72006-01-31 21:49:27 +1100466 if (r == -1)
467 fatal("addargs: argument too long");
Ben Lindstrom387c4722001-05-08 20:27:25 +0000468
Darren Tuckerfb16b242003-09-22 21:04:23 +1000469 nalloc = args->nalloc;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000470 if (args->list == NULL) {
Darren Tuckerfb16b242003-09-22 21:04:23 +1000471 nalloc = 32;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000472 args->num = 0;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000473 } else if (args->num+2 >= nalloc)
474 nalloc *= 2;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000475
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000476 args->list = xreallocarray(args->list, nalloc, sizeof(char *));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000477 args->nalloc = nalloc;
Damien Miller3eec6b72006-01-31 21:49:27 +1100478 args->list[args->num++] = cp;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000479 args->list[args->num] = NULL;
480}
Darren Tucker22cc7412004-12-06 22:47:41 +1100481
Damien Miller3eec6b72006-01-31 21:49:27 +1100482void
483replacearg(arglist *args, u_int which, char *fmt, ...)
484{
485 va_list ap;
486 char *cp;
487 int r;
488
489 va_start(ap, fmt);
490 r = vasprintf(&cp, fmt, ap);
491 va_end(ap);
492 if (r == -1)
493 fatal("replacearg: argument too long");
494
495 if (which >= args->num)
496 fatal("replacearg: tried to replace invalid arg %d >= %d",
497 which, args->num);
Darren Tuckera627d422013-06-02 07:31:17 +1000498 free(args->list[which]);
Damien Miller3eec6b72006-01-31 21:49:27 +1100499 args->list[which] = cp;
500}
501
502void
503freeargs(arglist *args)
504{
505 u_int i;
506
507 if (args->list != NULL) {
508 for (i = 0; i < args->num; i++)
Darren Tuckera627d422013-06-02 07:31:17 +1000509 free(args->list[i]);
510 free(args->list);
Damien Miller3eec6b72006-01-31 21:49:27 +1100511 args->nalloc = args->num = 0;
512 args->list = NULL;
513 }
514}
515
Darren Tucker22cc7412004-12-06 22:47:41 +1100516/*
Damien Miller5fd38c02005-05-26 12:02:14 +1000517 * Expands tildes in the file name. Returns data allocated by xmalloc.
518 * Warning: this calls getpw*.
519 */
520char *
521tilde_expand_filename(const char *filename, uid_t uid)
522{
Darren Tucker2ca51bf2013-05-16 20:22:46 +1000523 const char *path, *sep;
524 char user[128], *ret;
Damien Miller5fd38c02005-05-26 12:02:14 +1000525 struct passwd *pw;
Damien Millereccb9de2005-06-17 12:59:34 +1000526 u_int len, slash;
Damien Miller5fd38c02005-05-26 12:02:14 +1000527
528 if (*filename != '~')
529 return (xstrdup(filename));
530 filename++;
531
532 path = strchr(filename, '/');
533 if (path != NULL && path > filename) { /* ~user/path */
Damien Millereccb9de2005-06-17 12:59:34 +1000534 slash = path - filename;
535 if (slash > sizeof(user) - 1)
Damien Miller5fd38c02005-05-26 12:02:14 +1000536 fatal("tilde_expand_filename: ~username too long");
Damien Millereccb9de2005-06-17 12:59:34 +1000537 memcpy(user, filename, slash);
538 user[slash] = '\0';
Damien Miller5fd38c02005-05-26 12:02:14 +1000539 if ((pw = getpwnam(user)) == NULL)
540 fatal("tilde_expand_filename: No such user %s", user);
541 } else if ((pw = getpwuid(uid)) == NULL) /* ~/path */
Darren Tucker7517b5b2008-06-13 14:48:59 +1000542 fatal("tilde_expand_filename: No such uid %ld", (long)uid);
Damien Miller5fd38c02005-05-26 12:02:14 +1000543
Damien Miller5fd38c02005-05-26 12:02:14 +1000544 /* Make sure directory has a trailing '/' */
545 len = strlen(pw->pw_dir);
Darren Tucker026d9db2013-05-16 20:23:52 +1000546 if (len == 0 || pw->pw_dir[len - 1] != '/')
Darren Tucker2ca51bf2013-05-16 20:22:46 +1000547 sep = "/";
548 else
549 sep = "";
Damien Miller5fd38c02005-05-26 12:02:14 +1000550
551 /* Skip leading '/' from specified path */
552 if (path != NULL)
553 filename = path + 1;
Darren Tucker2ca51bf2013-05-16 20:22:46 +1000554
deraadt@openbsd.org2ae4f332015-01-16 06:40:12 +0000555 if (xasprintf(&ret, "%s%s%s", pw->pw_dir, sep, filename) >= PATH_MAX)
Damien Miller5fd38c02005-05-26 12:02:14 +1000556 fatal("tilde_expand_filename: Path too long");
557
Darren Tucker2ca51bf2013-05-16 20:22:46 +1000558 return (ret);
Damien Miller5fd38c02005-05-26 12:02:14 +1000559}
560
561/*
Damien Miller6476cad2005-06-16 13:18:34 +1000562 * Expand a string with a set of %[char] escapes. A number of escapes may be
563 * specified as (char *escape_chars, char *replacement) pairs. The list must
Darren Tuckerbee73d52005-07-14 17:05:02 +1000564 * be terminated by a NULL escape_char. Returns replaced string in memory
Damien Miller6476cad2005-06-16 13:18:34 +1000565 * allocated by xmalloc.
566 */
567char *
568percent_expand(const char *string, ...)
569{
570#define EXPAND_MAX_KEYS 16
Darren Tucker70d87692010-01-08 18:49:16 +1100571 u_int num_keys, i, j;
Damien Miller6476cad2005-06-16 13:18:34 +1000572 struct {
573 const char *key;
574 const char *repl;
575 } keys[EXPAND_MAX_KEYS];
Damien Miller6476cad2005-06-16 13:18:34 +1000576 char buf[4096];
577 va_list ap;
578
579 /* Gather keys */
580 va_start(ap, string);
581 for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) {
582 keys[num_keys].key = va_arg(ap, char *);
583 if (keys[num_keys].key == NULL)
584 break;
585 keys[num_keys].repl = va_arg(ap, char *);
586 if (keys[num_keys].repl == NULL)
Darren Tucker70d87692010-01-08 18:49:16 +1100587 fatal("%s: NULL replacement", __func__);
Damien Miller6476cad2005-06-16 13:18:34 +1000588 }
Darren Tucker70d87692010-01-08 18:49:16 +1100589 if (num_keys == EXPAND_MAX_KEYS && va_arg(ap, char *) != NULL)
590 fatal("%s: too many keys", __func__);
Damien Miller6476cad2005-06-16 13:18:34 +1000591 va_end(ap);
592
Damien Miller6476cad2005-06-16 13:18:34 +1000593 /* Expand string */
594 *buf = '\0';
595 for (i = 0; *string != '\0'; string++) {
596 if (*string != '%') {
597 append:
598 buf[i++] = *string;
599 if (i >= sizeof(buf))
Darren Tucker70d87692010-01-08 18:49:16 +1100600 fatal("%s: string too long", __func__);
Damien Miller6476cad2005-06-16 13:18:34 +1000601 buf[i] = '\0';
602 continue;
603 }
604 string++;
Darren Tucker70d87692010-01-08 18:49:16 +1100605 /* %% case */
Damien Miller6476cad2005-06-16 13:18:34 +1000606 if (*string == '%')
607 goto append;
tobias@openbsd.org8b56e592015-12-04 21:51:06 +0000608 if (*string == '\0')
609 fatal("%s: invalid format", __func__);
Damien Miller6476cad2005-06-16 13:18:34 +1000610 for (j = 0; j < num_keys; j++) {
611 if (strchr(keys[j].key, *string) != NULL) {
612 i = strlcat(buf, keys[j].repl, sizeof(buf));
613 if (i >= sizeof(buf))
Darren Tucker70d87692010-01-08 18:49:16 +1100614 fatal("%s: string too long", __func__);
Damien Miller6476cad2005-06-16 13:18:34 +1000615 break;
616 }
617 }
618 if (j >= num_keys)
Darren Tucker70d87692010-01-08 18:49:16 +1100619 fatal("%s: unknown key %%%c", __func__, *string);
Damien Miller6476cad2005-06-16 13:18:34 +1000620 }
621 return (xstrdup(buf));
622#undef EXPAND_MAX_KEYS
623}
624
625/*
Darren Tucker22cc7412004-12-06 22:47:41 +1100626 * Read an entire line from a public key file into a static buffer, discarding
627 * lines that exceed the buffer size. Returns 0 on success, -1 on failure.
628 */
629int
630read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
Darren Tuckerf0f90982004-12-11 13:39:50 +1100631 u_long *lineno)
Darren Tucker22cc7412004-12-06 22:47:41 +1100632{
633 while (fgets(buf, bufsz, f) != NULL) {
Damien Miller3ca8b772007-01-05 16:24:47 +1100634 if (buf[0] == '\0')
635 continue;
Darren Tucker22cc7412004-12-06 22:47:41 +1100636 (*lineno)++;
637 if (buf[strlen(buf) - 1] == '\n' || feof(f)) {
638 return 0;
639 } else {
Darren Tuckerf0f90982004-12-11 13:39:50 +1100640 debug("%s: %s line %lu exceeds size limit", __func__,
641 filename, *lineno);
Darren Tucker22cc7412004-12-06 22:47:41 +1100642 /* discard remainder of line */
Darren Tucker47eede72005-03-14 23:08:12 +1100643 while (fgetc(f) != '\n' && !feof(f))
Darren Tucker22cc7412004-12-06 22:47:41 +1100644 ; /* nothing */
645 }
646 }
647 return -1;
648}
Damien Miller13390022005-07-06 09:44:19 +1000649
Damien Millerd27b9472005-12-13 19:29:02 +1100650int
Damien Miller7b58e802005-12-13 19:33:19 +1100651tun_open(int tun, int mode)
Damien Millerd27b9472005-12-13 19:29:02 +1100652{
Damien Miller62a31c92005-12-13 20:44:13 +1100653#if defined(CUSTOM_SYS_TUN_OPEN)
654 return (sys_tun_open(tun, mode));
Damien Miller2dcddbf2006-01-01 19:47:05 +1100655#elif defined(SSH_TUN_OPENBSD)
Damien Miller7b58e802005-12-13 19:33:19 +1100656 struct ifreq ifr;
Damien Millerd27b9472005-12-13 19:29:02 +1100657 char name[100];
Damien Miller7b58e802005-12-13 19:33:19 +1100658 int fd = -1, sock;
sthen@openbsd.orga820a862015-10-24 08:34:09 +0000659 const char *tunbase = "tun";
660
661 if (mode == SSH_TUNMODE_ETHERNET)
662 tunbase = "tap";
Damien Millerd27b9472005-12-13 19:29:02 +1100663
Damien Miller7b58e802005-12-13 19:33:19 +1100664 /* Open the tunnel device */
665 if (tun <= SSH_TUNID_MAX) {
sthen@openbsd.orga820a862015-10-24 08:34:09 +0000666 snprintf(name, sizeof(name), "/dev/%s%d", tunbase, tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100667 fd = open(name, O_RDWR);
668 } else if (tun == SSH_TUNID_ANY) {
669 for (tun = 100; tun >= 0; tun--) {
sthen@openbsd.orga820a862015-10-24 08:34:09 +0000670 snprintf(name, sizeof(name), "/dev/%s%d",
671 tunbase, tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100672 if ((fd = open(name, O_RDWR)) >= 0)
673 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100674 }
675 } else {
Damien Millera210d522006-01-02 23:40:30 +1100676 debug("%s: invalid tunnel %u", __func__, tun);
djm@openbsd.org2905d6f2015-10-07 00:54:06 +0000677 return -1;
Damien Millerd27b9472005-12-13 19:29:02 +1100678 }
Damien Miller7b58e802005-12-13 19:33:19 +1100679
680 if (fd < 0) {
djm@openbsd.org2905d6f2015-10-07 00:54:06 +0000681 debug("%s: %s open: %s", __func__, name, strerror(errno));
682 return -1;
Damien Miller7b58e802005-12-13 19:33:19 +1100683 }
684
685 debug("%s: %s mode %d fd %d", __func__, name, mode, fd);
686
sthen@openbsd.orga820a862015-10-24 08:34:09 +0000687 /* Bring interface up if it is not already */
688 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", tunbase, tun);
689 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
Damien Miller7b58e802005-12-13 19:33:19 +1100690 goto failed;
691
djm@openbsd.org2905d6f2015-10-07 00:54:06 +0000692 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) {
693 debug("%s: get interface %s flags: %s", __func__,
694 ifr.ifr_name, strerror(errno));
Damien Miller7b58e802005-12-13 19:33:19 +1100695 goto failed;
djm@openbsd.org2905d6f2015-10-07 00:54:06 +0000696 }
Damien Millera1d9a182006-01-02 23:41:21 +1100697
djm@openbsd.org2905d6f2015-10-07 00:54:06 +0000698 if (!(ifr.ifr_flags & IFF_UP)) {
699 ifr.ifr_flags |= IFF_UP;
700 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) {
701 debug("%s: activate interface %s: %s", __func__,
702 ifr.ifr_name, strerror(errno));
703 goto failed;
704 }
705 }
Damien Miller7b58e802005-12-13 19:33:19 +1100706
707 close(sock);
djm@openbsd.org2905d6f2015-10-07 00:54:06 +0000708 return fd;
Damien Miller7b58e802005-12-13 19:33:19 +1100709
710 failed:
711 if (fd >= 0)
712 close(fd);
713 if (sock >= 0)
714 close(sock);
djm@openbsd.org2905d6f2015-10-07 00:54:06 +0000715 return -1;
Damien Miller62a31c92005-12-13 20:44:13 +1100716#else
717 error("Tunnel interfaces are not supported on this platform");
718 return (-1);
719#endif
Damien Millerd27b9472005-12-13 19:29:02 +1100720}
721
Darren Tuckerce321d82005-10-03 18:11:24 +1000722void
723sanitise_stdfd(void)
724{
Damien Miller72c5b7d2006-01-06 14:50:44 +1100725 int nullfd, dupfd;
Darren Tuckerce321d82005-10-03 18:11:24 +1000726
Damien Miller72c5b7d2006-01-06 14:50:44 +1100727 if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
Damien Miller9eab9562009-02-22 08:47:02 +1100728 fprintf(stderr, "Couldn't open /dev/null: %s\n",
729 strerror(errno));
Darren Tuckerce321d82005-10-03 18:11:24 +1000730 exit(1);
731 }
krw@openbsd.org574def02016-04-02 14:37:42 +0000732 while (++dupfd <= STDERR_FILENO) {
733 /* Only populate closed fds. */
734 if (fcntl(dupfd, F_GETFL) == -1 && errno == EBADF) {
735 if (dup2(nullfd, dupfd) == -1) {
736 fprintf(stderr, "dup2: %s\n", strerror(errno));
737 exit(1);
738 }
Darren Tuckerce321d82005-10-03 18:11:24 +1000739 }
Darren Tuckerce321d82005-10-03 18:11:24 +1000740 }
krw@openbsd.org574def02016-04-02 14:37:42 +0000741 if (nullfd > STDERR_FILENO)
Darren Tuckerce321d82005-10-03 18:11:24 +1000742 close(nullfd);
743}
744
Damien Miller13390022005-07-06 09:44:19 +1000745char *
Damien Miller3f941882006-03-31 23:13:02 +1100746tohex(const void *vp, size_t l)
Damien Miller13390022005-07-06 09:44:19 +1000747{
Damien Miller3f941882006-03-31 23:13:02 +1100748 const u_char *p = (const u_char *)vp;
Damien Miller13390022005-07-06 09:44:19 +1000749 char b[3], *r;
Damien Miller3f941882006-03-31 23:13:02 +1100750 size_t i, hl;
751
752 if (l > 65536)
753 return xstrdup("tohex: length > 65536");
Damien Miller13390022005-07-06 09:44:19 +1000754
755 hl = l * 2 + 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100756 r = xcalloc(1, hl);
Damien Miller13390022005-07-06 09:44:19 +1000757 for (i = 0; i < l; i++) {
Damien Miller3f941882006-03-31 23:13:02 +1100758 snprintf(b, sizeof(b), "%02x", p[i]);
Damien Miller13390022005-07-06 09:44:19 +1000759 strlcat(r, b, hl);
760 }
761 return (r);
762}
763
Damien Miller3f941882006-03-31 23:13:02 +1100764u_int64_t
765get_u64(const void *vp)
766{
767 const u_char *p = (const u_char *)vp;
768 u_int64_t v;
769
770 v = (u_int64_t)p[0] << 56;
771 v |= (u_int64_t)p[1] << 48;
772 v |= (u_int64_t)p[2] << 40;
773 v |= (u_int64_t)p[3] << 32;
774 v |= (u_int64_t)p[4] << 24;
775 v |= (u_int64_t)p[5] << 16;
776 v |= (u_int64_t)p[6] << 8;
777 v |= (u_int64_t)p[7];
778
779 return (v);
780}
781
782u_int32_t
783get_u32(const void *vp)
784{
785 const u_char *p = (const u_char *)vp;
786 u_int32_t v;
787
788 v = (u_int32_t)p[0] << 24;
789 v |= (u_int32_t)p[1] << 16;
790 v |= (u_int32_t)p[2] << 8;
791 v |= (u_int32_t)p[3];
792
793 return (v);
794}
795
Damien Miller88856692014-04-20 13:33:19 +1000796u_int32_t
797get_u32_le(const void *vp)
798{
799 const u_char *p = (const u_char *)vp;
800 u_int32_t v;
801
802 v = (u_int32_t)p[0];
803 v |= (u_int32_t)p[1] << 8;
804 v |= (u_int32_t)p[2] << 16;
805 v |= (u_int32_t)p[3] << 24;
806
807 return (v);
808}
809
Damien Miller3f941882006-03-31 23:13:02 +1100810u_int16_t
811get_u16(const void *vp)
812{
813 const u_char *p = (const u_char *)vp;
814 u_int16_t v;
815
816 v = (u_int16_t)p[0] << 8;
817 v |= (u_int16_t)p[1];
818
819 return (v);
820}
821
822void
823put_u64(void *vp, u_int64_t v)
824{
825 u_char *p = (u_char *)vp;
826
827 p[0] = (u_char)(v >> 56) & 0xff;
828 p[1] = (u_char)(v >> 48) & 0xff;
829 p[2] = (u_char)(v >> 40) & 0xff;
830 p[3] = (u_char)(v >> 32) & 0xff;
831 p[4] = (u_char)(v >> 24) & 0xff;
832 p[5] = (u_char)(v >> 16) & 0xff;
833 p[6] = (u_char)(v >> 8) & 0xff;
834 p[7] = (u_char)v & 0xff;
835}
836
837void
838put_u32(void *vp, u_int32_t v)
839{
840 u_char *p = (u_char *)vp;
841
842 p[0] = (u_char)(v >> 24) & 0xff;
843 p[1] = (u_char)(v >> 16) & 0xff;
844 p[2] = (u_char)(v >> 8) & 0xff;
845 p[3] = (u_char)v & 0xff;
846}
847
Damien Miller88856692014-04-20 13:33:19 +1000848void
849put_u32_le(void *vp, u_int32_t v)
850{
851 u_char *p = (u_char *)vp;
852
853 p[0] = (u_char)v & 0xff;
854 p[1] = (u_char)(v >> 8) & 0xff;
855 p[2] = (u_char)(v >> 16) & 0xff;
856 p[3] = (u_char)(v >> 24) & 0xff;
857}
Damien Miller3f941882006-03-31 23:13:02 +1100858
859void
860put_u16(void *vp, u_int16_t v)
861{
862 u_char *p = (u_char *)vp;
863
864 p[0] = (u_char)(v >> 8) & 0xff;
865 p[1] = (u_char)v & 0xff;
866}
Darren Tucker3fc464e2008-06-13 06:42:45 +1000867
868void
869ms_subtract_diff(struct timeval *start, int *ms)
870{
871 struct timeval diff, finish;
872
873 gettimeofday(&finish, NULL);
874 timersub(&finish, start, &diff);
875 *ms -= (diff.tv_sec * 1000) + (diff.tv_usec / 1000);
876}
877
878void
879ms_to_timeval(struct timeval *tv, int ms)
880{
881 if (ms < 0)
882 ms = 0;
883 tv->tv_sec = ms / 1000;
884 tv->tv_usec = (ms % 1000) * 1000;
885}
886
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000887time_t
888monotime(void)
889{
Damien Miller795b8632014-05-21 17:12:53 +1000890#if defined(HAVE_CLOCK_GETTIME) && \
891 (defined(CLOCK_MONOTONIC) || defined(CLOCK_BOOTTIME))
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000892 struct timespec ts;
Darren Tucker94396b72013-08-08 11:52:37 +1000893 static int gettime_failed = 0;
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000894
Darren Tucker94396b72013-08-08 11:52:37 +1000895 if (!gettime_failed) {
Damien Miller795b8632014-05-21 17:12:53 +1000896#if defined(CLOCK_BOOTTIME)
897 if (clock_gettime(CLOCK_BOOTTIME, &ts) == 0)
898 return (ts.tv_sec);
899#endif
900#if defined(CLOCK_MONOTONIC)
Darren Tucker94396b72013-08-08 11:52:37 +1000901 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
902 return (ts.tv_sec);
Damien Miller795b8632014-05-21 17:12:53 +1000903#endif
Darren Tucker94396b72013-08-08 11:52:37 +1000904 debug3("clock_gettime: %s", strerror(errno));
905 gettime_failed = 1;
906 }
Damien Miller795b8632014-05-21 17:12:53 +1000907#endif /* HAVE_CLOCK_GETTIME && (CLOCK_MONOTONIC || CLOCK_BOOTTIME */
Darren Tucker94396b72013-08-08 11:52:37 +1000908
909 return time(NULL);
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000910}
911
dtucker@openbsd.orgb8d4eaf2016-03-02 22:42:40 +0000912double
913monotime_double(void)
914{
Darren Tucker2c48bd32016-03-09 12:46:50 +1100915#if defined(HAVE_CLOCK_GETTIME) && \
916 (defined(CLOCK_MONOTONIC) || defined(CLOCK_BOOTTIME))
dtucker@openbsd.orgb8d4eaf2016-03-02 22:42:40 +0000917 struct timespec ts;
Darren Tucker2c48bd32016-03-09 12:46:50 +1100918 static int gettime_failed = 0;
dtucker@openbsd.orgb8d4eaf2016-03-02 22:42:40 +0000919
Darren Tucker2c48bd32016-03-09 12:46:50 +1100920 if (!gettime_failed) {
921#if defined(CLOCK_BOOTTIME)
922 if (clock_gettime(CLOCK_BOOTTIME, &ts) == 0)
923 return (ts.tv_sec + (double)ts.tv_nsec / 1000000000);
924#endif
925#if defined(CLOCK_MONOTONIC)
926 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
927 return (ts.tv_sec + (double)ts.tv_nsec / 1000000000);
928#endif
929 debug3("clock_gettime: %s", strerror(errno));
930 gettime_failed = 1;
931 }
932#endif /* HAVE_CLOCK_GETTIME && (CLOCK_MONOTONIC || CLOCK_BOOTTIME */
dtucker@openbsd.orgb8d4eaf2016-03-02 22:42:40 +0000933
Darren Tucker2c48bd32016-03-09 12:46:50 +1100934 return (double)time(NULL);
dtucker@openbsd.orgb8d4eaf2016-03-02 22:42:40 +0000935}
936
Damien Miller65e42f82010-09-24 22:15:11 +1000937void
938bandwidth_limit_init(struct bwlimit *bw, u_int64_t kbps, size_t buflen)
939{
940 bw->buflen = buflen;
941 bw->rate = kbps;
942 bw->thresh = bw->rate;
943 bw->lamt = 0;
944 timerclear(&bw->bwstart);
945 timerclear(&bw->bwend);
946}
947
948/* Callback from read/write loop to insert bandwidth-limiting delays */
949void
950bandwidth_limit(struct bwlimit *bw, size_t read_len)
951{
952 u_int64_t waitlen;
953 struct timespec ts, rm;
954
955 if (!timerisset(&bw->bwstart)) {
956 gettimeofday(&bw->bwstart, NULL);
957 return;
958 }
959
960 bw->lamt += read_len;
961 if (bw->lamt < bw->thresh)
962 return;
963
964 gettimeofday(&bw->bwend, NULL);
965 timersub(&bw->bwend, &bw->bwstart, &bw->bwend);
966 if (!timerisset(&bw->bwend))
967 return;
968
969 bw->lamt *= 8;
970 waitlen = (double)1000000L * bw->lamt / bw->rate;
971
972 bw->bwstart.tv_sec = waitlen / 1000000L;
973 bw->bwstart.tv_usec = waitlen % 1000000L;
974
975 if (timercmp(&bw->bwstart, &bw->bwend, >)) {
976 timersub(&bw->bwstart, &bw->bwend, &bw->bwend);
977
978 /* Adjust the wait time */
979 if (bw->bwend.tv_sec) {
980 bw->thresh /= 2;
981 if (bw->thresh < bw->buflen / 4)
982 bw->thresh = bw->buflen / 4;
983 } else if (bw->bwend.tv_usec < 10000) {
984 bw->thresh *= 2;
985 if (bw->thresh > bw->buflen * 8)
986 bw->thresh = bw->buflen * 8;
987 }
988
989 TIMEVAL_TO_TIMESPEC(&bw->bwend, &ts);
990 while (nanosleep(&ts, &rm) == -1) {
991 if (errno != EINTR)
992 break;
993 ts = rm;
994 }
995 }
996
997 bw->lamt = 0;
998 gettimeofday(&bw->bwstart, NULL);
999}
Damien Miller0dac6fb2010-11-20 15:19:38 +11001000
Damien Miller2cd62932010-12-01 11:50:35 +11001001/* Make a template filename for mk[sd]temp() */
1002void
1003mktemp_proto(char *s, size_t len)
1004{
1005 const char *tmpdir;
1006 int r;
1007
1008 if ((tmpdir = getenv("TMPDIR")) != NULL) {
1009 r = snprintf(s, len, "%s/ssh-XXXXXXXXXXXX", tmpdir);
1010 if (r > 0 && (size_t)r < len)
1011 return;
1012 }
1013 r = snprintf(s, len, "/tmp/ssh-XXXXXXXXXXXX");
1014 if (r < 0 || (size_t)r >= len)
1015 fatal("%s: template string too short", __func__);
1016}
1017
Damien Miller0dac6fb2010-11-20 15:19:38 +11001018static const struct {
1019 const char *name;
1020 int value;
1021} ipqos[] = {
1022 { "af11", IPTOS_DSCP_AF11 },
1023 { "af12", IPTOS_DSCP_AF12 },
1024 { "af13", IPTOS_DSCP_AF13 },
Damien Miller2918e032011-09-22 21:34:35 +10001025 { "af21", IPTOS_DSCP_AF21 },
Damien Miller0dac6fb2010-11-20 15:19:38 +11001026 { "af22", IPTOS_DSCP_AF22 },
1027 { "af23", IPTOS_DSCP_AF23 },
1028 { "af31", IPTOS_DSCP_AF31 },
1029 { "af32", IPTOS_DSCP_AF32 },
1030 { "af33", IPTOS_DSCP_AF33 },
1031 { "af41", IPTOS_DSCP_AF41 },
1032 { "af42", IPTOS_DSCP_AF42 },
1033 { "af43", IPTOS_DSCP_AF43 },
1034 { "cs0", IPTOS_DSCP_CS0 },
1035 { "cs1", IPTOS_DSCP_CS1 },
1036 { "cs2", IPTOS_DSCP_CS2 },
1037 { "cs3", IPTOS_DSCP_CS3 },
1038 { "cs4", IPTOS_DSCP_CS4 },
1039 { "cs5", IPTOS_DSCP_CS5 },
1040 { "cs6", IPTOS_DSCP_CS6 },
1041 { "cs7", IPTOS_DSCP_CS7 },
1042 { "ef", IPTOS_DSCP_EF },
1043 { "lowdelay", IPTOS_LOWDELAY },
1044 { "throughput", IPTOS_THROUGHPUT },
1045 { "reliability", IPTOS_RELIABILITY },
1046 { NULL, -1 }
1047};
1048
1049int
1050parse_ipqos(const char *cp)
1051{
1052 u_int i;
1053 char *ep;
1054 long val;
1055
1056 if (cp == NULL)
1057 return -1;
1058 for (i = 0; ipqos[i].name != NULL; i++) {
1059 if (strcasecmp(cp, ipqos[i].name) == 0)
1060 return ipqos[i].value;
1061 }
1062 /* Try parsing as an integer */
1063 val = strtol(cp, &ep, 0);
1064 if (*cp == '\0' || *ep != '\0' || val < 0 || val > 255)
1065 return -1;
1066 return val;
1067}
1068
Damien Miller91475862011-05-05 14:14:34 +10001069const char *
1070iptos2str(int iptos)
1071{
1072 int i;
1073 static char iptos_str[sizeof "0xff"];
1074
1075 for (i = 0; ipqos[i].name != NULL; i++) {
1076 if (ipqos[i].value == iptos)
1077 return ipqos[i].name;
1078 }
1079 snprintf(iptos_str, sizeof iptos_str, "0x%02x", iptos);
1080 return iptos_str;
1081}
Damien Millere9fc72e2013-10-15 12:14:12 +11001082
1083void
1084lowercase(char *s)
1085{
1086 for (; *s; s++)
1087 *s = tolower((u_char)*s);
1088}
Damien Miller7acefbb2014-07-18 14:11:24 +10001089
1090int
1091unix_listener(const char *path, int backlog, int unlink_first)
1092{
1093 struct sockaddr_un sunaddr;
1094 int saved_errno, sock;
1095
1096 memset(&sunaddr, 0, sizeof(sunaddr));
1097 sunaddr.sun_family = AF_UNIX;
1098 if (strlcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path)) >= sizeof(sunaddr.sun_path)) {
1099 error("%s: \"%s\" too long for Unix domain socket", __func__,
1100 path);
1101 errno = ENAMETOOLONG;
1102 return -1;
1103 }
1104
1105 sock = socket(PF_UNIX, SOCK_STREAM, 0);
1106 if (sock < 0) {
1107 saved_errno = errno;
1108 error("socket: %.100s", strerror(errno));
1109 errno = saved_errno;
1110 return -1;
1111 }
1112 if (unlink_first == 1) {
1113 if (unlink(path) != 0 && errno != ENOENT)
1114 error("unlink(%s): %.100s", path, strerror(errno));
1115 }
1116 if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0) {
1117 saved_errno = errno;
1118 error("bind: %.100s", strerror(errno));
1119 close(sock);
1120 error("%s: cannot bind to path: %s", __func__, path);
1121 errno = saved_errno;
1122 return -1;
1123 }
1124 if (listen(sock, backlog) < 0) {
1125 saved_errno = errno;
1126 error("listen: %.100s", strerror(errno));
1127 close(sock);
1128 unlink(path);
1129 error("%s: cannot listen on path: %s", __func__, path);
1130 errno = saved_errno;
1131 return -1;
1132 }
1133 return sock;
1134}
Damien Miller46878022014-08-30 03:29:19 +10001135
Damien Miller04ee0f82009-11-18 17:48:30 +11001136void
1137sock_set_v6only(int s)
1138{
Damien Millerd86a3ba2015-12-09 09:18:45 +11001139#if defined(IPV6_V6ONLY) && !defined(__OpenBSD__)
Damien Miller04ee0f82009-11-18 17:48:30 +11001140 int on = 1;
1141
1142 debug3("%s: set socket %d IPV6_V6ONLY", __func__, s);
1143 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) == -1)
1144 error("setsockopt IPV6_V6ONLY: %s", strerror(errno));
1145#endif
1146}
djm@openbsd.org0ccbd5e2016-04-06 06:42:17 +00001147
1148/*
1149 * Compares two strings that maybe be NULL. Returns non-zero if strings
1150 * are both NULL or are identical, returns zero otherwise.
1151 */
1152static int
1153strcmp_maybe_null(const char *a, const char *b)
1154{
1155 if ((a == NULL && b != NULL) || (a != NULL && b == NULL))
1156 return 0;
1157 if (a != NULL && strcmp(a, b) != 0)
1158 return 0;
1159 return 1;
1160}
1161
1162/*
1163 * Compare two forwards, returning non-zero if they are identical or
1164 * zero otherwise.
1165 */
1166int
1167forward_equals(const struct Forward *a, const struct Forward *b)
1168{
1169 if (strcmp_maybe_null(a->listen_host, b->listen_host) == 0)
1170 return 0;
1171 if (a->listen_port != b->listen_port)
1172 return 0;
1173 if (strcmp_maybe_null(a->listen_path, b->listen_path) == 0)
1174 return 0;
1175 if (strcmp_maybe_null(a->connect_host, b->connect_host) == 0)
1176 return 0;
1177 if (a->connect_port != b->connect_port)
1178 return 0;
1179 if (strcmp_maybe_null(a->connect_path, b->connect_path) == 0)
1180 return 0;
1181 /* allocated_port and handle are not checked */
1182 return 1;
1183}
1184