2 Unix SMB/CIFS implementation.
4 Database Glue between Samba and the KDC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2009
7 Copyright (C) Simo Sorce <idra@samba.org> 2010
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "libcli/security/security.h"
26 #include "auth/auth.h"
27 #include "auth/auth_sam.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "dsdb/common/util.h"
30 #include "librpc/gen_ndr/ndr_drsblobs.h"
31 #include "param/param.h"
32 #include "../lib/crypto/md4.h"
33 #include "system/kerberos.h"
34 #include "auth/kerberos/kerberos.h"
36 #include "kdc/samba_kdc.h"
37 #include "kdc/db-glue.h"
39 #define SAMBA_KVNO_GET_KRBTGT(kvno) \
40 ((uint16_t)(((uint32_t)kvno) >> 16))
42 #define SAMBA_KVNO_AND_KRBTGT(kvno, krbtgt) \
43 ((krb5_kvno)((((uint32_t)kvno) & 0xFFFF) | \
44 ((((uint32_t)krbtgt) << 16) & 0xFFFF0000)))
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",
69 static time_t ldb_msg_find_krb5time_ldap_time(struct ldb_message
*msg
, const char *attr
, time_t 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 struct SDBFlags
uf2SDBFlags(krb5_context context
, uint32_t userAccountControl
, enum samba_kdc_ent_type ent_type
)
89 struct SDBFlags flags
= int2SDBFlags(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
) {
134 flags
.locked_out
= 1;
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_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION
) {
163 * this is confusing...
165 * UF_TRUSTED_FOR_DELEGATION
170 * UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION
171 * => trusted_for_delegation
173 flags
.trusted_for_delegation
= 1;
175 if (!(userAccountControl
& UF_NOT_DELEGATED
)) {
176 flags
.forwardable
= 1;
180 if (userAccountControl
& UF_DONT_REQUIRE_PREAUTH
) {
181 flags
.require_preauth
= 0;
183 flags
.require_preauth
= 1;
189 static int samba_kdc_entry_destructor(struct samba_kdc_entry
*p
)
191 if (p
->entry_ex
!= NULL
) {
192 struct sdb_entry_ex
*entry_ex
= p
->entry_ex
;
193 free_sdb_entry(&entry_ex
->entry
);
199 static krb5_error_code
samba_kdc_message2entry_keys(krb5_context context
,
200 struct samba_kdc_db_context
*kdc_db_ctx
,
202 struct ldb_message
*msg
,
205 uint32_t userAccountControl
,
206 enum samba_kdc_ent_type ent_type
,
207 struct sdb_entry_ex
*entry_ex
)
209 krb5_error_code ret
= 0;
210 enum ndr_err_code ndr_err
;
211 struct samr_Password
*hash
;
212 const struct ldb_val
*sc_val
;
213 struct supplementalCredentialsBlob scb
;
214 struct supplementalCredentialsPackage
*scpk
= NULL
;
215 bool newer_keys
= false;
216 struct package_PrimaryKerberosBlob _pkb
;
217 struct package_PrimaryKerberosCtr3
*pkb3
= NULL
;
218 struct package_PrimaryKerberosCtr4
*pkb4
= NULL
;
220 uint16_t allocated_keys
= 0;
221 int rodc_krbtgt_number
= 0;
223 uint32_t supported_enctypes
224 = ldb_msg_find_attr_as_uint(msg
,
225 "msDS-SupportedEncryptionTypes",
228 if (rid
== DOMAIN_RID_KRBTGT
|| is_rodc
) {
229 /* KDCs (and KDCs on RODCs) use AES */
230 supported_enctypes
|= ENC_HMAC_SHA1_96_AES128
| ENC_HMAC_SHA1_96_AES256
;
231 } else if (userAccountControl
& (UF_PARTIAL_SECRETS_ACCOUNT
|UF_SERVER_TRUST_ACCOUNT
)) {
232 /* DCs and RODCs comptuer accounts use AES */
233 supported_enctypes
|= ENC_HMAC_SHA1_96_AES128
| ENC_HMAC_SHA1_96_AES256
;
234 } else if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
||
235 (ent_type
== SAMBA_KDC_ENT_TYPE_ANY
)) {
236 /* for AS-REQ the client chooses the enc types it
237 * supports, and this will vary between computers a
240 * likewise for 'any' return as much as is supported,
241 * to export into a keytab */
242 supported_enctypes
= ENC_ALL_TYPES
;
245 /* If UF_USE_DES_KEY_ONLY has been set, then don't allow use of the newer enc types */
246 if (userAccountControl
& UF_USE_DES_KEY_ONLY
) {
247 supported_enctypes
= ENC_CRC32
|ENC_RSA_MD5
;
249 /* Otherwise, add in the default enc types */
250 supported_enctypes
|= ENC_CRC32
| ENC_RSA_MD5
| ENC_RC4_HMAC_MD5
;
253 /* Is this the krbtgt or a RODC krbtgt */
255 rodc_krbtgt_number
= ldb_msg_find_attr_as_int(msg
, "msDS-SecondaryKrbTgtNumber", -1);
257 if (rodc_krbtgt_number
== -1) {
262 entry_ex
->entry
.keys
.val
= NULL
;
263 entry_ex
->entry
.keys
.len
= 0;
265 kvno
= ldb_msg_find_attr_as_int(msg
, "msDS-KeyVersionNumber", 0);
267 kvno
= SAMBA_KVNO_AND_KRBTGT(kvno
, rodc_krbtgt_number
);
269 entry_ex
->entry
.kvno
= kvno
;
271 /* Get keys from the db */
273 hash
= samdb_result_hash(mem_ctx
, msg
, "unicodePwd");
274 sc_val
= ldb_msg_find_ldb_val(msg
, "supplementalCredentials");
276 /* unicodePwd for enctype 0x17 (23) if present */
281 /* supplementalCredentials if present */
283 ndr_err
= ndr_pull_struct_blob_all(sc_val
, mem_ctx
, &scb
,
284 (ndr_pull_flags_fn_t
)ndr_pull_supplementalCredentialsBlob
);
285 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
286 dump_data(0, sc_val
->data
, sc_val
->length
);
291 if (scb
.sub
.signature
!= SUPPLEMENTAL_CREDENTIALS_SIGNATURE
) {
292 NDR_PRINT_DEBUG(supplementalCredentialsBlob
, &scb
);
297 for (i
=0; i
< scb
.sub
.num_packages
; i
++) {
298 if (strcmp("Primary:Kerberos-Newer-Keys", scb
.sub
.packages
[i
].name
) == 0) {
299 scpk
= &scb
.sub
.packages
[i
];
300 if (!scpk
->data
|| !scpk
->data
[0]) {
306 } else if (strcmp("Primary:Kerberos", scb
.sub
.packages
[i
].name
) == 0) {
307 scpk
= &scb
.sub
.packages
[i
];
308 if (!scpk
->data
|| !scpk
->data
[0]) {
312 * we don't break here in hope to find
313 * a Kerberos-Newer-Keys package
319 * Primary:Kerberos-Newer-Keys or Primary:Kerberos element
320 * of supplementalCredentials
325 blob
= strhex_to_data_blob(mem_ctx
, scpk
->data
);
331 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
332 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, &_pkb
,
333 (ndr_pull_flags_fn_t
)ndr_pull_package_PrimaryKerberosBlob
);
334 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
336 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
337 krb5_warnx(context
, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
341 if (newer_keys
&& _pkb
.version
!= 4) {
343 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
344 krb5_warnx(context
, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
348 if (!newer_keys
&& _pkb
.version
!= 3) {
350 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
351 krb5_warnx(context
, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
355 if (_pkb
.version
== 4) {
356 pkb4
= &_pkb
.ctr
.ctr4
;
357 allocated_keys
+= pkb4
->num_keys
;
358 } else if (_pkb
.version
== 3) {
359 pkb3
= &_pkb
.ctr
.ctr3
;
360 allocated_keys
+= pkb3
->num_keys
;
364 if (allocated_keys
== 0) {
365 if (kdc_db_ctx
->rodc
) {
366 /* We are on an RODC, but don't have keys for this account. Signal this to the caller */
367 /* TODO: We need to call a generalised version of auth_sam_trigger_repl_secret from here */
368 return SDB_ERR_NOT_FOUND_HERE
;
371 /* oh, no password. Apparently (comment in
372 * hdb-ldap.c) this violates the ASN.1, but this
373 * allows an entry with no keys (yet). */
377 /* allocate space to decode into */
378 entry_ex
->entry
.keys
.len
= 0;
379 entry_ex
->entry
.keys
.val
= calloc(allocated_keys
, sizeof(struct sdb_key
));
380 if (entry_ex
->entry
.keys
.val
== NULL
) {
385 if (hash
&& (supported_enctypes
& ENC_RC4_HMAC_MD5
)) {
386 struct sdb_key key
= {};
388 ret
= smb_krb5_keyblock_init_contents(context
,
389 ENCTYPE_ARCFOUR_HMAC
,
397 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
398 entry_ex
->entry
.keys
.len
++;
402 for (i
=0; i
< pkb4
->num_keys
; i
++) {
403 struct sdb_key key
= {};
405 if (!pkb4
->keys
[i
].value
) continue;
407 if (!(kerberos_enctype_to_bitmap(pkb4
->keys
[i
].keytype
) & supported_enctypes
)) {
411 if (pkb4
->salt
.string
) {
414 salt
= data_blob_string_const(pkb4
->salt
.string
);
416 key
.salt
= calloc(1, sizeof(*key
.salt
));
417 if (key
.salt
== NULL
) {
422 key
.salt
->type
= KRB5_PW_SALT
;
424 ret
= krb5_copy_data_contents(&key
.salt
->salt
,
434 /* TODO: maybe pass the iteration_count somehow... */
436 ret
= smb_krb5_keyblock_init_contents(context
,
437 pkb4
->keys
[i
].keytype
,
438 pkb4
->keys
[i
].value
->data
,
439 pkb4
->keys
[i
].value
->length
,
441 if (ret
== KRB5_PROG_ETYPE_NOSUPP
) {
442 DEBUG(2,("Unsupported keytype ignored - type %u\n",
443 pkb4
->keys
[i
].keytype
));
449 kerberos_free_data_contents(context
, &key
.salt
->salt
);
456 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
457 entry_ex
->entry
.keys
.len
++;
460 for (i
=0; i
< pkb3
->num_keys
; i
++) {
461 struct sdb_key key
= {};
463 if (!pkb3
->keys
[i
].value
) continue;
465 if (!(kerberos_enctype_to_bitmap(pkb3
->keys
[i
].keytype
) & supported_enctypes
)) {
469 if (pkb3
->salt
.string
) {
472 salt
= data_blob_string_const(pkb3
->salt
.string
);
474 key
.salt
= calloc(1, sizeof(*key
.salt
));
475 if (key
.salt
== NULL
) {
480 key
.salt
->type
= KRB5_PW_SALT
;
482 ret
= krb5_copy_data_contents(&key
.salt
->salt
,
492 ret
= smb_krb5_keyblock_init_contents(context
,
493 pkb3
->keys
[i
].keytype
,
494 pkb3
->keys
[i
].value
->data
,
495 pkb3
->keys
[i
].value
->length
,
499 kerberos_free_data_contents(context
, &key
.salt
->salt
);
506 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
507 entry_ex
->entry
.keys
.len
++;
513 entry_ex
->entry
.keys
.len
= 0;
515 if (entry_ex
->entry
.keys
.len
== 0 && entry_ex
->entry
.keys
.val
) {
516 free(entry_ex
->entry
.keys
.val
);
517 entry_ex
->entry
.keys
.val
= NULL
;
522 static int principal_comp_strcmp_int(krb5_context context
,
523 krb5_const_principal principal
,
524 unsigned int component
,
531 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
532 p
= krb5_principal_get_comp_string(context
, principal
, component
);
539 if (component
>= krb5_princ_size(context
, principal
)) {
543 d
= krb5_princ_component(context
, principal
, component
);
552 return strncasecmp(p
, string
, len
);
554 return strncmp(p
, string
, len
);
558 static int principal_comp_strcasecmp(krb5_context context
,
559 krb5_const_principal principal
,
560 unsigned int component
,
563 return principal_comp_strcmp_int(context
, principal
,
564 component
, string
, true);
567 static int principal_comp_strcmp(krb5_context context
,
568 krb5_const_principal principal
,
569 unsigned int component
,
572 return principal_comp_strcmp_int(context
, principal
,
573 component
, string
, false);
577 * Construct an hdb_entry from a directory entry.
579 static krb5_error_code
samba_kdc_message2entry(krb5_context context
,
580 struct samba_kdc_db_context
*kdc_db_ctx
,
582 krb5_const_principal principal
,
583 enum samba_kdc_ent_type ent_type
,
585 struct ldb_dn
*realm_dn
,
586 struct ldb_message
*msg
,
587 struct sdb_entry_ex
*entry_ex
)
589 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
590 uint32_t userAccountControl
;
591 uint32_t msDS_User_Account_Control_Computed
;
593 krb5_error_code ret
= 0;
594 krb5_boolean is_computer
= FALSE
;
596 struct samba_kdc_entry
*p
;
601 bool is_rodc
= false;
602 struct ldb_message_element
*objectclasses
;
603 struct ldb_val computer_val
;
604 const char *samAccountName
= ldb_msg_find_attr_as_string(msg
, "samAccountName", NULL
);
605 computer_val
.data
= discard_const_p(uint8_t,"computer");
606 computer_val
.length
= strlen((const char *)computer_val
.data
);
608 if (ldb_msg_find_element(msg
, "msDS-SecondaryKrbTgtNumber")) {
612 if (!samAccountName
) {
614 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: no samAccountName present");
618 objectclasses
= ldb_msg_find_element(msg
, "objectClass");
620 if (objectclasses
&& ldb_msg_find_val(objectclasses
, &computer_val
)) {
624 ZERO_STRUCTP(entry_ex
);
626 p
= talloc_zero(mem_ctx
, struct samba_kdc_entry
);
632 p
->kdc_db_ctx
= kdc_db_ctx
;
633 p
->realm_dn
= talloc_reference(p
, realm_dn
);
639 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
643 userAccountControl
= ldb_msg_find_attr_as_uint(msg
, "userAccountControl", 0);
645 msDS_User_Account_Control_Computed
646 = ldb_msg_find_attr_as_uint(msg
,
647 "msDS-User-Account-Control-Computed",
651 * This brings in the lockout flag, block the account if not
652 * found. We need the weird UF_ACCOUNTDISABLE check because
653 * we do not want to fail open if the value is not returned,
654 * but 0 is a valid value (all OK)
656 if (msDS_User_Account_Control_Computed
== UF_ACCOUNTDISABLE
) {
658 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: "
659 "no msDS-User-Account-Control-Computed present");
662 userAccountControl
|= msDS_User_Account_Control_Computed
;
666 * If we are set to canonicalize, we get back the fixed UPPER
667 * case realm, and the real username (ie matching LDAP
670 * Otherwise, if we are set to enterprise, we
671 * get back the whole principal as-sent
673 * Finally, if we are not set to canonicalize, we get back the
674 * fixed UPPER case realm, but the as-sent username
677 if (ent_type
== SAMBA_KDC_ENT_TYPE_KRBTGT
) {
678 if (flags
& (SDB_F_CANON
)) {
680 * When requested to do so, ensure that the
681 * both realm values in the principal are set
682 * to the upper case, canonical realm
684 ret
= smb_krb5_make_principal(context
, &entry_ex
->entry
.principal
,
685 lpcfg_realm(lp_ctx
), "krbtgt",
686 lpcfg_realm(lp_ctx
), NULL
);
688 krb5_clear_error_message(context
);
691 smb_krb5_principal_set_type(context
, entry_ex
->entry
.principal
, KRB5_NT_SRV_INST
);
693 ret
= krb5_copy_principal(context
, principal
, &entry_ex
->entry
.principal
);
695 krb5_clear_error_message(context
);
699 * this appears to be required regardless of
700 * the canonicalize flag from the client
702 ret
= smb_krb5_principal_set_realm(context
, entry_ex
->entry
.principal
, lpcfg_realm(lp_ctx
));
704 krb5_clear_error_message(context
);
709 } else if (ent_type
== SAMBA_KDC_ENT_TYPE_ANY
&& principal
== NULL
) {
710 ret
= smb_krb5_make_principal(context
, &entry_ex
->entry
.principal
, lpcfg_realm(lp_ctx
), samAccountName
, NULL
);
712 krb5_clear_error_message(context
);
715 } else if (flags
& SDB_F_CANON
&& flags
& SDB_F_FOR_AS_REQ
) {
717 * SDB_F_CANON maps from the canonicalize flag in the
718 * packet, and has a different meaning between AS-REQ
719 * and TGS-REQ. We only change the principal in the AS-REQ case
721 ret
= smb_krb5_make_principal(context
, &entry_ex
->entry
.principal
, lpcfg_realm(lp_ctx
), samAccountName
, NULL
);
723 krb5_clear_error_message(context
);
727 ret
= krb5_copy_principal(context
, principal
, &entry_ex
->entry
.principal
);
729 krb5_clear_error_message(context
);
733 if (smb_krb5_principal_get_type(context
, principal
) != KRB5_NT_ENTERPRISE_PRINCIPAL
) {
734 /* While we have copied the client principal, tests
735 * show that Win2k3 returns the 'corrected' realm, not
736 * the client-specified realm. This code attempts to
737 * replace the client principal's realm with the one
738 * we determine from our records */
740 /* this has to be with malloc() */
741 ret
= smb_krb5_principal_set_realm(context
, entry_ex
->entry
.principal
, lpcfg_realm(lp_ctx
));
743 krb5_clear_error_message(context
);
749 /* First try and figure out the flags based on the userAccountControl */
750 entry_ex
->entry
.flags
= uf2SDBFlags(context
, userAccountControl
, ent_type
);
752 /* Windows 2008 seems to enforce this (very sensible) rule by
753 * default - don't allow offline attacks on a user's password
754 * by asking for a ticket to them as a service (encrypted with
755 * their probably patheticly insecure password) */
757 if (entry_ex
->entry
.flags
.server
758 && lpcfg_parm_bool(lp_ctx
, NULL
, "kdc", "require spn for service", true)) {
759 if (!is_computer
&& !ldb_msg_find_attr_as_string(msg
, "servicePrincipalName", NULL
)) {
760 entry_ex
->entry
.flags
.server
= 0;
764 * To give the correct type of error to the client, we must
765 * not just return the entry without .server set, we must
766 * pretend the principal does not exist. Otherwise we may
767 * return ERR_POLICY instead of
768 * KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
770 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
&& entry_ex
->entry
.flags
.server
== 0) {
771 ret
= SDB_ERR_NOENTRY
;
772 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: no servicePrincipalName present for this server, refusing with no-such-entry");
775 if (flags
& SDB_F_ADMIN_DATA
) {
776 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
777 * of the Heimdal KDC. They are stored in a the traditional
778 * DB for audit purposes, and still form part of the structure
781 /* use 'whenCreated' */
782 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
783 /* use 'kadmin' for now (needed by mit_samba) */
785 ret
= smb_krb5_make_principal(context
,
786 &entry_ex
->entry
.created_by
.principal
,
787 lpcfg_realm(lp_ctx
), "kadmin", NULL
);
789 krb5_clear_error_message(context
);
793 entry_ex
->entry
.modified_by
= (struct sdb_event
*) malloc(sizeof(struct sdb_event
));
794 if (entry_ex
->entry
.modified_by
== NULL
) {
796 krb5_set_error_message(context
, ret
, "malloc: out of memory");
800 /* use 'whenChanged' */
801 entry_ex
->entry
.modified_by
->time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenChanged", 0);
802 /* use 'kadmin' for now (needed by mit_samba) */
803 ret
= smb_krb5_make_principal(context
,
804 &entry_ex
->entry
.modified_by
->principal
,
805 lpcfg_realm(lp_ctx
), "kadmin", NULL
);
807 krb5_clear_error_message(context
);
813 /* The lack of password controls etc applies to krbtgt by
814 * virtue of being that particular RID */
815 status
= dom_sid_split_rid(NULL
, samdb_result_dom_sid(mem_ctx
, msg
, "objectSid"), NULL
, &rid
);
817 if (!NT_STATUS_IS_OK(status
)) {
822 if (rid
== DOMAIN_RID_KRBTGT
) {
825 entry_ex
->entry
.valid_end
= NULL
;
826 entry_ex
->entry
.pw_end
= NULL
;
828 entry_ex
->entry
.flags
.invalid
= 0;
829 entry_ex
->entry
.flags
.server
= 1;
831 realm
= smb_krb5_principal_get_realm(context
, principal
);
837 /* Don't mark all requests for the krbtgt/realm as
838 * 'change password', as otherwise we could get into
839 * trouble, and not enforce the password expirty.
840 * Instead, only do it when request is for the kpasswd service */
841 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
842 && krb5_princ_size(context
, principal
) == 2
843 && (principal_comp_strcmp(context
, principal
, 0, "kadmin") == 0)
844 && (principal_comp_strcmp(context
, principal
, 1, "changepw") == 0)
845 && lpcfg_is_my_domain_or_realm(lp_ctx
, realm
)) {
846 entry_ex
->entry
.flags
.change_pw
= 1;
851 entry_ex
->entry
.flags
.client
= 0;
852 entry_ex
->entry
.flags
.forwardable
= 1;
853 entry_ex
->entry
.flags
.ok_as_delegate
= 1;
854 } else if (is_rodc
) {
855 /* The RODC krbtgt account is like the main krbtgt,
856 * but it does not have a changepw or kadmin
859 entry_ex
->entry
.valid_end
= NULL
;
860 entry_ex
->entry
.pw_end
= NULL
;
862 /* Also don't allow the RODC krbtgt to be a client (it should not be needed) */
863 entry_ex
->entry
.flags
.client
= 0;
864 entry_ex
->entry
.flags
.invalid
= 0;
865 entry_ex
->entry
.flags
.server
= 1;
867 entry_ex
->entry
.flags
.client
= 0;
868 entry_ex
->entry
.flags
.forwardable
= 1;
869 entry_ex
->entry
.flags
.ok_as_delegate
= 0;
870 } else if (entry_ex
->entry
.flags
.server
&& ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
871 /* The account/password expiry only applies when the account is used as a
872 * client (ie password login), not when used as a server */
874 /* Make very well sure we don't use this for a client,
875 * it could bypass the password restrictions */
876 entry_ex
->entry
.flags
.client
= 0;
878 entry_ex
->entry
.valid_end
= NULL
;
879 entry_ex
->entry
.pw_end
= NULL
;
882 NTTIME must_change_time
883 = samdb_result_force_password_change(kdc_db_ctx
->samdb
, mem_ctx
,
885 if (must_change_time
== 0x7FFFFFFFFFFFFFFFULL
) {
886 entry_ex
->entry
.pw_end
= NULL
;
888 entry_ex
->entry
.pw_end
= malloc(sizeof(*entry_ex
->entry
.pw_end
));
889 if (entry_ex
->entry
.pw_end
== NULL
) {
893 *entry_ex
->entry
.pw_end
= nt_time_to_unix(must_change_time
);
896 acct_expiry
= samdb_result_account_expires(msg
);
897 if (acct_expiry
== 0x7FFFFFFFFFFFFFFFULL
) {
898 entry_ex
->entry
.valid_end
= NULL
;
900 entry_ex
->entry
.valid_end
= malloc(sizeof(*entry_ex
->entry
.valid_end
));
901 if (entry_ex
->entry
.valid_end
== NULL
) {
905 *entry_ex
->entry
.valid_end
= nt_time_to_unix(acct_expiry
);
909 entry_ex
->entry
.valid_start
= NULL
;
911 entry_ex
->entry
.max_life
= malloc(sizeof(*entry_ex
->entry
.max_life
));
912 if (entry_ex
->entry
.max_life
== NULL
) {
917 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
918 *entry_ex
->entry
.max_life
= kdc_db_ctx
->policy
.svc_tkt_lifetime
;
919 } else if (ent_type
== SAMBA_KDC_ENT_TYPE_KRBTGT
|| ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
) {
920 *entry_ex
->entry
.max_life
= kdc_db_ctx
->policy
.usr_tkt_lifetime
;
922 *entry_ex
->entry
.max_life
= MIN(kdc_db_ctx
->policy
.svc_tkt_lifetime
,
923 kdc_db_ctx
->policy
.usr_tkt_lifetime
);
926 entry_ex
->entry
.max_renew
= malloc(sizeof(*entry_ex
->entry
.max_life
));
927 if (entry_ex
->entry
.max_renew
== NULL
) {
932 *entry_ex
->entry
.max_renew
= kdc_db_ctx
->policy
.renewal_lifetime
;
934 /* Get keys from the db */
935 ret
= samba_kdc_message2entry_keys(context
, kdc_db_ctx
, p
, msg
,
936 rid
, is_rodc
, userAccountControl
,
939 /* Could be bogus data in the entry, or out of memory */
943 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
944 if (entry_ex
->entry
.etypes
== NULL
) {
945 krb5_clear_error_message(context
);
949 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
950 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
951 if (entry_ex
->entry
.etypes
->val
== NULL
) {
952 krb5_clear_error_message(context
);
956 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
957 entry_ex
->entry
.etypes
->val
[i
] = KRB5_KEY_TYPE(&entry_ex
->entry
.keys
.val
[i
].key
);
961 p
->msg
= talloc_steal(p
, msg
);
965 /* This doesn't free ent itself, that is for the eventual caller to do */
966 sdb_free_entry(entry_ex
);
967 ZERO_STRUCTP(entry_ex
);
969 talloc_steal(kdc_db_ctx
, entry_ex
->ctx
);
976 * Construct an hdb_entry from a directory entry.
977 * The kvno is what the remote client asked for
979 static krb5_error_code
samba_kdc_trust_message2entry(krb5_context context
,
980 struct samba_kdc_db_context
*kdc_db_ctx
,
981 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
982 enum trust_direction direction
,
983 struct ldb_dn
*realm_dn
,
986 struct ldb_message
*msg
,
987 struct sdb_entry_ex
*entry_ex
)
989 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
990 const char *our_realm
= lpcfg_realm(lp_ctx
);
991 const char *dnsdomain
= NULL
;
992 char *partner_realm
= NULL
;
993 const char *realm
= NULL
;
994 const char *krbtgt_realm
= NULL
;
995 DATA_BLOB password_utf16
= data_blob_null
;
996 DATA_BLOB password_utf8
= data_blob_null
;
997 struct samr_Password _password_hash
;
998 const struct samr_Password
*password_hash
= NULL
;
999 const struct ldb_val
*password_val
;
1000 struct trustAuthInOutBlob password_blob
;
1001 struct samba_kdc_entry
*p
;
1002 bool use_previous
= false;
1003 uint32_t current_kvno
;
1004 uint32_t previous_kvno
;
1005 uint32_t num_keys
= 0;
1006 enum ndr_err_code ndr_err
;
1007 int ret
, trust_direction_flags
;
1009 struct AuthenticationInformationArray
*auth_array
;
1012 uint32_t *auth_kvno
;
1013 bool preferr_current
= false;
1014 uint32_t supported_enctypes
= ENC_RC4_HMAC_MD5
;
1016 if (dsdb_functional_level(kdc_db_ctx
->samdb
) >= DS_DOMAIN_FUNCTION_2008
) {
1017 supported_enctypes
= ldb_msg_find_attr_as_uint(msg
,
1018 "msDS-SupportedEncryptionTypes",
1019 supported_enctypes
);
1022 trust_direction_flags
= ldb_msg_find_attr_as_int(msg
, "trustDirection", 0);
1023 if (!(trust_direction_flags
& direction
)) {
1024 krb5_clear_error_message(context
);
1025 ret
= SDB_ERR_NOENTRY
;
1029 dnsdomain
= ldb_msg_find_attr_as_string(msg
, "trustPartner", NULL
);
1030 if (dnsdomain
== NULL
) {
1031 krb5_clear_error_message(context
);
1032 ret
= SDB_ERR_NOENTRY
;
1035 partner_realm
= strupper_talloc(mem_ctx
, dnsdomain
);
1036 if (partner_realm
== NULL
) {
1037 krb5_clear_error_message(context
);
1042 if (direction
== INBOUND
) {
1044 krbtgt_realm
= partner_realm
;
1046 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthIncoming");
1047 } else { /* OUTBOUND */
1048 realm
= partner_realm
;
1049 krbtgt_realm
= our_realm
;
1051 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthOutgoing");
1054 if (password_val
== NULL
) {
1055 krb5_clear_error_message(context
);
1056 ret
= SDB_ERR_NOENTRY
;
1060 ndr_err
= ndr_pull_struct_blob(password_val
, mem_ctx
, &password_blob
,
1061 (ndr_pull_flags_fn_t
)ndr_pull_trustAuthInOutBlob
);
1062 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1063 krb5_clear_error_message(context
);
1068 p
= talloc(mem_ctx
, struct samba_kdc_entry
);
1074 p
->kdc_db_ctx
= kdc_db_ctx
;
1075 p
->realm_dn
= realm_dn
;
1077 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
1079 /* make sure we do not have bogus data in there */
1080 memset(&entry_ex
->entry
, 0, sizeof(struct sdb_entry
));
1084 /* use 'whenCreated' */
1085 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
1086 /* use 'kadmin' for now (needed by mit_samba) */
1087 ret
= smb_krb5_make_principal(context
,
1088 &entry_ex
->entry
.created_by
.principal
,
1089 realm
, "kadmin", NULL
);
1091 krb5_clear_error_message(context
);
1096 * We always need to generate the canonicalized principal
1097 * with the values of our database.
1099 ret
= smb_krb5_make_principal(context
, &entry_ex
->entry
.principal
, realm
,
1100 "krbtgt", krbtgt_realm
, NULL
);
1102 krb5_clear_error_message(context
);
1105 smb_krb5_principal_set_type(context
, entry_ex
->entry
.principal
,
1108 entry_ex
->entry
.valid_start
= NULL
;
1110 /* we need to work out if we are going to use the current or
1111 * the previous password hash.
1112 * We base this on the kvno the client passes in. If the kvno
1113 * passed in is equal to the current kvno in our database then
1114 * we use the current structure. If it is the current kvno-1,
1115 * then we use the previous substrucure.
1119 * Windows preferrs the previous key for one hour.
1121 tv
= timeval_current();
1122 if (tv
.tv_sec
> 3600) {
1125 an_hour_ago
= timeval_to_nttime(&tv
);
1127 /* first work out the current kvno */
1129 for (i
=0; i
< password_blob
.count
; i
++) {
1130 struct AuthenticationInformation
*a
=
1131 &password_blob
.current
.array
[i
];
1133 if (a
->LastUpdateTime
<= an_hour_ago
) {
1134 preferr_current
= true;
1137 if (a
->AuthType
== TRUST_AUTH_TYPE_VERSION
) {
1138 current_kvno
= a
->AuthInfo
.version
.version
;
1141 if (current_kvno
== 0) {
1142 previous_kvno
= 255;
1144 previous_kvno
= current_kvno
- 1;
1146 for (i
=0; i
< password_blob
.count
; i
++) {
1147 struct AuthenticationInformation
*a
=
1148 &password_blob
.previous
.array
[i
];
1150 if (a
->AuthType
== TRUST_AUTH_TYPE_VERSION
) {
1151 previous_kvno
= a
->AuthInfo
.version
.version
;
1155 /* work out whether we will use the previous or current
1157 if (password_blob
.previous
.count
== 0) {
1158 /* there is no previous password */
1159 use_previous
= false;
1160 } else if (!(flags
& SDB_F_KVNO_SPECIFIED
)) {
1162 * If not specified we use the lowest kvno
1163 * for the first hour after an update.
1165 if (preferr_current
) {
1166 use_previous
= false;
1167 } else if (previous_kvno
< current_kvno
) {
1168 use_previous
= true;
1170 use_previous
= false;
1172 } else if (kvno
== current_kvno
) {
1176 use_previous
= false;
1177 } else if (kvno
== previous_kvno
) {
1181 use_previous
= true;
1184 * Fallback to the current one for anything else
1186 use_previous
= false;
1190 auth_array
= &password_blob
.previous
;
1191 auth_kvno
= &previous_kvno
;
1193 auth_array
= &password_blob
.current
;
1194 auth_kvno
= ¤t_kvno
;
1197 /* use the kvno the client specified, if available */
1198 if (flags
& SDB_F_KVNO_SPECIFIED
) {
1199 entry_ex
->entry
.kvno
= kvno
;
1201 entry_ex
->entry
.kvno
= *auth_kvno
;
1204 for (i
=0; i
< auth_array
->count
; i
++) {
1205 if (auth_array
->array
[i
].AuthType
== TRUST_AUTH_TYPE_CLEAR
) {
1208 password_utf16
= data_blob_const(auth_array
->array
[i
].AuthInfo
.clear
.password
,
1209 auth_array
->array
[i
].AuthInfo
.clear
.size
);
1210 if (password_utf16
.length
== 0) {
1214 if (supported_enctypes
& ENC_RC4_HMAC_MD5
) {
1215 mdfour(_password_hash
.hash
, password_utf16
.data
, password_utf16
.length
);
1216 if (password_hash
== NULL
) {
1219 password_hash
= &_password_hash
;
1222 if (!(supported_enctypes
& (ENC_HMAC_SHA1_96_AES128
|ENC_HMAC_SHA1_96_AES256
))) {
1226 ok
= convert_string_talloc(mem_ctx
,
1227 CH_UTF16MUNGED
, CH_UTF8
,
1228 password_utf16
.data
,
1229 password_utf16
.length
,
1230 (void *)&password_utf8
.data
,
1231 &password_utf8
.length
);
1233 krb5_clear_error_message(context
);
1238 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES128
) {
1241 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES256
) {
1245 } else if (auth_array
->array
[i
].AuthType
== TRUST_AUTH_TYPE_NT4OWF
) {
1246 if (supported_enctypes
& ENC_RC4_HMAC_MD5
) {
1247 password_hash
= &auth_array
->array
[i
].AuthInfo
.nt4owf
.password
;
1253 /* Must have found a cleartext or MD4 password */
1254 if (num_keys
== 0) {
1255 DEBUG(1,(__location__
": no usable key found\n"));
1256 krb5_clear_error_message(context
);
1257 ret
= SDB_ERR_NOENTRY
;
1261 entry_ex
->entry
.keys
.val
= calloc(num_keys
, sizeof(struct sdb_key
));
1262 if (entry_ex
->entry
.keys
.val
== NULL
) {
1263 krb5_clear_error_message(context
);
1268 if (password_utf8
.length
!= 0) {
1269 struct sdb_key key
= {};
1270 krb5_const_principal salt_principal
= entry_ex
->entry
.principal
;
1272 krb5_data cleartext_data
;
1274 cleartext_data
.data
= discard_const_p(char, password_utf8
.data
);
1275 cleartext_data
.length
= password_utf8
.length
;
1277 ret
= smb_krb5_get_pw_salt(context
,
1284 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES256
) {
1285 ret
= smb_krb5_create_key_from_string(context
,
1289 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
1292 kerberos_free_data_contents(context
, &salt
);
1296 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
1297 entry_ex
->entry
.keys
.len
++;
1300 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES128
) {
1301 ret
= smb_krb5_create_key_from_string(context
,
1305 ENCTYPE_AES128_CTS_HMAC_SHA1_96
,
1308 kerberos_free_data_contents(context
, &salt
);
1312 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
1313 entry_ex
->entry
.keys
.len
++;
1316 kerberos_free_data_contents(context
, &salt
);
1319 if (password_hash
!= NULL
) {
1320 struct sdb_key key
= {};
1322 ret
= smb_krb5_keyblock_init_contents(context
,
1323 ENCTYPE_ARCFOUR_HMAC
,
1324 password_hash
->hash
,
1325 sizeof(password_hash
->hash
),
1331 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
1332 entry_ex
->entry
.keys
.len
++;
1335 entry_ex
->entry
.flags
= int2SDBFlags(0);
1336 entry_ex
->entry
.flags
.immutable
= 1;
1337 entry_ex
->entry
.flags
.invalid
= 0;
1338 entry_ex
->entry
.flags
.server
= 1;
1339 entry_ex
->entry
.flags
.require_preauth
= 1;
1341 entry_ex
->entry
.pw_end
= NULL
;
1343 entry_ex
->entry
.max_life
= NULL
;
1345 entry_ex
->entry
.max_renew
= NULL
;
1347 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
1348 if (entry_ex
->entry
.etypes
== NULL
) {
1349 krb5_clear_error_message(context
);
1353 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
1354 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
1355 if (entry_ex
->entry
.etypes
->val
== NULL
) {
1356 krb5_clear_error_message(context
);
1360 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
1361 entry_ex
->entry
.etypes
->val
[i
] = KRB5_KEY_TYPE(&entry_ex
->entry
.keys
.val
[i
].key
);
1364 p
->msg
= talloc_steal(p
, msg
);
1367 TALLOC_FREE(partner_realm
);
1370 /* This doesn't free ent itself, that is for the eventual caller to do */
1371 sdb_free_entry(entry_ex
);
1373 talloc_steal(kdc_db_ctx
, entry_ex
->ctx
);
1380 static krb5_error_code
samba_kdc_lookup_trust(krb5_context context
, struct ldb_context
*ldb_ctx
,
1381 TALLOC_CTX
*mem_ctx
,
1383 struct ldb_dn
*realm_dn
,
1384 struct ldb_message
**pmsg
)
1387 const char * const *attrs
= trust_attrs
;
1389 status
= dsdb_trust_search_tdo(ldb_ctx
, realm
, realm
,
1390 attrs
, mem_ctx
, pmsg
);
1391 if (NT_STATUS_IS_OK(status
)) {
1393 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
1394 return SDB_ERR_NOENTRY
;
1395 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MEMORY
)) {
1397 krb5_set_error_message(context
, ret
, "get_sam_result_trust: out of memory");
1401 krb5_set_error_message(context
, ret
, "get_sam_result_trust: %s", nt_errstr(status
));
1406 static krb5_error_code
samba_kdc_lookup_client(krb5_context context
,
1407 struct samba_kdc_db_context
*kdc_db_ctx
,
1408 TALLOC_CTX
*mem_ctx
,
1409 krb5_const_principal principal
,
1411 struct ldb_dn
**realm_dn
,
1412 struct ldb_message
**msg
)
1415 char *principal_string
= NULL
;
1417 if (smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
1418 principal_string
= smb_krb5_principal_get_comp_string(mem_ctx
, context
,
1420 if (principal_string
== NULL
) {
1424 char *principal_string_m
= NULL
;
1425 krb5_error_code ret
;
1427 ret
= krb5_unparse_name(context
, principal
, &principal_string_m
);
1432 principal_string
= talloc_strdup(mem_ctx
, principal_string_m
);
1433 SAFE_FREE(principal_string_m
);
1434 if (principal_string
== NULL
) {
1439 nt_status
= sam_get_results_principal(kdc_db_ctx
->samdb
,
1440 mem_ctx
, principal_string
, attrs
,
1442 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
1443 krb5_principal fallback_principal
= NULL
;
1444 unsigned int num_comp
;
1445 char *fallback_realm
= NULL
;
1446 char *fallback_account
= NULL
;
1447 krb5_error_code ret
;
1449 ret
= krb5_parse_name(context
, principal_string
,
1450 &fallback_principal
);
1451 TALLOC_FREE(principal_string
);
1456 num_comp
= krb5_princ_size(context
, fallback_principal
);
1457 fallback_realm
= smb_krb5_principal_get_realm(context
,
1458 fallback_principal
);
1459 if (fallback_realm
== NULL
) {
1460 krb5_free_principal(context
, fallback_principal
);
1464 if (num_comp
== 1) {
1467 fallback_account
= smb_krb5_principal_get_comp_string(mem_ctx
,
1468 context
, fallback_principal
, 0);
1469 if (fallback_account
== NULL
) {
1470 krb5_free_principal(context
, fallback_principal
);
1471 SAFE_FREE(fallback_realm
);
1475 len
= strlen(fallback_account
);
1476 if (len
>= 2 && fallback_account
[len
- 1] == '$') {
1477 TALLOC_FREE(fallback_account
);
1480 krb5_free_principal(context
, fallback_principal
);
1481 fallback_principal
= NULL
;
1483 if (fallback_account
!= NULL
) {
1486 with_dollar
= talloc_asprintf(mem_ctx
, "%s$",
1488 if (with_dollar
== NULL
) {
1489 SAFE_FREE(fallback_realm
);
1492 TALLOC_FREE(fallback_account
);
1494 ret
= smb_krb5_make_principal(context
,
1495 &fallback_principal
,
1498 TALLOC_FREE(with_dollar
);
1500 SAFE_FREE(fallback_realm
);
1504 SAFE_FREE(fallback_realm
);
1506 if (fallback_principal
!= NULL
) {
1507 char *fallback_string
= NULL
;
1509 ret
= krb5_unparse_name(context
,
1513 krb5_free_principal(context
, fallback_principal
);
1517 nt_status
= sam_get_results_principal(kdc_db_ctx
->samdb
,
1522 SAFE_FREE(fallback_string
);
1524 krb5_free_principal(context
, fallback_principal
);
1525 fallback_principal
= NULL
;
1527 TALLOC_FREE(principal_string
);
1529 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
1530 return SDB_ERR_NOENTRY
;
1531 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_MEMORY
)) {
1533 } else if (!NT_STATUS_IS_OK(nt_status
)) {
1540 static krb5_error_code
samba_kdc_fetch_client(krb5_context context
,
1541 struct samba_kdc_db_context
*kdc_db_ctx
,
1542 TALLOC_CTX
*mem_ctx
,
1543 krb5_const_principal principal
,
1545 struct sdb_entry_ex
*entry_ex
) {
1546 struct ldb_dn
*realm_dn
;
1547 krb5_error_code ret
;
1548 struct ldb_message
*msg
= NULL
;
1550 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
1551 mem_ctx
, principal
, user_attrs
,
1557 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1558 principal
, SAMBA_KDC_ENT_TYPE_CLIENT
,
1560 realm_dn
, msg
, entry_ex
);
1564 static krb5_error_code
samba_kdc_fetch_krbtgt(krb5_context context
,
1565 struct samba_kdc_db_context
*kdc_db_ctx
,
1566 TALLOC_CTX
*mem_ctx
,
1567 krb5_const_principal principal
,
1570 struct sdb_entry_ex
*entry_ex
)
1572 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
1573 krb5_error_code ret
;
1574 struct ldb_message
*msg
= NULL
;
1575 struct ldb_dn
*realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
1576 char *realm_from_princ
, *realm_from_princ_malloc
;
1577 char *realm_princ_comp
= smb_krb5_principal_get_comp_string(mem_ctx
, context
, principal
, 1);
1579 realm_from_princ_malloc
= smb_krb5_principal_get_realm(context
, principal
);
1580 if (realm_from_princ_malloc
== NULL
) {
1582 return SDB_ERR_NOENTRY
;
1584 realm_from_princ
= talloc_strdup(mem_ctx
, realm_from_princ_malloc
);
1585 free(realm_from_princ_malloc
);
1586 if (realm_from_princ
== NULL
) {
1587 return SDB_ERR_NOENTRY
;
1590 if (krb5_princ_size(context
, principal
) != 2
1591 || (principal_comp_strcmp(context
, principal
, 0, KRB5_TGS_NAME
) != 0)) {
1593 return SDB_ERR_NOENTRY
;
1596 /* krbtgt case. Either us or a trusted realm */
1598 if (lpcfg_is_my_domain_or_realm(lp_ctx
, realm_from_princ
)
1599 && lpcfg_is_my_domain_or_realm(lp_ctx
, realm_princ_comp
)) {
1600 /* us, or someone quite like us */
1601 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1602 * is in our db, then direct the caller at our primary
1606 unsigned int krbtgt_number
;
1607 /* w2k8r2 sometimes gives us a kvno of 255 for inter-domain
1608 trust tickets. We don't yet know what this means, but we do
1609 seem to need to treat it as unspecified */
1610 if (flags
& SDB_F_KVNO_SPECIFIED
) {
1611 krbtgt_number
= SAMBA_KVNO_GET_KRBTGT(kvno
);
1612 if (kdc_db_ctx
->rodc
) {
1613 if (krbtgt_number
!= kdc_db_ctx
->my_krbtgt_number
) {
1614 return SDB_ERR_NOT_FOUND_HERE
;
1618 krbtgt_number
= kdc_db_ctx
->my_krbtgt_number
;
1621 if (krbtgt_number
== kdc_db_ctx
->my_krbtgt_number
) {
1622 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
,
1623 &msg
, kdc_db_ctx
->krbtgt_dn
, LDB_SCOPE_BASE
,
1624 krbtgt_attrs
, DSDB_SEARCH_NO_GLOBAL_CATALOG
,
1625 "(objectClass=user)");
1627 /* We need to look up an RODC krbtgt (perhaps
1628 * ours, if we are an RODC, perhaps another
1629 * RODC if we are a read-write DC */
1630 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
,
1631 &msg
, realm_dn
, LDB_SCOPE_SUBTREE
,
1633 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
1634 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=%u))", (unsigned)(krbtgt_number
));
1637 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
1638 krb5_warnx(context
, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1639 (unsigned)(krbtgt_number
));
1640 krb5_set_error_message(context
, SDB_ERR_NOENTRY
,
1641 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1642 (unsigned)(krbtgt_number
));
1643 return SDB_ERR_NOENTRY
;
1644 } else if (lret
!= LDB_SUCCESS
) {
1645 krb5_warnx(context
, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1646 (unsigned)(krbtgt_number
));
1647 krb5_set_error_message(context
, SDB_ERR_NOENTRY
,
1648 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1649 (unsigned)(krbtgt_number
));
1650 return SDB_ERR_NOENTRY
;
1653 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1654 principal
, SAMBA_KDC_ENT_TYPE_KRBTGT
,
1655 flags
, realm_dn
, msg
, entry_ex
);
1657 krb5_warnx(context
, "samba_kdc_fetch: self krbtgt message2entry failed");
1662 enum trust_direction direction
= UNKNOWN
;
1663 const char *realm
= NULL
;
1665 /* Either an inbound or outbound trust */
1667 if (strcasecmp(lpcfg_realm(lp_ctx
), realm_from_princ
) == 0) {
1668 /* look for inbound trust */
1669 direction
= INBOUND
;
1670 realm
= realm_princ_comp
;
1671 } else if (principal_comp_strcasecmp(context
, principal
, 1, lpcfg_realm(lp_ctx
)) == 0) {
1672 /* look for outbound trust */
1673 direction
= OUTBOUND
;
1674 realm
= realm_from_princ
;
1676 krb5_warnx(context
, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1679 krb5_set_error_message(context
, SDB_ERR_NOENTRY
, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1682 return SDB_ERR_NOENTRY
;
1685 /* Trusted domains are under CN=system */
1687 ret
= samba_kdc_lookup_trust(context
, kdc_db_ctx
->samdb
,
1689 realm
, realm_dn
, &msg
);
1692 krb5_warnx(context
, "samba_kdc_fetch: could not find principal in DB");
1693 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: could not find principal in DB");
1697 ret
= samba_kdc_trust_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1698 principal
, direction
,
1699 realm_dn
, flags
, kvno
, msg
, entry_ex
);
1701 krb5_warnx(context
, "samba_kdc_fetch: trust_message2entry failed for %s",
1702 ldb_dn_get_linearized(msg
->dn
));
1703 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: "
1704 "trust_message2entry failed for %s",
1705 ldb_dn_get_linearized(msg
->dn
));
1712 static krb5_error_code
samba_kdc_lookup_server(krb5_context context
,
1713 struct samba_kdc_db_context
*kdc_db_ctx
,
1714 TALLOC_CTX
*mem_ctx
,
1715 krb5_const_principal principal
,
1718 struct ldb_dn
**realm_dn
,
1719 struct ldb_message
**msg
)
1721 krb5_error_code ret
;
1722 if ((smb_krb5_principal_get_type(context
, principal
) != KRB5_NT_ENTERPRISE_PRINCIPAL
)
1723 && krb5_princ_size(context
, principal
) >= 2) {
1724 /* 'normal server' case */
1727 struct ldb_dn
*user_dn
;
1728 char *principal_string
;
1730 ret
= krb5_unparse_name_flags(context
, principal
,
1731 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
1737 /* At this point we may find the host is known to be
1738 * in a different realm, so we should generate a
1739 * referral instead */
1740 nt_status
= crack_service_principal_name(kdc_db_ctx
->samdb
,
1741 mem_ctx
, principal_string
,
1742 &user_dn
, realm_dn
);
1743 free(principal_string
);
1745 if (!NT_STATUS_IS_OK(nt_status
)) {
1746 return SDB_ERR_NOENTRY
;
1749 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
,
1751 msg
, user_dn
, LDB_SCOPE_BASE
,
1753 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
1755 if (ldb_ret
!= LDB_SUCCESS
) {
1756 return SDB_ERR_NOENTRY
;
1759 } else if (!(flags
& SDB_F_FOR_AS_REQ
)
1760 && smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
1762 * The behaviour of accepting an
1763 * KRB5_NT_ENTERPRISE_PRINCIPAL server principal
1764 * containing a UPN only applies to TGS-REQ packets,
1765 * not AS-REQ packets.
1767 return samba_kdc_lookup_client(context
, kdc_db_ctx
,
1768 mem_ctx
, principal
, attrs
,
1773 * - the AS-REQ, where we only accept
1774 * samAccountName based lookups for the server, no
1775 * matter if the name is an
1776 * KRB5_NT_ENTERPRISE_PRINCIPAL or not
1777 * - for the TGS-REQ when we are not given an
1778 * KRB5_NT_ENTERPRISE_PRINCIPAL, which also must
1779 * only lookup samAccountName based names.
1783 krb5_principal enterprise_principal
= NULL
;
1784 krb5_const_principal used_principal
= NULL
;
1787 char *filter
= NULL
;
1789 if (smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
1791 /* Need to reparse the enterprise principal to find the real target */
1792 if (krb5_princ_size(context
, principal
) != 1) {
1793 ret
= KRB5_PARSE_MALFORMED
;
1794 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_server: request for an "
1795 "enterprise principal with wrong (%d) number of components",
1796 krb5_princ_size(context
, principal
));
1799 str
= smb_krb5_principal_get_comp_string(mem_ctx
, context
, principal
, 0);
1801 return KRB5_PARSE_MALFORMED
;
1803 ret
= krb5_parse_name(context
, str
,
1804 &enterprise_principal
);
1809 used_principal
= enterprise_principal
;
1811 used_principal
= principal
;
1814 /* server as client principal case, but we must not lookup userPrincipalNames */
1815 *realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
1817 /* TODO: Check if it is our realm, otherwise give referral */
1819 ret
= krb5_unparse_name_flags(context
, used_principal
,
1820 KRB5_PRINCIPAL_UNPARSE_NO_REALM
|
1821 KRB5_PRINCIPAL_UNPARSE_DISPLAY
,
1823 used_principal
= NULL
;
1824 krb5_free_principal(context
, enterprise_principal
);
1825 enterprise_principal
= NULL
;
1828 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_principal: could not parse principal");
1829 krb5_warnx(context
, "samba_kdc_lookup_principal: could not parse principal");
1833 name1
= ldb_binary_encode_string(mem_ctx
, short_princ
);
1834 SAFE_FREE(short_princ
);
1835 if (name1
== NULL
) {
1838 len1
= strlen(name1
);
1839 if (len1
>= 1 && name1
[len1
- 1] != '$') {
1840 filter
= talloc_asprintf(mem_ctx
,
1841 "(&(objectClass=user)(|(samAccountName=%s)(samAccountName=%s$)))",
1843 if (filter
== NULL
) {
1847 filter
= talloc_asprintf(mem_ctx
,
1848 "(&(objectClass=user)(samAccountName=%s))",
1850 if (filter
== NULL
) {
1855 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
, msg
,
1856 *realm_dn
, LDB_SCOPE_SUBTREE
,
1858 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
1860 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
1861 DEBUG(10, ("Failed to find an entry for %s filter:%s\n",
1863 return SDB_ERR_NOENTRY
;
1865 if (lret
== LDB_ERR_CONSTRAINT_VIOLATION
) {
1866 DEBUG(10, ("Failed to find unique entry for %s filter:%s\n",
1868 return SDB_ERR_NOENTRY
;
1870 if (lret
!= LDB_SUCCESS
) {
1871 DEBUG(0, ("Failed single search for %s - %s\n",
1872 name1
, ldb_errstring(kdc_db_ctx
->samdb
)));
1873 return SDB_ERR_NOENTRY
;
1877 return SDB_ERR_NOENTRY
;
1882 static krb5_error_code
samba_kdc_fetch_server(krb5_context context
,
1883 struct samba_kdc_db_context
*kdc_db_ctx
,
1884 TALLOC_CTX
*mem_ctx
,
1885 krb5_const_principal principal
,
1887 struct sdb_entry_ex
*entry_ex
)
1889 krb5_error_code ret
;
1890 struct ldb_dn
*realm_dn
;
1891 struct ldb_message
*msg
;
1893 ret
= samba_kdc_lookup_server(context
, kdc_db_ctx
, mem_ctx
, principal
,
1894 flags
, server_attrs
, &realm_dn
, &msg
);
1899 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1900 principal
, SAMBA_KDC_ENT_TYPE_SERVER
,
1902 realm_dn
, msg
, entry_ex
);
1904 krb5_warnx(context
, "samba_kdc_fetch: message2entry failed");
1910 static krb5_error_code
samba_kdc_lookup_realm(krb5_context context
,
1911 struct samba_kdc_db_context
*kdc_db_ctx
,
1912 TALLOC_CTX
*mem_ctx
,
1913 krb5_const_principal principal
,
1915 struct sdb_entry_ex
*entry_ex
)
1917 TALLOC_CTX
*frame
= talloc_stackframe();
1919 krb5_error_code ret
;
1920 char *_realm
= NULL
;
1921 bool check_realm
= false;
1922 const char *realm
= NULL
;
1923 struct dsdb_trust_routing_table
*trt
= NULL
;
1924 const struct lsa_TrustDomainInfoInfoEx
*tdo
= NULL
;
1925 unsigned int num_comp
;
1929 num_comp
= krb5_princ_size(context
, principal
);
1931 if (flags
& SDB_F_GET_CLIENT
) {
1932 if (flags
& SDB_F_FOR_AS_REQ
) {
1936 if (flags
& SDB_F_GET_SERVER
) {
1937 if (flags
& SDB_F_FOR_TGS_REQ
) {
1947 _realm
= smb_krb5_principal_get_realm(context
, principal
);
1948 if (_realm
== NULL
) {
1954 * The requested realm needs to be our own
1956 ok
= lpcfg_is_my_domain_or_realm(kdc_db_ctx
->lp_ctx
, _realm
);
1959 * The request is not for us...
1963 return SDB_ERR_NOENTRY
;
1966 realm
= talloc_strdup(frame
, _realm
);
1968 if (realm
== NULL
) {
1973 if (smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
1974 char *principal_string
= NULL
;
1975 krb5_principal enterprise_principal
= NULL
;
1976 char *enterprise_realm
= NULL
;
1978 if (num_comp
!= 1) {
1980 return SDB_ERR_NOENTRY
;
1983 principal_string
= smb_krb5_principal_get_comp_string(frame
, context
,
1985 if (principal_string
== NULL
) {
1990 ret
= krb5_parse_name(context
, principal_string
,
1991 &enterprise_principal
);
1992 TALLOC_FREE(principal_string
);
1998 enterprise_realm
= smb_krb5_principal_get_realm(context
,
1999 enterprise_principal
);
2000 krb5_free_principal(context
, enterprise_principal
);
2001 if (enterprise_realm
!= NULL
) {
2002 realm
= talloc_strdup(frame
, enterprise_realm
);
2003 SAFE_FREE(enterprise_realm
);
2004 if (realm
== NULL
) {
2011 if (flags
& SDB_F_GET_SERVER
) {
2012 char *service_realm
= NULL
;
2014 ret
= principal_comp_strcmp(context
, principal
, 0, KRB5_TGS_NAME
);
2017 * we need to search krbtgt/ locally
2024 * We need to check the last component against the routing table.
2026 * Note this works only with 2 or 3 component principals, e.g:
2028 * servicePrincipalName: ldap/W2K8R2-219.bla.base
2029 * servicePrincipalName: ldap/W2K8R2-219.bla.base/bla.base
2030 * servicePrincipalName: ldap/W2K8R2-219.bla.base/ForestDnsZones.bla.base
2031 * servicePrincipalName: ldap/W2K8R2-219.bla.base/DomainDnsZones.bla.base
2034 if (num_comp
== 2 || num_comp
== 3) {
2035 service_realm
= smb_krb5_principal_get_comp_string(frame
,
2041 if (service_realm
!= NULL
) {
2042 realm
= service_realm
;
2046 ok
= lpcfg_is_my_domain_or_realm(kdc_db_ctx
->lp_ctx
, realm
);
2049 * skip the expensive routing lookup
2055 status
= dsdb_trust_routing_table_load(kdc_db_ctx
->samdb
,
2057 if (!NT_STATUS_IS_OK(status
)) {
2062 tdo
= dsdb_trust_routing_by_name(trt
, realm
);
2065 * This principal has to be local
2071 if (tdo
->trust_attributes
& LSA_TRUST_ATTRIBUTE_WITHIN_FOREST
) {
2073 * TODO: handle the routing within the forest
2075 * This should likely be handled in
2076 * samba_kdc_message2entry() in case we're
2077 * a global catalog. We'd need to check
2078 * if realm_dn is our own domain and derive
2079 * the dns domain name from realm_dn and check that
2080 * against the routing table or fallback to
2081 * the tdo we found here.
2083 * But for now we don't support multiple domains
2084 * in our forest correctly anyway.
2086 * Just search in our local database.
2092 ZERO_STRUCT(entry_ex
->entry
);
2094 ret
= krb5_copy_principal(context
, principal
,
2095 &entry_ex
->entry
.principal
);
2101 upper
= strupper_talloc(frame
, tdo
->domain_name
.string
);
2102 if (upper
== NULL
) {
2107 ret
= smb_krb5_principal_set_realm(context
,
2108 entry_ex
->entry
.principal
,
2116 return SDB_ERR_WRONG_REALM
;
2119 krb5_error_code
samba_kdc_fetch(krb5_context context
,
2120 struct samba_kdc_db_context
*kdc_db_ctx
,
2121 krb5_const_principal principal
,
2124 struct sdb_entry_ex
*entry_ex
)
2126 krb5_error_code ret
= SDB_ERR_NOENTRY
;
2127 TALLOC_CTX
*mem_ctx
;
2129 mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_fetch context");
2132 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
2136 ret
= samba_kdc_lookup_realm(context
, kdc_db_ctx
, mem_ctx
,
2137 principal
, flags
, entry_ex
);
2142 ret
= SDB_ERR_NOENTRY
;
2144 if (flags
& SDB_F_GET_CLIENT
) {
2145 ret
= samba_kdc_fetch_client(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, entry_ex
);
2146 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2148 if (flags
& SDB_F_GET_SERVER
) {
2149 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
2150 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, kvno
, entry_ex
);
2151 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2153 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
2154 ret
= samba_kdc_fetch_server(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, entry_ex
);
2155 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2157 if (flags
& SDB_F_GET_KRBTGT
) {
2158 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, kvno
, entry_ex
);
2159 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2163 talloc_free(mem_ctx
);
2167 struct samba_kdc_seq
{
2170 struct ldb_message
**msgs
;
2171 struct ldb_dn
*realm_dn
;
2174 static krb5_error_code
samba_kdc_seq(krb5_context context
,
2175 struct samba_kdc_db_context
*kdc_db_ctx
,
2176 struct sdb_entry_ex
*entry
)
2178 krb5_error_code ret
;
2179 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
2180 const char *realm
= lpcfg_realm(kdc_db_ctx
->lp_ctx
);
2181 struct ldb_message
*msg
= NULL
;
2182 const char *sAMAccountName
= NULL
;
2183 krb5_principal principal
= NULL
;
2184 TALLOC_CTX
*mem_ctx
;
2187 return SDB_ERR_NOENTRY
;
2190 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_seq context");
2194 krb5_set_error_message(context
, ret
, "samba_kdc_seq: talloc_named() failed!");
2198 while (priv
->index
< priv
->count
) {
2199 msg
= priv
->msgs
[priv
->index
++];
2201 sAMAccountName
= ldb_msg_find_attr_as_string(msg
, "sAMAccountName", NULL
);
2202 if (sAMAccountName
!= NULL
) {
2207 if (sAMAccountName
== NULL
) {
2208 ret
= SDB_ERR_NOENTRY
;
2212 ret
= smb_krb5_make_principal(context
, &principal
,
2213 realm
, sAMAccountName
, NULL
);
2218 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
2219 principal
, SAMBA_KDC_ENT_TYPE_ANY
,
2220 SDB_F_ADMIN_DATA
|SDB_F_GET_ANY
,
2221 priv
->realm_dn
, msg
, entry
);
2224 if (principal
!= NULL
) {
2225 krb5_free_principal(context
, principal
);
2230 kdc_db_ctx
->seq_ctx
= NULL
;
2232 talloc_free(mem_ctx
);
2238 krb5_error_code
samba_kdc_firstkey(krb5_context context
,
2239 struct samba_kdc_db_context
*kdc_db_ctx
,
2240 struct sdb_entry_ex
*entry
)
2242 struct ldb_context
*ldb_ctx
= kdc_db_ctx
->samdb
;
2243 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
2245 struct ldb_result
*res
= NULL
;
2246 krb5_error_code ret
;
2247 TALLOC_CTX
*mem_ctx
;
2252 kdc_db_ctx
->seq_ctx
= NULL
;
2255 priv
= (struct samba_kdc_seq
*) talloc(kdc_db_ctx
, struct samba_kdc_seq
);
2258 krb5_set_error_message(context
, ret
, "talloc: out of memory");
2264 priv
->realm_dn
= ldb_get_default_basedn(ldb_ctx
);
2267 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_firstkey context");
2271 krb5_set_error_message(context
, ret
, "samba_kdc_firstkey: talloc_named() failed!");
2275 ret
= krb5_get_default_realm(context
, &realm
);
2280 krb5_free_default_realm(context
, realm
);
2282 lret
= dsdb_search(ldb_ctx
, priv
, &res
,
2283 priv
->realm_dn
, LDB_SCOPE_SUBTREE
, user_attrs
,
2284 DSDB_SEARCH_NO_GLOBAL_CATALOG
,
2285 "(objectClass=user)");
2287 if (lret
!= LDB_SUCCESS
) {
2289 return SDB_ERR_NOENTRY
;
2292 priv
->count
= res
->count
;
2293 priv
->msgs
= talloc_steal(priv
, res
->msgs
);
2296 kdc_db_ctx
->seq_ctx
= priv
;
2298 ret
= samba_kdc_seq(context
, kdc_db_ctx
, entry
);
2302 kdc_db_ctx
->seq_ctx
= NULL
;
2304 talloc_free(mem_ctx
);
2309 krb5_error_code
samba_kdc_nextkey(krb5_context context
,
2310 struct samba_kdc_db_context
*kdc_db_ctx
,
2311 struct sdb_entry_ex
*entry
)
2313 return samba_kdc_seq(context
, kdc_db_ctx
, entry
);
2316 /* Check if a given entry may delegate or do s4u2self to this target principal
2318 * This is currently a very nasty hack - allowing only delegation to itself.
2321 samba_kdc_check_s4u2self(krb5_context context
,
2322 struct samba_kdc_db_context
*kdc_db_ctx
,
2323 struct samba_kdc_entry
*skdc_entry
,
2324 krb5_const_principal target_principal
)
2326 krb5_error_code ret
;
2327 struct ldb_dn
*realm_dn
;
2328 struct ldb_message
*msg
;
2329 struct dom_sid
*orig_sid
;
2330 struct dom_sid
*target_sid
;
2331 const char *delegation_check_attrs
[] = {
2335 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_s4u2self");
2339 krb5_set_error_message(context
, ret
, "samba_kdc_check_s4u2self: talloc_named() failed!");
2343 ret
= samba_kdc_lookup_server(context
, kdc_db_ctx
, mem_ctx
, target_principal
,
2344 SDB_F_GET_CLIENT
|SDB_F_GET_SERVER
,
2345 delegation_check_attrs
, &realm_dn
, &msg
);
2348 talloc_free(mem_ctx
);
2352 orig_sid
= samdb_result_dom_sid(mem_ctx
, skdc_entry
->msg
, "objectSid");
2353 target_sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
2355 /* Allow delegation to the same principal, even if by a different
2356 * name. The easy and safe way to prove this is by SID
2358 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
2359 talloc_free(mem_ctx
);
2360 return KRB5KDC_ERR_BADOPTION
;
2363 talloc_free(mem_ctx
);
2367 /* Certificates printed by a the Certificate Authority might have a
2368 * slightly different form of the user principal name to that in the
2369 * database. Allow a mismatch where they both refer to the same
2373 samba_kdc_check_pkinit_ms_upn_match(krb5_context context
,
2374 struct samba_kdc_db_context
*kdc_db_ctx
,
2375 struct samba_kdc_entry
*skdc_entry
,
2376 krb5_const_principal certificate_principal
)
2378 krb5_error_code ret
;
2379 struct ldb_dn
*realm_dn
;
2380 struct ldb_message
*msg
;
2381 struct dom_sid
*orig_sid
;
2382 struct dom_sid
*target_sid
;
2383 const char *ms_upn_check_attrs
[] = {
2387 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_pkinit_ms_upn_match");
2391 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
2395 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
2396 mem_ctx
, certificate_principal
,
2397 ms_upn_check_attrs
, &realm_dn
, &msg
);
2400 talloc_free(mem_ctx
);
2404 orig_sid
= samdb_result_dom_sid(mem_ctx
, skdc_entry
->msg
, "objectSid");
2405 target_sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
2407 /* Consider these to be the same principal, even if by a different
2408 * name. The easy and safe way to prove this is by SID
2410 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
2411 talloc_free(mem_ctx
);
2412 #ifdef KRB5_KDC_ERR_CLIENT_NAME_MISMATCH /* Heimdal */
2413 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
2414 #elif defined(KRB5KDC_ERR_CLIENT_NAME_MISMATCH) /* MIT */
2415 return KRB5KDC_ERR_CLIENT_NAME_MISMATCH
;
2419 talloc_free(mem_ctx
);
2424 * Check if a given entry may delegate to this target principal
2428 samba_kdc_check_s4u2proxy(krb5_context context
,
2429 struct samba_kdc_db_context
*kdc_db_ctx
,
2430 struct samba_kdc_entry
*skdc_entry
,
2431 krb5_const_principal target_principal
)
2433 krb5_error_code ret
;
2435 const char *client_dn
= NULL
;
2436 const char *target_principal_name
= NULL
;
2437 struct ldb_message_element
*el
;
2442 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_s4u2proxy");
2446 krb5_set_error_message(context
, ret
,
2447 "samba_kdc_check_s4u2proxy:"
2448 " talloc_named() failed!");
2452 client_dn
= ldb_dn_get_linearized(skdc_entry
->msg
->dn
);
2458 krb5_set_error_message(context
, ret
,
2459 "samba_kdc_check_s4u2proxy:"
2460 " ldb_dn_get_linearized() failed!");
2465 * The main heimdal code already checked that the target_principal
2466 * belongs to the same realm as the client.
2468 * So we just need the principal without the realm,
2469 * as that is what is configured in the "msDS-AllowedToDelegateTo"
2472 ret
= krb5_unparse_name_flags(context
, target_principal
,
2473 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &tmp
);
2475 talloc_free(mem_ctx
);
2476 krb5_set_error_message(context
, ret
,
2477 "samba_kdc_check_s4u2proxy:"
2478 " krb5_unparse_name() failed!");
2481 DEBUG(10,("samba_kdc_check_s4u2proxy: client[%s] for target[%s]\n",
2484 target_principal_name
= talloc_strdup(mem_ctx
, tmp
);
2486 if (target_principal_name
== NULL
) {
2488 krb5_set_error_message(context
, ret
,
2489 "samba_kdc_check_s4u2proxy:"
2490 " talloc_strdup() failed!");
2494 el
= ldb_msg_find_element(skdc_entry
->msg
, "msDS-AllowedToDelegateTo");
2499 val
= data_blob_string_const(target_principal_name
);
2501 for (i
=0; i
<el
->num_values
; i
++) {
2502 struct ldb_val
*val1
= &val
;
2503 struct ldb_val
*val2
= &el
->values
[i
];
2506 if (val1
->length
!= val2
->length
) {
2510 cmp
= strncasecmp((const char *)val1
->data
,
2511 (const char *)val2
->data
,
2525 DEBUG(10,("samba_kdc_check_s4u2proxy: client[%s] allowed target[%s]\n",
2527 talloc_free(mem_ctx
);
2531 krb5_set_error_message(context
, ret
,
2532 "samba_kdc_check_s4u2proxy: client[%s] "
2533 "not allowed for delegation to target[%s]",
2535 target_principal_name
);
2536 talloc_free(mem_ctx
);
2537 return KRB5KDC_ERR_BADOPTION
;
2540 NTSTATUS
samba_kdc_setup_db_ctx(TALLOC_CTX
*mem_ctx
, struct samba_kdc_base_context
*base_ctx
,
2541 struct samba_kdc_db_context
**kdc_db_ctx_out
)
2544 struct ldb_message
*msg
;
2545 struct auth_session_info
*session_info
;
2546 struct samba_kdc_db_context
*kdc_db_ctx
;
2547 /* The idea here is very simple. Using Kerberos to
2548 * authenticate the KDC to the LDAP server is higly likely to
2551 * In future we may set this up to use EXERNAL and SSL
2552 * certificates, for now it will almost certainly be NTLMSSP_SET_USERNAME
2555 kdc_db_ctx
= talloc_zero(mem_ctx
, struct samba_kdc_db_context
);
2556 if (kdc_db_ctx
== NULL
) {
2557 return NT_STATUS_NO_MEMORY
;
2559 kdc_db_ctx
->ev_ctx
= base_ctx
->ev_ctx
;
2560 kdc_db_ctx
->lp_ctx
= base_ctx
->lp_ctx
;
2562 /* get default kdc policy */
2563 lpcfg_default_kdc_policy(base_ctx
->lp_ctx
,
2564 &kdc_db_ctx
->policy
.svc_tkt_lifetime
,
2565 &kdc_db_ctx
->policy
.usr_tkt_lifetime
,
2566 &kdc_db_ctx
->policy
.renewal_lifetime
);
2568 session_info
= system_session(kdc_db_ctx
->lp_ctx
);
2569 if (session_info
== NULL
) {
2570 return NT_STATUS_INTERNAL_ERROR
;
2573 /* Setup the link to LDB */
2574 kdc_db_ctx
->samdb
= samdb_connect(kdc_db_ctx
, base_ctx
->ev_ctx
,
2575 base_ctx
->lp_ctx
, session_info
, 0);
2576 if (kdc_db_ctx
->samdb
== NULL
) {
2577 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot open samdb for KDC backend!"));
2578 talloc_free(kdc_db_ctx
);
2579 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2582 /* Find out our own krbtgt kvno */
2583 ldb_ret
= samdb_rodc(kdc_db_ctx
->samdb
, &kdc_db_ctx
->rodc
);
2584 if (ldb_ret
!= LDB_SUCCESS
) {
2585 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine if we are an RODC in KDC backend: %s\n",
2586 ldb_errstring(kdc_db_ctx
->samdb
)));
2587 talloc_free(kdc_db_ctx
);
2588 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2590 if (kdc_db_ctx
->rodc
) {
2591 int my_krbtgt_number
;
2592 const char *secondary_keytab
[] = { "msDS-SecondaryKrbTgtNumber", NULL
};
2593 struct ldb_dn
*account_dn
;
2594 struct ldb_dn
*server_dn
= samdb_server_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
);
2596 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine server DN in KDC backend: %s\n",
2597 ldb_errstring(kdc_db_ctx
->samdb
)));
2598 talloc_free(kdc_db_ctx
);
2599 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2602 ldb_ret
= samdb_reference_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
, server_dn
,
2603 "serverReference", &account_dn
);
2604 if (ldb_ret
!= LDB_SUCCESS
) {
2605 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine server account in KDC backend: %s\n",
2606 ldb_errstring(kdc_db_ctx
->samdb
)));
2607 talloc_free(kdc_db_ctx
);
2608 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2611 ldb_ret
= samdb_reference_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
, account_dn
,
2612 "msDS-KrbTgtLink", &kdc_db_ctx
->krbtgt_dn
);
2613 talloc_free(account_dn
);
2614 if (ldb_ret
!= LDB_SUCCESS
) {
2615 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine RODC krbtgt account in KDC backend: %s\n",
2616 ldb_errstring(kdc_db_ctx
->samdb
)));
2617 talloc_free(kdc_db_ctx
);
2618 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2621 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
, kdc_db_ctx
,
2622 &msg
, kdc_db_ctx
->krbtgt_dn
, LDB_SCOPE_BASE
,
2624 DSDB_SEARCH_NO_GLOBAL_CATALOG
,
2625 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=*))");
2626 if (ldb_ret
!= LDB_SUCCESS
) {
2627 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot read krbtgt account %s in KDC backend to get msDS-SecondaryKrbTgtNumber: %s: %s\n",
2628 ldb_dn_get_linearized(kdc_db_ctx
->krbtgt_dn
),
2629 ldb_errstring(kdc_db_ctx
->samdb
),
2630 ldb_strerror(ldb_ret
)));
2631 talloc_free(kdc_db_ctx
);
2632 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2634 my_krbtgt_number
= ldb_msg_find_attr_as_int(msg
, "msDS-SecondaryKrbTgtNumber", -1);
2635 if (my_krbtgt_number
== -1) {
2636 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot read msDS-SecondaryKrbTgtNumber from krbtgt account %s in KDC backend: got %d\n",
2637 ldb_dn_get_linearized(kdc_db_ctx
->krbtgt_dn
),
2639 talloc_free(kdc_db_ctx
);
2640 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2642 kdc_db_ctx
->my_krbtgt_number
= my_krbtgt_number
;
2645 kdc_db_ctx
->my_krbtgt_number
= 0;
2646 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
, kdc_db_ctx
,
2648 ldb_get_default_basedn(kdc_db_ctx
->samdb
),
2651 DSDB_SEARCH_NO_GLOBAL_CATALOG
,
2652 "(&(objectClass=user)(samAccountName=krbtgt))");
2654 if (ldb_ret
!= LDB_SUCCESS
) {
2655 DEBUG(1, ("samba_kdc_fetch: could not find own KRBTGT in DB: %s\n", ldb_errstring(kdc_db_ctx
->samdb
)));
2656 talloc_free(kdc_db_ctx
);
2657 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2659 kdc_db_ctx
->krbtgt_dn
= talloc_steal(kdc_db_ctx
, msg
->dn
);
2660 kdc_db_ctx
->my_krbtgt_number
= 0;
2663 *kdc_db_ctx_out
= kdc_db_ctx
;
2664 return NT_STATUS_OK
;