[BACK]Return to hostfile.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/hostfile.c, Revision 1.60

1.60    ! djm         1: /* $OpenBSD: hostfile.c,v 1.59 2015/01/15 09:40:00 djm Exp $ */
1.1       deraadt     2: /*
1.8       deraadt     3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
                      6:  * Functions for manipulating the known hosts files.
1.16      markus      7:  *
1.20      deraadt     8:  * As far as I am concerned, the code I have written for this software
                      9:  * can be used freely for any purpose.  Any derived versions of this
                     10:  * software must be clearly marked as such, and if the derived work is
                     11:  * incompatible with the protocol description in the RFC file, it must be
                     12:  * called by a name other than "ssh" or "Secure Shell".
                     13:  *
                     14:  *
1.28      markus     15:  * Copyright (c) 1999, 2000 Markus Friedl.  All rights reserved.
1.20      deraadt    16:  * Copyright (c) 1999 Niels Provos.  All rights reserved.
                     17:  *
                     18:  * Redistribution and use in source and binary forms, with or without
                     19:  * modification, are permitted provided that the following conditions
                     20:  * are met:
                     21:  * 1. Redistributions of source code must retain the above copyright
                     22:  *    notice, this list of conditions and the following disclaimer.
                     23:  * 2. Redistributions in binary form must reproduce the above copyright
                     24:  *    notice, this list of conditions and the following disclaimer in the
                     25:  *    documentation and/or other materials provided with the distribution.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     28:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     29:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     30:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     31:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     32:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     33:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     34:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     35:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     36:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.8       deraadt    37:  */
1.1       deraadt    38:
1.41      stevesk    39: #include <sys/types.h>
                     40:
                     41: #include <netinet/in.h>
1.33      djm        42:
1.60    ! djm        43: #include <errno.h>
1.33      djm        44: #include <resolv.h>
1.44      stevesk    45: #include <stdio.h>
1.43      stevesk    46: #include <stdlib.h>
1.42      stevesk    47: #include <string.h>
1.57      djm        48: #include <stdarg.h>
1.1       deraadt    49:
1.45      deraadt    50: #include "xmalloc.h"
1.14      markus     51: #include "match.h"
1.59      djm        52: #include "sshkey.h"
1.14      markus     53: #include "hostfile.h"
1.24      markus     54: #include "log.h"
1.49      djm        55: #include "misc.h"
1.59      djm        56: #include "ssherr.h"
1.53      djm        57: #include "digest.h"
1.54      markus     58: #include "hmac.h"
1.49      djm        59:
                     60: struct hostkeys {
                     61:        struct hostkey_entry *entries;
                     62:        u_int num_entries;
                     63: };
1.33      djm        64:
1.60    ! djm        65: /* XXX hmac is too easy to dictionary attack; use bcrypt? */
        !            66:
1.33      djm        67: static int
1.52      djm        68: extract_salt(const char *s, u_int l, u_char *salt, size_t salt_len)
1.33      djm        69: {
                     70:        char *p, *b64salt;
                     71:        u_int b64len;
                     72:        int ret;
                     73:
                     74:        if (l < sizeof(HASH_MAGIC) - 1) {
                     75:                debug2("extract_salt: string too short");
                     76:                return (-1);
                     77:        }
                     78:        if (strncmp(s, HASH_MAGIC, sizeof(HASH_MAGIC) - 1) != 0) {
                     79:                debug2("extract_salt: invalid magic identifier");
                     80:                return (-1);
                     81:        }
                     82:        s += sizeof(HASH_MAGIC) - 1;
                     83:        l -= sizeof(HASH_MAGIC) - 1;
                     84:        if ((p = memchr(s, HASH_DELIM, l)) == NULL) {
                     85:                debug2("extract_salt: missing salt termination character");
                     86:                return (-1);
                     87:        }
                     88:
                     89:        b64len = p - s;
                     90:        /* Sanity check */
                     91:        if (b64len == 0 || b64len > 1024) {
                     92:                debug2("extract_salt: bad encoded salt length %u", b64len);
                     93:                return (-1);
                     94:        }
                     95:        b64salt = xmalloc(1 + b64len);
                     96:        memcpy(b64salt, s, b64len);
                     97:        b64salt[b64len] = '\0';
                     98:
                     99:        ret = __b64_pton(b64salt, salt, salt_len);
1.51      djm       100:        free(b64salt);
1.33      djm       101:        if (ret == -1) {
                    102:                debug2("extract_salt: salt decode error");
                    103:                return (-1);
                    104:        }
1.54      markus    105:        if (ret != (int)ssh_hmac_bytes(SSH_DIGEST_SHA1)) {
                    106:                debug2("extract_salt: expected salt len %zd, got %d",
                    107:                    ssh_hmac_bytes(SSH_DIGEST_SHA1), ret);
1.33      djm       108:                return (-1);
                    109:        }
1.34      deraadt   110:
1.33      djm       111:        return (0);
                    112: }
                    113:
                    114: char *
                    115: host_hash(const char *host, const char *name_from_hostfile, u_int src_len)
                    116: {
1.54      markus    117:        struct ssh_hmac_ctx *ctx;
1.52      djm       118:        u_char salt[256], result[256];
                    119:        char uu_salt[512], uu_result[512];
1.33      djm       120:        static char encoded[1024];
                    121:        u_int i, len;
                    122:
1.54      markus    123:        len = ssh_digest_bytes(SSH_DIGEST_SHA1);
1.33      djm       124:
                    125:        if (name_from_hostfile == NULL) {
                    126:                /* Create new salt */
                    127:                for (i = 0; i < len; i++)
                    128:                        salt[i] = arc4random();
                    129:        } else {
                    130:                /* Extract salt from known host entry */
                    131:                if (extract_salt(name_from_hostfile, src_len, salt,
                    132:                    sizeof(salt)) == -1)
                    133:                        return (NULL);
                    134:        }
                    135:
1.54      markus    136:        if ((ctx = ssh_hmac_start(SSH_DIGEST_SHA1)) == NULL ||
                    137:            ssh_hmac_init(ctx, salt, len) < 0 ||
                    138:            ssh_hmac_update(ctx, host, strlen(host)) < 0 ||
                    139:            ssh_hmac_final(ctx, result, sizeof(result)))
                    140:                fatal("%s: ssh_hmac failed", __func__);
                    141:        ssh_hmac_free(ctx);
1.33      djm       142:
1.34      deraadt   143:        if (__b64_ntop(salt, len, uu_salt, sizeof(uu_salt)) == -1 ||
1.33      djm       144:            __b64_ntop(result, len, uu_result, sizeof(uu_result)) == -1)
1.54      markus    145:                fatal("%s: __b64_ntop failed", __func__);
1.33      djm       146:
                    147:        snprintf(encoded, sizeof(encoded), "%s%s%c%s", HASH_MAGIC, uu_salt,
                    148:            HASH_DELIM, uu_result);
                    149:
                    150:        return (encoded);
                    151: }
1.1       deraadt   152:
1.9       markus    153: /*
1.14      markus    154:  * Parses an RSA (number of bits, e, n) or DSA key from a string.  Moves the
                    155:  * pointer over the key.  Skips any whitespace at the beginning and at end.
1.9       markus    156:  */
1.1       deraadt   157:
1.29      jakob     158: int
1.59      djm       159: hostfile_read_key(char **cpp, u_int *bitsp, struct sshkey *ret)
1.1       deraadt   160: {
1.7       markus    161:        char *cp;
1.59      djm       162:        int r;
1.7       markus    163:
                    164:        /* Skip leading whitespace. */
1.9       markus    165:        for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
                    166:                ;
1.1       deraadt   167:
1.59      djm       168:        if ((r = sshkey_read(ret, &cp)) != 0)
1.7       markus    169:                return 0;
                    170:
                    171:        /* Skip trailing whitespace. */
1.9       markus    172:        for (; *cp == ' ' || *cp == '\t'; cp++)
                    173:                ;
1.7       markus    174:
                    175:        /* Return results. */
                    176:        *cpp = cp;
1.59      djm       177:        if (bitsp != NULL)
                    178:                *bitsp = sshkey_size(ret);
1.7       markus    179:        return 1;
1.14      markus    180: }
1.1       deraadt   181:
1.27      itojun    182: static int
1.59      djm       183: hostfile_check_key(int bits, const struct sshkey *key, const char *host,
1.49      djm       184:     const char *filename, u_long linenum)
1.1       deraadt   185: {
1.56      markus    186: #ifdef WITH_SSH1
1.21      markus    187:        if (key == NULL || key->type != KEY_RSA1 || key->rsa == NULL)
1.14      markus    188:                return 1;
                    189:        if (bits != BN_num_bits(key->rsa->n)) {
1.49      djm       190:                logit("Warning: %s, line %lu: keysize mismatch for host %s: "
1.14      markus    191:                    "actual %d vs. announced %d.",
                    192:                    filename, linenum, host, BN_num_bits(key->rsa->n), bits);
1.49      djm       193:                logit("Warning: replace %d with %d in %s, line %lu.",
1.14      markus    194:                    bits, BN_num_bits(key->rsa->n), filename, linenum);
1.1       deraadt   195:        }
1.56      markus    196: #endif
1.14      markus    197:        return 1;
1.1       deraadt   198: }
                    199:
1.49      djm       200: static HostkeyMarker
1.48      djm       201: check_markers(char **cpp)
                    202: {
                    203:        char marker[32], *sp, *cp = *cpp;
                    204:        int ret = MRK_NONE;
                    205:
                    206:        while (*cp == '@') {
                    207:                /* Only one marker is allowed */
                    208:                if (ret != MRK_NONE)
                    209:                        return MRK_ERROR;
                    210:                /* Markers are terminated by whitespace */
                    211:                if ((sp = strchr(cp, ' ')) == NULL &&
                    212:                    (sp = strchr(cp, '\t')) == NULL)
                    213:                        return MRK_ERROR;
                    214:                /* Extract marker for comparison */
                    215:                if (sp <= cp + 1 || sp >= cp + sizeof(marker))
                    216:                        return MRK_ERROR;
                    217:                memcpy(marker, cp, sp - cp);
                    218:                marker[sp - cp] = '\0';
                    219:                if (strcmp(marker, CA_MARKER) == 0)
                    220:                        ret = MRK_CA;
                    221:                else if (strcmp(marker, REVOKE_MARKER) == 0)
                    222:                        ret = MRK_REVOKE;
                    223:                else
                    224:                        return MRK_ERROR;
                    225:
                    226:                /* Skip past marker and any whitespace that follows it */
                    227:                cp = sp;
                    228:                for (; *cp == ' ' || *cp == '\t'; cp++)
                    229:                        ;
                    230:        }
                    231:        *cpp = cp;
                    232:        return ret;
                    233: }
                    234:
1.49      djm       235: struct hostkeys *
                    236: init_hostkeys(void)
                    237: {
                    238:        struct hostkeys *ret = xcalloc(1, sizeof(*ret));
                    239:
                    240:        ret->entries = NULL;
                    241:        return ret;
                    242: }
1.1       deraadt   243:
1.49      djm       244: void
                    245: load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
