[BACK]Return to ssh-keyscan.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / crypto / external / bsd / openssh / dist

Annotation of src/crypto/external/bsd/openssh/dist/ssh-keyscan.c, Revision 1.1.1.21

1.1.1.21! christos    1: /* $OpenBSD: ssh-keyscan.c,v 1.145 2022/01/21 00:53:40 deraadt Exp $ */
1.1       christos    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
                      7:  * OpenBSD project by leaving this copyright notice intact.
                      8:  */
                      9:
                     10: #include <sys/types.h>
                     11: #include <sys/socket.h>
                     12: #include <sys/queue.h>
                     13: #include <sys/time.h>
                     14: #include <sys/resource.h>
                     15:
1.1.1.17  christos   16: #ifdef WITH_OPENSSL
1.1       christos   17: #include <openssl/bn.h>
1.1.1.17  christos   18: #endif
1.1       christos   19:
                     20: #include <errno.h>
                     21: #include <netdb.h>
                     22: #include <stdarg.h>
                     23: #include <stdio.h>
                     24: #include <stdlib.h>
1.1.1.21! christos   25: #include <poll.h>
1.1       christos   26: #include <signal.h>
                     27: #include <string.h>
                     28: #include <unistd.h>
                     29:
                     30: #include "xmalloc.h"
                     31: #include "ssh.h"
1.1.1.8   christos   32: #include "sshbuf.h"
                     33: #include "sshkey.h"
1.1       christos   34: #include "cipher.h"
                     35: #include "kex.h"
                     36: #include "compat.h"
                     37: #include "myproposal.h"
                     38: #include "packet.h"
                     39: #include "dispatch.h"
                     40: #include "log.h"
                     41: #include "atomicio.h"
                     42: #include "misc.h"
                     43: #include "hostfile.h"
1.1.1.8   christos   44: #include "ssherr.h"
                     45: #include "ssh_api.h"
1.1.1.14  christos   46: #include "dns.h"
1.1       christos   47:
                     48: /* Flag indicating whether IPv4 or IPv6.  This can be set on the command line.
                     49:    Default value is AF_UNSPEC means both IPv4 and IPv6. */
                     50: int IPv4or6 = AF_UNSPEC;
                     51:
                     52: int ssh_port = SSH_DEFAULT_PORT;
                     53:
1.1.1.13  christos   54: #define KT_DSA         (1)
                     55: #define KT_RSA         (1<<1)
                     56: #define KT_ECDSA       (1<<2)
                     57: #define KT_ED25519     (1<<3)
1.1.1.14  christos   58: #define KT_XMSS                (1<<4)
1.1.1.18  christos   59: #define KT_ECDSA_SK    (1<<5)
                     60: #define KT_ED25519_SK  (1<<6)
1.1.1.13  christos   61:
                     62: #define KT_MIN         KT_DSA
1.1.1.18  christos   63: #define KT_MAX         KT_ED25519_SK
1.1       christos   64:
1.1.1.10  christos   65: int get_cert = 0;
1.1.1.18  christos   66: int get_keytypes = KT_RSA|KT_ECDSA|KT_ED25519|KT_ECDSA_SK|KT_ED25519_SK;
1.1       christos   67:
                     68: int hash_hosts = 0;            /* Hash hostname on output */
                     69:
1.1.1.14  christos   70: int print_sshfp = 0;           /* Print SSHFP records instead of known_hosts */
                     71:
1.1.1.16  christos   72: int found_one = 0;             /* Successfully found a key */
                     73:
1.1       christos   74: #define MAXMAXFD 256
                     75:
                     76: /* The number of seconds after which to give up on a TCP connection */
                     77: int timeout = 5;
                     78:
                     79: int maxfd;
                     80: #define MAXCON (maxfd - 10)
                     81:
                     82: extern char *__progname;
