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/db-glue.h"
40 #define SAMBA_KVNO_GET_KRBTGT(kvno) \
41 ((uint16_t)(((uint32_t)kvno) >> 16))
43 #define SAMBA_KVNO_AND_KRBTGT(kvno, krbtgt) \
44 ((krb5_kvno)((((uint32_t)kvno) & 0xFFFF) | \
45 ((((uint32_t)krbtgt) << 16) & 0xFFFF0000)))
47 enum samba_kdc_ent_type
48 { SAMBA_KDC_ENT_TYPE_CLIENT, SAMBA_KDC_ENT_TYPE_SERVER,
49 SAMBA_KDC_ENT_TYPE_KRBTGT, SAMBA_KDC_ENT_TYPE_TRUST, SAMBA_KDC_ENT_TYPE_ANY };
51 enum trust_direction {
53 INBOUND = LSA_TRUST_DIRECTION_INBOUND,
54 OUTBOUND = LSA_TRUST_DIRECTION_OUTBOUND
57 static const char *trust_attrs[] = {
62 "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, uint32_t 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) {
135 flags.locked_out = 1;
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_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION) {
164 * this is confusing...
166 * UF_TRUSTED_FOR_DELEGATION
171 * UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION
172 * => trusted_for_delegation
174 flags.trusted_for_delegation = 1;
176 if (!(userAccountControl & UF_NOT_DELEGATED)) {
177 flags.forwardable = 1;
181 if (userAccountControl & UF_DONT_REQUIRE_PREAUTH) {
182 flags.require_preauth = 0;
184 flags.require_preauth = 1;
190 static int samba_kdc_entry_destructor(struct samba_kdc_entry *p)
192 hdb_entry_ex *entry_ex = p->entry_ex;
193 free_hdb_entry(&entry_ex->entry);
197 static void samba_kdc_free_entry(krb5_context context, hdb_entry_ex *entry_ex)
199 /* this function is called only from hdb_free_entry().
200 * Make sure we neutralize the destructor or we will
201 * get a double free later when hdb_free_entry() will
202 * try to call free_hdb_entry() */
203 talloc_set_destructor(entry_ex->ctx, NULL);
205 /* now proceed to free the talloc part */
206 talloc_free(entry_ex->ctx);
209 static krb5_error_code samba_kdc_message2entry_keys(krb5_context context,
210 struct samba_kdc_db_context *kdc_db_ctx,
212 struct ldb_message *msg,
215 uint32_t userAccountControl,
216 enum samba_kdc_ent_type ent_type,
217 hdb_entry_ex *entry_ex)
219 krb5_error_code ret = 0;
220 enum ndr_err_code ndr_err;
221 struct samr_Password *hash;
222 const struct ldb_val *sc_val;
223 struct supplementalCredentialsBlob scb;
224 struct supplementalCredentialsPackage *scpk = NULL;
225 bool newer_keys = false;
226 struct package_PrimaryKerberosBlob _pkb;
227 struct package_PrimaryKerberosCtr3 *pkb3 = NULL;
228 struct package_PrimaryKerberosCtr4 *pkb4 = NULL;
230 uint16_t allocated_keys = 0;
231 int rodc_krbtgt_number = 0;
233 uint32_t supported_enctypes
234 = ldb_msg_find_attr_as_uint(msg,
235 "msDS-SupportedEncryptionTypes",
238 if (rid == DOMAIN_RID_KRBTGT || is_rodc) {
239 /* KDCs (and KDCs on RODCs) use AES */
240 supported_enctypes |= ENC_HMAC_SHA1_96_AES128 | ENC_HMAC_SHA1_96_AES256;
241 } else if (userAccountControl & (UF_PARTIAL_SECRETS_ACCOUNT|UF_SERVER_TRUST_ACCOUNT)) {
242 /* DCs and RODCs comptuer accounts use AES */
243 supported_enctypes |= ENC_HMAC_SHA1_96_AES128 | ENC_HMAC_SHA1_96_AES256;
244 } else if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT ||
245 (ent_type == SAMBA_KDC_ENT_TYPE_ANY)) {
246 /* for AS-REQ the client chooses the enc types it
247 * supports, and this will vary between computers a
250 * likewise for 'any' return as much as is supported,
251 * to export into a keytab */
252 supported_enctypes = ENC_ALL_TYPES;
255 /* If UF_USE_DES_KEY_ONLY has been set, then don't allow use of the newer enc types */
256 if (userAccountControl & UF_USE_DES_KEY_ONLY) {
257 supported_enctypes = ENC_CRC32|ENC_RSA_MD5;
259 /* Otherwise, add in the default enc types */
260 supported_enctypes |= ENC_CRC32 | ENC_RSA_MD5 | ENC_RC4_HMAC_MD5;
263 /* Is this the krbtgt or a RODC krbtgt */
265 rodc_krbtgt_number = ldb_msg_find_attr_as_int(msg, "msDS-SecondaryKrbTgtNumber", -1);
267 if (rodc_krbtgt_number == -1) {
272 entry_ex->entry.keys.val = NULL;
273 entry_ex->entry.keys.len = 0;
275 kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
277 kvno = SAMBA_KVNO_AND_KRBTGT(kvno, rodc_krbtgt_number);
279 entry_ex->entry.kvno = kvno;
281 /* Get keys from the db */
283 hash = samdb_result_hash(mem_ctx, msg, "unicodePwd");
284 sc_val = ldb_msg_find_ldb_val(msg, "supplementalCredentials");
286 /* unicodePwd for enctype 0x17 (23) if present */
291 /* supplementalCredentials if present */
293 ndr_err = ndr_pull_struct_blob_all(sc_val, mem_ctx, &scb,
294 (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob);
295 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
296 dump_data(0, sc_val->data, sc_val->length);
301 if (scb.sub.signature != SUPPLEMENTAL_CREDENTIALS_SIGNATURE) {
302 NDR_PRINT_DEBUG(supplementalCredentialsBlob, &scb);
307 for (i=0; i < scb.sub.num_packages; i++) {
308 if (strcmp("Primary:Kerberos-Newer-Keys", scb.sub.packages[i].name) == 0) {
309 scpk = &scb.sub.packages[i];
310 if (!scpk->data || !scpk->data[0]) {
316 } else if (strcmp("Primary:Kerberos", scb.sub.packages[i].name) == 0) {
317 scpk = &scb.sub.packages[i];
318 if (!scpk->data || !scpk->data[0]) {
322 * we don't break here in hope to find
323 * a Kerberos-Newer-Keys package
329 * Primary:Kerberos-Newer-Keys or Primary:Kerberos element
330 * of supplementalCredentials
335 blob = strhex_to_data_blob(mem_ctx, scpk->data);
341 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
342 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &_pkb,
343 (ndr_pull_flags_fn_t)ndr_pull_package_PrimaryKerberosBlob);
344 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
346 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
347 krb5_warnx(context, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
351 if (newer_keys && _pkb.version != 4) {
353 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
354 krb5_warnx(context, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
358 if (!newer_keys && _pkb.version != 3) {
360 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
361 krb5_warnx(context, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
365 if (_pkb.version == 4) {
366 pkb4 = &_pkb.ctr.ctr4;
367 allocated_keys += pkb4->num_keys;
368 } else if (_pkb.version == 3) {
369 pkb3 = &_pkb.ctr.ctr3;
370 allocated_keys += pkb3->num_keys;
374 if (allocated_keys == 0) {
375 if (kdc_db_ctx->rodc) {
376 /* We are on an RODC, but don't have keys for this account. Signal this to the caller */
377 /* TODO: We need to call a generalised version of auth_sam_trigger_repl_secret from here */
378 return HDB_ERR_NOT_FOUND_HERE;
381 /* oh, no password. Apparently (comment in
382 * hdb-ldap.c) this violates the ASN.1, but this
383 * allows an entry with no keys (yet). */
387 /* allocate space to decode into */
388 entry_ex->entry.keys.len = 0;
389 entry_ex->entry.keys.val = calloc(allocated_keys, sizeof(Key));
390 if (entry_ex->entry.keys.val == NULL) {
395 if (hash && (supported_enctypes & ENC_RC4_HMAC_MD5)) {
399 key.salt = NULL; /* No salt for this enc type */
401 ret = krb5_keyblock_init(context,
402 ENCTYPE_ARCFOUR_HMAC,
403 hash->hash, sizeof(hash->hash),
409 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
410 entry_ex->entry.keys.len++;
414 for (i=0; i < pkb4->num_keys; i++) {
417 if (!pkb4->keys[i].value) continue;
419 if (!(kerberos_enctype_to_bitmap(pkb4->keys[i].keytype) & supported_enctypes)) {
426 if (pkb4->salt.string) {
429 salt = data_blob_string_const(pkb4->salt.string);
431 key.salt = calloc(1, sizeof(*key.salt));
432 if (key.salt == NULL) {
437 key.salt->type = hdb_pw_salt;
439 ret = krb5_data_copy(&key.salt->salt, salt.data, salt.length);
447 /* TODO: maybe pass the iteration_count somehow... */
449 ret = krb5_keyblock_init(context,
450 pkb4->keys[i].keytype,
451 pkb4->keys[i].value->data,
452 pkb4->keys[i].value->length,
454 if (ret == KRB5_PROG_ETYPE_NOSUPP) {
455 DEBUG(2,("Unsupported keytype ignored - type %u\n",
456 pkb4->keys[i].keytype));
469 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
470 entry_ex->entry.keys.len++;
473 for (i=0; i < pkb3->num_keys; i++) {
476 if (!pkb3->keys[i].value) continue;
478 if (!(kerberos_enctype_to_bitmap(pkb3->keys[i].keytype) & supported_enctypes)) {
485 if (pkb3->salt.string) {
488 salt = data_blob_string_const(pkb3->salt.string);
490 key.salt = calloc(1, sizeof(*key.salt));
491 if (key.salt == NULL) {
496 key.salt->type = hdb_pw_salt;
498 ret = krb5_data_copy(&key.salt->salt, salt.data, salt.length);
506 ret = krb5_keyblock_init(context,
507 pkb3->keys[i].keytype,
508 pkb3->keys[i].value->data,
509 pkb3->keys[i].value->length,
520 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
521 entry_ex->entry.keys.len++;
527 entry_ex->entry.keys.len = 0;
529 if (entry_ex->entry.keys.len == 0 && entry_ex->entry.keys.val) {
530 free(entry_ex->entry.keys.val);
531 entry_ex->entry.keys.val = NULL;
537 * Construct an hdb_entry from a directory entry.
539 static krb5_error_code samba_kdc_message2entry(krb5_context context,
540 struct samba_kdc_db_context *kdc_db_ctx,
541 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
542 enum samba_kdc_ent_type ent_type,
544 struct ldb_dn *realm_dn,
545 struct ldb_message *msg,
546 hdb_entry_ex *entry_ex)
548 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
549 uint32_t userAccountControl;
550 uint32_t msDS_User_Account_Control_Computed;
552 krb5_error_code ret = 0;
553 krb5_boolean is_computer = FALSE;
555 struct samba_kdc_entry *p;
560 bool is_rodc = false;
561 struct ldb_message_element *objectclasses;
562 struct ldb_val computer_val;
563 const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
564 computer_val.data = discard_const_p(uint8_t,"computer");
565 computer_val.length = strlen((const char *)computer_val.data);
567 if (ldb_msg_find_element(msg, "msDS-SecondaryKrbTgtNumber")) {
571 if (!samAccountName) {
573 krb5_set_error_message(context, ret, "samba_kdc_message2entry: no samAccountName present");
577 objectclasses = ldb_msg_find_element(msg, "objectClass");
579 if (objectclasses && ldb_msg_find_val(objectclasses, &computer_val)) {
583 memset(entry_ex, 0, sizeof(*entry_ex));
585 p = talloc(mem_ctx, struct samba_kdc_entry);
591 p->kdc_db_ctx = kdc_db_ctx;
592 p->entry_ex = entry_ex;
593 p->realm_dn = talloc_reference(p, realm_dn);
599 talloc_set_destructor(p, samba_kdc_entry_destructor);
601 /* make sure we do not have bogus data in there */
602 memset(&entry_ex->entry, 0, sizeof(hdb_entry));
605 entry_ex->free_entry = samba_kdc_free_entry;
607 userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
609 msDS_User_Account_Control_Computed
610 = ldb_msg_find_attr_as_uint(msg,
611 "msDS-User-Account-Control-Computed",
615 * This brings in the lockout flag, block the account if not
616 * found. We need the weird UF_ACCOUNTDISABLE check because
617 * we do not want to fail open if the value is not returned,
618 * but 0 is a valid value (all OK)
620 if (msDS_User_Account_Control_Computed == UF_ACCOUNTDISABLE) {
622 krb5_set_error_message(context, ret, "samba_kdc_message2entry: "
623 "no msDS-User-Account-Control-Computed present");
626 userAccountControl |= msDS_User_Account_Control_Computed;
630 * If we are set to canonicalize, we get back the fixed UPPER
631 * case realm, and the real username (ie matching LDAP
634 * Otherwise, if we are set to enterprise, we
635 * get back the whole principal as-sent
637 * Finally, if we are not set to canonicalize, we get back the
638 * fixed UPPER case realm, but the as-sent username
641 entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
642 if (ent_type == SAMBA_KDC_ENT_TYPE_KRBTGT) {
643 if (flags & (HDB_F_CANON)) {
645 * When requested to do so, ensure that the
646 * both realm values in the principal are set
647 * to the upper case, canonical realm
649 ret = krb5_make_principal(context, &entry_ex->entry.principal,
650 lpcfg_realm(lp_ctx), "krbtgt",
651 lpcfg_realm(lp_ctx), NULL);
653 krb5_clear_error_message(context);
656 krb5_principal_set_type(context, entry_ex->entry.principal, KRB5_NT_SRV_INST);
658 ret = krb5_copy_principal(context, principal, &entry_ex->entry.principal);
660 krb5_clear_error_message(context);
664 * this appears to be required regardless of
665 * the canonicalize flag from the client
667 ret = krb5_principal_set_realm(context, entry_ex->entry.principal, lpcfg_realm(lp_ctx));
669 krb5_clear_error_message(context);
674 } else if (ent_type == SAMBA_KDC_ENT_TYPE_ANY && principal == NULL) {
675 ret = krb5_make_principal(context, &entry_ex->entry.principal, lpcfg_realm(lp_ctx), samAccountName, NULL);
677 krb5_clear_error_message(context);
680 } else if (flags & HDB_F_CANON && flags & HDB_F_FOR_AS_REQ) {
682 * HDB_F_CANON maps from the canonicalize flag in the
683 * packet, and has a different meaning between AS-REQ
684 * and TGS-REQ. We only change the principal in the AS-REQ case
686 ret = krb5_make_principal(context, &entry_ex->entry.principal, lpcfg_realm(lp_ctx), samAccountName, NULL);
688 krb5_clear_error_message(context);
692 ret = krb5_copy_principal(context, principal, &entry_ex->entry.principal);
694 krb5_clear_error_message(context);
698 if (krb5_principal_get_type(context, principal) != KRB5_NT_ENTERPRISE_PRINCIPAL) {
699 /* While we have copied the client principal, tests
700 * show that Win2k3 returns the 'corrected' realm, not
701 * the client-specified realm. This code attempts to
702 * replace the client principal's realm with the one
703 * we determine from our records */
705 /* this has to be with malloc() */
706 ret = krb5_principal_set_realm(context, entry_ex->entry.principal, lpcfg_realm(lp_ctx));
708 krb5_clear_error_message(context);
714 /* First try and figure out the flags based on the userAccountControl */
715 entry_ex->entry.flags = uf2HDBFlags(context, userAccountControl, ent_type);
717 /* Windows 2008 seems to enforce this (very sensible) rule by
718 * default - don't allow offline attacks on a user's password
719 * by asking for a ticket to them as a service (encrypted with
720 * their probably patheticly insecure password) */
722 if (entry_ex->entry.flags.server
723 && lpcfg_parm_bool(lp_ctx, NULL, "kdc", "require spn for service", true)) {
724 if (!is_computer && !ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL)) {
725 entry_ex->entry.flags.server = 0;
729 * To give the correct type of error to the client, we must
730 * not just return the entry without .server set, we must
731 * pretend the principal does not exist. Otherwise we may
732 * return ERR_POLICY instead of
733 * KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
735 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER && entry_ex->entry.flags.server == 0) {
736 ret = HDB_ERR_NOENTRY;
737 krb5_set_error_message(context, ret, "samba_kdc_message2entry: no servicePrincipalName present for this server, refusing with no-such-entry");
740 if (flags & HDB_F_ADMIN_DATA) {
741 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
742 * of the Heimdal KDC. They are stored in a the traditional
743 * DB for audit purposes, and still form part of the structure
746 /* use 'whenCreated' */
747 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
748 /* use 'kadmin' for now (needed by mit_samba) */
749 ret = krb5_make_principal(context,
750 &entry_ex->entry.created_by.principal,
751 lpcfg_realm(lp_ctx), "kadmin", NULL);
753 krb5_clear_error_message(context);
757 entry_ex->entry.modified_by = (Event *) malloc(sizeof(Event));
758 if (entry_ex->entry.modified_by == NULL) {
760 krb5_set_error_message(context, ret, "malloc: out of memory");
764 /* use 'whenChanged' */
765 entry_ex->entry.modified_by->time = ldb_msg_find_krb5time_ldap_time(msg, "whenChanged", 0);
766 /* use 'kadmin' for now (needed by mit_samba) */
767 ret = krb5_make_principal(context,
768 &entry_ex->entry.modified_by->principal,
769 lpcfg_realm(lp_ctx), "kadmin", NULL);
771 krb5_clear_error_message(context);
777 /* The lack of password controls etc applies to krbtgt by
778 * virtue of being that particular RID */
779 status = dom_sid_split_rid(NULL, samdb_result_dom_sid(mem_ctx, msg, "objectSid"), NULL, &rid);
781 if (!NT_STATUS_IS_OK(status)) {
786 if (rid == DOMAIN_RID_KRBTGT) {
787 entry_ex->entry.valid_end = NULL;
788 entry_ex->entry.pw_end = NULL;
790 entry_ex->entry.flags.invalid = 0;
791 entry_ex->entry.flags.server = 1;
793 /* Don't mark all requests for the krbtgt/realm as
794 * 'change password', as otherwise we could get into
795 * trouble, and not enforce the password expirty.
796 * Instead, only do it when request is for the kpasswd service */
797 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER
798 && principal->name.name_string.len == 2
799 && (strcmp(principal->name.name_string.val[0], "kadmin") == 0)
800 && (strcmp(principal->name.name_string.val[1], "changepw") == 0)
801 && lpcfg_is_my_domain_or_realm(lp_ctx, principal->realm)) {
802 entry_ex->entry.flags.change_pw = 1;
804 entry_ex->entry.flags.client = 0;
805 entry_ex->entry.flags.forwardable = 1;
806 entry_ex->entry.flags.ok_as_delegate = 1;
807 } else if (is_rodc) {
808 /* The RODC krbtgt account is like the main krbtgt,
809 * but it does not have a changepw or kadmin
812 entry_ex->entry.valid_end = NULL;
813 entry_ex->entry.pw_end = NULL;
815 /* Also don't allow the RODC krbtgt to be a client (it should not be needed) */
816 entry_ex->entry.flags.client = 0;
817 entry_ex->entry.flags.invalid = 0;
818 entry_ex->entry.flags.server = 1;
820 entry_ex->entry.flags.client = 0;
821 entry_ex->entry.flags.forwardable = 1;
822 entry_ex->entry.flags.ok_as_delegate = 0;
823 } else if (entry_ex->entry.flags.server && ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
824 /* The account/password expiry only applies when the account is used as a
825 * client (ie password login), not when used as a server */
827 /* Make very well sure we don't use this for a client,
828 * it could bypass the password restrictions */
829 entry_ex->entry.flags.client = 0;
831 entry_ex->entry.valid_end = NULL;
832 entry_ex->entry.pw_end = NULL;
835 NTTIME must_change_time
836 = samdb_result_force_password_change(kdc_db_ctx->samdb, mem_ctx,
838 if (must_change_time == 0x7FFFFFFFFFFFFFFFULL) {
839 entry_ex->entry.pw_end = NULL;
841 entry_ex->entry.pw_end = malloc(sizeof(*entry_ex->entry.pw_end));
842 if (entry_ex->entry.pw_end == NULL) {
846 *entry_ex->entry.pw_end = nt_time_to_unix(must_change_time);
849 acct_expiry = samdb_result_account_expires(msg);
850 if (acct_expiry == 0x7FFFFFFFFFFFFFFFULL) {
851 entry_ex->entry.valid_end = NULL;
853 entry_ex->entry.valid_end = malloc(sizeof(*entry_ex->entry.valid_end));
854 if (entry_ex->entry.valid_end == NULL) {
858 *entry_ex->entry.valid_end = nt_time_to_unix(acct_expiry);
862 entry_ex->entry.valid_start = NULL;
864 entry_ex->entry.max_life = malloc(sizeof(*entry_ex->entry.max_life));
865 if (entry_ex->entry.max_life == NULL) {
870 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
871 *entry_ex->entry.max_life = kdc_db_ctx->policy.svc_tkt_lifetime;
872 } else if (ent_type == SAMBA_KDC_ENT_TYPE_KRBTGT || ent_type == SAMBA_KDC_ENT_TYPE_CLIENT) {
873 *entry_ex->entry.max_life = kdc_db_ctx->policy.usr_tkt_lifetime;
875 *entry_ex->entry.max_life = MIN(kdc_db_ctx->policy.svc_tkt_lifetime,
876 kdc_db_ctx->policy.usr_tkt_lifetime);
879 entry_ex->entry.max_renew = malloc(sizeof(*entry_ex->entry.max_life));
880 if (entry_ex->entry.max_renew == NULL) {
885 *entry_ex->entry.max_renew = kdc_db_ctx->policy.renewal_lifetime;
887 entry_ex->entry.generation = NULL;
889 /* Get keys from the db */
890 ret = samba_kdc_message2entry_keys(context, kdc_db_ctx, p, msg,
891 rid, is_rodc, userAccountControl,
894 /* Could be bougus data in the entry, or out of memory */
898 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
899 if (entry_ex->entry.etypes == NULL) {
900 krb5_clear_error_message(context);
904 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
905 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
906 if (entry_ex->entry.etypes->val == NULL) {
907 krb5_clear_error_message(context);
911 for (i=0; i < entry_ex->entry.etypes->len; i++) {
912 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
916 p->msg = talloc_steal(p, msg);
920 /* This doesn't free ent itself, that is for the eventual caller to do */
921 hdb_free_entry(context, entry_ex);
923 talloc_steal(kdc_db_ctx, entry_ex->ctx);
930 * Construct an hdb_entry from a directory entry.
931 * The kvno is what the remote client asked for
933 static krb5_error_code samba_kdc_trust_message2entry(krb5_context context,
934 struct samba_kdc_db_context *kdc_db_ctx,
935 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
936 enum trust_direction direction,
937 struct ldb_dn *realm_dn,
940 struct ldb_message *msg,
941 hdb_entry_ex *entry_ex)
943 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
944 const char *dnsdomain;
945 const char *realm = lpcfg_realm(lp_ctx);
946 DATA_BLOB password_utf16 = data_blob_null;
947 DATA_BLOB password_utf8 = data_blob_null;
948 struct samr_Password _password_hash;
949 const struct samr_Password *password_hash = NULL;
950 const struct ldb_val *password_val;
951 struct trustAuthInOutBlob password_blob;
952 struct samba_kdc_entry *p;
954 uint32_t current_kvno;
955 uint32_t num_keys = 0;
956 enum ndr_err_code ndr_err;
957 int ret, trust_direction_flags;
959 struct AuthenticationInformationArray *auth_array;
960 uint32_t supported_enctypes = ENC_RC4_HMAC_MD5;
962 if (dsdb_functional_level(kdc_db_ctx->samdb) >= DS_DOMAIN_FUNCTION_2008) {
963 supported_enctypes = ldb_msg_find_attr_as_uint(msg,
964 "msDS-SupportedEncryptionTypes",
968 trust_direction_flags = ldb_msg_find_attr_as_int(msg, "trustDirection", 0);
970 if (direction == INBOUND) {
971 password_val = ldb_msg_find_ldb_val(msg, "trustAuthIncoming");
973 } else { /* OUTBOUND */
974 dnsdomain = ldb_msg_find_attr_as_string(msg, "trustPartner", NULL);
976 realm = strupper_talloc(mem_ctx, dnsdomain);
977 password_val = ldb_msg_find_ldb_val(msg, "trustAuthOutgoing");
980 if (!password_val || !(trust_direction_flags & direction)) {
981 krb5_clear_error_message(context);
982 ret = HDB_ERR_NOENTRY;
986 ndr_err = ndr_pull_struct_blob(password_val, mem_ctx, &password_blob,
987 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
988 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
989 krb5_clear_error_message(context);
994 p = talloc(mem_ctx, struct samba_kdc_entry);
1000 p->kdc_db_ctx = kdc_db_ctx;
1001 p->entry_ex = entry_ex;
1002 p->realm_dn = realm_dn;
1004 talloc_set_destructor(p, samba_kdc_entry_destructor);
1006 /* make sure we do not have bogus data in there */
1007 memset(&entry_ex->entry, 0, sizeof(hdb_entry));
1010 entry_ex->free_entry = samba_kdc_free_entry;
1012 /* use 'whenCreated' */
1013 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
1014 /* use 'kadmin' for now (needed by mit_samba) */
1015 ret = krb5_make_principal(context,
1016 &entry_ex->entry.created_by.principal,
1017 realm, "kadmin", NULL);
1019 krb5_clear_error_message(context);
1023 entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
1024 if (entry_ex->entry.principal == NULL) {
1025 krb5_clear_error_message(context);
1030 ret = copy_Principal(principal, entry_ex->entry.principal);
1032 krb5_clear_error_message(context);
1037 * While we have copied the client principal, tests
1038 * show that Win2k3 returns the 'corrected' realm, not
1039 * the client-specified realm. This code attempts to
1040 * replace the client principal's realm with the one
1041 * we determine from our records
1044 ret = krb5_principal_set_realm(context, entry_ex->entry.principal, realm);
1046 krb5_clear_error_message(context);
1050 entry_ex->entry.valid_start = NULL;
1052 /* we need to work out if we are going to use the current or
1053 * the previous password hash.
1054 * We base this on the kvno the client passes in. If the kvno
1055 * passed in is equal to the current kvno in our database then
1056 * we use the current structure. If it is the current kvno-1,
1057 * then we use the previous substrucure.
1060 /* first work out the current kvno */
1062 for (i=0; i < password_blob.count; i++) {
1063 if (password_blob.current.array[i].AuthType == TRUST_AUTH_TYPE_VERSION) {
1064 current_kvno = password_blob.current.array[i].AuthInfo.version.version;
1068 /* work out whether we will use the previous or current
1070 if (password_blob.previous.count == 0) {
1071 /* there is no previous password */
1072 use_previous = false;
1073 } else if (!(flags & HDB_F_KVNO_SPECIFIED) ||
1074 kvno == current_kvno) {
1075 use_previous = false;
1076 } else if ((kvno+1 == current_kvno) ||
1077 (kvno == 255 && current_kvno == 0)) {
1078 use_previous = true;
1080 DEBUG(1,(__location__ ": Request for unknown kvno %u - current kvno is %u\n",
1081 kvno, current_kvno));
1082 krb5_clear_error_message(context);
1083 ret = HDB_ERR_NOENTRY;
1088 auth_array = &password_blob.previous;
1090 auth_array = &password_blob.current;
1093 /* use the kvno the client specified, if available */
1094 if (flags & HDB_F_KVNO_SPECIFIED) {
1095 entry_ex->entry.kvno = kvno;
1097 entry_ex->entry.kvno = current_kvno;
1100 for (i=0; i < auth_array->count; i++) {
1101 if (auth_array->array[i].AuthType == TRUST_AUTH_TYPE_CLEAR) {
1104 password_utf16 = data_blob_const(auth_array->array[i].AuthInfo.clear.password,
1105 auth_array->array[i].AuthInfo.clear.size);
1106 if (password_utf16.length == 0) {
1110 if (supported_enctypes & ENC_RC4_HMAC_MD5) {
1111 mdfour(_password_hash.hash, password_utf16.data, password_utf16.length);
1112 if (password_hash == NULL) {
1115 password_hash = &_password_hash;
1118 if (!(supported_enctypes & (ENC_HMAC_SHA1_96_AES128|ENC_HMAC_SHA1_96_AES256))) {
1122 ok = convert_string_talloc(mem_ctx,
1123 CH_UTF16MUNGED, CH_UTF8,
1124 password_utf16.data,
1125 password_utf16.length,
1126 (void *)&password_utf8.data,
1127 &password_utf8.length);
1129 krb5_clear_error_message(context);
1134 if (supported_enctypes & ENC_HMAC_SHA1_96_AES128) {
1137 if (supported_enctypes & ENC_HMAC_SHA1_96_AES256) {
1141 } else if (auth_array->array[i].AuthType == TRUST_AUTH_TYPE_NT4OWF) {
1142 if (supported_enctypes & ENC_RC4_HMAC_MD5) {
1143 password_hash = &auth_array->array[i].AuthInfo.nt4owf.password;
1149 /* Must have found a cleartext or MD4 password */
1150 if (num_keys == 0) {
1151 DEBUG(1,(__location__ ": no usable key found\n"));
1152 krb5_clear_error_message(context);
1153 ret = HDB_ERR_NOENTRY;
1157 entry_ex->entry.keys.val = calloc(num_keys, sizeof(Key));
1158 if (entry_ex->entry.keys.val == NULL) {
1159 krb5_clear_error_message(context);
1164 if (password_utf8.length != 0) {
1166 krb5_const_principal salt_principal = principal;
1168 krb5_data cleartext_data;
1170 cleartext_data.data = password_utf8.data;
1171 cleartext_data.length = password_utf8.length;
1173 ret = krb5_get_pw_salt(context,
1180 if (supported_enctypes & ENC_HMAC_SHA1_96_AES256) {
1181 ret = krb5_string_to_key_data_salt(context,
1182 ENCTYPE_AES256_CTS_HMAC_SHA1_96,
1187 krb5_free_salt(context, salt);
1191 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
1192 entry_ex->entry.keys.len++;
1195 if (supported_enctypes & ENC_HMAC_SHA1_96_AES128) {
1196 ret = krb5_string_to_key_data_salt(context,
1197 ENCTYPE_AES128_CTS_HMAC_SHA1_96,
1202 krb5_free_salt(context, salt);
1206 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
1207 entry_ex->entry.keys.len++;
1210 krb5_free_salt(context, salt);
1213 if (password_hash != NULL) {
1216 ret = krb5_keyblock_init(context,
1217 ENCTYPE_ARCFOUR_HMAC,
1218 password_hash->hash,
1219 sizeof(password_hash->hash),
1225 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
1226 entry_ex->entry.keys.len++;
1229 entry_ex->entry.flags = int2HDBFlags(0);
1230 entry_ex->entry.flags.immutable = 1;
1231 entry_ex->entry.flags.invalid = 0;
1232 entry_ex->entry.flags.server = 1;
1233 entry_ex->entry.flags.require_preauth = 1;
1235 entry_ex->entry.pw_end = NULL;
1237 entry_ex->entry.max_life = NULL;
1239 entry_ex->entry.max_renew = NULL;
1241 entry_ex->entry.generation = NULL;
1243 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
1244 if (entry_ex->entry.etypes == NULL) {
1245 krb5_clear_error_message(context);
1249 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
1250 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
1251 if (entry_ex->entry.etypes->val == NULL) {
1252 krb5_clear_error_message(context);
1256 for (i=0; i < entry_ex->entry.etypes->len; i++) {
1257 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
1261 p->msg = talloc_steal(p, msg);
1265 /* This doesn't free ent itself, that is for the eventual caller to do */
1266 hdb_free_entry(context, entry_ex);
1268 talloc_steal(kdc_db_ctx, entry_ex->ctx);
1275 static krb5_error_code samba_kdc_lookup_trust(krb5_context context, struct ldb_context *ldb_ctx,
1276 TALLOC_CTX *mem_ctx,
1278 struct ldb_dn *realm_dn,
1279 struct ldb_message **pmsg)
1282 const char * const *attrs = trust_attrs;
1284 status = sam_get_results_trust(ldb_ctx,
1285 mem_ctx, realm, realm, attrs,
1287 if (NT_STATUS_IS_OK(status)) {
1289 } else if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
1290 return HDB_ERR_NOENTRY;
1291 } else if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1293 krb5_set_error_message(context, ret, "get_sam_result_trust: out of memory");
1297 krb5_set_error_message(context, ret, "get_sam_result_trust: %s", nt_errstr(status));
1302 static krb5_error_code samba_kdc_lookup_client(krb5_context context,
1303 struct samba_kdc_db_context *kdc_db_ctx,
1304 TALLOC_CTX *mem_ctx,
1305 krb5_const_principal principal,
1307 struct ldb_dn **realm_dn,
1308 struct ldb_message **msg) {
1310 char *principal_string;
1312 if (krb5_principal_get_type(context, principal) == KRB5_NT_ENTERPRISE_PRINCIPAL) {
1313 principal_string = smb_krb5_principal_get_comp_string(mem_ctx, context,
1315 if (principal_string == NULL) {
1318 nt_status = sam_get_results_principal(kdc_db_ctx->samdb,
1319 mem_ctx, principal_string, attrs,
1321 TALLOC_FREE(principal_string);
1323 krb5_error_code ret;
1324 ret = krb5_unparse_name(context, principal, &principal_string);
1328 nt_status = sam_get_results_principal(kdc_db_ctx->samdb,
1329 mem_ctx, principal_string, attrs,
1331 free(principal_string);
1334 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
1335 return HDB_ERR_NOENTRY;
1336 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
1338 } else if (!NT_STATUS_IS_OK(nt_status)) {
1345 static krb5_error_code samba_kdc_fetch_client(krb5_context context,
1346 struct samba_kdc_db_context *kdc_db_ctx,
1347 TALLOC_CTX *mem_ctx,
1348 krb5_const_principal principal,
1350 hdb_entry_ex *entry_ex) {
1351 struct ldb_dn *realm_dn;
1352 krb5_error_code ret;
1353 struct ldb_message *msg = NULL;
1355 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
1356 mem_ctx, principal, user_attrs,
1362 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1363 principal, SAMBA_KDC_ENT_TYPE_CLIENT,
1365 realm_dn, msg, entry_ex);
1369 static krb5_error_code samba_kdc_fetch_krbtgt(krb5_context context,
1370 struct samba_kdc_db_context *kdc_db_ctx,
1371 TALLOC_CTX *mem_ctx,
1372 krb5_const_principal principal,
1375 hdb_entry_ex *entry_ex)
1377 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
1378 krb5_error_code ret;
1379 struct ldb_message *msg = NULL;
1380 struct ldb_dn *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
1382 krb5_principal alloc_principal = NULL;
1383 if (principal->name.name_string.len != 2
1384 || (strcmp(principal->name.name_string.val[0], KRB5_TGS_NAME) != 0)) {
1386 return HDB_ERR_NOENTRY;
1389 /* krbtgt case. Either us or a trusted realm */
1391 if (lpcfg_is_my_domain_or_realm(lp_ctx, principal->realm)
1392 && lpcfg_is_my_domain_or_realm(lp_ctx, principal->name.name_string.val[1])) {
1393 /* us, or someone quite like us */
1394 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1395 * is in our db, then direct the caller at our primary
1399 unsigned int krbtgt_number;
1400 /* w2k8r2 sometimes gives us a kvno of 255 for inter-domain
1401 trust tickets. We don't yet know what this means, but we do
1402 seem to need to treat it as unspecified */
1403 if (flags & HDB_F_KVNO_SPECIFIED) {
1404 krbtgt_number = SAMBA_KVNO_GET_KRBTGT(kvno);
1405 if (kdc_db_ctx->rodc) {
1406 if (krbtgt_number != kdc_db_ctx->my_krbtgt_number) {
1407 return HDB_ERR_NOT_FOUND_HERE;
1411 krbtgt_number = kdc_db_ctx->my_krbtgt_number;
1414 if (krbtgt_number == kdc_db_ctx->my_krbtgt_number) {
1415 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
1416 &msg, kdc_db_ctx->krbtgt_dn, LDB_SCOPE_BASE,
1417 krbtgt_attrs, DSDB_SEARCH_NO_GLOBAL_CATALOG,
1418 "(objectClass=user)");
1420 /* We need to look up an RODC krbtgt (perhaps
1421 * ours, if we are an RODC, perhaps another
1422 * RODC if we are a read-write DC */
1423 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
1424 &msg, realm_dn, LDB_SCOPE_SUBTREE,
1426 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
1427 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=%u))", (unsigned)(krbtgt_number));
1430 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
1431 krb5_warnx(context, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1432 (unsigned)(krbtgt_number));
1433 krb5_set_error_message(context, HDB_ERR_NOENTRY,
1434 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1435 (unsigned)(krbtgt_number));
1436 return HDB_ERR_NOENTRY;
1437 } else if (lret != LDB_SUCCESS) {
1438 krb5_warnx(context, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1439 (unsigned)(krbtgt_number));
1440 krb5_set_error_message(context, HDB_ERR_NOENTRY,
1441 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1442 (unsigned)(krbtgt_number));
1443 return HDB_ERR_NOENTRY;
1446 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1447 principal, SAMBA_KDC_ENT_TYPE_KRBTGT,
1448 flags, realm_dn, msg, entry_ex);
1449 if (alloc_principal) {
1450 /* This is again copied in the message2entry call */
1451 krb5_free_principal(context, alloc_principal);
1454 krb5_warnx(context, "samba_kdc_fetch: self krbtgt message2entry failed");
1459 enum trust_direction direction = UNKNOWN;
1460 const char *realm = NULL;
1462 /* Either an inbound or outbound trust */
1464 if (strcasecmp(lpcfg_realm(lp_ctx), principal->realm) == 0) {
1465 /* look for inbound trust */
1466 direction = INBOUND;
1467 realm = principal->name.name_string.val[1];
1468 } else if (strcasecmp(lpcfg_realm(lp_ctx), principal->name.name_string.val[1]) == 0) {
1469 /* look for outbound trust */
1470 direction = OUTBOUND;
1471 realm = principal->realm;
1473 krb5_warnx(context, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1474 principal->realm, principal->name.name_string.val[1]);
1475 krb5_set_error_message(context, HDB_ERR_NOENTRY, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1476 principal->realm, principal->name.name_string.val[1]);
1477 return HDB_ERR_NOENTRY;
1480 /* Trusted domains are under CN=system */
1482 ret = samba_kdc_lookup_trust(context, kdc_db_ctx->samdb,
1484 realm, realm_dn, &msg);
1487 krb5_warnx(context, "samba_kdc_fetch: could not find principal in DB");
1488 krb5_set_error_message(context, ret, "samba_kdc_fetch: could not find principal in DB");
1492 ret = samba_kdc_trust_message2entry(context, kdc_db_ctx, mem_ctx,
1493 principal, direction,
1494 realm_dn, flags, kvno, msg, entry_ex);
1496 krb5_warnx(context, "samba_kdc_fetch: trust_message2entry failed for %s",
1497 ldb_dn_get_linearized(msg->dn));
1498 krb5_set_error_message(context, ret, "samba_kdc_fetch: "
1499 "trust_message2entry failed for %s",
1500 ldb_dn_get_linearized(msg->dn));
1507 static krb5_error_code samba_kdc_lookup_server(krb5_context context,
1508 struct samba_kdc_db_context *kdc_db_ctx,
1509 TALLOC_CTX *mem_ctx,
1510 krb5_const_principal principal,
1513 struct ldb_dn **realm_dn,
1514 struct ldb_message **msg)
1516 krb5_error_code ret;
1517 if ((smb_krb5_principal_get_type(context, principal) != KRB5_NT_ENTERPRISE_PRINCIPAL)
1518 && krb5_princ_size(context, principal) >= 2) {
1519 /* 'normal server' case */
1522 struct ldb_dn *user_dn;
1523 char *principal_string;
1525 ret = krb5_unparse_name_flags(context, principal,
1526 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
1532 /* At this point we may find the host is known to be
1533 * in a different realm, so we should generate a
1534 * referral instead */
1535 nt_status = crack_service_principal_name(kdc_db_ctx->samdb,
1536 mem_ctx, principal_string,
1537 &user_dn, realm_dn);
1538 free(principal_string);
1540 if (!NT_STATUS_IS_OK(nt_status)) {
1541 return HDB_ERR_NOENTRY;
1544 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb,
1546 msg, user_dn, LDB_SCOPE_BASE,
1548 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
1550 if (ldb_ret != LDB_SUCCESS) {
1551 return HDB_ERR_NOENTRY;
1554 } else if (!(flags & HDB_F_FOR_AS_REQ)
1555 && smb_krb5_principal_get_type(context, principal) == KRB5_NT_ENTERPRISE_PRINCIPAL) {
1557 * The behaviour of accepting an
1558 * KRB5_NT_ENTERPRISE_PRINCIPAL server principal
1559 * containing a UPN only applies to TGS-REQ packets,
1560 * not AS-REQ packets.
1562 return samba_kdc_lookup_client(context, kdc_db_ctx,
1563 mem_ctx, principal, attrs,
1568 * - the AS-REQ, where we only accept
1569 * samAccountName based lookups for the server, no
1570 * matter if the name is an
1571 * KRB5_NT_ENTERPRISE_PRINCIPAL or not
1572 * - for the TGS-REQ when we are not given an
1573 * KRB5_NT_ENTERPRISE_PRINCIPAL, which also must
1574 * only lookup samAccountName based names.
1578 krb5_principal enterprise_prinicpal = NULL;
1580 if (smb_krb5_principal_get_type(context, principal) == KRB5_NT_ENTERPRISE_PRINCIPAL) {
1581 /* Need to reparse the enterprise principal to find the real target */
1582 if (principal->name.name_string.len != 1) {
1583 ret = KRB5_PARSE_MALFORMED;
1584 krb5_set_error_message(context, ret, "samba_kdc_lookup_server: request for an "
1585 "enterprise principal with wrong (%d) number of components",
1586 principal->name.name_string.len);
1589 ret = krb5_parse_name(context, principal->name.name_string.val[0],
1590 &enterprise_prinicpal);
1592 talloc_free(mem_ctx);
1595 principal = enterprise_prinicpal;
1598 /* server as client principal case, but we must not lookup userPrincipalNames */
1599 *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
1601 /* TODO: Check if it is our realm, otherwise give referral */
1603 ret = krb5_unparse_name_flags(context, principal,
1604 KRB5_PRINCIPAL_UNPARSE_NO_REALM |
1605 KRB5_PRINCIPAL_UNPARSE_DISPLAY,
1609 krb5_set_error_message(context, ret, "samba_kdc_lookup_principal: could not parse principal");
1610 krb5_warnx(context, "samba_kdc_lookup_principal: could not parse principal");
1614 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx, msg,
1615 *realm_dn, LDB_SCOPE_SUBTREE,
1617 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
1618 "(&(objectClass=user)(samAccountName=%s))",
1619 ldb_binary_encode_string(mem_ctx, short_princ));
1620 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
1621 DEBUG(3, ("Failed to find an entry for %s\n", short_princ));
1623 return HDB_ERR_NOENTRY;
1625 if (lret != LDB_SUCCESS) {
1626 DEBUG(3, ("Failed single search for %s - %s\n",
1627 short_princ, ldb_errstring(kdc_db_ctx->samdb)));
1629 return HDB_ERR_NOENTRY;
1634 return HDB_ERR_NOENTRY;
1639 static krb5_error_code samba_kdc_fetch_server(krb5_context context,
1640 struct samba_kdc_db_context *kdc_db_ctx,
1641 TALLOC_CTX *mem_ctx,
1642 krb5_const_principal principal,
1644 hdb_entry_ex *entry_ex)
1646 krb5_error_code ret;
1647 struct ldb_dn *realm_dn;
1648 struct ldb_message *msg;
1650 ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, principal,
1651 flags, server_attrs, &realm_dn, &msg);
1656 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1657 principal, SAMBA_KDC_ENT_TYPE_SERVER,
1659 realm_dn, msg, entry_ex);
1661 krb5_warnx(context, "samba_kdc_fetch: message2entry failed");
1667 krb5_error_code samba_kdc_fetch(krb5_context context,
1668 struct samba_kdc_db_context *kdc_db_ctx,
1669 krb5_const_principal principal,
1672 hdb_entry_ex *entry_ex)
1674 krb5_error_code ret = HDB_ERR_NOENTRY;
1675 TALLOC_CTX *mem_ctx;
1677 mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_fetch context");
1680 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
1684 if (flags & HDB_F_GET_CLIENT) {
1685 ret = samba_kdc_fetch_client(context, kdc_db_ctx, mem_ctx, principal, flags, entry_ex);
1686 if (ret != HDB_ERR_NOENTRY) goto done;
1688 if (flags & HDB_F_GET_SERVER) {
1689 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
1690 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, flags, kvno, entry_ex);
1691 if (ret != HDB_ERR_NOENTRY) goto done;
1693 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
1694 ret = samba_kdc_fetch_server(context, kdc_db_ctx, mem_ctx, principal, flags, entry_ex);
1695 if (ret != HDB_ERR_NOENTRY) goto done;
1697 if (flags & HDB_F_GET_KRBTGT) {
1698 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, flags, kvno, entry_ex);
1699 if (ret != HDB_ERR_NOENTRY) goto done;
1703 talloc_free(mem_ctx);
1707 struct samba_kdc_seq {
1710 struct ldb_message **msgs;
1711 struct ldb_dn *realm_dn;
1714 static krb5_error_code samba_kdc_seq(krb5_context context,
1715 struct samba_kdc_db_context *kdc_db_ctx,
1716 hdb_entry_ex *entry)
1718 krb5_error_code ret;
1719 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
1720 TALLOC_CTX *mem_ctx;
1721 hdb_entry_ex entry_ex;
1722 memset(&entry_ex, '\0', sizeof(entry_ex));
1725 return HDB_ERR_NOENTRY;
1728 mem_ctx = talloc_named(priv, 0, "samba_kdc_seq context");
1732 krb5_set_error_message(context, ret, "samba_kdc_seq: talloc_named() failed!");
1736 if (priv->index < priv->count) {
1737 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1738 NULL, SAMBA_KDC_ENT_TYPE_ANY,
1739 HDB_F_ADMIN_DATA|HDB_F_GET_ANY,
1740 priv->realm_dn, priv->msgs[priv->index++], entry);
1742 ret = HDB_ERR_NOENTRY;
1747 kdc_db_ctx->seq_ctx = NULL;
1749 talloc_free(mem_ctx);
1755 krb5_error_code samba_kdc_firstkey(krb5_context context,
1756 struct samba_kdc_db_context *kdc_db_ctx,
1757 hdb_entry_ex *entry)
1759 struct ldb_context *ldb_ctx = kdc_db_ctx->samdb;
1760 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
1762 struct ldb_result *res = NULL;
1763 krb5_error_code ret;
1764 TALLOC_CTX *mem_ctx;
1769 kdc_db_ctx->seq_ctx = NULL;
1772 priv = (struct samba_kdc_seq *) talloc(kdc_db_ctx, struct samba_kdc_seq);
1775 krb5_set_error_message(context, ret, "talloc: out of memory");
1781 priv->realm_dn = ldb_get_default_basedn(ldb_ctx);
1784 mem_ctx = talloc_named(priv, 0, "samba_kdc_firstkey context");
1788 krb5_set_error_message(context, ret, "samba_kdc_firstkey: talloc_named() failed!");
1792 ret = krb5_get_default_realm(context, &realm);
1797 krb5_free_default_realm(context, realm);
1799 lret = dsdb_search(ldb_ctx, priv, &res,
1800 priv->realm_dn, LDB_SCOPE_SUBTREE, user_attrs,
1801 DSDB_SEARCH_NO_GLOBAL_CATALOG,
1802 "(objectClass=user)");
1804 if (lret != LDB_SUCCESS) {
1806 return HDB_ERR_NOENTRY;
1809 priv->count = res->count;
1810 priv->msgs = talloc_steal(priv, res->msgs);
1813 kdc_db_ctx->seq_ctx = priv;
1815 ret = samba_kdc_seq(context, kdc_db_ctx, entry);
1819 kdc_db_ctx->seq_ctx = NULL;
1821 talloc_free(mem_ctx);
1826 krb5_error_code samba_kdc_nextkey(krb5_context context,
1827 struct samba_kdc_db_context *kdc_db_ctx,
1828 hdb_entry_ex *entry)
1830 return samba_kdc_seq(context, kdc_db_ctx, entry);
1833 /* Check if a given entry may delegate or do s4u2self to this target principal
1835 * This is currently a very nasty hack - allowing only delegation to itself.
1838 samba_kdc_check_s4u2self(krb5_context context,
1839 struct samba_kdc_db_context *kdc_db_ctx,
1840 hdb_entry_ex *entry,
1841 krb5_const_principal target_principal)
1843 krb5_error_code ret;
1844 struct ldb_dn *realm_dn;
1845 struct ldb_message *msg;
1846 struct dom_sid *orig_sid;
1847 struct dom_sid *target_sid;
1848 struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
1849 const char *delegation_check_attrs[] = {
1853 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_s4u2self");
1857 krb5_set_error_message(context, ret, "samba_kdc_check_s4u2self: talloc_named() failed!");
1861 ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, target_principal,
1862 HDB_F_GET_CLIENT|HDB_F_GET_SERVER,
1863 delegation_check_attrs, &realm_dn, &msg);
1866 talloc_free(mem_ctx);
1870 orig_sid = samdb_result_dom_sid(mem_ctx, p->msg, "objectSid");
1871 target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
1873 /* Allow delegation to the same principal, even if by a different
1874 * name. The easy and safe way to prove this is by SID
1876 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
1877 talloc_free(mem_ctx);
1878 return KRB5KDC_ERR_BADOPTION;
1881 talloc_free(mem_ctx);
1885 /* Certificates printed by a the Certificate Authority might have a
1886 * slightly different form of the user principal name to that in the
1887 * database. Allow a mismatch where they both refer to the same
1891 samba_kdc_check_pkinit_ms_upn_match(krb5_context context,
1892 struct samba_kdc_db_context *kdc_db_ctx,
1893 hdb_entry_ex *entry,
1894 krb5_const_principal certificate_principal)
1896 krb5_error_code ret;
1897 struct ldb_dn *realm_dn;
1898 struct ldb_message *msg;
1899 struct dom_sid *orig_sid;
1900 struct dom_sid *target_sid;
1901 struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
1902 const char *ms_upn_check_attrs[] = {
1906 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_pkinit_ms_upn_match");
1910 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
1914 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
1915 mem_ctx, certificate_principal,
1916 ms_upn_check_attrs, &realm_dn, &msg);
1919 talloc_free(mem_ctx);
1923 orig_sid = samdb_result_dom_sid(mem_ctx, p->msg, "objectSid");
1924 target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
1926 /* Consider these to be the same principal, even if by a different
1927 * name. The easy and safe way to prove this is by SID
1929 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
1930 talloc_free(mem_ctx);
1931 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
1934 talloc_free(mem_ctx);
1939 * Check if a given entry may delegate to this target principal
1943 samba_kdc_check_s4u2proxy(krb5_context context,
1944 struct samba_kdc_db_context *kdc_db_ctx,
1945 hdb_entry_ex *entry,
1946 krb5_const_principal target_principal)
1948 krb5_error_code ret;
1950 const char *client_dn = NULL;
1951 const char *target_principal_name = NULL;
1952 struct ldb_message_element *el;
1956 struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
1958 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_s4u2proxy");
1962 krb5_set_error_message(context, ret,
1963 "samba_kdc_check_s4u2proxy:"
1964 " talloc_named() failed!");
1968 client_dn = ldb_dn_get_linearized(p->msg->dn);
1974 krb5_set_error_message(context, ret,
1975 "samba_kdc_check_s4u2proxy:"
1976 " ldb_dn_get_linearized() failed!");
1981 * The main heimdal code already checked that the target_principal
1982 * belongs to the same realm as the client.
1984 * So we just need the principal without the realm,
1985 * as that is what is configured in the "msDS-AllowedToDelegateTo"
1988 ret = krb5_unparse_name_flags(context, target_principal,
1989 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &tmp);
1991 talloc_free(mem_ctx);
1992 krb5_set_error_message(context, ret,
1993 "samba_kdc_check_s4u2proxy:"
1994 " krb5_unparse_name() failed!");
1997 DEBUG(10,("samba_kdc_check_s4u2proxy: client[%s] for target[%s]\n",
2000 target_principal_name = talloc_strdup(mem_ctx, tmp);
2002 if (target_principal_name == NULL) {
2004 krb5_set_error_message(context, ret,
2005 "samba_kdc_check_s4u2proxy:"
2006 " talloc_strdup() failed!");
2010 el = ldb_msg_find_element(p->msg, "msDS-AllowedToDelegateTo");
2015 val = data_blob_string_const(target_principal_name);
2017 for (i=0; i<el->num_values; i++) {
2018 struct ldb_val *val1 = &val;
2019 struct ldb_val *val2 = &el->values[i];
2022 if (val1->length != val2->length) {
2026 cmp = strncasecmp((const char *)val1->data,
2027 (const char *)val2->data,
2041 DEBUG(10,("samba_kdc_check_s4u2proxy: client[%s] allowed target[%s]\n",
2043 talloc_free(mem_ctx);
2047 krb5_set_error_message(context, ret,
2048 "samba_kdc_check_s4u2proxy: client[%s] "
2049 "not allowed for delegation to target[%s]",
2051 target_principal_name);
2052 talloc_free(mem_ctx);
2053 return KRB5KDC_ERR_BADOPTION;
2056 NTSTATUS samba_kdc_setup_db_ctx(TALLOC_CTX *mem_ctx, struct samba_kdc_base_context *base_ctx,
2057 struct samba_kdc_db_context **kdc_db_ctx_out)
2060 struct ldb_message *msg;
2061 struct auth_session_info *session_info;
2062 struct samba_kdc_db_context *kdc_db_ctx;
2063 /* The idea here is very simple. Using Kerberos to
2064 * authenticate the KDC to the LDAP server is higly likely to
2067 * In future we may set this up to use EXERNAL and SSL
2068 * certificates, for now it will almost certainly be NTLMSSP_SET_USERNAME
2071 kdc_db_ctx = talloc_zero(mem_ctx, struct samba_kdc_db_context);
2072 if (kdc_db_ctx == NULL) {
2073 return NT_STATUS_NO_MEMORY;
2075 kdc_db_ctx->ev_ctx = base_ctx->ev_ctx;
2076 kdc_db_ctx->lp_ctx = base_ctx->lp_ctx;
2078 /* get default kdc policy */
2079 lpcfg_default_kdc_policy(base_ctx->lp_ctx,
2080 &kdc_db_ctx->policy.svc_tkt_lifetime,
2081 &kdc_db_ctx->policy.usr_tkt_lifetime,
2082 &kdc_db_ctx->policy.renewal_lifetime);
2084 session_info = system_session(kdc_db_ctx->lp_ctx);
2085 if (session_info == NULL) {
2086 return NT_STATUS_INTERNAL_ERROR;
2089 /* Setup the link to LDB */
2090 kdc_db_ctx->samdb = samdb_connect(kdc_db_ctx, base_ctx->ev_ctx,
2091 base_ctx->lp_ctx, session_info, 0);
2092 if (kdc_db_ctx->samdb == NULL) {
2093 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot open samdb for KDC backend!"));
2094 talloc_free(kdc_db_ctx);
2095 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2098 /* Find out our own krbtgt kvno */
2099 ldb_ret = samdb_rodc(kdc_db_ctx->samdb, &kdc_db_ctx->rodc);
2100 if (ldb_ret != LDB_SUCCESS) {
2101 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine if we are an RODC in KDC backend: %s\n",
2102 ldb_errstring(kdc_db_ctx->samdb)));
2103 talloc_free(kdc_db_ctx);
2104 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2106 if (kdc_db_ctx->rodc) {
2107 int my_krbtgt_number;
2108 const char *secondary_keytab[] = { "msDS-SecondaryKrbTgtNumber", NULL };
2109 struct ldb_dn *account_dn;
2110 struct ldb_dn *server_dn = samdb_server_dn(kdc_db_ctx->samdb, kdc_db_ctx);
2112 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine server DN in KDC backend: %s\n",
2113 ldb_errstring(kdc_db_ctx->samdb)));
2114 talloc_free(kdc_db_ctx);
2115 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2118 ldb_ret = samdb_reference_dn(kdc_db_ctx->samdb, kdc_db_ctx, server_dn,
2119 "serverReference", &account_dn);
2120 if (ldb_ret != LDB_SUCCESS) {
2121 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine server account in KDC backend: %s\n",
2122 ldb_errstring(kdc_db_ctx->samdb)));
2123 talloc_free(kdc_db_ctx);
2124 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2127 ldb_ret = samdb_reference_dn(kdc_db_ctx->samdb, kdc_db_ctx, account_dn,
2128 "msDS-KrbTgtLink", &kdc_db_ctx->krbtgt_dn);
2129 talloc_free(account_dn);
2130 if (ldb_ret != LDB_SUCCESS) {
2131 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine RODC krbtgt account in KDC backend: %s\n",
2132 ldb_errstring(kdc_db_ctx->samdb)));
2133 talloc_free(kdc_db_ctx);
2134 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2137 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb, kdc_db_ctx,
2138 &msg, kdc_db_ctx->krbtgt_dn, LDB_SCOPE_BASE,
2140 DSDB_SEARCH_NO_GLOBAL_CATALOG,
2141 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=*))");
2142 if (ldb_ret != LDB_SUCCESS) {
2143 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot read krbtgt account %s in KDC backend to get msDS-SecondaryKrbTgtNumber: %s: %s\n",
2144 ldb_dn_get_linearized(kdc_db_ctx->krbtgt_dn),
2145 ldb_errstring(kdc_db_ctx->samdb),
2146 ldb_strerror(ldb_ret)));
2147 talloc_free(kdc_db_ctx);
2148 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2150 my_krbtgt_number = ldb_msg_find_attr_as_int(msg, "msDS-SecondaryKrbTgtNumber", -1);
2151 if (my_krbtgt_number == -1) {
2152 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot read msDS-SecondaryKrbTgtNumber from krbtgt account %s in KDC backend: got %d\n",
2153 ldb_dn_get_linearized(kdc_db_ctx->krbtgt_dn),
2155 talloc_free(kdc_db_ctx);
2156 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2158 kdc_db_ctx->my_krbtgt_number = my_krbtgt_number;
2161 kdc_db_ctx->my_krbtgt_number = 0;
2162 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb, kdc_db_ctx,
2164 ldb_get_default_basedn(kdc_db_ctx->samdb),
2167 DSDB_SEARCH_NO_GLOBAL_CATALOG,
2168 "(&(objectClass=user)(samAccountName=krbtgt))");
2170 if (ldb_ret != LDB_SUCCESS) {
2171 DEBUG(1, ("samba_kdc_fetch: could not find own KRBTGT in DB: %s\n", ldb_errstring(kdc_db_ctx->samdb)));
2172 talloc_free(kdc_db_ctx);
2173 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2175 kdc_db_ctx->krbtgt_dn = talloc_steal(kdc_db_ctx, msg->dn);
2176 kdc_db_ctx->my_krbtgt_number = 0;
2179 *kdc_db_ctx_out = kdc_db_ctx;
2180 return NT_STATUS_OK;