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 * Explanaton 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 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
1059 uint32_t userAccountControl
;
1060 uint32_t msDS_User_Account_Control_Computed
;
1061 krb5_error_code ret
= 0;
1062 krb5_boolean is_computer
= FALSE
;
1063 struct samba_kdc_entry
*p
;
1066 bool protected_user
= false;
1069 bool is_krbtgt
= false;
1070 bool is_rodc
= false;
1071 bool force_rc4
= lpcfg_kdc_force_enable_rc4_weak_session_keys(lp_ctx
);
1072 struct ldb_message_element
*objectclasses
;
1073 struct ldb_val computer_val
= data_blob_string_const("computer");
1074 uint32_t config_default_supported_enctypes
= lpcfg_kdc_default_domain_supported_enctypes(lp_ctx
);
1075 uint32_t default_supported_enctypes
=
1076 config_default_supported_enctypes
!= 0 ?
1077 config_default_supported_enctypes
:
1078 ENC_RC4_HMAC_MD5
| ENC_HMAC_SHA1_96_AES256_SK
;
1079 uint32_t supported_enctypes
1080 = ldb_msg_find_attr_as_uint(msg
,
1081 "msDS-SupportedEncryptionTypes",
1082 default_supported_enctypes
);
1083 uint32_t pa_supported_enctypes
;
1084 uint32_t supported_session_etypes
;
1085 uint32_t available_enctypes
= 0;
1087 * also legacy enctypes are announced,
1088 * but effectively restricted by kdc_enctypes
1090 uint32_t domain_enctypes
= ENC_RC4_HMAC_MD5
| ENC_RSA_MD5
| ENC_CRC32
;
1091 uint32_t config_kdc_enctypes
= lpcfg_kdc_supported_enctypes(lp_ctx
);
1092 uint32_t kdc_enctypes
=
1093 config_kdc_enctypes
!= 0 ?
1094 config_kdc_enctypes
:
1096 const char *samAccountName
= ldb_msg_find_attr_as_string(msg
, "samAccountName", NULL
);
1098 const struct authn_kerberos_client_policy
*authn_client_policy
= NULL
;
1099 const struct authn_server_policy
*authn_server_policy
= NULL
;
1100 int64_t enforced_tgt_lifetime_raw
;
1102 ZERO_STRUCTP(entry
);
1104 if (supported_enctypes
== 0) {
1105 supported_enctypes
= default_supported_enctypes
;
1108 if (dsdb_functional_level(kdc_db_ctx
->samdb
) >= DS_DOMAIN_FUNCTION_2008
) {
1109 domain_enctypes
|= ENC_HMAC_SHA1_96_AES128
| ENC_HMAC_SHA1_96_AES256
;
1112 if (ldb_msg_find_element(msg
, "msDS-SecondaryKrbTgtNumber")) {
1116 if (!samAccountName
) {
1118 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: no samAccountName present");
1122 objectclasses
= ldb_msg_find_element(msg
, "objectClass");
1124 if (objectclasses
&& ldb_msg_find_val(objectclasses
, &computer_val
)) {
1128 p
= talloc_zero(mem_ctx
, struct samba_kdc_entry
);
1134 p
->is_rodc
= is_rodc
;
1135 p
->kdc_db_ctx
= kdc_db_ctx
;
1136 p
->realm_dn
= talloc_reference(p
, realm_dn
);
1142 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
1144 entry
->skdc_entry
= p
;
1146 userAccountControl
= ldb_msg_find_attr_as_uint(msg
, "userAccountControl", 0);
1148 msDS_User_Account_Control_Computed
1149 = ldb_msg_find_attr_as_uint(msg
,
1150 "msDS-User-Account-Control-Computed",
1154 * This brings in the lockout flag, block the account if not
1155 * found. We need the weird UF_ACCOUNTDISABLE check because
1156 * we do not want to fail open if the value is not returned,
1157 * but 0 is a valid value (all OK)
1159 if (msDS_User_Account_Control_Computed
== UF_ACCOUNTDISABLE
) {
1161 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: "
1162 "no msDS-User-Account-Control-Computed present");
1165 userAccountControl
|= msDS_User_Account_Control_Computed
;
1168 if (ent_type
== SAMBA_KDC_ENT_TYPE_KRBTGT
) {
1169 p
->is_krbtgt
= true;
1172 /* First try and figure out the flags based on the userAccountControl */
1173 entry
->flags
= uf2SDBFlags(context
, userAccountControl
, ent_type
);
1176 * Take control of the returned principal here, rather than
1177 * allowing the Heimdal code to do it as we have specific
1178 * behaviour around the forced realm to honour
1180 entry
->flags
.force_canonicalize
= true;
1182 /* Windows 2008 seems to enforce this (very sensible) rule by
1183 * default - don't allow offline attacks on a user's password
1184 * by asking for a ticket to them as a service (encrypted with
1185 * their probably pathetically insecure password) */
1187 if (entry
->flags
.server
1188 && lpcfg_parm_bool(lp_ctx
, NULL
, "kdc", "require spn for service", true)) {
1189 if (!is_computer
&& !ldb_msg_find_attr_as_string(msg
, "servicePrincipalName", NULL
)) {
1190 entry
->flags
.server
= 0;
1195 * We restrict a 3-part SPN ending in my domain/realm to full
1196 * domain controllers.
1198 * This avoids any cases where (eg) a demoted DC still has
1199 * these more restricted SPNs.
1201 if (krb5_princ_size(context
, principal
) > 2) {
1203 = smb_krb5_principal_get_comp_string(mem_ctx
,
1208 lpcfg_is_my_domain_or_realm(lp_ctx
,
1210 bool is_dc
= userAccountControl
&
1211 (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
);
1212 if (is_our_realm
&& !is_dc
) {
1213 entry
->flags
.server
= 0;
1217 * To give the correct type of error to the client, we must
1218 * not just return the entry without .server set, we must
1219 * pretend the principal does not exist. Otherwise we may
1220 * return ERR_POLICY instead of
1221 * KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
1223 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
&& entry
->flags
.server
== 0) {
1224 ret
= SDB_ERR_NOENTRY
;
1225 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: no servicePrincipalName present for this server, refusing with no-such-entry");
1228 if (flags
& SDB_F_ADMIN_DATA
) {
1229 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
1230 * of the Heimdal KDC. They are stored in a the traditional
1231 * DB for audit purposes, and still form part of the structure
1234 /* use 'whenCreated' */
1235 entry
->created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
1236 /* use 'kadmin' for now (needed by mit_samba) */
1238 ret
= smb_krb5_make_principal(context
,
1239 &entry
->created_by
.principal
,
1240 lpcfg_realm(lp_ctx
), "kadmin", NULL
);
1242 krb5_clear_error_message(context
);
1246 entry
->modified_by
= calloc(1, sizeof(struct sdb_event
));
1247 if (entry
->modified_by
== NULL
) {
1249 krb5_set_error_message(context
, ret
, "calloc: out of memory");
1253 /* use 'whenChanged' */
1254 entry
->modified_by
->time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenChanged", 0);
1255 /* use 'kadmin' for now (needed by mit_samba) */
1256 ret
= smb_krb5_make_principal(context
,
1257 &entry
->modified_by
->principal
,
1258 lpcfg_realm(lp_ctx
), "kadmin", NULL
);
1260 krb5_clear_error_message(context
);
1266 /* The lack of password controls etc applies to krbtgt by
1267 * virtue of being that particular RID */
1268 ret
= samdb_result_dom_sid_buf(msg
, "objectSid", &sid
);
1272 status
= dom_sid_split_rid(NULL
, &sid
, NULL
, &rid
);
1273 if (!NT_STATUS_IS_OK(status
)) {
1278 if (rid
== DOMAIN_RID_KRBTGT
) {
1281 entry
->valid_end
= NULL
;
1282 entry
->pw_end
= NULL
;
1284 entry
->flags
.invalid
= 0;
1285 entry
->flags
.server
= 1;
1287 realm
= smb_krb5_principal_get_realm(
1288 mem_ctx
, context
, principal
);
1289 if (realm
== NULL
) {
1294 /* Don't mark all requests for the krbtgt/realm as
1295 * 'change password', as otherwise we could get into
1296 * trouble, and not enforce the password expirty.
1297 * Instead, only do it when request is for the kpasswd service */
1298 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
&&
1299 is_kadmin_changepw(context
, principal
) &&
1300 lpcfg_is_my_domain_or_realm(lp_ctx
, realm
)) {
1301 entry
->flags
.change_pw
= 1;
1306 entry
->flags
.client
= 0;
1307 entry
->flags
.forwardable
= 1;
1308 entry
->flags
.ok_as_delegate
= 1;
1309 } else if (is_rodc
) {
1310 /* The RODC krbtgt account is like the main krbtgt,
1311 * but it does not have a changepw or kadmin
1314 entry
->valid_end
= NULL
;
1315 entry
->pw_end
= NULL
;
1317 /* Also don't allow the RODC krbtgt to be a client (it should not be needed) */
1318 entry
->flags
.client
= 0;
1319 entry
->flags
.invalid
= 0;
1320 entry
->flags
.server
= 1;
1322 entry
->flags
.client
= 0;
1323 entry
->flags
.forwardable
= 1;
1324 entry
->flags
.ok_as_delegate
= 0;
1325 } else if (entry
->flags
.server
&& ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
1326 /* The account/password expiry only applies when the account is used as a
1327 * client (ie password login), not when used as a server */
1329 /* Make very well sure we don't use this for a client,
1330 * it could bypass the password restrictions */
1331 entry
->flags
.client
= 0;
1333 entry
->valid_end
= NULL
;
1334 entry
->pw_end
= NULL
;
1337 NTTIME must_change_time
1338 = samdb_result_nttime(msg
,
1339 "msDS-UserPasswordExpiryTimeComputed",
1341 if (must_change_time
== 0x7FFFFFFFFFFFFFFFULL
) {
1342 entry
->pw_end
= NULL
;
1344 entry
->pw_end
= malloc(sizeof(*entry
->pw_end
));
1345 if (entry
->pw_end
== NULL
) {
1349 *entry
->pw_end
= nt_time_to_unix(must_change_time
);
1352 acct_expiry
= samdb_result_account_expires(msg
);
1353 if (acct_expiry
== 0x7FFFFFFFFFFFFFFFULL
) {
1354 entry
->valid_end
= NULL
;
1356 entry
->valid_end
= malloc(sizeof(*entry
->valid_end
));
1357 if (entry
->valid_end
== NULL
) {
1361 *entry
->valid_end
= nt_time_to_unix(acct_expiry
);
1365 ret
= samba_kdc_get_entry_principal(context
,
1370 entry
->flags
.change_pw
,
1374 krb5_clear_error_message(context
);
1378 entry
->valid_start
= NULL
;
1380 entry
->max_life
= malloc(sizeof(*entry
->max_life
));
1381 if (entry
->max_life
== NULL
) {
1386 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
1387 *entry
->max_life
= kdc_db_ctx
->policy
.svc_tkt_lifetime
;
1388 } else if (ent_type
== SAMBA_KDC_ENT_TYPE_KRBTGT
|| ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
) {
1389 *entry
->max_life
= kdc_db_ctx
->policy
.usr_tkt_lifetime
;
1391 *entry
->max_life
= MIN(kdc_db_ctx
->policy
.svc_tkt_lifetime
,
1392 kdc_db_ctx
->policy
.usr_tkt_lifetime
);
1395 if (entry
->flags
.change_pw
) {
1396 /* Limit lifetime of kpasswd tickets to two minutes or less. */
1397 *entry
->max_life
= MIN(*entry
->max_life
, CHANGEPW_LIFETIME
);
1400 entry
->max_renew
= malloc(sizeof(*entry
->max_renew
));
1401 if (entry
->max_renew
== NULL
) {
1406 *entry
->max_renew
= kdc_db_ctx
->policy
.renewal_lifetime
;
1409 * A principal acting as a client that is not being looked up as the
1410 * principal of an armor ticket may have an authentication policy apply
1413 * We won’t get an authentication policy for the client of an S4U2Self
1414 * or S4U2Proxy request. Those clients are looked up with
1415 * SDB_F_FOR_TGS_REQ instead of with SDB_F_FOR_AS_REQ.
1417 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
&&
1418 (flags
& SDB_F_FOR_AS_REQ
) &&
1419 !(flags
& SDB_F_ARMOR_PRINCIPAL
))
1421 ret
= authn_policy_kerberos_client(kdc_db_ctx
->samdb
, mem_ctx
, msg
,
1422 &authn_client_policy
);
1429 * A principal acting as a server may have an authentication policy
1432 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
1433 ret
= authn_policy_server(kdc_db_ctx
->samdb
, mem_ctx
, msg
,
1434 &authn_server_policy
);
1440 enforced_tgt_lifetime_raw
= authn_policy_enforced_tgt_lifetime_raw(authn_client_policy
);
1441 if (enforced_tgt_lifetime_raw
!= 0) {
1442 int64_t lifetime_secs
= enforced_tgt_lifetime_raw
;
1444 lifetime_secs
/= INT64_C(1000) * 1000 * 10;
1445 lifetime_secs
= MIN(lifetime_secs
, INT_MAX
);
1446 lifetime_secs
= MAX(lifetime_secs
, INT_MIN
);
1449 * Set both lifetime and renewal time based only on the
1450 * configured maximum lifetime — not on the configured renewal
1451 * time. Yes, this is what Windows does.
1453 lifetime_secs
= MIN(*entry
->max_life
, lifetime_secs
);
1454 *entry
->max_life
= lifetime_secs
;
1455 *entry
->max_renew
= lifetime_secs
;
1458 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
&& (flags
& SDB_F_FOR_AS_REQ
)) {
1460 const struct auth_user_info_dc
*user_info_dc
= NULL
;
1462 * These protections only apply to clients, so servers in the
1463 * Protected Users group may still have service tickets to them
1464 * encrypted with RC4. For accounts looked up as servers, note
1465 * that 'msg' does not contain the 'memberOf' attribute for
1466 * determining whether the account is a member of Protected
1469 * Additionally, Microsoft advises that accounts for services
1470 * and computers should never be members of Protected Users, or
1471 * they may fail to authenticate.
1473 status
= samba_kdc_get_user_info_from_db(p
, msg
, &user_info_dc
);
1474 if (!NT_STATUS_IS_OK(status
)) {
1479 result
= dsdb_is_protected_user(kdc_db_ctx
->samdb
,
1481 user_info_dc
->num_sids
);
1487 protected_user
= result
;
1489 if (protected_user
) {
1490 entry
->flags
.forwardable
= 0;
1491 entry
->flags
.proxiable
= 0;
1493 if (enforced_tgt_lifetime_raw
== 0) {
1495 * If a TGT lifetime hasn’t been set, Protected
1496 * Users enforces a four hour TGT lifetime.
1498 *entry
->max_life
= MIN(*entry
->max_life
, 4 * 60 * 60);
1499 *entry
->max_renew
= MIN(*entry
->max_renew
, 4 * 60 * 60);
1504 if (rid
== DOMAIN_RID_KRBTGT
|| is_rodc
) {
1510 * KDCs (and KDCs on RODCs)
1511 * ignore msDS-SupportedEncryptionTypes completely
1512 * but support all supported enctypes by the domain.
1514 supported_enctypes
= domain_enctypes
;
1516 enable_fast
= lpcfg_kdc_enable_fast(kdc_db_ctx
->lp_ctx
);
1518 supported_enctypes
|= ENC_FAST_SUPPORTED
;
1521 supported_enctypes
|= ENC_CLAIMS_SUPPORTED
;
1522 supported_enctypes
|= ENC_COMPOUND_IDENTITY_SUPPORTED
;
1525 * Resource SID compression is enabled implicitly, unless
1526 * disabled in msDS-SupportedEncryptionTypes.
1529 } else if (userAccountControl
& (UF_PARTIAL_SECRETS_ACCOUNT
|UF_SERVER_TRUST_ACCOUNT
)) {
1531 * DCs and RODCs computer accounts take
1532 * msDS-SupportedEncryptionTypes unmodified, but
1533 * force all enctypes supported by the domain.
1535 supported_enctypes
|= domain_enctypes
;
1537 } else if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
||
1538 (ent_type
== SAMBA_KDC_ENT_TYPE_ANY
)) {
1540 * for AS-REQ the client chooses the enc types it
1541 * supports, and this will vary between computers a
1542 * user logs in from. Therefore, so that we accept any
1543 * of the client's keys for decrypting padata,
1544 * supported_enctypes should not restrict etype usage.
1546 * likewise for 'any' return as much as is supported,
1547 * to export into a keytab.
1549 supported_enctypes
|= ENC_ALL_TYPES
;
1552 /* If UF_USE_DES_KEY_ONLY has been set, then don't allow use of the newer enc types */
1553 if (userAccountControl
& UF_USE_DES_KEY_ONLY
) {
1554 supported_enctypes
&= ~ENC_ALL_TYPES
;
1557 if (protected_user
) {
1558 supported_enctypes
&= ~ENC_RC4_HMAC_MD5
;
1561 pa_supported_enctypes
= supported_enctypes
;
1562 supported_session_etypes
= supported_enctypes
;
1563 if (supported_session_etypes
& ENC_HMAC_SHA1_96_AES256_SK
) {
1564 supported_session_etypes
|= ENC_HMAC_SHA1_96_AES256
;
1565 supported_session_etypes
|= ENC_HMAC_SHA1_96_AES128
;
1568 supported_session_etypes
|= ENC_RC4_HMAC_MD5
;
1571 * now that we remembered what to announce in pa_supported_enctypes
1572 * and normalized ENC_HMAC_SHA1_96_AES256_SK, we restrict the
1573 * rest to the enc types the local kdc supports.
1575 supported_enctypes
&= kdc_enctypes
;
1576 supported_session_etypes
&= kdc_enctypes
;
1578 /* Get keys from the db */
1579 ret
= samba_kdc_message2entry_keys(context
, p
, msg
,
1582 ent_type
, flags
, kvno
, entry
,
1584 &available_enctypes
);
1586 /* Could be bogus data in the entry, or out of memory */
1591 * If we only have a nthash stored,
1592 * but a better session key would be
1593 * available, we fallback to fetching the
1594 * RC4_HMAC_MD5, which implicitly also
1595 * would allow an RC4_HMAC_MD5 session key.
1596 * But only if the kdc actually supports
1599 if (available_enctypes
== 0 &&
1600 (supported_enctypes
& ENC_RC4_HMAC_MD5
) == 0 &&
1601 (supported_enctypes
& ~ENC_RC4_HMAC_MD5
) != 0 &&
1602 (kdc_enctypes
& ENC_RC4_HMAC_MD5
) != 0)
1604 supported_enctypes
= ENC_RC4_HMAC_MD5
;
1605 ret
= samba_kdc_message2entry_keys(context
, p
, msg
,
1608 ent_type
, flags
, kvno
, entry
,
1610 &available_enctypes
);
1612 /* Could be bogus data in the entry, or out of memory */
1618 * We need to support all session keys enctypes for
1619 * all keys we provide
1621 supported_session_etypes
|= available_enctypes
;
1623 ret
= sdb_entry_set_etypes(entry
);
1628 if (entry
->flags
.server
) {
1630 supported_session_etypes
& KERB_ENCTYPE_AES256_CTS_HMAC_SHA1_96
;
1632 supported_session_etypes
& KERB_ENCTYPE_AES128_CTS_HMAC_SHA1_96
;
1634 supported_session_etypes
& ENC_RC4_HMAC_MD5
;
1635 ret
= sdb_entry_set_session_etypes(entry
,
1644 if (entry
->keys
.len
!= 0) {
1646 * FIXME: Currently limited to Heimdal so as not to
1647 * break MIT KDCs, for which no fix is available.
1649 #ifdef SAMBA4_USES_HEIMDAL
1652 * The krbtgt account, having no reason to
1653 * issue tickets encrypted in weaker keys,
1654 * shall only make available its strongest
1655 * key. All weaker keys are stripped out. This
1656 * makes it impossible for an RC4-encrypted
1657 * TGT to be accepted when AES KDC keys exist.
1659 * This controls the ticket key and so the PAC
1660 * signature algorithms indirectly, preventing
1661 * a weak KDC checksum from being accepted
1662 * when we verify the signatures for an
1663 * S4U2Proxy evidence ticket. As such, this is
1664 * indispensable for addressing
1667 * Being strict here also provides protection
1668 * against possible future attacks on weak
1671 entry
->keys
.len
= 1;
1672 if (entry
->etypes
!= NULL
) {
1673 entry
->etypes
->len
= MIN(entry
->etypes
->len
, 1);
1675 entry
->old_keys
.len
= MIN(entry
->old_keys
.len
, 1);
1676 entry
->older_keys
.len
= MIN(entry
->older_keys
.len
, 1);
1679 } else if (kdc_db_ctx
->rodc
) {
1681 * We are on an RODC, but don't have keys for this
1682 * account. Signal this to the caller
1684 auth_sam_trigger_repl_secret(kdc_db_ctx
,
1685 kdc_db_ctx
->msg_ctx
,
1688 ret
= SDB_ERR_NOT_FOUND_HERE
;
1692 * oh, no password. Apparently (comment in
1693 * hdb-ldap.c) this violates the ASN.1, but this
1694 * allows an entry with no keys (yet).
1698 p
->msg
= talloc_steal(p
, msg
);
1699 p
->supported_enctypes
= pa_supported_enctypes
;
1701 p
->client_policy
= talloc_steal(p
, authn_client_policy
);
1702 p
->server_policy
= talloc_steal(p
, authn_server_policy
);
1706 /* This doesn't free ent itself, that is for the eventual caller to do */
1707 sdb_entry_free(entry
);
1709 talloc_steal(kdc_db_ctx
, p
);
1716 * Construct an hdb_entry from a directory entry.
1717 * The kvno is what the remote client asked for
1719 static krb5_error_code
samba_kdc_trust_message2entry(krb5_context context
,
1720 struct samba_kdc_db_context
*kdc_db_ctx
,
1721 TALLOC_CTX
*mem_ctx
,
1722 enum trust_direction direction
,
1723 struct ldb_dn
*realm_dn
,
1726 struct ldb_message
*msg
,
1727 struct sdb_entry
*entry
)
1729 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
1730 const char *our_realm
= lpcfg_realm(lp_ctx
);
1731 char *partner_realm
= NULL
;
1732 const char *realm
= NULL
;
1733 const char *krbtgt_realm
= NULL
;
1734 DATA_BLOB password_utf16
= data_blob_null
;
1735 DATA_BLOB password_utf8
= data_blob_null
;
1736 struct samr_Password _password_hash
;
1737 const struct samr_Password
*password_hash
= NULL
;
1738 const struct ldb_val
*password_val
;
1739 struct trustAuthInOutBlob password_blob
;
1740 struct samba_kdc_entry
*p
;
1741 bool use_previous
= false;
1742 uint32_t current_kvno
;
1743 uint32_t previous_kvno
;
1744 uint32_t num_keys
= 0;
1745 enum ndr_err_code ndr_err
;
1748 struct AuthenticationInformationArray
*auth_array
;
1751 uint32_t *auth_kvno
;
1752 bool preferr_current
= false;
1753 bool force_rc4
= lpcfg_kdc_force_enable_rc4_weak_session_keys(lp_ctx
);
1754 uint32_t supported_enctypes
= ENC_RC4_HMAC_MD5
;
1755 uint32_t pa_supported_enctypes
;
1756 uint32_t supported_session_etypes
;
1757 uint32_t config_kdc_enctypes
= lpcfg_kdc_supported_enctypes(lp_ctx
);
1758 uint32_t kdc_enctypes
=
1759 config_kdc_enctypes
!= 0 ?
1760 config_kdc_enctypes
:
1762 struct lsa_TrustDomainInfoInfoEx
*tdo
= NULL
;
1765 ZERO_STRUCTP(entry
);
1767 if (dsdb_functional_level(kdc_db_ctx
->samdb
) >= DS_DOMAIN_FUNCTION_2008
) {
1768 /* If not told otherwise, Windows now assumes that trusts support AES. */
1769 supported_enctypes
= ldb_msg_find_attr_as_uint(msg
,
1770 "msDS-SupportedEncryptionTypes",
1771 ENC_HMAC_SHA1_96_AES256
);
1774 pa_supported_enctypes
= supported_enctypes
;
1775 supported_session_etypes
= supported_enctypes
;
1776 if (supported_session_etypes
& ENC_HMAC_SHA1_96_AES256_SK
) {
1777 supported_session_etypes
|= ENC_HMAC_SHA1_96_AES256
;
1778 supported_session_etypes
|= ENC_HMAC_SHA1_96_AES128
;
1781 supported_session_etypes
|= ENC_RC4_HMAC_MD5
;
1784 * now that we remembered what to announce in pa_supported_enctypes
1785 * and normalized ENC_HMAC_SHA1_96_AES256_SK, we restrict the
1786 * rest to the enc types the local kdc supports.
1788 supported_enctypes
&= kdc_enctypes
;
1789 supported_session_etypes
&= kdc_enctypes
;
1791 status
= dsdb_trust_parse_tdo_info(mem_ctx
, msg
, &tdo
);
1792 if (!NT_STATUS_IS_OK(status
)) {
1793 krb5_clear_error_message(context
);
1798 if (!(tdo
->trust_direction
& direction
)) {
1799 krb5_clear_error_message(context
);
1800 ret
= SDB_ERR_NOENTRY
;
1804 if (tdo
->trust_type
!= LSA_TRUST_TYPE_UPLEVEL
) {
1806 * Only UPLEVEL domains support kerberos here,
1807 * as we don't support LSA_TRUST_TYPE_MIT.
1809 krb5_clear_error_message(context
);
1810 ret
= SDB_ERR_NOENTRY
;
1814 if (tdo
->trust_attributes
& LSA_TRUST_ATTRIBUTE_CROSS_ORGANIZATION
) {
1816 * We don't support selective authentication yet.
1818 krb5_clear_error_message(context
);
1819 ret
= SDB_ERR_NOENTRY
;
1823 if (tdo
->domain_name
.string
== NULL
) {
1824 krb5_clear_error_message(context
);
1825 ret
= SDB_ERR_NOENTRY
;
1828 partner_realm
= strupper_talloc(mem_ctx
, tdo
->domain_name
.string
);
1829 if (partner_realm
== NULL
) {
1830 krb5_clear_error_message(context
);
1835 if (direction
== INBOUND
) {
1837 krbtgt_realm
= partner_realm
;
1839 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthIncoming");
1840 } else { /* OUTBOUND */
1841 realm
= partner_realm
;
1842 krbtgt_realm
= our_realm
;
1844 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthOutgoing");
1847 if (password_val
== NULL
) {
1848 krb5_clear_error_message(context
);
1849 ret
= SDB_ERR_NOENTRY
;
1853 ndr_err
= ndr_pull_struct_blob(password_val
, mem_ctx
, &password_blob
,
1854 (ndr_pull_flags_fn_t
)ndr_pull_trustAuthInOutBlob
);
1855 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
1856 krb5_clear_error_message(context
);
1861 p
= talloc_zero(mem_ctx
, struct samba_kdc_entry
);
1868 p
->kdc_db_ctx
= kdc_db_ctx
;
1869 p
->realm_dn
= realm_dn
;
1870 p
->supported_enctypes
= pa_supported_enctypes
;
1872 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
1874 entry
->skdc_entry
= p
;
1876 /* use 'whenCreated' */
1877 entry
->created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
1878 /* use 'kadmin' for now (needed by mit_samba) */
1879 ret
= smb_krb5_make_principal(context
,
1880 &entry
->created_by
.principal
,
1881 realm
, "kadmin", NULL
);
1883 krb5_clear_error_message(context
);
1888 * We always need to generate the canonicalized principal
1889 * with the values of our database.
1891 ret
= smb_krb5_make_principal(context
, &entry
->principal
, realm
,
1892 "krbtgt", krbtgt_realm
, NULL
);
1894 krb5_clear_error_message(context
);
1897 smb_krb5_principal_set_type(context
, entry
->principal
,
1900 entry
->valid_start
= NULL
;
1902 /* we need to work out if we are going to use the current or
1903 * the previous password hash.
1904 * We base this on the kvno the client passes in. If the kvno
1905 * passed in is equal to the current kvno in our database then
1906 * we use the current structure. If it is the current kvno-1,
1907 * then we use the previous substrucure.
1911 * Windows preferrs the previous key for one hour.
1913 tv
= timeval_current();
1914 if (tv
.tv_sec
> 3600) {
1917 an_hour_ago
= timeval_to_nttime(&tv
);
1919 /* first work out the current kvno */
1921 for (i
=0; i
< password_blob
.count
; i
++) {
1922 struct AuthenticationInformation
*a
=
1923 &password_blob
.current
.array
[i
];
1925 if (a
->LastUpdateTime
<= an_hour_ago
) {
1926 preferr_current
= true;
1929 if (a
->AuthType
== TRUST_AUTH_TYPE_VERSION
) {
1930 current_kvno
= a
->AuthInfo
.version
.version
;
1933 if (current_kvno
== 0) {
1934 previous_kvno
= 255;
1936 previous_kvno
= current_kvno
- 1;
1938 for (i
=0; i
< password_blob
.count
; i
++) {
1939 struct AuthenticationInformation
*a
=
1940 &password_blob
.previous
.array
[i
];
1942 if (a
->AuthType
== TRUST_AUTH_TYPE_VERSION
) {
1943 previous_kvno
= a
->AuthInfo
.version
.version
;
1947 /* work out whether we will use the previous or current
1949 if (password_blob
.previous
.count
== 0) {
1950 /* there is no previous password */
1951 use_previous
= false;
1952 } else if (!(flags
& SDB_F_KVNO_SPECIFIED
)) {
1954 * If not specified we use the lowest kvno
1955 * for the first hour after an update.
1957 if (preferr_current
) {
1958 use_previous
= false;
1959 } else if (previous_kvno
< current_kvno
) {
1960 use_previous
= true;
1962 use_previous
= false;
1964 } else if (kvno
== current_kvno
) {
1968 use_previous
= false;
1969 } else if (kvno
== previous_kvno
) {
1973 use_previous
= true;
1976 * Fallback to the current one for anything else
1978 use_previous
= false;
1982 auth_array
= &password_blob
.previous
;
1983 auth_kvno
= &previous_kvno
;
1985 auth_array
= &password_blob
.current
;
1986 auth_kvno
= ¤t_kvno
;
1989 /* use the kvno the client specified, if available */
1990 if (flags
& SDB_F_KVNO_SPECIFIED
) {
1993 entry
->kvno
= *auth_kvno
;
1996 for (i
=0; i
< auth_array
->count
; i
++) {
1997 if (auth_array
->array
[i
].AuthType
== TRUST_AUTH_TYPE_CLEAR
) {
2000 password_utf16
= data_blob_const(auth_array
->array
[i
].AuthInfo
.clear
.password
,
2001 auth_array
->array
[i
].AuthInfo
.clear
.size
);
2002 if (password_utf16
.length
== 0) {
2006 if (supported_enctypes
& ENC_RC4_HMAC_MD5
) {
2007 mdfour(_password_hash
.hash
, password_utf16
.data
, password_utf16
.length
);
2008 if (password_hash
== NULL
) {
2011 password_hash
= &_password_hash
;
2014 if (!(supported_enctypes
& (ENC_HMAC_SHA1_96_AES128
|ENC_HMAC_SHA1_96_AES256
))) {
2018 ok
= convert_string_talloc(mem_ctx
,
2019 CH_UTF16MUNGED
, CH_UTF8
,
2020 password_utf16
.data
,
2021 password_utf16
.length
,
2022 &password_utf8
.data
,
2023 &password_utf8
.length
);
2025 krb5_clear_error_message(context
);
2030 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES128
) {
2033 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES256
) {
2037 } else if (auth_array
->array
[i
].AuthType
== TRUST_AUTH_TYPE_NT4OWF
) {
2038 if (supported_enctypes
& ENC_RC4_HMAC_MD5
) {
2039 password_hash
= &auth_array
->array
[i
].AuthInfo
.nt4owf
.password
;
2045 /* Must have found a cleartext or MD4 password */
2046 if (num_keys
== 0) {
2047 DBG_WARNING("no usable key found\n");
2048 krb5_clear_error_message(context
);
2049 ret
= SDB_ERR_NOENTRY
;
2053 entry
->keys
.val
= calloc(num_keys
, sizeof(struct sdb_key
));
2054 if (entry
->keys
.val
== NULL
) {
2055 krb5_clear_error_message(context
);
2060 if (password_utf8
.length
!= 0) {
2061 struct sdb_key key
= {};
2062 krb5_const_principal salt_principal
= entry
->principal
;
2064 krb5_data cleartext_data
;
2066 cleartext_data
.data
= discard_const_p(char, password_utf8
.data
);
2067 cleartext_data
.length
= password_utf8
.length
;
2069 ret
= smb_krb5_get_pw_salt(context
,
2076 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES256
) {
2077 ret
= smb_krb5_create_key_from_string(context
,
2081 ENCTYPE_AES256_CTS_HMAC_SHA1_96
,
2084 smb_krb5_free_data_contents(context
, &salt
);
2088 entry
->keys
.val
[entry
->keys
.len
] = key
;
2092 if (supported_enctypes
& ENC_HMAC_SHA1_96_AES128
) {
2093 ret
= smb_krb5_create_key_from_string(context
,
2097 ENCTYPE_AES128_CTS_HMAC_SHA1_96
,
2100 smb_krb5_free_data_contents(context
, &salt
);
2104 entry
->keys
.val
[entry
->keys
.len
] = key
;
2108 smb_krb5_free_data_contents(context
, &salt
);
2111 if (password_hash
!= NULL
) {
2112 struct sdb_key key
= {};
2114 ret
= smb_krb5_keyblock_init_contents(context
,
2115 ENCTYPE_ARCFOUR_HMAC
,
2116 password_hash
->hash
,
2117 sizeof(password_hash
->hash
),
2123 entry
->keys
.val
[entry
->keys
.len
] = key
;
2127 entry
->flags
= int2SDBFlags(0);
2128 entry
->flags
.immutable
= 1;
2129 entry
->flags
.invalid
= 0;
2130 entry
->flags
.server
= 1;
2131 entry
->flags
.require_preauth
= 1;
2133 entry
->pw_end
= NULL
;
2135 entry
->max_life
= NULL
;
2137 entry
->max_renew
= NULL
;
2139 /* Match Windows behavior and allow forwardable flag in cross-realm. */
2140 entry
->flags
.forwardable
= 1;
2142 samba_kdc_sort_keys(&entry
->keys
);
2144 ret
= sdb_entry_set_etypes(entry
);
2151 supported_session_etypes
& KERB_ENCTYPE_AES256_CTS_HMAC_SHA1_96
;
2153 supported_session_etypes
& KERB_ENCTYPE_AES128_CTS_HMAC_SHA1_96
;
2155 supported_session_etypes
& ENC_RC4_HMAC_MD5
;
2156 ret
= sdb_entry_set_session_etypes(entry
,
2165 p
->msg
= talloc_steal(p
, msg
);
2168 TALLOC_FREE(partner_realm
);
2171 /* This doesn't free ent itself, that is for the eventual caller to do */
2172 sdb_entry_free(entry
);
2174 talloc_steal(kdc_db_ctx
, p
);
2181 static krb5_error_code
samba_kdc_lookup_trust(krb5_context context
, struct ldb_context
*ldb_ctx
,
2182 TALLOC_CTX
*mem_ctx
,
2184 struct ldb_dn
*realm_dn
,
2185 struct ldb_message
**pmsg
)
2188 const char * const *attrs
= trust_attrs
;
2190 status
= dsdb_trust_search_tdo(ldb_ctx
, realm
, realm
,
2191 attrs
, mem_ctx
, pmsg
);
2192 if (NT_STATUS_IS_OK(status
)) {
2194 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_OBJECT_NAME_NOT_FOUND
)) {
2195 return SDB_ERR_NOENTRY
;
2196 } else if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MEMORY
)) {
2198 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_trust: out of memory");
2202 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_trust: %s", nt_errstr(status
));
2207 static krb5_error_code
samba_kdc_lookup_client(krb5_context context
,
2208 struct samba_kdc_db_context
*kdc_db_ctx
,
2209 TALLOC_CTX
*mem_ctx
,
2210 krb5_const_principal principal
,
2212 struct ldb_dn
**realm_dn
,
2213 struct ldb_message
**msg
)
2216 char *principal_string
= NULL
;
2218 if (smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
2219 principal_string
= smb_krb5_principal_get_comp_string(mem_ctx
, context
,
2221 if (principal_string
== NULL
) {
2225 char *principal_string_m
= NULL
;
2226 krb5_error_code ret
;
2228 ret
= krb5_unparse_name(context
, principal
, &principal_string_m
);
2233 principal_string
= talloc_strdup(mem_ctx
, principal_string_m
);
2234 SAFE_FREE(principal_string_m
);
2235 if (principal_string
== NULL
) {
2240 nt_status
= sam_get_results_principal(kdc_db_ctx
->samdb
,
2241 mem_ctx
, principal_string
, attrs
,
2243 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
2244 krb5_principal fallback_principal
= NULL
;
2245 unsigned int num_comp
;
2246 char *fallback_realm
= NULL
;
2247 char *fallback_account
= NULL
;
2248 krb5_error_code ret
;
2250 ret
= krb5_parse_name(context
, principal_string
,
2251 &fallback_principal
);
2252 TALLOC_FREE(principal_string
);
2257 num_comp
= krb5_princ_size(context
, fallback_principal
);
2258 fallback_realm
= smb_krb5_principal_get_realm(
2259 mem_ctx
, context
, fallback_principal
);
2260 if (fallback_realm
== NULL
) {
2261 krb5_free_principal(context
, fallback_principal
);
2265 if (num_comp
== 1) {
2268 fallback_account
= smb_krb5_principal_get_comp_string(mem_ctx
,
2269 context
, fallback_principal
, 0);
2270 if (fallback_account
== NULL
) {
2271 krb5_free_principal(context
, fallback_principal
);
2272 TALLOC_FREE(fallback_realm
);
2276 len
= strlen(fallback_account
);
2277 if (len
>= 2 && fallback_account
[len
- 1] == '$') {
2278 TALLOC_FREE(fallback_account
);
2281 krb5_free_principal(context
, fallback_principal
);
2282 fallback_principal
= NULL
;
2284 if (fallback_account
!= NULL
) {
2287 with_dollar
= talloc_asprintf(mem_ctx
, "%s$",
2289 if (with_dollar
== NULL
) {
2290 TALLOC_FREE(fallback_realm
);
2293 TALLOC_FREE(fallback_account
);
2295 ret
= smb_krb5_make_principal(context
,
2296 &fallback_principal
,
2299 TALLOC_FREE(with_dollar
);
2301 TALLOC_FREE(fallback_realm
);
2305 TALLOC_FREE(fallback_realm
);
2307 if (fallback_principal
!= NULL
) {
2308 char *fallback_string
= NULL
;
2310 ret
= krb5_unparse_name(context
,
2314 krb5_free_principal(context
, fallback_principal
);
2318 nt_status
= sam_get_results_principal(kdc_db_ctx
->samdb
,
2323 SAFE_FREE(fallback_string
);
2325 krb5_free_principal(context
, fallback_principal
);
2326 fallback_principal
= NULL
;
2328 TALLOC_FREE(principal_string
);
2330 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
2331 return SDB_ERR_NOENTRY
;
2332 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_MEMORY
)) {
2334 } else if (!NT_STATUS_IS_OK(nt_status
)) {
2341 static krb5_error_code
samba_kdc_fetch_client(krb5_context context
,
2342 struct samba_kdc_db_context
*kdc_db_ctx
,
2343 TALLOC_CTX
*mem_ctx
,
2344 krb5_const_principal principal
,
2347 struct sdb_entry
*entry
)
2349 struct ldb_dn
*realm_dn
;
2350 krb5_error_code ret
;
2351 struct ldb_message
*msg
= NULL
;
2353 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
2354 mem_ctx
, principal
, user_attrs
,
2360 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
2361 principal
, SAMBA_KDC_ENT_TYPE_CLIENT
,
2363 realm_dn
, msg
, entry
);
2367 static krb5_error_code
samba_kdc_fetch_krbtgt(krb5_context context
,
2368 struct samba_kdc_db_context
*kdc_db_ctx
,
2369 TALLOC_CTX
*mem_ctx
,
2370 krb5_const_principal principal
,
2373 struct sdb_entry
*entry
)
2375 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
2376 krb5_error_code ret
;
2377 struct ldb_message
*msg
= NULL
;
2378 struct ldb_dn
*realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
2379 char *realm_from_princ
;
2380 char *realm_princ_comp
= smb_krb5_principal_get_comp_string(mem_ctx
, context
, principal
, 1);
2382 realm_from_princ
= smb_krb5_principal_get_realm(
2383 mem_ctx
, context
, principal
);
2384 if (realm_from_princ
== NULL
) {
2386 return SDB_ERR_NOENTRY
;
2389 if (krb5_princ_size(context
, principal
) != 2
2390 || (principal_comp_strcmp(context
, principal
, 0, KRB5_TGS_NAME
) != 0)) {
2392 return SDB_ERR_NOENTRY
;
2395 /* krbtgt case. Either us or a trusted realm */
2397 if (lpcfg_is_my_domain_or_realm(lp_ctx
, realm_from_princ
)
2398 && lpcfg_is_my_domain_or_realm(lp_ctx
, realm_princ_comp
)) {
2399 /* us, or someone quite like us */
2400 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
2401 * is in our db, then direct the caller at our primary
2405 unsigned int krbtgt_number
;
2406 /* w2k8r2 sometimes gives us a kvno of 255 for inter-domain
2407 trust tickets. We don't yet know what this means, but we do
2408 seem to need to treat it as unspecified */
2409 if (flags
& SDB_F_KVNO_SPECIFIED
) {
2410 krbtgt_number
= SAMBA_KVNO_GET_KRBTGT(kvno
);
2411 if (kdc_db_ctx
->rodc
) {
2412 if (krbtgt_number
!= kdc_db_ctx
->my_krbtgt_number
) {
2413 return SDB_ERR_NOT_FOUND_HERE
;
2417 krbtgt_number
= kdc_db_ctx
->my_krbtgt_number
;
2420 if (krbtgt_number
== kdc_db_ctx
->my_krbtgt_number
) {
2421 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
,
2422 &msg
, kdc_db_ctx
->krbtgt_dn
, LDB_SCOPE_BASE
,
2423 krbtgt_attrs
, DSDB_SEARCH_NO_GLOBAL_CATALOG
,
2424 "(objectClass=user)");
2426 /* We need to look up an RODC krbtgt (perhaps
2427 * ours, if we are an RODC, perhaps another
2428 * RODC if we are a read-write DC */
2429 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
,
2430 &msg
, realm_dn
, LDB_SCOPE_SUBTREE
,
2432 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
2433 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=%u))", (unsigned)(krbtgt_number
));
2436 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
2437 krb5_warnx(context
, "samba_kdc_fetch_krbtgt: could not find KRBTGT number %u in DB!",
2438 (unsigned)(krbtgt_number
));
2439 krb5_set_error_message(context
, SDB_ERR_NOENTRY
,
2440 "samba_kdc_fetch_krbtgt: could not find KRBTGT number %u in DB!",
2441 (unsigned)(krbtgt_number
));
2442 return SDB_ERR_NOENTRY
;
2443 } else if (lret
!= LDB_SUCCESS
) {
2444 krb5_warnx(context
, "samba_kdc_fetch_krbtgt: could not find KRBTGT number %u in DB!",
2445 (unsigned)(krbtgt_number
));
2446 krb5_set_error_message(context
, SDB_ERR_NOENTRY
,
2447 "samba_kdc_fetch_krbtgt: could not find KRBTGT number %u in DB!",
2448 (unsigned)(krbtgt_number
));
2449 return SDB_ERR_NOENTRY
;
2452 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
2453 principal
, SAMBA_KDC_ENT_TYPE_KRBTGT
,
2454 flags
, kvno
, realm_dn
, msg
, entry
);
2456 krb5_warnx(context
, "samba_kdc_fetch_krbtgt: self krbtgt message2entry failed");
2461 enum trust_direction direction
= UNKNOWN
;
2462 const char *realm
= NULL
;
2464 /* Either an inbound or outbound trust */
2466 if (strcasecmp(lpcfg_realm(lp_ctx
), realm_from_princ
) == 0) {
2467 /* look for inbound trust */
2468 direction
= INBOUND
;
2469 realm
= realm_princ_comp
;
2470 } else if (principal_comp_strcasecmp(context
, principal
, 1, lpcfg_realm(lp_ctx
)) == 0) {
2471 /* look for outbound trust */
2472 direction
= OUTBOUND
;
2473 realm
= realm_from_princ
;
2475 krb5_warnx(context
, "samba_kdc_fetch_krbtgt: not our realm for trusts ('%s', '%s')",
2478 krb5_set_error_message(context
, SDB_ERR_NOENTRY
, "samba_kdc_fetch_krbtgt: not our realm for trusts ('%s', '%s')",
2481 return SDB_ERR_NOENTRY
;
2484 /* Trusted domains are under CN=system */
2486 ret
= samba_kdc_lookup_trust(context
, kdc_db_ctx
->samdb
,
2488 realm
, realm_dn
, &msg
);
2491 krb5_warnx(context
, "samba_kdc_fetch_krbtgt: could not find principal in DB");
2492 krb5_set_error_message(context
, ret
, "samba_kdc_fetch_krbtgt: could not find principal in DB");
2496 ret
= samba_kdc_trust_message2entry(context
, kdc_db_ctx
, mem_ctx
,
2498 realm_dn
, flags
, kvno
, msg
, entry
);
2500 krb5_warnx(context
, "samba_kdc_fetch_krbtgt: trust_message2entry failed for %s",
2501 ldb_dn_get_linearized(msg
->dn
));
2502 krb5_set_error_message(context
, ret
, "samba_kdc_fetch_krbtgt: "
2503 "trust_message2entry failed for %s",
2504 ldb_dn_get_linearized(msg
->dn
));
2511 static krb5_error_code
samba_kdc_lookup_server(krb5_context context
,
2512 struct samba_kdc_db_context
*kdc_db_ctx
,
2513 TALLOC_CTX
*mem_ctx
,
2514 krb5_const_principal principal
,
2517 struct ldb_dn
**realm_dn
,
2518 struct ldb_message
**msg
)
2520 krb5_error_code ret
;
2521 if ((smb_krb5_principal_get_type(context
, principal
) != KRB5_NT_ENTERPRISE_PRINCIPAL
)
2522 && krb5_princ_size(context
, principal
) >= 2) {
2523 /* 'normal server' case */
2526 struct ldb_dn
*user_dn
;
2527 char *principal_string
;
2529 ret
= krb5_unparse_name_flags(context
, principal
,
2530 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
2536 /* At this point we may find the host is known to be
2537 * in a different realm, so we should generate a
2538 * referral instead */
2539 nt_status
= crack_service_principal_name(kdc_db_ctx
->samdb
,
2540 mem_ctx
, principal_string
,
2541 &user_dn
, realm_dn
);
2542 free(principal_string
);
2544 if (!NT_STATUS_IS_OK(nt_status
)) {
2545 return SDB_ERR_NOENTRY
;
2548 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
,
2550 msg
, user_dn
, LDB_SCOPE_BASE
,
2552 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
2554 if (ldb_ret
!= LDB_SUCCESS
) {
2555 return SDB_ERR_NOENTRY
;
2558 } else if (!(flags
& SDB_F_FOR_AS_REQ
)
2559 && smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
2561 * The behaviour of accepting an
2562 * KRB5_NT_ENTERPRISE_PRINCIPAL server principal
2563 * containing a UPN only applies to TGS-REQ packets,
2564 * not AS-REQ packets.
2566 return samba_kdc_lookup_client(context
, kdc_db_ctx
,
2567 mem_ctx
, principal
, attrs
,
2572 * - the AS-REQ, where we only accept
2573 * samAccountName based lookups for the server, no
2574 * matter if the name is an
2575 * KRB5_NT_ENTERPRISE_PRINCIPAL or not
2576 * - for the TGS-REQ when we are not given an
2577 * KRB5_NT_ENTERPRISE_PRINCIPAL, which also must
2578 * only lookup samAccountName based names.
2582 krb5_principal enterprise_principal
= NULL
;
2583 krb5_const_principal used_principal
= NULL
;
2586 char *filter
= NULL
;
2588 if (smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
2590 /* Need to reparse the enterprise principal to find the real target */
2591 if (krb5_princ_size(context
, principal
) != 1) {
2592 ret
= KRB5_PARSE_MALFORMED
;
2593 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_server: request for an "
2594 "enterprise principal with wrong (%d) number of components",
2595 krb5_princ_size(context
, principal
));
2598 str
= smb_krb5_principal_get_comp_string(mem_ctx
, context
, principal
, 0);
2600 return KRB5_PARSE_MALFORMED
;
2602 ret
= krb5_parse_name(context
, str
,
2603 &enterprise_principal
);
2608 used_principal
= enterprise_principal
;
2610 used_principal
= principal
;
2613 /* server as client principal case, but we must not lookup userPrincipalNames */
2614 *realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
2616 /* TODO: Check if it is our realm, otherwise give referral */
2618 ret
= krb5_unparse_name_flags(context
, used_principal
,
2619 KRB5_PRINCIPAL_UNPARSE_NO_REALM
|
2620 KRB5_PRINCIPAL_UNPARSE_DISPLAY
,
2622 used_principal
= NULL
;
2623 krb5_free_principal(context
, enterprise_principal
);
2624 enterprise_principal
= NULL
;
2627 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_server: could not parse principal");
2628 krb5_warnx(context
, "samba_kdc_lookup_server: could not parse principal");
2632 name1
= ldb_binary_encode_string(mem_ctx
, short_princ
);
2633 SAFE_FREE(short_princ
);
2634 if (name1
== NULL
) {
2637 len1
= strlen(name1
);
2638 if (len1
>= 1 && name1
[len1
- 1] != '$') {
2639 filter
= talloc_asprintf(mem_ctx
,
2640 "(&(objectClass=user)(|(samAccountName=%s)(samAccountName=%s$)))",
2642 if (filter
== NULL
) {
2646 filter
= talloc_asprintf(mem_ctx
,
2647 "(&(objectClass=user)(samAccountName=%s))",
2649 if (filter
== NULL
) {
2654 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
, msg
,
2655 *realm_dn
, LDB_SCOPE_SUBTREE
,
2657 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
2659 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
2660 DBG_DEBUG("Failed to find an entry for %s filter:%s\n",
2662 return SDB_ERR_NOENTRY
;
2664 if (lret
== LDB_ERR_CONSTRAINT_VIOLATION
) {
2665 DBG_DEBUG("Failed to find unique entry for %s filter:%s\n",
2667 return SDB_ERR_NOENTRY
;
2669 if (lret
!= LDB_SUCCESS
) {
2670 DBG_ERR("Failed single search for %s - %s\n",
2671 name1
, ldb_errstring(kdc_db_ctx
->samdb
));
2672 return SDB_ERR_NOENTRY
;
2676 return SDB_ERR_NOENTRY
;
2681 static krb5_error_code
samba_kdc_fetch_server(krb5_context context
,
2682 struct samba_kdc_db_context
*kdc_db_ctx
,
2683 TALLOC_CTX
*mem_ctx
,
2684 krb5_const_principal principal
,
2687 struct sdb_entry
*entry
)
2689 krb5_error_code ret
;
2690 struct ldb_dn
*realm_dn
;
2691 struct ldb_message
*msg
;
2693 ret
= samba_kdc_lookup_server(context
, kdc_db_ctx
, mem_ctx
, principal
,
2694 flags
, server_attrs
, &realm_dn
, &msg
);
2699 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
2700 principal
, SAMBA_KDC_ENT_TYPE_SERVER
,
2702 realm_dn
, msg
, entry
);
2704 char *client_name
= NULL
;
2705 krb5_error_code code
;
2707 code
= krb5_unparse_name(context
, principal
, &client_name
);
2710 "samba_kdc_fetch_server: message2entry failed for "
2715 "samba_kdc_fetch_server: message2entry and "
2716 "krb5_unparse_name failed");
2718 SAFE_FREE(client_name
);
2724 static krb5_error_code
samba_kdc_lookup_realm(krb5_context context
,
2725 struct samba_kdc_db_context
*kdc_db_ctx
,
2726 TALLOC_CTX
*mem_ctx
,
2727 krb5_const_principal principal
,
2729 struct sdb_entry
*entry
)
2731 TALLOC_CTX
*frame
= talloc_stackframe();
2733 krb5_error_code ret
;
2734 bool check_realm
= false;
2735 const char *realm
= NULL
;
2736 struct dsdb_trust_routing_table
*trt
= NULL
;
2737 const struct lsa_TrustDomainInfoInfoEx
*tdo
= NULL
;
2738 unsigned int num_comp
;
2742 num_comp
= krb5_princ_size(context
, principal
);
2744 if (flags
& SDB_F_GET_CLIENT
) {
2745 if (flags
& SDB_F_FOR_AS_REQ
) {
2749 if (flags
& SDB_F_GET_SERVER
) {
2750 if (flags
& SDB_F_FOR_TGS_REQ
) {
2760 realm
= smb_krb5_principal_get_realm(frame
, context
, principal
);
2761 if (realm
== NULL
) {
2767 * The requested realm needs to be our own
2769 ok
= lpcfg_is_my_domain_or_realm(kdc_db_ctx
->lp_ctx
, realm
);
2772 * The request is not for us...
2775 return SDB_ERR_NOENTRY
;
2778 if (smb_krb5_principal_get_type(context
, principal
) == KRB5_NT_ENTERPRISE_PRINCIPAL
) {
2779 char *principal_string
= NULL
;
2780 krb5_principal enterprise_principal
= NULL
;
2781 char *enterprise_realm
= NULL
;
2783 if (num_comp
!= 1) {
2785 return SDB_ERR_NOENTRY
;
2788 principal_string
= smb_krb5_principal_get_comp_string(frame
, context
,
2790 if (principal_string
== NULL
) {
2795 ret
= krb5_parse_name(context
, principal_string
,
2796 &enterprise_principal
);
2797 TALLOC_FREE(principal_string
);
2803 enterprise_realm
= smb_krb5_principal_get_realm(
2804 frame
, context
, enterprise_principal
);
2805 krb5_free_principal(context
, enterprise_principal
);
2806 if (enterprise_realm
!= NULL
) {
2807 realm
= enterprise_realm
;
2811 if (flags
& SDB_F_GET_SERVER
) {
2812 char *service_realm
= NULL
;
2814 ret
= principal_comp_strcmp(context
, principal
, 0, KRB5_TGS_NAME
);
2817 * we need to search krbtgt/ locally
2824 * We need to check the last component against the routing table.
2826 * Note this works only with 2 or 3 component principals, e.g:
2828 * servicePrincipalName: ldap/W2K8R2-219.bla.base
2829 * servicePrincipalName: ldap/W2K8R2-219.bla.base/bla.base
2830 * servicePrincipalName: ldap/W2K8R2-219.bla.base/ForestDnsZones.bla.base
2831 * servicePrincipalName: ldap/W2K8R2-219.bla.base/DomainDnsZones.bla.base
2834 if (num_comp
== 2 || num_comp
== 3) {
2835 service_realm
= smb_krb5_principal_get_comp_string(frame
,
2841 if (service_realm
!= NULL
) {
2842 realm
= service_realm
;
2846 ok
= lpcfg_is_my_domain_or_realm(kdc_db_ctx
->lp_ctx
, realm
);
2849 * skip the expensive routing lookup
2855 status
= dsdb_trust_routing_table_load(kdc_db_ctx
->samdb
,
2857 if (!NT_STATUS_IS_OK(status
)) {
2862 tdo
= dsdb_trust_routing_by_name(trt
, realm
);
2865 * This principal has to be local
2871 if (tdo
->trust_attributes
& LSA_TRUST_ATTRIBUTE_WITHIN_FOREST
) {
2873 * TODO: handle the routing within the forest
2875 * This should likely be handled in
2876 * samba_kdc_message2entry() in case we're
2877 * a global catalog. We'd need to check
2878 * if realm_dn is our own domain and derive
2879 * the dns domain name from realm_dn and check that
2880 * against the routing table or fallback to
2881 * the tdo we found here.
2883 * But for now we don't support multiple domains
2884 * in our forest correctly anyway.
2886 * Just search in our local database.
2892 ZERO_STRUCTP(entry
);
2894 ret
= krb5_copy_principal(context
, principal
,
2901 upper
= strupper_talloc(frame
, tdo
->domain_name
.string
);
2902 if (upper
== NULL
) {
2907 ret
= smb_krb5_principal_set_realm(context
,
2916 return SDB_ERR_WRONG_REALM
;
2919 krb5_error_code
samba_kdc_fetch(krb5_context context
,
2920 struct samba_kdc_db_context
*kdc_db_ctx
,
2921 krb5_const_principal principal
,
2924 struct sdb_entry
*entry
)
2926 krb5_error_code ret
= SDB_ERR_NOENTRY
;
2927 TALLOC_CTX
*mem_ctx
;
2929 mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_fetch context");
2932 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
2936 ret
= samba_kdc_lookup_realm(context
, kdc_db_ctx
, mem_ctx
,
2937 principal
, flags
, entry
);
2942 ret
= SDB_ERR_NOENTRY
;
2944 if (flags
& SDB_F_GET_CLIENT
) {
2945 ret
= samba_kdc_fetch_client(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, kvno
, entry
);
2946 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2948 if (flags
& SDB_F_GET_SERVER
) {
2949 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
2950 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, kvno
, entry
);
2951 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2953 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
2954 ret
= samba_kdc_fetch_server(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, kvno
, entry
);
2955 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2957 if (flags
& SDB_F_GET_KRBTGT
) {
2958 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, kvno
, entry
);
2959 if (ret
!= SDB_ERR_NOENTRY
) goto done
;
2963 talloc_free(mem_ctx
);
2967 struct samba_kdc_seq
{
2970 struct ldb_message
**msgs
;
2971 struct ldb_dn
*realm_dn
;
2974 static krb5_error_code
samba_kdc_seq(krb5_context context
,
2975 struct samba_kdc_db_context
*kdc_db_ctx
,
2976 struct sdb_entry
*entry
)
2978 krb5_error_code ret
;
2979 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
2980 const char *realm
= lpcfg_realm(kdc_db_ctx
->lp_ctx
);
2981 struct ldb_message
*msg
= NULL
;
2982 const char *sAMAccountName
= NULL
;
2983 krb5_principal principal
= NULL
;
2984 TALLOC_CTX
*mem_ctx
;
2987 return SDB_ERR_NOENTRY
;
2990 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_seq context");
2994 krb5_set_error_message(context
, ret
, "samba_kdc_seq: talloc_named() failed!");
2998 while (priv
->index
< priv
->count
) {
2999 msg
= priv
->msgs
[priv
->index
++];
3001 sAMAccountName
= ldb_msg_find_attr_as_string(msg
, "sAMAccountName", NULL
);
3002 if (sAMAccountName
!= NULL
) {
3007 if (sAMAccountName
== NULL
) {
3008 ret
= SDB_ERR_NOENTRY
;
3012 ret
= smb_krb5_make_principal(context
, &principal
,
3013 realm
, sAMAccountName
, NULL
);
3018 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
3019 principal
, SAMBA_KDC_ENT_TYPE_ANY
,
3020 SDB_F_ADMIN_DATA
|SDB_F_GET_ANY
,
3022 priv
->realm_dn
, msg
, entry
);
3025 if (principal
!= NULL
) {
3026 krb5_free_principal(context
, principal
);
3031 kdc_db_ctx
->seq_ctx
= NULL
;
3033 talloc_free(mem_ctx
);
3039 krb5_error_code
samba_kdc_firstkey(krb5_context context
,
3040 struct samba_kdc_db_context
*kdc_db_ctx
,
3041 struct sdb_entry
*entry
)
3043 struct ldb_context
*ldb_ctx
= kdc_db_ctx
->samdb
;
3044 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
3046 struct ldb_result
*res
= NULL
;
3047 krb5_error_code ret
;
3048 TALLOC_CTX
*mem_ctx
;
3053 kdc_db_ctx
->seq_ctx
= NULL
;
3056 priv
= (struct samba_kdc_seq
*) talloc(kdc_db_ctx
, struct samba_kdc_seq
);
3059 krb5_set_error_message(context
, ret
, "talloc: out of memory");
3065 priv
->realm_dn
= ldb_get_default_basedn(ldb_ctx
);
3068 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_firstkey context");
3072 krb5_set_error_message(context
, ret
, "samba_kdc_firstkey: talloc_named() failed!");
3077 ret
= krb5_get_default_realm(context
, &realm
);
3082 krb5_free_default_realm(context
, realm
);
3084 lret
= dsdb_search(ldb_ctx
, priv
, &res
,
3085 priv
->realm_dn
, LDB_SCOPE_SUBTREE
, user_attrs
,
3086 DSDB_SEARCH_NO_GLOBAL_CATALOG
,
3087 "(objectClass=user)");
3089 if (lret
!= LDB_SUCCESS
) {
3091 return SDB_ERR_NOENTRY
;
3094 priv
->count
= res
->count
;
3095 priv
->msgs
= talloc_steal(priv
, res
->msgs
);
3098 kdc_db_ctx
->seq_ctx
= priv
;
3100 ret
= samba_kdc_seq(context
, kdc_db_ctx
, entry
);
3104 kdc_db_ctx
->seq_ctx
= NULL
;
3106 talloc_free(mem_ctx
);
3111 krb5_error_code
samba_kdc_nextkey(krb5_context context
,
3112 struct samba_kdc_db_context
*kdc_db_ctx
,
3113 struct sdb_entry
*entry
)
3115 return samba_kdc_seq(context
, kdc_db_ctx
, entry
);
3118 /* Check if a given entry may delegate or do s4u2self to this target principal
3120 * The safest way to determine 'self' is to check the DB record made at
3121 * the time the principal was presented to the KDC.
3124 samba_kdc_check_client_matches_target_service(krb5_context context
,
3125 struct samba_kdc_entry
*skdc_entry_client
,
3126 struct samba_kdc_entry
*skdc_entry_server_target
)
3128 struct dom_sid
*orig_sid
;
3129 struct dom_sid
*target_sid
;
3130 TALLOC_CTX
*frame
= talloc_stackframe();
3132 orig_sid
= samdb_result_dom_sid(frame
,
3133 skdc_entry_client
->msg
,
3135 target_sid
= samdb_result_dom_sid(frame
,
3136 skdc_entry_server_target
->msg
,
3140 * Allow delegation to the same record (representing a
3141 * principal), even if by a different name. The easy and safe
3142 * way to prove this is by SID comparison
3144 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
3146 return KRB5KRB_AP_ERR_BADMATCH
;
3153 /* Certificates printed by a the Certificate Authority might have a
3154 * slightly different form of the user principal name to that in the
3155 * database. Allow a mismatch where they both refer to the same
3159 samba_kdc_check_pkinit_ms_upn_match(krb5_context context
,
3160 struct samba_kdc_db_context
*kdc_db_ctx
,
3161 struct samba_kdc_entry
*skdc_entry
,
3162 krb5_const_principal certificate_principal
)
3164 krb5_error_code ret
;
3165 struct ldb_dn
*realm_dn
;
3166 struct ldb_message
*msg
;
3167 struct dom_sid
*orig_sid
;
3168 struct dom_sid
*target_sid
;
3169 const char *ms_upn_check_attrs
[] = {
3173 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_pkinit_ms_upn_match");
3177 krb5_set_error_message(context
, ret
, "samba_kdc_check_pkinit_ms_upn_match: talloc_named() failed!");
3181 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
3182 mem_ctx
, certificate_principal
,
3183 ms_upn_check_attrs
, &realm_dn
, &msg
);
3186 talloc_free(mem_ctx
);
3190 orig_sid
= samdb_result_dom_sid(mem_ctx
, skdc_entry
->msg
, "objectSid");
3191 target_sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
3193 /* Consider these to be the same principal, even if by a different
3194 * name. The easy and safe way to prove this is by SID
3196 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
3197 talloc_free(mem_ctx
);
3198 #if defined(KRB5KDC_ERR_CLIENT_NAME_MISMATCH) /* MIT */
3199 return KRB5KDC_ERR_CLIENT_NAME_MISMATCH
;
3200 #else /* Heimdal (where this is an enum) */
3201 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
3205 talloc_free(mem_ctx
);
3210 * Check if a given entry may delegate to this target principal
3214 samba_kdc_check_s4u2proxy(krb5_context context
,
3215 struct samba_kdc_db_context
*kdc_db_ctx
,
3216 struct samba_kdc_entry
*skdc_entry
,
3217 krb5_const_principal target_principal
)
3219 krb5_error_code ret
;
3221 const char *client_dn
= NULL
;
3222 const char *target_principal_name
= NULL
;
3223 struct ldb_message_element
*el
;
3228 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_s4u2proxy");
3232 krb5_set_error_message(context
, ret
,
3233 "samba_kdc_check_s4u2proxy:"
3234 " talloc_named() failed!");
3238 client_dn
= ldb_dn_get_linearized(skdc_entry
->msg
->dn
);
3244 krb5_set_error_message(context
, ret
,
3245 "samba_kdc_check_s4u2proxy:"
3246 " ldb_dn_get_linearized() failed!");
3247 talloc_free(mem_ctx
);
3251 el
= ldb_msg_find_element(skdc_entry
->msg
, "msDS-AllowedToDelegateTo");
3256 SMB_ASSERT(el
->num_values
!= 0);
3259 * This is the Microsoft forwardable flag behavior.
3261 * If the proxy (target) principal is NULL, and we have any authorized
3262 * delegation target, allow to forward.
3264 if (target_principal
== NULL
) {
3265 talloc_free(mem_ctx
);
3271 * The main heimdal code already checked that the target_principal
3272 * belongs to the same realm as the client.
3274 * So we just need the principal without the realm,
3275 * as that is what is configured in the "msDS-AllowedToDelegateTo"
3278 ret
= krb5_unparse_name_flags(context
, target_principal
,
3279 KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &tmp
);
3281 talloc_free(mem_ctx
);
3282 krb5_set_error_message(context
, ret
,
3283 "samba_kdc_check_s4u2proxy:"
3284 " krb5_unparse_name() failed!");
3287 DBG_DEBUG("client[%s] for target[%s]\n",
3290 target_principal_name
= talloc_strdup(mem_ctx
, tmp
);
3292 if (target_principal_name
== NULL
) {
3294 krb5_set_error_message(context
, ret
,
3295 "samba_kdc_check_s4u2proxy:"
3296 " talloc_strdup() failed!");
3297 talloc_free(mem_ctx
);
3301 val
= data_blob_string_const(target_principal_name
);
3303 for (i
=0; i
<el
->num_values
; i
++) {
3304 struct ldb_val
*val1
= &val
;
3305 struct ldb_val
*val2
= &el
->values
[i
];
3308 if (val1
->length
!= val2
->length
) {
3312 cmp
= strncasecmp((const char *)val1
->data
,
3313 (const char *)val2
->data
,
3328 DBG_DEBUG("client[%s] allowed target[%s]\n",
3329 client_dn
, target_principal_name
);
3330 talloc_free(mem_ctx
);
3334 krb5_set_error_message(context
, ret
,
3335 "samba_kdc_check_s4u2proxy: client[%s] "
3336 "not allowed for delegation to target[%s]",
3338 target_principal_name
);
3339 talloc_free(mem_ctx
);
3340 return KRB5KDC_ERR_BADOPTION
;
3344 * This method is called for S4U2Proxy requests and implements the
3345 * resource-based constrained delegation variant, which can support
3346 * cross-realm delegation.
3348 krb5_error_code
samba_kdc_check_s4u2proxy_rbcd(
3349 krb5_context context
,
3350 struct samba_kdc_db_context
*kdc_db_ctx
,
3351 krb5_const_principal client_principal
,
3352 krb5_const_principal server_principal
,
3353 krb5_const_pac header_pac
,
3354 struct samba_kdc_entry
*proxy_skdc_entry
)
3356 krb5_error_code code
;
3357 enum ndr_err_code ndr_err
;
3358 char *client_name
= NULL
;
3359 char *server_name
= NULL
;
3360 const char *proxy_dn
= NULL
;
3361 const DATA_BLOB
*data
= NULL
;
3362 struct security_descriptor
*rbcd_security_descriptor
= NULL
;
3363 struct auth_user_info_dc
*user_info_dc
= NULL
;
3364 struct security_token
*security_token
= NULL
;
3365 uint32_t session_info_flags
=
3366 AUTH_SESSION_INFO_DEFAULT_GROUPS
|
3367 AUTH_SESSION_INFO_SIMPLE_PRIVILEGES
;
3369 * Testing shows that although Windows grants SEC_ADS_GENERIC_ALL access
3370 * in security descriptors it creates for RBCD, its KDC only requires
3371 * SEC_ADS_CONTROL_ACCESS for the access check to succeed.
3373 uint32_t access_desired
= SEC_ADS_CONTROL_ACCESS
;
3374 uint32_t access_granted
= 0;
3376 TALLOC_CTX
*mem_ctx
= NULL
;
3378 mem_ctx
= talloc_named(kdc_db_ctx
,
3380 "samba_kdc_check_s4u2proxy_rbcd");
3381 if (mem_ctx
== NULL
) {
3388 proxy_dn
= ldb_dn_get_linearized(proxy_skdc_entry
->msg
->dn
);
3389 if (proxy_dn
== NULL
) {
3390 DBG_ERR("ldb_dn_get_linearized failed for proxy_dn!\n");
3399 rbcd_security_descriptor
= talloc_zero(mem_ctx
,
3400 struct security_descriptor
);
3401 if (rbcd_security_descriptor
== NULL
) {
3408 code
= krb5_unparse_name_flags(context
,
3410 KRB5_PRINCIPAL_UNPARSE_DISPLAY
,
3413 DBG_ERR("Unable to parse client_principal!\n");
3417 code
= krb5_unparse_name_flags(context
,
3419 KRB5_PRINCIPAL_UNPARSE_DISPLAY
,
3422 DBG_ERR("Unable to parse server_principal!\n");
3426 DBG_INFO("Check delegation from client[%s] to server[%s] via "
3432 code
= kerberos_pac_to_user_info_dc(mem_ctx
,
3436 AUTH_INCLUDE_RESOURCE_GROUPS
,
3444 if (!(user_info_dc
->info
->user_flags
& NETLOGON_GUEST
)) {
3445 session_info_flags
|= AUTH_SESSION_INFO_AUTHENTICATED
;
3448 nt_status
= auth_generate_security_token(mem_ctx
,
3454 if (!NT_STATUS_IS_OK(nt_status
)) {
3455 code
= map_errno_from_nt_status(nt_status
);
3459 data
= ldb_msg_find_ldb_val(proxy_skdc_entry
->msg
,
3460 "msDS-AllowedToActOnBehalfOfOtherIdentity");
3462 DBG_WARNING("Could not find security descriptor "
3463 "msDS-AllowedToActOnBehalfOfOtherIdentity in "
3466 code
= KRB5KDC_ERR_BADOPTION
;
3470 ndr_err
= ndr_pull_struct_blob(
3473 rbcd_security_descriptor
,
3474 (ndr_pull_flags_fn_t
)ndr_pull_security_descriptor
);
3475 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
3476 errno
= ndr_map_error2errno(ndr_err
);
3477 DBG_ERR("Failed to unmarshall "
3478 "msDS-AllowedToActOnBehalfOfOtherIdentity "
3479 "security descriptor of proxy[%s]\n",
3481 code
= KRB5KDC_ERR_BADOPTION
;
3485 if (DEBUGLEVEL
>= 10) {
3486 NDR_PRINT_DEBUG(security_token
, security_token
);
3487 NDR_PRINT_DEBUG(security_descriptor
, rbcd_security_descriptor
);
3490 nt_status
= sec_access_check_ds(rbcd_security_descriptor
,
3497 if (!NT_STATUS_IS_OK(nt_status
)) {
3498 DBG_WARNING("RBCD: sec_access_check_ds(access_desired=%#08x, "
3499 "access_granted:%#08x) failed with: %s\n",
3502 nt_errstr(nt_status
));
3504 code
= KRB5KDC_ERR_BADOPTION
;
3508 DBG_NOTICE("RBCD: Access granted for client[%s]\n", client_name
);
3512 SAFE_FREE(client_name
);
3513 SAFE_FREE(server_name
);
3515 TALLOC_FREE(mem_ctx
);
3519 NTSTATUS
samba_kdc_setup_db_ctx(TALLOC_CTX
*mem_ctx
, struct samba_kdc_base_context
*base_ctx
,
3520 struct samba_kdc_db_context
**kdc_db_ctx_out
)
3523 struct ldb_message
*msg
;
3524 struct auth_session_info
*session_info
;
3525 struct samba_kdc_db_context
*kdc_db_ctx
;
3526 /* The idea here is very simple. Using Kerberos to
3527 * authenticate the KDC to the LDAP server is highly likely to
3530 * In future we may set this up to use EXTERNAL and SSL
3531 * certificates, for now it will almost certainly be NTLMSSP_SET_USERNAME
3534 kdc_db_ctx
= talloc_zero(mem_ctx
, struct samba_kdc_db_context
);
3535 if (kdc_db_ctx
== NULL
) {
3536 return NT_STATUS_NO_MEMORY
;
3538 kdc_db_ctx
->ev_ctx
= base_ctx
->ev_ctx
;
3539 kdc_db_ctx
->lp_ctx
= base_ctx
->lp_ctx
;
3540 kdc_db_ctx
->msg_ctx
= base_ctx
->msg_ctx
;
3542 /* get default kdc policy */
3543 lpcfg_default_kdc_policy(mem_ctx
,
3545 &kdc_db_ctx
->policy
.svc_tkt_lifetime
,
3546 &kdc_db_ctx
->policy
.usr_tkt_lifetime
,
3547 &kdc_db_ctx
->policy
.renewal_lifetime
);
3549 session_info
= system_session(kdc_db_ctx
->lp_ctx
);
3550 if (session_info
== NULL
) {
3551 talloc_free(kdc_db_ctx
);
3552 return NT_STATUS_INTERNAL_ERROR
;
3555 /* Setup the link to LDB */
3556 kdc_db_ctx
->samdb
= samdb_connect(kdc_db_ctx
,
3562 if (kdc_db_ctx
->samdb
== NULL
) {
3563 DBG_WARNING("Cannot open samdb for KDC backend!\n");
3564 talloc_free(kdc_db_ctx
);
3565 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3568 /* Find out our own krbtgt kvno */
3569 ldb_ret
= samdb_rodc(kdc_db_ctx
->samdb
, &kdc_db_ctx
->rodc
);
3570 if (ldb_ret
!= LDB_SUCCESS
) {
3571 DBG_WARNING("Cannot determine if we are an RODC in KDC backend: %s\n",
3572 ldb_errstring(kdc_db_ctx
->samdb
));
3573 talloc_free(kdc_db_ctx
);
3574 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3576 if (kdc_db_ctx
->rodc
) {
3577 int my_krbtgt_number
;
3578 const char *secondary_keytab
[] = { "msDS-SecondaryKrbTgtNumber", NULL
};
3579 struct ldb_dn
*account_dn
;
3580 struct ldb_dn
*server_dn
= samdb_server_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
);
3582 DBG_WARNING("Cannot determine server DN in KDC backend: %s\n",
3583 ldb_errstring(kdc_db_ctx
->samdb
));
3584 talloc_free(kdc_db_ctx
);
3585 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3588 ldb_ret
= samdb_reference_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
, server_dn
,
3589 "serverReference", &account_dn
);
3590 if (ldb_ret
!= LDB_SUCCESS
) {
3591 DBG_WARNING("Cannot determine server account in KDC backend: %s\n",
3592 ldb_errstring(kdc_db_ctx
->samdb
));
3593 talloc_free(kdc_db_ctx
);
3594 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3597 ldb_ret
= samdb_reference_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
, account_dn
,
3598 "msDS-KrbTgtLink", &kdc_db_ctx
->krbtgt_dn
);
3599 talloc_free(account_dn
);
3600 if (ldb_ret
!= LDB_SUCCESS
) {
3601 DBG_WARNING("Cannot determine RODC krbtgt account in KDC backend: %s\n",
3602 ldb_errstring(kdc_db_ctx
->samdb
));
3603 talloc_free(kdc_db_ctx
);
3604 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3607 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
, kdc_db_ctx
,
3608 &msg
, kdc_db_ctx
->krbtgt_dn
, LDB_SCOPE_BASE
,
3610 DSDB_SEARCH_NO_GLOBAL_CATALOG
,
3611 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=*))");
3612 if (ldb_ret
!= LDB_SUCCESS
) {
3613 DBG_WARNING("Cannot read krbtgt account %s in KDC backend to get msDS-SecondaryKrbTgtNumber: %s: %s\n",
3614 ldb_dn_get_linearized(kdc_db_ctx
->krbtgt_dn
),
3615 ldb_errstring(kdc_db_ctx
->samdb
),
3616 ldb_strerror(ldb_ret
));
3617 talloc_free(kdc_db_ctx
);
3618 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3620 my_krbtgt_number
= ldb_msg_find_attr_as_int(msg
, "msDS-SecondaryKrbTgtNumber", -1);
3621 if (my_krbtgt_number
== -1) {
3622 DBG_WARNING("Cannot read msDS-SecondaryKrbTgtNumber from krbtgt account %s in KDC backend: got %d\n",
3623 ldb_dn_get_linearized(kdc_db_ctx
->krbtgt_dn
),
3625 talloc_free(kdc_db_ctx
);
3626 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3628 kdc_db_ctx
->my_krbtgt_number
= my_krbtgt_number
;
3631 kdc_db_ctx
->my_krbtgt_number
= 0;
3632 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
, kdc_db_ctx
,
3634 ldb_get_default_basedn(kdc_db_ctx
->samdb
),
3637 DSDB_SEARCH_NO_GLOBAL_CATALOG
,
3638 "(&(objectClass=user)(samAccountName=krbtgt))");
3640 if (ldb_ret
!= LDB_SUCCESS
) {
3641 DBG_WARNING("could not find own KRBTGT in DB: %s\n", ldb_errstring(kdc_db_ctx
->samdb
));
3642 talloc_free(kdc_db_ctx
);
3643 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
3645 kdc_db_ctx
->krbtgt_dn
= talloc_steal(kdc_db_ctx
, msg
->dn
);
3646 kdc_db_ctx
->my_krbtgt_number
= 0;
3649 *kdc_db_ctx_out
= kdc_db_ctx
;
3650 return NT_STATUS_OK
;
3653 krb5_error_code
dsdb_extract_aes_256_key(krb5_context context
,
3654 TALLOC_CTX
*mem_ctx
,
3655 const struct ldb_message
*msg
,
3656 uint32_t user_account_control
,
3657 const uint32_t *kvno
,
3659 DATA_BLOB
*aes_256_key
,
3662 krb5_error_code krb5_ret
;
3663 uint32_t supported_enctypes
;
3664 unsigned flags
= SDB_F_GET_CLIENT
;
3665 struct sdb_entry sentry
= {};
3668 flags
|= SDB_F_KVNO_SPECIFIED
;
3671 krb5_ret
= samba_kdc_message2entry_keys(context
,
3674 false, /* is_krbtgt */
3675 false, /* is_rodc */
3676 user_account_control
,
3677 SAMBA_KDC_ENT_TYPE_CLIENT
,
3679 (kvno
!= NULL
) ? *kvno
: 0,
3681 ENC_HMAC_SHA1_96_AES256
,
3682 &supported_enctypes
);
3683 if (krb5_ret
!= 0) {
3684 DBG_ERR("Failed to parse supplementalCredentials "
3685 "of %s with %s kvno using "
3686 "ENCTYPE_HMAC_SHA1_96_AES256 "
3687 "Kerberos Key: %s\n",
3688 ldb_dn_get_linearized(msg
->dn
),
3689 (kvno
!= NULL
) ? "previous" : "current",
3690 krb5_get_error_message(context
,
3695 if ((supported_enctypes
& ENC_HMAC_SHA1_96_AES256
) == 0 ||
3696 sentry
.keys
.len
!= 1) {
3697 DBG_INFO("Failed to find a ENCTYPE_HMAC_SHA1_96_AES256 "
3698 "key in supplementalCredentials "
3699 "of %s at KVNO %u (got %u keys, expected 1)\n",
3700 ldb_dn_get_linearized(msg
->dn
),
3703 sdb_entry_free(&sentry
);
3707 if (sentry
.keys
.val
[0].salt
== NULL
) {
3708 DBG_INFO("Failed to find a salt in "
3709 "supplementalCredentials "
3710 "of %s at KVNO %u\n",
3711 ldb_dn_get_linearized(msg
->dn
),
3713 sdb_entry_free(&sentry
);
3717 if (aes_256_key
!= NULL
) {
3718 *aes_256_key
= data_blob_talloc(mem_ctx
,
3719 KRB5_KEY_DATA(&sentry
.keys
.val
[0].key
),
3720 KRB5_KEY_LENGTH(&sentry
.keys
.val
[0].key
));
3721 if (aes_256_key
->data
== NULL
) {
3722 sdb_entry_free(&sentry
);
3725 talloc_keep_secret(aes_256_key
->data
);
3729 *salt
= data_blob_talloc(mem_ctx
,
3730 sentry
.keys
.val
[0].salt
->salt
.data
,
3731 sentry
.keys
.val
[0].salt
->salt
.length
);
3732 if (salt
->data
== NULL
) {
3733 sdb_entry_free(&sentry
);
3738 if (kvno_out
!= NULL
) {
3739 *kvno_out
= sentry
.kvno
;
3742 sdb_entry_free(&sentry
);