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

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