blob: e4c8c3238074ef8472a72a55c5277b260dc22ed4 [file] [log] [blame]
Damien Millere9fc72e2013-10-15 12:14:12 +11001/* $OpenBSD: misc.c,v 1.92 2013/10/14 23:28:23 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>
Damien Miller8dbffe72006-08-05 11:02:17 +100032#include <sys/param.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100033
Darren Tucker5d196262006-07-12 22:15:16 +100034#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100035#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100036#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100037#include <string.h>
Damien Miller1708cb72011-01-13 12:21:34 +110038#include <time.h>
Damien Millere6b3b612006-07-24 14:01:23 +100039#include <unistd.h>
Darren Tucker5d196262006-07-12 22:15:16 +100040
Damien Miller8ec8c3e2006-07-10 20:35:38 +100041#include <netinet/in.h>
Damien Miller0dac6fb2010-11-20 15:19:38 +110042#include <netinet/in_systm.h>
43#include <netinet/ip.h>
Damien Miller3a4051e2006-03-15 11:19:42 +110044#include <netinet/tcp.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100045
Damien Millere9fc72e2013-10-15 12:14:12 +110046#include <ctype.h>
Darren Tucker39972492006-07-12 22:22:46 +100047#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100048#include <fcntl.h>
Darren Tucker4abde772007-12-29 02:43:51 +110049#include <netdb.h>
Damien Miller03e20032006-03-15 11:16:59 +110050#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110051# include <paths.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100052#include <pwd.h>
Damien Miller03e20032006-03-15 11:16:59 +110053#endif
Damien Miller3beb8522006-01-02 23:40:10 +110054#ifdef SSH_TUN_OPENBSD
55#include <net/if.h>
56#endif
Damien Miller61c51502000-08-18 14:01:04 +100057
Damien Millerd7834352006-08-05 12:39:39 +100058#include "xmalloc.h"
Kevin Stevesb6e773a2001-02-04 13:20:36 +000059#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000060#include "log.h"
Darren Tuckerda345532006-07-10 23:04:19 +100061#include "ssh.h"
Damien Miller61c51502000-08-18 14:01:04 +100062
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000063/* remove newline at end of string */
Damien Miller61c51502000-08-18 14:01:04 +100064char *
65chop(char *s)
66{
67 char *t = s;
68 while (*t) {
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +000069 if (*t == '\n' || *t == '\r') {
Damien Miller61c51502000-08-18 14:01:04 +100070 *t = '\0';
71 return s;
72 }
73 t++;
74 }
75 return s;
76
77}
78
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000079/* set/unset filedescriptor to non-blocking */
Damien Miller232711f2004-06-15 10:35:30 +100080int
Damien Miller61c51502000-08-18 14:01:04 +100081set_nonblock(int fd)
82{
83 int val;
Ben Lindstromc93e84c2001-05-12 00:08:37 +000084
Damien Miller61c51502000-08-18 14:01:04 +100085 val = fcntl(fd, F_GETFL, 0);
86 if (val < 0) {
87 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +100088 return (-1);
Damien Miller61c51502000-08-18 14:01:04 +100089 }
Damien Miller69b69aa2000-10-28 14:19:58 +110090 if (val & O_NONBLOCK) {
Damien Miller232711f2004-06-15 10:35:30 +100091 debug3("fd %d is O_NONBLOCK", fd);
92 return (0);
Damien Miller69b69aa2000-10-28 14:19:58 +110093 }
Damien Milleref095ce2003-05-14 13:41:39 +100094 debug2("fd %d setting O_NONBLOCK", fd);
Damien Miller61c51502000-08-18 14:01:04 +100095 val |= O_NONBLOCK;
Damien Miller232711f2004-06-15 10:35:30 +100096 if (fcntl(fd, F_SETFL, val) == -1) {
97 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd,
98 strerror(errno));
99 return (-1);
100 }
101 return (0);
Damien Miller61c51502000-08-18 14:01:04 +1000102}
103
Damien Miller232711f2004-06-15 10:35:30 +1000104int
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000105unset_nonblock(int fd)
106{
107 int val;
108
109 val = fcntl(fd, F_GETFL, 0);
110 if (val < 0) {
111 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +1000112 return (-1);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000113 }
114 if (!(val & O_NONBLOCK)) {
Damien Miller232711f2004-06-15 10:35:30 +1000115 debug3("fd %d is not O_NONBLOCK", fd);
116 return (0);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000117 }
Ben Lindstrom352b1c22001-06-21 03:04:37 +0000118 debug("fd %d clearing O_NONBLOCK", fd);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000119 val &= ~O_NONBLOCK;
Damien Miller232711f2004-06-15 10:35:30 +1000120 if (fcntl(fd, F_SETFL, val) == -1) {
121 debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s",
Ben Lindstrom84fcb312002-03-05 01:48:09 +0000122 fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +1000123 return (-1);
124 }
125 return (0);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000126}
127
Darren Tucker4abde772007-12-29 02:43:51 +1100128const char *
129ssh_gai_strerror(int gaierr)
130{
Damien Miller7313fc92013-07-18 16:13:19 +1000131 if (gaierr == EAI_SYSTEM && errno != 0)
Darren Tucker912428c2008-01-01 20:33:35 +1100132 return strerror(errno);
133 return gai_strerror(gaierr);
Darren Tucker4abde772007-12-29 02:43:51 +1100134}
135
Damien Miller398e1cf2002-02-05 11:52:13 +1100136/* disable nagle on socket */
137void
138set_nodelay(int fd)
139{
Ben Lindstrome86de512002-03-05 01:28:14 +0000140 int opt;
141 socklen_t optlen;
Damien Miller398e1cf2002-02-05 11:52:13 +1100142
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000143 optlen = sizeof opt;
144 if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
Darren Tucker6db8f932003-11-03 20:07:14 +1100145 debug("getsockopt TCP_NODELAY: %.100s", strerror(errno));
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000146 return;
147 }
148 if (opt == 1) {
149 debug2("fd %d is TCP_NODELAY", fd);
150 return;
151 }
152 opt = 1;
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000153 debug2("fd %d setting TCP_NODELAY", fd);
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000154 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
Damien Miller398e1cf2002-02-05 11:52:13 +1100155 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
156}
157
Damien Miller61c51502000-08-18 14:01:04 +1000158/* Characters considered whitespace in strsep calls. */
159#define WHITESPACE " \t\r\n"
Damien Miller306d1182006-03-15 12:05:59 +1100160#define QUOTE "\""
Damien Miller61c51502000-08-18 14:01:04 +1000161
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000162/* return next token in configuration line */
Damien Miller61c51502000-08-18 14:01:04 +1000163char *
164strdelim(char **s)
165{
166 char *old;
167 int wspace = 0;
168
169 if (*s == NULL)
170 return NULL;
171
172 old = *s;
173
Damien Miller306d1182006-03-15 12:05:59 +1100174 *s = strpbrk(*s, WHITESPACE QUOTE "=");
Damien Miller61c51502000-08-18 14:01:04 +1000175 if (*s == NULL)
176 return (old);
177
Damien Miller306d1182006-03-15 12:05:59 +1100178 if (*s[0] == '\"') {
179 memmove(*s, *s + 1, strlen(*s)); /* move nul too */
180 /* Find matching quote */
181 if ((*s = strpbrk(*s, QUOTE)) == NULL) {
182 return (NULL); /* no matching quote */
183 } else {
184 *s[0] = '\0';
Damien Miller9308fc72010-07-16 13:56:01 +1000185 *s += strspn(*s + 1, WHITESPACE) + 1;
Damien Miller306d1182006-03-15 12:05:59 +1100186 return (old);
187 }
188 }
189
Damien Miller61c51502000-08-18 14:01:04 +1000190 /* Allow only one '=' to be skipped */
191 if (*s[0] == '=')
192 wspace = 1;
193 *s[0] = '\0';
194
Damien Miller306d1182006-03-15 12:05:59 +1100195 /* Skip any extra whitespace after first token */
Damien Miller61c51502000-08-18 14:01:04 +1000196 *s += strspn(*s + 1, WHITESPACE) + 1;
197 if (*s[0] == '=' && !wspace)
198 *s += strspn(*s + 1, WHITESPACE) + 1;
199
200 return (old);
201}
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000202
Ben Lindstrom086cf212001-03-05 05:56:40 +0000203struct passwd *
204pwcopy(struct passwd *pw)
205{
Damien Miller07d86be2006-03-26 14:19:21 +1100206 struct passwd *copy = xcalloc(1, sizeof(*copy));
Ben Lindstrom40304422001-03-05 06:22:01 +0000207
Ben Lindstrom086cf212001-03-05 05:56:40 +0000208 copy->pw_name = xstrdup(pw->pw_name);
209 copy->pw_passwd = xstrdup(pw->pw_passwd);
Damien Miller6332da22013-04-23 14:25:52 +1000210#ifdef HAVE_STRUCT_PASSWD_PW_GECOS
Ben Lindstrom40304422001-03-05 06:22:01 +0000211 copy->pw_gecos = xstrdup(pw->pw_gecos);
Damien Miller6332da22013-04-23 14:25:52 +1000212#endif
Ben Lindstrom086cf212001-03-05 05:56:40 +0000213 copy->pw_uid = pw->pw_uid;
214 copy->pw_gid = pw->pw_gid;
Damien Miller6332da22013-04-23 14:25:52 +1000215#ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000216 copy->pw_expire = pw->pw_expire;
Kevin Steves82456952001-06-22 21:14:18 +0000217#endif
Damien Miller6332da22013-04-23 14:25:52 +1000218#ifdef HAVE_STRUCT_PASSWD_PW_CHANGE
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000219 copy->pw_change = pw->pw_change;
Kevin Steves82456952001-06-22 21:14:18 +0000220#endif
Damien Miller6332da22013-04-23 14:25:52 +1000221#ifdef HAVE_STRUCT_PASSWD_PW_CLASS
Ben Lindstrom086cf212001-03-05 05:56:40 +0000222 copy->pw_class = xstrdup(pw->pw_class);
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000223#endif
Ben Lindstrom086cf212001-03-05 05:56:40 +0000224 copy->pw_dir = xstrdup(pw->pw_dir);
225 copy->pw_shell = xstrdup(pw->pw_shell);
226 return copy;
227}
228
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000229/*
230 * Convert ASCII string to TCP/IP port number.
Damien Miller3dc71ad2009-01-28 16:31:22 +1100231 * Port must be >=0 and <=65535.
232 * Return -1 if invalid.
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000233 */
234int
235a2port(const char *s)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000236{
Damien Miller3dc71ad2009-01-28 16:31:22 +1100237 long long port;
238 const char *errstr;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000239
Damien Miller3dc71ad2009-01-28 16:31:22 +1100240 port = strtonum(s, 0, 65535, &errstr);
241 if (errstr != NULL)
242 return -1;
243 return (int)port;
Ben Lindstrom19066a12001-04-12 23:39:26 +0000244}
245
Damien Millerd27b9472005-12-13 19:29:02 +1100246int
247a2tun(const char *s, int *remote)
248{
249 const char *errstr = NULL;
250 char *sp, *ep;
251 int tun;
252
253 if (remote != NULL) {
Damien Miller7b58e802005-12-13 19:33:19 +1100254 *remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +1100255 sp = xstrdup(s);
256 if ((ep = strchr(sp, ':')) == NULL) {
Darren Tuckera627d422013-06-02 07:31:17 +1000257 free(sp);
Damien Millerd27b9472005-12-13 19:29:02 +1100258 return (a2tun(s, NULL));
259 }
260 ep[0] = '\0'; ep++;
261 *remote = a2tun(ep, NULL);
262 tun = a2tun(sp, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +1000263 free(sp);
Damien Miller7b58e802005-12-13 19:33:19 +1100264 return (*remote == SSH_TUNID_ERR ? *remote : tun);
Damien Millerd27b9472005-12-13 19:29:02 +1100265 }
266
267 if (strcasecmp(s, "any") == 0)
Damien Miller7b58e802005-12-13 19:33:19 +1100268 return (SSH_TUNID_ANY);
Damien Millerd27b9472005-12-13 19:29:02 +1100269
Damien Miller7b58e802005-12-13 19:33:19 +1100270 tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr);
271 if (errstr != NULL)
272 return (SSH_TUNID_ERR);
Damien Millerd27b9472005-12-13 19:29:02 +1100273
274 return (tun);
275}
276
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000277#define SECONDS 1
278#define MINUTES (SECONDS * 60)
279#define HOURS (MINUTES * 60)
280#define DAYS (HOURS * 24)
281#define WEEKS (DAYS * 7)
282
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000283/*
284 * Convert a time string into seconds; format is
285 * a sequence of:
286 * time[qualifier]
287 *
288 * Valid time qualifiers are:
289 * <none> seconds
290 * s|S seconds
291 * m|M minutes
292 * h|H hours
293 * d|D days
294 * w|W weeks
295 *
296 * Examples:
297 * 90m 90 minutes
298 * 1h30m 90 minutes
299 * 2d 2 days
300 * 1w 1 week
301 *
302 * Return -1 if time string is invalid.
303 */
304long
305convtime(const char *s)
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000306{
307 long total, secs;
308 const char *p;
309 char *endp;
310
311 errno = 0;
312 total = 0;
313 p = s;
314
315 if (p == NULL || *p == '\0')
316 return -1;
317
318 while (*p) {
319 secs = strtol(p, &endp, 10);
320 if (p == endp ||
321 (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
322 secs < 0)
323 return -1;
324
325 switch (*endp++) {
326 case '\0':
327 endp--;
Damien Miller69b72032006-03-26 14:02:35 +1100328 break;
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000329 case 's':
330 case 'S':
331 break;
332 case 'm':
333 case 'M':
334 secs *= MINUTES;
335 break;
336 case 'h':
337 case 'H':
338 secs *= HOURS;
339 break;
340 case 'd':
341 case 'D':
342 secs *= DAYS;
343 break;
344 case 'w':
345 case 'W':
346 secs *= WEEKS;
347 break;
348 default:
349 return -1;
350 }
351 total += secs;
352 if (total < 0)
353 return -1;
354 p = endp;
355 }
356
357 return total;
358}
359
Damien Millerf91ee4c2005-03-01 21:24:33 +1100360/*
Darren Tuckerda345532006-07-10 23:04:19 +1000361 * Returns a standardized host+port identifier string.
362 * Caller must free returned string.
363 */
364char *
365put_host_port(const char *host, u_short port)
366{
367 char *hoststr;
368
369 if (port == 0 || port == SSH_DEFAULT_PORT)
370 return(xstrdup(host));
371 if (asprintf(&hoststr, "[%s]:%d", host, (int)port) < 0)
372 fatal("put_host_port: asprintf: %s", strerror(errno));
373 debug3("put_host_port: %s", hoststr);
374 return hoststr;
375}
376
377/*
Damien Millerf91ee4c2005-03-01 21:24:33 +1100378 * Search for next delimiter between hostnames/addresses and ports.
379 * Argument may be modified (for termination).
380 * Returns *cp if parsing succeeds.
381 * *cp is set to the start of the next delimiter, if one was found.
382 * If this is the last field, *cp is set to NULL.
383 */
384char *
385hpdelim(char **cp)
386{
387 char *s, *old;
388
389 if (cp == NULL || *cp == NULL)
390 return NULL;
391
392 old = s = *cp;
393 if (*s == '[') {
394 if ((s = strchr(s, ']')) == NULL)
395 return NULL;
396 else
397 s++;
398 } else if ((s = strpbrk(s, ":/")) == NULL)
399 s = *cp + strlen(*cp); /* skip to end (see first case below) */
400
401 switch (*s) {
402 case '\0':
403 *cp = NULL; /* no more fields*/
404 break;
Darren Tucker47eede72005-03-14 23:08:12 +1100405
Damien Millerf91ee4c2005-03-01 21:24:33 +1100406 case ':':
407 case '/':
408 *s = '\0'; /* terminate */
409 *cp = s + 1;
410 break;
Darren Tucker47eede72005-03-14 23:08:12 +1100411
Damien Millerf91ee4c2005-03-01 21:24:33 +1100412 default:
413 return NULL;
414 }
415
416 return old;
417}
418
Ben Lindstrom4529b702001-05-03 23:39:53 +0000419char *
420cleanhostname(char *host)
421{
422 if (*host == '[' && host[strlen(host) - 1] == ']') {
423 host[strlen(host) - 1] = '\0';
424 return (host + 1);
425 } else
426 return host;
427}
428
429char *
430colon(char *cp)
431{
432 int flag = 0;
433
434 if (*cp == ':') /* Leading colon is part of file name. */
Damien Miller2e774462010-06-26 09:30:47 +1000435 return NULL;
Ben Lindstrom4529b702001-05-03 23:39:53 +0000436 if (*cp == '[')
437 flag = 1;
438
439 for (; *cp; ++cp) {
440 if (*cp == '@' && *(cp+1) == '[')
441 flag = 1;
442 if (*cp == ']' && *(cp+1) == ':' && flag)
443 return (cp+1);
444 if (*cp == ':' && !flag)
445 return (cp);
446 if (*cp == '/')
Damien Miller2e774462010-06-26 09:30:47 +1000447 return NULL;
Ben Lindstrom4529b702001-05-03 23:39:53 +0000448 }
Damien Miller2e774462010-06-26 09:30:47 +1000449 return NULL;
Ben Lindstrom4529b702001-05-03 23:39:53 +0000450}
451
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000452/* function to assist building execv() arguments */
Ben Lindstrom387c4722001-05-08 20:27:25 +0000453void
454addargs(arglist *args, char *fmt, ...)
455{
456 va_list ap;
Damien Miller3eec6b72006-01-31 21:49:27 +1100457 char *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000458 u_int nalloc;
Damien Miller3eec6b72006-01-31 21:49:27 +1100459 int r;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000460
461 va_start(ap, fmt);
Damien Miller3eec6b72006-01-31 21:49:27 +1100462 r = vasprintf(&cp, fmt, ap);
Ben Lindstrom387c4722001-05-08 20:27:25 +0000463 va_end(ap);
Damien Miller3eec6b72006-01-31 21:49:27 +1100464 if (r == -1)
465 fatal("addargs: argument too long");
Ben Lindstrom387c4722001-05-08 20:27:25 +0000466
Darren Tuckerfb16b242003-09-22 21:04:23 +1000467 nalloc = args->nalloc;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000468 if (args->list == NULL) {
Darren Tuckerfb16b242003-09-22 21:04:23 +1000469 nalloc = 32;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000470 args->num = 0;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000471 } else if (args->num+2 >= nalloc)
472 nalloc *= 2;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000473
Damien Miller36812092006-03-26 14:22:47 +1100474 args->list = xrealloc(args->list, nalloc, sizeof(char *));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000475 args->nalloc = nalloc;
Damien Miller3eec6b72006-01-31 21:49:27 +1100476 args->list[args->num++] = cp;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000477 args->list[args->num] = NULL;
478}
Darren Tucker22cc7412004-12-06 22:47:41 +1100479
Damien Miller3eec6b72006-01-31 21:49:27 +1100480void
481replacearg(arglist *args, u_int which, char *fmt, ...)
482{
483 va_list ap;
484 char *cp;
485 int r;
486
487 va_start(ap, fmt);
488 r = vasprintf(&cp, fmt, ap);
489 va_end(ap);
490 if (r == -1)
491 fatal("replacearg: argument too long");
492
493 if (which >= args->num)
494 fatal("replacearg: tried to replace invalid arg %d >= %d",
495 which, args->num);
Darren Tuckera627d422013-06-02 07:31:17 +1000496 free(args->list[which]);
Damien Miller3eec6b72006-01-31 21:49:27 +1100497 args->list[which] = cp;
498}
499
500void
501freeargs(arglist *args)
502{
503 u_int i;
504
505 if (args->list != NULL) {
506 for (i = 0; i < args->num; i++)
Darren Tuckera627d422013-06-02 07:31:17 +1000507 free(args->list[i]);
508 free(args->list);
Damien Miller3eec6b72006-01-31 21:49:27 +1100509 args->nalloc = args->num = 0;
510 args->list = NULL;
511 }
512}
513
Darren Tucker22cc7412004-12-06 22:47:41 +1100514/*
Damien Miller5fd38c02005-05-26 12:02:14 +1000515 * Expands tildes in the file name. Returns data allocated by xmalloc.
516 * Warning: this calls getpw*.
517 */
518char *
519tilde_expand_filename(const char *filename, uid_t uid)
520{
Darren Tucker2ca51bf2013-05-16 20:22:46 +1000521 const char *path, *sep;
522 char user[128], *ret;
Damien Miller5fd38c02005-05-26 12:02:14 +1000523 struct passwd *pw;
Damien Millereccb9de2005-06-17 12:59:34 +1000524 u_int len, slash;
Damien Miller5fd38c02005-05-26 12:02:14 +1000525
526 if (*filename != '~')
527 return (xstrdup(filename));
528 filename++;
529
530 path = strchr(filename, '/');
531 if (path != NULL && path > filename) { /* ~user/path */
Damien Millereccb9de2005-06-17 12:59:34 +1000532 slash = path - filename;
533 if (slash > sizeof(user) - 1)
Damien Miller5fd38c02005-05-26 12:02:14 +1000534 fatal("tilde_expand_filename: ~username too long");
Damien Millereccb9de2005-06-17 12:59:34 +1000535 memcpy(user, filename, slash);
536 user[slash] = '\0';
Damien Miller5fd38c02005-05-26 12:02:14 +1000537 if ((pw = getpwnam(user)) == NULL)
538 fatal("tilde_expand_filename: No such user %s", user);
539 } else if ((pw = getpwuid(uid)) == NULL) /* ~/path */
Darren Tucker7517b5b2008-06-13 14:48:59 +1000540 fatal("tilde_expand_filename: No such uid %ld", (long)uid);
Damien Miller5fd38c02005-05-26 12:02:14 +1000541
Damien Miller5fd38c02005-05-26 12:02:14 +1000542 /* Make sure directory has a trailing '/' */
543 len = strlen(pw->pw_dir);
Darren Tucker026d9db2013-05-16 20:23:52 +1000544 if (len == 0 || pw->pw_dir[len - 1] != '/')
Darren Tucker2ca51bf2013-05-16 20:22:46 +1000545 sep = "/";
546 else
547 sep = "";
Damien Miller5fd38c02005-05-26 12:02:14 +1000548
549 /* Skip leading '/' from specified path */
550 if (path != NULL)
551 filename = path + 1;
Darren Tucker2ca51bf2013-05-16 20:22:46 +1000552
553 if (xasprintf(&ret, "%s%s%s", pw->pw_dir, sep, filename) >= MAXPATHLEN)
Damien Miller5fd38c02005-05-26 12:02:14 +1000554 fatal("tilde_expand_filename: Path too long");
555
Darren Tucker2ca51bf2013-05-16 20:22:46 +1000556 return (ret);
Damien Miller5fd38c02005-05-26 12:02:14 +1000557}
558
559/*
Damien Miller6476cad2005-06-16 13:18:34 +1000560 * Expand a string with a set of %[char] escapes. A number of escapes may be
561 * specified as (char *escape_chars, char *replacement) pairs. The list must
Darren Tuckerbee73d52005-07-14 17:05:02 +1000562 * be terminated by a NULL escape_char. Returns replaced string in memory
Damien Miller6476cad2005-06-16 13:18:34 +1000563 * allocated by xmalloc.
564 */
565char *
566percent_expand(const char *string, ...)
567{
568#define EXPAND_MAX_KEYS 16
Darren Tucker70d87692010-01-08 18:49:16 +1100569 u_int num_keys, i, j;
Damien Miller6476cad2005-06-16 13:18:34 +1000570 struct {
571 const char *key;
572 const char *repl;
573 } keys[EXPAND_MAX_KEYS];
Damien Miller6476cad2005-06-16 13:18:34 +1000574 char buf[4096];
575 va_list ap;
576
577 /* Gather keys */
578 va_start(ap, string);
579 for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) {
580 keys[num_keys].key = va_arg(ap, char *);
581 if (keys[num_keys].key == NULL)
582 break;
583 keys[num_keys].repl = va_arg(ap, char *);
584 if (keys[num_keys].repl == NULL)
Darren Tucker70d87692010-01-08 18:49:16 +1100585 fatal("%s: NULL replacement", __func__);
Damien Miller6476cad2005-06-16 13:18:34 +1000586 }
Darren Tucker70d87692010-01-08 18:49:16 +1100587 if (num_keys == EXPAND_MAX_KEYS && va_arg(ap, char *) != NULL)
588 fatal("%s: too many keys", __func__);
Damien Miller6476cad2005-06-16 13:18:34 +1000589 va_end(ap);
590
Damien Miller6476cad2005-06-16 13:18:34 +1000591 /* Expand string */
592 *buf = '\0';
593 for (i = 0; *string != '\0'; string++) {
594 if (*string != '%') {
595 append:
596 buf[i++] = *string;
597 if (i >= sizeof(buf))
Darren Tucker70d87692010-01-08 18:49:16 +1100598 fatal("%s: string too long", __func__);
Damien Miller6476cad2005-06-16 13:18:34 +1000599 buf[i] = '\0';
600 continue;
601 }
602 string++;
Darren Tucker70d87692010-01-08 18:49:16 +1100603 /* %% case */
Damien Miller6476cad2005-06-16 13:18:34 +1000604 if (*string == '%')
605 goto append;
606 for (j = 0; j < num_keys; j++) {
607 if (strchr(keys[j].key, *string) != NULL) {
608 i = strlcat(buf, keys[j].repl, sizeof(buf));
609 if (i >= sizeof(buf))
Darren Tucker70d87692010-01-08 18:49:16 +1100610 fatal("%s: string too long", __func__);
Damien Miller6476cad2005-06-16 13:18:34 +1000611 break;
612 }
613 }
614 if (j >= num_keys)
Darren Tucker70d87692010-01-08 18:49:16 +1100615 fatal("%s: unknown key %%%c", __func__, *string);
Damien Miller6476cad2005-06-16 13:18:34 +1000616 }
617 return (xstrdup(buf));
618#undef EXPAND_MAX_KEYS
619}
620
621/*
Darren Tucker22cc7412004-12-06 22:47:41 +1100622 * Read an entire line from a public key file into a static buffer, discarding
623 * lines that exceed the buffer size. Returns 0 on success, -1 on failure.
624 */
625int
626read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
Darren Tuckerf0f90982004-12-11 13:39:50 +1100627 u_long *lineno)
Darren Tucker22cc7412004-12-06 22:47:41 +1100628{
629 while (fgets(buf, bufsz, f) != NULL) {
Damien Miller3ca8b772007-01-05 16:24:47 +1100630 if (buf[0] == '\0')
631 continue;
Darren Tucker22cc7412004-12-06 22:47:41 +1100632 (*lineno)++;
633 if (buf[strlen(buf) - 1] == '\n' || feof(f)) {
634 return 0;
635 } else {
Darren Tuckerf0f90982004-12-11 13:39:50 +1100636 debug("%s: %s line %lu exceeds size limit", __func__,
637 filename, *lineno);
Darren Tucker22cc7412004-12-06 22:47:41 +1100638 /* discard remainder of line */
Darren Tucker47eede72005-03-14 23:08:12 +1100639 while (fgetc(f) != '\n' && !feof(f))
Darren Tucker22cc7412004-12-06 22:47:41 +1100640 ; /* nothing */
641 }
642 }
643 return -1;
644}
Damien Miller13390022005-07-06 09:44:19 +1000645
Damien Millerd27b9472005-12-13 19:29:02 +1100646int
Damien Miller7b58e802005-12-13 19:33:19 +1100647tun_open(int tun, int mode)
Damien Millerd27b9472005-12-13 19:29:02 +1100648{
Damien Miller62a31c92005-12-13 20:44:13 +1100649#if defined(CUSTOM_SYS_TUN_OPEN)
650 return (sys_tun_open(tun, mode));
Damien Miller2dcddbf2006-01-01 19:47:05 +1100651#elif defined(SSH_TUN_OPENBSD)
Damien Miller7b58e802005-12-13 19:33:19 +1100652 struct ifreq ifr;
Damien Millerd27b9472005-12-13 19:29:02 +1100653 char name[100];
Damien Miller7b58e802005-12-13 19:33:19 +1100654 int fd = -1, sock;
Damien Millerd27b9472005-12-13 19:29:02 +1100655
Damien Miller7b58e802005-12-13 19:33:19 +1100656 /* Open the tunnel device */
657 if (tun <= SSH_TUNID_MAX) {
Damien Millerd27b9472005-12-13 19:29:02 +1100658 snprintf(name, sizeof(name), "/dev/tun%d", tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100659 fd = open(name, O_RDWR);
660 } else if (tun == SSH_TUNID_ANY) {
661 for (tun = 100; tun >= 0; tun--) {
662 snprintf(name, sizeof(name), "/dev/tun%d", tun);
663 if ((fd = open(name, O_RDWR)) >= 0)
664 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100665 }
666 } else {
Damien Millera210d522006-01-02 23:40:30 +1100667 debug("%s: invalid tunnel %u", __func__, tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100668 return (-1);
Damien Millerd27b9472005-12-13 19:29:02 +1100669 }
Damien Miller7b58e802005-12-13 19:33:19 +1100670
671 if (fd < 0) {
672 debug("%s: %s open failed: %s", __func__, name, strerror(errno));
673 return (-1);
674 }
675
676 debug("%s: %s mode %d fd %d", __func__, name, mode, fd);
677
678 /* Set the tunnel device operation mode */
679 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tun%d", tun);
680 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
681 goto failed;
682
683 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)
684 goto failed;
Damien Millera1d9a182006-01-02 23:41:21 +1100685
686 /* Set interface mode */
687 ifr.ifr_flags &= ~IFF_UP;
688 if (mode == SSH_TUNMODE_ETHERNET)
Damien Miller7b58e802005-12-13 19:33:19 +1100689 ifr.ifr_flags |= IFF_LINK0;
Damien Millera1d9a182006-01-02 23:41:21 +1100690 else
691 ifr.ifr_flags &= ~IFF_LINK0;
692 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
693 goto failed;
694
695 /* Bring interface up */
Damien Miller7b58e802005-12-13 19:33:19 +1100696 ifr.ifr_flags |= IFF_UP;
697 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
698 goto failed;
699
700 close(sock);
701 return (fd);
702
703 failed:
704 if (fd >= 0)
705 close(fd);
706 if (sock >= 0)
707 close(sock);
708 debug("%s: failed to set %s mode %d: %s", __func__, name,
709 mode, strerror(errno));
Damien Millerd27b9472005-12-13 19:29:02 +1100710 return (-1);
Damien Miller62a31c92005-12-13 20:44:13 +1100711#else
712 error("Tunnel interfaces are not supported on this platform");
713 return (-1);
714#endif
Damien Millerd27b9472005-12-13 19:29:02 +1100715}
716
Darren Tuckerce321d82005-10-03 18:11:24 +1000717void
718sanitise_stdfd(void)
719{
Damien Miller72c5b7d2006-01-06 14:50:44 +1100720 int nullfd, dupfd;
Darren Tuckerce321d82005-10-03 18:11:24 +1000721
Damien Miller72c5b7d2006-01-06 14:50:44 +1100722 if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
Damien Miller9eab9562009-02-22 08:47:02 +1100723 fprintf(stderr, "Couldn't open /dev/null: %s\n",
724 strerror(errno));
Darren Tuckerce321d82005-10-03 18:11:24 +1000725 exit(1);
726 }
Damien Miller72c5b7d2006-01-06 14:50:44 +1100727 while (++dupfd <= 2) {
728 /* Only clobber closed fds */
729 if (fcntl(dupfd, F_GETFL, 0) >= 0)
730 continue;
731 if (dup2(nullfd, dupfd) == -1) {
Damien Miller9eab9562009-02-22 08:47:02 +1100732 fprintf(stderr, "dup2: %s\n", strerror(errno));
Darren Tuckerce321d82005-10-03 18:11:24 +1000733 exit(1);
734 }
Darren Tuckerce321d82005-10-03 18:11:24 +1000735 }
736 if (nullfd > 2)
737 close(nullfd);
738}
739
Damien Miller13390022005-07-06 09:44:19 +1000740char *
Damien Miller3f941882006-03-31 23:13:02 +1100741tohex(const void *vp, size_t l)
Damien Miller13390022005-07-06 09:44:19 +1000742{
Damien Miller3f941882006-03-31 23:13:02 +1100743 const u_char *p = (const u_char *)vp;
Damien Miller13390022005-07-06 09:44:19 +1000744 char b[3], *r;
Damien Miller3f941882006-03-31 23:13:02 +1100745 size_t i, hl;
746
747 if (l > 65536)
748 return xstrdup("tohex: length > 65536");
Damien Miller13390022005-07-06 09:44:19 +1000749
750 hl = l * 2 + 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100751 r = xcalloc(1, hl);
Damien Miller13390022005-07-06 09:44:19 +1000752 for (i = 0; i < l; i++) {
Damien Miller3f941882006-03-31 23:13:02 +1100753 snprintf(b, sizeof(b), "%02x", p[i]);
Damien Miller13390022005-07-06 09:44:19 +1000754 strlcat(r, b, hl);
755 }
756 return (r);
757}
758
Damien Miller3f941882006-03-31 23:13:02 +1100759u_int64_t
760get_u64(const void *vp)
761{
762 const u_char *p = (const u_char *)vp;
763 u_int64_t v;
764
765 v = (u_int64_t)p[0] << 56;
766 v |= (u_int64_t)p[1] << 48;
767 v |= (u_int64_t)p[2] << 40;
768 v |= (u_int64_t)p[3] << 32;
769 v |= (u_int64_t)p[4] << 24;
770 v |= (u_int64_t)p[5] << 16;
771 v |= (u_int64_t)p[6] << 8;
772 v |= (u_int64_t)p[7];
773
774 return (v);
775}
776
777u_int32_t
778get_u32(const void *vp)
779{
780 const u_char *p = (const u_char *)vp;
781 u_int32_t v;
782
783 v = (u_int32_t)p[0] << 24;
784 v |= (u_int32_t)p[1] << 16;
785 v |= (u_int32_t)p[2] << 8;
786 v |= (u_int32_t)p[3];
787
788 return (v);
789}
790
791u_int16_t
792get_u16(const void *vp)
793{
794 const u_char *p = (const u_char *)vp;
795 u_int16_t v;
796
797 v = (u_int16_t)p[0] << 8;
798 v |= (u_int16_t)p[1];
799
800 return (v);
801}
802
803void
804put_u64(void *vp, u_int64_t v)
805{
806 u_char *p = (u_char *)vp;
807
808 p[0] = (u_char)(v >> 56) & 0xff;
809 p[1] = (u_char)(v >> 48) & 0xff;
810 p[2] = (u_char)(v >> 40) & 0xff;
811 p[3] = (u_char)(v >> 32) & 0xff;
812 p[4] = (u_char)(v >> 24) & 0xff;
813 p[5] = (u_char)(v >> 16) & 0xff;
814 p[6] = (u_char)(v >> 8) & 0xff;
815 p[7] = (u_char)v & 0xff;
816}
817
818void
819put_u32(void *vp, u_int32_t v)
820{
821 u_char *p = (u_char *)vp;
822
823 p[0] = (u_char)(v >> 24) & 0xff;
824 p[1] = (u_char)(v >> 16) & 0xff;
825 p[2] = (u_char)(v >> 8) & 0xff;
826 p[3] = (u_char)v & 0xff;
827}
828
829
830void
831put_u16(void *vp, u_int16_t v)
832{
833 u_char *p = (u_char *)vp;
834
835 p[0] = (u_char)(v >> 8) & 0xff;
836 p[1] = (u_char)v & 0xff;
837}
Darren Tucker3fc464e2008-06-13 06:42:45 +1000838
839void
840ms_subtract_diff(struct timeval *start, int *ms)
841{
842 struct timeval diff, finish;
843
844 gettimeofday(&finish, NULL);
845 timersub(&finish, start, &diff);
846 *ms -= (diff.tv_sec * 1000) + (diff.tv_usec / 1000);
847}
848
849void
850ms_to_timeval(struct timeval *tv, int ms)
851{
852 if (ms < 0)
853 ms = 0;
854 tv->tv_sec = ms / 1000;
855 tv->tv_usec = (ms % 1000) * 1000;
856}
857
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000858time_t
859monotime(void)
860{
Darren Tuckera7108912013-06-02 08:18:31 +1000861#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000862 struct timespec ts;
Darren Tucker94396b72013-08-08 11:52:37 +1000863 static int gettime_failed = 0;
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000864
Darren Tucker94396b72013-08-08 11:52:37 +1000865 if (!gettime_failed) {
866 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
867 return (ts.tv_sec);
868 debug3("clock_gettime: %s", strerror(errno));
869 gettime_failed = 1;
870 }
Darren Tuckera7108912013-06-02 08:18:31 +1000871#endif
Darren Tucker94396b72013-08-08 11:52:37 +1000872
873 return time(NULL);
Darren Tuckerb759c9c2013-06-02 07:46:16 +1000874}
875
Damien Miller65e42f82010-09-24 22:15:11 +1000876void
877bandwidth_limit_init(struct bwlimit *bw, u_int64_t kbps, size_t buflen)
878{
879 bw->buflen = buflen;
880 bw->rate = kbps;
881 bw->thresh = bw->rate;
882 bw->lamt = 0;
883 timerclear(&bw->bwstart);
884 timerclear(&bw->bwend);
885}
886
887/* Callback from read/write loop to insert bandwidth-limiting delays */
888void
889bandwidth_limit(struct bwlimit *bw, size_t read_len)
890{
891 u_int64_t waitlen;
892 struct timespec ts, rm;
893
894 if (!timerisset(&bw->bwstart)) {
895 gettimeofday(&bw->bwstart, NULL);
896 return;
897 }
898
899 bw->lamt += read_len;
900 if (bw->lamt < bw->thresh)
901 return;
902
903 gettimeofday(&bw->bwend, NULL);
904 timersub(&bw->bwend, &bw->bwstart, &bw->bwend);
905 if (!timerisset(&bw->bwend))
906 return;
907
908 bw->lamt *= 8;
909 waitlen = (double)1000000L * bw->lamt / bw->rate;
910
911 bw->bwstart.tv_sec = waitlen / 1000000L;
912 bw->bwstart.tv_usec = waitlen % 1000000L;
913
914 if (timercmp(&bw->bwstart, &bw->bwend, >)) {
915 timersub(&bw->bwstart, &bw->bwend, &bw->bwend);
916
917 /* Adjust the wait time */
918 if (bw->bwend.tv_sec) {
919 bw->thresh /= 2;
920 if (bw->thresh < bw->buflen / 4)
921 bw->thresh = bw->buflen / 4;
922 } else if (bw->bwend.tv_usec < 10000) {
923 bw->thresh *= 2;
924 if (bw->thresh > bw->buflen * 8)
925 bw->thresh = bw->buflen * 8;
926 }
927
928 TIMEVAL_TO_TIMESPEC(&bw->bwend, &ts);
929 while (nanosleep(&ts, &rm) == -1) {
930 if (errno != EINTR)
931 break;
932 ts = rm;
933 }
934 }
935
936 bw->lamt = 0;
937 gettimeofday(&bw->bwstart, NULL);
938}
Damien Miller0dac6fb2010-11-20 15:19:38 +1100939
Damien Miller2cd62932010-12-01 11:50:35 +1100940/* Make a template filename for mk[sd]temp() */
941void
942mktemp_proto(char *s, size_t len)
943{
944 const char *tmpdir;
945 int r;
946
947 if ((tmpdir = getenv("TMPDIR")) != NULL) {
948 r = snprintf(s, len, "%s/ssh-XXXXXXXXXXXX", tmpdir);
949 if (r > 0 && (size_t)r < len)
950 return;
951 }
952 r = snprintf(s, len, "/tmp/ssh-XXXXXXXXXXXX");
953 if (r < 0 || (size_t)r >= len)
954 fatal("%s: template string too short", __func__);
955}
956
Damien Miller0dac6fb2010-11-20 15:19:38 +1100957static const struct {
958 const char *name;
959 int value;
960} ipqos[] = {
961 { "af11", IPTOS_DSCP_AF11 },
962 { "af12", IPTOS_DSCP_AF12 },
963 { "af13", IPTOS_DSCP_AF13 },
Damien Miller2918e032011-09-22 21:34:35 +1000964 { "af21", IPTOS_DSCP_AF21 },
Damien Miller0dac6fb2010-11-20 15:19:38 +1100965 { "af22", IPTOS_DSCP_AF22 },
966 { "af23", IPTOS_DSCP_AF23 },
967 { "af31", IPTOS_DSCP_AF31 },
968 { "af32", IPTOS_DSCP_AF32 },
969 { "af33", IPTOS_DSCP_AF33 },
970 { "af41", IPTOS_DSCP_AF41 },
971 { "af42", IPTOS_DSCP_AF42 },
972 { "af43", IPTOS_DSCP_AF43 },
973 { "cs0", IPTOS_DSCP_CS0 },
974 { "cs1", IPTOS_DSCP_CS1 },
975 { "cs2", IPTOS_DSCP_CS2 },
976 { "cs3", IPTOS_DSCP_CS3 },
977 { "cs4", IPTOS_DSCP_CS4 },
978 { "cs5", IPTOS_DSCP_CS5 },
979 { "cs6", IPTOS_DSCP_CS6 },
980 { "cs7", IPTOS_DSCP_CS7 },
981 { "ef", IPTOS_DSCP_EF },
982 { "lowdelay", IPTOS_LOWDELAY },
983 { "throughput", IPTOS_THROUGHPUT },
984 { "reliability", IPTOS_RELIABILITY },
985 { NULL, -1 }
986};
987
988int
989parse_ipqos(const char *cp)
990{
991 u_int i;
992 char *ep;
993 long val;
994
995 if (cp == NULL)
996 return -1;
997 for (i = 0; ipqos[i].name != NULL; i++) {
998 if (strcasecmp(cp, ipqos[i].name) == 0)
999 return ipqos[i].value;
1000 }
1001 /* Try parsing as an integer */
1002 val = strtol(cp, &ep, 0);
1003 if (*cp == '\0' || *ep != '\0' || val < 0 || val > 255)
1004 return -1;
1005 return val;
1006}
1007
Damien Miller91475862011-05-05 14:14:34 +10001008const char *
1009iptos2str(int iptos)
1010{
1011 int i;
1012 static char iptos_str[sizeof "0xff"];
1013
1014 for (i = 0; ipqos[i].name != NULL; i++) {
1015 if (ipqos[i].value == iptos)
1016 return ipqos[i].name;
1017 }
1018 snprintf(iptos_str, sizeof iptos_str, "0x%02x", iptos);
1019 return iptos_str;
1020}
Damien Millere9fc72e2013-10-15 12:14:12 +11001021
1022void
1023lowercase(char *s)
1024{
1025 for (; *s; s++)
1026 *s = tolower((u_char)*s);
1027}
Damien Miller04ee0f82009-11-18 17:48:30 +11001028void
1029sock_set_v6only(int s)
1030{
1031#ifdef IPV6_V6ONLY
1032 int on = 1;
1033
1034 debug3("%s: set socket %d IPV6_V6ONLY", __func__, s);
1035 if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) == -1)
1036 error("setsockopt IPV6_V6ONLY: %s", strerror(errno));
1037#endif
1038}