2 Unix SMB/CIFS implementation.
4 Database Glue between Samba and the KDC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2009
7 Copyright (C) Simo Sorce <idra@samba.org> 2010
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "libcli/security/security.h"
26 #include "auth/auth.h"
27 #include "auth/auth_sam.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "dsdb/common/util.h"
30 #include "librpc/gen_ndr/ndr_drsblobs.h"
31 #include "param/param.h"
32 #include "../lib/crypto/md4.h"
33 #include "system/kerberos.h"
34 #include "auth/kerberos/kerberos.h"
36 #include "kdc/samba_kdc.h"
37 #include "kdc/kdc-glue.h"
38 #include "kdc/kdc-policy.h"
39 #include "kdc/db-glue.h"
41 #define SAMBA_KVNO_GET_KRBTGT(kvno) \
42 ((uint16_t)(((uint32_t)kvno) >> 16))
44 #define SAMBA_KVNO_AND_KRBTGT(kvno, krbtgt) \
45 ((krb5_kvno)((((uint32_t)kvno) & 0xFFFF) | \
46 ((((uint32_t)krbtgt) << 16) & 0xFFFF0000)))
48 enum samba_kdc_ent_type
49 { SAMBA_KDC_ENT_TYPE_CLIENT
, SAMBA_KDC_ENT_TYPE_SERVER
,
50 SAMBA_KDC_ENT_TYPE_KRBTGT
, SAMBA_KDC_ENT_TYPE_TRUST
, SAMBA_KDC_ENT_TYPE_ANY
};
52 enum trust_direction
{
54 INBOUND
= LSA_TRUST_DIRECTION_INBOUND
,
55 OUTBOUND
= LSA_TRUST_DIRECTION_OUTBOUND
58 static const char *trust_attrs
[] = {
63 "msDS-SupportedEncryptionTypes",
70 static KerberosTime
ldb_msg_find_krb5time_ldap_time(struct ldb_message
*msg
, const char *attr
, KerberosTime default_val
)
76 gentime
= ldb_msg_find_attr_as_string(msg
, attr
, NULL
);
80 tmp
= strptime(gentime
, "%Y%m%d%H%M%SZ", &tm
);
88 static HDBFlags
uf2HDBFlags(krb5_context context
, uint32_t userAccountControl
, enum samba_kdc_ent_type ent_type
)
90 HDBFlags flags
= int2HDBFlags(0);
92 /* we don't allow kadmin deletes */
95 /* mark the principal as invalid to start with */
100 /* All accounts are servers, but this may be disabled again in the caller */
103 /* Account types - clear the invalid bit if it turns out to be valid */
104 if (userAccountControl
& UF_NORMAL_ACCOUNT
) {
105 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
111 if (userAccountControl
& UF_INTERDOMAIN_TRUST_ACCOUNT
) {
112 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
117 if (userAccountControl
& UF_WORKSTATION_TRUST_ACCOUNT
) {
118 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
123 if (userAccountControl
& UF_SERVER_TRUST_ACCOUNT
) {
124 if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
|| ent_type
== SAMBA_KDC_ENT_TYPE_ANY
) {
130 /* Not permitted to act as a client if disabled */
131 if (userAccountControl
& UF_ACCOUNTDISABLE
) {
134 if (userAccountControl
& UF_LOCKOUT
) {
138 if (userAccountControl & UF_PASSWORD_NOTREQD) {
143 UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
145 if (userAccountControl
& UF_TEMP_DUPLICATE_ACCOUNT
) {
149 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in samba_kdc_message2entry() */
152 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
156 if (userAccountControl
& UF_SMARTCARD_REQUIRED
) {
157 flags
.require_hwauth
= 1;
159 if (userAccountControl
& UF_TRUSTED_FOR_DELEGATION
) {
160 flags
.ok_as_delegate
= 1;
162 if (userAccountControl
& UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION
) {
164 * this is confusing...
166 * UF_TRUSTED_FOR_DELEGATION
171 * UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION
172 * => trusted_for_delegation
174 flags
.trusted_for_delegation
= 1;
176 if (!(userAccountControl
& UF_NOT_DELEGATED
)) {
177 flags
.forwardable
= 1;
181 if (userAccountControl
& UF_DONT_REQUIRE_PREAUTH
) {
182 flags
.require_preauth
= 0;
184 flags
.require_preauth
= 1;
190 static int samba_kdc_entry_destructor(struct samba_kdc_entry
*p
)
192 hdb_entry_ex
*entry_ex
= p
->entry_ex
;
193 free_hdb_entry(&entry_ex
->entry
);
197 static void samba_kdc_free_entry(krb5_context context
, hdb_entry_ex
*entry_ex
)
199 /* this function is called only from hdb_free_entry().
200 * Make sure we neutralize the destructor or we will
201 * get a double free later when hdb_free_entry() will
202 * try to call free_hdb_entry() */
203 talloc_set_destructor(entry_ex
->ctx
, NULL
);
205 /* now proceed to free the talloc part */
206 talloc_free(entry_ex
->ctx
);
209 static krb5_error_code
samba_kdc_message2entry_keys(krb5_context context
,
210 struct samba_kdc_db_context
*kdc_db_ctx
,
212 struct ldb_message
*msg
,
215 uint32_t userAccountControl
,
216 enum samba_kdc_ent_type ent_type
,
217 hdb_entry_ex
*entry_ex
)
219 krb5_error_code ret
= 0;
220 enum ndr_err_code ndr_err
;
221 struct samr_Password
*hash
;
222 const struct ldb_val
*sc_val
;
223 struct supplementalCredentialsBlob scb
;
224 struct supplementalCredentialsPackage
*scpk
= NULL
;
225 bool newer_keys
= false;
226 struct package_PrimaryKerberosBlob _pkb
;
227 struct package_PrimaryKerberosCtr3
*pkb3
= NULL
;
228 struct package_PrimaryKerberosCtr4
*pkb4
= NULL
;
230 uint16_t allocated_keys
= 0;
231 int rodc_krbtgt_number
= 0;
233 uint32_t supported_enctypes
234 = ldb_msg_find_attr_as_uint(msg
,
235 "msDS-SupportedEncryptionTypes",
238 if (rid
== DOMAIN_RID_KRBTGT
|| is_rodc
) {
239 /* KDCs (and KDCs on RODCs) use AES */
240 supported_enctypes
|= ENC_HMAC_SHA1_96_AES128
| ENC_HMAC_SHA1_96_AES256
;
241 } else if (userAccountControl
& (UF_PARTIAL_SECRETS_ACCOUNT
|UF_SERVER_TRUST_ACCOUNT
)) {
242 /* DCs and RODCs comptuer accounts use AES */
243 supported_enctypes
|= ENC_HMAC_SHA1_96_AES128
| ENC_HMAC_SHA1_96_AES256
;
244 } else if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
||
245 (ent_type
== SAMBA_KDC_ENT_TYPE_ANY
)) {
246 /* for AS-REQ the client chooses the enc types it
247 * supports, and this will vary between computers a
250 * likewise for 'any' return as much as is supported,
251 * to export into a keytab */
252 supported_enctypes
= ENC_ALL_TYPES
;
255 /* If UF_USE_DES_KEY_ONLY has been set, then don't allow use of the newer enc types */
256 if (userAccountControl
& UF_USE_DES_KEY_ONLY
) {
257 supported_enctypes
= ENC_CRC32
|ENC_RSA_MD5
;
259 /* Otherwise, add in the default enc types */
260 supported_enctypes
|= ENC_CRC32
| ENC_RSA_MD5
| ENC_RC4_HMAC_MD5
;
263 /* Is this the krbtgt or a RODC krbtgt */
265 rodc_krbtgt_number
= ldb_msg_find_attr_as_int(msg
, "msDS-SecondaryKrbTgtNumber", -1);
267 if (rodc_krbtgt_number
== -1) {
272 entry_ex
->entry
.keys
.val
= NULL
;
273 entry_ex
->entry
.keys
.len
= 0;
275 kvno
= ldb_msg_find_attr_as_int(msg
, "msDS-KeyVersionNumber", 0);
277 kvno
= SAMBA_KVNO_AND_KRBTGT(kvno
, rodc_krbtgt_number
);
279 entry_ex
->entry
.kvno
= kvno
;
281 /* Get keys from the db */
283 hash
= samdb_result_hash(mem_ctx
, msg
, "unicodePwd");
284 sc_val
= ldb_msg_find_ldb_val(msg
, "supplementalCredentials");
286 /* unicodePwd for enctype 0x17 (23) if present */
291 /* supplementalCredentials if present */
293 ndr_err
= ndr_pull_struct_blob_all(sc_val
, mem_ctx
, &scb
,
294 (ndr_pull_flags_fn_t
)ndr_pull_supplementalCredentialsBlob
);
295 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
296 dump_data(0, sc_val
->data
, sc_val
->length
);
301 if (scb
.sub
.signature
!= SUPPLEMENTAL_CREDENTIALS_SIGNATURE
) {
302 NDR_PRINT_DEBUG(supplementalCredentialsBlob
, &scb
);
307 for (i
=0; i
< scb
.sub
.num_packages
; i
++) {
308 if (strcmp("Primary:Kerberos-Newer-Keys", scb
.sub
.packages
[i
].name
) == 0) {
309 scpk
= &scb
.sub
.packages
[i
];
310 if (!scpk
->data
|| !scpk
->data
[0]) {
316 } else if (strcmp("Primary:Kerberos", scb
.sub
.packages
[i
].name
) == 0) {
317 scpk
= &scb
.sub
.packages
[i
];
318 if (!scpk
->data
|| !scpk
->data
[0]) {
322 * we don't break here in hope to find
323 * a Kerberos-Newer-Keys package
329 * Primary:Kerberos-Newer-Keys or Primary:Kerberos element
330 * of supplementalCredentials
335 blob
= strhex_to_data_blob(mem_ctx
, scpk
->data
);
341 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
342 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, &_pkb
,
343 (ndr_pull_flags_fn_t
)ndr_pull_package_PrimaryKerberosBlob
);
344 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
346 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
347 krb5_warnx(context
, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
351 if (newer_keys
&& _pkb
.version
!= 4) {
353 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
354 krb5_warnx(context
, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
358 if (!newer_keys
&& _pkb
.version
!= 3) {
360 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
361 krb5_warnx(context
, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
365 if (_pkb
.version
== 4) {
366 pkb4
= &_pkb
.ctr
.ctr4
;
367 allocated_keys
+= pkb4
->num_keys
;
368 } else if (_pkb
.version
== 3) {
369 pkb3
= &_pkb
.ctr
.ctr3
;
370 allocated_keys
+= pkb3
->num_keys
;
374 if (allocated_keys
== 0) {
375 if (kdc_db_ctx
->rodc
) {
376 /* We are on an RODC, but don't have keys for this account. Signal this to the caller */
377 return HDB_ERR_NOT_FOUND_HERE
;
380 /* oh, no password. Apparently (comment in
381 * hdb-ldap.c) this violates the ASN.1, but this
382 * allows an entry with no keys (yet). */
386 /* allocate space to decode into */
387 entry_ex
->entry
.keys
.len
= 0;
388 entry_ex
->entry
.keys
.val
= calloc(allocated_keys
, sizeof(Key
));
389 if (entry_ex
->entry
.keys
.val
== NULL
) {
394 if (hash
&& (supported_enctypes
& ENC_RC4_HMAC_MD5
)) {
398 key
.salt
= NULL
; /* No salt for this enc type */
400 ret
= krb5_keyblock_init(context
,
401 ENCTYPE_ARCFOUR_HMAC
,
402 hash
->hash
, sizeof(hash
->hash
),
408 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
409 entry_ex
->entry
.keys
.len
++;
413 for (i
=0; i
< pkb4
->num_keys
; i
++) {
416 if (!pkb4
->keys
[i
].value
) continue;
418 if (!(kerberos_enctype_to_bitmap(pkb4
->keys
[i
].keytype
) & supported_enctypes
)) {
425 if (pkb4
->salt
.string
) {
428 salt
= data_blob_string_const(pkb4
->salt
.string
);
430 key
.salt
= calloc(1, sizeof(*key
.salt
));
431 if (key
.salt
== NULL
) {
436 key
.salt
->type
= hdb_pw_salt
;
438 ret
= krb5_data_copy(&key
.salt
->salt
, salt
.data
, salt
.length
);
446 /* TODO: maybe pass the iteration_count somehow... */
448 ret
= krb5_keyblock_init(context
,
449 pkb4
->keys
[i
].keytype
,
450 pkb4
->keys
[i
].value
->data
,
451 pkb4
->keys
[i
].value
->length
,
453 if (ret
== KRB5_PROG_ETYPE_NOSUPP
) {
454 DEBUG(2,("Unsupported keytype ignored - type %u\n",
455 pkb4
->keys
[i
].keytype
));
468 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
469 entry_ex
->entry
.keys
.len
++;
472 for (i
=0; i
< pkb3
->num_keys
; i
++) {
475 if (!pkb3
->keys
[i
].value
) continue;
477 if (!(kerberos_enctype_to_bitmap(pkb3
->keys
[i
].keytype
) & supported_enctypes
)) {
484 if (pkb3
->salt
.string
) {
487 salt
= data_blob_string_const(pkb3
->salt
.string
);
489 key
.salt
= calloc(1, sizeof(*key
.salt
));
490 if (key
.salt
== NULL
) {
495 key
.salt
->type
= hdb_pw_salt
;
497 ret
= krb5_data_copy(&key
.salt
->salt
, salt
.data
, salt
.length
);
505 ret
= krb5_keyblock_init(context
,
506 pkb3
->keys
[i
].keytype
,
507 pkb3
->keys
[i
].value
->data
,
508 pkb3
->keys
[i
].value
->length
,
519 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
520 entry_ex
->entry
.keys
.len
++;
526 entry_ex
->entry
.keys
.len
= 0;
528 if (entry_ex
->entry
.keys
.len
== 0 && entry_ex
->entry
.keys
.val
) {
529 free(entry_ex
->entry
.keys
.val
);
530 entry_ex
->entry
.keys
.val
= NULL
;
536 * Construct an hdb_entry from a directory entry.
538 static krb5_error_code
samba_kdc_message2entry(krb5_context context
,
539 struct samba_kdc_db_context
*kdc_db_ctx
,
540 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
541 enum samba_kdc_ent_type ent_type
,
543 struct ldb_dn
*realm_dn
,
544 struct ldb_message
*msg
,
545 hdb_entry_ex
*entry_ex
)
547 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
548 uint32_t userAccountControl
;
550 krb5_error_code ret
= 0;
551 krb5_boolean is_computer
= FALSE
;
553 struct samba_kdc_entry
*p
;
558 bool is_rodc
= false;
559 struct ldb_message_element
*objectclasses
;
560 struct ldb_val computer_val
;
561 const char *samAccountName
= ldb_msg_find_attr_as_string(msg
, "samAccountName", NULL
);
562 computer_val
.data
= discard_const_p(uint8_t,"computer");
563 computer_val
.length
= strlen((const char *)computer_val
.data
);
565 if (ldb_msg_find_element(msg
, "msDS-SecondaryKrbTgtNumber")) {
569 if (!samAccountName
) {
571 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: no samAccountName present");
575 objectclasses
= ldb_msg_find_element(msg
, "objectClass");
577 if (objectclasses
&& ldb_msg_find_val(objectclasses
, &computer_val
)) {
581 memset(entry_ex
, 0, sizeof(*entry_ex
));
583 p
= talloc(mem_ctx
, struct samba_kdc_entry
);
589 p
->kdc_db_ctx
= kdc_db_ctx
;
590 p
->entry_ex
= entry_ex
;
591 p
->realm_dn
= talloc_reference(p
, realm_dn
);
597 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
599 /* make sure we do not have bogus data in there */
600 memset(&entry_ex
->entry
, 0, sizeof(hdb_entry
));
603 entry_ex
->free_entry
= samba_kdc_free_entry
;
605 userAccountControl
= ldb_msg_find_attr_as_uint(msg
, "userAccountControl", 0);
608 entry_ex
->entry
.principal
= malloc(sizeof(*(entry_ex
->entry
.principal
)));
609 if (ent_type
== SAMBA_KDC_ENT_TYPE_ANY
&& principal
== NULL
) {
610 krb5_make_principal(context
, &entry_ex
->entry
.principal
, lpcfg_realm(lp_ctx
), samAccountName
, NULL
);
612 ret
= copy_Principal(principal
, entry_ex
->entry
.principal
);
614 krb5_clear_error_message(context
);
618 /* While we have copied the client principal, tests
619 * show that Win2k3 returns the 'corrected' realm, not
620 * the client-specified realm. This code attempts to
621 * replace the client principal's realm with the one
622 * we determine from our records */
624 /* this has to be with malloc() */
625 krb5_principal_set_realm(context
, entry_ex
->entry
.principal
, lpcfg_realm(lp_ctx
));
628 /* First try and figure out the flags based on the userAccountControl */
629 entry_ex
->entry
.flags
= uf2HDBFlags(context
, userAccountControl
, ent_type
);
631 /* Windows 2008 seems to enforce this (very sensible) rule by
632 * default - don't allow offline attacks on a user's password
633 * by asking for a ticket to them as a service (encrypted with
634 * their probably patheticly insecure password) */
636 if (entry_ex
->entry
.flags
.server
637 && lpcfg_parm_bool(lp_ctx
, NULL
, "kdc", "require spn for service", true)) {
638 if (!is_computer
&& !ldb_msg_find_attr_as_string(msg
, "servicePrincipalName", NULL
)) {
639 entry_ex
->entry
.flags
.server
= 0;
643 if (flags
& HDB_F_ADMIN_DATA
) {
644 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
645 * of the Heimdal KDC. They are stored in a the traditional
646 * DB for audit purposes, and still form part of the structure
649 /* use 'whenCreated' */
650 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
651 /* use 'kadmin' for now (needed by mit_samba) */
652 krb5_make_principal(context
,
653 &entry_ex
->entry
.created_by
.principal
,
654 lpcfg_realm(lp_ctx
), "kadmin", NULL
);
656 entry_ex
->entry
.modified_by
= (Event
*) malloc(sizeof(Event
));
657 if (entry_ex
->entry
.modified_by
== NULL
) {
659 krb5_set_error_message(context
, ret
, "malloc: out of memory");
663 /* use 'whenChanged' */
664 entry_ex
->entry
.modified_by
->time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenChanged", 0);
665 /* use 'kadmin' for now (needed by mit_samba) */
666 krb5_make_principal(context
,
667 &entry_ex
->entry
.modified_by
->principal
,
668 lpcfg_realm(lp_ctx
), "kadmin", NULL
);
672 /* The lack of password controls etc applies to krbtgt by
673 * virtue of being that particular RID */
674 status
= dom_sid_split_rid(NULL
, samdb_result_dom_sid(mem_ctx
, msg
, "objectSid"), NULL
, &rid
);
676 if (!NT_STATUS_IS_OK(status
)) {
681 if (rid
== DOMAIN_RID_KRBTGT
) {
682 entry_ex
->entry
.valid_end
= NULL
;
683 entry_ex
->entry
.pw_end
= NULL
;
685 entry_ex
->entry
.flags
.invalid
= 0;
686 entry_ex
->entry
.flags
.server
= 1;
688 /* Don't mark all requests for the krbtgt/realm as
689 * 'change password', as otherwise we could get into
690 * trouble, and not enforce the password expirty.
691 * Instead, only do it when request is for the kpasswd service */
692 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
693 && principal
->name
.name_string
.len
== 2
694 && (strcmp(principal
->name
.name_string
.val
[0], "kadmin") == 0)
695 && (strcmp(principal
->name
.name_string
.val
[1], "changepw") == 0)
696 && lpcfg_is_my_domain_or_realm(lp_ctx
, principal
->realm
)) {
697 entry_ex
->entry
.flags
.change_pw
= 1;
699 entry_ex
->entry
.flags
.client
= 0;
700 entry_ex
->entry
.flags
.forwardable
= 1;
701 entry_ex
->entry
.flags
.ok_as_delegate
= 1;
702 } else if (is_rodc
) {
703 /* The RODC krbtgt account is like the main krbtgt,
704 * but it does not have a changepw or kadmin
707 entry_ex
->entry
.valid_end
= NULL
;
708 entry_ex
->entry
.pw_end
= NULL
;
710 /* Also don't allow the RODC krbtgt to be a client (it should not be needed) */
711 entry_ex
->entry
.flags
.client
= 0;
712 entry_ex
->entry
.flags
.invalid
= 0;
713 entry_ex
->entry
.flags
.server
= 1;
715 entry_ex
->entry
.flags
.client
= 0;
716 entry_ex
->entry
.flags
.forwardable
= 1;
717 entry_ex
->entry
.flags
.ok_as_delegate
= 0;
718 } else if (entry_ex
->entry
.flags
.server
&& ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
719 /* The account/password expiry only applies when the account is used as a
720 * client (ie password login), not when used as a server */
722 /* Make very well sure we don't use this for a client,
723 * it could bypass the password restrictions */
724 entry_ex
->entry
.flags
.client
= 0;
726 entry_ex
->entry
.valid_end
= NULL
;
727 entry_ex
->entry
.pw_end
= NULL
;
730 NTTIME must_change_time
731 = samdb_result_force_password_change(kdc_db_ctx
->samdb
, mem_ctx
,
733 if (must_change_time
== 0x7FFFFFFFFFFFFFFFULL
) {
734 entry_ex
->entry
.pw_end
= NULL
;
736 entry_ex
->entry
.pw_end
= malloc(sizeof(*entry_ex
->entry
.pw_end
));
737 if (entry_ex
->entry
.pw_end
== NULL
) {
741 *entry_ex
->entry
.pw_end
= nt_time_to_unix(must_change_time
);
744 acct_expiry
= samdb_result_account_expires(msg
);
745 if (acct_expiry
== 0x7FFFFFFFFFFFFFFFULL
) {
746 entry_ex
->entry
.valid_end
= NULL
;
748 entry_ex
->entry
.valid_end
= malloc(sizeof(*entry_ex
->entry
.valid_end
));
749 if (entry_ex
->entry
.valid_end
== NULL
) {
753 *entry_ex
->entry
.valid_end
= nt_time_to_unix(acct_expiry
);
757 entry_ex
->entry
.valid_start
= NULL
;
759 entry_ex
->entry
.max_life
= malloc(sizeof(*entry_ex
->entry
.max_life
));
760 if (entry_ex
->entry
.max_life
== NULL
) {
765 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
766 *entry_ex
->entry
.max_life
= nt_time_to_unix(kdc_db_ctx
->policy
.service_tkt_lifetime
);
767 } else if (ent_type
== SAMBA_KDC_ENT_TYPE_KRBTGT
|| ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
) {
768 *entry_ex
->entry
.max_life
= nt_time_to_unix(kdc_db_ctx
->policy
.user_tkt_lifetime
);
770 *entry_ex
->entry
.max_life
= MIN(nt_time_to_unix(kdc_db_ctx
->policy
.service_tkt_lifetime
),
771 nt_time_to_unix(kdc_db_ctx
->policy
.user_tkt_lifetime
));
774 entry_ex
->entry
.max_renew
= malloc(sizeof(*entry_ex
->entry
.max_life
));
775 if (entry_ex
->entry
.max_renew
== NULL
) {
780 *entry_ex
->entry
.max_renew
= nt_time_to_unix(kdc_db_ctx
->policy
.user_tkt_renewaltime
);
782 entry_ex
->entry
.generation
= NULL
;
784 /* Get keys from the db */
785 ret
= samba_kdc_message2entry_keys(context
, kdc_db_ctx
, p
, msg
,
786 rid
, is_rodc
, userAccountControl
,
789 /* Could be bougus data in the entry, or out of memory */
793 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
794 if (entry_ex
->entry
.etypes
== NULL
) {
795 krb5_clear_error_message(context
);
799 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
800 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
801 if (entry_ex
->entry
.etypes
->val
== NULL
) {
802 krb5_clear_error_message(context
);
806 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
807 entry_ex
->entry
.etypes
->val
[i
] = entry_ex
->entry
.keys
.val
[i
].key
.keytype
;
811 p
->msg
= talloc_steal(p
, msg
);
815 /* This doesn't free ent itself, that is for the eventual caller to do */
816 hdb_free_entry(context
, entry_ex
);
818 talloc_steal(kdc_db_ctx
, entry_ex
->ctx
);
825 * Construct an hdb_entry from a directory entry.
827 static krb5_error_code
samba_kdc_trust_message2entry(krb5_context context
,
828 struct samba_kdc_db_context
*kdc_db_ctx
,
829 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
830 enum trust_direction direction
,
831 struct ldb_dn
*realm_dn
,
832 struct ldb_message
*msg
,
833 hdb_entry_ex
*entry_ex
)
835 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
836 const char *dnsdomain
;
837 const char *realm
= lpcfg_realm(lp_ctx
);
838 DATA_BLOB password_utf16
;
839 struct samr_Password password_hash
;
840 const struct ldb_val
*password_val
;
841 struct trustAuthInOutBlob password_blob
;
842 struct samba_kdc_entry
*p
;
844 enum ndr_err_code ndr_err
;
845 int ret
, trust_direction_flags
;
848 p
= talloc(mem_ctx
, struct samba_kdc_entry
);
854 p
->kdc_db_ctx
= kdc_db_ctx
;
855 p
->entry_ex
= entry_ex
;
856 p
->realm_dn
= realm_dn
;
858 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
860 /* make sure we do not have bogus data in there */
861 memset(&entry_ex
->entry
, 0, sizeof(hdb_entry
));
864 entry_ex
->free_entry
= samba_kdc_free_entry
;
866 /* use 'whenCreated' */
867 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
868 /* use 'kadmin' for now (needed by mit_samba) */
869 krb5_make_principal(context
,
870 &entry_ex
->entry
.created_by
.principal
,
871 realm
, "kadmin", NULL
);
873 entry_ex
->entry
.valid_start
= NULL
;
875 trust_direction_flags
= ldb_msg_find_attr_as_int(msg
, "trustDirection", 0);
877 if (direction
== INBOUND
) {
878 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthIncoming");
880 } else { /* OUTBOUND */
881 dnsdomain
= ldb_msg_find_attr_as_string(msg
, "trustPartner", NULL
);
883 realm
= strupper_talloc(mem_ctx
, dnsdomain
);
884 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthOutgoing");
887 if (!password_val
|| !(trust_direction_flags
& direction
)) {
892 ndr_err
= ndr_pull_struct_blob(password_val
, mem_ctx
, &password_blob
,
893 (ndr_pull_flags_fn_t
)ndr_pull_trustAuthInOutBlob
);
894 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
899 entry_ex
->entry
.kvno
= -1;
900 for (i
=0; i
< password_blob
.count
; i
++) {
901 if (password_blob
.current
.array
[i
].AuthType
== TRUST_AUTH_TYPE_VERSION
) {
902 entry_ex
->entry
.kvno
= password_blob
.current
.array
[i
].AuthInfo
.version
.version
;
906 for (i
=0; i
< password_blob
.count
; i
++) {
907 if (password_blob
.current
.array
[i
].AuthType
== TRUST_AUTH_TYPE_CLEAR
) {
908 password_utf16
= data_blob_const(password_blob
.current
.array
[i
].AuthInfo
.clear
.password
,
909 password_blob
.current
.array
[i
].AuthInfo
.clear
.size
);
910 /* In the future, generate all sorts of
911 * hashes, but for now we can't safely convert
912 * the random strings windows uses into
915 /* but as it is utf16 already, we can get the NT password/arcfour-hmac-md5 key */
916 mdfour(password_hash
.hash
, password_utf16
.data
, password_utf16
.length
);
918 } else if (password_blob
.current
.array
[i
].AuthType
== TRUST_AUTH_TYPE_NT4OWF
) {
919 password_hash
= password_blob
.current
.array
[i
].AuthInfo
.nt4owf
.password
;
924 if (i
< password_blob
.count
) {
926 /* Must have found a cleartext or MD4 password */
927 entry_ex
->entry
.keys
.val
= calloc(1, sizeof(Key
));
930 key
.salt
= NULL
; /* No salt for this enc type */
932 if (entry_ex
->entry
.keys
.val
== NULL
) {
937 ret
= krb5_keyblock_init(context
,
938 ENCTYPE_ARCFOUR_HMAC
,
939 password_hash
.hash
, sizeof(password_hash
.hash
),
942 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
943 entry_ex
->entry
.keys
.len
++;
946 entry_ex
->entry
.principal
= malloc(sizeof(*(entry_ex
->entry
.principal
)));
948 ret
= copy_Principal(principal
, entry_ex
->entry
.principal
);
950 krb5_clear_error_message(context
);
954 /* While we have copied the client principal, tests
955 * show that Win2k3 returns the 'corrected' realm, not
956 * the client-specified realm. This code attempts to
957 * replace the client principal's realm with the one
958 * we determine from our records */
960 krb5_principal_set_realm(context
, entry_ex
->entry
.principal
, realm
);
961 entry_ex
->entry
.flags
= int2HDBFlags(0);
962 entry_ex
->entry
.flags
.immutable
= 1;
963 entry_ex
->entry
.flags
.invalid
= 0;
964 entry_ex
->entry
.flags
.server
= 1;
965 entry_ex
->entry
.flags
.require_preauth
= 1;
967 entry_ex
->entry
.pw_end
= NULL
;
969 entry_ex
->entry
.max_life
= NULL
;
971 entry_ex
->entry
.max_renew
= NULL
;
973 entry_ex
->entry
.generation
= NULL
;
975 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
976 if (entry_ex
->entry
.etypes
== NULL
) {
977 krb5_clear_error_message(context
);
981 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
982 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
983 if (entry_ex
->entry
.etypes
->val
== NULL
) {
984 krb5_clear_error_message(context
);
988 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
989 entry_ex
->entry
.etypes
->val
[i
] = entry_ex
->entry
.keys
.val
[i
].key
.keytype
;
993 p
->msg
= talloc_steal(p
, msg
);
997 /* This doesn't free ent itself, that is for the eventual caller to do */
998 hdb_free_entry(context
, entry_ex
);
1000 talloc_steal(kdc_db_ctx
, entry_ex
->ctx
);
1007 static krb5_error_code
samba_kdc_lookup_trust(krb5_context context
, struct ldb_context
*ldb_ctx
,
1008 TALLOC_CTX
*mem_ctx
,
1010 struct ldb_dn
*realm_dn
,
1011 struct ldb_message
**pmsg
)
1014 krb5_error_code ret
;
1015 char *filter
= NULL
;
1016 const char * const *attrs
= trust_attrs
;
1018 struct ldb_result
*res
= NULL
;
1019 char *realm_encoded
= ldb_binary_encode_string(mem_ctx
, realm
);
1020 if (!realm_encoded
) {
1023 krb5_set_error_message(context
, ret
, "talloc_asprintf: out of memory");
1027 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=trustedDomain)(|(flatname=%s)(trustPartner=%s)))",
1028 realm_encoded
, realm_encoded
);
1031 talloc_free(realm_encoded
);
1033 krb5_set_error_message(context
, ret
, "talloc_asprintf: out of memory");
1037 lret
= ldb_search(ldb_ctx
, mem_ctx
, &res
,
1038 ldb_get_default_basedn(ldb_ctx
),
1039 LDB_SCOPE_SUBTREE
, attrs
, "%s", filter
);
1040 if (lret
!= LDB_SUCCESS
) {
1041 DEBUG(3, ("Failed to search for %s: %s\n", filter
, ldb_errstring(ldb_ctx
)));
1042 return HDB_ERR_NOENTRY
;
1043 } else if (res
->count
== 0 || res
->count
> 1) {
1044 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter
, res
->count
));
1046 return HDB_ERR_NOENTRY
;
1048 talloc_steal(mem_ctx
, res
->msgs
);
1049 *pmsg
= res
->msgs
[0];
1054 static krb5_error_code
samba_kdc_lookup_client(krb5_context context
,
1055 struct samba_kdc_db_context
*kdc_db_ctx
,
1056 TALLOC_CTX
*mem_ctx
,
1057 krb5_const_principal principal
,
1059 struct ldb_dn
**realm_dn
,
1060 struct ldb_message
**msg
) {
1062 char *principal_string
;
1063 krb5_error_code ret
;
1065 ret
= krb5_unparse_name(context
, principal
, &principal_string
);
1071 nt_status
= sam_get_results_principal(kdc_db_ctx
->samdb
,
1072 mem_ctx
, principal_string
, attrs
,
1074 free(principal_string
);
1075 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
1076 return HDB_ERR_NOENTRY
;
1077 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_MEMORY
)) {
1079 } else if (!NT_STATUS_IS_OK(nt_status
)) {
1086 static krb5_error_code
samba_kdc_fetch_client(krb5_context context
,
1087 struct samba_kdc_db_context
*kdc_db_ctx
,
1088 TALLOC_CTX
*mem_ctx
,
1089 krb5_const_principal principal
,
1091 hdb_entry_ex
*entry_ex
) {
1092 struct ldb_dn
*realm_dn
;
1093 krb5_error_code ret
;
1094 struct ldb_message
*msg
= NULL
;
1096 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
1097 mem_ctx
, principal
, user_attrs
,
1103 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1104 principal
, SAMBA_KDC_ENT_TYPE_CLIENT
,
1106 realm_dn
, msg
, entry_ex
);
1110 static krb5_error_code
samba_kdc_fetch_krbtgt(krb5_context context
,
1111 struct samba_kdc_db_context
*kdc_db_ctx
,
1112 TALLOC_CTX
*mem_ctx
,
1113 krb5_const_principal principal
,
1115 uint32_t krbtgt_number
,
1116 hdb_entry_ex
*entry_ex
)
1118 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
1119 krb5_error_code ret
;
1120 struct ldb_message
*msg
= NULL
;
1121 struct ldb_dn
*realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
1123 krb5_principal alloc_principal
= NULL
;
1124 if (principal
->name
.name_string
.len
!= 2
1125 || (strcmp(principal
->name
.name_string
.val
[0], KRB5_TGS_NAME
) != 0)) {
1127 return HDB_ERR_NOENTRY
;
1130 /* krbtgt case. Either us or a trusted realm */
1132 if (lpcfg_is_my_domain_or_realm(lp_ctx
, principal
->realm
)
1133 && lpcfg_is_my_domain_or_realm(lp_ctx
, principal
->name
.name_string
.val
[1])) {
1134 /* us, or someone quite like us */
1135 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1136 * is in our db, then direct the caller at our primary
1141 if (krbtgt_number
== kdc_db_ctx
->my_krbtgt_number
) {
1142 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
,
1143 &msg
, kdc_db_ctx
->krbtgt_dn
, LDB_SCOPE_BASE
,
1145 "(objectClass=user)");
1147 /* We need to look up an RODC krbtgt (perhaps
1148 * ours, if we are an RODC, perhaps another
1149 * RODC if we are a read-write DC */
1150 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
,
1151 &msg
, realm_dn
, LDB_SCOPE_SUBTREE
,
1153 DSDB_SEARCH_SHOW_EXTENDED_DN
,
1154 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=%u))", (unsigned)(krbtgt_number
));
1157 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
1158 krb5_warnx(context
, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1159 (unsigned)(krbtgt_number
));
1160 krb5_set_error_message(context
, HDB_ERR_NOENTRY
,
1161 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1162 (unsigned)(krbtgt_number
));
1163 return HDB_ERR_NOENTRY
;
1164 } else if (lret
!= LDB_SUCCESS
) {
1165 krb5_warnx(context
, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1166 (unsigned)(krbtgt_number
));
1167 krb5_set_error_message(context
, HDB_ERR_NOENTRY
,
1168 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1169 (unsigned)(krbtgt_number
));
1170 return HDB_ERR_NOENTRY
;
1173 if (flags
& HDB_F_CANON
) {
1174 ret
= krb5_copy_principal(context
, principal
, &alloc_principal
);
1179 /* When requested to do so, ensure that the
1180 * both realm values in the principal are set
1181 * to the upper case, canonical realm */
1182 free(alloc_principal
->name
.name_string
.val
[1]);
1183 alloc_principal
->name
.name_string
.val
[1] = strdup(lpcfg_realm(lp_ctx
));
1184 if (!alloc_principal
->name
.name_string
.val
[1]) {
1186 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: strdup() failed!");
1189 principal
= alloc_principal
;
1192 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1193 principal
, SAMBA_KDC_ENT_TYPE_KRBTGT
,
1194 flags
, realm_dn
, msg
, entry_ex
);
1195 if (flags
& HDB_F_CANON
) {
1196 /* This is again copied in the message2entry call */
1197 krb5_free_principal(context
, alloc_principal
);
1200 krb5_warnx(context
, "samba_kdc_fetch: self krbtgt message2entry failed");
1205 enum trust_direction direction
= UNKNOWN
;
1206 const char *realm
= NULL
;
1208 /* Either an inbound or outbound trust */
1210 if (strcasecmp(lpcfg_realm(lp_ctx
), principal
->realm
) == 0) {
1211 /* look for inbound trust */
1212 direction
= INBOUND
;
1213 realm
= principal
->name
.name_string
.val
[1];
1214 } else if (strcasecmp(lpcfg_realm(lp_ctx
), principal
->name
.name_string
.val
[1]) == 0) {
1215 /* look for outbound trust */
1216 direction
= OUTBOUND
;
1217 realm
= principal
->realm
;
1219 krb5_warnx(context
, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1220 principal
->realm
, principal
->name
.name_string
.val
[1]);
1221 krb5_set_error_message(context
, HDB_ERR_NOENTRY
, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1222 principal
->realm
, principal
->name
.name_string
.val
[1]);
1223 return HDB_ERR_NOENTRY
;
1226 /* Trusted domains are under CN=system */
1228 ret
= samba_kdc_lookup_trust(context
, kdc_db_ctx
->samdb
,
1230 realm
, realm_dn
, &msg
);
1233 krb5_warnx(context
, "samba_kdc_fetch: could not find principal in DB");
1234 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: could not find principal in DB");
1238 ret
= samba_kdc_trust_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1239 principal
, direction
,
1240 realm_dn
, msg
, entry_ex
);
1242 krb5_warnx(context
, "samba_kdc_fetch: trust_message2entry failed");
1249 static krb5_error_code
samba_kdc_lookup_server(krb5_context context
,
1250 struct samba_kdc_db_context
*kdc_db_ctx
,
1251 TALLOC_CTX
*mem_ctx
,
1252 krb5_const_principal principal
,
1254 struct ldb_dn
**realm_dn
,
1255 struct ldb_message
**msg
)
1257 krb5_error_code ret
;
1258 if (principal
->name
.name_string
.len
>= 2) {
1259 /* 'normal server' case */
1262 struct ldb_dn
*user_dn
;
1263 char *principal_string
;
1265 ret
= krb5_unparse_name_flags(context
, principal
,
1266 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
1272 /* At this point we may find the host is known to be
1273 * in a different realm, so we should generate a
1274 * referral instead */
1275 nt_status
= crack_service_principal_name(kdc_db_ctx
->samdb
,
1276 mem_ctx
, principal_string
,
1277 &user_dn
, realm_dn
);
1278 free(principal_string
);
1280 if (!NT_STATUS_IS_OK(nt_status
)) {
1281 return HDB_ERR_NOENTRY
;
1284 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
,
1286 msg
, user_dn
, LDB_SCOPE_BASE
,
1287 attrs
, DSDB_SEARCH_SHOW_EXTENDED_DN
, "(objectClass=*)");
1288 if (ldb_ret
!= LDB_SUCCESS
) {
1289 return HDB_ERR_NOENTRY
;
1294 char *filter
= NULL
;
1297 /* server as client principal case, but we must not lookup userPrincipalNames */
1298 *realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
1299 realm
= krb5_principal_get_realm(context
, principal
);
1301 /* TODO: Check if it is our realm, otherwise give referall */
1303 ret
= krb5_unparse_name_flags(context
, principal
, KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &short_princ
);
1306 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_principal: could not parse principal");
1307 krb5_warnx(context
, "samba_kdc_lookup_principal: could not parse principal");
1311 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
, msg
,
1312 *realm_dn
, LDB_SCOPE_SUBTREE
,
1314 DSDB_SEARCH_SHOW_EXTENDED_DN
,
1315 "(&(objectClass=user)(samAccountName=%s))",
1316 ldb_binary_encode_string(mem_ctx
, short_princ
));
1318 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
1319 DEBUG(3, ("Failed find a entry for %s\n", filter
));
1320 return HDB_ERR_NOENTRY
;
1322 if (lret
!= LDB_SUCCESS
) {
1323 DEBUG(3, ("Failed single search for for %s - %s\n",
1324 filter
, ldb_errstring(kdc_db_ctx
->samdb
)));
1325 return HDB_ERR_NOENTRY
;
1332 static krb5_error_code
samba_kdc_fetch_server(krb5_context context
,
1333 struct samba_kdc_db_context
*kdc_db_ctx
,
1334 TALLOC_CTX
*mem_ctx
,
1335 krb5_const_principal principal
,
1337 hdb_entry_ex
*entry_ex
)
1339 krb5_error_code ret
;
1340 struct ldb_dn
*realm_dn
;
1341 struct ldb_message
*msg
;
1343 ret
= samba_kdc_lookup_server(context
, kdc_db_ctx
, mem_ctx
, principal
,
1344 server_attrs
, &realm_dn
, &msg
);
1349 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1350 principal
, SAMBA_KDC_ENT_TYPE_SERVER
,
1352 realm_dn
, msg
, entry_ex
);
1354 krb5_warnx(context
, "samba_kdc_fetch: message2entry failed");
1360 krb5_error_code
samba_kdc_fetch(krb5_context context
,
1361 struct samba_kdc_db_context
*kdc_db_ctx
,
1362 krb5_const_principal principal
,
1365 hdb_entry_ex
*entry_ex
)
1367 krb5_error_code ret
= HDB_ERR_NOENTRY
;
1368 TALLOC_CTX
*mem_ctx
;
1369 unsigned int krbtgt_number
;
1370 if (flags
& HDB_F_KVNO_SPECIFIED
) {
1371 krbtgt_number
= SAMBA_KVNO_GET_KRBTGT(kvno
);
1372 if (kdc_db_ctx
->rodc
) {
1373 if (krbtgt_number
!= kdc_db_ctx
->my_krbtgt_number
) {
1374 return HDB_ERR_NOT_FOUND_HERE
;
1378 krbtgt_number
= kdc_db_ctx
->my_krbtgt_number
;
1381 mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_fetch context");
1384 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
1388 if (flags
& HDB_F_GET_CLIENT
) {
1389 ret
= samba_kdc_fetch_client(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, entry_ex
);
1390 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1392 if (flags
& HDB_F_GET_SERVER
) {
1393 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
1394 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, krbtgt_number
, entry_ex
);
1395 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1397 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
1398 ret
= samba_kdc_fetch_server(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, entry_ex
);
1399 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1401 if (flags
& HDB_F_GET_KRBTGT
) {
1402 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, krbtgt_number
, entry_ex
);
1403 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1407 talloc_free(mem_ctx
);
1411 struct samba_kdc_seq
{
1414 struct ldb_message
**msgs
;
1415 struct ldb_dn
*realm_dn
;
1418 static krb5_error_code
samba_kdc_seq(krb5_context context
,
1419 struct samba_kdc_db_context
*kdc_db_ctx
,
1420 hdb_entry_ex
*entry
)
1422 krb5_error_code ret
;
1423 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
1424 TALLOC_CTX
*mem_ctx
;
1425 hdb_entry_ex entry_ex
;
1426 memset(&entry_ex
, '\0', sizeof(entry_ex
));
1429 return HDB_ERR_NOENTRY
;
1432 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_seq context");
1436 krb5_set_error_message(context
, ret
, "samba_kdc_seq: talloc_named() failed!");
1440 if (priv
->index
< priv
->count
) {
1441 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1442 NULL
, SAMBA_KDC_ENT_TYPE_ANY
,
1443 HDB_F_ADMIN_DATA
|HDB_F_GET_ANY
,
1444 priv
->realm_dn
, priv
->msgs
[priv
->index
++], entry
);
1446 ret
= HDB_ERR_NOENTRY
;
1451 kdc_db_ctx
->seq_ctx
= NULL
;
1453 talloc_free(mem_ctx
);
1459 krb5_error_code
samba_kdc_firstkey(krb5_context context
,
1460 struct samba_kdc_db_context
*kdc_db_ctx
,
1461 hdb_entry_ex
*entry
)
1463 struct ldb_context
*ldb_ctx
= kdc_db_ctx
->samdb
;
1464 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
1466 struct ldb_result
*res
= NULL
;
1467 krb5_error_code ret
;
1468 TALLOC_CTX
*mem_ctx
;
1473 kdc_db_ctx
->seq_ctx
= NULL
;
1476 priv
= (struct samba_kdc_seq
*) talloc(kdc_db_ctx
, struct samba_kdc_seq
);
1479 krb5_set_error_message(context
, ret
, "talloc: out of memory");
1485 priv
->realm_dn
= ldb_get_default_basedn(ldb_ctx
);
1488 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_firstkey context");
1492 krb5_set_error_message(context
, ret
, "samba_kdc_firstkey: talloc_named() failed!");
1496 ret
= krb5_get_default_realm(context
, &realm
);
1502 lret
= ldb_search(ldb_ctx
, priv
, &res
,
1503 priv
->realm_dn
, LDB_SCOPE_SUBTREE
, user_attrs
,
1504 "(objectClass=user)");
1506 if (lret
!= LDB_SUCCESS
) {
1508 return HDB_ERR_NOENTRY
;
1511 priv
->count
= res
->count
;
1512 priv
->msgs
= talloc_steal(priv
, res
->msgs
);
1515 kdc_db_ctx
->seq_ctx
= priv
;
1517 ret
= samba_kdc_seq(context
, kdc_db_ctx
, entry
);
1521 kdc_db_ctx
->seq_ctx
= NULL
;
1523 talloc_free(mem_ctx
);
1528 krb5_error_code
samba_kdc_nextkey(krb5_context context
,
1529 struct samba_kdc_db_context
*kdc_db_ctx
,
1530 hdb_entry_ex
*entry
)
1532 return samba_kdc_seq(context
, kdc_db_ctx
, entry
);
1535 /* Check if a given entry may delegate or do s4u2self to this target principal
1537 * This is currently a very nasty hack - allowing only delegation to itself.
1540 samba_kdc_check_s4u2self(krb5_context context
,
1541 struct samba_kdc_db_context
*kdc_db_ctx
,
1542 hdb_entry_ex
*entry
,
1543 krb5_const_principal target_principal
)
1545 krb5_error_code ret
;
1546 krb5_principal enterprise_prinicpal
= NULL
;
1547 struct ldb_dn
*realm_dn
;
1548 struct ldb_message
*msg
;
1549 struct dom_sid
*orig_sid
;
1550 struct dom_sid
*target_sid
;
1551 struct samba_kdc_entry
*p
= talloc_get_type(entry
->ctx
, struct samba_kdc_entry
);
1552 const char *delegation_check_attrs
[] = {
1556 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_s4u2self");
1560 krb5_set_error_message(context
, ret
, "samba_kdc_check_s4u2self: talloc_named() failed!");
1564 if (target_principal
->name
.name_type
== KRB5_NT_ENTERPRISE_PRINCIPAL
) {
1565 /* Need to reparse the enterprise principal to find the real target */
1566 if (target_principal
->name
.name_string
.len
!= 1) {
1567 ret
= KRB5_PARSE_MALFORMED
;
1568 krb5_set_error_message(context
, ret
, "samba_kdc_check_s4u2self: request for delegation to enterprise principal with wrong (%d) number of components",
1569 target_principal
->name
.name_string
.len
);
1570 talloc_free(mem_ctx
);
1573 ret
= krb5_parse_name(context
, target_principal
->name
.name_string
.val
[0],
1574 &enterprise_prinicpal
);
1576 talloc_free(mem_ctx
);
1579 target_principal
= enterprise_prinicpal
;
1582 ret
= samba_kdc_lookup_server(context
, kdc_db_ctx
, mem_ctx
, target_principal
,
1583 delegation_check_attrs
, &realm_dn
, &msg
);
1585 krb5_free_principal(context
, enterprise_prinicpal
);
1588 talloc_free(mem_ctx
);
1592 orig_sid
= samdb_result_dom_sid(mem_ctx
, p
->msg
, "objectSid");
1593 target_sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
1595 /* Allow delegation to the same principal, even if by a different
1596 * name. The easy and safe way to prove this is by SID
1598 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
1599 talloc_free(mem_ctx
);
1600 return KRB5KDC_ERR_BADOPTION
;
1603 talloc_free(mem_ctx
);
1607 /* Certificates printed by a the Certificate Authority might have a
1608 * slightly different form of the user principal name to that in the
1609 * database. Allow a mismatch where they both refer to the same
1613 samba_kdc_check_pkinit_ms_upn_match(krb5_context context
,
1614 struct samba_kdc_db_context
*kdc_db_ctx
,
1615 hdb_entry_ex
*entry
,
1616 krb5_const_principal certificate_principal
)
1618 krb5_error_code ret
;
1619 struct ldb_dn
*realm_dn
;
1620 struct ldb_message
*msg
;
1621 struct dom_sid
*orig_sid
;
1622 struct dom_sid
*target_sid
;
1623 struct samba_kdc_entry
*p
= talloc_get_type(entry
->ctx
, struct samba_kdc_entry
);
1624 const char *ms_upn_check_attrs
[] = {
1628 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_pkinit_ms_upn_match");
1632 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
1636 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
1637 mem_ctx
, certificate_principal
,
1638 ms_upn_check_attrs
, &realm_dn
, &msg
);
1641 talloc_free(mem_ctx
);
1645 orig_sid
= samdb_result_dom_sid(mem_ctx
, p
->msg
, "objectSid");
1646 target_sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
1648 /* Consider these to be the same principal, even if by a different
1649 * name. The easy and safe way to prove this is by SID
1651 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
1652 talloc_free(mem_ctx
);
1653 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1656 talloc_free(mem_ctx
);
1661 * Check if a given entry may delegate to this target principal
1665 samba_kdc_check_s4u2proxy(krb5_context context
,
1666 struct samba_kdc_db_context
*kdc_db_ctx
,
1667 hdb_entry_ex
*entry
,
1668 krb5_const_principal target_principal
)
1670 return KRB5KDC_ERR_BADOPTION
;
1673 NTSTATUS
samba_kdc_setup_db_ctx(TALLOC_CTX
*mem_ctx
, struct samba_kdc_base_context
*base_ctx
,
1674 struct samba_kdc_db_context
**kdc_db_ctx_out
)
1677 struct ldb_message
*msg
;
1678 struct auth_session_info
*session_info
;
1679 struct samba_kdc_db_context
*kdc_db_ctx
;
1680 /* The idea here is very simple. Using Kerberos to
1681 * authenticate the KDC to the LDAP server is higly likely to
1684 * In future we may set this up to use EXERNAL and SSL
1685 * certificates, for now it will almost certainly be NTLMSSP_SET_USERNAME
1688 kdc_db_ctx
= talloc_zero(mem_ctx
, struct samba_kdc_db_context
);
1689 if (kdc_db_ctx
== NULL
) {
1690 return NT_STATUS_NO_MEMORY
;
1692 kdc_db_ctx
->ev_ctx
= base_ctx
->ev_ctx
;
1693 kdc_db_ctx
->lp_ctx
= base_ctx
->lp_ctx
;
1695 kdc_get_policy(base_ctx
->lp_ctx
, NULL
, &kdc_db_ctx
->policy
);
1697 session_info
= system_session(kdc_db_ctx
->lp_ctx
);
1698 if (session_info
== NULL
) {
1699 return NT_STATUS_INTERNAL_ERROR
;
1702 /* Setup the link to LDB */
1703 kdc_db_ctx
->samdb
= samdb_connect(kdc_db_ctx
, base_ctx
->ev_ctx
,
1704 base_ctx
->lp_ctx
, session_info
, 0);
1705 if (kdc_db_ctx
->samdb
== NULL
) {
1706 DEBUG(1, ("hdb_samba4_create: Cannot open samdb for KDC backend!"));
1707 talloc_free(kdc_db_ctx
);
1708 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1711 /* Find out our own krbtgt kvno */
1712 ldb_ret
= samdb_rodc(kdc_db_ctx
->samdb
, &kdc_db_ctx
->rodc
);
1713 if (ldb_ret
!= LDB_SUCCESS
) {
1714 DEBUG(1, ("hdb_samba4_create: Cannot determine if we are an RODC in KDC backend: %s\n",
1715 ldb_errstring(kdc_db_ctx
->samdb
)));
1716 talloc_free(kdc_db_ctx
);
1717 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1719 if (kdc_db_ctx
->rodc
) {
1720 int my_krbtgt_number
;
1721 const char *secondary_keytab
[] = { "msDS-SecondaryKrbTgtNumber", NULL
};
1722 struct ldb_dn
*account_dn
;
1723 struct ldb_dn
*server_dn
= samdb_server_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
);
1725 DEBUG(1, ("hdb_samba4_create: Cannot determine server DN in KDC backend: %s\n",
1726 ldb_errstring(kdc_db_ctx
->samdb
)));
1727 talloc_free(kdc_db_ctx
);
1728 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1731 ldb_ret
= samdb_reference_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
, server_dn
,
1732 "serverReference", &account_dn
);
1733 if (ldb_ret
!= LDB_SUCCESS
) {
1734 DEBUG(1, ("hdb_samba4_create: Cannot determine server account in KDC backend: %s\n",
1735 ldb_errstring(kdc_db_ctx
->samdb
)));
1736 talloc_free(kdc_db_ctx
);
1737 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1740 ldb_ret
= samdb_reference_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
, account_dn
,
1741 "msDS-KrbTgtLink", &kdc_db_ctx
->krbtgt_dn
);
1742 talloc_free(account_dn
);
1743 if (ldb_ret
!= LDB_SUCCESS
) {
1744 DEBUG(1, ("hdb_samba4_create: Cannot determine RODC krbtgt account in KDC backend: %s\n",
1745 ldb_errstring(kdc_db_ctx
->samdb
)));
1746 talloc_free(kdc_db_ctx
);
1747 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1750 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
, kdc_db_ctx
,
1751 &msg
, kdc_db_ctx
->krbtgt_dn
, LDB_SCOPE_BASE
,
1754 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=*))");
1755 if (ldb_ret
!= LDB_SUCCESS
) {
1756 DEBUG(1, ("hdb_samba4_create: Cannot read krbtgt account %s in KDC backend to get msDS-SecondaryKrbTgtNumber: %s: %s\n",
1757 ldb_dn_get_linearized(kdc_db_ctx
->krbtgt_dn
),
1758 ldb_errstring(kdc_db_ctx
->samdb
),
1759 ldb_strerror(ldb_ret
)));
1760 talloc_free(kdc_db_ctx
);
1761 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1763 my_krbtgt_number
= ldb_msg_find_attr_as_int(msg
, "msDS-SecondaryKrbTgtNumber", -1);
1764 if (my_krbtgt_number
== -1) {
1765 DEBUG(1, ("hdb_samba4_create: Cannot read msDS-SecondaryKrbTgtNumber from krbtgt account %s in KDC backend: got %d\n",
1766 ldb_dn_get_linearized(kdc_db_ctx
->krbtgt_dn
),
1768 talloc_free(kdc_db_ctx
);
1769 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1771 kdc_db_ctx
->my_krbtgt_number
= my_krbtgt_number
;
1774 kdc_db_ctx
->my_krbtgt_number
= 0;
1775 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
, kdc_db_ctx
,
1776 &msg
, NULL
, LDB_SCOPE_SUBTREE
,
1779 "(&(objectClass=user)(samAccountName=krbtgt))");
1781 if (ldb_ret
!= LDB_SUCCESS
) {
1782 DEBUG(1, ("samba_kdc_fetch: could not find own KRBTGT in DB: %s\n", ldb_errstring(kdc_db_ctx
->samdb
)));
1783 talloc_free(kdc_db_ctx
);
1784 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1786 kdc_db_ctx
->krbtgt_dn
= talloc_steal(kdc_db_ctx
, msg
->dn
);
1787 kdc_db_ctx
->my_krbtgt_number
= 0;
1790 *kdc_db_ctx_out
= kdc_db_ctx
;
1791 return NT_STATUS_OK
;