[BACK]Return to misc.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/misc.c, Revision 1.41

1.1       markus      1: /*
                      2:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
1.31      djm         3:  * Copyright (c) 2005 Damien Miller.  All rights reserved.
1.1       markus      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  *
                     14:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     15:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     16:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     17:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     18:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     19:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     20:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     21:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     22:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     23:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     24:  */
                     25:
                     26: #include "includes.h"
1.41    ! djm        27: RCSID("$OpenBSD: misc.c,v 1.40 2006/01/02 07:53:44 reyk Exp $");
1.38      stevesk    28:
                     29: #include <net/if.h>
1.1       markus     30:
                     31: #include "misc.h"
                     32: #include "log.h"
1.3       deraadt    33: #include "xmalloc.h"
1.1       markus     34:
1.12      markus     35: /* remove newline at end of string */
1.1       markus     36: char *
                     37: chop(char *s)
                     38: {
                     39:        char *t = s;
                     40:        while (*t) {
1.13      deraadt    41:                if (*t == '\n' || *t == '\r') {
1.1       markus     42:                        *t = '\0';
                     43:                        return s;
                     44:                }
                     45:                t++;
                     46:        }
                     47:        return s;
                     48:
                     49: }
                     50:
1.12      markus     51: /* set/unset filedescriptor to non-blocking */
1.24      djm        52: int
1.1       markus     53: set_nonblock(int fd)
                     54: {
                     55:        int val;
1.8       markus     56:
1.1       markus     57:        val = fcntl(fd, F_GETFL, 0);
                     58:        if (val < 0) {
                     59:                error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
1.24      djm        60:                return (-1);
1.1       markus     61:        }
                     62:        if (val & O_NONBLOCK) {
1.24      djm        63:                debug3("fd %d is O_NONBLOCK", fd);
                     64:                return (0);
1.1       markus     65:        }
1.21      markus     66:        debug2("fd %d setting O_NONBLOCK", fd);
1.1       markus     67:        val |= O_NONBLOCK;
1.24      djm        68:        if (fcntl(fd, F_SETFL, val) == -1) {
                     69:                debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd,
                     70:                    strerror(errno));
                     71:                return (-1);
                     72:        }
                     73:        return (0);
1.8       markus     74: }
                     75:
1.24      djm        76: int
1.8       markus     77: unset_nonblock(int fd)
                     78: {
                     79:        int val;
                     80:
                     81:        val = fcntl(fd, F_GETFL, 0);
                     82:        if (val < 0) {
                     83:                error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
1.24      djm        84:                return (-1);
1.8       markus     85:        }
                     86:        if (!(val & O_NONBLOCK)) {
1.24      djm        87:                debug3("fd %d is not O_NONBLOCK", fd);
                     88:                return (0);
1.8       markus     89:        }
1.10      markus     90:        debug("fd %d clearing O_NONBLOCK", fd);
1.8       markus     91:        val &= ~O_NONBLOCK;
1.24      djm        92:        if (fcntl(fd, F_SETFL, val) == -1) {
                     93:                debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s",
1.18      markus     94:                    fd, strerror(errno));
1.24      djm        95:                return (-1);
                     96:        }
                     97:        return (0);
1.15      stevesk    98: }
                     99:
                    100: /* disable nagle on socket */
                    101: void
                    102: set_nodelay(int fd)
                    103: {
1.17      stevesk   104:        int opt;
                    105:        socklen_t optlen;
1.15      stevesk   106:
1.16      stevesk   107:        optlen = sizeof opt;
                    108:        if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
1.23      markus    109:                debug("getsockopt TCP_NODELAY: %.100s", strerror(errno));
1.16      stevesk   110:                return;
                    111:        }
                    112:        if (opt == 1) {
                    113:                debug2("fd %d is TCP_NODELAY", fd);
                    114:                return;
                    115:        }
                    116:        opt = 1;
1.20      markus    117:        debug2("fd %d setting TCP_NODELAY", fd);
1.16      stevesk   118:        if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
1.15      stevesk   119:                error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
1.1       markus    120: }
                    121:
                    122: /* Characters considered whitespace in strsep calls. */
                    123: #define WHITESPACE " \t\r\n"
                    124:
1.12      markus    125: /* return next token in configuration line */
1.1       markus    126: char *
                    127: strdelim(char **s)
                    128: {
                    129:        char *old;
                    130:        int wspace = 0;
                    131:
                    132:        if (*s == NULL)
                    133:                return NULL;
                    134:
                    135:        old = *s;
                    136:
                    137:        *s = strpbrk(*s, WHITESPACE "=");
                    138:        if (*s == NULL)
                    139:                return (old);
                    140:
                    141:        /* Allow only one '=' to be skipped */
                    142:        if (*s[0] == '=')
                    143:                wspace = 1;
                    144:        *s[0] = '\0';
                    145:
                    146:        *s += strspn(*s + 1, WHITESPACE) + 1;
                    147:        if (*s[0] == '=' && !wspace)
                    148:                *s += strspn(*s + 1, WHITESPACE) + 1;
                    149:
                    150:        return (old);
1.2       markus    151: }
                    152:
                    153: struct passwd *
                    154: pwcopy(struct passwd *pw)
                    155: {
                    156:        struct passwd *copy = xmalloc(sizeof(*copy));
1.4       deraadt   157:
1.2       markus    158:        memset(copy, 0, sizeof(*copy));
                    159:        copy->pw_name = xstrdup(pw->pw_name);
                    160:        copy->pw_passwd = xstrdup(pw->pw_passwd);
1.4       deraadt   161:        copy->pw_gecos = xstrdup(pw->pw_gecos);
1.2       markus    162:        copy->pw_uid = pw->pw_uid;
                    163:        copy->pw_gid = pw->pw_gid;
1.11      markus    164:        copy->pw_expire = pw->pw_expire;
                    165:        copy->pw_change = pw->pw_change;
1.2       markus    166:        copy->pw_class = xstrdup(pw->pw_class);
                    167:        copy->pw_dir = xstrdup(pw->pw_dir);
                    168:        copy->pw_shell = xstrdup(pw->pw_shell);
                    169:        return copy;
1.5       stevesk   170: }
                    171:
1.12      markus    172: /*
                    173:  * Convert ASCII string to TCP/IP port number.
                    174:  * Port must be >0 and <=65535.
                    175:  * Return 0 if invalid.
                    176:  */
                    177: int
                    178: a2port(const char *s)
