2 Unix SMB/CIFS implementation.
4 Database Glue between Samba and the KDC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2009
7 Copyright (C) Simo Sorce <idra@samba.org> 2010
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "system/time.h"
26 #include "../libds/common/flags.h"
27 #include "lib/ldb/include/ldb.h"
28 #include "librpc/gen_ndr/netlogon.h"
29 #include "libcli/security/security.h"
30 #include "auth/auth.h"
31 #include "auth/credentials/credentials.h"
32 #include "auth/auth_sam.h"
33 #include "../lib/util/util_ldb.h"
34 #include "dsdb/samdb/samdb.h"
35 #include "dsdb/common/util.h"
36 #include "librpc/ndr/libndr.h"
37 #include "librpc/gen_ndr/ndr_drsblobs.h"
38 #include "librpc/gen_ndr/lsa.h"
39 #include "libcli/auth/libcli_auth.h"
40 #include "param/param.h"
41 #include "../lib/crypto/md4.h"
42 #include "system/kerberos.h"
43 #include "auth/kerberos/kerberos.h"
45 #include "kdc/samba_kdc.h"
46 #include "kdc/db-glue.h"
48 enum samba_kdc_ent_type
49 { SAMBA_KDC_ENT_TYPE_CLIENT
, SAMBA_KDC_ENT_TYPE_SERVER
,
50 SAMBA_KDC_ENT_TYPE_KRBTGT
, SAMBA_KDC_ENT_TYPE_TRUST
, SAMBA_KDC_ENT_TYPE_ANY
};
52 enum trust_direction
{
54 INBOUND
= LSA_TRUST_DIRECTION_INBOUND
,
55 OUTBOUND
= LSA_TRUST_DIRECTION_OUTBOUND
58 static const char *trust_attrs
[] = {
63 "msDS-SupportedEncryptionTypes",
70 static KerberosTime
ldb_msg_find_krb5time_ldap_time(struct ldb_message
*msg
, const char *attr
, KerberosTime default_val
)
76 gentime
= ldb_msg_find_attr_as_string(msg
, attr
, NULL
);
80 tmp
= strptime(gentime
, "%Y%m%d%H%M%SZ", &tm
);
88 static HDBFlags
uf2HDBFlags(krb5_context context
, int userAccountControl
, enum samba_kdc_ent_type ent_type
)
90 HDBFlags flags
= int2HDBFlags(0);
92 /* we don't allow kadmin deletes */
95 /* mark the principal as invalid to start with */
100 /* All accounts are servers, but this may be disabled again in the caller */
103 /* Account types - clear the invalid bit if it turns out to be valid */
104 if (userAccountControl
& UF_NORMAL_ACCOUNT
) {
105 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
111 if (userAccountControl
& UF_INTERDOMAIN_TRUST_ACCOUNT
) {
112 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
117 if (userAccountControl
& UF_WORKSTATION_TRUST_ACCOUNT
) {
118 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
123 if (userAccountControl
& UF_SERVER_TRUST_ACCOUNT
) {
124 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
130 /* Not permitted to act as a client if disabled */
131 if (userAccountControl
& UF_ACCOUNTDISABLE
) {
134 if (userAccountControl
& UF_LOCKOUT
) {
138 if (userAccountControl & UF_PASSWORD_NOTREQD) {
143 UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
145 if (userAccountControl
& UF_TEMP_DUPLICATE_ACCOUNT
) {
149 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in samba_kdc_message2entry() */
152 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
156 if (userAccountControl
& UF_SMARTCARD_REQUIRED
) {
157 flags
.require_hwauth
= 1;
159 if (userAccountControl
& UF_TRUSTED_FOR_DELEGATION
) {
160 flags
.ok_as_delegate
= 1;
162 if (!(userAccountControl
& UF_NOT_DELEGATED
)) {
163 flags
.forwardable
= 1;
167 if (userAccountControl
& UF_DONT_REQUIRE_PREAUTH
) {
168 flags
.require_preauth
= 0;
170 flags
.require_preauth
= 1;
176 static int samba_kdc_entry_destructor(struct samba_kdc_entry
*p
)
178 hdb_entry_ex
*entry_ex
= p
->entry_ex
;
179 free_hdb_entry(&entry_ex
->entry
);
183 static void samba_kdc_free_entry(krb5_context context
, hdb_entry_ex
*entry_ex
)
185 /* this function is called only from hdb_free_entry().
186 * Make sure we neutralize the destructor or we will
187 * get a double free later when hdb_free_entry() will
188 * try to call free_hdb_entry() */
189 talloc_set_destructor(entry_ex
->ctx
, NULL
);
191 /* now proceed to free the talloc part */
192 talloc_free(entry_ex
->ctx
);
195 static krb5_error_code
samba_kdc_message2entry_keys(krb5_context context
,
197 struct ldb_message
*msg
,
199 unsigned int userAccountControl
,
200 enum samba_kdc_ent_type ent_type
,
201 hdb_entry_ex
*entry_ex
)
203 krb5_error_code ret
= 0;
204 enum ndr_err_code ndr_err
;
205 struct samr_Password
*hash
;
206 const struct ldb_val
*sc_val
;
207 struct supplementalCredentialsBlob scb
;
208 struct supplementalCredentialsPackage
*scpk
= NULL
;
209 bool newer_keys
= false;
210 struct package_PrimaryKerberosBlob _pkb
;
211 struct package_PrimaryKerberosCtr3
*pkb3
= NULL
;
212 struct package_PrimaryKerberosCtr4
*pkb4
= NULL
;
214 uint16_t allocated_keys
= 0;
216 /* Supported Enc for this entry */
217 uint32_t supported_enctypes
= ENC_ALL_TYPES
; /* by default, we support all enc types */
219 /* However, if this is a TGS-REQ, then lock it down to a
220 * reasonable guess as to what the server can decode. The
221 * krbtgt is special - default to use what is stored for the KDC */
222 if (rid
!= DOMAIN_RID_KRBTGT
&& ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
223 /* This is the standard set for a server that has not declared a msDS-SupportedEncryptionTypes */
224 supported_enctypes
= ENC_CRC32
| ENC_RSA_MD5
| ENC_RC4_HMAC_MD5
;
226 supported_enctypes
= ldb_msg_find_attr_as_uint(msg
, "msDS-SupportedEncryptionTypes",
228 if (rid
== DOMAIN_RID_KRBTGT
) {
229 /* Be double-sure never to use DES here */
230 supported_enctypes
&= ~(ENC_CRC32
|ENC_RSA_MD5
);
234 case SAMBA_KDC_ENT_TYPE_KRBTGT
:
235 case SAMBA_KDC_ENT_TYPE_TRUST
:
236 /* Disallow krbtgt and trust tickets to be DES encrypted, it's just too dangerous */
237 supported_enctypes
&= ~(ENC_CRC32
|ENC_RSA_MD5
);
241 /* No further restrictions */
244 /* If UF_USE_DES_KEY_ONLY has been set, then don't allow use of the newer enc types */
245 if (userAccountControl
& UF_USE_DES_KEY_ONLY
) {
246 /* However, don't allow use of DES, if we were told not to by msDS-SupportedEncTypes */
247 supported_enctypes
&= ENC_CRC32
|ENC_RSA_MD5
;
250 entry_ex
->entry
.keys
.val
= NULL
;
251 entry_ex
->entry
.keys
.len
= 0;
253 entry_ex
->entry
.kvno
= ldb_msg_find_attr_as_int(msg
, "msDS-KeyVersionNumber", 0);
255 /* Get keys from the db */
257 hash
= samdb_result_hash(mem_ctx
, msg
, "unicodePwd");
258 sc_val
= ldb_msg_find_ldb_val(msg
, "supplementalCredentials");
260 /* unicodePwd for enctype 0x17 (23) if present */
265 /* supplementalCredentials if present */
267 ndr_err
= ndr_pull_struct_blob_all(sc_val
, mem_ctx
, &scb
,
268 (ndr_pull_flags_fn_t
)ndr_pull_supplementalCredentialsBlob
);
269 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
270 dump_data(0, sc_val
->data
, sc_val
->length
);
275 if (scb
.sub
.signature
!= SUPPLEMENTAL_CREDENTIALS_SIGNATURE
) {
276 NDR_PRINT_DEBUG(supplementalCredentialsBlob
, &scb
);
281 for (i
=0; i
< scb
.sub
.num_packages
; i
++) {
282 if (strcmp("Primary:Kerberos-Newer-Keys", scb
.sub
.packages
[i
].name
) == 0) {
283 scpk
= &scb
.sub
.packages
[i
];
284 if (!scpk
->data
|| !scpk
->data
[0]) {
290 } else if (strcmp("Primary:Kerberos", scb
.sub
.packages
[i
].name
) == 0) {
291 scpk
= &scb
.sub
.packages
[i
];
292 if (!scpk
->data
|| !scpk
->data
[0]) {
296 * we don't break here in hope to find
297 * a Kerberos-Newer-Keys package
303 * Primary:Kerberos-Newer-Keys or Primary:Kerberos element
304 * of supplementalCredentials
309 blob
= strhex_to_data_blob(mem_ctx
, scpk
->data
);
315 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
316 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, &_pkb
,
317 (ndr_pull_flags_fn_t
)ndr_pull_package_PrimaryKerberosBlob
);
318 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
320 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
321 krb5_warnx(context
, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
325 if (newer_keys
&& _pkb
.version
!= 4) {
327 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
328 krb5_warnx(context
, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
332 if (!newer_keys
&& _pkb
.version
!= 3) {
334 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
335 krb5_warnx(context
, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
339 if (_pkb
.version
== 4) {
340 pkb4
= &_pkb
.ctr
.ctr4
;
341 allocated_keys
+= pkb4
->num_keys
;
342 } else if (_pkb
.version
== 3) {
343 pkb3
= &_pkb
.ctr
.ctr3
;
344 allocated_keys
+= pkb3
->num_keys
;
348 if (allocated_keys
== 0) {
349 /* oh, no password. Apparently (comment in
350 * hdb-ldap.c) this violates the ASN.1, but this
351 * allows an entry with no keys (yet). */
355 /* allocate space to decode into */
356 entry_ex
->entry
.keys
.len
= 0;
357 entry_ex
->entry
.keys
.val
= calloc(allocated_keys
, sizeof(Key
));
358 if (entry_ex
->entry
.keys
.val
== NULL
) {
363 if (hash
&& (supported_enctypes
& ENC_RC4_HMAC_MD5
)) {
367 key
.salt
= NULL
; /* No salt for this enc type */
369 ret
= krb5_keyblock_init(context
,
370 ENCTYPE_ARCFOUR_HMAC
,
371 hash
->hash
, sizeof(hash
->hash
),
377 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
378 entry_ex
->entry
.keys
.len
++;
382 for (i
=0; i
< pkb4
->num_keys
; i
++) {
385 if (!pkb4
->keys
[i
].value
) continue;
387 if (!(kerberos_enctype_to_bitmap(pkb4
->keys
[i
].keytype
) & supported_enctypes
)) {
394 if (pkb4
->salt
.string
) {
397 salt
= data_blob_string_const(pkb4
->salt
.string
);
399 key
.salt
= calloc(1, sizeof(*key
.salt
));
400 if (key
.salt
== NULL
) {
405 key
.salt
->type
= hdb_pw_salt
;
407 ret
= krb5_data_copy(&key
.salt
->salt
, salt
.data
, salt
.length
);
415 /* TODO: maybe pass the iteration_count somehow... */
417 ret
= krb5_keyblock_init(context
,
418 pkb4
->keys
[i
].keytype
,
419 pkb4
->keys
[i
].value
->data
,
420 pkb4
->keys
[i
].value
->length
,
422 if (ret
== KRB5_PROG_ETYPE_NOSUPP
) {
423 DEBUG(2,("Unsupported keytype ignored - type %u\n",
424 pkb4
->keys
[i
].keytype
));
437 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
438 entry_ex
->entry
.keys
.len
++;
441 for (i
=0; i
< pkb3
->num_keys
; i
++) {
444 if (!pkb3
->keys
[i
].value
) continue;
446 if (!(kerberos_enctype_to_bitmap(pkb3
->keys
[i
].keytype
) & supported_enctypes
)) {
453 if (pkb3
->salt
.string
) {
456 salt
= data_blob_string_const(pkb3
->salt
.string
);
458 key
.salt
= calloc(1, sizeof(*key
.salt
));
459 if (key
.salt
== NULL
) {
464 key
.salt
->type
= hdb_pw_salt
;
466 ret
= krb5_data_copy(&key
.salt
->salt
, salt
.data
, salt
.length
);
474 ret
= krb5_keyblock_init(context
,
475 pkb3
->keys
[i
].keytype
,
476 pkb3
->keys
[i
].value
->data
,
477 pkb3
->keys
[i
].value
->length
,
488 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
489 entry_ex
->entry
.keys
.len
++;
495 entry_ex
->entry
.keys
.len
= 0;
497 if (entry_ex
->entry
.keys
.len
== 0 && entry_ex
->entry
.keys
.val
) {
498 free(entry_ex
->entry
.keys
.val
);
499 entry_ex
->entry
.keys
.val
= NULL
;
505 * Construct an hdb_entry from a directory entry.
507 static krb5_error_code
samba_kdc_message2entry(krb5_context context
,
508 struct samba_kdc_db_context
*kdc_db_ctx
,
509 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
510 enum samba_kdc_ent_type ent_type
,
511 struct ldb_dn
*realm_dn
,
512 struct ldb_message
*msg
,
513 hdb_entry_ex
*entry_ex
)
515 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
516 unsigned int userAccountControl
;
518 krb5_error_code ret
= 0;
519 krb5_boolean is_computer
= FALSE
;
520 char *realm
= strupper_talloc(mem_ctx
, lpcfg_realm(lp_ctx
));
522 struct samba_kdc_entry
*p
;
527 struct ldb_message_element
*objectclasses
;
528 struct ldb_val computer_val
;
529 const char *samAccountName
= ldb_msg_find_attr_as_string(msg
, "samAccountName", NULL
);
530 computer_val
.data
= discard_const_p(uint8_t,"computer");
531 computer_val
.length
= strlen((const char *)computer_val
.data
);
533 if (!samAccountName
) {
535 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: no samAccountName present");
539 objectclasses
= ldb_msg_find_element(msg
, "objectClass");
541 if (objectclasses
&& ldb_msg_find_val(objectclasses
, &computer_val
)) {
545 memset(entry_ex
, 0, sizeof(*entry_ex
));
549 krb5_set_error_message(context
, ret
, "talloc_strdup: out of memory");
553 p
= talloc(mem_ctx
, struct samba_kdc_entry
);
559 p
->kdc_db_ctx
= kdc_db_ctx
;
560 p
->entry_ex
= entry_ex
;
561 p
->realm_dn
= talloc_reference(p
, realm_dn
);
567 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
569 /* make sure we do not have bogus data in there */
570 memset(&entry_ex
->entry
, 0, sizeof(hdb_entry
));
573 entry_ex
->free_entry
= samba_kdc_free_entry
;
575 userAccountControl
= ldb_msg_find_attr_as_uint(msg
, "userAccountControl", 0);
578 entry_ex
->entry
.principal
= malloc(sizeof(*(entry_ex
->entry
.principal
)));
579 if (ent_type
== SAMBA_KDC_ENT_TYPE_ANY
&& principal
== NULL
) {
580 krb5_make_principal(context
, &entry_ex
->entry
.principal
, realm
, samAccountName
, NULL
);
582 ret
= copy_Principal(principal
, entry_ex
->entry
.principal
);
584 krb5_clear_error_message(context
);
588 /* While we have copied the client principal, tests
589 * show that Win2k3 returns the 'corrected' realm, not
590 * the client-specified realm. This code attempts to
591 * replace the client principal's realm with the one
592 * we determine from our records */
594 /* this has to be with malloc() */
595 krb5_principal_set_realm(context
, entry_ex
->entry
.principal
, realm
);
598 /* First try and figure out the flags based on the userAccountControl */
599 entry_ex
->entry
.flags
= uf2HDBFlags(context
, userAccountControl
, ent_type
);
601 /* Windows 2008 seems to enforce this (very sensible) rule by
602 * default - don't allow offline attacks on a user's password
603 * by asking for a ticket to them as a service (encrypted with
604 * their probably patheticly insecure password) */
606 if (entry_ex
->entry
.flags
.server
607 && lpcfg_parm_bool(lp_ctx
, NULL
, "kdc", "require spn for service", true)) {
608 if (!is_computer
&& !ldb_msg_find_attr_as_string(msg
, "servicePrincipalName", NULL
)) {
609 entry_ex
->entry
.flags
.server
= 0;
614 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
615 * of the Heimdal KDC. They are stored in a the traditional
616 * DB for audit purposes, and still form part of the structure
619 /* use 'whenCreated' */
620 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
621 /* use 'kadmin' for now (needed by mit_samba) */
622 krb5_make_principal(context
,
623 &entry_ex
->entry
.created_by
.principal
,
624 realm
, "kadmin", NULL
);
626 entry_ex
->entry
.modified_by
= (Event
*) malloc(sizeof(Event
));
627 if (entry_ex
->entry
.modified_by
== NULL
) {
629 krb5_set_error_message(context
, ret
, "malloc: out of memory");
633 /* use 'whenChanged' */
634 entry_ex
->entry
.modified_by
->time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenChanged", 0);
635 /* use 'kadmin' for now (needed by mit_samba) */
636 krb5_make_principal(context
,
637 &entry_ex
->entry
.modified_by
->principal
,
638 realm
, "kadmin", NULL
);
642 /* The lack of password controls etc applies to krbtgt by
643 * virtue of being that particular RID */
644 status
= dom_sid_split_rid(NULL
, samdb_result_dom_sid(mem_ctx
, msg
, "objectSid"), NULL
, &rid
);
646 if (!NT_STATUS_IS_OK(status
)) {
651 if (rid
== DOMAIN_RID_KRBTGT
) {
652 entry_ex
->entry
.valid_end
= NULL
;
653 entry_ex
->entry
.pw_end
= NULL
;
655 entry_ex
->entry
.flags
.invalid
= 0;
656 entry_ex
->entry
.flags
.server
= 1;
658 /* Don't mark all requests for the krbtgt/realm as
659 * 'change password', as otherwise we could get into
660 * trouble, and not enforce the password expirty.
661 * Instead, only do it when request is for the kpasswd service */
662 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
663 && principal
->name
.name_string
.len
== 2
664 && (strcmp(principal
->name
.name_string
.val
[0], "kadmin") == 0)
665 && (strcmp(principal
->name
.name_string
.val
[1], "changepw") == 0)
666 && lpcfg_is_my_domain_or_realm(lp_ctx
, principal
->realm
)) {
667 entry_ex
->entry
.flags
.change_pw
= 1;
669 entry_ex
->entry
.flags
.client
= 0;
670 entry_ex
->entry
.flags
.forwardable
= 1;
671 entry_ex
->entry
.flags
.ok_as_delegate
= 1;
672 } else if (entry_ex
->entry
.flags
.server
&& ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
673 /* The account/password expiry only applies when the account is used as a
674 * client (ie password login), not when used as a server */
676 /* Make very well sure we don't use this for a client,
677 * it could bypass the password restrictions */
678 entry_ex
->entry
.flags
.client
= 0;
680 entry_ex
->entry
.valid_end
= NULL
;
681 entry_ex
->entry
.pw_end
= NULL
;
684 NTTIME must_change_time
685 = samdb_result_force_password_change(kdc_db_ctx
->samdb
, mem_ctx
,
687 if (must_change_time
== 0x7FFFFFFFFFFFFFFFULL
) {
688 entry_ex
->entry
.pw_end
= NULL
;
690 entry_ex
->entry
.pw_end
= malloc(sizeof(*entry_ex
->entry
.pw_end
));
691 if (entry_ex
->entry
.pw_end
== NULL
) {
695 *entry_ex
->entry
.pw_end
= nt_time_to_unix(must_change_time
);
698 acct_expiry
= samdb_result_account_expires(msg
);
699 if (acct_expiry
== 0x7FFFFFFFFFFFFFFFULL
) {
700 entry_ex
->entry
.valid_end
= NULL
;
702 entry_ex
->entry
.valid_end
= malloc(sizeof(*entry_ex
->entry
.valid_end
));
703 if (entry_ex
->entry
.valid_end
== NULL
) {
707 *entry_ex
->entry
.valid_end
= nt_time_to_unix(acct_expiry
);
711 entry_ex
->entry
.valid_start
= NULL
;
713 entry_ex
->entry
.max_life
= NULL
;
715 entry_ex
->entry
.max_renew
= NULL
;
717 entry_ex
->entry
.generation
= NULL
;
719 /* Get keys from the db */
720 ret
= samba_kdc_message2entry_keys(context
, p
, msg
,
721 rid
, userAccountControl
,
724 /* Could be bougus data in the entry, or out of memory */
728 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
729 if (entry_ex
->entry
.etypes
== NULL
) {
730 krb5_clear_error_message(context
);
734 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
735 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
736 if (entry_ex
->entry
.etypes
->val
== NULL
) {
737 krb5_clear_error_message(context
);
741 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
742 entry_ex
->entry
.etypes
->val
[i
] = entry_ex
->entry
.keys
.val
[i
].key
.keytype
;
746 p
->msg
= talloc_steal(p
, msg
);
750 /* This doesn't free ent itself, that is for the eventual caller to do */
751 hdb_free_entry(context
, entry_ex
);
753 talloc_steal(kdc_db_ctx
, entry_ex
->ctx
);
760 * Construct an hdb_entry from a directory entry.
762 static krb5_error_code
samba_kdc_trust_message2entry(krb5_context context
,
763 struct samba_kdc_db_context
*kdc_db_ctx
,
764 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
765 enum trust_direction direction
,
766 struct ldb_dn
*realm_dn
,
767 struct ldb_message
*msg
,
768 hdb_entry_ex
*entry_ex
)
770 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
771 const char *dnsdomain
;
772 char *realm
= strupper_talloc(mem_ctx
, lpcfg_realm(lp_ctx
));
773 DATA_BLOB password_utf16
;
774 struct samr_Password password_hash
;
775 const struct ldb_val
*password_val
;
776 struct trustAuthInOutBlob password_blob
;
777 struct samba_kdc_entry
*p
;
779 enum ndr_err_code ndr_err
;
780 int ret
, trust_direction_flags
;
783 p
= talloc(mem_ctx
, struct samba_kdc_entry
);
789 p
->kdc_db_ctx
= kdc_db_ctx
;
790 p
->entry_ex
= entry_ex
;
791 p
->realm_dn
= realm_dn
;
793 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
795 /* make sure we do not have bogus data in there */
796 memset(&entry_ex
->entry
, 0, sizeof(hdb_entry
));
799 entry_ex
->free_entry
= samba_kdc_free_entry
;
801 /* use 'whenCreated' */
802 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
803 /* use 'kadmin' for now (needed by mit_samba) */
804 krb5_make_principal(context
,
805 &entry_ex
->entry
.created_by
.principal
,
806 realm
, "kadmin", NULL
);
808 entry_ex
->entry
.valid_start
= NULL
;
810 trust_direction_flags
= ldb_msg_find_attr_as_int(msg
, "trustDirection", 0);
812 if (direction
== INBOUND
) {
813 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthIncoming");
815 } else { /* OUTBOUND */
816 dnsdomain
= ldb_msg_find_attr_as_string(msg
, "trustPartner", NULL
);
819 realm
= strupper_talloc(mem_ctx
, dnsdomain
);
820 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthOutgoing");
823 if (!password_val
|| !(trust_direction_flags
& direction
)) {
828 ndr_err
= ndr_pull_struct_blob(password_val
, mem_ctx
, &password_blob
,
829 (ndr_pull_flags_fn_t
)ndr_pull_trustAuthInOutBlob
);
830 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
835 entry_ex
->entry
.kvno
= -1;
836 for (i
=0; i
< password_blob
.count
; i
++) {
837 if (password_blob
.current
->array
[i
].AuthType
== TRUST_AUTH_TYPE_VERSION
) {
838 entry_ex
->entry
.kvno
= password_blob
.current
->array
[i
].AuthInfo
.version
.version
;
842 for (i
=0; i
< password_blob
.count
; i
++) {
843 if (password_blob
.current
->array
[i
].AuthType
== TRUST_AUTH_TYPE_CLEAR
) {
844 password_utf16
= data_blob_const(password_blob
.current
->array
[i
].AuthInfo
.clear
.password
,
845 password_blob
.current
->array
[i
].AuthInfo
.clear
.size
);
846 /* In the future, generate all sorts of
847 * hashes, but for now we can't safely convert
848 * the random strings windows uses into
851 /* but as it is utf16 already, we can get the NT password/arcfour-hmac-md5 key */
852 mdfour(password_hash
.hash
, password_utf16
.data
, password_utf16
.length
);
854 } else if (password_blob
.current
->array
[i
].AuthType
== TRUST_AUTH_TYPE_NT4OWF
) {
855 password_hash
= password_blob
.current
->array
[i
].AuthInfo
.nt4owf
.password
;
860 if (i
< password_blob
.count
) {
862 /* Must have found a cleartext or MD4 password */
863 entry_ex
->entry
.keys
.val
= calloc(1, sizeof(Key
));
866 key
.salt
= NULL
; /* No salt for this enc type */
868 if (entry_ex
->entry
.keys
.val
== NULL
) {
873 ret
= krb5_keyblock_init(context
,
874 ENCTYPE_ARCFOUR_HMAC
,
875 password_hash
.hash
, sizeof(password_hash
.hash
),
878 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
879 entry_ex
->entry
.keys
.len
++;
882 entry_ex
->entry
.principal
= malloc(sizeof(*(entry_ex
->entry
.principal
)));
884 ret
= copy_Principal(principal
, entry_ex
->entry
.principal
);
886 krb5_clear_error_message(context
);
890 /* While we have copied the client principal, tests
891 * show that Win2k3 returns the 'corrected' realm, not
892 * the client-specified realm. This code attempts to
893 * replace the client principal's realm with the one
894 * we determine from our records */
896 krb5_principal_set_realm(context
, entry_ex
->entry
.principal
, realm
);
897 entry_ex
->entry
.flags
= int2HDBFlags(0);
898 entry_ex
->entry
.flags
.immutable
= 1;
899 entry_ex
->entry
.flags
.invalid
= 0;
900 entry_ex
->entry
.flags
.server
= 1;
901 entry_ex
->entry
.flags
.require_preauth
= 1;
903 entry_ex
->entry
.pw_end
= NULL
;
905 entry_ex
->entry
.max_life
= NULL
;
907 entry_ex
->entry
.max_renew
= NULL
;
909 entry_ex
->entry
.generation
= NULL
;
911 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
912 if (entry_ex
->entry
.etypes
== NULL
) {
913 krb5_clear_error_message(context
);
917 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
918 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
919 if (entry_ex
->entry
.etypes
->val
== NULL
) {
920 krb5_clear_error_message(context
);
924 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
925 entry_ex
->entry
.etypes
->val
[i
] = entry_ex
->entry
.keys
.val
[i
].key
.keytype
;
929 p
->msg
= talloc_steal(p
, msg
);
933 /* This doesn't free ent itself, that is for the eventual caller to do */
934 hdb_free_entry(context
, entry_ex
);
936 talloc_steal(kdc_db_ctx
, entry_ex
->ctx
);
943 static krb5_error_code
samba_kdc_lookup_trust(krb5_context context
, struct ldb_context
*ldb_ctx
,
946 struct ldb_dn
*realm_dn
,
947 struct ldb_message
**pmsg
)
952 const char * const *attrs
= trust_attrs
;
954 struct ldb_result
*res
= NULL
;
955 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=trustedDomain)(|(flatname=%s)(trustPartner=%s)))", realm
, realm
);
959 krb5_set_error_message(context
, ret
, "talloc_asprintf: out of memory");
963 lret
= ldb_search(ldb_ctx
, mem_ctx
, &res
,
964 ldb_get_default_basedn(ldb_ctx
),
965 LDB_SCOPE_SUBTREE
, attrs
, "%s", filter
);
966 if (lret
!= LDB_SUCCESS
) {
967 DEBUG(3, ("Failed to search for %s: %s\n", filter
, ldb_errstring(ldb_ctx
)));
968 return HDB_ERR_NOENTRY
;
969 } else if (res
->count
== 0 || res
->count
> 1) {
970 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter
, res
->count
));
972 return HDB_ERR_NOENTRY
;
974 talloc_steal(mem_ctx
, res
->msgs
);
975 *pmsg
= res
->msgs
[0];
980 static krb5_error_code
samba_kdc_lookup_client(krb5_context context
,
981 struct samba_kdc_db_context
*kdc_db_ctx
,
983 krb5_const_principal principal
,
985 struct ldb_dn
**realm_dn
,
986 struct ldb_message
**msg
) {
988 char *principal_string
;
991 ret
= krb5_unparse_name(context
, principal
, &principal_string
);
997 nt_status
= sam_get_results_principal(kdc_db_ctx
->samdb
,
998 mem_ctx
, principal_string
, attrs
,
1000 free(principal_string
);
1001 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
1002 return HDB_ERR_NOENTRY
;
1003 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_MEMORY
)) {
1005 } else if (!NT_STATUS_IS_OK(nt_status
)) {
1012 static krb5_error_code
samba_kdc_fetch_client(krb5_context context
,
1013 struct samba_kdc_db_context
*kdc_db_ctx
,
1014 TALLOC_CTX
*mem_ctx
,
1015 krb5_const_principal principal
,
1016 hdb_entry_ex
*entry_ex
) {
1017 struct ldb_dn
*realm_dn
;
1018 krb5_error_code ret
;
1019 struct ldb_message
*msg
= NULL
;
1021 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
1022 mem_ctx
, principal
, user_attrs
,
1028 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1029 principal
, SAMBA_KDC_ENT_TYPE_CLIENT
,
1030 realm_dn
, msg
, entry_ex
);
1034 static krb5_error_code
samba_kdc_fetch_krbtgt(krb5_context context
,
1035 struct samba_kdc_db_context
*kdc_db_ctx
,
1036 TALLOC_CTX
*mem_ctx
,
1037 krb5_const_principal principal
,
1038 hdb_entry_ex
*entry_ex
)
1040 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
1041 krb5_error_code ret
;
1042 struct ldb_message
*msg
= NULL
;
1043 struct ldb_dn
*realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
1046 krb5_principal alloc_principal
= NULL
;
1047 if (principal
->name
.name_string
.len
!= 2
1048 || (strcmp(principal
->name
.name_string
.val
[0], KRB5_TGS_NAME
) != 0)) {
1050 return HDB_ERR_NOENTRY
;
1053 /* krbtgt case. Either us or a trusted realm */
1055 if (lpcfg_is_my_domain_or_realm(lp_ctx
, principal
->realm
)
1056 && lpcfg_is_my_domain_or_realm(lp_ctx
, principal
->name
.name_string
.val
[1])) {
1058 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1059 * is in our db, then direct the caller at our primary
1065 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
,
1066 &msg
, realm_dn
, LDB_SCOPE_SUBTREE
,
1068 DSDB_SEARCH_SHOW_EXTENDED_DN
,
1069 "(&(objectClass=user)(samAccountName=krbtgt))");
1070 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
1071 krb5_warnx(context
, "samba_kdc_fetch: could not find own KRBTGT in DB!");
1072 krb5_set_error_message(context
, HDB_ERR_NOENTRY
, "samba_kdc_fetch: could not find own KRBTGT in DB!");
1073 return HDB_ERR_NOENTRY
;
1074 } else if (lret
!= LDB_SUCCESS
) {
1075 krb5_warnx(context
, "samba_kdc_fetch: could not find own KRBTGT in DB: %s", ldb_errstring(kdc_db_ctx
->samdb
));
1076 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
));
1077 return HDB_ERR_NOENTRY
;
1080 realm_fixed
= strupper_talloc(mem_ctx
, lpcfg_realm(lp_ctx
));
1083 krb5_set_error_message(context
, ret
, "strupper_talloc: out of memory");
1087 ret
= krb5_copy_principal(context
, principal
, &alloc_principal
);
1092 free(alloc_principal
->name
.name_string
.val
[1]);
1093 alloc_principal
->name
.name_string
.val
[1] = strdup(realm_fixed
);
1094 talloc_free(realm_fixed
);
1095 if (!alloc_principal
->name
.name_string
.val
[1]) {
1097 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: strdup() failed!");
1100 principal
= alloc_principal
;
1102 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1103 principal
, SAMBA_KDC_ENT_TYPE_KRBTGT
,
1104 realm_dn
, msg
, entry_ex
);
1106 krb5_warnx(context
, "samba_kdc_fetch: self krbtgt message2entry failed");
1111 enum trust_direction direction
= UNKNOWN
;
1113 /* Either an inbound or outbound trust */
1115 if (strcasecmp(lpcfg_realm(lp_ctx
), principal
->realm
) == 0) {
1116 /* look for inbound trust */
1117 direction
= INBOUND
;
1118 realm
= principal
->name
.name_string
.val
[1];
1121 if (strcasecmp(lpcfg_realm(lp_ctx
), principal
->name
.name_string
.val
[1]) == 0) {
1122 /* look for outbound trust */
1123 direction
= OUTBOUND
;
1124 realm
= principal
->realm
;
1127 /* Trusted domains are under CN=system */
1129 ret
= samba_kdc_lookup_trust(context
, kdc_db_ctx
->samdb
,
1131 realm
, realm_dn
, &msg
);
1134 krb5_warnx(context
, "samba_kdc_fetch: could not find principal in DB");
1135 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: could not find principal in DB");
1139 ret
= samba_kdc_trust_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1140 principal
, direction
,
1141 realm_dn
, msg
, entry_ex
);
1143 krb5_warnx(context
, "samba_kdc_fetch: trust_message2entry failed");
1150 static krb5_error_code
samba_kdc_lookup_server(krb5_context context
,
1151 struct samba_kdc_db_context
*kdc_db_ctx
,
1152 TALLOC_CTX
*mem_ctx
,
1153 krb5_const_principal principal
,
1155 struct ldb_dn
**realm_dn
,
1156 struct ldb_message
**msg
)
1158 krb5_error_code ret
;
1160 if (principal
->name
.name_string
.len
>= 2) {
1161 /* 'normal server' case */
1164 struct ldb_dn
*user_dn
;
1165 char *principal_string
;
1167 ret
= krb5_unparse_name_flags(context
, principal
,
1168 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
1174 /* At this point we may find the host is known to be
1175 * in a different realm, so we should generate a
1176 * referral instead */
1177 nt_status
= crack_service_principal_name(kdc_db_ctx
->samdb
,
1178 mem_ctx
, principal_string
,
1179 &user_dn
, realm_dn
);
1180 free(principal_string
);
1182 if (!NT_STATUS_IS_OK(nt_status
)) {
1183 return HDB_ERR_NOENTRY
;
1186 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
,
1188 msg
, user_dn
, LDB_SCOPE_BASE
,
1189 attrs
, DSDB_SEARCH_SHOW_EXTENDED_DN
, "(objectClass=*)");
1190 if (ldb_ret
!= LDB_SUCCESS
) {
1191 return HDB_ERR_NOENTRY
;
1196 char *filter
= NULL
;
1198 /* server as client principal case, but we must not lookup userPrincipalNames */
1199 *realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
1200 realm
= krb5_principal_get_realm(context
, principal
);
1202 /* TODO: Check if it is our realm, otherwise give referall */
1204 ret
= krb5_unparse_name_flags(context
, principal
, KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &short_princ
);
1207 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_principal: could not parse principal");
1208 krb5_warnx(context
, "samba_kdc_lookup_principal: could not parse principal");
1212 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
, msg
,
1213 *realm_dn
, LDB_SCOPE_SUBTREE
,
1215 DSDB_SEARCH_SHOW_EXTENDED_DN
,
1216 "(&(objectClass=user)(samAccountName=%s))",
1217 ldb_binary_encode_string(mem_ctx
, short_princ
));
1219 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
1220 DEBUG(3, ("Failed find a entry for %s\n", filter
));
1221 return HDB_ERR_NOENTRY
;
1223 if (lret
!= LDB_SUCCESS
) {
1224 DEBUG(3, ("Failed single search for for %s - %s\n",
1225 filter
, ldb_errstring(kdc_db_ctx
->samdb
)));
1226 return HDB_ERR_NOENTRY
;
1233 static krb5_error_code
samba_kdc_fetch_server(krb5_context context
,
1234 struct samba_kdc_db_context
*kdc_db_ctx
,
1235 TALLOC_CTX
*mem_ctx
,
1236 krb5_const_principal principal
,
1237 hdb_entry_ex
*entry_ex
)
1239 krb5_error_code ret
;
1240 struct ldb_dn
*realm_dn
;
1241 struct ldb_message
*msg
;
1243 ret
= samba_kdc_lookup_server(context
, kdc_db_ctx
, mem_ctx
, principal
,
1244 server_attrs
, &realm_dn
, &msg
);
1249 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1250 principal
, SAMBA_KDC_ENT_TYPE_SERVER
,
1251 realm_dn
, msg
, entry_ex
);
1253 krb5_warnx(context
, "samba_kdc_fetch: message2entry failed");
1259 krb5_error_code
samba_kdc_fetch(krb5_context context
,
1260 struct samba_kdc_db_context
*kdc_db_ctx
,
1261 krb5_const_principal principal
,
1263 hdb_entry_ex
*entry_ex
)
1265 krb5_error_code ret
= HDB_ERR_NOENTRY
;
1266 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_fetch context");
1270 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
1274 if (flags
& HDB_F_GET_CLIENT
) {
1275 ret
= samba_kdc_fetch_client(context
, kdc_db_ctx
, mem_ctx
, principal
, entry_ex
);
1276 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1278 if (flags
& HDB_F_GET_SERVER
) {
1279 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
1280 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, entry_ex
);
1281 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1283 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
1284 ret
= samba_kdc_fetch_server(context
, kdc_db_ctx
, mem_ctx
, principal
, entry_ex
);
1285 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1287 if (flags
& HDB_F_GET_KRBTGT
) {
1288 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, entry_ex
);
1289 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1293 talloc_free(mem_ctx
);
1297 struct samba_kdc_seq
{
1300 struct ldb_message
**msgs
;
1301 struct ldb_dn
*realm_dn
;
1304 static krb5_error_code
samba_kdc_seq(krb5_context context
,
1305 struct samba_kdc_db_context
*kdc_db_ctx
,
1306 hdb_entry_ex
*entry
)
1308 krb5_error_code ret
;
1309 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
1310 TALLOC_CTX
*mem_ctx
;
1311 hdb_entry_ex entry_ex
;
1312 memset(&entry_ex
, '\0', sizeof(entry_ex
));
1315 return HDB_ERR_NOENTRY
;
1318 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_seq context");
1322 krb5_set_error_message(context
, ret
, "samba_kdc_seq: talloc_named() failed!");
1326 if (priv
->index
< priv
->count
) {
1327 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1328 NULL
, SAMBA_KDC_ENT_TYPE_ANY
,
1329 priv
->realm_dn
, priv
->msgs
[priv
->index
++], entry
);
1331 ret
= HDB_ERR_NOENTRY
;
1336 kdc_db_ctx
->seq_ctx
= NULL
;
1338 talloc_free(mem_ctx
);
1344 krb5_error_code
samba_kdc_firstkey(krb5_context context
,
1345 struct samba_kdc_db_context
*kdc_db_ctx
,
1346 hdb_entry_ex
*entry
)
1348 struct ldb_context
*ldb_ctx
= kdc_db_ctx
->samdb
;
1349 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
1351 struct ldb_result
*res
= NULL
;
1352 krb5_error_code ret
;
1353 TALLOC_CTX
*mem_ctx
;
1358 kdc_db_ctx
->seq_ctx
= NULL
;
1361 priv
= (struct samba_kdc_seq
*) talloc(kdc_db_ctx
, struct samba_kdc_seq
);
1364 krb5_set_error_message(context
, ret
, "talloc: out of memory");
1370 priv
->realm_dn
= ldb_get_default_basedn(ldb_ctx
);
1373 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_firstkey context");
1377 krb5_set_error_message(context
, ret
, "samba_kdc_firstkey: talloc_named() failed!");
1381 ret
= krb5_get_default_realm(context
, &realm
);
1387 lret
= ldb_search(ldb_ctx
, priv
, &res
,
1388 priv
->realm_dn
, LDB_SCOPE_SUBTREE
, user_attrs
,
1389 "(objectClass=user)");
1391 if (lret
!= LDB_SUCCESS
) {
1393 return HDB_ERR_NOENTRY
;
1396 priv
->count
= res
->count
;
1397 priv
->msgs
= talloc_steal(priv
, res
->msgs
);
1400 kdc_db_ctx
->seq_ctx
= priv
;
1402 ret
= samba_kdc_seq(context
, kdc_db_ctx
, entry
);
1406 kdc_db_ctx
->seq_ctx
= NULL
;
1408 talloc_free(mem_ctx
);
1413 krb5_error_code
samba_kdc_nextkey(krb5_context context
,
1414 struct samba_kdc_db_context
*kdc_db_ctx
,
1415 hdb_entry_ex
*entry
)
1417 return samba_kdc_seq(context
, kdc_db_ctx
, entry
);
1420 /* Check if a given entry may delegate or do s4u2self to this target principal
1422 * This is currently a very nasty hack - allowing only delegation to itself.
1424 * This is shared between the constrained delegation and S4U2Self code.
1427 samba_kdc_check_identical_client_and_server(krb5_context context
,
1428 struct samba_kdc_db_context
*kdc_db_ctx
,
1429 hdb_entry_ex
*entry
,
1430 krb5_const_principal target_principal
)
1432 krb5_error_code ret
;
1433 krb5_principal enterprise_prinicpal
= NULL
;
1434 struct ldb_dn
*realm_dn
;
1435 struct ldb_message
*msg
;
1436 struct dom_sid
*orig_sid
;
1437 struct dom_sid
*target_sid
;
1438 struct samba_kdc_entry
*p
= talloc_get_type(entry
->ctx
, struct samba_kdc_entry
);
1439 const char *delegation_check_attrs
[] = {
1443 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_constrained_delegation");
1447 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
1451 if (target_principal
->name
.name_type
== KRB5_NT_ENTERPRISE_PRINCIPAL
) {
1452 /* Need to reparse the enterprise principal to find the real target */
1453 if (target_principal
->name
.name_string
.len
!= 1) {
1454 ret
= KRB5_PARSE_MALFORMED
;
1455 krb5_set_error_message(context
, ret
, "samba_kdc_check_constrained_delegation: request for delegation to enterprise principal with wrong (%d) number of components",
1456 target_principal
->name
.name_string
.len
);
1457 talloc_free(mem_ctx
);
1460 ret
= krb5_parse_name(context
, target_principal
->name
.name_string
.val
[0],
1461 &enterprise_prinicpal
);
1463 talloc_free(mem_ctx
);
1466 target_principal
= enterprise_prinicpal
;
1469 ret
= samba_kdc_lookup_server(context
, kdc_db_ctx
, mem_ctx
, target_principal
,
1470 delegation_check_attrs
, &realm_dn
, &msg
);
1472 krb5_free_principal(context
, enterprise_prinicpal
);
1475 talloc_free(mem_ctx
);
1479 orig_sid
= samdb_result_dom_sid(mem_ctx
, p
->msg
, "objectSid");
1480 target_sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
1482 /* Allow delegation to the same principal, even if by a different
1483 * name. The easy and safe way to prove this is by SID
1485 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
1486 talloc_free(mem_ctx
);
1487 return KRB5KDC_ERR_BADOPTION
;
1490 talloc_free(mem_ctx
);
1494 /* Certificates printed by a the Certificate Authority might have a
1495 * slightly different form of the user principal name to that in the
1496 * database. Allow a mismatch where they both refer to the same
1500 samba_kdc_check_pkinit_ms_upn_match(krb5_context context
,
1501 struct samba_kdc_db_context
*kdc_db_ctx
,
1502 hdb_entry_ex
*entry
,
1503 krb5_const_principal certificate_principal
)
1505 krb5_error_code ret
;
1506 struct ldb_dn
*realm_dn
;
1507 struct ldb_message
*msg
;
1508 struct dom_sid
*orig_sid
;
1509 struct dom_sid
*target_sid
;
1510 struct samba_kdc_entry
*p
= talloc_get_type(entry
->ctx
, struct samba_kdc_entry
);
1511 const char *ms_upn_check_attrs
[] = {
1515 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_pkinit_ms_upn_match");
1519 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
1523 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
1524 mem_ctx
, certificate_principal
,
1525 ms_upn_check_attrs
, &realm_dn
, &msg
);
1528 talloc_free(mem_ctx
);
1532 orig_sid
= samdb_result_dom_sid(mem_ctx
, p
->msg
, "objectSid");
1533 target_sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
1535 /* Consider these to be the same principal, even if by a different
1536 * name. The easy and safe way to prove this is by SID
1538 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
1539 talloc_free(mem_ctx
);
1540 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1543 talloc_free(mem_ctx
);