blob: fc3fcca44b8c523a7eed02cd538872fba1f832db [file] [log] [blame]
Damien Miller8dbffe72006-08-05 11:02:17 +10001/* $OpenBSD: misc.c,v 1.61 2006/07/26 02:35: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 Millere3476ed2006-07-24 14:13:33 +100035#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100036#include <unistd.h>
Darren Tucker5d196262006-07-12 22:15:16 +100037
Damien Miller8ec8c3e2006-07-10 20:35:38 +100038#include <netinet/in.h>
Damien Miller3a4051e2006-03-15 11:19:42 +110039#include <netinet/tcp.h>
Damien Miller8ec8c3e2006-07-10 20:35:38 +100040
Darren Tucker39972492006-07-12 22:22:46 +100041#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100042#include <fcntl.h>
Damien Miller03e20032006-03-15 11:16:59 +110043#ifdef HAVE_PATHS_H
Damien Millera9263d02006-03-15 11:18:26 +110044# include <paths.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100045#include <pwd.h>
Damien Miller03e20032006-03-15 11:16:59 +110046#endif
Damien Miller3beb8522006-01-02 23:40:10 +110047#ifdef SSH_TUN_OPENBSD
48#include <net/if.h>
49#endif
Damien Miller61c51502000-08-18 14:01:04 +100050
Kevin Stevesb6e773a2001-02-04 13:20:36 +000051#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000052#include "log.h"
Ben Lindstrom06909012001-03-05 06:09:31 +000053#include "xmalloc.h"
Darren Tuckerda345532006-07-10 23:04:19 +100054#include "ssh.h"
Damien Miller61c51502000-08-18 14:01:04 +100055
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000056/* remove newline at end of string */
Damien Miller61c51502000-08-18 14:01:04 +100057char *
58chop(char *s)
59{
60 char *t = s;
61 while (*t) {
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +000062 if (*t == '\n' || *t == '\r') {
Damien Miller61c51502000-08-18 14:01:04 +100063 *t = '\0';
64 return s;
65 }
66 t++;
67 }
68 return s;
69
70}
71
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000072/* set/unset filedescriptor to non-blocking */
Damien Miller232711f2004-06-15 10:35:30 +100073int
Damien Miller61c51502000-08-18 14:01:04 +100074set_nonblock(int fd)
75{
76 int val;
Ben Lindstromc93e84c2001-05-12 00:08:37 +000077
Damien Miller61c51502000-08-18 14:01:04 +100078 val = fcntl(fd, F_GETFL, 0);
79 if (val < 0) {
80 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +100081 return (-1);
Damien Miller61c51502000-08-18 14:01:04 +100082 }
Damien Miller69b69aa2000-10-28 14:19:58 +110083 if (val & O_NONBLOCK) {
Damien Miller232711f2004-06-15 10:35:30 +100084 debug3("fd %d is O_NONBLOCK", fd);
85 return (0);
Damien Miller69b69aa2000-10-28 14:19:58 +110086 }
Damien Milleref095ce2003-05-14 13:41:39 +100087 debug2("fd %d setting O_NONBLOCK", fd);
Damien Miller61c51502000-08-18 14:01:04 +100088 val |= O_NONBLOCK;
Damien Miller232711f2004-06-15 10:35:30 +100089 if (fcntl(fd, F_SETFL, val) == -1) {
90 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd,
91 strerror(errno));
92 return (-1);
93 }
94 return (0);
Damien Miller61c51502000-08-18 14:01:04 +100095}
96
Damien Miller232711f2004-06-15 10:35:30 +100097int
Ben Lindstromc93e84c2001-05-12 00:08:37 +000098unset_nonblock(int fd)
99{
100 int val;
101
102 val = fcntl(fd, F_GETFL, 0);
103 if (val < 0) {
104 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +1000105 return (-1);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000106 }
107 if (!(val & O_NONBLOCK)) {
Damien Miller232711f2004-06-15 10:35:30 +1000108 debug3("fd %d is not O_NONBLOCK", fd);
109 return (0);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000110 }
Ben Lindstrom352b1c22001-06-21 03:04:37 +0000111 debug("fd %d clearing O_NONBLOCK", fd);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000112 val &= ~O_NONBLOCK;
Damien Miller232711f2004-06-15 10:35:30 +1000113 if (fcntl(fd, F_SETFL, val) == -1) {
114 debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s",
Ben Lindstrom84fcb312002-03-05 01:48:09 +0000115 fd, strerror(errno));
Damien Miller232711f2004-06-15 10:35:30 +1000116 return (-1);
117 }
118 return (0);
Ben Lindstromc93e84c2001-05-12 00:08:37 +0000119}
120
Damien Miller398e1cf2002-02-05 11:52:13 +1100121/* disable nagle on socket */
122void
123set_nodelay(int fd)
124{
Ben Lindstrome86de512002-03-05 01:28:14 +0000125 int opt;
126 socklen_t optlen;
Damien Miller398e1cf2002-02-05 11:52:13 +1100127
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000128 optlen = sizeof opt;
129 if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
Darren Tucker6db8f932003-11-03 20:07:14 +1100130 debug("getsockopt TCP_NODELAY: %.100s", strerror(errno));
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000131 return;
132 }
133 if (opt == 1) {
134 debug2("fd %d is TCP_NODELAY", fd);
135 return;
136 }
137 opt = 1;
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000138 debug2("fd %d setting TCP_NODELAY", fd);
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000139 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
Damien Miller398e1cf2002-02-05 11:52:13 +1100140 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
141}
142
Damien Miller61c51502000-08-18 14:01:04 +1000143/* Characters considered whitespace in strsep calls. */
144#define WHITESPACE " \t\r\n"
Damien Miller306d1182006-03-15 12:05:59 +1100145#define QUOTE "\""
Damien Miller61c51502000-08-18 14:01:04 +1000146
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000147/* return next token in configuration line */
Damien Miller61c51502000-08-18 14:01:04 +1000148char *
149strdelim(char **s)
150{
151 char *old;
152 int wspace = 0;
153
154 if (*s == NULL)
155 return NULL;
156
157 old = *s;
158
Damien Miller306d1182006-03-15 12:05:59 +1100159 *s = strpbrk(*s, WHITESPACE QUOTE "=");
Damien Miller61c51502000-08-18 14:01:04 +1000160 if (*s == NULL)
161 return (old);
162
Damien Miller306d1182006-03-15 12:05:59 +1100163 if (*s[0] == '\"') {
164 memmove(*s, *s + 1, strlen(*s)); /* move nul too */
165 /* Find matching quote */
166 if ((*s = strpbrk(*s, QUOTE)) == NULL) {
167 return (NULL); /* no matching quote */
168 } else {
169 *s[0] = '\0';
170 return (old);
171 }
172 }
173
Damien Miller61c51502000-08-18 14:01:04 +1000174 /* Allow only one '=' to be skipped */
175 if (*s[0] == '=')
176 wspace = 1;
177 *s[0] = '\0';
178
Damien Miller306d1182006-03-15 12:05:59 +1100179 /* Skip any extra whitespace after first token */
Damien Miller61c51502000-08-18 14:01:04 +1000180 *s += strspn(*s + 1, WHITESPACE) + 1;
181 if (*s[0] == '=' && !wspace)
182 *s += strspn(*s + 1, WHITESPACE) + 1;
183
184 return (old);
185}
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000186
Ben Lindstrom086cf212001-03-05 05:56:40 +0000187struct passwd *
188pwcopy(struct passwd *pw)
189{
Damien Miller07d86be2006-03-26 14:19:21 +1100190 struct passwd *copy = xcalloc(1, sizeof(*copy));
Ben Lindstrom40304422001-03-05 06:22:01 +0000191
Ben Lindstrom086cf212001-03-05 05:56:40 +0000192 copy->pw_name = xstrdup(pw->pw_name);
193 copy->pw_passwd = xstrdup(pw->pw_passwd);
Ben Lindstrom40304422001-03-05 06:22:01 +0000194 copy->pw_gecos = xstrdup(pw->pw_gecos);
Ben Lindstrom086cf212001-03-05 05:56:40 +0000195 copy->pw_uid = pw->pw_uid;
196 copy->pw_gid = pw->pw_gid;
Kevin Steves82456952001-06-22 21:14:18 +0000197#ifdef HAVE_PW_EXPIRE_IN_PASSWD
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000198 copy->pw_expire = pw->pw_expire;
Kevin Steves82456952001-06-22 21:14:18 +0000199#endif
200#ifdef HAVE_PW_CHANGE_IN_PASSWD
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000201 copy->pw_change = pw->pw_change;
Kevin Steves82456952001-06-22 21:14:18 +0000202#endif
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000203#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom086cf212001-03-05 05:56:40 +0000204 copy->pw_class = xstrdup(pw->pw_class);
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000205#endif
Ben Lindstrom086cf212001-03-05 05:56:40 +0000206 copy->pw_dir = xstrdup(pw->pw_dir);
207 copy->pw_shell = xstrdup(pw->pw_shell);
208 return copy;
209}
210
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000211/*
212 * Convert ASCII string to TCP/IP port number.
213 * Port must be >0 and <=65535.
214 * Return 0 if invalid.
215 */
216int
217a2port(const char *s)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000218{
219 long port;
220 char *endp;
221
222 errno = 0;
223 port = strtol(s, &endp, 0);
224 if (s == endp || *endp != '\0' ||
225 (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
226 port <= 0 || port > 65535)
227 return 0;
228
229 return port;
230}
231
Damien Millerd27b9472005-12-13 19:29:02 +1100232int
233a2tun(const char *s, int *remote)
234{
235 const char *errstr = NULL;
236 char *sp, *ep;
237 int tun;
238
239 if (remote != NULL) {
Damien Miller7b58e802005-12-13 19:33:19 +1100240 *remote = SSH_TUNID_ANY;
Damien Millerd27b9472005-12-13 19:29:02 +1100241 sp = xstrdup(s);
242 if ((ep = strchr(sp, ':')) == NULL) {
243 xfree(sp);
244 return (a2tun(s, NULL));
245 }
246 ep[0] = '\0'; ep++;
247 *remote = a2tun(ep, NULL);
248 tun = a2tun(sp, NULL);
249 xfree(sp);
Damien Miller7b58e802005-12-13 19:33:19 +1100250 return (*remote == SSH_TUNID_ERR ? *remote : tun);
Damien Millerd27b9472005-12-13 19:29:02 +1100251 }
252
253 if (strcasecmp(s, "any") == 0)
Damien Miller7b58e802005-12-13 19:33:19 +1100254 return (SSH_TUNID_ANY);
Damien Millerd27b9472005-12-13 19:29:02 +1100255
Damien Miller7b58e802005-12-13 19:33:19 +1100256 tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr);
257 if (errstr != NULL)
258 return (SSH_TUNID_ERR);
Damien Millerd27b9472005-12-13 19:29:02 +1100259
260 return (tun);
261}
262
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000263#define SECONDS 1
264#define MINUTES (SECONDS * 60)
265#define HOURS (MINUTES * 60)
266#define DAYS (HOURS * 24)
267#define WEEKS (DAYS * 7)
268
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000269/*
270 * Convert a time string into seconds; format is
271 * a sequence of:
272 * time[qualifier]
273 *
274 * Valid time qualifiers are:
275 * <none> seconds
276 * s|S seconds
277 * m|M minutes
278 * h|H hours
279 * d|D days
280 * w|W weeks
281 *
282 * Examples:
283 * 90m 90 minutes
284 * 1h30m 90 minutes
285 * 2d 2 days
286 * 1w 1 week
287 *
288 * Return -1 if time string is invalid.
289 */
290long
291convtime(const char *s)
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000292{
293 long total, secs;
294 const char *p;
295 char *endp;
296
297 errno = 0;
298 total = 0;
299 p = s;
300
301 if (p == NULL || *p == '\0')
302 return -1;
303
304 while (*p) {
305 secs = strtol(p, &endp, 10);
306 if (p == endp ||
307 (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
308 secs < 0)
309 return -1;
310
311 switch (*endp++) {
312 case '\0':
313 endp--;
Damien Miller69b72032006-03-26 14:02:35 +1100314 break;
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000315 case 's':
316 case 'S':
317 break;
318 case 'm':
319 case 'M':
320 secs *= MINUTES;
321 break;
322 case 'h':
323 case 'H':
324 secs *= HOURS;
325 break;
326 case 'd':
327 case 'D':
328 secs *= DAYS;
329 break;
330 case 'w':
331 case 'W':
332 secs *= WEEKS;
333 break;
334 default:
335 return -1;
336 }
337 total += secs;
338 if (total < 0)
339 return -1;
340 p = endp;
341 }
342
343 return total;
344}
345
Damien Millerf91ee4c2005-03-01 21:24:33 +1100346/*
Darren Tuckerda345532006-07-10 23:04:19 +1000347 * Returns a standardized host+port identifier string.
348 * Caller must free returned string.
349 */
350char *
351put_host_port(const char *host, u_short port)
352{
353 char *hoststr;
354
355 if (port == 0 || port == SSH_DEFAULT_PORT)
356 return(xstrdup(host));
357 if (asprintf(&hoststr, "[%s]:%d", host, (int)port) < 0)
358 fatal("put_host_port: asprintf: %s", strerror(errno));
359 debug3("put_host_port: %s", hoststr);
360 return hoststr;
361}
362
363/*
Damien Millerf91ee4c2005-03-01 21:24:33 +1100364 * Search for next delimiter between hostnames/addresses and ports.
365 * Argument may be modified (for termination).
366 * Returns *cp if parsing succeeds.
367 * *cp is set to the start of the next delimiter, if one was found.
368 * If this is the last field, *cp is set to NULL.
369 */
370char *
371hpdelim(char **cp)
372{
373 char *s, *old;
374
375 if (cp == NULL || *cp == NULL)
376 return NULL;
377
378 old = s = *cp;
379 if (*s == '[') {
380 if ((s = strchr(s, ']')) == NULL)
381 return NULL;
382 else
383 s++;
384 } else if ((s = strpbrk(s, ":/")) == NULL)
385 s = *cp + strlen(*cp); /* skip to end (see first case below) */
386
387 switch (*s) {
388 case '\0':
389 *cp = NULL; /* no more fields*/
390 break;
Darren Tucker47eede72005-03-14 23:08:12 +1100391
Damien Millerf91ee4c2005-03-01 21:24:33 +1100392 case ':':
393 case '/':
394 *s = '\0'; /* terminate */
395 *cp = s + 1;
396 break;
Darren Tucker47eede72005-03-14 23:08:12 +1100397
Damien Millerf91ee4c2005-03-01 21:24:33 +1100398 default:
399 return NULL;
400 }
401
402 return old;
403}
404
Ben Lindstrom4529b702001-05-03 23:39:53 +0000405char *
406cleanhostname(char *host)
407{
408 if (*host == '[' && host[strlen(host) - 1] == ']') {
409 host[strlen(host) - 1] = '\0';
410 return (host + 1);
411 } else
412 return host;
413}
414
415char *
416colon(char *cp)
417{
418 int flag = 0;
419
420 if (*cp == ':') /* Leading colon is part of file name. */
421 return (0);
422 if (*cp == '[')
423 flag = 1;
424
425 for (; *cp; ++cp) {
426 if (*cp == '@' && *(cp+1) == '[')
427 flag = 1;
428 if (*cp == ']' && *(cp+1) == ':' && flag)
429 return (cp+1);
430 if (*cp == ':' && !flag)
431 return (cp);
432 if (*cp == '/')
433 return (0);
434 }
435 return (0);
436}
437
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000438/* function to assist building execv() arguments */
Ben Lindstrom387c4722001-05-08 20:27:25 +0000439void
440addargs(arglist *args, char *fmt, ...)
441{
442 va_list ap;
Damien Miller3eec6b72006-01-31 21:49:27 +1100443 char *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000444 u_int nalloc;
Damien Miller3eec6b72006-01-31 21:49:27 +1100445 int r;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000446
447 va_start(ap, fmt);
Damien Miller3eec6b72006-01-31 21:49:27 +1100448 r = vasprintf(&cp, fmt, ap);
Ben Lindstrom387c4722001-05-08 20:27:25 +0000449 va_end(ap);
Damien Miller3eec6b72006-01-31 21:49:27 +1100450 if (r == -1)
451 fatal("addargs: argument too long");
Ben Lindstrom387c4722001-05-08 20:27:25 +0000452
Darren Tuckerfb16b242003-09-22 21:04:23 +1000453 nalloc = args->nalloc;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000454 if (args->list == NULL) {
Darren Tuckerfb16b242003-09-22 21:04:23 +1000455 nalloc = 32;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000456 args->num = 0;
Darren Tuckerfb16b242003-09-22 21:04:23 +1000457 } else if (args->num+2 >= nalloc)
458 nalloc *= 2;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000459
Damien Miller36812092006-03-26 14:22:47 +1100460 args->list = xrealloc(args->list, nalloc, sizeof(char *));
Darren Tuckerfb16b242003-09-22 21:04:23 +1000461 args->nalloc = nalloc;
Damien Miller3eec6b72006-01-31 21:49:27 +1100462 args->list[args->num++] = cp;
Ben Lindstrom387c4722001-05-08 20:27:25 +0000463 args->list[args->num] = NULL;
464}
Darren Tucker22cc7412004-12-06 22:47:41 +1100465
Damien Miller3eec6b72006-01-31 21:49:27 +1100466void
467replacearg(arglist *args, u_int which, char *fmt, ...)
468{
469 va_list ap;
470 char *cp;
471 int r;
472
473 va_start(ap, fmt);
474 r = vasprintf(&cp, fmt, ap);
475 va_end(ap);
476 if (r == -1)
477 fatal("replacearg: argument too long");
478
479 if (which >= args->num)
480 fatal("replacearg: tried to replace invalid arg %d >= %d",
481 which, args->num);
482 xfree(args->list[which]);
483 args->list[which] = cp;
484}
485
486void
487freeargs(arglist *args)
488{
489 u_int i;
490
491 if (args->list != NULL) {
492 for (i = 0; i < args->num; i++)
493 xfree(args->list[i]);
494 xfree(args->list);
495 args->nalloc = args->num = 0;
496 args->list = NULL;
497 }
498}
499
Darren Tucker22cc7412004-12-06 22:47:41 +1100500/*
Damien Miller5fd38c02005-05-26 12:02:14 +1000501 * Expands tildes in the file name. Returns data allocated by xmalloc.
502 * Warning: this calls getpw*.
503 */
504char *
505tilde_expand_filename(const char *filename, uid_t uid)
506{
507 const char *path;
508 char user[128], ret[MAXPATHLEN];
509 struct passwd *pw;
Damien Millereccb9de2005-06-17 12:59:34 +1000510 u_int len, slash;
Damien Miller5fd38c02005-05-26 12:02:14 +1000511
512 if (*filename != '~')
513 return (xstrdup(filename));
514 filename++;
515
516 path = strchr(filename, '/');
517 if (path != NULL && path > filename) { /* ~user/path */
Damien Millereccb9de2005-06-17 12:59:34 +1000518 slash = path - filename;
519 if (slash > sizeof(user) - 1)
Damien Miller5fd38c02005-05-26 12:02:14 +1000520 fatal("tilde_expand_filename: ~username too long");
Damien Millereccb9de2005-06-17 12:59:34 +1000521 memcpy(user, filename, slash);
522 user[slash] = '\0';
Damien Miller5fd38c02005-05-26 12:02:14 +1000523 if ((pw = getpwnam(user)) == NULL)
524 fatal("tilde_expand_filename: No such user %s", user);
525 } else if ((pw = getpwuid(uid)) == NULL) /* ~/path */
526 fatal("tilde_expand_filename: No such uid %d", uid);
527
528 if (strlcpy(ret, pw->pw_dir, sizeof(ret)) >= sizeof(ret))
529 fatal("tilde_expand_filename: Path too long");
530
531 /* Make sure directory has a trailing '/' */
532 len = strlen(pw->pw_dir);
533 if ((len == 0 || pw->pw_dir[len - 1] != '/') &&
534 strlcat(ret, "/", sizeof(ret)) >= sizeof(ret))
535 fatal("tilde_expand_filename: Path too long");
536
537 /* Skip leading '/' from specified path */
538 if (path != NULL)
539 filename = path + 1;
540 if (strlcat(ret, filename, sizeof(ret)) >= sizeof(ret))
541 fatal("tilde_expand_filename: Path too long");
542
543 return (xstrdup(ret));
544}
545
546/*
Damien Miller6476cad2005-06-16 13:18:34 +1000547 * Expand a string with a set of %[char] escapes. A number of escapes may be
548 * specified as (char *escape_chars, char *replacement) pairs. The list must
Darren Tuckerbee73d52005-07-14 17:05:02 +1000549 * be terminated by a NULL escape_char. Returns replaced string in memory
Damien Miller6476cad2005-06-16 13:18:34 +1000550 * allocated by xmalloc.
551 */
552char *
553percent_expand(const char *string, ...)
554{
555#define EXPAND_MAX_KEYS 16
556 struct {
557 const char *key;
558 const char *repl;
559 } keys[EXPAND_MAX_KEYS];
Damien Millereccb9de2005-06-17 12:59:34 +1000560 u_int num_keys, i, j;
Damien Miller6476cad2005-06-16 13:18:34 +1000561 char buf[4096];
562 va_list ap;
563
564 /* Gather keys */
565 va_start(ap, string);
566 for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) {
567 keys[num_keys].key = va_arg(ap, char *);
568 if (keys[num_keys].key == NULL)
569 break;
570 keys[num_keys].repl = va_arg(ap, char *);
571 if (keys[num_keys].repl == NULL)
572 fatal("percent_expand: NULL replacement");
573 }
574 va_end(ap);
575
576 if (num_keys >= EXPAND_MAX_KEYS)
577 fatal("percent_expand: too many keys");
578
579 /* Expand string */
580 *buf = '\0';
581 for (i = 0; *string != '\0'; string++) {
582 if (*string != '%') {
583 append:
584 buf[i++] = *string;
585 if (i >= sizeof(buf))
586 fatal("percent_expand: string too long");
587 buf[i] = '\0';
588 continue;
589 }
590 string++;
591 if (*string == '%')
592 goto append;
593 for (j = 0; j < num_keys; j++) {
594 if (strchr(keys[j].key, *string) != NULL) {
595 i = strlcat(buf, keys[j].repl, sizeof(buf));
596 if (i >= sizeof(buf))
597 fatal("percent_expand: string too long");
598 break;
599 }
600 }
601 if (j >= num_keys)
602 fatal("percent_expand: unknown key %%%c", *string);
603 }
604 return (xstrdup(buf));
605#undef EXPAND_MAX_KEYS
606}
607
608/*
Darren Tucker22cc7412004-12-06 22:47:41 +1100609 * Read an entire line from a public key file into a static buffer, discarding
610 * lines that exceed the buffer size. Returns 0 on success, -1 on failure.
611 */
612int
613read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
Darren Tuckerf0f90982004-12-11 13:39:50 +1100614 u_long *lineno)
Darren Tucker22cc7412004-12-06 22:47:41 +1100615{
616 while (fgets(buf, bufsz, f) != NULL) {
617 (*lineno)++;
618 if (buf[strlen(buf) - 1] == '\n' || feof(f)) {
619 return 0;
620 } else {
Darren Tuckerf0f90982004-12-11 13:39:50 +1100621 debug("%s: %s line %lu exceeds size limit", __func__,
622 filename, *lineno);
Darren Tucker22cc7412004-12-06 22:47:41 +1100623 /* discard remainder of line */
Darren Tucker47eede72005-03-14 23:08:12 +1100624 while (fgetc(f) != '\n' && !feof(f))
Darren Tucker22cc7412004-12-06 22:47:41 +1100625 ; /* nothing */
626 }
627 }
628 return -1;
629}
Damien Miller13390022005-07-06 09:44:19 +1000630
Damien Millerd27b9472005-12-13 19:29:02 +1100631int
Damien Miller7b58e802005-12-13 19:33:19 +1100632tun_open(int tun, int mode)
Damien Millerd27b9472005-12-13 19:29:02 +1100633{
Damien Miller62a31c92005-12-13 20:44:13 +1100634#if defined(CUSTOM_SYS_TUN_OPEN)
635 return (sys_tun_open(tun, mode));
Damien Miller2dcddbf2006-01-01 19:47:05 +1100636#elif defined(SSH_TUN_OPENBSD)
Damien Miller7b58e802005-12-13 19:33:19 +1100637 struct ifreq ifr;
Damien Millerd27b9472005-12-13 19:29:02 +1100638 char name[100];
Damien Miller7b58e802005-12-13 19:33:19 +1100639 int fd = -1, sock;
Damien Millerd27b9472005-12-13 19:29:02 +1100640
Damien Miller7b58e802005-12-13 19:33:19 +1100641 /* Open the tunnel device */
642 if (tun <= SSH_TUNID_MAX) {
Damien Millerd27b9472005-12-13 19:29:02 +1100643 snprintf(name, sizeof(name), "/dev/tun%d", tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100644 fd = open(name, O_RDWR);
645 } else if (tun == SSH_TUNID_ANY) {
646 for (tun = 100; tun >= 0; tun--) {
647 snprintf(name, sizeof(name), "/dev/tun%d", tun);
648 if ((fd = open(name, O_RDWR)) >= 0)
649 break;
Damien Millerd27b9472005-12-13 19:29:02 +1100650 }
651 } else {
Damien Millera210d522006-01-02 23:40:30 +1100652 debug("%s: invalid tunnel %u", __func__, tun);
Damien Miller7b58e802005-12-13 19:33:19 +1100653 return (-1);
Damien Millerd27b9472005-12-13 19:29:02 +1100654 }
Damien Miller7b58e802005-12-13 19:33:19 +1100655
656 if (fd < 0) {
657 debug("%s: %s open failed: %s", __func__, name, strerror(errno));
658 return (-1);
659 }
660
661 debug("%s: %s mode %d fd %d", __func__, name, mode, fd);
662
663 /* Set the tunnel device operation mode */
664 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tun%d", tun);
665 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
666 goto failed;
667
668 if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)
669 goto failed;
Damien Millera1d9a182006-01-02 23:41:21 +1100670
671 /* Set interface mode */
672 ifr.ifr_flags &= ~IFF_UP;
673 if (mode == SSH_TUNMODE_ETHERNET)
Damien Miller7b58e802005-12-13 19:33:19 +1100674 ifr.ifr_flags |= IFF_LINK0;
Damien Millera1d9a182006-01-02 23:41:21 +1100675 else
676 ifr.ifr_flags &= ~IFF_LINK0;
677 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
678 goto failed;
679
680 /* Bring interface up */
Damien Miller7b58e802005-12-13 19:33:19 +1100681 ifr.ifr_flags |= IFF_UP;
682 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
683 goto failed;
684
685 close(sock);
686 return (fd);
687
688 failed:
689 if (fd >= 0)
690 close(fd);
691 if (sock >= 0)
692 close(sock);
693 debug("%s: failed to set %s mode %d: %s", __func__, name,
694 mode, strerror(errno));
Damien Millerd27b9472005-12-13 19:29:02 +1100695 return (-1);
Damien Miller62a31c92005-12-13 20:44:13 +1100696#else
697 error("Tunnel interfaces are not supported on this platform");
698 return (-1);
699#endif
Damien Millerd27b9472005-12-13 19:29:02 +1100700}
701
Darren Tuckerce321d82005-10-03 18:11:24 +1000702void
703sanitise_stdfd(void)
704{
Damien Miller72c5b7d2006-01-06 14:50:44 +1100705 int nullfd, dupfd;
Darren Tuckerce321d82005-10-03 18:11:24 +1000706
Damien Miller72c5b7d2006-01-06 14:50:44 +1100707 if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
Darren Tuckerce321d82005-10-03 18:11:24 +1000708 fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno));
709 exit(1);
710 }
Damien Miller72c5b7d2006-01-06 14:50:44 +1100711 while (++dupfd <= 2) {
712 /* Only clobber closed fds */
713 if (fcntl(dupfd, F_GETFL, 0) >= 0)
714 continue;
715 if (dup2(nullfd, dupfd) == -1) {
Darren Tuckerce321d82005-10-03 18:11:24 +1000716 fprintf(stderr, "dup2: %s", strerror(errno));
717 exit(1);
718 }
Darren Tuckerce321d82005-10-03 18:11:24 +1000719 }
720 if (nullfd > 2)
721 close(nullfd);
722}
723
Damien Miller13390022005-07-06 09:44:19 +1000724char *
Damien Miller3f941882006-03-31 23:13:02 +1100725tohex(const void *vp, size_t l)
Damien Miller13390022005-07-06 09:44:19 +1000726{
Damien Miller3f941882006-03-31 23:13:02 +1100727 const u_char *p = (const u_char *)vp;
Damien Miller13390022005-07-06 09:44:19 +1000728 char b[3], *r;
Damien Miller3f941882006-03-31 23:13:02 +1100729 size_t i, hl;
730
731 if (l > 65536)
732 return xstrdup("tohex: length > 65536");
Damien Miller13390022005-07-06 09:44:19 +1000733
734 hl = l * 2 + 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100735 r = xcalloc(1, hl);
Damien Miller13390022005-07-06 09:44:19 +1000736 for (i = 0; i < l; i++) {
Damien Miller3f941882006-03-31 23:13:02 +1100737 snprintf(b, sizeof(b), "%02x", p[i]);
Damien Miller13390022005-07-06 09:44:19 +1000738 strlcat(r, b, hl);
739 }
740 return (r);
741}
742
Damien Miller3f941882006-03-31 23:13:02 +1100743u_int64_t
744get_u64(const void *vp)
745{
746 const u_char *p = (const u_char *)vp;
747 u_int64_t v;
748
749 v = (u_int64_t)p[0] << 56;
750 v |= (u_int64_t)p[1] << 48;
751 v |= (u_int64_t)p[2] << 40;
752 v |= (u_int64_t)p[3] << 32;
753 v |= (u_int64_t)p[4] << 24;
754 v |= (u_int64_t)p[5] << 16;
755 v |= (u_int64_t)p[6] << 8;
756 v |= (u_int64_t)p[7];
757
758 return (v);
759}
760
761u_int32_t
762get_u32(const void *vp)
763{
764 const u_char *p = (const u_char *)vp;
765 u_int32_t v;
766
767 v = (u_int32_t)p[0] << 24;
768 v |= (u_int32_t)p[1] << 16;
769 v |= (u_int32_t)p[2] << 8;
770 v |= (u_int32_t)p[3];
771
772 return (v);
773}
774
775u_int16_t
776get_u16(const void *vp)
777{
778 const u_char *p = (const u_char *)vp;
779 u_int16_t v;
780
781 v = (u_int16_t)p[0] << 8;
782 v |= (u_int16_t)p[1];
783
784 return (v);
785}
786
787void
788put_u64(void *vp, u_int64_t v)
789{
790 u_char *p = (u_char *)vp;
791
792 p[0] = (u_char)(v >> 56) & 0xff;
793 p[1] = (u_char)(v >> 48) & 0xff;
794 p[2] = (u_char)(v >> 40) & 0xff;
795 p[3] = (u_char)(v >> 32) & 0xff;
796 p[4] = (u_char)(v >> 24) & 0xff;
797 p[5] = (u_char)(v >> 16) & 0xff;
798 p[6] = (u_char)(v >> 8) & 0xff;
799 p[7] = (u_char)v & 0xff;
800}
801
802void
803put_u32(void *vp, u_int32_t v)
804{
805 u_char *p = (u_char *)vp;
806
807 p[0] = (u_char)(v >> 24) & 0xff;
808 p[1] = (u_char)(v >> 16) & 0xff;
809 p[2] = (u_char)(v >> 8) & 0xff;
810 p[3] = (u_char)v & 0xff;
811}
812
813
814void
815put_u16(void *vp, u_int16_t v)
816{
817 u_char *p = (u_char *)vp;
818
819 p[0] = (u_char)(v >> 8) & 0xff;
820 p[1] = (u_char)v & 0xff;
821}