blob: e85b773d0b616127b04a2db78c364948547c6731 [file] [log] [blame]
Damien Millere4340be2000-09-16 13:29:08 +11001/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Miller6476cad2005-06-16 13:18:34 +10003 * Copyright (c) 2005 Damien Miller. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +11004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
Damien Miller61c51502000-08-18 14:01:04 +100026#include "includes.h"
Damien Miller03e20032006-03-15 11:16:59 +110027RCSID("$OpenBSD: misc.c,v 1.43 2006/02/08 12:15:27 stevesk Exp $");
Damien Miller3beb8522006-01-02 23:40:10 +110028
Damien Miller03e20032006-03-15 11:16:59 +110029#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110030# include <paths.h>
Damien Miller03e20032006-03-15 11:16:59 +110031#endif
Damien Miller3beb8522006-01-02 23:40:10 +110032#ifdef SSH_TUN_OPENBSD
33#include <net/if.h>
34#endif
Damien Miller61c51502000-08-18 14:01:04 +100035
Kevin Stevesb6e773a2001-02-04 13:20:36 +000036#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000037#include "log.h"
Ben Lindstrom06909012001-03-05 06:09:31 +000038#include "xmalloc.h"
Damien Miller61c51502000-08-18 14:01:04 +100039
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000040/* remove newline at end of string */
Damien Miller61c51502000-08-18 14:01:04 +100041char *
42chop(char *s)
43{
44 char *t = s;
45 while (*t) {
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +000046 if (*t == '\n' || *t == '\r') {
Damien Miller61c51502000-08-18 14:01:04 +100047 *t = '\0';
48 return s;
49 }
50 t++;
51 }
52 return s;
53
54}
55
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000056/* set/unset filedescriptor to non-blocking */
Damien Miller232711f2004-06-15 10:35:30 +100057int
Damien Miller61c51502000-08-18 14:01:04 +100058set_nonblock(int fd)
59{
60 int val;
Ben Lindstromc93e84c2001-05-12 00:08:37 +000061
Damien Miller61c51502000-08-18 14:01:04 +100062 val = fcntl(fd, F_GETFL, 0);
63 if (val < 0) {
64 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +100065 return (-1);
Damien Miller61c51502000-08-18 14:01:04 +100066 }
Damien Miller69b69aa2000-10-28 14:19:58 +110067 if (val & O_NONBLOCK) {
Damien Miller232711f2004-06-15 10:35:30 +100068 debug3("fd %d is O_NONBLOCK", fd);
69 return (0);
Damien Miller69b69aa2000-10-28 14:19:58 +110070 }
Damien Milleref095ce2003-05-14 13:41:39 +100071 debug2("fd %d setting O_NONBLOCK", fd);
Damien Miller61c51502000-08-18 14:01:04 +100072 val |= O_NONBLOCK;
Damien Miller232711f2004-06-15 10:35:30 +100073 if (fcntl(fd, F_SETFL, val) == -1) {
74 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd,
75 strerror(errno));
76 return (-1);
77 }
78 return (0);
Damien Miller61c51502000-08-18 14:01:04 +100079}
80
Damien Miller232711f2004-06-15 10:35:30 +100081int
Ben Lindstromc93e84c2001-05-12 00:08:37 +000082unset_nonblock(int fd)
83{
84 int val;
85
86 val = fcntl(fd, F_GETFL, 0);
87 if (val < 0) {
88 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +100089 return (-1);
Ben Lindstromc93e84c2001-05-12 00:08:37 +000090 }
91 if (!(val & O_NONBLOCK)) {
Damien Miller232711f2004-06-15 10:35:30 +100092 debug3("fd %d is not O_NONBLOCK", fd);
93 return (0);
Ben Lindstromc93e84c2001-05-12 00:08:37 +000094 }
Ben Lindstrom352b1c22001-06-21 03:04:37 +000095 debug("fd %d clearing O_NONBLOCK", fd);
Ben Lindstromc93e84c2001-05-12 00:08:37 +000096 val &= ~O_NONBLOCK;
Damien Miller232711f2004-06-15 10:35:30 +100097 if (fcntl(fd, F_SETFL, val) == -1) {
98 debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s",
Ben Lindstrom84fcb312002-03-05 01:48:09 +000099 fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +1000100 return (-1);
101 }
102 return (0);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000103}
104
Damien Miller398e1cf2002-02-05 11:52:13 +1100105/* disable nagle on socket */
106void
107set_nodelay(int fd)
108{
Ben Lindstrome86de512002-03-05 01:28:14 +0000109 int opt;
110 socklen_t optlen;
Damien Miller398e1cf2002-02-05 11:52:13 +1100111
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000112 optlen = sizeof opt;
113 if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
Darren Tucker6db8f932003-11-03 20:07:14 +1100114 debug("getsockopt TCP_NODELAY: %.100s", strerror(errno));
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000115 return;
116 }
117 if (opt == 1) {
118 debug2("fd %d is TCP_NODELAY", fd);
119 return;
120 }
121 opt = 1;
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000122 debug2("fd %d setting TCP_NODELAY", fd);
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000123 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
Damien Miller398e1cf2002-02-05 11:52:13 +1100124 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
125}
126
Damien Miller61c51502000-08-18 14:01:04 +1000127/* Characters considered whitespace in strsep calls. */
128#define WHITESPACE " \t\r\n"
129
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000130/* return next token in configuration line */
Damien Miller61c51502000-08-18 14:01:04 +1000131char *
132strdelim(char **s)
133{
134 char *old;
135 int wspace = 0;
136
137 if (*s == NULL)
138 return NULL;
139
140 old = *s;
141
142 *s = strpbrk(*s, WHITESPACE "=");
143 if (*s == NULL)
144 return (old);
145
146 /* Allow only one '=' to be skipped */
147 if (*s[0] == '=')
148 wspace = 1;
149 *s[0] = '\0';
150
151 *s += strspn(*s + 1, WHITESPACE) + 1;
152 if (*s[0] == '=' && !wspace)
153 *s += strspn(*s + 1, WHITESPACE) + 1;
154
155 return (old);
156}
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000157
Ben Lindstrom086cf212001-03-05 05:56:40 +0000158struct passwd *
159pwcopy(struct passwd *pw)
160{
161 struct passwd *copy = xmalloc(sizeof(*copy));
Ben Lindstrom40304422001-03-05 06:22:01 +0000162
Ben Lindstrom086cf212001-03-05 05:56:40 +0000163 memset(copy, 0, sizeof(*copy));
164 copy->pw_name = xstrdup(pw->pw_name);
165 copy->pw_passwd = xstrdup(pw->pw_passwd);
Ben Lindstrom40304422001-03-05 06:22:01 +0000166 copy->pw_gecos = xstrdup(pw->pw_gecos);
Ben Lindstrom086cf212001-03-05 05:56:40 +0000167 copy->pw_uid = pw->pw_uid;
168 copy->pw_gid = pw->pw_gid;
Kevin Steves82456952001-06-22 21:14:18 +0000169#ifdef HAVE_PW_EXPIRE_IN_PASSWD
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000170 copy->pw_expire = pw->pw_expire;
Kevin Steves82456952001-06-22 21:14:18 +0000171#endif
172#ifdef HAVE_PW_CHANGE_IN_PASSWD
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000173 copy->pw_change = pw->pw_change;
Kevin Steves82456952001-06-22 21:14:18 +0000174#endif
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000175#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom086cf212001-03-05 05:56:40 +0000176 copy->pw_class = xstrdup(pw->pw_class);
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000177#endif
Ben Lindstrom086cf212001-03-05 05:56:40 +0000178 copy->pw_dir = xstrdup(pw->pw_dir);
179 copy->pw_shell = xstrdup(pw->pw_shell);
180 return copy;
181}
182
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000183/*
184 * Convert ASCII string to TCP/IP port number.
185 * Port must be >0 and <=65535.
186 * Return 0 if invalid.
187 */
188int
189a2port(const char *s)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000190{
191 long port;
192 char *endp;
193
194 errno = 0;
195 port = strtol(s, &endp, 0);
196 if (s == endp || *endp != '\0' ||
197 (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
198 port <= 0 || port > 65535)
199 return 0;
200
201 return port;
202}
203
Damien Millerd27b9472005-12-13 19:29:02 +1100204int
205a2tun(const char *s, int *remote)
206{
207 const char *errstr = NULL;
208 char *sp, *ep;
209 int tun;
210
211 if (remote != NULL) {
Damien Miller7b58e802005-12-13 19:33:19 +1100212 *remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +1100213 sp = xstrdup(s);
214 if ((ep = strchr(sp, ':')) == NULL) {
215 xfree(sp);
216 return (a2tun(s, NULL));
217 }
218 ep[0] = '\0'; ep++;
219 *remote = a2tun(ep, NULL);
220 tun = a2tun(sp, NULL);
221 xfree(sp);
Damien Miller7b58e802005-12-13 19:33:19 +1100222 return (*remote == SSH_TUNID_ERR ? *remote : tun);
Damien Millerd27b9472005-12-13 19:29:02 +1100223 }
224
225 if (strcasecmp(s, "any") == 0)
Damien Miller7b58e802005-12-13 19:33:19 +1100226 return (SSH_TUNID_ANY);
Damien Millerd27b9472005-12-13 19:29:02 +1100227
Damien Miller7b58e802005-12-13 19:33:19 +1100228 tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr);
229 if (errstr != NULL)
230 return (SSH_TUNID_ERR);
Damien Millerd27b9472005-12-13 19:29:02 +1100231
232 return (tun);
233}
234
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000235#define SECONDS 1
236#define MINUTES (SECONDS * 60)
237#define HOURS (MINUTES * 60)
238#define DAYS (HOURS * 24)
239#define WEEKS (DAYS * 7)
240
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000241/*
242 * Convert a time string into seconds; format is
243 * a sequence of:
244 * time[qualifier]
245 *
246 * Valid time qualifiers are:
247 * <none> seconds
248 * s|S seconds
249 * m|M minutes
250 * h|H hours
251 * d|D days
252 * w|W weeks
253 *
254 * Examples:
255 * 90m 90 minutes
256 * 1h30m 90 minutes
257 * 2d 2 days
258 * 1w 1 week
259 *
260 * Return -1 if time string is invalid.
261 */
262long
263convtime(const char *s)
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000264{
265 long total, secs;
266 const char *p;
267 char *endp;
268
269 errno = 0;
270 total = 0;
271 p = s;
272
273 if (p == NULL || *p == '\0')
274 return -1;
275
276 while (*p) {
277 secs = strtol(p, &endp, 10);
278 if (p == endp ||
279 (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
280 secs < 0)
281 return -1;
282
283 switch (*endp++) {
284 case '\0':
285 endp--;
286 case 's':
287 case 'S':
288 break;
289 case 'm':
290 case 'M':
291 secs *= MINUTES;
292 break;
293 case 'h':
294 case 'H':
295 secs *= HOURS;
296 break;
297 case 'd':
298 case 'D':
299 secs *= DAYS;
300 break;
301 case 'w':
302 case 'W':
303 secs *= WEEKS;
304 break;
305 default:
306 return -1;
307 }
308 total += secs;
309 if (total < 0)
310 return -1;
311 p = endp;
312 }
313
314 return total;
315}
316
Damien Millerf91ee4c2005-03-01 21:24:33 +1100317/*
318 * Search for next delimiter between hostnames/addresses and ports.
319 * Argument may be modified (for termination).
320 * Returns *cp if parsing succeeds.
321 * *cp is set to the start of the next delimiter, if one was found.
322 * If this is the last field, *cp is set to NULL.
323 */
324char *
325hpdelim(char **cp)
326{
327 char *s, *old;
328
329 if (cp == NULL || *cp == NULL)
330 return NULL;
331
332 old = s = *cp;
333 if (*s == '[') {
334 if ((s = strchr(s, ']')) == NULL)
335 return NULL;
336 else
337 s++;
338 } else if ((s = strpbrk(s, ":/")) == NULL)
339 s = *cp + strlen(*cp); /* skip to end (see first case below) */
340
341 switch (*s) {
342 case '\0':
343 *cp = NULL; /* no more fields*/
344 break;
Darren Tucker47eede72005-03-14 23:08:12 +1100345
Damien Millerf91ee4c2005-03-01 21:24:33 +1100346 case ':':
347 case '/':
348 *s = '\0'; /* terminate */
349 *cp = s + 1;
350 break;
Darren Tucker47eede72005-03-14 23:08:12 +1100351
Damien Millerf91ee4c2005-03-01 21:24:33 +1100352 default:
353 return NULL;
354 }
355
356 return old;
357}
358
Ben Lindstrom4529b702001-05-03 23:39:53 +0000359char *
360cleanhostname(char *host)
361{
362 if (*host == '[' && host[strlen(host) - 1] == ']') {
363 host[strlen(host) - 1] = '\0';
364 return (host + 1);
365 } else
366 return host;
367}
368
369char *
370colon(char *cp)
371{
372 int flag = 0;
373
374 if (*cp == ':') /* Leading colon is part of file name. */
375 return (0);
376 if (*cp == '[')
377 flag = 1;
378
379 for (; *cp; ++cp) {
380 if (*cp == '@' && *(cp+1) == '[')
381 flag = 1;
382 if (*cp == ']' && *(cp+1) == ':' && flag)
383 return (cp+1);
384 if (*cp == ':' && !flag)
385 return (cp);
386 if (*cp == '/')
387 return (0);
388 }
389 return (0);
390}
391
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000392/* function to assist building execv() arguments */
Ben Lindstrom387c4722001-05-08 20:27:25 +0000393void
394addargs(arglist *args, char *fmt, ...)
395{
396 va_list ap;
Damien Miller3eec6b72006-01-31 21:49:27 +1100397 char *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000398 u_int nalloc;
Damien Miller3eec6b72006-01-31 21:49:27 +1100399 int r;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000400
401 va_start(ap, fmt);
Damien Miller3eec6b72006-01-31 21:49:27 +1100402 r = vasprintf(&cp, fmt, ap);
Ben Lindstrom387c4722001-05-08 20:27:25 +0000403 va_end(ap);
Damien Miller3eec6b72006-01-31 21:49:27 +1100404 if (r == -1)
405 fatal("addargs: argument too long");
Ben Lindstrom387c4722001-05-08 20:27:25 +0000406
Darren Tuckerfb16b242003-09-22 21:04:23 +1000407 nalloc = args->nalloc;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000408 if (args->list == NULL) {
Darren Tuckerfb16b242003-09-22 21:04:23 +1000409 nalloc = 32;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000410 args->num = 0;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000411 } else if (args->num+2 >= nalloc)
412 nalloc *= 2;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000413
Darren Tuckerfb16b242003-09-22 21:04:23 +1000414 args->list = xrealloc(args->list, nalloc * sizeof(char *));
415 args->nalloc = nalloc;
Damien Miller3eec6b72006-01-31 21:49:27 +1100416 args->list[args->num++] = cp;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000417 args->list[args->num] = NULL;
418}
Darren Tucker22cc7412004-12-06 22:47:41 +1100419
Damien Miller3eec6b72006-01-31 21:49:27 +1100420void
421replacearg(arglist *args, u_int which, char *fmt, ...)
422{
423 va_list ap;
424 char *cp;
425 int r;
426
427 va_start(ap, fmt);
428 r = vasprintf(&cp, fmt, ap);
429 va_end(ap);
430 if (r == -1)
431 fatal("replacearg: argument too long");
432
433 if (which >= args->num)
434 fatal("replacearg: tried to replace invalid arg %d >= %d",
435 which, args->num);
436 xfree(args->list[which]);
437 args->list[which] = cp;
438}
439
440void
441freeargs(arglist *args)
442{
443 u_int i;
444
445 if (args->list != NULL) {
446 for (i = 0; i < args->num; i++)
447 xfree(args->list[i]);
448 xfree(args->list);
449 args->nalloc = args->num = 0;
450 args->list = NULL;
451 }
452}
453
Darren Tucker22cc7412004-12-06 22:47:41 +1100454/*
Damien Miller5fd38c02005-05-26 12:02:14 +1000455 * Expands tildes in the file name. Returns data allocated by xmalloc.
456 * Warning: this calls getpw*.
457 */
458char *
459tilde_expand_filename(const char *filename, uid_t uid)
460{
461 const char *path;
462 char user[128], ret[MAXPATHLEN];
463 struct passwd *pw;
Damien Millereccb9de2005-06-17 12:59:34 +1000464 u_int len, slash;
Damien Miller5fd38c02005-05-26 12:02:14 +1000465
466 if (*filename != '~')
467 return (xstrdup(filename));
468 filename++;
469
470 path = strchr(filename, '/');
471 if (path != NULL && path > filename) { /* ~user/path */
Damien Millereccb9de2005-06-17 12:59:34 +1000472 slash = path - filename;
473 if (slash > sizeof(user) - 1)
Damien Miller5fd38c02005-05-26 12:02:14 +1000474 fatal("tilde_expand_filename: ~username too long");
Damien Millereccb9de2005-06-17 12:59:34 +1000475 memcpy(user, filename, slash);
476 user[slash] = '\0';
Damien Miller5fd38c02005-05-26 12:02:14 +1000477 if ((pw = getpwnam(user)) == NULL)
478 fatal("tilde_expand_filename: No such user %s", user);
479 } else if ((pw = getpwuid(uid)) == NULL) /* ~/path */
480 fatal("tilde_expand_filename: No such uid %d", uid);
481
482 if (strlcpy(ret, pw->pw_dir, sizeof(ret)) >= sizeof(ret))
483 fatal("tilde_expand_filename: Path too long");
484
485 /* Make sure directory has a trailing '/' */
486 len = strlen(pw->pw_dir);
487 if ((len == 0 || pw->pw_dir[len - 1] != '/') &&
488 strlcat(ret, "/", sizeof(ret)) >= sizeof(ret))
489 fatal("tilde_expand_filename: Path too long");
490
491 /* Skip leading '/' from specified path */
492 if (path != NULL)
493 filename = path + 1;
494 if (strlcat(ret, filename, sizeof(ret)) >= sizeof(ret))
495 fatal("tilde_expand_filename: Path too long");
496
497 return (xstrdup(ret));
498}
499
500/*
Damien Miller6476cad2005-06-16 13:18:34 +1000501 * Expand a string with a set of %[char] escapes. A number of escapes may be
502 * specified as (char *escape_chars, char *replacement) pairs. The list must
Darren Tuckerbee73d52005-07-14 17:05:02 +1000503 * be terminated by a NULL escape_char. Returns replaced string in memory
Damien Miller6476cad2005-06-16 13:18:34 +1000504 * allocated by xmalloc.
505 */
506char *
507percent_expand(const char *string, ...)
508{
509#define EXPAND_MAX_KEYS 16
510 struct {
511 const char *key;
512 const char *repl;
513 } keys[EXPAND_MAX_KEYS];
Damien Millereccb9de2005-06-17 12:59:34 +1000514 u_int num_keys, i, j;
Damien Miller6476cad2005-06-16 13:18:34 +1000515 char buf[4096];
516 va_list ap;
517
518 /* Gather keys */
519 va_start(ap, string);
520 for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) {
521 keys[num_keys].key = va_arg(ap, char *);
522 if (keys[num_keys].key == NULL)
523 break;
524 keys[num_keys].repl = va_arg(ap, char *);
525 if (keys[num_keys].repl == NULL)
526 fatal("percent_expand: NULL replacement");
527 }
528 va_end(ap);
529
530 if (num_keys >= EXPAND_MAX_KEYS)
531 fatal("percent_expand: too many keys");
532
533 /* Expand string */
534 *buf = '\0';
535 for (i = 0; *string != '\0'; string++) {
536 if (*string != '%') {
537 append:
538 buf[i++] = *string;
539 if (i >= sizeof(buf))
540 fatal("percent_expand: string too long");
541 buf[i] = '\0';
542 continue;
543 }
544 string++;
545 if (*string == '%')
546 goto append;
547 for (j = 0; j < num_keys; j++) {
548 if (strchr(keys[j].key, *string) != NULL) {
549 i = strlcat(buf, keys[j].repl, sizeof(buf));
550 if (i >= sizeof(buf))
551 fatal("percent_expand: string too long");
552 break;
553 }
554 }
555 if (j >= num_keys)
556 fatal("percent_expand: unknown key %%%c", *string);
557 }
558 return (xstrdup(buf));
559#undef EXPAND_MAX_KEYS
560}
561
562/*
Darren Tucker22cc7412004-12-06 22:47:41 +1100563 * Read an entire line from a public key file into a static buffer, discarding
564 * lines that exceed the buffer size. Returns 0 on success, -1 on failure.
565 */
566int
567read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
Darren Tuckerf0f90982004-12-11 13:39:50 +1100568 u_long *lineno)
Darren Tucker22cc7412004-12-06 22:47:41 +1100569{
570 while (fgets(buf, bufsz, f) != NULL) {
571 (*lineno)++;
572 if (buf[strlen(buf) - 1] == '\n' || feof(f)) {
573 return 0;
574 } else {
Darren Tuckerf0f90982004-12-11 13:39:50 +1100575 debug("%s: %s line %lu exceeds size limit", __func__,
576 filename, *lineno);
Darren Tucker22cc7412004-12-06 22:47:41 +1100577 /* discard remainder of line */
Darren Tucker47eede72005-03-14 23:08:12 +1100578 while (fgetc(f) != '\n' && !feof(f))
Darren Tucker22cc7412004-12-06 22:47:41 +1100579 ; /* nothing */
580 }
581 }
582 return -1;
583}
Damien Miller13390022005-07-06 09:44:19 +1000584
Damien Millerd27b9472005-12-13 19:29:02 +1100585int
Damien Miller7b58e802005-12-13 19:33:19 +1100586tun_open(int tun, int mode)
Damien Millerd27b9472005-12-13 19:29:02 +1100587{
Damien Miller62a31c92005-12-13 20:44:13 +1100588#if defined(CUSTOM_SYS_TUN_OPEN)
589 return (sys_tun_open(tun, mode));
Damien Miller2dcddbf2006-01-01 19:47:05 +1100590#elif defined(SSH_TUN_OPENBSD)
Damien Miller7b58e802005-12-13 19:33:19 +1100591 struct ifreq ifr;
Damien Millerd27b9472005-12-13 19:29:02 +1100592 char name[100];
Damien Miller7b58e802005-12-13 19:33:19 +1100593 int fd = -1, sock;
Damien Millerd27b9472005-12-13 19:29:02 +1100594
Damien Miller7b58e802005-12-13 19:33:19 +1100595 /* Open the tunnel device */
596 if (tun <= SSH_TUNID_MAX) {
Damien Millerd27b9472005-12-13 19:29:02 +1100597 snprintf(name, sizeof(name), "/dev/tun%d", tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100598 fd = open(name, O_RDWR);
599 } else if (tun == SSH_TUNID_ANY) {
600 for (tun = 100; tun >= 0; tun--) {
601 snprintf(name, sizeof(name), "/dev/tun%d", tun);
602 if ((fd = open(name, O_RDWR)) >= 0)
603 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100604 }
605 } else {
Damien Millera210d522006-01-02 23:40:30 +1100606 debug("%s: invalid tunnel %u", __func__, tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100607 return (-1);
Damien Millerd27b9472005-12-13 19:29:02 +1100608 }
Damien Miller7b58e802005-12-13 19:33:19 +1100609
610 if (fd < 0) {
611 debug("%s: %s open failed: %s", __func__, name, strerror(errno));
612 return (-1);
613 }
614
615 debug("%s: %s mode %d fd %d", __func__, name, mode, fd);
616
617 /* Set the tunnel device operation mode */
618 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tun%d", tun);
619 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
620 goto failed;
621
622 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)
623 goto failed;
Damien Millera1d9a182006-01-02 23:41:21 +1100624
625 /* Set interface mode */
626 ifr.ifr_flags &= ~IFF_UP;
627 if (mode == SSH_TUNMODE_ETHERNET)
Damien Miller7b58e802005-12-13 19:33:19 +1100628 ifr.ifr_flags |= IFF_LINK0;
Damien Millera1d9a182006-01-02 23:41:21 +1100629 else
630 ifr.ifr_flags &= ~IFF_LINK0;
631 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
632 goto failed;
633
634 /* Bring interface up */
Damien Miller7b58e802005-12-13 19:33:19 +1100635 ifr.ifr_flags |= IFF_UP;
636 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
637 goto failed;
638
639 close(sock);
640 return (fd);
641
642 failed:
643 if (fd >= 0)
644 close(fd);
645 if (sock >= 0)
646 close(sock);
647 debug("%s: failed to set %s mode %d: %s", __func__, name,
648 mode, strerror(errno));
Damien Millerd27b9472005-12-13 19:29:02 +1100649 return (-1);
Damien Miller62a31c92005-12-13 20:44:13 +1100650#else
651 error("Tunnel interfaces are not supported on this platform");
652 return (-1);
653#endif
Damien Millerd27b9472005-12-13 19:29:02 +1100654}
655
Darren Tuckerce321d82005-10-03 18:11:24 +1000656void
657sanitise_stdfd(void)
658{
Damien Miller72c5b7d2006-01-06 14:50:44 +1100659 int nullfd, dupfd;
Darren Tuckerce321d82005-10-03 18:11:24 +1000660
Damien Miller72c5b7d2006-01-06 14:50:44 +1100661 if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
Darren Tuckerce321d82005-10-03 18:11:24 +1000662 fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno));
663 exit(1);
664 }
Damien Miller72c5b7d2006-01-06 14:50:44 +1100665 while (++dupfd <= 2) {
666 /* Only clobber closed fds */
667 if (fcntl(dupfd, F_GETFL, 0) >= 0)
668 continue;
669 if (dup2(nullfd, dupfd) == -1) {
Darren Tuckerce321d82005-10-03 18:11:24 +1000670 fprintf(stderr, "dup2: %s", strerror(errno));
671 exit(1);
672 }
Darren Tuckerce321d82005-10-03 18:11:24 +1000673 }
674 if (nullfd > 2)
675 close(nullfd);
676}
677
Damien Miller13390022005-07-06 09:44:19 +1000678char *
679tohex(const u_char *d, u_int l)
680{
681 char b[3], *r;
682 u_int i, hl;
683
684 hl = l * 2 + 1;
685 r = xmalloc(hl);
686 *r = '\0';
687 for (i = 0; i < l; i++) {
688 snprintf(b, sizeof(b), "%02x", d[i]);
689 strlcat(r, b, hl);
690 }
691 return (r);
692}
693