]>
Commit | Line | Data |
---|---|---|
d4530052 | 1 | /* $OpenBSD: misc.c,v 1.56 2006/07/10 12:46:51 dtucker Exp $ */ |
bcbf86ec | 2 | /* |
3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | |
51e7a012 | 4 | * Copyright (c) 2005,2006 Damien Miller. All rights reserved. |
bcbf86ec | 5 | * |
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 | ||
6d24277f | 27 | #include "includes.h" |
bd7c69ea | 28 | |
29 | #include <sys/ioctl.h> | |
b1842393 | 30 | #include <sys/types.h> |
9794d008 | 31 | #include <sys/socket.h> |
32 | ||
33 | #include <netinet/in.h> | |
6425cf65 | 34 | #include <netinet/tcp.h> |
9794d008 | 35 | |
d3221cca | 36 | #include <fcntl.h> |
a75f5360 | 37 | #ifdef HAVE_PATHS_H |
6f61e0ec | 38 | # include <paths.h> |
b1842393 | 39 | #include <pwd.h> |
a75f5360 | 40 | #endif |
1f1fbbd8 | 41 | #ifdef SSH_TUN_OPENBSD |
42 | #include <net/if.h> | |
43 | #endif | |
6d24277f | 44 | |
1aa00dcb | 45 | #include "misc.h" |
42f11eb2 | 46 | #include "log.h" |
4ab21f86 | 47 | #include "xmalloc.h" |
d4530052 | 48 | #include "ssh.h" |
6d24277f | 49 | |
255cabd9 | 50 | /* remove newline at end of string */ |
6d24277f | 51 | char * |
52 | chop(char *s) | |
53 | { | |
54 | char *t = s; | |
55 | while (*t) { | |
6aacefa7 | 56 | if (*t == '\n' || *t == '\r') { |
6d24277f | 57 | *t = '\0'; |
58 | return s; | |
59 | } | |
60 | t++; | |
61 | } | |
62 | return s; | |
63 | ||
64 | } | |
65 | ||
255cabd9 | 66 | /* set/unset filedescriptor to non-blocking */ |
170694d7 | 67 | int |
6d24277f | 68 | set_nonblock(int fd) |
69 | { | |
70 | int val; | |
89aa792b | 71 | |
6d24277f | 72 | val = fcntl(fd, F_GETFL, 0); |
73 | if (val < 0) { | |
74 | error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno)); | |
170694d7 | 75 | return (-1); |
6d24277f | 76 | } |
a22aff1f | 77 | if (val & O_NONBLOCK) { |
170694d7 | 78 | debug3("fd %d is O_NONBLOCK", fd); |
79 | return (0); | |
a22aff1f | 80 | } |
45c42d58 | 81 | debug2("fd %d setting O_NONBLOCK", fd); |
6d24277f | 82 | val |= O_NONBLOCK; |
170694d7 | 83 | if (fcntl(fd, F_SETFL, val) == -1) { |
84 | debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd, | |
85 | strerror(errno)); | |
86 | return (-1); | |
87 | } | |
88 | return (0); | |
6d24277f | 89 | } |
90 | ||
170694d7 | 91 | int |
89aa792b | 92 | unset_nonblock(int fd) |
93 | { | |
94 | int val; | |
95 | ||
96 | val = fcntl(fd, F_GETFL, 0); | |
97 | if (val < 0) { | |
98 | error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno)); | |
170694d7 | 99 | return (-1); |
89aa792b | 100 | } |
101 | if (!(val & O_NONBLOCK)) { | |
170694d7 | 102 | debug3("fd %d is not O_NONBLOCK", fd); |
103 | return (0); | |
89aa792b | 104 | } |
e04e7a19 | 105 | debug("fd %d clearing O_NONBLOCK", fd); |
89aa792b | 106 | val &= ~O_NONBLOCK; |
170694d7 | 107 | if (fcntl(fd, F_SETFL, val) == -1) { |
108 | debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s", | |
c92ec40b | 109 | fd, strerror(errno)); |
170694d7 | 110 | return (-1); |
111 | } | |
112 | return (0); | |
89aa792b | 113 | } |
114 | ||
bcc0381e | 115 | /* disable nagle on socket */ |
116 | void | |
117 | set_nodelay(int fd) | |
118 | { | |
3c05447a | 119 | int opt; |
120 | socklen_t optlen; | |
bcc0381e | 121 | |
811a6342 | 122 | optlen = sizeof opt; |
123 | if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) { | |
f7fb35fe | 124 | debug("getsockopt TCP_NODELAY: %.100s", strerror(errno)); |
811a6342 | 125 | return; |
126 | } | |
127 | if (opt == 1) { | |
128 | debug2("fd %d is TCP_NODELAY", fd); | |
129 | return; | |
130 | } | |
131 | opt = 1; | |
6226a8f8 | 132 | debug2("fd %d setting TCP_NODELAY", fd); |
811a6342 | 133 | if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) |
bcc0381e | 134 | error("setsockopt TCP_NODELAY: %.100s", strerror(errno)); |
135 | } | |
136 | ||
6d24277f | 137 | /* Characters considered whitespace in strsep calls. */ |
138 | #define WHITESPACE " \t\r\n" | |
533b9133 | 139 | #define QUOTE "\"" |
6d24277f | 140 | |
255cabd9 | 141 | /* return next token in configuration line */ |
6d24277f | 142 | char * |
143 | strdelim(char **s) | |
144 | { | |
145 | char *old; | |
146 | int wspace = 0; | |
147 | ||
148 | if (*s == NULL) | |
149 | return NULL; | |
150 | ||
151 | old = *s; | |
152 | ||
533b9133 | 153 | *s = strpbrk(*s, WHITESPACE QUOTE "="); |
6d24277f | 154 | if (*s == NULL) |
155 | return (old); | |
156 | ||
533b9133 | 157 | if (*s[0] == '\"') { |
158 | memmove(*s, *s + 1, strlen(*s)); /* move nul too */ | |
159 | /* Find matching quote */ | |
160 | if ((*s = strpbrk(*s, QUOTE)) == NULL) { | |
161 | return (NULL); /* no matching quote */ | |
162 | } else { | |
163 | *s[0] = '\0'; | |
164 | return (old); | |
165 | } | |
166 | } | |
167 | ||
6d24277f | 168 | /* Allow only one '=' to be skipped */ |
169 | if (*s[0] == '=') | |
170 | wspace = 1; | |
171 | *s[0] = '\0'; | |
172 | ||
533b9133 | 173 | /* Skip any extra whitespace after first token */ |
6d24277f | 174 | *s += strspn(*s + 1, WHITESPACE) + 1; |
175 | if (*s[0] == '=' && !wspace) | |
176 | *s += strspn(*s + 1, WHITESPACE) + 1; | |
177 | ||
178 | return (old); | |
179 | } | |
1aa00dcb | 180 | |
3b1a83df | 181 | struct passwd * |
182 | pwcopy(struct passwd *pw) | |
183 | { | |
52e3daed | 184 | struct passwd *copy = xcalloc(1, sizeof(*copy)); |
5649fbbe | 185 | |
3b1a83df | 186 | copy->pw_name = xstrdup(pw->pw_name); |
187 | copy->pw_passwd = xstrdup(pw->pw_passwd); | |
5649fbbe | 188 | copy->pw_gecos = xstrdup(pw->pw_gecos); |
3b1a83df | 189 | copy->pw_uid = pw->pw_uid; |
190 | copy->pw_gid = pw->pw_gid; | |
7751d4eb | 191 | #ifdef HAVE_PW_EXPIRE_IN_PASSWD |
c4d49b85 | 192 | copy->pw_expire = pw->pw_expire; |
7751d4eb | 193 | #endif |
194 | #ifdef HAVE_PW_CHANGE_IN_PASSWD | |
c4d49b85 | 195 | copy->pw_change = pw->pw_change; |
7751d4eb | 196 | #endif |
2605addd | 197 | #ifdef HAVE_PW_CLASS_IN_PASSWD |
3b1a83df | 198 | copy->pw_class = xstrdup(pw->pw_class); |
2605addd | 199 | #endif |
3b1a83df | 200 | copy->pw_dir = xstrdup(pw->pw_dir); |
201 | copy->pw_shell = xstrdup(pw->pw_shell); | |
202 | return copy; | |
203 | } | |
204 | ||
255cabd9 | 205 | /* |
206 | * Convert ASCII string to TCP/IP port number. | |
207 | * Port must be >0 and <=65535. | |
208 | * Return 0 if invalid. | |
209 | */ | |
210 | int | |
211 | a2port(const char *s) | |
2d2a2c65 | 212 | { |
213 | long port; | |
214 | char *endp; | |
215 | ||
216 | errno = 0; | |
217 | port = strtol(s, &endp, 0); | |
218 | if (s == endp || *endp != '\0' || | |
219 | (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) || | |
220 | port <= 0 || port > 65535) | |
221 | return 0; | |
222 | ||
223 | return port; | |
224 | } | |
225 | ||
d20f3c9e | 226 | int |
227 | a2tun(const char *s, int *remote) | |
228 | { | |
229 | const char *errstr = NULL; | |
230 | char *sp, *ep; | |
231 | int tun; | |
232 | ||
233 | if (remote != NULL) { | |
a4f24bf8 | 234 | *remote = SSH_TUNID_ANY; |
d20f3c9e | 235 | sp = xstrdup(s); |
236 | if ((ep = strchr(sp, ':')) == NULL) { | |
237 | xfree(sp); | |
238 | return (a2tun(s, NULL)); | |
239 | } | |
240 | ep[0] = '\0'; ep++; | |
241 | *remote = a2tun(ep, NULL); | |
242 | tun = a2tun(sp, NULL); | |
243 | xfree(sp); | |
a4f24bf8 | 244 | return (*remote == SSH_TUNID_ERR ? *remote : tun); |
d20f3c9e | 245 | } |
246 | ||
247 | if (strcasecmp(s, "any") == 0) | |
a4f24bf8 | 248 | return (SSH_TUNID_ANY); |
d20f3c9e | 249 | |
a4f24bf8 | 250 | tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr); |
251 | if (errstr != NULL) | |
252 | return (SSH_TUNID_ERR); | |
d20f3c9e | 253 | |
254 | return (tun); | |
255 | } | |
256 | ||
e2b1fb42 | 257 | #define SECONDS 1 |
258 | #define MINUTES (SECONDS * 60) | |
259 | #define HOURS (MINUTES * 60) | |
260 | #define DAYS (HOURS * 24) | |
261 | #define WEEKS (DAYS * 7) | |
262 | ||
255cabd9 | 263 | /* |
264 | * Convert a time string into seconds; format is | |
265 | * a sequence of: | |
266 | * time[qualifier] | |
267 | * | |
268 | * Valid time qualifiers are: | |
269 | * <none> seconds | |
270 | * s|S seconds | |
271 | * m|M minutes | |
272 | * h|H hours | |
273 | * d|D days | |
274 | * w|W weeks | |
275 | * | |
276 | * Examples: | |
277 | * 90m 90 minutes | |
278 | * 1h30m 90 minutes | |
279 | * 2d 2 days | |
280 | * 1w 1 week | |
281 | * | |
282 | * Return -1 if time string is invalid. | |
283 | */ | |
284 | long | |
285 | convtime(const char *s) | |
e2b1fb42 | 286 | { |
287 | long total, secs; | |
288 | const char *p; | |
289 | char *endp; | |
290 | ||
291 | errno = 0; | |
292 | total = 0; | |
293 | p = s; | |
294 | ||
295 | if (p == NULL || *p == '\0') | |
296 | return -1; | |
297 | ||
298 | while (*p) { | |
299 | secs = strtol(p, &endp, 10); | |
300 | if (p == endp || | |
301 | (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) || | |
302 | secs < 0) | |
303 | return -1; | |
304 | ||
305 | switch (*endp++) { | |
306 | case '\0': | |
307 | endp--; | |
5ef36928 | 308 | break; |
e2b1fb42 | 309 | case 's': |
310 | case 'S': | |
311 | break; | |
312 | case 'm': | |
313 | case 'M': | |
314 | secs *= MINUTES; | |
315 | break; | |
316 | case 'h': | |
317 | case 'H': | |
318 | secs *= HOURS; | |
319 | break; | |
320 | case 'd': | |
321 | case 'D': | |
322 | secs *= DAYS; | |
323 | break; | |
324 | case 'w': | |
325 | case 'W': | |
326 | secs *= WEEKS; | |
327 | break; | |
328 | default: | |
329 | return -1; | |
330 | } | |
331 | total += secs; | |
332 | if (total < 0) | |
333 | return -1; | |
334 | p = endp; | |
335 | } | |
336 | ||
337 | return total; | |
338 | } | |
339 | ||
d4530052 | 340 | /* |
341 | * Returns a standardized host+port identifier string. | |
342 | * Caller must free returned string. | |
343 | */ | |
344 | char * | |
345 | put_host_port(const char *host, u_short port) | |
346 | { | |
347 | char *hoststr; | |
348 | ||
349 | if (port == 0 || port == SSH_DEFAULT_PORT) | |
350 | return(xstrdup(host)); | |
351 | if (asprintf(&hoststr, "[%s]:%d", host, (int)port) < 0) | |
352 | fatal("put_host_port: asprintf: %s", strerror(errno)); | |
353 | debug3("put_host_port: %s", hoststr); | |
354 | return hoststr; | |
355 | } | |
356 | ||
3867aa0a | 357 | /* |
358 | * Search for next delimiter between hostnames/addresses and ports. | |
359 | * Argument may be modified (for termination). | |
360 | * Returns *cp if parsing succeeds. | |
361 | * *cp is set to the start of the next delimiter, if one was found. | |
362 | * If this is the last field, *cp is set to NULL. | |
363 | */ | |
364 | char * | |
365 | hpdelim(char **cp) | |
366 | { | |
367 | char *s, *old; | |
368 | ||
369 | if (cp == NULL || *cp == NULL) | |
370 | return NULL; | |
371 | ||
372 | old = s = *cp; | |
373 | if (*s == '[') { | |
374 | if ((s = strchr(s, ']')) == NULL) | |
375 | return NULL; | |
376 | else | |
377 | s++; | |
378 | } else if ((s = strpbrk(s, ":/")) == NULL) | |
379 | s = *cp + strlen(*cp); /* skip to end (see first case below) */ | |
380 | ||
381 | switch (*s) { | |
382 | case '\0': | |
383 | *cp = NULL; /* no more fields*/ | |
384 | break; | |
f8cc7664 | 385 | |
3867aa0a | 386 | case ':': |
387 | case '/': | |
388 | *s = '\0'; /* terminate */ | |
389 | *cp = s + 1; | |
390 | break; | |
f8cc7664 | 391 | |
3867aa0a | 392 | default: |
393 | return NULL; | |
394 | } | |
395 | ||
396 | return old; | |
397 | } | |
398 | ||
1fcde3fe | 399 | char * |
400 | cleanhostname(char *host) | |
401 | { | |
402 | if (*host == '[' && host[strlen(host) - 1] == ']') { | |
403 | host[strlen(host) - 1] = '\0'; | |
404 | return (host + 1); | |
405 | } else | |
406 | return host; | |
407 | } | |
408 | ||
409 | char * | |
410 | colon(char *cp) | |
411 | { | |
412 | int flag = 0; | |
413 | ||
414 | if (*cp == ':') /* Leading colon is part of file name. */ | |
415 | return (0); | |
416 | if (*cp == '[') | |
417 | flag = 1; | |
418 | ||
419 | for (; *cp; ++cp) { | |
420 | if (*cp == '@' && *(cp+1) == '[') | |
421 | flag = 1; | |
422 | if (*cp == ']' && *(cp+1) == ':' && flag) | |
423 | return (cp+1); | |
424 | if (*cp == ':' && !flag) | |
425 | return (cp); | |
426 | if (*cp == '/') | |
427 | return (0); | |
428 | } | |
429 | return (0); | |
430 | } | |
431 | ||
255cabd9 | 432 | /* function to assist building execv() arguments */ |
8a624ebf | 433 | void |
434 | addargs(arglist *args, char *fmt, ...) | |
435 | { | |
436 | va_list ap; | |
4116f5c0 | 437 | char *cp; |
9a995072 | 438 | u_int nalloc; |
4116f5c0 | 439 | int r; |
8a624ebf | 440 | |
441 | va_start(ap, fmt); | |
4116f5c0 | 442 | r = vasprintf(&cp, fmt, ap); |
8a624ebf | 443 | va_end(ap); |
4116f5c0 | 444 | if (r == -1) |
445 | fatal("addargs: argument too long"); | |
8a624ebf | 446 | |
c46e584f | 447 | nalloc = args->nalloc; |
8a624ebf | 448 | if (args->list == NULL) { |
c46e584f | 449 | nalloc = 32; |
8a624ebf | 450 | args->num = 0; |
c46e584f | 451 | } else if (args->num+2 >= nalloc) |
452 | nalloc *= 2; | |
8a624ebf | 453 | |
c5d10563 | 454 | args->list = xrealloc(args->list, nalloc, sizeof(char *)); |
c46e584f | 455 | args->nalloc = nalloc; |
4116f5c0 | 456 | args->list[args->num++] = cp; |
8a624ebf | 457 | args->list[args->num] = NULL; |
458 | } | |
ea067773 | 459 | |
4116f5c0 | 460 | void |
461 | replacearg(arglist *args, u_int which, char *fmt, ...) | |
462 | { | |
463 | va_list ap; | |
464 | char *cp; | |
465 | int r; | |
466 | ||
467 | va_start(ap, fmt); | |
468 | r = vasprintf(&cp, fmt, ap); | |
469 | va_end(ap); | |
470 | if (r == -1) | |
471 | fatal("replacearg: argument too long"); | |
472 | ||
473 | if (which >= args->num) | |
474 | fatal("replacearg: tried to replace invalid arg %d >= %d", | |
475 | which, args->num); | |
476 | xfree(args->list[which]); | |
477 | args->list[which] = cp; | |
478 | } | |
479 | ||
480 | void | |
481 | freeargs(arglist *args) | |
482 | { | |
483 | u_int i; | |
484 | ||
485 | if (args->list != NULL) { | |
486 | for (i = 0; i < args->num; i++) | |
487 | xfree(args->list[i]); | |
488 | xfree(args->list); | |
489 | args->nalloc = args->num = 0; | |
490 | args->list = NULL; | |
491 | } | |
492 | } | |
493 | ||
49e71137 | 494 | /* |
495 | * Expands tildes in the file name. Returns data allocated by xmalloc. | |
496 | * Warning: this calls getpw*. | |
497 | */ | |
498 | char * | |
499 | tilde_expand_filename(const char *filename, uid_t uid) | |
500 | { | |
501 | const char *path; | |
502 | char user[128], ret[MAXPATHLEN]; | |
503 | struct passwd *pw; | |
2ceb8101 | 504 | u_int len, slash; |
49e71137 | 505 | |
506 | if (*filename != '~') | |
507 | return (xstrdup(filename)); | |
508 | filename++; | |
509 | ||
510 | path = strchr(filename, '/'); | |
511 | if (path != NULL && path > filename) { /* ~user/path */ | |
2ceb8101 | 512 | slash = path - filename; |
513 | if (slash > sizeof(user) - 1) | |
49e71137 | 514 | fatal("tilde_expand_filename: ~username too long"); |
2ceb8101 | 515 | memcpy(user, filename, slash); |
516 | user[slash] = '\0'; | |
49e71137 | 517 | if ((pw = getpwnam(user)) == NULL) |
518 | fatal("tilde_expand_filename: No such user %s", user); | |
519 | } else if ((pw = getpwuid(uid)) == NULL) /* ~/path */ | |
520 | fatal("tilde_expand_filename: No such uid %d", uid); | |
521 | ||
522 | if (strlcpy(ret, pw->pw_dir, sizeof(ret)) >= sizeof(ret)) | |
523 | fatal("tilde_expand_filename: Path too long"); | |
524 | ||
525 | /* Make sure directory has a trailing '/' */ | |
526 | len = strlen(pw->pw_dir); | |
527 | if ((len == 0 || pw->pw_dir[len - 1] != '/') && | |
528 | strlcat(ret, "/", sizeof(ret)) >= sizeof(ret)) | |
529 | fatal("tilde_expand_filename: Path too long"); | |
530 | ||
531 | /* Skip leading '/' from specified path */ | |
532 | if (path != NULL) | |
533 | filename = path + 1; | |
534 | if (strlcat(ret, filename, sizeof(ret)) >= sizeof(ret)) | |
535 | fatal("tilde_expand_filename: Path too long"); | |
536 | ||
537 | return (xstrdup(ret)); | |
538 | } | |
539 | ||
a980cbd7 | 540 | /* |
541 | * Expand a string with a set of %[char] escapes. A number of escapes may be | |
542 | * specified as (char *escape_chars, char *replacement) pairs. The list must | |
6381acf0 | 543 | * be terminated by a NULL escape_char. Returns replaced string in memory |
a980cbd7 | 544 | * allocated by xmalloc. |
545 | */ | |
546 | char * | |
547 | percent_expand(const char *string, ...) | |
548 | { | |
549 | #define EXPAND_MAX_KEYS 16 | |
550 | struct { | |
551 | const char *key; | |
552 | const char *repl; | |
553 | } keys[EXPAND_MAX_KEYS]; | |
2ceb8101 | 554 | u_int num_keys, i, j; |
a980cbd7 | 555 | char buf[4096]; |
556 | va_list ap; | |
557 | ||
558 | /* Gather keys */ | |
559 | va_start(ap, string); | |
560 | for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) { | |
561 | keys[num_keys].key = va_arg(ap, char *); | |
562 | if (keys[num_keys].key == NULL) | |
563 | break; | |
564 | keys[num_keys].repl = va_arg(ap, char *); | |
565 | if (keys[num_keys].repl == NULL) | |
566 | fatal("percent_expand: NULL replacement"); | |
567 | } | |
568 | va_end(ap); | |
569 | ||
570 | if (num_keys >= EXPAND_MAX_KEYS) | |
571 | fatal("percent_expand: too many keys"); | |
572 | ||
573 | /* Expand string */ | |
574 | *buf = '\0'; | |
575 | for (i = 0; *string != '\0'; string++) { | |
576 | if (*string != '%') { | |
577 | append: | |
578 | buf[i++] = *string; | |
579 | if (i >= sizeof(buf)) | |
580 | fatal("percent_expand: string too long"); | |
581 | buf[i] = '\0'; | |
582 | continue; | |
583 | } | |
584 | string++; | |
585 | if (*string == '%') | |
586 | goto append; | |
587 | for (j = 0; j < num_keys; j++) { | |
588 | if (strchr(keys[j].key, *string) != NULL) { | |
589 | i = strlcat(buf, keys[j].repl, sizeof(buf)); | |
590 | if (i >= sizeof(buf)) | |
591 | fatal("percent_expand: string too long"); | |
592 | break; | |
593 | } | |
594 | } | |
595 | if (j >= num_keys) | |
596 | fatal("percent_expand: unknown key %%%c", *string); | |
597 | } | |
598 | return (xstrdup(buf)); | |
599 | #undef EXPAND_MAX_KEYS | |
600 | } | |
601 | ||
ea067773 | 602 | /* |
603 | * Read an entire line from a public key file into a static buffer, discarding | |
604 | * lines that exceed the buffer size. Returns 0 on success, -1 on failure. | |
605 | */ | |
606 | int | |
607 | read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz, | |
41feb690 | 608 | u_long *lineno) |
ea067773 | 609 | { |
610 | while (fgets(buf, bufsz, f) != NULL) { | |
611 | (*lineno)++; | |
612 | if (buf[strlen(buf) - 1] == '\n' || feof(f)) { | |
613 | return 0; | |
614 | } else { | |
41feb690 | 615 | debug("%s: %s line %lu exceeds size limit", __func__, |
616 | filename, *lineno); | |
ea067773 | 617 | /* discard remainder of line */ |
f8cc7664 | 618 | while (fgetc(f) != '\n' && !feof(f)) |
ea067773 | 619 | ; /* nothing */ |
620 | } | |
621 | } | |
622 | return -1; | |
623 | } | |
ef07103c | 624 | |
d20f3c9e | 625 | int |
a4f24bf8 | 626 | tun_open(int tun, int mode) |
d20f3c9e | 627 | { |
6306853a | 628 | #if defined(CUSTOM_SYS_TUN_OPEN) |
629 | return (sys_tun_open(tun, mode)); | |
0f6cb079 | 630 | #elif defined(SSH_TUN_OPENBSD) |
a4f24bf8 | 631 | struct ifreq ifr; |
d20f3c9e | 632 | char name[100]; |
a4f24bf8 | 633 | int fd = -1, sock; |
d20f3c9e | 634 | |
a4f24bf8 | 635 | /* Open the tunnel device */ |
636 | if (tun <= SSH_TUNID_MAX) { | |
d20f3c9e | 637 | snprintf(name, sizeof(name), "/dev/tun%d", tun); |
a4f24bf8 | 638 | fd = open(name, O_RDWR); |
639 | } else if (tun == SSH_TUNID_ANY) { | |
640 | for (tun = 100; tun >= 0; tun--) { | |
641 | snprintf(name, sizeof(name), "/dev/tun%d", tun); | |
642 | if ((fd = open(name, O_RDWR)) >= 0) | |
643 | break; | |
d20f3c9e | 644 | } |
645 | } else { | |
81c042a3 | 646 | debug("%s: invalid tunnel %u", __func__, tun); |
a4f24bf8 | 647 | return (-1); |
648 | } | |
649 | ||
650 | if (fd < 0) { | |
651 | debug("%s: %s open failed: %s", __func__, name, strerror(errno)); | |
652 | return (-1); | |
653 | } | |
654 | ||
655 | debug("%s: %s mode %d fd %d", __func__, name, mode, fd); | |
656 | ||
657 | /* Set the tunnel device operation mode */ | |
658 | snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tun%d", tun); | |
659 | if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) | |
660 | goto failed; | |
661 | ||
662 | if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) | |
663 | goto failed; | |
b1b65311 | 664 | |
665 | /* Set interface mode */ | |
666 | ifr.ifr_flags &= ~IFF_UP; | |
667 | if (mode == SSH_TUNMODE_ETHERNET) | |
a4f24bf8 | 668 | ifr.ifr_flags |= IFF_LINK0; |
b1b65311 | 669 | else |
670 | ifr.ifr_flags &= ~IFF_LINK0; | |
671 | if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) | |
672 | goto failed; | |
673 | ||
674 | /* Bring interface up */ | |
a4f24bf8 | 675 | ifr.ifr_flags |= IFF_UP; |
676 | if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) | |
677 | goto failed; | |
678 | ||
679 | close(sock); | |
680 | return (fd); | |
681 | ||
682 | failed: | |
683 | if (fd >= 0) | |
684 | close(fd); | |
685 | if (sock >= 0) | |
686 | close(sock); | |
687 | debug("%s: failed to set %s mode %d: %s", __func__, name, | |
688 | mode, strerror(errno)); | |
d20f3c9e | 689 | return (-1); |
6306853a | 690 | #else |
691 | error("Tunnel interfaces are not supported on this platform"); | |
692 | return (-1); | |
693 | #endif | |
d20f3c9e | 694 | } |
695 | ||
fd6168c1 | 696 | void |
697 | sanitise_stdfd(void) | |
698 | { | |
db382686 | 699 | int nullfd, dupfd; |
fd6168c1 | 700 | |
db382686 | 701 | if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) { |
fd6168c1 | 702 | fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno)); |
703 | exit(1); | |
704 | } | |
db382686 | 705 | while (++dupfd <= 2) { |
706 | /* Only clobber closed fds */ | |
707 | if (fcntl(dupfd, F_GETFL, 0) >= 0) | |
708 | continue; | |
709 | if (dup2(nullfd, dupfd) == -1) { | |
fd6168c1 | 710 | fprintf(stderr, "dup2: %s", strerror(errno)); |
711 | exit(1); | |
712 | } | |
fd6168c1 | 713 | } |
714 | if (nullfd > 2) | |
715 | close(nullfd); | |
716 | } | |
717 | ||
ef07103c | 718 | char * |
51e7a012 | 719 | tohex(const void *vp, size_t l) |
ef07103c | 720 | { |
51e7a012 | 721 | const u_char *p = (const u_char *)vp; |
ef07103c | 722 | char b[3], *r; |
51e7a012 | 723 | size_t i, hl; |
724 | ||
725 | if (l > 65536) | |
726 | return xstrdup("tohex: length > 65536"); | |
ef07103c | 727 | |
728 | hl = l * 2 + 1; | |
52e3daed | 729 | r = xcalloc(1, hl); |
ef07103c | 730 | for (i = 0; i < l; i++) { |
51e7a012 | 731 | snprintf(b, sizeof(b), "%02x", p[i]); |
ef07103c | 732 | strlcat(r, b, hl); |
733 | } | |
734 | return (r); | |
735 | } | |
736 | ||
51e7a012 | 737 | u_int64_t |
738 | get_u64(const void *vp) | |
739 | { | |
740 | const u_char *p = (const u_char *)vp; | |
741 | u_int64_t v; | |
742 | ||
743 | v = (u_int64_t)p[0] << 56; | |
744 | v |= (u_int64_t)p[1] << 48; | |
745 | v |= (u_int64_t)p[2] << 40; | |
746 | v |= (u_int64_t)p[3] << 32; | |
747 | v |= (u_int64_t)p[4] << 24; | |
748 | v |= (u_int64_t)p[5] << 16; | |
749 | v |= (u_int64_t)p[6] << 8; | |
750 | v |= (u_int64_t)p[7]; | |
751 | ||
752 | return (v); | |
753 | } | |
754 | ||
755 | u_int32_t | |
756 | get_u32(const void *vp) | |
757 | { | |
758 | const u_char *p = (const u_char *)vp; | |
759 | u_int32_t v; | |
760 | ||
761 | v = (u_int32_t)p[0] << 24; | |
762 | v |= (u_int32_t)p[1] << 16; | |
763 | v |= (u_int32_t)p[2] << 8; | |
764 | v |= (u_int32_t)p[3]; | |
765 | ||
766 | return (v); | |
767 | } | |
768 | ||
769 | u_int16_t | |
770 | get_u16(const void *vp) | |
771 | { | |
772 | const u_char *p = (const u_char *)vp; | |
773 | u_int16_t v; | |
774 | ||
775 | v = (u_int16_t)p[0] << 8; | |
776 | v |= (u_int16_t)p[1]; | |
777 | ||
778 | return (v); | |
779 | } | |
780 | ||
781 | void | |
782 | put_u64(void *vp, u_int64_t v) | |
783 | { | |
784 | u_char *p = (u_char *)vp; | |
785 | ||
786 | p[0] = (u_char)(v >> 56) & 0xff; | |
787 | p[1] = (u_char)(v >> 48) & 0xff; | |
788 | p[2] = (u_char)(v >> 40) & 0xff; | |
789 | p[3] = (u_char)(v >> 32) & 0xff; | |
790 | p[4] = (u_char)(v >> 24) & 0xff; | |
791 | p[5] = (u_char)(v >> 16) & 0xff; | |
792 | p[6] = (u_char)(v >> 8) & 0xff; | |
793 | p[7] = (u_char)v & 0xff; | |
794 | } | |
795 | ||
796 | void | |
797 | put_u32(void *vp, u_int32_t v) | |
798 | { | |
799 | u_char *p = (u_char *)vp; | |
800 | ||
801 | p[0] = (u_char)(v >> 24) & 0xff; | |
802 | p[1] = (u_char)(v >> 16) & 0xff; | |
803 | p[2] = (u_char)(v >> 8) & 0xff; | |
804 | p[3] = (u_char)v & 0xff; | |
805 | } | |
806 | ||
807 | ||
808 | void | |
809 | put_u16(void *vp, u_int16_t v) | |
810 | { | |
811 | u_char *p = (u_char *)vp; | |
812 | ||
813 | p[0] = (u_char)(v >> 8) & 0xff; | |
814 | p[1] = (u_char)v & 0xff; | |
815 | } |