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

Annotation of src/usr.bin/ssh/auth2-hostbased.c, Revision 1.36

1.36    ! djm         1: /* $OpenBSD: auth2-hostbased.c,v 1.35 2018/07/09 21:35:50 markus Exp $ */
1.1       markus      2: /*
                      3:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  *
                     14:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     15:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     16:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     17:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     18:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     19:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     20:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     21:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     22:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     23:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     24:  */
                     25:
1.9       stevesk    26:
                     27: #include <sys/types.h>
                     28:
                     29: #include <pwd.h>
1.10      stevesk    30: #include <string.h>
1.11      deraadt    31: #include <stdarg.h>
1.1       markus     32:
1.11      deraadt    33: #include "xmalloc.h"
1.1       markus     34: #include "ssh2.h"
                     35: #include "packet.h"
1.35      markus     36: #include "sshbuf.h"
1.1       markus     37: #include "log.h"
1.18      millert    38: #include "misc.h"
1.1       markus     39: #include "servconf.h"
                     40: #include "compat.h"
1.29      markus     41: #include "sshkey.h"
1.11      deraadt    42: #include "hostfile.h"
1.1       markus     43: #include "auth.h"
                     44: #include "canohost.h"
1.11      deraadt    45: #ifdef GSSAPI
                     46: #include "ssh-gss.h"
                     47: #endif
1.1       markus     48: #include "monitor_wrap.h"
                     49: #include "pathnames.h"
1.29      markus     50: #include "ssherr.h"
1.22      djm        51: #include "match.h"
1.1       markus     52:
                     53: /* import */
                     54: extern ServerOptions options;
                     55: extern u_char *session_id2;
