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 "system/time.h"
26 #include "../libds/common/flags.h"
27 #include "lib/ldb/include/ldb.h"
28 #include "librpc/gen_ndr/netlogon.h"
29 #include "libcli/security/security.h"
30 #include "auth/auth.h"
31 #include "auth/credentials/credentials.h"
32 #include "auth/auth_sam.h"
33 #include "../lib/util/util_ldb.h"
34 #include "dsdb/samdb/samdb.h"
35 #include "dsdb/common/util.h"
36 #include "librpc/ndr/libndr.h"
37 #include "librpc/gen_ndr/ndr_drsblobs.h"
38 #include "librpc/gen_ndr/lsa.h"
39 #include "libcli/auth/libcli_auth.h"
40 #include "param/param.h"
41 #include "../lib/crypto/md4.h"
42 #include "system/kerberos.h"
43 #include "auth/kerberos/kerberos.h"
45 #include "kdc/samba_kdc.h"
46 #include "kdc/db-glue.h"
48 enum samba_kdc_ent_type
49 { SAMBA_KDC_ENT_TYPE_CLIENT, SAMBA_KDC_ENT_TYPE_SERVER,
50 SAMBA_KDC_ENT_TYPE_KRBTGT, SAMBA_KDC_ENT_TYPE_TRUST, SAMBA_KDC_ENT_TYPE_ANY };
52 enum trust_direction {
54 INBOUND = LSA_TRUST_DIRECTION_INBOUND,
55 OUTBOUND = LSA_TRUST_DIRECTION_OUTBOUND
58 static const char *trust_attrs[] = {
63 "msDS-SupportedEncryptionTypes",
70 static KerberosTime ldb_msg_find_krb5time_ldap_time(struct ldb_message *msg, const char *attr, KerberosTime default_val)
76 gentime = ldb_msg_find_attr_as_string(msg, attr, NULL);
80 tmp = strptime(gentime, "%Y%m%d%H%M%SZ", &tm);
88 static HDBFlags uf2HDBFlags(krb5_context context, int userAccountControl, enum samba_kdc_ent_type ent_type)
90 HDBFlags flags = int2HDBFlags(0);
92 /* we don't allow kadmin deletes */
95 /* mark the principal as invalid to start with */
100 /* All accounts are servers, but this may be disabled again in the caller */
103 /* Account types - clear the invalid bit if it turns out to be valid */
104 if (userAccountControl & UF_NORMAL_ACCOUNT) {
105 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
111 if (userAccountControl & UF_INTERDOMAIN_TRUST_ACCOUNT) {
112 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
117 if (userAccountControl & UF_WORKSTATION_TRUST_ACCOUNT) {
118 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
123 if (userAccountControl & UF_SERVER_TRUST_ACCOUNT) {
124 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
130 /* Not permitted to act as a client if disabled */
131 if (userAccountControl & UF_ACCOUNTDISABLE) {
134 if (userAccountControl & UF_LOCKOUT) {
138 if (userAccountControl & UF_PASSWORD_NOTREQD) {
143 UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
145 if (userAccountControl & UF_TEMP_DUPLICATE_ACCOUNT) {
149 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in samba_kdc_message2entry() */
152 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
156 if (userAccountControl & UF_SMARTCARD_REQUIRED) {
157 flags.require_hwauth = 1;
159 if (userAccountControl & UF_TRUSTED_FOR_DELEGATION) {
160 flags.ok_as_delegate = 1;
162 if (!(userAccountControl & UF_NOT_DELEGATED)) {
163 flags.forwardable = 1;
167 if (userAccountControl & UF_DONT_REQUIRE_PREAUTH) {
168 flags.require_preauth = 0;
170 flags.require_preauth = 1;
176 static int samba_kdc_entry_destructor(struct samba_kdc_entry *p)
178 hdb_entry_ex *entry_ex = p->entry_ex;
179 free_hdb_entry(&entry_ex->entry);
183 static void samba_kdc_free_entry(krb5_context context, hdb_entry_ex *entry_ex)
185 /* this function is called only from hdb_free_entry().
186 * Make sure we neutralize the destructor or we will
187 * get a double free later when hdb_free_entry() will
188 * try to call free_hdb_entry() */
189 talloc_set_destructor(entry_ex->ctx, NULL);
191 /* now proceed to free the talloc part */
192 talloc_free(entry_ex->ctx);
195 static krb5_error_code samba_kdc_message2entry_keys(krb5_context context,
197 struct ldb_message *msg,
200 unsigned int userAccountControl,
201 enum samba_kdc_ent_type ent_type,
202 hdb_entry_ex *entry_ex)
204 krb5_error_code ret = 0;
205 enum ndr_err_code ndr_err;
206 struct samr_Password *hash;
207 const struct ldb_val *sc_val;
208 struct supplementalCredentialsBlob scb;
209 struct supplementalCredentialsPackage *scpk = NULL;
210 bool newer_keys = false;
211 struct package_PrimaryKerberosBlob _pkb;
212 struct package_PrimaryKerberosCtr3 *pkb3 = NULL;
213 struct package_PrimaryKerberosCtr4 *pkb4 = NULL;
215 uint16_t allocated_keys = 0;
216 int rodc_krbtgt_number = 0;
218 /* Supported Enc for this entry */
219 uint32_t supported_enctypes = ENC_ALL_TYPES; /* by default, we support all enc types */
221 /* However, if this is a TGS-REQ, then lock it down to a
222 * reasonable guess as to what the server can decode. The
223 * krbtgt is special - default to use what is stored for the KDC */
224 if (rid != DOMAIN_RID_KRBTGT && ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
225 /* This is the standard set for a server that has not declared a msDS-SupportedEncryptionTypes */
226 supported_enctypes = ENC_CRC32 | ENC_RSA_MD5 | ENC_RC4_HMAC_MD5;
228 supported_enctypes = ldb_msg_find_attr_as_uint(msg, "msDS-SupportedEncryptionTypes",
230 /* Is this the krbtgt or a RODC */
233 rodc_krbtgt_number = ldb_msg_find_attr_as_int(msg, "msDS-SecondaryKrbTgtNumber", -1);
235 if (rodc_krbtgt_number == -1) {
240 if (rid == DOMAIN_RID_KRBTGT || is_rodc) {
241 /* Be double-sure never to use DES here */
242 supported_enctypes &= ~(ENC_CRC32|ENC_RSA_MD5);
246 case SAMBA_KDC_ENT_TYPE_KRBTGT:
247 case SAMBA_KDC_ENT_TYPE_TRUST:
248 /* Disallow krbtgt and trust tickets to be DES encrypted, it's just too dangerous */
249 supported_enctypes &= ~(ENC_CRC32|ENC_RSA_MD5);
253 /* No further restrictions */
256 /* If UF_USE_DES_KEY_ONLY has been set, then don't allow use of the newer enc types */
257 if (userAccountControl & UF_USE_DES_KEY_ONLY) {
258 /* However, don't allow use of DES, if we were told not to by msDS-SupportedEncTypes */
259 supported_enctypes &= ENC_CRC32|ENC_RSA_MD5;
262 entry_ex->entry.keys.val = NULL;
263 entry_ex->entry.keys.len = 0;
265 entry_ex->entry.kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
267 entry_ex->entry.kvno |= (rodc_krbtgt_number << 16);
270 /* Get keys from the db */
272 hash = samdb_result_hash(mem_ctx, msg, "unicodePwd");
273 sc_val = ldb_msg_find_ldb_val(msg, "supplementalCredentials");
275 /* unicodePwd for enctype 0x17 (23) if present */
280 /* supplementalCredentials if present */
282 ndr_err = ndr_pull_struct_blob_all(sc_val, mem_ctx, &scb,
283 (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob);
284 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
285 dump_data(0, sc_val->data, sc_val->length);
290 if (scb.sub.signature != SUPPLEMENTAL_CREDENTIALS_SIGNATURE) {
291 NDR_PRINT_DEBUG(supplementalCredentialsBlob, &scb);
296 for (i=0; i < scb.sub.num_packages; i++) {
297 if (strcmp("Primary:Kerberos-Newer-Keys", scb.sub.packages[i].name) == 0) {
298 scpk = &scb.sub.packages[i];
299 if (!scpk->data || !scpk->data[0]) {
305 } else if (strcmp("Primary:Kerberos", scb.sub.packages[i].name) == 0) {
306 scpk = &scb.sub.packages[i];
307 if (!scpk->data || !scpk->data[0]) {
311 * we don't break here in hope to find
312 * a Kerberos-Newer-Keys package
318 * Primary:Kerberos-Newer-Keys or Primary:Kerberos element
319 * of supplementalCredentials
324 blob = strhex_to_data_blob(mem_ctx, scpk->data);
330 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
331 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &_pkb,
332 (ndr_pull_flags_fn_t)ndr_pull_package_PrimaryKerberosBlob);
333 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
335 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
336 krb5_warnx(context, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
340 if (newer_keys && _pkb.version != 4) {
342 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
343 krb5_warnx(context, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
347 if (!newer_keys && _pkb.version != 3) {
349 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
350 krb5_warnx(context, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
354 if (_pkb.version == 4) {
355 pkb4 = &_pkb.ctr.ctr4;
356 allocated_keys += pkb4->num_keys;
357 } else if (_pkb.version == 3) {
358 pkb3 = &_pkb.ctr.ctr3;
359 allocated_keys += pkb3->num_keys;
363 if (allocated_keys == 0) {
364 /* oh, no password. Apparently (comment in
365 * hdb-ldap.c) this violates the ASN.1, but this
366 * allows an entry with no keys (yet). */
370 /* allocate space to decode into */
371 entry_ex->entry.keys.len = 0;
372 entry_ex->entry.keys.val = calloc(allocated_keys, sizeof(Key));
373 if (entry_ex->entry.keys.val == NULL) {
378 if (hash && (supported_enctypes & ENC_RC4_HMAC_MD5)) {
382 key.salt = NULL; /* No salt for this enc type */
384 ret = krb5_keyblock_init(context,
385 ENCTYPE_ARCFOUR_HMAC,
386 hash->hash, sizeof(hash->hash),
392 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
393 entry_ex->entry.keys.len++;
397 for (i=0; i < pkb4->num_keys; i++) {
400 if (!pkb4->keys[i].value) continue;
402 if (!(kerberos_enctype_to_bitmap(pkb4->keys[i].keytype) & supported_enctypes)) {
409 if (pkb4->salt.string) {
412 salt = data_blob_string_const(pkb4->salt.string);
414 key.salt = calloc(1, sizeof(*key.salt));
415 if (key.salt == NULL) {
420 key.salt->type = hdb_pw_salt;
422 ret = krb5_data_copy(&key.salt->salt, salt.data, salt.length);
430 /* TODO: maybe pass the iteration_count somehow... */
432 ret = krb5_keyblock_init(context,
433 pkb4->keys[i].keytype,
434 pkb4->keys[i].value->data,
435 pkb4->keys[i].value->length,
437 if (ret == KRB5_PROG_ETYPE_NOSUPP) {
438 DEBUG(2,("Unsupported keytype ignored - type %u\n",
439 pkb4->keys[i].keytype));
452 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
453 entry_ex->entry.keys.len++;
456 for (i=0; i < pkb3->num_keys; i++) {
459 if (!pkb3->keys[i].value) continue;
461 if (!(kerberos_enctype_to_bitmap(pkb3->keys[i].keytype) & supported_enctypes)) {
468 if (pkb3->salt.string) {
471 salt = data_blob_string_const(pkb3->salt.string);
473 key.salt = calloc(1, sizeof(*key.salt));
474 if (key.salt == NULL) {
479 key.salt->type = hdb_pw_salt;
481 ret = krb5_data_copy(&key.salt->salt, salt.data, salt.length);
489 ret = krb5_keyblock_init(context,
490 pkb3->keys[i].keytype,
491 pkb3->keys[i].value->data,
492 pkb3->keys[i].value->length,
503 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
504 entry_ex->entry.keys.len++;
510 entry_ex->entry.keys.len = 0;
512 if (entry_ex->entry.keys.len == 0 && entry_ex->entry.keys.val) {
513 free(entry_ex->entry.keys.val);
514 entry_ex->entry.keys.val = NULL;
520 * Construct an hdb_entry from a directory entry.
522 static krb5_error_code samba_kdc_message2entry(krb5_context context,
523 struct samba_kdc_db_context *kdc_db_ctx,
524 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
525 enum samba_kdc_ent_type ent_type,
526 struct ldb_dn *realm_dn,
527 struct ldb_message *msg,
528 hdb_entry_ex *entry_ex)
530 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
531 unsigned int userAccountControl;
533 krb5_error_code ret = 0;
534 krb5_boolean is_computer = FALSE;
535 char *realm = strupper_talloc(mem_ctx, lpcfg_realm(lp_ctx));
537 struct samba_kdc_entry *p;
542 bool is_rodc = false;
543 struct ldb_message_element *objectclasses;
544 struct ldb_val computer_val;
545 const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
546 computer_val.data = discard_const_p(uint8_t,"computer");
547 computer_val.length = strlen((const char *)computer_val.data);
549 if (ldb_msg_find_element(msg, "msDS-SecondaryKrbTgtNumber")) {
553 if (!samAccountName) {
555 krb5_set_error_message(context, ret, "samba_kdc_message2entry: no samAccountName present");
559 objectclasses = ldb_msg_find_element(msg, "objectClass");
561 if (objectclasses && ldb_msg_find_val(objectclasses, &computer_val)) {
565 memset(entry_ex, 0, sizeof(*entry_ex));
569 krb5_set_error_message(context, ret, "talloc_strdup: out of memory");
573 p = talloc(mem_ctx, struct samba_kdc_entry);
579 p->kdc_db_ctx = kdc_db_ctx;
580 p->entry_ex = entry_ex;
581 p->realm_dn = talloc_reference(p, realm_dn);
587 talloc_set_destructor(p, samba_kdc_entry_destructor);
589 /* make sure we do not have bogus data in there */
590 memset(&entry_ex->entry, 0, sizeof(hdb_entry));
593 entry_ex->free_entry = samba_kdc_free_entry;
595 userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
598 entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
599 if (ent_type == SAMBA_KDC_ENT_TYPE_ANY && principal == NULL) {
600 krb5_make_principal(context, &entry_ex->entry.principal, realm, samAccountName, NULL);
602 ret = copy_Principal(principal, entry_ex->entry.principal);
604 krb5_clear_error_message(context);
608 /* While we have copied the client principal, tests
609 * show that Win2k3 returns the 'corrected' realm, not
610 * the client-specified realm. This code attempts to
611 * replace the client principal's realm with the one
612 * we determine from our records */
614 /* this has to be with malloc() */
615 krb5_principal_set_realm(context, entry_ex->entry.principal, realm);
618 /* First try and figure out the flags based on the userAccountControl */
619 entry_ex->entry.flags = uf2HDBFlags(context, userAccountControl, ent_type);
621 /* Windows 2008 seems to enforce this (very sensible) rule by
622 * default - don't allow offline attacks on a user's password
623 * by asking for a ticket to them as a service (encrypted with
624 * their probably patheticly insecure password) */
626 if (entry_ex->entry.flags.server
627 && lpcfg_parm_bool(lp_ctx, NULL, "kdc", "require spn for service", true)) {
628 if (!is_computer && !ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL)) {
629 entry_ex->entry.flags.server = 0;
634 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
635 * of the Heimdal KDC. They are stored in a the traditional
636 * DB for audit purposes, and still form part of the structure
639 /* use 'whenCreated' */
640 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
641 /* use 'kadmin' for now (needed by mit_samba) */
642 krb5_make_principal(context,
643 &entry_ex->entry.created_by.principal,
644 realm, "kadmin", NULL);
646 entry_ex->entry.modified_by = (Event *) malloc(sizeof(Event));
647 if (entry_ex->entry.modified_by == NULL) {
649 krb5_set_error_message(context, ret, "malloc: out of memory");
653 /* use 'whenChanged' */
654 entry_ex->entry.modified_by->time = ldb_msg_find_krb5time_ldap_time(msg, "whenChanged", 0);
655 /* use 'kadmin' for now (needed by mit_samba) */
656 krb5_make_principal(context,
657 &entry_ex->entry.modified_by->principal,
658 realm, "kadmin", NULL);
662 /* The lack of password controls etc applies to krbtgt by
663 * virtue of being that particular RID */
664 status = dom_sid_split_rid(NULL, samdb_result_dom_sid(mem_ctx, msg, "objectSid"), NULL, &rid);
666 if (!NT_STATUS_IS_OK(status)) {
671 if (rid == DOMAIN_RID_KRBTGT) {
672 entry_ex->entry.valid_end = NULL;
673 entry_ex->entry.pw_end = NULL;
675 entry_ex->entry.flags.invalid = 0;
676 entry_ex->entry.flags.server = 1;
678 /* Don't mark all requests for the krbtgt/realm as
679 * 'change password', as otherwise we could get into
680 * trouble, and not enforce the password expirty.
681 * Instead, only do it when request is for the kpasswd service */
682 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER
683 && principal->name.name_string.len == 2
684 && (strcmp(principal->name.name_string.val[0], "kadmin") == 0)
685 && (strcmp(principal->name.name_string.val[1], "changepw") == 0)
686 && lpcfg_is_my_domain_or_realm(lp_ctx, principal->realm)) {
687 entry_ex->entry.flags.change_pw = 1;
689 entry_ex->entry.flags.client = 0;
690 entry_ex->entry.flags.forwardable = 1;
691 entry_ex->entry.flags.ok_as_delegate = 1;
692 } else if (is_rodc) {
693 /* The RODC krbtgt account is like the main krbtgt,
694 * but it does not have a changepw or kadmin
697 entry_ex->entry.valid_end = NULL;
698 entry_ex->entry.pw_end = NULL;
700 /* Also don't allow the RODC krbtgt to be a client (it should not be needed) */
701 entry_ex->entry.flags.client = 0;
702 entry_ex->entry.flags.invalid = 0;
703 entry_ex->entry.flags.server = 1;
705 entry_ex->entry.flags.client = 0;
706 entry_ex->entry.flags.forwardable = 1;
707 entry_ex->entry.flags.ok_as_delegate = 0;
708 } else if (entry_ex->entry.flags.server && ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
709 /* The account/password expiry only applies when the account is used as a
710 * client (ie password login), not when used as a server */
712 /* Make very well sure we don't use this for a client,
713 * it could bypass the password restrictions */
714 entry_ex->entry.flags.client = 0;
716 entry_ex->entry.valid_end = NULL;
717 entry_ex->entry.pw_end = NULL;
720 NTTIME must_change_time
721 = samdb_result_force_password_change(kdc_db_ctx->samdb, mem_ctx,
723 if (must_change_time == 0x7FFFFFFFFFFFFFFFULL) {
724 entry_ex->entry.pw_end = NULL;
726 entry_ex->entry.pw_end = malloc(sizeof(*entry_ex->entry.pw_end));
727 if (entry_ex->entry.pw_end == NULL) {
731 *entry_ex->entry.pw_end = nt_time_to_unix(must_change_time);
734 acct_expiry = samdb_result_account_expires(msg);
735 if (acct_expiry == 0x7FFFFFFFFFFFFFFFULL) {
736 entry_ex->entry.valid_end = NULL;
738 entry_ex->entry.valid_end = malloc(sizeof(*entry_ex->entry.valid_end));
739 if (entry_ex->entry.valid_end == NULL) {
743 *entry_ex->entry.valid_end = nt_time_to_unix(acct_expiry);
747 entry_ex->entry.valid_start = NULL;
749 entry_ex->entry.max_life = NULL;
751 entry_ex->entry.max_renew = NULL;
753 entry_ex->entry.generation = NULL;
755 /* Get keys from the db */
756 ret = samba_kdc_message2entry_keys(context, p, msg,
757 rid, is_rodc, userAccountControl,
760 /* Could be bougus data in the entry, or out of memory */
764 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
765 if (entry_ex->entry.etypes == NULL) {
766 krb5_clear_error_message(context);
770 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
771 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
772 if (entry_ex->entry.etypes->val == NULL) {
773 krb5_clear_error_message(context);
777 for (i=0; i < entry_ex->entry.etypes->len; i++) {
778 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
782 p->msg = talloc_steal(p, msg);
786 /* This doesn't free ent itself, that is for the eventual caller to do */
787 hdb_free_entry(context, entry_ex);
789 talloc_steal(kdc_db_ctx, entry_ex->ctx);
796 * Construct an hdb_entry from a directory entry.
798 static krb5_error_code samba_kdc_trust_message2entry(krb5_context context,
799 struct samba_kdc_db_context *kdc_db_ctx,
800 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
801 enum trust_direction direction,
802 struct ldb_dn *realm_dn,
803 struct ldb_message *msg,
804 hdb_entry_ex *entry_ex)
806 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
807 const char *dnsdomain;
808 char *realm = strupper_talloc(mem_ctx, lpcfg_realm(lp_ctx));
809 DATA_BLOB password_utf16;
810 struct samr_Password password_hash;
811 const struct ldb_val *password_val;
812 struct trustAuthInOutBlob password_blob;
813 struct samba_kdc_entry *p;
815 enum ndr_err_code ndr_err;
816 int ret, trust_direction_flags;
819 p = talloc(mem_ctx, struct samba_kdc_entry);
825 p->kdc_db_ctx = kdc_db_ctx;
826 p->entry_ex = entry_ex;
827 p->realm_dn = realm_dn;
829 talloc_set_destructor(p, samba_kdc_entry_destructor);
831 /* make sure we do not have bogus data in there */
832 memset(&entry_ex->entry, 0, sizeof(hdb_entry));
835 entry_ex->free_entry = samba_kdc_free_entry;
837 /* use 'whenCreated' */
838 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
839 /* use 'kadmin' for now (needed by mit_samba) */
840 krb5_make_principal(context,
841 &entry_ex->entry.created_by.principal,
842 realm, "kadmin", NULL);
844 entry_ex->entry.valid_start = NULL;
846 trust_direction_flags = ldb_msg_find_attr_as_int(msg, "trustDirection", 0);
848 if (direction == INBOUND) {
849 password_val = ldb_msg_find_ldb_val(msg, "trustAuthIncoming");
851 } else { /* OUTBOUND */
852 dnsdomain = ldb_msg_find_attr_as_string(msg, "trustPartner", NULL);
855 realm = strupper_talloc(mem_ctx, dnsdomain);
856 password_val = ldb_msg_find_ldb_val(msg, "trustAuthOutgoing");
859 if (!password_val || !(trust_direction_flags & direction)) {
864 ndr_err = ndr_pull_struct_blob(password_val, mem_ctx, &password_blob,
865 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
866 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
871 entry_ex->entry.kvno = -1;
872 for (i=0; i < password_blob.count; i++) {
873 if (password_blob.current.array[i].AuthType == TRUST_AUTH_TYPE_VERSION) {
874 entry_ex->entry.kvno = password_blob.current.array[i].AuthInfo.version.version;
878 for (i=0; i < password_blob.count; i++) {
879 if (password_blob.current.array[i].AuthType == TRUST_AUTH_TYPE_CLEAR) {
880 password_utf16 = data_blob_const(password_blob.current.array[i].AuthInfo.clear.password,
881 password_blob.current.array[i].AuthInfo.clear.size);
882 /* In the future, generate all sorts of
883 * hashes, but for now we can't safely convert
884 * the random strings windows uses into
887 /* but as it is utf16 already, we can get the NT password/arcfour-hmac-md5 key */
888 mdfour(password_hash.hash, password_utf16.data, password_utf16.length);
890 } else if (password_blob.current.array[i].AuthType == TRUST_AUTH_TYPE_NT4OWF) {
891 password_hash = password_blob.current.array[i].AuthInfo.nt4owf.password;
896 if (i < password_blob.count) {
898 /* Must have found a cleartext or MD4 password */
899 entry_ex->entry.keys.val = calloc(1, sizeof(Key));
902 key.salt = NULL; /* No salt for this enc type */
904 if (entry_ex->entry.keys.val == NULL) {
909 ret = krb5_keyblock_init(context,
910 ENCTYPE_ARCFOUR_HMAC,
911 password_hash.hash, sizeof(password_hash.hash),
914 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
915 entry_ex->entry.keys.len++;
918 entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
920 ret = copy_Principal(principal, entry_ex->entry.principal);
922 krb5_clear_error_message(context);
926 /* While we have copied the client principal, tests
927 * show that Win2k3 returns the 'corrected' realm, not
928 * the client-specified realm. This code attempts to
929 * replace the client principal's realm with the one
930 * we determine from our records */
932 krb5_principal_set_realm(context, entry_ex->entry.principal, realm);
933 entry_ex->entry.flags = int2HDBFlags(0);
934 entry_ex->entry.flags.immutable = 1;
935 entry_ex->entry.flags.invalid = 0;
936 entry_ex->entry.flags.server = 1;
937 entry_ex->entry.flags.require_preauth = 1;
939 entry_ex->entry.pw_end = NULL;
941 entry_ex->entry.max_life = NULL;
943 entry_ex->entry.max_renew = NULL;
945 entry_ex->entry.generation = NULL;
947 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
948 if (entry_ex->entry.etypes == NULL) {
949 krb5_clear_error_message(context);
953 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
954 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
955 if (entry_ex->entry.etypes->val == NULL) {
956 krb5_clear_error_message(context);
960 for (i=0; i < entry_ex->entry.etypes->len; i++) {
961 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
965 p->msg = talloc_steal(p, msg);
969 /* This doesn't free ent itself, that is for the eventual caller to do */
970 hdb_free_entry(context, entry_ex);
972 talloc_steal(kdc_db_ctx, entry_ex->ctx);
979 static krb5_error_code samba_kdc_lookup_trust(krb5_context context, struct ldb_context *ldb_ctx,
982 struct ldb_dn *realm_dn,
983 struct ldb_message **pmsg)
988 const char * const *attrs = trust_attrs;
990 struct ldb_result *res = NULL;
991 filter = talloc_asprintf(mem_ctx, "(&(objectClass=trustedDomain)(|(flatname=%s)(trustPartner=%s)))", realm, realm);
995 krb5_set_error_message(context, ret, "talloc_asprintf: out of memory");
999 lret = ldb_search(ldb_ctx, mem_ctx, &res,
1000 ldb_get_default_basedn(ldb_ctx),
1001 LDB_SCOPE_SUBTREE, attrs, "%s", filter);
1002 if (lret != LDB_SUCCESS) {
1003 DEBUG(3, ("Failed to search for %s: %s\n", filter, ldb_errstring(ldb_ctx)));
1004 return HDB_ERR_NOENTRY;
1005 } else if (res->count == 0 || res->count > 1) {
1006 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter, res->count));
1008 return HDB_ERR_NOENTRY;
1010 talloc_steal(mem_ctx, res->msgs);
1011 *pmsg = res->msgs[0];
1016 static krb5_error_code samba_kdc_lookup_client(krb5_context context,
1017 struct samba_kdc_db_context *kdc_db_ctx,
1018 TALLOC_CTX *mem_ctx,
1019 krb5_const_principal principal,
1021 struct ldb_dn **realm_dn,
1022 struct ldb_message **msg) {
1024 char *principal_string;
1025 krb5_error_code ret;
1027 ret = krb5_unparse_name(context, principal, &principal_string);
1033 nt_status = sam_get_results_principal(kdc_db_ctx->samdb,
1034 mem_ctx, principal_string, attrs,
1036 free(principal_string);
1037 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
1038 return HDB_ERR_NOENTRY;
1039 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
1041 } else if (!NT_STATUS_IS_OK(nt_status)) {
1048 static krb5_error_code samba_kdc_fetch_client(krb5_context context,
1049 struct samba_kdc_db_context *kdc_db_ctx,
1050 TALLOC_CTX *mem_ctx,
1051 krb5_const_principal principal,
1052 hdb_entry_ex *entry_ex) {
1053 struct ldb_dn *realm_dn;
1054 krb5_error_code ret;
1055 struct ldb_message *msg = NULL;
1057 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
1058 mem_ctx, principal, user_attrs,
1064 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1065 principal, SAMBA_KDC_ENT_TYPE_CLIENT,
1066 realm_dn, msg, entry_ex);
1070 static krb5_error_code samba_kdc_fetch_krbtgt(krb5_context context,
1071 struct samba_kdc_db_context *kdc_db_ctx,
1072 TALLOC_CTX *mem_ctx,
1073 krb5_const_principal principal,
1074 uint32_t krbtgt_number,
1075 hdb_entry_ex *entry_ex)
1077 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
1078 krb5_error_code ret;
1079 struct ldb_message *msg = NULL;
1080 struct ldb_dn *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
1082 krb5_principal alloc_principal = NULL;
1083 if (principal->name.name_string.len != 2
1084 || (strcmp(principal->name.name_string.val[0], KRB5_TGS_NAME) != 0)) {
1086 return HDB_ERR_NOENTRY;
1089 /* krbtgt case. Either us or a trusted realm */
1091 if (lpcfg_is_my_domain_or_realm(lp_ctx, principal->realm)
1092 && lpcfg_is_my_domain_or_realm(lp_ctx, principal->name.name_string.val[1])) {
1093 /* us, or someone quite like us */
1094 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1095 * is in our db, then direct the caller at our primary
1101 if (krbtgt_number == kdc_db_ctx->my_krbtgt_number) {
1102 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
1103 &msg, kdc_db_ctx->krbtgt_dn, LDB_SCOPE_BASE,
1105 "(objectClass=user)");
1107 /* We need to look up an RODC krbtgt (perhaps
1108 * ours, if we are an RODC, perhaps another
1109 * RODC if we are a read-write DC */
1110 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
1111 &msg, realm_dn, LDB_SCOPE_SUBTREE,
1113 DSDB_SEARCH_SHOW_EXTENDED_DN,
1114 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=%u))", (unsigned)(krbtgt_number));
1117 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
1118 krb5_warnx(context, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1119 (unsigned)(krbtgt_number));
1120 krb5_set_error_message(context, HDB_ERR_NOENTRY,
1121 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1122 (unsigned)(krbtgt_number));
1123 return HDB_ERR_NOENTRY;
1124 } else if (lret != LDB_SUCCESS) {
1125 krb5_warnx(context, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1126 (unsigned)(krbtgt_number));
1127 krb5_set_error_message(context, HDB_ERR_NOENTRY,
1128 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1129 (unsigned)(krbtgt_number));
1130 return HDB_ERR_NOENTRY;
1133 realm_fixed = strupper_talloc(mem_ctx, lpcfg_realm(lp_ctx));
1136 krb5_set_error_message(context, ret, "strupper_talloc: out of memory");
1140 ret = krb5_copy_principal(context, principal, &alloc_principal);
1145 free(alloc_principal->name.name_string.val[1]);
1146 alloc_principal->name.name_string.val[1] = strdup(realm_fixed);
1147 talloc_free(realm_fixed);
1148 if (!alloc_principal->name.name_string.val[1]) {
1150 krb5_set_error_message(context, ret, "samba_kdc_fetch: strdup() failed!");
1153 principal = alloc_principal;
1155 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1156 principal, SAMBA_KDC_ENT_TYPE_KRBTGT,
1157 realm_dn, msg, entry_ex);
1159 krb5_warnx(context, "samba_kdc_fetch: self krbtgt message2entry failed");
1164 enum trust_direction direction = UNKNOWN;
1165 const char *realm = NULL;
1167 /* Either an inbound or outbound trust */
1169 if (strcasecmp(lpcfg_realm(lp_ctx), principal->realm) == 0) {
1170 /* look for inbound trust */
1171 direction = INBOUND;
1172 realm = principal->name.name_string.val[1];
1173 } else if (strcasecmp(lpcfg_realm(lp_ctx), principal->name.name_string.val[1]) == 0) {
1174 /* look for outbound trust */
1175 direction = OUTBOUND;
1176 realm = principal->realm;
1178 krb5_warnx(context, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1179 principal->realm, principal->name.name_string.val[1]);
1180 krb5_set_error_message(context, HDB_ERR_NOENTRY, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1181 principal->realm, principal->name.name_string.val[1]);
1182 return HDB_ERR_NOENTRY;
1185 /* Trusted domains are under CN=system */
1187 ret = samba_kdc_lookup_trust(context, kdc_db_ctx->samdb,
1189 realm, realm_dn, &msg);
1192 krb5_warnx(context, "samba_kdc_fetch: could not find principal in DB");
1193 krb5_set_error_message(context, ret, "samba_kdc_fetch: could not find principal in DB");
1197 ret = samba_kdc_trust_message2entry(context, kdc_db_ctx, mem_ctx,
1198 principal, direction,
1199 realm_dn, msg, entry_ex);
1201 krb5_warnx(context, "samba_kdc_fetch: trust_message2entry failed");
1208 static krb5_error_code samba_kdc_lookup_server(krb5_context context,
1209 struct samba_kdc_db_context *kdc_db_ctx,
1210 TALLOC_CTX *mem_ctx,
1211 krb5_const_principal principal,
1213 struct ldb_dn **realm_dn,
1214 struct ldb_message **msg)
1216 krb5_error_code ret;
1218 if (principal->name.name_string.len >= 2) {
1219 /* 'normal server' case */
1222 struct ldb_dn *user_dn;
1223 char *principal_string;
1225 ret = krb5_unparse_name_flags(context, principal,
1226 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
1232 /* At this point we may find the host is known to be
1233 * in a different realm, so we should generate a
1234 * referral instead */
1235 nt_status = crack_service_principal_name(kdc_db_ctx->samdb,
1236 mem_ctx, principal_string,
1237 &user_dn, realm_dn);
1238 free(principal_string);
1240 if (!NT_STATUS_IS_OK(nt_status)) {
1241 return HDB_ERR_NOENTRY;
1244 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb,
1246 msg, user_dn, LDB_SCOPE_BASE,
1247 attrs, DSDB_SEARCH_SHOW_EXTENDED_DN, "(objectClass=*)");
1248 if (ldb_ret != LDB_SUCCESS) {
1249 return HDB_ERR_NOENTRY;
1254 char *filter = NULL;
1256 /* server as client principal case, but we must not lookup userPrincipalNames */
1257 *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
1258 realm = krb5_principal_get_realm(context, principal);
1260 /* TODO: Check if it is our realm, otherwise give referall */
1262 ret = krb5_unparse_name_flags(context, principal, KRB5_PRINCIPAL_UNPARSE_NO_REALM, &short_princ);
1265 krb5_set_error_message(context, ret, "samba_kdc_lookup_principal: could not parse principal");
1266 krb5_warnx(context, "samba_kdc_lookup_principal: could not parse principal");
1270 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx, msg,
1271 *realm_dn, LDB_SCOPE_SUBTREE,
1273 DSDB_SEARCH_SHOW_EXTENDED_DN,
1274 "(&(objectClass=user)(samAccountName=%s))",
1275 ldb_binary_encode_string(mem_ctx, short_princ));
1277 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
1278 DEBUG(3, ("Failed find a entry for %s\n", filter));
1279 return HDB_ERR_NOENTRY;
1281 if (lret != LDB_SUCCESS) {
1282 DEBUG(3, ("Failed single search for for %s - %s\n",
1283 filter, ldb_errstring(kdc_db_ctx->samdb)));
1284 return HDB_ERR_NOENTRY;
1291 static krb5_error_code samba_kdc_fetch_server(krb5_context context,
1292 struct samba_kdc_db_context *kdc_db_ctx,
1293 TALLOC_CTX *mem_ctx,
1294 krb5_const_principal principal,
1295 hdb_entry_ex *entry_ex)
1297 krb5_error_code ret;
1298 struct ldb_dn *realm_dn;
1299 struct ldb_message *msg;
1301 ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, principal,
1302 server_attrs, &realm_dn, &msg);
1307 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1308 principal, SAMBA_KDC_ENT_TYPE_SERVER,
1309 realm_dn, msg, entry_ex);
1311 krb5_warnx(context, "samba_kdc_fetch: message2entry failed");
1317 krb5_error_code samba_kdc_fetch(krb5_context context,
1318 struct samba_kdc_db_context *kdc_db_ctx,
1319 krb5_const_principal principal,
1322 hdb_entry_ex *entry_ex)
1324 krb5_error_code ret = HDB_ERR_NOENTRY;
1325 TALLOC_CTX *mem_ctx;
1326 unsigned int krbtgt_number;
1327 if (flags & HDB_F_KVNO_SPECIFIED) {
1328 krbtgt_number = kvno >> 16;
1329 if (kdc_db_ctx->rodc) {
1330 if (krbtgt_number != kdc_db_ctx->my_krbtgt_number) {
1331 return HDB_ERR_NOT_FOUND_HERE;
1335 krbtgt_number = kdc_db_ctx->my_krbtgt_number;
1338 mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_fetch context");
1341 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
1345 if (flags & HDB_F_GET_CLIENT) {
1346 ret = samba_kdc_fetch_client(context, kdc_db_ctx, mem_ctx, principal, entry_ex);
1347 if (ret != HDB_ERR_NOENTRY) goto done;
1349 if (flags & HDB_F_GET_SERVER) {
1350 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
1351 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, krbtgt_number, entry_ex);
1352 if (ret != HDB_ERR_NOENTRY) goto done;
1354 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
1355 ret = samba_kdc_fetch_server(context, kdc_db_ctx, mem_ctx, principal, entry_ex);
1356 if (ret != HDB_ERR_NOENTRY) goto done;
1358 if (flags & HDB_F_GET_KRBTGT) {
1359 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, krbtgt_number, entry_ex);
1360 if (ret != HDB_ERR_NOENTRY) goto done;
1364 talloc_free(mem_ctx);
1368 struct samba_kdc_seq {
1371 struct ldb_message **msgs;
1372 struct ldb_dn *realm_dn;
1375 static krb5_error_code samba_kdc_seq(krb5_context context,
1376 struct samba_kdc_db_context *kdc_db_ctx,
1377 hdb_entry_ex *entry)
1379 krb5_error_code ret;
1380 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
1381 TALLOC_CTX *mem_ctx;
1382 hdb_entry_ex entry_ex;
1383 memset(&entry_ex, '\0', sizeof(entry_ex));
1386 return HDB_ERR_NOENTRY;
1389 mem_ctx = talloc_named(priv, 0, "samba_kdc_seq context");
1393 krb5_set_error_message(context, ret, "samba_kdc_seq: talloc_named() failed!");
1397 if (priv->index < priv->count) {
1398 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1399 NULL, SAMBA_KDC_ENT_TYPE_ANY,
1400 priv->realm_dn, priv->msgs[priv->index++], entry);
1402 ret = HDB_ERR_NOENTRY;
1407 kdc_db_ctx->seq_ctx = NULL;
1409 talloc_free(mem_ctx);
1415 krb5_error_code samba_kdc_firstkey(krb5_context context,
1416 struct samba_kdc_db_context *kdc_db_ctx,
1417 hdb_entry_ex *entry)
1419 struct ldb_context *ldb_ctx = kdc_db_ctx->samdb;
1420 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
1422 struct ldb_result *res = NULL;
1423 krb5_error_code ret;
1424 TALLOC_CTX *mem_ctx;
1429 kdc_db_ctx->seq_ctx = NULL;
1432 priv = (struct samba_kdc_seq *) talloc(kdc_db_ctx, struct samba_kdc_seq);
1435 krb5_set_error_message(context, ret, "talloc: out of memory");
1441 priv->realm_dn = ldb_get_default_basedn(ldb_ctx);
1444 mem_ctx = talloc_named(priv, 0, "samba_kdc_firstkey context");
1448 krb5_set_error_message(context, ret, "samba_kdc_firstkey: talloc_named() failed!");
1452 ret = krb5_get_default_realm(context, &realm);
1458 lret = ldb_search(ldb_ctx, priv, &res,
1459 priv->realm_dn, LDB_SCOPE_SUBTREE, user_attrs,
1460 "(objectClass=user)");
1462 if (lret != LDB_SUCCESS) {
1464 return HDB_ERR_NOENTRY;
1467 priv->count = res->count;
1468 priv->msgs = talloc_steal(priv, res->msgs);
1471 kdc_db_ctx->seq_ctx = priv;
1473 ret = samba_kdc_seq(context, kdc_db_ctx, entry);
1477 kdc_db_ctx->seq_ctx = NULL;
1479 talloc_free(mem_ctx);
1484 krb5_error_code samba_kdc_nextkey(krb5_context context,
1485 struct samba_kdc_db_context *kdc_db_ctx,
1486 hdb_entry_ex *entry)
1488 return samba_kdc_seq(context, kdc_db_ctx, entry);
1491 /* Check if a given entry may delegate or do s4u2self to this target principal
1493 * This is currently a very nasty hack - allowing only delegation to itself.
1495 * This is shared between the constrained delegation and S4U2Self code.
1498 samba_kdc_check_identical_client_and_server(krb5_context context,
1499 struct samba_kdc_db_context *kdc_db_ctx,
1500 hdb_entry_ex *entry,
1501 krb5_const_principal target_principal)
1503 krb5_error_code ret;
1504 krb5_principal enterprise_prinicpal = NULL;
1505 struct ldb_dn *realm_dn;
1506 struct ldb_message *msg;
1507 struct dom_sid *orig_sid;
1508 struct dom_sid *target_sid;
1509 struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
1510 const char *delegation_check_attrs[] = {
1514 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_constrained_delegation");
1518 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
1522 if (target_principal->name.name_type == KRB5_NT_ENTERPRISE_PRINCIPAL) {
1523 /* Need to reparse the enterprise principal to find the real target */
1524 if (target_principal->name.name_string.len != 1) {
1525 ret = KRB5_PARSE_MALFORMED;
1526 krb5_set_error_message(context, ret, "samba_kdc_check_constrained_delegation: request for delegation to enterprise principal with wrong (%d) number of components",
1527 target_principal->name.name_string.len);
1528 talloc_free(mem_ctx);
1531 ret = krb5_parse_name(context, target_principal->name.name_string.val[0],
1532 &enterprise_prinicpal);
1534 talloc_free(mem_ctx);
1537 target_principal = enterprise_prinicpal;
1540 ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, target_principal,
1541 delegation_check_attrs, &realm_dn, &msg);
1543 krb5_free_principal(context, enterprise_prinicpal);
1546 talloc_free(mem_ctx);
1550 orig_sid = samdb_result_dom_sid(mem_ctx, p->msg, "objectSid");
1551 target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
1553 /* Allow delegation to the same principal, even if by a different
1554 * name. The easy and safe way to prove this is by SID
1556 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
1557 talloc_free(mem_ctx);
1558 return KRB5KDC_ERR_BADOPTION;
1561 talloc_free(mem_ctx);
1565 /* Certificates printed by a the Certificate Authority might have a
1566 * slightly different form of the user principal name to that in the
1567 * database. Allow a mismatch where they both refer to the same
1571 samba_kdc_check_pkinit_ms_upn_match(krb5_context context,
1572 struct samba_kdc_db_context *kdc_db_ctx,
1573 hdb_entry_ex *entry,
1574 krb5_const_principal certificate_principal)
1576 krb5_error_code ret;
1577 struct ldb_dn *realm_dn;
1578 struct ldb_message *msg;
1579 struct dom_sid *orig_sid;
1580 struct dom_sid *target_sid;
1581 struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
1582 const char *ms_upn_check_attrs[] = {
1586 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_pkinit_ms_upn_match");
1590 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
1594 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
1595 mem_ctx, certificate_principal,
1596 ms_upn_check_attrs, &realm_dn, &msg);
1599 talloc_free(mem_ctx);
1603 orig_sid = samdb_result_dom_sid(mem_ctx, p->msg, "objectSid");
1604 target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
1606 /* Consider these to be the same principal, even if by a different
1607 * name. The easy and safe way to prove this is by SID
1609 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
1610 talloc_free(mem_ctx);
1611 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
1614 talloc_free(mem_ctx);
1618 NTSTATUS samba_kdc_setup_db_ctx(TALLOC_CTX *mem_ctx, struct samba_kdc_base_context *base_ctx,
1619 struct samba_kdc_db_context **kdc_db_ctx_out)
1623 struct ldb_message *msg;
1624 struct auth_session_info *session_info;
1625 struct samba_kdc_db_context *kdc_db_ctx;
1626 /* The idea here is very simple. Using Kerberos to
1627 * authenticate the KDC to the LDAP server is higly likely to
1630 * In future we may set this up to use EXERNAL and SSL
1631 * certificates, for now it will almost certainly be NTLMSSP_SET_USERNAME
1634 kdc_db_ctx = talloc_zero(mem_ctx, struct samba_kdc_db_context);
1635 if (kdc_db_ctx == NULL) {
1636 return NT_STATUS_NO_MEMORY;
1638 kdc_db_ctx->ev_ctx = base_ctx->ev_ctx;
1639 kdc_db_ctx->lp_ctx = base_ctx->lp_ctx;
1642 /* we would prefer to use system_session(), as that would
1643 * allow us to share the samdb backend context with other parts of the
1644 * system. For now we can't as we need to override the
1645 * credentials to set CRED_DONT_USE_KERBEROS, which would
1646 * break other users of the system_session */
1647 DEBUG(0,("FIXME: Using new system session for hdb\n"));
1648 nt_status = auth_system_session_info(kdc_db_ctx, base_ctx->lp_ctx, &session_info);
1649 if (!NT_STATUS_IS_OK(nt_status)) {
1653 session_info = system_session(kdc_db_ctx->lp_ctx);
1654 if (session_info == NULL) {
1655 return NT_STATUS_INTERNAL_ERROR;
1659 cli_credentials_set_kerberos_state(session_info->credentials,
1660 CRED_DONT_USE_KERBEROS);
1662 /* Setup the link to LDB */
1663 kdc_db_ctx->samdb = samdb_connect(kdc_db_ctx, base_ctx->ev_ctx,
1664 base_ctx->lp_ctx, session_info);
1665 if (kdc_db_ctx->samdb == NULL) {
1666 DEBUG(1, ("hdb_samba4_create: Cannot open samdb for KDC backend!"));
1667 talloc_free(kdc_db_ctx);
1668 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1671 /* Find out our own krbtgt kvno */
1672 ldb_ret = samdb_rodc(kdc_db_ctx->samdb, &kdc_db_ctx->rodc);
1673 if (ldb_ret != LDB_SUCCESS) {
1674 DEBUG(1, ("hdb_samba4_create: Cannot determine if we are an RODC in KDC backend: %s\n",
1675 ldb_errstring(kdc_db_ctx->samdb)));
1676 talloc_free(kdc_db_ctx);
1677 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1679 if (kdc_db_ctx->rodc) {
1680 int my_krbtgt_number;
1681 const char *secondary_keytab[] = { "msDS-SecondaryKrbTgtNumber", NULL };
1682 struct ldb_dn *account_dn;
1683 struct ldb_dn *server_dn = samdb_server_dn(kdc_db_ctx->samdb, kdc_db_ctx);
1685 DEBUG(1, ("hdb_samba4_create: Cannot determine server DN in KDC backend: %s\n",
1686 ldb_errstring(kdc_db_ctx->samdb)));
1687 talloc_free(kdc_db_ctx);
1688 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1691 ldb_ret = samdb_reference_dn(kdc_db_ctx->samdb, kdc_db_ctx, server_dn,
1692 "serverReference", &account_dn);
1693 if (ldb_ret != LDB_SUCCESS) {
1694 DEBUG(1, ("hdb_samba4_create: Cannot determine server account in KDC backend: %s\n",
1695 ldb_errstring(kdc_db_ctx->samdb)));
1696 talloc_free(kdc_db_ctx);
1697 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1700 ldb_ret = samdb_reference_dn(kdc_db_ctx->samdb, kdc_db_ctx, account_dn,
1701 "msDS-KrbTgtLink", &kdc_db_ctx->krbtgt_dn);
1702 talloc_free(account_dn);
1703 if (ldb_ret != LDB_SUCCESS) {
1704 DEBUG(1, ("hdb_samba4_create: Cannot determine RODC krbtgt account in KDC backend: %s\n",
1705 ldb_errstring(kdc_db_ctx->samdb)));
1706 talloc_free(kdc_db_ctx);
1707 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1710 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb, kdc_db_ctx,
1711 &msg, kdc_db_ctx->krbtgt_dn, LDB_SCOPE_BASE,
1714 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=*))");
1715 if (ldb_ret != LDB_SUCCESS) {
1716 DEBUG(1, ("hdb_samba4_create: Cannot read krbtgt account %s in KDC backend to get msDS-SecondaryKrbTgtNumber: %s: %s\n",
1717 ldb_dn_get_linearized(kdc_db_ctx->krbtgt_dn),
1718 ldb_errstring(kdc_db_ctx->samdb),
1719 ldb_strerror(ldb_ret)));
1720 talloc_free(kdc_db_ctx);
1721 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1723 my_krbtgt_number = ldb_msg_find_attr_as_int(msg, "msDS-SecondaryKrbTgtNumber", -1);
1724 if (my_krbtgt_number == -1) {
1725 DEBUG(1, ("hdb_samba4_create: Cannot read msDS-SecondaryKrbTgtNumber from krbtgt account %s in KDC backend: got %d\n",
1726 ldb_dn_get_linearized(kdc_db_ctx->krbtgt_dn),
1728 talloc_free(kdc_db_ctx);
1729 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1731 kdc_db_ctx->my_krbtgt_number = my_krbtgt_number;
1734 kdc_db_ctx->my_krbtgt_number = 0;
1735 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb, kdc_db_ctx,
1736 &msg, NULL, LDB_SCOPE_SUBTREE,
1738 DSDB_SEARCH_SHOW_EXTENDED_DN,
1739 "(&(objectClass=user)(samAccountName=krbtgt))");
1741 if (ldb_ret != LDB_SUCCESS) {
1742 DEBUG(1, ("samba_kdc_fetch: could not find own KRBTGT in DB: %s\n", ldb_errstring(kdc_db_ctx->samdb)));
1743 talloc_free(kdc_db_ctx);
1744 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1746 kdc_db_ctx->krbtgt_dn = talloc_steal(kdc_db_ctx, msg->dn);
1747 kdc_db_ctx->my_krbtgt_number = 0;
1750 *kdc_db_ctx_out = kdc_db_ctx;
1751 return NT_STATUS_OK;