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

Annotation of src/usr.bin/ssh/ssh-keyscan.c, Revision 1.30.2.3

1.1       markus      1: /*
                      2:  * Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>.
                      3:  *
                      4:  * Modification and redistribution in source and binary forms is
                      5:  * permitted provided that due credit is given to the author and the
1.23      pvalchev    6:  * OpenBSD project by leaving this copyright notice intact.
1.1       markus      7:  */
                      8:
                      9: #include "includes.h"
1.30.2.3! miod       10: RCSID("$OpenBSD: ssh-keyscan.c,v 1.40 2002/07/06 17:47:58 stevesk Exp $");
1.1       markus     11:
                     12: #include <sys/queue.h>
                     13: #include <errno.h>
                     14:
1.5       markus     15: #include <openssl/bn.h>
1.1       markus     16:
1.26      markus     17: #include <setjmp.h>
1.1       markus     18: #include "xmalloc.h"
                     19: #include "ssh.h"
1.10      markus     20: #include "ssh1.h"
1.1       markus     21: #include "key.h"
1.26      markus     22: #include "kex.h"
                     23: #include "compat.h"
                     24: #include "myproposal.h"
                     25: #include "packet.h"
                     26: #include "dispatch.h"
1.1       markus     27: #include "buffer.h"
                     28: #include "bufaux.h"
1.11      markus     29: #include "log.h"
1.18      deraadt    30: #include "atomicio.h"
1.26      markus     31: #include "misc.h"
1.1       markus     32:
1.26      markus     33: /* Flag indicating whether IPv4 or IPv6.  This can be set on the command line.
                     34:    Default value is AF_UNSPEC means both IPv4 and IPv6. */
                     35: int IPv4or6 = AF_UNSPEC;
                     36:
                     37: int ssh_port = SSH_DEFAULT_PORT;
1.1       markus     38:
1.26      markus     39: #define KT_RSA1        1
                     40: #define KT_DSA 2
                     41: #define KT_RSA 4
                     42:
                     43: int get_keytypes = KT_RSA1;    /* Get only RSA1 keys by default */
1.1       markus     44:
                     45: #define MAXMAXFD 256
                     46:
                     47: /* The number of seconds after which to give up on a TCP connection */
                     48: int timeout = 5;
                     49:
                     50: int maxfd;
1.18      deraadt    51: #define MAXCON (maxfd - 10)
1.1       markus     52:
1.3       markus     53: extern char *__progname;
1.19      millert    54: fd_set *read_wait;
                     55: size_t read_wait_size;
1.1       markus     56: int ncon;
1.26      markus     57: int nonfatal_fatal = 0;
                     58: jmp_buf kexjmp;
1.29      markus     59: Key *kexjmp_key;
1.1       markus     60:
                     61: /*
                     62:  * Keep a connection structure for each file descriptor.  The state
                     63:  * associated with file descriptor n is held in fdcon[n].
                     64:  */
                     65: typedef struct Connection {
1.6       markus     66:        u_char c_status;        /* State of connection on this file desc. */
1.1       markus     67: #define CS_UNUSED 0            /* File descriptor unused */
                     68: #define CS_CON 1               /* Waiting to connect/read greeting */
                     69: #define CS_SIZE 2              /* Waiting to read initial packet size */
                     70: #define CS_KEYS 3              /* Waiting to read public key packet */
                     71:        int c_fd;               /* Quick lookup: c->c_fd == c - fdcon */
                     72:        int c_plen;             /* Packet length field for ssh packet */
                     73:        int c_len;              /* Total bytes which must be read. */
                     74:        int c_off;              /* Length of data read so far. */
1.26      markus     75:        int c_keytype;          /* Only one of KT_RSA1, KT_DSA, or KT_RSA */
1.1       markus     76:        char *c_namebase;       /* Address to free for c_name and c_namelist */
                     77:        char *c_name;           /* Hostname of connection for errors */
                     78:        char *c_namelist;       /* Pointer to other possible addresses */
                     79:        char *c_output_name;    /* Hostname of connection for output */
                     80:        char *c_data;           /* Data read from this fd */
1.26      markus     81:        Kex *c_kex;             /* The key-exchange struct for ssh2 */
1.1       markus     82:        struct timeval c_tv;    /* Time at which connection gets aborted */
                     83:        TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */
                     84: } con;
                     85:
                     86: TAILQ_HEAD(conlist, Connection) tq;    /* Timeout Queue */
                     87: con *fdcon;
                     88:
                     89: /*
                     90:  *  This is just a wrapper around fgets() to make it usable.
                     91:  */
                     92:
                     93: /* Stress-test.  Increase this later. */
                     94: #define LINEBUF_SIZE 16
                     95:
                     96: typedef struct {
                     97:        char *buf;
1.6       markus     98:        u_int size;
1.1       markus     99:        int lineno;
                    100:        const char *filename;
                    101:        FILE *stream;
                    102:        void (*errfun) (const char *,...);
                    103: } Linebuf;
                    104:
1.24      itojun    105: static Linebuf *
1.1       markus    106: Linebuf_alloc(const char *filename, void (*errfun) (const char *,...))
                    107: {
                    108:        Linebuf *lb;
                    109:
                    110:        if (!(lb = malloc(sizeof(*lb)))) {
                    111:                if (errfun)
1.30.2.3! miod      112:                        (*errfun) ("linebuf (%s): malloc failed\n",
        !           113:                            filename ? filename : "(stdin)");
1.1       markus    114:                return (NULL);
                    115:        }
                    116:        if (filename) {
                    117:                lb->filename = filename;
                    118:                if (!(lb->stream = fopen(filename, "r"))) {
1.9       markus    119:                        xfree(lb);
1.1       markus    120:                        if (errfun)
                    121:                                (*errfun) ("%s: %s\n", filename, strerror(errno));
                    122:                        return (NULL);
                    123:                }
                    124:        } else {
                    125:                lb->filename = "(stdin)";
                    126:                lb->stream = stdin;
                    127:        }
                    128:
                    129:        if (!(lb->buf = malloc(lb->size = LINEBUF_SIZE))) {
                    130:                if (errfun)
                    131:                        (*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
1.9       markus    132:                xfree(lb);
1.1       markus    133:                return (NULL);
                    134:        }
                    135:        lb->errfun = errfun;
                    136:        lb->lineno = 0;
                    137:        return (lb);
                    138: }
                    139:
1.24      itojun    140: static void
1.1       markus    141: Linebuf_free(Linebuf * lb)
                    142: {
                    143:        fclose(lb->stream);
1.9       markus    144:        xfree(lb->buf);
                    145:        xfree(lb);
1.1       markus    146: }
                    147:
1.24      itojun    148: #if 0
                    149: static void
1.1       markus    150: Linebuf_restart(Linebuf * lb)
                    151: {
                    152:        clearerr(lb->stream);
                    153:        rewind(lb->stream);
                    154:        lb->lineno = 0;
                    155: }
                    156:
1.24      itojun    157: static int
1.1       markus    158: Linebuf_lineno(Linebuf * lb)
                    159: {
                    160:        return (lb->lineno);
                    161: }
1.24      itojun    162: #endif
1.1       markus    163:
1.24      itojun    164: static char *
1.14      markus    165: Linebuf_getline(Linebuf * lb)
1.1       markus    166: {
                    167:        int n = 0;
1.30.2.3! miod      168:        void *p;
1.1       markus    169:
                    170:        lb->lineno++;
                    171:        for (;;) {
                    172:                /* Read a line */
                    173:                if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
                    174:                        if (ferror(lb->stream) && lb->errfun)
1.30.2.3! miod      175:                                (*lb->errfun)("%s: %s\n", lb->filename,
1.17      deraadt   176:                                    strerror(errno));
1.1       markus    177:                        return (NULL);
                    178:                }
                    179:                n = strlen(lb->buf);
                    180:
                    181:                /* Return it or an error if it fits */
                    182:                if (n > 0 && lb->buf[n - 1] == '\n') {
                    183:                        lb->buf[n - 1] = '\0';
                    184:                        return (lb->buf);
                    185:                }
                    186:                if (n != lb->size - 1) {
                    187:                        if (lb->errfun)
1.30.2.3! miod      188:                                (*lb->errfun)("%s: skipping incomplete last line\n",
1.17      deraadt   189:                                    lb->filename);
1.1       markus    190:                        return (NULL);
                    191:                }
                    192:                /* Double the buffer if we need more space */
1.30.2.3! miod      193:                lb->size *= 2;
        !           194:                if ((p = realloc(lb->buf, lb->size)) == NULL) {
        !           195:                        lb->size /= 2;
1.1       markus    196:                        if (lb->errfun)
1.30.2.3! miod      197:                                (*lb->errfun)("linebuf (%s): realloc failed\n",
1.17      deraadt   198:                                    lb->filename);
1.1       markus    199:                        return (NULL);
                    200:                }
1.30.2.3! miod      201:                lb->buf = p;
1.1       markus    202:        }
                    203: }
                    204:
