2020-10-18 11:32:01 +00:00
|
|
|
/* $OpenBSD: auth2-hostbased.c,v 1.43 2020/10/18 11:32:01 djm Exp $ */
|
2002-06-06 20:27:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2000 Markus Friedl. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
2006-07-10 10:53:08 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2019-09-06 04:53:27 +00:00
|
|
|
#include <stdlib.h>
|
2006-07-10 10:53:08 +00:00
|
|
|
#include <pwd.h>
|
2006-07-24 04:13:33 +00:00
|
|
|
#include <string.h>
|
2006-08-05 02:39:39 +00:00
|
|
|
#include <stdarg.h>
|
2006-07-10 10:53:08 +00:00
|
|
|
|
2002-06-06 20:27:55 +00:00
|
|
|
#include "xmalloc.h"
|
2006-08-05 02:39:39 +00:00
|
|
|
#include "ssh2.h"
|
2002-06-06 20:27:55 +00:00
|
|
|
#include "packet.h"
|
2018-07-09 21:35:50 +00:00
|
|
|
#include "sshbuf.h"
|
2002-06-06 20:27:55 +00:00
|
|
|
#include "log.h"
|
2014-07-18 04:11:24 +00:00
|
|
|
#include "misc.h"
|
2002-06-06 20:27:55 +00:00
|
|
|
#include "servconf.h"
|
|
|
|
#include "compat.h"
|
2017-05-30 14:25:42 +00:00
|
|
|
#include "sshkey.h"
|
2006-08-05 02:39:39 +00:00
|
|
|
#include "hostfile.h"
|
|
|
|
#include "auth.h"
|
2002-06-06 20:27:55 +00:00
|
|
|
#include "canohost.h"
|
2006-08-05 02:39:39 +00:00
|
|
|
#ifdef GSSAPI
|
|
|
|
#include "ssh-gss.h"
|
|
|
|
#endif
|
2002-06-06 20:27:55 +00:00
|
|
|
#include "monitor_wrap.h"
|
|
|
|
#include "pathnames.h"
|
2017-05-30 14:25:42 +00:00
|
|
|
#include "ssherr.h"
|
2015-01-13 07:39:19 +00:00
|
|
|
#include "match.h"
|
2002-06-06 20:27:55 +00:00
|
|
|
|
|
|
|
/* import */
|
|
|
|
extern ServerOptions options;
|
|
|
|
extern u_char *session_id2;
|
2003-06-28 02:38:01 +00:00
|
|
|
extern u_int session_id2_len;
|
2002-06-06 20:27:55 +00:00
|
|
|
|
|
|
|
static int
|
2017-05-30 14:29:59 +00:00
|
|
|
userauth_hostbased(struct ssh *ssh)
|
2002-06-06 20:27:55 +00:00
|
|
|
{
|
2017-05-30 14:29:59 +00:00
|
|
|
Authctxt *authctxt = ssh->authctxt;
|
2017-05-30 14:25:42 +00:00
|
|
|
struct sshbuf *b;
|
2017-05-30 08:52:19 +00:00
|
|
|
struct sshkey *key = NULL;
|
2018-01-23 05:27:21 +00:00
|
|
|
char *pkalg, *cuser, *chost;
|
2002-06-06 20:27:55 +00:00
|
|
|
u_char *pkblob, *sig;
|
2017-05-30 14:25:42 +00:00
|
|
|
size_t alen, blen, slen;
|
|
|
|
int r, pktype, authenticated = 0;
|
2002-06-06 20:27:55 +00:00
|
|
|
|
2017-05-30 14:25:42 +00:00
|
|
|
/* XXX use sshkey_froms() */
|
|
|
|
if ((r = sshpkt_get_cstring(ssh, &pkalg, &alen)) != 0 ||
|
|
|
|
(r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
|
|
|
|
(r = sshpkt_get_cstring(ssh, &chost, NULL)) != 0 ||
|
|
|
|
(r = sshpkt_get_cstring(ssh, &cuser, NULL)) != 0 ||
|
|
|
|
(r = sshpkt_get_string(ssh, &sig, &slen)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_fr(r, "parse packet");
|
2002-06-06 20:27:55 +00:00
|
|
|
|
2020-10-18 11:32:01 +00:00
|
|
|
debug_f("cuser %s chost %s pkalg %s slen %zu",
|
2002-06-06 20:27:55 +00:00
|
|
|
cuser, chost, pkalg, slen);
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
debug("signature:");
|
2018-08-28 12:17:45 +00:00
|
|
|
sshbuf_dump_data(sig, slen, stderr);
|
2002-06-06 20:27:55 +00:00
|
|
|
#endif
|
2017-05-30 14:25:42 +00:00
|
|
|
pktype = sshkey_type_from_name(pkalg);
|
2002-06-06 20:27:55 +00:00
|
|
|
if (pktype == KEY_UNSPEC) {
|
|
|
|
/* this is perfectly legal */
|
2020-10-18 11:32:01 +00:00
|
|
|
logit_f("unsupported public key algorithm: %s",
|
|
|
|
pkalg);
|
2017-05-30 14:25:42 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "key_from_blob");
|
2002-06-06 20:27:55 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (key == NULL) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("cannot decode key: %s", pkalg);
|
2002-06-06 20:27:55 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (key->type != pktype) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("type mismatch for decoded key "
|
|
|
|
"(received %d, expected %d)", key->type, pktype);
|
2002-06-06 20:27:55 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2017-05-30 14:25:42 +00:00
|
|
|
if (sshkey_type_plain(key->type) == KEY_RSA &&
|
|
|
|
(ssh->compat & SSH_BUG_RSASIGMD5) != 0) {
|
2013-12-31 01:25:40 +00:00
|
|
|
error("Refusing RSA key because peer uses unsafe "
|
|
|
|
"signature format");
|
|
|
|
goto done;
|
|
|
|
}
|
2018-07-03 11:39:54 +00:00
|
|
|
if (match_pattern_list(pkalg, options.hostbased_key_types, 0) != 1) {
|
2020-10-18 11:32:01 +00:00
|
|
|
logit_f("key type %s not in HostbasedAcceptedKeyTypes",
|
|
|
|
sshkey_type(key));
|
2015-01-13 07:39:19 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2018-09-20 03:28:06 +00:00
|
|
|
if ((r = sshkey_check_cert_sigtype(key,
|
|
|
|
options.ca_sign_algorithms)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
logit_fr(r, "certificate signature algorithm %s",
|
2018-09-20 03:28:06 +00:00
|
|
|
(key->cert == NULL || key->cert->signature_type == NULL) ?
|
2020-10-18 11:32:01 +00:00
|
|
|
"(null)" : key->cert->signature_type);
|
2018-09-20 03:28:06 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2015-01-13 07:39:19 +00:00
|
|
|
|
2018-07-31 03:10:27 +00:00
|
|
|
if (!authctxt->valid || authctxt->user == NULL) {
|
2020-10-18 11:32:01 +00:00
|
|
|
debug2_f("disabled because of invalid user");
|
2018-07-31 03:10:27 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2017-05-30 14:25:42 +00:00
|
|
|
if ((b = sshbuf_new()) == NULL)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_f("sshbuf_new failed");
|
2002-06-06 20:27:55 +00:00
|
|
|
/* reconstruct packet */
|
2017-05-30 14:25:42 +00:00
|
|
|
if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
|
|
|
|
(r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(b, authctxt->user)) != 0 ||
|
2018-01-23 05:27:21 +00:00
|
|
|
(r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
|
2017-05-30 14:25:42 +00:00
|
|
|
(r = sshbuf_put_cstring(b, "hostbased")) != 0 ||
|
|
|
|
(r = sshbuf_put_string(b, pkalg, alen)) != 0 ||
|
|
|
|
(r = sshbuf_put_string(b, pkblob, blen)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(b, chost)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(b, cuser)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_fr(r, "reconstruct packet");
|
2002-06-06 20:27:55 +00:00
|
|
|
#ifdef DEBUG_PK
|
2017-05-30 14:25:42 +00:00
|
|
|
sshbuf_dump(b, stderr);
|
2002-06-06 20:27:55 +00:00
|
|
|
#endif
|
2013-07-18 06:10:09 +00:00
|
|
|
|
2017-06-24 06:34:38 +00:00
|
|
|
auth2_record_info(authctxt,
|
2013-07-18 06:10:09 +00:00
|
|
|
"client user \"%.100s\", client host \"%.100s\"", cuser, chost);
|
|
|
|
|
2002-06-06 20:27:55 +00:00
|
|
|
/* test for allowed key and correct signature */
|
|
|
|
authenticated = 0;
|
2019-01-19 21:43:56 +00:00
|
|
|
if (PRIVSEP(hostbased_key_allowed(ssh, authctxt->pw, cuser,
|
|
|
|
chost, key)) &&
|
2017-05-30 14:25:42 +00:00
|
|
|
PRIVSEP(sshkey_verify(key, sig, slen,
|
2019-11-25 00:51:37 +00:00
|
|
|
sshbuf_ptr(b), sshbuf_len(b), pkalg, ssh->compat, NULL)) == 0)
|
2002-06-06 20:27:55 +00:00
|
|
|
authenticated = 1;
|
|
|
|
|
2017-06-24 06:34:38 +00:00
|
|
|
auth2_record_key(authctxt, authenticated, key);
|
2017-05-30 14:25:42 +00:00
|
|
|
sshbuf_free(b);
|
2002-06-06 20:27:55 +00:00
|
|
|
done:
|
2020-10-18 11:32:01 +00:00
|
|
|
debug2_f("authenticated %d", authenticated);
|
2017-06-24 06:34:38 +00:00
|
|
|
sshkey_free(key);
|
2013-06-01 21:31:17 +00:00
|
|
|
free(pkalg);
|
|
|
|
free(pkblob);
|
|
|
|
free(cuser);
|
|
|
|
free(chost);
|
|
|
|
free(sig);
|
2002-06-06 20:27:55 +00:00
|
|
|
return authenticated;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return 1 if given hostkey is allowed */
|
|
|
|
int
|
2019-01-19 21:43:56 +00:00
|
|
|
hostbased_key_allowed(struct ssh *ssh, struct passwd *pw,
|
|
|
|
const char *cuser, char *chost, struct sshkey *key)
|
2002-06-06 20:27:55 +00:00
|
|
|
{
|
2010-08-05 03:04:50 +00:00
|
|
|
const char *resolvedname, *ipaddr, *lookup, *reason;
|
2002-06-06 20:27:55 +00:00
|
|
|
HostStatus host_status;
|
|
|
|
int len;
|
2010-08-05 03:04:50 +00:00
|
|
|
char *fp;
|
2002-06-06 20:27:55 +00:00
|
|
|
|
2010-03-04 10:53:35 +00:00
|
|
|
if (auth_key_is_revoked(key))
|
|
|
|
return 0;
|
|
|
|
|
2016-03-07 19:02:43 +00:00
|
|
|
resolvedname = auth_get_canonical_hostname(ssh, options.use_dns);
|
|
|
|
ipaddr = ssh_remote_ipaddr(ssh);
|
2002-06-06 20:27:55 +00:00
|
|
|
|
2020-10-18 11:32:01 +00:00
|
|
|
debug2_f("chost %s resolvedname %s ipaddr %s",
|
2002-06-06 20:27:55 +00:00
|
|
|
chost, resolvedname, ipaddr);
|
|
|
|
|
2008-07-17 08:57:19 +00:00
|
|
|
if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
|
|
|
|
debug2("stripping trailing dot from chost %s", chost);
|
|
|
|
chost[len - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
2002-06-06 20:27:55 +00:00
|
|
|
if (options.hostbased_uses_name_from_packet_only) {
|
2014-12-23 22:42:48 +00:00
|
|
|
if (auth_rhosts2(pw, cuser, chost, chost) == 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
debug2_f("auth_rhosts2 refused user \"%.100s\" "
|
|
|
|
"host \"%.100s\" (from packet)", cuser, chost);
|
2002-06-06 20:27:55 +00:00
|
|
|
return 0;
|
2014-12-23 22:42:48 +00:00
|
|
|
}
|
2002-06-06 20:27:55 +00:00
|
|
|
lookup = chost;
|
|
|
|
} else {
|
|
|
|
if (strcasecmp(resolvedname, chost) != 0)
|
2003-04-09 10:59:48 +00:00
|
|
|
logit("userauth_hostbased mismatch: "
|
2002-06-06 20:27:55 +00:00
|
|
|
"client sends %s, but we resolve %s to %s",
|
|
|
|
chost, ipaddr, resolvedname);
|
2014-12-23 22:42:48 +00:00
|
|
|
if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
debug2_f("auth_rhosts2 refused "
|
2014-12-23 22:42:48 +00:00
|
|
|
"user \"%.100s\" host \"%.100s\" addr \"%.100s\"",
|
2020-10-18 11:32:01 +00:00
|
|
|
cuser, resolvedname, ipaddr);
|
2002-06-06 20:27:55 +00:00
|
|
|
return 0;
|
2014-12-23 22:42:48 +00:00
|
|
|
}
|
2002-06-06 20:27:55 +00:00
|
|
|
lookup = resolvedname;
|
|
|
|
}
|
2020-10-18 11:32:01 +00:00
|
|
|
debug2_f("access allowed by auth_rhosts2");
|
2002-06-06 20:27:55 +00:00
|
|
|
|
2017-05-30 14:25:42 +00:00
|
|
|
if (sshkey_is_cert(key) &&
|
|
|
|
sshkey_cert_check_authority(key, 1, 0, lookup, &reason)) {
|
2010-08-05 03:04:50 +00:00
|
|
|
error("%s", reason);
|
|
|
|
auth_debug_add("%s", reason);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-06 20:27:55 +00:00
|
|
|
host_status = check_key_in_hostfiles(pw, key, lookup,
|
|
|
|
_PATH_SSH_SYSTEM_HOSTFILE,
|
|
|
|
options.ignore_user_known_hosts ? NULL : _PATH_SSH_USER_HOSTFILE);
|
|
|
|
|
|
|
|
/* backward compat if no key has been found. */
|
2010-08-05 03:04:50 +00:00
|
|
|
if (host_status == HOST_NEW) {
|
2002-06-06 20:27:55 +00:00
|
|
|
host_status = check_key_in_hostfiles(pw, key, lookup,
|
|
|
|
_PATH_SSH_SYSTEM_HOSTFILE2,
|
|
|
|
options.ignore_user_known_hosts ? NULL :
|
|
|
|
_PATH_SSH_USER_HOSTFILE2);
|
2010-08-05 03:04:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (host_status == HOST_OK) {
|
2017-05-30 14:25:42 +00:00
|
|
|
if (sshkey_is_cert(key)) {
|
2015-01-28 22:36:00 +00:00
|
|
|
if ((fp = sshkey_fingerprint(key->cert->signature_key,
|
|
|
|
options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_f("sshkey_fingerprint fail");
|
2010-08-05 03:04:50 +00:00
|
|
|
verbose("Accepted certificate ID \"%s\" signed by "
|
|
|
|
"%s CA %s from %s@%s", key->cert->key_id,
|
2017-05-30 14:25:42 +00:00
|
|
|
sshkey_type(key->cert->signature_key), fp,
|
2010-08-05 03:04:50 +00:00
|
|
|
cuser, lookup);
|
|
|
|
} else {
|
2015-01-28 22:36:00 +00:00
|
|
|
if ((fp = sshkey_fingerprint(key,
|
|
|
|
options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_f("sshkey_fingerprint fail");
|
2010-08-05 03:04:50 +00:00
|
|
|
verbose("Accepted %s public key %s from %s@%s",
|
2017-05-30 14:25:42 +00:00
|
|
|
sshkey_type(key), fp, cuser, lookup);
|
2010-08-05 03:04:50 +00:00
|
|
|
}
|
2013-06-01 21:31:17 +00:00
|
|
|
free(fp);
|
2010-08-05 03:04:50 +00:00
|
|
|
}
|
2002-06-06 20:27:55 +00:00
|
|
|
|
|
|
|
return (host_status == HOST_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
Authmethod method_hostbased = {
|
|
|
|
"hostbased",
|
|
|
|
userauth_hostbased,
|
|
|
|
&options.hostbased_authentication
|
|
|
|
};
|