1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-03 01:18:10 +03:00

s4:kdc: Adapt to hdb_entry_ex removal

Rather than having a 'free_entry' member that can be called to free an
hdb_entry, we now implement the free function in HDB. We perform the
free only if the context pointer is non-NULL.

We also remove the ZERO_STRUCTP() in sdb_entry_to_hdb_entry(), as the
context pointer is now part of the 'hdb_entry' structure itself, and
this would undesirably zero it out.

This is an adaptation to Heimdal commits:

commit c5551775e204d00c7ee8055ab6ddbba7e0590584
Author: Luke Howard <lukeh@padl.com>
Date:   Fri Jan 7 12:15:55 2022 +1100

    hdb: decorate HDB_entry with context member

    Decorate HDB_entry with context and move free_entry callback into HDB structure
    itself. Requires updating hdb_free_entry() signature to include HDB parameter.
    A follow-up commit will consolidate hdb_entry_ex (which has a single hdb_entry
    member) into hdb_entry.

commit 0e8c4ccc6ee0123ea39e53e8917fc3f6bb74e8c8
Author: Luke Howard <lukeh@padl.com>
Date:   Fri Jan 7 12:54:40 2022 +1100

    hdb: eliminate hdb_entry_ex

    Remove hdb_entry_ex and revert to the original design of hdb_entry (except with
    an additional context member in hdb_entry which is managed by the free_entry
    method in HDB).

NOTE: THIS COMMIT WON'T COMPILE/WORK ON ITS OWN!

BUG: https://bugzilla.samba.org/show_bug.cgi?id=14995

Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
This commit is contained in:
Joseph Sutton 2022-02-22 19:41:14 +13:00 committed by Joseph Sutton
parent 068f2bf117
commit 94d387abd5
7 changed files with 77 additions and 75 deletions

View File

