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

1.66    ! stevesk     1: /* $OpenBSD: ssh-keyscan.c,v 1.65 2006/07/08 21:47:12 stevesk 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:
                     10: #include "includes.h"
                     11:
1.65      stevesk    12: #include <sys/types.h>
                     13: #include <sys/socket.h>
1.1       markus     14: #include <sys/queue.h>
1.59      stevesk    15: #include <sys/resource.h>
1.58      stevesk    16:
1.1       markus     17: #include <errno.h>
1.58      stevesk    18: #include <setjmp.h>
1.66    ! stevesk    19: #include <stdarg.h>
1.1       markus     20:
1.5       markus     21: #include <openssl/bn.h>
1.1       markus     22:
                     23: #include "xmalloc.h"
                     24: #include "ssh.h"
1.10      markus     25: #include "ssh1.h"
1.1       markus     26: #include "key.h"
1.26      markus     27: #include "kex.h"
                     28: #include "compat.h"
                     29: #include "myproposal.h"
                     30: #include "packet.h"
                     31: #include "dispatch.h"
1.1       markus     32: #include "buffer.h"
                     33: #include "bufaux.h"
1.11      markus     34: #include "log.h"
1.18      deraadt    35: #include "atomicio.h"
1.26      markus     36: #include "misc.h"
1.51      djm        37: #include "hostfile.h"
1.1       markus     38:
1.26      markus     39: /* Flag indicating whether IPv4 or IPv6.  This can be set on the command line.
                     40:    Default value is AF_UNSPEC means both IPv4 and IPv6. */
                     41: int IPv4or6 = AF_UNSPEC;
                     42:
                     43: int ssh_port = SSH_DEFAULT_PORT;
1.1       markus     44:
1.26      markus     45: #define KT_RSA1        1
                     46: #define KT_DSA 2
                     47: #define KT_RSA 4
                     48:
                     49: int get_keytypes = KT_RSA1;    /* Get only RSA1 keys by default */
1.1       markus     50:
1.51      djm        51: int hash_hosts = 0;            /* Hash hostname on output */
                     52:
1.1       markus     53: #define MAXMAXFD 256
                     54:
                     55: /* The number of seconds after which to give up on a TCP connection */
                     56: int timeout = 5;
                     57:
                     58: int maxfd;
1.18      deraadt    59: #define MAXCON (maxfd - 10)
1.1       markus     60:
1.3       markus     61: extern char *__progname;
1.19      millert    62: fd_set *read_wait;
1.63      djm        63: size_t read_wait_nfdset;
1.1       markus     64: int ncon;
1.26      markus     65: int nonfatal_fatal = 0;
                     66: jmp_buf kexjmp;
1.29      markus     67: Key *kexjmp_key;
1.1       markus     68:
                     69: /*
                     70:  * Keep a connection structure for each file descriptor.  The state
                     71:  * associated with file descriptor n is held in fdcon[n].
                     72:  */
                     73: typedef struct Connection {
1.6       markus     74:        u_char c_status;        /* State of connection on this file desc. */
1.1       markus     75: #define CS_UNUSED 0            /* File descriptor unused */
                     76: #define CS_CON 1               /* Waiting to connect/read greeting */
                     77: #define CS_SIZE 2              /* Waiting to read initial packet size */
                     78: #define CS_KEYS 3              /* Waiting to read public key packet */
                     79:        int c_fd;               /* Quick lookup: c->c_fd == c - fdcon */
                     80:        int c_plen;             /* Packet length field for ssh packet */
                     81:        int c_len;              /* Total bytes which must be read. */
                     82:        int c_off;              /* Length of data read so far. */
1.26      markus     83:        int c_keytype;          /* Only one of KT_RSA1, KT_DSA, or KT_RSA */
1.1       markus     84:        char *c_namebase;       /* Address to free for c_name and c_namelist */
                     85:        char *c_name;           /* Hostname of connection for errors */
                     86:        char *c_namelist;       /* Pointer to other possible addresses */
                     87:        char *c_output_name;    /* Hostname of connection for output */
                     88:        char *c_data;           /* Data read from this fd */
1.26      markus     89:        Kex *c_kex;             /* The key-exchange struct for ssh2 */
1.1       markus     90:        struct timeval c_tv;    /* Time at which connection gets aborted */
                     91:        TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */
                     92: } con;
                     93:
                     94: TAILQ_HEAD(conlist, Connection) tq;    /* Timeout Queue */
                     95: con *fdcon;
                     96:
                     97: /*
                     98:  *  This is just a wrapper around fgets() to make it usable.
                     99:  */
                    100:
                    101: /* Stress-test.  Increase this later. */
                    102: #define LINEBUF_SIZE 16
                    103:
                    104: typedef struct {
                    105:        char *buf;
1.6       markus    106:        u_int size;
1.1       markus    107:        int lineno;
                    108:        const char *filename;
                    109:        FILE *stream;
                    110:        void (*errfun) (const char *,...);
                    111: } Linebuf;
                    112:
1.24      itojun    113: static Linebuf *
1.1       markus    114: Linebuf_alloc(const char *filename, void (*errfun) (const char *,...))
                    115: {
                    116:        Linebuf *lb;
                    117:
                    118:        if (!(lb = malloc(sizeof(*lb)))) {
                    119:                if (errfun)
1.37      markus    120:                        (*errfun) ("linebuf (%s): malloc failed\n",
                    121:                            filename ? filename : "(stdin)");
1.1       markus    122:                return (NULL);
                    123:        }
                    124:        if (filename) {
                    125:                lb->filename = filename;
                    126:                if (!(lb->stream = fopen(filename, "r"))) {
1.9       markus    127:                        xfree(lb);
1.1       markus    128:                        if (errfun)
                    129:                                (*errfun) ("%s: %s\n", filename, strerror(errno));
                    130:                        return (NULL);
                    131:                }
                    132:        } else {
                    133:                lb->filename = "(stdin)";
                    134:                lb->stream = stdin;
                    135:        }
                    136:
1.62      deraadt   137:        if (!(lb->buf = malloc((lb->size = LINEBUF_SIZE)))) {
1.1       markus    138:                if (errfun)
                    139:                        (*errfun) ("linebuf (%s): malloc failed\n", lb->filename);
1.9       markus    140:                xfree(lb);
1.1       markus    141:                return (NULL);
                    142:        }
                    143:        lb->errfun = errfun;
                    144:        lb->lineno = 0;
                    145:        return (lb);
                    146: }
                    147:
1.24      itojun    148: static void
1.1       markus    149: Linebuf_free(Linebuf * lb)
                    150: {
                    151:        fclose(lb->stream);
1.9       markus    152:        xfree(lb->buf);
                    153:        xfree(lb);
1.1       markus    154: }
                    155:
1.24      itojun    156: #if 0
                    157: static void
1.1       markus    158: Linebuf_restart(Linebuf * lb)
                    159: {
                    160:        clearerr(lb->stream);
                    161:        rewind(lb->stream);
                    162:        lb->lineno = 0;
                    163: }
                    164:
1.24      itojun    165: static int
1.1       markus    166: Linebuf_lineno(Linebuf * lb)
                    167: {
                    168:        return (lb->lineno);
                    169: }
1.24      itojun    170: #endif
1.1       markus    171:
1.24      itojun    172: static char *
1.14      markus    173: Linebuf_getline(Linebuf * lb)
1.1       markus    174: {
1.55      djm       175:        size_t n = 0;
1.39      deraadt   176:        void *p;
1.1       markus    177:
                    178:        lb->lineno++;
                    179:        for (;;) {
                    180:                /* Read a line */
                    181:                if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) {
                    182:                        if (ferror(lb->stream) && lb->errfun)
1.39      deraadt   183:                                (*lb->errfun)("%s: %s\n", lb->filename,
1.17      deraadt   184:                                    strerror(errno));
1.1       markus    185:                        return (NULL);
                    186:                }
                    187:                n = strlen(lb->buf);
                    188:
                    189:                /* Return it or an error if it fits */
                    190:                if (n > 0 && lb->buf[n - 1] == '\n') {
                    191:                        lb->buf[n - 1] = '\0';
                    192:                        return (lb->buf);
                    193:                }
                    194:                if (n != lb->size - 1) {
                    195:                        if (lb->errfun)
1.39      deraadt   196:                                (*lb->errfun)("%s: skipping incomplete last line\n",
1.17      deraadt   197:                                    lb->filename);
1.1       markus    198:                        return (NULL);
                    199:                }
                    200:                /* Double the buffer if we need more space */
1.39      deraadt   201:                lb->size *= 2;
                    202:                if ((p = realloc(lb->buf, lb->size)) == NULL) {
                    203:                        lb->size /= 2;
1.1       markus    204:                        if (lb->errfun)
1.39      deraadt   205:                                (*lb->errfun)("linebuf (%s): realloc failed\n",
1.17      deraadt   206:                                    lb->filename);
1.1       markus    207:                        return (NULL);
                    208:                }
1.39      deraadt   209:                lb->buf = p;
1.1       markus    210:        }
                    211: }
                    212:
