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