@ -79,7 +79,7 @@ static krb5_error_code hdb_samba4_rename(krb5_context context, HDB *db, const ch
return HDB_ERR_DB_INUSE;
}
static krb5_error_code hdb_samba4_store(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
static krb5_error_code hdb_samba4_store(krb5_context context, HDB *db, unsigned flags, hdb_entry *entry)
{
return HDB_ERR_DB_INUSE;
}
@ -93,6 +93,24 @@ static krb5_error_code hdb_samba4_set_sync(krb5_context context, struct HDB *db,
return 0;
}
static void hdb_samba4_free_entry_context(krb5_context context, struct HDB *db, hdb_entry *entry)
{
/*
* This function is now called for every HDB entry, not just those with
* 'context' set, so we have to check that the context is not NULL.
*/
if (entry->context != NULL) {
/* this function is called only from hdb_free_entry().
* Make sure we neutralize the destructor or we will
* get a double free later when hdb_free_entry() will
* try to call free_hdb_entry() */
talloc_set_destructor(entry->context, NULL);
/* now proceed to free the talloc part */
talloc_free(entry->context);
}
}
static int hdb_samba4_fill_fast_cookie(krb5_context context,
struct samba_kdc_db_context *kdc_db_ctx)
{
@ -131,7 +149,7 @@ static int hdb_samba4_fill_fast_cookie(krb5_context context,
static krb5_error_code hdb_samba4_fetch_fast_cookie(krb5_context context,
struct samba_kdc_db_context *kdc_db_ctx,
hdb_entry_ex *entry_ex)
hdb_entry *entry_ex)
{
krb5_error_code ret = SDB_ERR_NOENTRY;
TALLOC_CTX *mem_ctx;
@ -219,7 +237,7 @@ static krb5_error_code hdb_samba4_fetch_kvno(krb5_context context, HDB *db,
krb5_const_principal principal,
unsigned flags,
krb5_kvno kvno,
hdb_entry_ex *entry_ex)
hdb_entry *entry_ex)
{
struct samba_kdc_db_context *kdc_db_ctx;
struct sdb_entry_ex sdb_entry_ex = {};
@ -273,7 +291,7 @@ static krb5_error_code hdb_samba4_fetch_kvno(krb5_context context, HDB *db,
}
static krb5_error_code hdb_samba4_firstkey(krb5_context context, HDB *db, unsigned flags,
hdb_entry_ex *entry)
hdb_entry *entry)
{
struct samba_kdc_db_context *kdc_db_ctx;
struct sdb_entry_ex sdb_entry_ex = {};
@ -302,7 +320,7 @@ static krb5_error_code hdb_samba4_firstkey(krb5_context context, HDB *db, unsign
}
static krb5_error_code hdb_samba4_nextkey(krb5_context context, HDB *db, unsigned flags,
hdb_entry_ex *entry)
hdb_entry *entry)
{
struct samba_kdc_db_context *kdc_db_ctx;
struct sdb_entry_ex sdb_entry_ex = {};
@ -338,7 +356,7 @@ static krb5_error_code hdb_samba4_destroy(krb5_context context, HDB *db)
static krb5_error_code
hdb_samba4_check_constrained_delegation(krb5_context context, HDB *db,
hdb_entry_ex *entry,
hdb_entry *entry,
krb5_const_principal target_principal)
{
struct samba_kdc_db_context *kdc_db_ctx;
@ -347,7 +365,7 @@ hdb_samba4_check_constrained_delegation(krb5_context context, HDB *db,
kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
struct samba_kdc_db_context);
skdc_entry = talloc_get_type_abort(entry->ctx,
skdc_entry = talloc_get_type_abort(entry->context,
struct samba_kdc_entry);
ret = samba_kdc_check_s4u2proxy(context, kdc_db_ctx,
@ -374,7 +392,7 @@ hdb_samba4_check_constrained_delegation(krb5_context context, HDB *db,
static krb5_error_code
hdb_samba4_check_pkinit_ms_upn_match(krb5_context context, HDB *db,
hdb_entry_ex *entry,
hdb_entry *entry,
krb5_const_principal certificate_principal)
{
struct samba_kdc_db_context *kdc_db_ctx;
@ -383,7 +401,7 @@ hdb_samba4_check_pkinit_ms_upn_match(krb5_context context, HDB *db,
kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
struct samba_kdc_db_context);
skdc_entry = talloc_get_type_abort(entry->ctx,
skdc_entry = talloc_get_type_abort(entry->context,
struct samba_kdc_entry);
ret = samba_kdc_check_pkinit_ms_upn_match(context, kdc_db_ctx,
@ -410,14 +428,14 @@ hdb_samba4_check_pkinit_ms_upn_match(krb5_context context, HDB *db,
static krb5_error_code
hdb_samba4_check_client_matches_target_service(krb5_context context, HDB *db,
hdb_entry_ex *client_entry,
hdb_entry_ex *server_target_entry)
hdb_entry *client_entry,
hdb_entry *server_target_entry)
{
struct samba_kdc_entry *skdc_client_entry
= talloc_get_type_abort(client_entry->ctx,
= talloc_get_type_abort(client_entry->context,
struct samba_kdc_entry);
struct samba_kdc_entry *skdc_server_target_entry
= talloc_get_type_abort(server_target_entry->ctx,
= talloc_get_type_abort(server_target_entry->context,
struct samba_kdc_entry);
return samba_kdc_check_client_matches_target_service(context,
@ -503,7 +521,7 @@ static void send_bad_password_netlogon(TALLOC_CTX *mem_ctx,
static krb5_error_code hdb_samba4_audit(krb5_context context,
HDB *db,
hdb_entry_ex *entry,
hdb_entry *entry,
hdb_request_t r)
{
struct samba_kdc_db_context *kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
@ -595,7 +613,7 @@ static krb5_error_code hdb_samba4_audit(krb5_context context,
case HDB_AUTH_EVENT_CLIENT_AUTHORIZED:
{
TALLOC_CTX *frame = talloc_stackframe();
struct samba_kdc_entry *p = talloc_get_type(entry->ctx,
struct samba_kdc_entry *p = talloc_get_type(entry->context,
struct samba_kdc_entry);
struct netr_SendToSamBase *send_to_sam = NULL;
@ -619,7 +637,7 @@ static krb5_error_code hdb_samba4_audit(krb5_context context,
case HDB_AUTH_EVENT_PREAUTH_FAILED:
{
TALLOC_CTX *frame = talloc_stackframe();
struct samba_kdc_entry *p = talloc_get_type(entry->ctx,
struct samba_kdc_entry *p = talloc_get_type(entry->context,
struct samba_kdc_entry);
struct dom_sid *sid
= samdb_result_dom_sid(frame, p->msg, "objectSid");
@ -766,6 +784,7 @@ NTSTATUS hdb_samba4_create_kdc(struct samba_kdc_base_context *base_ctx,
(*db)->hdb_dbc = NULL;
(*db)->hdb_open = hdb_samba4_open;
(*db)->hdb_close = hdb_samba4_close;
(*db)->hdb_free_entry_context = hdb_samba4_free_entry_context;
(*db)->hdb_fetch_kvno = hdb_samba4_fetch_kvno;
(*db)->hdb_store = hdb_samba4_store;
(*db)->hdb_firstkey = hdb_samba4_firstkey;

View File

@ -34,7 +34,7 @@
int kdc_check_pac(krb5_context context,
DATA_BLOB srv_sig,
struct PAC_SIGNATURE_DATA *kdc_sig,
struct hdb_entry_ex *ent)
hdb_entry *ent)
{
krb5_enctype etype;
int ret;
@ -52,7 +52,7 @@ int kdc_check_pac(krb5_context context,
}
}
ret = hdb_enctype2key(context, &ent->entry, NULL, etype, &key);
ret = hdb_enctype2key(context, ent, NULL, etype, &key);
if (ret != 0) {
return ret;

View File

@ -50,5 +50,5 @@ NTSTATUS hdb_samba4_create_kdc(struct samba_kdc_base_context *base_ctx,
int kdc_check_pac(krb5_context krb5_context,
DATA_BLOB server_sig,
struct PAC_SIGNATURE_DATA *kdc_sig,
hdb_entry_ex *ent);
hdb_entry *ent);
#endif

View File

@ -191,7 +191,7 @@ static NTSTATUS kdc_check_generic_kerberos(struct irpc_message *msg,
(krb5_kdc_configuration *)kdc->private_data;
enum ndr_err_code ndr_err;
int ret;
hdb_entry_ex ent;
hdb_entry ent;
krb5_principal principal;
@ -235,7 +235,7 @@ static NTSTATUS kdc_check_generic_kerberos(struct irpc_message *msg,
&ent);
if (ret != 0) {
hdb_free_entry(kdc->smb_krb5_context->krb5_context, &ent);
hdb_free_entry(kdc->smb_krb5_context->krb5_context, kdc_config->db[0], &ent);
krb5_free_principal(kdc->smb_krb5_context->krb5_context, principal);
return NT_STATUS_LOGON_FAILURE;
@ -247,7 +247,7 @@ static NTSTATUS kdc_check_generic_kerberos(struct irpc_message *msg,
ret = kdc_check_pac(kdc->smb_krb5_context->krb5_context, srv_sig, &kdc_sig, &ent);
hdb_free_entry(kdc->smb_krb5_context->krb5_context, &ent);
hdb_free_entry(kdc->smb_krb5_context->krb5_context, kdc_config->db[0], &ent);
krb5_free_principal(kdc->smb_krb5_context->krb5_context, principal);
if (ret != 0) {

View File

@ -180,8 +180,6 @@ static int sdb_entry_to_hdb_entry(krb5_context context,
unsigned int i;
int rc;
ZERO_STRUCTP(h);
rc = krb5_copy_principal(context,
s->principal,
&h->principal);
@ -311,28 +309,15 @@ error:
static int samba_kdc_hdb_entry_destructor(struct samba_kdc_entry *p)
{
struct hdb_entry_ex *entry_ex = p->entry_ex;
free_hdb_entry(&entry_ex->entry);
hdb_entry *entry_ex = p->entry_ex;
free_hdb_entry(entry_ex);
return 0;
}
static void samba_kdc_free_hdb_entry(krb5_context context,
struct hdb_entry_ex *entry_ex)
{
/* this function is called only from hdb_free_entry().
* Make sure we neutralize the destructor or we will
* get a double free later when hdb_free_entry() will
* try to call free_hdb_entry() */
talloc_set_destructor(entry_ex->ctx, NULL);
/* now proceed to free the talloc part */
talloc_free(entry_ex->ctx);
}
int sdb_entry_ex_to_hdb_entry_ex(krb5_context context,
const struct sdb_entry_ex *s,
struct hdb_entry_ex *h)
hdb_entry *h)
{
struct samba_kdc_entry *skdc_entry;
@ -341,12 +326,11 @@ int sdb_entry_ex_to_hdb_entry_ex(krb5_context context,
if (s->ctx != NULL) {
skdc_entry = talloc_get_type(s->ctx, struct samba_kdc_entry);
h->ctx = skdc_entry;
h->free_entry = samba_kdc_free_hdb_entry;
h->context = skdc_entry;
talloc_set_destructor(skdc_entry,
samba_kdc_hdb_entry_destructor);
}
return sdb_entry_to_hdb_entry(context, &s->entry, &h->entry);
return sdb_entry_to_hdb_entry(context, &s->entry, h);
}

View File

@ -37,8 +37,9 @@
* For PKINIT we also get pk_reply_key and can add PAC_CREDENTIAL_INFO.
*/
static krb5_error_code samba_wdc_get_pac(void *priv, krb5_context context,
struct hdb_entry_ex *client,
struct hdb_entry_ex *server,
krb5_kdc_configuration *config,
hdb_entry *client,
hdb_entry *server,
const krb5_keyblock *pk_reply_key,
uint64_t pac_attributes,
krb5_pac *pac)
@ -55,11 +56,11 @@ static krb5_error_code samba_wdc_get_pac(void *priv, krb5_context context,
krb5_error_code ret;
NTSTATUS nt_status;
struct samba_kdc_entry *skdc_entry =
talloc_get_type_abort(client->ctx,
talloc_get_type_abort(client->context,
struct samba_kdc_entry);
bool is_krbtgt;
mem_ctx = talloc_named(client->ctx, 0, "samba_get_pac context");
mem_ctx = talloc_named(client->context, 0, "samba_get_pac context");
if (!mem_ctx) {
return ENOMEM;
}
@ -68,7 +69,7 @@ static krb5_error_code samba_wdc_get_pac(void *priv, krb5_context context,
cred_ndr_ptr = &cred_ndr;
}
is_krbtgt = krb5_principal_is_krbtgt(context, server->entry.principal);
is_krbtgt = krb5_principal_is_krbtgt(context, server->principal);
nt_status = samba_kdc_get_pac_blobs(mem_ctx, skdc_entry,
&logon_blob,
@ -112,17 +113,17 @@ static krb5_error_code samba_wdc_get_pac(void *priv, krb5_context context,
static krb5_error_code samba_wdc_reget_pac2(krb5_context context,
const krb5_principal delegated_proxy_principal,
struct hdb_entry_ex *client,
struct hdb_entry_ex *server,
struct hdb_entry_ex *krbtgt,
hdb_entry *client,
hdb_entry *server,
hdb_entry *krbtgt,
krb5_pac *pac,
krb5_cksumtype ctype)
{
struct samba_kdc_entry *server_skdc_entry =
talloc_get_type_abort(server->ctx,
talloc_get_type_abort(server->context,
struct samba_kdc_entry);
struct samba_kdc_entry *krbtgt_skdc_entry =
talloc_get_type_abort(krbtgt->ctx,
talloc_get_type_abort(krbtgt->context,
struct samba_kdc_entry);
TALLOC_CTX *mem_ctx = talloc_named(server_skdc_entry,
0,
@ -157,7 +158,7 @@ static krb5_error_code samba_wdc_reget_pac2(krb5_context context,
if (client != NULL) {
struct samba_kdc_entry *client_skdc_entry = NULL;
client_skdc_entry = talloc_get_type_abort(client->ctx,
client_skdc_entry = talloc_get_type_abort(client->context,
struct samba_kdc_entry);
/*
@ -214,7 +215,7 @@ static krb5_error_code samba_wdc_reget_pac2(krb5_context context,
return ret;
}
}
ret = hdb_enctype2key(context, &krbtgt->entry, NULL, etype, &key);
ret = hdb_enctype2key(context, krbtgt, NULL, etype, &key);
if (ret != 0) {
talloc_free(mem_ctx);
return ret;
@ -241,7 +242,7 @@ static krb5_error_code samba_wdc_reget_pac2(krb5_context context,
nt_status = samba_kdc_update_delegation_info_blob(mem_ctx,
context, *pac,
server->entry.principal,
server->principal,
delegated_proxy_principal,
deleg_blob);
if (!NT_STATUS_IS_OK(nt_status)) {
@ -261,7 +262,7 @@ static krb5_error_code samba_wdc_reget_pac2(krb5_context context,
return KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
}
client_skdc_entry = talloc_get_type_abort(client->ctx,
client_skdc_entry = talloc_get_type_abort(client->context,
struct samba_kdc_entry);
nt_status = samba_kdc_get_pac_blobs(mem_ctx, client_skdc_entry,
@ -485,7 +486,7 @@ static krb5_error_code samba_wdc_reget_pac2(krb5_context context,
goto out;
}
is_krbtgt = krb5_principal_is_krbtgt(context, server->entry.principal);
is_krbtgt = krb5_principal_is_krbtgt(context, server->principal);
if (!is_untrusted && !is_krbtgt) {
/*
@ -663,19 +664,20 @@ out:
/* Resign (and reform, including possibly new groups) a PAC */
static krb5_error_code samba_wdc_reget_pac(void *priv, krb5_context context,
krb5_kdc_configuration *config,
const krb5_principal client_principal,
const krb5_principal delegated_proxy_principal,
struct hdb_entry_ex *client,
struct hdb_entry_ex *server,
struct hdb_entry_ex *krbtgt,
hdb_entry *client,
hdb_entry *server,
hdb_entry *krbtgt,
krb5_pac *pac)
{
struct samba_kdc_entry *krbtgt_skdc_entry =
talloc_get_type_abort(krbtgt->ctx,
talloc_get_type_abort(krbtgt->context,
struct samba_kdc_entry);
krb5_error_code ret;
krb5_cksumtype ctype = CKSUMTYPE_NONE;
struct hdb_entry_ex signing_krbtgt_hdb;
hdb_entry signing_krbtgt_hdb;
if (delegated_proxy_principal) {
uint16_t rodc_id;
@ -726,7 +728,7 @@ static krb5_error_code samba_wdc_reget_pac(void *priv, krb5_context context,
* different KDC than the one that issued the header
* ticket.
*/
if (rodc_id != krbtgt->entry.kvno >> 16) {
if (rodc_id != krbtgt->kvno >> 16) {
struct sdb_entry_ex signing_krbtgt_sdb;
/*
@ -745,7 +747,7 @@ static krb5_error_code samba_wdc_reget_pac(void *priv, krb5_context context,
*/
ret = samba_kdc_fetch(context,
krbtgt_skdc_entry->kdc_db_ctx,
krbtgt->entry.principal,
krbtgt->principal,
SDB_F_GET_KRBTGT | SDB_F_CANON,
0,
&signing_krbtgt_sdb);
@ -779,7 +781,7 @@ static krb5_error_code samba_wdc_reget_pac(void *priv, krb5_context context,
ctype);
if (krbtgt == &signing_krbtgt_hdb) {
hdb_free_entry(context, &signing_krbtgt_hdb);
hdb_free_entry(context, config->db[0], &signing_krbtgt_hdb);
}
return ret;
@ -844,9 +846,9 @@ static krb5_error_code samba_wdc_check_client_access(void *priv,
char *workstation;
NTSTATUS nt_status;
kdc_entry = talloc_get_type(r->client->ctx, struct samba_kdc_entry);
password_change = (r->server && r->server->entry.flags.change_pw);
workstation = get_netbios_name((TALLOC_CTX *)r->client->ctx,
kdc_entry = talloc_get_type(r->client->context, struct samba_kdc_entry);
password_change = (r->server && r->server->flags.change_pw);
workstation = get_netbios_name((TALLOC_CTX *)r->client->context,
r->req.req_body.addresses);
nt_status = samba_kdc_check_client_access(kdc_entry,
@ -905,7 +907,7 @@ static krb5_error_code samba_wdc_finalize_reply(void *priv,
struct samba_kdc_entry *server_kdc_entry;
uint32_t supported_enctypes;
server_kdc_entry = talloc_get_type(r->server->ctx, struct samba_kdc_entry);
server_kdc_entry = talloc_get_type(r->server->context, struct samba_kdc_entry);
/*
* If the canonicalize flag is set, add PA-SUPPORTED-ENCTYPES padata
@ -960,7 +962,7 @@ static krb5_error_code samba_wdc_referral_policy(void *priv,
}
struct krb5plugin_kdc_ftable kdc_plugin_table = {
.minor_version = KRB5_PLUGIN_KDC_VERSION_9,
.minor_version = KRB5_PLUGIN_KDC_VERSION_10,
.init = samba_wdc_plugin_init,
.fini = samba_wdc_plugin_fini,
.pac_verify = samba_wdc_reget_pac,

View File

@ -433,10 +433,7 @@ if not bld.CONFIG_SET("USING_SYSTEM_HEIMNTLM"):
if not bld.CONFIG_SET("USING_SYSTEM_HDB"):
HEIMDAL_ASN1('HEIMDAL_HDB_ASN1', 'lib/hdb/hdb.asn1',
directory='lib/asn1',
options="--sequence=HDB-extensions \
--sequence=HDB-Ext-KeyRotation \
--sequence=HDB-Ext-KeySet \
--sequence=Keys",
option_file="lib/hdb/hdb.opt",
template=False,
includes='../heimdal/lib/asn1')