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

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