blob: b7316519d82a045953c0039208ef7bc8c690143c [file] [log] [blame]
Damien Miller9f2abc42006-07-10 20:53:08 +10001/* $OpenBSD: misc.c,v 1.54 2006/07/06 16:03:53 stevesk 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
29#include <sys/ioctl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100030#include <sys/types.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100031#include <sys/socket.h>
32
33#include <netinet/in.h>
Damien Miller3a4051e2006-03-15 11:19:42 +110034#include <netinet/tcp.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100035
Damien Miller03e20032006-03-15 11:16:59 +110036#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110037# include <paths.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100038#include <pwd.h>
Damien Miller03e20032006-03-15 11:16:59 +110039#endif
Damien Miller3beb8522006-01-02 23:40:10 +110040#ifdef SSH_TUN_OPENBSD
41#include <net/if.h>
42#endif
Damien Miller61c51502000-08-18 14:01:04 +100043
Kevin Stevesb6e773a2001-02-04 13:20:36 +000044#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "log.h"
Ben Lindstrom06909012001-03-05 06:09:31 +000046#include "xmalloc.h"
Damien Miller61c51502000-08-18 14:01:04 +100047
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000048/* remove newline at end of string */
Damien Miller61c51502000-08-18 14:01:04 +100049char *
50chop(char *s)
51{
52 char *t = s;
53 while (*t) {
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +000054 if (*t == '\n' || *t == '\r') {
Damien Miller61c51502000-08-18 14:01:04 +100055 *t = '\0';
56 return s;
57 }
58 t++;
59 }
60 return s;
61
62}
63
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000064/* set/unset filedescriptor to non-blocking */
Damien Miller232711f2004-06-15 10:35:30 +100065int
Damien Miller61c51502000-08-18 14:01:04 +100066set_nonblock(int fd)
67{
68 int val;
Ben Lindstromc93e84c2001-05-12 00:08:37 +000069
Damien Miller61c51502000-08-18 14:01:04 +100070 val = fcntl(fd, F_GETFL, 0);
71 if (val < 0) {
72 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +100073 return (-1);
Damien Miller61c51502000-08-18 14:01:04 +100074 }
Damien Miller69b69aa2000-10-28 14:19:58 +110075 if (val & O_NONBLOCK) {
Damien Miller232711f2004-06-15 10:35:30 +100076 debug3("fd %d is O_NONBLOCK", fd);
77 return (0);
Damien Miller69b69aa2000-10-28 14:19:58 +110078 }
Damien Milleref095ce2003-05-14 13:41:39 +100079 debug2("fd %d setting O_NONBLOCK", fd);
Damien Miller61c51502000-08-18 14:01:04 +100080 val |= O_NONBLOCK;
Damien Miller232711f2004-06-15 10:35:30 +100081 if (fcntl(fd, F_SETFL, val) == -1) {
82 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd,
83 strerror(errno));
84 return (-1);
85 }
86 return (0);
Damien Miller61c51502000-08-18 14:01:04 +100087}
88
Damien Miller232711f2004-06-15 10:35:30 +100089int
Ben Lindstromc93e84c2001-05-12 00:08:37 +000090unset_nonblock(int fd)
91{
92 int val;
93
94 val = fcntl(fd, F_GETFL, 0);
95 if (val < 0) {
96 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +100097 return (-1);
Ben Lindstromc93e84c2001-05-12 00:08:37 +000098 }
99 if (!(val & O_NONBLOCK)) {
Damien Miller232711f2004-06-15 10:35:30 +1000100 debug3("fd %d is not O_NONBLOCK", fd);
101 return (0);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000102 }
Ben Lindstrom352b1c22001-06-21 03:04:37 +0000103 debug("fd %d clearing O_NONBLOCK", fd);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000104 val &= ~O_NONBLOCK;
Damien Miller232711f2004-06-15 10:35:30 +1000105 if (fcntl(fd, F_SETFL, val) == -1) {
106 debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s",
Ben Lindstrom84fcb312002-03-05 01:48:09 +0000107 fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +1000108 return (-1);
109 }
110 return (0);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000111}
112
Damien Miller398e1cf2002-02-05 11:52:13 +1100113/* disable nagle on socket */
114void
115set_nodelay(int fd)
116{
Ben Lindstrome86de512002-03-05 01:28:14 +0000117 int opt;
118 socklen_t optlen;
Damien Miller398e1cf2002-02-05 11:52:13 +1100119
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000120 optlen = sizeof opt;
121 if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
Darren Tucker6db8f932003-11-03 20:07:14 +1100122 debug("getsockopt TCP_NODELAY: %.100s", strerror(errno));
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000123 return;
124 }
125 if (opt == 1) {
126 debug2("fd %d is TCP_NODELAY", fd);
127 return;
128 }
129 opt = 1;
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000130 debug2("fd %d setting TCP_NODELAY", fd);
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000131 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
Damien Miller398e1cf2002-02-05 11:52:13 +1100132 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
133}
134
Damien Miller61c51502000-08-18 14:01:04 +1000135/* Characters considered whitespace in strsep calls. */
136#define WHITESPACE " \t\r\n"
Damien Miller306d1182006-03-15 12:05:59 +1100137#define QUOTE "\""
Damien Miller61c51502000-08-18 14:01:04 +1000138
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000139/* return next token in configuration line */
Damien Miller61c51502000-08-18 14:01:04 +1000140char *
141strdelim(char **s)
142{
143 char *old;
144 int wspace = 0;
145
146 if (*s == NULL)
147 return NULL;
148
149 old = *s;
150
Damien Miller306d1182006-03-15 12:05:59 +1100151 *s = strpbrk(*s, WHITESPACE QUOTE "=");
Damien Miller61c51502000-08-18 14:01:04 +1000152 if (*s == NULL)
153 return (old);
154
Damien Miller306d1182006-03-15 12:05:59 +1100155 if (*s[0] == '\"') {
156 memmove(*s, *s + 1, strlen(*s)); /* move nul too */
157 /* Find matching quote */
158 if ((*s = strpbrk(*s, QUOTE)) == NULL) {
159 return (NULL); /* no matching quote */
160 } else {
161 *s[0] = '\0';
162 return (old);
163 }
164 }
165
Damien Miller61c51502000-08-18 14:01:04 +1000166 /* Allow only one '=' to be skipped */
167 if (*s[0] == '=')
168 wspace = 1;
169 *s[0] = '\0';
170
Damien Miller306d1182006-03-15 12:05:59 +1100171 /* Skip any extra whitespace after first token */
Damien Miller61c51502000-08-18 14:01:04 +1000172 *s += strspn(*s + 1, WHITESPACE) + 1;
173 if (*s[0] == '=' && !wspace)
174 *s += strspn(*s + 1, WHITESPACE) + 1;
175
176 return (old);
177}
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000178
Ben Lindstrom086cf212001-03-05 05:56:40 +0000179struct passwd *
180pwcopy(struct passwd *pw)
181{
Damien Miller07d86be2006-03-26 14:19:21 +1100182 struct passwd *copy = xcalloc(1, sizeof(*copy));
Ben Lindstrom40304422001-03-05 06:22:01 +0000183
Ben Lindstrom086cf212001-03-05 05:56:40 +0000184 copy->pw_name = xstrdup(pw->pw_name);
185 copy->pw_passwd = xstrdup(pw->pw_passwd);
Ben Lindstrom40304422001-03-05 06:22:01 +0000186 copy->pw_gecos = xstrdup(pw->pw_gecos);
Ben Lindstrom086cf212001-03-05 05:56:40 +0000187 copy->pw_uid = pw->pw_uid;
188 copy->pw_gid = pw->pw_gid;
Kevin Steves82456952001-06-22 21:14:18 +0000189#ifdef HAVE_PW_EXPIRE_IN_PASSWD
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000190 copy->pw_expire = pw->pw_expire;
Kevin Steves82456952001-06-22 21:14:18 +0000191#endif
192#ifdef HAVE_PW_CHANGE_IN_PASSWD
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000193 copy->pw_change = pw->pw_change;
Kevin Steves82456952001-06-22 21:14:18 +0000194#endif
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000195#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom086cf212001-03-05 05:56:40 +0000196 copy->pw_class = xstrdup(pw->pw_class);
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000197#endif
Ben Lindstrom086cf212001-03-05 05:56:40 +0000198 copy->pw_dir = xstrdup(pw->pw_dir);
199 copy->pw_shell = xstrdup(pw->pw_shell);
200 return copy;
201}
202
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000203/*
204 * Convert ASCII string to TCP/IP port number.
205 * Port must be >0 and <=65535.
206 * Return 0 if invalid.
207 */
208int
209a2port(const char *s)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000210{
211 long port;
212 char *endp;
213
214 errno = 0;
215 port = strtol(s, &endp, 0);
216 if (s == endp || *endp != '\0' ||
217 (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
218 port <= 0 || port > 65535)
219 return 0;
220
221 return port;
222}
223
Damien Millerd27b9472005-12-13 19:29:02 +1100224int
225a2tun(const char *s, int *remote)
226{
227 const char *errstr = NULL;
228 char *sp, *ep;
229 int tun;
230
231 if (remote != NULL) {
Damien Miller7b58e802005-12-13 19:33:19 +1100232 *remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +1100233 sp = xstrdup(s);
234 if ((ep = strchr(sp, ':')) == NULL) {
235 xfree(sp);
236 return (a2tun(s, NULL));
237 }
238 ep[0] = '\0'; ep++;
239 *remote = a2tun(ep, NULL);
240 tun = a2tun(sp, NULL);
241 xfree(sp);
Damien Miller7b58e802005-12-13 19:33:19 +1100242 return (*remote == SSH_TUNID_ERR ? *remote : tun);
Damien Millerd27b9472005-12-13 19:29:02 +1100243 }
244
245 if (strcasecmp(s, "any") == 0)
Damien Miller7b58e802005-12-13 19:33:19 +1100246 return (SSH_TUNID_ANY);
Damien Millerd27b9472005-12-13 19:29:02 +1100247
Damien Miller7b58e802005-12-13 19:33:19 +1100248 tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr);
249 if (errstr != NULL)
250 return (SSH_TUNID_ERR);
Damien Millerd27b9472005-12-13 19:29:02 +1100251
252 return (tun);
253}
254
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000255#define SECONDS 1
256#define MINUTES (SECONDS * 60)
257#define HOURS (MINUTES * 60)
258#define DAYS (HOURS * 24)
259#define WEEKS (DAYS * 7)
260
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000261/*
262 * Convert a time string into seconds; format is
263 * a sequence of:
264 * time[qualifier]
265 *
266 * Valid time qualifiers are:
267 * <none> seconds
268 * s|S seconds
269 * m|M minutes
270 * h|H hours
271 * d|D days
272 * w|W weeks
273 *
274 * Examples:
275 * 90m 90 minutes
276 * 1h30m 90 minutes
277 * 2d 2 days
278 * 1w 1 week
279 *
280 * Return -1 if time string is invalid.
281 */
282long
283convtime(const char *s)
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000284{
285 long total, secs;
286 const char *p;
287 char *endp;
288
289 errno = 0;
290 total = 0;
291 p = s;
292
293 if (p == NULL || *p == '\0')
294 return -1;
295
296 while (*p) {
297 secs = strtol(p, &endp, 10);
298 if (p == endp ||
299 (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
300 secs < 0)
301 return -1;
302
303 switch (*endp++) {
304 case '\0':
305 endp--;
Damien Miller69b72032006-03-26 14:02:35 +1100306 break;
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000307 case 's':
308 case 'S':
309 break;
310 case 'm':
311 case 'M':
312 secs *= MINUTES;
313 break;
314 case 'h':
315 case 'H':
316 secs *= HOURS;
317 break;
318 case 'd':
319 case 'D':
320 secs *= DAYS;
321 break;
322 case 'w':
323 case 'W':
324 secs *= WEEKS;
325 break;
326 default:
327 return -1;
328 }
329 total += secs;
330 if (total < 0)
331 return -1;
332 p = endp;
333 }
334
335 return total;
336}
337
Damien Millerf91ee4c2005-03-01 21:24:33 +1100338/*
339 * Search for next delimiter between hostnames/addresses and ports.
340 * Argument may be modified (for termination).
341 * Returns *cp if parsing succeeds.
342 * *cp is set to the start of the next delimiter, if one was found.
343 * If this is the last field, *cp is set to NULL.
344 */
345char *
346hpdelim(char **cp)
347{
348 char *s, *old;
349
350 if (cp == NULL || *cp == NULL)
351 return NULL;
352
353 old = s = *cp;
354 if (*s == '[') {
355 if ((s = strchr(s, ']')) == NULL)
356 return NULL;
357 else
358 s++;
359 } else if ((s = strpbrk(s, ":/")) == NULL)
360 s = *cp + strlen(*cp); /* skip to end (see first case below) */
361
362 switch (*s) {
363 case '\0':
364 *cp = NULL; /* no more fields*/
365 break;
Darren Tucker47eede72005-03-14 23:08:12 +1100366
Damien Millerf91ee4c2005-03-01 21:24:33 +1100367 case ':':
368 case '/':
369 *s = '\0'; /* terminate */
370 *cp = s + 1;
371 break;
Darren Tucker47eede72005-03-14 23:08:12 +1100372
Damien Millerf91ee4c2005-03-01 21:24:33 +1100373 default:
374 return NULL;
375 }
376
377 return old;
378}
379
Ben Lindstrom4529b702001-05-03 23:39:53 +0000380char *
381cleanhostname(char *host)
382{
383 if (*host == '[' && host[strlen(host) - 1] == ']') {
384 host[strlen(host) - 1] = '\0';
385 return (host + 1);
386 } else
387 return host;
388}
389
390char *
391colon(char *cp)
392{
393 int flag = 0;
394
395 if (*cp == ':') /* Leading colon is part of file name. */
396 return (0);
397 if (*cp == '[')
398 flag = 1;
399
400 for (; *cp; ++cp) {
401 if (*cp == '@' && *(cp+1) == '[')
402 flag = 1;
403 if (*cp == ']' && *(cp+1) == ':' && flag)
404 return (cp+1);
405 if (*cp == ':' && !flag)
406 return (cp);
407 if (*cp == '/')
408 return (0);
409 }
410 return (0);
411}
412
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000413/* function to assist building execv() arguments */
Ben Lindstrom387c4722001-05-08 20:27:25 +0000414void
415addargs(arglist *args, char *fmt, ...)
416{
417 va_list ap;
Damien Miller3eec6b72006-01-31 21:49:27 +1100418 char *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000419 u_int nalloc;
Damien Miller3eec6b72006-01-31 21:49:27 +1100420 int r;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000421
422 va_start(ap, fmt);
Damien Miller3eec6b72006-01-31 21:49:27 +1100423 r = vasprintf(&cp, fmt, ap);
Ben Lindstrom387c4722001-05-08 20:27:25 +0000424 va_end(ap);
Damien Miller3eec6b72006-01-31 21:49:27 +1100425 if (r == -1)
426 fatal("addargs: argument too long");
Ben Lindstrom387c4722001-05-08 20:27:25 +0000427
Darren Tuckerfb16b242003-09-22 21:04:23 +1000428 nalloc = args->nalloc;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000429 if (args->list == NULL) {
Darren Tuckerfb16b242003-09-22 21:04:23 +1000430 nalloc = 32;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000431 args->num = 0;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000432 } else if (args->num+2 >= nalloc)
433 nalloc *= 2;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000434
Damien Miller36812092006-03-26 14:22:47 +1100435 args->list = xrealloc(args->list, nalloc, sizeof(char *));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000436 args->nalloc = nalloc;
Damien Miller3eec6b72006-01-31 21:49:27 +1100437 args->list[args->num++] = cp;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000438 args->list[args->num] = NULL;
439}
Darren Tucker22cc7412004-12-06 22:47:41 +1100440
Damien Miller3eec6b72006-01-31 21:49:27 +1100441void
442replacearg(arglist *args, u_int which, char *fmt, ...)
443{
444 va_list ap;
445 char *cp;
446 int r;
447
448 va_start(ap, fmt);
449 r = vasprintf(&cp, fmt, ap);
450 va_end(ap);
451 if (r == -1)
452 fatal("replacearg: argument too long");
453
454 if (which >= args->num)
455 fatal("replacearg: tried to replace invalid arg %d >= %d",
456 which, args->num);
457 xfree(args->list[which]);
458 args->list[which] = cp;
459}
460
461void
462freeargs(arglist *args)
463{
464 u_int i;
465
466 if (args->list != NULL) {
467 for (i = 0; i < args->num; i++)
468 xfree(args->list[i]);
469 xfree(args->list);
470 args->nalloc = args->num = 0;
471 args->list = NULL;
472 }
473}
474
Darren Tucker22cc7412004-12-06 22:47:41 +1100475/*
Damien Miller5fd38c02005-05-26 12:02:14 +1000476 * Expands tildes in the file name. Returns data allocated by xmalloc.
477 * Warning: this calls getpw*.
478 */
479char *
480tilde_expand_filename(const char *filename, uid_t uid)
481{
482 const char *path;
483 char user[128], ret[MAXPATHLEN];
484 struct passwd *pw;
Damien Millereccb9de2005-06-17 12:59:34 +1000485 u_int len, slash;
Damien Miller5fd38c02005-05-26 12:02:14 +1000486
487 if (*filename != '~')
488 return (xstrdup(filename));
489 filename++;
490
491 path = strchr(filename, '/');
492 if (path != NULL && path > filename) { /* ~user/path */
Damien Millereccb9de2005-06-17 12:59:34 +1000493 slash = path - filename;
494 if (slash > sizeof(user) - 1)
Damien Miller5fd38c02005-05-26 12:02:14 +1000495 fatal("tilde_expand_filename: ~username too long");
Damien Millereccb9de2005-06-17 12:59:34 +1000496 memcpy(user, filename, slash);
497 user[slash] = '\0';
Damien Miller5fd38c02005-05-26 12:02:14 +1000498 if ((pw = getpwnam(user)) == NULL)
499 fatal("tilde_expand_filename: No such user %s", user);
500 } else if ((pw = getpwuid(uid)) == NULL) /* ~/path */
501 fatal("tilde_expand_filename: No such uid %d", uid);
502
503 if (strlcpy(ret, pw->pw_dir, sizeof(ret)) >= sizeof(ret))
504 fatal("tilde_expand_filename: Path too long");
505
506 /* Make sure directory has a trailing '/' */
507 len = strlen(pw->pw_dir);
508 if ((len == 0 || pw->pw_dir[len - 1] != '/') &&
509 strlcat(ret, "/", sizeof(ret)) >= sizeof(ret))
510 fatal("tilde_expand_filename: Path too long");
511
512 /* Skip leading '/' from specified path */
513 if (path != NULL)
514 filename = path + 1;
515 if (strlcat(ret, filename, sizeof(ret)) >= sizeof(ret))
516 fatal("tilde_expand_filename: Path too long");
517
518 return (xstrdup(ret));
519}
520
521/*
Damien Miller6476cad2005-06-16 13:18:34 +1000522 * Expand a string with a set of %[char] escapes. A number of escapes may be
523 * specified as (char *escape_chars, char *replacement) pairs. The list must
Darren Tuckerbee73d52005-07-14 17:05:02 +1000524 * be terminated by a NULL escape_char. Returns replaced string in memory
Damien Miller6476cad2005-06-16 13:18:34 +1000525 * allocated by xmalloc.
526 */
527char *
528percent_expand(const char *string, ...)
529{
530#define EXPAND_MAX_KEYS 16
531 struct {
532 const char *key;
533 const char *repl;
534 } keys[EXPAND_MAX_KEYS];
Damien Millereccb9de2005-06-17 12:59:34 +1000535 u_int num_keys, i, j;
Damien Miller6476cad2005-06-16 13:18:34 +1000536 char buf[4096];
537 va_list ap;
538
539 /* Gather keys */
540 va_start(ap, string);
541 for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) {
542 keys[num_keys].key = va_arg(ap, char *);
543 if (keys[num_keys].key == NULL)
544 break;
545 keys[num_keys].repl = va_arg(ap, char *);
546 if (keys[num_keys].repl == NULL)
547 fatal("percent_expand: NULL replacement");
548 }
549 va_end(ap);
550
551 if (num_keys >= EXPAND_MAX_KEYS)
552 fatal("percent_expand: too many keys");
553
554 /* Expand string */
555 *buf = '\0';
556 for (i = 0; *string != '\0'; string++) {
557 if (*string != '%') {
558 append:
559 buf[i++] = *string;
560 if (i >= sizeof(buf))
561 fatal("percent_expand: string too long");
562 buf[i] = '\0';
563 continue;
564 }
565 string++;
566 if (*string == '%')
567 goto append;
568 for (j = 0; j < num_keys; j++) {
569 if (strchr(keys[j].key, *string) != NULL) {
570 i = strlcat(buf, keys[j].repl, sizeof(buf));
571 if (i >= sizeof(buf))
572 fatal("percent_expand: string too long");
573 break;
574 }
575 }
576 if (j >= num_keys)
577 fatal("percent_expand: unknown key %%%c", *string);
578 }
579 return (xstrdup(buf));
580#undef EXPAND_MAX_KEYS
581}
582
583/*
Darren Tucker22cc7412004-12-06 22:47:41 +1100584 * Read an entire line from a public key file into a static buffer, discarding
585 * lines that exceed the buffer size. Returns 0 on success, -1 on failure.
586 */
587int
588read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
Darren Tuckerf0f90982004-12-11 13:39:50 +1100589 u_long *lineno)
Darren Tucker22cc7412004-12-06 22:47:41 +1100590{
591 while (fgets(buf, bufsz, f) != NULL) {
592 (*lineno)++;
593 if (buf[strlen(buf) - 1] == '\n' || feof(f)) {
594 return 0;
595 } else {
Darren Tuckerf0f90982004-12-11 13:39:50 +1100596 debug("%s: %s line %lu exceeds size limit", __func__,
597 filename, *lineno);
Darren Tucker22cc7412004-12-06 22:47:41 +1100598 /* discard remainder of line */
Darren Tucker47eede72005-03-14 23:08:12 +1100599 while (fgetc(f) != '\n' && !feof(f))
Darren Tucker22cc7412004-12-06 22:47:41 +1100600 ; /* nothing */
601 }
602 }
603 return -1;
604}
Damien Miller13390022005-07-06 09:44:19 +1000605
Damien Millerd27b9472005-12-13 19:29:02 +1100606int
Damien Miller7b58e802005-12-13 19:33:19 +1100607tun_open(int tun, int mode)
Damien Millerd27b9472005-12-13 19:29:02 +1100608{
Damien Miller62a31c92005-12-13 20:44:13 +1100609#if defined(CUSTOM_SYS_TUN_OPEN)
610 return (sys_tun_open(tun, mode));
Damien Miller2dcddbf2006-01-01 19:47:05 +1100611#elif defined(SSH_TUN_OPENBSD)
Damien Miller7b58e802005-12-13 19:33:19 +1100612 struct ifreq ifr;
Damien Millerd27b9472005-12-13 19:29:02 +1100613 char name[100];
Damien Miller7b58e802005-12-13 19:33:19 +1100614 int fd = -1, sock;
Damien Millerd27b9472005-12-13 19:29:02 +1100615
Damien Miller7b58e802005-12-13 19:33:19 +1100616 /* Open the tunnel device */
617 if (tun <= SSH_TUNID_MAX) {
Damien Millerd27b9472005-12-13 19:29:02 +1100618 snprintf(name, sizeof(name), "/dev/tun%d", tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100619 fd = open(name, O_RDWR);
620 } else if (tun == SSH_TUNID_ANY) {
621 for (tun = 100; tun >= 0; tun--) {
622 snprintf(name, sizeof(name), "/dev/tun%d", tun);
623 if ((fd = open(name, O_RDWR)) >= 0)
624 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100625 }
626 } else {
Damien Millera210d522006-01-02 23:40:30 +1100627 debug("%s: invalid tunnel %u", __func__, tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100628 return (-1);
Damien Millerd27b9472005-12-13 19:29:02 +1100629 }
Damien Miller7b58e802005-12-13 19:33:19 +1100630
631 if (fd < 0) {
632 debug("%s: %s open failed: %s", __func__, name, strerror(errno));
633 return (-1);
634 }
635
636 debug("%s: %s mode %d fd %d", __func__, name, mode, fd);
637
638 /* Set the tunnel device operation mode */
639 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tun%d", tun);
640 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
641 goto failed;
642
643 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)
644 goto failed;
Damien Millera1d9a182006-01-02 23:41:21 +1100645
646 /* Set interface mode */
647 ifr.ifr_flags &= ~IFF_UP;
648 if (mode == SSH_TUNMODE_ETHERNET)
Damien Miller7b58e802005-12-13 19:33:19 +1100649 ifr.ifr_flags |= IFF_LINK0;
Damien Millera1d9a182006-01-02 23:41:21 +1100650 else
651 ifr.ifr_flags &= ~IFF_LINK0;
652 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
653 goto failed;
654
655 /* Bring interface up */
Damien Miller7b58e802005-12-13 19:33:19 +1100656 ifr.ifr_flags |= IFF_UP;
657 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
658 goto failed;
659
660 close(sock);
661 return (fd);
662
663 failed:
664 if (fd >= 0)
665 close(fd);
666 if (sock >= 0)
667 close(sock);
668 debug("%s: failed to set %s mode %d: %s", __func__, name,
669 mode, strerror(errno));
Damien Millerd27b9472005-12-13 19:29:02 +1100670 return (-1);
Damien Miller62a31c92005-12-13 20:44:13 +1100671#else
672 error("Tunnel interfaces are not supported on this platform");
673 return (-1);
674#endif
Damien Millerd27b9472005-12-13 19:29:02 +1100675}
676
Darren Tuckerce321d82005-10-03 18:11:24 +1000677void
678sanitise_stdfd(void)
679{
Damien Miller72c5b7d2006-01-06 14:50:44 +1100680 int nullfd, dupfd;
Darren Tuckerce321d82005-10-03 18:11:24 +1000681
Damien Miller72c5b7d2006-01-06 14:50:44 +1100682 if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
Darren Tuckerce321d82005-10-03 18:11:24 +1000683 fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno));
684 exit(1);
685 }
Damien Miller72c5b7d2006-01-06 14:50:44 +1100686 while (++dupfd <= 2) {
687 /* Only clobber closed fds */
688 if (fcntl(dupfd, F_GETFL, 0) >= 0)
689 continue;
690 if (dup2(nullfd, dupfd) == -1) {
Darren Tuckerce321d82005-10-03 18:11:24 +1000691 fprintf(stderr, "dup2: %s", strerror(errno));
692 exit(1);
693 }
Darren Tuckerce321d82005-10-03 18:11:24 +1000694 }
695 if (nullfd > 2)
696 close(nullfd);
697}
698
Damien Miller13390022005-07-06 09:44:19 +1000699char *
Damien Miller3f941882006-03-31 23:13:02 +1100700tohex(const void *vp, size_t l)
Damien Miller13390022005-07-06 09:44:19 +1000701{
Damien Miller3f941882006-03-31 23:13:02 +1100702 const u_char *p = (const u_char *)vp;
Damien Miller13390022005-07-06 09:44:19 +1000703 char b[3], *r;
Damien Miller3f941882006-03-31 23:13:02 +1100704 size_t i, hl;
705
706 if (l > 65536)
707 return xstrdup("tohex: length > 65536");
Damien Miller13390022005-07-06 09:44:19 +1000708
709 hl = l * 2 + 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100710 r = xcalloc(1, hl);
Damien Miller13390022005-07-06 09:44:19 +1000711 for (i = 0; i < l; i++) {
Damien Miller3f941882006-03-31 23:13:02 +1100712 snprintf(b, sizeof(b), "%02x", p[i]);
Damien Miller13390022005-07-06 09:44:19 +1000713 strlcat(r, b, hl);
714 }
715 return (r);
716}
717
Damien Miller3f941882006-03-31 23:13:02 +1100718u_int64_t
719get_u64(const void *vp)
720{
721 const u_char *p = (const u_char *)vp;
722 u_int64_t v;
723
724 v = (u_int64_t)p[0] << 56;
725 v |= (u_int64_t)p[1] << 48;
726 v |= (u_int64_t)p[2] << 40;
727 v |= (u_int64_t)p[3] << 32;
728 v |= (u_int64_t)p[4] << 24;
729 v |= (u_int64_t)p[5] << 16;
730 v |= (u_int64_t)p[6] << 8;
731 v |= (u_int64_t)p[7];
732
733 return (v);
734}
735
736u_int32_t
737get_u32(const void *vp)
738{
739 const u_char *p = (const u_char *)vp;
740 u_int32_t v;
741
742 v = (u_int32_t)p[0] << 24;
743 v |= (u_int32_t)p[1] << 16;
744 v |= (u_int32_t)p[2] << 8;
745 v |= (u_int32_t)p[3];
746
747 return (v);
748}
749
750u_int16_t
751get_u16(const void *vp)
752{
753 const u_char *p = (const u_char *)vp;
754 u_int16_t v;
755
756 v = (u_int16_t)p[0] << 8;
757 v |= (u_int16_t)p[1];
758
759 return (v);
760}
761
762void
763put_u64(void *vp, u_int64_t v)
764{
765 u_char *p = (u_char *)vp;
766
767 p[0] = (u_char)(v >> 56) & 0xff;
768 p[1] = (u_char)(v >> 48) & 0xff;
769 p[2] = (u_char)(v >> 40) & 0xff;
770 p[3] = (u_char)(v >> 32) & 0xff;
771 p[4] = (u_char)(v >> 24) & 0xff;
772 p[5] = (u_char)(v >> 16) & 0xff;
773 p[6] = (u_char)(v >> 8) & 0xff;
774 p[7] = (u_char)v & 0xff;
775}
776
777void
778put_u32(void *vp, u_int32_t v)
779{
780 u_char *p = (u_char *)vp;
781
782 p[0] = (u_char)(v >> 24) & 0xff;
783 p[1] = (u_char)(v >> 16) & 0xff;
784 p[2] = (u_char)(v >> 8) & 0xff;
785 p[3] = (u_char)v & 0xff;
786}
787
788
789void
790put_u16(void *vp, u_int16_t v)
791{
792 u_char *p = (u_char *)vp;
793
794 p[0] = (u_char)(v >> 8) & 0xff;
795 p[1] = (u_char)v & 0xff;
796}