1.1.1.21! christos   83: struct pollfd *read_wait;
1.1       christos   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 {
                     91:        u_char c_status;        /* State of connection on this file desc. */
                     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.1.1.13  christos  100:        int c_keytype;          /* Only one of KT_* */
1.1.1.9   christos  101:        sig_atomic_t c_done;    /* SSH2 done */
1.1       christos  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.1.1.8   christos  107:        struct ssh *c_ssh;      /* SSH-connection */
1.1.1.21! christos  108:        struct timespec c_ts;   /* Time at which connection gets aborted */
1.1       christos  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.1.1.8   christos  115: static void keyprint(con *c, struct sshkey *key);
                    116:
1.1       christos  117: static int
                    118: fdlim_get(int hard)
                    119: {
                    120:        struct rlimit rlfd;
                    121:
1.1.1.17  christos  122:        if (getrlimit(RLIMIT_NOFILE, &rlfd) == -1)
1.1       christos  123:                return (-1);
                    124:        if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
                    125:                return sysconf(_SC_OPEN_MAX);
                    126:        else
                    127:                return hard ? rlfd.rlim_max : rlfd.rlim_cur;
                    128: }
                    129:
                    130: static int
                    131: fdlim_set(int lim)
                    132: {
                    133:        struct rlimit rlfd;
                    134:
                    135:        if (lim <= 0)
                    136:                return (-1);
1.1.1.17  christos  137:        if (getrlimit(RLIMIT_NOFILE, &rlfd) == -1)
1.1       christos  138:                return (-1);
                    139:        rlfd.rlim_cur = lim;
1.1.1.17  christos  140:        if (setrlimit(RLIMIT_NOFILE, &rlfd) == -1)
1.1       christos  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:  */
                    150: static char *
                    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:  */
                    172: static char *
                    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:
                    183:
                    184: static int
1.1.1.8   christos  185: key_print_wrapper(struct sshkey *hostkey, struct ssh *ssh)
1.1       christos  186: {
1.1.1.8   christos  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.1       christos  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.1.1.8   christos  211: static void
1.1       christos  212: keygrab_ssh2(con *c)
                    213: {
1.1.1.7   christos  214:        char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
1.1.1.8   christos  215:        int r;
1.1       christos  216:
1.1.1.10  christos  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.1.1.17  christos  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.1.1.10  christos  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.1.1.14  christos  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.1.1.10  christos  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.1.1.18  christos  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.1.1.10  christos  258:        default:
                    259:                fatal("unknown key type %d", c->c_keytype);
                    260:                break;
                    261:        }
1.1.1.8   christos  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));
1.1       christos  265:                exit(1);
                    266:        }
1.1.1.8   christos  267: #ifdef WITH_OPENSSL
1.1.1.16  christos  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.1.1.8   christos  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.1.1.16  christos  275:        c->c_ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client;
1.1.1.8   christos  276: #endif
1.1.1.16  christos  277:        c->c_ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client;
1.1.1.20  christos  278:        c->c_ssh->kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_client;
1.1.1.8   christos  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.1.1.13  christos  284:        ssh_dispatch_run(c->c_ssh, DISPATCH_BLOCK, &c->c_done);
1.1       christos  285: }
                    286:
                    287: static void
1.1.1.12  christos  288: keyprint_one(const char *host, struct sshkey *key)
1.1       christos  289: {
1.1.1.21! christos  290:        char *hostport = NULL, *hashed = NULL;
        !           291:        const char *known_host;
1.1       christos  292:
1.1.1.16  christos  293:        found_one = 1;
                    294:
1.1.1.14  christos  295:        if (print_sshfp) {
                    296:                export_dns_rr(host, key, stdout, 0);
                    297:                return;
                    298:        }
                    299:
1.1.1.9   christos  300:        hostport = put_host_port(host, ssh_port);
1.1.1.12  christos  301:        lowercase(hostport);
1.1.1.21! christos  302:        if (hash_hosts && (hashed = host_hash(hostport, NULL, 0)) == NULL)
1.1.1.12  christos  303:                fatal("host_hash failed");
                    304:        known_host = hash_hosts ? hashed : hostport;
1.1.1.10  christos  305:        if (!get_cert)
1.1.1.12  christos  306:                fprintf(stdout, "%s ", known_host);
1.1.1.8   christos  307:        sshkey_write(key, stdout);
1.1       christos  308:        fputs("\n", stdout);
1.1.1.21! christos  309:        free(hashed);
1.1.1.9   christos  310:        free(hostport);
1.1       christos  311: }
                    312:
