2 Unix SMB/CIFS implementation.
4 Database Glue between Samba and the KDC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2009
7 Copyright (C) Simo Sorce <idra@samba.org> 2010
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "libcli/security/security.h"
26 #include "auth/auth.h"
27 #include "auth/auth_sam.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "dsdb/common/util.h"
30 #include "librpc/gen_ndr/ndr_drsblobs.h"
31 #include "param/param.h"
32 #include "../lib/crypto/md4.h"
33 #include "system/kerberos.h"
34 #include "auth/kerberos/kerberos.h"
36 #include "kdc/samba_kdc.h"
37 #include "kdc/kdc-glue.h"
38 #include "kdc/kdc-policy.h"
39 #include "kdc/db-glue.h"
41 #define SAMBA_KVNO_GET_KRBTGT(kvno) \
42 ((uint16_t)(((uint32_t)kvno) >> 16))
44 #define SAMBA_KVNO_AND_KRBTGT(kvno, krbtgt) \
45 ((krb5_kvno)((((uint32_t)kvno) & 0xFFFF) | \
46 ((((uint32_t)krbtgt) << 16) & 0xFFFF0000)))
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",
71 /* Translate between the IETF encryption type values and the Microsoft
72 * msDS-SupportedEncryptionTypes values */
73 static uint32_t kerberos_enctype_to_bitmap(krb5_enctype enc_type_enum)
75 switch (enc_type_enum) {
76 case ENCTYPE_DES_CBC_CRC:
78 case ENCTYPE_DES_CBC_MD5:
80 case ENCTYPE_ARCFOUR_HMAC_MD5:
81 return ENC_RC4_HMAC_MD5;
82 case ENCTYPE_AES128_CTS_HMAC_SHA1_96:
83 return ENC_HMAC_SHA1_96_AES128;
84 case ENCTYPE_AES256_CTS_HMAC_SHA1_96:
85 return ENC_HMAC_SHA1_96_AES256;
91 static KerberosTime ldb_msg_find_krb5time_ldap_time(struct ldb_message *msg, const char *attr, KerberosTime default_val)
97 gentime = ldb_msg_find_attr_as_string(msg, attr, NULL);
101 tmp = strptime(gentime, "%Y%m%d%H%M%SZ", &tm);
109 static HDBFlags uf2HDBFlags(krb5_context context, uint32_t userAccountControl, enum samba_kdc_ent_type ent_type)
111 HDBFlags flags = int2HDBFlags(0);
113 /* we don't allow kadmin deletes */
116 /* mark the principal as invalid to start with */
121 /* All accounts are servers, but this may be disabled again in the caller */
124 /* Account types - clear the invalid bit if it turns out to be valid */
125 if (userAccountControl & UF_NORMAL_ACCOUNT) {
126 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
132 if (userAccountControl & UF_INTERDOMAIN_TRUST_ACCOUNT) {
133 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
138 if (userAccountControl & UF_WORKSTATION_TRUST_ACCOUNT) {
139 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
144 if (userAccountControl & UF_SERVER_TRUST_ACCOUNT) {
145 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
151 /* Not permitted to act as a client if disabled */
152 if (userAccountControl & UF_ACCOUNTDISABLE) {
155 if (userAccountControl & UF_LOCKOUT) {
159 if (userAccountControl & UF_PASSWORD_NOTREQD) {
164 UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
166 if (userAccountControl & UF_TEMP_DUPLICATE_ACCOUNT) {
170 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in samba_kdc_message2entry() */
173 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
177 if (userAccountControl & UF_SMARTCARD_REQUIRED) {
178 flags.require_hwauth = 1;
180 if (userAccountControl & UF_TRUSTED_FOR_DELEGATION) {
181 flags.ok_as_delegate = 1;
183 if (userAccountControl & UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION) {
185 * this is confusing...
187 * UF_TRUSTED_FOR_DELEGATION
192 * UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION
193 * => trusted_for_delegation
195 flags.trusted_for_delegation = 1;
197 if (!(userAccountControl & UF_NOT_DELEGATED)) {
198 flags.forwardable = 1;
202 if (userAccountControl & UF_DONT_REQUIRE_PREAUTH) {
203 flags.require_preauth = 0;
205 flags.require_preauth = 1;
211 static int samba_kdc_entry_destructor(struct samba_kdc_entry *p)
213 hdb_entry_ex *entry_ex = p->entry_ex;
214 free_hdb_entry(&entry_ex->entry);
218 static void samba_kdc_free_entry(krb5_context context, hdb_entry_ex *entry_ex)
220 /* this function is called only from hdb_free_entry().
221 * Make sure we neutralize the destructor or we will
222 * get a double free later when hdb_free_entry() will
223 * try to call free_hdb_entry() */
224 talloc_set_destructor(entry_ex->ctx, NULL);
226 /* now proceed to free the talloc part */
227 talloc_free(entry_ex->ctx);
230 static krb5_error_code samba_kdc_message2entry_keys(krb5_context context,
231 struct samba_kdc_db_context *kdc_db_ctx,
233 struct ldb_message *msg,
236 uint32_t userAccountControl,
237 enum samba_kdc_ent_type ent_type,
238 hdb_entry_ex *entry_ex)
240 krb5_error_code ret = 0;
241 enum ndr_err_code ndr_err;
242 struct samr_Password *hash;
243 const struct ldb_val *sc_val;
244 struct supplementalCredentialsBlob scb;
245 struct supplementalCredentialsPackage *scpk = NULL;
246 bool newer_keys = false;
247 struct package_PrimaryKerberosBlob _pkb;
248 struct package_PrimaryKerberosCtr3 *pkb3 = NULL;
249 struct package_PrimaryKerberosCtr4 *pkb4 = NULL;
251 uint16_t allocated_keys = 0;
252 int rodc_krbtgt_number = 0;
254 uint32_t supported_enctypes
255 = ldb_msg_find_attr_as_uint(msg,
256 "msDS-SupportedEncryptionTypes",
259 if (rid == DOMAIN_RID_KRBTGT || is_rodc) {
260 /* KDCs (and KDCs on RODCs) use AES */
261 supported_enctypes |= ENC_HMAC_SHA1_96_AES128 | ENC_HMAC_SHA1_96_AES256;
262 } else if (userAccountControl & (UF_PARTIAL_SECRETS_ACCOUNT|UF_SERVER_TRUST_ACCOUNT)) {
263 /* DCs and RODCs comptuer accounts use AES */
264 supported_enctypes |= ENC_HMAC_SHA1_96_AES128 | ENC_HMAC_SHA1_96_AES256;
265 } else if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT ||
266 (ent_type == SAMBA_KDC_ENT_TYPE_ANY)) {
267 /* for AS-REQ the client chooses the enc types it
268 * supports, and this will vary between computers a
271 * likewise for 'any' return as much as is supported,
272 * to export into a keytab */
273 supported_enctypes = ENC_ALL_TYPES;
276 /* If UF_USE_DES_KEY_ONLY has been set, then don't allow use of the newer enc types */
277 if (userAccountControl & UF_USE_DES_KEY_ONLY) {
278 supported_enctypes = ENC_CRC32|ENC_RSA_MD5;
280 /* Otherwise, add in the default enc types */
281 supported_enctypes |= ENC_CRC32 | ENC_RSA_MD5 | ENC_RC4_HMAC_MD5;
284 /* Is this the krbtgt or a RODC krbtgt */
286 rodc_krbtgt_number = ldb_msg_find_attr_as_int(msg, "msDS-SecondaryKrbTgtNumber", -1);
288 if (rodc_krbtgt_number == -1) {
293 entry_ex->entry.keys.val = NULL;
294 entry_ex->entry.keys.len = 0;
296 kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
298 kvno = SAMBA_KVNO_AND_KRBTGT(kvno, rodc_krbtgt_number);
300 entry_ex->entry.kvno = kvno;
302 /* Get keys from the db */
304 hash = samdb_result_hash(mem_ctx, msg, "unicodePwd");
305 sc_val = ldb_msg_find_ldb_val(msg, "supplementalCredentials");
307 /* unicodePwd for enctype 0x17 (23) if present */
312 /* supplementalCredentials if present */
314 ndr_err = ndr_pull_struct_blob_all(sc_val, mem_ctx, &scb,
315 (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob);
316 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
317 dump_data(0, sc_val->data, sc_val->length);
322 if (scb.sub.signature != SUPPLEMENTAL_CREDENTIALS_SIGNATURE) {
323 NDR_PRINT_DEBUG(supplementalCredentialsBlob, &scb);
328 for (i=0; i < scb.sub.num_packages; i++) {
329 if (strcmp("Primary:Kerberos-Newer-Keys", scb.sub.packages[i].name) == 0) {
330 scpk = &scb.sub.packages[i];
331 if (!scpk->data || !scpk->data[0]) {
337 } else if (strcmp("Primary:Kerberos", scb.sub.packages[i].name) == 0) {
338 scpk = &scb.sub.packages[i];
339 if (!scpk->data || !scpk->data[0]) {
343 * we don't break here in hope to find
344 * a Kerberos-Newer-Keys package
350 * Primary:Kerberos-Newer-Keys or Primary:Kerberos element
351 * of supplementalCredentials
356 blob = strhex_to_data_blob(mem_ctx, scpk->data);
362 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
363 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &_pkb,
364 (ndr_pull_flags_fn_t)ndr_pull_package_PrimaryKerberosBlob);
365 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
367 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
368 krb5_warnx(context, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
372 if (newer_keys && _pkb.version != 4) {
374 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
375 krb5_warnx(context, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
379 if (!newer_keys && _pkb.version != 3) {
381 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
382 krb5_warnx(context, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
386 if (_pkb.version == 4) {
387 pkb4 = &_pkb.ctr.ctr4;
388 allocated_keys += pkb4->num_keys;
389 } else if (_pkb.version == 3) {
390 pkb3 = &_pkb.ctr.ctr3;
391 allocated_keys += pkb3->num_keys;
395 if (allocated_keys == 0) {
396 if (kdc_db_ctx->rodc) {
397 /* We are on an RODC, but don't have keys for this account. Signal this to the caller */
398 return HDB_ERR_NOT_FOUND_HERE;
401 /* oh, no password. Apparently (comment in
402 * hdb-ldap.c) this violates the ASN.1, but this
403 * allows an entry with no keys (yet). */
407 /* allocate space to decode into */
408 entry_ex->entry.keys.len = 0;
409 entry_ex->entry.keys.val = calloc(allocated_keys, sizeof(Key));
410 if (entry_ex->entry.keys.val == NULL) {
415 if (hash && (supported_enctypes & ENC_RC4_HMAC_MD5)) {
419 key.salt = NULL; /* No salt for this enc type */
421 ret = krb5_keyblock_init(context,
422 ENCTYPE_ARCFOUR_HMAC,
423 hash->hash, sizeof(hash->hash),
429 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
430 entry_ex->entry.keys.len++;
434 for (i=0; i < pkb4->num_keys; i++) {
437 if (!pkb4->keys[i].value) continue;
439 if (!(kerberos_enctype_to_bitmap(pkb4->keys[i].keytype) & supported_enctypes)) {
446 if (pkb4->salt.string) {
449 salt = data_blob_string_const(pkb4->salt.string);
451 key.salt = calloc(1, sizeof(*key.salt));
452 if (key.salt == NULL) {
457 key.salt->type = hdb_pw_salt;
459 ret = krb5_data_copy(&key.salt->salt, salt.data, salt.length);
467 /* TODO: maybe pass the iteration_count somehow... */
469 ret = krb5_keyblock_init(context,
470 pkb4->keys[i].keytype,
471 pkb4->keys[i].value->data,
472 pkb4->keys[i].value->length,
474 if (ret == KRB5_PROG_ETYPE_NOSUPP) {
475 DEBUG(2,("Unsupported keytype ignored - type %u\n",
476 pkb4->keys[i].keytype));
489 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
490 entry_ex->entry.keys.len++;
493 for (i=0; i < pkb3->num_keys; i++) {
496 if (!pkb3->keys[i].value) continue;
498 if (!(kerberos_enctype_to_bitmap(pkb3->keys[i].keytype) & supported_enctypes)) {
505 if (pkb3->salt.string) {
508 salt = data_blob_string_const(pkb3->salt.string);
510 key.salt = calloc(1, sizeof(*key.salt));
511 if (key.salt == NULL) {
516 key.salt->type = hdb_pw_salt;
518 ret = krb5_data_copy(&key.salt->salt, salt.data, salt.length);
526 ret = krb5_keyblock_init(context,
527 pkb3->keys[i].keytype,
528 pkb3->keys[i].value->data,
529 pkb3->keys[i].value->length,
540 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
541 entry_ex->entry.keys.len++;
547 entry_ex->entry.keys.len = 0;
549 if (entry_ex->entry.keys.len == 0 && entry_ex->entry.keys.val) {
550 free(entry_ex->entry.keys.val);
551 entry_ex->entry.keys.val = NULL;
557 * Construct an hdb_entry from a directory entry.
559 static krb5_error_code samba_kdc_message2entry(krb5_context context,
560 struct samba_kdc_db_context *kdc_db_ctx,
561 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
562 enum samba_kdc_ent_type ent_type,
564 struct ldb_dn *realm_dn,
565 struct ldb_message *msg,
566 hdb_entry_ex *entry_ex)
568 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
569 uint32_t userAccountControl;
571 krb5_error_code ret = 0;
572 krb5_boolean is_computer = FALSE;
574 struct samba_kdc_entry *p;
579 bool is_rodc = false;
580 struct ldb_message_element *objectclasses;
581 struct ldb_val computer_val;
582 const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
583 computer_val.data = discard_const_p(uint8_t,"computer");
584 computer_val.length = strlen((const char *)computer_val.data);
586 if (ldb_msg_find_element(msg, "msDS-SecondaryKrbTgtNumber")) {
590 if (!samAccountName) {
592 krb5_set_error_message(context, ret, "samba_kdc_message2entry: no samAccountName present");
596 objectclasses = ldb_msg_find_element(msg, "objectClass");
598 if (objectclasses && ldb_msg_find_val(objectclasses, &computer_val)) {
602 memset(entry_ex, 0, sizeof(*entry_ex));
604 p = talloc(mem_ctx, struct samba_kdc_entry);
610 p->kdc_db_ctx = kdc_db_ctx;
611 p->entry_ex = entry_ex;
612 p->realm_dn = talloc_reference(p, realm_dn);
618 talloc_set_destructor(p, samba_kdc_entry_destructor);
620 /* make sure we do not have bogus data in there */
621 memset(&entry_ex->entry, 0, sizeof(hdb_entry));
624 entry_ex->free_entry = samba_kdc_free_entry;
626 userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
629 entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
630 if (ent_type == SAMBA_KDC_ENT_TYPE_ANY && principal == NULL) {
631 krb5_make_principal(context, &entry_ex->entry.principal, lpcfg_realm(lp_ctx), samAccountName, NULL);
633 ret = copy_Principal(principal, entry_ex->entry.principal);
635 krb5_clear_error_message(context);
639 /* While we have copied the client principal, tests
640 * show that Win2k3 returns the 'corrected' realm, not
641 * the client-specified realm. This code attempts to
642 * replace the client principal's realm with the one
643 * we determine from our records */
645 /* this has to be with malloc() */
646 krb5_principal_set_realm(context, entry_ex->entry.principal, lpcfg_realm(lp_ctx));
649 /* First try and figure out the flags based on the userAccountControl */
650 entry_ex->entry.flags = uf2HDBFlags(context, userAccountControl, ent_type);
652 /* Windows 2008 seems to enforce this (very sensible) rule by
653 * default - don't allow offline attacks on a user's password
654 * by asking for a ticket to them as a service (encrypted with
655 * their probably patheticly insecure password) */
657 if (entry_ex->entry.flags.server
658 && lpcfg_parm_bool(lp_ctx, NULL, "kdc", "require spn for service", true)) {
659 if (!is_computer && !ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL)) {
660 entry_ex->entry.flags.server = 0;
664 if (flags & HDB_F_ADMIN_DATA) {
665 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
666 * of the Heimdal KDC. They are stored in a the traditional
667 * DB for audit purposes, and still form part of the structure
670 /* use 'whenCreated' */
671 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
672 /* use 'kadmin' for now (needed by mit_samba) */
673 krb5_make_principal(context,
674 &entry_ex->entry.created_by.principal,
675 lpcfg_realm(lp_ctx), "kadmin", NULL);
677 entry_ex->entry.modified_by = (Event *) malloc(sizeof(Event));
678 if (entry_ex->entry.modified_by == NULL) {
680 krb5_set_error_message(context, ret, "malloc: out of memory");
684 /* use 'whenChanged' */
685 entry_ex->entry.modified_by->time = ldb_msg_find_krb5time_ldap_time(msg, "whenChanged", 0);
686 /* use 'kadmin' for now (needed by mit_samba) */
687 krb5_make_principal(context,
688 &entry_ex->entry.modified_by->principal,
689 lpcfg_realm(lp_ctx), "kadmin", NULL);
693 /* The lack of password controls etc applies to krbtgt by
694 * virtue of being that particular RID */
695 status = dom_sid_split_rid(NULL, samdb_result_dom_sid(mem_ctx, msg, "objectSid"), NULL, &rid);
697 if (!NT_STATUS_IS_OK(status)) {
702 if (rid == DOMAIN_RID_KRBTGT) {
703 entry_ex->entry.valid_end = NULL;
704 entry_ex->entry.pw_end = NULL;
706 entry_ex->entry.flags.invalid = 0;
707 entry_ex->entry.flags.server = 1;
709 /* Don't mark all requests for the krbtgt/realm as
710 * 'change password', as otherwise we could get into
711 * trouble, and not enforce the password expirty.
712 * Instead, only do it when request is for the kpasswd service */
713 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER
714 && principal->name.name_string.len == 2
715 && (strcmp(principal->name.name_string.val[0], "kadmin") == 0)
716 && (strcmp(principal->name.name_string.val[1], "changepw") == 0)
717 && lpcfg_is_my_domain_or_realm(lp_ctx, principal->realm)) {
718 entry_ex->entry.flags.change_pw = 1;
720 entry_ex->entry.flags.client = 0;
721 entry_ex->entry.flags.forwardable = 1;
722 entry_ex->entry.flags.ok_as_delegate = 1;
723 } else if (is_rodc) {
724 /* The RODC krbtgt account is like the main krbtgt,
725 * but it does not have a changepw or kadmin
728 entry_ex->entry.valid_end = NULL;
729 entry_ex->entry.pw_end = NULL;
731 /* Also don't allow the RODC krbtgt to be a client (it should not be needed) */
732 entry_ex->entry.flags.client = 0;
733 entry_ex->entry.flags.invalid = 0;
734 entry_ex->entry.flags.server = 1;
736 entry_ex->entry.flags.client = 0;
737 entry_ex->entry.flags.forwardable = 1;
738 entry_ex->entry.flags.ok_as_delegate = 0;
739 } else if (entry_ex->entry.flags.server && ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
740 /* The account/password expiry only applies when the account is used as a
741 * client (ie password login), not when used as a server */
743 /* Make very well sure we don't use this for a client,
744 * it could bypass the password restrictions */
745 entry_ex->entry.flags.client = 0;
747 entry_ex->entry.valid_end = NULL;
748 entry_ex->entry.pw_end = NULL;
751 NTTIME must_change_time
752 = samdb_result_force_password_change(kdc_db_ctx->samdb, mem_ctx,
754 if (must_change_time == 0x7FFFFFFFFFFFFFFFULL) {
755 entry_ex->entry.pw_end = NULL;
757 entry_ex->entry.pw_end = malloc(sizeof(*entry_ex->entry.pw_end));
758 if (entry_ex->entry.pw_end == NULL) {
762 *entry_ex->entry.pw_end = nt_time_to_unix(must_change_time);
765 acct_expiry = samdb_result_account_expires(msg);
766 if (acct_expiry == 0x7FFFFFFFFFFFFFFFULL) {
767 entry_ex->entry.valid_end = NULL;
769 entry_ex->entry.valid_end = malloc(sizeof(*entry_ex->entry.valid_end));
770 if (entry_ex->entry.valid_end == NULL) {
774 *entry_ex->entry.valid_end = nt_time_to_unix(acct_expiry);
778 entry_ex->entry.valid_start = NULL;
780 entry_ex->entry.max_life = malloc(sizeof(*entry_ex->entry.max_life));
781 if (entry_ex->entry.max_life == NULL) {
786 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
787 *entry_ex->entry.max_life = nt_time_to_unix(kdc_db_ctx->policy.service_tkt_lifetime);
788 } else if (ent_type == SAMBA_KDC_ENT_TYPE_KRBTGT || ent_type == SAMBA_KDC_ENT_TYPE_CLIENT) {
789 *entry_ex->entry.max_life = nt_time_to_unix(kdc_db_ctx->policy.user_tkt_lifetime);
791 *entry_ex->entry.max_life = MIN(nt_time_to_unix(kdc_db_ctx->policy.service_tkt_lifetime),
792 nt_time_to_unix(kdc_db_ctx->policy.user_tkt_lifetime));
795 entry_ex->entry.max_renew = malloc(sizeof(*entry_ex->entry.max_life));
796 if (entry_ex->entry.max_renew == NULL) {
801 *entry_ex->entry.max_renew = nt_time_to_unix(kdc_db_ctx->policy.user_tkt_renewaltime);
803 entry_ex->entry.generation = NULL;
805 /* Get keys from the db */
806 ret = samba_kdc_message2entry_keys(context, kdc_db_ctx, p, msg,
807 rid, is_rodc, userAccountControl,
810 /* Could be bougus data in the entry, or out of memory */
814 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
815 if (entry_ex->entry.etypes == NULL) {
816 krb5_clear_error_message(context);
820 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
821 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
822 if (entry_ex->entry.etypes->val == NULL) {
823 krb5_clear_error_message(context);
827 for (i=0; i < entry_ex->entry.etypes->len; i++) {
828 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
832 p->msg = talloc_steal(p, msg);
836 /* This doesn't free ent itself, that is for the eventual caller to do */
837 hdb_free_entry(context, entry_ex);
839 talloc_steal(kdc_db_ctx, entry_ex->ctx);
846 * Construct an hdb_entry from a directory entry.
847 * The kvno is what the remote client asked for
849 static krb5_error_code samba_kdc_trust_message2entry(krb5_context context,
850 struct samba_kdc_db_context *kdc_db_ctx,
851 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
852 enum trust_direction direction,
853 struct ldb_dn *realm_dn,
856 struct ldb_message *msg,
857 hdb_entry_ex *entry_ex)
859 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
860 const char *dnsdomain;
861 const char *realm = lpcfg_realm(lp_ctx);
862 DATA_BLOB password_utf16;
863 struct samr_Password password_hash;
864 const struct ldb_val *password_val;
865 struct trustAuthInOutBlob password_blob;
866 struct samba_kdc_entry *p;
868 uint32_t current_kvno;
869 enum ndr_err_code ndr_err;
870 int ret, trust_direction_flags;
872 struct AuthenticationInformationArray *auth_array;
874 p = talloc(mem_ctx, struct samba_kdc_entry);
880 p->kdc_db_ctx = kdc_db_ctx;
881 p->entry_ex = entry_ex;
882 p->realm_dn = realm_dn;
884 talloc_set_destructor(p, samba_kdc_entry_destructor);
886 /* make sure we do not have bogus data in there */
887 memset(&entry_ex->entry, 0, sizeof(hdb_entry));
890 entry_ex->free_entry = samba_kdc_free_entry;
892 /* use 'whenCreated' */
893 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
894 /* use 'kadmin' for now (needed by mit_samba) */
895 krb5_make_principal(context,
896 &entry_ex->entry.created_by.principal,
897 realm, "kadmin", NULL);
899 entry_ex->entry.valid_start = NULL;
901 trust_direction_flags = ldb_msg_find_attr_as_int(msg, "trustDirection", 0);
903 if (direction == INBOUND) {
904 password_val = ldb_msg_find_ldb_val(msg, "trustAuthIncoming");
906 } else { /* OUTBOUND */
907 dnsdomain = ldb_msg_find_attr_as_string(msg, "trustPartner", NULL);
909 realm = strupper_talloc(mem_ctx, dnsdomain);
910 password_val = ldb_msg_find_ldb_val(msg, "trustAuthOutgoing");
913 if (!password_val || !(trust_direction_flags & direction)) {
918 ndr_err = ndr_pull_struct_blob(password_val, mem_ctx, &password_blob,
919 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
920 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
926 /* we need to work out if we are going to use the current or
927 * the previous password hash.
928 * We base this on the kvno the client passes in. If the kvno
929 * passed in is equal to the current kvno in our database then
930 * we use the current structure. If it is the current kvno-1,
931 * then we use the previous substrucure.
934 /* first work out the current kvno */
936 for (i=0; i < password_blob.count; i++) {
937 if (password_blob.current.array[i].AuthType == TRUST_AUTH_TYPE_VERSION) {
938 current_kvno = password_blob.current.array[i].AuthInfo.version.version;
942 /* work out whether we will use the previous or current
944 if (password_blob.previous.count == 0) {
945 /* there is no previous password */
946 use_previous = false;
947 } else if (!(flags & HDB_F_KVNO_SPECIFIED) ||
948 kvno == current_kvno) {
949 use_previous = false;
950 } else if ((kvno+1 == current_kvno) ||
951 (kvno == 255 && current_kvno == 0)) {
954 DEBUG(1,(__location__ ": Request for unknown kvno %u - current kvno is %u\n",
955 kvno, current_kvno));
961 auth_array = &password_blob.previous;
963 auth_array = &password_blob.current;
966 /* use the kvno the client specified, if available */
967 if (flags & HDB_F_KVNO_SPECIFIED) {
968 entry_ex->entry.kvno = kvno;
970 entry_ex->entry.kvno = current_kvno;
973 for (i=0; i < auth_array->count; i++) {
974 if (auth_array->array[i].AuthType == TRUST_AUTH_TYPE_CLEAR) {
975 password_utf16 = data_blob_const(auth_array->array[i].AuthInfo.clear.password,
976 auth_array->array[i].AuthInfo.clear.size);
977 /* In the future, generate all sorts of
978 * hashes, but for now we can't safely convert
979 * the random strings windows uses into
982 /* but as it is utf16 already, we can get the NT password/arcfour-hmac-md5 key */
983 mdfour(password_hash.hash, password_utf16.data, password_utf16.length);
985 } else if (auth_array->array[i].AuthType == TRUST_AUTH_TYPE_NT4OWF) {
986 password_hash = auth_array->array[i].AuthInfo.nt4owf.password;
991 if (i < auth_array->count) {
993 /* Must have found a cleartext or MD4 password */
994 entry_ex->entry.keys.val = calloc(1, sizeof(Key));
997 key.salt = NULL; /* No salt for this enc type */
999 if (entry_ex->entry.keys.val == NULL) {
1004 ret = krb5_keyblock_init(context,
1005 ENCTYPE_ARCFOUR_HMAC,
1006 password_hash.hash, sizeof(password_hash.hash),
1012 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
1013 entry_ex->entry.keys.len++;
1016 entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
1018 ret = copy_Principal(principal, entry_ex->entry.principal);
1020 krb5_clear_error_message(context);
1024 /* While we have copied the client principal, tests
1025 * show that Win2k3 returns the 'corrected' realm, not
1026 * the client-specified realm. This code attempts to
1027 * replace the client principal's realm with the one
1028 * we determine from our records */
1030 krb5_principal_set_realm(context, entry_ex->entry.principal, realm);
1031 entry_ex->entry.flags = int2HDBFlags(0);
1032 entry_ex->entry.flags.immutable = 1;
1033 entry_ex->entry.flags.invalid = 0;
1034 entry_ex->entry.flags.server = 1;
1035 entry_ex->entry.flags.require_preauth = 1;
1037 entry_ex->entry.pw_end = NULL;
1039 entry_ex->entry.max_life = NULL;
1041 entry_ex->entry.max_renew = NULL;
1043 entry_ex->entry.generation = NULL;
1045 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
1046 if (entry_ex->entry.etypes == NULL) {
1047 krb5_clear_error_message(context);
1051 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
1052 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
1053 if (entry_ex->entry.etypes->val == NULL) {
1054 krb5_clear_error_message(context);
1058 for (i=0; i < entry_ex->entry.etypes->len; i++) {
1059 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
1063 p->msg = talloc_steal(p, msg);
1067 /* This doesn't free ent itself, that is for the eventual caller to do */
1068 hdb_free_entry(context, entry_ex);
1070 talloc_steal(kdc_db_ctx, entry_ex->ctx);
1077 static krb5_error_code samba_kdc_lookup_trust(krb5_context context, struct ldb_context *ldb_ctx,
1078 TALLOC_CTX *mem_ctx,
1080 struct ldb_dn *realm_dn,
1081 struct ldb_message **pmsg)
1084 krb5_error_code ret;
1085 char *filter = NULL;
1086 const char * const *attrs = trust_attrs;
1088 struct ldb_result *res = NULL;
1089 char *realm_encoded = ldb_binary_encode_string(mem_ctx, realm);
1090 if (!realm_encoded) {
1093 krb5_set_error_message(context, ret, "talloc_asprintf: out of memory");
1097 filter = talloc_asprintf(mem_ctx, "(&(objectClass=trustedDomain)(|(flatname=%s)(trustPartner=%s)))",
1098 realm_encoded, realm_encoded);
1101 talloc_free(realm_encoded);
1103 krb5_set_error_message(context, ret, "talloc_asprintf: out of memory");
1107 lret = dsdb_search(ldb_ctx, mem_ctx, &res,
1108 ldb_get_default_basedn(ldb_ctx),
1109 LDB_SCOPE_SUBTREE, attrs,
1110 DSDB_SEARCH_NO_GLOBAL_CATALOG,
1112 if (lret != LDB_SUCCESS) {
1113 DEBUG(3, ("Failed to search for %s: %s\n", filter, ldb_errstring(ldb_ctx)));
1114 return HDB_ERR_NOENTRY;
1115 } else if (res->count == 0 || res->count > 1) {
1116 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter, res->count));
1118 return HDB_ERR_NOENTRY;
1120 talloc_steal(mem_ctx, res->msgs);
1121 *pmsg = res->msgs[0];
1126 static krb5_error_code samba_kdc_lookup_client(krb5_context context,
1127 struct samba_kdc_db_context *kdc_db_ctx,
1128 TALLOC_CTX *mem_ctx,
1129 krb5_const_principal principal,
1131 struct ldb_dn **realm_dn,
1132 struct ldb_message **msg) {
1134 char *principal_string;
1135 krb5_error_code ret;
1137 ret = krb5_unparse_name(context, principal, &principal_string);
1143 nt_status = sam_get_results_principal(kdc_db_ctx->samdb,
1144 mem_ctx, principal_string, attrs,
1146 free(principal_string);
1147 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
1148 return HDB_ERR_NOENTRY;
1149 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
1151 } else if (!NT_STATUS_IS_OK(nt_status)) {
1158 static krb5_error_code samba_kdc_fetch_client(krb5_context context,
1159 struct samba_kdc_db_context *kdc_db_ctx,
1160 TALLOC_CTX *mem_ctx,
1161 krb5_const_principal principal,
1163 hdb_entry_ex *entry_ex) {
1164 struct ldb_dn *realm_dn;
1165 krb5_error_code ret;
1166 struct ldb_message *msg = NULL;
1168 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
1169 mem_ctx, principal, user_attrs,
1175 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1176 principal, SAMBA_KDC_ENT_TYPE_CLIENT,
1178 realm_dn, msg, entry_ex);
1182 static krb5_error_code samba_kdc_fetch_krbtgt(krb5_context context,
1183 struct samba_kdc_db_context *kdc_db_ctx,
1184 TALLOC_CTX *mem_ctx,
1185 krb5_const_principal principal,
1188 hdb_entry_ex *entry_ex)
1190 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
1191 krb5_error_code ret;
1192 struct ldb_message *msg = NULL;
1193 struct ldb_dn *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
1195 krb5_principal alloc_principal = NULL;
1196 if (principal->name.name_string.len != 2
1197 || (strcmp(principal->name.name_string.val[0], KRB5_TGS_NAME) != 0)) {
1199 return HDB_ERR_NOENTRY;
1202 /* krbtgt case. Either us or a trusted realm */
1204 if (lpcfg_is_my_domain_or_realm(lp_ctx, principal->realm)
1205 && lpcfg_is_my_domain_or_realm(lp_ctx, principal->name.name_string.val[1])) {
1206 /* us, or someone quite like us */
1207 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1208 * is in our db, then direct the caller at our primary
1212 unsigned int krbtgt_number;
1213 /* w2k8r2 sometimes gives us a kvno of 255 for inter-domain
1214 trust tickets. We don't yet know what this means, but we do
1215 seem to need to treat it as unspecified */
1216 if (flags & HDB_F_KVNO_SPECIFIED) {
1217 krbtgt_number = SAMBA_KVNO_GET_KRBTGT(kvno);
1218 if (kdc_db_ctx->rodc) {
1219 if (krbtgt_number != kdc_db_ctx->my_krbtgt_number) {
1220 return HDB_ERR_NOT_FOUND_HERE;
1224 krbtgt_number = kdc_db_ctx->my_krbtgt_number;
1227 if (krbtgt_number == kdc_db_ctx->my_krbtgt_number) {
1228 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
1229 &msg, kdc_db_ctx->krbtgt_dn, LDB_SCOPE_BASE,
1230 krbtgt_attrs, DSDB_SEARCH_NO_GLOBAL_CATALOG,
1231 "(objectClass=user)");
1233 /* We need to look up an RODC krbtgt (perhaps
1234 * ours, if we are an RODC, perhaps another
1235 * RODC if we are a read-write DC */
1236 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
1237 &msg, realm_dn, LDB_SCOPE_SUBTREE,
1239 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
1240 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=%u))", (unsigned)(krbtgt_number));
1243 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
1244 krb5_warnx(context, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1245 (unsigned)(krbtgt_number));
1246 krb5_set_error_message(context, HDB_ERR_NOENTRY,
1247 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1248 (unsigned)(krbtgt_number));
1249 return HDB_ERR_NOENTRY;
1250 } else if (lret != LDB_SUCCESS) {
1251 krb5_warnx(context, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1252 (unsigned)(krbtgt_number));
1253 krb5_set_error_message(context, HDB_ERR_NOENTRY,
1254 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1255 (unsigned)(krbtgt_number));
1256 return HDB_ERR_NOENTRY;
1260 * Windows seems to canonicalize the principal
1261 * in a TGS REP even if the client did not specify
1262 * the canonicalize flag.
1264 if (flags & (HDB_F_CANON|HDB_F_FOR_TGS_REQ)) {
1265 ret = krb5_copy_principal(context, principal, &alloc_principal);
1270 /* When requested to do so, ensure that the
1271 * both realm values in the principal are set
1272 * to the upper case, canonical realm */
1273 free(alloc_principal->name.name_string.val[1]);
1274 alloc_principal->name.name_string.val[1] = strdup(lpcfg_realm(lp_ctx));
1275 if (!alloc_principal->name.name_string.val[1]) {
1277 krb5_set_error_message(context, ret, "samba_kdc_fetch: strdup() failed!");
1280 principal = alloc_principal;
1283 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1284 principal, SAMBA_KDC_ENT_TYPE_KRBTGT,
1285 flags, realm_dn, msg, entry_ex);
1286 if (alloc_principal) {
1287 /* This is again copied in the message2entry call */
1288 krb5_free_principal(context, alloc_principal);
1291 krb5_warnx(context, "samba_kdc_fetch: self krbtgt message2entry failed");
1296 enum trust_direction direction = UNKNOWN;
1297 const char *realm = NULL;
1299 /* Either an inbound or outbound trust */
1301 if (strcasecmp(lpcfg_realm(lp_ctx), principal->realm) == 0) {
1302 /* look for inbound trust */
1303 direction = INBOUND;
1304 realm = principal->name.name_string.val[1];
1305 } else if (strcasecmp(lpcfg_realm(lp_ctx), principal->name.name_string.val[1]) == 0) {
1306 /* look for outbound trust */
1307 direction = OUTBOUND;
1308 realm = principal->realm;
1310 krb5_warnx(context, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1311 principal->realm, principal->name.name_string.val[1]);
1312 krb5_set_error_message(context, HDB_ERR_NOENTRY, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1313 principal->realm, principal->name.name_string.val[1]);
1314 return HDB_ERR_NOENTRY;
1317 /* Trusted domains are under CN=system */
1319 ret = samba_kdc_lookup_trust(context, kdc_db_ctx->samdb,
1321 realm, realm_dn, &msg);
1324 krb5_warnx(context, "samba_kdc_fetch: could not find principal in DB");
1325 krb5_set_error_message(context, ret, "samba_kdc_fetch: could not find principal in DB");
1329 ret = samba_kdc_trust_message2entry(context, kdc_db_ctx, mem_ctx,
1330 principal, direction,
1331 realm_dn, flags, kvno, msg, entry_ex);
1333 krb5_warnx(context, "samba_kdc_fetch: trust_message2entry failed");
1340 static krb5_error_code samba_kdc_lookup_server(krb5_context context,
1341 struct samba_kdc_db_context *kdc_db_ctx,
1342 TALLOC_CTX *mem_ctx,
1343 krb5_const_principal principal,
1345 struct ldb_dn **realm_dn,
1346 struct ldb_message **msg)
1348 krb5_error_code ret;
1349 if (principal->name.name_string.len >= 2) {
1350 /* 'normal server' case */
1353 struct ldb_dn *user_dn;
1354 char *principal_string;
1356 ret = krb5_unparse_name_flags(context, principal,
1357 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
1363 /* At this point we may find the host is known to be
1364 * in a different realm, so we should generate a
1365 * referral instead */
1366 nt_status = crack_service_principal_name(kdc_db_ctx->samdb,
1367 mem_ctx, principal_string,
1368 &user_dn, realm_dn);
1369 free(principal_string);
1371 if (!NT_STATUS_IS_OK(nt_status)) {
1372 return HDB_ERR_NOENTRY;
1375 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb,
1377 msg, user_dn, LDB_SCOPE_BASE,
1379 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
1381 if (ldb_ret != LDB_SUCCESS) {
1382 return HDB_ERR_NOENTRY;
1389 /* server as client principal case, but we must not lookup userPrincipalNames */
1390 *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
1391 realm = krb5_principal_get_realm(context, principal);
1393 /* TODO: Check if it is our realm, otherwise give referral */
1395 ret = krb5_unparse_name_flags(context, principal, KRB5_PRINCIPAL_UNPARSE_NO_REALM, &short_princ);
1398 krb5_set_error_message(context, ret, "samba_kdc_lookup_principal: could not parse principal");
1399 krb5_warnx(context, "samba_kdc_lookup_principal: could not parse principal");
1403 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx, msg,
1404 *realm_dn, LDB_SCOPE_SUBTREE,
1406 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
1407 "(&(objectClass=user)(samAccountName=%s))",
1408 ldb_binary_encode_string(mem_ctx, short_princ));
1409 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
1410 DEBUG(3, ("Failed to find an entry for %s\n", short_princ));
1412 return HDB_ERR_NOENTRY;
1414 if (lret != LDB_SUCCESS) {
1415 DEBUG(3, ("Failed single search for %s - %s\n",
1416 short_princ, ldb_errstring(kdc_db_ctx->samdb)));
1418 return HDB_ERR_NOENTRY;
1426 static krb5_error_code samba_kdc_fetch_server(krb5_context context,
1427 struct samba_kdc_db_context *kdc_db_ctx,
1428 TALLOC_CTX *mem_ctx,
1429 krb5_const_principal principal,
1431 hdb_entry_ex *entry_ex)
1433 krb5_error_code ret;
1434 struct ldb_dn *realm_dn;
1435 struct ldb_message *msg;
1437 ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, principal,
1438 server_attrs, &realm_dn, &msg);
1443 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1444 principal, SAMBA_KDC_ENT_TYPE_SERVER,
1446 realm_dn, msg, entry_ex);
1448 krb5_warnx(context, "samba_kdc_fetch: message2entry failed");
1454 krb5_error_code samba_kdc_fetch(krb5_context context,
1455 struct samba_kdc_db_context *kdc_db_ctx,
1456 krb5_const_principal principal,
1459 hdb_entry_ex *entry_ex)
1461 krb5_error_code ret = HDB_ERR_NOENTRY;
1462 TALLOC_CTX *mem_ctx;
1464 mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_fetch context");
1467 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
1471 if (flags & HDB_F_GET_CLIENT) {
1472 ret = samba_kdc_fetch_client(context, kdc_db_ctx, mem_ctx, principal, flags, entry_ex);
1473 if (ret != HDB_ERR_NOENTRY) goto done;
1475 if (flags & HDB_F_GET_SERVER) {
1476 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
1477 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, flags, kvno, entry_ex);
1478 if (ret != HDB_ERR_NOENTRY) goto done;
1480 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
1481 ret = samba_kdc_fetch_server(context, kdc_db_ctx, mem_ctx, principal, flags, entry_ex);
1482 if (ret != HDB_ERR_NOENTRY) goto done;
1484 if (flags & HDB_F_GET_KRBTGT) {
1485 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, flags, kvno, entry_ex);
1486 if (ret != HDB_ERR_NOENTRY) goto done;
1490 talloc_free(mem_ctx);
1494 struct samba_kdc_seq {
1497 struct ldb_message **msgs;
1498 struct ldb_dn *realm_dn;
1501 static krb5_error_code samba_kdc_seq(krb5_context context,
1502 struct samba_kdc_db_context *kdc_db_ctx,
1503 hdb_entry_ex *entry)
1505 krb5_error_code ret;
1506 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
1507 TALLOC_CTX *mem_ctx;
1508 hdb_entry_ex entry_ex;
1509 memset(&entry_ex, '\0', sizeof(entry_ex));
1512 return HDB_ERR_NOENTRY;
1515 mem_ctx = talloc_named(priv, 0, "samba_kdc_seq context");
1519 krb5_set_error_message(context, ret, "samba_kdc_seq: talloc_named() failed!");
1523 if (priv->index < priv->count) {
1524 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1525 NULL, SAMBA_KDC_ENT_TYPE_ANY,
1526 HDB_F_ADMIN_DATA|HDB_F_GET_ANY,
1527 priv->realm_dn, priv->msgs[priv->index++], entry);
1529 ret = HDB_ERR_NOENTRY;
1534 kdc_db_ctx->seq_ctx = NULL;
1536 talloc_free(mem_ctx);
1542 krb5_error_code samba_kdc_firstkey(krb5_context context,
1543 struct samba_kdc_db_context *kdc_db_ctx,
1544 hdb_entry_ex *entry)
1546 struct ldb_context *ldb_ctx = kdc_db_ctx->samdb;
1547 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
1549 struct ldb_result *res = NULL;
1550 krb5_error_code ret;
1551 TALLOC_CTX *mem_ctx;
1556 kdc_db_ctx->seq_ctx = NULL;
1559 priv = (struct samba_kdc_seq *) talloc(kdc_db_ctx, struct samba_kdc_seq);
1562 krb5_set_error_message(context, ret, "talloc: out of memory");
1568 priv->realm_dn = ldb_get_default_basedn(ldb_ctx);
1571 mem_ctx = talloc_named(priv, 0, "samba_kdc_firstkey context");
1575 krb5_set_error_message(context, ret, "samba_kdc_firstkey: talloc_named() failed!");
1579 ret = krb5_get_default_realm(context, &realm);
1585 lret = dsdb_search(ldb_ctx, priv, &res,
1586 priv->realm_dn, LDB_SCOPE_SUBTREE, user_attrs,
1587 DSDB_SEARCH_NO_GLOBAL_CATALOG,
1588 "(objectClass=user)");
1590 if (lret != LDB_SUCCESS) {
1592 return HDB_ERR_NOENTRY;
1595 priv->count = res->count;
1596 priv->msgs = talloc_steal(priv, res->msgs);
1599 kdc_db_ctx->seq_ctx = priv;
1601 ret = samba_kdc_seq(context, kdc_db_ctx, entry);
1605 kdc_db_ctx->seq_ctx = NULL;
1607 talloc_free(mem_ctx);
1612 krb5_error_code samba_kdc_nextkey(krb5_context context,
1613 struct samba_kdc_db_context *kdc_db_ctx,
1614 hdb_entry_ex *entry)
1616 return samba_kdc_seq(context, kdc_db_ctx, entry);
1619 /* Check if a given entry may delegate or do s4u2self to this target principal
1621 * This is currently a very nasty hack - allowing only delegation to itself.
1624 samba_kdc_check_s4u2self(krb5_context context,
1625 struct samba_kdc_db_context *kdc_db_ctx,
1626 hdb_entry_ex *entry,
1627 krb5_const_principal target_principal)
1629 krb5_error_code ret;
1630 krb5_principal enterprise_prinicpal = NULL;
1631 struct ldb_dn *realm_dn;
1632 struct ldb_message *msg;
1633 struct dom_sid *orig_sid;
1634 struct dom_sid *target_sid;
1635 struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
1636 const char *delegation_check_attrs[] = {
1640 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_s4u2self");
1644 krb5_set_error_message(context, ret, "samba_kdc_check_s4u2self: talloc_named() failed!");
1648 if (target_principal->name.name_type == KRB5_NT_ENTERPRISE_PRINCIPAL) {
1649 /* Need to reparse the enterprise principal to find the real target */
1650 if (target_principal->name.name_string.len != 1) {
1651 ret = KRB5_PARSE_MALFORMED;
1652 krb5_set_error_message(context, ret, "samba_kdc_check_s4u2self: request for delegation to enterprise principal with wrong (%d) number of components",
1653 target_principal->name.name_string.len);
1654 talloc_free(mem_ctx);
1657 ret = krb5_parse_name(context, target_principal->name.name_string.val[0],
1658 &enterprise_prinicpal);
1660 talloc_free(mem_ctx);
1663 target_principal = enterprise_prinicpal;
1666 ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, target_principal,
1667 delegation_check_attrs, &realm_dn, &msg);
1669 krb5_free_principal(context, enterprise_prinicpal);
1672 talloc_free(mem_ctx);
1676 orig_sid = samdb_result_dom_sid(mem_ctx, p->msg, "objectSid");
1677 target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
1679 /* Allow delegation to the same principal, even if by a different
1680 * name. The easy and safe way to prove this is by SID
1682 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
1683 talloc_free(mem_ctx);
1684 return KRB5KDC_ERR_BADOPTION;
1687 talloc_free(mem_ctx);
1691 /* Certificates printed by a the Certificate Authority might have a
1692 * slightly different form of the user principal name to that in the
1693 * database. Allow a mismatch where they both refer to the same
1697 samba_kdc_check_pkinit_ms_upn_match(krb5_context context,
1698 struct samba_kdc_db_context *kdc_db_ctx,
1699 hdb_entry_ex *entry,
1700 krb5_const_principal certificate_principal)
1702 krb5_error_code ret;
1703 struct ldb_dn *realm_dn;
1704 struct ldb_message *msg;
1705 struct dom_sid *orig_sid;
1706 struct dom_sid *target_sid;
1707 struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
1708 const char *ms_upn_check_attrs[] = {
1712 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_pkinit_ms_upn_match");
1716 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
1720 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
1721 mem_ctx, certificate_principal,
1722 ms_upn_check_attrs, &realm_dn, &msg);
1725 talloc_free(mem_ctx);
1729 orig_sid = samdb_result_dom_sid(mem_ctx, p->msg, "objectSid");
1730 target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
1732 /* Consider these to be the same principal, even if by a different
1733 * name. The easy and safe way to prove this is by SID
1735 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
1736 talloc_free(mem_ctx);
1737 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
1740 talloc_free(mem_ctx);
1745 * Check if a given entry may delegate to this target principal
1749 samba_kdc_check_s4u2proxy(krb5_context context,
1750 struct samba_kdc_db_context *kdc_db_ctx,
1751 hdb_entry_ex *entry,
1752 krb5_const_principal target_principal)
1754 krb5_error_code ret;
1756 const char *client_dn = NULL;
1757 const char *target_principal_name = NULL;
1758 struct ldb_message_element *el;
1762 struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
1764 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_s4u2proxy");
1768 krb5_set_error_message(context, ret,
1769 "samba_kdc_check_s4u2proxy:"
1770 " talloc_named() failed!");
1774 client_dn = ldb_dn_get_linearized(p->msg->dn);
1780 krb5_set_error_message(context, ret,
1781 "samba_kdc_check_s4u2proxy:"
1782 " ldb_dn_get_linearized() failed!");
1787 * The main heimdal code already checked that the target_principal
1788 * belongs to the same realm as the client.
1790 * So we just need the principal without the realm,
1791 * as that is what is configured in the "msDS-AllowedToDelegateTo"
1794 ret = krb5_unparse_name_flags(context, target_principal,
1795 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &tmp);
1797 talloc_free(mem_ctx);
1798 krb5_set_error_message(context, ret,
1799 "samba_kdc_check_s4u2proxy:"
1800 " krb5_unparse_name() failed!");
1803 DEBUG(10,("samba_kdc_check_s4u2proxy: client[%s] for target[%s]\n",
1806 target_principal_name = talloc_strdup(mem_ctx, tmp);
1808 if (target_principal_name == NULL) {
1810 krb5_set_error_message(context, ret,
1811 "samba_kdc_check_s4u2proxy:"
1812 " talloc_strdup() failed!");
1816 el = ldb_msg_find_element(p->msg, "msDS-AllowedToDelegateTo");
1821 val = data_blob_string_const(target_principal_name);
1823 for (i=0; i<el->num_values; i++) {
1824 struct ldb_val *val1 = &val;
1825 struct ldb_val *val2 = &el->values[i];
1828 if (val1->length != val2->length) {
1832 cmp = strncasecmp((const char *)val1->data,
1833 (const char *)val2->data,
1847 DEBUG(10,("samba_kdc_check_s4u2proxy: client[%s] allowed target[%s]\n",
1849 talloc_free(mem_ctx);
1853 krb5_set_error_message(context, ret,
1854 "samba_kdc_check_s4u2proxy: client[%s] "
1855 "not allowed for delegation to target[%s]",
1857 target_principal_name);
1858 talloc_free(mem_ctx);
1859 return KRB5KDC_ERR_BADOPTION;
1862 NTSTATUS samba_kdc_setup_db_ctx(TALLOC_CTX *mem_ctx, struct samba_kdc_base_context *base_ctx,
1863 struct samba_kdc_db_context **kdc_db_ctx_out)
1866 struct ldb_message *msg;
1867 struct auth_session_info *session_info;
1868 struct samba_kdc_db_context *kdc_db_ctx;
1869 /* The idea here is very simple. Using Kerberos to
1870 * authenticate the KDC to the LDAP server is higly likely to
1873 * In future we may set this up to use EXERNAL and SSL
1874 * certificates, for now it will almost certainly be NTLMSSP_SET_USERNAME
1877 kdc_db_ctx = talloc_zero(mem_ctx, struct samba_kdc_db_context);
1878 if (kdc_db_ctx == NULL) {
1879 return NT_STATUS_NO_MEMORY;
1881 kdc_db_ctx->ev_ctx = base_ctx->ev_ctx;
1882 kdc_db_ctx->lp_ctx = base_ctx->lp_ctx;
1884 kdc_get_policy(base_ctx->lp_ctx, NULL, &kdc_db_ctx->policy);
1886 session_info = system_session(kdc_db_ctx->lp_ctx);
1887 if (session_info == NULL) {
1888 return NT_STATUS_INTERNAL_ERROR;
1891 /* Setup the link to LDB */
1892 kdc_db_ctx->samdb = samdb_connect(kdc_db_ctx, base_ctx->ev_ctx,
1893 base_ctx->lp_ctx, session_info, 0);
1894 if (kdc_db_ctx->samdb == NULL) {
1895 DEBUG(1, ("hdb_samba4_create: Cannot open samdb for KDC backend!"));
1896 talloc_free(kdc_db_ctx);
1897 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1900 /* Find out our own krbtgt kvno */
1901 ldb_ret = samdb_rodc(kdc_db_ctx->samdb, &kdc_db_ctx->rodc);
1902 if (ldb_ret != LDB_SUCCESS) {
1903 DEBUG(1, ("hdb_samba4_create: Cannot determine if we are an RODC in KDC backend: %s\n",
1904 ldb_errstring(kdc_db_ctx->samdb)));
1905 talloc_free(kdc_db_ctx);
1906 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1908 if (kdc_db_ctx->rodc) {
1909 int my_krbtgt_number;
1910 const char *secondary_keytab[] = { "msDS-SecondaryKrbTgtNumber", NULL };
1911 struct ldb_dn *account_dn;
1912 struct ldb_dn *server_dn = samdb_server_dn(kdc_db_ctx->samdb, kdc_db_ctx);
1914 DEBUG(1, ("hdb_samba4_create: Cannot determine server DN in KDC backend: %s\n",
1915 ldb_errstring(kdc_db_ctx->samdb)));
1916 talloc_free(kdc_db_ctx);
1917 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1920 ldb_ret = samdb_reference_dn(kdc_db_ctx->samdb, kdc_db_ctx, server_dn,
1921 "serverReference", &account_dn);
1922 if (ldb_ret != LDB_SUCCESS) {
1923 DEBUG(1, ("hdb_samba4_create: Cannot determine server account in KDC backend: %s\n",
1924 ldb_errstring(kdc_db_ctx->samdb)));
1925 talloc_free(kdc_db_ctx);
1926 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1929 ldb_ret = samdb_reference_dn(kdc_db_ctx->samdb, kdc_db_ctx, account_dn,
1930 "msDS-KrbTgtLink", &kdc_db_ctx->krbtgt_dn);
1931 talloc_free(account_dn);
1932 if (ldb_ret != LDB_SUCCESS) {
1933 DEBUG(1, ("hdb_samba4_create: Cannot determine RODC krbtgt account in KDC backend: %s\n",
1934 ldb_errstring(kdc_db_ctx->samdb)));
1935 talloc_free(kdc_db_ctx);
1936 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1939 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb, kdc_db_ctx,
1940 &msg, kdc_db_ctx->krbtgt_dn, LDB_SCOPE_BASE,
1942 DSDB_SEARCH_NO_GLOBAL_CATALOG,
1943 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=*))");
1944 if (ldb_ret != LDB_SUCCESS) {
1945 DEBUG(1, ("hdb_samba4_create: Cannot read krbtgt account %s in KDC backend to get msDS-SecondaryKrbTgtNumber: %s: %s\n",
1946 ldb_dn_get_linearized(kdc_db_ctx->krbtgt_dn),
1947 ldb_errstring(kdc_db_ctx->samdb),
1948 ldb_strerror(ldb_ret)));
1949 talloc_free(kdc_db_ctx);
1950 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1952 my_krbtgt_number = ldb_msg_find_attr_as_int(msg, "msDS-SecondaryKrbTgtNumber", -1);
1953 if (my_krbtgt_number == -1) {
1954 DEBUG(1, ("hdb_samba4_create: Cannot read msDS-SecondaryKrbTgtNumber from krbtgt account %s in KDC backend: got %d\n",
1955 ldb_dn_get_linearized(kdc_db_ctx->krbtgt_dn),
1957 talloc_free(kdc_db_ctx);
1958 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1960 kdc_db_ctx->my_krbtgt_number = my_krbtgt_number;
1963 kdc_db_ctx->my_krbtgt_number = 0;
1964 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb, kdc_db_ctx,
1966 ldb_get_default_basedn(kdc_db_ctx->samdb),
1969 DSDB_SEARCH_NO_GLOBAL_CATALOG,
1970 "(&(objectClass=user)(samAccountName=krbtgt))");
1972 if (ldb_ret != LDB_SUCCESS) {
1973 DEBUG(1, ("samba_kdc_fetch: could not find own KRBTGT in DB: %s\n", ldb_errstring(kdc_db_ctx->samdb)));
1974 talloc_free(kdc_db_ctx);
1975 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1977 kdc_db_ctx->krbtgt_dn = talloc_steal(kdc_db_ctx, msg->dn);
1978 kdc_db_ctx->my_krbtgt_number = 0;
1981 *kdc_db_ctx_out = kdc_db_ctx;
1982 return NT_STATUS_OK;