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

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