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

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