2015-05-04 08:10:48 +02:00
|
|
|
/* $OpenBSD: auth2-hostbased.c,v 1.25 2015/05/04 06:10:48 djm Exp $ */
|
2002-06-06 22:27:55 +02: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 12:53:08 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <pwd.h>
|
2006-07-24 06:13:33 +02:00
|
|
|
#include <string.h>
|
2006-08-05 04:39:39 +02:00
|
|
|
#include <stdarg.h>
|
2006-07-10 12:53:08 +02:00
|
|
|
|
2002-06-06 22:27:55 +02:00
|
|
|
#include "xmalloc.h"
|
2006-08-05 04:39:39 +02:00
|
|
|
#include "ssh2.h"
|
2002-06-06 22:27:55 +02:00
|
|
|
#include "packet.h"
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "log.h"
|
2014-07-18 06:11:24 +02:00
|
|
|
#include "misc.h"
|
2002-06-06 22:27:55 +02:00
|
|
|
#include "servconf.h"
|
|
|
|
#include "compat.h"
|
|
|
|
#include "key.h"
|
2006-08-05 04:39:39 +02:00
|
|
|
#include "hostfile.h"
|
|
|
|
#include "auth.h"
|
2002-06-06 22:27:55 +02:00
|
|
|
#include "canohost.h"
|
2006-08-05 04:39:39 +02:00
|
|
|
#ifdef GSSAPI
|
|
|
|
#include "ssh-gss.h"
|
|
|
|
#endif
|
2002-06-06 22:27:55 +02:00
|
|
|
#include "monitor_wrap.h"
|
|
|
|
#include "pathnames.h"
|
2015-01-13 08:39:19 +01:00
|
|
|
#include "match.h"
|
2002-06-06 22:27:55 +02:00
|
|
|
|
|
|
|
/* import */
|
|
|
|
extern ServerOptions options;
|
|
|
|
extern u_char *session_id2;
|
2003-06-28 04:38:01 +02:00
|
|
|
extern u_int session_id2_len;
|
2002-06-06 22:27:55 +02:00
|
|
|
|
|
|
|
static int
|
|
|
|
userauth_hostbased(Authctxt *authctxt)
|
|
|
|
{
|
|
|
|
Buffer b;
|
|
|
|
Key *key = NULL;
|
|
|
|
char *pkalg, *cuser, *chost, *service;
|
|
|
|
u_char *pkblob, *sig;
|
|
|
|
u_int alen, blen, slen;
|
|
|
|
int pktype;
|
|
|
|
int authenticated = 0;
|
|
|
|
|
|
|
|
if (!authctxt->valid) {
|
|
|
|
debug2("userauth_hostbased: disabled because of invalid user");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
pkalg = packet_get_string(&alen);
|
|
|
|
pkblob = packet_get_string(&blen);
|
|
|
|
chost = packet_get_string(NULL);
|
|
|
|
cuser = packet_get_string(NULL);
|
|
|
|
sig = packet_get_string(&slen);
|
|
|
|
|
|
|
|
debug("userauth_hostbased: cuser %s chost %s pkalg %s slen %d",
|
|
|
|
cuser, chost, pkalg, slen);
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
debug("signature:");
|
|
|
|
buffer_init(&b);
|
|
|
|
buffer_append(&b, sig, slen);
|
|
|
|
buffer_dump(&b);
|
|
|
|
buffer_free(&b);
|
|
|
|
#endif
|
|
|
|
pktype = key_type_from_name(pkalg);
|
|
|
|
if (pktype == KEY_UNSPEC) {
|
|
|
|
/* this is perfectly legal */
|
2003-04-09 12:59:48 +02:00
|
|
|
logit("userauth_hostbased: unsupported "
|
2002-06-06 22:27:55 +02:00
|
|
|
"public key algorithm: %s", pkalg);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
key = key_from_blob(pkblob, blen);
|
|
|
|
if (key == NULL) {
|
|
|
|
error("userauth_hostbased: cannot decode key: %s", pkalg);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (key->type != pktype) {
|
|
|
|
error("userauth_hostbased: type mismatch for decoded key "
|
|
|
|
"(received %d, expected %d)", key->type, pktype);
|
|
|
|
goto done;
|
|
|
|
}
|
2013-12-31 02:25:40 +01:00
|
|
|
if (key_type_plain(key->type) == KEY_RSA &&
|
|
|
|
(datafellows & SSH_BUG_RSASIGMD5) != 0) {
|
|
|
|
error("Refusing RSA key because peer uses unsafe "
|
|
|
|
"signature format");
|
|
|
|
goto done;
|
|
|
|
}
|
2015-01-13 08:39:19 +01:00
|
|
|
if (match_pattern_list(sshkey_ssh_name(key),
|
2015-05-04 08:10:48 +02:00
|
|
|
options.hostbased_key_types, 0) != 1) {
|
2015-01-13 08:39:19 +01:00
|
|
|
logit("%s: key type %s not in HostbasedAcceptedKeyTypes",
|
|
|
|
__func__, sshkey_type(key));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2002-06-06 22:27:55 +02:00
|
|
|
service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
|
|
|
|
authctxt->service;
|
|
|
|
buffer_init(&b);
|
|
|
|
buffer_put_string(&b, session_id2, session_id2_len);
|
|
|
|
/* reconstruct packet */
|
|
|
|
buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
|
|
|
|
buffer_put_cstring(&b, authctxt->user);
|
|
|
|
buffer_put_cstring(&b, service);
|
|
|
|
buffer_put_cstring(&b, "hostbased");
|
|
|
|
buffer_put_string(&b, pkalg, alen);
|
|
|
|
buffer_put_string(&b, pkblob, blen);
|
|
|
|
buffer_put_cstring(&b, chost);
|
|
|
|
buffer_put_cstring(&b, cuser);
|
|
|
|
#ifdef DEBUG_PK
|
|
|
|
buffer_dump(&b);
|
|
|
|
#endif
|
2013-07-18 08:10:09 +02:00
|
|
|
|
|
|
|
pubkey_auth_info(authctxt, key,
|
|
|
|
"client user \"%.100s\", client host \"%.100s\"", cuser, chost);
|
|
|
|
|
2002-06-06 22:27:55 +02:00
|
|
|
/* test for allowed key and correct signature */
|
|
|
|
authenticated = 0;
|
|
|
|
if (PRIVSEP(hostbased_key_allowed(authctxt->pw, cuser, chost, key)) &&
|
|
|
|
PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
|
|
|
|
buffer_len(&b))) == 1)
|
|
|
|
authenticated = 1;
|
|
|
|
|
2004-01-21 01:02:50 +01:00
|
|
|
buffer_free(&b);
|
2002-06-06 22:27:55 +02:00
|
|
|
done:
|
|
|
|
debug2("userauth_hostbased: authenticated %d", authenticated);
|
|
|
|
if (key != NULL)
|
|
|
|
key_free(key);
|
2013-06-01 23:31:17 +02:00
|
|
|
free(pkalg);
|
|
|
|
free(pkblob);
|
|
|
|
free(cuser);
|
|
|
|
free(chost);
|
|
|
|
free(sig);
|
2002-06-06 22:27:55 +02:00
|
|
|
return authenticated;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return 1 if given hostkey is allowed */
|
|
|
|
int
|
|
|
|
hostbased_key_allowed(struct passwd *pw, const char *cuser, char *chost,
|
|
|
|
Key *key)
|
|
|
|
{
|
2010-08-05 05:04:50 +02:00
|
|
|
const char *resolvedname, *ipaddr, *lookup, *reason;
|
2002-06-06 22:27:55 +02:00
|
|
|
HostStatus host_status;
|
|
|
|
int len;
|
2010-08-05 05:04:50 +02:00
|
|
|
char *fp;
|
2002-06-06 22:27:55 +02:00
|
|
|
|
2010-03-04 11:53:35 +01:00
|
|
|
if (auth_key_is_revoked(key))
|
|
|
|
return 0;
|
|
|
|
|
2003-06-03 02:25:48 +02:00
|
|
|
resolvedname = get_canonical_hostname(options.use_dns);
|
2002-06-06 22:27:55 +02:00
|
|
|
ipaddr = get_remote_ipaddr();
|
|
|
|
|
2014-12-23 23:42:48 +01:00
|
|
|
debug2("%s: chost %s resolvedname %s ipaddr %s", __func__,
|
2002-06-06 22:27:55 +02:00
|
|
|
chost, resolvedname, ipaddr);
|
|
|
|
|
2008-07-17 10:57:19 +02:00
|
|
|
if (((len = strlen(chost)) > 0) && chost[len - 1] == '.') {
|
|
|
|
debug2("stripping trailing dot from chost %s", chost);
|
|
|
|
chost[len - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
2002-06-06 22:27:55 +02:00
|
|
|
if (options.hostbased_uses_name_from_packet_only) {
|
2014-12-23 23:42:48 +01:00
|
|
|
if (auth_rhosts2(pw, cuser, chost, chost) == 0) {
|
|
|
|
debug2("%s: auth_rhosts2 refused "
|
|
|
|
"user \"%.100s\" host \"%.100s\" (from packet)",
|
|
|
|
__func__, cuser, chost);
|
2002-06-06 22:27:55 +02:00
|
|
|
return 0;
|
2014-12-23 23:42:48 +01:00
|
|
|
}
|
2002-06-06 22:27:55 +02:00
|
|
|
lookup = chost;
|
|
|
|
} else {
|
|
|
|
if (strcasecmp(resolvedname, chost) != 0)
|
2003-04-09 12:59:48 +02:00
|
|
|
logit("userauth_hostbased mismatch: "
|
2002-06-06 22:27:55 +02:00
|
|
|
"client sends %s, but we resolve %s to %s",
|
|
|
|
chost, ipaddr, resolvedname);
|
2014-12-23 23:42:48 +01:00
|
|
|
if (auth_rhosts2(pw, cuser, resolvedname, ipaddr) == 0) {
|
|
|
|
debug2("%s: auth_rhosts2 refused "
|
|
|
|
"user \"%.100s\" host \"%.100s\" addr \"%.100s\"",
|
|
|
|
__func__, cuser, resolvedname, ipaddr);
|
2002-06-06 22:27:55 +02:00
|
|
|
return 0;
|
2014-12-23 23:42:48 +01:00
|
|
|
}
|
2002-06-06 22:27:55 +02:00
|
|
|
lookup = resolvedname;
|
|
|
|
}
|
2014-12-23 23:42:48 +01:00
|
|
|
debug2("%s: access allowed by auth_rhosts2", __func__);
|
2002-06-06 22:27:55 +02:00
|
|
|
|
2010-08-05 05:04:50 +02:00
|
|
|
if (key_is_cert(key) &&
|
|
|
|
key_cert_check_authority(key, 1, 0, lookup, &reason)) {
|
|
|
|
error("%s", reason);
|
|
|
|
auth_debug_add("%s", reason);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-06 22:27:55 +02: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 05:04:50 +02:00
|
|
|
if (host_status == HOST_NEW) {
|
2002-06-06 22:27:55 +02: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 05:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (host_status == HOST_OK) {
|
|
|
|
if (key_is_cert(key)) {
|
2015-01-28 23:36:00 +01:00
|
|
|
if ((fp = sshkey_fingerprint(key->cert->signature_key,
|
|
|
|
options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
|
|
|
|
fatal("%s: sshkey_fingerprint fail", __func__);
|
2010-08-05 05:04:50 +02:00
|
|
|
verbose("Accepted certificate ID \"%s\" signed by "
|
|
|
|
"%s CA %s from %s@%s", key->cert->key_id,
|
|
|
|
key_type(key->cert->signature_key), fp,
|
|
|
|
cuser, lookup);
|
|
|
|
} else {
|
2015-01-28 23:36:00 +01:00
|
|
|
if ((fp = sshkey_fingerprint(key,
|
|
|
|
options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
|
|
|
|
fatal("%s: sshkey_fingerprint fail", __func__);
|
2010-08-05 05:04:50 +02:00
|
|
|
verbose("Accepted %s public key %s from %s@%s",
|
|
|
|
key_type(key), fp, cuser, lookup);
|
|
|
|
}
|
2013-06-01 23:31:17 +02:00
|
|
|
free(fp);
|
2010-08-05 05:04:50 +02:00
|
|
|
}
|
2002-06-06 22:27:55 +02:00
|
|
|
|
|
|
|
return (host_status == HOST_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
Authmethod method_hostbased = {
|
|
|
|
"hostbased",
|
|
|
|
userauth_hostbased,
|
|
|
|
&options.hostbased_authentication
|
|
|
|
};
|