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

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