]> andersk Git - openssh.git/blame - misc.c
- stevesk@cvs.openbsd.org 2006/07/26 02:35:17
[openssh.git] / misc.c
CommitLineData
536c14e8 1/* $OpenBSD: misc.c,v 1.61 2006/07/26 02:35:17 stevesk Exp $ */
bcbf86ec 2/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
51e7a012 4 * Copyright (c) 2005,2006 Damien Miller. All rights reserved.
bcbf86ec 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
6d24277f 27#include "includes.h"
bd7c69ea 28
29#include <sys/ioctl.h>
b1842393 30#include <sys/types.h>
9794d008 31#include <sys/socket.h>
536c14e8 32#include <sys/param.h>
9794d008 33
4c72fcfd 34#include <stdarg.h>
00146caa 35#include <string.h>
5188ba17 36#include <unistd.h>
4c72fcfd 37
9794d008 38#include <netinet/in.h>
6425cf65 39#include <netinet/tcp.h>
9794d008 40
028094f4 41#include <errno.h>
d3221cca 42#include <fcntl.h>
a75f5360 43#ifdef HAVE_PATHS_H
6f61e0ec 44# include <paths.h>
b1842393 45#include <pwd.h>
a75f5360 46#endif
1f1fbbd8 47#ifdef SSH_TUN_OPENBSD
48#include <net/if.h>
49#endif
6d24277f 50
1aa00dcb 51#include "misc.h"
42f11eb2 52#include "log.h"
4ab21f86 53#include "xmalloc.h"
d4530052 54#include "ssh.h"
6d24277f 55
255cabd9 56/* remove newline at end of string */
6d24277f 57char *
58chop(char *s)
59{
60 char *t = s;
61 while (*t) {
6aacefa7 62 if (*t == '\n' || *t == '\r') {
6d24277f 63 *t = '\0';
64 return s;
65 }
66 t++;
67 }
68 return s;
69
70}
71
255cabd9 72/* set/unset filedescriptor to non-blocking */
170694d7 73int
6d24277f 74set_nonblock(int fd)
75{
76 int val;
89aa792b 77
6d24277f 78 val = fcntl(fd, F_GETFL, 0);
79 if (val < 0) {
80 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
170694d7 81 return (-1);
6d24277f 82 }
a22aff1f 83 if (val & O_NONBLOCK) {
170694d7 84 debug3("fd %d is O_NONBLOCK", fd);
85 return (0);
a22aff1f 86 }
45c42d58 87 debug2("fd %d setting O_NONBLOCK", fd);
6d24277f 88 val |= O_NONBLOCK;
170694d7 89 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);
6d24277f 95}
96
170694d7 97int
89aa792b 98unset_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));
170694d7 105 return (-1);
89aa792b 106 }
107 if (!(val & O_NONBLOCK)) {
170694d7 108 debug3("fd %d is not O_NONBLOCK", fd);
109 return (0);
89aa792b 110 }
e04e7a19 111 debug("fd %d clearing O_NONBLOCK", fd);
89aa792b 112 val &= ~O_NONBLOCK;
170694d7 113 if (fcntl(fd, F_SETFL, val) == -1) {
114 debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s",
c92ec40b 115 fd, strerror(errno));
170694d7 116 return (-1);
117 }
118 return (0);
89aa792b 119}
120
bcc0381e 121/* disable nagle on socket */
122void
123set_nodelay(int fd)
124{
3c05447a 125 int opt;
126 socklen_t optlen;
bcc0381e 127
811a6342 128 optlen = sizeof opt;
129 if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
f7fb35fe 130 debug("getsockopt TCP_NODELAY: %.100s", strerror(errno));
811a6342 131 return;
132 }
133 if (opt == 1) {
134 debug2("fd %d is TCP_NODELAY", fd);
135 return;
136 }
137 opt = 1;
6226a8f8 138 debug2("fd %d setting TCP_NODELAY", fd);
811a6342 139 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
bcc0381e 140 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
141}
142
6d24277f 143/* Characters considered whitespace in strsep calls. */
144#define WHITESPACE " \t\r\n"
533b9133 145#define QUOTE "\""
6d24277f 146
255cabd9 147/* return next token in configuration line */
6d24277f 148char *
149strdelim(char **s)
150{
151 char *old;
152 int wspace = 0;
153
154 if (*s == NULL)
155 return NULL;
156
157 old = *s;
158
533b9133 159 *s = strpbrk(*s, WHITESPACE QUOTE "=");
6d24277f 160 if (*s == NULL)
161 return (old);
162
533b9133 163 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
6d24277f 174 /* Allow only one '=' to be skipped */
175 if (*s[0] == '=')
176 wspace = 1;
177 *s[0] = '\0';
178
533b9133 179 /* Skip any extra whitespace after first token */
6d24277f 180 *s += strspn(*s + 1, WHITESPACE) + 1;
181 if (*s[0] == '=' && !wspace)
182 *s += strspn(*s + 1, WHITESPACE) + 1;
183
184 return (old);
185}
1aa00dcb 186
3b1a83df 187struct passwd *
188pwcopy(struct passwd *pw)
189{
52e3daed 190 struct passwd *copy = xcalloc(1, sizeof(*copy));
5649fbbe 191
3b1a83df 192 copy->pw_name = xstrdup(pw->pw_name);
193 copy->pw_passwd = xstrdup(pw->pw_passwd);
5649fbbe 194 copy->pw_gecos = xstrdup(pw->pw_gecos);
3b1a83df 195 copy->pw_uid = pw->pw_uid;
196 copy->pw_gid = pw->pw_gid;
7751d4eb 197#ifdef HAVE_PW_EXPIRE_IN_PASSWD
c4d49b85 198 copy->pw_expire = pw->pw_expire;
7751d4eb 199#endif
200#ifdef HAVE_PW_CHANGE_IN_PASSWD
c4d49b85 201 copy->pw_change = pw->pw_change;
7751d4eb 202#endif
2605addd 203#ifdef HAVE_PW_CLASS_IN_PASSWD
3b1a83df 204 copy->pw_class = xstrdup(pw->pw_class);
2605addd 205#endif
3b1a83df 206 copy->pw_dir = xstrdup(pw->pw_dir);
207 copy->pw_shell = xstrdup(pw->pw_shell);
208 return copy;
209}
210
255cabd9 211/*
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)
2d2a2c65 218{
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
d20f3c9e 232int
233a2tun(const char *s, int *remote)
234{
235 const char *errstr = NULL;
236 char *sp, *ep;
237 int tun;
238
239 if (remote != NULL) {
a4f24bf8 240 *remote = SSH_TUNID_ANY;
d20f3c9e 241 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);
a4f24bf8 250 return (*remote == SSH_TUNID_ERR ? *remote : tun);
d20f3c9e 251 }
252
253 if (strcasecmp(s, "any") == 0)
a4f24bf8 254 return (SSH_TUNID_ANY);
d20f3c9e 255
a4f24bf8 256 tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr);
257 if (errstr != NULL)
258 return (SSH_TUNID_ERR);
d20f3c9e 259
260 return (tun);
261}
262
e2b1fb42 263#define SECONDS 1
264#define MINUTES (SECONDS * 60)
265#define HOURS (MINUTES * 60)
266#define DAYS (HOURS * 24)
267#define WEEKS (DAYS * 7)
268
255cabd9 269/*
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)
e2b1fb42 292{
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--;
5ef36928 314 break;
e2b1fb42 315 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
d4530052 346/*
347 * 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
3867aa0a 363/*
364 * 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;
f8cc7664 391
3867aa0a 392 case ':':
393 case '/':
394 *s = '\0'; /* terminate */
395 *cp = s + 1;
396 break;
f8cc7664 397
3867aa0a 398 default:
399 return NULL;
400 }
401
402 return old;
403}
404
1fcde3fe 405char *
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
255cabd9 438/* function to assist building execv() arguments */
8a624ebf 439void
440addargs(arglist *args, char *fmt, ...)
441{
442 va_list ap;
4116f5c0 443 char *cp;
9a995072 444 u_int nalloc;
4116f5c0 445 int r;
8a624ebf 446
447 va_start(ap, fmt);
4116f5c0 448 r = vasprintf(&cp, fmt, ap);
8a624ebf 449 va_end(ap);
4116f5c0 450 if (r == -1)
451 fatal("addargs: argument too long");
8a624ebf 452
c46e584f 453 nalloc = args->nalloc;
8a624ebf 454 if (args->list == NULL) {
c46e584f 455 nalloc = 32;
8a624ebf 456 args->num = 0;
c46e584f 457 } else if (args->num+2 >= nalloc)
458 nalloc *= 2;
8a624ebf 459
c5d10563 460 args->list = xrealloc(args->list, nalloc, sizeof(char *));
c46e584f 461 args->nalloc = nalloc;
4116f5c0 462 args->list[args->num++] = cp;
8a624ebf 463 args->list[args->num] = NULL;
464}
ea067773 465
4116f5c0 466void
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
49e71137 500/*
501 * 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;
2ceb8101 510 u_int len, slash;
49e71137 511
512 if (*filename != '~')
513 return (xstrdup(filename));
514 filename++;
515
516 path = strchr(filename, '/');
517 if (path != NULL && path > filename) { /* ~user/path */
2ceb8101 518 slash = path - filename;
519 if (slash > sizeof(user) - 1)
49e71137 520 fatal("tilde_expand_filename: ~username too long");
2ceb8101 521 memcpy(user, filename, slash);
522 user[slash] = '\0';
49e71137 523 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
a980cbd7 546/*
547 * 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
6381acf0 549 * be terminated by a NULL escape_char. Returns replaced string in memory
a980cbd7 550 * 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];
2ceb8101 560 u_int num_keys, i, j;
a980cbd7 561 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
ea067773 608/*
609 * 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,
41feb690 614 u_long *lineno)
ea067773 615{
616 while (fgets(buf, bufsz, f) != NULL) {
617 (*lineno)++;
618 if (buf[strlen(buf) - 1] == '\n' || feof(f)) {
619 return 0;
620 } else {
41feb690 621 debug("%s: %s line %lu exceeds size limit", __func__,
622 filename, *lineno);
ea067773 623 /* discard remainder of line */
f8cc7664 624 while (fgetc(f) != '\n' && !feof(f))
ea067773 625 ; /* nothing */
626 }
627 }
628 return -1;
629}
ef07103c 630
d20f3c9e 631int
a4f24bf8 632tun_open(int tun, int mode)
d20f3c9e 633{
6306853a 634#if defined(CUSTOM_SYS_TUN_OPEN)
635 return (sys_tun_open(tun, mode));
0f6cb079 636#elif defined(SSH_TUN_OPENBSD)
a4f24bf8 637 struct ifreq ifr;
d20f3c9e 638 char name[100];
a4f24bf8 639 int fd = -1, sock;
d20f3c9e 640
a4f24bf8 641 /* Open the tunnel device */
642 if (tun <= SSH_TUNID_MAX) {
d20f3c9e 643 snprintf(name, sizeof(name), "/dev/tun%d", tun);
a4f24bf8 644 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;
d20f3c9e 650 }
651 } else {
81c042a3 652 debug("%s: invalid tunnel %u", __func__, tun);
a4f24bf8 653 return (-1);
654 }
655
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;
b1b65311 670
671 /* Set interface mode */
672 ifr.ifr_flags &= ~IFF_UP;
673 if (mode == SSH_TUNMODE_ETHERNET)
a4f24bf8 674 ifr.ifr_flags |= IFF_LINK0;
b1b65311 675 else
676 ifr.ifr_flags &= ~IFF_LINK0;
677 if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
678 goto failed;
679
680 /* Bring interface up */
a4f24bf8 681 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));
d20f3c9e 695 return (-1);
6306853a 696#else
697 error("Tunnel interfaces are not supported on this platform");
698 return (-1);
699#endif
d20f3c9e 700}
701
fd6168c1 702void
703sanitise_stdfd(void)
704{
db382686 705 int nullfd, dupfd;
fd6168c1 706
db382686 707 if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
fd6168c1 708 fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno));
709 exit(1);
710 }
db382686 711 while (++dupfd <= 2) {
712 /* Only clobber closed fds */
713 if (fcntl(dupfd, F_GETFL, 0) >= 0)
714 continue;
715 if (dup2(nullfd, dupfd) == -1) {
fd6168c1 716 fprintf(stderr, "dup2: %s", strerror(errno));
717 exit(1);
718 }
fd6168c1 719 }
720 if (nullfd > 2)
721 close(nullfd);
722}
723
ef07103c 724char *
51e7a012 725tohex(const void *vp, size_t l)
ef07103c 726{
51e7a012 727 const u_char *p = (const u_char *)vp;
ef07103c 728 char b[3], *r;
51e7a012 729 size_t i, hl;
730
731 if (l > 65536)
732 return xstrdup("tohex: length > 65536");
ef07103c 733
734 hl = l * 2 + 1;
52e3daed 735 r = xcalloc(1, hl);
ef07103c 736 for (i = 0; i < l; i++) {
51e7a012 737 snprintf(b, sizeof(b), "%02x", p[i]);
ef07103c 738 strlcat(r, b, hl);
739 }
740 return (r);
741}
742
51e7a012 743u_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}
This page took 0.326858 seconds and 5 git commands to generate.