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