blob: c251139f09066b61b8859764becc7dbdbb188ef5 [file] [log] [blame]
Damien Millere7a1e5c2006-08-05 11:34:19 +10001/* $OpenBSD: misc.c,v 1.62 2006/07/26 13:57:17 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>
Damien Miller8dbffe72006-08-05 11:02:17 +100032#include <sys/param.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100033
Darren Tucker5d196262006-07-12 22:15:16 +100034#include <stdarg.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100035#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100036#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100037#include <unistd.h>
Darren Tucker5d196262006-07-12 22:15:16 +100038
Damien Miller8ec8c3e2006-07-10 20:35:38 +100039#include <netinet/in.h>
Damien Miller3a4051e2006-03-15 11:19:42 +110040#include <netinet/tcp.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100041
Darren Tucker39972492006-07-12 22:22:46 +100042#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100043#include <fcntl.h>
Damien Miller03e20032006-03-15 11:16:59 +110044#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110045# include <paths.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100046#include <pwd.h>
Damien Miller03e20032006-03-15 11:16:59 +110047#endif
Damien Miller3beb8522006-01-02 23:40:10 +110048#ifdef SSH_TUN_OPENBSD
49#include <net/if.h>
50#endif
Damien Miller61c51502000-08-18 14:01:04 +100051
Kevin Stevesb6e773a2001-02-04 13:20:36 +000052#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000053#include "log.h"
Ben Lindstrom06909012001-03-05 06:09:31 +000054#include "xmalloc.h"
Darren Tuckerda345532006-07-10 23:04:19 +100055#include "ssh.h"
Damien Miller61c51502000-08-18 14:01:04 +100056
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000057/* remove newline at end of string */
Damien Miller61c51502000-08-18 14:01:04 +100058char *
59chop(char *s)
60{
61 char *t = s;
62 while (*t) {
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +000063 if (*t == '\n' || *t == '\r') {
Damien Miller61c51502000-08-18 14:01:04 +100064 *t = '\0';
65 return s;
66 }
67 t++;
68 }
69 return s;
70
71}
72
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000073/* set/unset filedescriptor to non-blocking */
Damien Miller232711f2004-06-15 10:35:30 +100074int
Damien Miller61c51502000-08-18 14:01:04 +100075set_nonblock(int fd)
76{
77 int val;
Ben Lindstromc93e84c2001-05-12 00:08:37 +000078
Damien Miller61c51502000-08-18 14:01:04 +100079 val = fcntl(fd, F_GETFL, 0);
80 if (val < 0) {
81 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +100082 return (-1);
Damien Miller61c51502000-08-18 14:01:04 +100083 }
Damien Miller69b69aa2000-10-28 14:19:58 +110084 if (val & O_NONBLOCK) {
Damien Miller232711f2004-06-15 10:35:30 +100085 debug3("fd %d is O_NONBLOCK", fd);
86 return (0);
Damien Miller69b69aa2000-10-28 14:19:58 +110087 }
Damien Milleref095ce2003-05-14 13:41:39 +100088 debug2("fd %d setting O_NONBLOCK", fd);
Damien Miller61c51502000-08-18 14:01:04 +100089 val |= O_NONBLOCK;
Damien Miller232711f2004-06-15 10:35:30 +100090 if (fcntl(fd, F_SETFL, val) == -1) {
91 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd,
92 strerror(errno));
93 return (-1);
94 }
95 return (0);
Damien Miller61c51502000-08-18 14:01:04 +100096}
97
Damien Miller232711f2004-06-15 10:35:30 +100098int
Ben Lindstromc93e84c2001-05-12 00:08:37 +000099unset_nonblock(int fd)
100{
101 int val;
102
103 val = fcntl(fd, F_GETFL, 0);
104 if (val < 0) {
105 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +1000106 return (-1);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000107 }
108 if (!(val & O_NONBLOCK)) {
Damien Miller232711f2004-06-15 10:35:30 +1000109 debug3("fd %d is not O_NONBLOCK", fd);
110 return (0);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000111 }
Ben Lindstrom352b1c22001-06-21 03:04:37 +0000112 debug("fd %d clearing O_NONBLOCK", fd);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000113 val &= ~O_NONBLOCK;
Damien Miller232711f2004-06-15 10:35:30 +1000114 if (fcntl(fd, F_SETFL, val) == -1) {
115 debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s",
Ben Lindstrom84fcb312002-03-05 01:48:09 +0000116 fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +1000117 return (-1);
118 }
119 return (0);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000120}
121
Damien Miller398e1cf2002-02-05 11:52:13 +1100122/* disable nagle on socket */
123void
124set_nodelay(int fd)
125{
Ben Lindstrome86de512002-03-05 01:28:14 +0000126 int opt;
127 socklen_t optlen;
Damien Miller398e1cf2002-02-05 11:52:13 +1100128
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000129 optlen = sizeof opt;
130 if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
Darren Tucker6db8f932003-11-03 20:07:14 +1100131 debug("getsockopt TCP_NODELAY: %.100s", strerror(errno));
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000132 return;
133 }
134 if (opt == 1) {
135 debug2("fd %d is TCP_NODELAY", fd);
136 return;
137 }
138 opt = 1;
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000139 debug2("fd %d setting TCP_NODELAY", fd);
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000140 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
Damien Miller398e1cf2002-02-05 11:52:13 +1100141 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
142}
143
Damien Miller61c51502000-08-18 14:01:04 +1000144/* Characters considered whitespace in strsep calls. */
145#define WHITESPACE " \t\r\n"
Damien Miller306d1182006-03-15 12:05:59 +1100146#define QUOTE "\""
Damien Miller61c51502000-08-18 14:01:04 +1000147
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000148/* return next token in configuration line */
Damien Miller61c51502000-08-18 14:01:04 +1000149char *
150strdelim(char **s)
151{
152 char *old;
153 int wspace = 0;
154
155 if (*s == NULL)
156 return NULL;
157
158 old = *s;
159
Damien Miller306d1182006-03-15 12:05:59 +1100160 *s = strpbrk(*s, WHITESPACE QUOTE "=");
Damien Miller61c51502000-08-18 14:01:04 +1000161 if (*s == NULL)
162 return (old);
163
Damien Miller306d1182006-03-15 12:05:59 +1100164 if (*s[0] == '\"') {
165 memmove(*s, *s + 1, strlen(*s)); /* move nul too */
166 /* Find matching quote */
167 if ((*s = strpbrk(*s, QUOTE)) == NULL) {
168 return (NULL); /* no matching quote */
169 } else {
170 *s[0] = '\0';
171 return (old);
172 }
173 }
174
Damien Miller61c51502000-08-18 14:01:04 +1000175 /* Allow only one '=' to be skipped */
176 if (*s[0] == '=')
177 wspace = 1;
178 *s[0] = '\0';
179
Damien Miller306d1182006-03-15 12:05:59 +1100180 /* Skip any extra whitespace after first token */
Damien Miller61c51502000-08-18 14:01:04 +1000181 *s += strspn(*s + 1, WHITESPACE) + 1;
182 if (*s[0] == '=' && !wspace)
183 *s += strspn(*s + 1, WHITESPACE) + 1;
184
185 return (old);
186}
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000187
Ben Lindstrom086cf212001-03-05 05:56:40 +0000188struct passwd *
189pwcopy(struct passwd *pw)
190{
Damien Miller07d86be2006-03-26 14:19:21 +1100191 struct passwd *copy = xcalloc(1, sizeof(*copy));
Ben Lindstrom40304422001-03-05 06:22:01 +0000192
Ben Lindstrom086cf212001-03-05 05:56:40 +0000193 copy->pw_name = xstrdup(pw->pw_name);
194 copy->pw_passwd = xstrdup(pw->pw_passwd);
Ben Lindstrom40304422001-03-05 06:22:01 +0000195 copy->pw_gecos = xstrdup(pw->pw_gecos);
Ben Lindstrom086cf212001-03-05 05:56:40 +0000196 copy->pw_uid = pw->pw_uid;
197 copy->pw_gid = pw->pw_gid;
Kevin Steves82456952001-06-22 21:14:18 +0000198#ifdef HAVE_PW_EXPIRE_IN_PASSWD
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000199 copy->pw_expire = pw->pw_expire;
Kevin Steves82456952001-06-22 21:14:18 +0000200#endif
201#ifdef HAVE_PW_CHANGE_IN_PASSWD
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000202 copy->pw_change = pw->pw_change;
Kevin Steves82456952001-06-22 21:14:18 +0000203#endif
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000204#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom086cf212001-03-05 05:56:40 +0000205 copy->pw_class = xstrdup(pw->pw_class);
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000206#endif
Ben Lindstrom086cf212001-03-05 05:56:40 +0000207 copy->pw_dir = xstrdup(pw->pw_dir);
208 copy->pw_shell = xstrdup(pw->pw_shell);
209 return copy;
210}
211
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000212/*
213 * Convert ASCII string to TCP/IP port number.
214 * Port must be >0 and <=65535.
215 * Return 0 if invalid.
216 */
217int
218a2port(const char *s)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000219{
220 long port;
221 char *endp;
222
223 errno = 0;
224 port = strtol(s, &endp, 0);
225 if (s == endp || *endp != '\0' ||
226 (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
227 port <= 0 || port > 65535)
228 return 0;
229
230 return port;
231}
232
Damien Millerd27b9472005-12-13 19:29:02 +1100233int
234a2tun(const char *s, int *remote)
235{
236 const char *errstr = NULL;
237 char *sp, *ep;
238 int tun;
239
240 if (remote != NULL) {
Damien Miller7b58e802005-12-13 19:33:19 +1100241 *remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +1100242 sp = xstrdup(s);
243 if ((ep = strchr(sp, ':')) == NULL) {
244 xfree(sp);
245 return (a2tun(s, NULL));
246 }
247 ep[0] = '\0'; ep++;
248 *remote = a2tun(ep, NULL);
249 tun = a2tun(sp, NULL);
250 xfree(sp);
Damien Miller7b58e802005-12-13 19:33:19 +1100251 return (*remote == SSH_TUNID_ERR ? *remote : tun);
Damien Millerd27b9472005-12-13 19:29:02 +1100252 }
253
254 if (strcasecmp(s, "any") == 0)
Damien Miller7b58e802005-12-13 19:33:19 +1100255 return (SSH_TUNID_ANY);
Damien Millerd27b9472005-12-13 19:29:02 +1100256
Damien Miller7b58e802005-12-13 19:33:19 +1100257 tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr);
258 if (errstr != NULL)
259 return (SSH_TUNID_ERR);
Damien Millerd27b9472005-12-13 19:29:02 +1100260
261 return (tun);
262}
263
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000264#define SECONDS 1
265#define MINUTES (SECONDS * 60)
266#define HOURS (MINUTES * 60)
267#define DAYS (HOURS * 24)
268#define WEEKS (DAYS * 7)
269
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000270/*
271 * Convert a time string into seconds; format is
272 * a sequence of:
273 * time[qualifier]
274 *
275 * Valid time qualifiers are:
276 * <none> seconds
277 * s|S seconds
278 * m|M minutes
279 * h|H hours
280 * d|D days
281 * w|W weeks
282 *
283 * Examples:
284 * 90m 90 minutes
285 * 1h30m 90 minutes
286 * 2d 2 days
287 * 1w 1 week
288 *
289 * Return -1 if time string is invalid.
290 */
291long
292convtime(const char *s)
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000293{
294 long total, secs;
295 const char *p;
296 char *endp;
297
298 errno = 0;
299 total = 0;
300 p = s;
301
302 if (p == NULL || *p == '\0')
303 return -1;
304
305 while (*p) {
306 secs = strtol(p, &endp, 10);
307 if (p == endp ||
308 (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
309 secs < 0)
310 return -1;
311
312 switch (*endp++) {
313 case '\0':
314 endp--;
Damien Miller69b72032006-03-26 14:02:35 +1100315 break;
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000316 case 's':
317 case 'S':
318 break;
319 case 'm':
320 case 'M':
321 secs *= MINUTES;
322 break;
323 case 'h':
324 case 'H':
325 secs *= HOURS;
326 break;
327 case 'd':
328 case 'D':
329 secs *= DAYS;
330 break;
331 case 'w':
332 case 'W':
333 secs *= WEEKS;
334 break;
335 default:
336 return -1;
337 }
338 total += secs;
339 if (total < 0)
340 return -1;
341 p = endp;
342 }
343
344 return total;
345}
346
Damien Millerf91ee4c2005-03-01 21:24:33 +1100347/*
Darren Tuckerda345532006-07-10 23:04:19 +1000348 * Returns a standardized host+port identifier string.
349 * Caller must free returned string.
350 */
351char *
352put_host_port(const char *host, u_short port)
353{
354 char *hoststr;
355
356 if (port == 0 || port == SSH_DEFAULT_PORT)
357 return(xstrdup(host));
358 if (asprintf(&hoststr, "[%s]:%d", host, (int)port) < 0)
359 fatal("put_host_port: asprintf: %s", strerror(errno));
360 debug3("put_host_port: %s", hoststr);
361 return hoststr;
362}
363
364/*
Damien Millerf91ee4c2005-03-01 21:24:33 +1100365 * Search for next delimiter between hostnames/addresses and ports.
366 * Argument may be modified (for termination).
367 * Returns *cp if parsing succeeds.
368 * *cp is set to the start of the next delimiter, if one was found.
369 * If this is the last field, *cp is set to NULL.
370 */
371char *
372hpdelim(char **cp)
373{
374 char *s, *old;
375
376 if (cp == NULL || *cp == NULL)
377 return NULL;
378
379 old = s = *cp;
380 if (*s == '[') {
381 if ((s = strchr(s, ']')) == NULL)
382 return NULL;
383 else
384 s++;
385 } else if ((s = strpbrk(s, ":/")) == NULL)
386 s = *cp + strlen(*cp); /* skip to end (see first case below) */
387
388 switch (*s) {
389 case '\0':
390 *cp = NULL; /* no more fields*/
391 break;
Darren Tucker47eede72005-03-14 23:08:12 +1100392
Damien Millerf91ee4c2005-03-01 21:24:33 +1100393 case ':':
394 case '/':
395 *s = '\0'; /* terminate */
396 *cp = s + 1;
397 break;
Darren Tucker47eede72005-03-14 23:08:12 +1100398
Damien Millerf91ee4c2005-03-01 21:24:33 +1100399 default:
400 return NULL;
401 }
402
403 return old;
404}
405
Ben Lindstrom4529b702001-05-03 23:39:53 +0000406char *
407cleanhostname(char *host)
408{
409 if (*host == '[' && host[strlen(host) - 1] == ']') {
410 host[strlen(host) - 1] = '\0';
411 return (host + 1);
412 } else
413 return host;
414}
415
416char *
417colon(char *cp)
418{
419 int flag = 0;
420
421 if (*cp == ':') /* Leading colon is part of file name. */
422 return (0);
423 if (*cp == '[')
424 flag = 1;
425
426 for (; *cp; ++cp) {
427 if (*cp == '@' && *(cp+1) == '[')
428 flag = 1;
429 if (*cp == ']' && *(cp+1) == ':' && flag)
430 return (cp+1);
431 if (*cp == ':' && !flag)
432 return (cp);
433 if (*cp == '/')
434 return (0);
435 }
436 return (0);
437}
438
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000439/* function to assist building execv() arguments */
Ben Lindstrom387c4722001-05-08 20:27:25 +0000440void
441addargs(arglist *args, char *fmt, ...)
442{
443 va_list ap;
Damien Miller3eec6b72006-01-31 21:49:27 +1100444 char *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000445 u_int nalloc;
Damien Miller3eec6b72006-01-31 21:49:27 +1100446 int r;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000447
448 va_start(ap, fmt);
Damien Miller3eec6b72006-01-31 21:49:27 +1100449 r = vasprintf(&cp, fmt, ap);
Ben Lindstrom387c4722001-05-08 20:27:25 +0000450 va_end(ap);
Damien Miller3eec6b72006-01-31 21:49:27 +1100451 if (r == -1)
452 fatal("addargs: argument too long");
Ben Lindstrom387c4722001-05-08 20:27:25 +0000453
Darren Tuckerfb16b242003-09-22 21:04:23 +1000454 nalloc = args->nalloc;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000455 if (args->list == NULL) {
Darren Tuckerfb16b242003-09-22 21:04:23 +1000456 nalloc = 32;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000457 args->num = 0;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000458 } else if (args->num+2 >= nalloc)
459 nalloc *= 2;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000460
Damien Miller36812092006-03-26 14:22:47 +1100461 args->list = xrealloc(args->list, nalloc, sizeof(char *));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000462 args->nalloc = nalloc;
Damien Miller3eec6b72006-01-31 21:49:27 +1100463 args->list[args->num++] = cp;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000464 args->list[args->num] = NULL;
465}
Darren Tucker22cc7412004-12-06 22:47:41 +1100466
Damien Miller3eec6b72006-01-31 21:49:27 +1100467void
468replacearg(arglist *args, u_int which, char *fmt, ...)
469{
470 va_list ap;
471 char *cp;
472 int r;
473
474 va_start(ap, fmt);
475 r = vasprintf(&cp, fmt, ap);
476 va_end(ap);
477 if (r == -1)
478 fatal("replacearg: argument too long");
479
480 if (which >= args->num)
481 fatal("replacearg: tried to replace invalid arg %d >= %d",
482 which, args->num);
483 xfree(args->list[which]);
484 args->list[which] = cp;
485}
486
487void
488freeargs(arglist *args)
489{
490 u_int i;
491
492 if (args->list != NULL) {
493 for (i = 0; i < args->num; i++)
494 xfree(args->list[i]);
495 xfree(args->list);
496 args->nalloc = args->num = 0;
497 args->list = NULL;
498 }
499}
500
Darren Tucker22cc7412004-12-06 22:47:41 +1100501/*
Damien Miller5fd38c02005-05-26 12:02:14 +1000502 * Expands tildes in the file name. Returns data allocated by xmalloc.
503 * Warning: this calls getpw*.
504 */
505char *
506tilde_expand_filename(const char *filename, uid_t uid)
507{
508 const char *path;
509 char user[128], ret[MAXPATHLEN];
510 struct passwd *pw;
Damien Millereccb9de2005-06-17 12:59:34 +1000511 u_int len, slash;
Damien Miller5fd38c02005-05-26 12:02:14 +1000512
513 if (*filename != '~')
514 return (xstrdup(filename));
515 filename++;
516
517 path = strchr(filename, '/');
518 if (path != NULL && path > filename) { /* ~user/path */
Damien Millereccb9de2005-06-17 12:59:34 +1000519 slash = path - filename;
520 if (slash > sizeof(user) - 1)
Damien Miller5fd38c02005-05-26 12:02:14 +1000521 fatal("tilde_expand_filename: ~username too long");
Damien Millereccb9de2005-06-17 12:59:34 +1000522 memcpy(user, filename, slash);
523 user[slash] = '\0';
Damien Miller5fd38c02005-05-26 12:02:14 +1000524 if ((pw = getpwnam(user)) == NULL)
525 fatal("tilde_expand_filename: No such user %s", user);
526 } else if ((pw = getpwuid(uid)) == NULL) /* ~/path */
527 fatal("tilde_expand_filename: No such uid %d", uid);
528
529 if (strlcpy(ret, pw->pw_dir, sizeof(ret)) >= sizeof(ret))
530 fatal("tilde_expand_filename: Path too long");
531
532 /* Make sure directory has a trailing '/' */
533 len = strlen(pw->pw_dir);
534 if ((len == 0 || pw->pw_dir[len - 1] != '/') &&
535 strlcat(ret, "/", sizeof(ret)) >= sizeof(ret))
536 fatal("tilde_expand_filename: Path too long");
537
538 /* Skip leading '/' from specified path */
539 if (path != NULL)
540 filename = path + 1;
541 if (strlcat(ret, filename, sizeof(ret)) >= sizeof(ret))
542 fatal("tilde_expand_filename: Path too long");
543
544 return (xstrdup(ret));
545}
546
547/*
Damien Miller6476cad2005-06-16 13:18:34 +1000548 * Expand a string with a set of %[char] escapes. A number of escapes may be
549 * specified as (char *escape_chars, char *replacement) pairs. The list must
Darren Tuckerbee73d52005-07-14 17:05:02 +1000550 * be terminated by a NULL escape_char. Returns replaced string in memory
Damien Miller6476cad2005-06-16 13:18:34 +1000551 * allocated by xmalloc.
552 */
553char *
554percent_expand(const char *string, ...)
555{
556#define EXPAND_MAX_KEYS 16
557 struct {
558 const char *key;
559 const char *repl;
560 } keys[EXPAND_MAX_KEYS];
Damien Millereccb9de2005-06-17 12:59:34 +1000561 u_int num_keys, i, j;
Damien Miller6476cad2005-06-16 13:18:34 +1000562 char buf[4096];
563 va_list ap;
564
565 /* Gather keys */
566 va_start(ap, string);
567 for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) {
568 keys[num_keys].key = va_arg(ap, char *);
569 if (keys[num_keys].key == NULL)
570 break;
571 keys[num_keys].repl = va_arg(ap, char *);
572 if (keys[num_keys].repl == NULL)
573 fatal("percent_expand: NULL replacement");
574 }
575 va_end(ap);
576
577 if (num_keys >= EXPAND_MAX_KEYS)
578 fatal("percent_expand: too many keys");
579
580 /* Expand string */
581 *buf = '\0';
582 for (i = 0; *string != '\0'; string++) {
583 if (*string != '%') {
584 append:
585 buf[i++] = *string;
586 if (i >= sizeof(buf))
587 fatal("percent_expand: string too long");
588 buf[i] = '\0';
589 continue;
590 }
591 string++;
592 if (*string == '%')
593 goto append;
594 for (j = 0; j < num_keys; j++) {
595 if (strchr(keys[j].key, *string) != NULL) {
596 i = strlcat(buf, keys[j].repl, sizeof(buf));
597 if (i >= sizeof(buf))
598 fatal("percent_expand: string too long");
599 break;
600 }
601 }
602 if (j >= num_keys)
603 fatal("percent_expand: unknown key %%%c", *string);
604 }
605 return (xstrdup(buf));
606#undef EXPAND_MAX_KEYS
607}
608
609/*
Darren Tucker22cc7412004-12-06 22:47:41 +1100610 * Read an entire line from a public key file into a static buffer, discarding
611 * lines that exceed the buffer size. Returns 0 on success, -1 on failure.
612 */
613int
614read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
Darren Tuckerf0f90982004-12-11 13:39:50 +1100615 u_long *lineno)
Darren Tucker22cc7412004-12-06 22:47:41 +1100616{
617 while (fgets(buf, bufsz, f) != NULL) {
618 (*lineno)++;
619 if (buf[strlen(buf) - 1] == '\n' || feof(f)) {
620 return 0;
621 } else {
Darren Tuckerf0f90982004-12-11 13:39:50 +1100622 debug("%s: %s line %lu exceeds size limit", __func__,
623 filename, *lineno);
Darren Tucker22cc7412004-12-06 22:47:41 +1100624 /* discard remainder of line */
Darren Tucker47eede72005-03-14 23:08:12 +1100625 while (fgetc(f) != '\n' && !feof(f))
Darren Tucker22cc7412004-12-06 22:47:41 +1100626 ; /* nothing */
627 }
628 }
629 return -1;
630}
Damien Miller13390022005-07-06 09:44:19 +1000631
Damien Millerd27b9472005-12-13 19:29:02 +1100632int
Damien Miller7b58e802005-12-13 19:33:19 +1100633tun_open(int tun, int mode)
Damien Millerd27b9472005-12-13 19:29:02 +1100634{
Damien Miller62a31c92005-12-13 20:44:13 +1100635#if defined(CUSTOM_SYS_TUN_OPEN)
636 return (sys_tun_open(tun, mode));
Damien Miller2dcddbf2006-01-01 19:47:05 +1100637#elif defined(SSH_TUN_OPENBSD)
Damien Miller7b58e802005-12-13 19:33:19 +1100638 struct ifreq ifr;
Damien Millerd27b9472005-12-13 19:29:02 +1100639 char name[100];
Damien Miller7b58e802005-12-13 19:33:19 +1100640 int fd = -1, sock;
Damien Millerd27b9472005-12-13 19:29:02 +1100641
Damien Miller7b58e802005-12-13 19:33:19 +1100642 /* Open the tunnel device */
643 if (tun <= SSH_TUNID_MAX) {
Damien Millerd27b9472005-12-13 19:29:02 +1100644 snprintf(name, sizeof(name), "/dev/tun%d", tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100645 fd = open(name, O_RDWR);
646 } else if (tun == SSH_TUNID_ANY) {
647 for (tun = 100; tun >= 0; tun--) {
648 snprintf(name, sizeof(name), "/dev/tun%d", tun);
649 if ((fd = open(name, O_RDWR)) >= 0)
650 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100651 }
652 } else {
Damien Millera210d522006-01-02 23:40:30 +1100653 debug("%s: invalid tunnel %u", __func__, tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100654 return (-1);
Damien Millerd27b9472005-12-13 19:29:02 +1100655 }
Damien Miller7b58e802005-12-13 19:33:19 +1100656
657 if (fd < 0) {
658 debug("%s: %s open failed: %s", __func__, name, strerror(errno));
659 return (-1);
660 }
661
662 debug("%s: %s mode %d fd %d", __func__, name, mode, fd);
663
664 /* Set the tunnel device operation mode */
665 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tun%d", tun);
666 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
667 goto failed;
668
669 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)
670 goto failed;
Damien Millera1d9a182006-01-02 23:41:21 +1100671
672 /* Set interface mode */
673 ifr.ifr_flags &= ~IFF_UP;
674 if (mode == SSH_TUNMODE_ETHERNET)
Damien Miller7b58e802005-12-13 19:33:19 +1100675 ifr.ifr_flags |= IFF_LINK0;
Damien Millera1d9a182006-01-02 23:41:21 +1100676 else
677 ifr.ifr_flags &= ~IFF_LINK0;
678 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
679 goto failed;
680
681 /* Bring interface up */
Damien Miller7b58e802005-12-13 19:33:19 +1100682 ifr.ifr_flags |= IFF_UP;
683 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
684 goto failed;
685
686 close(sock);
687 return (fd);
688
689 failed:
690 if (fd >= 0)
691 close(fd);
692 if (sock >= 0)
693 close(sock);
694 debug("%s: failed to set %s mode %d: %s", __func__, name,
695 mode, strerror(errno));
Damien Millerd27b9472005-12-13 19:29:02 +1100696 return (-1);
Damien Miller62a31c92005-12-13 20:44:13 +1100697#else
698 error("Tunnel interfaces are not supported on this platform");
699 return (-1);
700#endif
Damien Millerd27b9472005-12-13 19:29:02 +1100701}
702
Darren Tuckerce321d82005-10-03 18:11:24 +1000703void
704sanitise_stdfd(void)
705{
Damien Miller72c5b7d2006-01-06 14:50:44 +1100706 int nullfd, dupfd;
Darren Tuckerce321d82005-10-03 18:11:24 +1000707
Damien Miller72c5b7d2006-01-06 14:50:44 +1100708 if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
Darren Tuckerce321d82005-10-03 18:11:24 +1000709 fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno));
710 exit(1);
711 }
Damien Miller72c5b7d2006-01-06 14:50:44 +1100712 while (++dupfd <= 2) {
713 /* Only clobber closed fds */
714 if (fcntl(dupfd, F_GETFL, 0) >= 0)
715 continue;
716 if (dup2(nullfd, dupfd) == -1) {
Darren Tuckerce321d82005-10-03 18:11:24 +1000717 fprintf(stderr, "dup2: %s", strerror(errno));
718 exit(1);
719 }
Darren Tuckerce321d82005-10-03 18:11:24 +1000720 }
721 if (nullfd > 2)
722 close(nullfd);
723}
724
Damien Miller13390022005-07-06 09:44:19 +1000725char *
Damien Miller3f941882006-03-31 23:13:02 +1100726tohex(const void *vp, size_t l)
Damien Miller13390022005-07-06 09:44:19 +1000727{
Damien Miller3f941882006-03-31 23:13:02 +1100728 const u_char *p = (const u_char *)vp;
Damien Miller13390022005-07-06 09:44:19 +1000729 char b[3], *r;
Damien Miller3f941882006-03-31 23:13:02 +1100730 size_t i, hl;
731
732 if (l > 65536)
733 return xstrdup("tohex: length > 65536");
Damien Miller13390022005-07-06 09:44:19 +1000734
735 hl = l * 2 + 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100736 r = xcalloc(1, hl);
Damien Miller13390022005-07-06 09:44:19 +1000737 for (i = 0; i < l; i++) {
Damien Miller3f941882006-03-31 23:13:02 +1100738 snprintf(b, sizeof(b), "%02x", p[i]);
Damien Miller13390022005-07-06 09:44:19 +1000739 strlcat(r, b, hl);
740 }
741 return (r);
742}
743
Damien Miller3f941882006-03-31 23:13:02 +1100744u_int64_t
745get_u64(const void *vp)
746{
747 const u_char *p = (const u_char *)vp;
748 u_int64_t v;
749
750 v = (u_int64_t)p[0] << 56;
751 v |= (u_int64_t)p[1] << 48;
752 v |= (u_int64_t)p[2] << 40;
753 v |= (u_int64_t)p[3] << 32;
754 v |= (u_int64_t)p[4] << 24;
755 v |= (u_int64_t)p[5] << 16;
756 v |= (u_int64_t)p[6] << 8;
757 v |= (u_int64_t)p[7];
758
759 return (v);
760}
761
762u_int32_t
763get_u32(const void *vp)
764{
765 const u_char *p = (const u_char *)vp;
766 u_int32_t v;
767
768 v = (u_int32_t)p[0] << 24;
769 v |= (u_int32_t)p[1] << 16;
770 v |= (u_int32_t)p[2] << 8;
771 v |= (u_int32_t)p[3];
772
773 return (v);
774}
775
776u_int16_t
777get_u16(const void *vp)
778{
779 const u_char *p = (const u_char *)vp;
780 u_int16_t v;
781
782 v = (u_int16_t)p[0] << 8;
783 v |= (u_int16_t)p[1];
784
785 return (v);
786}
787
788void
789put_u64(void *vp, u_int64_t v)
790{
791 u_char *p = (u_char *)vp;
792
793 p[0] = (u_char)(v >> 56) & 0xff;
794 p[1] = (u_char)(v >> 48) & 0xff;
795 p[2] = (u_char)(v >> 40) & 0xff;
796 p[3] = (u_char)(v >> 32) & 0xff;
797 p[4] = (u_char)(v >> 24) & 0xff;
798 p[5] = (u_char)(v >> 16) & 0xff;
799 p[6] = (u_char)(v >> 8) & 0xff;
800 p[7] = (u_char)v & 0xff;
801}
802
803void
804put_u32(void *vp, u_int32_t v)
805{
806 u_char *p = (u_char *)vp;
807
808 p[0] = (u_char)(v >> 24) & 0xff;
809 p[1] = (u_char)(v >> 16) & 0xff;
810 p[2] = (u_char)(v >> 8) & 0xff;
811 p[3] = (u_char)v & 0xff;
812}
813
814
815void
816put_u16(void *vp, u_int16_t v)
817{
818 u_char *p = (u_char *)vp;
819
820 p[0] = (u_char)(v >> 8) & 0xff;
821 p[1] = (u_char)v & 0xff;
822}