1.24      itojun    213: static int
1.1       markus    214: fdlim_get(int hard)
                    215: {
                    216:        struct rlimit rlfd;
1.17      deraadt   217:
1.1       markus    218:        if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
                    219:                return (-1);
                    220:        if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY)
1.46      djm       221:                return sysconf(_SC_OPEN_MAX);
1.1       markus    222:        else
                    223:                return hard ? rlfd.rlim_max : rlfd.rlim_cur;
                    224: }
                    225:
1.24      itojun    226: static int
1.1       markus    227: fdlim_set(int lim)
                    228: {
                    229:        struct rlimit rlfd;
1.39      deraadt   230:
1.1       markus    231:        if (lim <= 0)
                    232:                return (-1);
                    233:        if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0)
                    234:                return (-1);
                    235:        rlfd.rlim_cur = lim;
                    236:        if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0)
                    237:                return (-1);
                    238:        return (0);
                    239: }
                    240:
                    241: /*
                    242:  * This is an strsep function that returns a null field for adjacent
                    243:  * separators.  This is the same as the 4.4BSD strsep, but different from the
                    244:  * one in the GNU libc.
                    245:  */
1.24      itojun    246: static char *
1.1       markus    247: xstrsep(char **str, const char *delim)
                    248: {
                    249:        char *s, *e;
                    250:
                    251:        if (!**str)
                    252:                return (NULL);
                    253:
                    254:        s = *str;
                    255:        e = s + strcspn(s, delim);
                    256:
                    257:        if (*e != '\0')
                    258:                *e++ = '\0';
                    259:        *str = e;
                    260:
                    261:        return (s);
                    262: }
                    263:
                    264: /*
                    265:  * Get the next non-null token (like GNU strsep).  Strsep() will return a
                    266:  * null token for two adjacent separators, so we may have to loop.
                    267:  */
1.24      itojun    268: static char *
1.1       markus    269: strnnsep(char **stringp, char *delim)
                    270: {
                    271:        char *tok;
                    272:
                    273:        do {
                    274:                tok = xstrsep(stringp, delim);
                    275:        } while (tok && *tok == '\0');
                    276:        return (tok);
                    277: }
                    278:
1.26      markus    279: static Key *
                    280: keygrab_ssh1(con *c)
1.1       markus    281: {
                    282:        static Key *rsa;
                    283:        static Buffer msg;
                    284:
                    285:        if (rsa == NULL) {
                    286:                buffer_init(&msg);
                    287:                rsa = key_new(KEY_RSA1);
                    288:        }
1.26      markus    289:        buffer_append(&msg, c->c_data, c->c_plen);
                    290:        buffer_consume(&msg, 8 - (c->c_plen & 7));      /* padding */
1.1       markus    291:        if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) {
1.26      markus    292:                error("%s: invalid packet type", c->c_name);
1.1       markus    293:                buffer_clear(&msg);
1.26      markus    294:                return NULL;
1.1       markus    295:        }
                    296:        buffer_consume(&msg, 8);                /* cookie */
                    297:
                    298:        /* server key */
                    299:        (void) buffer_get_int(&msg);
                    300:        buffer_get_bignum(&msg, rsa->rsa->e);
                    301:        buffer_get_bignum(&msg, rsa->rsa->n);
                    302:
                    303:        /* host key */
                    304:        (void) buffer_get_int(&msg);
                    305:        buffer_get_bignum(&msg, rsa->rsa->e);
                    306:        buffer_get_bignum(&msg, rsa->rsa->n);
1.26      markus    307:
1.1       markus    308:        buffer_clear(&msg);
                    309:
1.26      markus    310:        return (rsa);
                    311: }
                    312:
                    313: static int
                    314: hostjump(Key *hostkey)
                    315: {
1.29      markus    316:        kexjmp_key = hostkey;
                    317:        longjmp(kexjmp, 1);
1.26      markus    318: }
                    319:
                    320: static int
                    321: ssh2_capable(int remote_major, int remote_minor)
                    322: {
                    323:        switch (remote_major) {
                    324:        case 1:
                    325:                if (remote_minor == 99)
                    326:                        return 1;
                    327:                break;
                    328:        case 2:
                    329:                return 1;
                    330:        default:
                    331:                break;
                    332:        }
                    333:        return 0;
                    334: }
                    335:
                    336: static Key *
                    337: keygrab_ssh2(con *c)
                    338: {
                    339:        int j;
                    340:
                    341:        packet_set_connection(c->c_fd, c->c_fd);
                    342:        enable_compat20();
                    343:        myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = c->c_keytype == KT_DSA?
                    344:            "ssh-dss": "ssh-rsa";
                    345:        c->c_kex = kex_setup(myproposal);
1.41      markus    346:        c->c_kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
1.48      djm       347:        c->c_kex->kex[KEX_DH_GRP14_SHA1] = kexdh_client;
1.41      markus    348:        c->c_kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
1.60      djm       349:        c->c_kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
1.26      markus    350:        c->c_kex->verify_host_key = hostjump;
                    351:
                    352:        if (!(j = setjmp(kexjmp))) {
                    353:                nonfatal_fatal = 1;
                    354:                dispatch_run(DISPATCH_BLOCK, &c->c_kex->done, c->c_kex);
                    355:                fprintf(stderr, "Impossible! dispatch_run() returned!\n");
                    356:                exit(1);
                    357:        }
                    358:        nonfatal_fatal = 0;
                    359:        xfree(c->c_kex);
                    360:        c->c_kex = NULL;
                    361:        packet_close();
                    362:
1.29      markus    363:        return j < 0? NULL : kexjmp_key;
1.26      markus    364: }
                    365:
                    366: static void
                    367: keyprint(con *c, Key *key)
                    368: {
1.51      djm       369:        char *host = c->c_output_name ? c->c_output_name : c->c_name;
                    370:
1.26      markus    371:        if (!key)
                    372:                return;
1.51      djm       373:        if (hash_hosts && (host = host_hash(host, NULL, 0)) == NULL)
                    374:                fatal("host_hash failed");
1.26      markus    375:
1.51      djm       376:        fprintf(stdout, "%s ", host);
1.26      markus    377:        key_write(key, stdout);
1.1       markus    378:        fputs("\n", stdout);
                    379: }
                    380:
1.24      itojun    381: static int
1.1       markus    382: tcpconnect(char *host)
                    383: {
                    384:        struct addrinfo hints, *ai, *aitop;
                    385:        char strport[NI_MAXSERV];
                    386:        int gaierr, s = -1;
                    387:
1.26      markus    388:        snprintf(strport, sizeof strport, "%d", ssh_port);
1.1       markus    389:        memset(&hints, 0, sizeof(hints));
1.26      markus    390:        hints.ai_family = IPv4or6;
1.1       markus    391:        hints.ai_socktype = SOCK_STREAM;
                    392:        if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0)
                    393:                fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr));
                    394:        for (ai = aitop; ai; ai = ai->ai_next) {
1.42      markus    395:                s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
1.1       markus    396:                if (s < 0) {
                    397:                        error("socket: %s", strerror(errno));
                    398:                        continue;
                    399:                }
1.49      djm       400:                if (set_nonblock(s) == -1)
                    401:                        fatal("%s: set_nonblock(%d)", __func__, s);
1.1       markus    402:                if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 &&
                    403:                    errno != EINPROGRESS)
                    404:                        error("connect (`%s'): %s", host, strerror(errno));
                    405:                else
                    406:                        break;
                    407:                close(s);
                    408:                s = -1;
                    409:        }
                    410:        freeaddrinfo(aitop);
                    411:        return s;
                    412: }
                    413:
1.24      itojun    414: static int
1.26      markus    415: conalloc(char *iname, char *oname, int keytype)
1.1       markus    416: {
1.39      deraadt   417:        char *namebase, *name, *namelist;
1.1       markus    418:        int s;
                    419:
                    420:        namebase = namelist = xstrdup(iname);
                    421:
                    422:        do {
                    423:                name = xstrsep(&namelist, ",");
                    424:                if (!name) {
1.9       markus    425:                        xfree(namebase);
1.1       markus    426:                        return (-1);
                    427:                }
                    428:        } while ((s = tcpconnect(name)) < 0);
                    429:
                    430:        if (s >= maxfd)
1.4       markus    431:                fatal("conalloc: fdno %d too high", s);
1.1       markus    432:        if (fdcon[s].c_status)
1.4       markus    433:                fatal("conalloc: attempt to reuse fdno %d", s);
1.1       markus    434:
                    435:        fdcon[s].c_fd = s;
                    436:        fdcon[s].c_status = CS_CON;
                    437:        fdcon[s].c_namebase = namebase;
                    438:        fdcon[s].c_name = name;
                    439:        fdcon[s].c_namelist = namelist;
                    440:        fdcon[s].c_output_name = xstrdup(oname);
                    441:        fdcon[s].c_data = (char *) &fdcon[s].c_plen;
                    442:        fdcon[s].c_len = 4;
                    443:        fdcon[s].c_off = 0;
1.26      markus    444:        fdcon[s].c_keytype = keytype;
1.1       markus    445:        gettimeofday(&fdcon[s].c_tv, NULL);
                    446:        fdcon[s].c_tv.tv_sec += timeout;
                    447:        TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
1.19      millert   448:        FD_SET(s, read_wait);
1.1       markus    449:        ncon++;
                    450:        return (s);
                    451: }
                    452:
1.24      itojun    453: static void
1.1       markus    454: confree(int s)
                    455: {
                    456:        if (s >= maxfd || fdcon[s].c_status == CS_UNUSED)
1.4       markus    457:                fatal("confree: attempt to free bad fdno %d", s);
1.18      deraadt   458:        close(s);
1.9       markus    459:        xfree(fdcon[s].c_namebase);
                    460:        xfree(fdcon[s].c_output_name);
1.1       markus    461:        if (fdcon[s].c_status == CS_KEYS)
1.9       markus    462:                xfree(fdcon[s].c_data);
1.1       markus    463:        fdcon[s].c_status = CS_UNUSED;
1.26      markus    464:        fdcon[s].c_keytype = 0;
1.1       markus    465:        TAILQ_REMOVE(&tq, &fdcon[s], c_link);
1.19      millert   466:        FD_CLR(s, read_wait);
1.1       markus    467:        ncon--;
                    468: }
                    469:
1.24      itojun    470: static void
1.1       markus    471: contouch(int s)
                    472: {
                    473:        TAILQ_REMOVE(&tq, &fdcon[s], c_link);
                    474:        gettimeofday(&fdcon[s].c_tv, NULL);
                    475:        fdcon[s].c_tv.tv_sec += timeout;
                    476:        TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link);
                    477: }
                    478:
1.24      itojun    479: static int
1.1       markus    480: conrecycle(int s)
                    481: {
1.39      deraadt   482:        con *c = &fdcon[s];
1.1       markus    483:        int ret;
                    484:
1.26      markus    485:        ret = conalloc(c->c_namelist, c->c_output_name, c->c_keytype);
1.1       markus    486:        confree(s);
                    487:        return (ret);
                    488: }
                    489:
1.24      itojun    490: static void
1.1       markus    491: congreet(int s)
                    492: {
1.55      djm       493:        int n = 0, remote_major = 0, remote_minor = 0;
1.26      markus    494:        char buf[256], *cp;
1.33      markus    495:        char remote_version[sizeof buf];
1.55      djm       496:        size_t bufsiz;
1.1       markus    497:        con *c = &fdcon[s];
                    498:
1.57      djm       499:        for (;;) {
                    500:                memset(buf, '\0', sizeof(buf));
                    501:                bufsiz = sizeof(buf);
                    502:                cp = buf;
                    503:                while (bufsiz-- &&
                    504:                    (n = atomicio(read, s, cp, 1)) == 1 && *cp != '\n') {
                    505:                        if (*cp == '\r')
                    506:                                *cp = '\n';
                    507:                        cp++;
                    508:                }
                    509:                if (n != 1 || strncmp(buf, "SSH-", 4) == 0)
                    510:                        break;
1.27      markus    511:        }
1.54      avsm      512:        if (n == 0) {
                    513:                switch (errno) {
                    514:                case EPIPE:
                    515:                        error("%s: Connection closed by remote host", c->c_name);
                    516:                        break;
                    517:                case ECONNREFUSED:
                    518:                        break;
                    519:                default:
1.1       markus    520:                        error("read (%s): %s", c->c_name, strerror(errno));
1.54      avsm      521:                        break;
                    522:                }
1.1       markus    523:                conrecycle(s);
                    524:                return;
                    525:        }
1.21      millert   526:        if (*cp != '\n' && *cp != '\r') {
1.1       markus    527:                error("%s: bad greeting", c->c_name);
                    528:                confree(s);
                    529:                return;
                    530:        }
1.21      millert   531:        *cp = '\0';
1.33      markus    532:        if (sscanf(buf, "SSH-%d.%d-%[^\n]\n",
                    533:            &remote_major, &remote_minor, remote_version) == 3)
                    534:                compat_datafellows(remote_version);
                    535:        else
                    536:                datafellows = 0;
1.26      markus    537:        if (c->c_keytype != KT_RSA1) {
                    538:                if (!ssh2_capable(remote_major, remote_minor)) {
                    539:                        debug("%s doesn't support ssh2", c->c_name);
                    540:                        confree(s);
                    541:                        return;
                    542:                }
1.33      markus    543:        } else if (remote_major != 1) {
                    544:                debug("%s doesn't support ssh1", c->c_name);
                    545:                confree(s);
                    546:                return;
1.26      markus    547:        }
1.27      markus    548:        fprintf(stderr, "# %s %s\n", c->c_name, chop(buf));
1.26      markus    549:        n = snprintf(buf, sizeof buf, "SSH-%d.%d-OpenSSH-keyscan\r\n",
                    550:            c->c_keytype == KT_RSA1? PROTOCOL_MAJOR_1 : PROTOCOL_MAJOR_2,
                    551:            c->c_keytype == KT_RSA1? PROTOCOL_MINOR_1 : PROTOCOL_MINOR_2);
1.55      djm       552:        if (n < 0 || (size_t)n >= sizeof(buf)) {
1.53      moritz    553:                error("snprintf: buffer too small");
                    554:                confree(s);
                    555:                return;
                    556:        }
1.55      djm       557:        if (atomicio(vwrite, s, buf, n) != (size_t)n) {
1.1       markus    558:                error("write (%s): %s", c->c_name, strerror(errno));
                    559:                confree(s);
                    560:                return;
                    561:        }
1.26      markus    562:        if (c->c_keytype != KT_RSA1) {
                    563:                keyprint(c, keygrab_ssh2(c));
                    564:                confree(s);
                    565:                return;
                    566:        }
1.1       markus    567:        c->c_status = CS_SIZE;
                    568:        contouch(s);
                    569: }
                    570:
1.24      itojun    571: static void
1.1       markus    572: conread(int s)
                    573: {
1.39      deraadt   574:        con *c = &fdcon[s];
1.54      avsm      575:        size_t n;
1.1       markus    576:
                    577:        if (c->c_status == CS_CON) {
                    578:                congreet(s);
                    579:                return;
                    580:        }
1.50      avsm      581:        n = atomicio(read, s, c->c_data + c->c_off, c->c_len - c->c_off);
1.54      avsm      582:        if (n == 0) {
1.1       markus    583:                error("read (%s): %s", c->c_name, strerror(errno));
                    584:                confree(s);
                    585:                return;
                    586:        }
                    587:        c->c_off += n;
                    588:
                    589:        if (c->c_off == c->c_len)
                    590:                switch (c->c_status) {
                    591:                case CS_SIZE:
                    592:                        c->c_plen = htonl(c->c_plen);
                    593:                        c->c_len = c->c_plen + 8 - (c->c_plen & 7);
                    594:                        c->c_off = 0;
                    595:                        c->c_data = xmalloc(c->c_len);
                    596:                        c->c_status = CS_KEYS;
                    597:                        break;
                    598:                case CS_KEYS:
1.26      markus    599:                        keyprint(c, keygrab_ssh1(c));
1.1       markus    600:                        confree(s);
                    601:                        return;
                    602:                default:
1.4       markus    603:                        fatal("conread: invalid status %d", c->c_status);
1.1       markus    604:                        break;
                    605:                }
                    606:
                    607:        contouch(s);
                    608: }
                    609:
1.24      itojun    610: static void
1.1       markus    611: conloop(void)
                    612: {
1.39      deraadt   613:        struct timeval seltime, now;
1.19      millert   614:        fd_set *r, *e;
1.39      deraadt   615:        con *c;
1.1       markus    616:        int i;
                    617:
                    618:        gettimeofday(&now, NULL);
1.36      itojun    619:        c = TAILQ_FIRST(&tq);
1.1       markus    620:
1.18      deraadt   621:        if (c && (c->c_tv.tv_sec > now.tv_sec ||
                    622:            (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) {
1.1       markus    623:                seltime = c->c_tv;
                    624:                seltime.tv_sec -= now.tv_sec;
                    625:                seltime.tv_usec -= now.tv_usec;
1.13      itojun    626:                if (seltime.tv_usec < 0) {
1.1       markus    627:                        seltime.tv_usec += 1000000;
                    628:                        seltime.tv_sec--;
                    629:                }
                    630:        } else
                    631:                seltime.tv_sec = seltime.tv_usec = 0;
                    632:
1.63      djm       633:        r = xcalloc(read_wait_nfdset, sizeof(fd_mask));
                    634:        e = xcalloc(read_wait_nfdset, sizeof(fd_mask));
                    635:        memcpy(r, read_wait, read_wait_nfdset * sizeof(fd_mask));
                    636:        memcpy(e, read_wait, read_wait_nfdset * sizeof(fd_mask));
1.19      millert   637:
                    638:        while (select(maxfd, r, NULL, e, &seltime) == -1 &&
1.16      deraadt   639:            (errno == EAGAIN || errno == EINTR))
                    640:                ;
                    641:
1.18      deraadt   642:        for (i = 0; i < maxfd; i++) {
1.19      millert   643:                if (FD_ISSET(i, e)) {
1.1       markus    644:                        error("%s: exception!", fdcon[i].c_name);
                    645:                        confree(i);
1.19      millert   646:                } else if (FD_ISSET(i, r))
1.1       markus    647:                        conread(i);
1.18      deraadt   648:        }
1.19      millert   649:        xfree(r);
                    650:        xfree(e);
1.1       markus    651:
1.36      itojun    652:        c = TAILQ_FIRST(&tq);
1.18      deraadt   653:        while (c && (c->c_tv.tv_sec < now.tv_sec ||
                    654:            (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) {
1.1       markus    655:                int s = c->c_fd;
1.18      deraadt   656:
1.36      itojun    657:                c = TAILQ_NEXT(c, c_link);
1.1       markus    658:                conrecycle(s);
                    659:        }
                    660: }
                    661:
1.26      markus    662: static void
                    663: do_host(char *host)
1.1       markus    664: {
1.26      markus    665:        char *name = strnnsep(&host, " \t\n");
                    666:        int j;
1.1       markus    667:
1.31      markus    668:        if (name == NULL)
                    669:                return;
1.26      markus    670:        for (j = KT_RSA1; j <= KT_RSA; j *= 2) {
                    671:                if (get_keytypes & j) {
                    672:                        while (ncon >= MAXCON)
                    673:                                conloop();
                    674:                        conalloc(name, *host ? host : name, j);
1.1       markus    675:                }
                    676:        }
                    677: }
                    678:
1.34      markus    679: void
                    680: fatal(const char *fmt,...)
1.26      markus    681: {
1.34      markus    682:        va_list args;
1.39      deraadt   683:
1.34      markus    684:        va_start(args, fmt);
                    685:        do_log(SYSLOG_LEVEL_FATAL, fmt, args);
                    686:        va_end(args);
1.26      markus    687:        if (nonfatal_fatal)
                    688:                longjmp(kexjmp, -1);
1.34      markus    689:        else
1.45      markus    690:                exit(255);
1.26      markus    691: }
                    692:
                    693: static void
1.1       markus    694: usage(void)
                    695: {
1.52      jmc       696:        fprintf(stderr, "usage: %s [-46Hv] [-f file] [-p port] [-T timeout] [-t type]\n"
1.39      deraadt   697:            "\t\t   [host | addrlist namelist] [...]\n",
1.25      jakob     698:            __progname);
                    699:        exit(1);
1.1       markus    700: }
                    701:
                    702: int
                    703: main(int argc, char **argv)
                    704: {
1.26      markus    705:        int debug_flag = 0, log_level = SYSLOG_LEVEL_INFO;
                    706:        int opt, fopt_count = 0;
                    707:        char *tname;
                    708:
                    709:        extern int optind;
                    710:        extern char *optarg;
1.1       markus    711:
                    712:        TAILQ_INIT(&tq);
1.56      djm       713:
                    714:        /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
                    715:        sanitise_stdfd();
1.1       markus    716:
1.26      markus    717:        if (argc <= 1)
1.1       markus    718:                usage();
                    719:
1.51      djm       720:        while ((opt = getopt(argc, argv, "Hv46p:T:t:f:")) != -1) {
1.26      markus    721:                switch (opt) {
1.51      djm       722:                case 'H':
                    723:                        hash_hosts = 1;
                    724:                        break;
1.26      markus    725:                case 'p':
                    726:                        ssh_port = a2port(optarg);
                    727:                        if (ssh_port == 0) {
                    728:                                fprintf(stderr, "Bad port '%s'\n", optarg);
                    729:                                exit(1);
                    730:                        }
                    731:                        break;
                    732:                case 'T':
1.38      stevesk   733:                        timeout = convtime(optarg);
                    734:                        if (timeout == -1 || timeout == 0) {
                    735:                                fprintf(stderr, "Bad timeout '%s'\n", optarg);
1.1       markus    736:                                usage();
1.38      stevesk   737:                        }
1.26      markus    738:                        break;
                    739:                case 'v':
                    740:                        if (!debug_flag) {
                    741:                                debug_flag = 1;
                    742:                                log_level = SYSLOG_LEVEL_DEBUG1;
                    743:                        }
                    744:                        else if (log_level < SYSLOG_LEVEL_DEBUG3)
                    745:                                log_level++;
                    746:                        else
                    747:                                fatal("Too high debugging level.");
                    748:                        break;
                    749:                case 'f':
                    750:                        if (strcmp(optarg, "-") == 0)
                    751:                                optarg = NULL;
                    752:                        argv[fopt_count++] = optarg;
                    753:                        break;
                    754:                case 't':
                    755:                        get_keytypes = 0;
                    756:                        tname = strtok(optarg, ",");
                    757:                        while (tname) {
                    758:                                int type = key_type_from_name(tname);
                    759:                                switch (type) {
                    760:                                case KEY_RSA1:
                    761:                                        get_keytypes |= KT_RSA1;
                    762:                                        break;
                    763:                                case KEY_DSA:
                    764:                                        get_keytypes |= KT_DSA;
                    765:                                        break;
                    766:                                case KEY_RSA:
                    767:                                        get_keytypes |= KT_RSA;
                    768:                                        break;
                    769:                                case KEY_UNSPEC:
1.32      stevesk   770:                                        fatal("unknown key type %s", tname);
1.26      markus    771:                                }
                    772:                                tname = strtok(NULL, ",");
                    773:                        }
                    774:                        break;
                    775:                case '4':
                    776:                        IPv4or6 = AF_INET;
                    777:                        break;
                    778:                case '6':
                    779:                        IPv4or6 = AF_INET6;
                    780:                        break;
                    781:                case '?':
                    782:                default:
                    783:                        usage();
1.1       markus    784:                }
                    785:        }
1.26      markus    786:        if (optind == argc && !fopt_count)
1.1       markus    787:                usage();
                    788:
1.26      markus    789:        log_init("ssh-keyscan", log_level, SYSLOG_FACILITY_USER, 1);
                    790:
1.1       markus    791:        maxfd = fdlim_get(1);
                    792:        if (maxfd < 0)
1.4       markus    793:                fatal("%s: fdlim_get: bad value", __progname);
1.1       markus    794:        if (maxfd > MAXMAXFD)
                    795:                maxfd = MAXMAXFD;
1.18      deraadt   796:        if (MAXCON <= 0)
1.4       markus    797:                fatal("%s: not enough file descriptors", __progname);
1.1       markus    798:        if (maxfd > fdlim_get(0))
                    799:                fdlim_set(maxfd);
1.63      djm       800:        fdcon = xcalloc(maxfd, sizeof(con));
1.19      millert   801:
1.63      djm       802:        read_wait_nfdset = howmany(maxfd, NFDBITS);
                    803:        read_wait = xcalloc(read_wait_nfdset, sizeof(fd_mask));
1.1       markus    804:
1.26      markus    805:        if (fopt_count) {
                    806:                Linebuf *lb;
                    807:                char *line;
                    808:                int j;
                    809:
                    810:                for (j = 0; j < fopt_count; j++) {
                    811:                        lb = Linebuf_alloc(argv[j], error);
1.28      danh      812:                        if (!lb)
                    813:                                continue;
1.26      markus    814:                        while ((line = Linebuf_getline(lb)) != NULL)
                    815:                                do_host(line);
                    816:                        Linebuf_free(lb);
                    817:                }
                    818:        }
                    819:
                    820:        while (optind < argc)
                    821:                do_host(argv[optind++]);
1.1       markus    822:
                    823:        while (ncon > 0)
                    824:                conloop();
                    825:
                    826:        return (0);
                    827: }