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