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