1.1       deraadt   246: {
1.7       markus    247:        FILE *f;
                    248:        char line[8192];
1.49      djm       249:        u_long linenum = 0, num_loaded = 0;
1.33      djm       250:        char *cp, *cp2, *hashed_host;
1.49      djm       251:        HostkeyMarker marker;
1.59      djm       252:        struct sshkey *key;
                    253:        u_int kbits;
1.49      djm       254:
                    255:        if ((f = fopen(path, "r")) == NULL)
                    256:                return;
                    257:        debug3("%s: loading entries for host \"%.100s\" from file \"%s\"",
                    258:            __func__, host, path);
                    259:        while (read_keyfile_line(f, path, line, sizeof(line), &linenum) == 0) {
1.7       markus    260:                cp = line;
                    261:
1.9       markus    262:                /* Skip any leading whitespace, comments and empty lines. */
                    263:                for (; *cp == ' ' || *cp == '\t'; cp++)
                    264:                        ;
1.7       markus    265:                if (!*cp || *cp == '#' || *cp == '\n')
                    266:                        continue;
                    267:
1.49      djm       268:                if ((marker = check_markers(&cp)) == MRK_ERROR) {
                    269:                        verbose("%s: invalid marker at %s:%lu",
                    270:                            __func__, path, linenum);
1.47      djm       271:                        continue;
1.49      djm       272:                }
1.47      djm       273:
1.7       markus    274:                /* Find the end of the host name portion. */
1.9       markus    275:                for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++)
                    276:                        ;
1.7       markus    277:
                    278:                /* Check if the host name matches. */
1.33      djm       279:                if (match_hostname(host, cp, (u_int) (cp2 - cp)) != 1) {
                    280:                        if (*cp != HASH_DELIM)
                    281:                                continue;
                    282:                        hashed_host = host_hash(host, cp, (u_int) (cp2 - cp));
                    283:                        if (hashed_host == NULL) {
1.49      djm       284:                                debug("Invalid hashed host line %lu of %s",
                    285:                                    linenum, path);
1.33      djm       286:                                continue;
                    287:                        }
                    288:                        if (strncmp(hashed_host, cp, (u_int) (cp2 - cp)) != 0)
                    289:                                continue;
                    290:                }
1.7       markus    291:
                    292:                /* Got a match.  Skip host name. */
                    293:                cp = cp2;
                    294:
1.9       markus    295:                /*
                    296:                 * Extract the key from the line.  This will skip any leading
                    297:                 * whitespace.  Ignore badly formatted lines.
                    298:                 */
1.59      djm       299:                if ((key = sshkey_new(KEY_UNSPEC)) == NULL) {
                    300:                        error("%s: sshkey_new failed", __func__);
                    301:                        break;
                    302:                }
1.49      djm       303:                if (!hostfile_read_key(&cp, &kbits, key)) {
1.59      djm       304:                        sshkey_free(key);
1.56      markus    305: #ifdef WITH_SSH1
1.59      djm       306:                        if ((key = sshkey_new(KEY_RSA1)) == NULL) {
                    307:                                error("%s: sshkey_new failed", __func__);
                    308:                                break;
                    309:                        }
1.49      djm       310:                        if (!hostfile_read_key(&cp, &kbits, key)) {
1.59      djm       311:                                sshkey_free(key);
1.49      djm       312:                                continue;
                    313:                        }
1.56      markus    314: #else
                    315:                        continue;
                    316: #endif
1.49      djm       317:                }
                    318:                if (!hostfile_check_key(kbits, key, host, path, linenum))
1.14      markus    319:                        continue;
1.23      markus    320:
1.49      djm       321:                debug3("%s: found %skey type %s in file %s:%lu", __func__,
                    322:                    marker == MRK_NONE ? "" :
                    323:                    (marker == MRK_CA ? "ca " : "revoked "),
1.59      djm       324:                    sshkey_type(key), path, linenum);
1.49      djm       325:                hostkeys->entries = xrealloc(hostkeys->entries,
                    326:                    hostkeys->num_entries + 1, sizeof(*hostkeys->entries));
                    327:                hostkeys->entries[hostkeys->num_entries].host = xstrdup(host);
                    328:                hostkeys->entries[hostkeys->num_entries].file = xstrdup(path);
                    329:                hostkeys->entries[hostkeys->num_entries].line = linenum;
                    330:                hostkeys->entries[hostkeys->num_entries].key = key;
                    331:                hostkeys->entries[hostkeys->num_entries].marker = marker;
                    332:                hostkeys->num_entries++;
                    333:                num_loaded++;
                    334:        }
                    335:        debug3("%s: loaded %lu keys", __func__, num_loaded);
1.50      djm       336:        fclose(f);
1.49      djm       337:        return;
1.58      djm       338: }
1.49      djm       339:
                    340: void
                    341: free_hostkeys(struct hostkeys *hostkeys)
                    342: {
                    343:        u_int i;
                    344:
                    345:        for (i = 0; i < hostkeys->num_entries; i++) {
1.51      djm       346:                free(hostkeys->entries[i].host);
                    347:                free(hostkeys->entries[i].file);
1.59      djm       348:                sshkey_free(hostkeys->entries[i].key);
1.55      tedu      349:                explicit_bzero(hostkeys->entries + i, sizeof(*hostkeys->entries));
1.49      djm       350:        }
1.51      djm       351:        free(hostkeys->entries);
1.55      tedu      352:        explicit_bzero(hostkeys, sizeof(*hostkeys));
1.51      djm       353:        free(hostkeys);
1.49      djm       354: }
                    355:
                    356: static int
