[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.88

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