2023-07-27 22:25:17 +00:00
|
|
|
/* $OpenBSD: auth2-pubkey.c,v 1.119 2023/07/27 22:25:17 djm Exp $ */
|
2002-06-06 20:27:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2000 Markus Friedl. All rights reserved.
|
2022-05-27 05:02:46 +00:00
|
|
|
* Copyright (c) 2010 Damien Miller. All rights reserved.
|
2002-06-06 20:27:55 +00:00
|
|
|
*
|
|
|
|
* 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-03-15 00:45:54 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2002-06-06 20:27:55 +00:00
|
|
|
|
2019-09-06 04:53:27 +00:00
|
|
|
#include <stdlib.h>
|
2012-10-30 21:58:58 +00:00
|
|
|
#include <errno.h>
|
2012-11-05 06:07:43 +00:00
|
|
|
#ifdef HAVE_PATHS_H
|
|
|
|
# include <paths.h>
|
|
|
|
#endif
|
2006-07-10 10:53:08 +00:00
|
|
|
#include <pwd.h>
|
2012-10-30 21:58:58 +00:00
|
|
|
#include <signal.h>
|
2006-08-05 01:37:59 +00:00
|
|
|
#include <stdio.h>
|
2006-08-05 02:39:39 +00:00
|
|
|
#include <stdarg.h>
|
2010-02-26 20:55:05 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2008-06-13 23:01:24 +00:00
|
|
|
#include <unistd.h>
|
2014-12-22 07:51:30 +00:00
|
|
|
#include <limits.h>
|
2006-07-10 10:53:08 +00:00
|
|
|
|
2006-08-05 02:39:39 +00:00
|
|
|
#include "xmalloc.h"
|
2004-12-06 11:47:41 +00:00
|
|
|
#include "ssh.h"
|
2002-06-06 20:27:55 +00:00
|
|
|
#include "ssh2.h"
|
|
|
|
#include "packet.h"
|
2021-01-27 10:05:28 +00:00
|
|
|
#include "kex.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:10:53 +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 "pathnames.h"
|
|
|
|
#include "uidswap.h"
|
|
|
|
#include "auth-options.h"
|
|
|
|
#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"
|
2010-03-04 10:53:35 +00:00
|
|
|
#include "authfile.h"
|
2010-05-10 01:58:03 +00:00
|
|
|
#include "match.h"
|
2015-05-21 06:38:35 +00:00
|
|
|
#include "ssherr.h"
|
|
|
|
#include "channels.h" /* XXX for session.h */
|
|
|
|
#include "session.h" /* XXX for child_set_env(); refactor? */
|
2019-11-25 00:52:46 +00:00
|
|
|
#include "sk-api.h"
|
2002-06-06 20:27:55 +00:00
|
|
|
|
|
|
|
/* import */
|
|
|
|
extern ServerOptions options;
|
|
|
|
|
2017-12-19 00:24:34 +00:00
|
|
|
static char *
|
|
|
|
format_key(const struct sshkey *key)
|
|
|
|
{
|
|
|
|
char *ret, *fp = sshkey_fingerprint(key,
|
|
|
|
options.fingerprint_hash, SSH_FP_DEFAULT);
|
|
|
|
|
|
|
|
xasprintf(&ret, "%s %s", sshkey_type(key), fp);
|
|
|
|
free(fp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2002-06-06 20:27:55 +00:00
|
|
|
static int
|
2021-12-19 22:12:07 +00:00
|
|
|
userauth_pubkey(struct ssh *ssh, const char *method)
|
2002-06-06 20:27:55 +00:00
|
|
|
{
|
2017-05-30 14:29:59 +00:00
|
|
|
Authctxt *authctxt = ssh->authctxt;
|
2018-03-03 03:15:51 +00:00
|
|
|
struct passwd *pw = authctxt->pw;
|
2018-07-31 03:10:27 +00:00
|
|
|
struct sshbuf *b = NULL;
|
2021-12-19 22:12:30 +00:00
|
|
|
struct sshkey *key = NULL, *hostkey = NULL;
|
2018-07-31 03:10:27 +00:00
|
|
|
char *pkalg = NULL, *userstyle = NULL, *key_s = NULL, *ca_s = NULL;
|
|
|
|
u_char *pkblob = NULL, *sig = NULL, have_sig;
|
2017-05-30 14:10:53 +00:00
|
|
|
size_t blen, slen;
|
2021-12-19 22:12:30 +00:00
|
|
|
int hostbound, r, pktype;
|
2020-08-27 01:07:09 +00:00
|
|
|
int req_presence = 0, req_verify = 0, authenticated = 0;
|
2018-03-03 03:15:51 +00:00
|
|
|
struct sshauthopt *authopts = NULL;
|
2019-11-25 00:51:37 +00:00
|
|
|
struct sshkey_sig_details *sig_details = NULL;
|
2002-06-06 20:27:55 +00:00
|
|
|
|
2021-12-19 22:12:30 +00:00
|
|
|
hostbound = strcmp(method, "publickey-hostbound-v00@openssh.com") == 0;
|
|
|
|
|
2018-01-23 05:27:21 +00:00
|
|
|
if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0 ||
|
|
|
|
(r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
|
|
|
|
(r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0)
|
2021-12-19 22:12:30 +00:00
|
|
|
fatal_fr(r, "parse %s packet", method);
|
|
|
|
|
|
|
|
/* hostbound auth includes the hostkey offered at initial KEX */
|
|
|
|
if (hostbound) {
|
|
|
|
if ((r = sshpkt_getb_froms(ssh, &b)) != 0 ||
|
|
|
|
(r = sshkey_fromb(b, &hostkey)) != 0)
|
|
|
|
fatal_fr(r, "parse %s hostkey", method);
|
|
|
|
if (ssh->kex->initial_hostkey == NULL)
|
|
|
|
fatal_f("internal error: initial hostkey not recorded");
|
|
|
|
if (!sshkey_equal(hostkey, ssh->kex->initial_hostkey))
|
|
|
|
fatal_f("%s packet contained wrong host key", method);
|
|
|
|
sshbuf_free(b);
|
|
|
|
b = NULL;
|
|
|
|
}
|
2019-01-22 11:26:16 +00:00
|
|
|
|
|
|
|
if (log_level_get() >= SYSLOG_LEVEL_DEBUG2) {
|
|
|
|
char *keystring;
|
|
|
|
struct sshbuf *pkbuf;
|
|
|
|
|
|
|
|
if ((pkbuf = sshbuf_from(pkblob, blen)) == NULL)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_f("sshbuf_from failed");
|
2019-07-16 13:18:39 +00:00
|
|
|
if ((keystring = sshbuf_dtob64_string(pkbuf, 0)) == NULL)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_f("sshbuf_dtob64 failed");
|
|
|
|
debug2_f("%s user %s %s public key %s %s",
|
2019-01-22 11:26:16 +00:00
|
|
|
authctxt->valid ? "valid" : "invalid", authctxt->user,
|
|
|
|
have_sig ? "attempting" : "querying", pkalg, keystring);
|
|
|
|
sshbuf_free(pkbuf);
|
|
|
|
free(keystring);
|
|
|
|
}
|
|
|
|
|
2017-05-30 14:10:53 +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
|
|
|
verbose_f("unsupported public key algorithm: %s", pkalg);
|
2002-06-06 20:27:55 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2017-05-30 14:10:53 +00:00
|
|
|
if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "parse key");
|
2017-05-30 14:10:53 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2002-06-06 20:27:55 +00:00
|
|
|
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-06-24 06:34:38 +00:00
|
|
|
if (auth2_key_already_used(authctxt, key)) {
|
2017-05-30 14:10:53 +00:00
|
|
|
logit("refusing previously-used %s key", sshkey_type(key));
|
2014-12-22 07:51:30 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2021-01-22 02:44:58 +00:00
|
|
|
if (match_pattern_list(pkalg, options.pubkey_accepted_algos, 0) != 1) {
|
2022-02-27 01:33:59 +00:00
|
|
|
logit_f("signature algorithm %s not in "
|
|
|
|
"PubkeyAcceptedAlgorithms", pkalg);
|
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;
|
|
|
|
}
|
2022-09-17 10:34:29 +00:00
|
|
|
if ((r = sshkey_check_rsa_length(key,
|
|
|
|
options.required_rsa_size)) != 0) {
|
|
|
|
logit_r(r, "refusing %s key", sshkey_type(key));
|
|
|
|
goto done;
|
|
|
|
}
|
2017-12-19 00:24:34 +00:00
|
|
|
key_s = format_key(key);
|
|
|
|
if (sshkey_is_cert(key))
|
|
|
|
ca_s = format_key(key->cert->signature_key);
|
|
|
|
|
2002-06-06 20:27:55 +00:00
|
|
|
if (have_sig) {
|
2021-12-19 22:12:30 +00:00
|
|
|
debug3_f("%s have %s signature for %s%s%s",
|
|
|
|
method, pkalg, key_s,
|
2020-10-18 11:32:01 +00:00
|
|
|
ca_s == NULL ? "" : " CA ", ca_s == NULL ? "" : ca_s);
|
2017-05-30 14:10:53 +00:00
|
|
|
if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 ||
|
|
|
|
(r = sshpkt_get_end(ssh)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_fr(r, "parse signature packet");
|
2017-05-30 14:10:53 +00:00
|
|
|
if ((b = sshbuf_new()) == NULL)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_f("sshbuf_new failed");
|
2017-05-30 14:10:53 +00:00
|
|
|
if (ssh->compat & SSH_OLD_SESSIONID) {
|
2021-01-27 10:05:28 +00:00
|
|
|
if ((r = sshbuf_putb(b, ssh->kex->session_id)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_fr(r, "put old session id");
|
2002-06-06 20:27:55 +00:00
|
|
|
} else {
|
2021-01-27 10:05:28 +00:00
|
|
|
if ((r = sshbuf_put_stringb(b,
|
|
|
|
ssh->kex->session_id)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_fr(r, "put session id");
|
2002-06-06 20:27:55 +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;
|
|
|
|
}
|
2002-06-06 20:27:55 +00:00
|
|
|
/* reconstruct packet */
|
2013-04-23 05:17:52 +00:00
|
|
|
xasprintf(&userstyle, "%s%s%s", authctxt->user,
|
|
|
|
authctxt->style ? ":" : "",
|
|
|
|
authctxt->style ? authctxt->style : "");
|
2017-05-30 14:10:53 +00:00
|
|
|
if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(b, userstyle)) != 0 ||
|
2018-01-23 05:27:21 +00:00
|
|
|
(r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
|
2021-12-19 22:12:07 +00:00
|
|
|
(r = sshbuf_put_cstring(b, method)) != 0 ||
|
2018-01-23 05:27:21 +00:00
|
|
|
(r = sshbuf_put_u8(b, have_sig)) != 0 ||
|
2018-08-28 12:25:53 +00:00
|
|
|
(r = sshbuf_put_cstring(b, pkalg)) != 0 ||
|
2018-01-23 05:27:21 +00:00
|
|
|
(r = sshbuf_put_string(b, pkblob, blen)) != 0)
|
2021-12-19 22:12:30 +00:00
|
|
|
fatal_fr(r, "reconstruct %s packet", method);
|
|
|
|
if (hostbound &&
|
|
|
|
(r = sshkey_puts(ssh->kex->initial_hostkey, b)) != 0)
|
|
|
|
fatal_fr(r, "reconstruct %s packet", method);
|
2002-06-06 20:27:55 +00:00
|
|
|
#ifdef DEBUG_PK
|
2017-05-30 14:10:53 +00:00
|
|
|
sshbuf_dump(b, stderr);
|
2002-06-06 20:27:55 +00:00
|
|
|
#endif
|
|
|
|
/* test for correct signature */
|
|
|
|
authenticated = 0;
|
2022-06-15 16:08:25 +00:00
|
|
|
if (PRIVSEP(user_key_allowed(ssh, pw, key, 1, &authopts)) &&
|
2018-07-03 11:39:54 +00:00
|
|
|
PRIVSEP(sshkey_verify(key, sig, slen,
|
|
|
|
sshbuf_ptr(b), sshbuf_len(b),
|
|
|
|
(ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL,
|
2019-11-25 00:51:37 +00:00
|
|
|
ssh->compat, &sig_details)) == 0) {
|
2002-06-06 20:27:55 +00:00
|
|
|
authenticated = 1;
|
2014-12-22 07:51:30 +00:00
|
|
|
}
|
2019-11-25 00:52:46 +00:00
|
|
|
if (authenticated == 1 && sig_details != NULL) {
|
|
|
|
auth2_record_info(authctxt, "signature count = %u",
|
|
|
|
sig_details->sk_counter);
|
2020-10-18 11:32:01 +00:00
|
|
|
debug_f("sk_counter = %u, sk_flags = 0x%02x",
|
|
|
|
sig_details->sk_counter, sig_details->sk_flags);
|
2019-11-25 00:52:46 +00:00
|
|
|
req_presence = (options.pubkey_auth_options &
|
2019-11-25 00:54:23 +00:00
|
|
|
PUBKEYAUTH_TOUCH_REQUIRED) ||
|
|
|
|
!authopts->no_require_user_presence;
|
2019-11-25 00:52:46 +00:00
|
|
|
if (req_presence && (sig_details->sk_flags &
|
|
|
|
SSH_SK_USER_PRESENCE_REQD) == 0) {
|
|
|
|
error("public key %s signature for %s%s from "
|
|
|
|
"%.128s port %d rejected: user presence "
|
2020-02-06 22:30:54 +00:00
|
|
|
"(authenticator touch) requirement "
|
|
|
|
"not met ", key_s,
|
2019-11-25 00:52:46 +00:00
|
|
|
authctxt->valid ? "" : "invalid user ",
|
|
|
|
authctxt->user, ssh_remote_ipaddr(ssh),
|
|
|
|
ssh_remote_port(ssh));
|
|
|
|
authenticated = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
2020-08-27 01:07:09 +00:00
|
|
|
req_verify = (options.pubkey_auth_options &
|
|
|
|
PUBKEYAUTH_VERIFY_REQUIRED) ||
|
|
|
|
authopts->require_verify;
|
|
|
|
if (req_verify && (sig_details->sk_flags &
|
|
|
|
SSH_SK_USER_VERIFICATION_REQD) == 0) {
|
|
|
|
error("public key %s signature for %s%s from "
|
|
|
|
"%.128s port %d rejected: user "
|
|
|
|
"verification requirement not met ", key_s,
|
|
|
|
authctxt->valid ? "" : "invalid user ",
|
|
|
|
authctxt->user, ssh_remote_ipaddr(ssh),
|
|
|
|
ssh_remote_port(ssh));
|
|
|
|
authenticated = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
2019-11-25 00:51:37 +00:00
|
|
|
}
|
2017-06-24 06:34:38 +00:00
|
|
|
auth2_record_key(authctxt, authenticated, key);
|
2002-06-06 20:27:55 +00:00
|
|
|
} else {
|
2021-12-19 22:12:30 +00:00
|
|
|
debug_f("%s test pkalg %s pkblob %s%s%s", method, pkalg, key_s,
|
2020-10-18 11:32:01 +00:00
|
|
|
ca_s == NULL ? "" : " CA ", ca_s == NULL ? "" : ca_s);
|
2017-12-19 00:24:34 +00:00
|
|
|
|
2017-05-30 14:10:53 +00:00
|
|
|
if ((r = sshpkt_get_end(ssh)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_fr(r, "parse packet");
|
2002-06-06 20:27:55 +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;
|
|
|
|
}
|
2002-06-06 20:27:55 +00:00
|
|
|
/* XXX fake reply and always send PK_OK ? */
|
|
|
|
/*
|
|
|
|
* XXX this allows testing whether a user is allowed
|
|
|
|
* to login: if you happen to have a valid pubkey this
|
|
|
|
* message is sent. the message is NEVER sent at all
|
|
|
|
* if a user is not allowed to login. is this an
|
|
|
|
* issue? -markus
|
|
|
|
*/
|
2022-06-15 16:08:25 +00:00
|
|
|
if (PRIVSEP(user_key_allowed(ssh, pw, key, 0, NULL))) {
|
2017-05-30 14:10:53 +00:00
|
|
|
if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK))
|
|
|
|
!= 0 ||
|
|
|
|
(r = sshpkt_put_cstring(ssh, pkalg)) != 0 ||
|
|
|
|
(r = sshpkt_put_string(ssh, pkblob, blen)) != 0 ||
|
2018-07-11 18:55:11 +00:00
|
|
|
(r = sshpkt_send(ssh)) != 0 ||
|
|
|
|
(r = ssh_packet_write_wait(ssh)) != 0)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_fr(r, "send packet");
|
2002-06-06 20:27:55 +00:00
|
|
|
authctxt->postponed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
2018-03-03 03:15:51 +00:00
|
|
|
if (authenticated == 1 && auth_activate_options(ssh, authopts) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
debug_f("key options inconsistent with existing");
|
2018-03-03 03:15:51 +00:00
|
|
|
authenticated = 0;
|
|
|
|
}
|
2020-10-18 11:32:01 +00:00
|
|
|
debug2_f("authenticated %d pkalg %s", authenticated, pkalg);
|
2018-03-03 03:15:51 +00:00
|
|
|
|
2018-08-23 03:01:08 +00:00
|
|
|
sshbuf_free(b);
|
2018-03-03 03:15:51 +00:00
|
|
|
sshauthopt_free(authopts);
|
2017-06-24 06:34:38 +00:00
|
|
|
sshkey_free(key);
|
2021-12-19 22:12:30 +00:00
|
|
|
sshkey_free(hostkey);
|
2017-05-30 14:10:53 +00:00
|
|
|
free(userstyle);
|
2013-06-01 21:31:17 +00:00
|
|
|
free(pkalg);
|
|
|
|
free(pkblob);
|
2017-12-19 00:24:34 +00:00
|
|
|
free(key_s);
|
|
|
|
free(ca_s);
|
2018-07-31 03:10:27 +00:00
|
|
|
free(sig);
|
2019-11-25 00:51:37 +00:00
|
|
|
sshkey_sig_details_free(sig_details);
|
2002-06-06 20:27:55 +00:00
|
|
|
return authenticated;
|
|
|
|
}
|
|
|
|
|
2015-05-21 06:43:30 +00:00
|
|
|
static int
|
2022-05-27 05:01:25 +00:00
|
|
|
match_principals_file(struct passwd *pw, char *file,
|
2018-03-03 03:15:51 +00:00
|
|
|
struct sshkey_cert *cert, struct sshauthopt **authoptsp)
|
2015-05-21 06:43:30 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
int success;
|
|
|
|
|
2018-03-03 03:15:51 +00:00
|
|
|
if (authoptsp != NULL)
|
|
|
|
*authoptsp = NULL;
|
|
|
|
|
2015-05-21 06:43:30 +00:00
|
|
|
temporarily_use_uid(pw);
|
|
|
|
debug("trying authorized principals file %s", file);
|
|
|
|
if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
|
|
|
|
restore_uid();
|
|
|
|
return 0;
|
|
|
|
}
|
2022-05-27 05:02:46 +00:00
|
|
|
success = auth_process_principals(f, file, cert, authoptsp);
|
2010-05-10 01:58:03 +00:00
|
|
|
fclose(f);
|
|
|
|
restore_uid();
|
2015-05-21 06:43:30 +00:00
|
|
|
return success;
|
2012-10-30 21:58:58 +00:00
|
|
|
}
|
2010-05-10 01:58:03 +00:00
|
|
|
|
2015-05-21 06:43:30 +00:00
|
|
|
/*
|
|
|
|
* Checks whether principal is allowed in output of command.
|
|
|
|
* returns 1 if the principal is allowed or 0 otherwise.
|
|
|
|
*/
|
|
|
|
static int
|
2023-07-27 22:25:17 +00:00
|
|
|
match_principals_command(struct passwd *user_pw, const struct sshkey *key,
|
|
|
|
const char *conn_id, const char *rdomain, struct sshauthopt **authoptsp)
|
2015-05-21 06:43:30 +00:00
|
|
|
{
|
2018-03-03 03:15:51 +00:00
|
|
|
struct passwd *runas_pw = NULL;
|
2016-09-14 05:42:25 +00:00
|
|
|
const struct sshkey_cert *cert = key->cert;
|
2015-05-21 06:43:30 +00:00
|
|
|
FILE *f = NULL;
|
2016-09-14 05:42:25 +00:00
|
|
|
int r, ok, found_principal = 0;
|
2015-05-21 06:43:30 +00:00
|
|
|
int i, ac = 0, uid_swapped = 0;
|
|
|
|
pid_t pid;
|
|
|
|
char *tmp, *username = NULL, *command = NULL, **av = NULL;
|
2016-09-14 05:42:25 +00:00
|
|
|
char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL;
|
2019-05-20 00:25:55 +00:00
|
|
|
char serial_s[32], uidstr[32];
|
2015-05-21 06:43:30 +00:00
|
|
|
void (*osigchld)(int);
|
|
|
|
|
2018-03-03 03:15:51 +00:00
|
|
|
if (authoptsp != NULL)
|
|
|
|
*authoptsp = NULL;
|
2015-05-21 06:43:30 +00:00
|
|
|
if (options.authorized_principals_command == NULL)
|
|
|
|
return 0;
|
|
|
|
if (options.authorized_principals_command_user == NULL) {
|
|
|
|
error("No user for AuthorizedPrincipalsCommand specified, "
|
|
|
|
"skipping");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NB. all returns later this function should go via "out" to
|
|
|
|
* ensure the original SIGCHLD handler is restored properly.
|
|
|
|
*/
|
2020-01-23 07:10:22 +00:00
|
|
|
osigchld = ssh_signal(SIGCHLD, SIG_DFL);
|
2015-05-21 06:43:30 +00:00
|
|
|
|
|
|
|
/* Prepare and verify the user for the command */
|
|
|
|
username = percent_expand(options.authorized_principals_command_user,
|
|
|
|
"u", user_pw->pw_name, (char *)NULL);
|
2018-03-03 03:15:51 +00:00
|
|
|
runas_pw = getpwnam(username);
|
|
|
|
if (runas_pw == NULL) {
|
2015-05-21 06:43:30 +00:00
|
|
|
error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
|
|
|
|
username, strerror(errno));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Turn the command into an argument vector */
|
2021-06-08 06:54:40 +00:00
|
|
|
if (argv_split(options.authorized_principals_command,
|
|
|
|
&ac, &av, 0) != 0) {
|
2015-05-21 06:43:30 +00:00
|
|
|
error("AuthorizedPrincipalsCommand \"%s\" contains "
|
2019-06-21 03:19:59 +00:00
|
|
|
"invalid quotes", options.authorized_principals_command);
|
2015-05-21 06:43:30 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (ac == 0) {
|
|
|
|
error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
|
2019-06-21 03:19:59 +00:00
|
|
|
options.authorized_principals_command);
|
2015-05-21 06:43:30 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2016-09-14 05:42:25 +00:00
|
|
|
if ((ca_fp = sshkey_fingerprint(cert->signature_key,
|
|
|
|
options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("sshkey_fingerprint failed");
|
2016-09-14 05:42:25 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2016-09-14 20:11:26 +00:00
|
|
|
if ((key_fp = sshkey_fingerprint(key,
|
2016-09-14 05:42:25 +00:00
|
|
|
options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("sshkey_fingerprint failed");
|
2016-09-14 05:42:25 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "sshkey_to_base64 failed");
|
2016-09-14 05:42:25 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshkey_to_base64(key, &keytext)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "sshkey_to_base64 failed");
|
2016-09-14 05:42:25 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2016-09-21 17:44:20 +00:00
|
|
|
snprintf(serial_s, sizeof(serial_s), "%llu",
|
|
|
|
(unsigned long long)cert->serial);
|
2018-06-01 03:33:53 +00:00
|
|
|
snprintf(uidstr, sizeof(uidstr), "%llu",
|
|
|
|
(unsigned long long)user_pw->pw_uid);
|
2015-05-21 06:43:30 +00:00
|
|
|
for (i = 1; i < ac; i++) {
|
|
|
|
tmp = percent_expand(av[i],
|
2023-07-27 22:25:17 +00:00
|
|
|
"C", conn_id,
|
|
|
|
"D", rdomain,
|
2018-06-01 03:33:53 +00:00
|
|
|
"U", uidstr,
|
2015-05-21 06:43:30 +00:00
|
|
|
"u", user_pw->pw_name,
|
|
|
|
"h", user_pw->pw_dir,
|
2016-09-14 05:42:25 +00:00
|
|
|
"t", sshkey_ssh_name(key),
|
|
|
|
"T", sshkey_ssh_name(cert->signature_key),
|
|
|
|
"f", key_fp,
|
|
|
|
"F", ca_fp,
|
|
|
|
"k", keytext,
|
|
|
|
"K", catext,
|
2016-09-21 01:34:45 +00:00
|
|
|
"i", cert->key_id,
|
|
|
|
"s", serial_s,
|
2015-05-21 06:43:30 +00:00
|
|
|
(char *)NULL);
|
|
|
|
if (tmp == NULL)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_f("percent_expand failed");
|
2015-05-21 06:43:30 +00:00
|
|
|
free(av[i]);
|
|
|
|
av[i] = tmp;
|
|
|
|
}
|
|
|
|
/* Prepare a printable command for logs, etc. */
|
2017-08-18 05:36:45 +00:00
|
|
|
command = argv_assemble(ac, av);
|
2015-05-21 06:43:30 +00:00
|
|
|
|
2020-12-22 00:12:22 +00:00
|
|
|
if ((pid = subprocess("AuthorizedPrincipalsCommand", command,
|
2017-08-18 05:36:45 +00:00
|
|
|
ac, av, &f,
|
2020-12-22 00:12:22 +00:00
|
|
|
SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD,
|
|
|
|
runas_pw, temporarily_use_uid, restore_uid)) == 0)
|
2015-05-21 06:43:30 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
uid_swapped = 1;
|
2018-03-03 03:15:51 +00:00
|
|
|
temporarily_use_uid(runas_pw);
|
2015-05-21 06:43:30 +00:00
|
|
|
|
2022-05-27 05:02:46 +00:00
|
|
|
ok = auth_process_principals(f, "(command)", cert, authoptsp);
|
2015-05-21 06:43:30 +00:00
|
|
|
|
2016-12-30 22:08:02 +00:00
|
|
|
fclose(f);
|
|
|
|
f = NULL;
|
|
|
|
|
2017-08-18 05:48:04 +00:00
|
|
|
if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0)
|
2015-05-21 06:43:30 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Read completed successfully */
|
|
|
|
found_principal = ok;
|
|
|
|
out:
|
|
|
|
if (f != NULL)
|
|
|
|
fclose(f);
|
2020-01-23 07:10:22 +00:00
|
|
|
ssh_signal(SIGCHLD, osigchld);
|
2015-05-21 06:43:30 +00:00
|
|
|
for (i = 0; i < ac; i++)
|
|
|
|
free(av[i]);
|
|
|
|
free(av);
|
|
|
|
if (uid_swapped)
|
|
|
|
restore_uid();
|
|
|
|
free(command);
|
|
|
|
free(username);
|
2016-09-14 05:42:25 +00:00
|
|
|
free(ca_fp);
|
|
|
|
free(key_fp);
|
|
|
|
free(catext);
|
|
|
|
free(keytext);
|
2015-05-21 06:43:30 +00:00
|
|
|
return found_principal;
|
|
|
|
}
|
2018-03-03 03:15:51 +00:00
|
|
|
|
2010-03-04 10:53:35 +00:00
|
|
|
/* Authenticate a certificate key against TrustedUserCAKeys */
|
|
|
|
static int
|
2022-05-27 05:01:25 +00:00
|
|
|
user_cert_trusted_ca(struct passwd *pw, struct sshkey *key,
|
|
|
|
const char *remote_ip, const char *remote_host,
|
2023-07-27 22:25:17 +00:00
|
|
|
const char *conn_id, const char *rdomain, struct sshauthopt **authoptsp)
|
2010-03-04 10:53:35 +00:00
|
|
|
{
|
2010-05-10 01:58:03 +00:00
|
|
|
char *ca_fp, *principals_file = NULL;
|
2010-03-04 10:53:35 +00:00
|
|
|
const char *reason;
|
2018-03-03 03:15:51 +00:00
|
|
|
struct sshauthopt *principals_opts = NULL, *cert_opts = NULL;
|
|
|
|
struct sshauthopt *final_opts = NULL;
|
2017-05-30 14:10:53 +00:00
|
|
|
int r, ret = 0, found_principal = 0, use_authorized_principals;
|
2010-03-04 10:53:35 +00:00
|
|
|
|
2018-03-03 03:15:51 +00:00
|
|
|
if (authoptsp != NULL)
|
|
|
|
*authoptsp = NULL;
|
|
|
|
|
2017-05-30 14:10:53 +00:00
|
|
|
if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL)
|
2010-03-04 10:53:35 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-01-28 22:36:00 +00:00
|
|
|
if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
|
|
|
|
options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
|
|
|
|
return 0;
|
2010-03-04 10:53:35 +00:00
|
|
|
|
2017-05-30 14:10:53 +00:00
|
|
|
if ((r = sshkey_in_file(key->cert->signature_key,
|
|
|
|
options.trusted_user_ca_keys, 1, 0)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
debug2_fr(r, "CA %s %s is not listed in %s",
|
2017-05-30 14:10:53 +00:00
|
|
|
sshkey_type(key->cert->signature_key), ca_fp,
|
2020-10-18 11:32:01 +00:00
|
|
|
options.trusted_user_ca_keys);
|
2010-03-04 10:53:35 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2010-05-10 01:58:03 +00:00
|
|
|
/*
|
|
|
|
* If AuthorizedPrincipals is in use, then compare the certificate
|
|
|
|
* principals against the names in that file rather than matching
|
|
|
|
* against the username.
|
|
|
|
*/
|
|
|
|
if ((principals_file = authorized_principals_file(pw)) != NULL) {
|
2022-05-27 05:01:25 +00:00
|
|
|
if (match_principals_file(pw, principals_file,
|
2018-03-03 03:15:51 +00:00
|
|
|
key->cert, &principals_opts))
|
2015-05-21 06:43:30 +00:00
|
|
|
found_principal = 1;
|
|
|
|
}
|
|
|
|
/* Try querying command if specified */
|
2022-05-27 05:01:25 +00:00
|
|
|
if (!found_principal && match_principals_command(pw, key,
|
2023-07-27 22:25:17 +00:00
|
|
|
conn_id, rdomain, &principals_opts))
|
2015-05-21 06:43:30 +00:00
|
|
|
found_principal = 1;
|
2015-06-15 18:44:22 +00:00
|
|
|
/* If principals file or command is specified, then require a match */
|
|
|
|
use_authorized_principals = principals_file != NULL ||
|
2021-04-03 06:18:40 +00:00
|
|
|
options.authorized_principals_command != NULL;
|
2015-06-15 18:44:22 +00:00
|
|
|
if (!found_principal && use_authorized_principals) {
|
2015-05-21 06:43:30 +00:00
|
|
|
reason = "Certificate does not contain an authorized principal";
|
2018-03-03 03:15:51 +00:00
|
|
|
goto fail_reason;
|
2010-03-04 10:53:35 +00:00
|
|
|
}
|
2018-03-03 03:15:51 +00:00
|
|
|
if (use_authorized_principals && principals_opts == NULL)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_f("internal error: missing principals_opts");
|
2021-07-23 03:37:52 +00:00
|
|
|
if (sshkey_cert_check_authority_now(key, 0, 1, 0,
|
2015-06-15 18:44:22 +00:00
|
|
|
use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
|
2010-05-10 01:58:03 +00:00
|
|
|
goto fail_reason;
|
2018-03-03 03:15:51 +00:00
|
|
|
|
|
|
|
/* Check authority from options in key and from principals file/cmd */
|
|
|
|
if ((cert_opts = sshauthopt_from_cert(key)) == NULL) {
|
|
|
|
reason = "Invalid certificate options";
|
|
|
|
goto fail_reason;
|
|
|
|
}
|
2022-05-27 05:01:25 +00:00
|
|
|
if (auth_authorise_keyopts(pw, cert_opts, 0,
|
|
|
|
remote_ip, remote_host, "cert") != 0) {
|
2018-03-03 03:15:51 +00:00
|
|
|
reason = "Refused by certificate options";
|
2016-11-30 02:57:40 +00:00
|
|
|
goto fail_reason;
|
2018-03-03 03:15:51 +00:00
|
|
|
}
|
|
|
|
if (principals_opts == NULL) {
|
|
|
|
final_opts = cert_opts;
|
|
|
|
cert_opts = NULL;
|
|
|
|
} else {
|
2022-05-27 05:01:25 +00:00
|
|
|
if (auth_authorise_keyopts(pw, principals_opts, 0,
|
|
|
|
remote_ip, remote_host, "principals") != 0) {
|
2018-03-03 03:15:51 +00:00
|
|
|
reason = "Refused by certificate principals options";
|
|
|
|
goto fail_reason;
|
|
|
|
}
|
|
|
|
if ((final_opts = sshauthopt_merge(principals_opts,
|
|
|
|
cert_opts, &reason)) == NULL) {
|
|
|
|
fail_reason:
|
|
|
|
error("%s", reason);
|
|
|
|
auth_debug_add("%s", reason);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
2010-03-04 10:53:35 +00:00
|
|
|
|
2018-03-03 03:15:51 +00:00
|
|
|
/* Success */
|
2015-10-27 01:44:45 +00:00
|
|
|
verbose("Accepted certificate ID \"%s\" (serial %llu) signed by "
|
|
|
|
"%s CA %s via %s", key->cert->key_id,
|
|
|
|
(unsigned long long)key->cert->serial,
|
2017-05-30 14:10:53 +00:00
|
|
|
sshkey_type(key->cert->signature_key), ca_fp,
|
2010-03-21 18:51:21 +00:00
|
|
|
options.trusted_user_ca_keys);
|
2018-03-03 03:15:51 +00:00
|
|
|
if (authoptsp != NULL) {
|
|
|
|
*authoptsp = final_opts;
|
|
|
|
final_opts = NULL;
|
|
|
|
}
|
2010-03-04 10:53:35 +00:00
|
|
|
ret = 1;
|
|
|
|
out:
|
2018-03-03 03:15:51 +00:00
|
|
|
sshauthopt_free(principals_opts);
|
|
|
|
sshauthopt_free(cert_opts);
|
|
|
|
sshauthopt_free(final_opts);
|
2013-06-01 21:31:17 +00:00
|
|
|
free(principals_file);
|
|
|
|
free(ca_fp);
|
2010-03-04 10:53:35 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-30 21:58:58 +00:00
|
|
|
/*
|
|
|
|
* Checks whether key is allowed in file.
|
|
|
|
* returns 1 if the key is allowed or 0 otherwise.
|
|
|
|
*/
|
|
|
|
static int
|
2022-05-27 05:01:25 +00:00
|
|
|
user_key_allowed2(struct passwd *pw, struct sshkey *key,
|
|
|
|
char *file, const char *remote_ip, const char *remote_host,
|
|
|
|
struct sshauthopt **authoptsp)
|
2012-10-30 21:58:58 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
int found_key = 0;
|
|
|
|
|
2018-03-03 03:15:51 +00:00
|
|
|
if (authoptsp != NULL)
|
|
|
|
*authoptsp = NULL;
|
|
|
|
|
2012-10-30 21:58:58 +00:00
|
|
|
/* Temporarily use the user's uid. */
|
|
|
|
temporarily_use_uid(pw);
|
|
|
|
|
|
|
|
debug("trying public key file %s", file);
|
|
|
|
if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
|
2022-05-27 05:02:46 +00:00
|
|
|
found_key = auth_check_authkeys_file(pw, f, file,
|
2022-05-27 05:01:25 +00:00
|
|
|
key, remote_ip, remote_host, authoptsp);
|
2012-10-30 21:58:58 +00:00
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
restore_uid();
|
|
|
|
return found_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks whether key is allowed in output of command.
|
|
|
|
* returns 1 if the key is allowed or 0 otherwise.
|
|
|
|
*/
|
|
|
|
static int
|
2022-05-27 05:01:25 +00:00
|
|
|
user_key_command_allowed2(struct passwd *user_pw, struct sshkey *key,
|
|
|
|
const char *remote_ip, const char *remote_host,
|
2023-07-27 22:25:17 +00:00
|
|
|
const char *conn_id, const char *rdomain, struct sshauthopt **authoptsp)
|
2012-10-30 21:58:58 +00:00
|
|
|
{
|
2018-03-03 03:15:51 +00:00
|
|
|
struct passwd *runas_pw = NULL;
|
2015-05-21 06:38:35 +00:00
|
|
|
FILE *f = NULL;
|
|
|
|
int r, ok, found_key = 0;
|
|
|
|
int i, uid_swapped = 0, ac = 0;
|
2012-10-30 21:58:58 +00:00
|
|
|
pid_t pid;
|
2015-05-21 06:38:35 +00:00
|
|
|
char *username = NULL, *key_fp = NULL, *keytext = NULL;
|
2018-06-01 03:33:53 +00:00
|
|
|
char uidstr[32], *tmp, *command = NULL, **av = NULL;
|
2015-05-21 06:38:35 +00:00
|
|
|
void (*osigchld)(int);
|
2012-10-30 21:58:58 +00:00
|
|
|
|
2018-03-03 03:15:51 +00:00
|
|
|
if (authoptsp != NULL)
|
|
|
|
*authoptsp = NULL;
|
2015-05-21 06:38:35 +00:00
|
|
|
if (options.authorized_keys_command == NULL)
|
2012-10-30 21:58:58 +00:00
|
|
|
return 0;
|
2012-11-04 11:23:14 +00:00
|
|
|
if (options.authorized_keys_command_user == NULL) {
|
|
|
|
error("No user for AuthorizedKeysCommand specified, skipping");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-21 06:38:35 +00:00
|
|
|
/*
|
|
|
|
* NB. all returns later this function should go via "out" to
|
|
|
|
* ensure the original SIGCHLD handler is restored properly.
|
|
|
|
*/
|
2020-01-23 07:10:22 +00:00
|
|
|
osigchld = ssh_signal(SIGCHLD, SIG_DFL);
|
2015-05-21 06:38:35 +00:00
|
|
|
|
|
|
|
/* Prepare and verify the user for the command */
|
2012-11-04 11:23:14 +00:00
|
|
|
username = percent_expand(options.authorized_keys_command_user,
|
|
|
|
"u", user_pw->pw_name, (char *)NULL);
|
2018-03-03 03:15:51 +00:00
|
|
|
runas_pw = getpwnam(username);
|
|
|
|
if (runas_pw == NULL) {
|
2013-02-14 23:28:55 +00:00
|
|
|
error("AuthorizedKeysCommandUser \"%s\" not found: %s",
|
|
|
|
username, strerror(errno));
|
2015-05-21 06:38:35 +00:00
|
|
|
goto out;
|
2012-10-30 21:58:58 +00:00
|
|
|
}
|
|
|
|
|
2015-05-21 06:38:35 +00:00
|
|
|
/* Prepare AuthorizedKeysCommand */
|
|
|
|
if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
|
|
|
|
SSH_FP_DEFAULT)) == NULL) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("sshkey_fingerprint failed");
|
2012-10-30 21:58:58 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2015-05-21 06:38:35 +00:00
|
|
|
if ((r = sshkey_to_base64(key, &keytext)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "sshkey_to_base64 failed");
|
2012-10-30 21:58:58 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-05-21 06:38:35 +00:00
|
|
|
/* Turn the command into an argument vector */
|
2021-06-08 06:54:40 +00:00
|
|
|
if (argv_split(options.authorized_keys_command, &ac, &av, 0) != 0) {
|
2015-05-21 06:38:35 +00:00
|
|
|
error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
|
2020-12-17 23:28:50 +00:00
|
|
|
options.authorized_keys_command);
|
2012-10-30 21:58:58 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2015-05-21 06:38:35 +00:00
|
|
|
if (ac == 0) {
|
|
|
|
error("AuthorizedKeysCommand \"%s\" yielded no arguments",
|
2020-12-17 23:28:50 +00:00
|
|
|
options.authorized_keys_command);
|
2015-05-21 06:38:35 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2018-06-01 03:33:53 +00:00
|
|
|
snprintf(uidstr, sizeof(uidstr), "%llu",
|
|
|
|
(unsigned long long)user_pw->pw_uid);
|
2015-05-21 06:38:35 +00:00
|
|
|
for (i = 1; i < ac; i++) {
|
|
|
|
tmp = percent_expand(av[i],
|
2023-07-27 22:25:17 +00:00
|
|
|
"C", conn_id,
|
|
|
|
"D", rdomain,
|
2018-06-01 03:33:53 +00:00
|
|
|
"U", uidstr,
|
2015-05-21 06:38:35 +00:00
|
|
|
"u", user_pw->pw_name,
|
|
|
|
"h", user_pw->pw_dir,
|
|
|
|
"t", sshkey_ssh_name(key),
|
|
|
|
"f", key_fp,
|
|
|
|
"k", keytext,
|
|
|
|
(char *)NULL);
|
|
|
|
if (tmp == NULL)
|
2020-10-18 11:32:01 +00:00
|
|
|
fatal_f("percent_expand failed");
|
2015-05-21 06:38:35 +00:00
|
|
|
free(av[i]);
|
|
|
|
av[i] = tmp;
|
|
|
|
}
|
|
|
|
/* Prepare a printable command for logs, etc. */
|
2017-08-18 05:36:45 +00:00
|
|
|
command = argv_assemble(ac, av);
|
2012-10-30 21:58:58 +00:00
|
|
|
|
|
|
|
/*
|
2015-05-21 06:38:35 +00:00
|
|
|
* If AuthorizedKeysCommand was run without arguments
|
|
|
|
* then fall back to the old behaviour of passing the
|
|
|
|
* target username as a single argument.
|
2012-10-30 21:58:58 +00:00
|
|
|
*/
|
2015-05-21 06:38:35 +00:00
|
|
|
if (ac == 1) {
|
|
|
|
av = xreallocarray(av, ac + 2, sizeof(*av));
|
|
|
|
av[1] = xstrdup(user_pw->pw_name);
|
|
|
|
av[2] = NULL;
|
|
|
|
/* Fix up command too, since it is used in log messages */
|
|
|
|
free(command);
|
|
|
|
xasprintf(&command, "%s %s", av[0], av[1]);
|
2012-10-30 21:58:58 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 00:12:22 +00:00
|
|
|
if ((pid = subprocess("AuthorizedKeysCommand", command,
|
2017-08-18 05:36:45 +00:00
|
|
|
ac, av, &f,
|
2020-12-22 00:12:22 +00:00
|
|
|
SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD,
|
|
|
|
runas_pw, temporarily_use_uid, restore_uid)) == 0)
|
2015-05-21 06:38:35 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
uid_swapped = 1;
|
2018-03-03 03:15:51 +00:00
|
|
|
temporarily_use_uid(runas_pw);
|
2012-10-30 21:58:58 +00:00
|
|
|
|
2022-05-27 05:02:46 +00:00
|
|
|
ok = auth_check_authkeys_file(user_pw, f,
|
2022-05-27 05:01:25 +00:00
|
|
|
options.authorized_keys_command, key, remote_ip,
|
|
|
|
remote_host, authoptsp);
|
2012-10-30 21:58:58 +00:00
|
|
|
|
2016-12-30 22:08:02 +00:00
|
|
|
fclose(f);
|
|
|
|
f = NULL;
|
|
|
|
|
2017-08-18 05:48:04 +00:00
|
|
|
if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0)
|
2012-10-30 21:58:58 +00:00
|
|
|
goto out;
|
2015-05-21 06:38:35 +00:00
|
|
|
|
|
|
|
/* Read completed successfully */
|
2012-10-30 21:58:58 +00:00
|
|
|
found_key = ok;
|
|
|
|
out:
|
2015-05-21 06:38:35 +00:00
|
|
|
if (f != NULL)
|
|
|
|
fclose(f);
|
2020-01-23 07:10:22 +00:00
|
|
|
ssh_signal(SIGCHLD, osigchld);
|
2015-05-21 06:38:35 +00:00
|
|
|
for (i = 0; i < ac; i++)
|
|
|
|
free(av[i]);
|
|
|
|
free(av);
|
|
|
|
if (uid_swapped)
|
|
|
|
restore_uid();
|
|
|
|
free(command);
|
|
|
|
free(username);
|
|
|
|
free(key_fp);
|
|
|
|
free(keytext);
|
2012-10-30 21:58:58 +00:00
|
|
|
return found_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether key authenticates and authorises the user.
|
|
|
|
*/
|
2002-06-06 20:27:55 +00:00
|
|
|
int
|
2022-06-15 16:08:25 +00:00
|
|
|
user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
|
|
|
|
int auth_attempt, struct sshauthopt **authoptsp)
|
2002-06-06 20:27:55 +00:00
|
|
|
{
|
2019-06-14 03:39:59 +00:00
|
|
|
u_int success = 0, i;
|
2023-07-27 22:25:17 +00:00
|
|
|
char *file, *conn_id;
|
2018-03-03 03:15:51 +00:00
|
|
|
struct sshauthopt *opts = NULL;
|
2023-07-27 22:25:17 +00:00
|
|
|
const char *rdomain, *remote_ip, *remote_host;
|
2019-06-14 03:39:59 +00:00
|
|
|
|
2018-03-03 03:15:51 +00:00
|
|
|
if (authoptsp != NULL)
|
|
|
|
*authoptsp = NULL;
|
2002-06-06 20:27:55 +00:00
|
|
|
|
2010-03-04 10:53:35 +00:00
|
|
|
if (auth_key_is_revoked(key))
|
|
|
|
return 0;
|
2017-05-30 14:10:53 +00:00
|
|
|
if (sshkey_is_cert(key) &&
|
|
|
|
auth_key_is_revoked(key->cert->signature_key))
|
2010-03-04 10:53:35 +00:00
|
|
|
return 0;
|
|
|
|
|
2023-07-27 22:25:17 +00:00
|
|
|
if ((rdomain = ssh_packet_rdomain_in(ssh)) == NULL)
|
|
|
|
rdomain = "";
|
|
|
|
remote_ip = ssh_remote_ipaddr(ssh);
|
|
|
|
remote_host = auth_get_canonical_hostname(ssh, options.use_dns);
|
|
|
|
xasprintf(&conn_id, "%s %d %s %d",
|
|
|
|
ssh_local_ipaddr(ssh), ssh_local_port(ssh),
|
|
|
|
remote_ip, ssh_remote_port(ssh));
|
|
|
|
|
2019-06-14 03:39:59 +00:00
|
|
|
for (i = 0; !success && i < options.num_authkeys_files; i++) {
|
|
|
|
if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
|
|
|
|
continue;
|
|
|
|
file = expand_authorized_keys(
|
|
|
|
options.authorized_keys_files[i], pw);
|
2022-05-27 05:01:25 +00:00
|
|
|
success = user_key_allowed2(pw, key, file,
|
|
|
|
remote_ip, remote_host, &opts);
|
2019-06-14 03:39:59 +00:00
|
|
|
free(file);
|
|
|
|
if (!success) {
|
|
|
|
sshauthopt_free(opts);
|
|
|
|
opts = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (success)
|
|
|
|
goto out;
|
|
|
|
|
2022-05-27 05:01:25 +00:00
|
|
|
if ((success = user_cert_trusted_ca(pw, key, remote_ip, remote_host,
|
2023-07-27 22:25:17 +00:00
|
|
|
conn_id, rdomain, &opts)) != 0)
|
2018-03-03 03:15:51 +00:00
|
|
|
goto out;
|
|
|
|
sshauthopt_free(opts);
|
|
|
|
opts = NULL;
|
2010-03-04 10:53:35 +00:00
|
|
|
|
2022-05-27 05:01:25 +00:00
|
|
|
if ((success = user_key_command_allowed2(pw, key, remote_ip,
|
2023-07-27 22:25:17 +00:00
|
|
|
remote_host, conn_id, rdomain, &opts)) != 0)
|
2018-03-03 03:15:51 +00:00
|
|
|
goto out;
|
|
|
|
sshauthopt_free(opts);
|
|
|
|
opts = NULL;
|
2012-10-30 21:58:58 +00:00
|
|
|
|
2018-03-03 03:15:51 +00:00
|
|
|
out:
|
2023-07-27 22:25:17 +00:00
|
|
|
free(conn_id);
|
2018-03-03 03:15:51 +00:00
|
|
|
if (success && authoptsp != NULL) {
|
|
|
|
*authoptsp = opts;
|
|
|
|
opts = NULL;
|
|
|
|
}
|
|
|
|
sshauthopt_free(opts);
|
2002-06-06 20:27:55 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
Authmethod method_pubkey = {
|
|
|
|
"publickey",
|
2021-12-19 22:12:30 +00:00
|
|
|
"publickey-hostbound-v00@openssh.com",
|
2002-06-06 20:27:55 +00:00
|
|
|
userauth_pubkey,
|
|
|
|
&options.pubkey_authentication
|
|
|
|
};
|