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
);
200 * Sort keys in descending order of strength.
202 * Explanaton from Greg Hudson:
204 * To encrypt tickets only the first returned key is used by the MIT KDC. The
205 * other keys just communicate support for session key enctypes, and aren't
206 * really used. The encryption key for the ticket enc part doesn't have
207 * to be of a type requested by the client. The session key enctype is chosen
208 * based on the client preference order, limited by the set of enctypes present
209 * in the server keys (unless the string attribute is set on the server
210 * principal overriding that set).
212 static int samba_kdc_sort_encryption_keys(struct sdb_entry_ex
*entry_ex
)
214 unsigned int i
, j
, idx
= 0;
215 static const krb5_enctype etype_list
[] = {
216 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
217 ENCTYPE_AES128_CTS_HMAC_SHA1_96
,
218 ENCTYPE_DES3_CBC_SHA1
,
219 ENCTYPE_ARCFOUR_HMAC
,
225 size_t etype_len
= ARRAY_SIZE(etype_list
);
226 size_t keys_size
= entry_ex
->entry
.keys
.len
;
227 struct sdb_key
*keys
= entry_ex
->entry
.keys
.val
;
228 struct sdb_key
*sorted_keys
;
230 sorted_keys
= calloc(keys_size
, sizeof(struct sdb_key
));
231 if (sorted_keys
== NULL
) {
235 for (i
= 0; i
< etype_len
; i
++) {
236 for (j
= 0; j
< keys_size
; j
++) {
237 const struct sdb_key skey
= keys
[j
];
239 if (idx
== keys_size
) {
243 if (KRB5_KEY_TYPE(&skey
.key
) == etype_list
[i
]) {
244 sorted_keys
[idx
] = skey
;
250 /* Paranoia: Something went wrong during data copy */
251 if (idx
!= keys_size
) {
256 free(entry_ex
->entry
.keys
.val
);
257 entry_ex
->entry
.keys
.val
= sorted_keys
;
262 static krb5_error_code
samba_kdc_message2entry_keys(krb5_context context
,
263 struct samba_kdc_db_context
*kdc_db_ctx
,
265 struct ldb_message
*msg
,
268 uint32_t userAccountControl
,
269 enum samba_kdc_ent_type ent_type
,
270 struct sdb_entry_ex
*entry_ex
)
272 krb5_error_code ret
= 0;
273 enum ndr_err_code ndr_err
;
274 struct samr_Password
*hash
;
275 const struct ldb_val
*sc_val
;
276 struct supplementalCredentialsBlob scb
;
277 struct supplementalCredentialsPackage
*scpk
= NULL
;
278 bool newer_keys
= false;
279 struct package_PrimaryKerberosBlob _pkb
;
280 struct package_PrimaryKerberosCtr3
*pkb3
= NULL
;
281 struct package_PrimaryKerberosCtr4
*pkb4
= NULL
;
283 uint16_t allocated_keys
= 0;
284 int rodc_krbtgt_number
= 0;
286 uint32_t supported_enctypes
287 = ldb_msg_find_attr_as_uint(msg
,
288 "msDS-SupportedEncryptionTypes",
291 if (rid
== DOMAIN_RID_KRBTGT
|| is_rodc
) {
292 /* KDCs (and KDCs on RODCs) use AES */
293 supported_enctypes
|= ENC_HMAC_SHA1_96_AES128
| ENC_HMAC_SHA1_96_AES256
;
294 } else if (userAccountControl
& (UF_PARTIAL_SECRETS_ACCOUNT
|UF_SERVER_TRUST_ACCOUNT
)) {
295 /* DCs and RODCs comptuer accounts use AES */
296 supported_enctypes
|= ENC_HMAC_SHA1_96_AES128
| ENC_HMAC_SHA1_96_AES256
;
297 } else if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
||
298 (ent_type
== SAMBA_KDC_ENT_TYPE_ANY
)) {
299 /* for AS-REQ the client chooses the enc types it
300 * supports, and this will vary between computers a
303 * likewise for 'any' return as much as is supported,
304 * to export into a keytab */
305 supported_enctypes
= ENC_ALL_TYPES
;
308 /* If UF_USE_DES_KEY_ONLY has been set, then don't allow use of the newer enc types */
309 if (userAccountControl
& UF_USE_DES_KEY_ONLY
) {
310 supported_enctypes
= ENC_CRC32
|ENC_RSA_MD5
;
312 /* Otherwise, add in the default enc types */
313 supported_enctypes
|= ENC_CRC32
| ENC_RSA_MD5
| ENC_RC4_HMAC_MD5
;
316 /* Is this the krbtgt or a RODC krbtgt */
318 rodc_krbtgt_number
= ldb_msg_find_attr_as_int(msg
, "msDS-SecondaryKrbTgtNumber", -1);
320 if (rodc_krbtgt_number
== -1) {
325 entry_ex
->entry
.keys
.val
= NULL
;
326 entry_ex
->entry
.keys
.len
= 0;
327 entry_ex
->entry
.kvno
= 0;
329 if ((ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
)
330 && (userAccountControl
& UF_SMARTCARD_REQUIRED
)) {
331 uint8_t secretbuffer
[32];
334 * Fake keys until we have a better way to reject
335 * non-pkinit requests.
337 * We just need to indicate which encryption types are
340 generate_secret_buffer(secretbuffer
, sizeof(secretbuffer
));
343 entry_ex
->entry
.keys
.len
= 0;
344 entry_ex
->entry
.keys
.val
= calloc(allocated_keys
, sizeof(struct sdb_key
));
345 if (entry_ex
->entry
.keys
.val
== NULL
) {
346 ZERO_STRUCT(secretbuffer
);
351 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES256
) {
352 struct sdb_key key
= {};
354 ret
= smb_krb5_keyblock_init_contents(context
,
355 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
359 ZERO_STRUCT(secretbuffer
);
363 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
364 entry_ex
->entry
.keys
.len
++;
367 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES128
) {
368 struct sdb_key key
= {};
370 ret
= smb_krb5_keyblock_init_contents(context
,
371 ENCTYPE_AES128_CTS_HMAC_SHA1_96
,
375 ZERO_STRUCT(secretbuffer
);
379 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
380 entry_ex
->entry
.keys
.len
++;
383 if (supported_enctypes
& ENC_RC4_HMAC_MD5
) {
384 struct sdb_key key
= {};
386 ret
= smb_krb5_keyblock_init_contents(context
,
387 ENCTYPE_ARCFOUR_HMAC
,
391 ZERO_STRUCT(secretbuffer
);
395 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
396 entry_ex
->entry
.keys
.len
++;
403 kvno
= ldb_msg_find_attr_as_int(msg
, "msDS-KeyVersionNumber", 0);
405 kvno
= SAMBA_KVNO_AND_KRBTGT(kvno
, rodc_krbtgt_number
);
407 entry_ex
->entry
.kvno
= kvno
;
409 /* Get keys from the db */
411 hash
= samdb_result_hash(mem_ctx
, msg
, "unicodePwd");
412 sc_val
= ldb_msg_find_ldb_val(msg
, "supplementalCredentials");
414 /* unicodePwd for enctype 0x17 (23) if present */
419 /* supplementalCredentials if present */
421 ndr_err
= ndr_pull_struct_blob_all(sc_val
, mem_ctx
, &scb
,
422 (ndr_pull_flags_fn_t
)ndr_pull_supplementalCredentialsBlob
);
423 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
424 dump_data(0, sc_val
->data
, sc_val
->length
);
429 if (scb
.sub
.signature
!= SUPPLEMENTAL_CREDENTIALS_SIGNATURE
) {
430 if (scb
.sub
.num_packages
!= 0) {
431 NDR_PRINT_DEBUG(supplementalCredentialsBlob
, &scb
);
437 for (i
=0; i
< scb
.sub
.num_packages
; i
++) {
438 if (strcmp("Primary:Kerberos-Newer-Keys", scb
.sub
.packages
[i
].name
) == 0) {
439 scpk
= &scb
.sub
.packages
[i
];
440 if (!scpk
->data
|| !scpk
->data
[0]) {
446 } else if (strcmp("Primary:Kerberos", scb
.sub
.packages
[i
].name
) == 0) {
447 scpk
= &scb
.sub
.packages
[i
];
448 if (!scpk
->data
|| !scpk
->data
[0]) {
452 * we don't break here in hope to find
453 * a Kerberos-Newer-Keys package
459 * Primary:Kerberos-Newer-Keys or Primary:Kerberos element
460 * of supplementalCredentials
465 blob
= strhex_to_data_blob(mem_ctx
, scpk
->data
);
471 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
472 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, &_pkb
,
473 (ndr_pull_flags_fn_t
)ndr_pull_package_PrimaryKerberosBlob
);
474 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
476 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
477 krb5_warnx(context
, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
481 if (newer_keys
&& _pkb
.version
!= 4) {
483 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
484 krb5_warnx(context
, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
488 if (!newer_keys
&& _pkb
.version
!= 3) {
490 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
491 krb5_warnx(context
, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
495 if (_pkb
.version
== 4) {
496 pkb4
= &_pkb
.ctr
.ctr4
;
497 allocated_keys
+= pkb4
->num_keys
;
498 } else if (_pkb
.version
== 3) {
499 pkb3
= &_pkb
.ctr
.ctr3
;
500 allocated_keys
+= pkb3
->num_keys
;
504 if (allocated_keys
== 0) {
505 if (kdc_db_ctx
->rodc
) {
506 /* We are on an RODC, but don't have keys for this account. Signal this to the caller */
507 /* TODO: We need to call a generalised version of auth_sam_trigger_repl_secret from here */
508 return SDB_ERR_NOT_FOUND_HERE
;
511 /* oh, no password. Apparently (comment in
512 * hdb-ldap.c) this violates the ASN.1, but this
513 * allows an entry with no keys (yet). */
517 /* allocate space to decode into */
518 entry_ex
->entry
.keys
.len
= 0;
519 entry_ex
->entry
.keys
.val
= calloc(allocated_keys
, sizeof(struct sdb_key
));
520 if (entry_ex
->entry
.keys
.val
== NULL
) {
525 if (hash
&& (supported_enctypes
& ENC_RC4_HMAC_MD5
)) {
526 struct sdb_key key
= {};
528 ret
= smb_krb5_keyblock_init_contents(context
,
529 ENCTYPE_ARCFOUR_HMAC
,
537 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
538 entry_ex
->entry
.keys
.len
++;
542 for (i
=0; i
< pkb4
->num_keys
; i
++) {
543 struct sdb_key key
= {};
545 if (!pkb4
->keys
[i
].value
) continue;
547 if (!(kerberos_enctype_to_bitmap(pkb4
->keys
[i
].keytype
) & supported_enctypes
)) {
551 if (pkb4
->salt
.string
) {
554 salt
= data_blob_string_const(pkb4
->salt
.string
);
556 key
.salt
= calloc(1, sizeof(*key
.salt
));
557 if (key
.salt
== NULL
) {
562 key
.salt
->type
= KRB5_PW_SALT
;
564 ret
= smb_krb5_copy_data_contents(&key
.salt
->salt
,
574 /* TODO: maybe pass the iteration_count somehow... */
576 ret
= smb_krb5_keyblock_init_contents(context
,
577 pkb4
->keys
[i
].keytype
,
578 pkb4
->keys
[i
].value
->data
,
579 pkb4
->keys
[i
].value
->length
,
581 if (ret
== KRB5_PROG_ETYPE_NOSUPP
) {
582 DEBUG(2,("Unsupported keytype ignored - type %u\n",
583 pkb4
->keys
[i
].keytype
));
589 smb_krb5_free_data_contents(context
, &key
.salt
->salt
);
596 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
597 entry_ex
->entry
.keys
.len
++;
600 for (i
=0; i
< pkb3
->num_keys
; i
++) {
601 struct sdb_key key
= {};
603 if (!pkb3
->keys
[i
].value
) continue;
605 if (!(kerberos_enctype_to_bitmap(pkb3
->keys
[i
].keytype
) & supported_enctypes
)) {
609 if (pkb3
->salt
.string
) {
612 salt
= data_blob_string_const(pkb3
->salt
.string
);
614 key
.salt
= calloc(1, sizeof(*key
.salt
));
615 if (key
.salt
== NULL
) {
620 key
.salt
->type
= KRB5_PW_SALT
;
622 ret
= smb_krb5_copy_data_contents(&key
.salt
->salt
,
632 ret
= smb_krb5_keyblock_init_contents(context
,
633 pkb3
->keys
[i
].keytype
,
634 pkb3
->keys
[i
].value
->data
,
635 pkb3
->keys
[i
].value
->length
,
639 smb_krb5_free_data_contents(context
, &key
.salt
->salt
);
646 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
647 entry_ex
->entry
.keys
.len
++;
653 entry_ex
->entry
.keys
.len
= 0;
654 } else if (entry_ex
->entry
.keys
.len
> 0 &&
655 entry_ex
->entry
.keys
.val
!= NULL
) {
656 ret
= samba_kdc_sort_encryption_keys(entry_ex
);
658 entry_ex
->entry
.keys
.len
= 0;
662 if (entry_ex
->entry
.keys
.len
== 0 && entry_ex
->entry
.keys
.val
) {
663 free(entry_ex
->entry
.keys
.val
);
664 entry_ex
->entry
.keys
.val
= NULL
;
669 static int principal_comp_strcmp_int(krb5_context context
,
670 krb5_const_principal principal
,
671 unsigned int component
,
678 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
679 p
= krb5_principal_get_comp_string(context
, principal
, component
);
686 if (component
>= krb5_princ_size(context
, principal
)) {
690 d
= krb5_princ_component(context
, principal
, component
);
699 return strncasecmp(p
, string
, len
);
701 return strncmp(p
, string
, len
);
705 static int principal_comp_strcasecmp(krb5_context context
,
706 krb5_const_principal principal
,
707 unsigned int component
,
710 return principal_comp_strcmp_int(context
, principal
,
711 component
, string
, true);
714 static int principal_comp_strcmp(krb5_context context
,
715 krb5_const_principal principal
,
716 unsigned int component
,
719 return principal_comp_strcmp_int(context
, principal
,
720 component
, string
, false);
724 * Construct an hdb_entry from a directory entry.
726 static krb5_error_code
samba_kdc_message2entry(krb5_context context
,
727 struct samba_kdc_db_context
*kdc_db_ctx
,
729 krb5_const_principal principal
,
730 enum samba_kdc_ent_type ent_type
,
732 struct ldb_dn
*realm_dn
,
733 struct ldb_message
*msg
,
734 struct sdb_entry_ex
*entry_ex
)
736 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
737 uint32_t userAccountControl
;
738 uint32_t msDS_User_Account_Control_Computed
;
739 krb5_error_code ret
= 0;
740 krb5_boolean is_computer
= FALSE
;
742 struct samba_kdc_entry
*p
;
747 bool is_rodc
= false;
748 struct ldb_message_element
*objectclasses
;
749 struct ldb_val computer_val
;
750 const char *samAccountName
= ldb_msg_find_attr_as_string(msg
, "samAccountName", NULL
);
751 computer_val
.data
= discard_const_p(uint8_t,"computer");
752 computer_val
.length
= strlen((const char *)computer_val
.data
);
754 if (ldb_msg_find_element(msg
, "msDS-SecondaryKrbTgtNumber")) {
758 if (!samAccountName
) {
760 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: no samAccountName present");
764 objectclasses
= ldb_msg_find_element(msg
, "objectClass");
766 if (objectclasses
&& ldb_msg_find_val(objectclasses
, &computer_val
)) {
770 ZERO_STRUCTP(entry_ex
);
772 p
= talloc_zero(mem_ctx
, struct samba_kdc_entry
);
778 p
->kdc_db_ctx
= kdc_db_ctx
;
779 p
->realm_dn
= talloc_reference(p
, realm_dn
);
785 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
789 userAccountControl
= ldb_msg_find_attr_as_uint(msg
, "userAccountControl", 0);
791 msDS_User_Account_Control_Computed
792 = ldb_msg_find_attr_as_uint(msg
,
793 "msDS-User-Account-Control-Computed",
797 * This brings in the lockout flag, block the account if not
798 * found. We need the weird UF_ACCOUNTDISABLE check because
799 * we do not want to fail open if the value is not returned,
800 * but 0 is a valid value (all OK)
802 if (msDS_User_Account_Control_Computed
== UF_ACCOUNTDISABLE
) {
804 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: "
805 "no msDS-User-Account-Control-Computed present");
808 userAccountControl
|= msDS_User_Account_Control_Computed
;
812 * If we are set to canonicalize, we get back the fixed UPPER
813 * case realm, and the real username (ie matching LDAP
816 * Otherwise, if we are set to enterprise, we
817 * get back the whole principal as-sent
819 * Finally, if we are not set to canonicalize, we get back the
820 * fixed UPPER case realm, but the as-sent username
823 if (ent_type
== SAMBA_KDC_ENT_TYPE_KRBTGT
) {
824 if (flags
& (SDB_F_CANON
)) {
826 * When requested to do so, ensure that the
827 * both realm values in the principal are set
828 * to the upper case, canonical realm
830 ret
= smb_krb5_make_principal(context
, &entry_ex
->entry
.principal
,
831 lpcfg_realm(lp_ctx
), "krbtgt",
832 lpcfg_realm(lp_ctx
), NULL
);
834 krb5_clear_error_message(context
);
837 smb_krb5_principal_set_type(context
, entry_ex
->entry
.principal
, KRB5_NT_SRV_INST
);
839 ret
= krb5_copy_principal(context
, principal
, &entry_ex
->entry
.principal
);
841 krb5_clear_error_message(context
);
845 * this appears to be required regardless of
846 * the canonicalize flag from the client
848 ret
= smb_krb5_principal_set_realm(context
, entry_ex
->entry
.principal
, lpcfg_realm(lp_ctx
));
850 krb5_clear_error_message(context
);
855 } else if (ent_type
== SAMBA_KDC_ENT_TYPE_ANY
&& principal
== NULL
) {
856 ret
= smb_krb5_make_principal(context
, &entry_ex
->entry
.principal
, lpcfg_realm(lp_ctx
), samAccountName
, NULL
);
858 krb5_clear_error_message(context
);
861 } else if (flags
& SDB_F_CANON
&& flags
& SDB_F_FOR_AS_REQ
) {
863 * SDB_F_CANON maps from the canonicalize flag in the
864 * packet, and has a different meaning between AS-REQ
865 * and TGS-REQ. We only change the principal in the AS-REQ case
867 ret
= smb_krb5_make_principal(context
, &entry_ex
->entry
.principal
, lpcfg_realm(lp_ctx
), samAccountName
, NULL
);
869 krb5_clear_error_message(context
);
873 ret
= krb5_copy_principal(context
, principal
, &entry_ex
->entry
.principal
);
875 krb5_clear_error_message(context
);
879 if (smb_krb5_principal_get_type(context
, principal
) != KRB5_NT_ENTERPRISE_PRINCIPAL
) {
880 /* While we have copied the client principal, tests
881 * show that Win2k3 returns the 'corrected' realm, not
882 * the client-specified realm. This code attempts to
883 * replace the client principal's realm with the one
884 * we determine from our records */
886 /* this has to be with malloc() */
887 ret
= smb_krb5_principal_set_realm(context
, entry_ex
->entry
.principal
, lpcfg_realm(lp_ctx
));
889 krb5_clear_error_message(context
);
895 /* First try and figure out the flags based on the userAccountControl */
896 entry_ex
->entry
.flags
= uf2SDBFlags(context
, userAccountControl
, ent_type
);
898 /* Windows 2008 seems to enforce this (very sensible) rule by
899 * default - don't allow offline attacks on a user's password
900 * by asking for a ticket to them as a service (encrypted with
901 * their probably patheticly insecure password) */
903 if (entry_ex
->entry
.flags
.server
904 && lpcfg_parm_bool(lp_ctx
, NULL
, "kdc", "require spn for service", true)) {
905 if (!is_computer
&& !ldb_msg_find_attr_as_string(msg
, "servicePrincipalName", NULL
)) {
906 entry_ex
->entry
.flags
.server
= 0;
910 * To give the correct type of error to the client, we must
911 * not just return the entry without .server set, we must
912 * pretend the principal does not exist. Otherwise we may
913 * return ERR_POLICY instead of
914 * KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
916 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
&& entry_ex
->entry
.flags
.server
== 0) {
917 ret
= SDB_ERR_NOENTRY
;
918 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: no servicePrincipalName present for this server, refusing with no-such-entry");
921 if (flags
& SDB_F_ADMIN_DATA
) {
922 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
923 * of the Heimdal KDC. They are stored in a the traditional
924 * DB for audit purposes, and still form part of the structure
927 /* use 'whenCreated' */
928 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
929 /* use 'kadmin' for now (needed by mit_samba) */
931 ret
= smb_krb5_make_principal(context
,
932 &entry_ex
->entry
.created_by
.principal
,
933 lpcfg_realm(lp_ctx
), "kadmin", NULL
);
935 krb5_clear_error_message(context
);
939 entry_ex
->entry
.modified_by
= (struct sdb_event
*) malloc(sizeof(struct sdb_event
));
940 if (entry_ex
->entry
.modified_by
== NULL
) {
942 krb5_set_error_message(context
, ret
, "malloc: out of memory");
946 /* use 'whenChanged' */
947 entry_ex
->entry
.modified_by
->time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenChanged", 0);
948 /* use 'kadmin' for now (needed by mit_samba) */
949 ret
= smb_krb5_make_principal(context
,
950 &entry_ex
->entry
.modified_by
->principal
,
951 lpcfg_realm(lp_ctx
), "kadmin", NULL
);
953 krb5_clear_error_message(context
);
959 /* The lack of password controls etc applies to krbtgt by
960 * virtue of being that particular RID */
961 status
= dom_sid_split_rid(NULL
, samdb_result_dom_sid(mem_ctx
, msg
, "objectSid"), NULL
, &rid
);
963 if (!NT_STATUS_IS_OK(status
)) {
968 if (rid
== DOMAIN_RID_KRBTGT
) {
971 entry_ex
->entry
.valid_end
= NULL
;
972 entry_ex
->entry
.pw_end
= NULL
;
974 entry_ex
->entry
.flags
.invalid
= 0;
975 entry_ex
->entry
.flags
.server
= 1;
977 realm
= smb_krb5_principal_get_realm(context
, principal
);
983 /* Don't mark all requests for the krbtgt/realm as
984 * 'change password', as otherwise we could get into
985 * trouble, and not enforce the password expirty.
986 * Instead, only do it when request is for the kpasswd service */
987 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
988 && krb5_princ_size(context
, principal
) == 2
989 && (principal_comp_strcmp(context
, principal
, 0, "kadmin") == 0)
990 && (principal_comp_strcmp(context
, principal
, 1, "changepw") == 0)
991 && lpcfg_is_my_domain_or_realm(lp_ctx
, realm
)) {
992 entry_ex
->entry
.flags
.change_pw
= 1;
997 entry_ex
->entry
.flags
.client
= 0;
998 entry_ex
->entry
.flags
.forwardable
= 1;
999 entry_ex
->entry
.flags
.ok_as_delegate
= 1;
1000 } else if (is_rodc
) {
1001 /* The RODC krbtgt account is like the main krbtgt,
1002 * but it does not have a changepw or kadmin
1005 entry_ex
->entry
.valid_end
= NULL
;
1006 entry_ex
->entry
.pw_end
= NULL
;
1008 /* Also don't allow the RODC krbtgt to be a client (it should not be needed) */
1009 entry_ex
->entry
.flags
.client
= 0;
1010 entry_ex
->entry
.flags
.invalid
= 0;
1011 entry_ex
->entry
.flags
.server
= 1;
1013 entry_ex
->entry
.flags
.client
= 0;
1014 entry_ex
->entry
.flags
.forwardable
= 1;
1015 entry_ex
->entry
.flags
.ok_as_delegate
= 0;
1016 } else if (entry_ex
->entry
.flags
.server
&& ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
1017 /* The account/password expiry only applies when the account is used as a
1018 * client (ie password login), not when used as a server */
1020 /* Make very well sure we don't use this for a client,
1021 * it could bypass the password restrictions */
1022 entry_ex
->entry
.flags
.client
= 0;
1024 entry_ex
->entry
.valid_end
= NULL
;
1025 entry_ex
->entry
.pw_end
= NULL
;
1028 NTTIME must_change_time
1029 = samdb_result_nttime(msg
,
1030 "msDS-UserPasswordExpiryTimeComputed",
1032 if (must_change_time
== 0x7FFFFFFFFFFFFFFFULL
) {
1033 entry_ex
->entry
.pw_end
= NULL
;
1035 entry_ex
->entry
.pw_end
= malloc(sizeof(*entry_ex
->entry
.pw_end
));
1036 if (entry_ex
->entry
.pw_end
== NULL
) {
1040 *entry_ex
->entry
.pw_end
= nt_time_to_unix(must_change_time
);
1043 acct_expiry
= samdb_result_account_expires(msg
);
1044 if (acct_expiry
== 0x7FFFFFFFFFFFFFFFULL
) {
1045 entry_ex
->entry
.valid_end
= NULL
;
1047 entry_ex
->entry
.valid_end
= malloc(sizeof(*entry_ex
->entry
.valid_end
));
1048 if (entry_ex
->entry
.valid_end
== NULL
) {
1052 *entry_ex
->entry
.valid_end
= nt_time_to_unix(acct_expiry
);
1056 entry_ex
->entry
.valid_start
= NULL
;
1058 entry_ex
->entry
.max_life
= malloc(sizeof(*entry_ex
->entry
.max_life
));
1059 if (entry_ex
->entry
.max_life
== NULL
) {
1064 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
1065 *entry_ex
->entry
.max_life
= kdc_db_ctx
->policy
.svc_tkt_lifetime
;
1066 } else if (ent_type
== SAMBA_KDC_ENT_TYPE_KRBTGT
|| ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
) {
1067 *entry_ex
->entry
.max_life
= kdc_db_ctx
->policy
.usr_tkt_lifetime
;
1069 *entry_ex
->entry
.max_life
= MIN(kdc_db_ctx
->policy
.svc_tkt_lifetime
,
1070 kdc_db_ctx
->policy
.usr_tkt_lifetime
);
1073 entry_ex
->entry
.max_renew
= malloc(sizeof(*entry_ex
->entry
.max_life
));
1074 if (entry_ex
->entry
.max_renew
== NULL
) {
1079 *entry_ex
->entry
.max_renew
= kdc_db_ctx
->policy
.renewal_lifetime
;
1081 /* Get keys from the db */
1082 ret
= samba_kdc_message2entry_keys(context
, kdc_db_ctx
, p
, msg
,
1083 rid
, is_rodc
, userAccountControl
,
1084 ent_type
, entry_ex
);
1086 /* Could be bogus data in the entry, or out of memory */
1090 p
->msg
= talloc_steal(p
, msg
);
1094 /* This doesn't free ent itself, that is for the eventual caller to do */
1095 sdb_free_entry(entry_ex
);
1096 ZERO_STRUCTP(entry_ex
);
1098 talloc_steal(kdc_db_ctx
, entry_ex
->ctx
);
1105 * Construct an hdb_entry from a directory entry.
1106 * The kvno is what the remote client asked for
1108 static krb5_error_code
samba_kdc_trust_message2entry(krb5_context context
,
1109 struct samba_kdc_db_context
*kdc_db_ctx
,
1110 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
1111 enum trust_direction direction
,
1112 struct ldb_dn
*realm_dn
,
1115 struct ldb_message
*msg
,
1116 struct sdb_entry_ex
*entry_ex
)
1118 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
1119 const char *our_realm
= lpcfg_realm(lp_ctx
);
1120 const char *dnsdomain
= NULL
;
1121 char *partner_realm
= NULL
;
1122 const char *realm
= NULL
;
1123 const char *krbtgt_realm
= NULL
;
1124 DATA_BLOB password_utf16
= data_blob_null
;
1125 DATA_BLOB password_utf8
= data_blob_null
;
1126 struct samr_Password _password_hash
;
1127 const struct samr_Password
*password_hash
= NULL
;
1128 const struct ldb_val
*password_val
;
1129 struct trustAuthInOutBlob password_blob
;
1130 struct samba_kdc_entry
*p
;
1131 bool use_previous
= false;
1132 uint32_t current_kvno
;
1133 uint32_t previous_kvno
;
1134 uint32_t num_keys
= 0;
1135 enum ndr_err_code ndr_err
;
1136 int ret
, trust_direction_flags
;
1138 struct AuthenticationInformationArray
*auth_array
;
1141 uint32_t *auth_kvno
;
1142 bool preferr_current
= false;
1143 uint32_t supported_enctypes
= ENC_RC4_HMAC_MD5
;
1145 if (dsdb_functional_level(kdc_db_ctx
->samdb
) >= DS_DOMAIN_FUNCTION_2008
) {
1146 supported_enctypes
= ldb_msg_find_attr_as_uint(msg
,
1147 "msDS-SupportedEncryptionTypes",
1148 supported_enctypes
);
1151 trust_direction_flags
= ldb_msg_find_attr_as_int(msg
, "trustDirection", 0);
1152 if (!(trust_direction_flags
& direction
)) {
1153 krb5_clear_error_message(context
);
1154 ret
= SDB_ERR_NOENTRY
;
1158 dnsdomain
= ldb_msg_find_attr_as_string(msg
, "trustPartner", NULL
);
1159 if (dnsdomain
== NULL
) {
1160 krb5_clear_error_message(context
);
1161 ret
= SDB_ERR_NOENTRY
;
1164 partner_realm
= strupper_talloc(mem_ctx
, dnsdomain
);
1165 if (partner_realm
== NULL
) {
1166 krb5_clear_error_message(context
);
1171 if (direction
== INBOUND
) {
1173 krbtgt_realm
= partner_realm
;
1175 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthIncoming");
1176 } else { /* OUTBOUND */
1177 realm
= partner_realm
;
1178 krbtgt_realm
= our_realm
;
1180 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthOutgoing");
1183 if (password_val
== NULL
) {
1184 krb5_clear_error_message(context
);
1185 ret
= SDB_ERR_NOENTRY
;
1189 ndr_err
= ndr_pull_struct_blob(password_val
, mem_ctx
, &password_blob
,
1190 (ndr_pull_flags_fn_t
)ndr_pull_trustAuthInOutBlob
);
1191 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1192 krb5_clear_error_message(context
);
1197 p
= talloc(mem_ctx
, struct samba_kdc_entry
);
1203 p
->kdc_db_ctx
= kdc_db_ctx
;
1204 p
->realm_dn
= realm_dn
;
1206 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
1208 /* make sure we do not have bogus data in there */
1209 memset(&entry_ex
->entry
, 0, sizeof(struct sdb_entry
));
1213 /* use 'whenCreated' */
1214 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
1215 /* use 'kadmin' for now (needed by mit_samba) */
1216 ret
= smb_krb5_make_principal(context
,
1217 &entry_ex
->entry
.created_by
.principal
,
1218 realm
, "kadmin", NULL
);
1220 krb5_clear_error_message(context
);
1225 * We always need to generate the canonicalized principal
1226 * with the values of our database.
1228 ret
= smb_krb5_make_principal(context
, &entry_ex
->entry
.principal
, realm
,
1229 "krbtgt", krbtgt_realm
, NULL
);
1231 krb5_clear_error_message(context
);
1234 smb_krb5_principal_set_type(context
, entry_ex
->entry
.principal
,
1237 entry_ex
->entry
.valid_start
= NULL
;
1239 /* we need to work out if we are going to use the current or
1240 * the previous password hash.
1241 * We base this on the kvno the client passes in. If the kvno
1242 * passed in is equal to the current kvno in our database then
1243 * we use the current structure. If it is the current kvno-1,
1244 * then we use the previous substrucure.
1248 * Windows preferrs the previous key for one hour.
1250 tv
= timeval_current();
1251 if (tv
.tv_sec
> 3600) {
1254 an_hour_ago
= timeval_to_nttime(&tv
);
1256 /* first work out the current kvno */
1258 for (i
=0; i
< password_blob
.count
; i
++) {
1259 struct AuthenticationInformation
*a
=
1260 &password_blob
.current
.array
[i
];
1262 if (a
->LastUpdateTime
<= an_hour_ago
) {
1263 preferr_current
= true;
1266 if (a
->AuthType
== TRUST_AUTH_TYPE_VERSION
) {
1267 current_kvno
= a
->AuthInfo
.version
.version
;
1270 if (current_kvno
== 0) {
1271 previous_kvno
= 255;
1273 previous_kvno
= current_kvno
- 1;
1275 for (i
=0; i
< password_blob
.count
; i
++) {
1276 struct AuthenticationInformation
*a
=
1277 &password_blob
.previous
.array
[i
];
1279 if (a
->AuthType
== TRUST_AUTH_TYPE_VERSION
) {
1280 previous_kvno
= a
->AuthInfo
.version
.version
;
1284 /* work out whether we will use the previous or current
1286 if (password_blob
.previous
.count
== 0) {
1287 /* there is no previous password */
1288 use_previous
= false;
1289 } else if (!(flags
& SDB_F_KVNO_SPECIFIED
)) {
1291 * If not specified we use the lowest kvno
1292 * for the first hour after an update.
1294 if (preferr_current
) {
1295 use_previous
= false;
1296 } else if (previous_kvno
< current_kvno
) {
1297 use_previous
= true;
1299 use_previous
= false;
1301 } else if (kvno
== current_kvno
) {
1305 use_previous
= false;
1306 } else if (kvno
== previous_kvno
) {
1310 use_previous
= true;
1313 * Fallback to the current one for anything else
1315 use_previous
= false;
1319 auth_array
= &password_blob
.previous
;
1320 auth_kvno
= &previous_kvno
;
1322 auth_array
= &password_blob
.current
;
1323 auth_kvno
= ¤t_kvno
;
1326 /* use the kvno the client specified, if available */
1327 if (flags
& SDB_F_KVNO_SPECIFIED
) {
1328 entry_ex
->entry
.kvno
= kvno
;
1330 entry_ex
->entry
.kvno
= *auth_kvno
;
1333 for (i
=0; i
< auth_array
->count
; i
++) {
1334 if (auth_array
->array
[i
].AuthType
== TRUST_AUTH_TYPE_CLEAR
) {
1337 password_utf16
= data_blob_const(auth_array
->array
[i
].AuthInfo
.clear
.password
,
1338 auth_array
->array
[i
].AuthInfo
.clear
.size
);
1339 if (password_utf16
.length
== 0) {
1343 if (supported_enctypes
& ENC_RC4_HMAC_MD5
) {
1344 mdfour(_password_hash
.hash
, password_utf16
.data
, password_utf16
.length
);
1345 if (password_hash
== NULL
) {
1348 password_hash
= &_password_hash
;
1351 if (!(supported_enctypes
& (ENC_HMAC_SHA1_96_AES128
|ENC_HMAC_SHA1_96_AES256
))) {
1355 ok
= convert_string_talloc(mem_ctx
,
1356 CH_UTF16MUNGED
, CH_UTF8
,
1357 password_utf16
.data
,
1358 password_utf16
.length
,
1359 (void *)&password_utf8
.data
,
1360 &password_utf8
.length
);
1362 krb5_clear_error_message(context
);
1367 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES128
) {
1370 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES256
) {
1374 } else if (auth_array
->array
[i
].AuthType
== TRUST_AUTH_TYPE_NT4OWF
) {
1375 if (supported_enctypes
& ENC_RC4_HMAC_MD5
) {
1376 password_hash
= &auth_array
->array
[i
].AuthInfo
.nt4owf
.password
;
1382 /* Must have found a cleartext or MD4 password */
1383 if (num_keys
== 0) {
1384 DEBUG(1,(__location__
": no usable key found\n"));
1385 krb5_clear_error_message(context
);
1386 ret
= SDB_ERR_NOENTRY
;
1390 entry_ex
->entry
.keys
.val
= calloc(num_keys
, sizeof(struct sdb_key
));
1391 if (entry_ex
->entry
.keys
.val
== NULL
) {
1392 krb5_clear_error_message(context
);
1397 if (password_utf8
.length
!= 0) {
1398 struct sdb_key key
= {};
1399 krb5_const_principal salt_principal
= entry_ex
->entry
.principal
;
1401 krb5_data cleartext_data
;
1403 cleartext_data
.data
= discard_const_p(char, password_utf8
.data
);
1404 cleartext_data
.length
= password_utf8
.length
;
1406 ret
= smb_krb5_get_pw_salt(context
,
1413 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES256
) {
1414 ret
= smb_krb5_create_key_from_string(context
,
1418 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
1421 smb_krb5_free_data_contents(context
, &salt
);
1425 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
1426 entry_ex
->entry
.keys
.len
++;
1429 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES128
) {
1430 ret
= smb_krb5_create_key_from_string(context
,
1434 ENCTYPE_AES128_CTS_HMAC_SHA1_96
,
1437 smb_krb5_free_data_contents(context
, &salt
);
1441 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
1442 entry_ex
->entry
.keys
.len
++;
1445 smb_krb5_free_data_contents(context
, &salt
);
1448 if (password_hash
!= NULL
) {
1449 struct sdb_key key
= {};
1451 ret
= smb_krb5_keyblock_init_contents(context
,
1452 ENCTYPE_ARCFOUR_HMAC
,
1453 password_hash
->hash
,
1454 sizeof(password_hash
->hash
),
1460 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
1461 entry_ex
->entry
.keys
.len
++;
1464 entry_ex
->entry
.flags
= int2SDBFlags(0);
1465 entry_ex
->entry
.flags
.immutable
= 1;
1466 entry_ex
->entry
.flags
.invalid
= 0;
1467 entry_ex
->entry
.flags
.server
= 1;
1468 entry_ex
->entry
.flags
.require_preauth
= 1;
1470 entry_ex
->entry
.pw_end
= NULL
;
1472 entry_ex
->entry
.max_life
= NULL
;
1474 entry_ex
->entry
.max_renew
= NULL
;
1476 ret
= samba_kdc_sort_encryption_keys(entry_ex
);
1478 krb5_clear_error_message(context
);
1483 p
->msg
= talloc_steal(p
, msg
);
1486 TALLOC_FREE(partner_realm
);
1489 /* This doesn't free ent itself, that is for the eventual caller to do */
1490 sdb_free_entry(entry_ex
);
1492 talloc_steal(kdc_db_ctx
, entry_ex
->ctx
);
1499 static krb5_error_code
samba_kdc_lookup_trust(krb5_context context
, struct ldb_context
*ldb_ctx
,
1500 TALLOC_CTX
*mem_ctx
,
1502 struct ldb_dn
*realm_dn
,
1503 struct ldb_message
**pmsg
)
1506 const char * const *attrs
= trust_attrs
;
1508 status
= dsdb_trust_search_tdo(ldb_ctx
, realm
, realm
,
1509 attrs
, mem_ctx
, pmsg
);
1510 if (NT_STATUS_IS_OK(status
)) {
1512 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
1513 return SDB_ERR_NOENTRY
;
1514 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MEMORY
)) {
1516 krb5_set_error_message(context
, ret
, "get_sam_result_trust: out of memory");
1520 krb5_set_error_message(context
, ret
, "get_sam_result_trust: %s", nt_errstr(status
));
1525 static krb5_error_code
samba_kdc_lookup_client(krb5_context context
,
1526 struct samba_kdc_db_context
*kdc_db_ctx
,
1527 TALLOC_CTX
*mem_ctx
,
1528 krb5_const_principal principal
,
1530 struct ldb_dn
**realm_dn
,
1531 struct ldb_message
**msg
)
1534 char *principal_string
= NULL
;
1536 if (smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
1537 principal_string
= smb_krb5_principal_get_comp_string(mem_ctx
, context
,
1539 if (principal_string
== NULL
) {
1543 char *principal_string_m
= NULL
;
1544 krb5_error_code ret
;
1546 ret
= krb5_unparse_name(context
, principal
, &principal_string_m
);
1551 principal_string
= talloc_strdup(mem_ctx
, principal_string_m
);
1552 SAFE_FREE(principal_string_m
);
1553 if (principal_string
== NULL
) {
1558 nt_status
= sam_get_results_principal(kdc_db_ctx
->samdb
,
1559 mem_ctx
, principal_string
, attrs
,
1561 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
1562 krb5_principal fallback_principal
= NULL
;
1563 unsigned int num_comp
;
1564 char *fallback_realm
= NULL
;
1565 char *fallback_account
= NULL
;
1566 krb5_error_code ret
;
1568 ret
= krb5_parse_name(context
, principal_string
,
1569 &fallback_principal
);
1570 TALLOC_FREE(principal_string
);
1575 num_comp
= krb5_princ_size(context
, fallback_principal
);
1576 fallback_realm
= smb_krb5_principal_get_realm(context
,
1577 fallback_principal
);
1578 if (fallback_realm
== NULL
) {
1579 krb5_free_principal(context
, fallback_principal
);
1583 if (num_comp
== 1) {
1586 fallback_account
= smb_krb5_principal_get_comp_string(mem_ctx
,
1587 context
, fallback_principal
, 0);
1588 if (fallback_account
== NULL
) {
1589 krb5_free_principal(context
, fallback_principal
);
1590 SAFE_FREE(fallback_realm
);
1594 len
= strlen(fallback_account
);
1595 if (len
>= 2 && fallback_account
[len
- 1] == '$') {
1596 TALLOC_FREE(fallback_account
);
1599 krb5_free_principal(context
, fallback_principal
);
1600 fallback_principal
= NULL
;
1602 if (fallback_account
!= NULL
) {
1605 with_dollar
= talloc_asprintf(mem_ctx
, "%s$",
1607 if (with_dollar
== NULL
) {
1608 SAFE_FREE(fallback_realm
);
1611 TALLOC_FREE(fallback_account
);
1613 ret
= smb_krb5_make_principal(context
,
1614 &fallback_principal
,
1617 TALLOC_FREE(with_dollar
);
1619 SAFE_FREE(fallback_realm
);
1623 SAFE_FREE(fallback_realm
);
1625 if (fallback_principal
!= NULL
) {
1626 char *fallback_string
= NULL
;
1628 ret
= krb5_unparse_name(context
,
1632 krb5_free_principal(context
, fallback_principal
);
1636 nt_status
= sam_get_results_principal(kdc_db_ctx
->samdb
,
1641 SAFE_FREE(fallback_string
);
1643 krb5_free_principal(context
, fallback_principal
);
1644 fallback_principal
= NULL
;
1646 TALLOC_FREE(principal_string
);
1648 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
1649 return SDB_ERR_NOENTRY
;
1650 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_MEMORY
)) {
1652 } else if (!NT_STATUS_IS_OK(nt_status
)) {
1659 static krb5_error_code
samba_kdc_fetch_client(krb5_context context
,
1660 struct samba_kdc_db_context
*kdc_db_ctx
,
1661 TALLOC_CTX
*mem_ctx
,
1662 krb5_const_principal principal
,
1664 struct sdb_entry_ex
*entry_ex
) {
1665 struct ldb_dn
*realm_dn
;
1666 krb5_error_code ret
;
1667 struct ldb_message
*msg
= NULL
;
1669 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
1670 mem_ctx
, principal
, user_attrs
,
1676 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1677 principal
, SAMBA_KDC_ENT_TYPE_CLIENT
,
1679 realm_dn
, msg
, entry_ex
);
1683 static krb5_error_code
samba_kdc_fetch_krbtgt(krb5_context context
,
1684 struct samba_kdc_db_context
*kdc_db_ctx
,
1685 TALLOC_CTX
*mem_ctx
,
1686 krb5_const_principal principal
,
1689 struct sdb_entry_ex
*entry_ex
)
1691 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
1692 krb5_error_code ret
;
1693 struct ldb_message
*msg
= NULL
;
1694 struct ldb_dn
*realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
1695 char *realm_from_princ
, *realm_from_princ_malloc
;
1696 char *realm_princ_comp
= smb_krb5_principal_get_comp_string(mem_ctx
, context
, principal
, 1);
1698 realm_from_princ_malloc
= smb_krb5_principal_get_realm(context
, principal
);
1699 if (realm_from_princ_malloc
== NULL
) {
1701 return SDB_ERR_NOENTRY
;
1703 realm_from_princ
= talloc_strdup(mem_ctx
, realm_from_princ_malloc
);
1704 free(realm_from_princ_malloc
);
1705 if (realm_from_princ
== NULL
) {
1706 return SDB_ERR_NOENTRY
;
1709 if (krb5_princ_size(context
, principal
) != 2
1710 || (principal_comp_strcmp(context
, principal
, 0, KRB5_TGS_NAME
) != 0)) {
1712 return SDB_ERR_NOENTRY
;
1715 /* krbtgt case. Either us or a trusted realm */
1717 if (lpcfg_is_my_domain_or_realm(lp_ctx
, realm_from_princ
)
1718 && lpcfg_is_my_domain_or_realm(lp_ctx
, realm_princ_comp
)) {
1719 /* us, or someone quite like us */
1720 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1721 * is in our db, then direct the caller at our primary
1725 unsigned int krbtgt_number
;
1726 /* w2k8r2 sometimes gives us a kvno of 255 for inter-domain
1727 trust tickets. We don't yet know what this means, but we do
1728 seem to need to treat it as unspecified */
1729 if (flags
& SDB_F_KVNO_SPECIFIED
) {
1730 krbtgt_number
= SAMBA_KVNO_GET_KRBTGT(kvno
);
1731 if (kdc_db_ctx
->rodc
) {
1732 if (krbtgt_number
!= kdc_db_ctx
->my_krbtgt_number
) {
1733 return SDB_ERR_NOT_FOUND_HERE
;
1737 krbtgt_number
= kdc_db_ctx
->my_krbtgt_number
;
1740 if (krbtgt_number
== kdc_db_ctx
->my_krbtgt_number
) {
1741 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
,
1742 &msg
, kdc_db_ctx
->krbtgt_dn
, LDB_SCOPE_BASE
,
1743 krbtgt_attrs
, DSDB_SEARCH_NO_GLOBAL_CATALOG
,
1744 "(objectClass=user)");
1746 /* We need to look up an RODC krbtgt (perhaps
1747 * ours, if we are an RODC, perhaps another
1748 * RODC if we are a read-write DC */
1749 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
,
1750 &msg
, realm_dn
, LDB_SCOPE_SUBTREE
,
1752 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
1753 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=%u))", (unsigned)(krbtgt_number
));
1756 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
1757 krb5_warnx(context
, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1758 (unsigned)(krbtgt_number
));
1759 krb5_set_error_message(context
, SDB_ERR_NOENTRY
,
1760 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1761 (unsigned)(krbtgt_number
));
1762 return SDB_ERR_NOENTRY
;
1763 } else if (lret
!= LDB_SUCCESS
) {
1764 krb5_warnx(context
, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1765 (unsigned)(krbtgt_number
));
1766 krb5_set_error_message(context
, SDB_ERR_NOENTRY
,
1767 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1768 (unsigned)(krbtgt_number
));
1769 return SDB_ERR_NOENTRY
;
1772 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1773 principal
, SAMBA_KDC_ENT_TYPE_KRBTGT
,
1774 flags
, realm_dn
, msg
, entry_ex
);
1776 krb5_warnx(context
, "samba_kdc_fetch: self krbtgt message2entry failed");
1781 enum trust_direction direction
= UNKNOWN
;
1782 const char *realm
= NULL
;
1784 /* Either an inbound or outbound trust */
1786 if (strcasecmp(lpcfg_realm(lp_ctx
), realm_from_princ
) == 0) {
1787 /* look for inbound trust */
1788 direction
= INBOUND
;
1789 realm
= realm_princ_comp
;
1790 } else if (principal_comp_strcasecmp(context
, principal
, 1, lpcfg_realm(lp_ctx
)) == 0) {
1791 /* look for outbound trust */
1792 direction
= OUTBOUND
;
1793 realm
= realm_from_princ
;
1795 krb5_warnx(context
, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1798 krb5_set_error_message(context
, SDB_ERR_NOENTRY
, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1801 return SDB_ERR_NOENTRY
;
1804 /* Trusted domains are under CN=system */
1806 ret
= samba_kdc_lookup_trust(context
, kdc_db_ctx
->samdb
,
1808 realm
, realm_dn
, &msg
);
1811 krb5_warnx(context
, "samba_kdc_fetch: could not find principal in DB");
1812 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: could not find principal in DB");
1816 ret
= samba_kdc_trust_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1817 principal
, direction
,
1818 realm_dn
, flags
, kvno
, msg
, entry_ex
);
1820 krb5_warnx(context
, "samba_kdc_fetch: trust_message2entry failed for %s",
1821 ldb_dn_get_linearized(msg
->dn
));
1822 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: "
1823 "trust_message2entry failed for %s",
1824 ldb_dn_get_linearized(msg
->dn
));
1831 static krb5_error_code
samba_kdc_lookup_server(krb5_context context
,
1832 struct samba_kdc_db_context
*kdc_db_ctx
,
1833 TALLOC_CTX
*mem_ctx
,
1834 krb5_const_principal principal
,
1837 struct ldb_dn
**realm_dn
,
1838 struct ldb_message
**msg
)
1840 krb5_error_code ret
;
1841 if ((smb_krb5_principal_get_type(context
, principal
) != KRB5_NT_ENTERPRISE_PRINCIPAL
)
1842 && krb5_princ_size(context
, principal
) >= 2) {
1843 /* 'normal server' case */
1846 struct ldb_dn
*user_dn
;
1847 char *principal_string
;
1849 ret
= krb5_unparse_name_flags(context
, principal
,
1850 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
1856 /* At this point we may find the host is known to be
1857 * in a different realm, so we should generate a
1858 * referral instead */
1859 nt_status
= crack_service_principal_name(kdc_db_ctx
->samdb
,
1860 mem_ctx
, principal_string
,
1861 &user_dn
, realm_dn
);
1862 free(principal_string
);
1864 if (!NT_STATUS_IS_OK(nt_status
)) {
1865 return SDB_ERR_NOENTRY
;
1868 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
,
1870 msg
, user_dn
, LDB_SCOPE_BASE
,
1872 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
1874 if (ldb_ret
!= LDB_SUCCESS
) {
1875 return SDB_ERR_NOENTRY
;
1878 } else if (!(flags
& SDB_F_FOR_AS_REQ
)
1879 && smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
1881 * The behaviour of accepting an
1882 * KRB5_NT_ENTERPRISE_PRINCIPAL server principal
1883 * containing a UPN only applies to TGS-REQ packets,
1884 * not AS-REQ packets.
1886 return samba_kdc_lookup_client(context
, kdc_db_ctx
,
1887 mem_ctx
, principal
, attrs
,
1892 * - the AS-REQ, where we only accept
1893 * samAccountName based lookups for the server, no
1894 * matter if the name is an
1895 * KRB5_NT_ENTERPRISE_PRINCIPAL or not
1896 * - for the TGS-REQ when we are not given an
1897 * KRB5_NT_ENTERPRISE_PRINCIPAL, which also must
1898 * only lookup samAccountName based names.
1902 krb5_principal enterprise_principal
= NULL
;
1903 krb5_const_principal used_principal
= NULL
;
1906 char *filter
= NULL
;
1908 if (smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
1910 /* Need to reparse the enterprise principal to find the real target */
1911 if (krb5_princ_size(context
, principal
) != 1) {
1912 ret
= KRB5_PARSE_MALFORMED
;
1913 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_server: request for an "
1914 "enterprise principal with wrong (%d) number of components",
1915 krb5_princ_size(context
, principal
));
1918 str
= smb_krb5_principal_get_comp_string(mem_ctx
, context
, principal
, 0);
1920 return KRB5_PARSE_MALFORMED
;
1922 ret
= krb5_parse_name(context
, str
,
1923 &enterprise_principal
);
1928 used_principal
= enterprise_principal
;
1930 used_principal
= principal
;
1933 /* server as client principal case, but we must not lookup userPrincipalNames */
1934 *realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
1936 /* TODO: Check if it is our realm, otherwise give referral */
1938 ret
= krb5_unparse_name_flags(context
, used_principal
,
1939 KRB5_PRINCIPAL_UNPARSE_NO_REALM
|
1940 KRB5_PRINCIPAL_UNPARSE_DISPLAY
,
1942 used_principal
= NULL
;
1943 krb5_free_principal(context
, enterprise_principal
);
1944 enterprise_principal
= NULL
;
1947 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_principal: could not parse principal");
1948 krb5_warnx(context
, "samba_kdc_lookup_principal: could not parse principal");
1952 name1
= ldb_binary_encode_string(mem_ctx
, short_princ
);
1953 SAFE_FREE(short_princ
);
1954 if (name1
== NULL
) {
1957 len1
= strlen(name1
);
1958 if (len1
>= 1 && name1
[len1
- 1] != '$') {
1959 filter
= talloc_asprintf(mem_ctx
,
1960 "(&(objectClass=user)(|(samAccountName=%s)(samAccountName=%s$)))",
1962 if (filter
== NULL
) {
1966 filter
= talloc_asprintf(mem_ctx
,
1967 "(&(objectClass=user)(samAccountName=%s))",
1969 if (filter
== NULL
) {
1974 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
, msg
,
1975 *realm_dn
, LDB_SCOPE_SUBTREE
,
1977 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
1979 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
1980 DEBUG(10, ("Failed to find an entry for %s filter:%s\n",
1982 return SDB_ERR_NOENTRY
;
1984 if (lret
== LDB_ERR_CONSTRAINT_VIOLATION
) {
1985 DEBUG(10, ("Failed to find unique entry for %s filter:%s\n",
1987 return SDB_ERR_NOENTRY
;
1989 if (lret
!= LDB_SUCCESS
) {
1990 DEBUG(0, ("Failed single search for %s - %s\n",
1991 name1
, ldb_errstring(kdc_db_ctx
->samdb
)));
1992 return SDB_ERR_NOENTRY
;
1996 return SDB_ERR_NOENTRY
;
2001 static krb5_error_code
samba_kdc_fetch_server(krb5_context context
,
2002 struct samba_kdc_db_context
*kdc_db_ctx
,
2003 TALLOC_CTX
*mem_ctx
,
2004 krb5_const_principal principal
,
2006 struct sdb_entry_ex
*entry_ex
)
2008 krb5_error_code ret
;
2009 struct ldb_dn
*realm_dn
;
2010 struct ldb_message
*msg
;
2012 ret
= samba_kdc_lookup_server(context
, kdc_db_ctx
, mem_ctx
, principal
,
2013 flags
, server_attrs
, &realm_dn
, &msg
);
2018 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
2019 principal
, SAMBA_KDC_ENT_TYPE_SERVER
,
2021 realm_dn
, msg
, entry_ex
);
2023 krb5_warnx(context
, "samba_kdc_fetch: message2entry failed");
2029 static krb5_error_code
samba_kdc_lookup_realm(krb5_context context
,
2030 struct samba_kdc_db_context
*kdc_db_ctx
,
2031 TALLOC_CTX
*mem_ctx
,
2032 krb5_const_principal principal
,
2034 struct sdb_entry_ex
*entry_ex
)
2036 TALLOC_CTX
*frame
= talloc_stackframe();
2038 krb5_error_code ret
;
2039 char *_realm
= NULL
;
2040 bool check_realm
= false;
2041 const char *realm
= NULL
;
2042 struct dsdb_trust_routing_table
*trt
= NULL
;
2043 const struct lsa_TrustDomainInfoInfoEx
*tdo
= NULL
;
2044 unsigned int num_comp
;
2048 num_comp
= krb5_princ_size(context
, principal
);
2050 if (flags
& SDB_F_GET_CLIENT
) {
2051 if (flags
& SDB_F_FOR_AS_REQ
) {
2055 if (flags
& SDB_F_GET_SERVER
) {
2056 if (flags
& SDB_F_FOR_TGS_REQ
) {
2066 _realm
= smb_krb5_principal_get_realm(context
, principal
);
2067 if (_realm
== NULL
) {
2073 * The requested realm needs to be our own
2075 ok
= lpcfg_is_my_domain_or_realm(kdc_db_ctx
->lp_ctx
, _realm
);
2078 * The request is not for us...
2082 return SDB_ERR_NOENTRY
;
2085 realm
= talloc_strdup(frame
, _realm
);
2087 if (realm
== NULL
) {
2092 if (smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
2093 char *principal_string
= NULL
;
2094 krb5_principal enterprise_principal
= NULL
;
2095 char *enterprise_realm
= NULL
;
2097 if (num_comp
!= 1) {
2099 return SDB_ERR_NOENTRY
;
2102 principal_string
= smb_krb5_principal_get_comp_string(frame
, context
,
2104 if (principal_string
== NULL
) {
2109 ret
= krb5_parse_name(context
, principal_string
,
2110 &enterprise_principal
);
2111 TALLOC_FREE(principal_string
);
2117 enterprise_realm
= smb_krb5_principal_get_realm(context
,
2118 enterprise_principal
);
2119 krb5_free_principal(context
, enterprise_principal
);
2120 if (enterprise_realm
!= NULL
) {
2121 realm
= talloc_strdup(frame
, enterprise_realm
);
2122 SAFE_FREE(enterprise_realm
);
2123 if (realm
== NULL
) {
2130 if (flags
& SDB_F_GET_SERVER
) {
2131 char *service_realm
= NULL
;
2133 ret
= principal_comp_strcmp(context
, principal
, 0, KRB5_TGS_NAME
);
2136 * we need to search krbtgt/ locally
2143 * We need to check the last component against the routing table.
2145 * Note this works only with 2 or 3 component principals, e.g:
2147 * servicePrincipalName: ldap/W2K8R2-219.bla.base
2148 * servicePrincipalName: ldap/W2K8R2-219.bla.base/bla.base
2149 * servicePrincipalName: ldap/W2K8R2-219.bla.base/ForestDnsZones.bla.base
2150 * servicePrincipalName: ldap/W2K8R2-219.bla.base/DomainDnsZones.bla.base
2153 if (num_comp
== 2 || num_comp
== 3) {
2154 service_realm
= smb_krb5_principal_get_comp_string(frame
,
2160 if (service_realm
!= NULL
) {
2161 realm
= service_realm
;
2165 ok
= lpcfg_is_my_domain_or_realm(kdc_db_ctx
->lp_ctx
, realm
);
2168 * skip the expensive routing lookup
2174 status
= dsdb_trust_routing_table_load(kdc_db_ctx
->samdb
,
2176 if (!NT_STATUS_IS_OK(status
)) {
2181 tdo
= dsdb_trust_routing_by_name(trt
, realm
);
2184 * This principal has to be local
2190 if (tdo
->trust_attributes
& LSA_TRUST_ATTRIBUTE_WITHIN_FOREST
) {
2192 * TODO: handle the routing within the forest
2194 * This should likely be handled in
2195 * samba_kdc_message2entry() in case we're
2196 * a global catalog. We'd need to check
2197 * if realm_dn is our own domain and derive
2198 * the dns domain name from realm_dn and check that
2199 * against the routing table or fallback to
2200 * the tdo we found here.
2202 * But for now we don't support multiple domains
2203 * in our forest correctly anyway.
2205 * Just search in our local database.
2211 ZERO_STRUCT(entry_ex
->entry
);
2213 ret
= krb5_copy_principal(context
, principal
,
2214 &entry_ex
->entry
.principal
);
2220 upper
= strupper_talloc(frame
, tdo
->domain_name
.string
);
2221 if (upper
== NULL
) {
2226 ret
= smb_krb5_principal_set_realm(context
,
2227 entry_ex
->entry
.principal
,
2235 return SDB_ERR_WRONG_REALM
;
2238 krb5_error_code
samba_kdc_fetch(krb5_context context
,
2239 struct samba_kdc_db_context
*kdc_db_ctx
,
2240 krb5_const_principal principal
,
2243 struct sdb_entry_ex
*entry_ex
)
2245 krb5_error_code ret
= SDB_ERR_NOENTRY
;
2246 TALLOC_CTX
*mem_ctx
;
2248 mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_fetch context");
2251 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
2255 ret
= samba_kdc_lookup_realm(context
, kdc_db_ctx
, mem_ctx
,
2256 principal
, flags
, entry_ex
);
2261 ret
= SDB_ERR_NOENTRY
;
2263 if (flags
& SDB_F_GET_CLIENT
) {
2264 ret
= samba_kdc_fetch_client(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, entry_ex
);
2265 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2267 if (flags
& SDB_F_GET_SERVER
) {
2268 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
2269 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, kvno
, entry_ex
);
2270 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2272 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
2273 ret
= samba_kdc_fetch_server(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, entry_ex
);
2274 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2276 if (flags
& SDB_F_GET_KRBTGT
) {
2277 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, kvno
, entry_ex
);
2278 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2282 talloc_free(mem_ctx
);
2286 struct samba_kdc_seq
{
2289 struct ldb_message
**msgs
;
2290 struct ldb_dn
*realm_dn
;
2293 static krb5_error_code
samba_kdc_seq(krb5_context context
,
2294 struct samba_kdc_db_context
*kdc_db_ctx
,
2295 struct sdb_entry_ex
*entry
)
2297 krb5_error_code ret
;
2298 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
2299 const char *realm
= lpcfg_realm(kdc_db_ctx
->lp_ctx
);
2300 struct ldb_message
*msg
= NULL
;
2301 const char *sAMAccountName
= NULL
;
2302 krb5_principal principal
= NULL
;
2303 TALLOC_CTX
*mem_ctx
;
2306 return SDB_ERR_NOENTRY
;
2309 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_seq context");
2313 krb5_set_error_message(context
, ret
, "samba_kdc_seq: talloc_named() failed!");
2317 while (priv
->index
< priv
->count
) {
2318 msg
= priv
->msgs
[priv
->index
++];
2320 sAMAccountName
= ldb_msg_find_attr_as_string(msg
, "sAMAccountName", NULL
);
2321 if (sAMAccountName
!= NULL
) {
2326 if (sAMAccountName
== NULL
) {
2327 ret
= SDB_ERR_NOENTRY
;
2331 ret
= smb_krb5_make_principal(context
, &principal
,
2332 realm
, sAMAccountName
, NULL
);
2337 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
2338 principal
, SAMBA_KDC_ENT_TYPE_ANY
,
2339 SDB_F_ADMIN_DATA
|SDB_F_GET_ANY
,
2340 priv
->realm_dn
, msg
, entry
);
2343 if (principal
!= NULL
) {
2344 krb5_free_principal(context
, principal
);
2349 kdc_db_ctx
->seq_ctx
= NULL
;
2351 talloc_free(mem_ctx
);
2357 krb5_error_code
samba_kdc_firstkey(krb5_context context
,
2358 struct samba_kdc_db_context
*kdc_db_ctx
,
2359 struct sdb_entry_ex
*entry
)
2361 struct ldb_context
*ldb_ctx
= kdc_db_ctx
->samdb
;
2362 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
2364 struct ldb_result
*res
= NULL
;
2365 krb5_error_code ret
;
2366 TALLOC_CTX
*mem_ctx
;
2371 kdc_db_ctx
->seq_ctx
= NULL
;
2374 priv
= (struct samba_kdc_seq
*) talloc(kdc_db_ctx
, struct samba_kdc_seq
);
2377 krb5_set_error_message(context
, ret
, "talloc: out of memory");
2383 priv
->realm_dn
= ldb_get_default_basedn(ldb_ctx
);
2386 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_firstkey context");
2390 krb5_set_error_message(context
, ret
, "samba_kdc_firstkey: talloc_named() failed!");
2394 ret
= krb5_get_default_realm(context
, &realm
);
2399 krb5_free_default_realm(context
, realm
);
2401 lret
= dsdb_search(ldb_ctx
, priv
, &res
,
2402 priv
->realm_dn
, LDB_SCOPE_SUBTREE
, user_attrs
,
2403 DSDB_SEARCH_NO_GLOBAL_CATALOG
,
2404 "(objectClass=user)");
2406 if (lret
!= LDB_SUCCESS
) {
2408 return SDB_ERR_NOENTRY
;
2411 priv
->count
= res
->count
;
2412 priv
->msgs
= talloc_steal(priv
, res
->msgs
);
2415 kdc_db_ctx
->seq_ctx
= priv
;
2417 ret
= samba_kdc_seq(context
, kdc_db_ctx
, entry
);
2421 kdc_db_ctx
->seq_ctx
= NULL
;
2423 talloc_free(mem_ctx
);
2428 krb5_error_code
samba_kdc_nextkey(krb5_context context
,
2429 struct samba_kdc_db_context
*kdc_db_ctx
,
2430 struct sdb_entry_ex
*entry
)
2432 return samba_kdc_seq(context
, kdc_db_ctx
, entry
);
2435 /* Check if a given entry may delegate or do s4u2self to this target principal
2437 * This is currently a very nasty hack - allowing only delegation to itself.
2440 samba_kdc_check_s4u2self(krb5_context context
,
2441 struct samba_kdc_db_context
*kdc_db_ctx
,
2442 struct samba_kdc_entry
*skdc_entry
,
2443 krb5_const_principal target_principal
)
2445 krb5_error_code ret
;
2446 struct ldb_dn
*realm_dn
;
2447 struct ldb_message
*msg
;
2448 struct dom_sid
*orig_sid
;
2449 struct dom_sid
*target_sid
;
2450 const char *delegation_check_attrs
[] = {
2454 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_s4u2self");
2458 krb5_set_error_message(context
, ret
, "samba_kdc_check_s4u2self: talloc_named() failed!");
2462 ret
= samba_kdc_lookup_server(context
, kdc_db_ctx
, mem_ctx
, target_principal
,
2463 SDB_F_GET_CLIENT
|SDB_F_GET_SERVER
,
2464 delegation_check_attrs
, &realm_dn
, &msg
);
2467 talloc_free(mem_ctx
);
2471 orig_sid
= samdb_result_dom_sid(mem_ctx
, skdc_entry
->msg
, "objectSid");
2472 target_sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
2474 /* Allow delegation to the same principal, even if by a different
2475 * name. The easy and safe way to prove this is by SID
2477 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
2478 talloc_free(mem_ctx
);
2479 return KRB5KDC_ERR_BADOPTION
;
2482 talloc_free(mem_ctx
);
2486 /* Certificates printed by a the Certificate Authority might have a
2487 * slightly different form of the user principal name to that in the
2488 * database. Allow a mismatch where they both refer to the same
2492 samba_kdc_check_pkinit_ms_upn_match(krb5_context context
,
2493 struct samba_kdc_db_context
*kdc_db_ctx
,
2494 struct samba_kdc_entry
*skdc_entry
,
2495 krb5_const_principal certificate_principal
)
2497 krb5_error_code ret
;
2498 struct ldb_dn
*realm_dn
;
2499 struct ldb_message
*msg
;
2500 struct dom_sid
*orig_sid
;
2501 struct dom_sid
*target_sid
;
2502 const char *ms_upn_check_attrs
[] = {
2506 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_pkinit_ms_upn_match");
2510 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
2514 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
2515 mem_ctx
, certificate_principal
,
2516 ms_upn_check_attrs
, &realm_dn
, &msg
);
2519 talloc_free(mem_ctx
);
2523 orig_sid
= samdb_result_dom_sid(mem_ctx
, skdc_entry
->msg
, "objectSid");
2524 target_sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
2526 /* Consider these to be the same principal, even if by a different
2527 * name. The easy and safe way to prove this is by SID
2529 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
2530 talloc_free(mem_ctx
);
2531 #ifdef KRB5_KDC_ERR_CLIENT_NAME_MISMATCH /* Heimdal */
2532 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
2533 #elif defined(KRB5KDC_ERR_CLIENT_NAME_MISMATCH) /* MIT */
2534 return KRB5KDC_ERR_CLIENT_NAME_MISMATCH
;
2538 talloc_free(mem_ctx
);
2543 * Check if a given entry may delegate to this target principal
2547 samba_kdc_check_s4u2proxy(krb5_context context
,
2548 struct samba_kdc_db_context
*kdc_db_ctx
,
2549 struct samba_kdc_entry
*skdc_entry
,
2550 krb5_const_principal target_principal
)
2552 krb5_error_code ret
;
2554 const char *client_dn
= NULL
;
2555 const char *target_principal_name
= NULL
;
2556 struct ldb_message_element
*el
;
2561 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_s4u2proxy");
2565 krb5_set_error_message(context
, ret
,
2566 "samba_kdc_check_s4u2proxy:"
2567 " talloc_named() failed!");
2571 client_dn
= ldb_dn_get_linearized(skdc_entry
->msg
->dn
);
2577 krb5_set_error_message(context
, ret
,
2578 "samba_kdc_check_s4u2proxy:"
2579 " ldb_dn_get_linearized() failed!");
2584 * The main heimdal code already checked that the target_principal
2585 * belongs to the same realm as the client.
2587 * So we just need the principal without the realm,
2588 * as that is what is configured in the "msDS-AllowedToDelegateTo"
2591 ret
= krb5_unparse_name_flags(context
, target_principal
,
2592 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &tmp
);
2594 talloc_free(mem_ctx
);
2595 krb5_set_error_message(context
, ret
,
2596 "samba_kdc_check_s4u2proxy:"
2597 " krb5_unparse_name() failed!");
2600 DEBUG(10,("samba_kdc_check_s4u2proxy: client[%s] for target[%s]\n",
2603 target_principal_name
= talloc_strdup(mem_ctx
, tmp
);
2605 if (target_principal_name
== NULL
) {
2607 krb5_set_error_message(context
, ret
,
2608 "samba_kdc_check_s4u2proxy:"
2609 " talloc_strdup() failed!");
2613 el
= ldb_msg_find_element(skdc_entry
->msg
, "msDS-AllowedToDelegateTo");
2618 val
= data_blob_string_const(target_principal_name
);
2620 for (i
=0; i
<el
->num_values
; i
++) {
2621 struct ldb_val
*val1
= &val
;
2622 struct ldb_val
*val2
= &el
->values
[i
];
2625 if (val1
->length
!= val2
->length
) {
2629 cmp
= strncasecmp((const char *)val1
->data
,
2630 (const char *)val2
->data
,
2644 DEBUG(10,("samba_kdc_check_s4u2proxy: client[%s] allowed target[%s]\n",
2646 talloc_free(mem_ctx
);
2650 krb5_set_error_message(context
, ret
,
2651 "samba_kdc_check_s4u2proxy: client[%s] "
2652 "not allowed for delegation to target[%s]",
2654 target_principal_name
);
2655 talloc_free(mem_ctx
);
2656 return KRB5KDC_ERR_BADOPTION
;
2659 NTSTATUS
samba_kdc_setup_db_ctx(TALLOC_CTX
*mem_ctx
, struct samba_kdc_base_context
*base_ctx
,
2660 struct samba_kdc_db_context
**kdc_db_ctx_out
)
2663 struct ldb_message
*msg
;
2664 struct auth_session_info
*session_info
;
2665 struct samba_kdc_db_context
*kdc_db_ctx
;
2666 /* The idea here is very simple. Using Kerberos to
2667 * authenticate the KDC to the LDAP server is higly likely to
2670 * In future we may set this up to use EXERNAL and SSL
2671 * certificates, for now it will almost certainly be NTLMSSP_SET_USERNAME
2674 kdc_db_ctx
= talloc_zero(mem_ctx
, struct samba_kdc_db_context
);
2675 if (kdc_db_ctx
== NULL
) {
2676 return NT_STATUS_NO_MEMORY
;
2678 kdc_db_ctx
->ev_ctx
= base_ctx
->ev_ctx
;
2679 kdc_db_ctx
->lp_ctx
= base_ctx
->lp_ctx
;
2680 kdc_db_ctx
->msg_ctx
= base_ctx
->msg_ctx
;
2682 /* get default kdc policy */
2683 lpcfg_default_kdc_policy(base_ctx
->lp_ctx
,
2684 &kdc_db_ctx
->policy
.svc_tkt_lifetime
,
2685 &kdc_db_ctx
->policy
.usr_tkt_lifetime
,
2686 &kdc_db_ctx
->policy
.renewal_lifetime
);
2688 session_info
= system_session(kdc_db_ctx
->lp_ctx
);
2689 if (session_info
== NULL
) {
2690 return NT_STATUS_INTERNAL_ERROR
;
2693 /* Setup the link to LDB */
2694 kdc_db_ctx
->samdb
= samdb_connect(kdc_db_ctx
, base_ctx
->ev_ctx
,
2695 base_ctx
->lp_ctx
, session_info
, 0);
2696 if (kdc_db_ctx
->samdb
== NULL
) {
2697 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot open samdb for KDC backend!"));
2698 talloc_free(kdc_db_ctx
);
2699 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2702 /* Find out our own krbtgt kvno */
2703 ldb_ret
= samdb_rodc(kdc_db_ctx
->samdb
, &kdc_db_ctx
->rodc
);
2704 if (ldb_ret
!= LDB_SUCCESS
) {
2705 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine if we are an RODC in KDC backend: %s\n",
2706 ldb_errstring(kdc_db_ctx
->samdb
)));
2707 talloc_free(kdc_db_ctx
);
2708 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2710 if (kdc_db_ctx
->rodc
) {
2711 int my_krbtgt_number
;
2712 const char *secondary_keytab
[] = { "msDS-SecondaryKrbTgtNumber", NULL
};
2713 struct ldb_dn
*account_dn
;
2714 struct ldb_dn
*server_dn
= samdb_server_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
);
2716 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine server DN in KDC backend: %s\n",
2717 ldb_errstring(kdc_db_ctx
->samdb
)));
2718 talloc_free(kdc_db_ctx
);
2719 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2722 ldb_ret
= samdb_reference_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
, server_dn
,
2723 "serverReference", &account_dn
);
2724 if (ldb_ret
!= LDB_SUCCESS
) {
2725 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine server account in KDC backend: %s\n",
2726 ldb_errstring(kdc_db_ctx
->samdb
)));
2727 talloc_free(kdc_db_ctx
);
2728 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2731 ldb_ret
= samdb_reference_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
, account_dn
,
2732 "msDS-KrbTgtLink", &kdc_db_ctx
->krbtgt_dn
);
2733 talloc_free(account_dn
);
2734 if (ldb_ret
!= LDB_SUCCESS
) {
2735 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine RODC krbtgt account in KDC backend: %s\n",
2736 ldb_errstring(kdc_db_ctx
->samdb
)));
2737 talloc_free(kdc_db_ctx
);
2738 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2741 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
, kdc_db_ctx
,
2742 &msg
, kdc_db_ctx
->krbtgt_dn
, LDB_SCOPE_BASE
,
2744 DSDB_SEARCH_NO_GLOBAL_CATALOG
,
2745 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=*))");
2746 if (ldb_ret
!= LDB_SUCCESS
) {
2747 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot read krbtgt account %s in KDC backend to get msDS-SecondaryKrbTgtNumber: %s: %s\n",
2748 ldb_dn_get_linearized(kdc_db_ctx
->krbtgt_dn
),
2749 ldb_errstring(kdc_db_ctx
->samdb
),
2750 ldb_strerror(ldb_ret
)));
2751 talloc_free(kdc_db_ctx
);
2752 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2754 my_krbtgt_number
= ldb_msg_find_attr_as_int(msg
, "msDS-SecondaryKrbTgtNumber", -1);
2755 if (my_krbtgt_number
== -1) {
2756 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot read msDS-SecondaryKrbTgtNumber from krbtgt account %s in KDC backend: got %d\n",
2757 ldb_dn_get_linearized(kdc_db_ctx
->krbtgt_dn
),
2759 talloc_free(kdc_db_ctx
);
2760 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2762 kdc_db_ctx
->my_krbtgt_number
= my_krbtgt_number
;
2765 kdc_db_ctx
->my_krbtgt_number
= 0;
2766 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
, kdc_db_ctx
,
2768 ldb_get_default_basedn(kdc_db_ctx
->samdb
),
2771 DSDB_SEARCH_NO_GLOBAL_CATALOG
,
2772 "(&(objectClass=user)(samAccountName=krbtgt))");
2774 if (ldb_ret
!= LDB_SUCCESS
) {
2775 DEBUG(1, ("samba_kdc_fetch: could not find own KRBTGT in DB: %s\n", ldb_errstring(kdc_db_ctx
->samdb
)));
2776 talloc_free(kdc_db_ctx
);
2777 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2779 kdc_db_ctx
->krbtgt_dn
= talloc_steal(kdc_db_ctx
, msg
->dn
);
2780 kdc_db_ctx
->my_krbtgt_number
= 0;
2783 *kdc_db_ctx_out
= kdc_db_ctx
;
2784 return NT_STATUS_OK
;