1.5       stevesk   179: {
                    180:        long port;
                    181:        char *endp;
                    182:
                    183:        errno = 0;
                    184:        port = strtol(s, &endp, 0);
                    185:        if (s == endp || *endp != '\0' ||
                    186:            (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
                    187:            port <= 0 || port > 65535)
                    188:                return 0;
                    189:
                    190:        return port;
1.9       stevesk   191: }
                    192:
1.36      reyk      193: int
                    194: a2tun(const char *s, int *remote)
                    195: {
                    196:        const char *errstr = NULL;
                    197:        char *sp, *ep;
                    198:        int tun;
                    199:
                    200:        if (remote != NULL) {
1.37      reyk      201:                *remote = SSH_TUNID_ANY;
1.36      reyk      202:                sp = xstrdup(s);
                    203:                if ((ep = strchr(sp, ':')) == NULL) {
                    204:                        xfree(sp);
                    205:                        return (a2tun(s, NULL));
                    206:                }
                    207:                ep[0] = '\0'; ep++;
                    208:                *remote = a2tun(ep, NULL);
                    209:                tun = a2tun(sp, NULL);
                    210:                xfree(sp);
1.37      reyk      211:                return (*remote == SSH_TUNID_ERR ? *remote : tun);
1.36      reyk      212:        }
                    213:
                    214:        if (strcasecmp(s, "any") == 0)
1.37      reyk      215:                return (SSH_TUNID_ANY);
1.36      reyk      216:
1.37      reyk      217:        tun = strtonum(s, 0, SSH_TUNID_MAX, &errstr);
                    218:        if (errstr != NULL)
                    219:                return (SSH_TUNID_ERR);
1.36      reyk      220:
                    221:        return (tun);
                    222: }
                    223:
1.9       stevesk   224: #define SECONDS                1
                    225: #define MINUTES                (SECONDS * 60)
                    226: #define HOURS          (MINUTES * 60)
                    227: #define DAYS           (HOURS * 24)
                    228: #define WEEKS          (DAYS * 7)
                    229:
1.12      markus    230: /*
                    231:  * Convert a time string into seconds; format is
                    232:  * a sequence of:
                    233:  *      time[qualifier]
                    234:  *
                    235:  * Valid time qualifiers are:
                    236:  *      <none>  seconds
                    237:  *      s|S     seconds
                    238:  *      m|M     minutes
                    239:  *      h|H     hours
                    240:  *      d|D     days
                    241:  *      w|W     weeks
                    242:  *
                    243:  * Examples:
                    244:  *      90m     90 minutes
                    245:  *      1h30m   90 minutes
                    246:  *      2d      2 days
                    247:  *      1w      1 week
                    248:  *
                    249:  * Return -1 if time string is invalid.
                    250:  */
                    251: long
                    252: convtime(const char *s)
1.9       stevesk   253: {
                    254:        long total, secs;
                    255:        const char *p;
                    256:        char *endp;
                    257:
                    258:        errno = 0;
                    259:        total = 0;
                    260:        p = s;
                    261:
                    262:        if (p == NULL || *p == '\0')
                    263:                return -1;
                    264:
                    265:        while (*p) {
                    266:                secs = strtol(p, &endp, 10);
                    267:                if (p == endp ||
                    268:                    (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
                    269:                    secs < 0)
                    270:                        return -1;
                    271:
                    272:                switch (*endp++) {
                    273:                case '\0':
                    274:                        endp--;
                    275:                case 's':
                    276:                case 'S':
                    277:                        break;
                    278:                case 'm':
                    279:                case 'M':
                    280:                        secs *= MINUTES;
                    281:                        break;
                    282:                case 'h':
                    283:                case 'H':
                    284:                        secs *= HOURS;
                    285:                        break;
                    286:                case 'd':
                    287:                case 'D':
                    288:                        secs *= DAYS;
                    289:                        break;
                    290:                case 'w':
                    291:                case 'W':
                    292:                        secs *= WEEKS;
                    293:                        break;
                    294:                default:
                    295:                        return -1;
                    296:                }
                    297:                total += secs;
                    298:                if (total < 0)
                    299:                        return -1;
                    300:                p = endp;
                    301:        }
                    302:
                    303:        return total;
1.28      djm       304: }
                    305:
                    306: /*
                    307:  * Search for next delimiter between hostnames/addresses and ports.
                    308:  * Argument may be modified (for termination).
                    309:  * Returns *cp if parsing succeeds.
                    310:  * *cp is set to the start of the next delimiter, if one was found.
                    311:  * If this is the last field, *cp is set to NULL.
                    312:  */
                    313: char *
                    314: hpdelim(char **cp)
                    315: {
                    316:        char *s, *old;
                    317:
                    318:        if (cp == NULL || *cp == NULL)
                    319:                return NULL;
                    320:
                    321:        old = s = *cp;
                    322:        if (*s == '[') {
                    323:                if ((s = strchr(s, ']')) == NULL)
                    324:                        return NULL;
                    325:                else
                    326:                        s++;
                    327:        } else if ((s = strpbrk(s, ":/")) == NULL)
                    328:                s = *cp + strlen(*cp); /* skip to end (see first case below) */
                    329:
                    330:        switch (*s) {
                    331:        case '\0':
                    332:                *cp = NULL;     /* no more fields*/
                    333:                break;
1.29      deraadt   334:
1.28      djm       335:        case ':':
                    336:        case '/':
                    337:                *s = '\0';      /* terminate */
                    338:                *cp = s + 1;
                    339:                break;
1.29      deraadt   340:
1.28      djm       341:        default:
                    342:                return NULL;
                    343:        }
                    344:
                    345:        return old;
1.6       mouring   346: }
                    347:
                    348: char *
                    349: cleanhostname(char *host)
                    350: {
                    351:        if (*host == '[' && host[strlen(host) - 1] == ']') {
                    352:                host[strlen(host) - 1] = '\0';
                    353:                return (host + 1);
                    354:        } else
                    355:                return host;
                    356: }
                    357:
                    358: char *
                    359: colon(char *cp)
                    360: {
                    361:        int flag = 0;
                    362:
                    363:        if (*cp == ':')         /* Leading colon is part of file name. */
                    364:                return (0);
                    365:        if (*cp == '[')
                    366:                flag = 1;
                    367:
                    368:        for (; *cp; ++cp) {
                    369:                if (*cp == '@' && *(cp+1) == '[')
                    370:                        flag = 1;
                    371:                if (*cp == ']' && *(cp+1) == ':' && flag)
                    372:                        return (cp+1);
                    373:                if (*cp == ':' && !flag)
                    374:                        return (cp);
                    375:                if (*cp == '/')
                    376:                        return (0);
                    377:        }
                    378:        return (0);
1.7       mouring   379: }
                    380:
1.12      markus    381: /* function to assist building execv() arguments */
1.7       mouring   382: void
                    383: addargs(arglist *args, char *fmt, ...)
                    384: {
                    385:        va_list ap;
                    386:        char buf[1024];
1.25      avsm      387:        u_int nalloc;
1.7       mouring   388:
                    389:        va_start(ap, fmt);
                    390:        vsnprintf(buf, sizeof(buf), fmt, ap);
                    391:        va_end(ap);
                    392:
1.22      markus    393:        nalloc = args->nalloc;
1.7       mouring   394:        if (args->list == NULL) {
1.22      markus    395:                nalloc = 32;
1.7       mouring   396:                args->num = 0;
1.22      markus    397:        } else if (args->num+2 >= nalloc)
                    398:                nalloc *= 2;
1.7       mouring   399:
1.22      markus    400:        args->list = xrealloc(args->list, nalloc * sizeof(char *));
                    401:        args->nalloc = nalloc;
1.7       mouring   402:        args->list[args->num++] = xstrdup(buf);
                    403:        args->list[args->num] = NULL;
1.30      djm       404: }
                    405:
                    406: /*
                    407:  * Expands tildes in the file name.  Returns data allocated by xmalloc.
                    408:  * Warning: this calls getpw*.
                    409:  */
                    410: char *
                    411: tilde_expand_filename(const char *filename, uid_t uid)
                    412: {
                    413:        const char *path;
                    414:        char user[128], ret[MAXPATHLEN];
                    415:        struct passwd *pw;
1.32      djm       416:        u_int len, slash;
1.30      djm       417:
                    418:        if (*filename != '~')
                    419:                return (xstrdup(filename));
                    420:        filename++;
                    421:
                    422:        path = strchr(filename, '/');
                    423:        if (path != NULL && path > filename) {          /* ~user/path */
1.32      djm       424:                slash = path - filename;
                    425:                if (slash > sizeof(user) - 1)
1.30      djm       426:                        fatal("tilde_expand_filename: ~username too long");
1.32      djm       427:                memcpy(user, filename, slash);
                    428:                user[slash] = '\0';
1.30      djm       429:                if ((pw = getpwnam(user)) == NULL)
                    430:                        fatal("tilde_expand_filename: No such user %s", user);
                    431:        } else if ((pw = getpwuid(uid)) == NULL)        /* ~/path */
                    432:                fatal("tilde_expand_filename: No such uid %d", uid);
                    433:
                    434:        if (strlcpy(ret, pw->pw_dir, sizeof(ret)) >= sizeof(ret))
                    435:                fatal("tilde_expand_filename: Path too long");
                    436:
                    437:        /* Make sure directory has a trailing '/' */
                    438:        len = strlen(pw->pw_dir);
                    439:        if ((len == 0 || pw->pw_dir[len - 1] != '/') &&
                    440:            strlcat(ret, "/", sizeof(ret)) >= sizeof(ret))
                    441:                fatal("tilde_expand_filename: Path too long");
                    442:
                    443:        /* Skip leading '/' from specified path */
                    444:        if (path != NULL)
                    445:                filename = path + 1;
                    446:        if (strlcat(ret, filename, sizeof(ret)) >= sizeof(ret))
                    447:                fatal("tilde_expand_filename: Path too long");
                    448:
                    449:        return (xstrdup(ret));
1.31      djm       450: }
                    451:
                    452: /*
                    453:  * Expand a string with a set of %[char] escapes. A number of escapes may be
                    454:  * specified as (char *escape_chars, char *replacement) pairs. The list must
1.34      dtucker   455:  * be terminated by a NULL escape_char. Returns replaced string in memory
1.31      djm       456:  * allocated by xmalloc.
                    457:  */
                    458: char *
                    459: percent_expand(const char *string, ...)
                    460: {
                    461: #define EXPAND_MAX_KEYS        16
                    462:        struct {
                    463:                const char *key;
                    464:                const char *repl;
                    465:        } keys[EXPAND_MAX_KEYS];
1.32      djm       466:        u_int num_keys, i, j;
1.31      djm       467:        char buf[4096];
                    468:        va_list ap;
                    469:
                    470:        /* Gather keys */
                    471:        va_start(ap, string);
                    472:        for (num_keys = 0; num_keys < EXPAND_MAX_KEYS; num_keys++) {
                    473:                keys[num_keys].key = va_arg(ap, char *);
                    474:                if (keys[num_keys].key == NULL)
                    475:                        break;
                    476:                keys[num_keys].repl = va_arg(ap, char *);
                    477:                if (keys[num_keys].repl == NULL)
                    478:                        fatal("percent_expand: NULL replacement");
                    479:        }
                    480:        va_end(ap);
                    481:
                    482:        if (num_keys >= EXPAND_MAX_KEYS)
                    483:                fatal("percent_expand: too many keys");
                    484:
                    485:        /* Expand string */
                    486:        *buf = '\0';
                    487:        for (i = 0; *string != '\0'; string++) {
                    488:                if (*string != '%') {
                    489:  append:
                    490:                        buf[i++] = *string;
                    491:                        if (i >= sizeof(buf))
                    492:                                fatal("percent_expand: string too long");
                    493:                        buf[i] = '\0';
                    494:                        continue;
                    495:                }
                    496:                string++;
                    497:                if (*string == '%')
                    498:                        goto append;
                    499:                for (j = 0; j < num_keys; j++) {
                    500:                        if (strchr(keys[j].key, *string) != NULL) {
                    501:                                i = strlcat(buf, keys[j].repl, sizeof(buf));
                    502:                                if (i >= sizeof(buf))
                    503:                                        fatal("percent_expand: string too long");
                    504:                                break;
                    505:                        }
                    506:                }
                    507:                if (j >= num_keys)
                    508:                        fatal("percent_expand: unknown key %%%c", *string);
                    509:        }
                    510:        return (xstrdup(buf));
                    511: #undef EXPAND_MAX_KEYS
1.26      dtucker   512: }
                    513:
                    514: /*
                    515:  * Read an entire line from a public key file into a static buffer, discarding
                    516:  * lines that exceed the buffer size.  Returns 0 on success, -1 on failure.
                    517:  */
                    518: int
                    519: read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
1.27      dtucker   520:    u_long *lineno)
1.26      dtucker   521: {
                    522:        while (fgets(buf, bufsz, f) != NULL) {
                    523:                (*lineno)++;
                    524:                if (buf[strlen(buf) - 1] == '\n' || feof(f)) {
                    525:                        return 0;
                    526:                } else {
1.27      dtucker   527:                        debug("%s: %s line %lu exceeds size limit", __func__,
                    528:                            filename, *lineno);
1.26      dtucker   529:                        /* discard remainder of line */
1.29      deraadt   530:                        while (fgetc(f) != '\n' && !feof(f))
1.26      dtucker   531:                                ;       /* nothing */
                    532:                }
                    533:        }
                    534:        return -1;
1.36      reyk      535: }
                    536:
                    537: int
1.37      reyk      538: tun_open(int tun, int mode)
1.36      reyk      539: {
1.37      reyk      540:        struct ifreq ifr;
1.36      reyk      541:        char name[100];
1.37      reyk      542:        int fd = -1, sock;
1.36      reyk      543:
1.37      reyk      544:        /* Open the tunnel device */
                    545:        if (tun <= SSH_TUNID_MAX) {
1.36      reyk      546:                snprintf(name, sizeof(name), "/dev/tun%d", tun);
1.37      reyk      547:                fd = open(name, O_RDWR);
                    548:        } else if (tun == SSH_TUNID_ANY) {
                    549:                for (tun = 100; tun >= 0; tun--) {
                    550:                        snprintf(name, sizeof(name), "/dev/tun%d", tun);
                    551:                        if ((fd = open(name, O_RDWR)) >= 0)
                    552:                                break;
1.36      reyk      553:                }
                    554:        } else {
1.39      stevesk   555:                debug("%s: invalid tunnel %u", __func__, tun);
1.37      reyk      556:                return (-1);
                    557:        }
                    558:
                    559:        if (fd < 0) {
                    560:                debug("%s: %s open failed: %s", __func__, name, strerror(errno));
                    561:                return (-1);
1.36      reyk      562:        }
1.37      reyk      563:
                    564:        debug("%s: %s mode %d fd %d", __func__, name, mode, fd);
                    565:
                    566:        /* Set the tunnel device operation mode */
                    567:        snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tun%d", tun);
                    568:        if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
                    569:                goto failed;
                    570:
                    571:        if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)
                    572:                goto failed;
1.40      reyk      573:
                    574:        /* Set interface mode */
                    575:        ifr.ifr_flags &= ~IFF_UP;
                    576:        if (mode == SSH_TUNMODE_ETHERNET)
1.37      reyk      577:                ifr.ifr_flags |= IFF_LINK0;
1.40      reyk      578:        else
                    579:                ifr.ifr_flags &= ~IFF_LINK0;
                    580:        if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
                    581:                goto failed;
                    582:
                    583:        /* Bring interface up */
1.37      reyk      584:        ifr.ifr_flags |= IFF_UP;
                    585:        if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
                    586:                goto failed;
                    587:
                    588:        close(sock);
                    589:        return (fd);
                    590:
                    591:  failed:
                    592:        if (fd >= 0)
                    593:                close(fd);
                    594:        if (sock >= 0)
                    595:                close(sock);
                    596:        debug("%s: failed to set %s mode %d: %s", __func__, name,
                    597:            mode, strerror(errno));
1.36      reyk      598:        return (-1);
1.35      djm       599: }
                    600:
                    601: void
                    602: sanitise_stdfd(void)
                    603: {
1.41    ! djm       604:        int nullfd, dupfd;
1.35      djm       605:
1.41    ! djm       606:        if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
1.35      djm       607:                fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno));
                    608:                exit(1);
                    609:        }
1.41    ! djm       610:        while (++dupfd <= 2) {
        !           611:                /* Only clobber closed fds */
        !           612:                if (fcntl(dupfd, F_GETFL, 0) >= 0)
        !           613:                        continue;
        !           614:                if (dup2(nullfd, dupfd) == -1) {
1.35      djm       615:                        fprintf(stderr, "dup2: %s", strerror(errno));
                    616:                        exit(1);
                    617:                }
                    618:        }
                    619:        if (nullfd > 2)
                    620:                close(nullfd);
1.1       markus    621: }
1.33      djm       622:
                    623: char *
                    624: tohex(const u_char *d, u_int l)
                    625: {
                    626:        char b[3], *r;
                    627:        u_int i, hl;
                    628:
                    629:        hl = l * 2 + 1;
                    630:        r = xmalloc(hl);
                    631:        *r = '\0';
                    632:        for (i = 0; i < l; i++) {
                    633:                snprintf(b, sizeof(b), "%02x", d[i]);
                    634:                strlcat(r, b, hl);
                    635:        }
                    636:        return (r);
                    637: }
                    638: