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