1.59      djm       357: check_key_not_revoked(struct hostkeys *hostkeys, struct sshkey *k)
1.49      djm       358: {
1.59      djm       359:        int is_cert = sshkey_is_cert(k);
1.49      djm       360:        u_int i;
1.7       markus    361:
1.49      djm       362:        for (i = 0; i < hostkeys->num_entries; i++) {
                    363:                if (hostkeys->entries[i].marker != MRK_REVOKE)
1.30      markus    364:                        continue;
1.59      djm       365:                if (sshkey_equal_public(k, hostkeys->entries[i].key))
1.49      djm       366:                        return -1;
                    367:                if (is_cert &&
1.59      djm       368:                    sshkey_equal_public(k->cert->signature_key,
1.49      djm       369:                    hostkeys->entries[i].key))
                    370:                        return -1;
                    371:        }
                    372:        return 0;
                    373: }
                    374:
                    375: /*
                    376:  * Match keys against a specified key, or look one up by key type.
                    377:  *
                    378:  * If looking for a keytype (key == NULL) and one is found then return
                    379:  * HOST_FOUND, otherwise HOST_NEW.
                    380:  *
                    381:  * If looking for a key (key != NULL):
                    382:  *  1. If the key is a cert and a matching CA is found, return HOST_OK
                    383:  *  2. If the key is not a cert and a matching key is found, return HOST_OK
                    384:  *  3. If no key matches but a key with a different type is found, then
                    385:  *     return HOST_CHANGED
                    386:  *  4. If no matching keys are found, then return HOST_NEW.
                    387:  *
                    388:  * Finally, check any found key is not revoked.
                    389:  */
                    390: static HostStatus
                    391: check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
1.59      djm       392:     struct sshkey *k, int keytype, const struct hostkey_entry **found)
1.49      djm       393: {
                    394:        u_int i;
                    395:        HostStatus end_return = HOST_NEW;
1.59      djm       396:        int want_cert = sshkey_is_cert(k);
1.49      djm       397:        HostkeyMarker want_marker = want_cert ? MRK_CA : MRK_NONE;
                    398:        int proto = (k ? k->type : keytype) == KEY_RSA1 ? 1 : 2;
                    399:
                    400:        if (found != NULL)
                    401:                *found = NULL;
1.30      markus    402:
1.49      djm       403:        for (i = 0; i < hostkeys->num_entries; i++) {
                    404:                if (proto == 1 && hostkeys->entries[i].key->type != KEY_RSA1)
                    405:                        continue;
                    406:                if (proto == 2 && hostkeys->entries[i].key->type == KEY_RSA1)
1.30      markus    407:                        continue;
1.49      djm       408:                if (hostkeys->entries[i].marker != want_marker)
                    409:                        continue;
                    410:                if (k == NULL) {
                    411:                        if (hostkeys->entries[i].key->type != keytype)
                    412:                                continue;
                    413:                        end_return = HOST_FOUND;
                    414:                        if (found != NULL)
                    415:                                *found = hostkeys->entries + i;
                    416:                        k = hostkeys->entries[i].key;
                    417:                        break;
                    418:                }
                    419:                if (want_cert) {
1.59      djm       420:                        if (sshkey_equal_public(k->cert->signature_key,
1.49      djm       421:                            hostkeys->entries[i].key)) {
                    422:                                /* A matching CA exists */
                    423:                                end_return = HOST_OK;
                    424:                                if (found != NULL)
                    425:                                        *found = hostkeys->entries + i;
                    426:                                break;
1.48      djm       427:                        }
1.49      djm       428:                } else {
1.59      djm       429:                        if (sshkey_equal(k, hostkeys->entries[i].key)) {
1.49      djm       430:                                end_return = HOST_OK;
                    431:                                if (found != NULL)
                    432:                                        *found = hostkeys->entries + i;
                    433:                                break;
1.48      djm       434:                        }
1.49      djm       435:                        /* A non-maching key exists */
                    436:                        end_return = HOST_CHANGED;
                    437:                        if (found != NULL)
                    438:                                *found = hostkeys->entries + i;
1.48      djm       439:                }
1.1       deraadt   440:        }
1.49      djm       441:        if (check_key_not_revoked(hostkeys, k) != 0) {
                    442:                end_return = HOST_REVOKED;
                    443:                if (found != NULL)
                    444:                        *found = NULL;
                    445:        }
1.7       markus    446:        return end_return;
1.30      markus    447: }
1.58      djm       448:
1.30      markus    449: HostStatus
1.59      djm       450: check_key_in_hostkeys(struct hostkeys *hostkeys, struct sshkey *key,
1.49      djm       451:     const struct hostkey_entry **found)
1.30      markus    452: {
                    453:        if (key == NULL)
                    454:                fatal("no key to look up");
1.49      djm       455:        return check_hostkeys_by_key_or_type(hostkeys, key, 0, found);
1.30      markus    456: }
                    457:
                    458: int
