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

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