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

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