Annotation of src/usr.bin/ssh/auth2-hostbased.c, Revision 1.44
1.44 ! djm 1: /* $OpenBSD: auth2-hostbased.c,v 1.43 2020/10/18 11:32:01 djm 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:
1.41 djm 29: #include <stdlib.h>
1.9 stevesk 30: #include <pwd.h>
1.10 stevesk 31: #include <string.h>
1.11 deraadt 32: #include <stdarg.h>
1.1 markus 33:
1.11 deraadt 34: #include "xmalloc.h"
1.1 markus 35: #include "ssh2.h"
36: #include "packet.h"
1.35 markus 37: #include "sshbuf.h"
1.1 markus 38: #include "log.h"
1.18 millert 39: #include "misc.h"
1.1 markus 40: #include "servconf.h"
41: #include "compat.h"
1.29 markus 42: #include "sshkey.h"
1.11 deraadt 43: #include "hostfile.h"
1.1 markus 44: #include "auth.h"
45: #include "canohost.h"
1.11 deraadt 46: #ifdef GSSAPI
47: #include "ssh-gss.h"
48: #endif
1.1 markus 49: #include "monitor_wrap.h"
50: #include "pathnames.h"
1.29 markus 51: #include "ssherr.h"
1.22 djm 52: #include "match.h"
1.39 djm 53:
1.1 markus 54: /* import */
55: extern ServerOptions options;
56: extern u_char *session_id2;
1.5 markus 57: extern u_int session_id2_len;
1.1 markus 58:
1.2 markus 59: static int
1.30 markus 60: userauth_hostbased(struct ssh *ssh)
1.1 markus 61: {
1.30 markus 62: Authctxt *authctxt = ssh->authctxt;
1.29 markus 63: struct sshbuf *b;
1.27 markus 64: struct sshkey *key = NULL;
1.33 djm 65: char *pkalg, *cuser, *chost;
1.1 markus 66: u_char *pkblob, *sig;
1.29 markus 67: size_t alen, blen, slen;
68: int r, pktype, authenticated = 0;
1.1 markus 69:
1.29 markus 70: /* XXX use sshkey_froms() */
71: if ((r = sshpkt_get_cstring(ssh, &pkalg, &alen)) != 0 ||
72: (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
73: (r = sshpkt_get_cstring(ssh, &chost, NULL)) != 0 ||
74: (r = sshpkt_get_cstring(ssh, &cuser, NULL)) != 0 ||
75: (r = sshpkt_get_string(ssh, &sig, &slen)) != 0)
1.43 djm 76: fatal_fr(r, "parse packet");
1.1 markus 77:
1.43 djm 78: debug_f("cuser %s chost %s pkalg %s slen %zu",
1.1 markus 79: cuser, chost, pkalg, slen);
80: #ifdef DEBUG_PK
81: debug("signature:");
1.37 mestre 82: sshbuf_dump_data(sig, slen, stderr);
1.1 markus 83: #endif
1.29 markus 84: pktype = sshkey_type_from_name(pkalg);
1.1 markus 85: if (pktype == KEY_UNSPEC) {
86: /* this is perfectly legal */
1.43 djm 87: logit_f("unsupported public key algorithm: %s",
88: pkalg);
1.29 markus 89: goto done;
90: }
91: if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
1.43 djm 92: error_fr(r, "key_from_blob");
1.1 markus 93: goto done;
94: }
95: if (key == NULL) {
1.43 djm 96: error_f("cannot decode key: %s", pkalg);
1.1 markus 97: goto done;
98: }
99: if (key->type != pktype) {
1.43 djm 100: error_f("type mismatch for decoded key "
101: "(received %d, expected %d)", key->type, pktype);
1.17 djm 102: goto done;
103: }
1.29 markus 104: if (sshkey_type_plain(key->type) == KEY_RSA &&
105: (ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
1.17 djm 106: error("Refusing RSA key because peer uses unsafe "
107: "signature format");
1.1 markus 108: goto done;
109: }
1.34 djm 110: if (match_pattern_list(pkalg, options.hostbased_key_types, 0) != 1) {
1.43 djm 111: logit_f("key type %s not in HostbasedAcceptedKeyTypes",
112: sshkey_type(key));
1.38 djm 113: goto done;
114: }
115: if ((r = sshkey_check_cert_sigtype(key,
116: options.ca_sign_algorithms)) != 0) {
1.43 djm 117: logit_fr(r, "certificate signature algorithm %s",
1.38 djm 118: (key->cert == NULL || key->cert->signature_type == NULL) ?
1.43 djm 119: "(null)" : key->cert->signature_type);
1.36 djm 120: goto done;
121: }
122:
123: if (!authctxt->valid || authctxt->user == NULL) {
1.43 djm 124: debug2_f("disabled because of invalid user");
1.22 djm 125: goto done;
126: }
127:
1.29 markus 128: if ((b = sshbuf_new()) == NULL)
1.43 djm 129: fatal_f("sshbuf_new failed");
1.1 markus 130: /* reconstruct packet */
1.29 markus 131: if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
132: (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
133: (r = sshbuf_put_cstring(b, authctxt->user)) != 0 ||
1.33 djm 134: (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1.29 markus 135: (r = sshbuf_put_cstring(b, "hostbased")) != 0 ||
136: (r = sshbuf_put_string(b, pkalg, alen)) != 0 ||
137: (r = sshbuf_put_string(b, pkblob, blen)) != 0 ||
138: (r = sshbuf_put_cstring(b, chost)) != 0 ||
139: (r = sshbuf_put_cstring(b, cuser)) != 0)
1.43 djm 140: fatal_fr(r, "reconstruct packet");
1.1 markus 141: #ifdef DEBUG_PK
1.29 markus 142: sshbuf_dump(b, stderr);
1.1 markus 143: #endif
1.16 djm 144:
1.31 djm 145: auth2_record_info(authctxt,
1.16 djm 146: "client user \"%.100s\", client host \"%.100s\"", cuser, chost);
147:
1.1 markus 148: /* test for allowed key and correct signature */
149: authenticated = 0;
1.40 djm 150: if (PRIVSEP(hostbased_key_allowed(ssh, authctxt->pw, cuser,
151: chost, key)) &&
1.29 markus 152: PRIVSEP(sshkey_verify(key, sig, slen,
1.42 djm 153: sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat, NULL)) == 0)
1.1 markus 154: authenticated = 1;
155:
1.31 djm 156: auth2_record_key(authctxt, authenticated, key);
1.29 markus 157: sshbuf_free(b);
1.1 markus 158: done:
1.43 djm 159: debug2_f("authenticated %d", authenticated);
1.31 djm 160: sshkey_free(key);
1.15 djm 161: free(pkalg);
162: free(pkblob);
163: free(cuser);
164: free(chost);
165: free(sig);
1.1 markus 166: return authenticated;
167: }
168:
169: /* return 1 if given hostkey is allowed */
170: int
1.40 djm 171: hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
172: const char *cuser, char *chost, struct sshkey *key)
1.1 markus 173: {
1.14 djm 174: const char *resolvedname, *ipaddr, *lookup, *reason;
1.1 markus 175: HostStatus host_status;
176: int len;
1.14 djm 177: char *fp;
1.13 djm 178:
179: if (auth_key_is_revoked(key))
180: return 0;
1.1 markus 181:
1.26 djm 182: resolvedname = auth_get_canonical_hostname(ssh, options.use_dns);
183: ipaddr = ssh_remote_ipaddr(ssh);
1.1 markus 184:
1.43 djm 185: debug2_f("chost %s resolvedname %s ipaddr %s",
1.1 markus 186: chost, resolvedname, ipaddr);
187:
1.12 djm 188: if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
189: debug2("stripping trailing dot from chost %s", chost);
190: chost[len - 1] = '\0';
191: }
192:
1.1 markus 193: if (options.hostbased_uses_name_from_packet_only) {
1.20 djm 194: if (auth_rhosts2(pw, cuser, chost, chost) == 0) {
1.43 djm 195: debug2_f("auth_rhosts2 refused user \"%.100s\" "
196: "host \"%.100s\" (from packet)", cuser, chost);
1.1 markus 197: return 0;
1.20 djm 198: }
1.1 markus 199: lookup = chost;
200: } else {
201: if (strcasecmp(resolvedname, chost) != 0)
1.3 itojun 202: logit("userauth_hostbased mismatch: "
1.1 markus 203: "client sends %s, but we resolve %s to %s",
204: chost, ipaddr, resolvedname);
1.20 djm 205: if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) {
1.43 djm 206: debug2_f("auth_rhosts2 refused "
1.20 djm 207: "user \"%.100s\" host \"%.100s\" addr \"%.100s\"",
1.43 djm 208: cuser, resolvedname, ipaddr);
1.1 markus 209: return 0;
1.20 djm 210: }
1.1 markus 211: lookup = resolvedname;
212: }
1.43 djm 213: debug2_f("access allowed by auth_rhosts2");
1.1 markus 214:
1.29 markus 215: if (sshkey_is_cert(key) &&
1.44 ! djm 216: sshkey_cert_check_authority(key, 1, 0, 0, lookup, &reason)) {
1.14 djm 217: error("%s", reason);
218: auth_debug_add("%s", reason);
219: return 0;
220: }
221:
1.1 markus 222: host_status = check_key_in_hostfiles(pw, key, lookup,
223: _PATH_SSH_SYSTEM_HOSTFILE,
224: options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
225:
226: /* backward compat if no key has been found. */
1.14 djm 227: if (host_status == HOST_NEW) {
1.1 markus 228: host_status = check_key_in_hostfiles(pw, key, lookup,
229: _PATH_SSH_SYSTEM_HOSTFILE2,
230: options.ignore_user_known_hosts ? NULL :
231: _PATH_SSH_USER_HOSTFILE2);
1.14 djm 232: }
233:
234: if (host_status == HOST_OK) {
1.29 markus 235: if (sshkey_is_cert(key)) {
1.24 djm 236: if ((fp = sshkey_fingerprint(key->cert->signature_key,
237: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1.43 djm 238: fatal_f("sshkey_fingerprint fail");
1.14 djm 239: verbose("Accepted certificate ID \"%s\" signed by "
240: "%s CA %s from %s@%s", key->cert->key_id,
1.29 markus 241: sshkey_type(key->cert->signature_key), fp,
1.14 djm 242: cuser, lookup);
243: } else {
1.24 djm 244: if ((fp = sshkey_fingerprint(key,
245: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1.43 djm 246: fatal_f("sshkey_fingerprint fail");
1.14 djm 247: verbose("Accepted %s public key %s from %s@%s",
1.29 markus 248: sshkey_type(key), fp, cuser, lookup);
1.14 djm 249: }
1.15 djm 250: free(fp);
1.14 djm 251: }
1.1 markus 252:
253: return (host_status == HOST_OK);
254: }
1.2 markus 255:
256: Authmethod method_hostbased = {
257: "hostbased",
258: userauth_hostbased,
259: &options.hostbased_authentication
260: };