1.24      itojun    205: static int
1.1       markus    206: fdlim_get(int hard)
                    207: {
                    208:        struct rlimit rlfd;
1.17      deraadt   209:
1.1       markus    210:        if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
                    211:                return (-1);
                    212:        if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
                    213:                return 10000;
                    214:        else
                    215:                return hard ? rlfd.rlim_max : rlfd.rlim_cur;
                    216: }
                    217:
1.24      itojun    218: static int
1.1       markus    219: fdlim_set(int lim)
                    220: {
                    221:        struct rlimit rlfd;
1.30.2.3! miod      222:
1.1       markus    223:        if (lim <= 0)
                    224:                return (-1);
                    225:        if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
                    226:                return (-1);
                    227:        rlfd.rlim_cur = lim;
                    228:        if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
                    229:                return (-1);
                    230:        return (0);
                    231: }
                    232:
                    233: /*
                    234:  * This is an strsep function that returns a null field for adjacent
                    235:  * separators.  This is the same as the 4.4BSD strsep, but different from the
                    236:  * one in the GNU libc.
                    237:  */
1.24      itojun    238: static char *
1.1       markus    239: xstrsep(char **str, const char *delim)
                    240: {
                    241:        char *s, *e;
                    242:
                    243:        if (!**str)
                    244:                return (NULL);
                    245:
                    246:        s = *str;
                    247:        e = s + strcspn(s, delim);
                    248:
                    249:        if (*e != '\0')
                    250:                *e++ = '\0';
                    251:        *str = e;
                    252:
                    253:        return (s);
                    254: }
                    255:
                    256: /*
                    257:  * Get the next non-null token (like GNU strsep).  Strsep() will return a
                    258:  * null token for two adjacent separators, so we may have to loop.
                    259:  */
1.24      itojun    260: static char *
1.1       markus    261: strnnsep(char **stringp, char *delim)
                    262: {
                    263:        char *tok;
                    264:
                    265:        do {
                    266:                tok = xstrsep(stringp, delim);
                    267:        } while (tok && *tok == '\0');
                    268:        return (tok);
                    269: }
                    270:
1.26      markus    271: static Key *
                    272: keygrab_ssh1(con *c)
1.1       markus    273: {
                    274:        static Key *rsa;
                    275:        static Buffer msg;
                    276:
                    277:        if (rsa == NULL) {
                    278:                buffer_init(&msg);
                    279:                rsa = key_new(KEY_RSA1);
                    280:        }
1.26      markus    281:        buffer_append(&msg, c->c_data, c->c_plen);
                    282:        buffer_consume(&msg, 8 - (c->c_plen & 7));      /* padding */
1.1       markus    283:        if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
1.26      markus    284:                error("%s: invalid packet type", c->c_name);
1.1       markus    285:                buffer_clear(&msg);
1.26      markus    286:                return NULL;
1.1       markus    287:        }
                    288:        buffer_consume(&msg, 8);                /* cookie */
                    289:
                    290:        /* server key */
                    291:        (void) buffer_get_int(&msg);
                    292:        buffer_get_bignum(&msg, rsa->rsa->e);
                    293:        buffer_get_bignum(&msg, rsa->rsa->n);
                    294:
                    295:        /* host key */
                    296:        (void) buffer_get_int(&msg);
                    297:        buffer_get_bignum(&msg, rsa->rsa->e);
                    298:        buffer_get_bignum(&msg, rsa->rsa->n);
