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