1.5       markus     56: extern u_int session_id2_len;
1.1       markus     57:
1.2       markus     58: static int
1.30      markus     59: userauth_hostbased(struct ssh *ssh)
1.1       markus     60: {
1.30      markus     61:        Authctxt *authctxt = ssh->authctxt;
1.29      markus     62:        struct sshbuf *b;
1.27      markus     63:        struct sshkey *key = NULL;
1.33      djm        64:        char *pkalg, *cuser, *chost;
1.1       markus     65:        u_char *pkblob, *sig;
1.29      markus     66:        size_t alen, blen, slen;
                     67:        int r, pktype, authenticated = 0;
1.1       markus     68:
1.29      markus     69:        /* XXX use sshkey_froms() */
                     70:        if ((r = sshpkt_get_cstring(ssh, &pkalg, &alen)) != 0 ||
                     71:            (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
                     72:            (r = sshpkt_get_cstring(ssh, &chost, NULL)) != 0 ||
                     73:            (r = sshpkt_get_cstring(ssh, &cuser, NULL)) != 0 ||
                     74:            (r = sshpkt_get_string(ssh, &sig, &slen)) != 0)
                     75:                fatal("%s: packet parsing: %s", __func__, ssh_err(r));
1.1       markus     76:
1.29      markus     77:        debug("%s: cuser %s chost %s pkalg %s slen %zu", __func__,
1.1       markus     78:            cuser, chost, pkalg, slen);
                     79: #ifdef DEBUG_PK
                     80:        debug("signature:");
1.29      markus     81:        sshbuf_dump_data(sig, siglen, stderr);
1.1       markus     82: #endif
1.29      markus     83:        pktype = sshkey_type_from_name(pkalg);
1.1       markus     84:        if (pktype == KEY_UNSPEC) {
                     85:                /* this is perfectly legal */
1.29      markus     86:                logit("%s: unsupported public key algorithm: %s",
                     87:                    __func__, pkalg);
                     88:                goto done;
                     89:        }
                     90:        if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
                     91:                error("%s: key_from_blob: %s", __func__, ssh_err(r));
1.1       markus     92:                goto done;
                     93:        }
                     94:        if (key == NULL) {
1.29      markus     95:                error("%s: cannot decode key: %s", __func__, pkalg);
1.1       markus     96:                goto done;
                     97:        }
                     98:        if (key->type != pktype) {
1.29      markus     99:                error("%s: type mismatch for decoded key "
                    100:                    "(received %d, expected %d)", __func__, key->type, pktype);
1.17      djm       101:                goto done;
                    102:        }
1.29      markus    103:        if (sshkey_type_plain(key->type) == KEY_RSA &&
                    104:            (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
1.17      djm       105:                error("Refusing RSA key because peer uses unsafe "
                    106:                    "signature format");
1.1       markus    107:                goto done;
                    108:        }
1.34      djm       109:        if (match_pattern_list(pkalg, options.hostbased_key_types, 0) != 1) {
1.22      djm       110:                logit("%s: key type %s not in HostbasedAcceptedKeyTypes",
                    111:                    __func__, sshkey_type(key));
1.36    ! djm       112:                goto done;
        !           113:        }
        !           114:
        !           115:        if (!authctxt->valid || authctxt->user == NULL) {
        !           116:                debug2("%s: disabled because of invalid user", __func__);
1.22      djm       117:                goto done;
                    118:        }
                    119:
1.29      markus    120:        if ((b = sshbuf_new()) == NULL)
                    121:                fatal("%s: sshbuf_new failed", __func__);
1.1       markus    122:        /* reconstruct packet */
1.29      markus    123:        if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
                    124:            (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                    125:            (r = sshbuf_put_cstring(b, authctxt->user)) != 0 ||
1.33      djm       126:            (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1.29      markus    127:            (r = sshbuf_put_cstring(b, "hostbased")) != 0 ||
                    128:            (r = sshbuf_put_string(b, pkalg, alen)) != 0 ||
                    129:            (r = sshbuf_put_string(b, pkblob, blen)) != 0 ||
                    130:            (r = sshbuf_put_cstring(b, chost)) != 0 ||
                    131:            (r = sshbuf_put_cstring(b, cuser)) != 0)
                    132:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.1       markus    133: #ifdef DEBUG_PK
1.29      markus    134:        sshbuf_dump(b, stderr);
1.1       markus    135: #endif
1.16      djm       136:
1.31      djm       137:        auth2_record_info(authctxt,
1.16      djm       138:            "client user \"%.100s\", client host \"%.100s\"", cuser, chost);
                    139:
1.1       markus    140:        /* test for allowed key and correct signature */
                    141:        authenticated = 0;
                    142:        if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
1.29      markus    143:            PRIVSEP(sshkey_verify(key, sig, slen,
1.32      djm       144:            sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat)) == 0)
1.1       markus    145:                authenticated = 1;
                    146:
1.31      djm       147:        auth2_record_key(authctxt, authenticated, key);
1.29      markus    148:        sshbuf_free(b);
1.1       markus    149: done:
1.29      markus    150:        debug2("%s: authenticated %d", __func__, authenticated);
1.31      djm       151:        sshkey_free(key);
1.15      djm       152:        free(pkalg);
                    153:        free(pkblob);
                    154:        free(cuser);
                    155:        free(chost);
                    156:        free(sig);
1.1       markus    157:        return authenticated;
                    158: }
                    159:
                    160: /* return 1 if given hostkey is allowed */
                    161: int
                    162: hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
1.27      markus    163:     struct sshkey *key)
1.1       markus    164: {
1.26      djm       165:        struct ssh *ssh = active_state; /* XXX */
1.14      djm       166:        const char *resolvedname, *ipaddr, *lookup, *reason;
1.1       markus    167:        HostStatus host_status;
                    168:        int len;
1.14      djm       169:        char *fp;
1.13      djm       170:
                    171:        if (auth_key_is_revoked(key))
                    172:                return 0;
1.1       markus    173:
1.26      djm       174:        resolvedname = auth_get_canonical_hostname(ssh, options.use_dns);
                    175:        ipaddr = ssh_remote_ipaddr(ssh);
1.1       markus    176:
1.20      djm       177:        debug2("%s: chost %s resolvedname %s ipaddr %s", __func__,
1.1       markus    178:            chost, resolvedname, ipaddr);
                    179:
1.12      djm       180:        if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
                    181:                debug2("stripping trailing dot from chost %s", chost);
                    182:                chost[len - 1] = '\0';
                    183:        }
                    184:
