2 Unix SMB/CIFS implementation.
4 Database Glue between Samba and the KDC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2009
7 Copyright (C) Simo Sorce <idra@samba.org> 2010
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "system/time.h"
26 #include "../libds/common/flags.h"
27 #include "lib/ldb/include/ldb.h"
28 #include "librpc/gen_ndr/netlogon.h"
29 #include "libcli/security/security.h"
30 #include "auth/auth.h"
31 #include "auth/credentials/credentials.h"
32 #include "auth/auth_sam.h"
33 #include "../lib/util/util_ldb.h"
34 #include "dsdb/samdb/samdb.h"
35 #include "dsdb/common/util.h"
36 #include "librpc/ndr/libndr.h"
37 #include "librpc/gen_ndr/ndr_drsblobs.h"
38 #include "librpc/gen_ndr/lsa.h"
39 #include "libcli/auth/libcli_auth.h"
40 #include "param/param.h"
41 #include "../lib/crypto/md4.h"
42 #include "system/kerberos.h"
44 #include "kdc/samba_kdc.h"
45 #include "kdc/db-glue.h"
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",
69 static KerberosTime
ldb_msg_find_krb5time_ldap_time(struct ldb_message
*msg
, const char *attr
, KerberosTime default_val
)
75 gentime
= ldb_msg_find_attr_as_string(msg
, attr
, NULL
);
79 tmp
= strptime(gentime
, "%Y%m%d%H%M%SZ", &tm
);
87 static HDBFlags
uf2HDBFlags(krb5_context context
, int userAccountControl
, enum samba_kdc_ent_type ent_type
)
89 HDBFlags flags
= int2HDBFlags(0);
91 /* we don't allow kadmin deletes */
94 /* mark the principal as invalid to start with */
99 /* All accounts are servers, but this may be disabled again in the caller */
102 /* Account types - clear the invalid bit if it turns out to be valid */
103 if (userAccountControl
& UF_NORMAL_ACCOUNT
) {
104 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
110 if (userAccountControl
& UF_INTERDOMAIN_TRUST_ACCOUNT
) {
111 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
116 if (userAccountControl
& UF_WORKSTATION_TRUST_ACCOUNT
) {
117 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
122 if (userAccountControl
& UF_SERVER_TRUST_ACCOUNT
) {
123 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
129 /* Not permitted to act as a client if disabled */
130 if (userAccountControl
& UF_ACCOUNTDISABLE
) {
133 if (userAccountControl
& UF_LOCKOUT
) {
137 if (userAccountControl & UF_PASSWORD_NOTREQD) {
142 UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
144 if (userAccountControl
& UF_TEMP_DUPLICATE_ACCOUNT
) {
148 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in samba_kdc_message2entry() */
151 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
155 if (userAccountControl
& UF_SMARTCARD_REQUIRED
) {
156 flags
.require_hwauth
= 1;
158 if (userAccountControl
& UF_TRUSTED_FOR_DELEGATION
) {
159 flags
.ok_as_delegate
= 1;
161 if (!(userAccountControl
& UF_NOT_DELEGATED
)) {
162 flags
.forwardable
= 1;
166 if (userAccountControl
& UF_DONT_REQUIRE_PREAUTH
) {
167 flags
.require_preauth
= 0;
169 flags
.require_preauth
= 1;
175 static int samba_kdc_entry_destructor(struct samba_kdc_entry
*p
)
177 hdb_entry_ex
*entry_ex
= p
->entry_ex
;
178 free_hdb_entry(&entry_ex
->entry
);
182 static void samba_kdc_free_entry(krb5_context context
, hdb_entry_ex
*entry_ex
)
184 /* this function is called only from hdb_free_entry().
185 * Make sure we neutralize the destructor or we will
186 * get a double free later when hdb_free_entry() will
187 * try to call free_hdb_entry() */
188 talloc_set_destructor(entry_ex
->ctx
, NULL
);
190 /* now proceed to free the talloc part */
191 talloc_free(entry_ex
->ctx
);
194 static krb5_error_code
samba_kdc_message2entry_keys(krb5_context context
,
195 struct smb_iconv_convenience
*iconv_convenience
,
197 struct ldb_message
*msg
,
198 unsigned int userAccountControl
,
199 hdb_entry_ex
*entry_ex
)
201 krb5_error_code ret
= 0;
202 enum ndr_err_code ndr_err
;
203 struct samr_Password
*hash
;
204 const struct ldb_val
*sc_val
;
205 struct supplementalCredentialsBlob scb
;
206 struct supplementalCredentialsPackage
*scpk
= NULL
;
207 bool newer_keys
= false;
208 struct package_PrimaryKerberosBlob _pkb
;
209 struct package_PrimaryKerberosCtr3
*pkb3
= NULL
;
210 struct package_PrimaryKerberosCtr4
*pkb4
= NULL
;
212 uint32_t allocated_keys
= 0;
214 entry_ex
->entry
.keys
.val
= NULL
;
215 entry_ex
->entry
.keys
.len
= 0;
217 entry_ex
->entry
.kvno
= ldb_msg_find_attr_as_int(msg
, "msDS-KeyVersionNumber", 0);
219 /* Get keys from the db */
221 hash
= samdb_result_hash(mem_ctx
, msg
, "unicodePwd");
222 sc_val
= ldb_msg_find_ldb_val(msg
, "supplementalCredentials");
224 /* unicodePwd for enctype 0x17 (23) if present */
229 /* supplementalCredentials if present */
231 ndr_err
= ndr_pull_struct_blob_all(sc_val
, mem_ctx
, iconv_convenience
, &scb
,
232 (ndr_pull_flags_fn_t
)ndr_pull_supplementalCredentialsBlob
);
233 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
234 dump_data(0, sc_val
->data
, sc_val
->length
);
239 if (scb
.sub
.signature
!= SUPPLEMENTAL_CREDENTIALS_SIGNATURE
) {
240 NDR_PRINT_DEBUG(supplementalCredentialsBlob
, &scb
);
245 for (i
=0; i
< scb
.sub
.num_packages
; i
++) {
246 if (strcmp("Primary:Kerberos-Newer-Keys", scb
.sub
.packages
[i
].name
) == 0) {
247 scpk
= &scb
.sub
.packages
[i
];
248 if (!scpk
->data
|| !scpk
->data
[0]) {
254 } else if (strcmp("Primary:Kerberos", scb
.sub
.packages
[i
].name
) == 0) {
255 scpk
= &scb
.sub
.packages
[i
];
256 if (!scpk
->data
|| !scpk
->data
[0]) {
260 * we don't break here in hope to find
261 * a Kerberos-Newer-Keys package
267 * Primary:Kerberos-Newer-Keys or Primary:Kerberos element
268 * of supplementalCredentials
273 blob
= strhex_to_data_blob(mem_ctx
, scpk
->data
);
279 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
280 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, iconv_convenience
, &_pkb
,
281 (ndr_pull_flags_fn_t
)ndr_pull_package_PrimaryKerberosBlob
);
282 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
284 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
285 krb5_warnx(context
, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
289 if (newer_keys
&& _pkb
.version
!= 4) {
291 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
292 krb5_warnx(context
, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
296 if (!newer_keys
&& _pkb
.version
!= 3) {
298 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
299 krb5_warnx(context
, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
303 if (_pkb
.version
== 4) {
304 pkb4
= &_pkb
.ctr
.ctr4
;
305 allocated_keys
+= pkb4
->num_keys
;
306 } else if (_pkb
.version
== 3) {
307 pkb3
= &_pkb
.ctr
.ctr3
;
308 allocated_keys
+= pkb3
->num_keys
;
312 if (allocated_keys
== 0) {
313 /* oh, no password. Apparently (comment in
314 * hdb-ldap.c) this violates the ASN.1, but this
315 * allows an entry with no keys (yet). */
319 /* allocate space to decode into */
320 entry_ex
->entry
.keys
.len
= 0;
321 entry_ex
->entry
.keys
.val
= calloc(allocated_keys
, sizeof(Key
));
322 if (entry_ex
->entry
.keys
.val
== NULL
) {
327 if (hash
&& !(userAccountControl
& UF_USE_DES_KEY_ONLY
)) {
331 key
.salt
= NULL
; /* No salt for this enc type */
333 ret
= krb5_keyblock_init(context
,
334 ENCTYPE_ARCFOUR_HMAC
,
335 hash
->hash
, sizeof(hash
->hash
),
341 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
342 entry_ex
->entry
.keys
.len
++;
346 for (i
=0; i
< pkb4
->num_keys
; i
++) {
350 if (!pkb4
->keys
[i
].value
) continue;
352 if (userAccountControl
& UF_USE_DES_KEY_ONLY
) {
353 switch (pkb4
->keys
[i
].keytype
) {
354 case ENCTYPE_DES_CBC_CRC
:
355 case ENCTYPE_DES_CBC_MD5
:
368 if (pkb4
->salt
.string
) {
371 salt
= data_blob_string_const(pkb4
->salt
.string
);
373 key
.salt
= calloc(1, sizeof(*key
.salt
));
374 if (key
.salt
== NULL
) {
379 key
.salt
->type
= hdb_pw_salt
;
381 ret
= krb5_data_copy(&key
.salt
->salt
, salt
.data
, salt
.length
);
389 /* TODO: maybe pass the iteration_count somehow... */
391 ret
= krb5_keyblock_init(context
,
392 pkb4
->keys
[i
].keytype
,
393 pkb4
->keys
[i
].value
->data
,
394 pkb4
->keys
[i
].value
->length
,
396 if (ret
== KRB5_PROG_ETYPE_NOSUPP
) {
397 DEBUG(2,("Unsupported keytype ignored - type %u\n",
398 pkb4
->keys
[i
].keytype
));
411 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
412 entry_ex
->entry
.keys
.len
++;
415 for (i
=0; i
< pkb3
->num_keys
; i
++) {
419 if (!pkb3
->keys
[i
].value
) continue;
421 if (userAccountControl
& UF_USE_DES_KEY_ONLY
) {
422 switch (pkb3
->keys
[i
].keytype
) {
423 case ENCTYPE_DES_CBC_CRC
:
424 case ENCTYPE_DES_CBC_MD5
:
437 if (pkb3
->salt
.string
) {
440 salt
= data_blob_string_const(pkb3
->salt
.string
);
442 key
.salt
= calloc(1, sizeof(*key
.salt
));
443 if (key
.salt
== NULL
) {
448 key
.salt
->type
= hdb_pw_salt
;
450 ret
= krb5_data_copy(&key
.salt
->salt
, salt
.data
, salt
.length
);
458 ret
= krb5_keyblock_init(context
,
459 pkb3
->keys
[i
].keytype
,
460 pkb3
->keys
[i
].value
->data
,
461 pkb3
->keys
[i
].value
->length
,
472 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
473 entry_ex
->entry
.keys
.len
++;
479 entry_ex
->entry
.keys
.len
= 0;
481 if (entry_ex
->entry
.keys
.len
== 0 && entry_ex
->entry
.keys
.val
) {
482 free(entry_ex
->entry
.keys
.val
);
483 entry_ex
->entry
.keys
.val
= NULL
;
489 * Construct an hdb_entry from a directory entry.
491 static krb5_error_code
samba_kdc_message2entry(krb5_context context
,
492 struct samba_kdc_db_context
*kdc_db_ctx
,
493 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
494 enum samba_kdc_ent_type ent_type
,
495 struct ldb_dn
*realm_dn
,
496 struct ldb_message
*msg
,
497 hdb_entry_ex
*entry_ex
)
499 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
500 unsigned int userAccountControl
;
502 krb5_error_code ret
= 0;
503 krb5_boolean is_computer
= FALSE
;
504 char *realm
= strupper_talloc(mem_ctx
, lp_realm(lp_ctx
));
506 struct samba_kdc_entry
*p
;
511 struct ldb_message_element
*objectclasses
;
512 struct ldb_val computer_val
;
513 const char *samAccountName
= ldb_msg_find_attr_as_string(msg
, "samAccountName", NULL
);
514 computer_val
.data
= discard_const_p(uint8_t,"computer");
515 computer_val
.length
= strlen((const char *)computer_val
.data
);
517 if (!samAccountName
) {
519 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: no samAccountName present");
523 objectclasses
= ldb_msg_find_element(msg
, "objectClass");
525 if (objectclasses
&& ldb_msg_find_val(objectclasses
, &computer_val
)) {
529 memset(entry_ex
, 0, sizeof(*entry_ex
));
533 krb5_set_error_message(context
, ret
, "talloc_strdup: out of memory");
537 p
= talloc(mem_ctx
, struct samba_kdc_entry
);
543 p
->kdc_db_ctx
= kdc_db_ctx
;
544 p
->entry_ex
= entry_ex
;
545 p
->realm_dn
= talloc_reference(p
, realm_dn
);
551 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
553 /* make sure we do not have bogus data in there */
554 memset(&entry_ex
->entry
, 0, sizeof(hdb_entry
));
557 entry_ex
->free_entry
= samba_kdc_free_entry
;
559 userAccountControl
= ldb_msg_find_attr_as_uint(msg
, "userAccountControl", 0);
562 entry_ex
->entry
.principal
= malloc(sizeof(*(entry_ex
->entry
.principal
)));
563 if (ent_type
== SAMBA_KDC_ENT_TYPE_ANY
&& principal
== NULL
) {
564 krb5_make_principal(context
, &entry_ex
->entry
.principal
, realm
, samAccountName
, NULL
);
566 ret
= copy_Principal(principal
, entry_ex
->entry
.principal
);
568 krb5_clear_error_message(context
);
572 /* While we have copied the client principal, tests
573 * show that Win2k3 returns the 'corrected' realm, not
574 * the client-specified realm. This code attempts to
575 * replace the client principal's realm with the one
576 * we determine from our records */
578 /* this has to be with malloc() */
579 krb5_principal_set_realm(context
, entry_ex
->entry
.principal
, realm
);
582 /* First try and figure out the flags based on the userAccountControl */
583 entry_ex
->entry
.flags
= uf2HDBFlags(context
, userAccountControl
, ent_type
);
585 /* Windows 2008 seems to enforce this (very sensible) rule by
586 * default - don't allow offline attacks on a user's password
587 * by asking for a ticket to them as a service (encrypted with
588 * their probably patheticly insecure password) */
590 if (entry_ex
->entry
.flags
.server
591 && lp_parm_bool(lp_ctx
, NULL
, "kdc", "require spn for service", true)) {
592 if (!is_computer
&& !ldb_msg_find_attr_as_string(msg
, "servicePrincipalName", NULL
)) {
593 entry_ex
->entry
.flags
.server
= 0;
598 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
599 * of the Heimdal KDC. They are stored in a the traditional
600 * DB for audit purposes, and still form part of the structure
603 /* use 'whenCreated' */
604 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
605 /* use 'kadmin' for now (needed by mit_samba) */
606 krb5_make_principal(context
,
607 &entry_ex
->entry
.created_by
.principal
,
608 realm
, "kadmin", NULL
);
610 entry_ex
->entry
.modified_by
= (Event
*) malloc(sizeof(Event
));
611 if (entry_ex
->entry
.modified_by
== NULL
) {
613 krb5_set_error_message(context
, ret
, "malloc: out of memory");
617 /* use 'whenChanged' */
618 entry_ex
->entry
.modified_by
->time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenChanged", 0);
619 /* use 'kadmin' for now (needed by mit_samba) */
620 krb5_make_principal(context
,
621 &entry_ex
->entry
.modified_by
->principal
,
622 realm
, "kadmin", NULL
);
626 /* The lack of password controls etc applies to krbtgt by
627 * virtue of being that particular RID */
628 status
= dom_sid_split_rid(NULL
, samdb_result_dom_sid(mem_ctx
, msg
, "objectSid"), NULL
, &rid
);
630 if (!NT_STATUS_IS_OK(status
)) {
635 if (rid
== DOMAIN_RID_KRBTGT
) {
636 entry_ex
->entry
.valid_end
= NULL
;
637 entry_ex
->entry
.pw_end
= NULL
;
639 entry_ex
->entry
.flags
.invalid
= 0;
640 entry_ex
->entry
.flags
.server
= 1;
642 /* Don't mark all requests for the krbtgt/realm as
643 * 'change password', as otherwise we could get into
644 * trouble, and not enforce the password expirty.
645 * Instead, only do it when request is for the kpasswd service */
646 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
647 && principal
->name
.name_string
.len
== 2
648 && (strcmp(principal
->name
.name_string
.val
[0], "kadmin") == 0)
649 && (strcmp(principal
->name
.name_string
.val
[1], "changepw") == 0)
650 && lp_is_my_domain_or_realm(lp_ctx
, principal
->realm
)) {
651 entry_ex
->entry
.flags
.change_pw
= 1;
653 entry_ex
->entry
.flags
.client
= 0;
654 entry_ex
->entry
.flags
.forwardable
= 1;
655 entry_ex
->entry
.flags
.ok_as_delegate
= 1;
656 } else if (entry_ex
->entry
.flags
.server
&& ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
657 /* The account/password expiry only applies when the account is used as a
658 * client (ie password login), not when used as a server */
660 /* Make very well sure we don't use this for a client,
661 * it could bypass the password restrictions */
662 entry_ex
->entry
.flags
.client
= 0;
664 entry_ex
->entry
.valid_end
= NULL
;
665 entry_ex
->entry
.pw_end
= NULL
;
668 NTTIME must_change_time
669 = samdb_result_force_password_change(kdc_db_ctx
->samdb
, mem_ctx
,
671 if (must_change_time
== 0x7FFFFFFFFFFFFFFFULL
) {
672 entry_ex
->entry
.pw_end
= NULL
;
674 entry_ex
->entry
.pw_end
= malloc(sizeof(*entry_ex
->entry
.pw_end
));
675 if (entry_ex
->entry
.pw_end
== NULL
) {
679 *entry_ex
->entry
.pw_end
= nt_time_to_unix(must_change_time
);
682 acct_expiry
= samdb_result_account_expires(msg
);
683 if (acct_expiry
== 0x7FFFFFFFFFFFFFFFULL
) {
684 entry_ex
->entry
.valid_end
= NULL
;
686 entry_ex
->entry
.valid_end
= malloc(sizeof(*entry_ex
->entry
.valid_end
));
687 if (entry_ex
->entry
.valid_end
== NULL
) {
691 *entry_ex
->entry
.valid_end
= nt_time_to_unix(acct_expiry
);
695 entry_ex
->entry
.valid_start
= NULL
;
697 entry_ex
->entry
.max_life
= NULL
;
699 entry_ex
->entry
.max_renew
= NULL
;
701 entry_ex
->entry
.generation
= NULL
;
703 /* Get keys from the db */
704 ret
= samba_kdc_message2entry_keys(context
, p
->kdc_db_ctx
->ic_ctx
, p
,
705 msg
, userAccountControl
, entry_ex
);
707 /* Could be bougus data in the entry, or out of memory */
711 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
712 if (entry_ex
->entry
.etypes
== NULL
) {
713 krb5_clear_error_message(context
);
717 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
718 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
719 if (entry_ex
->entry
.etypes
->val
== NULL
) {
720 krb5_clear_error_message(context
);
724 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
725 entry_ex
->entry
.etypes
->val
[i
] = entry_ex
->entry
.keys
.val
[i
].key
.keytype
;
729 p
->msg
= talloc_steal(p
, msg
);
733 /* This doesn't free ent itself, that is for the eventual caller to do */
734 hdb_free_entry(context
, entry_ex
);
736 talloc_steal(kdc_db_ctx
, entry_ex
->ctx
);
743 * Construct an hdb_entry from a directory entry.
745 static krb5_error_code
samba_kdc_trust_message2entry(krb5_context context
,
746 struct samba_kdc_db_context
*kdc_db_ctx
,
747 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
748 enum trust_direction direction
,
749 struct ldb_dn
*realm_dn
,
750 struct ldb_message
*msg
,
751 hdb_entry_ex
*entry_ex
)
753 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
754 const char *dnsdomain
;
755 char *realm
= strupper_talloc(mem_ctx
, lp_realm(lp_ctx
));
756 DATA_BLOB password_utf16
;
757 struct samr_Password password_hash
;
758 const struct ldb_val
*password_val
;
759 struct trustAuthInOutBlob password_blob
;
760 struct samba_kdc_entry
*p
;
762 enum ndr_err_code ndr_err
;
763 int i
, ret
, trust_direction_flags
;
765 p
= talloc(mem_ctx
, struct samba_kdc_entry
);
771 p
->kdc_db_ctx
= kdc_db_ctx
;
772 p
->entry_ex
= entry_ex
;
773 p
->realm_dn
= realm_dn
;
775 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
777 /* make sure we do not have bogus data in there */
778 memset(&entry_ex
->entry
, 0, sizeof(hdb_entry
));
781 entry_ex
->free_entry
= samba_kdc_free_entry
;
783 /* use 'whenCreated' */
784 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
785 /* use 'kadmin' for now (needed by mit_samba) */
786 krb5_make_principal(context
,
787 &entry_ex
->entry
.created_by
.principal
,
788 realm
, "kadmin", NULL
);
790 entry_ex
->entry
.valid_start
= NULL
;
792 trust_direction_flags
= ldb_msg_find_attr_as_int(msg
, "trustDirection", 0);
794 if (direction
== INBOUND
) {
795 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthIncoming");
797 } else { /* OUTBOUND */
798 dnsdomain
= ldb_msg_find_attr_as_string(msg
, "trustPartner", NULL
);
801 realm
= strupper_talloc(mem_ctx
, dnsdomain
);
802 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthOutgoing");
805 if (!password_val
|| !(trust_direction_flags
& direction
)) {
810 ndr_err
= ndr_pull_struct_blob(password_val
, mem_ctx
, p
->kdc_db_ctx
->ic_ctx
, &password_blob
,
811 (ndr_pull_flags_fn_t
)ndr_pull_trustAuthInOutBlob
);
812 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
817 entry_ex
->entry
.kvno
= -1;
818 for (i
=0; i
< password_blob
.count
; i
++) {
819 if (password_blob
.current
->array
[i
].AuthType
== TRUST_AUTH_TYPE_VERSION
) {
820 entry_ex
->entry
.kvno
= password_blob
.current
->array
[i
].AuthInfo
.version
.version
;
824 for (i
=0; i
< password_blob
.count
; i
++) {
825 if (password_blob
.current
->array
[i
].AuthType
== TRUST_AUTH_TYPE_CLEAR
) {
826 password_utf16
= data_blob_const(password_blob
.current
->array
[i
].AuthInfo
.clear
.password
,
827 password_blob
.current
->array
[i
].AuthInfo
.clear
.size
);
828 /* In the future, generate all sorts of
829 * hashes, but for now we can't safely convert
830 * the random strings windows uses into
833 /* but as it is utf16 already, we can get the NT password/arcfour-hmac-md5 key */
834 mdfour(password_hash
.hash
, password_utf16
.data
, password_utf16
.length
);
836 } else if (password_blob
.current
->array
[i
].AuthType
== TRUST_AUTH_TYPE_NT4OWF
) {
837 password_hash
= password_blob
.current
->array
[i
].AuthInfo
.nt4owf
.password
;
842 if (i
< password_blob
.count
) {
844 /* Must have found a cleartext or MD4 password */
845 entry_ex
->entry
.keys
.val
= calloc(1, sizeof(Key
));
848 key
.salt
= NULL
; /* No salt for this enc type */
850 if (entry_ex
->entry
.keys
.val
== NULL
) {
855 ret
= krb5_keyblock_init(context
,
856 ENCTYPE_ARCFOUR_HMAC
,
857 password_hash
.hash
, sizeof(password_hash
.hash
),
860 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
861 entry_ex
->entry
.keys
.len
++;
864 entry_ex
->entry
.principal
= malloc(sizeof(*(entry_ex
->entry
.principal
)));
866 ret
= copy_Principal(principal
, entry_ex
->entry
.principal
);
868 krb5_clear_error_message(context
);
872 /* While we have copied the client principal, tests
873 * show that Win2k3 returns the 'corrected' realm, not
874 * the client-specified realm. This code attempts to
875 * replace the client principal's realm with the one
876 * we determine from our records */
878 krb5_principal_set_realm(context
, entry_ex
->entry
.principal
, realm
);
879 entry_ex
->entry
.flags
= int2HDBFlags(0);
880 entry_ex
->entry
.flags
.immutable
= 1;
881 entry_ex
->entry
.flags
.invalid
= 0;
882 entry_ex
->entry
.flags
.server
= 1;
883 entry_ex
->entry
.flags
.require_preauth
= 1;
885 entry_ex
->entry
.pw_end
= NULL
;
887 entry_ex
->entry
.max_life
= NULL
;
889 entry_ex
->entry
.max_renew
= NULL
;
891 entry_ex
->entry
.generation
= NULL
;
893 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
894 if (entry_ex
->entry
.etypes
== NULL
) {
895 krb5_clear_error_message(context
);
899 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
900 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
901 if (entry_ex
->entry
.etypes
->val
== NULL
) {
902 krb5_clear_error_message(context
);
906 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
907 entry_ex
->entry
.etypes
->val
[i
] = entry_ex
->entry
.keys
.val
[i
].key
.keytype
;
911 p
->msg
= talloc_steal(p
, msg
);
915 /* This doesn't free ent itself, that is for the eventual caller to do */
916 hdb_free_entry(context
, entry_ex
);
918 talloc_steal(kdc_db_ctx
, entry_ex
->ctx
);
925 static krb5_error_code
samba_kdc_lookup_trust(krb5_context context
, struct ldb_context
*ldb_ctx
,
928 struct ldb_dn
*realm_dn
,
929 struct ldb_message
**pmsg
)
934 const char * const *attrs
= trust_attrs
;
936 struct ldb_result
*res
= NULL
;
937 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=trustedDomain)(|(flatname=%s)(trustPartner=%s)))", realm
, realm
);
941 krb5_set_error_message(context
, ret
, "talloc_asprintf: out of memory");
945 lret
= ldb_search(ldb_ctx
, mem_ctx
, &res
,
946 ldb_get_default_basedn(ldb_ctx
),
947 LDB_SCOPE_SUBTREE
, attrs
, "%s", filter
);
948 if (lret
!= LDB_SUCCESS
) {
949 DEBUG(3, ("Failed to search for %s: %s\n", filter
, ldb_errstring(ldb_ctx
)));
950 return HDB_ERR_NOENTRY
;
951 } else if (res
->count
== 0 || res
->count
> 1) {
952 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter
, res
->count
));
954 return HDB_ERR_NOENTRY
;
956 talloc_steal(mem_ctx
, res
->msgs
);
957 *pmsg
= res
->msgs
[0];
962 static krb5_error_code
samba_kdc_lookup_client(krb5_context context
,
963 struct samba_kdc_db_context
*kdc_db_ctx
,
965 krb5_const_principal principal
,
967 struct ldb_dn
**realm_dn
,
968 struct ldb_message
**msg
) {
970 char *principal_string
;
973 ret
= krb5_unparse_name(context
, principal
, &principal_string
);
979 nt_status
= sam_get_results_principal(kdc_db_ctx
->samdb
,
980 mem_ctx
, principal_string
, attrs
,
982 free(principal_string
);
983 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
984 return HDB_ERR_NOENTRY
;
985 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_MEMORY
)) {
987 } else if (!NT_STATUS_IS_OK(nt_status
)) {
994 static krb5_error_code
samba_kdc_fetch_client(krb5_context context
,
995 struct samba_kdc_db_context
*kdc_db_ctx
,
997 krb5_const_principal principal
,
998 hdb_entry_ex
*entry_ex
) {
999 struct ldb_dn
*realm_dn
;
1000 krb5_error_code ret
;
1001 struct ldb_message
*msg
= NULL
;
1003 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
1004 mem_ctx
, principal
, user_attrs
,
1010 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1011 principal
, SAMBA_KDC_ENT_TYPE_CLIENT
,
1012 realm_dn
, msg
, entry_ex
);
1016 static krb5_error_code
samba_kdc_fetch_krbtgt(krb5_context context
,
1017 struct samba_kdc_db_context
*kdc_db_ctx
,
1018 TALLOC_CTX
*mem_ctx
,
1019 krb5_const_principal principal
,
1020 hdb_entry_ex
*entry_ex
)
1022 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
1023 krb5_error_code ret
;
1024 struct ldb_message
*msg
= NULL
;
1025 struct ldb_dn
*realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
1028 krb5_principal alloc_principal
= NULL
;
1029 if (principal
->name
.name_string
.len
!= 2
1030 || (strcmp(principal
->name
.name_string
.val
[0], KRB5_TGS_NAME
) != 0)) {
1032 return HDB_ERR_NOENTRY
;
1035 /* krbtgt case. Either us or a trusted realm */
1037 if (lp_is_my_domain_or_realm(lp_ctx
, principal
->realm
)
1038 && lp_is_my_domain_or_realm(lp_ctx
, principal
->name
.name_string
.val
[1])) {
1040 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1041 * is in our db, then direct the caller at our primary
1047 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
,
1048 &msg
, realm_dn
, LDB_SCOPE_SUBTREE
,
1050 DSDB_SEARCH_SHOW_EXTENDED_DN
,
1051 "(&(objectClass=user)(samAccountName=krbtgt))");
1052 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
1053 krb5_warnx(context
, "samba_kdc_fetch: could not find own KRBTGT in DB!");
1054 krb5_set_error_message(context
, HDB_ERR_NOENTRY
, "samba_kdc_fetch: could not find own KRBTGT in DB!");
1055 return HDB_ERR_NOENTRY
;
1056 } else if (lret
!= LDB_SUCCESS
) {
1057 krb5_warnx(context
, "samba_kdc_fetch: could not find own KRBTGT in DB: %s", ldb_errstring(kdc_db_ctx
->samdb
));
1058 krb5_set_error_message(context
, HDB_ERR_NOENTRY
, "samba_kdc_fetch: could not find own KRBTGT in DB: %s", ldb_errstring(kdc_db_ctx
->samdb
));
1059 return HDB_ERR_NOENTRY
;
1062 realm_fixed
= strupper_talloc(mem_ctx
, lp_realm(lp_ctx
));
1065 krb5_set_error_message(context
, ret
, "strupper_talloc: out of memory");
1069 ret
= krb5_copy_principal(context
, principal
, &alloc_principal
);
1074 free(alloc_principal
->name
.name_string
.val
[1]);
1075 alloc_principal
->name
.name_string
.val
[1] = strdup(realm_fixed
);
1076 talloc_free(realm_fixed
);
1077 if (!alloc_principal
->name
.name_string
.val
[1]) {
1079 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: strdup() failed!");
1082 principal
= alloc_principal
;
1084 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1085 principal
, SAMBA_KDC_ENT_TYPE_KRBTGT
,
1086 realm_dn
, msg
, entry_ex
);
1088 krb5_warnx(context
, "samba_kdc_fetch: self krbtgt message2entry failed");
1093 enum trust_direction direction
= UNKNOWN
;
1095 /* Either an inbound or outbound trust */
1097 if (strcasecmp(lp_realm(lp_ctx
), principal
->realm
) == 0) {
1098 /* look for inbound trust */
1099 direction
= INBOUND
;
1100 realm
= principal
->name
.name_string
.val
[1];
1103 if (strcasecmp(lp_realm(lp_ctx
), principal
->name
.name_string
.val
[1]) == 0) {
1104 /* look for outbound trust */
1105 direction
= OUTBOUND
;
1106 realm
= principal
->realm
;
1109 /* Trusted domains are under CN=system */
1111 ret
= samba_kdc_lookup_trust(context
, kdc_db_ctx
->samdb
,
1113 realm
, realm_dn
, &msg
);
1116 krb5_warnx(context
, "samba_kdc_fetch: could not find principal in DB");
1117 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: could not find principal in DB");
1121 ret
= samba_kdc_trust_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1122 principal
, direction
,
1123 realm_dn
, msg
, entry_ex
);
1125 krb5_warnx(context
, "samba_kdc_fetch: trust_message2entry failed");
1130 /* we should lookup trusted domains */
1131 return HDB_ERR_NOENTRY
;
1136 static krb5_error_code
samba_kdc_lookup_server(krb5_context context
,
1137 struct samba_kdc_db_context
*kdc_db_ctx
,
1138 TALLOC_CTX
*mem_ctx
,
1139 krb5_const_principal principal
,
1141 struct ldb_dn
**realm_dn
,
1142 struct ldb_message
**msg
)
1144 krb5_error_code ret
;
1146 if (principal
->name
.name_string
.len
>= 2) {
1147 /* 'normal server' case */
1150 struct ldb_dn
*user_dn
;
1151 char *principal_string
;
1153 ret
= krb5_unparse_name_flags(context
, principal
,
1154 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
1160 /* At this point we may find the host is known to be
1161 * in a different realm, so we should generate a
1162 * referral instead */
1163 nt_status
= crack_service_principal_name(kdc_db_ctx
->samdb
,
1164 mem_ctx
, principal_string
,
1165 &user_dn
, realm_dn
);
1166 free(principal_string
);
1168 if (!NT_STATUS_IS_OK(nt_status
)) {
1169 return HDB_ERR_NOENTRY
;
1172 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
,
1174 msg
, user_dn
, LDB_SCOPE_BASE
,
1175 attrs
, DSDB_SEARCH_SHOW_EXTENDED_DN
, "(objectClass=*)");
1176 if (ldb_ret
!= LDB_SUCCESS
) {
1177 return HDB_ERR_NOENTRY
;
1182 char *filter
= NULL
;
1184 /* server as client principal case, but we must not lookup userPrincipalNames */
1185 *realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
1186 realm
= krb5_principal_get_realm(context
, principal
);
1188 /* TODO: Check if it is our realm, otherwise give referall */
1190 ret
= krb5_unparse_name_flags(context
, principal
, KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &short_princ
);
1193 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_principal: could not parse principal");
1194 krb5_warnx(context
, "samba_kdc_lookup_principal: could not parse principal");
1198 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
, msg
,
1199 *realm_dn
, LDB_SCOPE_SUBTREE
,
1201 DSDB_SEARCH_SHOW_EXTENDED_DN
,
1202 "(&(objectClass=user)(samAccountName=%s))",
1203 ldb_binary_encode_string(mem_ctx
, short_princ
));
1205 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
1206 DEBUG(3, ("Failed find a entry for %s\n", filter
));
1207 return HDB_ERR_NOENTRY
;
1209 if (lret
!= LDB_SUCCESS
) {
1210 DEBUG(3, ("Failed single search for for %s - %s\n",
1211 filter
, ldb_errstring(kdc_db_ctx
->samdb
)));
1212 return HDB_ERR_NOENTRY
;
1219 static krb5_error_code
samba_kdc_fetch_server(krb5_context context
,
1220 struct samba_kdc_db_context
*kdc_db_ctx
,
1221 TALLOC_CTX
*mem_ctx
,
1222 krb5_const_principal principal
,
1223 hdb_entry_ex
*entry_ex
)
1225 krb5_error_code ret
;
1226 struct ldb_dn
*realm_dn
;
1227 struct ldb_message
*msg
;
1229 ret
= samba_kdc_lookup_server(context
, kdc_db_ctx
, mem_ctx
, principal
,
1230 server_attrs
, &realm_dn
, &msg
);
1235 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1236 principal
, SAMBA_KDC_ENT_TYPE_SERVER
,
1237 realm_dn
, msg
, entry_ex
);
1239 krb5_warnx(context
, "samba_kdc_fetch: message2entry failed");
1245 krb5_error_code
samba_kdc_fetch(krb5_context context
,
1246 struct samba_kdc_db_context
*kdc_db_ctx
,
1247 krb5_const_principal principal
,
1249 hdb_entry_ex
*entry_ex
)
1251 krb5_error_code ret
= HDB_ERR_NOENTRY
;
1252 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_fetch context");
1256 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
1260 if (flags
& HDB_F_GET_CLIENT
) {
1261 ret
= samba_kdc_fetch_client(context
, kdc_db_ctx
, mem_ctx
, principal
, entry_ex
);
1262 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1264 if (flags
& HDB_F_GET_SERVER
) {
1265 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
1266 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, entry_ex
);
1267 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1269 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
1270 ret
= samba_kdc_fetch_server(context
, kdc_db_ctx
, mem_ctx
, principal
, entry_ex
);
1271 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1273 if (flags
& HDB_F_GET_KRBTGT
) {
1274 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, entry_ex
);
1275 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1279 talloc_free(mem_ctx
);
1283 struct samba_kdc_seq
{
1286 struct ldb_message
**msgs
;
1287 struct ldb_dn
*realm_dn
;
1290 static krb5_error_code
samba_kdc_seq(krb5_context context
,
1291 struct samba_kdc_db_context
*kdc_db_ctx
,
1292 hdb_entry_ex
*entry
)
1294 krb5_error_code ret
;
1295 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
1296 TALLOC_CTX
*mem_ctx
;
1297 hdb_entry_ex entry_ex
;
1298 memset(&entry_ex
, '\0', sizeof(entry_ex
));
1301 return HDB_ERR_NOENTRY
;
1304 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_seq context");
1308 krb5_set_error_message(context
, ret
, "samba_kdc_seq: talloc_named() failed!");
1312 if (priv
->index
< priv
->count
) {
1313 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1314 NULL
, SAMBA_KDC_ENT_TYPE_ANY
,
1315 priv
->realm_dn
, priv
->msgs
[priv
->index
++], entry
);
1317 ret
= HDB_ERR_NOENTRY
;
1322 kdc_db_ctx
->seq_ctx
= NULL
;
1324 talloc_free(mem_ctx
);
1330 krb5_error_code
samba_kdc_firstkey(krb5_context context
,
1331 struct samba_kdc_db_context
*kdc_db_ctx
,
1332 hdb_entry_ex
*entry
)
1334 struct ldb_context
*ldb_ctx
= kdc_db_ctx
->samdb
;
1335 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
1337 struct ldb_result
*res
= NULL
;
1338 krb5_error_code ret
;
1339 TALLOC_CTX
*mem_ctx
;
1344 kdc_db_ctx
->seq_ctx
= NULL
;
1347 priv
= (struct samba_kdc_seq
*) talloc(kdc_db_ctx
, struct samba_kdc_seq
);
1350 krb5_set_error_message(context
, ret
, "talloc: out of memory");
1356 priv
->realm_dn
= ldb_get_default_basedn(ldb_ctx
);
1359 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_firstkey context");
1363 krb5_set_error_message(context
, ret
, "samba_kdc_firstkey: talloc_named() failed!");
1367 ret
= krb5_get_default_realm(context
, &realm
);
1373 lret
= ldb_search(ldb_ctx
, priv
, &res
,
1374 priv
->realm_dn
, LDB_SCOPE_SUBTREE
, user_attrs
,
1375 "(objectClass=user)");
1377 if (lret
!= LDB_SUCCESS
) {
1379 return HDB_ERR_NOENTRY
;
1382 priv
->count
= res
->count
;
1383 priv
->msgs
= talloc_steal(priv
, res
->msgs
);
1386 kdc_db_ctx
->seq_ctx
= priv
;
1388 ret
= samba_kdc_seq(context
, kdc_db_ctx
, entry
);
1392 kdc_db_ctx
->seq_ctx
= NULL
;
1394 talloc_free(mem_ctx
);
1399 krb5_error_code
samba_kdc_nextkey(krb5_context context
,
1400 struct samba_kdc_db_context
*kdc_db_ctx
,
1401 hdb_entry_ex
*entry
)
1403 return samba_kdc_seq(context
, kdc_db_ctx
, entry
);
1406 /* Check if a given entry may delegate to this target principal
1408 * This is currently a very nasty hack - allowing only delegation to itself.
1411 samba_kdc_check_constrained_delegation(krb5_context context
,
1412 struct samba_kdc_db_context
*kdc_db_ctx
,
1413 hdb_entry_ex
*entry
,
1414 krb5_const_principal target_principal
)
1416 krb5_error_code ret
;
1417 krb5_principal enterprise_prinicpal
= NULL
;
1418 struct ldb_dn
*realm_dn
;
1419 struct ldb_message
*msg
;
1420 struct dom_sid
*orig_sid
;
1421 struct dom_sid
*target_sid
;
1422 struct samba_kdc_entry
*p
= talloc_get_type(entry
->ctx
, struct samba_kdc_entry
);
1423 const char *delegation_check_attrs
[] = {
1427 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_constrained_delegation");
1431 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
1435 if (target_principal
->name
.name_type
== KRB5_NT_ENTERPRISE_PRINCIPAL
) {
1436 /* Need to reparse the enterprise principal to find the real target */
1437 if (target_principal
->name
.name_string
.len
!= 1) {
1438 ret
= KRB5_PARSE_MALFORMED
;
1439 krb5_set_error_message(context
, ret
, "samba_kdc_check_constrained_delegation: request for delegation to enterprise principal with wrong (%d) number of components",
1440 target_principal
->name
.name_string
.len
);
1441 talloc_free(mem_ctx
);
1444 ret
= krb5_parse_name(context
, target_principal
->name
.name_string
.val
[0],
1445 &enterprise_prinicpal
);
1447 talloc_free(mem_ctx
);
1450 target_principal
= enterprise_prinicpal
;
1453 ret
= samba_kdc_lookup_server(context
, kdc_db_ctx
, mem_ctx
, target_principal
,
1454 delegation_check_attrs
, &realm_dn
, &msg
);
1456 krb5_free_principal(context
, enterprise_prinicpal
);
1459 talloc_free(mem_ctx
);
1463 orig_sid
= samdb_result_dom_sid(mem_ctx
, p
->msg
, "objectSid");
1464 target_sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
1466 /* Allow delegation to the same principal, even if by a different
1467 * name. The easy and safe way to prove this is by SID
1469 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
1470 talloc_free(mem_ctx
);
1471 return KRB5KDC_ERR_BADOPTION
;
1474 talloc_free(mem_ctx
);
1478 /* Certificates printed by a the Certificate Authority might have a
1479 * slightly different form of the user principal name to that in the
1480 * database. Allow a mismatch where they both refer to the same
1484 samba_kdc_check_pkinit_ms_upn_match(krb5_context context
,
1485 struct samba_kdc_db_context
*kdc_db_ctx
,
1486 hdb_entry_ex
*entry
,
1487 krb5_const_principal certificate_principal
)
1489 krb5_error_code ret
;
1490 struct ldb_dn
*realm_dn
;
1491 struct ldb_message
*msg
;
1492 struct dom_sid
*orig_sid
;
1493 struct dom_sid
*target_sid
;
1494 struct samba_kdc_entry
*p
= talloc_get_type(entry
->ctx
, struct samba_kdc_entry
);
1495 const char *ms_upn_check_attrs
[] = {
1499 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_pkinit_ms_upn_match");
1503 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
1507 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
1508 mem_ctx
, certificate_principal
,
1509 ms_upn_check_attrs
, &realm_dn
, &msg
);
1512 talloc_free(mem_ctx
);
1516 orig_sid
= samdb_result_dom_sid(mem_ctx
, p
->msg
, "objectSid");
1517 target_sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
1519 /* Consider these to be the same principal, even if by a different
1520 * name. The easy and safe way to prove this is by SID
1522 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
1523 talloc_free(mem_ctx
);
1524 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1527 talloc_free(mem_ctx
);