1.49      djm       459: lookup_key_in_hostkeys_by_type(struct hostkeys *hostkeys, int keytype,
                    460:     const struct hostkey_entry **found)
1.30      markus    461: {
1.49      djm       462:        return (check_hostkeys_by_key_or_type(hostkeys, NULL, keytype,
                    463:            found) == HOST_FOUND);
1.1       deraadt   464: }
                    465:
1.9       markus    466: /*
                    467:  * Appends an entry to the host file.  Returns false if the entry could not
                    468:  * be appended.
                    469:  */
1.1       deraadt   470:
1.2       provos    471: int
1.59      djm       472: add_host_to_hostfile(const char *filename, const char *host,
                    473:     const struct sshkey *key, int store_hash)
1.1       deraadt   474: {
1.7       markus    475:        FILE *f;
1.59      djm       476:        int r, success = 0;
1.35      dtucker   477:        char *hashed_host = NULL;
1.33      djm       478:
1.14      markus    479:        if (key == NULL)
1.17      markus    480:                return 1;       /* XXX ? */
1.7       markus    481:        f = fopen(filename, "a");
                    482:        if (!f)
                    483:                return 0;
1.33      djm       484:
                    485:        if (store_hash) {
                    486:                if ((hashed_host = host_hash(host, NULL, 0)) == NULL) {
                    487:                        error("add_host_to_hostfile: host_hash failed");
                    488:                        fclose(f);
                    489:                        return 0;
                    490:                }
                    491:        }
                    492:        fprintf(f, "%s ", store_hash ? hashed_host : host);
                    493:
1.59      djm       494:        if ((r = sshkey_write(key, f)) != 0) {
                    495:                error("%s: saving key in %s failed: %s",
                    496:                    __func__, filename, ssh_err(r));
                    497:        } else
1.14      markus    498:                success = 1;
1.60    ! djm       499:        fputc('\n', f);
1.7       markus    500:        fclose(f);
1.14      markus    501:        return success;
1.60    ! djm       502: }
        !           503:
        !           504: static int
        !           505: match_maybe_hashed(const char *host, const char *names, int *was_hashed)
        !           506: {
        !           507:        int hashed = *names == HASH_DELIM;
        !           508:        const char *hashed_host;
        !           509:        size_t nlen = strlen(names);
        !           510:
        !           511:        if (was_hashed != NULL)
        !           512:                *was_hashed = hashed;
        !           513:        if (hashed) {
        !           514:                if ((hashed_host = host_hash(host, names, nlen)) == NULL)
        !           515:                        return -1;
        !           516:                return nlen == strlen(hashed_host) &&
        !           517:                    strncmp(hashed_host, names, nlen) == 0;
        !           518:        }
        !           519:        return match_hostname(host, names, nlen) == 1;
        !           520: }
        !           521:
        !           522: int
        !           523: hostkeys_foreach(const char *path, hostkeys_foreach_fn *callback, void *ctx,
        !           524:     const char *host, u_int options)
        !           525: {
        !           526:        FILE *f;
        !           527:        char line[8192], oline[8192];
        !           528:        u_long linenum = 0;
        !           529:        char *cp, *cp2;
        !           530:        u_int kbits;
        !           531:        int s, r = 0;
        !           532:        struct hostkey_foreach_line lineinfo;
        !           533:
        !           534:        memset(&lineinfo, 0, sizeof(lineinfo));
        !           535:        if (host == NULL && (options & HKF_WANT_MATCH_HOST) != 0)
        !           536:                return SSH_ERR_INVALID_ARGUMENT;
        !           537:        if ((f = fopen(path, "r")) == NULL)
        !           538:                return SSH_ERR_SYSTEM_ERROR;
        !           539:
        !           540:        debug3("%s: reading file \"%s\"", __func__, path);
        !           541:        while (read_keyfile_line(f, path, line, sizeof(line), &linenum) == 0) {
        !           542:                line[strcspn(line, "\n")] = '\0';
        !           543:                strlcpy(oline, line, sizeof(oline));
        !           544:
        !           545:                sshkey_free(lineinfo.key);
        !           546:                memset(&lineinfo, 0, sizeof(lineinfo));
        !           547:                lineinfo.path = path;
        !           548:                lineinfo.linenum = linenum;
        !           549:                lineinfo.line = oline;
        !           550:                lineinfo.status = HKF_STATUS_OK;
        !           551:
        !           552:                /* Skip any leading whitespace, comments and empty lines. */
        !           553:                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
        !           554:                        ;
        !           555:                if (!*cp || *cp == '#' || *cp == '\n') {
        !           556:                        if ((options & HKF_WANT_MATCH_HOST) == 0) {
        !           557:                                lineinfo.status = HKF_STATUS_COMMENT;
        !           558:                                if ((r = callback(&lineinfo, ctx)) != 0)
        !           559:                                        break;
        !           560:                        }
        !           561:                        continue;
        !           562:                }
        !           563:
        !           564:                if ((lineinfo.marker = check_markers(&cp)) == MRK_ERROR) {
        !           565:                        verbose("%s: invalid marker at %s:%lu",
        !           566:                            __func__, path, linenum);
        !           567:                        if ((options & HKF_WANT_MATCH_HOST) == 0)
        !           568:                                goto bad;
        !           569:                        continue;
        !           570:                }
        !           571:
        !           572:                /* Find the end of the host name portion. */
        !           573:                for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++)
        !           574:                        ;
        !           575:                lineinfo.hosts = cp;
        !           576:                *cp2++ = '\0';
        !           577:
        !           578:                /* Check if the host name matches. */
        !           579:                if (host != NULL) {
        !           580:                        s = match_maybe_hashed(host, lineinfo.hosts,
        !           581:                            &lineinfo.was_hashed);
        !           582:                        if (s == 1)
        !           583:                                lineinfo.status = HKF_STATUS_HOST_MATCHED;
        !           584:                        else if ((options & HKF_WANT_MATCH_HOST) != 0)
        !           585:                                continue;
        !           586:                        else if (s == -1) {
        !           587:                                debug2("%s: %s:%ld: bad host hash \"%.32s\"",
        !           588:                                    __func__, path, linenum, lineinfo.hosts);
        !           589:                                goto bad;
        !           590:                        }
        !           591:                }
        !           592:
        !           593:                /* Got a match.  Skip host name and any following whitespace */
        !           594:                for (; *cp2 == ' ' || *cp2 == '\t'; cp2++)
        !           595:                        ;
        !           596:                if (*cp2 == '\0' || *cp2 == '#') {
        !           597:                        debug2("%s:%ld: truncated before key", path, linenum);
        !           598:                        goto bad;
        !           599:                }
        !           600:                lineinfo.rawkey = cp = cp2;
        !           601:
        !           602:                if ((options & HKF_WANT_PARSE_KEY) != 0) {
        !           603:                        /*
        !           604:                         * Extract the key from the line.  This will skip
        !           605:                         * any leading whitespace.  Ignore badly formatted
        !           606:                         * lines.
        !           607:                         */
        !           608:                        if ((lineinfo.key = sshkey_new(KEY_UNSPEC)) == NULL) {
        !           609:                                error("%s: sshkey_new failed", __func__);
        !           610:                                return SSH_ERR_ALLOC_FAIL;
        !           611:                        }
        !           612:                        if (!hostfile_read_key(&cp, &kbits, lineinfo.key)) {
        !           613: #ifdef WITH_SSH1
        !           614:                                sshkey_free(lineinfo.key);
        !           615:                                lineinfo.key = sshkey_new(KEY_RSA1);
        !           616:                                if (lineinfo.key  == NULL) {
        !           617:                                        error("%s: sshkey_new fail", __func__);
        !           618:                                        return SSH_ERR_ALLOC_FAIL;
        !           619:                                }
        !           620:                                if (!hostfile_read_key(&cp, &kbits,
        !           621:                                    lineinfo.key))
        !           622:                                        goto bad;
        !           623: #else
        !           624:                                goto bad;
        !           625: #endif
        !           626:                        }
        !           627:                        if (!hostfile_check_key(kbits, lineinfo.key, host,
        !           628:                            path, linenum)) {
        !           629:  bad:
        !           630:                                lineinfo.status = HKF_STATUS_INVALID;
        !           631:                                if ((r = callback(&lineinfo, ctx)) != 0)
        !           632:                                        break;
        !           633:                                continue;
        !           634:                        }
        !           635:                }
        !           636:                if ((r = callback(&lineinfo, ctx)) != 0)
        !           637:                        break;
        !           638:        }
        !           639:        sshkey_free(lineinfo.key);
        !           640:        fclose(f);
        !           641:        return r;
1.1       deraadt   642: }