1.1       markus    185:        if (options.hostbased_uses_name_from_packet_only) {
1.20      djm       186:                if (auth_rhosts2(pw, cuser, chost, chost) == 0) {
                    187:                        debug2("%s: auth_rhosts2 refused "
                    188:                            "user \"%.100s\" host \"%.100s\" (from packet)",
                    189:                            __func__, cuser, chost);
1.1       markus    190:                        return 0;
1.20      djm       191:                }
1.1       markus    192:                lookup = chost;
                    193:        } else {
                    194:                if (strcasecmp(resolvedname, chost) != 0)
1.3       itojun    195:                        logit("userauth_hostbased mismatch: "
1.1       markus    196:                            "client sends %s, but we resolve %s to %s",
                    197:                            chost, ipaddr, resolvedname);
1.20      djm       198:                if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) {
                    199:                        debug2("%s: auth_rhosts2 refused "
                    200:                            "user \"%.100s\" host \"%.100s\" addr \"%.100s\"",
                    201:                            __func__, cuser, resolvedname, ipaddr);
1.1       markus    202:                        return 0;
1.20      djm       203:                }
1.1       markus    204:                lookup = resolvedname;
                    205:        }
1.20      djm       206:        debug2("%s: access allowed by auth_rhosts2", __func__);
1.1       markus    207:
1.29      markus    208:        if (sshkey_is_cert(key) &&
                    209:            sshkey_cert_check_authority(key, 1, 0, lookup, &reason)) {
1.14      djm       210:                error("%s", reason);
                    211:                auth_debug_add("%s", reason);
                    212:                return 0;
                    213:        }
                    214:
1.1       markus    215:        host_status = check_key_in_hostfiles(pw, key, lookup,
                    216:            _PATH_SSH_SYSTEM_HOSTFILE,
                    217:            options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
                    218:
                    219:        /* backward compat if no key has been found. */
1.14      djm       220:        if (host_status == HOST_NEW) {
1.1       markus    221:                host_status = check_key_in_hostfiles(pw, key, lookup,
                    222:                    _PATH_SSH_SYSTEM_HOSTFILE2,
                    223:                    options.ignore_user_known_hosts ? NULL :
                    224:                    _PATH_SSH_USER_HOSTFILE2);
1.14      djm       225:        }
                    226:
                    227:        if (host_status == HOST_OK) {
1.29      markus    228:                if (sshkey_is_cert(key)) {
1.24      djm       229:                        if ((fp = sshkey_fingerprint(key->cert->signature_key,
                    230:                            options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
                    231:                                fatal("%s: sshkey_fingerprint fail", __func__);
1.14      djm       232:                        verbose("Accepted certificate ID \"%s\" signed by "
                    233:                            "%s CA %s from %s@%s", key->cert->key_id,
1.29      markus    234:                            sshkey_type(key->cert->signature_key), fp,
1.14      djm       235:                            cuser, lookup);
                    236:                } else {
1.24      djm       237:                        if ((fp = sshkey_fingerprint(key,
                    238:                            options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
                    239:                                fatal("%s: sshkey_fingerprint fail", __func__);
1.14      djm       240:                        verbose("Accepted %s public key %s from %s@%s",
1.29      markus    241:                            sshkey_type(key), fp, cuser, lookup);
1.14      djm       242:                }
1.15      djm       243:                free(fp);
1.14      djm       244:        }
1.1       markus    245:
                    246:        return (host_status == HOST_OK);
                    247: }
1.2       markus    248:
                    249: Authmethod method_hostbased = {
                    250:        "hostbased",
                    251:        userauth_hostbased,
                    252:        &options.hostbased_authentication
                    253: };