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