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;
217 uint32_t supported_enctypes;
219 if (rid == DOMAIN_RID_KRBTGT || is_rodc) {
220 /* KDCs (and KDCs on RODCs) use AES, but not DES */
221 supported_enctypes = ENC_ALL_TYPES;
222 supported_enctypes &= ~(ENC_CRC32|ENC_RSA_MD5);
223 } else if (userAccountControl & (UF_PARTIAL_SECRETS_ACCOUNT|UF_SERVER_TRUST_ACCOUNT)) {
224 /* DCs and RODCs comptuer accounts use AES */
225 supported_enctypes = ENC_ALL_TYPES;
226 } else if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT ||
227 (ent_type == SAMBA_KDC_ENT_TYPE_ANY)) {
228 /* for AS-REQ the client chooses the enc types it
229 * supports, and this will vary between computers a
230 * user logs in from. However, some accounts may be
231 * banned from using DES, so allow the default to be
234 * likewise for 'any' return as much as is supported,
235 * to export into a keytab */
236 supported_enctypes = ldb_msg_find_attr_as_uint(msg, "msDS-SupportedEncryptionTypes",
239 /* However, if this is a TGS-REQ, then lock it down to
240 * a reasonable guess as to what the server can decode
241 * - we must use whatever is in
242 * "msDS-SupportedEncryptionTypes", or the 'old' set
243 * of keys (ie, what Windows 2000 supported) */
244 supported_enctypes = ldb_msg_find_attr_as_uint(msg, "msDS-SupportedEncryptionTypes",
245 ENC_CRC32 | ENC_RSA_MD5 | ENC_RC4_HMAC_MD5);
248 /* Is this the krbtgt or a RODC krbtgt */
250 rodc_krbtgt_number = ldb_msg_find_attr_as_int(msg, "msDS-SecondaryKrbTgtNumber", -1);
252 if (rodc_krbtgt_number == -1) {
258 /* If UF_USE_DES_KEY_ONLY has been set, then don't allow use of the newer enc types */
259 if (userAccountControl & UF_USE_DES_KEY_ONLY) {
260 /* However, this still won't allow use of DES, if we
261 * were told not to by msDS-SupportedEncTypes */
262 supported_enctypes &= ENC_CRC32|ENC_RSA_MD5;
265 case SAMBA_KDC_ENT_TYPE_KRBTGT:
266 case SAMBA_KDC_ENT_TYPE_TRUST:
267 /* Unless a very special effort it made,
268 * disallow trust tickets to be DES encrypted,
269 * it's just too dangerous */
270 supported_enctypes &= ~(ENC_CRC32|ENC_RSA_MD5);
274 /* No further restrictions */
278 entry_ex->entry.keys.val = NULL;
279 entry_ex->entry.keys.len = 0;
281 entry_ex->entry.kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
283 entry_ex->entry.kvno |= (rodc_krbtgt_number << 16);
286 /* Get keys from the db */
288 hash = samdb_result_hash(mem_ctx, msg, "unicodePwd");
289 sc_val = ldb_msg_find_ldb_val(msg, "supplementalCredentials");
291 /* unicodePwd for enctype 0x17 (23) if present */
296 /* supplementalCredentials if present */
298 ndr_err = ndr_pull_struct_blob_all(sc_val, mem_ctx, &scb,
299 (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob);
300 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
301 dump_data(0, sc_val->data, sc_val->length);
306 if (scb.sub.signature != SUPPLEMENTAL_CREDENTIALS_SIGNATURE) {
307 NDR_PRINT_DEBUG(supplementalCredentialsBlob, &scb);
312 for (i=0; i < scb.sub.num_packages; i++) {
313 if (strcmp("Primary:Kerberos-Newer-Keys", scb.sub.packages[i].name) == 0) {
314 scpk = &scb.sub.packages[i];
315 if (!scpk->data || !scpk->data[0]) {
321 } else if (strcmp("Primary:Kerberos", scb.sub.packages[i].name) == 0) {
322 scpk = &scb.sub.packages[i];
323 if (!scpk->data || !scpk->data[0]) {
327 * we don't break here in hope to find
328 * a Kerberos-Newer-Keys package
334 * Primary:Kerberos-Newer-Keys or Primary:Kerberos element
335 * of supplementalCredentials
340 blob = strhex_to_data_blob(mem_ctx, scpk->data);
346 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
347 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &_pkb,
348 (ndr_pull_flags_fn_t)ndr_pull_package_PrimaryKerberosBlob);
349 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
351 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
352 krb5_warnx(context, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
356 if (newer_keys && _pkb.version != 4) {
358 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
359 krb5_warnx(context, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
363 if (!newer_keys && _pkb.version != 3) {
365 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
366 krb5_warnx(context, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
370 if (_pkb.version == 4) {
371 pkb4 = &_pkb.ctr.ctr4;
372 allocated_keys += pkb4->num_keys;
373 } else if (_pkb.version == 3) {
374 pkb3 = &_pkb.ctr.ctr3;
375 allocated_keys += pkb3->num_keys;
379 if (allocated_keys == 0) {
380 /* oh, no password. Apparently (comment in
381 * hdb-ldap.c) this violates the ASN.1, but this
382 * allows an entry with no keys (yet). */
386 /* allocate space to decode into */
387 entry_ex->entry.keys.len = 0;
388 entry_ex->entry.keys.val = calloc(allocated_keys, sizeof(Key));
389 if (entry_ex->entry.keys.val == NULL) {
394 if (hash && (supported_enctypes & ENC_RC4_HMAC_MD5)) {
398 key.salt = NULL; /* No salt for this enc type */
400 ret = krb5_keyblock_init(context,
401 ENCTYPE_ARCFOUR_HMAC,
402 hash->hash, sizeof(hash->hash),
408 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
409 entry_ex->entry.keys.len++;
413 for (i=0; i < pkb4->num_keys; i++) {
416 if (!pkb4->keys[i].value) continue;
418 if (!(kerberos_enctype_to_bitmap(pkb4->keys[i].keytype) & supported_enctypes)) {
425 if (pkb4->salt.string) {
428 salt = data_blob_string_const(pkb4->salt.string);
430 key.salt = calloc(1, sizeof(*key.salt));
431 if (key.salt == NULL) {
436 key.salt->type = hdb_pw_salt;
438 ret = krb5_data_copy(&key.salt->salt, salt.data, salt.length);
446 /* TODO: maybe pass the iteration_count somehow... */
448 ret = krb5_keyblock_init(context,
449 pkb4->keys[i].keytype,
450 pkb4->keys[i].value->data,
451 pkb4->keys[i].value->length,
453 if (ret == KRB5_PROG_ETYPE_NOSUPP) {
454 DEBUG(2,("Unsupported keytype ignored - type %u\n",
455 pkb4->keys[i].keytype));
468 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
469 entry_ex->entry.keys.len++;
472 for (i=0; i < pkb3->num_keys; i++) {
475 if (!pkb3->keys[i].value) continue;
477 if (!(kerberos_enctype_to_bitmap(pkb3->keys[i].keytype) & supported_enctypes)) {
484 if (pkb3->salt.string) {
487 salt = data_blob_string_const(pkb3->salt.string);
489 key.salt = calloc(1, sizeof(*key.salt));
490 if (key.salt == NULL) {
495 key.salt->type = hdb_pw_salt;
497 ret = krb5_data_copy(&key.salt->salt, salt.data, salt.length);
505 ret = krb5_keyblock_init(context,
506 pkb3->keys[i].keytype,
507 pkb3->keys[i].value->data,
508 pkb3->keys[i].value->length,
519 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
520 entry_ex->entry.keys.len++;
526 entry_ex->entry.keys.len = 0;
528 if (entry_ex->entry.keys.len == 0 && entry_ex->entry.keys.val) {
529 free(entry_ex->entry.keys.val);
530 entry_ex->entry.keys.val = NULL;
536 * Construct an hdb_entry from a directory entry.
538 static krb5_error_code samba_kdc_message2entry(krb5_context context,
539 struct samba_kdc_db_context *kdc_db_ctx,
540 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
541 enum samba_kdc_ent_type ent_type,
542 struct ldb_dn *realm_dn,
543 struct ldb_message *msg,
544 hdb_entry_ex *entry_ex)
546 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
547 unsigned int userAccountControl;
549 krb5_error_code ret = 0;
550 krb5_boolean is_computer = FALSE;
551 char *realm = strupper_talloc(mem_ctx, lpcfg_realm(lp_ctx));
553 struct samba_kdc_entry *p;
558 bool is_rodc = false;
559 struct ldb_message_element *objectclasses;
560 struct ldb_val computer_val;
561 const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
562 computer_val.data = discard_const_p(uint8_t,"computer");
563 computer_val.length = strlen((const char *)computer_val.data);
565 if (ldb_msg_find_element(msg, "msDS-SecondaryKrbTgtNumber")) {
569 if (!samAccountName) {
571 krb5_set_error_message(context, ret, "samba_kdc_message2entry: no samAccountName present");
575 objectclasses = ldb_msg_find_element(msg, "objectClass");
577 if (objectclasses && ldb_msg_find_val(objectclasses, &computer_val)) {
581 memset(entry_ex, 0, sizeof(*entry_ex));
585 krb5_set_error_message(context, ret, "talloc_strdup: out of memory");
589 p = talloc(mem_ctx, struct samba_kdc_entry);
595 p->kdc_db_ctx = kdc_db_ctx;
596 p->entry_ex = entry_ex;
597 p->realm_dn = talloc_reference(p, realm_dn);
603 talloc_set_destructor(p, samba_kdc_entry_destructor);
605 /* make sure we do not have bogus data in there */
606 memset(&entry_ex->entry, 0, sizeof(hdb_entry));
609 entry_ex->free_entry = samba_kdc_free_entry;
611 userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
614 entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
615 if (ent_type == SAMBA_KDC_ENT_TYPE_ANY && principal == NULL) {
616 krb5_make_principal(context, &entry_ex->entry.principal, realm, samAccountName, NULL);
618 ret = copy_Principal(principal, entry_ex->entry.principal);
620 krb5_clear_error_message(context);
624 /* While we have copied the client principal, tests
625 * show that Win2k3 returns the 'corrected' realm, not
626 * the client-specified realm. This code attempts to
627 * replace the client principal's realm with the one
628 * we determine from our records */
630 /* this has to be with malloc() */
631 krb5_principal_set_realm(context, entry_ex->entry.principal, realm);
634 /* First try and figure out the flags based on the userAccountControl */
635 entry_ex->entry.flags = uf2HDBFlags(context, userAccountControl, ent_type);
637 /* Windows 2008 seems to enforce this (very sensible) rule by
638 * default - don't allow offline attacks on a user's password
639 * by asking for a ticket to them as a service (encrypted with
640 * their probably patheticly insecure password) */
642 if (entry_ex->entry.flags.server
643 && lpcfg_parm_bool(lp_ctx, NULL, "kdc", "require spn for service", true)) {
644 if (!is_computer && !ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL)) {
645 entry_ex->entry.flags.server = 0;
650 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
651 * of the Heimdal KDC. They are stored in a the traditional
652 * DB for audit purposes, and still form part of the structure
655 /* use 'whenCreated' */
656 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
657 /* use 'kadmin' for now (needed by mit_samba) */
658 krb5_make_principal(context,
659 &entry_ex->entry.created_by.principal,
660 realm, "kadmin", NULL);
662 entry_ex->entry.modified_by = (Event *) malloc(sizeof(Event));
663 if (entry_ex->entry.modified_by == NULL) {
665 krb5_set_error_message(context, ret, "malloc: out of memory");
669 /* use 'whenChanged' */
670 entry_ex->entry.modified_by->time = ldb_msg_find_krb5time_ldap_time(msg, "whenChanged", 0);
671 /* use 'kadmin' for now (needed by mit_samba) */
672 krb5_make_principal(context,
673 &entry_ex->entry.modified_by->principal,
674 realm, "kadmin", NULL);
678 /* The lack of password controls etc applies to krbtgt by
679 * virtue of being that particular RID */
680 status = dom_sid_split_rid(NULL, samdb_result_dom_sid(mem_ctx, msg, "objectSid"), NULL, &rid);
682 if (!NT_STATUS_IS_OK(status)) {
687 if (rid == DOMAIN_RID_KRBTGT) {
688 entry_ex->entry.valid_end = NULL;
689 entry_ex->entry.pw_end = NULL;
691 entry_ex->entry.flags.invalid = 0;
692 entry_ex->entry.flags.server = 1;
694 /* Don't mark all requests for the krbtgt/realm as
695 * 'change password', as otherwise we could get into
696 * trouble, and not enforce the password expirty.
697 * Instead, only do it when request is for the kpasswd service */
698 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER
699 && principal->name.name_string.len == 2
700 && (strcmp(principal->name.name_string.val[0], "kadmin") == 0)
701 && (strcmp(principal->name.name_string.val[1], "changepw") == 0)
702 && lpcfg_is_my_domain_or_realm(lp_ctx, principal->realm)) {
703 entry_ex->entry.flags.change_pw = 1;
705 entry_ex->entry.flags.client = 0;
706 entry_ex->entry.flags.forwardable = 1;
707 entry_ex->entry.flags.ok_as_delegate = 1;
708 } else if (is_rodc) {
709 /* The RODC krbtgt account is like the main krbtgt,
710 * but it does not have a changepw or kadmin
713 entry_ex->entry.valid_end = NULL;
714 entry_ex->entry.pw_end = NULL;
716 /* Also don't allow the RODC krbtgt to be a client (it should not be needed) */
717 entry_ex->entry.flags.client = 0;
718 entry_ex->entry.flags.invalid = 0;
719 entry_ex->entry.flags.server = 1;
721 entry_ex->entry.flags.client = 0;
722 entry_ex->entry.flags.forwardable = 1;
723 entry_ex->entry.flags.ok_as_delegate = 0;
724 } else if (entry_ex->entry.flags.server && ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
725 /* The account/password expiry only applies when the account is used as a
726 * client (ie password login), not when used as a server */
728 /* Make very well sure we don't use this for a client,
729 * it could bypass the password restrictions */
730 entry_ex->entry.flags.client = 0;
732 entry_ex->entry.valid_end = NULL;
733 entry_ex->entry.pw_end = NULL;
736 NTTIME must_change_time
737 = samdb_result_force_password_change(kdc_db_ctx->samdb, mem_ctx,
739 if (must_change_time == 0x7FFFFFFFFFFFFFFFULL) {
740 entry_ex->entry.pw_end = NULL;
742 entry_ex->entry.pw_end = malloc(sizeof(*entry_ex->entry.pw_end));
743 if (entry_ex->entry.pw_end == NULL) {
747 *entry_ex->entry.pw_end = nt_time_to_unix(must_change_time);
750 acct_expiry = samdb_result_account_expires(msg);
751 if (acct_expiry == 0x7FFFFFFFFFFFFFFFULL) {
752 entry_ex->entry.valid_end = NULL;
754 entry_ex->entry.valid_end = malloc(sizeof(*entry_ex->entry.valid_end));
755 if (entry_ex->entry.valid_end == NULL) {
759 *entry_ex->entry.valid_end = nt_time_to_unix(acct_expiry);
763 entry_ex->entry.valid_start = NULL;
765 entry_ex->entry.max_life = NULL;
767 entry_ex->entry.max_renew = NULL;
769 entry_ex->entry.generation = NULL;
771 /* Get keys from the db */
772 ret = samba_kdc_message2entry_keys(context, p, msg,
773 rid, is_rodc, userAccountControl,
776 /* Could be bougus data in the entry, or out of memory */
780 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
781 if (entry_ex->entry.etypes == NULL) {
782 krb5_clear_error_message(context);
786 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
787 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
788 if (entry_ex->entry.etypes->val == NULL) {
789 krb5_clear_error_message(context);
793 for (i=0; i < entry_ex->entry.etypes->len; i++) {
794 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
798 p->msg = talloc_steal(p, msg);
802 /* This doesn't free ent itself, that is for the eventual caller to do */
803 hdb_free_entry(context, entry_ex);
805 talloc_steal(kdc_db_ctx, entry_ex->ctx);
812 * Construct an hdb_entry from a directory entry.
814 static krb5_error_code samba_kdc_trust_message2entry(krb5_context context,
815 struct samba_kdc_db_context *kdc_db_ctx,
816 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
817 enum trust_direction direction,
818 struct ldb_dn *realm_dn,
819 struct ldb_message *msg,
820 hdb_entry_ex *entry_ex)
822 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
823 const char *dnsdomain;
824 char *realm = strupper_talloc(mem_ctx, lpcfg_realm(lp_ctx));
825 DATA_BLOB password_utf16;
826 struct samr_Password password_hash;
827 const struct ldb_val *password_val;
828 struct trustAuthInOutBlob password_blob;
829 struct samba_kdc_entry *p;
831 enum ndr_err_code ndr_err;
832 int ret, trust_direction_flags;
835 p = talloc(mem_ctx, struct samba_kdc_entry);
841 p->kdc_db_ctx = kdc_db_ctx;
842 p->entry_ex = entry_ex;
843 p->realm_dn = realm_dn;
845 talloc_set_destructor(p, samba_kdc_entry_destructor);
847 /* make sure we do not have bogus data in there */
848 memset(&entry_ex->entry, 0, sizeof(hdb_entry));
851 entry_ex->free_entry = samba_kdc_free_entry;
853 /* use 'whenCreated' */
854 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
855 /* use 'kadmin' for now (needed by mit_samba) */
856 krb5_make_principal(context,
857 &entry_ex->entry.created_by.principal,
858 realm, "kadmin", NULL);
860 entry_ex->entry.valid_start = NULL;
862 trust_direction_flags = ldb_msg_find_attr_as_int(msg, "trustDirection", 0);
864 if (direction == INBOUND) {
865 password_val = ldb_msg_find_ldb_val(msg, "trustAuthIncoming");
867 } else { /* OUTBOUND */
868 dnsdomain = ldb_msg_find_attr_as_string(msg, "trustPartner", NULL);
871 realm = strupper_talloc(mem_ctx, dnsdomain);
872 password_val = ldb_msg_find_ldb_val(msg, "trustAuthOutgoing");
875 if (!password_val || !(trust_direction_flags & direction)) {
880 ndr_err = ndr_pull_struct_blob(password_val, mem_ctx, &password_blob,
881 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
882 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
887 entry_ex->entry.kvno = -1;
888 for (i=0; i < password_blob.count; i++) {
889 if (password_blob.current.array[i].AuthType == TRUST_AUTH_TYPE_VERSION) {
890 entry_ex->entry.kvno = password_blob.current.array[i].AuthInfo.version.version;
894 for (i=0; i < password_blob.count; i++) {
895 if (password_blob.current.array[i].AuthType == TRUST_AUTH_TYPE_CLEAR) {
896 password_utf16 = data_blob_const(password_blob.current.array[i].AuthInfo.clear.password,
897 password_blob.current.array[i].AuthInfo.clear.size);
898 /* In the future, generate all sorts of
899 * hashes, but for now we can't safely convert
900 * the random strings windows uses into
903 /* but as it is utf16 already, we can get the NT password/arcfour-hmac-md5 key */
904 mdfour(password_hash.hash, password_utf16.data, password_utf16.length);
906 } else if (password_blob.current.array[i].AuthType == TRUST_AUTH_TYPE_NT4OWF) {
907 password_hash = password_blob.current.array[i].AuthInfo.nt4owf.password;
912 if (i < password_blob.count) {
914 /* Must have found a cleartext or MD4 password */
915 entry_ex->entry.keys.val = calloc(1, sizeof(Key));
918 key.salt = NULL; /* No salt for this enc type */
920 if (entry_ex->entry.keys.val == NULL) {
925 ret = krb5_keyblock_init(context,
926 ENCTYPE_ARCFOUR_HMAC,
927 password_hash.hash, sizeof(password_hash.hash),
930 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
931 entry_ex->entry.keys.len++;
934 entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
936 ret = copy_Principal(principal, entry_ex->entry.principal);
938 krb5_clear_error_message(context);
942 /* While we have copied the client principal, tests
943 * show that Win2k3 returns the 'corrected' realm, not
944 * the client-specified realm. This code attempts to
945 * replace the client principal's realm with the one
946 * we determine from our records */
948 krb5_principal_set_realm(context, entry_ex->entry.principal, realm);
949 entry_ex->entry.flags = int2HDBFlags(0);
950 entry_ex->entry.flags.immutable = 1;
951 entry_ex->entry.flags.invalid = 0;
952 entry_ex->entry.flags.server = 1;
953 entry_ex->entry.flags.require_preauth = 1;
955 entry_ex->entry.pw_end = NULL;
957 entry_ex->entry.max_life = NULL;
959 entry_ex->entry.max_renew = NULL;
961 entry_ex->entry.generation = NULL;
963 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
964 if (entry_ex->entry.etypes == NULL) {
965 krb5_clear_error_message(context);
969 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
970 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
971 if (entry_ex->entry.etypes->val == NULL) {
972 krb5_clear_error_message(context);
976 for (i=0; i < entry_ex->entry.etypes->len; i++) {
977 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
981 p->msg = talloc_steal(p, msg);
985 /* This doesn't free ent itself, that is for the eventual caller to do */
986 hdb_free_entry(context, entry_ex);
988 talloc_steal(kdc_db_ctx, entry_ex->ctx);
995 static krb5_error_code samba_kdc_lookup_trust(krb5_context context, struct ldb_context *ldb_ctx,
998 struct ldb_dn *realm_dn,
999 struct ldb_message **pmsg)
1002 krb5_error_code ret;
1003 char *filter = NULL;
1004 const char * const *attrs = trust_attrs;
1006 struct ldb_result *res = NULL;
1007 filter = talloc_asprintf(mem_ctx, "(&(objectClass=trustedDomain)(|(flatname=%s)(trustPartner=%s)))", realm, realm);
1011 krb5_set_error_message(context, ret, "talloc_asprintf: out of memory");
1015 lret = ldb_search(ldb_ctx, mem_ctx, &res,
1016 ldb_get_default_basedn(ldb_ctx),
1017 LDB_SCOPE_SUBTREE, attrs, "%s", filter);
1018 if (lret != LDB_SUCCESS) {
1019 DEBUG(3, ("Failed to search for %s: %s\n", filter, ldb_errstring(ldb_ctx)));
1020 return HDB_ERR_NOENTRY;
1021 } else if (res->count == 0 || res->count > 1) {
1022 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter, res->count));
1024 return HDB_ERR_NOENTRY;
1026 talloc_steal(mem_ctx, res->msgs);
1027 *pmsg = res->msgs[0];
1032 static krb5_error_code samba_kdc_lookup_client(krb5_context context,
1033 struct samba_kdc_db_context *kdc_db_ctx,
1034 TALLOC_CTX *mem_ctx,
1035 krb5_const_principal principal,
1037 struct ldb_dn **realm_dn,
1038 struct ldb_message **msg) {
1040 char *principal_string;
1041 krb5_error_code ret;
1043 ret = krb5_unparse_name(context, principal, &principal_string);
1049 nt_status = sam_get_results_principal(kdc_db_ctx->samdb,
1050 mem_ctx, principal_string, attrs,
1052 free(principal_string);
1053 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
1054 return HDB_ERR_NOENTRY;
1055 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
1057 } else if (!NT_STATUS_IS_OK(nt_status)) {
1064 static krb5_error_code samba_kdc_fetch_client(krb5_context context,
1065 struct samba_kdc_db_context *kdc_db_ctx,
1066 TALLOC_CTX *mem_ctx,
1067 krb5_const_principal principal,
1068 hdb_entry_ex *entry_ex) {
1069 struct ldb_dn *realm_dn;
1070 krb5_error_code ret;
1071 struct ldb_message *msg = NULL;
1073 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
1074 mem_ctx, principal, user_attrs,
1080 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1081 principal, SAMBA_KDC_ENT_TYPE_CLIENT,
1082 realm_dn, msg, entry_ex);
1086 static krb5_error_code samba_kdc_fetch_krbtgt(krb5_context context,
1087 struct samba_kdc_db_context *kdc_db_ctx,
1088 TALLOC_CTX *mem_ctx,
1089 krb5_const_principal principal,
1090 uint32_t krbtgt_number,
1091 hdb_entry_ex *entry_ex)
1093 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
1094 krb5_error_code ret;
1095 struct ldb_message *msg = NULL;
1096 struct ldb_dn *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
1098 krb5_principal alloc_principal = NULL;
1099 if (principal->name.name_string.len != 2
1100 || (strcmp(principal->name.name_string.val[0], KRB5_TGS_NAME) != 0)) {
1102 return HDB_ERR_NOENTRY;
1105 /* krbtgt case. Either us or a trusted realm */
1107 if (lpcfg_is_my_domain_or_realm(lp_ctx, principal->realm)
1108 && lpcfg_is_my_domain_or_realm(lp_ctx, principal->name.name_string.val[1])) {
1109 /* us, or someone quite like us */
1110 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1111 * is in our db, then direct the caller at our primary
1117 if (krbtgt_number == kdc_db_ctx->my_krbtgt_number) {
1118 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
1119 &msg, kdc_db_ctx->krbtgt_dn, LDB_SCOPE_BASE,
1121 "(objectClass=user)");
1123 /* We need to look up an RODC krbtgt (perhaps
1124 * ours, if we are an RODC, perhaps another
1125 * RODC if we are a read-write DC */
1126 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
1127 &msg, realm_dn, LDB_SCOPE_SUBTREE,
1129 DSDB_SEARCH_SHOW_EXTENDED_DN,
1130 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=%u))", (unsigned)(krbtgt_number));
1133 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
1134 krb5_warnx(context, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1135 (unsigned)(krbtgt_number));
1136 krb5_set_error_message(context, HDB_ERR_NOENTRY,
1137 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1138 (unsigned)(krbtgt_number));
1139 return HDB_ERR_NOENTRY;
1140 } else if (lret != LDB_SUCCESS) {
1141 krb5_warnx(context, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1142 (unsigned)(krbtgt_number));
1143 krb5_set_error_message(context, HDB_ERR_NOENTRY,
1144 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1145 (unsigned)(krbtgt_number));
1146 return HDB_ERR_NOENTRY;
1149 realm_fixed = strupper_talloc(mem_ctx, lpcfg_realm(lp_ctx));
1152 krb5_set_error_message(context, ret, "strupper_talloc: out of memory");
1156 ret = krb5_copy_principal(context, principal, &alloc_principal);
1161 free(alloc_principal->name.name_string.val[1]);
1162 alloc_principal->name.name_string.val[1] = strdup(realm_fixed);
1163 talloc_free(realm_fixed);
1164 if (!alloc_principal->name.name_string.val[1]) {
1166 krb5_set_error_message(context, ret, "samba_kdc_fetch: strdup() failed!");
1169 principal = alloc_principal;
1171 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1172 principal, SAMBA_KDC_ENT_TYPE_KRBTGT,
1173 realm_dn, msg, entry_ex);
1175 krb5_warnx(context, "samba_kdc_fetch: self krbtgt message2entry failed");
1180 enum trust_direction direction = UNKNOWN;
1181 const char *realm = NULL;
1183 /* Either an inbound or outbound trust */
1185 if (strcasecmp(lpcfg_realm(lp_ctx), principal->realm) == 0) {
1186 /* look for inbound trust */
1187 direction = INBOUND;
1188 realm = principal->name.name_string.val[1];
1189 } else if (strcasecmp(lpcfg_realm(lp_ctx), principal->name.name_string.val[1]) == 0) {
1190 /* look for outbound trust */
1191 direction = OUTBOUND;
1192 realm = principal->realm;
1194 krb5_warnx(context, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1195 principal->realm, principal->name.name_string.val[1]);
1196 krb5_set_error_message(context, HDB_ERR_NOENTRY, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1197 principal->realm, principal->name.name_string.val[1]);
1198 return HDB_ERR_NOENTRY;
1201 /* Trusted domains are under CN=system */
1203 ret = samba_kdc_lookup_trust(context, kdc_db_ctx->samdb,
1205 realm, realm_dn, &msg);
1208 krb5_warnx(context, "samba_kdc_fetch: could not find principal in DB");
1209 krb5_set_error_message(context, ret, "samba_kdc_fetch: could not find principal in DB");
1213 ret = samba_kdc_trust_message2entry(context, kdc_db_ctx, mem_ctx,
1214 principal, direction,
1215 realm_dn, msg, entry_ex);
1217 krb5_warnx(context, "samba_kdc_fetch: trust_message2entry failed");
1224 static krb5_error_code samba_kdc_lookup_server(krb5_context context,
1225 struct samba_kdc_db_context *kdc_db_ctx,
1226 TALLOC_CTX *mem_ctx,
1227 krb5_const_principal principal,
1229 struct ldb_dn **realm_dn,
1230 struct ldb_message **msg)
1232 krb5_error_code ret;
1234 if (principal->name.name_string.len >= 2) {
1235 /* 'normal server' case */
1238 struct ldb_dn *user_dn;
1239 char *principal_string;
1241 ret = krb5_unparse_name_flags(context, principal,
1242 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
1248 /* At this point we may find the host is known to be
1249 * in a different realm, so we should generate a
1250 * referral instead */
1251 nt_status = crack_service_principal_name(kdc_db_ctx->samdb,
1252 mem_ctx, principal_string,
1253 &user_dn, realm_dn);
1254 free(principal_string);
1256 if (!NT_STATUS_IS_OK(nt_status)) {
1257 return HDB_ERR_NOENTRY;
1260 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb,
1262 msg, user_dn, LDB_SCOPE_BASE,
1263 attrs, DSDB_SEARCH_SHOW_EXTENDED_DN, "(objectClass=*)");
1264 if (ldb_ret != LDB_SUCCESS) {
1265 return HDB_ERR_NOENTRY;
1270 char *filter = NULL;
1272 /* server as client principal case, but we must not lookup userPrincipalNames */
1273 *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
1274 realm = krb5_principal_get_realm(context, principal);
1276 /* TODO: Check if it is our realm, otherwise give referall */
1278 ret = krb5_unparse_name_flags(context, principal, KRB5_PRINCIPAL_UNPARSE_NO_REALM, &short_princ);
1281 krb5_set_error_message(context, ret, "samba_kdc_lookup_principal: could not parse principal");
1282 krb5_warnx(context, "samba_kdc_lookup_principal: could not parse principal");
1286 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx, msg,
1287 *realm_dn, LDB_SCOPE_SUBTREE,
1289 DSDB_SEARCH_SHOW_EXTENDED_DN,
1290 "(&(objectClass=user)(samAccountName=%s))",
1291 ldb_binary_encode_string(mem_ctx, short_princ));
1293 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
1294 DEBUG(3, ("Failed find a entry for %s\n", filter));
1295 return HDB_ERR_NOENTRY;
1297 if (lret != LDB_SUCCESS) {
1298 DEBUG(3, ("Failed single search for for %s - %s\n",
1299 filter, ldb_errstring(kdc_db_ctx->samdb)));
1300 return HDB_ERR_NOENTRY;
1307 static krb5_error_code samba_kdc_fetch_server(krb5_context context,
1308 struct samba_kdc_db_context *kdc_db_ctx,
1309 TALLOC_CTX *mem_ctx,
1310 krb5_const_principal principal,
1311 hdb_entry_ex *entry_ex)
1313 krb5_error_code ret;
1314 struct ldb_dn *realm_dn;
1315 struct ldb_message *msg;
1317 ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, principal,
1318 server_attrs, &realm_dn, &msg);
1323 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1324 principal, SAMBA_KDC_ENT_TYPE_SERVER,
1325 realm_dn, msg, entry_ex);
1327 krb5_warnx(context, "samba_kdc_fetch: message2entry failed");
1333 krb5_error_code samba_kdc_fetch(krb5_context context,
1334 struct samba_kdc_db_context *kdc_db_ctx,
1335 krb5_const_principal principal,
1338 hdb_entry_ex *entry_ex)
1340 krb5_error_code ret = HDB_ERR_NOENTRY;
1341 TALLOC_CTX *mem_ctx;
1342 unsigned int krbtgt_number;
1343 if (flags & HDB_F_KVNO_SPECIFIED) {
1344 krbtgt_number = kvno >> 16;
1345 if (kdc_db_ctx->rodc) {
1346 if (krbtgt_number != kdc_db_ctx->my_krbtgt_number) {
1347 return HDB_ERR_NOT_FOUND_HERE;
1351 krbtgt_number = kdc_db_ctx->my_krbtgt_number;
1354 mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_fetch context");
1357 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
1361 if (flags & HDB_F_GET_CLIENT) {
1362 ret = samba_kdc_fetch_client(context, kdc_db_ctx, mem_ctx, principal, entry_ex);
1363 if (ret != HDB_ERR_NOENTRY) goto done;
1365 if (flags & HDB_F_GET_SERVER) {
1366 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
1367 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, krbtgt_number, entry_ex);
1368 if (ret != HDB_ERR_NOENTRY) goto done;
1370 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
1371 ret = samba_kdc_fetch_server(context, kdc_db_ctx, mem_ctx, principal, entry_ex);
1372 if (ret != HDB_ERR_NOENTRY) goto done;
1374 if (flags & HDB_F_GET_KRBTGT) {
1375 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, krbtgt_number, entry_ex);
1376 if (ret != HDB_ERR_NOENTRY) goto done;
1380 talloc_free(mem_ctx);
1384 struct samba_kdc_seq {
1387 struct ldb_message **msgs;
1388 struct ldb_dn *realm_dn;
1391 static krb5_error_code samba_kdc_seq(krb5_context context,
1392 struct samba_kdc_db_context *kdc_db_ctx,
1393 hdb_entry_ex *entry)
1395 krb5_error_code ret;
1396 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
1397 TALLOC_CTX *mem_ctx;
1398 hdb_entry_ex entry_ex;
1399 memset(&entry_ex, '\0', sizeof(entry_ex));
1402 return HDB_ERR_NOENTRY;
1405 mem_ctx = talloc_named(priv, 0, "samba_kdc_seq context");
1409 krb5_set_error_message(context, ret, "samba_kdc_seq: talloc_named() failed!");
1413 if (priv->index < priv->count) {
1414 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1415 NULL, SAMBA_KDC_ENT_TYPE_ANY,
1416 priv->realm_dn, priv->msgs[priv->index++], entry);
1418 ret = HDB_ERR_NOENTRY;
1423 kdc_db_ctx->seq_ctx = NULL;
1425 talloc_free(mem_ctx);
1431 krb5_error_code samba_kdc_firstkey(krb5_context context,
1432 struct samba_kdc_db_context *kdc_db_ctx,
1433 hdb_entry_ex *entry)
1435 struct ldb_context *ldb_ctx = kdc_db_ctx->samdb;
1436 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
1438 struct ldb_result *res = NULL;
1439 krb5_error_code ret;
1440 TALLOC_CTX *mem_ctx;
1445 kdc_db_ctx->seq_ctx = NULL;
1448 priv = (struct samba_kdc_seq *) talloc(kdc_db_ctx, struct samba_kdc_seq);
1451 krb5_set_error_message(context, ret, "talloc: out of memory");
1457 priv->realm_dn = ldb_get_default_basedn(ldb_ctx);
1460 mem_ctx = talloc_named(priv, 0, "samba_kdc_firstkey context");
1464 krb5_set_error_message(context, ret, "samba_kdc_firstkey: talloc_named() failed!");
1468 ret = krb5_get_default_realm(context, &realm);
1474 lret = ldb_search(ldb_ctx, priv, &res,
1475 priv->realm_dn, LDB_SCOPE_SUBTREE, user_attrs,
1476 "(objectClass=user)");
1478 if (lret != LDB_SUCCESS) {
1480 return HDB_ERR_NOENTRY;
1483 priv->count = res->count;
1484 priv->msgs = talloc_steal(priv, res->msgs);
1487 kdc_db_ctx->seq_ctx = priv;
1489 ret = samba_kdc_seq(context, kdc_db_ctx, entry);
1493 kdc_db_ctx->seq_ctx = NULL;
1495 talloc_free(mem_ctx);
1500 krb5_error_code samba_kdc_nextkey(krb5_context context,
1501 struct samba_kdc_db_context *kdc_db_ctx,
1502 hdb_entry_ex *entry)
1504 return samba_kdc_seq(context, kdc_db_ctx, entry);
1507 /* Check if a given entry may delegate or do s4u2self to this target principal
1509 * This is currently a very nasty hack - allowing only delegation to itself.
1511 * This is shared between the constrained delegation and S4U2Self code.
1514 samba_kdc_check_identical_client_and_server(krb5_context context,
1515 struct samba_kdc_db_context *kdc_db_ctx,
1516 hdb_entry_ex *entry,
1517 krb5_const_principal target_principal)
1519 krb5_error_code ret;
1520 krb5_principal enterprise_prinicpal = NULL;
1521 struct ldb_dn *realm_dn;
1522 struct ldb_message *msg;
1523 struct dom_sid *orig_sid;
1524 struct dom_sid *target_sid;
1525 struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
1526 const char *delegation_check_attrs[] = {
1530 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_constrained_delegation");
1534 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
1538 if (target_principal->name.name_type == KRB5_NT_ENTERPRISE_PRINCIPAL) {
1539 /* Need to reparse the enterprise principal to find the real target */
1540 if (target_principal->name.name_string.len != 1) {
1541 ret = KRB5_PARSE_MALFORMED;
1542 krb5_set_error_message(context, ret, "samba_kdc_check_constrained_delegation: request for delegation to enterprise principal with wrong (%d) number of components",
1543 target_principal->name.name_string.len);
1544 talloc_free(mem_ctx);
1547 ret = krb5_parse_name(context, target_principal->name.name_string.val[0],
1548 &enterprise_prinicpal);
1550 talloc_free(mem_ctx);
1553 target_principal = enterprise_prinicpal;
1556 ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, target_principal,
1557 delegation_check_attrs, &realm_dn, &msg);
1559 krb5_free_principal(context, enterprise_prinicpal);
1562 talloc_free(mem_ctx);
1566 orig_sid = samdb_result_dom_sid(mem_ctx, p->msg, "objectSid");
1567 target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
1569 /* Allow delegation to the same principal, even if by a different
1570 * name. The easy and safe way to prove this is by SID
1572 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
1573 talloc_free(mem_ctx);
1574 return KRB5KDC_ERR_BADOPTION;
1577 talloc_free(mem_ctx);
1581 /* Certificates printed by a the Certificate Authority might have a
1582 * slightly different form of the user principal name to that in the
1583 * database. Allow a mismatch where they both refer to the same
1587 samba_kdc_check_pkinit_ms_upn_match(krb5_context context,
1588 struct samba_kdc_db_context *kdc_db_ctx,
1589 hdb_entry_ex *entry,
1590 krb5_const_principal certificate_principal)
1592 krb5_error_code ret;
1593 struct ldb_dn *realm_dn;
1594 struct ldb_message *msg;
1595 struct dom_sid *orig_sid;
1596 struct dom_sid *target_sid;
1597 struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
1598 const char *ms_upn_check_attrs[] = {
1602 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_pkinit_ms_upn_match");
1606 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
1610 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
1611 mem_ctx, certificate_principal,
1612 ms_upn_check_attrs, &realm_dn, &msg);
1615 talloc_free(mem_ctx);
1619 orig_sid = samdb_result_dom_sid(mem_ctx, p->msg, "objectSid");
1620 target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
1622 /* Consider these to be the same principal, even if by a different
1623 * name. The easy and safe way to prove this is by SID
1625 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
1626 talloc_free(mem_ctx);
1627 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
1630 talloc_free(mem_ctx);
1634 NTSTATUS samba_kdc_setup_db_ctx(TALLOC_CTX *mem_ctx, struct samba_kdc_base_context *base_ctx,
1635 struct samba_kdc_db_context **kdc_db_ctx_out)
1639 struct ldb_message *msg;
1640 struct auth_session_info *session_info;
1641 struct samba_kdc_db_context *kdc_db_ctx;
1642 /* The idea here is very simple. Using Kerberos to
1643 * authenticate the KDC to the LDAP server is higly likely to
1646 * In future we may set this up to use EXERNAL and SSL
1647 * certificates, for now it will almost certainly be NTLMSSP_SET_USERNAME
1650 kdc_db_ctx = talloc_zero(mem_ctx, struct samba_kdc_db_context);
1651 if (kdc_db_ctx == NULL) {
1652 return NT_STATUS_NO_MEMORY;
1654 kdc_db_ctx->ev_ctx = base_ctx->ev_ctx;
1655 kdc_db_ctx->lp_ctx = base_ctx->lp_ctx;
1658 /* we would prefer to use system_session(), as that would
1659 * allow us to share the samdb backend context with other parts of the
1660 * system. For now we can't as we need to override the
1661 * credentials to set CRED_DONT_USE_KERBEROS, which would
1662 * break other users of the system_session */
1663 DEBUG(0,("FIXME: Using new system session for hdb\n"));
1664 nt_status = auth_system_session_info(kdc_db_ctx, base_ctx->lp_ctx, &session_info);
1665 if (!NT_STATUS_IS_OK(nt_status)) {
1669 session_info = system_session(kdc_db_ctx->lp_ctx);
1670 if (session_info == NULL) {
1671 return NT_STATUS_INTERNAL_ERROR;
1675 cli_credentials_set_kerberos_state(session_info->credentials,
1676 CRED_DONT_USE_KERBEROS);
1678 /* Setup the link to LDB */
1679 kdc_db_ctx->samdb = samdb_connect(kdc_db_ctx, base_ctx->ev_ctx,
1680 base_ctx->lp_ctx, session_info);
1681 if (kdc_db_ctx->samdb == NULL) {
1682 DEBUG(1, ("hdb_samba4_create: Cannot open samdb for KDC backend!"));
1683 talloc_free(kdc_db_ctx);
1684 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1687 /* Find out our own krbtgt kvno */
1688 ldb_ret = samdb_rodc(kdc_db_ctx->samdb, &kdc_db_ctx->rodc);
1689 if (ldb_ret != LDB_SUCCESS) {
1690 DEBUG(1, ("hdb_samba4_create: Cannot determine if we are an RODC in KDC backend: %s\n",
1691 ldb_errstring(kdc_db_ctx->samdb)));
1692 talloc_free(kdc_db_ctx);
1693 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1695 if (kdc_db_ctx->rodc) {
1696 int my_krbtgt_number;
1697 const char *secondary_keytab[] = { "msDS-SecondaryKrbTgtNumber", NULL };
1698 struct ldb_dn *account_dn;
1699 struct ldb_dn *server_dn = samdb_server_dn(kdc_db_ctx->samdb, kdc_db_ctx);
1701 DEBUG(1, ("hdb_samba4_create: Cannot determine server DN in KDC backend: %s\n",
1702 ldb_errstring(kdc_db_ctx->samdb)));
1703 talloc_free(kdc_db_ctx);
1704 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1707 ldb_ret = samdb_reference_dn(kdc_db_ctx->samdb, kdc_db_ctx, server_dn,
1708 "serverReference", &account_dn);
1709 if (ldb_ret != LDB_SUCCESS) {
1710 DEBUG(1, ("hdb_samba4_create: Cannot determine server account in KDC backend: %s\n",
1711 ldb_errstring(kdc_db_ctx->samdb)));
1712 talloc_free(kdc_db_ctx);
1713 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1716 ldb_ret = samdb_reference_dn(kdc_db_ctx->samdb, kdc_db_ctx, account_dn,
1717 "msDS-KrbTgtLink", &kdc_db_ctx->krbtgt_dn);
1718 talloc_free(account_dn);
1719 if (ldb_ret != LDB_SUCCESS) {
1720 DEBUG(1, ("hdb_samba4_create: Cannot determine RODC krbtgt account in KDC backend: %s\n",
1721 ldb_errstring(kdc_db_ctx->samdb)));
1722 talloc_free(kdc_db_ctx);
1723 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1726 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb, kdc_db_ctx,
1727 &msg, kdc_db_ctx->krbtgt_dn, LDB_SCOPE_BASE,
1730 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=*))");
1731 if (ldb_ret != LDB_SUCCESS) {
1732 DEBUG(1, ("hdb_samba4_create: Cannot read krbtgt account %s in KDC backend to get msDS-SecondaryKrbTgtNumber: %s: %s\n",
1733 ldb_dn_get_linearized(kdc_db_ctx->krbtgt_dn),
1734 ldb_errstring(kdc_db_ctx->samdb),
1735 ldb_strerror(ldb_ret)));
1736 talloc_free(kdc_db_ctx);
1737 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1739 my_krbtgt_number = ldb_msg_find_attr_as_int(msg, "msDS-SecondaryKrbTgtNumber", -1);
1740 if (my_krbtgt_number == -1) {
1741 DEBUG(1, ("hdb_samba4_create: Cannot read msDS-SecondaryKrbTgtNumber from krbtgt account %s in KDC backend: got %d\n",
1742 ldb_dn_get_linearized(kdc_db_ctx->krbtgt_dn),
1744 talloc_free(kdc_db_ctx);
1745 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1747 kdc_db_ctx->my_krbtgt_number = my_krbtgt_number;
1750 kdc_db_ctx->my_krbtgt_number = 0;
1751 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb, kdc_db_ctx,
1752 &msg, NULL, LDB_SCOPE_SUBTREE,
1754 DSDB_SEARCH_SHOW_EXTENDED_DN,
1755 "(&(objectClass=user)(samAccountName=krbtgt))");
1757 if (ldb_ret != LDB_SUCCESS) {
1758 DEBUG(1, ("samba_kdc_fetch: could not find own KRBTGT in DB: %s\n", ldb_errstring(kdc_db_ctx->samdb)));
1759 talloc_free(kdc_db_ctx);
1760 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1762 kdc_db_ctx->krbtgt_dn = talloc_steal(kdc_db_ctx, msg->dn);
1763 kdc_db_ctx->my_krbtgt_number = 0;
1766 *kdc_db_ctx_out = kdc_db_ctx;
1767 return NT_STATUS_OK;