1.26      markus    299:
1.1       markus    300:        buffer_clear(&msg);
                    301:
1.26      markus    302:        return (rsa);
                    303: }
                    304:
                    305: static int
                    306: hostjump(Key *hostkey)
                    307: {
1.29      markus    308:        kexjmp_key = hostkey;
                    309:        longjmp(kexjmp, 1);
1.26      markus    310: }
                    311:
                    312: static int
                    313: ssh2_capable(int remote_major, int remote_minor)
                    314: {
                    315:        switch (remote_major) {
                    316:        case 1:
                    317:                if (remote_minor == 99)
                    318:                        return 1;
                    319:                break;
                    320:        case 2:
                    321:                return 1;
                    322:        default:
                    323:                break;
                    324:        }
                    325:        return 0;
                    326: }
                    327:
                    328: static Key *
                    329: keygrab_ssh2(con *c)
                    330: {
                    331:        int j;
                    332:
                    333:        packet_set_connection(c->c_fd, c->c_fd);
                    334:        enable_compat20();
                    335:        myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA?
                    336:            "ssh-dss": "ssh-rsa";
                    337:        c->c_kex = kex_setup(myproposal);
                    338:        c->c_kex->verify_host_key = hostjump;
                    339:
                    340:        if (!(j = setjmp(kexjmp))) {
                    341:                nonfatal_fatal = 1;
                    342:                dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
                    343:                fprintf(stderr, "Impossible! dispatch_run() returned!\n");
                    344:                exit(1);
                    345:        }
                    346:        nonfatal_fatal = 0;
                    347:        xfree(c->c_kex);
                    348:        c->c_kex = NULL;
                    349:        packet_close();
                    350:
1.29      markus    351:        return j < 0? NULL : kexjmp_key;
1.26      markus    352: }
                    353:
                    354: static void
                    355: keyprint(con *c, Key *key)
                    356: {
                    357:        if (!key)
                    358:                return;
                    359:
                    360:        fprintf(stdout, "%s ", c->c_output_name ? c->c_output_name : c->c_name);
                    361:        key_write(key, stdout);
1.1       markus    362:        fputs("\n", stdout);
                    363: }
                    364:
1.24      itojun    365: static int
1.1       markus    366: tcpconnect(char *host)
                    367: {
                    368:        struct addrinfo hints, *ai, *aitop;
                    369:        char strport[NI_MAXSERV];
                    370:        int gaierr, s = -1;
                    371:
1.26      markus    372:        snprintf(strport, sizeof strport, "%d", ssh_port);
1.1       markus    373:        memset(&hints, 0, sizeof(hints));
1.26      markus    374:        hints.ai_family = IPv4or6;
1.1       markus    375:        hints.ai_socktype = SOCK_STREAM;
                    376:        if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
                    377:                fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
                    378:        for (ai = aitop; ai; ai = ai->ai_next) {
                    379:                s = socket(ai->ai_family, SOCK_STREAM, 0);
                    380:                if (s < 0) {
                    381:                        error("socket: %s", strerror(errno));
                    382:                        continue;
                    383:                }
1.7       markus    384:                if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
1.1       markus    385:                        fatal("F_SETFL: %s", strerror(errno));
                    386:                if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
                    387:                    errno != EINPROGRESS)
                    388:                        error("connect (`%s'): %s", host, strerror(errno));
                    389:                else
                    390:                        break;
                    391:                close(s);
                    392:                s = -1;
                    393:        }
                    394:        freeaddrinfo(aitop);
                    395:        return s;
                    396: }
                    397:
1.24      itojun    398: static int
1.26      markus    399: conalloc(char *iname, char *oname, int keytype)
1.1       markus    400: {
                    401:        char *namebase, *name, *namelist;
1.30.2.3! miod      402:        int s;
1.1       markus    403:
                    404:        namebase = namelist = xstrdup(iname);
                    405:
                    406:        do {
                    407:                name = xstrsep(&namelist, ",");
                    408:                if (!name) {
1.9       markus    409:                        xfree(namebase);
1.1       markus    410:                        return (-1);
                    411:                }
                    412:        } while ((s = tcpconnect(name)) < 0);
                    413:
                    414:        if (s >= maxfd)
1.4       markus    415:                fatal("conalloc: fdno %d too high", s);
1.1       markus    416:        if (fdcon[s].c_status)
1.4       markus    417:                fatal("conalloc: attempt to reuse fdno %d", s);
1.1       markus    418:
                    419:        fdcon[s].c_fd = s;
                    420:        fdcon[s].c_status = CS_CON;
                    421:        fdcon[s].c_namebase = namebase;
                    422:        fdcon[s].c_name = name;
                    423:        fdcon[s].c_namelist = namelist;
                    424:        fdcon[s].c_output_name = xstrdup(oname);
                    425:        fdcon[s].c_data = (char *) &fdcon[s].c_plen;
                    426:        fdcon[s].c_len = 4;
                    427:        fdcon[s].c_off = 0;
1.26      markus    428:        fdcon[s].c_keytype = keytype;
1.1       markus    429:        gettimeofday(&fdcon[s].c_tv, NULL);
                    430:        fdcon[s].c_tv.tv_sec += timeout;
                    431:        TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
1.19      millert   432:        FD_SET(s, read_wait);
1.1       markus    433:        ncon++;
                    434:        return (s);
                    435: }
                    436:
1.24      itojun    437: static void
1.1       markus    438: confree(int s)
                    439: {
                    440:        if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
1.4       markus    441:                fatal("confree: attempt to free bad fdno %d", s);
1.18      deraadt   442:        close(s);
1.9       markus    443:        xfree(fdcon[s].c_namebase);
                    444:        xfree(fdcon[s].c_output_name);
1.1       markus    445:        if (fdcon[s].c_status == CS_KEYS)
1.9       markus    446:                xfree(fdcon[s].c_data);
1.1       markus    447:        fdcon[s].c_status = CS_UNUSED;
1.26      markus    448:        fdcon[s].c_keytype = 0;
1.1       markus    449:        TAILQ_REMOVE(&tq, &fdcon[s], c_link);
1.19      millert   450:        FD_CLR(s, read_wait);
1.1       markus    451:        ncon--;
                    452: }
                    453:
1.24      itojun    454: static void
1.1       markus    455: contouch(int s)
                    456: {
                    457:        TAILQ_REMOVE(&tq, &fdcon[s], c_link);
                    458:        gettimeofday(&fdcon[s].c_tv, NULL);
                    459:        fdcon[s].c_tv.tv_sec += timeout;
                    460:        TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
                    461: }
                    462:
1.24      itojun    463: static int
1.1       markus    464: conrecycle(int s)
                    465: {
                    466:        con *c = &fdcon[s];
1.30.2.3! miod      467:        int ret;
1.1       markus    468:
1.26      markus    469:        ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
1.1       markus    470:        confree(s);
                    471:        return (ret);
                    472: }
                    473:
1.24      itojun    474: static void
1.1       markus    475: congreet(int s)
                    476: {
1.30.2.3! miod      477:        int remote_major, remote_minor, n = 0;
1.26      markus    478:        char buf[256], *cp;
1.30.2.1  jason     479:        char remote_version[sizeof buf];
1.21      millert   480:        size_t bufsiz;
1.1       markus    481:        con *c = &fdcon[s];
                    482:
1.21      millert   483:        bufsiz = sizeof(buf);
                    484:        cp = buf;
1.27      markus    485:        while (bufsiz-- && (n = read(s, cp, 1)) == 1 && *cp != '\n') {
                    486:                if (*cp == '\r')
                    487:                        *cp = '\n';
1.21      millert   488:                cp++;
1.27      markus    489:        }
1.1       markus    490:        if (n < 0) {
                    491:                if (errno != ECONNREFUSED)
                    492:                        error("read (%s): %s", c->c_name, strerror(errno));
                    493:                conrecycle(s);
                    494:                return;
                    495:        }
1.30.2.1  jason     496:        if (n == 0) {
                    497:                error("%s: Connection closed by remote host", c->c_name);
                    498:                conrecycle(s);
                    499:                return;
                    500:        }
1.21      millert   501:        if (*cp != '\n' && *cp != '\r') {
1.1       markus    502:                error("%s: bad greeting", c->c_name);
                    503:                confree(s);
                    504:                return;
                    505:        }
1.21      millert   506:        *cp = '\0';
1.30.2.1  jason     507:        if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
                    508:            &remote_major, &remote_minor, remote_version) == 3)
                    509:                compat_datafellows(remote_version);
                    510:        else
                    511:                datafellows = 0;