1.1.1.10  christos  313: static void
                    314: keyprint(con *c, struct sshkey *key)
                    315: {
                    316:        char *hosts = c->c_output_name ? c->c_output_name : c->c_name;
                    317:        char *host, *ohosts;
                    318:
                    319:        if (key == NULL)
                    320:                return;
                    321:        if (get_cert || (!hash_hosts && ssh_port == SSH_DEFAULT_PORT)) {
                    322:                keyprint_one(hosts, key);
                    323:                return;
                    324:        }
                    325:        ohosts = hosts = xstrdup(hosts);
                    326:        while ((host = strsep(&hosts, ",")) != NULL)
                    327:                keyprint_one(host, key);
                    328:        free(ohosts);
                    329: }
                    330:
1.1       christos  331: static int
                    332: tcpconnect(char *host)
                    333: {
                    334:        struct addrinfo hints, *ai, *aitop;
                    335:        char strport[NI_MAXSERV];
                    336:        int gaierr, s = -1;
                    337:
                    338:        snprintf(strport, sizeof strport, "%d", ssh_port);
                    339:        memset(&hints, 0, sizeof(hints));
                    340:        hints.ai_family = IPv4or6;
                    341:        hints.ai_socktype = SOCK_STREAM;
1.1.1.8   christos  342:        if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
                    343:                error("getaddrinfo %s: %s", host, ssh_gai_strerror(gaierr));
                    344:                return -1;
                    345:        }
