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"
38 #include "librpc/gen_ndr/ndr_irpc_c.h"
39 #include "lib/messaging/irpc.h"
42 #define SAMBA_KVNO_GET_KRBTGT(kvno) \
43 ((uint16_t)(((uint32_t)kvno) >> 16))
45 #define SAMBA_KVNO_AND_KRBTGT(kvno, krbtgt) \
46 ((krb5_kvno)((((uint32_t)kvno) & 0xFFFF) | \
47 ((((uint32_t)krbtgt) << 16) & 0xFFFF0000)))
49 enum samba_kdc_ent_type
50 { SAMBA_KDC_ENT_TYPE_CLIENT
, SAMBA_KDC_ENT_TYPE_SERVER
,
51 SAMBA_KDC_ENT_TYPE_KRBTGT
, SAMBA_KDC_ENT_TYPE_TRUST
, SAMBA_KDC_ENT_TYPE_ANY
};
53 enum trust_direction
{
55 INBOUND
= LSA_TRUST_DIRECTION_INBOUND
,
56 OUTBOUND
= LSA_TRUST_DIRECTION_OUTBOUND
59 static const char *trust_attrs
[] = {
66 "msDS-TrustForestTrustInfo",
70 "msDS-SupportedEncryptionTypes",
75 send a message to the drepl server telling it to initiate a
76 REPL_SECRET getncchanges extended op to fetch the users secrets
78 static void auth_sam_trigger_repl_secret(TALLOC_CTX
*mem_ctx
,
79 struct imessaging_context
*msg_ctx
,
80 struct tevent_context
*event_ctx
,
81 struct ldb_dn
*user_dn
)
83 struct dcerpc_binding_handle
*irpc_handle
;
84 struct drepl_trigger_repl_secret r
;
85 struct tevent_req
*req
;
88 tmp_ctx
= talloc_new(mem_ctx
);
89 if (tmp_ctx
== NULL
) {
93 irpc_handle
= irpc_binding_handle_by_name(tmp_ctx
, msg_ctx
,
96 if (irpc_handle
== NULL
) {
97 DEBUG(1,(__location__
": Unable to get binding handle for dreplsrv\n"));
102 r
.in
.user_dn
= ldb_dn_get_linearized(user_dn
);
105 * This seem to rely on the current IRPC implementation,
106 * which delivers the message in the _send function.
108 * TODO: we need a ONE_WAY IRPC handle and register
109 * a callback and wait for it to be triggered!
111 req
= dcerpc_drepl_trigger_repl_secret_r_send(tmp_ctx
,
116 /* we aren't interested in a reply */
118 TALLOC_FREE(tmp_ctx
);
121 static time_t ldb_msg_find_krb5time_ldap_time(struct ldb_message
*msg
, const char *attr
, time_t default_val
)
127 gentime
= ldb_msg_find_attr_as_string(msg
, attr
, NULL
);
131 tmp
= strptime(gentime
, "%Y%m%d%H%M%SZ", &tm
);
139 static struct SDBFlags
uf2SDBFlags(krb5_context context
, uint32_t userAccountControl
, enum samba_kdc_ent_type ent_type
)
141 struct SDBFlags flags
= int2SDBFlags(0);
143 /* we don't allow kadmin deletes */
146 /* mark the principal as invalid to start with */
151 /* All accounts are servers, but this may be disabled again in the caller */
154 /* Account types - clear the invalid bit if it turns out to be valid */
155 if (userAccountControl
& UF_NORMAL_ACCOUNT
) {
156 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
162 if (userAccountControl
& UF_INTERDOMAIN_TRUST_ACCOUNT
) {
163 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
168 if (userAccountControl
& UF_WORKSTATION_TRUST_ACCOUNT
) {
169 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
174 if (userAccountControl
& UF_SERVER_TRUST_ACCOUNT
) {
175 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
181 /* Not permitted to act as a client if disabled */
182 if (userAccountControl
& UF_ACCOUNTDISABLE
) {
185 if (userAccountControl
& UF_LOCKOUT
) {
186 flags
.locked_out
= 1;
189 if (userAccountControl & UF_PASSWORD_NOTREQD) {
194 UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
196 if (userAccountControl
& UF_TEMP_DUPLICATE_ACCOUNT
) {
200 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in samba_kdc_message2entry() */
203 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
207 if (userAccountControl
& UF_SMARTCARD_REQUIRED
) {
208 flags
.require_hwauth
= 1;
210 if (userAccountControl
& UF_TRUSTED_FOR_DELEGATION
) {
211 flags
.ok_as_delegate
= 1;
213 if (userAccountControl
& UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION
) {
215 * this is confusing...
217 * UF_TRUSTED_FOR_DELEGATION
222 * UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION
223 * => trusted_for_delegation
225 flags
.trusted_for_delegation
= 1;
227 if (!(userAccountControl
& UF_NOT_DELEGATED
)) {
228 flags
.forwardable
= 1;
232 if (userAccountControl
& UF_DONT_REQUIRE_PREAUTH
) {
233 flags
.require_preauth
= 0;
235 flags
.require_preauth
= 1;
241 static int samba_kdc_entry_destructor(struct samba_kdc_entry
*p
)
243 if (p
->entry_ex
!= NULL
) {
244 struct sdb_entry_ex
*entry_ex
= p
->entry_ex
;
245 free_sdb_entry(&entry_ex
->entry
);
252 * Sort keys in descending order of strength.
254 * Explanaton from Greg Hudson:
256 * To encrypt tickets only the first returned key is used by the MIT KDC. The
257 * other keys just communicate support for session key enctypes, and aren't
258 * really used. The encryption key for the ticket enc part doesn't have
259 * to be of a type requested by the client. The session key enctype is chosen
260 * based on the client preference order, limited by the set of enctypes present
261 * in the server keys (unless the string attribute is set on the server
262 * principal overriding that set).
264 static int samba_kdc_sort_encryption_keys(struct sdb_entry_ex
*entry_ex
)
266 unsigned int i
, j
, idx
= 0;
267 static const krb5_enctype etype_list
[] = {
268 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
269 ENCTYPE_AES128_CTS_HMAC_SHA1_96
,
270 ENCTYPE_DES3_CBC_SHA1
,
271 ENCTYPE_ARCFOUR_HMAC
,
277 size_t etype_len
= ARRAY_SIZE(etype_list
);
278 size_t keys_size
= entry_ex
->entry
.keys
.len
;
279 struct sdb_key
*keys
= entry_ex
->entry
.keys
.val
;
280 struct sdb_key
*sorted_keys
;
282 sorted_keys
= calloc(keys_size
, sizeof(struct sdb_key
));
283 if (sorted_keys
== NULL
) {
287 for (i
= 0; i
< etype_len
; i
++) {
288 for (j
= 0; j
< keys_size
; j
++) {
289 const struct sdb_key skey
= keys
[j
];
291 if (idx
== keys_size
) {
295 if (KRB5_KEY_TYPE(&skey
.key
) == etype_list
[i
]) {
296 sorted_keys
[idx
] = skey
;
302 /* Paranoia: Something went wrong during data copy */
303 if (idx
!= keys_size
) {
308 free(entry_ex
->entry
.keys
.val
);
309 entry_ex
->entry
.keys
.val
= sorted_keys
;
314 static krb5_error_code
samba_kdc_message2entry_keys(krb5_context context
,
315 struct samba_kdc_db_context
*kdc_db_ctx
,
317 struct ldb_message
*msg
,
320 uint32_t userAccountControl
,
321 enum samba_kdc_ent_type ent_type
,
322 struct sdb_entry_ex
*entry_ex
)
324 krb5_error_code ret
= 0;
325 enum ndr_err_code ndr_err
;
326 struct samr_Password
*hash
;
327 const struct ldb_val
*sc_val
;
328 struct supplementalCredentialsBlob scb
;
329 struct supplementalCredentialsPackage
*scpk
= NULL
;
330 bool newer_keys
= false;
331 struct package_PrimaryKerberosBlob _pkb
;
332 struct package_PrimaryKerberosCtr3
*pkb3
= NULL
;
333 struct package_PrimaryKerberosCtr4
*pkb4
= NULL
;
335 uint16_t allocated_keys
= 0;
336 int rodc_krbtgt_number
= 0;
338 uint32_t supported_enctypes
339 = ldb_msg_find_attr_as_uint(msg
,
340 "msDS-SupportedEncryptionTypes",
343 if (rid
== DOMAIN_RID_KRBTGT
|| is_rodc
) {
344 /* KDCs (and KDCs on RODCs) use AES */
345 supported_enctypes
|= ENC_HMAC_SHA1_96_AES128
| ENC_HMAC_SHA1_96_AES256
;
346 } else if (userAccountControl
& (UF_PARTIAL_SECRETS_ACCOUNT
|UF_SERVER_TRUST_ACCOUNT
)) {
347 /* DCs and RODCs comptuer accounts use AES */
348 supported_enctypes
|= ENC_HMAC_SHA1_96_AES128
| ENC_HMAC_SHA1_96_AES256
;
349 } else if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
||
350 (ent_type
== SAMBA_KDC_ENT_TYPE_ANY
)) {
351 /* for AS-REQ the client chooses the enc types it
352 * supports, and this will vary between computers a
355 * likewise for 'any' return as much as is supported,
356 * to export into a keytab */
357 supported_enctypes
= ENC_ALL_TYPES
;
360 /* If UF_USE_DES_KEY_ONLY has been set, then don't allow use of the newer enc types */
361 if (userAccountControl
& UF_USE_DES_KEY_ONLY
) {
362 supported_enctypes
= ENC_CRC32
|ENC_RSA_MD5
;
364 /* Otherwise, add in the default enc types */
365 supported_enctypes
|= ENC_CRC32
| ENC_RSA_MD5
| ENC_RC4_HMAC_MD5
;
368 /* Is this the krbtgt or a RODC krbtgt */
370 rodc_krbtgt_number
= ldb_msg_find_attr_as_int(msg
, "msDS-SecondaryKrbTgtNumber", -1);
372 if (rodc_krbtgt_number
== -1) {
377 entry_ex
->entry
.keys
.val
= NULL
;
378 entry_ex
->entry
.keys
.len
= 0;
379 entry_ex
->entry
.kvno
= 0;
381 if ((ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
)
382 && (userAccountControl
& UF_SMARTCARD_REQUIRED
)) {
383 uint8_t secretbuffer
[32];
386 * Fake keys until we have a better way to reject
387 * non-pkinit requests.
389 * We just need to indicate which encryption types are
392 generate_secret_buffer(secretbuffer
, sizeof(secretbuffer
));
395 entry_ex
->entry
.keys
.len
= 0;
396 entry_ex
->entry
.keys
.val
= calloc(allocated_keys
, sizeof(struct sdb_key
));
397 if (entry_ex
->entry
.keys
.val
== NULL
) {
398 ZERO_STRUCT(secretbuffer
);
403 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES256
) {
404 struct sdb_key key
= {};
406 ret
= smb_krb5_keyblock_init_contents(context
,
407 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
411 ZERO_STRUCT(secretbuffer
);
415 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
416 entry_ex
->entry
.keys
.len
++;
419 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES128
) {
420 struct sdb_key key
= {};
422 ret
= smb_krb5_keyblock_init_contents(context
,
423 ENCTYPE_AES128_CTS_HMAC_SHA1_96
,
427 ZERO_STRUCT(secretbuffer
);
431 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
432 entry_ex
->entry
.keys
.len
++;
435 if (supported_enctypes
& ENC_RC4_HMAC_MD5
) {
436 struct sdb_key key
= {};
438 ret
= smb_krb5_keyblock_init_contents(context
,
439 ENCTYPE_ARCFOUR_HMAC
,
443 ZERO_STRUCT(secretbuffer
);
447 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
448 entry_ex
->entry
.keys
.len
++;
455 kvno
= ldb_msg_find_attr_as_int(msg
, "msDS-KeyVersionNumber", 0);
457 kvno
= SAMBA_KVNO_AND_KRBTGT(kvno
, rodc_krbtgt_number
);
459 entry_ex
->entry
.kvno
= kvno
;
461 /* Get keys from the db */
463 hash
= samdb_result_hash(mem_ctx
, msg
, "unicodePwd");
464 sc_val
= ldb_msg_find_ldb_val(msg
, "supplementalCredentials");
466 /* unicodePwd for enctype 0x17 (23) if present */
471 /* supplementalCredentials if present */
473 ndr_err
= ndr_pull_struct_blob_all(sc_val
, mem_ctx
, &scb
,
474 (ndr_pull_flags_fn_t
)ndr_pull_supplementalCredentialsBlob
);
475 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
476 dump_data(0, sc_val
->data
, sc_val
->length
);
481 if (scb
.sub
.signature
!= SUPPLEMENTAL_CREDENTIALS_SIGNATURE
) {
482 if (scb
.sub
.num_packages
!= 0) {
483 NDR_PRINT_DEBUG(supplementalCredentialsBlob
, &scb
);
489 for (i
=0; i
< scb
.sub
.num_packages
; i
++) {
490 if (strcmp("Primary:Kerberos-Newer-Keys", scb
.sub
.packages
[i
].name
) == 0) {
491 scpk
= &scb
.sub
.packages
[i
];
492 if (!scpk
->data
|| !scpk
->data
[0]) {
498 } else if (strcmp("Primary:Kerberos", scb
.sub
.packages
[i
].name
) == 0) {
499 scpk
= &scb
.sub
.packages
[i
];
500 if (!scpk
->data
|| !scpk
->data
[0]) {
504 * we don't break here in hope to find
505 * a Kerberos-Newer-Keys package
511 * Primary:Kerberos-Newer-Keys or Primary:Kerberos element
512 * of supplementalCredentials
517 blob
= strhex_to_data_blob(mem_ctx
, scpk
->data
);
523 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
524 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, &_pkb
,
525 (ndr_pull_flags_fn_t
)ndr_pull_package_PrimaryKerberosBlob
);
526 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
528 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
529 krb5_warnx(context
, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
533 if (newer_keys
&& _pkb
.version
!= 4) {
535 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
536 krb5_warnx(context
, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
540 if (!newer_keys
&& _pkb
.version
!= 3) {
542 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
543 krb5_warnx(context
, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
547 if (_pkb
.version
== 4) {
548 pkb4
= &_pkb
.ctr
.ctr4
;
549 allocated_keys
+= pkb4
->num_keys
;
550 } else if (_pkb
.version
== 3) {
551 pkb3
= &_pkb
.ctr
.ctr3
;
552 allocated_keys
+= pkb3
->num_keys
;
556 if (allocated_keys
== 0) {
557 if (kdc_db_ctx
->rodc
) {
558 /* We are on an RODC, but don't have keys for this account. Signal this to the caller */
559 auth_sam_trigger_repl_secret(kdc_db_ctx
, kdc_db_ctx
->msg_ctx
,
560 kdc_db_ctx
->ev_ctx
, msg
->dn
);
561 return SDB_ERR_NOT_FOUND_HERE
;
564 /* oh, no password. Apparently (comment in
565 * hdb-ldap.c) this violates the ASN.1, but this
566 * allows an entry with no keys (yet). */
570 /* allocate space to decode into */
571 entry_ex
->entry
.keys
.len
= 0;
572 entry_ex
->entry
.keys
.val
= calloc(allocated_keys
, sizeof(struct sdb_key
));
573 if (entry_ex
->entry
.keys
.val
== NULL
) {
578 if (hash
&& (supported_enctypes
& ENC_RC4_HMAC_MD5
)) {
579 struct sdb_key key
= {};
581 ret
= smb_krb5_keyblock_init_contents(context
,
582 ENCTYPE_ARCFOUR_HMAC
,
590 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
591 entry_ex
->entry
.keys
.len
++;
595 for (i
=0; i
< pkb4
->num_keys
; i
++) {
596 struct sdb_key key
= {};
598 if (!pkb4
->keys
[i
].value
) continue;
600 if (!(kerberos_enctype_to_bitmap(pkb4
->keys
[i
].keytype
) & supported_enctypes
)) {
604 if (pkb4
->salt
.string
) {
607 salt
= data_blob_string_const(pkb4
->salt
.string
);
609 key
.salt
= calloc(1, sizeof(*key
.salt
));
610 if (key
.salt
== NULL
) {
615 key
.salt
->type
= KRB5_PW_SALT
;
617 ret
= smb_krb5_copy_data_contents(&key
.salt
->salt
,
627 /* TODO: maybe pass the iteration_count somehow... */
629 ret
= smb_krb5_keyblock_init_contents(context
,
630 pkb4
->keys
[i
].keytype
,
631 pkb4
->keys
[i
].value
->data
,
632 pkb4
->keys
[i
].value
->length
,
634 if (ret
== KRB5_PROG_ETYPE_NOSUPP
) {
635 DEBUG(2,("Unsupported keytype ignored - type %u\n",
636 pkb4
->keys
[i
].keytype
));
642 smb_krb5_free_data_contents(context
, &key
.salt
->salt
);
649 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
650 entry_ex
->entry
.keys
.len
++;
653 for (i
=0; i
< pkb3
->num_keys
; i
++) {
654 struct sdb_key key
= {};
656 if (!pkb3
->keys
[i
].value
) continue;
658 if (!(kerberos_enctype_to_bitmap(pkb3
->keys
[i
].keytype
) & supported_enctypes
)) {
662 if (pkb3
->salt
.string
) {
665 salt
= data_blob_string_const(pkb3
->salt
.string
);
667 key
.salt
= calloc(1, sizeof(*key
.salt
));
668 if (key
.salt
== NULL
) {
673 key
.salt
->type
= KRB5_PW_SALT
;
675 ret
= smb_krb5_copy_data_contents(&key
.salt
->salt
,
685 ret
= smb_krb5_keyblock_init_contents(context
,
686 pkb3
->keys
[i
].keytype
,
687 pkb3
->keys
[i
].value
->data
,
688 pkb3
->keys
[i
].value
->length
,
692 smb_krb5_free_data_contents(context
, &key
.salt
->salt
);
699 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
700 entry_ex
->entry
.keys
.len
++;
706 entry_ex
->entry
.keys
.len
= 0;
707 } else if (entry_ex
->entry
.keys
.len
> 0 &&
708 entry_ex
->entry
.keys
.val
!= NULL
) {
709 ret
= samba_kdc_sort_encryption_keys(entry_ex
);
711 entry_ex
->entry
.keys
.len
= 0;
715 if (entry_ex
->entry
.keys
.len
== 0 && entry_ex
->entry
.keys
.val
) {
716 free(entry_ex
->entry
.keys
.val
);
717 entry_ex
->entry
.keys
.val
= NULL
;
722 static int principal_comp_strcmp_int(krb5_context context
,
723 krb5_const_principal principal
,
724 unsigned int component
,
731 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
732 p
= krb5_principal_get_comp_string(context
, principal
, component
);
739 if (component
>= krb5_princ_size(context
, principal
)) {
743 d
= krb5_princ_component(context
, principal
, component
);
752 return strncasecmp(p
, string
, len
);
754 return strncmp(p
, string
, len
);
758 static int principal_comp_strcasecmp(krb5_context context
,
759 krb5_const_principal principal
,
760 unsigned int component
,
763 return principal_comp_strcmp_int(context
, principal
,
764 component
, string
, true);
767 static int principal_comp_strcmp(krb5_context context
,
768 krb5_const_principal principal
,
769 unsigned int component
,
772 return principal_comp_strcmp_int(context
, principal
,
773 component
, string
, false);
777 * Construct an hdb_entry from a directory entry.
779 static krb5_error_code
samba_kdc_message2entry(krb5_context context
,
780 struct samba_kdc_db_context
*kdc_db_ctx
,
782 krb5_const_principal principal
,
783 enum samba_kdc_ent_type ent_type
,
785 struct ldb_dn
*realm_dn
,
786 struct ldb_message
*msg
,
787 struct sdb_entry_ex
*entry_ex
)
789 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
790 uint32_t userAccountControl
;
791 uint32_t msDS_User_Account_Control_Computed
;
792 krb5_error_code ret
= 0;
793 krb5_boolean is_computer
= FALSE
;
795 struct samba_kdc_entry
*p
;
800 bool is_rodc
= false;
801 struct ldb_message_element
*objectclasses
;
802 struct ldb_val computer_val
;
803 const char *samAccountName
= ldb_msg_find_attr_as_string(msg
, "samAccountName", NULL
);
804 computer_val
.data
= discard_const_p(uint8_t,"computer");
805 computer_val
.length
= strlen((const char *)computer_val
.data
);
807 if (ldb_msg_find_element(msg
, "msDS-SecondaryKrbTgtNumber")) {
811 if (!samAccountName
) {
813 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: no samAccountName present");
817 objectclasses
= ldb_msg_find_element(msg
, "objectClass");
819 if (objectclasses
&& ldb_msg_find_val(objectclasses
, &computer_val
)) {
823 ZERO_STRUCTP(entry_ex
);
825 p
= talloc_zero(mem_ctx
, struct samba_kdc_entry
);
831 p
->is_rodc
= is_rodc
;
832 p
->kdc_db_ctx
= kdc_db_ctx
;
833 p
->realm_dn
= talloc_reference(p
, realm_dn
);
839 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
843 userAccountControl
= ldb_msg_find_attr_as_uint(msg
, "userAccountControl", 0);
845 msDS_User_Account_Control_Computed
846 = ldb_msg_find_attr_as_uint(msg
,
847 "msDS-User-Account-Control-Computed",
851 * This brings in the lockout flag, block the account if not
852 * found. We need the weird UF_ACCOUNTDISABLE check because
853 * we do not want to fail open if the value is not returned,
854 * but 0 is a valid value (all OK)
856 if (msDS_User_Account_Control_Computed
== UF_ACCOUNTDISABLE
) {
858 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: "
859 "no msDS-User-Account-Control-Computed present");
862 userAccountControl
|= msDS_User_Account_Control_Computed
;
866 * If we are set to canonicalize, we get back the fixed UPPER
867 * case realm, and the real username (ie matching LDAP
870 * Otherwise, if we are set to enterprise, we
871 * get back the whole principal as-sent
873 * Finally, if we are not set to canonicalize, we get back the
874 * fixed UPPER case realm, but the as-sent username
877 if (ent_type
== SAMBA_KDC_ENT_TYPE_KRBTGT
) {
880 if (flags
& (SDB_F_CANON
)) {
882 * When requested to do so, ensure that the
883 * both realm values in the principal are set
884 * to the upper case, canonical realm
886 ret
= smb_krb5_make_principal(context
, &entry_ex
->entry
.principal
,
887 lpcfg_realm(lp_ctx
), "krbtgt",
888 lpcfg_realm(lp_ctx
), NULL
);
890 krb5_clear_error_message(context
);
893 smb_krb5_principal_set_type(context
, entry_ex
->entry
.principal
, KRB5_NT_SRV_INST
);
895 ret
= krb5_copy_principal(context
, principal
, &entry_ex
->entry
.principal
);
897 krb5_clear_error_message(context
);
901 * this appears to be required regardless of
902 * the canonicalize flag from the client
904 ret
= smb_krb5_principal_set_realm(context
, entry_ex
->entry
.principal
, lpcfg_realm(lp_ctx
));
906 krb5_clear_error_message(context
);
911 } else if (ent_type
== SAMBA_KDC_ENT_TYPE_ANY
&& principal
== NULL
) {
912 ret
= smb_krb5_make_principal(context
, &entry_ex
->entry
.principal
, lpcfg_realm(lp_ctx
), samAccountName
, NULL
);
914 krb5_clear_error_message(context
);
917 } else if ((flags
& SDB_F_CANON
) && (flags
& SDB_F_FOR_AS_REQ
)) {
919 * SDB_F_CANON maps from the canonicalize flag in the
920 * packet, and has a different meaning between AS-REQ
921 * and TGS-REQ. We only change the principal in the AS-REQ case
923 ret
= smb_krb5_make_principal(context
, &entry_ex
->entry
.principal
, lpcfg_realm(lp_ctx
), samAccountName
, NULL
);
925 krb5_clear_error_message(context
);
929 ret
= krb5_copy_principal(context
, principal
, &entry_ex
->entry
.principal
);
931 krb5_clear_error_message(context
);
935 if (smb_krb5_principal_get_type(context
, principal
) != KRB5_NT_ENTERPRISE_PRINCIPAL
) {
936 /* While we have copied the client principal, tests
937 * show that Win2k3 returns the 'corrected' realm, not
938 * the client-specified realm. This code attempts to
939 * replace the client principal's realm with the one
940 * we determine from our records */
942 /* this has to be with malloc() */
943 ret
= smb_krb5_principal_set_realm(context
, entry_ex
->entry
.principal
, lpcfg_realm(lp_ctx
));
945 krb5_clear_error_message(context
);
951 /* First try and figure out the flags based on the userAccountControl */
952 entry_ex
->entry
.flags
= uf2SDBFlags(context
, userAccountControl
, ent_type
);
954 /* Windows 2008 seems to enforce this (very sensible) rule by
955 * default - don't allow offline attacks on a user's password
956 * by asking for a ticket to them as a service (encrypted with
957 * their probably patheticly insecure password) */
959 if (entry_ex
->entry
.flags
.server
960 && lpcfg_parm_bool(lp_ctx
, NULL
, "kdc", "require spn for service", true)) {
961 if (!is_computer
&& !ldb_msg_find_attr_as_string(msg
, "servicePrincipalName", NULL
)) {
962 entry_ex
->entry
.flags
.server
= 0;
966 * To give the correct type of error to the client, we must
967 * not just return the entry without .server set, we must
968 * pretend the principal does not exist. Otherwise we may
969 * return ERR_POLICY instead of
970 * KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
972 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
&& entry_ex
->entry
.flags
.server
== 0) {
973 ret
= SDB_ERR_NOENTRY
;
974 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: no servicePrincipalName present for this server, refusing with no-such-entry");
977 if (flags
& SDB_F_ADMIN_DATA
) {
978 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
979 * of the Heimdal KDC. They are stored in a the traditional
980 * DB for audit purposes, and still form part of the structure
983 /* use 'whenCreated' */
984 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
985 /* use 'kadmin' for now (needed by mit_samba) */
987 ret
= smb_krb5_make_principal(context
,
988 &entry_ex
->entry
.created_by
.principal
,
989 lpcfg_realm(lp_ctx
), "kadmin", NULL
);
991 krb5_clear_error_message(context
);
995 entry_ex
->entry
.modified_by
= (struct sdb_event
*) malloc(sizeof(struct sdb_event
));
996 if (entry_ex
->entry
.modified_by
== NULL
) {
998 krb5_set_error_message(context
, ret
, "malloc: out of memory");
1002 /* use 'whenChanged' */
1003 entry_ex
->entry
.modified_by
->time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenChanged", 0);
1004 /* use 'kadmin' for now (needed by mit_samba) */
1005 ret
= smb_krb5_make_principal(context
,
1006 &entry_ex
->entry
.modified_by
->principal
,
1007 lpcfg_realm(lp_ctx
), "kadmin", NULL
);
1009 krb5_clear_error_message(context
);
1015 /* The lack of password controls etc applies to krbtgt by
1016 * virtue of being that particular RID */
1017 status
= dom_sid_split_rid(NULL
, samdb_result_dom_sid(mem_ctx
, msg
, "objectSid"), NULL
, &rid
);
1019 if (!NT_STATUS_IS_OK(status
)) {
1024 if (rid
== DOMAIN_RID_KRBTGT
) {
1027 entry_ex
->entry
.valid_end
= NULL
;
1028 entry_ex
->entry
.pw_end
= NULL
;
1030 entry_ex
->entry
.flags
.invalid
= 0;
1031 entry_ex
->entry
.flags
.server
= 1;
1033 realm
= smb_krb5_principal_get_realm(
1034 mem_ctx
, context
, principal
);
1035 if (realm
== NULL
) {
1040 /* Don't mark all requests for the krbtgt/realm as
1041 * 'change password', as otherwise we could get into
1042 * trouble, and not enforce the password expirty.
1043 * Instead, only do it when request is for the kpasswd service */
1044 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
1045 && krb5_princ_size(context
, principal
) == 2
1046 && (principal_comp_strcmp(context
, principal
, 0, "kadmin") == 0)
1047 && (principal_comp_strcmp(context
, principal
, 1, "changepw") == 0)
1048 && lpcfg_is_my_domain_or_realm(lp_ctx
, realm
)) {
1049 entry_ex
->entry
.flags
.change_pw
= 1;
1054 entry_ex
->entry
.flags
.client
= 0;
1055 entry_ex
->entry
.flags
.forwardable
= 1;
1056 entry_ex
->entry
.flags
.ok_as_delegate
= 1;
1057 } else if (is_rodc
) {
1058 /* The RODC krbtgt account is like the main krbtgt,
1059 * but it does not have a changepw or kadmin
1062 entry_ex
->entry
.valid_end
= NULL
;
1063 entry_ex
->entry
.pw_end
= NULL
;
1065 /* Also don't allow the RODC krbtgt to be a client (it should not be needed) */
1066 entry_ex
->entry
.flags
.client
= 0;
1067 entry_ex
->entry
.flags
.invalid
= 0;
1068 entry_ex
->entry
.flags
.server
= 1;
1070 entry_ex
->entry
.flags
.client
= 0;
1071 entry_ex
->entry
.flags
.forwardable
= 1;
1072 entry_ex
->entry
.flags
.ok_as_delegate
= 0;
1073 } else if (entry_ex
->entry
.flags
.server
&& ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
1074 /* The account/password expiry only applies when the account is used as a
1075 * client (ie password login), not when used as a server */
1077 /* Make very well sure we don't use this for a client,
1078 * it could bypass the password restrictions */
1079 entry_ex
->entry
.flags
.client
= 0;
1081 entry_ex
->entry
.valid_end
= NULL
;
1082 entry_ex
->entry
.pw_end
= NULL
;
1085 NTTIME must_change_time
1086 = samdb_result_nttime(msg
,
1087 "msDS-UserPasswordExpiryTimeComputed",
1089 if (must_change_time
== 0x7FFFFFFFFFFFFFFFULL
) {
1090 entry_ex
->entry
.pw_end
= NULL
;
1092 entry_ex
->entry
.pw_end
= malloc(sizeof(*entry_ex
->entry
.pw_end
));
1093 if (entry_ex
->entry
.pw_end
== NULL
) {
1097 *entry_ex
->entry
.pw_end
= nt_time_to_unix(must_change_time
);
1100 acct_expiry
= samdb_result_account_expires(msg
);
1101 if (acct_expiry
== 0x7FFFFFFFFFFFFFFFULL
) {
1102 entry_ex
->entry
.valid_end
= NULL
;
1104 entry_ex
->entry
.valid_end
= malloc(sizeof(*entry_ex
->entry
.valid_end
));
1105 if (entry_ex
->entry
.valid_end
== NULL
) {
1109 *entry_ex
->entry
.valid_end
= nt_time_to_unix(acct_expiry
);
1113 entry_ex
->entry
.valid_start
= NULL
;
1115 entry_ex
->entry
.max_life
= malloc(sizeof(*entry_ex
->entry
.max_life
));
1116 if (entry_ex
->entry
.max_life
== NULL
) {
1121 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
1122 *entry_ex
->entry
.max_life
= kdc_db_ctx
->policy
.svc_tkt_lifetime
;
1123 } else if (ent_type
== SAMBA_KDC_ENT_TYPE_KRBTGT
|| ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
) {
1124 *entry_ex
->entry
.max_life
= kdc_db_ctx
->policy
.usr_tkt_lifetime
;
1126 *entry_ex
->entry
.max_life
= MIN(kdc_db_ctx
->policy
.svc_tkt_lifetime
,
1127 kdc_db_ctx
->policy
.usr_tkt_lifetime
);
1130 entry_ex
->entry
.max_renew
= malloc(sizeof(*entry_ex
->entry
.max_life
));
1131 if (entry_ex
->entry
.max_renew
== NULL
) {
1136 *entry_ex
->entry
.max_renew
= kdc_db_ctx
->policy
.renewal_lifetime
;
1138 /* Get keys from the db */
1139 ret
= samba_kdc_message2entry_keys(context
, kdc_db_ctx
, p
, msg
,
1140 rid
, is_rodc
, userAccountControl
,
1141 ent_type
, entry_ex
);
1143 /* Could be bogus data in the entry, or out of memory */
1147 p
->msg
= talloc_steal(p
, msg
);
1151 /* This doesn't free ent itself, that is for the eventual caller to do */
1152 sdb_free_entry(entry_ex
);
1153 ZERO_STRUCTP(entry_ex
);
1155 talloc_steal(kdc_db_ctx
, entry_ex
->ctx
);
1162 * Construct an hdb_entry from a directory entry.
1163 * The kvno is what the remote client asked for
1165 static krb5_error_code
samba_kdc_trust_message2entry(krb5_context context
,
1166 struct samba_kdc_db_context
*kdc_db_ctx
,
1167 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
1168 enum trust_direction direction
,
1169 struct ldb_dn
*realm_dn
,
1172 struct ldb_message
*msg
,
1173 struct sdb_entry_ex
*entry_ex
)
1175 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
1176 const char *our_realm
= lpcfg_realm(lp_ctx
);
1177 char *partner_realm
= NULL
;
1178 const char *realm
= NULL
;
1179 const char *krbtgt_realm
= NULL
;
1180 DATA_BLOB password_utf16
= data_blob_null
;
1181 DATA_BLOB password_utf8
= data_blob_null
;
1182 struct samr_Password _password_hash
;
1183 const struct samr_Password
*password_hash
= NULL
;
1184 const struct ldb_val
*password_val
;
1185 struct trustAuthInOutBlob password_blob
;
1186 struct samba_kdc_entry
*p
;
1187 bool use_previous
= false;
1188 uint32_t current_kvno
;
1189 uint32_t previous_kvno
;
1190 uint32_t num_keys
= 0;
1191 enum ndr_err_code ndr_err
;
1194 struct AuthenticationInformationArray
*auth_array
;
1197 uint32_t *auth_kvno
;
1198 bool preferr_current
= false;
1199 uint32_t supported_enctypes
= ENC_RC4_HMAC_MD5
;
1200 struct lsa_TrustDomainInfoInfoEx
*tdo
= NULL
;
1203 if (dsdb_functional_level(kdc_db_ctx
->samdb
) >= DS_DOMAIN_FUNCTION_2008
) {
1204 supported_enctypes
= ldb_msg_find_attr_as_uint(msg
,
1205 "msDS-SupportedEncryptionTypes",
1206 supported_enctypes
);
1209 status
= dsdb_trust_parse_tdo_info(mem_ctx
, msg
, &tdo
);
1210 if (!NT_STATUS_IS_OK(status
)) {
1211 krb5_clear_error_message(context
);
1216 if (!(tdo
->trust_direction
& direction
)) {
1217 krb5_clear_error_message(context
);
1218 ret
= SDB_ERR_NOENTRY
;
1222 if (tdo
->trust_type
!= LSA_TRUST_TYPE_UPLEVEL
) {
1224 * Only UPLEVEL domains support kerberos here,
1225 * as we don't support LSA_TRUST_TYPE_MIT.
1227 krb5_clear_error_message(context
);
1228 ret
= SDB_ERR_NOENTRY
;
1232 if (tdo
->trust_attributes
& LSA_TRUST_ATTRIBUTE_CROSS_ORGANIZATION
) {
1234 * We don't support selective authentication yet.
1236 krb5_clear_error_message(context
);
1237 ret
= SDB_ERR_NOENTRY
;
1241 if (tdo
->domain_name
.string
== NULL
) {
1242 krb5_clear_error_message(context
);
1243 ret
= SDB_ERR_NOENTRY
;
1246 partner_realm
= strupper_talloc(mem_ctx
, tdo
->domain_name
.string
);
1247 if (partner_realm
== NULL
) {
1248 krb5_clear_error_message(context
);
1253 if (direction
== INBOUND
) {
1255 krbtgt_realm
= partner_realm
;
1257 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthIncoming");
1258 } else { /* OUTBOUND */
1259 realm
= partner_realm
;
1260 krbtgt_realm
= our_realm
;
1262 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthOutgoing");
1265 if (password_val
== NULL
) {
1266 krb5_clear_error_message(context
);
1267 ret
= SDB_ERR_NOENTRY
;
1271 ndr_err
= ndr_pull_struct_blob(password_val
, mem_ctx
, &password_blob
,
1272 (ndr_pull_flags_fn_t
)ndr_pull_trustAuthInOutBlob
);
1273 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1274 krb5_clear_error_message(context
);
1279 p
= talloc_zero(mem_ctx
, struct samba_kdc_entry
);
1286 p
->kdc_db_ctx
= kdc_db_ctx
;
1287 p
->realm_dn
= realm_dn
;
1289 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
1291 /* make sure we do not have bogus data in there */
1292 memset(&entry_ex
->entry
, 0, sizeof(struct sdb_entry
));
1296 /* use 'whenCreated' */
1297 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
1298 /* use 'kadmin' for now (needed by mit_samba) */
1299 ret
= smb_krb5_make_principal(context
,
1300 &entry_ex
->entry
.created_by
.principal
,
1301 realm
, "kadmin", NULL
);
1303 krb5_clear_error_message(context
);
1308 * We always need to generate the canonicalized principal
1309 * with the values of our database.
1311 ret
= smb_krb5_make_principal(context
, &entry_ex
->entry
.principal
, realm
,
1312 "krbtgt", krbtgt_realm
, NULL
);
1314 krb5_clear_error_message(context
);
1317 smb_krb5_principal_set_type(context
, entry_ex
->entry
.principal
,
1320 entry_ex
->entry
.valid_start
= NULL
;
1322 /* we need to work out if we are going to use the current or
1323 * the previous password hash.
1324 * We base this on the kvno the client passes in. If the kvno
1325 * passed in is equal to the current kvno in our database then
1326 * we use the current structure. If it is the current kvno-1,
1327 * then we use the previous substrucure.
1331 * Windows preferrs the previous key for one hour.
1333 tv
= timeval_current();
1334 if (tv
.tv_sec
> 3600) {
1337 an_hour_ago
= timeval_to_nttime(&tv
);
1339 /* first work out the current kvno */
1341 for (i
=0; i
< password_blob
.count
; i
++) {
1342 struct AuthenticationInformation
*a
=
1343 &password_blob
.current
.array
[i
];
1345 if (a
->LastUpdateTime
<= an_hour_ago
) {
1346 preferr_current
= true;
1349 if (a
->AuthType
== TRUST_AUTH_TYPE_VERSION
) {
1350 current_kvno
= a
->AuthInfo
.version
.version
;
1353 if (current_kvno
== 0) {
1354 previous_kvno
= 255;
1356 previous_kvno
= current_kvno
- 1;
1358 for (i
=0; i
< password_blob
.count
; i
++) {
1359 struct AuthenticationInformation
*a
=
1360 &password_blob
.previous
.array
[i
];
1362 if (a
->AuthType
== TRUST_AUTH_TYPE_VERSION
) {
1363 previous_kvno
= a
->AuthInfo
.version
.version
;
1367 /* work out whether we will use the previous or current
1369 if (password_blob
.previous
.count
== 0) {
1370 /* there is no previous password */
1371 use_previous
= false;
1372 } else if (!(flags
& SDB_F_KVNO_SPECIFIED
)) {
1374 * If not specified we use the lowest kvno
1375 * for the first hour after an update.
1377 if (preferr_current
) {
1378 use_previous
= false;
1379 } else if (previous_kvno
< current_kvno
) {
1380 use_previous
= true;
1382 use_previous
= false;
1384 } else if (kvno
== current_kvno
) {
1388 use_previous
= false;
1389 } else if (kvno
== previous_kvno
) {
1393 use_previous
= true;
1396 * Fallback to the current one for anything else
1398 use_previous
= false;
1402 auth_array
= &password_blob
.previous
;
1403 auth_kvno
= &previous_kvno
;
1405 auth_array
= &password_blob
.current
;
1406 auth_kvno
= ¤t_kvno
;
1409 /* use the kvno the client specified, if available */
1410 if (flags
& SDB_F_KVNO_SPECIFIED
) {
1411 entry_ex
->entry
.kvno
= kvno
;
1413 entry_ex
->entry
.kvno
= *auth_kvno
;
1416 for (i
=0; i
< auth_array
->count
; i
++) {
1417 if (auth_array
->array
[i
].AuthType
== TRUST_AUTH_TYPE_CLEAR
) {
1420 password_utf16
= data_blob_const(auth_array
->array
[i
].AuthInfo
.clear
.password
,
1421 auth_array
->array
[i
].AuthInfo
.clear
.size
);
1422 if (password_utf16
.length
== 0) {
1426 if (supported_enctypes
& ENC_RC4_HMAC_MD5
) {
1427 mdfour(_password_hash
.hash
, password_utf16
.data
, password_utf16
.length
);
1428 if (password_hash
== NULL
) {
1431 password_hash
= &_password_hash
;
1434 if (!(supported_enctypes
& (ENC_HMAC_SHA1_96_AES128
|ENC_HMAC_SHA1_96_AES256
))) {
1438 ok
= convert_string_talloc(mem_ctx
,
1439 CH_UTF16MUNGED
, CH_UTF8
,
1440 password_utf16
.data
,
1441 password_utf16
.length
,
1442 (void *)&password_utf8
.data
,
1443 &password_utf8
.length
);
1445 krb5_clear_error_message(context
);
1450 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES128
) {
1453 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES256
) {
1457 } else if (auth_array
->array
[i
].AuthType
== TRUST_AUTH_TYPE_NT4OWF
) {
1458 if (supported_enctypes
& ENC_RC4_HMAC_MD5
) {
1459 password_hash
= &auth_array
->array
[i
].AuthInfo
.nt4owf
.password
;
1465 /* Must have found a cleartext or MD4 password */
1466 if (num_keys
== 0) {
1467 DEBUG(1,(__location__
": no usable key found\n"));
1468 krb5_clear_error_message(context
);
1469 ret
= SDB_ERR_NOENTRY
;
1473 entry_ex
->entry
.keys
.val
= calloc(num_keys
, sizeof(struct sdb_key
));
1474 if (entry_ex
->entry
.keys
.val
== NULL
) {
1475 krb5_clear_error_message(context
);
1480 if (password_utf8
.length
!= 0) {
1481 struct sdb_key key
= {};
1482 krb5_const_principal salt_principal
= entry_ex
->entry
.principal
;
1484 krb5_data cleartext_data
;
1486 cleartext_data
.data
= discard_const_p(char, password_utf8
.data
);
1487 cleartext_data
.length
= password_utf8
.length
;
1489 ret
= smb_krb5_get_pw_salt(context
,
1496 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES256
) {
1497 ret
= smb_krb5_create_key_from_string(context
,
1501 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
1504 smb_krb5_free_data_contents(context
, &salt
);
1508 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
1509 entry_ex
->entry
.keys
.len
++;
1512 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES128
) {
1513 ret
= smb_krb5_create_key_from_string(context
,
1517 ENCTYPE_AES128_CTS_HMAC_SHA1_96
,
1520 smb_krb5_free_data_contents(context
, &salt
);
1524 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
1525 entry_ex
->entry
.keys
.len
++;
1528 smb_krb5_free_data_contents(context
, &salt
);
1531 if (password_hash
!= NULL
) {
1532 struct sdb_key key
= {};
1534 ret
= smb_krb5_keyblock_init_contents(context
,
1535 ENCTYPE_ARCFOUR_HMAC
,
1536 password_hash
->hash
,
1537 sizeof(password_hash
->hash
),
1543 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
1544 entry_ex
->entry
.keys
.len
++;
1547 entry_ex
->entry
.flags
= int2SDBFlags(0);
1548 entry_ex
->entry
.flags
.immutable
= 1;
1549 entry_ex
->entry
.flags
.invalid
= 0;
1550 entry_ex
->entry
.flags
.server
= 1;
1551 entry_ex
->entry
.flags
.require_preauth
= 1;
1553 entry_ex
->entry
.pw_end
= NULL
;
1555 entry_ex
->entry
.max_life
= NULL
;
1557 entry_ex
->entry
.max_renew
= NULL
;
1559 ret
= samba_kdc_sort_encryption_keys(entry_ex
);
1561 krb5_clear_error_message(context
);
1566 p
->msg
= talloc_steal(p
, msg
);
1569 TALLOC_FREE(partner_realm
);
1572 /* This doesn't free ent itself, that is for the eventual caller to do */
1573 sdb_free_entry(entry_ex
);
1575 talloc_steal(kdc_db_ctx
, entry_ex
->ctx
);
1582 static krb5_error_code
samba_kdc_lookup_trust(krb5_context context
, struct ldb_context
*ldb_ctx
,
1583 TALLOC_CTX
*mem_ctx
,
1585 struct ldb_dn
*realm_dn
,
1586 struct ldb_message
**pmsg
)
1589 const char * const *attrs
= trust_attrs
;
1591 status
= dsdb_trust_search_tdo(ldb_ctx
, realm
, realm
,
1592 attrs
, mem_ctx
, pmsg
);
1593 if (NT_STATUS_IS_OK(status
)) {
1595 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
1596 return SDB_ERR_NOENTRY
;
1597 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MEMORY
)) {
1599 krb5_set_error_message(context
, ret
, "get_sam_result_trust: out of memory");
1603 krb5_set_error_message(context
, ret
, "get_sam_result_trust: %s", nt_errstr(status
));
1608 static krb5_error_code
samba_kdc_lookup_client(krb5_context context
,
1609 struct samba_kdc_db_context
*kdc_db_ctx
,
1610 TALLOC_CTX
*mem_ctx
,
1611 krb5_const_principal principal
,
1613 struct ldb_dn
**realm_dn
,
1614 struct ldb_message
**msg
)
1617 char *principal_string
= NULL
;
1619 if (smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
1620 principal_string
= smb_krb5_principal_get_comp_string(mem_ctx
, context
,
1622 if (principal_string
== NULL
) {
1626 char *principal_string_m
= NULL
;
1627 krb5_error_code ret
;
1629 ret
= krb5_unparse_name(context
, principal
, &principal_string_m
);
1634 principal_string
= talloc_strdup(mem_ctx
, principal_string_m
);
1635 SAFE_FREE(principal_string_m
);
1636 if (principal_string
== NULL
) {
1641 nt_status
= sam_get_results_principal(kdc_db_ctx
->samdb
,
1642 mem_ctx
, principal_string
, attrs
,
1644 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
1645 krb5_principal fallback_principal
= NULL
;
1646 unsigned int num_comp
;
1647 char *fallback_realm
= NULL
;
1648 char *fallback_account
= NULL
;
1649 krb5_error_code ret
;
1651 ret
= krb5_parse_name(context
, principal_string
,
1652 &fallback_principal
);
1653 TALLOC_FREE(principal_string
);
1658 num_comp
= krb5_princ_size(context
, fallback_principal
);
1659 fallback_realm
= smb_krb5_principal_get_realm(
1660 mem_ctx
, context
, fallback_principal
);
1661 if (fallback_realm
== NULL
) {
1662 krb5_free_principal(context
, fallback_principal
);
1666 if (num_comp
== 1) {
1669 fallback_account
= smb_krb5_principal_get_comp_string(mem_ctx
,
1670 context
, fallback_principal
, 0);
1671 if (fallback_account
== NULL
) {
1672 krb5_free_principal(context
, fallback_principal
);
1673 TALLOC_FREE(fallback_realm
);
1677 len
= strlen(fallback_account
);
1678 if (len
>= 2 && fallback_account
[len
- 1] == '$') {
1679 TALLOC_FREE(fallback_account
);
1682 krb5_free_principal(context
, fallback_principal
);
1683 fallback_principal
= NULL
;
1685 if (fallback_account
!= NULL
) {
1688 with_dollar
= talloc_asprintf(mem_ctx
, "%s$",
1690 if (with_dollar
== NULL
) {
1691 TALLOC_FREE(fallback_realm
);
1694 TALLOC_FREE(fallback_account
);
1696 ret
= smb_krb5_make_principal(context
,
1697 &fallback_principal
,
1700 TALLOC_FREE(with_dollar
);
1702 TALLOC_FREE(fallback_realm
);
1706 TALLOC_FREE(fallback_realm
);
1708 if (fallback_principal
!= NULL
) {
1709 char *fallback_string
= NULL
;
1711 ret
= krb5_unparse_name(context
,
1715 krb5_free_principal(context
, fallback_principal
);
1719 nt_status
= sam_get_results_principal(kdc_db_ctx
->samdb
,
1724 SAFE_FREE(fallback_string
);
1726 krb5_free_principal(context
, fallback_principal
);
1727 fallback_principal
= NULL
;
1729 TALLOC_FREE(principal_string
);
1731 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
1732 return SDB_ERR_NOENTRY
;
1733 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_MEMORY
)) {
1735 } else if (!NT_STATUS_IS_OK(nt_status
)) {
1742 static krb5_error_code
samba_kdc_fetch_client(krb5_context context
,
1743 struct samba_kdc_db_context
*kdc_db_ctx
,
1744 TALLOC_CTX
*mem_ctx
,
1745 krb5_const_principal principal
,
1747 struct sdb_entry_ex
*entry_ex
) {
1748 struct ldb_dn
*realm_dn
;
1749 krb5_error_code ret
;
1750 struct ldb_message
*msg
= NULL
;
1752 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
1753 mem_ctx
, principal
, user_attrs
,
1759 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1760 principal
, SAMBA_KDC_ENT_TYPE_CLIENT
,
1762 realm_dn
, msg
, entry_ex
);
1766 static krb5_error_code
samba_kdc_fetch_krbtgt(krb5_context context
,
1767 struct samba_kdc_db_context
*kdc_db_ctx
,
1768 TALLOC_CTX
*mem_ctx
,
1769 krb5_const_principal principal
,
1772 struct sdb_entry_ex
*entry_ex
)
1774 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
1775 krb5_error_code ret
;
1776 struct ldb_message
*msg
= NULL
;
1777 struct ldb_dn
*realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
1778 char *realm_from_princ
;
1779 char *realm_princ_comp
= smb_krb5_principal_get_comp_string(mem_ctx
, context
, principal
, 1);
1781 realm_from_princ
= smb_krb5_principal_get_realm(
1782 mem_ctx
, context
, principal
);
1783 if (realm_from_princ
== NULL
) {
1785 return SDB_ERR_NOENTRY
;
1788 if (krb5_princ_size(context
, principal
) != 2
1789 || (principal_comp_strcmp(context
, principal
, 0, KRB5_TGS_NAME
) != 0)) {
1791 return SDB_ERR_NOENTRY
;
1794 /* krbtgt case. Either us or a trusted realm */
1796 if (lpcfg_is_my_domain_or_realm(lp_ctx
, realm_from_princ
)
1797 && lpcfg_is_my_domain_or_realm(lp_ctx
, realm_princ_comp
)) {
1798 /* us, or someone quite like us */
1799 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1800 * is in our db, then direct the caller at our primary
1804 unsigned int krbtgt_number
;
1805 /* w2k8r2 sometimes gives us a kvno of 255 for inter-domain
1806 trust tickets. We don't yet know what this means, but we do
1807 seem to need to treat it as unspecified */
1808 if (flags
& SDB_F_KVNO_SPECIFIED
) {
1809 krbtgt_number
= SAMBA_KVNO_GET_KRBTGT(kvno
);
1810 if (kdc_db_ctx
->rodc
) {
1811 if (krbtgt_number
!= kdc_db_ctx
->my_krbtgt_number
) {
1812 return SDB_ERR_NOT_FOUND_HERE
;
1816 krbtgt_number
= kdc_db_ctx
->my_krbtgt_number
;
1819 if (krbtgt_number
== kdc_db_ctx
->my_krbtgt_number
) {
1820 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
,
1821 &msg
, kdc_db_ctx
->krbtgt_dn
, LDB_SCOPE_BASE
,
1822 krbtgt_attrs
, DSDB_SEARCH_NO_GLOBAL_CATALOG
,
1823 "(objectClass=user)");
1825 /* We need to look up an RODC krbtgt (perhaps
1826 * ours, if we are an RODC, perhaps another
1827 * RODC if we are a read-write DC */
1828 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
,
1829 &msg
, realm_dn
, LDB_SCOPE_SUBTREE
,
1831 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
1832 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=%u))", (unsigned)(krbtgt_number
));
1835 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
1836 krb5_warnx(context
, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1837 (unsigned)(krbtgt_number
));
1838 krb5_set_error_message(context
, SDB_ERR_NOENTRY
,
1839 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1840 (unsigned)(krbtgt_number
));
1841 return SDB_ERR_NOENTRY
;
1842 } else if (lret
!= LDB_SUCCESS
) {
1843 krb5_warnx(context
, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1844 (unsigned)(krbtgt_number
));
1845 krb5_set_error_message(context
, SDB_ERR_NOENTRY
,
1846 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1847 (unsigned)(krbtgt_number
));
1848 return SDB_ERR_NOENTRY
;
1851 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1852 principal
, SAMBA_KDC_ENT_TYPE_KRBTGT
,
1853 flags
, realm_dn
, msg
, entry_ex
);
1855 krb5_warnx(context
, "samba_kdc_fetch: self krbtgt message2entry failed");
1860 enum trust_direction direction
= UNKNOWN
;
1861 const char *realm
= NULL
;
1863 /* Either an inbound or outbound trust */
1865 if (strcasecmp(lpcfg_realm(lp_ctx
), realm_from_princ
) == 0) {
1866 /* look for inbound trust */
1867 direction
= INBOUND
;
1868 realm
= realm_princ_comp
;
1869 } else if (principal_comp_strcasecmp(context
, principal
, 1, lpcfg_realm(lp_ctx
)) == 0) {
1870 /* look for outbound trust */
1871 direction
= OUTBOUND
;
1872 realm
= realm_from_princ
;
1874 krb5_warnx(context
, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1877 krb5_set_error_message(context
, SDB_ERR_NOENTRY
, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1880 return SDB_ERR_NOENTRY
;
1883 /* Trusted domains are under CN=system */
1885 ret
= samba_kdc_lookup_trust(context
, kdc_db_ctx
->samdb
,
1887 realm
, realm_dn
, &msg
);
1890 krb5_warnx(context
, "samba_kdc_fetch: could not find principal in DB");
1891 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: could not find principal in DB");
1895 ret
= samba_kdc_trust_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1896 principal
, direction
,
1897 realm_dn
, flags
, kvno
, msg
, entry_ex
);
1899 krb5_warnx(context
, "samba_kdc_fetch: trust_message2entry failed for %s",
1900 ldb_dn_get_linearized(msg
->dn
));
1901 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: "
1902 "trust_message2entry failed for %s",
1903 ldb_dn_get_linearized(msg
->dn
));
1910 static krb5_error_code
samba_kdc_lookup_server(krb5_context context
,
1911 struct samba_kdc_db_context
*kdc_db_ctx
,
1912 TALLOC_CTX
*mem_ctx
,
1913 krb5_const_principal principal
,
1916 struct ldb_dn
**realm_dn
,
1917 struct ldb_message
**msg
)
1919 krb5_error_code ret
;
1920 if ((smb_krb5_principal_get_type(context
, principal
) != KRB5_NT_ENTERPRISE_PRINCIPAL
)
1921 && krb5_princ_size(context
, principal
) >= 2) {
1922 /* 'normal server' case */
1925 struct ldb_dn
*user_dn
;
1926 char *principal_string
;
1928 ret
= krb5_unparse_name_flags(context
, principal
,
1929 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
1935 /* At this point we may find the host is known to be
1936 * in a different realm, so we should generate a
1937 * referral instead */
1938 nt_status
= crack_service_principal_name(kdc_db_ctx
->samdb
,
1939 mem_ctx
, principal_string
,
1940 &user_dn
, realm_dn
);
1941 free(principal_string
);
1943 if (!NT_STATUS_IS_OK(nt_status
)) {
1944 return SDB_ERR_NOENTRY
;
1947 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
,
1949 msg
, user_dn
, LDB_SCOPE_BASE
,
1951 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
1953 if (ldb_ret
!= LDB_SUCCESS
) {
1954 return SDB_ERR_NOENTRY
;
1957 } else if (!(flags
& SDB_F_FOR_AS_REQ
)
1958 && smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
1960 * The behaviour of accepting an
1961 * KRB5_NT_ENTERPRISE_PRINCIPAL server principal
1962 * containing a UPN only applies to TGS-REQ packets,
1963 * not AS-REQ packets.
1965 return samba_kdc_lookup_client(context
, kdc_db_ctx
,
1966 mem_ctx
, principal
, attrs
,
1971 * - the AS-REQ, where we only accept
1972 * samAccountName based lookups for the server, no
1973 * matter if the name is an
1974 * KRB5_NT_ENTERPRISE_PRINCIPAL or not
1975 * - for the TGS-REQ when we are not given an
1976 * KRB5_NT_ENTERPRISE_PRINCIPAL, which also must
1977 * only lookup samAccountName based names.
1981 krb5_principal enterprise_principal
= NULL
;
1982 krb5_const_principal used_principal
= NULL
;
1985 char *filter
= NULL
;
1987 if (smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
1989 /* Need to reparse the enterprise principal to find the real target */
1990 if (krb5_princ_size(context
, principal
) != 1) {
1991 ret
= KRB5_PARSE_MALFORMED
;
1992 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_server: request for an "
1993 "enterprise principal with wrong (%d) number of components",
1994 krb5_princ_size(context
, principal
));
1997 str
= smb_krb5_principal_get_comp_string(mem_ctx
, context
, principal
, 0);
1999 return KRB5_PARSE_MALFORMED
;
2001 ret
= krb5_parse_name(context
, str
,
2002 &enterprise_principal
);
2007 used_principal
= enterprise_principal
;
2009 used_principal
= principal
;
2012 /* server as client principal case, but we must not lookup userPrincipalNames */
2013 *realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
2015 /* TODO: Check if it is our realm, otherwise give referral */
2017 ret
= krb5_unparse_name_flags(context
, used_principal
,
2018 KRB5_PRINCIPAL_UNPARSE_NO_REALM
|
2019 KRB5_PRINCIPAL_UNPARSE_DISPLAY
,
2021 used_principal
= NULL
;
2022 krb5_free_principal(context
, enterprise_principal
);
2023 enterprise_principal
= NULL
;
2026 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_principal: could not parse principal");
2027 krb5_warnx(context
, "samba_kdc_lookup_principal: could not parse principal");
2031 name1
= ldb_binary_encode_string(mem_ctx
, short_princ
);
2032 SAFE_FREE(short_princ
);
2033 if (name1
== NULL
) {
2036 len1
= strlen(name1
);
2037 if (len1
>= 1 && name1
[len1
- 1] != '$') {
2038 filter
= talloc_asprintf(mem_ctx
,
2039 "(&(objectClass=user)(|(samAccountName=%s)(samAccountName=%s$)))",
2041 if (filter
== NULL
) {
2045 filter
= talloc_asprintf(mem_ctx
,
2046 "(&(objectClass=user)(samAccountName=%s))",
2048 if (filter
== NULL
) {
2053 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
, msg
,
2054 *realm_dn
, LDB_SCOPE_SUBTREE
,
2056 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
2058 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
2059 DEBUG(10, ("Failed to find an entry for %s filter:%s\n",
2061 return SDB_ERR_NOENTRY
;
2063 if (lret
== LDB_ERR_CONSTRAINT_VIOLATION
) {
2064 DEBUG(10, ("Failed to find unique entry for %s filter:%s\n",
2066 return SDB_ERR_NOENTRY
;
2068 if (lret
!= LDB_SUCCESS
) {
2069 DEBUG(0, ("Failed single search for %s - %s\n",
2070 name1
, ldb_errstring(kdc_db_ctx
->samdb
)));
2071 return SDB_ERR_NOENTRY
;
2075 return SDB_ERR_NOENTRY
;
2080 static krb5_error_code
samba_kdc_fetch_server(krb5_context context
,
2081 struct samba_kdc_db_context
*kdc_db_ctx
,
2082 TALLOC_CTX
*mem_ctx
,
2083 krb5_const_principal principal
,
2085 struct sdb_entry_ex
*entry_ex
)
2087 krb5_error_code ret
;
2088 struct ldb_dn
*realm_dn
;
2089 struct ldb_message
*msg
;
2091 ret
= samba_kdc_lookup_server(context
, kdc_db_ctx
, mem_ctx
, principal
,
2092 flags
, server_attrs
, &realm_dn
, &msg
);
2097 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
2098 principal
, SAMBA_KDC_ENT_TYPE_SERVER
,
2100 realm_dn
, msg
, entry_ex
);
2102 krb5_warnx(context
, "samba_kdc_fetch: message2entry failed");
2108 static krb5_error_code
samba_kdc_lookup_realm(krb5_context context
,
2109 struct samba_kdc_db_context
*kdc_db_ctx
,
2110 TALLOC_CTX
*mem_ctx
,
2111 krb5_const_principal principal
,
2113 struct sdb_entry_ex
*entry_ex
)
2115 TALLOC_CTX
*frame
= talloc_stackframe();
2117 krb5_error_code ret
;
2118 bool check_realm
= false;
2119 const char *realm
= NULL
;
2120 struct dsdb_trust_routing_table
*trt
= NULL
;
2121 const struct lsa_TrustDomainInfoInfoEx
*tdo
= NULL
;
2122 unsigned int num_comp
;
2126 num_comp
= krb5_princ_size(context
, principal
);
2128 if (flags
& SDB_F_GET_CLIENT
) {
2129 if (flags
& SDB_F_FOR_AS_REQ
) {
2133 if (flags
& SDB_F_GET_SERVER
) {
2134 if (flags
& SDB_F_FOR_TGS_REQ
) {
2144 realm
= smb_krb5_principal_get_realm(frame
, context
, principal
);
2145 if (realm
== NULL
) {
2151 * The requested realm needs to be our own
2153 ok
= lpcfg_is_my_domain_or_realm(kdc_db_ctx
->lp_ctx
, realm
);
2156 * The request is not for us...
2159 return SDB_ERR_NOENTRY
;
2162 if (smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
2163 char *principal_string
= NULL
;
2164 krb5_principal enterprise_principal
= NULL
;
2165 char *enterprise_realm
= NULL
;
2167 if (num_comp
!= 1) {
2169 return SDB_ERR_NOENTRY
;
2172 principal_string
= smb_krb5_principal_get_comp_string(frame
, context
,
2174 if (principal_string
== NULL
) {
2179 ret
= krb5_parse_name(context
, principal_string
,
2180 &enterprise_principal
);
2181 TALLOC_FREE(principal_string
);
2187 enterprise_realm
= smb_krb5_principal_get_realm(
2188 frame
, context
, enterprise_principal
);
2189 krb5_free_principal(context
, enterprise_principal
);
2190 if (enterprise_realm
!= NULL
) {
2191 realm
= enterprise_realm
;
2195 if (flags
& SDB_F_GET_SERVER
) {
2196 char *service_realm
= NULL
;
2198 ret
= principal_comp_strcmp(context
, principal
, 0, KRB5_TGS_NAME
);
2201 * we need to search krbtgt/ locally
2208 * We need to check the last component against the routing table.
2210 * Note this works only with 2 or 3 component principals, e.g:
2212 * servicePrincipalName: ldap/W2K8R2-219.bla.base
2213 * servicePrincipalName: ldap/W2K8R2-219.bla.base/bla.base
2214 * servicePrincipalName: ldap/W2K8R2-219.bla.base/ForestDnsZones.bla.base
2215 * servicePrincipalName: ldap/W2K8R2-219.bla.base/DomainDnsZones.bla.base
2218 if (num_comp
== 2 || num_comp
== 3) {
2219 service_realm
= smb_krb5_principal_get_comp_string(frame
,
2225 if (service_realm
!= NULL
) {
2226 realm
= service_realm
;
2230 ok
= lpcfg_is_my_domain_or_realm(kdc_db_ctx
->lp_ctx
, realm
);
2233 * skip the expensive routing lookup
2239 status
= dsdb_trust_routing_table_load(kdc_db_ctx
->samdb
,
2241 if (!NT_STATUS_IS_OK(status
)) {
2246 tdo
= dsdb_trust_routing_by_name(trt
, realm
);
2249 * This principal has to be local
2255 if (tdo
->trust_attributes
& LSA_TRUST_ATTRIBUTE_WITHIN_FOREST
) {
2257 * TODO: handle the routing within the forest
2259 * This should likely be handled in
2260 * samba_kdc_message2entry() in case we're
2261 * a global catalog. We'd need to check
2262 * if realm_dn is our own domain and derive
2263 * the dns domain name from realm_dn and check that
2264 * against the routing table or fallback to
2265 * the tdo we found here.
2267 * But for now we don't support multiple domains
2268 * in our forest correctly anyway.
2270 * Just search in our local database.
2276 ZERO_STRUCT(entry_ex
->entry
);
2278 ret
= krb5_copy_principal(context
, principal
,
2279 &entry_ex
->entry
.principal
);
2285 upper
= strupper_talloc(frame
, tdo
->domain_name
.string
);
2286 if (upper
== NULL
) {
2291 ret
= smb_krb5_principal_set_realm(context
,
2292 entry_ex
->entry
.principal
,
2300 return SDB_ERR_WRONG_REALM
;
2303 krb5_error_code
samba_kdc_fetch(krb5_context context
,
2304 struct samba_kdc_db_context
*kdc_db_ctx
,
2305 krb5_const_principal principal
,
2308 struct sdb_entry_ex
*entry_ex
)
2310 krb5_error_code ret
= SDB_ERR_NOENTRY
;
2311 TALLOC_CTX
*mem_ctx
;
2313 mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_fetch context");
2316 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
2320 ret
= samba_kdc_lookup_realm(context
, kdc_db_ctx
, mem_ctx
,
2321 principal
, flags
, entry_ex
);
2326 ret
= SDB_ERR_NOENTRY
;
2328 if (flags
& SDB_F_GET_CLIENT
) {
2329 ret
= samba_kdc_fetch_client(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, entry_ex
);
2330 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2332 if (flags
& SDB_F_GET_SERVER
) {
2333 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
2334 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, kvno
, entry_ex
);
2335 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2337 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
2338 ret
= samba_kdc_fetch_server(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, entry_ex
);
2339 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2341 if (flags
& SDB_F_GET_KRBTGT
) {
2342 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, kvno
, entry_ex
);
2343 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2347 talloc_free(mem_ctx
);
2351 struct samba_kdc_seq
{
2354 struct ldb_message
**msgs
;
2355 struct ldb_dn
*realm_dn
;
2358 static krb5_error_code
samba_kdc_seq(krb5_context context
,
2359 struct samba_kdc_db_context
*kdc_db_ctx
,
2360 struct sdb_entry_ex
*entry
)
2362 krb5_error_code ret
;
2363 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
2364 const char *realm
= lpcfg_realm(kdc_db_ctx
->lp_ctx
);
2365 struct ldb_message
*msg
= NULL
;
2366 const char *sAMAccountName
= NULL
;
2367 krb5_principal principal
= NULL
;
2368 TALLOC_CTX
*mem_ctx
;
2371 return SDB_ERR_NOENTRY
;
2374 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_seq context");
2378 krb5_set_error_message(context
, ret
, "samba_kdc_seq: talloc_named() failed!");
2382 while (priv
->index
< priv
->count
) {
2383 msg
= priv
->msgs
[priv
->index
++];
2385 sAMAccountName
= ldb_msg_find_attr_as_string(msg
, "sAMAccountName", NULL
);
2386 if (sAMAccountName
!= NULL
) {
2391 if (sAMAccountName
== NULL
) {
2392 ret
= SDB_ERR_NOENTRY
;
2396 ret
= smb_krb5_make_principal(context
, &principal
,
2397 realm
, sAMAccountName
, NULL
);
2402 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
2403 principal
, SAMBA_KDC_ENT_TYPE_ANY
,
2404 SDB_F_ADMIN_DATA
|SDB_F_GET_ANY
,
2405 priv
->realm_dn
, msg
, entry
);
2408 if (principal
!= NULL
) {
2409 krb5_free_principal(context
, principal
);
2414 kdc_db_ctx
->seq_ctx
= NULL
;
2416 talloc_free(mem_ctx
);
2422 krb5_error_code
samba_kdc_firstkey(krb5_context context
,
2423 struct samba_kdc_db_context
*kdc_db_ctx
,
2424 struct sdb_entry_ex
*entry
)
2426 struct ldb_context
*ldb_ctx
= kdc_db_ctx
->samdb
;
2427 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
2429 struct ldb_result
*res
= NULL
;
2430 krb5_error_code ret
;
2431 TALLOC_CTX
*mem_ctx
;
2436 kdc_db_ctx
->seq_ctx
= NULL
;
2439 priv
= (struct samba_kdc_seq
*) talloc(kdc_db_ctx
, struct samba_kdc_seq
);
2442 krb5_set_error_message(context
, ret
, "talloc: out of memory");
2448 priv
->realm_dn
= ldb_get_default_basedn(ldb_ctx
);
2451 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_firstkey context");
2455 krb5_set_error_message(context
, ret
, "samba_kdc_firstkey: talloc_named() failed!");
2459 ret
= krb5_get_default_realm(context
, &realm
);
2464 krb5_free_default_realm(context
, realm
);
2466 lret
= dsdb_search(ldb_ctx
, priv
, &res
,
2467 priv
->realm_dn
, LDB_SCOPE_SUBTREE
, user_attrs
,
2468 DSDB_SEARCH_NO_GLOBAL_CATALOG
,
2469 "(objectClass=user)");
2471 if (lret
!= LDB_SUCCESS
) {
2473 return SDB_ERR_NOENTRY
;
2476 priv
->count
= res
->count
;
2477 priv
->msgs
= talloc_steal(priv
, res
->msgs
);
2480 kdc_db_ctx
->seq_ctx
= priv
;
2482 ret
= samba_kdc_seq(context
, kdc_db_ctx
, entry
);
2486 kdc_db_ctx
->seq_ctx
= NULL
;
2488 talloc_free(mem_ctx
);
2493 krb5_error_code
samba_kdc_nextkey(krb5_context context
,
2494 struct samba_kdc_db_context
*kdc_db_ctx
,
2495 struct sdb_entry_ex
*entry
)
2497 return samba_kdc_seq(context
, kdc_db_ctx
, entry
);
2500 /* Check if a given entry may delegate or do s4u2self to this target principal
2502 * This is currently a very nasty hack - allowing only delegation to itself.
2505 samba_kdc_check_s4u2self(krb5_context context
,
2506 struct samba_kdc_db_context
*kdc_db_ctx
,
2507 struct samba_kdc_entry
*skdc_entry
,
2508 krb5_const_principal target_principal
)
2510 krb5_error_code ret
;
2511 struct ldb_dn
*realm_dn
;
2512 struct ldb_message
*msg
;
2513 struct dom_sid
*orig_sid
;
2514 struct dom_sid
*target_sid
;
2515 const char *delegation_check_attrs
[] = {
2519 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_s4u2self");
2523 krb5_set_error_message(context
, ret
, "samba_kdc_check_s4u2self: talloc_named() failed!");
2527 ret
= samba_kdc_lookup_server(context
, kdc_db_ctx
, mem_ctx
, target_principal
,
2528 SDB_F_GET_CLIENT
|SDB_F_GET_SERVER
,
2529 delegation_check_attrs
, &realm_dn
, &msg
);
2532 talloc_free(mem_ctx
);
2536 orig_sid
= samdb_result_dom_sid(mem_ctx
, skdc_entry
->msg
, "objectSid");
2537 target_sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
2539 /* Allow delegation to the same principal, even if by a different
2540 * name. The easy and safe way to prove this is by SID
2542 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
2543 talloc_free(mem_ctx
);
2544 return KRB5KDC_ERR_BADOPTION
;
2547 talloc_free(mem_ctx
);
2551 /* Certificates printed by a the Certificate Authority might have a
2552 * slightly different form of the user principal name to that in the
2553 * database. Allow a mismatch where they both refer to the same
2557 samba_kdc_check_pkinit_ms_upn_match(krb5_context context
,
2558 struct samba_kdc_db_context
*kdc_db_ctx
,
2559 struct samba_kdc_entry
*skdc_entry
,
2560 krb5_const_principal certificate_principal
)
2562 krb5_error_code ret
;
2563 struct ldb_dn
*realm_dn
;
2564 struct ldb_message
*msg
;
2565 struct dom_sid
*orig_sid
;
2566 struct dom_sid
*target_sid
;
2567 const char *ms_upn_check_attrs
[] = {
2571 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_pkinit_ms_upn_match");
2575 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
2579 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
2580 mem_ctx
, certificate_principal
,
2581 ms_upn_check_attrs
, &realm_dn
, &msg
);
2584 talloc_free(mem_ctx
);
2588 orig_sid
= samdb_result_dom_sid(mem_ctx
, skdc_entry
->msg
, "objectSid");
2589 target_sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
2591 /* Consider these to be the same principal, even if by a different
2592 * name. The easy and safe way to prove this is by SID
2594 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
2595 talloc_free(mem_ctx
);
2596 #if defined(KRB5KDC_ERR_CLIENT_NAME_MISMATCH) /* MIT */
2597 return KRB5KDC_ERR_CLIENT_NAME_MISMATCH
;
2598 #else /* Heimdal (where this is an enum) */
2599 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
2603 talloc_free(mem_ctx
);
2608 * Check if a given entry may delegate to this target principal
2612 samba_kdc_check_s4u2proxy(krb5_context context
,
2613 struct samba_kdc_db_context
*kdc_db_ctx
,
2614 struct samba_kdc_entry
*skdc_entry
,
2615 krb5_const_principal target_principal
)
2617 krb5_error_code ret
;
2619 const char *client_dn
= NULL
;
2620 const char *target_principal_name
= NULL
;
2621 struct ldb_message_element
*el
;
2626 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_s4u2proxy");
2630 krb5_set_error_message(context
, ret
,
2631 "samba_kdc_check_s4u2proxy:"
2632 " talloc_named() failed!");
2636 client_dn
= ldb_dn_get_linearized(skdc_entry
->msg
->dn
);
2642 krb5_set_error_message(context
, ret
,
2643 "samba_kdc_check_s4u2proxy:"
2644 " ldb_dn_get_linearized() failed!");
2649 * The main heimdal code already checked that the target_principal
2650 * belongs to the same realm as the client.
2652 * So we just need the principal without the realm,
2653 * as that is what is configured in the "msDS-AllowedToDelegateTo"
2656 ret
= krb5_unparse_name_flags(context
, target_principal
,
2657 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &tmp
);
2659 talloc_free(mem_ctx
);
2660 krb5_set_error_message(context
, ret
,
2661 "samba_kdc_check_s4u2proxy:"
2662 " krb5_unparse_name() failed!");
2665 DEBUG(10,("samba_kdc_check_s4u2proxy: client[%s] for target[%s]\n",
2668 target_principal_name
= talloc_strdup(mem_ctx
, tmp
);
2670 if (target_principal_name
== NULL
) {
2672 krb5_set_error_message(context
, ret
,
2673 "samba_kdc_check_s4u2proxy:"
2674 " talloc_strdup() failed!");
2678 el
= ldb_msg_find_element(skdc_entry
->msg
, "msDS-AllowedToDelegateTo");
2683 val
= data_blob_string_const(target_principal_name
);
2685 for (i
=0; i
<el
->num_values
; i
++) {
2686 struct ldb_val
*val1
= &val
;
2687 struct ldb_val
*val2
= &el
->values
[i
];
2690 if (val1
->length
!= val2
->length
) {
2694 cmp
= strncasecmp((const char *)val1
->data
,
2695 (const char *)val2
->data
,
2709 DEBUG(10,("samba_kdc_check_s4u2proxy: client[%s] allowed target[%s]\n",
2711 talloc_free(mem_ctx
);
2715 krb5_set_error_message(context
, ret
,
2716 "samba_kdc_check_s4u2proxy: client[%s] "
2717 "not allowed for delegation to target[%s]",
2719 target_principal_name
);
2720 talloc_free(mem_ctx
);
2721 return KRB5KDC_ERR_BADOPTION
;
2724 NTSTATUS
samba_kdc_setup_db_ctx(TALLOC_CTX
*mem_ctx
, struct samba_kdc_base_context
*base_ctx
,
2725 struct samba_kdc_db_context
**kdc_db_ctx_out
)
2728 struct ldb_message
*msg
;
2729 struct auth_session_info
*session_info
;
2730 struct samba_kdc_db_context
*kdc_db_ctx
;
2731 /* The idea here is very simple. Using Kerberos to
2732 * authenticate the KDC to the LDAP server is higly likely to
2735 * In future we may set this up to use EXERNAL and SSL
2736 * certificates, for now it will almost certainly be NTLMSSP_SET_USERNAME
2739 kdc_db_ctx
= talloc_zero(mem_ctx
, struct samba_kdc_db_context
);
2740 if (kdc_db_ctx
== NULL
) {
2741 return NT_STATUS_NO_MEMORY
;
2743 kdc_db_ctx
->ev_ctx
= base_ctx
->ev_ctx
;
2744 kdc_db_ctx
->lp_ctx
= base_ctx
->lp_ctx
;
2745 kdc_db_ctx
->msg_ctx
= base_ctx
->msg_ctx
;
2747 /* get default kdc policy */
2748 lpcfg_default_kdc_policy(mem_ctx
,
2750 &kdc_db_ctx
->policy
.svc_tkt_lifetime
,
2751 &kdc_db_ctx
->policy
.usr_tkt_lifetime
,
2752 &kdc_db_ctx
->policy
.renewal_lifetime
);
2754 session_info
= system_session(kdc_db_ctx
->lp_ctx
);
2755 if (session_info
== NULL
) {
2756 return NT_STATUS_INTERNAL_ERROR
;
2759 /* Setup the link to LDB */
2760 kdc_db_ctx
->samdb
= samdb_connect(kdc_db_ctx
,
2766 if (kdc_db_ctx
->samdb
== NULL
) {
2767 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot open samdb for KDC backend!"));
2768 talloc_free(kdc_db_ctx
);
2769 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2772 /* Find out our own krbtgt kvno */
2773 ldb_ret
= samdb_rodc(kdc_db_ctx
->samdb
, &kdc_db_ctx
->rodc
);
2774 if (ldb_ret
!= LDB_SUCCESS
) {
2775 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine if we are an RODC in KDC backend: %s\n",
2776 ldb_errstring(kdc_db_ctx
->samdb
)));
2777 talloc_free(kdc_db_ctx
);
2778 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2780 if (kdc_db_ctx
->rodc
) {
2781 int my_krbtgt_number
;
2782 const char *secondary_keytab
[] = { "msDS-SecondaryKrbTgtNumber", NULL
};
2783 struct ldb_dn
*account_dn
;
2784 struct ldb_dn
*server_dn
= samdb_server_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
);
2786 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine server DN in KDC backend: %s\n",
2787 ldb_errstring(kdc_db_ctx
->samdb
)));
2788 talloc_free(kdc_db_ctx
);
2789 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2792 ldb_ret
= samdb_reference_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
, server_dn
,
2793 "serverReference", &account_dn
);
2794 if (ldb_ret
!= LDB_SUCCESS
) {
2795 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine server account in KDC backend: %s\n",
2796 ldb_errstring(kdc_db_ctx
->samdb
)));
2797 talloc_free(kdc_db_ctx
);
2798 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2801 ldb_ret
= samdb_reference_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
, account_dn
,
2802 "msDS-KrbTgtLink", &kdc_db_ctx
->krbtgt_dn
);
2803 talloc_free(account_dn
);
2804 if (ldb_ret
!= LDB_SUCCESS
) {
2805 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine RODC krbtgt account in KDC backend: %s\n",
2806 ldb_errstring(kdc_db_ctx
->samdb
)));
2807 talloc_free(kdc_db_ctx
);
2808 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2811 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
, kdc_db_ctx
,
2812 &msg
, kdc_db_ctx
->krbtgt_dn
, LDB_SCOPE_BASE
,
2814 DSDB_SEARCH_NO_GLOBAL_CATALOG
,
2815 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=*))");
2816 if (ldb_ret
!= LDB_SUCCESS
) {
2817 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot read krbtgt account %s in KDC backend to get msDS-SecondaryKrbTgtNumber: %s: %s\n",
2818 ldb_dn_get_linearized(kdc_db_ctx
->krbtgt_dn
),
2819 ldb_errstring(kdc_db_ctx
->samdb
),
2820 ldb_strerror(ldb_ret
)));
2821 talloc_free(kdc_db_ctx
);
2822 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2824 my_krbtgt_number
= ldb_msg_find_attr_as_int(msg
, "msDS-SecondaryKrbTgtNumber", -1);
2825 if (my_krbtgt_number
== -1) {
2826 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot read msDS-SecondaryKrbTgtNumber from krbtgt account %s in KDC backend: got %d\n",
2827 ldb_dn_get_linearized(kdc_db_ctx
->krbtgt_dn
),
2829 talloc_free(kdc_db_ctx
);
2830 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2832 kdc_db_ctx
->my_krbtgt_number
= my_krbtgt_number
;
2835 kdc_db_ctx
->my_krbtgt_number
= 0;
2836 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
, kdc_db_ctx
,
2838 ldb_get_default_basedn(kdc_db_ctx
->samdb
),
2841 DSDB_SEARCH_NO_GLOBAL_CATALOG
,
2842 "(&(objectClass=user)(samAccountName=krbtgt))");
2844 if (ldb_ret
!= LDB_SUCCESS
) {
2845 DEBUG(1, ("samba_kdc_fetch: could not find own KRBTGT in DB: %s\n", ldb_errstring(kdc_db_ctx
->samdb
)));
2846 talloc_free(kdc_db_ctx
);
2847 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
2849 kdc_db_ctx
->krbtgt_dn
= talloc_steal(kdc_db_ctx
, msg
->dn
);
2850 kdc_db_ctx
->my_krbtgt_number
= 0;
2853 *kdc_db_ctx_out
= kdc_db_ctx
;
2854 return NT_STATUS_OK
;