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

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