1.1       christos  346:        for (ai = aitop; ai; ai = ai->ai_next) {
                    347:                s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.1.1.17  christos  348:                if (s == -1) {
1.1       christos  349:                        error("socket: %s", strerror(errno));
                    350:                        continue;
                    351:                }
                    352:                if (set_nonblock(s) == -1)
1.1.1.20  christos  353:                        fatal_f("set_nonblock(%d)", s);
1.1.1.17  christos  354:                if (connect(s, ai->ai_addr, ai->ai_addrlen) == -1 &&
1.1       christos  355:                    errno != EINPROGRESS)
                    356:                        error("connect (`%s'): %s", host, strerror(errno));
                    357:                else
                    358:                        break;
                    359:                close(s);
                    360:                s = -1;
                    361:        }
                    362:        freeaddrinfo(aitop);
                    363:        return s;
                    364: }
                    365:
                    366: static int
                    367: conalloc(char *iname, char *oname, int keytype)
                    368: {
                    369:        char *namebase, *name, *namelist;
                    370:        int s;
                    371:
                    372:        namebase = namelist = xstrdup(iname);
                    373:
                    374:        do {
                    375:                name = xstrsep(&namelist, ",");
                    376:                if (!name) {
1.1.1.6   christos  377:                        free(namebase);
1.1       christos  378:                        return (-1);
                    379:                }
                    380:        } while ((s = tcpconnect(name)) < 0);
                    381:
                    382:        if (s >= maxfd)
                    383:                fatal("conalloc: fdno %d too high", s);
                    384:        if (fdcon[s].c_status)
                    385:                fatal("conalloc: attempt to reuse fdno %d", s);
                    386:
1.1.1.20  christos  387:        debug3_f("oname %s kt %d", oname, keytype);
1.1       christos  388:        fdcon[s].c_fd = s;
                    389:        fdcon[s].c_status = CS_CON;
                    390:        fdcon[s].c_namebase = namebase;
                    391:        fdcon[s].c_name = name;
                    392:        fdcon[s].c_namelist = namelist;
                    393:        fdcon[s].c_output_name = xstrdup(oname);
                    394:        fdcon[s].c_data = (char *) &fdcon[s].c_plen;
                    395:        fdcon[s].c_len = 4;
                    396:        fdcon[s].c_off = 0;
                    397:        fdcon[s].c_keytype = keytype;
1.1.1.21! christos  398:        monotime_ts(&fdcon[s].c_ts);
        !           399:        fdcon[s].c_ts.tv_sec += timeout;
1.1       christos  400:        TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
1.1.1.21! christos  401:        read_wait[s].fd = s;
        !           402:        read_wait[s].events = POLLIN;
1.1       christos  403:        ncon++;
                    404:        return (s);
                    405: }
                    406:
                    407: static void
                    408: confree(int s)
                    409: {
                    410:        if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
                    411:                fatal("confree: attempt to free bad fdno %d", s);
1.1.1.6   christos  412:        free(fdcon[s].c_namebase);
                    413:        free(fdcon[s].c_output_name);
1.1       christos  414:        if (fdcon[s].c_status == CS_KEYS)
1.1.1.6   christos  415:                free(fdcon[s].c_data);
1.1       christos  416:        fdcon[s].c_status = CS_UNUSED;
                    417:        fdcon[s].c_keytype = 0;
1.1.1.8   christos  418:        if (fdcon[s].c_ssh) {
                    419:                ssh_packet_close(fdcon[s].c_ssh);
                    420:                free(fdcon[s].c_ssh);
                    421:                fdcon[s].c_ssh = NULL;
1.1.1.13  christos  422:        } else
                    423:                close(s);
1.1       christos  424:        TAILQ_REMOVE(&tq, &fdcon[s], c_link);
1.1.1.21! christos  425:        read_wait[s].fd = -1;
        !           426:        read_wait[s].events = 0;
1.1       christos  427:        ncon--;
                    428: }
                    429:
                    430: static void
                    431: contouch(int s)
                    432: {
                    433:        TAILQ_REMOVE(&tq, &fdcon[s], c_link);
1.1.1.21! christos  434:        monotime_ts(&fdcon[s].c_ts);
        !           435:        fdcon[s].c_ts.tv_sec += timeout;
1.1       christos  436:        TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
                    437: }
                    438:
                    439: static int
                    440: conrecycle(int s)
                    441: {
                    442:        con *c = &fdcon[s];
                    443:        int ret;
                    444:
                    445:        ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
                    446:        confree(s);
                    447:        return (ret);
                    448: }
                    449:
                    450: static void
                    451: congreet(int s)
                    452: {
                    453:        int n = 0, remote_major = 0, remote_minor = 0;
                    454:        char buf[256], *cp;
                    455:        char remote_version[sizeof buf];
                    456:        size_t bufsiz;
                    457:        con *c = &fdcon[s];
                    458:
1.1.1.13  christos  459:        /* send client banner */
                    460:        n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
                    461:            PROTOCOL_MAJOR_2, PROTOCOL_MINOR_2);
                    462:        if (n < 0 || (size_t)n >= sizeof(buf)) {
                    463:                error("snprintf: buffer too small");
                    464:                confree(s);
                    465:                return;
                    466:        }
                    467:        if (atomicio(vwrite, s, buf, n) != (size_t)n) {
                    468:                error("write (%s): %s", c->c_name, strerror(errno));
                    469:                confree(s);
                    470:                return;
                    471:        }
                    472:
1.1       christos  473:        for (;;) {
                    474:                memset(buf, '\0', sizeof(buf));
                    475:                bufsiz = sizeof(buf);
                    476:                cp = buf;
                    477:                while (bufsiz-- &&
                    478:                    (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
                    479:                        if (*cp == '\r')
                    480:                                *cp = '\n';
                    481:                        cp++;
                    482:                }
                    483:                if (n != 1 || strncmp(buf, "SSH-", 4) == 0)
                    484:                        break;
                    485:        }
                    486:        if (n == 0) {
                    487:                switch (errno) {
                    488:                case EPIPE:
                    489:                        error("%s: Connection closed by remote host", c->c_name);
                    490:                        break;
                    491:                case ECONNREFUSED:
                    492:                        break;
                    493:                default:
                    494:                        error("read (%s): %s", c->c_name, strerror(errno));
                    495:                        break;
                    496:                }
                    497:                conrecycle(s);
                    498:                return;
                    499:        }
                    500:        if (*cp != '\n' && *cp != '\r') {
                    501:                error("%s: bad greeting", c->c_name);
                    502:                confree(s);
                    503:                return;
                    504:        }
                    505:        *cp = '\0';
1.1.1.8   christos  506:        if ((c->c_ssh = ssh_packet_set_connection(NULL, s, s)) == NULL)
                    507:                fatal("ssh_packet_set_connection failed");
                    508:        ssh_packet_set_timeout(c->c_ssh, timeout, 1);
                    509:        ssh_set_app_data(c->c_ssh, c);  /* back link */
1.1.1.20  christos  510:        c->c_ssh->compat = 0;
1.1       christos  511:        if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
                    512:            &remote_major, &remote_minor, remote_version) == 3)
