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