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 "librpc/ndr/libndr.h"
36 #include "librpc/gen_ndr/ndr_drsblobs.h"
37 #include "librpc/gen_ndr/lsa.h"
38 #include "libcli/auth/libcli_auth.h"
39 #include "param/param.h"
40 #include "../lib/crypto/md4.h"
41 #include "system/kerberos.h"
43 #include "kdc/samba_kdc.h"
44 #include "kdc/db-glue.h"
46 enum samba_kdc_ent_type
47 { SAMBA_KDC_ENT_TYPE_CLIENT
, SAMBA_KDC_ENT_TYPE_SERVER
,
48 SAMBA_KDC_ENT_TYPE_KRBTGT
, SAMBA_KDC_ENT_TYPE_TRUST
, SAMBA_KDC_ENT_TYPE_ANY
};
50 enum trust_direction
{
52 INBOUND
= LSA_TRUST_DIRECTION_INBOUND
,
53 OUTBOUND
= LSA_TRUST_DIRECTION_OUTBOUND
56 static const char *trust_attrs
[] = {
61 "msDS-SupportedEncryptionTypes",
68 static KerberosTime
ldb_msg_find_krb5time_ldap_time(struct ldb_message
*msg
, const char *attr
, KerberosTime default_val
)
74 gentime
= ldb_msg_find_attr_as_string(msg
, attr
, NULL
);
78 tmp
= strptime(gentime
, "%Y%m%d%H%M%SZ", &tm
);
86 static HDBFlags
uf2HDBFlags(krb5_context context
, int userAccountControl
, enum samba_kdc_ent_type ent_type
)
88 HDBFlags flags
= int2HDBFlags(0);
90 /* we don't allow kadmin deletes */
93 /* mark the principal as invalid to start with */
98 /* All accounts are servers, but this may be disabled again in the caller */
101 /* Account types - clear the invalid bit if it turns out to be valid */
102 if (userAccountControl
& UF_NORMAL_ACCOUNT
) {
103 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
109 if (userAccountControl
& UF_INTERDOMAIN_TRUST_ACCOUNT
) {
110 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
115 if (userAccountControl
& UF_WORKSTATION_TRUST_ACCOUNT
) {
116 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
121 if (userAccountControl
& UF_SERVER_TRUST_ACCOUNT
) {
122 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
128 /* Not permitted to act as a client if disabled */
129 if (userAccountControl
& UF_ACCOUNTDISABLE
) {
132 if (userAccountControl
& UF_LOCKOUT
) {
136 if (userAccountControl & UF_PASSWORD_NOTREQD) {
141 UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
143 if (userAccountControl
& UF_TEMP_DUPLICATE_ACCOUNT
) {
147 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in samba_kdc_message2entry() */
150 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
154 if (userAccountControl
& UF_SMARTCARD_REQUIRED
) {
155 flags
.require_hwauth
= 1;
157 if (userAccountControl
& UF_TRUSTED_FOR_DELEGATION
) {
158 flags
.ok_as_delegate
= 1;
160 if (!(userAccountControl
& UF_NOT_DELEGATED
)) {
161 flags
.forwardable
= 1;
165 if (userAccountControl
& UF_DONT_REQUIRE_PREAUTH
) {
166 flags
.require_preauth
= 0;
168 flags
.require_preauth
= 1;
174 static int samba_kdc_entry_destructor(struct samba_kdc_entry
*p
)
176 hdb_entry_ex
*entry_ex
= p
->entry_ex
;
177 free_hdb_entry(&entry_ex
->entry
);
181 static void samba_kdc_free_entry(krb5_context context
, hdb_entry_ex
*entry_ex
)
183 talloc_free(entry_ex
->ctx
);
186 static krb5_error_code
samba_kdc_message2entry_keys(krb5_context context
,
187 struct smb_iconv_convenience
*iconv_convenience
,
189 struct ldb_message
*msg
,
190 unsigned int userAccountControl
,
191 hdb_entry_ex
*entry_ex
)
193 krb5_error_code ret
= 0;
194 enum ndr_err_code ndr_err
;
195 struct samr_Password
*hash
;
196 const struct ldb_val
*sc_val
;
197 struct supplementalCredentialsBlob scb
;
198 struct supplementalCredentialsPackage
*scpk
= NULL
;
199 bool newer_keys
= false;
200 struct package_PrimaryKerberosBlob _pkb
;
201 struct package_PrimaryKerberosCtr3
*pkb3
= NULL
;
202 struct package_PrimaryKerberosCtr4
*pkb4
= NULL
;
204 uint32_t allocated_keys
= 0;
206 entry_ex
->entry
.keys
.val
= NULL
;
207 entry_ex
->entry
.keys
.len
= 0;
209 entry_ex
->entry
.kvno
= ldb_msg_find_attr_as_int(msg
, "msDS-KeyVersionNumber", 0);
211 /* Get keys from the db */
213 hash
= samdb_result_hash(mem_ctx
, msg
, "unicodePwd");
214 sc_val
= ldb_msg_find_ldb_val(msg
, "supplementalCredentials");
216 /* unicodePwd for enctype 0x17 (23) if present */
221 /* supplementalCredentials if present */
223 ndr_err
= ndr_pull_struct_blob_all(sc_val
, mem_ctx
, iconv_convenience
, &scb
,
224 (ndr_pull_flags_fn_t
)ndr_pull_supplementalCredentialsBlob
);
225 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
226 dump_data(0, sc_val
->data
, sc_val
->length
);
231 if (scb
.sub
.signature
!= SUPPLEMENTAL_CREDENTIALS_SIGNATURE
) {
232 NDR_PRINT_DEBUG(supplementalCredentialsBlob
, &scb
);
237 for (i
=0; i
< scb
.sub
.num_packages
; i
++) {
238 if (strcmp("Primary:Kerberos-Newer-Keys", scb
.sub
.packages
[i
].name
) == 0) {
239 scpk
= &scb
.sub
.packages
[i
];
240 if (!scpk
->data
|| !scpk
->data
[0]) {
246 } else if (strcmp("Primary:Kerberos", scb
.sub
.packages
[i
].name
) == 0) {
247 scpk
= &scb
.sub
.packages
[i
];
248 if (!scpk
->data
|| !scpk
->data
[0]) {
252 * we don't break here in hope to find
253 * a Kerberos-Newer-Keys package
259 * Primary:Kerberos-Newer-Keys or Primary:Kerberos element
260 * of supplementalCredentials
265 blob
= strhex_to_data_blob(mem_ctx
, scpk
->data
);
271 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
272 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, iconv_convenience
, &_pkb
,
273 (ndr_pull_flags_fn_t
)ndr_pull_package_PrimaryKerberosBlob
);
274 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
276 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
277 krb5_warnx(context
, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
281 if (newer_keys
&& _pkb
.version
!= 4) {
283 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
284 krb5_warnx(context
, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
288 if (!newer_keys
&& _pkb
.version
!= 3) {
290 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
291 krb5_warnx(context
, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
295 if (_pkb
.version
== 4) {
296 pkb4
= &_pkb
.ctr
.ctr4
;
297 allocated_keys
+= pkb4
->num_keys
;
298 } else if (_pkb
.version
== 3) {
299 pkb3
= &_pkb
.ctr
.ctr3
;
300 allocated_keys
+= pkb3
->num_keys
;
304 if (allocated_keys
== 0) {
305 /* oh, no password. Apparently (comment in
306 * hdb-ldap.c) this violates the ASN.1, but this
307 * allows an entry with no keys (yet). */
311 /* allocate space to decode into */
312 entry_ex
->entry
.keys
.len
= 0;
313 entry_ex
->entry
.keys
.val
= calloc(allocated_keys
, sizeof(Key
));
314 if (entry_ex
->entry
.keys
.val
== NULL
) {
319 if (hash
&& !(userAccountControl
& UF_USE_DES_KEY_ONLY
)) {
323 key
.salt
= NULL
; /* No salt for this enc type */
325 ret
= krb5_keyblock_init(context
,
326 ENCTYPE_ARCFOUR_HMAC
,
327 hash
->hash
, sizeof(hash
->hash
),
333 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
334 entry_ex
->entry
.keys
.len
++;
338 for (i
=0; i
< pkb4
->num_keys
; i
++) {
342 if (!pkb4
->keys
[i
].value
) continue;
344 if (userAccountControl
& UF_USE_DES_KEY_ONLY
) {
345 switch (pkb4
->keys
[i
].keytype
) {
346 case ENCTYPE_DES_CBC_CRC
:
347 case ENCTYPE_DES_CBC_MD5
:
360 if (pkb4
->salt
.string
) {
363 salt
= data_blob_string_const(pkb4
->salt
.string
);
365 key
.salt
= calloc(1, sizeof(*key
.salt
));
366 if (key
.salt
== NULL
) {
371 key
.salt
->type
= hdb_pw_salt
;
373 ret
= krb5_data_copy(&key
.salt
->salt
, salt
.data
, salt
.length
);
381 /* TODO: maybe pass the iteration_count somehow... */
383 ret
= krb5_keyblock_init(context
,
384 pkb4
->keys
[i
].keytype
,
385 pkb4
->keys
[i
].value
->data
,
386 pkb4
->keys
[i
].value
->length
,
388 if (ret
== KRB5_PROG_ETYPE_NOSUPP
) {
389 DEBUG(2,("Unsupported keytype ignored - type %u\n",
390 pkb4
->keys
[i
].keytype
));
403 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
404 entry_ex
->entry
.keys
.len
++;
407 for (i
=0; i
< pkb3
->num_keys
; i
++) {
411 if (!pkb3
->keys
[i
].value
) continue;
413 if (userAccountControl
& UF_USE_DES_KEY_ONLY
) {
414 switch (pkb3
->keys
[i
].keytype
) {
415 case ENCTYPE_DES_CBC_CRC
:
416 case ENCTYPE_DES_CBC_MD5
:
429 if (pkb3
->salt
.string
) {
432 salt
= data_blob_string_const(pkb3
->salt
.string
);
434 key
.salt
= calloc(1, sizeof(*key
.salt
));
435 if (key
.salt
== NULL
) {
440 key
.salt
->type
= hdb_pw_salt
;
442 ret
= krb5_data_copy(&key
.salt
->salt
, salt
.data
, salt
.length
);
450 ret
= krb5_keyblock_init(context
,
451 pkb3
->keys
[i
].keytype
,
452 pkb3
->keys
[i
].value
->data
,
453 pkb3
->keys
[i
].value
->length
,
464 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
465 entry_ex
->entry
.keys
.len
++;
471 entry_ex
->entry
.keys
.len
= 0;
473 if (entry_ex
->entry
.keys
.len
== 0 && entry_ex
->entry
.keys
.val
) {
474 free(entry_ex
->entry
.keys
.val
);
475 entry_ex
->entry
.keys
.val
= NULL
;
481 * Construct an hdb_entry from a directory entry.
483 static krb5_error_code
samba_kdc_message2entry(krb5_context context
,
484 struct samba_kdc_db_context
*kdc_db_ctx
,
485 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
486 enum samba_kdc_ent_type ent_type
,
487 struct ldb_dn
*realm_dn
,
488 struct ldb_message
*msg
,
489 hdb_entry_ex
*entry_ex
)
491 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
492 unsigned int userAccountControl
;
494 krb5_error_code ret
= 0;
495 krb5_boolean is_computer
= FALSE
;
496 char *realm
= strupper_talloc(mem_ctx
, lp_realm(lp_ctx
));
498 struct samba_kdc_entry
*p
;
503 struct ldb_message_element
*objectclasses
;
504 struct ldb_val computer_val
;
505 const char *samAccountName
= ldb_msg_find_attr_as_string(msg
, "samAccountName", NULL
);
506 computer_val
.data
= discard_const_p(uint8_t,"computer");
507 computer_val
.length
= strlen((const char *)computer_val
.data
);
509 if (!samAccountName
) {
511 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: no samAccountName present");
515 objectclasses
= ldb_msg_find_element(msg
, "objectClass");
517 if (objectclasses
&& ldb_msg_find_val(objectclasses
, &computer_val
)) {
521 memset(entry_ex
, 0, sizeof(*entry_ex
));
525 krb5_set_error_message(context
, ret
, "talloc_strdup: out of memory");
529 p
= talloc(mem_ctx
, struct samba_kdc_entry
);
535 p
->kdc_db_ctx
= kdc_db_ctx
;
536 p
->entry_ex
= entry_ex
;
537 p
->realm_dn
= talloc_reference(p
, realm_dn
);
543 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
546 entry_ex
->free_entry
= samba_kdc_free_entry
;
548 userAccountControl
= ldb_msg_find_attr_as_uint(msg
, "userAccountControl", 0);
551 entry_ex
->entry
.principal
= malloc(sizeof(*(entry_ex
->entry
.principal
)));
552 if (ent_type
== SAMBA_KDC_ENT_TYPE_ANY
&& principal
== NULL
) {
553 krb5_make_principal(context
, &entry_ex
->entry
.principal
, realm
, samAccountName
, NULL
);
555 ret
= copy_Principal(principal
, entry_ex
->entry
.principal
);
557 krb5_clear_error_message(context
);
561 /* While we have copied the client principal, tests
562 * show that Win2k3 returns the 'corrected' realm, not
563 * the client-specified realm. This code attempts to
564 * replace the client principal's realm with the one
565 * we determine from our records */
567 /* this has to be with malloc() */
568 krb5_principal_set_realm(context
, entry_ex
->entry
.principal
, realm
);
571 /* First try and figure out the flags based on the userAccountControl */
572 entry_ex
->entry
.flags
= uf2HDBFlags(context
, userAccountControl
, ent_type
);
574 /* Windows 2008 seems to enforce this (very sensible) rule by
575 * default - don't allow offline attacks on a user's password
576 * by asking for a ticket to them as a service (encrypted with
577 * their probably patheticly insecure password) */
579 if (entry_ex
->entry
.flags
.server
580 && lp_parm_bool(lp_ctx
, NULL
, "kdc", "require spn for service", true)) {
581 if (!is_computer
&& !ldb_msg_find_attr_as_string(msg
, "servicePrincipalName", NULL
)) {
582 entry_ex
->entry
.flags
.server
= 0;
587 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
588 * of the Heimdal KDC. They are stored in a the traditional
589 * DB for audit purposes, and still form part of the structure
592 /* use 'whenCreated' */
593 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
594 /* use 'kadmin' for now (needed by mit_samba) */
595 krb5_make_principal(context
,
596 &entry_ex
->entry
.created_by
.principal
,
597 realm
, "kadmin", NULL
);
599 entry_ex
->entry
.modified_by
= (Event
*) malloc(sizeof(Event
));
600 if (entry_ex
->entry
.modified_by
== NULL
) {
602 krb5_set_error_message(context
, ret
, "malloc: out of memory");
606 /* use 'whenChanged' */
607 entry_ex
->entry
.modified_by
->time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenChanged", 0);
608 /* use 'kadmin' for now (needed by mit_samba) */
609 krb5_make_principal(context
,
610 &entry_ex
->entry
.modified_by
->principal
,
611 realm
, "kadmin", NULL
);
615 /* The lack of password controls etc applies to krbtgt by
616 * virtue of being that particular RID */
617 status
= dom_sid_split_rid(NULL
, samdb_result_dom_sid(mem_ctx
, msg
, "objectSid"), NULL
, &rid
);
619 if (!NT_STATUS_IS_OK(status
)) {
624 if (rid
== DOMAIN_RID_KRBTGT
) {
625 entry_ex
->entry
.valid_end
= NULL
;
626 entry_ex
->entry
.pw_end
= NULL
;
628 entry_ex
->entry
.flags
.invalid
= 0;
629 entry_ex
->entry
.flags
.server
= 1;
631 /* Don't mark all requests for the krbtgt/realm as
632 * 'change password', as otherwise we could get into
633 * trouble, and not enforce the password expirty.
634 * Instead, only do it when request is for the kpasswd service */
635 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
636 && principal
->name
.name_string
.len
== 2
637 && (strcmp(principal
->name
.name_string
.val
[0], "kadmin") == 0)
638 && (strcmp(principal
->name
.name_string
.val
[1], "changepw") == 0)
639 && lp_is_my_domain_or_realm(lp_ctx
, principal
->realm
)) {
640 entry_ex
->entry
.flags
.change_pw
= 1;
642 entry_ex
->entry
.flags
.client
= 0;
643 entry_ex
->entry
.flags
.forwardable
= 1;
644 entry_ex
->entry
.flags
.ok_as_delegate
= 1;
645 } else if (entry_ex
->entry
.flags
.server
&& ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
646 /* The account/password expiry only applies when the account is used as a
647 * client (ie password login), not when used as a server */
649 /* Make very well sure we don't use this for a client,
650 * it could bypass the password restrictions */
651 entry_ex
->entry
.flags
.client
= 0;
653 entry_ex
->entry
.valid_end
= NULL
;
654 entry_ex
->entry
.pw_end
= NULL
;
657 NTTIME must_change_time
658 = samdb_result_force_password_change(kdc_db_ctx
->samdb
, mem_ctx
,
660 if (must_change_time
== 0x7FFFFFFFFFFFFFFFULL
) {
661 entry_ex
->entry
.pw_end
= NULL
;
663 entry_ex
->entry
.pw_end
= malloc(sizeof(*entry_ex
->entry
.pw_end
));
664 if (entry_ex
->entry
.pw_end
== NULL
) {
668 *entry_ex
->entry
.pw_end
= nt_time_to_unix(must_change_time
);
671 acct_expiry
= samdb_result_account_expires(msg
);
672 if (acct_expiry
== 0x7FFFFFFFFFFFFFFFULL
) {
673 entry_ex
->entry
.valid_end
= NULL
;
675 entry_ex
->entry
.valid_end
= malloc(sizeof(*entry_ex
->entry
.valid_end
));
676 if (entry_ex
->entry
.valid_end
== NULL
) {
680 *entry_ex
->entry
.valid_end
= nt_time_to_unix(acct_expiry
);
684 entry_ex
->entry
.valid_start
= NULL
;
686 entry_ex
->entry
.max_life
= NULL
;
688 entry_ex
->entry
.max_renew
= NULL
;
690 entry_ex
->entry
.generation
= NULL
;
692 /* Get keys from the db */
693 ret
= samba_kdc_message2entry_keys(context
, p
->kdc_db_ctx
->ic_ctx
, p
,
694 msg
, userAccountControl
, entry_ex
);
696 /* Could be bougus data in the entry, or out of memory */
700 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
701 if (entry_ex
->entry
.etypes
== NULL
) {
702 krb5_clear_error_message(context
);
706 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
707 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
708 if (entry_ex
->entry
.etypes
->val
== NULL
) {
709 krb5_clear_error_message(context
);
713 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
714 entry_ex
->entry
.etypes
->val
[i
] = entry_ex
->entry
.keys
.val
[i
].key
.keytype
;
718 p
->msg
= talloc_steal(p
, msg
);
722 /* This doesn't free ent itself, that is for the eventual caller to do */
723 hdb_free_entry(context
, entry_ex
);
725 talloc_steal(kdc_db_ctx
, entry_ex
->ctx
);
732 * Construct an hdb_entry from a directory entry.
734 static krb5_error_code
samba_kdc_trust_message2entry(krb5_context context
,
735 struct samba_kdc_db_context
*kdc_db_ctx
,
736 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
737 enum trust_direction direction
,
738 struct ldb_dn
*realm_dn
,
739 struct ldb_message
*msg
,
740 hdb_entry_ex
*entry_ex
)
742 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
743 const char *dnsdomain
;
745 DATA_BLOB password_utf16
;
746 struct samr_Password password_hash
;
747 const struct ldb_val
*password_val
;
748 struct trustAuthInOutBlob password_blob
;
749 struct samba_kdc_entry
*p
;
751 enum ndr_err_code ndr_err
;
752 int i
, ret
, trust_direction_flags
;
754 p
= talloc(mem_ctx
, struct samba_kdc_entry
);
760 p
->kdc_db_ctx
= kdc_db_ctx
;
761 p
->entry_ex
= entry_ex
;
762 p
->realm_dn
= realm_dn
;
764 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
767 entry_ex
->free_entry
= samba_kdc_free_entry
;
769 /* use 'whenCreated' */
770 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
772 entry_ex
->entry
.created_by
.principal
= NULL
;
774 entry_ex
->entry
.valid_start
= NULL
;
776 trust_direction_flags
= ldb_msg_find_attr_as_int(msg
, "trustDirection", 0);
778 if (direction
== INBOUND
) {
779 realm
= strupper_talloc(mem_ctx
, lp_realm(lp_ctx
));
780 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthIncoming");
782 } else { /* OUTBOUND */
783 dnsdomain
= ldb_msg_find_attr_as_string(msg
, "trustPartner", NULL
);
784 realm
= strupper_talloc(mem_ctx
, dnsdomain
);
785 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthOutgoing");
788 if (!password_val
|| !(trust_direction_flags
& direction
)) {
793 ndr_err
= ndr_pull_struct_blob(password_val
, mem_ctx
, p
->kdc_db_ctx
->ic_ctx
, &password_blob
,
794 (ndr_pull_flags_fn_t
)ndr_pull_trustAuthInOutBlob
);
795 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
800 entry_ex
->entry
.kvno
= -1;
801 for (i
=0; i
< password_blob
.count
; i
++) {
802 if (password_blob
.current
->array
[i
].AuthType
== TRUST_AUTH_TYPE_VERSION
) {
803 entry_ex
->entry
.kvno
= password_blob
.current
->array
[i
].AuthInfo
.version
.version
;
807 for (i
=0; i
< password_blob
.count
; i
++) {
808 if (password_blob
.current
->array
[i
].AuthType
== TRUST_AUTH_TYPE_CLEAR
) {
809 password_utf16
= data_blob_const(password_blob
.current
->array
[i
].AuthInfo
.clear
.password
,
810 password_blob
.current
->array
[i
].AuthInfo
.clear
.size
);
811 /* In the future, generate all sorts of
812 * hashes, but for now we can't safely convert
813 * the random strings windows uses into
816 /* but as it is utf16 already, we can get the NT password/arcfour-hmac-md5 key */
817 mdfour(password_hash
.hash
, password_utf16
.data
, password_utf16
.length
);
819 } else if (password_blob
.current
->array
[i
].AuthType
== TRUST_AUTH_TYPE_NT4OWF
) {
820 password_hash
= password_blob
.current
->array
[i
].AuthInfo
.nt4owf
.password
;
824 entry_ex
->entry
.keys
.len
= 0;
825 entry_ex
->entry
.keys
.val
= NULL
;
827 if (i
< password_blob
.count
) {
829 /* Must have found a cleartext or MD4 password */
830 entry_ex
->entry
.keys
.val
= calloc(1, sizeof(Key
));
833 key
.salt
= NULL
; /* No salt for this enc type */
835 if (entry_ex
->entry
.keys
.val
== NULL
) {
840 ret
= krb5_keyblock_init(context
,
841 ENCTYPE_ARCFOUR_HMAC
,
842 password_hash
.hash
, sizeof(password_hash
.hash
),
845 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
846 entry_ex
->entry
.keys
.len
++;
849 entry_ex
->entry
.principal
= malloc(sizeof(*(entry_ex
->entry
.principal
)));
851 ret
= copy_Principal(principal
, entry_ex
->entry
.principal
);
853 krb5_clear_error_message(context
);
857 /* While we have copied the client principal, tests
858 * show that Win2k3 returns the 'corrected' realm, not
859 * the client-specified realm. This code attempts to
860 * replace the client principal's realm with the one
861 * we determine from our records */
863 krb5_principal_set_realm(context
, entry_ex
->entry
.principal
, realm
);
864 entry_ex
->entry
.flags
= int2HDBFlags(0);
865 entry_ex
->entry
.flags
.immutable
= 1;
866 entry_ex
->entry
.flags
.invalid
= 0;
867 entry_ex
->entry
.flags
.server
= 1;
868 entry_ex
->entry
.flags
.require_preauth
= 1;
870 entry_ex
->entry
.pw_end
= NULL
;
872 entry_ex
->entry
.max_life
= NULL
;
874 entry_ex
->entry
.max_renew
= NULL
;
876 entry_ex
->entry
.generation
= NULL
;
878 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
879 if (entry_ex
->entry
.etypes
== NULL
) {
880 krb5_clear_error_message(context
);
884 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
885 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
886 if (entry_ex
->entry
.etypes
->val
== NULL
) {
887 krb5_clear_error_message(context
);
891 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
892 entry_ex
->entry
.etypes
->val
[i
] = entry_ex
->entry
.keys
.val
[i
].key
.keytype
;
896 p
->msg
= talloc_steal(p
, msg
);
900 /* This doesn't free ent itself, that is for the eventual caller to do */
901 hdb_free_entry(context
, entry_ex
);
903 talloc_steal(kdc_db_ctx
, entry_ex
->ctx
);
910 static krb5_error_code
samba_kdc_lookup_trust(krb5_context context
, struct ldb_context
*ldb_ctx
,
913 struct ldb_dn
*realm_dn
,
914 struct ldb_message
**pmsg
)
919 const char * const *attrs
= trust_attrs
;
921 struct ldb_result
*res
= NULL
;
922 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=trustedDomain)(|(flatname=%s)(trustPartner=%s)))", realm
, realm
);
926 krb5_set_error_message(context
, ret
, "talloc_asprintf: out of memory");
930 lret
= ldb_search(ldb_ctx
, mem_ctx
, &res
,
931 ldb_get_default_basedn(ldb_ctx
),
932 LDB_SCOPE_SUBTREE
, attrs
, "%s", filter
);
933 if (lret
!= LDB_SUCCESS
) {
934 DEBUG(3, ("Failed to search for %s: %s\n", filter
, ldb_errstring(ldb_ctx
)));
935 return HDB_ERR_NOENTRY
;
936 } else if (res
->count
== 0 || res
->count
> 1) {
937 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter
, res
->count
));
939 return HDB_ERR_NOENTRY
;
941 talloc_steal(mem_ctx
, res
->msgs
);
942 *pmsg
= res
->msgs
[0];
947 static krb5_error_code
samba_kdc_lookup_client(krb5_context context
,
948 struct samba_kdc_db_context
*kdc_db_ctx
,
950 krb5_const_principal principal
,
952 struct ldb_dn
**realm_dn
,
953 struct ldb_message
**msg
) {
955 char *principal_string
;
958 ret
= krb5_unparse_name(context
, principal
, &principal_string
);
964 nt_status
= sam_get_results_principal(kdc_db_ctx
->samdb
,
965 mem_ctx
, principal_string
, attrs
,
967 free(principal_string
);
968 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
969 return HDB_ERR_NOENTRY
;
970 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_MEMORY
)) {
972 } else if (!NT_STATUS_IS_OK(nt_status
)) {
979 static krb5_error_code
samba_kdc_fetch_client(krb5_context context
,
980 struct samba_kdc_db_context
*kdc_db_ctx
,
982 krb5_const_principal principal
,
983 hdb_entry_ex
*entry_ex
) {
984 struct ldb_dn
*realm_dn
;
986 struct ldb_message
*msg
= NULL
;
988 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
989 mem_ctx
, principal
, user_attrs
,
995 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
996 principal
, SAMBA_KDC_ENT_TYPE_CLIENT
,
997 realm_dn
, msg
, entry_ex
);
1001 static krb5_error_code
samba_kdc_fetch_krbtgt(krb5_context context
,
1002 struct samba_kdc_db_context
*kdc_db_ctx
,
1003 TALLOC_CTX
*mem_ctx
,
1004 krb5_const_principal principal
,
1005 hdb_entry_ex
*entry_ex
)
1007 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
1008 krb5_error_code ret
;
1009 struct ldb_message
*msg
= NULL
;
1010 struct ldb_dn
*realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
1013 krb5_principal alloc_principal
= NULL
;
1014 if (principal
->name
.name_string
.len
!= 2
1015 || (strcmp(principal
->name
.name_string
.val
[0], KRB5_TGS_NAME
) != 0)) {
1017 return HDB_ERR_NOENTRY
;
1020 /* krbtgt case. Either us or a trusted realm */
1022 if (lp_is_my_domain_or_realm(lp_ctx
, principal
->realm
)
1023 && lp_is_my_domain_or_realm(lp_ctx
, principal
->name
.name_string
.val
[1])) {
1025 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1026 * is in our db, then direct the caller at our primary
1032 lret
= gendb_search_single_extended_dn(kdc_db_ctx
->samdb
, mem_ctx
,
1033 realm_dn
, LDB_SCOPE_SUBTREE
,
1035 "(&(objectClass=user)(samAccountName=krbtgt))");
1036 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
1037 krb5_warnx(context
, "samba_kdc_fetch: could not find own KRBTGT in DB!");
1038 krb5_set_error_message(context
, HDB_ERR_NOENTRY
, "samba_kdc_fetch: could not find own KRBTGT in DB!");
1039 return HDB_ERR_NOENTRY
;
1040 } else if (lret
!= LDB_SUCCESS
) {
1041 krb5_warnx(context
, "samba_kdc_fetch: could not find own KRBTGT in DB: %s", ldb_errstring(kdc_db_ctx
->samdb
));
1042 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
));
1043 return HDB_ERR_NOENTRY
;
1046 realm_fixed
= strupper_talloc(mem_ctx
, lp_realm(lp_ctx
));
1049 krb5_set_error_message(context
, ret
, "strupper_talloc: out of memory");
1053 ret
= krb5_copy_principal(context
, principal
, &alloc_principal
);
1058 free(alloc_principal
->name
.name_string
.val
[1]);
1059 alloc_principal
->name
.name_string
.val
[1] = strdup(realm_fixed
);
1060 talloc_free(realm_fixed
);
1061 if (!alloc_principal
->name
.name_string
.val
[1]) {
1063 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: strdup() failed!");
1066 principal
= alloc_principal
;
1068 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1069 principal
, SAMBA_KDC_ENT_TYPE_KRBTGT
,
1070 realm_dn
, msg
, entry_ex
);
1072 krb5_warnx(context
, "samba_kdc_fetch: self krbtgt message2entry failed");
1077 enum trust_direction direction
= UNKNOWN
;
1079 /* Either an inbound or outbound trust */
1081 if (strcasecmp(lp_realm(lp_ctx
), principal
->realm
) == 0) {
1082 /* look for inbound trust */
1083 direction
= INBOUND
;
1084 realm
= principal
->name
.name_string
.val
[1];
1087 if (strcasecmp(lp_realm(lp_ctx
), principal
->name
.name_string
.val
[1]) == 0) {
1088 /* look for outbound trust */
1089 direction
= OUTBOUND
;
1090 realm
= principal
->realm
;
1093 /* Trusted domains are under CN=system */
1095 ret
= samba_kdc_lookup_trust(context
, kdc_db_ctx
->samdb
,
1097 realm
, realm_dn
, &msg
);
1100 krb5_warnx(context
, "samba_kdc_fetch: could not find principal in DB");
1101 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: could not find principal in DB");
1105 ret
= samba_kdc_trust_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1106 principal
, direction
,
1107 realm_dn
, msg
, entry_ex
);
1109 krb5_warnx(context
, "samba_kdc_fetch: trust_message2entry failed");
1114 /* we should lookup trusted domains */
1115 return HDB_ERR_NOENTRY
;
1120 static krb5_error_code
samba_kdc_lookup_server(krb5_context context
,
1121 struct samba_kdc_db_context
*kdc_db_ctx
,
1122 TALLOC_CTX
*mem_ctx
,
1123 krb5_const_principal principal
,
1125 struct ldb_dn
**realm_dn
,
1126 struct ldb_message
**msg
)
1128 krb5_error_code ret
;
1130 if (principal
->name
.name_string
.len
>= 2) {
1131 /* 'normal server' case */
1134 struct ldb_dn
*user_dn
;
1135 char *principal_string
;
1137 ret
= krb5_unparse_name_flags(context
, principal
,
1138 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
1144 /* At this point we may find the host is known to be
1145 * in a different realm, so we should generate a
1146 * referral instead */
1147 nt_status
= crack_service_principal_name(kdc_db_ctx
->samdb
,
1148 mem_ctx
, principal_string
,
1149 &user_dn
, realm_dn
);
1150 free(principal_string
);
1152 if (!NT_STATUS_IS_OK(nt_status
)) {
1153 return HDB_ERR_NOENTRY
;
1156 ldb_ret
= gendb_search_single_extended_dn(kdc_db_ctx
->samdb
,
1158 user_dn
, LDB_SCOPE_BASE
,
1161 if (ldb_ret
!= LDB_SUCCESS
) {
1162 return HDB_ERR_NOENTRY
;
1167 char *filter
= NULL
;
1169 /* server as client principal case, but we must not lookup userPrincipalNames */
1170 *realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
1171 realm
= krb5_principal_get_realm(context
, principal
);
1173 /* TODO: Check if it is our realm, otherwise give referall */
1175 ret
= krb5_unparse_name_flags(context
, principal
, KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &short_princ
);
1178 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_principal: could not parse principal");
1179 krb5_warnx(context
, "samba_kdc_lookup_principal: could not parse principal");
1183 lret
= gendb_search_single_extended_dn(kdc_db_ctx
->samdb
, mem_ctx
,
1184 *realm_dn
, LDB_SCOPE_SUBTREE
,
1185 msg
, attrs
, "(&(objectClass=user)(samAccountName=%s))",
1186 ldb_binary_encode_string(mem_ctx
, short_princ
));
1188 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
1189 DEBUG(3, ("Failed find a entry for %s\n", filter
));
1190 return HDB_ERR_NOENTRY
;
1192 if (lret
!= LDB_SUCCESS
) {
1193 DEBUG(3, ("Failed single search for for %s - %s\n",
1194 filter
, ldb_errstring(kdc_db_ctx
->samdb
)));
1195 return HDB_ERR_NOENTRY
;
1202 static krb5_error_code
samba_kdc_fetch_server(krb5_context context
,
1203 struct samba_kdc_db_context
*kdc_db_ctx
,
1204 TALLOC_CTX
*mem_ctx
,
1205 krb5_const_principal principal
,
1206 hdb_entry_ex
*entry_ex
)
1208 krb5_error_code ret
;
1209 struct ldb_dn
*realm_dn
;
1210 struct ldb_message
*msg
;
1212 ret
= samba_kdc_lookup_server(context
, kdc_db_ctx
, mem_ctx
, principal
,
1213 server_attrs
, &realm_dn
, &msg
);
1218 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1219 principal
, SAMBA_KDC_ENT_TYPE_SERVER
,
1220 realm_dn
, msg
, entry_ex
);
1222 krb5_warnx(context
, "samba_kdc_fetch: message2entry failed");
1228 krb5_error_code
samba_kdc_fetch(krb5_context context
,
1229 struct samba_kdc_db_context
*kdc_db_ctx
,
1230 krb5_const_principal principal
,
1232 hdb_entry_ex
*entry_ex
)
1234 krb5_error_code ret
= HDB_ERR_NOENTRY
;
1235 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_fetch context");
1239 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
1243 if (flags
& HDB_F_GET_CLIENT
) {
1244 ret
= samba_kdc_fetch_client(context
, kdc_db_ctx
, mem_ctx
, principal
, entry_ex
);
1245 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1247 if (flags
& HDB_F_GET_SERVER
) {
1248 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
1249 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, entry_ex
);
1250 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1252 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
1253 ret
= samba_kdc_fetch_server(context
, kdc_db_ctx
, mem_ctx
, principal
, entry_ex
);
1254 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1256 if (flags
& HDB_F_GET_KRBTGT
) {
1257 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, entry_ex
);
1258 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1262 talloc_free(mem_ctx
);
1266 struct samba_kdc_seq
{
1269 struct ldb_message
**msgs
;
1270 struct ldb_dn
*realm_dn
;
1273 static krb5_error_code
samba_kdc_seq(krb5_context context
,
1274 struct samba_kdc_db_context
*kdc_db_ctx
,
1275 hdb_entry_ex
*entry
)
1277 krb5_error_code ret
;
1278 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
1279 TALLOC_CTX
*mem_ctx
;
1280 hdb_entry_ex entry_ex
;
1281 memset(&entry_ex
, '\0', sizeof(entry_ex
));
1284 return HDB_ERR_NOENTRY
;
1287 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_seq context");
1291 krb5_set_error_message(context
, ret
, "samba_kdc_seq: talloc_named() failed!");
1295 if (priv
->index
< priv
->count
) {
1296 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1297 NULL
, SAMBA_KDC_ENT_TYPE_ANY
,
1298 priv
->realm_dn
, priv
->msgs
[priv
->index
++], entry
);
1300 ret
= HDB_ERR_NOENTRY
;
1305 kdc_db_ctx
->seq_ctx
= NULL
;
1307 talloc_free(mem_ctx
);
1313 krb5_error_code
samba_kdc_firstkey(krb5_context context
,
1314 struct samba_kdc_db_context
*kdc_db_ctx
,
1315 hdb_entry_ex
*entry
)
1317 struct ldb_context
*ldb_ctx
= kdc_db_ctx
->samdb
;
1318 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
1320 struct ldb_result
*res
= NULL
;
1321 krb5_error_code ret
;
1322 TALLOC_CTX
*mem_ctx
;
1327 kdc_db_ctx
->seq_ctx
= NULL
;
1330 priv
= (struct samba_kdc_seq
*) talloc(kdc_db_ctx
, struct samba_kdc_seq
);
1333 krb5_set_error_message(context
, ret
, "talloc: out of memory");
1339 priv
->realm_dn
= ldb_get_default_basedn(ldb_ctx
);
1342 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_firstkey context");
1346 krb5_set_error_message(context
, ret
, "samba_kdc_firstkey: talloc_named() failed!");
1350 ret
= krb5_get_default_realm(context
, &realm
);
1356 lret
= ldb_search(ldb_ctx
, priv
, &res
,
1357 priv
->realm_dn
, LDB_SCOPE_SUBTREE
, user_attrs
,
1358 "(objectClass=user)");
1360 if (lret
!= LDB_SUCCESS
) {
1362 return HDB_ERR_NOENTRY
;
1365 priv
->count
= res
->count
;
1366 priv
->msgs
= talloc_steal(priv
, res
->msgs
);
1369 kdc_db_ctx
->seq_ctx
= priv
;
1371 ret
= samba_kdc_seq(context
, kdc_db_ctx
, entry
);
1375 kdc_db_ctx
->seq_ctx
= NULL
;
1377 talloc_free(mem_ctx
);
1382 krb5_error_code
samba_kdc_nextkey(krb5_context context
,
1383 struct samba_kdc_db_context
*kdc_db_ctx
,
1384 hdb_entry_ex
*entry
)
1386 return samba_kdc_seq(context
, kdc_db_ctx
, entry
);
1389 /* Check if a given entry may delegate to this target principal
1391 * This is currently a very nasty hack - allowing only delegation to itself.
1394 samba_kdc_check_constrained_delegation(krb5_context context
,
1395 struct samba_kdc_db_context
*kdc_db_ctx
,
1396 hdb_entry_ex
*entry
,
1397 krb5_const_principal target_principal
)
1399 krb5_error_code ret
;
1400 krb5_principal enterprise_prinicpal
= NULL
;
1401 struct ldb_dn
*realm_dn
;
1402 struct ldb_message
*msg
;
1403 struct dom_sid
*orig_sid
;
1404 struct dom_sid
*target_sid
;
1405 struct samba_kdc_entry
*p
= talloc_get_type(entry
->ctx
, struct samba_kdc_entry
);
1406 const char *delegation_check_attrs
[] = {
1410 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_constrained_delegation");
1414 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
1418 if (target_principal
->name
.name_type
== KRB5_NT_ENTERPRISE_PRINCIPAL
) {
1419 /* Need to reparse the enterprise principal to find the real target */
1420 if (target_principal
->name
.name_string
.len
!= 1) {
1421 ret
= KRB5_PARSE_MALFORMED
;
1422 krb5_set_error_message(context
, ret
, "samba_kdc_check_constrained_delegation: request for delegation to enterprise principal with wrong (%d) number of components",
1423 target_principal
->name
.name_string
.len
);
1424 talloc_free(mem_ctx
);
1427 ret
= krb5_parse_name(context
, target_principal
->name
.name_string
.val
[0],
1428 &enterprise_prinicpal
);
1430 talloc_free(mem_ctx
);
1433 target_principal
= enterprise_prinicpal
;
1436 ret
= samba_kdc_lookup_server(context
, kdc_db_ctx
, mem_ctx
, target_principal
,
1437 delegation_check_attrs
, &realm_dn
, &msg
);
1439 krb5_free_principal(context
, enterprise_prinicpal
);
1442 talloc_free(mem_ctx
);
1446 orig_sid
= samdb_result_dom_sid(mem_ctx
, p
->msg
, "objectSid");
1447 target_sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
1449 /* Allow delegation to the same principal, even if by a different
1450 * name. The easy and safe way to prove this is by SID
1452 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
1453 talloc_free(mem_ctx
);
1454 return KRB5KDC_ERR_BADOPTION
;
1457 talloc_free(mem_ctx
);
1461 /* Certificates printed by a the Certificate Authority might have a
1462 * slightly different form of the user principal name to that in the
1463 * database. Allow a mismatch where they both refer to the same
1467 samba_kdc_check_pkinit_ms_upn_match(krb5_context context
,
1468 struct samba_kdc_db_context
*kdc_db_ctx
,
1469 hdb_entry_ex
*entry
,
1470 krb5_const_principal certificate_principal
)
1472 krb5_error_code ret
;
1473 struct ldb_dn
*realm_dn
;
1474 struct ldb_message
*msg
;
1475 struct dom_sid
*orig_sid
;
1476 struct dom_sid
*target_sid
;
1477 struct samba_kdc_entry
*p
= talloc_get_type(entry
->ctx
, struct samba_kdc_entry
);
1478 const char *ms_upn_check_attrs
[] = {
1482 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_pkinit_ms_upn_match");
1486 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
1490 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
1491 mem_ctx
, certificate_principal
,
1492 ms_upn_check_attrs
, &realm_dn
, &msg
);
1495 talloc_free(mem_ctx
);
1499 orig_sid
= samdb_result_dom_sid(mem_ctx
, p
->msg
, "objectSid");
1500 target_sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
1502 /* Consider these to be the same principal, even if by a different
1503 * name. The easy and safe way to prove this is by SID
1505 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
1506 talloc_free(mem_ctx
);
1507 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1510 talloc_free(mem_ctx
);