1.1.1.20  christos  513:                compat_banner(c->c_ssh, remote_version);
1.1.1.13  christos  514:        if (!ssh2_capable(remote_major, remote_minor)) {
                    515:                debug("%s doesn't support ssh2", c->c_name);
1.1       christos  516:                confree(s);
                    517:                return;
                    518:        }
1.1.1.14  christos  519:        fprintf(stderr, "%c %s:%d %s\n", print_sshfp ? ';' : '#',
                    520:            c->c_name, ssh_port, chop(buf));
1.1.1.13  christos  521:        keygrab_ssh2(c);
                    522:        confree(s);
1.1       christos  523: }
                    524:
                    525: static void
                    526: conread(int s)
                    527: {
                    528:        con *c = &fdcon[s];
                    529:        size_t n;
                    530:
                    531:        if (c->c_status == CS_CON) {
                    532:                congreet(s);
                    533:                return;
                    534:        }
                    535:        n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
                    536:        if (n == 0) {
                    537:                error("read (%s): %s", c->c_name, strerror(errno));
                    538:                confree(s);
                    539:                return;
                    540:        }
                    541:        c->c_off += n;
                    542:
                    543:        if (c->c_off == c->c_len)
                    544:                switch (c->c_status) {
                    545:                case CS_SIZE:
                    546:                        c->c_plen = htonl(c->c_plen);
                    547:                        c->c_len = c->c_plen + 8 - (c->c_plen & 7);
                    548:                        c->c_off = 0;
                    549:                        c->c_data = xmalloc(c->c_len);
                    550:                        c->c_status = CS_KEYS;
                    551:                        break;
                    552:                default:
                    553:                        fatal("conread: invalid status %d", c->c_status);
                    554:                        break;
                    555:                }
                    556:
                    557:        contouch(s);
                    558: }
                    559:
                    560: static void
                    561: conloop(void)
                    562: {
1.1.1.21! christos  563:        struct timespec seltime, now;
1.1       christos  564:        con *c;
                    565:        int i;
                    566:
1.1.1.21! christos  567:        monotime_ts(&now);
1.1       christos  568:        c = TAILQ_FIRST(&tq);
                    569:
1.1.1.21! christos  570:        if (c && timespeccmp(&c->c_ts, &now, >))
        !           571:                timespecsub(&c->c_ts, &now, &seltime);
1.1.1.19  christos  572:        else
1.1.1.21! christos  573:                timespecclear(&seltime);
1.1       christos  574:
1.1.1.21! christos  575:        while (ppoll(read_wait, maxfd, &seltime, NULL) == -1) {
        !           576:                if (errno == EAGAIN || errno == EINTR)
        !           577:                        continue;
        !           578:                error("poll error");
        !           579:        }
1.1       christos  580:
                    581:        for (i = 0; i < maxfd; i++) {
1.1.1.21! christos  582:                if (read_wait[i].revents & (POLLHUP|POLLERR|POLLNVAL))
1.1       christos  583:                        confree(i);
1.1.1.21! christos  584:                else if (read_wait[i].revents & (POLLIN|POLLHUP))
1.1       christos  585:                        conread(i);
                    586:        }
                    587:
                    588:        c = TAILQ_FIRST(&tq);
1.1.1.21! christos  589:        while (c && timespeccmp(&c->c_ts, &now, <)) {
1.1       christos  590:                int s = c->c_fd;
                    591:
                    592:                c = TAILQ_NEXT(c, c_link);
                    593:                conrecycle(s);
                    594:        }
                    595: }
                    596:
                    597: static void
                    598: do_host(char *host)
                    599: {
                    600:        char *name = strnnsep(&host, " \t\n");
                    601:        int j;
                    602:
                    603:        if (name == NULL)
                    604:                return;
1.1.1.13  christos  605:        for (j = KT_MIN; j <= KT_MAX; j *= 2) {
1.1       christos  606:                if (get_keytypes & j) {
                    607:                        while (ncon >= MAXCON)
                    608:                                conloop();
                    609:                        conalloc(name, *host ? host : name, j);
                    610:                }
                    611:        }
                    612: }
                    613:
                    614: void
