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 "librpc/gen_ndr/ndr_security.h"
27 #include "auth/auth.h"
28 #include "auth/auth_sam.h"
29 #include "dsdb/samdb/samdb.h"
30 #include "dsdb/common/util.h"
31 #include "librpc/gen_ndr/ndr_drsblobs.h"
32 #include "param/param.h"
33 #include "param/secrets.h"
34 #include "../lib/crypto/md4.h"
35 #include "system/kerberos.h"
36 #include "auth/kerberos/kerberos.h"
37 #include "kdc/authn_policy_util.h"
39 #include "kdc/samba_kdc.h"
40 #include "kdc/db-glue.h"
41 #include "kdc/pac-glue.h"
42 #include "librpc/gen_ndr/ndr_irpc_c.h"
43 #include "lib/messaging/irpc.h"
46 #define DBGC_CLASS DBGC_KERBEROS
51 #define SAMBA_KVNO_GET_KRBTGT(kvno) \
52 ((uint16_t)(((uint32_t)kvno) >> 16))
54 #define SAMBA_KVNO_GET_VALUE(kvno) \
55 ((uint16_t)(((uint32_t)kvno) & 0xFFFF))
57 #define SAMBA_KVNO_AND_KRBTGT(kvno, krbtgt) \
58 ((krb5_kvno)((((uint32_t)kvno) & 0xFFFF) | \
59 ((((uint32_t)krbtgt) << 16) & 0xFFFF0000)))
61 enum trust_direction
{
63 INBOUND
= LSA_TRUST_DIRECTION_INBOUND
,
64 OUTBOUND
= LSA_TRUST_DIRECTION_OUTBOUND
67 static const char *trust_attrs
[] = {
74 "msDS-TrustForestTrustInfo",
78 "msDS-SupportedEncryptionTypes",
83 send a message to the drepl server telling it to initiate a
84 REPL_SECRET getncchanges extended op to fetch the users secrets
86 static void auth_sam_trigger_repl_secret(TALLOC_CTX
*mem_ctx
,
87 struct imessaging_context
*msg_ctx
,
88 struct tevent_context
*event_ctx
,
89 struct ldb_dn
*user_dn
)
91 struct dcerpc_binding_handle
*irpc_handle
;
92 struct drepl_trigger_repl_secret r
;
93 struct tevent_req
*req
;
96 tmp_ctx
= talloc_new(mem_ctx
);
97 if (tmp_ctx
== NULL
) {
101 irpc_handle
= irpc_binding_handle_by_name(tmp_ctx
, msg_ctx
,
104 if (irpc_handle
== NULL
) {
105 DBG_WARNING("Unable to get binding handle for dreplsrv\n");
106 TALLOC_FREE(tmp_ctx
);
110 r
.in
.user_dn
= ldb_dn_get_linearized(user_dn
);
111 if (r
.in
.user_dn
== NULL
) {
112 DBG_WARNING("Unable to get user DN\n");
113 TALLOC_FREE(tmp_ctx
);
118 * This seem to rely on the current IRPC implementation,
119 * which delivers the message in the _send function.
121 * TODO: we need a ONE_WAY IRPC handle and register
122 * a callback and wait for it to be triggered!
124 req
= dcerpc_drepl_trigger_repl_secret_r_send(tmp_ctx
,
129 /* we aren't interested in a reply */
131 TALLOC_FREE(tmp_ctx
);
134 static time_t ldb_msg_find_krb5time_ldap_time(struct ldb_message
*msg
, const char *attr
, time_t default_val
)
140 gentime
= ldb_msg_find_attr_as_string(msg
, attr
, NULL
);
144 tmp
= strptime(gentime
, "%Y%m%d%H%M%SZ", &tm
);
152 static struct SDBFlags
uf2SDBFlags(krb5_context context
, uint32_t userAccountControl
, enum samba_kdc_ent_type ent_type
)
154 struct SDBFlags flags
= int2SDBFlags(0);
156 /* we don't allow kadmin deletes */
159 /* mark the principal as invalid to start with */
164 /* All accounts are servers, but this may be disabled again in the caller */
167 /* Account types - clear the invalid bit if it turns out to be valid */
168 if (userAccountControl
& UF_NORMAL_ACCOUNT
) {
169 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
175 if (userAccountControl
& UF_INTERDOMAIN_TRUST_ACCOUNT
) {
176 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
181 if (userAccountControl
& UF_WORKSTATION_TRUST_ACCOUNT
) {
182 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
187 if (userAccountControl
& UF_SERVER_TRUST_ACCOUNT
) {
188 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
194 /* Not permitted to act as a client if disabled */
195 if (userAccountControl
& UF_ACCOUNTDISABLE
) {
198 if (userAccountControl
& UF_LOCKOUT
) {
199 flags
.locked_out
= 1;
202 if (userAccountControl & UF_PASSWD_NOTREQD) {
207 UF_PASSWD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevant
209 if (userAccountControl
& UF_TEMP_DUPLICATE_ACCOUNT
) {
213 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in samba_kdc_message2entry() */
216 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
220 if (userAccountControl
& UF_SMARTCARD_REQUIRED
) {
221 flags
.require_hwauth
= 1;
223 if (userAccountControl
& UF_TRUSTED_FOR_DELEGATION
) {
224 flags
.ok_as_delegate
= 1;
226 if (userAccountControl
& UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION
) {
228 * this is confusing...
230 * UF_TRUSTED_FOR_DELEGATION
235 * UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION
236 * => trusted_for_delegation
238 flags
.trusted_for_delegation
= 1;
240 if (!(userAccountControl
& UF_NOT_DELEGATED
)) {
241 flags
.forwardable
= 1;
245 if (userAccountControl
& UF_DONT_REQUIRE_PREAUTH
) {
246 flags
.require_preauth
= 0;
248 flags
.require_preauth
= 1;
251 if (userAccountControl
& UF_NO_AUTH_DATA_REQUIRED
) {
252 flags
.no_auth_data_reqd
= 1;
258 static int samba_kdc_entry_destructor(struct samba_kdc_entry
*p
)
260 if (p
->db_entry
!= NULL
) {
262 * A sdb_entry still has a reference
267 if (p
->kdc_entry
!= NULL
) {
269 * hdb_entry or krb5_db_entry still
270 * have a reference...
279 * Sort keys in descending order of strength.
281 * Explanation from Greg Hudson:
283 * To encrypt tickets only the first returned key is used by the MIT KDC. The
284 * other keys just communicate support for session key enctypes, and aren't
285 * really used. The encryption key for the ticket enc part doesn't have
286 * to be of a type requested by the client. The session key enctype is chosen
287 * based on the client preference order, limited by the set of enctypes present
288 * in the server keys (unless the string attribute is set on the server
289 * principal overriding that set).
292 static int sdb_key_strength_priority(krb5_enctype etype
)
294 static const krb5_enctype etype_list
[] = {
295 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
296 ENCTYPE_AES128_CTS_HMAC_SHA1_96
,
297 ENCTYPE_DES3_CBC_SHA1
,
298 ENCTYPE_ARCFOUR_HMAC
,
306 for (i
= 0; i
< ARRAY_SIZE(etype_list
); i
++) {
307 if (etype
== etype_list
[i
]) {
312 return ARRAY_SIZE(etype_list
) - i
;
315 static int sdb_key_strength_cmp(const struct sdb_key
*k1
, const struct sdb_key
*k2
)
317 int p1
= sdb_key_strength_priority(KRB5_KEY_TYPE(&k1
->key
));
318 int p2
= sdb_key_strength_priority(KRB5_KEY_TYPE(&k2
->key
));
326 * Higher priority comes first
334 static void samba_kdc_sort_keys(struct sdb_keys
*keys
)
340 TYPESAFE_QSORT(keys
->val
, keys
->len
, sdb_key_strength_cmp
);
343 int samba_kdc_set_fixed_keys(krb5_context context
,
344 const struct ldb_val
*secretbuffer
,
345 uint32_t supported_enctypes
,
346 struct sdb_keys
*keys
)
348 uint16_t allocated_keys
= 0;
353 keys
->val
= calloc(allocated_keys
, sizeof(struct sdb_key
));
354 if (keys
->val
== NULL
) {
355 memset(secretbuffer
->data
, 0, secretbuffer
->length
);
360 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES256
) {
361 struct sdb_key key
= {};
363 ret
= smb_krb5_keyblock_init_contents(context
,
364 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
366 MIN(secretbuffer
->length
, 32),
369 memset(secretbuffer
->data
, 0, secretbuffer
->length
);
373 keys
->val
[keys
->len
] = key
;
377 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES128
) {
378 struct sdb_key key
= {};
380 ret
= smb_krb5_keyblock_init_contents(context
,
381 ENCTYPE_AES128_CTS_HMAC_SHA1_96
,
383 MIN(secretbuffer
->length
, 16),
386 memset(secretbuffer
->data
, 0, secretbuffer
->length
);
390 keys
->val
[keys
->len
] = key
;
394 if (supported_enctypes
& ENC_RC4_HMAC_MD5
) {
395 struct sdb_key key
= {};
397 ret
= smb_krb5_keyblock_init_contents(context
,
398 ENCTYPE_ARCFOUR_HMAC
,
400 MIN(secretbuffer
->length
, 16),
403 memset(secretbuffer
->data
, 0, secretbuffer
->length
);
407 keys
->val
[keys
->len
] = key
;
416 static int samba_kdc_set_random_keys(krb5_context context
,
417 uint32_t supported_enctypes
,
418 struct sdb_keys
*keys
)
420 struct ldb_val secret_val
;
421 uint8_t secretbuffer
[32];
424 * Fake keys until we have a better way to reject
425 * non-pkinit requests.
427 * We just need to indicate which encryption types are
430 generate_secret_buffer(secretbuffer
, sizeof(secretbuffer
));
432 secret_val
= data_blob_const(secretbuffer
,
433 sizeof(secretbuffer
));
434 return samba_kdc_set_fixed_keys(context
,
440 struct samba_kdc_user_keys
{
441 struct sdb_keys
*skeys
;
443 uint32_t *returned_kvno
;
444 uint32_t supported_enctypes
;
445 uint32_t *available_enctypes
;
446 const struct samr_Password
*nthash
;
447 const char *salt_string
;
449 const struct package_PrimaryKerberosKey4
*pkeys
;
452 static krb5_error_code
samba_kdc_fill_user_keys(krb5_context context
,
453 struct samba_kdc_user_keys
*p
)
456 * Make sure we'll never reveal DES keys
458 uint32_t supported_enctypes
= p
->supported_enctypes
&= ~(ENC_CRC32
| ENC_RSA_MD5
);
459 uint32_t _available_enctypes
= 0;
460 uint32_t *available_enctypes
= p
->available_enctypes
;
461 uint32_t _returned_kvno
= 0;
462 uint32_t *returned_kvno
= p
->returned_kvno
;
463 uint32_t num_pkeys
= p
->num_pkeys
;
464 uint32_t allocated_keys
= num_pkeys
;
468 if (available_enctypes
== NULL
) {
469 available_enctypes
= &_available_enctypes
;
472 *available_enctypes
= 0;
474 if (returned_kvno
== NULL
) {
475 returned_kvno
= &_returned_kvno
;
478 *returned_kvno
= p
->kvno
;
480 if (p
->nthash
!= NULL
) {
484 allocated_keys
= MAX(1, allocated_keys
);
486 /* allocate space to decode into */
488 p
->skeys
->val
= calloc(allocated_keys
, sizeof(struct sdb_key
));
489 if (p
->skeys
->val
== NULL
) {
493 for (i
=0; i
< num_pkeys
; i
++) {
494 struct sdb_key key
= {};
495 uint32_t enctype_bit
;
497 if (p
->pkeys
[i
].value
== NULL
) {
501 enctype_bit
= kerberos_enctype_to_bitmap(p
->pkeys
[i
].keytype
);
502 if (!(enctype_bit
& supported_enctypes
)) {
506 if (p
->salt_string
!= NULL
) {
509 salt
= data_blob_string_const(p
->salt_string
);
511 key
.salt
= calloc(1, sizeof(*key
.salt
));
512 if (key
.salt
== NULL
) {
517 key
.salt
->type
= KRB5_PW_SALT
;
519 ret
= smb_krb5_copy_data_contents(&key
.salt
->salt
,
523 ZERO_STRUCTP(key
.salt
);
529 ret
= smb_krb5_keyblock_init_contents(context
,
531 p
->pkeys
[i
].value
->data
,
532 p
->pkeys
[i
].value
->length
,
535 p
->skeys
->val
[p
->skeys
->len
++] = key
;
536 *available_enctypes
|= enctype_bit
;
539 ZERO_STRUCT(key
.key
);
541 if (ret
== KRB5_PROG_ETYPE_NOSUPP
) {
542 DEBUG(2,("Unsupported keytype ignored - type %u\n",
543 p
->pkeys
[i
].keytype
));
551 if (p
->nthash
!= NULL
&& (supported_enctypes
& ENC_RC4_HMAC_MD5
)) {
552 struct sdb_key key
= {};
554 ret
= smb_krb5_keyblock_init_contents(context
,
555 ENCTYPE_ARCFOUR_HMAC
,
557 sizeof(p
->nthash
->hash
),
560 p
->skeys
->val
[p
->skeys
->len
++] = key
;
562 *available_enctypes
|= ENC_RC4_HMAC_MD5
;
563 } else if (ret
== KRB5_PROG_ETYPE_NOSUPP
) {
564 DEBUG(2,("Unsupported keytype ignored - type %u\n",
565 ENCTYPE_ARCFOUR_HMAC
));
573 samba_kdc_sort_keys(p
->skeys
);
577 sdb_keys_free(p
->skeys
);
581 krb5_error_code
samba_kdc_message2entry_keys(krb5_context context
,
583 const struct ldb_message
*msg
,
586 uint32_t userAccountControl
,
587 enum samba_kdc_ent_type ent_type
,
589 krb5_kvno requested_kvno
,
590 struct sdb_entry
*entry
,
591 const uint32_t supported_enctypes_in
,
592 uint32_t *supported_enctypes_out
)
594 krb5_error_code ret
= 0;
595 enum ndr_err_code ndr_err
;
596 struct samr_Password
*hash
;
597 unsigned int num_ntPwdHistory
= 0;
598 struct samr_Password
*ntPwdHistory
= NULL
;
599 struct samr_Password
*old_hash
= NULL
;
600 struct samr_Password
*older_hash
= NULL
;
601 const struct ldb_val
*sc_val
;
602 struct supplementalCredentialsBlob scb
;
603 struct supplementalCredentialsPackage
*scpk
= NULL
;
604 struct package_PrimaryKerberosBlob _pkb
;
605 struct package_PrimaryKerberosCtr4
*pkb4
= NULL
;
606 int krbtgt_number
= 0;
607 uint32_t current_kvno
;
608 uint32_t old_kvno
= 0;
609 uint32_t older_kvno
= 0;
610 uint32_t returned_kvno
= 0;
612 struct samba_kdc_user_keys keys
= { .num_pkeys
= 0, };
613 struct samba_kdc_user_keys old_keys
= { .num_pkeys
= 0, };
614 struct samba_kdc_user_keys older_keys
= { .num_pkeys
= 0, };
615 uint32_t available_enctypes
= 0;
616 uint32_t supported_enctypes
= supported_enctypes_in
;
618 *supported_enctypes_out
= 0;
620 /* Is this the krbtgt or a RODC krbtgt */
622 krbtgt_number
= ldb_msg_find_attr_as_int(msg
, "msDS-SecondaryKrbTgtNumber", -1);
624 if (krbtgt_number
== -1) {
627 if (krbtgt_number
== 0) {
632 if ((ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
)
633 && (userAccountControl
& UF_SMARTCARD_REQUIRED
)) {
634 ret
= samba_kdc_set_random_keys(context
,
638 *supported_enctypes_out
= supported_enctypes
& ENC_ALL_TYPES
;
643 current_kvno
= ldb_msg_find_attr_as_int(msg
, "msDS-KeyVersionNumber", 0);
644 if (current_kvno
> 1) {
645 old_kvno
= current_kvno
- 1;
647 if (current_kvno
> 2) {
648 older_kvno
= current_kvno
- 2;
652 * Even for the main krbtgt account
653 * we have to strictly split the kvno into
654 * two 16-bit parts and the upper 16-bit
655 * need to be all zero, even if
656 * the msDS-KeyVersionNumber has a value
659 * See https://bugzilla.samba.org/show_bug.cgi?id=14951
661 current_kvno
= SAMBA_KVNO_GET_VALUE(current_kvno
);
662 old_kvno
= SAMBA_KVNO_GET_VALUE(old_kvno
);
663 older_kvno
= SAMBA_KVNO_GET_VALUE(older_kvno
);
664 requested_kvno
= SAMBA_KVNO_GET_VALUE(requested_kvno
);
667 /* Get keys from the db */
669 hash
= samdb_result_hash(mem_ctx
, msg
, "unicodePwd");
670 num_ntPwdHistory
= samdb_result_hashes(mem_ctx
, msg
,
673 if (num_ntPwdHistory
> 1) {
674 old_hash
= &ntPwdHistory
[1];
676 if (num_ntPwdHistory
> 2) {
677 older_hash
= &ntPwdHistory
[1];
679 sc_val
= ldb_msg_find_ldb_val(msg
, "supplementalCredentials");
681 /* supplementalCredentials if present */
683 ndr_err
= ndr_pull_struct_blob_all(sc_val
, mem_ctx
, &scb
,
684 (ndr_pull_flags_fn_t
)ndr_pull_supplementalCredentialsBlob
);
685 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
690 if (scb
.sub
.signature
!= SUPPLEMENTAL_CREDENTIALS_SIGNATURE
) {
691 if (scb
.sub
.num_packages
!= 0) {
692 NDR_PRINT_DEBUG(supplementalCredentialsBlob
, &scb
);
698 for (i
=0; i
< scb
.sub
.num_packages
; i
++) {
699 if (strcmp("Primary:Kerberos-Newer-Keys", scb
.sub
.packages
[i
].name
) == 0) {
700 scpk
= &scb
.sub
.packages
[i
];
701 if (!scpk
->data
|| !scpk
->data
[0]) {
710 * Primary:Kerberos-Newer-Keys element
711 * of supplementalCredentials
713 * The legacy Primary:Kerberos only contains
714 * single DES keys, which are completely ignored
720 blob
= strhex_to_data_blob(mem_ctx
, scpk
->data
);
726 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
727 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, &_pkb
,
728 (ndr_pull_flags_fn_t
)ndr_pull_package_PrimaryKerberosBlob
);
729 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
731 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
732 krb5_warnx(context
, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
736 if (_pkb
.version
!= 4) {
738 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
739 krb5_warnx(context
, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
743 pkb4
= &_pkb
.ctr
.ctr4
;
746 keys
= (struct samba_kdc_user_keys
) {
747 .kvno
= current_kvno
,
748 .supported_enctypes
= supported_enctypes
,
750 .salt_string
= pkb4
!= NULL
? pkb4
->salt
.string
: NULL
,
751 .num_pkeys
= pkb4
!= NULL
? pkb4
->num_keys
: 0,
752 .pkeys
= pkb4
!= NULL
? pkb4
->keys
: NULL
,
755 old_keys
= (struct samba_kdc_user_keys
) {
757 .supported_enctypes
= supported_enctypes
,
759 .salt_string
= pkb4
!= NULL
? pkb4
->salt
.string
: NULL
,
760 .num_pkeys
= pkb4
!= NULL
? pkb4
->num_old_keys
: 0,
761 .pkeys
= pkb4
!= NULL
? pkb4
->old_keys
: NULL
,
763 older_keys
= (struct samba_kdc_user_keys
) {
765 .supported_enctypes
= supported_enctypes
,
766 .nthash
= older_hash
,
767 .salt_string
= pkb4
!= NULL
? pkb4
->salt
.string
: NULL
,
768 .num_pkeys
= pkb4
!= NULL
? pkb4
->num_older_keys
: 0,
769 .pkeys
= pkb4
!= NULL
? pkb4
->older_keys
: NULL
,
772 if (flags
& SDB_F_KVNO_SPECIFIED
) {
773 if (requested_kvno
== keys
.kvno
) {
775 * The current kvno was requested,
778 keys
.skeys
= &entry
->keys
;
779 keys
.available_enctypes
= &available_enctypes
;
780 keys
.returned_kvno
= &returned_kvno
;
781 } else if (requested_kvno
== 0) {
783 * don't return any keys
785 } else if (requested_kvno
== old_keys
.kvno
) {
787 * return the old keys as default keys
788 * with the requested kvno.
790 old_keys
.skeys
= &entry
->keys
;
791 old_keys
.available_enctypes
= &available_enctypes
;
792 old_keys
.returned_kvno
= &returned_kvno
;
793 } else if (requested_kvno
== older_keys
.kvno
) {
795 * return the older keys as default keys
796 * with the requested kvno.
798 older_keys
.skeys
= &entry
->keys
;
799 older_keys
.available_enctypes
= &available_enctypes
;
800 older_keys
.returned_kvno
= &returned_kvno
;
803 * don't return any keys
807 bool include_history
= false;
809 if ((flags
& SDB_F_GET_CLIENT
) && (flags
& SDB_F_FOR_AS_REQ
)) {
810 include_history
= true;
811 } else if (flags
& SDB_F_ADMIN_DATA
) {
812 include_history
= true;
815 keys
.skeys
= &entry
->keys
;
816 keys
.available_enctypes
= &available_enctypes
;
817 keys
.returned_kvno
= &returned_kvno
;
819 if (include_history
&& old_keys
.kvno
!= 0) {
820 old_keys
.skeys
= &entry
->old_keys
;
822 if (include_history
&& older_keys
.kvno
!= 0) {
823 older_keys
.skeys
= &entry
->older_keys
;
827 if (keys
.skeys
!= NULL
) {
828 ret
= samba_kdc_fill_user_keys(context
, &keys
);
834 if (old_keys
.skeys
!= NULL
) {
835 ret
= samba_kdc_fill_user_keys(context
, &old_keys
);
841 if (older_keys
.skeys
!= NULL
) {
842 ret
= samba_kdc_fill_user_keys(context
, &older_keys
);
848 *supported_enctypes_out
|= available_enctypes
;
852 * Even for the main krbtgt account
853 * we have to strictly split the kvno into
854 * two 16-bit parts and the upper 16-bit
855 * need to be all zero, even if
856 * the msDS-KeyVersionNumber has a value
859 * See https://bugzilla.samba.org/show_bug.cgi?id=14951
861 returned_kvno
= SAMBA_KVNO_AND_KRBTGT(returned_kvno
, krbtgt_number
);
863 entry
->kvno
= returned_kvno
;
869 static int principal_comp_strcmp_int(krb5_context context
,
870 krb5_const_principal principal
,
871 unsigned int component
,
877 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
878 p
= krb5_principal_get_comp_string(context
, principal
, component
);
883 return strcasecmp(p
, string
);
885 return strcmp(p
, string
);
890 if (component
>= krb5_princ_size(context
, principal
)) {
894 d
= krb5_princ_component(context
, principal
, component
);
901 len
= strlen(string
);
904 * We explicitly return -1 or 1. Subtracting of the two lengths might
905 * give the wrong result if the result overflows or loses data when
908 if (d
->length
< len
) {
910 } else if (d
->length
> len
) {
915 return strncasecmp(p
, string
, len
);
917 return memcmp(p
, string
, len
);
922 static int principal_comp_strcasecmp(krb5_context context
,
923 krb5_const_principal principal
,
924 unsigned int component
,
927 return principal_comp_strcmp_int(context
, principal
,
928 component
, string
, true);
931 static int principal_comp_strcmp(krb5_context context
,
932 krb5_const_principal principal
,
933 unsigned int component
,
936 return principal_comp_strcmp_int(context
, principal
,
937 component
, string
, false);
940 static bool is_kadmin_changepw(krb5_context context
,
941 krb5_const_principal principal
)
943 return krb5_princ_size(context
, principal
) == 2 &&
944 (principal_comp_strcmp(context
, principal
, 0, "kadmin") == 0) &&
945 (principal_comp_strcmp(context
, principal
, 1, "changepw") == 0);
948 static krb5_error_code
samba_kdc_get_entry_principal(
949 krb5_context context
,
950 struct samba_kdc_db_context
*kdc_db_ctx
,
951 const char *samAccountName
,
952 enum samba_kdc_ent_type ent_type
,
954 bool is_kadmin_changepw
,
955 krb5_const_principal in_princ
,
956 krb5_principal
*out_princ
)
958 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
959 krb5_error_code code
= 0;
960 bool canon
= flags
& (SDB_F_CANON
|SDB_F_FORCE_CANON
);
963 * If we are set to canonicalize, we get back the fixed UPPER
964 * case realm, and the real username (ie matching LDAP
967 * Otherwise, if we are set to enterprise, we
968 * get back the whole principal as-sent
970 * Finally, if we are not set to canonicalize, we get back the
971 * fixed UPPER case realm, but the as-sent username
975 * We need to ensure that the kadmin/changepw principal isn't able to
976 * issue krbtgt tickets, even if canonicalization is turned on.
978 if (!is_kadmin_changepw
) {
979 if (ent_type
== SAMBA_KDC_ENT_TYPE_KRBTGT
&& canon
) {
981 * When requested to do so, ensure that both
982 * the realm values in the principal are set
983 * to the upper case, canonical realm
985 code
= smb_krb5_make_principal(context
,
994 smb_krb5_principal_set_type(context
,
1001 if ((canon
&& flags
& (SDB_F_FORCE_CANON
|SDB_F_FOR_AS_REQ
)) ||
1002 (ent_type
== SAMBA_KDC_ENT_TYPE_ANY
&& in_princ
== NULL
)) {
1004 * SDB_F_CANON maps from the canonicalize flag in the
1005 * packet, and has a different meaning between AS-REQ
1006 * and TGS-REQ. We only change the principal in the
1009 * The SDB_F_FORCE_CANON if for new MIT KDC code that
1010 * wants the canonical name in all lookups, and takes
1011 * care to canonicalize only when appropriate.
1013 code
= smb_krb5_make_principal(context
,
1015 lpcfg_realm(lp_ctx
),
1023 * For a krbtgt entry, this appears to be required regardless of the
1024 * canonicalize flag from the client.
1026 code
= krb5_copy_principal(context
, in_princ
, out_princ
);
1032 * While we have copied the client principal, tests show that Win2k3
1033 * returns the 'corrected' realm, not the client-specified realm. This
1034 * code attempts to replace the client principal's realm with the one
1035 * we determine from our records
1037 code
= smb_krb5_principal_set_realm(context
,
1039 lpcfg_realm(lp_ctx
));
1045 * Construct an hdb_entry from a directory entry.
1047 static krb5_error_code
samba_kdc_message2entry(krb5_context context
,
1048 struct samba_kdc_db_context
*kdc_db_ctx
,
1049 TALLOC_CTX
*mem_ctx
,
1050 krb5_const_principal principal
,
1051 enum samba_kdc_ent_type ent_type
,
1054 struct ldb_dn
*realm_dn
,
1055 struct ldb_message
*msg
,
1056 struct sdb_entry
*entry
)
1058 TALLOC_CTX
*tmp_ctx
= NULL
;
1059 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
1060 uint32_t userAccountControl
;
1061 uint32_t msDS_User_Account_Control_Computed
;
1062 krb5_error_code ret
= 0;
1063 krb5_boolean is_computer
= FALSE
;
1064 struct samba_kdc_entry
*p
;
1067 bool protected_user
= false;
1070 bool is_krbtgt
= false;
1071 bool is_rodc
= false;
1072 bool force_rc4
= lpcfg_kdc_force_enable_rc4_weak_session_keys(lp_ctx
);
1073 struct ldb_message_element
*objectclasses
;
1074 struct ldb_val computer_val
= data_blob_string_const("computer");
1075 uint32_t config_default_supported_enctypes
= lpcfg_kdc_default_domain_supported_enctypes(lp_ctx
);
1076 uint32_t default_supported_enctypes
=
1077 config_default_supported_enctypes
!= 0 ?
1078 config_default_supported_enctypes
:
1079 ENC_RC4_HMAC_MD5
| ENC_HMAC_SHA1_96_AES256_SK
;
1080 uint32_t supported_enctypes
1081 = ldb_msg_find_attr_as_uint(msg
,
1082 "msDS-SupportedEncryptionTypes",
1083 default_supported_enctypes
);
1084 uint32_t pa_supported_enctypes
;
1085 uint32_t supported_session_etypes
;
1086 uint32_t available_enctypes
= 0;
1088 * also legacy enctypes are announced,
1089 * but effectively restricted by kdc_enctypes
1091 uint32_t domain_enctypes
= ENC_RC4_HMAC_MD5
| ENC_RSA_MD5
| ENC_CRC32
;
1092 uint32_t config_kdc_enctypes
= lpcfg_kdc_supported_enctypes(lp_ctx
);
1093 uint32_t kdc_enctypes
=
1094 config_kdc_enctypes
!= 0 ?
1095 config_kdc_enctypes
:
1097 const char *samAccountName
= ldb_msg_find_attr_as_string(msg
, "samAccountName", NULL
);
1099 const struct authn_kerberos_client_policy
*authn_client_policy
= NULL
;
1100 const struct authn_server_policy
*authn_server_policy
= NULL
;
1101 int64_t enforced_tgt_lifetime_raw
;
1103 ZERO_STRUCTP(entry
);
1105 tmp_ctx
= talloc_new(mem_ctx
);
1106 if (tmp_ctx
== NULL
) {
1110 if (supported_enctypes
== 0) {
1111 supported_enctypes
= default_supported_enctypes
;
1114 if (dsdb_functional_level(kdc_db_ctx
->samdb
) >= DS_DOMAIN_FUNCTION_2008
) {
1115 domain_enctypes
|= ENC_HMAC_SHA1_96_AES128
| ENC_HMAC_SHA1_96_AES256
;
1118 if (ldb_msg_find_element(msg
, "msDS-SecondaryKrbTgtNumber")) {
1122 if (!samAccountName
) {
1124 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: no samAccountName present");
1128 objectclasses
= ldb_msg_find_element(msg
, "objectClass");
1130 if (objectclasses
&& ldb_msg_find_val(objectclasses
, &computer_val
)) {
1134 p
= talloc_zero(tmp_ctx
, struct samba_kdc_entry
);
1140 p
->is_rodc
= is_rodc
;
1141 p
->kdc_db_ctx
= kdc_db_ctx
;
1142 p
->realm_dn
= talloc_reference(p
, realm_dn
);
1148 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
1150 entry
->skdc_entry
= p
;
1152 userAccountControl
= ldb_msg_find_attr_as_uint(msg
, "userAccountControl", 0);
1154 msDS_User_Account_Control_Computed
1155 = ldb_msg_find_attr_as_uint(msg
,
1156 "msDS-User-Account-Control-Computed",
1160 * This brings in the lockout flag, block the account if not
1161 * found. We need the weird UF_ACCOUNTDISABLE check because
1162 * we do not want to fail open if the value is not returned,
1163 * but 0 is a valid value (all OK)
1165 if (msDS_User_Account_Control_Computed
== UF_ACCOUNTDISABLE
) {
1167 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: "
1168 "no msDS-User-Account-Control-Computed present");
1171 userAccountControl
|= msDS_User_Account_Control_Computed
;
1174 if (ent_type
== SAMBA_KDC_ENT_TYPE_KRBTGT
) {
1175 p
->is_krbtgt
= true;
1178 /* First try and figure out the flags based on the userAccountControl */
1179 entry
->flags
= uf2SDBFlags(context
, userAccountControl
, ent_type
);
1182 * Take control of the returned principal here, rather than
1183 * allowing the Heimdal code to do it as we have specific
1184 * behaviour around the forced realm to honour
1186 entry
->flags
.force_canonicalize
= true;
1188 /* Windows 2008 seems to enforce this (very sensible) rule by
1189 * default - don't allow offline attacks on a user's password
1190 * by asking for a ticket to them as a service (encrypted with
1191 * their probably pathetically insecure password) */
1193 if (entry
->flags
.server
1194 && lpcfg_parm_bool(lp_ctx
, NULL
, "kdc", "require spn for service", true)) {
1195 if (!is_computer
&& !ldb_msg_find_attr_as_string(msg
, "servicePrincipalName", NULL
)) {
1196 entry
->flags
.server
= 0;
1201 * We restrict a 3-part SPN ending in my domain/realm to full
1202 * domain controllers.
1204 * This avoids any cases where (eg) a demoted DC still has
1205 * these more restricted SPNs.
1207 if (krb5_princ_size(context
, principal
) > 2) {
1209 = smb_krb5_principal_get_comp_string(tmp_ctx
,
1214 lpcfg_is_my_domain_or_realm(lp_ctx
,
1216 bool is_dc
= userAccountControl
&
1217 (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
);
1218 if (is_our_realm
&& !is_dc
) {
1219 entry
->flags
.server
= 0;
1223 * To give the correct type of error to the client, we must
1224 * not just return the entry without .server set, we must
1225 * pretend the principal does not exist. Otherwise we may
1226 * return ERR_POLICY instead of
1227 * KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
1229 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
&& entry
->flags
.server
== 0) {
1230 ret
= SDB_ERR_NOENTRY
;
1231 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: no servicePrincipalName present for this server, refusing with no-such-entry");
1234 if (flags
& SDB_F_ADMIN_DATA
) {
1235 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
1236 * of the Heimdal KDC. They are stored in the traditional
1237 * DB for audit purposes, and still form part of the structure
1240 /* use 'whenCreated' */
1241 entry
->created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
1242 /* use 'kadmin' for now (needed by mit_samba) */
1244 ret
= smb_krb5_make_principal(context
,
1245 &entry
->created_by
.principal
,
1246 lpcfg_realm(lp_ctx
), "kadmin", NULL
);
1248 krb5_clear_error_message(context
);
1252 entry
->modified_by
= calloc(1, sizeof(struct sdb_event
));
1253 if (entry
->modified_by
== NULL
) {
1255 krb5_set_error_message(context
, ret
, "calloc: out of memory");
1259 /* use 'whenChanged' */
1260 entry
->modified_by
->time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenChanged", 0);
1261 /* use 'kadmin' for now (needed by mit_samba) */
1262 ret
= smb_krb5_make_principal(context
,
1263 &entry
->modified_by
->principal
,
1264 lpcfg_realm(lp_ctx
), "kadmin", NULL
);
1266 krb5_clear_error_message(context
);
1272 /* The lack of password controls etc applies to krbtgt by
1273 * virtue of being that particular RID */
1274 ret
= samdb_result_dom_sid_buf(msg
, "objectSid", &sid
);
1278 status
= dom_sid_split_rid(NULL
, &sid
, NULL
, &rid
);
1279 if (!NT_STATUS_IS_OK(status
)) {
1284 if (rid
== DOMAIN_RID_KRBTGT
) {
1287 entry
->valid_end
= NULL
;
1288 entry
->pw_end
= NULL
;
1290 entry
->flags
.invalid
= 0;
1291 entry
->flags
.server
= 1;
1293 realm
= smb_krb5_principal_get_realm(
1294 tmp_ctx
, context
, principal
);
1295 if (realm
== NULL
) {
1300 /* Don't mark all requests for the krbtgt/realm as
1301 * 'change password', as otherwise we could get into
1302 * trouble, and not enforce the password expiry.
1303 * Instead, only do it when request is for the kpasswd service */
1304 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
&&
1305 is_kadmin_changepw(context
, principal
) &&
1306 lpcfg_is_my_domain_or_realm(lp_ctx
, realm
)) {
1307 entry
->flags
.change_pw
= 1;
1312 entry
->flags
.client
= 0;
1313 entry
->flags
.forwardable
= 1;
1314 entry
->flags
.ok_as_delegate
= 1;
1315 } else if (is_rodc
) {
1316 /* The RODC krbtgt account is like the main krbtgt,
1317 * but it does not have a changepw or kadmin
1320 entry
->valid_end
= NULL
;
1321 entry
->pw_end
= NULL
;
1323 /* Also don't allow the RODC krbtgt to be a client (it should not be needed) */
1324 entry
->flags
.client
= 0;
1325 entry
->flags
.invalid
= 0;
1326 entry
->flags
.server
= 1;
1328 entry
->flags
.client
= 0;
1329 entry
->flags
.forwardable
= 1;
1330 entry
->flags
.ok_as_delegate
= 0;
1331 } else if (entry
->flags
.server
&& ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
1332 /* The account/password expiry only applies when the account is used as a
1333 * client (ie password login), not when used as a server */
1335 /* Make very well sure we don't use this for a client,
1336 * it could bypass the password restrictions */
1337 entry
->flags
.client
= 0;
1339 entry
->valid_end
= NULL
;
1340 entry
->pw_end
= NULL
;
1343 NTTIME must_change_time
1344 = samdb_result_nttime(msg
,
1345 "msDS-UserPasswordExpiryTimeComputed",
1347 if (must_change_time
== 0x7FFFFFFFFFFFFFFFULL
) {
1348 entry
->pw_end
= NULL
;
1350 entry
->pw_end
= malloc(sizeof(*entry
->pw_end
));
1351 if (entry
->pw_end
== NULL
) {
1355 *entry
->pw_end
= nt_time_to_unix(must_change_time
);
1358 acct_expiry
= samdb_result_account_expires(msg
);
1359 if (acct_expiry
== 0x7FFFFFFFFFFFFFFFULL
) {
1360 entry
->valid_end
= NULL
;
1362 entry
->valid_end
= malloc(sizeof(*entry
->valid_end
));
1363 if (entry
->valid_end
== NULL
) {
1367 *entry
->valid_end
= nt_time_to_unix(acct_expiry
);
1371 ret
= samba_kdc_get_entry_principal(context
,
1376 entry
->flags
.change_pw
,
1380 krb5_clear_error_message(context
);
1384 entry
->valid_start
= NULL
;
1386 entry
->max_life
= malloc(sizeof(*entry
->max_life
));
1387 if (entry
->max_life
== NULL
) {
1392 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
1393 *entry
->max_life
= kdc_db_ctx
->policy
.svc_tkt_lifetime
;
1394 } else if (ent_type
== SAMBA_KDC_ENT_TYPE_KRBTGT
|| ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
) {
1395 *entry
->max_life
= kdc_db_ctx
->policy
.usr_tkt_lifetime
;
1397 *entry
->max_life
= MIN(kdc_db_ctx
->policy
.svc_tkt_lifetime
,
1398 kdc_db_ctx
->policy
.usr_tkt_lifetime
);
1401 if (entry
->flags
.change_pw
) {
1402 /* Limit lifetime of kpasswd tickets to two minutes or less. */
1403 *entry
->max_life
= MIN(*entry
->max_life
, CHANGEPW_LIFETIME
);
1406 entry
->max_renew
= malloc(sizeof(*entry
->max_renew
));
1407 if (entry
->max_renew
== NULL
) {
1412 *entry
->max_renew
= kdc_db_ctx
->policy
.renewal_lifetime
;
1415 * A principal acting as a client that is not being looked up as the
1416 * principal of an armor ticket may have an authentication policy apply
1419 * We won’t get an authentication policy for the client of an S4U2Self
1420 * or S4U2Proxy request. Those clients are looked up with
1421 * SDB_F_FOR_TGS_REQ instead of with SDB_F_FOR_AS_REQ.
1423 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
&&
1424 (flags
& SDB_F_FOR_AS_REQ
) &&
1425 !(flags
& SDB_F_ARMOR_PRINCIPAL
))
1427 ret
= authn_policy_kerberos_client(kdc_db_ctx
->samdb
, tmp_ctx
, msg
,
1428 &authn_client_policy
);
1435 * A principal acting as a server may have an authentication policy
1438 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
1439 ret
= authn_policy_server(kdc_db_ctx
->samdb
, tmp_ctx
, msg
,
1440 &authn_server_policy
);
1446 enforced_tgt_lifetime_raw
= authn_policy_enforced_tgt_lifetime_raw(authn_client_policy
);
1447 if (enforced_tgt_lifetime_raw
!= 0) {
1448 int64_t lifetime_secs
= enforced_tgt_lifetime_raw
;
1450 lifetime_secs
/= INT64_C(1000) * 1000 * 10;
1451 lifetime_secs
= MIN(lifetime_secs
, INT_MAX
);
1452 lifetime_secs
= MAX(lifetime_secs
, INT_MIN
);
1455 * Set both lifetime and renewal time based only on the
1456 * configured maximum lifetime — not on the configured renewal
1457 * time. Yes, this is what Windows does.
1459 lifetime_secs
= MIN(*entry
->max_life
, lifetime_secs
);
1460 *entry
->max_life
= lifetime_secs
;
1461 *entry
->max_renew
= lifetime_secs
;
1464 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
&& (flags
& SDB_F_FOR_AS_REQ
)) {
1466 const struct auth_user_info_dc
*user_info_dc
= NULL
;
1468 * These protections only apply to clients, so servers in the
1469 * Protected Users group may still have service tickets to them
1470 * encrypted with RC4. For accounts looked up as servers, note
1471 * that 'msg' does not contain the 'memberOf' attribute for
1472 * determining whether the account is a member of Protected
1475 * Additionally, Microsoft advises that accounts for services
1476 * and computers should never be members of Protected Users, or
1477 * they may fail to authenticate.
1479 status
= samba_kdc_get_user_info_from_db(p
, msg
, &user_info_dc
);
1480 if (!NT_STATUS_IS_OK(status
)) {
1485 result
= dsdb_is_protected_user(kdc_db_ctx
->samdb
,
1487 user_info_dc
->num_sids
);
1493 protected_user
= result
;
1495 if (protected_user
) {
1496 entry
->flags
.forwardable
= 0;
1497 entry
->flags
.proxiable
= 0;
1499 if (enforced_tgt_lifetime_raw
== 0) {
1501 * If a TGT lifetime hasn’t been set, Protected
1502 * Users enforces a four hour TGT lifetime.
1504 *entry
->max_life
= MIN(*entry
->max_life
, 4 * 60 * 60);
1505 *entry
->max_renew
= MIN(*entry
->max_renew
, 4 * 60 * 60);
1510 if (rid
== DOMAIN_RID_KRBTGT
|| is_rodc
) {
1516 * KDCs (and KDCs on RODCs)
1517 * ignore msDS-SupportedEncryptionTypes completely
1518 * but support all supported enctypes by the domain.
1520 supported_enctypes
= domain_enctypes
;
1522 enable_fast
= lpcfg_kdc_enable_fast(kdc_db_ctx
->lp_ctx
);
1524 supported_enctypes
|= ENC_FAST_SUPPORTED
;
1527 supported_enctypes
|= ENC_CLAIMS_SUPPORTED
;
1528 supported_enctypes
|= ENC_COMPOUND_IDENTITY_SUPPORTED
;
1531 * Resource SID compression is enabled implicitly, unless
1532 * disabled in msDS-SupportedEncryptionTypes.
1535 } else if (userAccountControl
& (UF_PARTIAL_SECRETS_ACCOUNT
|UF_SERVER_TRUST_ACCOUNT
)) {
1537 * DCs and RODCs computer accounts take
1538 * msDS-SupportedEncryptionTypes unmodified, but
1539 * force all enctypes supported by the domain.
1541 supported_enctypes
|= domain_enctypes
;
1543 } else if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
||
1544 (ent_type
== SAMBA_KDC_ENT_TYPE_ANY
)) {
1546 * for AS-REQ the client chooses the enc types it
1547 * supports, and this will vary between computers a
1548 * user logs in from. Therefore, so that we accept any
1549 * of the client's keys for decrypting padata,
1550 * supported_enctypes should not restrict etype usage.
1552 * likewise for 'any' return as much as is supported,
1553 * to export into a keytab.
1555 supported_enctypes
|= ENC_ALL_TYPES
;
1558 /* If UF_USE_DES_KEY_ONLY has been set, then don't allow use of the newer enc types */
1559 if (userAccountControl
& UF_USE_DES_KEY_ONLY
) {
1560 supported_enctypes
&= ~ENC_ALL_TYPES
;
1563 if (protected_user
) {
1564 supported_enctypes
&= ~ENC_RC4_HMAC_MD5
;
1567 pa_supported_enctypes
= supported_enctypes
;
1568 supported_session_etypes
= supported_enctypes
;
1569 if (supported_session_etypes
& ENC_HMAC_SHA1_96_AES256_SK
) {
1570 supported_session_etypes
|= ENC_HMAC_SHA1_96_AES256
;
1571 supported_session_etypes
|= ENC_HMAC_SHA1_96_AES128
;
1574 supported_session_etypes
|= ENC_RC4_HMAC_MD5
;
1577 * now that we remembered what to announce in pa_supported_enctypes
1578 * and normalized ENC_HMAC_SHA1_96_AES256_SK, we restrict the
1579 * rest to the enc types the local kdc supports.
1581 supported_enctypes
&= kdc_enctypes
;
1582 supported_session_etypes
&= kdc_enctypes
;
1584 /* Get keys from the db */
1585 ret
= samba_kdc_message2entry_keys(context
, p
, msg
,
1588 ent_type
, flags
, kvno
, entry
,
1590 &available_enctypes
);
1592 /* Could be bogus data in the entry, or out of memory */
1597 * If we only have a nthash stored,
1598 * but a better session key would be
1599 * available, we fallback to fetching the
1600 * RC4_HMAC_MD5, which implicitly also
1601 * would allow an RC4_HMAC_MD5 session key.
1602 * But only if the kdc actually supports
1605 if (available_enctypes
== 0 &&
1606 (supported_enctypes
& ENC_RC4_HMAC_MD5
) == 0 &&
1607 (supported_enctypes
& ~ENC_RC4_HMAC_MD5
) != 0 &&
1608 (kdc_enctypes
& ENC_RC4_HMAC_MD5
) != 0)
1610 supported_enctypes
= ENC_RC4_HMAC_MD5
;
1611 ret
= samba_kdc_message2entry_keys(context
, p
, msg
,
1614 ent_type
, flags
, kvno
, entry
,
1616 &available_enctypes
);
1618 /* Could be bogus data in the entry, or out of memory */
1624 * We need to support all session keys enctypes for
1625 * all keys we provide
1627 supported_session_etypes
|= available_enctypes
;
1629 ret
= sdb_entry_set_etypes(entry
);
1634 if (entry
->flags
.server
) {
1636 supported_session_etypes
& KERB_ENCTYPE_AES256_CTS_HMAC_SHA1_96
;
1638 supported_session_etypes
& KERB_ENCTYPE_AES128_CTS_HMAC_SHA1_96
;
1640 supported_session_etypes
& ENC_RC4_HMAC_MD5
;
1641 ret
= sdb_entry_set_session_etypes(entry
,
1650 if (entry
->keys
.len
!= 0) {
1652 * FIXME: Currently limited to Heimdal so as not to
1653 * break MIT KDCs, for which no fix is available.
1655 #ifdef SAMBA4_USES_HEIMDAL
1658 * The krbtgt account, having no reason to
1659 * issue tickets encrypted in weaker keys,
1660 * shall only make available its strongest
1661 * key. All weaker keys are stripped out. This
1662 * makes it impossible for an RC4-encrypted
1663 * TGT to be accepted when AES KDC keys exist.
1665 * This controls the ticket key and so the PAC
1666 * signature algorithms indirectly, preventing
1667 * a weak KDC checksum from being accepted
1668 * when we verify the signatures for an
1669 * S4U2Proxy evidence ticket. As such, this is
1670 * indispensable for addressing
1673 * Being strict here also provides protection
1674 * against possible future attacks on weak
1677 entry
->keys
.len
= 1;
1678 if (entry
->etypes
!= NULL
) {
1679 entry
->etypes
->len
= MIN(entry
->etypes
->len
, 1);
1681 entry
->old_keys
.len
= MIN(entry
->old_keys
.len
, 1);
1682 entry
->older_keys
.len
= MIN(entry
->older_keys
.len
, 1);
1685 } else if (kdc_db_ctx
->rodc
) {
1687 * We are on an RODC, but don't have keys for this
1688 * account. Signal this to the caller
1690 auth_sam_trigger_repl_secret(kdc_db_ctx
,
1691 kdc_db_ctx
->msg_ctx
,
1694 ret
= SDB_ERR_NOT_FOUND_HERE
;
1698 * oh, no password. Apparently (comment in
1699 * hdb-ldap.c) this violates the ASN.1, but this
1700 * allows an entry with no keys (yet).
1704 p
->msg
= talloc_steal(p
, msg
);
1705 p
->supported_enctypes
= pa_supported_enctypes
;
1707 p
->client_policy
= talloc_steal(p
, authn_client_policy
);
1708 p
->server_policy
= talloc_steal(p
, authn_server_policy
);
1710 talloc_steal(kdc_db_ctx
, p
);
1714 /* This doesn't free ent itself, that is for the eventual caller to do */
1715 sdb_entry_free(entry
);
1718 talloc_free(tmp_ctx
);
1723 * Construct an hdb_entry from a directory entry.
1724 * The kvno is what the remote client asked for
1726 static krb5_error_code
samba_kdc_trust_message2entry(krb5_context context
,
1727 struct samba_kdc_db_context
*kdc_db_ctx
,
1728 TALLOC_CTX
*mem_ctx
,
1729 enum trust_direction direction
,
1730 struct ldb_dn
*realm_dn
,
1733 struct ldb_message
*msg
,
1734 struct sdb_entry
*entry
)
1736 TALLOC_CTX
*tmp_ctx
= NULL
;
1737 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
1738 const char *our_realm
= lpcfg_realm(lp_ctx
);
1739 char *partner_realm
= NULL
;
1740 const char *realm
= NULL
;
1741 const char *krbtgt_realm
= NULL
;
1742 DATA_BLOB password_utf16
= data_blob_null
;
1743 DATA_BLOB password_utf8
= data_blob_null
;
1744 struct samr_Password _password_hash
;
1745 const struct samr_Password
*password_hash
= NULL
;
1746 const struct ldb_val
*password_val
;
1747 struct trustAuthInOutBlob password_blob
;
1748 struct samba_kdc_entry
*p
;
1749 bool use_previous
= false;
1750 uint32_t current_kvno
;
1751 uint32_t previous_kvno
;
1752 uint32_t num_keys
= 0;
1753 enum ndr_err_code ndr_err
;
1756 struct AuthenticationInformationArray
*auth_array
;
1759 uint32_t *auth_kvno
;
1760 bool prefer_current
= false;
1761 bool force_rc4
= lpcfg_kdc_force_enable_rc4_weak_session_keys(lp_ctx
);
1762 uint32_t supported_enctypes
= ENC_RC4_HMAC_MD5
;
1763 uint32_t pa_supported_enctypes
;
1764 uint32_t supported_session_etypes
;
1765 uint32_t config_kdc_enctypes
= lpcfg_kdc_supported_enctypes(lp_ctx
);
1766 uint32_t kdc_enctypes
=
1767 config_kdc_enctypes
!= 0 ?
1768 config_kdc_enctypes
:
1770 struct lsa_TrustDomainInfoInfoEx
*tdo
= NULL
;
1773 ZERO_STRUCTP(entry
);
1775 tmp_ctx
= talloc_new(mem_ctx
);
1776 if (tmp_ctx
== NULL
) {
1780 if (dsdb_functional_level(kdc_db_ctx
->samdb
) >= DS_DOMAIN_FUNCTION_2008
) {
1781 /* If not told otherwise, Windows now assumes that trusts support AES. */
1782 supported_enctypes
= ldb_msg_find_attr_as_uint(msg
,
1783 "msDS-SupportedEncryptionTypes",
1784 ENC_HMAC_SHA1_96_AES256
);
1787 pa_supported_enctypes
= supported_enctypes
;
1788 supported_session_etypes
= supported_enctypes
;
1789 if (supported_session_etypes
& ENC_HMAC_SHA1_96_AES256_SK
) {
1790 supported_session_etypes
|= ENC_HMAC_SHA1_96_AES256
;
1791 supported_session_etypes
|= ENC_HMAC_SHA1_96_AES128
;
1794 supported_session_etypes
|= ENC_RC4_HMAC_MD5
;
1797 * now that we remembered what to announce in pa_supported_enctypes
1798 * and normalized ENC_HMAC_SHA1_96_AES256_SK, we restrict the
1799 * rest to the enc types the local kdc supports.
1801 supported_enctypes
&= kdc_enctypes
;
1802 supported_session_etypes
&= kdc_enctypes
;
1804 status
= dsdb_trust_parse_tdo_info(tmp_ctx
, msg
, &tdo
);
1805 if (!NT_STATUS_IS_OK(status
)) {
1806 krb5_clear_error_message(context
);
1811 if (!(tdo
->trust_direction
& direction
)) {
1812 krb5_clear_error_message(context
);
1813 ret
= SDB_ERR_NOENTRY
;
1817 if (tdo
->trust_type
!= LSA_TRUST_TYPE_UPLEVEL
) {
1819 * Only UPLEVEL domains support kerberos here,
1820 * as we don't support LSA_TRUST_TYPE_MIT.
1822 krb5_clear_error_message(context
);
1823 ret
= SDB_ERR_NOENTRY
;
1827 if (tdo
->trust_attributes
& LSA_TRUST_ATTRIBUTE_CROSS_ORGANIZATION
) {
1829 * We don't support selective authentication yet.
1831 krb5_clear_error_message(context
);
1832 ret
= SDB_ERR_NOENTRY
;
1836 if (tdo
->domain_name
.string
== NULL
) {
1837 krb5_clear_error_message(context
);
1838 ret
= SDB_ERR_NOENTRY
;
1841 partner_realm
= strupper_talloc(tmp_ctx
, tdo
->domain_name
.string
);
1842 if (partner_realm
== NULL
) {
1843 krb5_clear_error_message(context
);
1848 if (direction
== INBOUND
) {
1850 krbtgt_realm
= partner_realm
;
1852 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthIncoming");
1853 } else { /* OUTBOUND */
1854 realm
= partner_realm
;
1855 krbtgt_realm
= our_realm
;
1857 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthOutgoing");
1860 if (password_val
== NULL
) {
1861 krb5_clear_error_message(context
);
1862 ret
= SDB_ERR_NOENTRY
;
1866 ndr_err
= ndr_pull_struct_blob(password_val
, tmp_ctx
, &password_blob
,
1867 (ndr_pull_flags_fn_t
)ndr_pull_trustAuthInOutBlob
);
1868 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1869 krb5_clear_error_message(context
);
1874 p
= talloc_zero(tmp_ctx
, struct samba_kdc_entry
);
1881 p
->kdc_db_ctx
= kdc_db_ctx
;
1882 p
->realm_dn
= realm_dn
;
1883 p
->supported_enctypes
= pa_supported_enctypes
;
1885 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
1887 entry
->skdc_entry
= p
;
1889 /* use 'whenCreated' */
1890 entry
->created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
1891 /* use 'kadmin' for now (needed by mit_samba) */
1892 ret
= smb_krb5_make_principal(context
,
1893 &entry
->created_by
.principal
,
1894 realm
, "kadmin", NULL
);
1896 krb5_clear_error_message(context
);
1901 * We always need to generate the canonicalized principal
1902 * with the values of our database.
1904 ret
= smb_krb5_make_principal(context
, &entry
->principal
, realm
,
1905 "krbtgt", krbtgt_realm
, NULL
);
1907 krb5_clear_error_message(context
);
1910 smb_krb5_principal_set_type(context
, entry
->principal
,
1913 entry
->valid_start
= NULL
;
1915 /* we need to work out if we are going to use the current or
1916 * the previous password hash.
1917 * We base this on the kvno the client passes in. If the kvno
1918 * passed in is equal to the current kvno in our database then
1919 * we use the current structure. If it is the current kvno-1,
1920 * then we use the previous substructure.
1924 * Windows prefers the previous key for one hour.
1926 tv
= timeval_current();
1927 if (tv
.tv_sec
> 3600) {
1930 an_hour_ago
= timeval_to_nttime(&tv
);
1932 /* first work out the current kvno */
1934 for (i
=0; i
< password_blob
.count
; i
++) {
1935 struct AuthenticationInformation
*a
=
1936 &password_blob
.current
.array
[i
];
1938 if (a
->LastUpdateTime
<= an_hour_ago
) {
1939 prefer_current
= true;
1942 if (a
->AuthType
== TRUST_AUTH_TYPE_VERSION
) {
1943 current_kvno
= a
->AuthInfo
.version
.version
;
1946 if (current_kvno
== 0) {
1947 previous_kvno
= 255;
1949 previous_kvno
= current_kvno
- 1;
1951 for (i
=0; i
< password_blob
.count
; i
++) {
1952 struct AuthenticationInformation
*a
=
1953 &password_blob
.previous
.array
[i
];
1955 if (a
->AuthType
== TRUST_AUTH_TYPE_VERSION
) {
1956 previous_kvno
= a
->AuthInfo
.version
.version
;
1960 /* work out whether we will use the previous or current
1962 if (password_blob
.previous
.count
== 0) {
1963 /* there is no previous password */
1964 use_previous
= false;
1965 } else if (!(flags
& SDB_F_KVNO_SPECIFIED
)) {
1967 * If not specified we use the lowest kvno
1968 * for the first hour after an update.
1970 if (prefer_current
) {
1971 use_previous
= false;
1972 } else if (previous_kvno
< current_kvno
) {
1973 use_previous
= true;
1975 use_previous
= false;
1977 } else if (kvno
== current_kvno
) {
1981 use_previous
= false;
1982 } else if (kvno
== previous_kvno
) {
1986 use_previous
= true;
1989 * Fallback to the current one for anything else
1991 use_previous
= false;
1995 auth_array
= &password_blob
.previous
;
1996 auth_kvno
= &previous_kvno
;
1998 auth_array
= &password_blob
.current
;
1999 auth_kvno
= ¤t_kvno
;
2002 /* use the kvno the client specified, if available */
2003 if (flags
& SDB_F_KVNO_SPECIFIED
) {
2006 entry
->kvno
= *auth_kvno
;
2009 for (i
=0; i
< auth_array
->count
; i
++) {
2010 if (auth_array
->array
[i
].AuthType
== TRUST_AUTH_TYPE_CLEAR
) {
2013 password_utf16
= data_blob_const(auth_array
->array
[i
].AuthInfo
.clear
.password
,
2014 auth_array
->array
[i
].AuthInfo
.clear
.size
);
2015 if (password_utf16
.length
== 0) {
2019 if (supported_enctypes
& ENC_RC4_HMAC_MD5
) {
2020 mdfour(_password_hash
.hash
, password_utf16
.data
, password_utf16
.length
);
2021 if (password_hash
== NULL
) {
2024 password_hash
= &_password_hash
;
2027 if (!(supported_enctypes
& (ENC_HMAC_SHA1_96_AES128
|ENC_HMAC_SHA1_96_AES256
))) {
2031 ok
= convert_string_talloc(tmp_ctx
,
2032 CH_UTF16MUNGED
, CH_UTF8
,
2033 password_utf16
.data
,
2034 password_utf16
.length
,
2035 &password_utf8
.data
,
2036 &password_utf8
.length
);
2038 krb5_clear_error_message(context
);
2043 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES128
) {
2046 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES256
) {
2050 } else if (auth_array
->array
[i
].AuthType
== TRUST_AUTH_TYPE_NT4OWF
) {
2051 if (supported_enctypes
& ENC_RC4_HMAC_MD5
) {
2052 password_hash
= &auth_array
->array
[i
].AuthInfo
.nt4owf
.password
;
2058 /* Must have found a cleartext or MD4 password */
2059 if (num_keys
== 0) {
2060 DBG_WARNING("no usable key found\n");
2061 krb5_clear_error_message(context
);
2062 ret
= SDB_ERR_NOENTRY
;
2066 entry
->keys
.val
= calloc(num_keys
, sizeof(struct sdb_key
));
2067 if (entry
->keys
.val
== NULL
) {
2068 krb5_clear_error_message(context
);
2073 if (password_utf8
.length
!= 0) {
2074 struct sdb_key key
= {};
2075 krb5_const_principal salt_principal
= entry
->principal
;
2077 krb5_data cleartext_data
;
2079 cleartext_data
.data
= discard_const_p(char, password_utf8
.data
);
2080 cleartext_data
.length
= password_utf8
.length
;
2082 ret
= smb_krb5_get_pw_salt(context
,
2089 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES256
) {
2090 ret
= smb_krb5_create_key_from_string(context
,
2094 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
2097 smb_krb5_free_data_contents(context
, &salt
);
2101 entry
->keys
.val
[entry
->keys
.len
] = key
;
2105 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES128
) {
2106 ret
= smb_krb5_create_key_from_string(context
,
2110 ENCTYPE_AES128_CTS_HMAC_SHA1_96
,
2113 smb_krb5_free_data_contents(context
, &salt
);
2117 entry
->keys
.val
[entry
->keys
.len
] = key
;
2121 smb_krb5_free_data_contents(context
, &salt
);
2124 if (password_hash
!= NULL
) {
2125 struct sdb_key key
= {};
2127 ret
= smb_krb5_keyblock_init_contents(context
,
2128 ENCTYPE_ARCFOUR_HMAC
,
2129 password_hash
->hash
,
2130 sizeof(password_hash
->hash
),
2136 entry
->keys
.val
[entry
->keys
.len
] = key
;
2140 entry
->flags
= int2SDBFlags(0);
2141 entry
->flags
.immutable
= 1;
2142 entry
->flags
.invalid
= 0;
2143 entry
->flags
.server
= 1;
2144 entry
->flags
.require_preauth
= 1;
2146 entry
->pw_end
= NULL
;
2148 entry
->max_life
= NULL
;
2150 entry
->max_renew
= NULL
;
2152 /* Match Windows behavior and allow forwardable flag in cross-realm. */
2153 entry
->flags
.forwardable
= 1;
2155 samba_kdc_sort_keys(&entry
->keys
);
2157 ret
= sdb_entry_set_etypes(entry
);
2164 supported_session_etypes
& KERB_ENCTYPE_AES256_CTS_HMAC_SHA1_96
;
2166 supported_session_etypes
& KERB_ENCTYPE_AES128_CTS_HMAC_SHA1_96
;
2168 supported_session_etypes
& ENC_RC4_HMAC_MD5
;
2169 ret
= sdb_entry_set_session_etypes(entry
,
2178 p
->msg
= talloc_steal(p
, msg
);
2180 talloc_steal(kdc_db_ctx
, p
);
2183 TALLOC_FREE(partner_realm
);
2186 /* This doesn't free ent itself, that is for the eventual caller to do */
2187 sdb_entry_free(entry
);
2190 talloc_free(tmp_ctx
);
2195 static krb5_error_code
samba_kdc_lookup_trust(krb5_context context
, struct ldb_context
*ldb_ctx
,
2196 TALLOC_CTX
*mem_ctx
,
2198 struct ldb_dn
*realm_dn
,
2199 struct ldb_message
**pmsg
)
2202 const char * const *attrs
= trust_attrs
;
2204 status
= dsdb_trust_search_tdo(ldb_ctx
, realm
, realm
,
2205 attrs
, mem_ctx
, pmsg
);
2206 if (NT_STATUS_IS_OK(status
)) {
2208 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
2209 return SDB_ERR_NOENTRY
;
2210 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MEMORY
)) {
2212 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_trust: out of memory");
2216 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_trust: %s", nt_errstr(status
));
2221 static krb5_error_code
samba_kdc_lookup_client(krb5_context context
,
2222 struct samba_kdc_db_context
*kdc_db_ctx
,
2223 TALLOC_CTX
*mem_ctx
,
2224 krb5_const_principal principal
,
2226 struct ldb_dn
**realm_dn
,
2227 struct ldb_message
**msg
)
2230 char *principal_string
= NULL
;
2232 if (smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
2233 principal_string
= smb_krb5_principal_get_comp_string(mem_ctx
, context
,
2235 if (principal_string
== NULL
) {
2239 char *principal_string_m
= NULL
;
2240 krb5_error_code ret
;
2242 ret
= krb5_unparse_name(context
, principal
, &principal_string_m
);
2247 principal_string
= talloc_strdup(mem_ctx
, principal_string_m
);
2248 SAFE_FREE(principal_string_m
);
2249 if (principal_string
== NULL
) {
2254 nt_status
= sam_get_results_principal(kdc_db_ctx
->samdb
,
2255 mem_ctx
, principal_string
, attrs
,
2257 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
2258 krb5_principal fallback_principal
= NULL
;
2259 unsigned int num_comp
;
2260 char *fallback_realm
= NULL
;
2261 char *fallback_account
= NULL
;
2262 krb5_error_code ret
;
2264 ret
= krb5_parse_name(context
, principal_string
,
2265 &fallback_principal
);
2266 TALLOC_FREE(principal_string
);
2271 num_comp
= krb5_princ_size(context
, fallback_principal
);
2272 fallback_realm
= smb_krb5_principal_get_realm(
2273 mem_ctx
, context
, fallback_principal
);
2274 if (fallback_realm
== NULL
) {
2275 krb5_free_principal(context
, fallback_principal
);
2279 if (num_comp
== 1) {
2282 fallback_account
= smb_krb5_principal_get_comp_string(mem_ctx
,
2283 context
, fallback_principal
, 0);
2284 if (fallback_account
== NULL
) {
2285 krb5_free_principal(context
, fallback_principal
);
2286 TALLOC_FREE(fallback_realm
);
2290 len
= strlen(fallback_account
);
2291 if (len
>= 2 && fallback_account
[len
- 1] == '$') {
2292 TALLOC_FREE(fallback_account
);
2295 krb5_free_principal(context
, fallback_principal
);
2296 fallback_principal
= NULL
;
2298 if (fallback_account
!= NULL
) {
2301 with_dollar
= talloc_asprintf(mem_ctx
, "%s$",
2303 if (with_dollar
== NULL
) {
2304 TALLOC_FREE(fallback_realm
);
2307 TALLOC_FREE(fallback_account
);
2309 ret
= smb_krb5_make_principal(context
,
2310 &fallback_principal
,
2313 TALLOC_FREE(with_dollar
);
2315 TALLOC_FREE(fallback_realm
);
2319 TALLOC_FREE(fallback_realm
);
2321 if (fallback_principal
!= NULL
) {
2322 char *fallback_string
= NULL
;
2324 ret
= krb5_unparse_name(context
,
2328 krb5_free_principal(context
, fallback_principal
);
2332 nt_status
= sam_get_results_principal(kdc_db_ctx
->samdb
,
2337 SAFE_FREE(fallback_string
);
2339 krb5_free_principal(context
, fallback_principal
);
2340 fallback_principal
= NULL
;
2342 TALLOC_FREE(principal_string
);
2344 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
2345 return SDB_ERR_NOENTRY
;
2346 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_MEMORY
)) {
2348 } else if (!NT_STATUS_IS_OK(nt_status
)) {
2355 static krb5_error_code
samba_kdc_fetch_client(krb5_context context
,
2356 struct samba_kdc_db_context
*kdc_db_ctx
,
2357 TALLOC_CTX
*mem_ctx
,
2358 krb5_const_principal principal
,
2361 struct sdb_entry
*entry
)
2363 struct ldb_dn
*realm_dn
;
2364 krb5_error_code ret
;
2365 struct ldb_message
*msg
= NULL
;
2367 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
2368 mem_ctx
, principal
, user_attrs
,
2374 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
2375 principal
, SAMBA_KDC_ENT_TYPE_CLIENT
,
2377 realm_dn
, msg
, entry
);
2381 static krb5_error_code
samba_kdc_fetch_krbtgt(krb5_context context
,
2382 struct samba_kdc_db_context
*kdc_db_ctx
,
2383 TALLOC_CTX
*mem_ctx
,
2384 krb5_const_principal principal
,
2387 struct sdb_entry
*entry
)
2389 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
2390 krb5_error_code ret
;
2391 struct ldb_message
*msg
= NULL
;
2392 struct ldb_dn
*realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
2393 char *realm_from_princ
;
2394 char *realm_princ_comp
= smb_krb5_principal_get_comp_string(mem_ctx
, context
, principal
, 1);
2396 realm_from_princ
= smb_krb5_principal_get_realm(
2397 mem_ctx
, context
, principal
);
2398 if (realm_from_princ
== NULL
) {
2400 return SDB_ERR_NOENTRY
;
2403 if (krb5_princ_size(context
, principal
) != 2
2404 || (principal_comp_strcmp(context
, principal
, 0, KRB5_TGS_NAME
) != 0)) {
2406 return SDB_ERR_NOENTRY
;
2409 /* krbtgt case. Either us or a trusted realm */
2411 if (lpcfg_is_my_domain_or_realm(lp_ctx
, realm_from_princ
)
2412 && lpcfg_is_my_domain_or_realm(lp_ctx
, realm_princ_comp
)) {
2413 /* us, or someone quite like us */
2414 /* Kludge, kludge, kludge. If the realm part of krbtgt/realm,
2415 * is in our db, then direct the caller at our primary
2419 unsigned int krbtgt_number
;
2420 /* w2k8r2 sometimes gives us a kvno of 255 for inter-domain
2421 trust tickets. We don't yet know what this means, but we do
2422 seem to need to treat it as unspecified */
2423 if (flags
& SDB_F_KVNO_SPECIFIED
) {
2424 krbtgt_number
= SAMBA_KVNO_GET_KRBTGT(kvno
);
2425 if (kdc_db_ctx
->rodc
) {
2426 if (krbtgt_number
!= kdc_db_ctx
->my_krbtgt_number
) {
2427 return SDB_ERR_NOT_FOUND_HERE
;
2431 krbtgt_number
= kdc_db_ctx
->my_krbtgt_number
;
2434 if (krbtgt_number
== kdc_db_ctx
->my_krbtgt_number
) {
2435 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
,
2436 &msg
, kdc_db_ctx
->krbtgt_dn
, LDB_SCOPE_BASE
,
2437 krbtgt_attrs
, DSDB_SEARCH_NO_GLOBAL_CATALOG
,
2438 "(objectClass=user)");
2440 /* We need to look up an RODC krbtgt (perhaps
2441 * ours, if we are an RODC, perhaps another
2442 * RODC if we are a read-write DC */
2443 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
,
2444 &msg
, realm_dn
, LDB_SCOPE_SUBTREE
,
2446 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
2447 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=%u))", (unsigned)(krbtgt_number
));
2450 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
2451 krb5_warnx(context
, "samba_kdc_fetch_krbtgt: could not find KRBTGT number %u in DB!",
2452 (unsigned)(krbtgt_number
));
2453 krb5_set_error_message(context
, SDB_ERR_NOENTRY
,
2454 "samba_kdc_fetch_krbtgt: could not find KRBTGT number %u in DB!",
2455 (unsigned)(krbtgt_number
));
2456 return SDB_ERR_NOENTRY
;
2457 } else if (lret
!= LDB_SUCCESS
) {
2458 krb5_warnx(context
, "samba_kdc_fetch_krbtgt: could not find KRBTGT number %u in DB!",
2459 (unsigned)(krbtgt_number
));
2460 krb5_set_error_message(context
, SDB_ERR_NOENTRY
,
2461 "samba_kdc_fetch_krbtgt: could not find KRBTGT number %u in DB!",
2462 (unsigned)(krbtgt_number
));
2463 return SDB_ERR_NOENTRY
;
2466 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
2467 principal
, SAMBA_KDC_ENT_TYPE_KRBTGT
,
2468 flags
, kvno
, realm_dn
, msg
, entry
);
2470 krb5_warnx(context
, "samba_kdc_fetch_krbtgt: self krbtgt message2entry failed");
2475 enum trust_direction direction
= UNKNOWN
;
2476 const char *realm
= NULL
;
2478 /* Either an inbound or outbound trust */
2480 if (strcasecmp(lpcfg_realm(lp_ctx
), realm_from_princ
) == 0) {
2481 /* look for inbound trust */
2482 direction
= INBOUND
;
2483 realm
= realm_princ_comp
;
2484 } else if (principal_comp_strcasecmp(context
, principal
, 1, lpcfg_realm(lp_ctx
)) == 0) {
2485 /* look for outbound trust */
2486 direction
= OUTBOUND
;
2487 realm
= realm_from_princ
;
2489 krb5_warnx(context
, "samba_kdc_fetch_krbtgt: not our realm for trusts ('%s', '%s')",
2492 krb5_set_error_message(context
, SDB_ERR_NOENTRY
, "samba_kdc_fetch_krbtgt: not our realm for trusts ('%s', '%s')",
2495 return SDB_ERR_NOENTRY
;
2498 /* Trusted domains are under CN=system */
2500 ret
= samba_kdc_lookup_trust(context
, kdc_db_ctx
->samdb
,
2502 realm
, realm_dn
, &msg
);
2505 krb5_warnx(context
, "samba_kdc_fetch_krbtgt: could not find principal in DB");
2506 krb5_set_error_message(context
, ret
, "samba_kdc_fetch_krbtgt: could not find principal in DB");
2510 ret
= samba_kdc_trust_message2entry(context
, kdc_db_ctx
, mem_ctx
,
2512 realm_dn
, flags
, kvno
, msg
, entry
);
2514 krb5_warnx(context
, "samba_kdc_fetch_krbtgt: trust_message2entry failed for %s",
2515 ldb_dn_get_linearized(msg
->dn
));
2516 krb5_set_error_message(context
, ret
, "samba_kdc_fetch_krbtgt: "
2517 "trust_message2entry failed for %s",
2518 ldb_dn_get_linearized(msg
->dn
));
2525 static krb5_error_code
samba_kdc_lookup_server(krb5_context context
,
2526 struct samba_kdc_db_context
*kdc_db_ctx
,
2527 TALLOC_CTX
*mem_ctx
,
2528 krb5_const_principal principal
,
2531 struct ldb_dn
**realm_dn
,
2532 struct ldb_message
**msg
)
2534 krb5_error_code ret
;
2535 if ((smb_krb5_principal_get_type(context
, principal
) != KRB5_NT_ENTERPRISE_PRINCIPAL
)
2536 && krb5_princ_size(context
, principal
) >= 2) {
2537 /* 'normal server' case */
2540 struct ldb_dn
*user_dn
;
2541 char *principal_string
;
2543 ret
= krb5_unparse_name_flags(context
, principal
,
2544 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
2550 /* At this point we may find the host is known to be
2551 * in a different realm, so we should generate a
2552 * referral instead */
2553 nt_status
= crack_service_principal_name(kdc_db_ctx
->samdb
,
2554 mem_ctx
, principal_string
,
2555 &user_dn
, realm_dn
);
2556 free(principal_string
);
2558 if (!NT_STATUS_IS_OK(nt_status
)) {
2559 return SDB_ERR_NOENTRY
;
2562 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
,
2564 msg
, user_dn
, LDB_SCOPE_BASE
,
2566 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
2568 if (ldb_ret
!= LDB_SUCCESS
) {
2569 return SDB_ERR_NOENTRY
;
2572 } else if (!(flags
& SDB_F_FOR_AS_REQ
)
2573 && smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
2575 * The behaviour of accepting an
2576 * KRB5_NT_ENTERPRISE_PRINCIPAL server principal
2577 * containing a UPN only applies to TGS-REQ packets,
2578 * not AS-REQ packets.
2580 return samba_kdc_lookup_client(context
, kdc_db_ctx
,
2581 mem_ctx
, principal
, attrs
,
2586 * - the AS-REQ, where we only accept
2587 * samAccountName based lookups for the server, no
2588 * matter if the name is an
2589 * KRB5_NT_ENTERPRISE_PRINCIPAL or not
2590 * - for the TGS-REQ when we are not given an
2591 * KRB5_NT_ENTERPRISE_PRINCIPAL, which also must
2592 * only lookup samAccountName based names.
2596 krb5_principal enterprise_principal
= NULL
;
2597 krb5_const_principal used_principal
= NULL
;
2600 char *filter
= NULL
;
2602 if (smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
2604 /* Need to reparse the enterprise principal to find the real target */
2605 if (krb5_princ_size(context
, principal
) != 1) {
2606 ret
= KRB5_PARSE_MALFORMED
;
2607 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_server: request for an "
2608 "enterprise principal with wrong (%d) number of components",
2609 krb5_princ_size(context
, principal
));
2612 str
= smb_krb5_principal_get_comp_string(mem_ctx
, context
, principal
, 0);
2614 return KRB5_PARSE_MALFORMED
;
2616 ret
= krb5_parse_name(context
, str
,
2617 &enterprise_principal
);
2622 used_principal
= enterprise_principal
;
2624 used_principal
= principal
;
2627 /* server as client principal case, but we must not lookup userPrincipalNames */
2628 *realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
2630 /* TODO: Check if it is our realm, otherwise give referral */
2632 ret
= krb5_unparse_name_flags(context
, used_principal
,
2633 KRB5_PRINCIPAL_UNPARSE_NO_REALM
|
2634 KRB5_PRINCIPAL_UNPARSE_DISPLAY
,
2636 used_principal
= NULL
;
2637 krb5_free_principal(context
, enterprise_principal
);
2638 enterprise_principal
= NULL
;
2641 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_server: could not parse principal");
2642 krb5_warnx(context
, "samba_kdc_lookup_server: could not parse principal");
2646 name1
= ldb_binary_encode_string(mem_ctx
, short_princ
);
2647 SAFE_FREE(short_princ
);
2648 if (name1
== NULL
) {
2651 len1
= strlen(name1
);
2652 if (len1
>= 1 && name1
[len1
- 1] != '$') {
2653 filter
= talloc_asprintf(mem_ctx
,
2654 "(&(objectClass=user)(|(samAccountName=%s)(samAccountName=%s$)))",
2656 if (filter
== NULL
) {
2660 filter
= talloc_asprintf(mem_ctx
,
2661 "(&(objectClass=user)(samAccountName=%s))",
2663 if (filter
== NULL
) {
2668 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
, msg
,
2669 *realm_dn
, LDB_SCOPE_SUBTREE
,
2671 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
2673 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
2674 DBG_DEBUG("Failed to find an entry for %s filter:%s\n",
2676 return SDB_ERR_NOENTRY
;
2678 if (lret
== LDB_ERR_CONSTRAINT_VIOLATION
) {
2679 DBG_DEBUG("Failed to find unique entry for %s filter:%s\n",
2681 return SDB_ERR_NOENTRY
;
2683 if (lret
!= LDB_SUCCESS
) {
2684 DBG_ERR("Failed single search for %s - %s\n",
2685 name1
, ldb_errstring(kdc_db_ctx
->samdb
));
2686 return SDB_ERR_NOENTRY
;
2690 return SDB_ERR_NOENTRY
;
2695 static krb5_error_code
samba_kdc_fetch_server(krb5_context context
,
2696 struct samba_kdc_db_context
*kdc_db_ctx
,
2697 TALLOC_CTX
*mem_ctx
,
2698 krb5_const_principal principal
,
2701 struct sdb_entry
*entry
)
2703 krb5_error_code ret
;
2704 struct ldb_dn
*realm_dn
;
2705 struct ldb_message
*msg
;
2707 ret
= samba_kdc_lookup_server(context
, kdc_db_ctx
, mem_ctx
, principal
,
2708 flags
, server_attrs
, &realm_dn
, &msg
);
2713 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
2714 principal
, SAMBA_KDC_ENT_TYPE_SERVER
,
2716 realm_dn
, msg
, entry
);
2718 char *client_name
= NULL
;
2719 krb5_error_code code
;
2721 code
= krb5_unparse_name(context
, principal
, &client_name
);
2724 "samba_kdc_fetch_server: message2entry failed for "
2729 "samba_kdc_fetch_server: message2entry and "
2730 "krb5_unparse_name failed");
2732 SAFE_FREE(client_name
);
2738 static krb5_error_code
samba_kdc_lookup_realm(krb5_context context
,
2739 struct samba_kdc_db_context
*kdc_db_ctx
,
2740 TALLOC_CTX
*mem_ctx
,
2741 krb5_const_principal principal
,
2743 struct sdb_entry
*entry
)
2745 TALLOC_CTX
*frame
= talloc_stackframe();
2747 krb5_error_code ret
;
2748 bool check_realm
= false;
2749 const char *realm
= NULL
;
2750 struct dsdb_trust_routing_table
*trt
= NULL
;
2751 const struct lsa_TrustDomainInfoInfoEx
*tdo
= NULL
;
2752 unsigned int num_comp
;
2756 num_comp
= krb5_princ_size(context
, principal
);
2758 if (flags
& SDB_F_GET_CLIENT
) {
2759 if (flags
& SDB_F_FOR_AS_REQ
) {
2763 if (flags
& SDB_F_GET_SERVER
) {
2764 if (flags
& SDB_F_FOR_TGS_REQ
) {
2774 realm
= smb_krb5_principal_get_realm(frame
, context
, principal
);
2775 if (realm
== NULL
) {
2781 * The requested realm needs to be our own
2783 ok
= lpcfg_is_my_domain_or_realm(kdc_db_ctx
->lp_ctx
, realm
);
2786 * The request is not for us...
2789 return SDB_ERR_NOENTRY
;
2792 if (smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
2793 char *principal_string
= NULL
;
2794 krb5_principal enterprise_principal
= NULL
;
2795 char *enterprise_realm
= NULL
;
2797 if (num_comp
!= 1) {
2799 return SDB_ERR_NOENTRY
;
2802 principal_string
= smb_krb5_principal_get_comp_string(frame
, context
,
2804 if (principal_string
== NULL
) {
2809 ret
= krb5_parse_name(context
, principal_string
,
2810 &enterprise_principal
);
2811 TALLOC_FREE(principal_string
);
2817 enterprise_realm
= smb_krb5_principal_get_realm(
2818 frame
, context
, enterprise_principal
);
2819 krb5_free_principal(context
, enterprise_principal
);
2820 if (enterprise_realm
!= NULL
) {
2821 realm
= enterprise_realm
;
2825 if (flags
& SDB_F_GET_SERVER
) {
2826 char *service_realm
= NULL
;
2828 ret
= principal_comp_strcmp(context
, principal
, 0, KRB5_TGS_NAME
);
2831 * we need to search krbtgt/ locally
2838 * We need to check the last component against the routing table.
2840 * Note this works only with 2 or 3 component principals, e.g:
2842 * servicePrincipalName: ldap/W2K8R2-219.bla.base
2843 * servicePrincipalName: ldap/W2K8R2-219.bla.base/bla.base
2844 * servicePrincipalName: ldap/W2K8R2-219.bla.base/ForestDnsZones.bla.base
2845 * servicePrincipalName: ldap/W2K8R2-219.bla.base/DomainDnsZones.bla.base
2848 if (num_comp
== 2 || num_comp
== 3) {
2849 service_realm
= smb_krb5_principal_get_comp_string(frame
,
2855 if (service_realm
!= NULL
) {
2856 realm
= service_realm
;
2860 ok
= lpcfg_is_my_domain_or_realm(kdc_db_ctx
->lp_ctx
, realm
);
2863 * skip the expensive routing lookup
2869 status
= dsdb_trust_routing_table_load(kdc_db_ctx
->samdb
,
2871 if (!NT_STATUS_IS_OK(status
)) {
2876 tdo
= dsdb_trust_routing_by_name(trt
, realm
);
2879 * This principal has to be local
2885 if (tdo
->trust_attributes
& LSA_TRUST_ATTRIBUTE_WITHIN_FOREST
) {
2887 * TODO: handle the routing within the forest
2889 * This should likely be handled in
2890 * samba_kdc_message2entry() in case we're
2891 * a global catalog. We'd need to check
2892 * if realm_dn is our own domain and derive
2893 * the dns domain name from realm_dn and check that
2894 * against the routing table or fallback to
2895 * the tdo we found here.
2897 * But for now we don't support multiple domains
2898 * in our forest correctly anyway.
2900 * Just search in our local database.
2906 ZERO_STRUCTP(entry
);
2908 ret
= krb5_copy_principal(context
, principal
,
2915 upper
= strupper_talloc(frame
, tdo
->domain_name
.string
);
2916 if (upper
== NULL
) {
2921 ret
= smb_krb5_principal_set_realm(context
,
2930 return SDB_ERR_WRONG_REALM
;
2933 krb5_error_code
samba_kdc_fetch(krb5_context context
,
2934 struct samba_kdc_db_context
*kdc_db_ctx
,
2935 krb5_const_principal principal
,
2938 struct sdb_entry
*entry
)
2940 krb5_error_code ret
= SDB_ERR_NOENTRY
;
2941 TALLOC_CTX
*mem_ctx
;
2943 mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_fetch context");
2946 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
2950 ret
= samba_kdc_lookup_realm(context
, kdc_db_ctx
, mem_ctx
,
2951 principal
, flags
, entry
);
2956 ret
= SDB_ERR_NOENTRY
;
2958 if (flags
& SDB_F_GET_CLIENT
) {
2959 ret
= samba_kdc_fetch_client(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, kvno
, entry
);
2960 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2962 if (flags
& SDB_F_GET_SERVER
) {
2963 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
2964 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, kvno
, entry
);
2965 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2967 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
2968 ret
= samba_kdc_fetch_server(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, kvno
, entry
);
2969 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2971 if (flags
& SDB_F_GET_KRBTGT
) {
2972 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, kvno
, entry
);
2973 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2977 talloc_free(mem_ctx
);
2981 struct samba_kdc_seq
{
2984 struct ldb_message
**msgs
;
2985 struct ldb_dn
*realm_dn
;
2988 static krb5_error_code
samba_kdc_seq(krb5_context context
,
2989 struct samba_kdc_db_context
*kdc_db_ctx
,
2990 struct sdb_entry
*entry
)
2992 krb5_error_code ret
;
2993 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
2994 const char *realm
= lpcfg_realm(kdc_db_ctx
->lp_ctx
);
2995 struct ldb_message
*msg
= NULL
;
2996 const char *sAMAccountName
= NULL
;
2997 krb5_principal principal
= NULL
;
2998 TALLOC_CTX
*mem_ctx
;
3001 return SDB_ERR_NOENTRY
;
3004 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_seq context");
3008 krb5_set_error_message(context
, ret
, "samba_kdc_seq: talloc_named() failed!");
3012 while (priv
->index
< priv
->count
) {
3013 msg
= priv
->msgs
[priv
->index
++];
3015 sAMAccountName
= ldb_msg_find_attr_as_string(msg
, "sAMAccountName", NULL
);
3016 if (sAMAccountName
!= NULL
) {
3021 if (sAMAccountName
== NULL
) {
3022 ret
= SDB_ERR_NOENTRY
;
3026 ret
= smb_krb5_make_principal(context
, &principal
,
3027 realm
, sAMAccountName
, NULL
);
3032 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
3033 principal
, SAMBA_KDC_ENT_TYPE_ANY
,
3034 SDB_F_ADMIN_DATA
|SDB_F_GET_ANY
,
3036 priv
->realm_dn
, msg
, entry
);
3037 krb5_free_principal(context
, principal
);
3042 kdc_db_ctx
->seq_ctx
= NULL
;
3044 talloc_free(mem_ctx
);
3050 krb5_error_code
samba_kdc_firstkey(krb5_context context
,
3051 struct samba_kdc_db_context
*kdc_db_ctx
,
3052 struct sdb_entry
*entry
)
3054 struct ldb_context
*ldb_ctx
= kdc_db_ctx
->samdb
;
3055 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
3057 struct ldb_result
*res
= NULL
;
3058 krb5_error_code ret
;
3063 kdc_db_ctx
->seq_ctx
= NULL
;
3066 priv
= (struct samba_kdc_seq
*) talloc(kdc_db_ctx
, struct samba_kdc_seq
);
3069 krb5_set_error_message(context
, ret
, "talloc: out of memory");
3075 priv
->realm_dn
= ldb_get_default_basedn(ldb_ctx
);
3078 ret
= krb5_get_default_realm(context
, &realm
);
3083 krb5_free_default_realm(context
, realm
);
3085 lret
= dsdb_search(ldb_ctx
, priv
, &res
,
3086 priv
->realm_dn
, LDB_SCOPE_SUBTREE
, user_attrs
,
3087 DSDB_SEARCH_NO_GLOBAL_CATALOG
,
3088 "(objectClass=user)");
3090 if (lret
!= LDB_SUCCESS
) {
3092 return SDB_ERR_NOENTRY
;
3095 priv
->count
= res
->count
;
3096 priv
->msgs
= talloc_steal(priv
, res
->msgs
);
3099 kdc_db_ctx
->seq_ctx
= priv
;
3101 ret
= samba_kdc_seq(context
, kdc_db_ctx
, entry
);
3105 kdc_db_ctx
->seq_ctx
= NULL
;
3110 krb5_error_code
samba_kdc_nextkey(krb5_context context
,
3111 struct samba_kdc_db_context
*kdc_db_ctx
,
3112 struct sdb_entry
*entry
)
3114 return samba_kdc_seq(context
, kdc_db_ctx
, entry
);
3117 /* Check if a given entry may delegate or do s4u2self to this target principal
3119 * The safest way to determine 'self' is to check the DB record made at
3120 * the time the principal was presented to the KDC.
3123 samba_kdc_check_client_matches_target_service(krb5_context context
,
3124 struct samba_kdc_entry
*skdc_entry_client
,
3125 struct samba_kdc_entry
*skdc_entry_server_target
)
3127 struct dom_sid
*orig_sid
;
3128 struct dom_sid
*target_sid
;
3129 TALLOC_CTX
*frame
= talloc_stackframe();
3131 orig_sid
= samdb_result_dom_sid(frame
,
3132 skdc_entry_client
->msg
,
3134 target_sid
= samdb_result_dom_sid(frame
,
3135 skdc_entry_server_target
->msg
,
3139 * Allow delegation to the same record (representing a
3140 * principal), even if by a different name. The easy and safe
3141 * way to prove this is by SID comparison
3143 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
3145 return KRB5KRB_AP_ERR_BADMATCH
;
3152 /* Certificates printed by the Certificate Authority might have a
3153 * slightly different form of the user principal name to that in the
3154 * database. Allow a mismatch where they both refer to the same
3158 samba_kdc_check_pkinit_ms_upn_match(krb5_context context
,
3159 struct samba_kdc_db_context
*kdc_db_ctx
,
3160 struct samba_kdc_entry
*skdc_entry
,
3161 krb5_const_principal certificate_principal
)
3163 krb5_error_code ret
;
3164 struct ldb_dn
*realm_dn
;
3165 struct ldb_message
*msg
;
3166 struct dom_sid
*orig_sid
;
3167 struct dom_sid
*target_sid
;
3168 const char *ms_upn_check_attrs
[] = {
3172 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_pkinit_ms_upn_match");
3176 krb5_set_error_message(context
, ret
, "samba_kdc_check_pkinit_ms_upn_match: talloc_named() failed!");
3180 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
3181 mem_ctx
, certificate_principal
,
3182 ms_upn_check_attrs
, &realm_dn
, &msg
);
3185 talloc_free(mem_ctx
);
3189 orig_sid
= samdb_result_dom_sid(mem_ctx
, skdc_entry
->msg
, "objectSid");
3190 target_sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
3192 /* Consider these to be the same principal, even if by a different
3193 * name. The easy and safe way to prove this is by SID
3195 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
3196 talloc_free(mem_ctx
);
3197 #if defined(KRB5KDC_ERR_CLIENT_NAME_MISMATCH) /* MIT */
3198 return KRB5KDC_ERR_CLIENT_NAME_MISMATCH
;
3199 #else /* Heimdal (where this is an enum) */
3200 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
3204 talloc_free(mem_ctx
);
3209 * Check if a given entry may delegate to this target principal
3213 samba_kdc_check_s4u2proxy(krb5_context context
,
3214 struct samba_kdc_db_context
*kdc_db_ctx
,
3215 struct samba_kdc_entry
*skdc_entry
,
3216 krb5_const_principal target_principal
)
3218 krb5_error_code ret
;
3220 const char *client_dn
= NULL
;
3221 const char *target_principal_name
= NULL
;
3222 struct ldb_message_element
*el
;
3227 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_s4u2proxy");
3231 krb5_set_error_message(context
, ret
,
3232 "samba_kdc_check_s4u2proxy:"
3233 " talloc_named() failed!");
3237 client_dn
= ldb_dn_get_linearized(skdc_entry
->msg
->dn
);
3243 krb5_set_error_message(context
, ret
,
3244 "samba_kdc_check_s4u2proxy:"
3245 " ldb_dn_get_linearized() failed!");
3246 talloc_free(mem_ctx
);
3250 el
= ldb_msg_find_element(skdc_entry
->msg
, "msDS-AllowedToDelegateTo");
3255 SMB_ASSERT(el
->num_values
!= 0);
3258 * This is the Microsoft forwardable flag behavior.
3260 * If the proxy (target) principal is NULL, and we have any authorized
3261 * delegation target, allow to forward.
3263 if (target_principal
== NULL
) {
3264 talloc_free(mem_ctx
);
3270 * The main heimdal code already checked that the target_principal
3271 * belongs to the same realm as the client.
3273 * So we just need the principal without the realm,
3274 * as that is what is configured in the "msDS-AllowedToDelegateTo"
3277 ret
= krb5_unparse_name_flags(context
, target_principal
,
3278 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &tmp
);
3280 talloc_free(mem_ctx
);
3281 krb5_set_error_message(context
, ret
,
3282 "samba_kdc_check_s4u2proxy:"
3283 " krb5_unparse_name() failed!");
3286 DBG_DEBUG("client[%s] for target[%s]\n",
3289 target_principal_name
= talloc_strdup(mem_ctx
, tmp
);
3291 if (target_principal_name
== NULL
) {
3293 krb5_set_error_message(context
, ret
,
3294 "samba_kdc_check_s4u2proxy:"
3295 " talloc_strdup() failed!");
3296 talloc_free(mem_ctx
);
3300 val
= data_blob_string_const(target_principal_name
);
3302 for (i
=0; i
<el
->num_values
; i
++) {
3303 struct ldb_val
*val1
= &val
;
3304 struct ldb_val
*val2
= &el
->values
[i
];
3307 if (val1
->length
!= val2
->length
) {
3311 cmp
= strncasecmp((const char *)val1
->data
,
3312 (const char *)val2
->data
,
3327 DBG_DEBUG("client[%s] allowed target[%s]\n",
3328 client_dn
, target_principal_name
);
3329 talloc_free(mem_ctx
);
3333 krb5_set_error_message(context
, ret
,
3334 "samba_kdc_check_s4u2proxy: client[%s] "
3335 "not allowed for delegation to target[%s]",
3337 target_principal_name
);
3338 talloc_free(mem_ctx
);
3339 return KRB5KDC_ERR_BADOPTION
;
3343 * This method is called for S4U2Proxy requests and implements the
3344 * resource-based constrained delegation variant, which can support
3345 * cross-realm delegation.
3347 krb5_error_code
samba_kdc_check_s4u2proxy_rbcd(
3348 krb5_context context
,
3349 struct samba_kdc_db_context
*kdc_db_ctx
,
3350 krb5_const_principal client_principal
,
3351 krb5_const_principal server_principal
,
3352 krb5_const_pac header_pac
,
3353 struct samba_kdc_entry
*proxy_skdc_entry
)
3355 krb5_error_code code
;
3356 enum ndr_err_code ndr_err
;
3357 char *client_name
= NULL
;
3358 char *server_name
= NULL
;
3359 const char *proxy_dn
= NULL
;
3360 const DATA_BLOB
*data
= NULL
;
3361 struct security_descriptor
*rbcd_security_descriptor
= NULL
;
3362 struct auth_user_info_dc
*user_info_dc
= NULL
;
3363 struct security_token
*security_token
= NULL
;
3364 uint32_t session_info_flags
=
3365 AUTH_SESSION_INFO_DEFAULT_GROUPS
|
3366 AUTH_SESSION_INFO_SIMPLE_PRIVILEGES
;
3368 * Testing shows that although Windows grants SEC_ADS_GENERIC_ALL access
3369 * in security descriptors it creates for RBCD, its KDC only requires
3370 * SEC_ADS_CONTROL_ACCESS for the access check to succeed.
3372 uint32_t access_desired
= SEC_ADS_CONTROL_ACCESS
;
3373 uint32_t access_granted
= 0;
3375 TALLOC_CTX
*mem_ctx
= NULL
;
3377 mem_ctx
= talloc_named(kdc_db_ctx
,
3379 "samba_kdc_check_s4u2proxy_rbcd");
3380 if (mem_ctx
== NULL
) {
3387 proxy_dn
= ldb_dn_get_linearized(proxy_skdc_entry
->msg
->dn
);
3388 if (proxy_dn
== NULL
) {
3389 DBG_ERR("ldb_dn_get_linearized failed for proxy_dn!\n");
3398 rbcd_security_descriptor
= talloc_zero(mem_ctx
,
3399 struct security_descriptor
);
3400 if (rbcd_security_descriptor
== NULL
) {
3407 code
= krb5_unparse_name_flags(context
,
3409 KRB5_PRINCIPAL_UNPARSE_DISPLAY
,
3412 DBG_ERR("Unable to parse client_principal!\n");
3416 code
= krb5_unparse_name_flags(context
,
3418 KRB5_PRINCIPAL_UNPARSE_DISPLAY
,
3421 DBG_ERR("Unable to parse server_principal!\n");
3425 DBG_INFO("Check delegation from client[%s] to server[%s] via "
3431 code
= kerberos_pac_to_user_info_dc(mem_ctx
,
3435 AUTH_INCLUDE_RESOURCE_GROUPS
,
3443 if (!(user_info_dc
->info
->user_flags
& NETLOGON_GUEST
)) {
3444 session_info_flags
|= AUTH_SESSION_INFO_AUTHENTICATED
;
3447 nt_status
= auth_generate_security_token(mem_ctx
,
3453 if (!NT_STATUS_IS_OK(nt_status
)) {
3454 code
= map_errno_from_nt_status(nt_status
);
3458 data
= ldb_msg_find_ldb_val(proxy_skdc_entry
->msg
,
3459 "msDS-AllowedToActOnBehalfOfOtherIdentity");
3461 DBG_WARNING("Could not find security descriptor "
3462 "msDS-AllowedToActOnBehalfOfOtherIdentity in "
3465 code
= KRB5KDC_ERR_BADOPTION
;
3469 ndr_err
= ndr_pull_struct_blob(
3472 rbcd_security_descriptor
,
3473 (ndr_pull_flags_fn_t
)ndr_pull_security_descriptor
);
3474 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
3475 errno
= ndr_map_error2errno(ndr_err
);
3476 DBG_ERR("Failed to unmarshall "
3477 "msDS-AllowedToActOnBehalfOfOtherIdentity "
3478 "security descriptor of proxy[%s]\n",
3480 code
= KRB5KDC_ERR_BADOPTION
;
3484 if (DEBUGLEVEL
>= 10) {
3485 NDR_PRINT_DEBUG(security_token
, security_token
);
3486 NDR_PRINT_DEBUG(security_descriptor
, rbcd_security_descriptor
);
3489 nt_status
= sec_access_check_ds(rbcd_security_descriptor
,
3496 if (!NT_STATUS_IS_OK(nt_status
)) {
3497 DBG_WARNING("RBCD: sec_access_check_ds(access_desired=%#08x, "
3498 "access_granted:%#08x) failed with: %s\n",
3501 nt_errstr(nt_status
));
3503 code
= KRB5KDC_ERR_BADOPTION
;
3507 DBG_NOTICE("RBCD: Access granted for client[%s]\n", client_name
);
3511 SAFE_FREE(client_name
);
3512 SAFE_FREE(server_name
);
3514 TALLOC_FREE(mem_ctx
);
3518 NTSTATUS
samba_kdc_setup_db_ctx(TALLOC_CTX
*mem_ctx
, struct samba_kdc_base_context
*base_ctx
,
3519 struct samba_kdc_db_context
**kdc_db_ctx_out
)
3522 struct ldb_message
*msg
;
3523 struct auth_session_info
*session_info
;
3524 struct samba_kdc_db_context
*kdc_db_ctx
;
3525 /* The idea here is very simple. Using Kerberos to
3526 * authenticate the KDC to the LDAP server is highly likely to
3529 * In future we may set this up to use EXTERNAL and SSL
3530 * certificates, for now it will almost certainly be NTLMSSP_SET_USERNAME
3533 kdc_db_ctx
= talloc_zero(mem_ctx
, struct samba_kdc_db_context
);
3534 if (kdc_db_ctx
== NULL
) {
3535 return NT_STATUS_NO_MEMORY
;
3537 kdc_db_ctx
->ev_ctx
= base_ctx
->ev_ctx
;
3538 kdc_db_ctx
->lp_ctx
= base_ctx
->lp_ctx
;
3539 kdc_db_ctx
->msg_ctx
= base_ctx
->msg_ctx
;
3541 /* get default kdc policy */
3542 lpcfg_default_kdc_policy(mem_ctx
,
3544 &kdc_db_ctx
->policy
.svc_tkt_lifetime
,
3545 &kdc_db_ctx
->policy
.usr_tkt_lifetime
,
3546 &kdc_db_ctx
->policy
.renewal_lifetime
);
3548 session_info
= system_session(kdc_db_ctx
->lp_ctx
);
3549 if (session_info
== NULL
) {
3550 talloc_free(kdc_db_ctx
);
3551 return NT_STATUS_INTERNAL_ERROR
;
3554 /* Setup the link to LDB */
3555 kdc_db_ctx
->samdb
= samdb_connect(kdc_db_ctx
,
3561 if (kdc_db_ctx
->samdb
== NULL
) {
3562 DBG_WARNING("Cannot open samdb for KDC backend!\n");
3563 talloc_free(kdc_db_ctx
);
3564 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3567 /* Find out our own krbtgt kvno */
3568 ldb_ret
= samdb_rodc(kdc_db_ctx
->samdb
, &kdc_db_ctx
->rodc
);
3569 if (ldb_ret
!= LDB_SUCCESS
) {
3570 DBG_WARNING("Cannot determine if we are an RODC in KDC backend: %s\n",
3571 ldb_errstring(kdc_db_ctx
->samdb
));
3572 talloc_free(kdc_db_ctx
);
3573 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3575 if (kdc_db_ctx
->rodc
) {
3576 int my_krbtgt_number
;
3577 const char *secondary_keytab
[] = { "msDS-SecondaryKrbTgtNumber", NULL
};
3578 struct ldb_dn
*account_dn
;
3579 struct ldb_dn
*server_dn
= samdb_server_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
);
3581 DBG_WARNING("Cannot determine server DN in KDC backend: %s\n",
3582 ldb_errstring(kdc_db_ctx
->samdb
));
3583 talloc_free(kdc_db_ctx
);
3584 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3587 ldb_ret
= samdb_reference_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
, server_dn
,
3588 "serverReference", &account_dn
);
3589 if (ldb_ret
!= LDB_SUCCESS
) {
3590 DBG_WARNING("Cannot determine server account in KDC backend: %s\n",
3591 ldb_errstring(kdc_db_ctx
->samdb
));
3592 talloc_free(kdc_db_ctx
);
3593 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3596 ldb_ret
= samdb_reference_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
, account_dn
,
3597 "msDS-KrbTgtLink", &kdc_db_ctx
->krbtgt_dn
);
3598 talloc_free(account_dn
);
3599 if (ldb_ret
!= LDB_SUCCESS
) {
3600 DBG_WARNING("Cannot determine RODC krbtgt account in KDC backend: %s\n",
3601 ldb_errstring(kdc_db_ctx
->samdb
));
3602 talloc_free(kdc_db_ctx
);
3603 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3606 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
, kdc_db_ctx
,
3607 &msg
, kdc_db_ctx
->krbtgt_dn
, LDB_SCOPE_BASE
,
3609 DSDB_SEARCH_NO_GLOBAL_CATALOG
,
3610 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=*))");
3611 if (ldb_ret
!= LDB_SUCCESS
) {
3612 DBG_WARNING("Cannot read krbtgt account %s in KDC backend to get msDS-SecondaryKrbTgtNumber: %s: %s\n",
3613 ldb_dn_get_linearized(kdc_db_ctx
->krbtgt_dn
),
3614 ldb_errstring(kdc_db_ctx
->samdb
),
3615 ldb_strerror(ldb_ret
));
3616 talloc_free(kdc_db_ctx
);
3617 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3619 my_krbtgt_number
= ldb_msg_find_attr_as_int(msg
, "msDS-SecondaryKrbTgtNumber", -1);
3620 if (my_krbtgt_number
== -1) {
3621 DBG_WARNING("Cannot read msDS-SecondaryKrbTgtNumber from krbtgt account %s in KDC backend: got %d\n",
3622 ldb_dn_get_linearized(kdc_db_ctx
->krbtgt_dn
),
3624 talloc_free(kdc_db_ctx
);
3625 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3627 kdc_db_ctx
->my_krbtgt_number
= my_krbtgt_number
;
3630 kdc_db_ctx
->my_krbtgt_number
= 0;
3631 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
, kdc_db_ctx
,
3633 ldb_get_default_basedn(kdc_db_ctx
->samdb
),
3636 DSDB_SEARCH_NO_GLOBAL_CATALOG
,
3637 "(&(objectClass=user)(samAccountName=krbtgt))");
3639 if (ldb_ret
!= LDB_SUCCESS
) {
3640 DBG_WARNING("could not find own KRBTGT in DB: %s\n", ldb_errstring(kdc_db_ctx
->samdb
));
3641 talloc_free(kdc_db_ctx
);
3642 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3644 kdc_db_ctx
->krbtgt_dn
= talloc_steal(kdc_db_ctx
, msg
->dn
);
3645 kdc_db_ctx
->my_krbtgt_number
= 0;
3648 *kdc_db_ctx_out
= kdc_db_ctx
;
3649 return NT_STATUS_OK
;
3652 krb5_error_code
dsdb_extract_aes_256_key(krb5_context context
,
3653 TALLOC_CTX
*mem_ctx
,
3654 const struct ldb_message
*msg
,
3655 uint32_t user_account_control
,
3656 const uint32_t *kvno
,
3658 DATA_BLOB
*aes_256_key
,
3661 krb5_error_code krb5_ret
;
3662 uint32_t supported_enctypes
;
3663 unsigned flags
= SDB_F_GET_CLIENT
;
3664 struct sdb_entry sentry
= {};
3667 flags
|= SDB_F_KVNO_SPECIFIED
;
3670 krb5_ret
= samba_kdc_message2entry_keys(context
,
3673 false, /* is_krbtgt */
3674 false, /* is_rodc */
3675 user_account_control
,
3676 SAMBA_KDC_ENT_TYPE_CLIENT
,
3678 (kvno
!= NULL
) ? *kvno
: 0,
3680 ENC_HMAC_SHA1_96_AES256
,
3681 &supported_enctypes
);
3682 if (krb5_ret
!= 0) {
3683 const char *krb5_err
= krb5_get_error_message(context
, krb5_ret
);
3685 DBG_ERR("Failed to parse supplementalCredentials "
3686 "of %s with %s kvno using "
3687 "ENCTYPE_HMAC_SHA1_96_AES256 "
3688 "Kerberos Key: %s\n",
3689 ldb_dn_get_linearized(msg
->dn
),
3690 (kvno
!= NULL
) ? "previous" : "current",
3691 krb5_err
!= NULL
? krb5_err
: "<unknown>");
3693 krb5_free_error_message(context
, krb5_err
);
3698 if ((supported_enctypes
& ENC_HMAC_SHA1_96_AES256
) == 0 ||
3699 sentry
.keys
.len
!= 1) {
3700 DBG_INFO("Failed to find a ENCTYPE_HMAC_SHA1_96_AES256 "
3701 "key in supplementalCredentials "
3702 "of %s at KVNO %u (got %u keys, expected 1)\n",
3703 ldb_dn_get_linearized(msg
->dn
),
3706 sdb_entry_free(&sentry
);
3710 if (sentry
.keys
.val
[0].salt
== NULL
) {
3711 DBG_INFO("Failed to find a salt in "
3712 "supplementalCredentials "
3713 "of %s at KVNO %u\n",
3714 ldb_dn_get_linearized(msg
->dn
),
3716 sdb_entry_free(&sentry
);
3720 if (aes_256_key
!= NULL
) {
3721 *aes_256_key
= data_blob_talloc(mem_ctx
,
3722 KRB5_KEY_DATA(&sentry
.keys
.val
[0].key
),
3723 KRB5_KEY_LENGTH(&sentry
.keys
.val
[0].key
));
3724 if (aes_256_key
->data
== NULL
) {
3725 sdb_entry_free(&sentry
);
3728 talloc_keep_secret(aes_256_key
->data
);
3732 *salt
= data_blob_talloc(mem_ctx
,
3733 sentry
.keys
.val
[0].salt
->salt
.data
,
3734 sentry
.keys
.val
[0].salt
->salt
.length
);
3735 if (salt
->data
== NULL
) {
3736 sdb_entry_free(&sentry
);
3741 if (kvno_out
!= NULL
) {
3742 *kvno_out
= sentry
.kvno
;
3745 sdb_entry_free(&sentry
);