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

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

CVSweb <webmaster@jp.NetBSD.org>