1.1.1.20  christos  615: sshfatal(const char *file, const char *func, int line, int showfunc,
                    616:     LogLevel level, const char *suffix, const char *fmt, ...)
1.1       christos  617: {
                    618:        va_list args;
                    619:
                    620:        va_start(args, fmt);
1.1.1.20  christos  621:        sshlogv(file, func, line, showfunc, level, suffix, fmt, args);
1.1       christos  622:        va_end(args);
1.1.1.20  christos  623:        cleanup_exit(255);
1.1       christos  624: }
                    625:
                    626: static void
                    627: usage(void)
                    628: {
                    629:        fprintf(stderr,
1.1.1.14  christos  630:            "usage: %s [-46cDHv] [-f file] [-p port] [-T timeout] [-t type]\n"
                    631:            "\t\t   [host | addrlist namelist]\n",
1.1       christos  632:            __progname);
                    633:        exit(1);
                    634: }
                    635:
                    636: int
                    637: main(int argc, char **argv)
                    638: {
                    639:        int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
1.1.1.2   adam      640:        int opt, fopt_count = 0, j;
1.1.1.15  christos  641:        char *tname, *cp, *line = NULL;
                    642:        size_t linesize = 0;
1.1.1.2   adam      643:        FILE *fp;
1.1       christos  644:
                    645:        extern int optind;
                    646:        extern char *optarg;
                    647:
                    648:        TAILQ_INIT(&tq);
                    649:
                    650:        /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
                    651:        sanitise_stdfd();
                    652:
                    653:        if (argc <= 1)
                    654:                usage();
                    655:
1.1.1.14  christos  656:        while ((opt = getopt(argc, argv, "cDHv46p:T:t:f:")) != -1) {
1.1       christos  657:                switch (opt) {
                    658:                case 'H':
                    659:                        hash_hosts = 1;
                    660:                        break;
1.1.1.10  christos  661:                case 'c':
                    662:                        get_cert = 1;
                    663:                        break;
1.1.1.14  christos  664:                case 'D':
                    665:                        print_sshfp = 1;
                    666:                        break;
1.1       christos  667:                case 'p':
                    668:                        ssh_port = a2port(optarg);
                    669:                        if (ssh_port <= 0) {
                    670:                                fprintf(stderr, "Bad port '%s'\n", optarg);
                    671:                                exit(1);
                    672:                        }
                    673:                        break;
                    674:                case 'T':
                    675:                        timeout = convtime(optarg);
                    676:                        if (timeout == -1 || timeout == 0) {
                    677:                                fprintf(stderr, "Bad timeout '%s'\n", optarg);
                    678:                                usage();
                    679:                        }
                    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.1.1.8   christos  700:                                int type = sshkey_type_from_name(tname);
1.1.1.12  christos  701:
1.1       christos  702:                                switch (type) {
                    703:                                case KEY_DSA:
                    704:                                        get_keytypes |= KT_DSA;
                    705:                                        break;
1.1.1.3   christos  706:                                case KEY_ECDSA:
                    707:                                        get_keytypes |= KT_ECDSA;
                    708:                                        break;
1.1       christos  709:                                case KEY_RSA:
                    710:                                        get_keytypes |= KT_RSA;
                    711:                                        break;
1.1.1.7   christos  712:                                case KEY_ED25519:
                    713:                                        get_keytypes |= KT_ED25519;
                    714:                                        break;
1.1.1.14  christos  715:                                case KEY_XMSS:
                    716:                                        get_keytypes |= KT_XMSS;
                    717:                                        break;
1.1.1.18  christos  718:                                case KEY_ED25519_SK:
                    719:                                        get_keytypes |= KT_ED25519_SK;
                    720:                                        break;
                    721:                                case KEY_ECDSA_SK:
                    722:                                        get_keytypes |= KT_ECDSA_SK;
                    723:                                        break;
1.1       christos  724:                                case KEY_UNSPEC:
1.1.1.12  christos  725:                                default:
                    726:                                        fatal("Unknown key type \"%s\"", tname);
1.1       christos  727:                                }
                    728:                                tname = strtok(NULL, ",");
                    729:                        }
                    730:                        break;
                    731:                case '4':
                    732:                        IPv4or6 = AF_INET;
                    733:                        break;
                    734:                case '6':
                    735:                        IPv4or6 = AF_INET6;
                    736:                        break;
                    737:                case '?':
                    738:                default:
                    739:                        usage();
                    740:                }
                    741:        }
                    742:        if (optind == argc && !fopt_count)
                    743:                usage();
                    744:
                    745:        log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
                    746:
                    747:        maxfd = fdlim_get(1);
                    748:        if (maxfd < 0)
                    749:                fatal("%s: fdlim_get: bad value", __progname);
                    750:        if (maxfd > MAXMAXFD)
                    751:                maxfd = MAXMAXFD;
                    752:        if (MAXCON <= 0)
                    753:                fatal("%s: not enough file descriptors", __progname);
                    754:        if (maxfd > fdlim_get(0))
                    755:                fdlim_set(maxfd);
                    756:        fdcon = xcalloc(maxfd, sizeof(con));
