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

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