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_NOT_DELEGATED
)) {
163 flags
.forwardable
= 1;
167 if (userAccountControl
& UF_DONT_REQUIRE_PREAUTH
) {
168 flags
.require_preauth
= 0;
170 flags
.require_preauth
= 1;
176 static int samba_kdc_entry_destructor(struct samba_kdc_entry
*p
)
178 hdb_entry_ex
*entry_ex
= p
->entry_ex
;
179 free_hdb_entry(&entry_ex
->entry
);
183 static void samba_kdc_free_entry(krb5_context context
, hdb_entry_ex
*entry_ex
)
185 /* this function is called only from hdb_free_entry().
186 * Make sure we neutralize the destructor or we will
187 * get a double free later when hdb_free_entry() will
188 * try to call free_hdb_entry() */
189 talloc_set_destructor(entry_ex
->ctx
, NULL
);
191 /* now proceed to free the talloc part */
192 talloc_free(entry_ex
->ctx
);
195 static krb5_error_code
samba_kdc_message2entry_keys(krb5_context context
,
196 struct samba_kdc_db_context
*kdc_db_ctx
,
198 struct ldb_message
*msg
,
201 uint32_t userAccountControl
,
202 enum samba_kdc_ent_type ent_type
,
203 hdb_entry_ex
*entry_ex
)
205 krb5_error_code ret
= 0;
206 enum ndr_err_code ndr_err
;
207 struct samr_Password
*hash
;
208 const struct ldb_val
*sc_val
;
209 struct supplementalCredentialsBlob scb
;
210 struct supplementalCredentialsPackage
*scpk
= NULL
;
211 bool newer_keys
= false;
212 struct package_PrimaryKerberosBlob _pkb
;
213 struct package_PrimaryKerberosCtr3
*pkb3
= NULL
;
214 struct package_PrimaryKerberosCtr4
*pkb4
= NULL
;
216 uint16_t allocated_keys
= 0;
217 int rodc_krbtgt_number
= 0;
219 uint32_t supported_enctypes
220 = ldb_msg_find_attr_as_uint(msg
,
221 "msDS-SupportedEncryptionTypes",
224 if (rid
== DOMAIN_RID_KRBTGT
|| is_rodc
) {
225 /* KDCs (and KDCs on RODCs) use AES */
226 supported_enctypes
|= ENC_HMAC_SHA1_96_AES128
| ENC_HMAC_SHA1_96_AES256
;
227 } else if (userAccountControl
& (UF_PARTIAL_SECRETS_ACCOUNT
|UF_SERVER_TRUST_ACCOUNT
)) {
228 /* DCs and RODCs comptuer accounts use AES */
229 supported_enctypes
|= ENC_HMAC_SHA1_96_AES128
| ENC_HMAC_SHA1_96_AES256
;
230 } else if (ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
||
231 (ent_type
== SAMBA_KDC_ENT_TYPE_ANY
)) {
232 /* for AS-REQ the client chooses the enc types it
233 * supports, and this will vary between computers a
236 * likewise for 'any' return as much as is supported,
237 * to export into a keytab */
238 supported_enctypes
= ENC_ALL_TYPES
;
241 /* If UF_USE_DES_KEY_ONLY has been set, then don't allow use of the newer enc types */
242 if (userAccountControl
& UF_USE_DES_KEY_ONLY
) {
243 supported_enctypes
= ENC_CRC32
|ENC_RSA_MD5
;
245 /* Otherwise, add in the default enc types */
246 supported_enctypes
|= ENC_CRC32
| ENC_RSA_MD5
| ENC_RC4_HMAC_MD5
;
249 /* Is this the krbtgt or a RODC krbtgt */
251 rodc_krbtgt_number
= ldb_msg_find_attr_as_int(msg
, "msDS-SecondaryKrbTgtNumber", -1);
253 if (rodc_krbtgt_number
== -1) {
258 entry_ex
->entry
.keys
.val
= NULL
;
259 entry_ex
->entry
.keys
.len
= 0;
261 kvno
= ldb_msg_find_attr_as_int(msg
, "msDS-KeyVersionNumber", 0);
263 kvno
= SAMBA_KVNO_AND_KRBTGT(kvno
, rodc_krbtgt_number
);
265 entry_ex
->entry
.kvno
= kvno
;
267 /* Get keys from the db */
269 hash
= samdb_result_hash(mem_ctx
, msg
, "unicodePwd");
270 sc_val
= ldb_msg_find_ldb_val(msg
, "supplementalCredentials");
272 /* unicodePwd for enctype 0x17 (23) if present */
277 /* supplementalCredentials if present */
279 ndr_err
= ndr_pull_struct_blob_all(sc_val
, mem_ctx
, &scb
,
280 (ndr_pull_flags_fn_t
)ndr_pull_supplementalCredentialsBlob
);
281 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
282 dump_data(0, sc_val
->data
, sc_val
->length
);
287 if (scb
.sub
.signature
!= SUPPLEMENTAL_CREDENTIALS_SIGNATURE
) {
288 NDR_PRINT_DEBUG(supplementalCredentialsBlob
, &scb
);
293 for (i
=0; i
< scb
.sub
.num_packages
; i
++) {
294 if (strcmp("Primary:Kerberos-Newer-Keys", scb
.sub
.packages
[i
].name
) == 0) {
295 scpk
= &scb
.sub
.packages
[i
];
296 if (!scpk
->data
|| !scpk
->data
[0]) {
302 } else if (strcmp("Primary:Kerberos", scb
.sub
.packages
[i
].name
) == 0) {
303 scpk
= &scb
.sub
.packages
[i
];
304 if (!scpk
->data
|| !scpk
->data
[0]) {
308 * we don't break here in hope to find
309 * a Kerberos-Newer-Keys package
315 * Primary:Kerberos-Newer-Keys or Primary:Kerberos element
316 * of supplementalCredentials
321 blob
= strhex_to_data_blob(mem_ctx
, scpk
->data
);
327 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
328 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, &_pkb
,
329 (ndr_pull_flags_fn_t
)ndr_pull_package_PrimaryKerberosBlob
);
330 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
332 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
333 krb5_warnx(context
, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
337 if (newer_keys
&& _pkb
.version
!= 4) {
339 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
340 krb5_warnx(context
, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
344 if (!newer_keys
&& _pkb
.version
!= 3) {
346 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
347 krb5_warnx(context
, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
351 if (_pkb
.version
== 4) {
352 pkb4
= &_pkb
.ctr
.ctr4
;
353 allocated_keys
+= pkb4
->num_keys
;
354 } else if (_pkb
.version
== 3) {
355 pkb3
= &_pkb
.ctr
.ctr3
;
356 allocated_keys
+= pkb3
->num_keys
;
360 if (allocated_keys
== 0) {
361 if (kdc_db_ctx
->rodc
) {
362 /* We are on an RODC, but don't have keys for this account. Signal this to the caller */
363 return HDB_ERR_NOT_FOUND_HERE
;
366 /* oh, no password. Apparently (comment in
367 * hdb-ldap.c) this violates the ASN.1, but this
368 * allows an entry with no keys (yet). */
372 /* allocate space to decode into */
373 entry_ex
->entry
.keys
.len
= 0;
374 entry_ex
->entry
.keys
.val
= calloc(allocated_keys
, sizeof(Key
));
375 if (entry_ex
->entry
.keys
.val
== NULL
) {
380 if (hash
&& (supported_enctypes
& ENC_RC4_HMAC_MD5
)) {
384 key
.salt
= NULL
; /* No salt for this enc type */
386 ret
= krb5_keyblock_init(context
,
387 ENCTYPE_ARCFOUR_HMAC
,
388 hash
->hash
, sizeof(hash
->hash
),
394 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
395 entry_ex
->entry
.keys
.len
++;
399 for (i
=0; i
< pkb4
->num_keys
; i
++) {
402 if (!pkb4
->keys
[i
].value
) continue;
404 if (!(kerberos_enctype_to_bitmap(pkb4
->keys
[i
].keytype
) & supported_enctypes
)) {
411 if (pkb4
->salt
.string
) {
414 salt
= data_blob_string_const(pkb4
->salt
.string
);
416 key
.salt
= calloc(1, sizeof(*key
.salt
));
417 if (key
.salt
== NULL
) {
422 key
.salt
->type
= hdb_pw_salt
;
424 ret
= krb5_data_copy(&key
.salt
->salt
, salt
.data
, salt
.length
);
432 /* TODO: maybe pass the iteration_count somehow... */
434 ret
= krb5_keyblock_init(context
,
435 pkb4
->keys
[i
].keytype
,
436 pkb4
->keys
[i
].value
->data
,
437 pkb4
->keys
[i
].value
->length
,
439 if (ret
== KRB5_PROG_ETYPE_NOSUPP
) {
440 DEBUG(2,("Unsupported keytype ignored - type %u\n",
441 pkb4
->keys
[i
].keytype
));
454 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
455 entry_ex
->entry
.keys
.len
++;
458 for (i
=0; i
< pkb3
->num_keys
; i
++) {
461 if (!pkb3
->keys
[i
].value
) continue;
463 if (!(kerberos_enctype_to_bitmap(pkb3
->keys
[i
].keytype
) & supported_enctypes
)) {
470 if (pkb3
->salt
.string
) {
473 salt
= data_blob_string_const(pkb3
->salt
.string
);
475 key
.salt
= calloc(1, sizeof(*key
.salt
));
476 if (key
.salt
== NULL
) {
481 key
.salt
->type
= hdb_pw_salt
;
483 ret
= krb5_data_copy(&key
.salt
->salt
, salt
.data
, salt
.length
);
491 ret
= krb5_keyblock_init(context
,
492 pkb3
->keys
[i
].keytype
,
493 pkb3
->keys
[i
].value
->data
,
494 pkb3
->keys
[i
].value
->length
,
505 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
506 entry_ex
->entry
.keys
.len
++;
512 entry_ex
->entry
.keys
.len
= 0;
514 if (entry_ex
->entry
.keys
.len
== 0 && entry_ex
->entry
.keys
.val
) {
515 free(entry_ex
->entry
.keys
.val
);
516 entry_ex
->entry
.keys
.val
= NULL
;
522 * Construct an hdb_entry from a directory entry.
524 static krb5_error_code
samba_kdc_message2entry(krb5_context context
,
525 struct samba_kdc_db_context
*kdc_db_ctx
,
526 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
527 enum samba_kdc_ent_type ent_type
,
529 struct ldb_dn
*realm_dn
,
530 struct ldb_message
*msg
,
531 hdb_entry_ex
*entry_ex
)
533 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
534 uint32_t userAccountControl
;
536 krb5_error_code ret
= 0;
537 krb5_boolean is_computer
= FALSE
;
539 struct samba_kdc_entry
*p
;
544 bool is_rodc
= false;
545 struct ldb_message_element
*objectclasses
;
546 struct ldb_val computer_val
;
547 const char *samAccountName
= ldb_msg_find_attr_as_string(msg
, "samAccountName", NULL
);
548 computer_val
.data
= discard_const_p(uint8_t,"computer");
549 computer_val
.length
= strlen((const char *)computer_val
.data
);
551 if (ldb_msg_find_element(msg
, "msDS-SecondaryKrbTgtNumber")) {
555 if (!samAccountName
) {
557 krb5_set_error_message(context
, ret
, "samba_kdc_message2entry: no samAccountName present");
561 objectclasses
= ldb_msg_find_element(msg
, "objectClass");
563 if (objectclasses
&& ldb_msg_find_val(objectclasses
, &computer_val
)) {
567 memset(entry_ex
, 0, sizeof(*entry_ex
));
569 p
= talloc(mem_ctx
, struct samba_kdc_entry
);
575 p
->kdc_db_ctx
= kdc_db_ctx
;
576 p
->entry_ex
= entry_ex
;
577 p
->realm_dn
= talloc_reference(p
, realm_dn
);
583 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
585 /* make sure we do not have bogus data in there */
586 memset(&entry_ex
->entry
, 0, sizeof(hdb_entry
));
589 entry_ex
->free_entry
= samba_kdc_free_entry
;
591 userAccountControl
= ldb_msg_find_attr_as_uint(msg
, "userAccountControl", 0);
594 entry_ex
->entry
.principal
= malloc(sizeof(*(entry_ex
->entry
.principal
)));
595 if (ent_type
== SAMBA_KDC_ENT_TYPE_ANY
&& principal
== NULL
) {
596 krb5_make_principal(context
, &entry_ex
->entry
.principal
, lpcfg_realm(lp_ctx
), samAccountName
, NULL
);
598 ret
= copy_Principal(principal
, entry_ex
->entry
.principal
);
600 krb5_clear_error_message(context
);
604 /* While we have copied the client principal, tests
605 * show that Win2k3 returns the 'corrected' realm, not
606 * the client-specified realm. This code attempts to
607 * replace the client principal's realm with the one
608 * we determine from our records */
610 /* this has to be with malloc() */
611 krb5_principal_set_realm(context
, entry_ex
->entry
.principal
, lpcfg_realm(lp_ctx
));
614 /* First try and figure out the flags based on the userAccountControl */
615 entry_ex
->entry
.flags
= uf2HDBFlags(context
, userAccountControl
, ent_type
);
617 /* Windows 2008 seems to enforce this (very sensible) rule by
618 * default - don't allow offline attacks on a user's password
619 * by asking for a ticket to them as a service (encrypted with
620 * their probably patheticly insecure password) */
622 if (entry_ex
->entry
.flags
.server
623 && lpcfg_parm_bool(lp_ctx
, NULL
, "kdc", "require spn for service", true)) {
624 if (!is_computer
&& !ldb_msg_find_attr_as_string(msg
, "servicePrincipalName", NULL
)) {
625 entry_ex
->entry
.flags
.server
= 0;
629 if (flags
& HDB_F_ADMIN_DATA
) {
630 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
631 * of the Heimdal KDC. They are stored in a the traditional
632 * DB for audit purposes, and still form part of the structure
635 /* use 'whenCreated' */
636 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
637 /* use 'kadmin' for now (needed by mit_samba) */
638 krb5_make_principal(context
,
639 &entry_ex
->entry
.created_by
.principal
,
640 lpcfg_realm(lp_ctx
), "kadmin", NULL
);
642 entry_ex
->entry
.modified_by
= (Event
*) malloc(sizeof(Event
));
643 if (entry_ex
->entry
.modified_by
== NULL
) {
645 krb5_set_error_message(context
, ret
, "malloc: out of memory");
649 /* use 'whenChanged' */
650 entry_ex
->entry
.modified_by
->time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenChanged", 0);
651 /* use 'kadmin' for now (needed by mit_samba) */
652 krb5_make_principal(context
,
653 &entry_ex
->entry
.modified_by
->principal
,
654 lpcfg_realm(lp_ctx
), "kadmin", NULL
);
658 /* The lack of password controls etc applies to krbtgt by
659 * virtue of being that particular RID */
660 status
= dom_sid_split_rid(NULL
, samdb_result_dom_sid(mem_ctx
, msg
, "objectSid"), NULL
, &rid
);
662 if (!NT_STATUS_IS_OK(status
)) {
667 if (rid
== DOMAIN_RID_KRBTGT
) {
668 entry_ex
->entry
.valid_end
= NULL
;
669 entry_ex
->entry
.pw_end
= NULL
;
671 entry_ex
->entry
.flags
.invalid
= 0;
672 entry_ex
->entry
.flags
.server
= 1;
674 /* Don't mark all requests for the krbtgt/realm as
675 * 'change password', as otherwise we could get into
676 * trouble, and not enforce the password expirty.
677 * Instead, only do it when request is for the kpasswd service */
678 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
679 && principal
->name
.name_string
.len
== 2
680 && (strcmp(principal
->name
.name_string
.val
[0], "kadmin") == 0)
681 && (strcmp(principal
->name
.name_string
.val
[1], "changepw") == 0)
682 && lpcfg_is_my_domain_or_realm(lp_ctx
, principal
->realm
)) {
683 entry_ex
->entry
.flags
.change_pw
= 1;
685 entry_ex
->entry
.flags
.client
= 0;
686 entry_ex
->entry
.flags
.forwardable
= 1;
687 entry_ex
->entry
.flags
.ok_as_delegate
= 1;
688 } else if (is_rodc
) {
689 /* The RODC krbtgt account is like the main krbtgt,
690 * but it does not have a changepw or kadmin
693 entry_ex
->entry
.valid_end
= NULL
;
694 entry_ex
->entry
.pw_end
= NULL
;
696 /* Also don't allow the RODC krbtgt to be a client (it should not be needed) */
697 entry_ex
->entry
.flags
.client
= 0;
698 entry_ex
->entry
.flags
.invalid
= 0;
699 entry_ex
->entry
.flags
.server
= 1;
701 entry_ex
->entry
.flags
.client
= 0;
702 entry_ex
->entry
.flags
.forwardable
= 1;
703 entry_ex
->entry
.flags
.ok_as_delegate
= 0;
704 } else if (entry_ex
->entry
.flags
.server
&& ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
705 /* The account/password expiry only applies when the account is used as a
706 * client (ie password login), not when used as a server */
708 /* Make very well sure we don't use this for a client,
709 * it could bypass the password restrictions */
710 entry_ex
->entry
.flags
.client
= 0;
712 entry_ex
->entry
.valid_end
= NULL
;
713 entry_ex
->entry
.pw_end
= NULL
;
716 NTTIME must_change_time
717 = samdb_result_force_password_change(kdc_db_ctx
->samdb
, mem_ctx
,
719 if (must_change_time
== 0x7FFFFFFFFFFFFFFFULL
) {
720 entry_ex
->entry
.pw_end
= NULL
;
722 entry_ex
->entry
.pw_end
= malloc(sizeof(*entry_ex
->entry
.pw_end
));
723 if (entry_ex
->entry
.pw_end
== NULL
) {
727 *entry_ex
->entry
.pw_end
= nt_time_to_unix(must_change_time
);
730 acct_expiry
= samdb_result_account_expires(msg
);
731 if (acct_expiry
== 0x7FFFFFFFFFFFFFFFULL
) {
732 entry_ex
->entry
.valid_end
= NULL
;
734 entry_ex
->entry
.valid_end
= malloc(sizeof(*entry_ex
->entry
.valid_end
));
735 if (entry_ex
->entry
.valid_end
== NULL
) {
739 *entry_ex
->entry
.valid_end
= nt_time_to_unix(acct_expiry
);
743 entry_ex
->entry
.valid_start
= NULL
;
745 entry_ex
->entry
.max_life
= malloc(sizeof(*entry_ex
->entry
.max_life
));
746 if (entry_ex
->entry
.max_life
== NULL
) {
751 if (ent_type
== SAMBA_KDC_ENT_TYPE_SERVER
) {
752 *entry_ex
->entry
.max_life
= nt_time_to_unix(kdc_db_ctx
->policy
.service_tkt_lifetime
);
753 } else if (ent_type
== SAMBA_KDC_ENT_TYPE_KRBTGT
|| ent_type
== SAMBA_KDC_ENT_TYPE_CLIENT
) {
754 *entry_ex
->entry
.max_life
= nt_time_to_unix(kdc_db_ctx
->policy
.user_tkt_lifetime
);
756 *entry_ex
->entry
.max_life
= MIN(nt_time_to_unix(kdc_db_ctx
->policy
.service_tkt_lifetime
),
757 nt_time_to_unix(kdc_db_ctx
->policy
.user_tkt_lifetime
));
760 entry_ex
->entry
.max_renew
= malloc(sizeof(*entry_ex
->entry
.max_life
));
761 if (entry_ex
->entry
.max_renew
== NULL
) {
766 *entry_ex
->entry
.max_renew
= nt_time_to_unix(kdc_db_ctx
->policy
.user_tkt_renewaltime
);
768 entry_ex
->entry
.generation
= NULL
;
770 /* Get keys from the db */
771 ret
= samba_kdc_message2entry_keys(context
, kdc_db_ctx
, p
, msg
,
772 rid
, is_rodc
, userAccountControl
,
775 /* Could be bougus data in the entry, or out of memory */
779 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
780 if (entry_ex
->entry
.etypes
== NULL
) {
781 krb5_clear_error_message(context
);
785 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
786 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
787 if (entry_ex
->entry
.etypes
->val
== NULL
) {
788 krb5_clear_error_message(context
);
792 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
793 entry_ex
->entry
.etypes
->val
[i
] = entry_ex
->entry
.keys
.val
[i
].key
.keytype
;
797 p
->msg
= talloc_steal(p
, msg
);
801 /* This doesn't free ent itself, that is for the eventual caller to do */
802 hdb_free_entry(context
, entry_ex
);
804 talloc_steal(kdc_db_ctx
, entry_ex
->ctx
);
811 * Construct an hdb_entry from a directory entry.
813 static krb5_error_code
samba_kdc_trust_message2entry(krb5_context context
,
814 struct samba_kdc_db_context
*kdc_db_ctx
,
815 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
816 enum trust_direction direction
,
817 struct ldb_dn
*realm_dn
,
818 struct ldb_message
*msg
,
819 hdb_entry_ex
*entry_ex
)
821 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
822 const char *dnsdomain
;
823 const char *realm
= lpcfg_realm(lp_ctx
);
824 DATA_BLOB password_utf16
;
825 struct samr_Password password_hash
;
826 const struct ldb_val
*password_val
;
827 struct trustAuthInOutBlob password_blob
;
828 struct samba_kdc_entry
*p
;
830 enum ndr_err_code ndr_err
;
831 int ret
, trust_direction_flags
;
834 p
= talloc(mem_ctx
, struct samba_kdc_entry
);
840 p
->kdc_db_ctx
= kdc_db_ctx
;
841 p
->entry_ex
= entry_ex
;
842 p
->realm_dn
= realm_dn
;
844 talloc_set_destructor(p
, samba_kdc_entry_destructor
);
846 /* make sure we do not have bogus data in there */
847 memset(&entry_ex
->entry
, 0, sizeof(hdb_entry
));
850 entry_ex
->free_entry
= samba_kdc_free_entry
;
852 /* use 'whenCreated' */
853 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
854 /* use 'kadmin' for now (needed by mit_samba) */
855 krb5_make_principal(context
,
856 &entry_ex
->entry
.created_by
.principal
,
857 realm
, "kadmin", NULL
);
859 entry_ex
->entry
.valid_start
= NULL
;
861 trust_direction_flags
= ldb_msg_find_attr_as_int(msg
, "trustDirection", 0);
863 if (direction
== INBOUND
) {
864 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthIncoming");
866 } else { /* OUTBOUND */
867 dnsdomain
= ldb_msg_find_attr_as_string(msg
, "trustPartner", NULL
);
869 realm
= strupper_talloc(mem_ctx
, dnsdomain
);
870 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthOutgoing");
873 if (!password_val
|| !(trust_direction_flags
& direction
)) {
878 ndr_err
= ndr_pull_struct_blob(password_val
, mem_ctx
, &password_blob
,
879 (ndr_pull_flags_fn_t
)ndr_pull_trustAuthInOutBlob
);
880 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
885 entry_ex
->entry
.kvno
= -1;
886 for (i
=0; i
< password_blob
.count
; i
++) {
887 if (password_blob
.current
.array
[i
].AuthType
== TRUST_AUTH_TYPE_VERSION
) {
888 entry_ex
->entry
.kvno
= password_blob
.current
.array
[i
].AuthInfo
.version
.version
;
892 for (i
=0; i
< password_blob
.count
; i
++) {
893 if (password_blob
.current
.array
[i
].AuthType
== TRUST_AUTH_TYPE_CLEAR
) {
894 password_utf16
= data_blob_const(password_blob
.current
.array
[i
].AuthInfo
.clear
.password
,
895 password_blob
.current
.array
[i
].AuthInfo
.clear
.size
);
896 /* In the future, generate all sorts of
897 * hashes, but for now we can't safely convert
898 * the random strings windows uses into
901 /* but as it is utf16 already, we can get the NT password/arcfour-hmac-md5 key */
902 mdfour(password_hash
.hash
, password_utf16
.data
, password_utf16
.length
);
904 } else if (password_blob
.current
.array
[i
].AuthType
== TRUST_AUTH_TYPE_NT4OWF
) {
905 password_hash
= password_blob
.current
.array
[i
].AuthInfo
.nt4owf
.password
;
910 if (i
< password_blob
.count
) {
912 /* Must have found a cleartext or MD4 password */
913 entry_ex
->entry
.keys
.val
= calloc(1, sizeof(Key
));
916 key
.salt
= NULL
; /* No salt for this enc type */
918 if (entry_ex
->entry
.keys
.val
== NULL
) {
923 ret
= krb5_keyblock_init(context
,
924 ENCTYPE_ARCFOUR_HMAC
,
925 password_hash
.hash
, sizeof(password_hash
.hash
),
928 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
929 entry_ex
->entry
.keys
.len
++;
932 entry_ex
->entry
.principal
= malloc(sizeof(*(entry_ex
->entry
.principal
)));
934 ret
= copy_Principal(principal
, entry_ex
->entry
.principal
);
936 krb5_clear_error_message(context
);
940 /* While we have copied the client principal, tests
941 * show that Win2k3 returns the 'corrected' realm, not
942 * the client-specified realm. This code attempts to
943 * replace the client principal's realm with the one
944 * we determine from our records */
946 krb5_principal_set_realm(context
, entry_ex
->entry
.principal
, realm
);
947 entry_ex
->entry
.flags
= int2HDBFlags(0);
948 entry_ex
->entry
.flags
.immutable
= 1;
949 entry_ex
->entry
.flags
.invalid
= 0;
950 entry_ex
->entry
.flags
.server
= 1;
951 entry_ex
->entry
.flags
.require_preauth
= 1;
953 entry_ex
->entry
.pw_end
= NULL
;
955 entry_ex
->entry
.max_life
= NULL
;
957 entry_ex
->entry
.max_renew
= NULL
;
959 entry_ex
->entry
.generation
= NULL
;
961 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
962 if (entry_ex
->entry
.etypes
== NULL
) {
963 krb5_clear_error_message(context
);
967 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
968 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
969 if (entry_ex
->entry
.etypes
->val
== NULL
) {
970 krb5_clear_error_message(context
);
974 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
975 entry_ex
->entry
.etypes
->val
[i
] = entry_ex
->entry
.keys
.val
[i
].key
.keytype
;
979 p
->msg
= talloc_steal(p
, msg
);
983 /* This doesn't free ent itself, that is for the eventual caller to do */
984 hdb_free_entry(context
, entry_ex
);
986 talloc_steal(kdc_db_ctx
, entry_ex
->ctx
);
993 static krb5_error_code
samba_kdc_lookup_trust(krb5_context context
, struct ldb_context
*ldb_ctx
,
996 struct ldb_dn
*realm_dn
,
997 struct ldb_message
**pmsg
)
1000 krb5_error_code ret
;
1001 char *filter
= NULL
;
1002 const char * const *attrs
= trust_attrs
;
1004 struct ldb_result
*res
= NULL
;
1005 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=trustedDomain)(|(flatname=%s)(trustPartner=%s)))", realm
, realm
);
1009 krb5_set_error_message(context
, ret
, "talloc_asprintf: out of memory");
1013 lret
= ldb_search(ldb_ctx
, mem_ctx
, &res
,
1014 ldb_get_default_basedn(ldb_ctx
),
1015 LDB_SCOPE_SUBTREE
, attrs
, "%s", filter
);
1016 if (lret
!= LDB_SUCCESS
) {
1017 DEBUG(3, ("Failed to search for %s: %s\n", filter
, ldb_errstring(ldb_ctx
)));
1018 return HDB_ERR_NOENTRY
;
1019 } else if (res
->count
== 0 || res
->count
> 1) {
1020 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter
, res
->count
));
1022 return HDB_ERR_NOENTRY
;
1024 talloc_steal(mem_ctx
, res
->msgs
);
1025 *pmsg
= res
->msgs
[0];
1030 static krb5_error_code
samba_kdc_lookup_client(krb5_context context
,
1031 struct samba_kdc_db_context
*kdc_db_ctx
,
1032 TALLOC_CTX
*mem_ctx
,
1033 krb5_const_principal principal
,
1035 struct ldb_dn
**realm_dn
,
1036 struct ldb_message
**msg
) {
1038 char *principal_string
;
1039 krb5_error_code ret
;
1041 ret
= krb5_unparse_name(context
, principal
, &principal_string
);
1047 nt_status
= sam_get_results_principal(kdc_db_ctx
->samdb
,
1048 mem_ctx
, principal_string
, attrs
,
1050 free(principal_string
);
1051 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
1052 return HDB_ERR_NOENTRY
;
1053 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_MEMORY
)) {
1055 } else if (!NT_STATUS_IS_OK(nt_status
)) {
1062 static krb5_error_code
samba_kdc_fetch_client(krb5_context context
,
1063 struct samba_kdc_db_context
*kdc_db_ctx
,
1064 TALLOC_CTX
*mem_ctx
,
1065 krb5_const_principal principal
,
1067 hdb_entry_ex
*entry_ex
) {
1068 struct ldb_dn
*realm_dn
;
1069 krb5_error_code ret
;
1070 struct ldb_message
*msg
= NULL
;
1072 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
1073 mem_ctx
, principal
, user_attrs
,
1079 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1080 principal
, SAMBA_KDC_ENT_TYPE_CLIENT
,
1082 realm_dn
, msg
, entry_ex
);
1086 static krb5_error_code
samba_kdc_fetch_krbtgt(krb5_context context
,
1087 struct samba_kdc_db_context
*kdc_db_ctx
,
1088 TALLOC_CTX
*mem_ctx
,
1089 krb5_const_principal principal
,
1091 uint32_t krbtgt_number
,
1092 hdb_entry_ex
*entry_ex
)
1094 struct loadparm_context
*lp_ctx
= kdc_db_ctx
->lp_ctx
;
1095 krb5_error_code ret
;
1096 struct ldb_message
*msg
= NULL
;
1097 struct ldb_dn
*realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
1099 krb5_principal alloc_principal
= NULL
;
1100 if (principal
->name
.name_string
.len
!= 2
1101 || (strcmp(principal
->name
.name_string
.val
[0], KRB5_TGS_NAME
) != 0)) {
1103 return HDB_ERR_NOENTRY
;
1106 /* krbtgt case. Either us or a trusted realm */
1108 if (lpcfg_is_my_domain_or_realm(lp_ctx
, principal
->realm
)
1109 && lpcfg_is_my_domain_or_realm(lp_ctx
, principal
->name
.name_string
.val
[1])) {
1110 /* us, or someone quite like us */
1111 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1112 * is in our db, then direct the caller at our primary
1117 if (krbtgt_number
== kdc_db_ctx
->my_krbtgt_number
) {
1118 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
,
1119 &msg
, kdc_db_ctx
->krbtgt_dn
, LDB_SCOPE_BASE
,
1121 "(objectClass=user)");
1123 /* We need to look up an RODC krbtgt (perhaps
1124 * ours, if we are an RODC, perhaps another
1125 * RODC if we are a read-write DC */
1126 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
,
1127 &msg
, realm_dn
, LDB_SCOPE_SUBTREE
,
1129 DSDB_SEARCH_SHOW_EXTENDED_DN
,
1130 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=%u))", (unsigned)(krbtgt_number
));
1133 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
1134 krb5_warnx(context
, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1135 (unsigned)(krbtgt_number
));
1136 krb5_set_error_message(context
, HDB_ERR_NOENTRY
,
1137 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1138 (unsigned)(krbtgt_number
));
1139 return HDB_ERR_NOENTRY
;
1140 } else if (lret
!= LDB_SUCCESS
) {
1141 krb5_warnx(context
, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1142 (unsigned)(krbtgt_number
));
1143 krb5_set_error_message(context
, HDB_ERR_NOENTRY
,
1144 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1145 (unsigned)(krbtgt_number
));
1146 return HDB_ERR_NOENTRY
;
1149 if (flags
& HDB_F_CANON
) {
1150 ret
= krb5_copy_principal(context
, principal
, &alloc_principal
);
1155 /* When requested to do so, ensure that the
1156 * both realm values in the principal are set
1157 * to the upper case, canonical realm */
1158 free(alloc_principal
->name
.name_string
.val
[1]);
1159 alloc_principal
->name
.name_string
.val
[1] = strdup(lpcfg_realm(lp_ctx
));
1160 if (!alloc_principal
->name
.name_string
.val
[1]) {
1162 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: strdup() failed!");
1165 principal
= alloc_principal
;
1168 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1169 principal
, SAMBA_KDC_ENT_TYPE_KRBTGT
,
1170 flags
, realm_dn
, msg
, entry_ex
);
1171 if (flags
& HDB_F_CANON
) {
1172 /* This is again copied in the message2entry call */
1173 krb5_free_principal(context
, alloc_principal
);
1176 krb5_warnx(context
, "samba_kdc_fetch: self krbtgt message2entry failed");
1181 enum trust_direction direction
= UNKNOWN
;
1182 const char *realm
= NULL
;
1184 /* Either an inbound or outbound trust */
1186 if (strcasecmp(lpcfg_realm(lp_ctx
), principal
->realm
) == 0) {
1187 /* look for inbound trust */
1188 direction
= INBOUND
;
1189 realm
= principal
->name
.name_string
.val
[1];
1190 } else if (strcasecmp(lpcfg_realm(lp_ctx
), principal
->name
.name_string
.val
[1]) == 0) {
1191 /* look for outbound trust */
1192 direction
= OUTBOUND
;
1193 realm
= principal
->realm
;
1195 krb5_warnx(context
, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1196 principal
->realm
, principal
->name
.name_string
.val
[1]);
1197 krb5_set_error_message(context
, HDB_ERR_NOENTRY
, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1198 principal
->realm
, principal
->name
.name_string
.val
[1]);
1199 return HDB_ERR_NOENTRY
;
1202 /* Trusted domains are under CN=system */
1204 ret
= samba_kdc_lookup_trust(context
, kdc_db_ctx
->samdb
,
1206 realm
, realm_dn
, &msg
);
1209 krb5_warnx(context
, "samba_kdc_fetch: could not find principal in DB");
1210 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: could not find principal in DB");
1214 ret
= samba_kdc_trust_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1215 principal
, direction
,
1216 realm_dn
, msg
, entry_ex
);
1218 krb5_warnx(context
, "samba_kdc_fetch: trust_message2entry failed");
1225 static krb5_error_code
samba_kdc_lookup_server(krb5_context context
,
1226 struct samba_kdc_db_context
*kdc_db_ctx
,
1227 TALLOC_CTX
*mem_ctx
,
1228 krb5_const_principal principal
,
1230 struct ldb_dn
**realm_dn
,
1231 struct ldb_message
**msg
)
1233 krb5_error_code ret
;
1234 if (principal
->name
.name_string
.len
>= 2) {
1235 /* 'normal server' case */
1238 struct ldb_dn
*user_dn
;
1239 char *principal_string
;
1241 ret
= krb5_unparse_name_flags(context
, principal
,
1242 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
1248 /* At this point we may find the host is known to be
1249 * in a different realm, so we should generate a
1250 * referral instead */
1251 nt_status
= crack_service_principal_name(kdc_db_ctx
->samdb
,
1252 mem_ctx
, principal_string
,
1253 &user_dn
, realm_dn
);
1254 free(principal_string
);
1256 if (!NT_STATUS_IS_OK(nt_status
)) {
1257 return HDB_ERR_NOENTRY
;
1260 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
,
1262 msg
, user_dn
, LDB_SCOPE_BASE
,
1263 attrs
, DSDB_SEARCH_SHOW_EXTENDED_DN
, "(objectClass=*)");
1264 if (ldb_ret
!= LDB_SUCCESS
) {
1265 return HDB_ERR_NOENTRY
;
1270 char *filter
= NULL
;
1273 /* server as client principal case, but we must not lookup userPrincipalNames */
1274 *realm_dn
= ldb_get_default_basedn(kdc_db_ctx
->samdb
);
1275 realm
= krb5_principal_get_realm(context
, principal
);
1277 /* TODO: Check if it is our realm, otherwise give referall */
1279 ret
= krb5_unparse_name_flags(context
, principal
, KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &short_princ
);
1282 krb5_set_error_message(context
, ret
, "samba_kdc_lookup_principal: could not parse principal");
1283 krb5_warnx(context
, "samba_kdc_lookup_principal: could not parse principal");
1287 lret
= dsdb_search_one(kdc_db_ctx
->samdb
, mem_ctx
, msg
,
1288 *realm_dn
, LDB_SCOPE_SUBTREE
,
1290 DSDB_SEARCH_SHOW_EXTENDED_DN
,
1291 "(&(objectClass=user)(samAccountName=%s))",
1292 ldb_binary_encode_string(mem_ctx
, short_princ
));
1294 if (lret
== LDB_ERR_NO_SUCH_OBJECT
) {
1295 DEBUG(3, ("Failed find a entry for %s\n", filter
));
1296 return HDB_ERR_NOENTRY
;
1298 if (lret
!= LDB_SUCCESS
) {
1299 DEBUG(3, ("Failed single search for for %s - %s\n",
1300 filter
, ldb_errstring(kdc_db_ctx
->samdb
)));
1301 return HDB_ERR_NOENTRY
;
1308 static krb5_error_code
samba_kdc_fetch_server(krb5_context context
,
1309 struct samba_kdc_db_context
*kdc_db_ctx
,
1310 TALLOC_CTX
*mem_ctx
,
1311 krb5_const_principal principal
,
1313 hdb_entry_ex
*entry_ex
)
1315 krb5_error_code ret
;
1316 struct ldb_dn
*realm_dn
;
1317 struct ldb_message
*msg
;
1319 ret
= samba_kdc_lookup_server(context
, kdc_db_ctx
, mem_ctx
, principal
,
1320 server_attrs
, &realm_dn
, &msg
);
1325 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1326 principal
, SAMBA_KDC_ENT_TYPE_SERVER
,
1328 realm_dn
, msg
, entry_ex
);
1330 krb5_warnx(context
, "samba_kdc_fetch: message2entry failed");
1336 krb5_error_code
samba_kdc_fetch(krb5_context context
,
1337 struct samba_kdc_db_context
*kdc_db_ctx
,
1338 krb5_const_principal principal
,
1341 hdb_entry_ex
*entry_ex
)
1343 krb5_error_code ret
= HDB_ERR_NOENTRY
;
1344 TALLOC_CTX
*mem_ctx
;
1345 unsigned int krbtgt_number
;
1346 if (flags
& HDB_F_KVNO_SPECIFIED
) {
1347 krbtgt_number
= SAMBA_KVNO_GET_KRBTGT(kvno
);
1348 if (kdc_db_ctx
->rodc
) {
1349 if (krbtgt_number
!= kdc_db_ctx
->my_krbtgt_number
) {
1350 return HDB_ERR_NOT_FOUND_HERE
;
1354 krbtgt_number
= kdc_db_ctx
->my_krbtgt_number
;
1357 mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_fetch context");
1360 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
1364 if (flags
& HDB_F_GET_CLIENT
) {
1365 ret
= samba_kdc_fetch_client(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, entry_ex
);
1366 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1368 if (flags
& HDB_F_GET_SERVER
) {
1369 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
1370 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, krbtgt_number
, entry_ex
);
1371 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1373 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
1374 ret
= samba_kdc_fetch_server(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, entry_ex
);
1375 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1377 if (flags
& HDB_F_GET_KRBTGT
) {
1378 ret
= samba_kdc_fetch_krbtgt(context
, kdc_db_ctx
, mem_ctx
, principal
, flags
, krbtgt_number
, entry_ex
);
1379 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1383 talloc_free(mem_ctx
);
1387 struct samba_kdc_seq
{
1390 struct ldb_message
**msgs
;
1391 struct ldb_dn
*realm_dn
;
1394 static krb5_error_code
samba_kdc_seq(krb5_context context
,
1395 struct samba_kdc_db_context
*kdc_db_ctx
,
1396 hdb_entry_ex
*entry
)
1398 krb5_error_code ret
;
1399 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
1400 TALLOC_CTX
*mem_ctx
;
1401 hdb_entry_ex entry_ex
;
1402 memset(&entry_ex
, '\0', sizeof(entry_ex
));
1405 return HDB_ERR_NOENTRY
;
1408 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_seq context");
1412 krb5_set_error_message(context
, ret
, "samba_kdc_seq: talloc_named() failed!");
1416 if (priv
->index
< priv
->count
) {
1417 ret
= samba_kdc_message2entry(context
, kdc_db_ctx
, mem_ctx
,
1418 NULL
, SAMBA_KDC_ENT_TYPE_ANY
,
1419 HDB_F_ADMIN_DATA
|HDB_F_GET_ANY
,
1420 priv
->realm_dn
, priv
->msgs
[priv
->index
++], entry
);
1422 ret
= HDB_ERR_NOENTRY
;
1427 kdc_db_ctx
->seq_ctx
= NULL
;
1429 talloc_free(mem_ctx
);
1435 krb5_error_code
samba_kdc_firstkey(krb5_context context
,
1436 struct samba_kdc_db_context
*kdc_db_ctx
,
1437 hdb_entry_ex
*entry
)
1439 struct ldb_context
*ldb_ctx
= kdc_db_ctx
->samdb
;
1440 struct samba_kdc_seq
*priv
= kdc_db_ctx
->seq_ctx
;
1442 struct ldb_result
*res
= NULL
;
1443 krb5_error_code ret
;
1444 TALLOC_CTX
*mem_ctx
;
1449 kdc_db_ctx
->seq_ctx
= NULL
;
1452 priv
= (struct samba_kdc_seq
*) talloc(kdc_db_ctx
, struct samba_kdc_seq
);
1455 krb5_set_error_message(context
, ret
, "talloc: out of memory");
1461 priv
->realm_dn
= ldb_get_default_basedn(ldb_ctx
);
1464 mem_ctx
= talloc_named(priv
, 0, "samba_kdc_firstkey context");
1468 krb5_set_error_message(context
, ret
, "samba_kdc_firstkey: talloc_named() failed!");
1472 ret
= krb5_get_default_realm(context
, &realm
);
1478 lret
= ldb_search(ldb_ctx
, priv
, &res
,
1479 priv
->realm_dn
, LDB_SCOPE_SUBTREE
, user_attrs
,
1480 "(objectClass=user)");
1482 if (lret
!= LDB_SUCCESS
) {
1484 return HDB_ERR_NOENTRY
;
1487 priv
->count
= res
->count
;
1488 priv
->msgs
= talloc_steal(priv
, res
->msgs
);
1491 kdc_db_ctx
->seq_ctx
= priv
;
1493 ret
= samba_kdc_seq(context
, kdc_db_ctx
, entry
);
1497 kdc_db_ctx
->seq_ctx
= NULL
;
1499 talloc_free(mem_ctx
);
1504 krb5_error_code
samba_kdc_nextkey(krb5_context context
,
1505 struct samba_kdc_db_context
*kdc_db_ctx
,
1506 hdb_entry_ex
*entry
)
1508 return samba_kdc_seq(context
, kdc_db_ctx
, entry
);
1511 /* Check if a given entry may delegate or do s4u2self to this target principal
1513 * This is currently a very nasty hack - allowing only delegation to itself.
1515 * This is shared between the constrained delegation and S4U2Self code.
1518 samba_kdc_check_identical_client_and_server(krb5_context context
,
1519 struct samba_kdc_db_context
*kdc_db_ctx
,
1520 hdb_entry_ex
*entry
,
1521 krb5_const_principal target_principal
)
1523 krb5_error_code ret
;
1524 krb5_principal enterprise_prinicpal
= NULL
;
1525 struct ldb_dn
*realm_dn
;
1526 struct ldb_message
*msg
;
1527 struct dom_sid
*orig_sid
;
1528 struct dom_sid
*target_sid
;
1529 struct samba_kdc_entry
*p
= talloc_get_type(entry
->ctx
, struct samba_kdc_entry
);
1530 const char *delegation_check_attrs
[] = {
1534 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_constrained_delegation");
1538 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
1542 if (target_principal
->name
.name_type
== KRB5_NT_ENTERPRISE_PRINCIPAL
) {
1543 /* Need to reparse the enterprise principal to find the real target */
1544 if (target_principal
->name
.name_string
.len
!= 1) {
1545 ret
= KRB5_PARSE_MALFORMED
;
1546 krb5_set_error_message(context
, ret
, "samba_kdc_check_constrained_delegation: request for delegation to enterprise principal with wrong (%d) number of components",
1547 target_principal
->name
.name_string
.len
);
1548 talloc_free(mem_ctx
);
1551 ret
= krb5_parse_name(context
, target_principal
->name
.name_string
.val
[0],
1552 &enterprise_prinicpal
);
1554 talloc_free(mem_ctx
);
1557 target_principal
= enterprise_prinicpal
;
1560 ret
= samba_kdc_lookup_server(context
, kdc_db_ctx
, mem_ctx
, target_principal
,
1561 delegation_check_attrs
, &realm_dn
, &msg
);
1563 krb5_free_principal(context
, enterprise_prinicpal
);
1566 talloc_free(mem_ctx
);
1570 orig_sid
= samdb_result_dom_sid(mem_ctx
, p
->msg
, "objectSid");
1571 target_sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
1573 /* Allow delegation to the same principal, even if by a different
1574 * name. The easy and safe way to prove this is by SID
1576 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
1577 talloc_free(mem_ctx
);
1578 return KRB5KDC_ERR_BADOPTION
;
1581 talloc_free(mem_ctx
);
1585 /* Certificates printed by a the Certificate Authority might have a
1586 * slightly different form of the user principal name to that in the
1587 * database. Allow a mismatch where they both refer to the same
1591 samba_kdc_check_pkinit_ms_upn_match(krb5_context context
,
1592 struct samba_kdc_db_context
*kdc_db_ctx
,
1593 hdb_entry_ex
*entry
,
1594 krb5_const_principal certificate_principal
)
1596 krb5_error_code ret
;
1597 struct ldb_dn
*realm_dn
;
1598 struct ldb_message
*msg
;
1599 struct dom_sid
*orig_sid
;
1600 struct dom_sid
*target_sid
;
1601 struct samba_kdc_entry
*p
= talloc_get_type(entry
->ctx
, struct samba_kdc_entry
);
1602 const char *ms_upn_check_attrs
[] = {
1606 TALLOC_CTX
*mem_ctx
= talloc_named(kdc_db_ctx
, 0, "samba_kdc_check_pkinit_ms_upn_match");
1610 krb5_set_error_message(context
, ret
, "samba_kdc_fetch: talloc_named() failed!");
1614 ret
= samba_kdc_lookup_client(context
, kdc_db_ctx
,
1615 mem_ctx
, certificate_principal
,
1616 ms_upn_check_attrs
, &realm_dn
, &msg
);
1619 talloc_free(mem_ctx
);
1623 orig_sid
= samdb_result_dom_sid(mem_ctx
, p
->msg
, "objectSid");
1624 target_sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
1626 /* Consider these to be the same principal, even if by a different
1627 * name. The easy and safe way to prove this is by SID
1629 if (!(orig_sid
&& target_sid
&& dom_sid_equal(orig_sid
, target_sid
))) {
1630 talloc_free(mem_ctx
);
1631 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH
;
1634 talloc_free(mem_ctx
);
1638 NTSTATUS
samba_kdc_setup_db_ctx(TALLOC_CTX
*mem_ctx
, struct samba_kdc_base_context
*base_ctx
,
1639 struct samba_kdc_db_context
**kdc_db_ctx_out
)
1642 struct ldb_message
*msg
;
1643 struct auth_session_info
*session_info
;
1644 struct samba_kdc_db_context
*kdc_db_ctx
;
1645 /* The idea here is very simple. Using Kerberos to
1646 * authenticate the KDC to the LDAP server is higly likely to
1649 * In future we may set this up to use EXERNAL and SSL
1650 * certificates, for now it will almost certainly be NTLMSSP_SET_USERNAME
1653 kdc_db_ctx
= talloc_zero(mem_ctx
, struct samba_kdc_db_context
);
1654 if (kdc_db_ctx
== NULL
) {
1655 return NT_STATUS_NO_MEMORY
;
1657 kdc_db_ctx
->ev_ctx
= base_ctx
->ev_ctx
;
1658 kdc_db_ctx
->lp_ctx
= base_ctx
->lp_ctx
;
1660 kdc_get_policy(base_ctx
->lp_ctx
, NULL
, &kdc_db_ctx
->policy
);
1662 session_info
= system_session(kdc_db_ctx
->lp_ctx
);
1663 if (session_info
== NULL
) {
1664 return NT_STATUS_INTERNAL_ERROR
;
1667 /* Setup the link to LDB */
1668 kdc_db_ctx
->samdb
= samdb_connect(kdc_db_ctx
, base_ctx
->ev_ctx
,
1669 base_ctx
->lp_ctx
, session_info
, 0);
1670 if (kdc_db_ctx
->samdb
== NULL
) {
1671 DEBUG(1, ("hdb_samba4_create: Cannot open samdb for KDC backend!"));
1672 talloc_free(kdc_db_ctx
);
1673 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1676 /* Find out our own krbtgt kvno */
1677 ldb_ret
= samdb_rodc(kdc_db_ctx
->samdb
, &kdc_db_ctx
->rodc
);
1678 if (ldb_ret
!= LDB_SUCCESS
) {
1679 DEBUG(1, ("hdb_samba4_create: Cannot determine if we are an RODC in KDC backend: %s\n",
1680 ldb_errstring(kdc_db_ctx
->samdb
)));
1681 talloc_free(kdc_db_ctx
);
1682 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1684 if (kdc_db_ctx
->rodc
) {
1685 int my_krbtgt_number
;
1686 const char *secondary_keytab
[] = { "msDS-SecondaryKrbTgtNumber", NULL
};
1687 struct ldb_dn
*account_dn
;
1688 struct ldb_dn
*server_dn
= samdb_server_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
);
1690 DEBUG(1, ("hdb_samba4_create: Cannot determine server DN in KDC backend: %s\n",
1691 ldb_errstring(kdc_db_ctx
->samdb
)));
1692 talloc_free(kdc_db_ctx
);
1693 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1696 ldb_ret
= samdb_reference_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
, server_dn
,
1697 "serverReference", &account_dn
);
1698 if (ldb_ret
!= LDB_SUCCESS
) {
1699 DEBUG(1, ("hdb_samba4_create: Cannot determine server account in KDC backend: %s\n",
1700 ldb_errstring(kdc_db_ctx
->samdb
)));
1701 talloc_free(kdc_db_ctx
);
1702 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1705 ldb_ret
= samdb_reference_dn(kdc_db_ctx
->samdb
, kdc_db_ctx
, account_dn
,
1706 "msDS-KrbTgtLink", &kdc_db_ctx
->krbtgt_dn
);
1707 talloc_free(account_dn
);
1708 if (ldb_ret
!= LDB_SUCCESS
) {
1709 DEBUG(1, ("hdb_samba4_create: Cannot determine RODC krbtgt account in KDC backend: %s\n",
1710 ldb_errstring(kdc_db_ctx
->samdb
)));
1711 talloc_free(kdc_db_ctx
);
1712 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1715 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
, kdc_db_ctx
,
1716 &msg
, kdc_db_ctx
->krbtgt_dn
, LDB_SCOPE_BASE
,
1719 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=*))");
1720 if (ldb_ret
!= LDB_SUCCESS
) {
1721 DEBUG(1, ("hdb_samba4_create: Cannot read krbtgt account %s in KDC backend to get msDS-SecondaryKrbTgtNumber: %s: %s\n",
1722 ldb_dn_get_linearized(kdc_db_ctx
->krbtgt_dn
),
1723 ldb_errstring(kdc_db_ctx
->samdb
),
1724 ldb_strerror(ldb_ret
)));
1725 talloc_free(kdc_db_ctx
);
1726 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1728 my_krbtgt_number
= ldb_msg_find_attr_as_int(msg
, "msDS-SecondaryKrbTgtNumber", -1);
1729 if (my_krbtgt_number
== -1) {
1730 DEBUG(1, ("hdb_samba4_create: Cannot read msDS-SecondaryKrbTgtNumber from krbtgt account %s in KDC backend: got %d\n",
1731 ldb_dn_get_linearized(kdc_db_ctx
->krbtgt_dn
),
1733 talloc_free(kdc_db_ctx
);
1734 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1736 kdc_db_ctx
->my_krbtgt_number
= my_krbtgt_number
;
1739 kdc_db_ctx
->my_krbtgt_number
= 0;
1740 ldb_ret
= dsdb_search_one(kdc_db_ctx
->samdb
, kdc_db_ctx
,
1741 &msg
, NULL
, LDB_SCOPE_SUBTREE
,
1744 "(&(objectClass=user)(samAccountName=krbtgt))");
1746 if (ldb_ret
!= LDB_SUCCESS
) {
1747 DEBUG(1, ("samba_kdc_fetch: could not find own KRBTGT in DB: %s\n", ldb_errstring(kdc_db_ctx
->samdb
)));
1748 talloc_free(kdc_db_ctx
);
1749 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1751 kdc_db_ctx
->krbtgt_dn
= talloc_steal(kdc_db_ctx
, msg
->dn
);
1752 kdc_db_ctx
->my_krbtgt_number
= 0;
1755 *kdc_db_ctx_out
= kdc_db_ctx
;
1756 return NT_STATUS_OK
;