blob: a65b1fdedf3b22b5f2150278feb9e83150c85d0e [file] [log] [blame]
Darren Tuckerda345532006-07-10 23:04:19 +10001/* $OpenBSD: misc.c,v 1.56 2006/07/10 12:46:51 dtucker 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 Miller57cf6382006-07-10 21:13:46 +100036#include <fcntl.h>
Damien Miller03e20032006-03-15 11:16:59 +110037#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110038# include <paths.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100039#include <pwd.h>
Damien Miller03e20032006-03-15 11:16:59 +110040#endif
Damien Miller3beb8522006-01-02 23:40:10 +110041#ifdef SSH_TUN_OPENBSD
42#include <net/if.h>
43#endif
Damien Miller61c51502000-08-18 14:01:04 +100044
Kevin Stevesb6e773a2001-02-04 13:20:36 +000045#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include "log.h"
Ben Lindstrom06909012001-03-05 06:09:31 +000047#include "xmalloc.h"
Darren Tuckerda345532006-07-10 23:04:19 +100048#include "ssh.h"
Damien Miller61c51502000-08-18 14:01:04 +100049
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000050/* remove newline at end of string */
Damien Miller61c51502000-08-18 14:01:04 +100051char *
52chop(char *s)
53{
54 char *t = s;
55 while (*t) {
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +000056 if (*t == '\n' || *t == '\r') {
Damien Miller61c51502000-08-18 14:01:04 +100057 *t = '\0';
58 return s;
59 }
60 t++;
61 }
62 return s;
63
64}
65
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000066/* set/unset filedescriptor to non-blocking */
Damien Miller232711f2004-06-15 10:35:30 +100067int
Damien Miller61c51502000-08-18 14:01:04 +100068set_nonblock(int fd)
69{
70 int val;
Ben Lindstromc93e84c2001-05-12 00:08:37 +000071
Damien Miller61c51502000-08-18 14:01:04 +100072 val = fcntl(fd, F_GETFL, 0);
73 if (val < 0) {
74 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +100075 return (-1);
Damien Miller61c51502000-08-18 14:01:04 +100076 }
Damien Miller69b69aa2000-10-28 14:19:58 +110077 if (val & O_NONBLOCK) {
Damien Miller232711f2004-06-15 10:35:30 +100078 debug3("fd %d is O_NONBLOCK", fd);
79 return (0);
Damien Miller69b69aa2000-10-28 14:19:58 +110080 }
Damien Milleref095ce2003-05-14 13:41:39 +100081 debug2("fd %d setting O_NONBLOCK", fd);
Damien Miller61c51502000-08-18 14:01:04 +100082 val |= O_NONBLOCK;
Damien Miller232711f2004-06-15 10:35:30 +100083 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);
Damien Miller61c51502000-08-18 14:01:04 +100089}
90
Damien Miller232711f2004-06-15 10:35:30 +100091int
Ben Lindstromc93e84c2001-05-12 00:08:37 +000092unset_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));
Damien Miller232711f2004-06-15 10:35:30 +100099 return (-1);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000100 }
101 if (!(val & O_NONBLOCK)) {
Damien Miller232711f2004-06-15 10:35:30 +1000102 debug3("fd %d is not O_NONBLOCK", fd);
103 return (0);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000104 }
Ben Lindstrom352b1c22001-06-21 03:04:37 +0000105 debug("fd %d clearing O_NONBLOCK", fd);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000106 val &= ~O_NONBLOCK;
Damien Miller232711f2004-06-15 10:35:30 +1000107 if (fcntl(fd, F_SETFL, val) == -1) {
108 debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s",
Ben Lindstrom84fcb312002-03-05 01:48:09 +0000109 fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +1000110 return (-1);
111 }
112 return (0);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000113}
114
Damien Miller398e1cf2002-02-05 11:52:13 +1100115/* disable nagle on socket */
116void
117set_nodelay(int fd)
118{
Ben Lindstrome86de512002-03-05 01:28:14 +0000119 int opt;
120 socklen_t optlen;
Damien Miller398e1cf2002-02-05 11:52:13 +1100121
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000122 optlen = sizeof opt;
123 if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
Darren Tucker6db8f932003-11-03 20:07:14 +1100124 debug("getsockopt TCP_NODELAY: %.100s", strerror(errno));
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000125 return;
126 }
127 if (opt == 1) {
128 debug2("fd %d is TCP_NODELAY", fd);
129 return;
130 }
131 opt = 1;
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000132 debug2("fd %d setting TCP_NODELAY", fd);
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000133 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
Damien Miller398e1cf2002-02-05 11:52:13 +1100134 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
135}
136
Damien Miller61c51502000-08-18 14:01:04 +1000137/* Characters considered whitespace in strsep calls. */
138#define WHITESPACE " \t\r\n"
Damien Miller306d1182006-03-15 12:05:59 +1100139#define QUOTE "\""
Damien Miller61c51502000-08-18 14:01:04 +1000140
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000141/* return next token in configuration line */
Damien Miller61c51502000-08-18 14:01:04 +1000142char *
143strdelim(char **s)
144{
145 char *old;
146 int wspace = 0;
147
148 if (*s == NULL)
149 return NULL;
150
151 old = *s;
152
Damien Miller306d1182006-03-15 12:05:59 +1100153 *s = strpbrk(*s, WHITESPACE QUOTE "=");
Damien Miller61c51502000-08-18 14:01:04 +1000154 if (*s == NULL)
155 return (old);
156
Damien Miller306d1182006-03-15 12:05:59 +1100157 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
Damien Miller61c51502000-08-18 14:01:04 +1000168 /* Allow only one '=' to be skipped */
169 if (*s[0] == '=')
170 wspace = 1;
171 *s[0] = '\0';
172
Damien Miller306d1182006-03-15 12:05:59 +1100173 /* Skip any extra whitespace after first token */
Damien Miller61c51502000-08-18 14:01:04 +1000174 *s += strspn(*s + 1, WHITESPACE) + 1;
175 if (*s[0] == '=' && !wspace)
176 *s += strspn(*s + 1, WHITESPACE) + 1;
177
178 return (old);
179}
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000180
Ben Lindstrom086cf212001-03-05 05:56:40 +0000181struct passwd *
182pwcopy(struct passwd *pw)
183{
Damien Miller07d86be2006-03-26 14:19:21 +1100184 struct passwd *copy = xcalloc(1, sizeof(*copy));
Ben Lindstrom40304422001-03-05 06:22:01 +0000185
Ben Lindstrom086cf212001-03-05 05:56:40 +0000186 copy->pw_name = xstrdup(pw->pw_name);
187 copy->pw_passwd = xstrdup(pw->pw_passwd);
Ben Lindstrom40304422001-03-05 06:22:01 +0000188 copy->pw_gecos = xstrdup(pw->pw_gecos);
Ben Lindstrom086cf212001-03-05 05:56:40 +0000189 copy->pw_uid = pw->pw_uid;
190 copy->pw_gid = pw->pw_gid;
Kevin Steves82456952001-06-22 21:14:18 +0000191#ifdef HAVE_PW_EXPIRE_IN_PASSWD
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000192 copy->pw_expire = pw->pw_expire;
Kevin Steves82456952001-06-22 21:14:18 +0000193#endif
194#ifdef HAVE_PW_CHANGE_IN_PASSWD
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000195 copy->pw_change = pw->pw_change;
Kevin Steves82456952001-06-22 21:14:18 +0000196#endif
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000197#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom086cf212001-03-05 05:56:40 +0000198 copy->pw_class = xstrdup(pw->pw_class);
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000199#endif
Ben Lindstrom086cf212001-03-05 05:56:40 +0000200 copy->pw_dir = xstrdup(pw->pw_dir);
201 copy->pw_shell = xstrdup(pw->pw_shell);
202 return copy;
203}
204
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000205/*
206 * Convert ASCII string to TCP/IP port number.
207 * Port must be >0 and <=65535.
208 * Return 0 if invalid.
209 */
210int
211a2port(const char *s)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000212{
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
Damien Millerd27b9472005-12-13 19:29:02 +1100226int
227a2tun(const char *s, int *remote)
228{
229 const char *errstr = NULL;
230 char *sp, *ep;
231 int tun;
232
233 if (remote != NULL) {
Damien Miller7b58e802005-12-13 19:33:19 +1100234 *remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +1100235 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);
Damien Miller7b58e802005-12-13 19:33:19 +1100244 return (*remote == SSH_TUNID_ERR ? *remote : tun);
Damien Millerd27b9472005-12-13 19:29:02 +1100245 }
246
247 if (strcasecmp(s, "any") == 0)
Damien Miller7b58e802005-12-13 19:33:19 +1100248 return (SSH_TUNID_ANY);
Damien Millerd27b9472005-12-13 19:29:02 +1100249
Damien Miller7b58e802005-12-13 19:33:19 +1100250 tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr);
251 if (errstr != NULL)
252 return (SSH_TUNID_ERR);
Damien Millerd27b9472005-12-13 19:29:02 +1100253
254 return (tun);
255}
256
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000257#define SECONDS 1
258#define MINUTES (SECONDS * 60)
259#define HOURS (MINUTES * 60)
260#define DAYS (HOURS * 24)
261#define WEEKS (DAYS * 7)
262
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000263/*
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 */
284long
285convtime(const char *s)
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000286{
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--;
Damien Miller69b72032006-03-26 14:02:35 +1100308 break;
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000309 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
Damien Millerf91ee4c2005-03-01 21:24:33 +1100340/*
Darren Tuckerda345532006-07-10 23:04:19 +1000341 * Returns a standardized host+port identifier string.
342 * Caller must free returned string.
343 */
344char *
345put_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
357/*
Damien Millerf91ee4c2005-03-01 21:24:33 +1100358 * 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 */
364char *
365hpdelim(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;
Darren Tucker47eede72005-03-14 23:08:12 +1100385
Damien Millerf91ee4c2005-03-01 21:24:33 +1100386 case ':':
387 case '/':
388 *s = '\0'; /* terminate */
389 *cp = s + 1;
390 break;
Darren Tucker47eede72005-03-14 23:08:12 +1100391
Damien Millerf91ee4c2005-03-01 21:24:33 +1100392 default:
393 return NULL;
394 }
395
396 return old;
397}
398
Ben Lindstrom4529b702001-05-03 23:39:53 +0000399char *
400cleanhostname(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
409char *
410colon(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
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000432/* function to assist building execv() arguments */
Ben Lindstrom387c4722001-05-08 20:27:25 +0000433void
434addargs(arglist *args, char *fmt, ...)
435{
436 va_list ap;
Damien Miller3eec6b72006-01-31 21:49:27 +1100437 char *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000438 u_int nalloc;
Damien Miller3eec6b72006-01-31 21:49:27 +1100439 int r;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000440
441 va_start(ap, fmt);
Damien Miller3eec6b72006-01-31 21:49:27 +1100442 r = vasprintf(&cp, fmt, ap);
Ben Lindstrom387c4722001-05-08 20:27:25 +0000443 va_end(ap);
Damien Miller3eec6b72006-01-31 21:49:27 +1100444 if (r == -1)
445 fatal("addargs: argument too long");
Ben Lindstrom387c4722001-05-08 20:27:25 +0000446
Darren Tuckerfb16b242003-09-22 21:04:23 +1000447 nalloc = args->nalloc;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000448 if (args->list == NULL) {
Darren Tuckerfb16b242003-09-22 21:04:23 +1000449 nalloc = 32;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000450 args->num = 0;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000451 } else if (args->num+2 >= nalloc)
452 nalloc *= 2;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000453
Damien Miller36812092006-03-26 14:22:47 +1100454 args->list = xrealloc(args->list, nalloc, sizeof(char *));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000455 args->nalloc = nalloc;
Damien Miller3eec6b72006-01-31 21:49:27 +1100456 args->list[args->num++] = cp;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000457 args->list[args->num] = NULL;
458}
Darren Tucker22cc7412004-12-06 22:47:41 +1100459
Damien Miller3eec6b72006-01-31 21:49:27 +1100460void
461replacearg(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
480void
481freeargs(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
Darren Tucker22cc7412004-12-06 22:47:41 +1100494/*
Damien Miller5fd38c02005-05-26 12:02:14 +1000495 * Expands tildes in the file name. Returns data allocated by xmalloc.
496 * Warning: this calls getpw*.
497 */
498char *
499tilde_expand_filename(const char *filename, uid_t uid)
500{
501 const char *path;
502 char user[128], ret[MAXPATHLEN];
503 struct passwd *pw;
Damien Millereccb9de2005-06-17 12:59:34 +1000504 u_int len, slash;
Damien Miller5fd38c02005-05-26 12:02:14 +1000505
506 if (*filename != '~')
507 return (xstrdup(filename));
508 filename++;
509
510 path = strchr(filename, '/');
511 if (path != NULL && path > filename) { /* ~user/path */
Damien Millereccb9de2005-06-17 12:59:34 +1000512 slash = path - filename;
513 if (slash > sizeof(user) - 1)
Damien Miller5fd38c02005-05-26 12:02:14 +1000514 fatal("tilde_expand_filename: ~username too long");
Damien Millereccb9de2005-06-17 12:59:34 +1000515 memcpy(user, filename, slash);
516 user[slash] = '\0';
Damien Miller5fd38c02005-05-26 12:02:14 +1000517 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
540/*
Damien Miller6476cad2005-06-16 13:18:34 +1000541 * 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
Darren Tuckerbee73d52005-07-14 17:05:02 +1000543 * be terminated by a NULL escape_char. Returns replaced string in memory
Damien Miller6476cad2005-06-16 13:18:34 +1000544 * allocated by xmalloc.
545 */
546char *
547percent_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];
Damien Millereccb9de2005-06-17 12:59:34 +1000554 u_int num_keys, i, j;
Damien Miller6476cad2005-06-16 13:18:34 +1000555 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
602/*
Darren Tucker22cc7412004-12-06 22:47:41 +1100603 * 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 */
606int
607read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
Darren Tuckerf0f90982004-12-11 13:39:50 +1100608 u_long *lineno)
Darren Tucker22cc7412004-12-06 22:47:41 +1100609{
610 while (fgets(buf, bufsz, f) != NULL) {
611 (*lineno)++;
612 if (buf[strlen(buf) - 1] == '\n' || feof(f)) {
613 return 0;
614 } else {
Darren Tuckerf0f90982004-12-11 13:39:50 +1100615 debug("%s: %s line %lu exceeds size limit", __func__,
616 filename, *lineno);
Darren Tucker22cc7412004-12-06 22:47:41 +1100617 /* discard remainder of line */
Darren Tucker47eede72005-03-14 23:08:12 +1100618 while (fgetc(f) != '\n' && !feof(f))
Darren Tucker22cc7412004-12-06 22:47:41 +1100619 ; /* nothing */
620 }
621 }
622 return -1;
623}
Damien Miller13390022005-07-06 09:44:19 +1000624
Damien Millerd27b9472005-12-13 19:29:02 +1100625int
Damien Miller7b58e802005-12-13 19:33:19 +1100626tun_open(int tun, int mode)
Damien Millerd27b9472005-12-13 19:29:02 +1100627{
Damien Miller62a31c92005-12-13 20:44:13 +1100628#if defined(CUSTOM_SYS_TUN_OPEN)
629 return (sys_tun_open(tun, mode));
Damien Miller2dcddbf2006-01-01 19:47:05 +1100630#elif defined(SSH_TUN_OPENBSD)
Damien Miller7b58e802005-12-13 19:33:19 +1100631 struct ifreq ifr;
Damien Millerd27b9472005-12-13 19:29:02 +1100632 char name[100];
Damien Miller7b58e802005-12-13 19:33:19 +1100633 int fd = -1, sock;
Damien Millerd27b9472005-12-13 19:29:02 +1100634
Damien Miller7b58e802005-12-13 19:33:19 +1100635 /* Open the tunnel device */
636 if (tun <= SSH_TUNID_MAX) {
Damien Millerd27b9472005-12-13 19:29:02 +1100637 snprintf(name, sizeof(name), "/dev/tun%d", tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100638 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;
Damien Millerd27b9472005-12-13 19:29:02 +1100644 }
645 } else {
Damien Millera210d522006-01-02 23:40:30 +1100646 debug("%s: invalid tunnel %u", __func__, tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100647 return (-1);
Damien Millerd27b9472005-12-13 19:29:02 +1100648 }
Damien Miller7b58e802005-12-13 19:33:19 +1100649
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;
Damien Millera1d9a182006-01-02 23:41:21 +1100664
665 /* Set interface mode */
666 ifr.ifr_flags &= ~IFF_UP;
667 if (mode == SSH_TUNMODE_ETHERNET)
Damien Miller7b58e802005-12-13 19:33:19 +1100668 ifr.ifr_flags |= IFF_LINK0;
Damien Millera1d9a182006-01-02 23:41:21 +1100669 else
670 ifr.ifr_flags &= ~IFF_LINK0;
671 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
672 goto failed;
673
674 /* Bring interface up */
Damien Miller7b58e802005-12-13 19:33:19 +1100675 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));
Damien Millerd27b9472005-12-13 19:29:02 +1100689 return (-1);
Damien Miller62a31c92005-12-13 20:44:13 +1100690#else
691 error("Tunnel interfaces are not supported on this platform");
692 return (-1);
693#endif
Damien Millerd27b9472005-12-13 19:29:02 +1100694}
695
Darren Tuckerce321d82005-10-03 18:11:24 +1000696void
697sanitise_stdfd(void)
698{
Damien Miller72c5b7d2006-01-06 14:50:44 +1100699 int nullfd, dupfd;
Darren Tuckerce321d82005-10-03 18:11:24 +1000700
Damien Miller72c5b7d2006-01-06 14:50:44 +1100701 if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
Darren Tuckerce321d82005-10-03 18:11:24 +1000702 fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno));
703 exit(1);
704 }
Damien Miller72c5b7d2006-01-06 14:50:44 +1100705 while (++dupfd <= 2) {
706 /* Only clobber closed fds */
707 if (fcntl(dupfd, F_GETFL, 0) >= 0)
708 continue;
709 if (dup2(nullfd, dupfd) == -1) {
Darren Tuckerce321d82005-10-03 18:11:24 +1000710 fprintf(stderr, "dup2: %s", strerror(errno));
711 exit(1);
712 }
Darren Tuckerce321d82005-10-03 18:11:24 +1000713 }
714 if (nullfd > 2)
715 close(nullfd);
716}
717
Damien Miller13390022005-07-06 09:44:19 +1000718char *
Damien Miller3f941882006-03-31 23:13:02 +1100719tohex(const void *vp, size_t l)
Damien Miller13390022005-07-06 09:44:19 +1000720{
Damien Miller3f941882006-03-31 23:13:02 +1100721 const u_char *p = (const u_char *)vp;
Damien Miller13390022005-07-06 09:44:19 +1000722 char b[3], *r;
Damien Miller3f941882006-03-31 23:13:02 +1100723 size_t i, hl;
724
725 if (l > 65536)
726 return xstrdup("tohex: length > 65536");
Damien Miller13390022005-07-06 09:44:19 +1000727
728 hl = l * 2 + 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100729 r = xcalloc(1, hl);
Damien Miller13390022005-07-06 09:44:19 +1000730 for (i = 0; i < l; i++) {
Damien Miller3f941882006-03-31 23:13:02 +1100731 snprintf(b, sizeof(b), "%02x", p[i]);
Damien Miller13390022005-07-06 09:44:19 +1000732 strlcat(r, b, hl);
733 }
734 return (r);
735}
736
Damien Miller3f941882006-03-31 23:13:02 +1100737u_int64_t
738get_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
755u_int32_t
756get_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
769u_int16_t
770get_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
781void
782put_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
796void
797put_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
808void
809put_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}