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

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