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

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