2 Unix SMB/CIFS implementation.
4 Database Glue between Samba and the KDC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2009
7 Copyright (C) Simo Sorce <idra@samba.org> 2010
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "libcli/security/security.h"
26 #include "librpc/gen_ndr/ndr_security.h"
27 #include "auth/auth.h"
28 #include "auth/auth_sam.h"
29 #include "dsdb/samdb/samdb.h"
30 #include "dsdb/common/util.h"
31 #include "librpc/gen_ndr/ndr_drsblobs.h"
32 #include "param/param.h"
33 #include "param/secrets.h"
34 #include "../lib/crypto/md4.h"
35 #include "system/kerberos.h"
36 #include "auth/kerberos/kerberos.h"
38 #include "kdc/samba_kdc.h"
39 #include "kdc/db-glue.h"
40 #include "kdc/pac-glue.h"
41 #include "librpc/gen_ndr/ndr_irpc_c.h"
42 #include "lib/messaging/irpc.h"
47 #define SAMBA_KVNO_GET_KRBTGT(kvno) \
48 ((uint16_t)(((uint32_t)kvno) >> 16))
50 #define SAMBA_KVNO_GET_VALUE(kvno) \
51 ((uint16_t)(((uint32_t)kvno) & 0xFFFF))
53 #define SAMBA_KVNO_AND_KRBTGT(kvno, krbtgt) \
54 ((krb5_kvno)((((uint32_t)kvno) & 0xFFFF) | \
55 ((((uint32_t)krbtgt) << 16) & 0xFFFF0000)))
57 enum trust_direction {
59 INBOUND = LSA_TRUST_DIRECTION_INBOUND,
60 OUTBOUND = LSA_TRUST_DIRECTION_OUTBOUND
63 static const char *trust_attrs[] = {
70 "msDS-TrustForestTrustInfo",
74 "msDS-SupportedEncryptionTypes",
79 send a message to the drepl server telling it to initiate a
80 REPL_SECRET getncchanges extended op to fetch the users secrets
82 static void auth_sam_trigger_repl_secret(TALLOC_CTX *mem_ctx,
83 struct imessaging_context *msg_ctx,
84 struct tevent_context *event_ctx,
85 struct ldb_dn *user_dn)
87 struct dcerpc_binding_handle *irpc_handle;
88 struct drepl_trigger_repl_secret r;
89 struct tevent_req *req;
92 tmp_ctx = talloc_new(mem_ctx);
93 if (tmp_ctx == NULL) {
97 irpc_handle = irpc_binding_handle_by_name(tmp_ctx, msg_ctx,
100 if (irpc_handle == NULL) {
101 DEBUG(1,(__location__ ": Unable to get binding handle for dreplsrv\n"));
102 TALLOC_FREE(tmp_ctx);
106 r.in.user_dn = ldb_dn_get_linearized(user_dn);
109 * This seem to rely on the current IRPC implementation,
110 * which delivers the message in the _send function.
112 * TODO: we need a ONE_WAY IRPC handle and register
113 * a callback and wait for it to be triggered!
115 req = dcerpc_drepl_trigger_repl_secret_r_send(tmp_ctx,
120 /* we aren't interested in a reply */
122 TALLOC_FREE(tmp_ctx);
125 static time_t ldb_msg_find_krb5time_ldap_time(struct ldb_message *msg, const char *attr, time_t default_val)
131 gentime = ldb_msg_find_attr_as_string(msg, attr, NULL);
135 tmp = strptime(gentime, "%Y%m%d%H%M%SZ", &tm);
143 static struct SDBFlags uf2SDBFlags(krb5_context context, uint32_t userAccountControl, enum samba_kdc_ent_type ent_type)
145 struct SDBFlags flags = int2SDBFlags(0);
147 /* we don't allow kadmin deletes */
150 /* mark the principal as invalid to start with */
155 /* All accounts are servers, but this may be disabled again in the caller */
158 /* Account types - clear the invalid bit if it turns out to be valid */
159 if (userAccountControl & UF_NORMAL_ACCOUNT) {
160 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
166 if (userAccountControl & UF_INTERDOMAIN_TRUST_ACCOUNT) {
167 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
172 if (userAccountControl & UF_WORKSTATION_TRUST_ACCOUNT) {
173 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
178 if (userAccountControl & UF_SERVER_TRUST_ACCOUNT) {
179 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
185 /* Not permitted to act as a client if disabled */
186 if (userAccountControl & UF_ACCOUNTDISABLE) {
189 if (userAccountControl & UF_LOCKOUT) {
190 flags.locked_out = 1;
193 if (userAccountControl & UF_PASSWORD_NOTREQD) {
198 UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
200 if (userAccountControl & UF_TEMP_DUPLICATE_ACCOUNT) {
204 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in samba_kdc_message2entry() */
207 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
211 if (userAccountControl & UF_SMARTCARD_REQUIRED) {
212 flags.require_hwauth = 1;
214 if (userAccountControl & UF_TRUSTED_FOR_DELEGATION) {
215 flags.ok_as_delegate = 1;
217 if (userAccountControl & UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION) {
219 * this is confusing...
221 * UF_TRUSTED_FOR_DELEGATION
226 * UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION
227 * => trusted_for_delegation
229 flags.trusted_for_delegation = 1;
231 if (!(userAccountControl & UF_NOT_DELEGATED)) {
232 flags.forwardable = 1;
236 if (userAccountControl & UF_DONT_REQUIRE_PREAUTH) {
237 flags.require_preauth = 0;
239 flags.require_preauth = 1;
242 if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
243 flags.no_auth_data_reqd = 1;
249 static int samba_kdc_entry_destructor(struct samba_kdc_entry *p)
251 if (p->db_entry != NULL) {
253 * A sdb_entry still has a reference
258 if (p->kdc_entry != NULL) {
260 * hdb_entry or krb5_db_entry still
261 * have a reference...
270 * Sort keys in descending order of strength.
272 * Explanaton from Greg Hudson:
274 * To encrypt tickets only the first returned key is used by the MIT KDC. The
275 * other keys just communicate support for session key enctypes, and aren't
276 * really used. The encryption key for the ticket enc part doesn't have
277 * to be of a type requested by the client. The session key enctype is chosen
278 * based on the client preference order, limited by the set of enctypes present
279 * in the server keys (unless the string attribute is set on the server
280 * principal overriding that set).
283 static int sdb_key_strength_priority(krb5_enctype etype)
285 static const krb5_enctype etype_list[] = {
286 ENCTYPE_AES256_CTS_HMAC_SHA1_96,
287 ENCTYPE_AES128_CTS_HMAC_SHA1_96,
288 ENCTYPE_DES3_CBC_SHA1,
289 ENCTYPE_ARCFOUR_HMAC,
297 for (i = 0; i < ARRAY_SIZE(etype_list); i++) {
298 if (etype == etype_list[i]) {
303 return ARRAY_SIZE(etype_list) - i;
306 static int sdb_key_strength_cmp(const struct sdb_key *k1, const struct sdb_key *k2)
308 int p1 = sdb_key_strength_priority(KRB5_KEY_TYPE(&k1->key));
309 int p2 = sdb_key_strength_priority(KRB5_KEY_TYPE(&k2->key));
317 * Higher priority comes first
325 static void samba_kdc_sort_keys(struct sdb_keys *keys)
331 TYPESAFE_QSORT(keys->val, keys->len, sdb_key_strength_cmp);
334 int samba_kdc_set_fixed_keys(krb5_context context,
335 const struct ldb_val *secretbuffer,
336 uint32_t supported_enctypes,
337 struct sdb_keys *keys)
339 uint16_t allocated_keys = 0;
344 keys->val = calloc(allocated_keys, sizeof(struct sdb_key));
345 if (keys->val == NULL) {
346 memset(secretbuffer->data, 0, secretbuffer->length);
351 if (supported_enctypes & ENC_HMAC_SHA1_96_AES256) {
352 struct sdb_key key = {};
354 ret = smb_krb5_keyblock_init_contents(context,
355 ENCTYPE_AES256_CTS_HMAC_SHA1_96,
357 MIN(secretbuffer->length, 32),
360 memset(secretbuffer->data, 0, secretbuffer->length);
364 keys->val[keys->len] = key;
368 if (supported_enctypes & ENC_HMAC_SHA1_96_AES128) {
369 struct sdb_key key = {};
371 ret = smb_krb5_keyblock_init_contents(context,
372 ENCTYPE_AES128_CTS_HMAC_SHA1_96,
374 MIN(secretbuffer->length, 16),
377 memset(secretbuffer->data, 0, secretbuffer->length);
381 keys->val[keys->len] = key;
385 if (supported_enctypes & ENC_RC4_HMAC_MD5) {
386 struct sdb_key key = {};
388 ret = smb_krb5_keyblock_init_contents(context,
389 ENCTYPE_ARCFOUR_HMAC,
391 MIN(secretbuffer->length, 16),
394 memset(secretbuffer->data, 0, secretbuffer->length);
398 keys->val[keys->len] = key;
407 static int samba_kdc_set_random_keys(krb5_context context,
408 uint32_t supported_enctypes,
409 struct sdb_keys *keys)
411 struct ldb_val secret_val;
412 uint8_t secretbuffer[32];
415 * Fake keys until we have a better way to reject
416 * non-pkinit requests.
418 * We just need to indicate which encryption types are
421 generate_secret_buffer(secretbuffer, sizeof(secretbuffer));
423 secret_val = data_blob_const(secretbuffer,
424 sizeof(secretbuffer));
425 return samba_kdc_set_fixed_keys(context,
431 struct samba_kdc_user_keys {
432 struct sdb_keys *skeys;
434 uint32_t *returned_kvno;
435 uint32_t supported_enctypes;
436 uint32_t *available_enctypes;
437 const struct samr_Password *nthash;
438 const char *salt_string;
440 const struct package_PrimaryKerberosKey4 *pkeys;
443 static krb5_error_code samba_kdc_fill_user_keys(krb5_context context,
444 struct samba_kdc_user_keys *p)
447 * Make sure we'll never reveal DES keys
449 uint32_t supported_enctypes = p->supported_enctypes & ENC_ALL_TYPES;
450 uint32_t _available_enctypes = 0;
451 uint32_t *available_enctypes = p->available_enctypes;
452 uint32_t _returned_kvno = 0;
453 uint32_t *returned_kvno = p->returned_kvno;
454 uint32_t num_pkeys = p->num_pkeys;
455 uint32_t allocated_keys = num_pkeys;
459 if (available_enctypes == NULL) {
460 available_enctypes = &_available_enctypes;
463 *available_enctypes = 0;
465 if (returned_kvno == NULL) {
466 returned_kvno = &_returned_kvno;
469 *returned_kvno = p->kvno;
471 if (p->nthash != NULL) {
475 allocated_keys = MAX(1, allocated_keys);
477 /* allocate space to decode into */
479 p->skeys->val = calloc(allocated_keys, sizeof(struct sdb_key));
480 if (p->skeys->val == NULL) {
484 for (i=0; i < num_pkeys; i++) {
485 struct sdb_key key = {};
486 uint32_t enctype_bit;
488 if (p->pkeys[i].value == NULL) {
492 enctype_bit = kerberos_enctype_to_bitmap(p->pkeys[i].keytype);
493 if (!(enctype_bit & supported_enctypes)) {
497 if (p->salt_string != NULL) {
500 salt = data_blob_string_const(p->salt_string);
502 key.salt = calloc(1, sizeof(*key.salt));
503 if (key.salt == NULL) {
508 key.salt->type = KRB5_PW_SALT;
510 ret = smb_krb5_copy_data_contents(&key.salt->salt,
514 ZERO_STRUCTP(key.salt);
520 ret = smb_krb5_keyblock_init_contents(context,
522 p->pkeys[i].value->data,
523 p->pkeys[i].value->length,
526 p->skeys->val[p->skeys->len++] = key;
527 *available_enctypes |= enctype_bit;
530 ZERO_STRUCT(key.key);
532 if (ret == KRB5_PROG_ETYPE_NOSUPP) {
533 DEBUG(2,("Unsupported keytype ignored - type %u\n",
534 p->pkeys[i].keytype));
542 if (p->nthash != NULL && (supported_enctypes & ENC_RC4_HMAC_MD5)) {
543 struct sdb_key key = {};
545 ret = smb_krb5_keyblock_init_contents(context,
546 ENCTYPE_ARCFOUR_HMAC,
548 sizeof(p->nthash->hash),
551 p->skeys->val[p->skeys->len++] = key;
553 *available_enctypes |= ENC_RC4_HMAC_MD5;
554 } else if (ret == KRB5_PROG_ETYPE_NOSUPP) {
555 DEBUG(2,("Unsupported keytype ignored - type %u\n",
556 ENCTYPE_ARCFOUR_HMAC));
564 samba_kdc_sort_keys(p->skeys);
568 sdb_keys_free(p->skeys);
572 krb5_error_code samba_kdc_message2entry_keys(krb5_context context,
574 const struct ldb_message *msg,
577 uint32_t userAccountControl,
578 enum samba_kdc_ent_type ent_type,
580 krb5_kvno requested_kvno,
581 struct sdb_entry *entry,
582 const uint32_t supported_enctypes_in,
583 uint32_t *supported_enctypes_out)
585 krb5_error_code ret = 0;
586 enum ndr_err_code ndr_err;
587 struct samr_Password *hash;
588 unsigned int num_ntPwdHistory = 0;
589 struct samr_Password *ntPwdHistory = NULL;
590 struct samr_Password *old_hash = NULL;
591 struct samr_Password *older_hash = NULL;
592 const struct ldb_val *sc_val;
593 struct supplementalCredentialsBlob scb;
594 struct supplementalCredentialsPackage *scpk = NULL;
595 struct package_PrimaryKerberosBlob _pkb;
596 struct package_PrimaryKerberosCtr4 *pkb4 = NULL;
597 int krbtgt_number = 0;
598 uint32_t current_kvno;
599 uint32_t old_kvno = 0;
600 uint32_t older_kvno = 0;
601 uint32_t returned_kvno = 0;
603 struct samba_kdc_user_keys keys = { .num_pkeys = 0, };
604 struct samba_kdc_user_keys old_keys = { .num_pkeys = 0, };
605 struct samba_kdc_user_keys older_keys = { .num_pkeys = 0, };
606 uint32_t available_enctypes = 0;
607 uint32_t supported_enctypes = supported_enctypes_in;
609 *supported_enctypes_out = 0;
611 /* Is this the krbtgt or a RODC krbtgt */
613 krbtgt_number = ldb_msg_find_attr_as_int(msg, "msDS-SecondaryKrbTgtNumber", -1);
615 if (krbtgt_number == -1) {
618 if (krbtgt_number == 0) {
623 if ((ent_type == SAMBA_KDC_ENT_TYPE_CLIENT)
624 && (userAccountControl & UF_SMARTCARD_REQUIRED)) {
625 ret = samba_kdc_set_random_keys(context,
629 *supported_enctypes_out = supported_enctypes;
634 current_kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
635 if (current_kvno > 1) {
636 old_kvno = current_kvno - 1;
638 if (current_kvno > 2) {
639 older_kvno = current_kvno - 2;
643 * Even for the main krbtgt account
644 * we have to strictly split the kvno into
645 * two 16-bit parts and the upper 16-bit
646 * need to be all zero, even if
647 * the msDS-KeyVersionNumber has a value
650 * See https://bugzilla.samba.org/show_bug.cgi?id=14951
652 current_kvno = SAMBA_KVNO_GET_VALUE(current_kvno);
653 old_kvno = SAMBA_KVNO_GET_VALUE(old_kvno);
654 older_kvno = SAMBA_KVNO_GET_VALUE(older_kvno);
655 requested_kvno = SAMBA_KVNO_GET_VALUE(requested_kvno);
658 /* Get keys from the db */
660 hash = samdb_result_hash(mem_ctx, msg, "unicodePwd");
661 num_ntPwdHistory = samdb_result_hashes(mem_ctx, msg,
664 if (num_ntPwdHistory > 1) {
665 old_hash = &ntPwdHistory[1];
667 if (num_ntPwdHistory > 2) {
668 older_hash = &ntPwdHistory[1];
670 sc_val = ldb_msg_find_ldb_val(msg, "supplementalCredentials");
672 /* supplementalCredentials if present */
674 ndr_err = ndr_pull_struct_blob_all(sc_val, mem_ctx, &scb,
675 (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob);
676 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
677 dump_data(0, sc_val->data, sc_val->length);
682 if (scb.sub.signature != SUPPLEMENTAL_CREDENTIALS_SIGNATURE) {
683 if (scb.sub.num_packages != 0) {
684 NDR_PRINT_DEBUG(supplementalCredentialsBlob, &scb);
690 for (i=0; i < scb.sub.num_packages; i++) {
691 if (strcmp("Primary:Kerberos-Newer-Keys", scb.sub.packages[i].name) == 0) {
692 scpk = &scb.sub.packages[i];
693 if (!scpk->data || !scpk->data[0]) {
702 * Primary:Kerberos-Newer-Keys element
703 * of supplementalCredentials
705 * The legacy Primary:Kerberos only contains
706 * single DES keys, which are completely ignored
712 blob = strhex_to_data_blob(mem_ctx, scpk->data);
718 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
719 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &_pkb,
720 (ndr_pull_flags_fn_t)ndr_pull_package_PrimaryKerberosBlob);
721 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
723 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
724 krb5_warnx(context, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
728 if (_pkb.version != 4) {
730 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
731 krb5_warnx(context, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
735 pkb4 = &_pkb.ctr.ctr4;
738 keys = (struct samba_kdc_user_keys) {
739 .kvno = current_kvno,
740 .supported_enctypes = supported_enctypes,
742 .salt_string = pkb4 != NULL ? pkb4->salt.string : NULL,
743 .num_pkeys = pkb4 != NULL ? pkb4->num_keys : 0,
744 .pkeys = pkb4 != NULL ? pkb4->keys : NULL,
747 old_keys = (struct samba_kdc_user_keys) {
749 .supported_enctypes = supported_enctypes,
751 .salt_string = pkb4 != NULL ? pkb4->salt.string : NULL,
752 .num_pkeys = pkb4 != NULL ? pkb4->num_old_keys : 0,
753 .pkeys = pkb4 != NULL ? pkb4->old_keys : NULL,
755 older_keys = (struct samba_kdc_user_keys) {
757 .supported_enctypes = supported_enctypes,
758 .nthash = older_hash,
759 .salt_string = pkb4 != NULL ? pkb4->salt.string : NULL,
760 .num_pkeys = pkb4 != NULL ? pkb4->num_older_keys : 0,
761 .pkeys = pkb4 != NULL ? pkb4->older_keys : NULL,
764 if (flags & SDB_F_KVNO_SPECIFIED) {
765 if (requested_kvno == keys.kvno) {
767 * The current kvno was requested,
770 keys.skeys = &entry->keys;
771 keys.available_enctypes = &available_enctypes;
772 keys.returned_kvno = &returned_kvno;
773 } else if (requested_kvno == 0) {
775 * don't return any keys
777 } else if (requested_kvno == old_keys.kvno) {
779 * return the old keys as default keys
780 * with the requested kvno.
782 old_keys.skeys = &entry->keys;
783 old_keys.available_enctypes = &available_enctypes;
784 old_keys.returned_kvno = &returned_kvno;
785 } else if (requested_kvno == older_keys.kvno) {
787 * return the older keys as default keys
788 * with the requested kvno.
790 older_keys.skeys = &entry->keys;
791 older_keys.available_enctypes = &available_enctypes;
792 older_keys.returned_kvno = &returned_kvno;
795 * don't return any keys
799 bool include_history = false;
801 if ((flags & SDB_F_GET_CLIENT) && (flags & SDB_F_FOR_AS_REQ)) {
802 include_history = true;
803 } else if (flags & SDB_F_ADMIN_DATA) {
804 include_history = true;
807 keys.skeys = &entry->keys;
808 keys.available_enctypes = &available_enctypes;
809 keys.returned_kvno = &returned_kvno;
811 if (include_history && old_keys.kvno != 0) {
812 old_keys.skeys = &entry->old_keys;
814 if (include_history && older_keys.kvno != 0) {
815 older_keys.skeys = &entry->older_keys;
819 if (keys.skeys != NULL) {
820 ret = samba_kdc_fill_user_keys(context, &keys);
826 if (old_keys.skeys != NULL) {
827 ret = samba_kdc_fill_user_keys(context, &old_keys);
833 if (older_keys.skeys != NULL) {
834 ret = samba_kdc_fill_user_keys(context, &older_keys);
840 *supported_enctypes_out |= available_enctypes;
842 /* Set FAST support bits */
843 *supported_enctypes_out |= supported_enctypes & (ENC_FAST_SUPPORTED |
844 ENC_COMPOUND_IDENTITY_SUPPORTED |
845 ENC_CLAIMS_SUPPORTED);
849 * Even for the main krbtgt account
850 * we have to strictly split the kvno into
851 * two 16-bit parts and the upper 16-bit
852 * need to be all zero, even if
853 * the msDS-KeyVersionNumber has a value
856 * See https://bugzilla.samba.org/show_bug.cgi?id=14951
858 returned_kvno = SAMBA_KVNO_AND_KRBTGT(returned_kvno, krbtgt_number);
860 entry->kvno = returned_kvno;
866 static int principal_comp_strcmp_int(krb5_context context,
867 krb5_const_principal principal,
868 unsigned int component,
875 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
876 p = krb5_principal_get_comp_string(context, principal, component);
883 if (component >= krb5_princ_size(context, principal)) {
887 d = krb5_princ_component(context, principal, component);
896 return strncasecmp(p, string, len);
898 return strncmp(p, string, len);
902 static int principal_comp_strcasecmp(krb5_context context,
903 krb5_const_principal principal,
904 unsigned int component,
907 return principal_comp_strcmp_int(context, principal,
908 component, string, true);
911 static int principal_comp_strcmp(krb5_context context,
912 krb5_const_principal principal,
913 unsigned int component,
916 return principal_comp_strcmp_int(context, principal,
917 component, string, false);
921 * Construct an hdb_entry from a directory entry.
923 static krb5_error_code samba_kdc_message2entry(krb5_context context,
924 struct samba_kdc_db_context *kdc_db_ctx,
926 krb5_const_principal principal,
927 enum samba_kdc_ent_type ent_type,
930 struct ldb_dn *realm_dn,
931 struct ldb_message *msg,
932 struct sdb_entry *entry)
934 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
935 uint32_t userAccountControl;
936 uint32_t msDS_User_Account_Control_Computed;
937 krb5_error_code ret = 0;
938 krb5_boolean is_computer = FALSE;
939 struct samba_kdc_entry *p;
942 bool protected_user = false;
944 bool is_krbtgt = false;
945 bool is_rodc = false;
946 struct ldb_message_element *objectclasses;
947 struct ldb_val computer_val = data_blob_string_const("computer");
948 uint32_t supported_enctypes
949 = ldb_msg_find_attr_as_uint(msg,
950 "msDS-SupportedEncryptionTypes",
952 const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
956 if (ldb_msg_find_element(msg, "msDS-SecondaryKrbTgtNumber")) {
960 if (!samAccountName) {
962 krb5_set_error_message(context, ret, "samba_kdc_message2entry: no samAccountName present");
966 objectclasses = ldb_msg_find_element(msg, "objectClass");
968 if (objectclasses && ldb_msg_find_val(objectclasses, &computer_val)) {
972 p = talloc_zero(mem_ctx, struct samba_kdc_entry);
978 p->is_rodc = is_rodc;
979 p->kdc_db_ctx = kdc_db_ctx;
980 p->realm_dn = talloc_reference(p, realm_dn);
986 talloc_set_destructor(p, samba_kdc_entry_destructor);
988 entry->skdc_entry = p;
990 userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
992 msDS_User_Account_Control_Computed
993 = ldb_msg_find_attr_as_uint(msg,
994 "msDS-User-Account-Control-Computed",
998 * This brings in the lockout flag, block the account if not
999 * found. We need the weird UF_ACCOUNTDISABLE check because
1000 * we do not want to fail open if the value is not returned,
1001 * but 0 is a valid value (all OK)
1003 if (msDS_User_Account_Control_Computed == UF_ACCOUNTDISABLE) {
1005 krb5_set_error_message(context, ret, "samba_kdc_message2entry: "
1006 "no msDS-User-Account-Control-Computed present");
1009 userAccountControl |= msDS_User_Account_Control_Computed;
1013 * If we are set to canonicalize, we get back the fixed UPPER
1014 * case realm, and the real username (ie matching LDAP
1017 * Otherwise, if we are set to enterprise, we
1018 * get back the whole principal as-sent
1020 * Finally, if we are not set to canonicalize, we get back the
1021 * fixed UPPER case realm, but the as-sent username
1024 if (ent_type == SAMBA_KDC_ENT_TYPE_KRBTGT) {
1025 p->is_krbtgt = true;
1027 if (flags & (SDB_F_CANON|SDB_F_FORCE_CANON)) {
1029 * When requested to do so, ensure that the
1030 * both realm values in the principal are set
1031 * to the upper case, canonical realm
1033 ret = smb_krb5_make_principal(context, &entry->principal,
1034 lpcfg_realm(lp_ctx), "krbtgt",
1035 lpcfg_realm(lp_ctx), NULL);
1037 krb5_clear_error_message(context);
1040 smb_krb5_principal_set_type(context, entry->principal, KRB5_NT_SRV_INST);
1042 ret = krb5_copy_principal(context, principal, &entry->principal);
1044 krb5_clear_error_message(context);
1048 * this appears to be required regardless of
1049 * the canonicalize flag from the client
1051 ret = smb_krb5_principal_set_realm(context, entry->principal, lpcfg_realm(lp_ctx));
1053 krb5_clear_error_message(context);
1058 } else if (ent_type == SAMBA_KDC_ENT_TYPE_ANY && principal == NULL) {
1059 ret = smb_krb5_make_principal(context, &entry->principal, lpcfg_realm(lp_ctx), samAccountName, NULL);
1061 krb5_clear_error_message(context);
1064 } else if ((flags & SDB_F_FORCE_CANON) ||
1065 ((flags & SDB_F_CANON) && (flags & SDB_F_FOR_AS_REQ))) {
1067 * SDB_F_CANON maps from the canonicalize flag in the
1068 * packet, and has a different meaning between AS-REQ
1069 * and TGS-REQ. We only change the principal in the AS-REQ case
1071 * The SDB_F_FORCE_CANON if for new MIT KDC code that wants
1072 * the canonical name in all lookups, and takes care to
1073 * canonicalize only when appropriate.
1075 ret = smb_krb5_make_principal(context, &entry->principal, lpcfg_realm(lp_ctx), samAccountName, NULL);
1077 krb5_clear_error_message(context);
1081 ret = krb5_copy_principal(context, principal, &entry->principal);
1083 krb5_clear_error_message(context);
1087 /* While we have copied the client principal, tests
1088 * show that Win2k3 returns the 'corrected' realm, not
1089 * the client-specified realm. This code attempts to
1090 * replace the client principal's realm with the one
1091 * we determine from our records */
1093 /* this has to be with malloc() */
1094 ret = smb_krb5_principal_set_realm(context, entry->principal, lpcfg_realm(lp_ctx));
1096 krb5_clear_error_message(context);
1101 /* First try and figure out the flags based on the userAccountControl */
1102 entry->flags = uf2SDBFlags(context, userAccountControl, ent_type);
1105 * Take control of the returned principal here, rather than
1106 * allowing the Heimdal code to do it as we have specific
1107 * behaviour around the forced realm to honour
1109 entry->flags.force_canonicalize = true;
1111 /* Windows 2008 seems to enforce this (very sensible) rule by
1112 * default - don't allow offline attacks on a user's password
1113 * by asking for a ticket to them as a service (encrypted with
1114 * their probably patheticly insecure password) */
1116 if (entry->flags.server
1117 && lpcfg_parm_bool(lp_ctx, NULL, "kdc", "require spn for service", true)) {
1118 if (!is_computer && !ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL)) {
1119 entry->flags.server = 0;
1124 * We restrict a 3-part SPN ending in my domain/realm to full
1125 * domain controllers.
1127 * This avoids any cases where (eg) a demoted DC still has
1128 * these more restricted SPNs.
1130 if (krb5_princ_size(context, principal) > 2) {
1132 = smb_krb5_principal_get_comp_string(mem_ctx,
1137 lpcfg_is_my_domain_or_realm(lp_ctx,
1139 bool is_dc = userAccountControl &
1140 (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT);
1141 if (is_our_realm && !is_dc) {
1142 entry->flags.server = 0;
1146 * To give the correct type of error to the client, we must
1147 * not just return the entry without .server set, we must
1148 * pretend the principal does not exist. Otherwise we may
1149 * return ERR_POLICY instead of
1150 * KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
1152 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER && entry->flags.server == 0) {
1153 ret = SDB_ERR_NOENTRY;
1154 krb5_set_error_message(context, ret, "samba_kdc_message2entry: no servicePrincipalName present for this server, refusing with no-such-entry");
1157 if (flags & SDB_F_ADMIN_DATA) {
1158 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
1159 * of the Heimdal KDC. They are stored in a the traditional
1160 * DB for audit purposes, and still form part of the structure
1163 /* use 'whenCreated' */
1164 entry->created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
1165 /* use 'kadmin' for now (needed by mit_samba) */
1167 ret = smb_krb5_make_principal(context,
1168 &entry->created_by.principal,
1169 lpcfg_realm(lp_ctx), "kadmin", NULL);
1171 krb5_clear_error_message(context);
1175 entry->modified_by = (struct sdb_event *) malloc(sizeof(struct sdb_event));
1176 if (entry->modified_by == NULL) {
1178 krb5_set_error_message(context, ret, "malloc: out of memory");
1182 /* use 'whenChanged' */
1183 entry->modified_by->time = ldb_msg_find_krb5time_ldap_time(msg, "whenChanged", 0);
1184 /* use 'kadmin' for now (needed by mit_samba) */
1185 ret = smb_krb5_make_principal(context,
1186 &entry->modified_by->principal,
1187 lpcfg_realm(lp_ctx), "kadmin", NULL);
1189 krb5_clear_error_message(context);
1195 /* The lack of password controls etc applies to krbtgt by
1196 * virtue of being that particular RID */
1197 status = dom_sid_split_rid(NULL, samdb_result_dom_sid(mem_ctx, msg, "objectSid"), NULL, &rid);
1199 if (!NT_STATUS_IS_OK(status)) {
1204 if (rid == DOMAIN_RID_KRBTGT) {
1207 entry->valid_end = NULL;
1208 entry->pw_end = NULL;
1210 entry->flags.invalid = 0;
1211 entry->flags.server = 1;
1213 realm = smb_krb5_principal_get_realm(
1214 mem_ctx, context, principal);
1215 if (realm == NULL) {
1220 /* Don't mark all requests for the krbtgt/realm as
1221 * 'change password', as otherwise we could get into
1222 * trouble, and not enforce the password expirty.
1223 * Instead, only do it when request is for the kpasswd service */
1224 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER
1225 && krb5_princ_size(context, principal) == 2
1226 && (principal_comp_strcmp(context, principal, 0, "kadmin") == 0)
1227 && (principal_comp_strcmp(context, principal, 1, "changepw") == 0)
1228 && lpcfg_is_my_domain_or_realm(lp_ctx, realm)) {
1229 entry->flags.change_pw = 1;
1234 entry->flags.client = 0;
1235 entry->flags.forwardable = 1;
1236 entry->flags.ok_as_delegate = 1;
1237 } else if (is_rodc) {
1238 /* The RODC krbtgt account is like the main krbtgt,
1239 * but it does not have a changepw or kadmin
1242 entry->valid_end = NULL;
1243 entry->pw_end = NULL;
1245 /* Also don't allow the RODC krbtgt to be a client (it should not be needed) */
1246 entry->flags.client = 0;
1247 entry->flags.invalid = 0;
1248 entry->flags.server = 1;
1250 entry->flags.client = 0;
1251 entry->flags.forwardable = 1;
1252 entry->flags.ok_as_delegate = 0;
1253 } else if (entry->flags.server && ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
1254 /* The account/password expiry only applies when the account is used as a
1255 * client (ie password login), not when used as a server */
1257 /* Make very well sure we don't use this for a client,
1258 * it could bypass the password restrictions */
1259 entry->flags.client = 0;
1261 entry->valid_end = NULL;
1262 entry->pw_end = NULL;
1265 NTTIME must_change_time
1266 = samdb_result_nttime(msg,
1267 "msDS-UserPasswordExpiryTimeComputed",
1269 if (must_change_time == 0x7FFFFFFFFFFFFFFFULL) {
1270 entry->pw_end = NULL;
1272 entry->pw_end = malloc(sizeof(*entry->pw_end));
1273 if (entry->pw_end == NULL) {
1277 *entry->pw_end = nt_time_to_unix(must_change_time);
1280 acct_expiry = samdb_result_account_expires(msg);
1281 if (acct_expiry == 0x7FFFFFFFFFFFFFFFULL) {
1282 entry->valid_end = NULL;
1284 entry->valid_end = malloc(sizeof(*entry->valid_end));
1285 if (entry->valid_end == NULL) {
1289 *entry->valid_end = nt_time_to_unix(acct_expiry);
1293 entry->valid_start = NULL;
1295 entry->max_life = malloc(sizeof(*entry->max_life));
1296 if (entry->max_life == NULL) {
1301 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
1302 *entry->max_life = kdc_db_ctx->policy.svc_tkt_lifetime;
1303 } else if (ent_type == SAMBA_KDC_ENT_TYPE_KRBTGT || ent_type == SAMBA_KDC_ENT_TYPE_CLIENT) {
1304 *entry->max_life = kdc_db_ctx->policy.usr_tkt_lifetime;
1306 *entry->max_life = MIN(kdc_db_ctx->policy.svc_tkt_lifetime,
1307 kdc_db_ctx->policy.usr_tkt_lifetime);
1310 entry->max_renew = malloc(sizeof(*entry->max_renew));
1311 if (entry->max_renew == NULL) {
1316 *entry->max_renew = kdc_db_ctx->policy.renewal_lifetime;
1318 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT && (flags & SDB_F_FOR_AS_REQ)) {
1320 struct auth_user_info_dc *user_info_dc = NULL;
1322 * These protections only apply to clients, so servers in the
1323 * Protected Users group may still have service tickets to them
1324 * encrypted with RC4. For accounts looked up as servers, note
1325 * that 'msg' does not contain the 'memberOf' attribute for
1326 * determining whether the account is a member of Protected
1329 * Additionally, Microsoft advises that accounts for services
1330 * and computers should never be members of Protected Users, or
1331 * they may fail to authenticate.
1333 status = samba_kdc_get_user_info_from_db(p, msg, &user_info_dc);
1334 if (!NT_STATUS_IS_OK(status)) {
1339 result = dsdb_is_protected_user(kdc_db_ctx->samdb,
1341 user_info_dc->num_sids);
1347 protected_user = result;
1349 if (protected_user) {
1350 *entry->max_life = MIN(*entry->max_life, 4 * 60 * 60);
1351 *entry->max_renew = MIN(*entry->max_renew, 4 * 60 * 60);
1353 entry->flags.forwardable = 0;
1354 entry->flags.proxiable = 0;
1358 if (rid == DOMAIN_RID_KRBTGT || is_rodc) {
1363 /* KDCs (and KDCs on RODCs) use AES */
1364 supported_enctypes |= ENC_HMAC_SHA1_96_AES128 | ENC_HMAC_SHA1_96_AES256;
1366 enable_fast = lpcfg_kdc_enable_fast(kdc_db_ctx->lp_ctx);
1368 supported_enctypes |= ENC_FAST_SUPPORTED;
1370 } else if (userAccountControl & (UF_PARTIAL_SECRETS_ACCOUNT|UF_SERVER_TRUST_ACCOUNT)) {
1371 /* DCs and RODCs comptuer accounts use AES */
1372 supported_enctypes |= ENC_HMAC_SHA1_96_AES128 | ENC_HMAC_SHA1_96_AES256;
1373 } else if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT ||
1374 (ent_type == SAMBA_KDC_ENT_TYPE_ANY)) {
1375 /* for AS-REQ the client chooses the enc types it
1376 * supports, and this will vary between computers a
1377 * user logs in from.
1379 * likewise for 'any' return as much as is supported,
1380 * to export into a keytab */
1381 supported_enctypes = ENC_ALL_TYPES;
1384 /* If UF_USE_DES_KEY_ONLY has been set, then don't allow use of the newer enc types */
1385 if (userAccountControl & UF_USE_DES_KEY_ONLY) {
1386 supported_enctypes = 0;
1388 /* Otherwise, add in the default enc types */
1389 supported_enctypes |= ENC_RC4_HMAC_MD5;
1392 if (protected_user) {
1393 supported_enctypes &= ~ENC_RC4_HMAC_MD5;
1396 /* Get keys from the db */
1397 ret = samba_kdc_message2entry_keys(context, p, msg,
1400 ent_type, flags, kvno, entry,
1402 &supported_enctypes);
1404 /* Could be bogus data in the entry, or out of memory */
1408 if (entry->keys.len == 0) {
1409 if (kdc_db_ctx->rodc) {
1411 * We are on an RODC, but don't have keys for this
1412 * account. Signal this to the caller
1414 auth_sam_trigger_repl_secret(kdc_db_ctx,
1415 kdc_db_ctx->msg_ctx,
1418 return SDB_ERR_NOT_FOUND_HERE;
1422 * oh, no password. Apparently (comment in
1423 * hdb-ldap.c) this violates the ASN.1, but this
1424 * allows an entry with no keys (yet).
1428 p->msg = talloc_steal(p, msg);
1429 p->supported_enctypes = supported_enctypes;
1433 /* This doesn't free ent itself, that is for the eventual caller to do */
1434 sdb_entry_free(entry);
1436 talloc_steal(kdc_db_ctx, p);
1443 * Construct an hdb_entry from a directory entry.
1444 * The kvno is what the remote client asked for
1446 static krb5_error_code samba_kdc_trust_message2entry(krb5_context context,
1447 struct samba_kdc_db_context *kdc_db_ctx,
1448 TALLOC_CTX *mem_ctx,
1449 enum trust_direction direction,
1450 struct ldb_dn *realm_dn,
1453 struct ldb_message *msg,
1454 struct sdb_entry *entry)
1456 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
1457 const char *our_realm = lpcfg_realm(lp_ctx);
1458 char *partner_realm = NULL;
1459 const char *realm = NULL;
1460 const char *krbtgt_realm = NULL;
1461 DATA_BLOB password_utf16 = data_blob_null;
1462 DATA_BLOB password_utf8 = data_blob_null;
1463 struct samr_Password _password_hash;
1464 const struct samr_Password *password_hash = NULL;
1465 const struct ldb_val *password_val;
1466 struct trustAuthInOutBlob password_blob;
1467 struct samba_kdc_entry *p;
1468 bool use_previous = false;
1469 uint32_t current_kvno;
1470 uint32_t previous_kvno;
1471 uint32_t num_keys = 0;
1472 enum ndr_err_code ndr_err;
1475 struct AuthenticationInformationArray *auth_array;
1478 uint32_t *auth_kvno;
1479 bool preferr_current = false;
1480 uint32_t supported_enctypes = ENC_RC4_HMAC_MD5;
1481 struct lsa_TrustDomainInfoInfoEx *tdo = NULL;
1484 ZERO_STRUCTP(entry);
1486 if (dsdb_functional_level(kdc_db_ctx->samdb) >= DS_DOMAIN_FUNCTION_2008) {
1487 supported_enctypes = ldb_msg_find_attr_as_uint(msg,
1488 "msDS-SupportedEncryptionTypes",
1489 supported_enctypes);
1492 status = dsdb_trust_parse_tdo_info(mem_ctx, msg, &tdo);
1493 if (!NT_STATUS_IS_OK(status)) {
1494 krb5_clear_error_message(context);
1499 if (!(tdo->trust_direction & direction)) {
1500 krb5_clear_error_message(context);
1501 ret = SDB_ERR_NOENTRY;
1505 if (tdo->trust_type != LSA_TRUST_TYPE_UPLEVEL) {
1507 * Only UPLEVEL domains support kerberos here,
1508 * as we don't support LSA_TRUST_TYPE_MIT.
1510 krb5_clear_error_message(context);
1511 ret = SDB_ERR_NOENTRY;
1515 if (tdo->trust_attributes & LSA_TRUST_ATTRIBUTE_CROSS_ORGANIZATION) {
1517 * We don't support selective authentication yet.
1519 krb5_clear_error_message(context);
1520 ret = SDB_ERR_NOENTRY;
1524 if (tdo->domain_name.string == NULL) {
1525 krb5_clear_error_message(context);
1526 ret = SDB_ERR_NOENTRY;
1529 partner_realm = strupper_talloc(mem_ctx, tdo->domain_name.string);
1530 if (partner_realm == NULL) {
1531 krb5_clear_error_message(context);
1536 if (direction == INBOUND) {
1538 krbtgt_realm = partner_realm;
1540 password_val = ldb_msg_find_ldb_val(msg, "trustAuthIncoming");
1541 } else { /* OUTBOUND */
1542 realm = partner_realm;
1543 krbtgt_realm = our_realm;
1545 password_val = ldb_msg_find_ldb_val(msg, "trustAuthOutgoing");
1548 if (password_val == NULL) {
1549 krb5_clear_error_message(context);
1550 ret = SDB_ERR_NOENTRY;
1554 ndr_err = ndr_pull_struct_blob(password_val, mem_ctx, &password_blob,
1555 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
1556 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1557 krb5_clear_error_message(context);
1562 p = talloc_zero(mem_ctx, struct samba_kdc_entry);
1569 p->kdc_db_ctx = kdc_db_ctx;
1570 p->realm_dn = realm_dn;
1571 p->supported_enctypes = supported_enctypes;
1573 talloc_set_destructor(p, samba_kdc_entry_destructor);
1575 entry->skdc_entry = p;
1577 /* use 'whenCreated' */
1578 entry->created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
1579 /* use 'kadmin' for now (needed by mit_samba) */
1580 ret = smb_krb5_make_principal(context,
1581 &entry->created_by.principal,
1582 realm, "kadmin", NULL);
1584 krb5_clear_error_message(context);
1589 * We always need to generate the canonicalized principal
1590 * with the values of our database.
1592 ret = smb_krb5_make_principal(context, &entry->principal, realm,
1593 "krbtgt", krbtgt_realm, NULL);
1595 krb5_clear_error_message(context);
1598 smb_krb5_principal_set_type(context, entry->principal,
1601 entry->valid_start = NULL;
1603 /* we need to work out if we are going to use the current or
1604 * the previous password hash.
1605 * We base this on the kvno the client passes in. If the kvno
1606 * passed in is equal to the current kvno in our database then
1607 * we use the current structure. If it is the current kvno-1,
1608 * then we use the previous substrucure.
1612 * Windows preferrs the previous key for one hour.
1614 tv = timeval_current();
1615 if (tv.tv_sec > 3600) {
1618 an_hour_ago = timeval_to_nttime(&tv);
1620 /* first work out the current kvno */
1622 for (i=0; i < password_blob.count; i++) {
1623 struct AuthenticationInformation *a =
1624 &password_blob.current.array[i];
1626 if (a->LastUpdateTime <= an_hour_ago) {
1627 preferr_current = true;
1630 if (a->AuthType == TRUST_AUTH_TYPE_VERSION) {
1631 current_kvno = a->AuthInfo.version.version;
1634 if (current_kvno == 0) {
1635 previous_kvno = 255;
1637 previous_kvno = current_kvno - 1;
1639 for (i=0; i < password_blob.count; i++) {
1640 struct AuthenticationInformation *a =
1641 &password_blob.previous.array[i];
1643 if (a->AuthType == TRUST_AUTH_TYPE_VERSION) {
1644 previous_kvno = a->AuthInfo.version.version;
1648 /* work out whether we will use the previous or current
1650 if (password_blob.previous.count == 0) {
1651 /* there is no previous password */
1652 use_previous = false;
1653 } else if (!(flags & SDB_F_KVNO_SPECIFIED)) {
1655 * If not specified we use the lowest kvno
1656 * for the first hour after an update.
1658 if (preferr_current) {
1659 use_previous = false;
1660 } else if (previous_kvno < current_kvno) {
1661 use_previous = true;
1663 use_previous = false;
1665 } else if (kvno == current_kvno) {
1669 use_previous = false;
1670 } else if (kvno == previous_kvno) {
1674 use_previous = true;
1677 * Fallback to the current one for anything else
1679 use_previous = false;
1683 auth_array = &password_blob.previous;
1684 auth_kvno = &previous_kvno;
1686 auth_array = &password_blob.current;
1687 auth_kvno = ¤t_kvno;
1690 /* use the kvno the client specified, if available */
1691 if (flags & SDB_F_KVNO_SPECIFIED) {
1694 entry->kvno = *auth_kvno;
1697 for (i=0; i < auth_array->count; i++) {
1698 if (auth_array->array[i].AuthType == TRUST_AUTH_TYPE_CLEAR) {
1701 password_utf16 = data_blob_const(auth_array->array[i].AuthInfo.clear.password,
1702 auth_array->array[i].AuthInfo.clear.size);
1703 if (password_utf16.length == 0) {
1707 if (supported_enctypes & ENC_RC4_HMAC_MD5) {
1708 mdfour(_password_hash.hash, password_utf16.data, password_utf16.length);
1709 if (password_hash == NULL) {
1712 password_hash = &_password_hash;
1715 if (!(supported_enctypes & (ENC_HMAC_SHA1_96_AES128|ENC_HMAC_SHA1_96_AES256))) {
1719 ok = convert_string_talloc(mem_ctx,
1720 CH_UTF16MUNGED, CH_UTF8,
1721 password_utf16.data,
1722 password_utf16.length,
1723 (void *)&password_utf8.data,
1724 &password_utf8.length);
1726 krb5_clear_error_message(context);
1731 if (supported_enctypes & ENC_HMAC_SHA1_96_AES128) {
1734 if (supported_enctypes & ENC_HMAC_SHA1_96_AES256) {
1738 } else if (auth_array->array[i].AuthType == TRUST_AUTH_TYPE_NT4OWF) {
1739 if (supported_enctypes & ENC_RC4_HMAC_MD5) {
1740 password_hash = &auth_array->array[i].AuthInfo.nt4owf.password;
1746 /* Must have found a cleartext or MD4 password */
1747 if (num_keys == 0) {
1748 DEBUG(1,(__location__ ": no usable key found\n"));
1749 krb5_clear_error_message(context);
1750 ret = SDB_ERR_NOENTRY;
1754 entry->keys.val = calloc(num_keys, sizeof(struct sdb_key));
1755 if (entry->keys.val == NULL) {
1756 krb5_clear_error_message(context);
1761 if (password_utf8.length != 0) {
1762 struct sdb_key key = {};
1763 krb5_const_principal salt_principal = entry->principal;
1765 krb5_data cleartext_data;
1767 cleartext_data.data = discard_const_p(char, password_utf8.data);
1768 cleartext_data.length = password_utf8.length;
1770 ret = smb_krb5_get_pw_salt(context,
1777 if (supported_enctypes & ENC_HMAC_SHA1_96_AES256) {
1778 ret = smb_krb5_create_key_from_string(context,
1782 ENCTYPE_AES256_CTS_HMAC_SHA1_96,
1785 smb_krb5_free_data_contents(context, &salt);
1789 entry->keys.val[entry->keys.len] = key;
1793 if (supported_enctypes & ENC_HMAC_SHA1_96_AES128) {
1794 ret = smb_krb5_create_key_from_string(context,
1798 ENCTYPE_AES128_CTS_HMAC_SHA1_96,
1801 smb_krb5_free_data_contents(context, &salt);
1805 entry->keys.val[entry->keys.len] = key;
1809 smb_krb5_free_data_contents(context, &salt);
1812 if (password_hash != NULL) {
1813 struct sdb_key key = {};
1815 ret = smb_krb5_keyblock_init_contents(context,
1816 ENCTYPE_ARCFOUR_HMAC,
1817 password_hash->hash,
1818 sizeof(password_hash->hash),
1824 entry->keys.val[entry->keys.len] = key;
1828 entry->flags = int2SDBFlags(0);
1829 entry->flags.immutable = 1;
1830 entry->flags.invalid = 0;
1831 entry->flags.server = 1;
1832 entry->flags.require_preauth = 1;
1834 entry->pw_end = NULL;
1836 entry->max_life = NULL;
1838 entry->max_renew = NULL;
1840 /* Match Windows behavior and allow forwardable flag in cross-realm. */
1841 entry->flags.forwardable = 1;
1843 samba_kdc_sort_keys(&entry->keys);
1845 p->msg = talloc_steal(p, msg);
1848 TALLOC_FREE(partner_realm);
1851 /* This doesn't free ent itself, that is for the eventual caller to do */
1852 sdb_entry_free(entry);
1854 talloc_steal(kdc_db_ctx, p);
1861 static krb5_error_code samba_kdc_lookup_trust(krb5_context context, struct ldb_context *ldb_ctx,
1862 TALLOC_CTX *mem_ctx,
1864 struct ldb_dn *realm_dn,
1865 struct ldb_message **pmsg)
1868 const char * const *attrs = trust_attrs;
1870 status = dsdb_trust_search_tdo(ldb_ctx, realm, realm,
1871 attrs, mem_ctx, pmsg);
1872 if (NT_STATUS_IS_OK(status)) {
1874 } else if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1875 return SDB_ERR_NOENTRY;
1876 } else if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1878 krb5_set_error_message(context, ret, "get_sam_result_trust: out of memory");
1882 krb5_set_error_message(context, ret, "get_sam_result_trust: %s", nt_errstr(status));
1887 static krb5_error_code samba_kdc_lookup_client(krb5_context context,
1888 struct samba_kdc_db_context *kdc_db_ctx,
1889 TALLOC_CTX *mem_ctx,
1890 krb5_const_principal principal,
1892 struct ldb_dn **realm_dn,
1893 struct ldb_message **msg)
1896 char *principal_string = NULL;
1898 if (smb_krb5_principal_get_type(context, principal) == KRB5_NT_ENTERPRISE_PRINCIPAL) {
1899 principal_string = smb_krb5_principal_get_comp_string(mem_ctx, context,
1901 if (principal_string == NULL) {
1905 char *principal_string_m = NULL;
1906 krb5_error_code ret;
1908 ret = krb5_unparse_name(context, principal, &principal_string_m);
1913 principal_string = talloc_strdup(mem_ctx, principal_string_m);
1914 SAFE_FREE(principal_string_m);
1915 if (principal_string == NULL) {
1920 nt_status = sam_get_results_principal(kdc_db_ctx->samdb,
1921 mem_ctx, principal_string, attrs,
1923 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
1924 krb5_principal fallback_principal = NULL;
1925 unsigned int num_comp;
1926 char *fallback_realm = NULL;
1927 char *fallback_account = NULL;
1928 krb5_error_code ret;
1930 ret = krb5_parse_name(context, principal_string,
1931 &fallback_principal);
1932 TALLOC_FREE(principal_string);
1937 num_comp = krb5_princ_size(context, fallback_principal);
1938 fallback_realm = smb_krb5_principal_get_realm(
1939 mem_ctx, context, fallback_principal);
1940 if (fallback_realm == NULL) {
1941 krb5_free_principal(context, fallback_principal);
1945 if (num_comp == 1) {
1948 fallback_account = smb_krb5_principal_get_comp_string(mem_ctx,
1949 context, fallback_principal, 0);
1950 if (fallback_account == NULL) {
1951 krb5_free_principal(context, fallback_principal);
1952 TALLOC_FREE(fallback_realm);
1956 len = strlen(fallback_account);
1957 if (len >= 2 && fallback_account[len - 1] == '$') {
1958 TALLOC_FREE(fallback_account);
1961 krb5_free_principal(context, fallback_principal);
1962 fallback_principal = NULL;
1964 if (fallback_account != NULL) {
1967 with_dollar = talloc_asprintf(mem_ctx, "%s$",
1969 if (with_dollar == NULL) {
1970 TALLOC_FREE(fallback_realm);
1973 TALLOC_FREE(fallback_account);
1975 ret = smb_krb5_make_principal(context,
1976 &fallback_principal,
1979 TALLOC_FREE(with_dollar);
1981 TALLOC_FREE(fallback_realm);
1985 TALLOC_FREE(fallback_realm);
1987 if (fallback_principal != NULL) {
1988 char *fallback_string = NULL;
1990 ret = krb5_unparse_name(context,
1994 krb5_free_principal(context, fallback_principal);
1998 nt_status = sam_get_results_principal(kdc_db_ctx->samdb,
2003 SAFE_FREE(fallback_string);
2005 krb5_free_principal(context, fallback_principal);
2006 fallback_principal = NULL;
2008 TALLOC_FREE(principal_string);
2010 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
2011 return SDB_ERR_NOENTRY;
2012 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
2014 } else if (!NT_STATUS_IS_OK(nt_status)) {
2021 static krb5_error_code samba_kdc_fetch_client(krb5_context context,
2022 struct samba_kdc_db_context *kdc_db_ctx,
2023 TALLOC_CTX *mem_ctx,
2024 krb5_const_principal principal,
2027 struct sdb_entry *entry)
2029 struct ldb_dn *realm_dn;
2030 krb5_error_code ret;
2031 struct ldb_message *msg = NULL;
2033 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
2034 mem_ctx, principal, user_attrs,
2040 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
2041 principal, SAMBA_KDC_ENT_TYPE_CLIENT,
2043 realm_dn, msg, entry);
2047 static krb5_error_code samba_kdc_fetch_krbtgt(krb5_context context,
2048 struct samba_kdc_db_context *kdc_db_ctx,
2049 TALLOC_CTX *mem_ctx,
2050 krb5_const_principal principal,
2053 struct sdb_entry *entry)
2055 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
2056 krb5_error_code ret;
2057 struct ldb_message *msg = NULL;
2058 struct ldb_dn *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
2059 char *realm_from_princ;
2060 char *realm_princ_comp = smb_krb5_principal_get_comp_string(mem_ctx, context, principal, 1);
2062 realm_from_princ = smb_krb5_principal_get_realm(
2063 mem_ctx, context, principal);
2064 if (realm_from_princ == NULL) {
2066 return SDB_ERR_NOENTRY;
2069 if (krb5_princ_size(context, principal) != 2
2070 || (principal_comp_strcmp(context, principal, 0, KRB5_TGS_NAME) != 0)) {
2072 return SDB_ERR_NOENTRY;
2075 /* krbtgt case. Either us or a trusted realm */
2077 if (lpcfg_is_my_domain_or_realm(lp_ctx, realm_from_princ)
2078 && lpcfg_is_my_domain_or_realm(lp_ctx, realm_princ_comp)) {
2079 /* us, or someone quite like us */
2080 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
2081 * is in our db, then direct the caller at our primary
2085 unsigned int krbtgt_number;
2086 /* w2k8r2 sometimes gives us a kvno of 255 for inter-domain
2087 trust tickets. We don't yet know what this means, but we do
2088 seem to need to treat it as unspecified */
2089 if (flags & SDB_F_KVNO_SPECIFIED) {
2090 krbtgt_number = SAMBA_KVNO_GET_KRBTGT(kvno);
2091 if (kdc_db_ctx->rodc) {
2092 if (krbtgt_number != kdc_db_ctx->my_krbtgt_number) {
2093 return SDB_ERR_NOT_FOUND_HERE;
2097 krbtgt_number = kdc_db_ctx->my_krbtgt_number;
2100 if (krbtgt_number == kdc_db_ctx->my_krbtgt_number) {
2101 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
2102 &msg, kdc_db_ctx->krbtgt_dn, LDB_SCOPE_BASE,
2103 krbtgt_attrs, DSDB_SEARCH_NO_GLOBAL_CATALOG,
2104 "(objectClass=user)");
2106 /* We need to look up an RODC krbtgt (perhaps
2107 * ours, if we are an RODC, perhaps another
2108 * RODC if we are a read-write DC */
2109 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
2110 &msg, realm_dn, LDB_SCOPE_SUBTREE,
2112 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
2113 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=%u))", (unsigned)(krbtgt_number));
2116 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
2117 krb5_warnx(context, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
2118 (unsigned)(krbtgt_number));
2119 krb5_set_error_message(context, SDB_ERR_NOENTRY,
2120 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
2121 (unsigned)(krbtgt_number));
2122 return SDB_ERR_NOENTRY;
2123 } else if (lret != LDB_SUCCESS) {
2124 krb5_warnx(context, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
2125 (unsigned)(krbtgt_number));
2126 krb5_set_error_message(context, SDB_ERR_NOENTRY,
2127 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
2128 (unsigned)(krbtgt_number));
2129 return SDB_ERR_NOENTRY;
2132 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
2133 principal, SAMBA_KDC_ENT_TYPE_KRBTGT,
2134 flags, kvno, realm_dn, msg, entry);
2136 krb5_warnx(context, "samba_kdc_fetch: self krbtgt message2entry failed");
2141 enum trust_direction direction = UNKNOWN;
2142 const char *realm = NULL;
2144 /* Either an inbound or outbound trust */
2146 if (strcasecmp(lpcfg_realm(lp_ctx), realm_from_princ) == 0) {
2147 /* look for inbound trust */
2148 direction = INBOUND;
2149 realm = realm_princ_comp;
2150 } else if (principal_comp_strcasecmp(context, principal, 1, lpcfg_realm(lp_ctx)) == 0) {
2151 /* look for outbound trust */
2152 direction = OUTBOUND;
2153 realm = realm_from_princ;
2155 krb5_warnx(context, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
2158 krb5_set_error_message(context, SDB_ERR_NOENTRY, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
2161 return SDB_ERR_NOENTRY;
2164 /* Trusted domains are under CN=system */
2166 ret = samba_kdc_lookup_trust(context, kdc_db_ctx->samdb,
2168 realm, realm_dn, &msg);
2171 krb5_warnx(context, "samba_kdc_fetch: could not find principal in DB");
2172 krb5_set_error_message(context, ret, "samba_kdc_fetch: could not find principal in DB");
2176 ret = samba_kdc_trust_message2entry(context, kdc_db_ctx, mem_ctx,
2178 realm_dn, flags, kvno, msg, entry);
2180 krb5_warnx(context, "samba_kdc_fetch: trust_message2entry failed for %s",
2181 ldb_dn_get_linearized(msg->dn));
2182 krb5_set_error_message(context, ret, "samba_kdc_fetch: "
2183 "trust_message2entry failed for %s",
2184 ldb_dn_get_linearized(msg->dn));
2191 static krb5_error_code samba_kdc_lookup_server(krb5_context context,
2192 struct samba_kdc_db_context *kdc_db_ctx,
2193 TALLOC_CTX *mem_ctx,
2194 krb5_const_principal principal,
2197 struct ldb_dn **realm_dn,
2198 struct ldb_message **msg)
2200 krb5_error_code ret;
2201 if ((smb_krb5_principal_get_type(context, principal) != KRB5_NT_ENTERPRISE_PRINCIPAL)
2202 && krb5_princ_size(context, principal) >= 2) {
2203 /* 'normal server' case */
2206 struct ldb_dn *user_dn;
2207 char *principal_string;
2209 ret = krb5_unparse_name_flags(context, principal,
2210 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
2216 /* At this point we may find the host is known to be
2217 * in a different realm, so we should generate a
2218 * referral instead */
2219 nt_status = crack_service_principal_name(kdc_db_ctx->samdb,
2220 mem_ctx, principal_string,
2221 &user_dn, realm_dn);
2222 free(principal_string);
2224 if (!NT_STATUS_IS_OK(nt_status)) {
2225 return SDB_ERR_NOENTRY;
2228 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb,
2230 msg, user_dn, LDB_SCOPE_BASE,
2232 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
2234 if (ldb_ret != LDB_SUCCESS) {
2235 return SDB_ERR_NOENTRY;
2238 } else if (!(flags & SDB_F_FOR_AS_REQ)
2239 && smb_krb5_principal_get_type(context, principal) == KRB5_NT_ENTERPRISE_PRINCIPAL) {
2241 * The behaviour of accepting an
2242 * KRB5_NT_ENTERPRISE_PRINCIPAL server principal
2243 * containing a UPN only applies to TGS-REQ packets,
2244 * not AS-REQ packets.
2246 return samba_kdc_lookup_client(context, kdc_db_ctx,
2247 mem_ctx, principal, attrs,
2252 * - the AS-REQ, where we only accept
2253 * samAccountName based lookups for the server, no
2254 * matter if the name is an
2255 * KRB5_NT_ENTERPRISE_PRINCIPAL or not
2256 * - for the TGS-REQ when we are not given an
2257 * KRB5_NT_ENTERPRISE_PRINCIPAL, which also must
2258 * only lookup samAccountName based names.
2262 krb5_principal enterprise_principal = NULL;
2263 krb5_const_principal used_principal = NULL;
2266 char *filter = NULL;
2268 if (smb_krb5_principal_get_type(context, principal) == KRB5_NT_ENTERPRISE_PRINCIPAL) {
2270 /* Need to reparse the enterprise principal to find the real target */
2271 if (krb5_princ_size(context, principal) != 1) {
2272 ret = KRB5_PARSE_MALFORMED;
2273 krb5_set_error_message(context, ret, "samba_kdc_lookup_server: request for an "
2274 "enterprise principal with wrong (%d) number of components",
2275 krb5_princ_size(context, principal));
2278 str = smb_krb5_principal_get_comp_string(mem_ctx, context, principal, 0);
2280 return KRB5_PARSE_MALFORMED;
2282 ret = krb5_parse_name(context, str,
2283 &enterprise_principal);
2288 used_principal = enterprise_principal;
2290 used_principal = principal;
2293 /* server as client principal case, but we must not lookup userPrincipalNames */
2294 *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
2296 /* TODO: Check if it is our realm, otherwise give referral */
2298 ret = krb5_unparse_name_flags(context, used_principal,
2299 KRB5_PRINCIPAL_UNPARSE_NO_REALM |
2300 KRB5_PRINCIPAL_UNPARSE_DISPLAY,
2302 used_principal = NULL;
2303 krb5_free_principal(context, enterprise_principal);
2304 enterprise_principal = NULL;
2307 krb5_set_error_message(context, ret, "samba_kdc_lookup_principal: could not parse principal");
2308 krb5_warnx(context, "samba_kdc_lookup_principal: could not parse principal");
2312 name1 = ldb_binary_encode_string(mem_ctx, short_princ);
2313 SAFE_FREE(short_princ);
2314 if (name1 == NULL) {
2317 len1 = strlen(name1);
2318 if (len1 >= 1 && name1[len1 - 1] != '$') {
2319 filter = talloc_asprintf(mem_ctx,
2320 "(&(objectClass=user)(|(samAccountName=%s)(samAccountName=%s$)))",
2322 if (filter == NULL) {
2326 filter = talloc_asprintf(mem_ctx,
2327 "(&(objectClass=user)(samAccountName=%s))",
2329 if (filter == NULL) {
2334 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx, msg,
2335 *realm_dn, LDB_SCOPE_SUBTREE,
2337 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
2339 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
2340 DEBUG(10, ("Failed to find an entry for %s filter:%s\n",
2342 return SDB_ERR_NOENTRY;
2344 if (lret == LDB_ERR_CONSTRAINT_VIOLATION) {
2345 DEBUG(10, ("Failed to find unique entry for %s filter:%s\n",
2347 return SDB_ERR_NOENTRY;
2349 if (lret != LDB_SUCCESS) {
2350 DEBUG(0, ("Failed single search for %s - %s\n",
2351 name1, ldb_errstring(kdc_db_ctx->samdb)));
2352 return SDB_ERR_NOENTRY;
2356 return SDB_ERR_NOENTRY;
2361 static krb5_error_code samba_kdc_fetch_server(krb5_context context,
2362 struct samba_kdc_db_context *kdc_db_ctx,
2363 TALLOC_CTX *mem_ctx,
2364 krb5_const_principal principal,
2367 struct sdb_entry *entry)
2369 krb5_error_code ret;
2370 struct ldb_dn *realm_dn;
2371 struct ldb_message *msg;
2373 ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, principal,
2374 flags, server_attrs, &realm_dn, &msg);
2379 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
2380 principal, SAMBA_KDC_ENT_TYPE_SERVER,
2382 realm_dn, msg, entry);
2384 krb5_warnx(context, "samba_kdc_fetch: message2entry failed");
2390 static krb5_error_code samba_kdc_lookup_realm(krb5_context context,
2391 struct samba_kdc_db_context *kdc_db_ctx,
2392 TALLOC_CTX *mem_ctx,
2393 krb5_const_principal principal,
2395 struct sdb_entry *entry)
2397 TALLOC_CTX *frame = talloc_stackframe();
2399 krb5_error_code ret;
2400 bool check_realm = false;
2401 const char *realm = NULL;
2402 struct dsdb_trust_routing_table *trt = NULL;
2403 const struct lsa_TrustDomainInfoInfoEx *tdo = NULL;
2404 unsigned int num_comp;
2408 num_comp = krb5_princ_size(context, principal);
2410 if (flags & SDB_F_GET_CLIENT) {
2411 if (flags & SDB_F_FOR_AS_REQ) {
2415 if (flags & SDB_F_GET_SERVER) {
2416 if (flags & SDB_F_FOR_TGS_REQ) {
2426 realm = smb_krb5_principal_get_realm(frame, context, principal);
2427 if (realm == NULL) {
2433 * The requested realm needs to be our own
2435 ok = lpcfg_is_my_domain_or_realm(kdc_db_ctx->lp_ctx, realm);
2438 * The request is not for us...
2441 return SDB_ERR_NOENTRY;
2444 if (smb_krb5_principal_get_type(context, principal) == KRB5_NT_ENTERPRISE_PRINCIPAL) {
2445 char *principal_string = NULL;
2446 krb5_principal enterprise_principal = NULL;
2447 char *enterprise_realm = NULL;
2449 if (num_comp != 1) {
2451 return SDB_ERR_NOENTRY;
2454 principal_string = smb_krb5_principal_get_comp_string(frame, context,
2456 if (principal_string == NULL) {
2461 ret = krb5_parse_name(context, principal_string,
2462 &enterprise_principal);
2463 TALLOC_FREE(principal_string);
2469 enterprise_realm = smb_krb5_principal_get_realm(
2470 frame, context, enterprise_principal);
2471 krb5_free_principal(context, enterprise_principal);
2472 if (enterprise_realm != NULL) {
2473 realm = enterprise_realm;
2477 if (flags & SDB_F_GET_SERVER) {
2478 char *service_realm = NULL;
2480 ret = principal_comp_strcmp(context, principal, 0, KRB5_TGS_NAME);
2483 * we need to search krbtgt/ locally
2490 * We need to check the last component against the routing table.
2492 * Note this works only with 2 or 3 component principals, e.g:
2494 * servicePrincipalName: ldap/W2K8R2-219.bla.base
2495 * servicePrincipalName: ldap/W2K8R2-219.bla.base/bla.base
2496 * servicePrincipalName: ldap/W2K8R2-219.bla.base/ForestDnsZones.bla.base
2497 * servicePrincipalName: ldap/W2K8R2-219.bla.base/DomainDnsZones.bla.base
2500 if (num_comp == 2 || num_comp == 3) {
2501 service_realm = smb_krb5_principal_get_comp_string(frame,
2507 if (service_realm != NULL) {
2508 realm = service_realm;
2512 ok = lpcfg_is_my_domain_or_realm(kdc_db_ctx->lp_ctx, realm);
2515 * skip the expensive routing lookup
2521 status = dsdb_trust_routing_table_load(kdc_db_ctx->samdb,
2523 if (!NT_STATUS_IS_OK(status)) {
2528 tdo = dsdb_trust_routing_by_name(trt, realm);
2531 * This principal has to be local
2537 if (tdo->trust_attributes & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST) {
2539 * TODO: handle the routing within the forest
2541 * This should likely be handled in
2542 * samba_kdc_message2entry() in case we're
2543 * a global catalog. We'd need to check
2544 * if realm_dn is our own domain and derive
2545 * the dns domain name from realm_dn and check that
2546 * against the routing table or fallback to
2547 * the tdo we found here.
2549 * But for now we don't support multiple domains
2550 * in our forest correctly anyway.
2552 * Just search in our local database.
2558 ZERO_STRUCTP(entry);
2560 ret = krb5_copy_principal(context, principal,
2567 upper = strupper_talloc(frame, tdo->domain_name.string);
2568 if (upper == NULL) {
2573 ret = smb_krb5_principal_set_realm(context,
2582 return SDB_ERR_WRONG_REALM;
2585 krb5_error_code samba_kdc_fetch(krb5_context context,
2586 struct samba_kdc_db_context *kdc_db_ctx,
2587 krb5_const_principal principal,
2590 struct sdb_entry *entry)
2592 krb5_error_code ret = SDB_ERR_NOENTRY;
2593 TALLOC_CTX *mem_ctx;
2595 mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_fetch context");
2598 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
2602 ret = samba_kdc_lookup_realm(context, kdc_db_ctx, mem_ctx,
2603 principal, flags, entry);
2608 ret = SDB_ERR_NOENTRY;
2610 if (flags & SDB_F_GET_CLIENT) {
2611 ret = samba_kdc_fetch_client(context, kdc_db_ctx, mem_ctx, principal, flags, kvno, entry);
2612 if (ret != SDB_ERR_NOENTRY) goto done;
2614 if (flags & SDB_F_GET_SERVER) {
2615 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
2616 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, flags, kvno, entry);
2617 if (ret != SDB_ERR_NOENTRY) goto done;
2619 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
2620 ret = samba_kdc_fetch_server(context, kdc_db_ctx, mem_ctx, principal, flags, kvno, entry);
2621 if (ret != SDB_ERR_NOENTRY) goto done;
2623 if (flags & SDB_F_GET_KRBTGT) {
2624 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, flags, kvno, entry);
2625 if (ret != SDB_ERR_NOENTRY) goto done;
2629 talloc_free(mem_ctx);
2633 struct samba_kdc_seq {
2636 struct ldb_message **msgs;
2637 struct ldb_dn *realm_dn;
2640 static krb5_error_code samba_kdc_seq(krb5_context context,
2641 struct samba_kdc_db_context *kdc_db_ctx,
2642 struct sdb_entry *entry)
2644 krb5_error_code ret;
2645 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
2646 const char *realm = lpcfg_realm(kdc_db_ctx->lp_ctx);
2647 struct ldb_message *msg = NULL;
2648 const char *sAMAccountName = NULL;
2649 krb5_principal principal = NULL;
2650 TALLOC_CTX *mem_ctx;
2653 return SDB_ERR_NOENTRY;
2656 mem_ctx = talloc_named(priv, 0, "samba_kdc_seq context");
2660 krb5_set_error_message(context, ret, "samba_kdc_seq: talloc_named() failed!");
2664 while (priv->index < priv->count) {
2665 msg = priv->msgs[priv->index++];
2667 sAMAccountName = ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL);
2668 if (sAMAccountName != NULL) {
2673 if (sAMAccountName == NULL) {
2674 ret = SDB_ERR_NOENTRY;
2678 ret = smb_krb5_make_principal(context, &principal,
2679 realm, sAMAccountName, NULL);
2684 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
2685 principal, SAMBA_KDC_ENT_TYPE_ANY,
2686 SDB_F_ADMIN_DATA|SDB_F_GET_ANY,
2688 priv->realm_dn, msg, entry);
2691 if (principal != NULL) {
2692 krb5_free_principal(context, principal);
2697 kdc_db_ctx->seq_ctx = NULL;
2699 talloc_free(mem_ctx);
2705 krb5_error_code samba_kdc_firstkey(krb5_context context,
2706 struct samba_kdc_db_context *kdc_db_ctx,
2707 struct sdb_entry *entry)
2709 struct ldb_context *ldb_ctx = kdc_db_ctx->samdb;
2710 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
2712 struct ldb_result *res = NULL;
2713 krb5_error_code ret;
2714 TALLOC_CTX *mem_ctx;
2719 kdc_db_ctx->seq_ctx = NULL;
2722 priv = (struct samba_kdc_seq *) talloc(kdc_db_ctx, struct samba_kdc_seq);
2725 krb5_set_error_message(context, ret, "talloc: out of memory");
2731 priv->realm_dn = ldb_get_default_basedn(ldb_ctx);
2734 mem_ctx = talloc_named(priv, 0, "samba_kdc_firstkey context");
2738 krb5_set_error_message(context, ret, "samba_kdc_firstkey: talloc_named() failed!");
2742 ret = krb5_get_default_realm(context, &realm);
2747 krb5_free_default_realm(context, realm);
2749 lret = dsdb_search(ldb_ctx, priv, &res,
2750 priv->realm_dn, LDB_SCOPE_SUBTREE, user_attrs,
2751 DSDB_SEARCH_NO_GLOBAL_CATALOG,
2752 "(objectClass=user)");
2754 if (lret != LDB_SUCCESS) {
2756 return SDB_ERR_NOENTRY;
2759 priv->count = res->count;
2760 priv->msgs = talloc_steal(priv, res->msgs);
2763 kdc_db_ctx->seq_ctx = priv;
2765 ret = samba_kdc_seq(context, kdc_db_ctx, entry);
2769 kdc_db_ctx->seq_ctx = NULL;
2771 talloc_free(mem_ctx);
2776 krb5_error_code samba_kdc_nextkey(krb5_context context,
2777 struct samba_kdc_db_context *kdc_db_ctx,
2778 struct sdb_entry *entry)
2780 return samba_kdc_seq(context, kdc_db_ctx, entry);
2783 /* Check if a given entry may delegate or do s4u2self to this target principal
2785 * The safest way to determine 'self' is to check the DB record made at
2786 * the time the principal was presented to the KDC.
2789 samba_kdc_check_client_matches_target_service(krb5_context context,
2790 struct samba_kdc_entry *skdc_entry_client,
2791 struct samba_kdc_entry *skdc_entry_server_target)
2793 struct dom_sid *orig_sid;
2794 struct dom_sid *target_sid;
2795 TALLOC_CTX *frame = talloc_stackframe();
2797 orig_sid = samdb_result_dom_sid(frame,
2798 skdc_entry_client->msg,
2800 target_sid = samdb_result_dom_sid(frame,
2801 skdc_entry_server_target->msg,
2805 * Allow delegation to the same record (representing a
2806 * principal), even if by a different name. The easy and safe
2807 * way to prove this is by SID comparison
2809 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
2811 return KRB5KRB_AP_ERR_BADMATCH;
2818 /* Certificates printed by a the Certificate Authority might have a
2819 * slightly different form of the user principal name to that in the
2820 * database. Allow a mismatch where they both refer to the same
2824 samba_kdc_check_pkinit_ms_upn_match(krb5_context context,
2825 struct samba_kdc_db_context *kdc_db_ctx,
2826 struct samba_kdc_entry *skdc_entry,
2827 krb5_const_principal certificate_principal)
2829 krb5_error_code ret;
2830 struct ldb_dn *realm_dn;
2831 struct ldb_message *msg;
2832 struct dom_sid *orig_sid;
2833 struct dom_sid *target_sid;
2834 const char *ms_upn_check_attrs[] = {
2838 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_pkinit_ms_upn_match");
2842 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
2846 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
2847 mem_ctx, certificate_principal,
2848 ms_upn_check_attrs, &realm_dn, &msg);
2851 talloc_free(mem_ctx);
2855 orig_sid = samdb_result_dom_sid(mem_ctx, skdc_entry->msg, "objectSid");
2856 target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
2858 /* Consider these to be the same principal, even if by a different
2859 * name. The easy and safe way to prove this is by SID
2861 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
2862 talloc_free(mem_ctx);
2863 #if defined(KRB5KDC_ERR_CLIENT_NAME_MISMATCH) /* MIT */
2864 return KRB5KDC_ERR_CLIENT_NAME_MISMATCH;
2865 #else /* Heimdal (where this is an enum) */
2866 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
2870 talloc_free(mem_ctx);
2875 * Check if a given entry may delegate to this target principal
2879 samba_kdc_check_s4u2proxy(krb5_context context,
2880 struct samba_kdc_db_context *kdc_db_ctx,
2881 struct samba_kdc_entry *skdc_entry,
2882 krb5_const_principal target_principal)
2884 krb5_error_code ret;
2886 const char *client_dn = NULL;
2887 const char *target_principal_name = NULL;
2888 struct ldb_message_element *el;
2893 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_s4u2proxy");
2897 krb5_set_error_message(context, ret,
2898 "samba_kdc_check_s4u2proxy:"
2899 " talloc_named() failed!");
2903 client_dn = ldb_dn_get_linearized(skdc_entry->msg->dn);
2909 krb5_set_error_message(context, ret,
2910 "samba_kdc_check_s4u2proxy:"
2911 " ldb_dn_get_linearized() failed!");
2915 el = ldb_msg_find_element(skdc_entry->msg, "msDS-AllowedToDelegateTo");
2920 SMB_ASSERT(el->num_values != 0);
2923 * This is the Microsoft forwardable flag behavior.
2925 * If the proxy (target) principal is NULL, and we have any authorized
2926 * delegation target, allow to forward.
2928 if (target_principal == NULL) {
2934 * The main heimdal code already checked that the target_principal
2935 * belongs to the same realm as the client.
2937 * So we just need the principal without the realm,
2938 * as that is what is configured in the "msDS-AllowedToDelegateTo"
2941 ret = krb5_unparse_name_flags(context, target_principal,
2942 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &tmp);
2944 talloc_free(mem_ctx);
2945 krb5_set_error_message(context, ret,
2946 "samba_kdc_check_s4u2proxy:"
2947 " krb5_unparse_name() failed!");
2950 DEBUG(10,("samba_kdc_check_s4u2proxy: client[%s] for target[%s]\n",
2953 target_principal_name = talloc_strdup(mem_ctx, tmp);
2955 if (target_principal_name == NULL) {
2957 krb5_set_error_message(context, ret,
2958 "samba_kdc_check_s4u2proxy:"
2959 " talloc_strdup() failed!");
2963 val = data_blob_string_const(target_principal_name);
2965 for (i=0; i<el->num_values; i++) {
2966 struct ldb_val *val1 = &val;
2967 struct ldb_val *val2 = &el->values[i];
2970 if (val1->length != val2->length) {
2974 cmp = strncasecmp((const char *)val1->data,
2975 (const char *)val2->data,
2990 DEBUG(10,("samba_kdc_check_s4u2proxy: client[%s] allowed target[%s]\n",
2992 talloc_free(mem_ctx);
2996 krb5_set_error_message(context, ret,
2997 "samba_kdc_check_s4u2proxy: client[%s] "
2998 "not allowed for delegation to target[%s]",
3000 target_principal_name);
3001 talloc_free(mem_ctx);
3002 return KRB5KDC_ERR_BADOPTION;
3006 * This method is called for S4U2Proxy requests and implements the
3007 * resource-based constrained delegation variant, which can support
3008 * cross-realm delegation.
3010 krb5_error_code samba_kdc_check_s4u2proxy_rbcd(
3011 krb5_context context,
3012 struct samba_kdc_db_context *kdc_db_ctx,
3013 krb5_const_principal client_principal,
3014 krb5_const_principal server_principal,
3015 krb5_pac header_pac,
3016 struct samba_kdc_entry *proxy_skdc_entry)
3018 krb5_error_code code;
3019 enum ndr_err_code ndr_err;
3020 char *client_name = NULL;
3021 char *server_name = NULL;
3022 const char *proxy_dn = NULL;
3023 const DATA_BLOB *data = NULL;
3024 struct security_descriptor *rbcd_security_descriptor = NULL;
3025 struct auth_user_info_dc *user_info_dc = NULL;
3026 struct auth_session_info *session_info = NULL;
3027 uint32_t session_info_flags = AUTH_SESSION_INFO_SIMPLE_PRIVILEGES;
3028 uint32_t access_desired = SEC_ADS_GENERIC_ALL; /* => 0x000f01ff */
3029 uint32_t access_granted = 0;
3031 TALLOC_CTX *mem_ctx = NULL;
3033 mem_ctx = talloc_named(kdc_db_ctx,
3035 "samba_kdc_check_s4u2proxy_rbcd");
3036 if (mem_ctx == NULL) {
3043 proxy_dn = ldb_dn_get_linearized(proxy_skdc_entry->msg->dn);
3044 if (proxy_dn == NULL) {
3045 DBG_ERR("ldb_dn_get_linearized failed for proxy_dn!\n");
3046 TALLOC_FREE(mem_ctx);
3055 rbcd_security_descriptor = talloc_zero(mem_ctx,
3056 struct security_descriptor);
3057 if (rbcd_security_descriptor == NULL) {
3064 code = krb5_unparse_name_flags(context,
3066 KRB5_PRINCIPAL_UNPARSE_DISPLAY,
3069 DBG_ERR("Unable to parse client_principal!\n");
3073 code = krb5_unparse_name_flags(context,
3075 KRB5_PRINCIPAL_UNPARSE_DISPLAY,
3078 DBG_ERR("Unable to parse server_principal!\n");
3079 SAFE_FREE(client_name);
3083 DBG_INFO("Check delegation from client[%s] to server[%s] via "
3089 code = kerberos_pac_to_user_info_dc(mem_ctx,
3099 if (user_info_dc->info->authenticated) {
3100 session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
3103 nt_status = auth_generate_session_info(mem_ctx,
3109 if (!NT_STATUS_IS_OK(nt_status)) {
3110 code = map_errno_from_nt_status(nt_status);
3114 data = ldb_msg_find_ldb_val(proxy_skdc_entry->msg,
3115 "msDS-AllowedToActOnBehalfOfOtherIdentity");
3117 DBG_ERR("Could not find security descriptor"
3118 "msDS-AllowedToActOnBehalfOfOtherIdentity in "
3121 code = KRB5KDC_ERR_BADOPTION;
3125 ndr_err = ndr_pull_struct_blob(
3128 rbcd_security_descriptor,
3129 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
3130 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3131 errno = ndr_map_error2errno(ndr_err);
3132 DBG_ERR("Failed to unmarshall "
3133 "msDS-AllowedToActOnBehalfOfOtherIdentity "
3134 "security descriptor of proxy[%s]\n",
3136 code = KRB5KDC_ERR_BADOPTION;
3140 if (DEBUGLEVEL >= 10) {
3141 NDR_PRINT_DEBUG(security_token, session_info->security_token);
3142 NDR_PRINT_DEBUG(security_descriptor, rbcd_security_descriptor);
3145 nt_status = sec_access_check_ds(rbcd_security_descriptor,
3146 session_info->security_token,
3152 if (!NT_STATUS_IS_OK(nt_status)) {
3153 DBG_WARNING("RBCD: sec_access_check_ds(access_desired=%#08x, "
3154 "access_granted:%#08x) failed with: %s\n",
3157 nt_errstr(nt_status));
3159 code = KRB5KDC_ERR_BADOPTION;
3163 DBG_NOTICE("RBCD: Access granted for client[%s]\n", client_name);
3167 SAFE_FREE(client_name);
3168 SAFE_FREE(server_name);
3170 TALLOC_FREE(mem_ctx);
3174 NTSTATUS samba_kdc_setup_db_ctx(TALLOC_CTX *mem_ctx, struct samba_kdc_base_context *base_ctx,
3175 struct samba_kdc_db_context **kdc_db_ctx_out)
3178 struct ldb_message *msg;
3179 struct auth_session_info *session_info;
3180 struct samba_kdc_db_context *kdc_db_ctx;
3181 /* The idea here is very simple. Using Kerberos to
3182 * authenticate the KDC to the LDAP server is higly likely to
3185 * In future we may set this up to use EXERNAL and SSL
3186 * certificates, for now it will almost certainly be NTLMSSP_SET_USERNAME
3189 kdc_db_ctx = talloc_zero(mem_ctx, struct samba_kdc_db_context);
3190 if (kdc_db_ctx == NULL) {
3191 return NT_STATUS_NO_MEMORY;
3193 kdc_db_ctx->ev_ctx = base_ctx->ev_ctx;
3194 kdc_db_ctx->lp_ctx = base_ctx->lp_ctx;
3195 kdc_db_ctx->msg_ctx = base_ctx->msg_ctx;
3197 /* get default kdc policy */
3198 lpcfg_default_kdc_policy(mem_ctx,
3200 &kdc_db_ctx->policy.svc_tkt_lifetime,
3201 &kdc_db_ctx->policy.usr_tkt_lifetime,
3202 &kdc_db_ctx->policy.renewal_lifetime);
3204 session_info = system_session(kdc_db_ctx->lp_ctx);
3205 if (session_info == NULL) {
3206 return NT_STATUS_INTERNAL_ERROR;
3209 /* Setup the link to secrets.ldb */
3211 kdc_db_ctx->secrets_db = secrets_db_connect(kdc_db_ctx,
3213 if (kdc_db_ctx->secrets_db == NULL) {
3214 DEBUG(1, ("samba_kdc_setup_db_ctx: "
3215 "Cannot open secrets.ldb for KDC backend!"));
3216 talloc_free(kdc_db_ctx);
3217 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3220 kdc_db_ctx->fx_cookie_dn = ldb_dn_new(kdc_db_ctx,
3221 kdc_db_ctx->secrets_db,
3224 /* Setup the link to LDB */
3225 kdc_db_ctx->samdb = samdb_connect(kdc_db_ctx,
3231 if (kdc_db_ctx->samdb == NULL) {
3232 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot open samdb for KDC backend!"));
3233 talloc_free(kdc_db_ctx);
3234 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3237 /* Find out our own krbtgt kvno */
3238 ldb_ret = samdb_rodc(kdc_db_ctx->samdb, &kdc_db_ctx->rodc);
3239 if (ldb_ret != LDB_SUCCESS) {
3240 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine if we are an RODC in KDC backend: %s\n",
3241 ldb_errstring(kdc_db_ctx->samdb)));
3242 talloc_free(kdc_db_ctx);
3243 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3245 if (kdc_db_ctx->rodc) {
3246 int my_krbtgt_number;
3247 const char *secondary_keytab[] = { "msDS-SecondaryKrbTgtNumber", NULL };
3248 struct ldb_dn *account_dn;
3249 struct ldb_dn *server_dn = samdb_server_dn(kdc_db_ctx->samdb, kdc_db_ctx);
3251 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine server DN in KDC backend: %s\n",
3252 ldb_errstring(kdc_db_ctx->samdb)));
3253 talloc_free(kdc_db_ctx);
3254 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3257 ldb_ret = samdb_reference_dn(kdc_db_ctx->samdb, kdc_db_ctx, server_dn,
3258 "serverReference", &account_dn);
3259 if (ldb_ret != LDB_SUCCESS) {
3260 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine server account in KDC backend: %s\n",
3261 ldb_errstring(kdc_db_ctx->samdb)));
3262 talloc_free(kdc_db_ctx);
3263 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3266 ldb_ret = samdb_reference_dn(kdc_db_ctx->samdb, kdc_db_ctx, account_dn,
3267 "msDS-KrbTgtLink", &kdc_db_ctx->krbtgt_dn);
3268 talloc_free(account_dn);
3269 if (ldb_ret != LDB_SUCCESS) {
3270 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine RODC krbtgt account in KDC backend: %s\n",
3271 ldb_errstring(kdc_db_ctx->samdb)));
3272 talloc_free(kdc_db_ctx);
3273 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3276 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb, kdc_db_ctx,
3277 &msg, kdc_db_ctx->krbtgt_dn, LDB_SCOPE_BASE,
3279 DSDB_SEARCH_NO_GLOBAL_CATALOG,
3280 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=*))");
3281 if (ldb_ret != LDB_SUCCESS) {
3282 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot read krbtgt account %s in KDC backend to get msDS-SecondaryKrbTgtNumber: %s: %s\n",
3283 ldb_dn_get_linearized(kdc_db_ctx->krbtgt_dn),
3284 ldb_errstring(kdc_db_ctx->samdb),
3285 ldb_strerror(ldb_ret)));
3286 talloc_free(kdc_db_ctx);
3287 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3289 my_krbtgt_number = ldb_msg_find_attr_as_int(msg, "msDS-SecondaryKrbTgtNumber", -1);
3290 if (my_krbtgt_number == -1) {
3291 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot read msDS-SecondaryKrbTgtNumber from krbtgt account %s in KDC backend: got %d\n",
3292 ldb_dn_get_linearized(kdc_db_ctx->krbtgt_dn),
3294 talloc_free(kdc_db_ctx);
3295 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3297 kdc_db_ctx->my_krbtgt_number = my_krbtgt_number;
3300 kdc_db_ctx->my_krbtgt_number = 0;
3301 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb, kdc_db_ctx,
3303 ldb_get_default_basedn(kdc_db_ctx->samdb),
3306 DSDB_SEARCH_NO_GLOBAL_CATALOG,
3307 "(&(objectClass=user)(samAccountName=krbtgt))");
3309 if (ldb_ret != LDB_SUCCESS) {
3310 DEBUG(1, ("samba_kdc_fetch: could not find own KRBTGT in DB: %s\n", ldb_errstring(kdc_db_ctx->samdb)));
3311 talloc_free(kdc_db_ctx);
3312 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3314 kdc_db_ctx->krbtgt_dn = talloc_steal(kdc_db_ctx, msg->dn);
3315 kdc_db_ctx->my_krbtgt_number = 0;
3318 *kdc_db_ctx_out = kdc_db_ctx;
3319 return NT_STATUS_OK;