1.26      markus    512:        if (c->c_keytype != KT_RSA1) {
                    513:                if (!ssh2_capable(remote_major, remote_minor)) {
                    514:                        debug("%s doesn't support ssh2", c->c_name);
                    515:                        confree(s);
                    516:                        return;
                    517:                }
1.30.2.1  jason     518:        } else if (remote_major != 1) {
                    519:                debug("%s doesn't support ssh1", c->c_name);
                    520:                confree(s);
                    521:                return;
1.26      markus    522:        }
1.27      markus    523:        fprintf(stderr, "# %s %s\n", c->c_name, chop(buf));
1.26      markus    524:        n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
                    525:            c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
                    526:            c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
1.18      deraadt   527:        if (atomicio(write, s, buf, n) != n) {
1.1       markus    528:                error("write (%s): %s", c->c_name, strerror(errno));
                    529:                confree(s);
                    530:                return;
                    531:        }
1.26      markus    532:        if (c->c_keytype != KT_RSA1) {
                    533:                keyprint(c, keygrab_ssh2(c));
                    534:                confree(s);
                    535:                return;
                    536:        }
1.1       markus    537:        c->c_status = CS_SIZE;
                    538:        contouch(s);
                    539: }
                    540:
1.24      itojun    541: static void
1.1       markus    542: conread(int s)
                    543: {
                    544:        con *c = &fdcon[s];
1.30.2.3! miod      545:        int n;
1.1       markus    546:
                    547:        if (c->c_status == CS_CON) {
                    548:                congreet(s);
                    549:                return;
                    550:        }
                    551:        n = read(s, c->c_data + c->c_off, c->c_len - c->c_off);
                    552:        if (n < 0) {
                    553:                error("read (%s): %s", c->c_name, strerror(errno));
                    554:                confree(s);
                    555:                return;
                    556:        }
                    557:        c->c_off += n;
                    558:
                    559:        if (c->c_off == c->c_len)
                    560:                switch (c->c_status) {
                    561:                case CS_SIZE:
                    562:                        c->c_plen = htonl(c->c_plen);
                    563:                        c->c_len = c->c_plen + 8 - (c->c_plen & 7);
                    564:                        c->c_off = 0;
                    565:                        c->c_data = xmalloc(c->c_len);
                    566:                        c->c_status = CS_KEYS;
                    567:                        break;
                    568:                case CS_KEYS:
1.26      markus    569:                        keyprint(c, keygrab_ssh1(c));
1.1       markus    570:                        confree(s);
                    571:                        return;
                    572:                        break;
                    573:                default:
1.4       markus    574:                        fatal("conread: invalid status %d", c->c_status);
1.1       markus    575:                        break;
                    576:                }
                    577:
                    578:        contouch(s);
                    579: }
                    580:
1.24      itojun    581: static void
1.1       markus    582: conloop(void)
                    583: {
                    584:        struct timeval seltime, now;
1.30.2.3! miod      585:        fd_set *r, *e;
1.1       markus    586:        con *c;
1.30.2.3! miod      587:        int i;
1.1       markus    588:
                    589:        gettimeofday(&now, NULL);
1.30.2.2  miod      590:        c = TAILQ_FIRST(&tq);
1.1       markus    591:
1.18      deraadt   592:        if (c && (c->c_tv.tv_sec > now.tv_sec ||
                    593:            (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
1.1       markus    594:                seltime = c->c_tv;
                    595:                seltime.tv_sec -= now.tv_sec;
                    596:                seltime.tv_usec -= now.tv_usec;
1.13      itojun    597:                if (seltime.tv_usec < 0) {
1.1       markus    598:                        seltime.tv_usec += 1000000;
                    599:                        seltime.tv_sec--;
                    600:                }
                    601:        } else
                    602:                seltime.tv_sec = seltime.tv_usec = 0;
                    603:
1.19      millert   604:        r = xmalloc(read_wait_size);
                    605:        memcpy(r, read_wait, read_wait_size);
                    606:        e = xmalloc(read_wait_size);
                    607:        memcpy(e, read_wait, read_wait_size);
                    608:
                    609:        while (select(maxfd, r, NULL, e, &seltime) == -1 &&
1.16      deraadt   610:            (errno == EAGAIN || errno == EINTR))
                    611:                ;
                    612:
1.18      deraadt   613:        for (i = 0; i < maxfd; i++) {
1.19      millert   614:                if (FD_ISSET(i, e)) {
1.1       markus    615:                        error("%s: exception!", fdcon[i].c_name);
                    616:                        confree(i);
1.19      millert   617:                } else if (FD_ISSET(i, r))
1.1       markus    618:                        conread(i);
1.18      deraadt   619:        }
1.19      millert   620:        xfree(r);
                    621:        xfree(e);
1.1       markus    622:
1.30.2.2  miod      623:        c = TAILQ_FIRST(&tq);
1.18      deraadt   624:        while (c && (c->c_tv.tv_sec < now.tv_sec ||
                    625:            (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
1.1       markus    626:                int s = c->c_fd;
1.18      deraadt   627:
1.30.2.2  miod      628:                c = TAILQ_NEXT(c, c_link);
1.1       markus    629:                conrecycle(s);
                    630:        }
                    631: }
                    632:
1.26      markus    633: static void
                    634: do_host(char *host)
1.1       markus    635: {
1.26      markus    636:        char *name = strnnsep(&host, " \t\n");
                    637:        int j;
1.1       markus    638:
1.30.2.1  jason     639:        if (name == NULL)
                    640:                return;
1.26      markus    641:        for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
                    642:                if (get_keytypes & j) {
                    643:                        while (ncon >= MAXCON)
                    644:                                conloop();
                    645:                        conalloc(name, *host ? host : name, j);
1.1       markus    646:                }
                    647:        }
                    648: }
                    649:
1.30.2.1  jason     650: void
                    651: fatal(const char *fmt,...)
1.26      markus    652: {
1.30.2.1  jason     653:        va_list args;
1.30.2.3! miod      654:
1.30.2.1  jason     655:        va_start(args, fmt);
                    656:        do_log(SYSLOG_LEVEL_FATAL, fmt, args);
                    657:        va_end(args);
1.26      markus    658:        if (nonfatal_fatal)
                    659:                longjmp(kexjmp, -1);
1.30.2.1  jason     660:        else
                    661:                fatal_cleanup();
1.26      markus    662: }
                    663:
                    664: static void
1.1       markus    665: usage(void)
                    666: {
1.30.2.3! miod      667:        fprintf(stderr, "usage: %s [-v46] [-p port] [-T timeout] [-f file]\n"
        !           668:            "\t\t   [host | addrlist namelist] [...]\n",
1.25      jakob     669:            __progname);
                    670:        exit(1);
1.1       markus    671: }
                    672:
                    673: int
                    674: main(int argc, char **argv)
                    675: {
1.26      markus    676:        int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
                    677:        int opt, fopt_count = 0;
                    678:        char *tname;
                    679:
                    680:        extern int optind;
                    681:        extern char *optarg;
1.1       markus    682:
                    683:        TAILQ_INIT(&tq);
                    684:
1.26      markus    685:        if (argc <= 1)
1.1       markus    686:                usage();
                    687:
1.26      markus    688:        while ((opt = getopt(argc, argv, "v46p:T:t:f:")) != -1) {
                    689:                switch (opt) {
                    690:                case 'p':
                    691:                        ssh_port = a2port(optarg);
                    692:                        if (ssh_port == 0) {
                    693:                                fprintf(stderr, "Bad port '%s'\n", optarg);
                    694:                                exit(1);
                    695:                        }
                    696:                        break;
                    697:                case 'T':
1.30.2.3! miod      698:                        timeout = convtime(optarg);
        !           699:                        if (timeout == -1 || timeout == 0) {
        !           700:                                fprintf(stderr, "Bad timeout '%s'\n", optarg);
1.1       markus    701:                                usage();
1.30.2.3! miod      702:                        }
1.26      markus    703:                        break;
                    704:                case 'v':
                    705:                        if (!debug_flag) {
                    706:                                debug_flag = 1;
                    707:                                log_level = SYSLOG_LEVEL_DEBUG1;
                    708:                        }
                    709:                        else if (log_level < SYSLOG_LEVEL_DEBUG3)
                    710:                                log_level++;
                    711:                        else
                    712:                                fatal("Too high debugging level.");
                    713:                        break;
                    714:                case 'f':
                    715:                        if (strcmp(optarg, "-") == 0)
                    716:                                optarg = NULL;
                    717:                        argv[fopt_count++] = optarg;
                    718:                        break;
                    719:                case 't':
                    720:                        get_keytypes = 0;
                    721:                        tname = strtok(optarg, ",");
                    722:                        while (tname) {
                    723:                                int type = key_type_from_name(tname);
                    724:                                switch (type) {
                    725:                                case KEY_RSA1:
                    726:                                        get_keytypes |= KT_RSA1;
                    727:                                        break;
                    728:                                case KEY_DSA:
                    729:                                        get_keytypes |= KT_DSA;
                    730:                                        break;
                    731:                                case KEY_RSA:
                    732:                                        get_keytypes |= KT_RSA;
                    733:                                        break;
                    734:                                case KEY_UNSPEC:
1.30.2.1  jason     735:                                        fatal("unknown key type %s", tname);
1.26      markus    736:                                }
                    737:                                tname = strtok(NULL, ",");
                    738:                        }
                    739:                        break;
                    740:                case '4':
                    741:                        IPv4or6 = AF_INET;
                    742:                        break;
                    743:                case '6':
                    744:                        IPv4or6 = AF_INET6;
                    745:                        break;
                    746:                case '?':
                    747:                default:
                    748:                        usage();
1.1       markus    749:                }
                    750:        }
1.26      markus    751:        if (optind == argc && !fopt_count)
1.1       markus    752:                usage();
                    753:
1.26      markus    754:        log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
                    755:
1.1       markus    756:        maxfd = fdlim_get(1);
                    757:        if (maxfd < 0)
1.4       markus    758:                fatal("%s: fdlim_get: bad value", __progname);
1.1       markus    759:        if (maxfd > MAXMAXFD)
                    760:                maxfd = MAXMAXFD;
1.18      deraadt   761:        if (MAXCON <= 0)
1.4       markus    762:                fatal("%s: not enough file descriptors", __progname);
1.1       markus    763:        if (maxfd > fdlim_get(0))
                    764:                fdlim_set(maxfd);
                    765:        fdcon = xmalloc(maxfd * sizeof(con));
1.15      itojun    766:        memset(fdcon, 0, maxfd * sizeof(con));
1.19      millert   767:
                    768:        read_wait_size = howmany(maxfd, NFDBITS) * sizeof(fd_mask);
                    769:        read_wait = xmalloc(read_wait_size);
                    770:        memset(read_wait, 0, read_wait_size);
1.1       markus    771:
1.26      markus    772:        if (fopt_count) {
                    773:                Linebuf *lb;
                    774:                char *line;
                    775:                int j;
                    776:
                    777:                for (j = 0; j < fopt_count; j++) {
                    778:                        lb = Linebuf_alloc(argv[j], error);
1.28      danh      779:                        if (!lb)
                    780:                                continue;
1.26      markus    781:                        while ((line = Linebuf_getline(lb)) != NULL)
                    782:                                do_host(line);
                    783:                        Linebuf_free(lb);
                    784:                }
                    785:        }
                    786:
                    787:        while (optind < argc)
                    788:                do_host(argv[optind++]);
1.1       markus    789:
                    790:        while (ncon > 0)
                    791:                conloop();
                    792:
                    793:        return (0);
                    794: }