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