1.1.1.21! christos  757:        read_wait = xcalloc(maxfd, sizeof(struct pollfd));
        !           758:        for (j = 0; j < maxfd; j++)
        !           759:                read_wait[j].fd = -1;
1.1       christos  760:
1.1.1.2   adam      761:        for (j = 0; j < fopt_count; j++) {
                    762:                if (argv[j] == NULL)
                    763:                        fp = stdin;
                    764:                else if ((fp = fopen(argv[j], "r")) == NULL)
1.1.1.20  christos  765:                        fatal("%s: %s: %s", __progname, argv[j], strerror(errno));
1.1.1.2   adam      766:
1.1.1.15  christos  767:                while (getline(&line, &linesize, fp) != -1) {
1.1.1.2   adam      768:                        /* Chomp off trailing whitespace and comments */
                    769:                        if ((cp = strchr(line, '#')) == NULL)
                    770:                                cp = line + strlen(line) - 1;
                    771:                        while (cp >= line) {
                    772:                                if (*cp == ' ' || *cp == '\t' ||
                    773:                                    *cp == '\n' || *cp == '#')
                    774:                                        *cp-- = '\0';
                    775:                                else
                    776:                                        break;
                    777:                        }
                    778:
                    779:                        /* Skip empty lines */
                    780:                        if (*line == '\0')
1.1       christos  781:                                continue;
1.1.1.2   adam      782:
                    783:                        do_host(line);
1.1       christos  784:                }
1.1.1.2   adam      785:
                    786:                if (ferror(fp))
1.1.1.20  christos  787:                        fatal("%s: %s: %s", __progname, argv[j], strerror(errno));
1.1.1.2   adam      788:
                    789:                fclose(fp);
1.1       christos  790:        }
1.1.1.15  christos  791:        free(line);
1.1       christos  792:
                    793:        while (optind < argc)
                    794:                do_host(argv[optind++]);
                    795:
                    796:        while (ncon > 0)
                    797:                conloop();
                    798:
1.1.1.16  christos  799:        return found_one ? 0 : 1;
1.1       christos  800: }

CVSweb <webmaster@jp.NetBSD.org>