2 * Copyright (c) 1999-2001, 2003, PADL Software Pty Ltd.
3 * Copyright (c) 2004, Andrew Bartlett <abartlet@samba.org>.
4 * Copyright (c) 2004, Stefan Metzmacher <metze@samba.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of PADL Software nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "system/time.h"
37 #include "dsdb/common/flags.h"
38 #include "lib/ldb/include/ldb.h"
39 #include "lib/ldb/include/ldb_errors.h"
40 #include "librpc/gen_ndr/netlogon.h"
41 #include "auth/auth.h"
42 #include "auth/credentials/credentials.h"
43 #include "auth/auth_sam.h"
44 #include "../lib/util/util_ldb.h"
45 #include "dsdb/samdb/samdb.h"
46 #include "librpc/ndr/libndr.h"
47 #include "librpc/gen_ndr/ndr_drsblobs.h"
48 #include "librpc/gen_ndr/lsa.h"
49 #include "libcli/auth/libcli_auth.h"
50 #include "param/param.h"
51 #include "events/events.h"
53 #include "../lib/crypto/md4.h"
56 { HDB_SAMBA4_ENT_TYPE_CLIENT
, HDB_SAMBA4_ENT_TYPE_SERVER
,
57 HDB_SAMBA4_ENT_TYPE_KRBTGT
, HDB_SAMBA4_ENT_TYPE_TRUST
, HDB_SAMBA4_ENT_TYPE_ANY
};
59 enum trust_direction
{
61 INBOUND
= LSA_TRUST_DIRECTION_INBOUND
,
62 OUTBOUND
= LSA_TRUST_DIRECTION_OUTBOUND
65 static const char *realm_ref_attrs
[] = {
71 static const char *trust_attrs
[] = {
76 "msDS-SupportedEncryptionTypes",
83 static KerberosTime
ldb_msg_find_krb5time_ldap_time(struct ldb_message
*msg
, const char *attr
, KerberosTime default_val
)
89 gentime
= ldb_msg_find_attr_as_string(msg
, attr
, NULL
);
93 tmp
= strptime(gentime
, "%Y%m%d%H%M%SZ", &tm
);
101 static HDBFlags
uf2HDBFlags(krb5_context context
, int userAccountControl
, enum hdb_ldb_ent_type ent_type
)
103 HDBFlags flags
= int2HDBFlags(0);
105 /* we don't allow kadmin deletes */
108 /* mark the principal as invalid to start with */
113 /* All accounts are servers, but this may be disabled again in the caller */
116 /* Account types - clear the invalid bit if it turns out to be valid */
117 if (userAccountControl
& UF_NORMAL_ACCOUNT
) {
118 if (ent_type
== HDB_SAMBA4_ENT_TYPE_CLIENT
|| ent_type
== HDB_SAMBA4_ENT_TYPE_ANY
) {
124 if (userAccountControl
& UF_INTERDOMAIN_TRUST_ACCOUNT
) {
125 if (ent_type
== HDB_SAMBA4_ENT_TYPE_CLIENT
|| ent_type
== HDB_SAMBA4_ENT_TYPE_ANY
) {
130 if (userAccountControl
& UF_WORKSTATION_TRUST_ACCOUNT
) {
131 if (ent_type
== HDB_SAMBA4_ENT_TYPE_CLIENT
|| ent_type
== HDB_SAMBA4_ENT_TYPE_ANY
) {
136 if (userAccountControl
& UF_SERVER_TRUST_ACCOUNT
) {
137 if (ent_type
== HDB_SAMBA4_ENT_TYPE_CLIENT
|| ent_type
== HDB_SAMBA4_ENT_TYPE_ANY
) {
143 /* Not permitted to act as a client if disabled */
144 if (userAccountControl
& UF_ACCOUNTDISABLE
) {
147 if (userAccountControl
& UF_LOCKOUT
) {
151 if (userAccountControl & UF_PASSWORD_NOTREQD) {
156 UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
158 if (userAccountControl
& UF_TEMP_DUPLICATE_ACCOUNT
) {
162 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in LDB_message2entry() */
165 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
169 if (userAccountControl
& UF_SMARTCARD_REQUIRED
) {
170 flags
.require_hwauth
= 1;
172 if (userAccountControl
& UF_TRUSTED_FOR_DELEGATION
) {
173 flags
.ok_as_delegate
= 1;
175 if (!(userAccountControl
& UF_NOT_DELEGATED
)) {
176 flags
.forwardable
= 1;
180 if (userAccountControl
& UF_DONT_REQUIRE_PREAUTH
) {
181 flags
.require_preauth
= 0;
183 flags
.require_preauth
= 1;
189 static int hdb_ldb_destructor(struct hdb_ldb_private
*p
)
191 hdb_entry_ex
*entry_ex
= p
->entry_ex
;
192 free_hdb_entry(&entry_ex
->entry
);
196 static void hdb_ldb_free_entry(krb5_context context
, hdb_entry_ex
*entry_ex
)
198 talloc_free(entry_ex
->ctx
);
201 static krb5_error_code
LDB_message2entry_keys(krb5_context context
,
202 struct smb_iconv_convenience
*iconv_convenience
,
204 struct ldb_message
*msg
,
205 unsigned int userAccountControl
,
206 hdb_entry_ex
*entry_ex
)
208 krb5_error_code ret
= 0;
209 enum ndr_err_code ndr_err
;
210 struct samr_Password
*hash
;
211 const struct ldb_val
*sc_val
;
212 struct supplementalCredentialsBlob scb
;
213 struct supplementalCredentialsPackage
*scpk
= NULL
;
214 bool newer_keys
= false;
215 struct package_PrimaryKerberosBlob _pkb
;
216 struct package_PrimaryKerberosCtr3
*pkb3
= NULL
;
217 struct package_PrimaryKerberosCtr4
*pkb4
= NULL
;
219 uint32_t allocated_keys
= 0;
221 entry_ex
->entry
.keys
.val
= NULL
;
222 entry_ex
->entry
.keys
.len
= 0;
224 entry_ex
->entry
.kvno
= ldb_msg_find_attr_as_int(msg
, "msDS-KeyVersionNumber", 0);
226 /* Get keys from the db */
228 hash
= samdb_result_hash(mem_ctx
, msg
, "unicodePwd");
229 sc_val
= ldb_msg_find_ldb_val(msg
, "supplementalCredentials");
231 /* unicodePwd for enctype 0x17 (23) if present */
236 /* supplementalCredentials if present */
238 ndr_err
= ndr_pull_struct_blob_all(sc_val
, mem_ctx
, iconv_convenience
, &scb
,
239 (ndr_pull_flags_fn_t
)ndr_pull_supplementalCredentialsBlob
);
240 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
241 dump_data(0, sc_val
->data
, sc_val
->length
);
246 if (scb
.sub
.signature
!= SUPPLEMENTAL_CREDENTIALS_SIGNATURE
) {
247 NDR_PRINT_DEBUG(supplementalCredentialsBlob
, &scb
);
252 for (i
=0; i
< scb
.sub
.num_packages
; i
++) {
253 if (strcmp("Primary:Kerberos-Newer-Keys", scb
.sub
.packages
[i
].name
) == 0) {
254 scpk
= &scb
.sub
.packages
[i
];
255 if (!scpk
->data
|| !scpk
->data
[0]) {
261 } else if (strcmp("Primary:Kerberos", scb
.sub
.packages
[i
].name
) == 0) {
262 scpk
= &scb
.sub
.packages
[i
];
263 if (!scpk
->data
|| !scpk
->data
[0]) {
267 * we don't break here in hope to find
268 * a Kerberos-Newer-Keys package
274 * Primary:Kerberos-Newer-Keys or Primary:Kerberos element
275 * of supplementalCredentials
280 blob
= strhex_to_data_blob(mem_ctx
, scpk
->data
);
286 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
287 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, iconv_convenience
, &_pkb
,
288 (ndr_pull_flags_fn_t
)ndr_pull_package_PrimaryKerberosBlob
);
289 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
290 krb5_set_error_string(context
, "LDB_message2entry_keys: could not parse package_PrimaryKerberosBlob");
291 krb5_warnx(context
, "LDB_message2entry_keys: could not parse package_PrimaryKerberosBlob");
296 if (newer_keys
&& _pkb
.version
!= 4) {
297 krb5_set_error_string(context
, "LDB_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
298 krb5_warnx(context
, "LDB_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
303 if (!newer_keys
&& _pkb
.version
!= 3) {
304 krb5_set_error_string(context
, "LDB_message2entry_keys: could not parse Primary:Kerberos not version 3");
305 krb5_warnx(context
, "LDB_message2entry_keys: could not parse Primary:Kerberos not version 3");
310 if (_pkb
.version
== 4) {
311 pkb4
= &_pkb
.ctr
.ctr4
;
312 allocated_keys
+= pkb4
->num_keys
;
313 } else if (_pkb
.version
== 3) {
314 pkb3
= &_pkb
.ctr
.ctr3
;
315 allocated_keys
+= pkb3
->num_keys
;
319 if (allocated_keys
== 0) {
320 /* oh, no password. Apparently (comment in
321 * hdb-ldap.c) this violates the ASN.1, but this
322 * allows an entry with no keys (yet). */
326 /* allocate space to decode into */
327 entry_ex
->entry
.keys
.len
= 0;
328 entry_ex
->entry
.keys
.val
= calloc(allocated_keys
, sizeof(Key
));
329 if (entry_ex
->entry
.keys
.val
== NULL
) {
334 if (hash
&& !(userAccountControl
& UF_USE_DES_KEY_ONLY
)) {
338 key
.salt
= NULL
; /* No salt for this enc type */
340 ret
= krb5_keyblock_init(context
,
341 ENCTYPE_ARCFOUR_HMAC_MD5
,
342 hash
->hash
, sizeof(hash
->hash
),
348 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
349 entry_ex
->entry
.keys
.len
++;
353 for (i
=0; i
< pkb4
->num_keys
; i
++) {
357 if (!pkb4
->keys
[i
].value
) continue;
359 if (userAccountControl
& UF_USE_DES_KEY_ONLY
) {
360 switch (pkb4
->keys
[i
].keytype
) {
361 case ENCTYPE_DES_CBC_CRC
:
362 case ENCTYPE_DES_CBC_MD5
:
375 if (pkb4
->salt
.string
) {
378 salt
= data_blob_string_const(pkb4
->salt
.string
);
380 key
.salt
= calloc(1, sizeof(*key
.salt
));
381 if (key
.salt
== NULL
) {
386 key
.salt
->type
= hdb_pw_salt
;
388 ret
= krb5_data_copy(&key
.salt
->salt
, salt
.data
, salt
.length
);
396 /* TODO: maybe pass the iteration_count somehow... */
398 ret
= krb5_keyblock_init(context
,
399 pkb4
->keys
[i
].keytype
,
400 pkb4
->keys
[i
].value
->data
,
401 pkb4
->keys
[i
].value
->length
,
412 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
413 entry_ex
->entry
.keys
.len
++;
416 for (i
=0; i
< pkb3
->num_keys
; i
++) {
420 if (!pkb3
->keys
[i
].value
) continue;
422 if (userAccountControl
& UF_USE_DES_KEY_ONLY
) {
423 switch (pkb3
->keys
[i
].keytype
) {
424 case ENCTYPE_DES_CBC_CRC
:
425 case ENCTYPE_DES_CBC_MD5
:
438 if (pkb3
->salt
.string
) {
441 salt
= data_blob_string_const(pkb3
->salt
.string
);
443 key
.salt
= calloc(1, sizeof(*key
.salt
));
444 if (key
.salt
== NULL
) {
449 key
.salt
->type
= hdb_pw_salt
;
451 ret
= krb5_data_copy(&key
.salt
->salt
, salt
.data
, salt
.length
);
459 ret
= krb5_keyblock_init(context
,
460 pkb3
->keys
[i
].keytype
,
461 pkb3
->keys
[i
].value
->data
,
462 pkb3
->keys
[i
].value
->length
,
473 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
474 entry_ex
->entry
.keys
.len
++;
480 entry_ex
->entry
.keys
.len
= 0;
482 if (entry_ex
->entry
.keys
.len
== 0 && entry_ex
->entry
.keys
.val
) {
483 free(entry_ex
->entry
.keys
.val
);
484 entry_ex
->entry
.keys
.val
= NULL
;
490 * Construct an hdb_entry from a directory entry.
492 static krb5_error_code
LDB_message2entry(krb5_context context
, HDB
*db
,
493 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
494 enum hdb_ldb_ent_type ent_type
,
495 struct ldb_message
*msg
,
496 struct ldb_message
*realm_ref_msg
,
497 hdb_entry_ex
*entry_ex
)
499 unsigned int userAccountControl
;
501 krb5_error_code ret
= 0;
502 krb5_boolean is_computer
= FALSE
;
503 const char *dnsdomain
= ldb_msg_find_attr_as_string(realm_ref_msg
, "dnsRoot", NULL
);
504 char *realm
= strupper_talloc(mem_ctx
, dnsdomain
);
505 struct loadparm_context
*lp_ctx
= ldb_get_opaque((struct ldb_context
*)db
->hdb_db
, "loadparm");
506 struct ldb_dn
*domain_dn
= samdb_result_dn((struct ldb_context
*)db
->hdb_db
,
510 ldb_dn_new(mem_ctx
, (struct ldb_context
*)db
->hdb_db
, NULL
));
512 struct hdb_ldb_private
*p
;
515 struct ldb_message_element
*objectclasses
;
516 struct ldb_val computer_val
;
517 computer_val
.data
= discard_const_p(uint8_t,"computer");
518 computer_val
.length
= strlen((const char *)computer_val
.data
);
520 objectclasses
= ldb_msg_find_element(msg
, "objectClass");
522 if (objectclasses
&& ldb_msg_find_val(objectclasses
, &computer_val
)) {
526 memset(entry_ex
, 0, sizeof(*entry_ex
));
529 krb5_set_error_string(context
, "talloc_strdup: out of memory");
534 p
= talloc(mem_ctx
, struct hdb_ldb_private
);
540 p
->entry_ex
= entry_ex
;
541 p
->iconv_convenience
= lp_iconv_convenience(lp_ctx
);
542 p
->netbios_name
= lp_netbios_name(lp_ctx
);
544 talloc_set_destructor(p
, hdb_ldb_destructor
);
547 entry_ex
->free_entry
= hdb_ldb_free_entry
;
549 userAccountControl
= ldb_msg_find_attr_as_uint(msg
, "userAccountControl", 0);
552 entry_ex
->entry
.principal
= malloc(sizeof(*(entry_ex
->entry
.principal
)));
553 if (ent_type
== HDB_SAMBA4_ENT_TYPE_ANY
&& principal
== NULL
) {
554 const char *samAccountName
= ldb_msg_find_attr_as_string(msg
, "samAccountName", NULL
);
555 if (!samAccountName
) {
556 krb5_set_error_string(context
, "LDB_message2entry: no samAccountName present");
560 samAccountName
= ldb_msg_find_attr_as_string(msg
, "samAccountName", NULL
);
561 krb5_make_principal(context
, &entry_ex
->entry
.principal
, realm
, samAccountName
, NULL
);
564 ret
= copy_Principal(principal
, entry_ex
->entry
.principal
);
566 krb5_clear_error_string(context
);
570 /* While we have copied the client principal, tests
571 * show that Win2k3 returns the 'corrected' realm, not
572 * the client-specified realm. This code attempts to
573 * replace the client principal's realm with the one
574 * we determine from our records */
576 /* this has to be with malloc() */
577 strdup_realm
= strdup(realm
);
580 krb5_clear_error_string(context
);
583 free(*krb5_princ_realm(context
, entry_ex
->entry
.principal
));
584 krb5_princ_set_realm(context
, entry_ex
->entry
.principal
, &strdup_realm
);
587 entry_ex
->entry
.flags
= uf2HDBFlags(context
, userAccountControl
, ent_type
);
589 if (ent_type
== HDB_SAMBA4_ENT_TYPE_KRBTGT
) {
590 entry_ex
->entry
.flags
.invalid
= 0;
591 entry_ex
->entry
.flags
.server
= 1;
592 entry_ex
->entry
.flags
.forwardable
= 1;
593 entry_ex
->entry
.flags
.ok_as_delegate
= 1;
596 if (lp_parm_bool(lp_ctx
, NULL
, "kdc", "require spn for service", true)) {
597 if (!is_computer
&& !ldb_msg_find_attr_as_string(msg
, "servicePrincipalName", NULL
)) {
598 entry_ex
->entry
.flags
.server
= 0;
602 /* use 'whenCreated' */
603 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
605 entry_ex
->entry
.created_by
.principal
= NULL
;
607 entry_ex
->entry
.modified_by
= (Event
*) malloc(sizeof(Event
));
608 if (entry_ex
->entry
.modified_by
== NULL
) {
609 krb5_set_error_string(context
, "malloc: out of memory");
614 /* use 'whenChanged' */
615 entry_ex
->entry
.modified_by
->time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenChanged", 0);
617 entry_ex
->entry
.modified_by
->principal
= NULL
;
619 entry_ex
->entry
.valid_start
= NULL
;
621 acct_expiry
= samdb_result_account_expires(msg
);
622 if (acct_expiry
== 0x7FFFFFFFFFFFFFFFULL
) {
623 entry_ex
->entry
.valid_end
= NULL
;
625 entry_ex
->entry
.valid_end
= malloc(sizeof(*entry_ex
->entry
.valid_end
));
626 if (entry_ex
->entry
.valid_end
== NULL
) {
630 *entry_ex
->entry
.valid_end
= nt_time_to_unix(acct_expiry
);
633 if (ent_type
!= HDB_SAMBA4_ENT_TYPE_KRBTGT
) {
634 NTTIME must_change_time
635 = samdb_result_force_password_change((struct ldb_context
*)db
->hdb_db
, mem_ctx
,
637 if (must_change_time
== 0x7FFFFFFFFFFFFFFFULL
) {
638 entry_ex
->entry
.pw_end
= NULL
;
640 entry_ex
->entry
.pw_end
= malloc(sizeof(*entry_ex
->entry
.pw_end
));
641 if (entry_ex
->entry
.pw_end
== NULL
) {
645 *entry_ex
->entry
.pw_end
= nt_time_to_unix(must_change_time
);
648 entry_ex
->entry
.pw_end
= NULL
;
651 entry_ex
->entry
.max_life
= NULL
;
653 entry_ex
->entry
.max_renew
= NULL
;
655 entry_ex
->entry
.generation
= NULL
;
657 /* Get keys from the db */
658 ret
= LDB_message2entry_keys(context
, p
->iconv_convenience
, p
, msg
, userAccountControl
, entry_ex
);
660 /* Could be bougus data in the entry, or out of memory */
664 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
665 if (entry_ex
->entry
.etypes
== NULL
) {
666 krb5_clear_error_string(context
);
670 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
671 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
672 if (entry_ex
->entry
.etypes
->val
== NULL
) {
673 krb5_clear_error_string(context
);
677 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
678 entry_ex
->entry
.etypes
->val
[i
] = entry_ex
->entry
.keys
.val
[i
].key
.keytype
;
682 p
->msg
= talloc_steal(p
, msg
);
683 p
->realm_ref_msg
= talloc_steal(p
, realm_ref_msg
);
684 p
->samdb
= (struct ldb_context
*)db
->hdb_db
;
688 /* This doesn't free ent itself, that is for the eventual caller to do */
689 hdb_free_entry(context
, entry_ex
);
691 talloc_steal(db
, entry_ex
->ctx
);
698 * Construct an hdb_entry from a directory entry.
700 static krb5_error_code
LDB_trust_message2entry(krb5_context context
, HDB
*db
,
701 struct loadparm_context
*lp_ctx
,
702 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
703 enum trust_direction direction
,
704 struct ldb_message
*msg
,
705 hdb_entry_ex
*entry_ex
)
708 const char *dnsdomain
;
711 DATA_BLOB password_utf16
;
712 struct samr_Password password_hash
;
713 const struct ldb_val
*password_val
;
714 struct trustAuthInOutBlob password_blob
;
715 struct hdb_ldb_private
*p
;
717 enum ndr_err_code ndr_err
;
718 int i
, ret
, trust_direction_flags
;
720 p
= talloc(mem_ctx
, struct hdb_ldb_private
);
726 p
->entry_ex
= entry_ex
;
727 p
->iconv_convenience
= lp_iconv_convenience(lp_ctx
);
728 p
->netbios_name
= lp_netbios_name(lp_ctx
);
730 talloc_set_destructor(p
, hdb_ldb_destructor
);
733 entry_ex
->free_entry
= hdb_ldb_free_entry
;
735 /* use 'whenCreated' */
736 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
738 entry_ex
->entry
.created_by
.principal
= NULL
;
740 entry_ex
->entry
.valid_start
= NULL
;
742 trust_direction_flags
= ldb_msg_find_attr_as_int(msg
, "trustDirection", 0);
744 if (direction
== INBOUND
) {
745 realm
= strupper_talloc(mem_ctx
, lp_realm(lp_ctx
));
746 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthIncoming");
748 } else { /* OUTBOUND */
749 dnsdomain
= ldb_msg_find_attr_as_string(msg
, "trustPartner", NULL
);
750 realm
= strupper_talloc(mem_ctx
, dnsdomain
);
751 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthOutgoing");
754 if (!password_val
|| !(trust_direction_flags
& direction
)) {
759 ndr_err
= ndr_pull_struct_blob(password_val
, mem_ctx
, p
->iconv_convenience
, &password_blob
,
760 (ndr_pull_flags_fn_t
)ndr_pull_trustAuthInOutBlob
);
761 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
766 entry_ex
->entry
.kvno
= -1;
767 for (i
=0; i
< password_blob
.count
; i
++) {
768 if (password_blob
.current
->array
[i
].AuthType
== TRUST_AUTH_TYPE_VERSION
) {
769 entry_ex
->entry
.kvno
= password_blob
.current
->array
[i
].AuthInfo
.version
.version
;
773 for (i
=0; i
< password_blob
.count
; i
++) {
774 if (password_blob
.current
->array
[i
].AuthType
== TRUST_AUTH_TYPE_CLEAR
) {
775 password_utf16
= data_blob_const(password_blob
.current
->array
[i
].AuthInfo
.clear
.password
,
776 password_blob
.current
->array
[i
].AuthInfo
.clear
.size
);
777 /* In the future, generate all sorts of
778 * hashes, but for now we can't safely convert
779 * the random strings windows uses into
782 /* but as it is utf16 already, we can get the NT password/arcfour-hmac-md5 key */
783 mdfour(password_hash
.hash
, password_utf16
.data
, password_utf16
.length
);
785 } else if (password_blob
.current
->array
[i
].AuthType
== TRUST_AUTH_TYPE_NT4OWF
) {
786 password_hash
= password_blob
.current
->array
[i
].AuthInfo
.nt4owf
.password
;
790 entry_ex
->entry
.keys
.len
= 0;
791 entry_ex
->entry
.keys
.val
= NULL
;
793 if (i
< password_blob
.count
) {
795 /* Must have found a cleartext or MD4 password */
796 entry_ex
->entry
.keys
.val
= calloc(1, sizeof(Key
));
799 key
.salt
= NULL
; /* No salt for this enc type */
801 if (entry_ex
->entry
.keys
.val
== NULL
) {
806 ret
= krb5_keyblock_init(context
,
807 ENCTYPE_ARCFOUR_HMAC_MD5
,
808 password_hash
.hash
, sizeof(password_hash
.hash
),
811 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
812 entry_ex
->entry
.keys
.len
++;
815 entry_ex
->entry
.principal
= malloc(sizeof(*(entry_ex
->entry
.principal
)));
817 ret
= copy_Principal(principal
, entry_ex
->entry
.principal
);
819 krb5_clear_error_string(context
);
823 /* While we have copied the client principal, tests
824 * show that Win2k3 returns the 'corrected' realm, not
825 * the client-specified realm. This code attempts to
826 * replace the client principal's realm with the one
827 * we determine from our records */
829 /* this has to be with malloc() */
830 strdup_realm
= strdup(realm
);
833 krb5_clear_error_string(context
);
836 free(*krb5_princ_realm(context
, entry_ex
->entry
.principal
));
837 krb5_princ_set_realm(context
, entry_ex
->entry
.principal
, &strdup_realm
);
839 entry_ex
->entry
.flags
= int2HDBFlags(0);
840 entry_ex
->entry
.flags
.immutable
= 1;
841 entry_ex
->entry
.flags
.invalid
= 0;
842 entry_ex
->entry
.flags
.server
= 1;
843 entry_ex
->entry
.flags
.require_preauth
= 1;
845 entry_ex
->entry
.pw_end
= NULL
;
847 entry_ex
->entry
.max_life
= NULL
;
849 entry_ex
->entry
.max_renew
= NULL
;
851 entry_ex
->entry
.generation
= NULL
;
853 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
854 if (entry_ex
->entry
.etypes
== NULL
) {
855 krb5_clear_error_string(context
);
859 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
860 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
861 if (entry_ex
->entry
.etypes
->val
== NULL
) {
862 krb5_clear_error_string(context
);
866 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
867 entry_ex
->entry
.etypes
->val
[i
] = entry_ex
->entry
.keys
.val
[i
].key
.keytype
;
871 p
->msg
= talloc_steal(p
, msg
);
872 p
->realm_ref_msg
= NULL
;
873 p
->samdb
= (struct ldb_context
*)db
->hdb_db
;
877 /* This doesn't free ent itself, that is for the eventual caller to do */
878 hdb_free_entry(context
, entry_ex
);
880 talloc_steal(db
, entry_ex
->ctx
);
887 static krb5_error_code
LDB_lookup_principal(krb5_context context
, struct ldb_context
*ldb_ctx
,
889 krb5_const_principal principal
,
890 enum hdb_ldb_ent_type ent_type
,
891 struct ldb_dn
*realm_dn
,
892 struct ldb_message
***pmsg
)
897 const char * const *princ_attrs
= user_attrs
;
900 char *short_princ_talloc
;
902 struct ldb_result
*res
= NULL
;
904 ret
= krb5_unparse_name_flags(context
, principal
, KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &short_princ
);
907 krb5_set_error_string(context
, "LDB_lookup_principal: could not parse principal");
908 krb5_warnx(context
, "LDB_lookup_principal: could not parse principal");
912 short_princ_talloc
= talloc_strdup(mem_ctx
, short_princ
);
914 if (!short_princ_talloc
) {
915 krb5_set_error_string(context
, "LDB_lookup_principal: talloc_strdup() failed!");
920 case HDB_SAMBA4_ENT_TYPE_CLIENT
:
921 case HDB_SAMBA4_ENT_TYPE_TRUST
:
922 case HDB_SAMBA4_ENT_TYPE_ANY
:
925 case HDB_SAMBA4_ENT_TYPE_KRBTGT
:
926 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=user)(samAccountName=%s))",
929 case HDB_SAMBA4_ENT_TYPE_SERVER
:
930 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=user)(samAccountName=%s))",
936 krb5_set_error_string(context
, "talloc_asprintf: out of memory");
940 lret
= ldb_search(ldb_ctx
, mem_ctx
, &res
, realm_dn
,
941 LDB_SCOPE_SUBTREE
, princ_attrs
, "%s", filter
);
942 if (lret
!= LDB_SUCCESS
) {
943 DEBUG(3, ("Failed to search for %s: %s\n", filter
, ldb_errstring(ldb_ctx
)));
944 return HDB_ERR_NOENTRY
;
945 } else if (res
->count
== 0 || res
->count
> 1) {
946 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter
, res
->count
));
948 return HDB_ERR_NOENTRY
;
950 talloc_steal(mem_ctx
, res
->msgs
);
956 static krb5_error_code
LDB_lookup_trust(krb5_context context
, struct ldb_context
*ldb_ctx
,
959 struct ldb_dn
*realm_dn
,
960 struct ldb_message
***pmsg
)
964 const char * const *attrs
= trust_attrs
;
966 struct ldb_result
*res
= NULL
;
967 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=trustedDomain)(|(flatname=%s)(trustPartner=%s)))", realm
, realm
);
970 krb5_set_error_string(context
, "talloc_asprintf: out of memory");
974 lret
= ldb_search(ldb_ctx
, mem_ctx
, &res
,
975 ldb_get_default_basedn(ldb_ctx
),
976 LDB_SCOPE_SUBTREE
, attrs
, "%s", filter
);
977 if (lret
!= LDB_SUCCESS
) {
978 DEBUG(3, ("Failed to search for %s: %s\n", filter
, ldb_errstring(ldb_ctx
)));
979 return HDB_ERR_NOENTRY
;
980 } else if (res
->count
== 0 || res
->count
> 1) {
981 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter
, res
->count
));
983 return HDB_ERR_NOENTRY
;
985 talloc_steal(mem_ctx
, res
->msgs
);
991 static krb5_error_code
LDB_lookup_realm(krb5_context context
, struct ldb_context
*ldb_ctx
,
994 struct ldb_message
***pmsg
)
997 struct ldb_result
*cross_ref_res
;
998 struct ldb_dn
*partitions_basedn
= samdb_partitions_dn(ldb_ctx
, mem_ctx
);
1000 ret
= ldb_search(ldb_ctx
, mem_ctx
, &cross_ref_res
,
1001 partitions_basedn
, LDB_SCOPE_SUBTREE
, realm_ref_attrs
,
1002 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
1005 if (ret
!= LDB_SUCCESS
) {
1006 DEBUG(3, ("Failed to search to lookup realm(%s): %s\n", realm
, ldb_errstring(ldb_ctx
)));
1007 talloc_free(cross_ref_res
);
1008 return HDB_ERR_NOENTRY
;
1009 } else if (cross_ref_res
->count
== 0 || cross_ref_res
->count
> 1) {
1010 DEBUG(3, ("Failed find a single entry for realm %s: got %d\n", realm
, cross_ref_res
->count
));
1011 talloc_free(cross_ref_res
);
1012 return HDB_ERR_NOENTRY
;
1016 *pmsg
= cross_ref_res
->msgs
;
1017 talloc_steal(mem_ctx
, cross_ref_res
->msgs
);
1019 talloc_free(cross_ref_res
);
1025 static krb5_error_code
LDB_open(krb5_context context
, HDB
*db
, int flags
, mode_t mode
)
1027 if (db
->hdb_master_key_set
) {
1028 krb5_warnx(context
, "LDB_open: use of a master key incompatible with LDB\n");
1029 krb5_set_error_string(context
, "LDB_open: use of a master key incompatible with LDB\n");
1030 return HDB_ERR_NOENTRY
;
1036 static krb5_error_code
LDB_close(krb5_context context
, HDB
*db
)
1041 static krb5_error_code
LDB_lock(krb5_context context
, HDB
*db
, int operation
)
1046 static krb5_error_code
LDB_unlock(krb5_context context
, HDB
*db
)
1051 static krb5_error_code
LDB_rename(krb5_context context
, HDB
*db
, const char *new_name
)
1053 return HDB_ERR_DB_INUSE
;
1056 static krb5_error_code
LDB_fetch_client(krb5_context context
, HDB
*db
,
1057 TALLOC_CTX
*mem_ctx
,
1058 krb5_const_principal principal
,
1060 hdb_entry_ex
*entry_ex
) {
1062 char *principal_string
;
1063 krb5_error_code ret
;
1064 struct ldb_message
**msg
= NULL
;
1065 struct ldb_message
**realm_ref_msg
= NULL
;
1067 ret
= krb5_unparse_name(context
, principal
, &principal_string
);
1073 nt_status
= sam_get_results_principal((struct ldb_context
*)db
->hdb_db
,
1074 mem_ctx
, principal_string
,
1075 &msg
, &realm_ref_msg
);
1076 free(principal_string
);
1077 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
1078 return HDB_ERR_NOENTRY
;
1079 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_MEMORY
)) {
1081 } else if (!NT_STATUS_IS_OK(nt_status
)) {
1085 ret
= LDB_message2entry(context
, db
, mem_ctx
,
1086 principal
, HDB_SAMBA4_ENT_TYPE_CLIENT
,
1087 msg
[0], realm_ref_msg
[0], entry_ex
);
1091 static krb5_error_code
LDB_fetch_krbtgt(krb5_context context
, HDB
*db
,
1092 TALLOC_CTX
*mem_ctx
,
1093 krb5_const_principal principal
,
1095 hdb_entry_ex
*entry_ex
)
1097 krb5_error_code ret
;
1098 struct ldb_message
**msg
= NULL
;
1099 struct ldb_message
**realm_ref_msg_1
= NULL
;
1100 struct ldb_message
**realm_ref_msg_2
= NULL
;
1101 struct ldb_dn
*realm_dn
;
1104 krb5_principal alloc_principal
= NULL
;
1105 if (principal
->name
.name_string
.len
!= 2
1106 || (strcmp(principal
->name
.name_string
.val
[0], KRB5_TGS_NAME
) != 0)) {
1108 return HDB_ERR_NOENTRY
;
1111 /* krbtgt case. Either us or a trusted realm */
1113 if ((LDB_lookup_realm(context
, (struct ldb_context
*)db
->hdb_db
,
1114 mem_ctx
, principal
->realm
, &realm_ref_msg_1
) == 0)
1115 && (LDB_lookup_realm(context
, (struct ldb_context
*)db
->hdb_db
,
1116 mem_ctx
, principal
->name
.name_string
.val
[1], &realm_ref_msg_2
) == 0)
1117 && (ldb_dn_compare(realm_ref_msg_1
[0]->dn
, realm_ref_msg_1
[0]->dn
) == 0)) {
1119 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1120 * is in our db, then direct the caller at our primary
1123 const char *dnsdomain
= ldb_msg_find_attr_as_string(realm_ref_msg_1
[0], "dnsRoot", NULL
);
1124 char *realm_fixed
= strupper_talloc(mem_ctx
, dnsdomain
);
1126 krb5_set_error_string(context
, "strupper_talloc: out of memory");
1130 ret
= krb5_copy_principal(context
, principal
, &alloc_principal
);
1135 free(alloc_principal
->name
.name_string
.val
[1]);
1136 alloc_principal
->name
.name_string
.val
[1] = strdup(realm_fixed
);
1137 talloc_free(realm_fixed
);
1138 if (!alloc_principal
->name
.name_string
.val
[1]) {
1139 krb5_set_error_string(context
, "LDB_fetch: strdup() failed!");
1142 principal
= alloc_principal
;
1143 realm_dn
= samdb_result_dn((struct ldb_context
*)db
->hdb_db
, mem_ctx
, realm_ref_msg_1
[0], "nCName", NULL
);
1145 ret
= LDB_lookup_principal(context
, (struct ldb_context
*)db
->hdb_db
,
1147 principal
, HDB_SAMBA4_ENT_TYPE_KRBTGT
, realm_dn
, &msg
);
1150 krb5_warnx(context
, "LDB_fetch: could not find principal in DB");
1151 krb5_set_error_string(context
, "LDB_fetch: could not find principal in DB");
1155 ret
= LDB_message2entry(context
, db
, mem_ctx
,
1156 principal
, HDB_SAMBA4_ENT_TYPE_KRBTGT
,
1157 msg
[0], realm_ref_msg_1
[0], entry_ex
);
1159 krb5_warnx(context
, "LDB_fetch: self krbtgt message2entry failed");
1164 enum trust_direction direction
= UNKNOWN
;
1166 struct loadparm_context
*lp_ctx
= talloc_get_type(ldb_get_opaque(db
->hdb_db
, "loadparm"), struct loadparm_context
);
1167 /* Either an inbound or outbound trust */
1169 if (strcasecmp(lp_realm(lp_ctx
), principal
->realm
) == 0) {
1170 /* look for inbound trust */
1171 direction
= INBOUND
;
1172 realm
= principal
->name
.name_string
.val
[1];
1175 if (strcasecmp(lp_realm(lp_ctx
), principal
->name
.name_string
.val
[1]) == 0) {
1176 /* look for outbound trust */
1177 direction
= OUTBOUND
;
1178 realm
= principal
->realm
;
1181 /* Trusted domains are under CN=system */
1183 ret
= LDB_lookup_trust(context
, (struct ldb_context
*)db
->hdb_db
,
1185 realm
, realm_dn
, &msg
);
1188 krb5_warnx(context
, "LDB_fetch: could not find principal in DB");
1189 krb5_set_error_string(context
, "LDB_fetch: could not find principal in DB");
1193 ret
= LDB_trust_message2entry(context
, db
, lp_ctx
, mem_ctx
,
1194 principal
, direction
,
1197 krb5_warnx(context
, "LDB_fetch: trust_message2entry failed");
1202 /* we should lookup trusted domains */
1203 return HDB_ERR_NOENTRY
;
1208 static krb5_error_code
LDB_fetch_server(krb5_context context
, HDB
*db
,
1209 TALLOC_CTX
*mem_ctx
,
1210 krb5_const_principal principal
,
1212 hdb_entry_ex
*entry_ex
)
1214 krb5_error_code ret
;
1216 struct ldb_message
**msg
= NULL
;
1217 struct ldb_message
**realm_ref_msg
= NULL
;
1218 struct ldb_dn
*partitions_basedn
= samdb_partitions_dn(db
->hdb_db
, mem_ctx
);
1219 if (principal
->name
.name_string
.len
>= 2) {
1220 /* 'normal server' case */
1223 struct ldb_dn
*user_dn
, *domain_dn
;
1224 char *principal_string
;
1226 ret
= krb5_unparse_name_flags(context
, principal
,
1227 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
1233 /* At this point we may find the host is known to be
1234 * in a different realm, so we should generate a
1235 * referral instead */
1236 nt_status
= crack_service_principal_name((struct ldb_context
*)db
->hdb_db
,
1237 mem_ctx
, principal_string
,
1238 &user_dn
, &domain_dn
);
1239 free(principal_string
);
1241 if (!NT_STATUS_IS_OK(nt_status
)) {
1242 return HDB_ERR_NOENTRY
;
1245 ldb_ret
= gendb_search_dn((struct ldb_context
*)db
->hdb_db
,
1246 mem_ctx
, user_dn
, &msg
, user_attrs
);
1249 return HDB_ERR_NOENTRY
;
1252 ldb_ret
= gendb_search((struct ldb_context
*)db
->hdb_db
,
1253 mem_ctx
, partitions_basedn
, &realm_ref_msg
, realm_ref_attrs
,
1254 "ncName=%s", ldb_dn_get_linearized(domain_dn
));
1257 return HDB_ERR_NOENTRY
;
1261 struct ldb_dn
*realm_dn
;
1262 /* server as client principal case, but we must not lookup userPrincipalNames */
1264 realm
= krb5_principal_get_realm(context
, principal
);
1266 ret
= LDB_lookup_realm(context
, (struct ldb_context
*)db
->hdb_db
,
1267 mem_ctx
, realm
, &realm_ref_msg
);
1269 return HDB_ERR_NOENTRY
;
1272 realm_dn
= samdb_result_dn((struct ldb_context
*)db
->hdb_db
, mem_ctx
, realm_ref_msg
[0], "nCName", NULL
);
1274 ret
= LDB_lookup_principal(context
, (struct ldb_context
*)db
->hdb_db
,
1276 principal
, HDB_SAMBA4_ENT_TYPE_SERVER
, realm_dn
, &msg
);
1283 ret
= LDB_message2entry(context
, db
, mem_ctx
,
1284 principal
, HDB_SAMBA4_ENT_TYPE_SERVER
,
1285 msg
[0], realm_ref_msg
[0], entry_ex
);
1287 krb5_warnx(context
, "LDB_fetch: message2entry failed");
1293 static krb5_error_code
LDB_fetch(krb5_context context
, HDB
*db
,
1294 krb5_const_principal principal
,
1296 hdb_entry_ex
*entry_ex
)
1298 krb5_error_code ret
= HDB_ERR_NOENTRY
;
1300 TALLOC_CTX
*mem_ctx
= talloc_named(db
, 0, "LDB_fetch context");
1303 krb5_set_error_string(context
, "LDB_fetch: talloc_named() failed!");
1307 if (flags
& HDB_F_GET_CLIENT
) {
1308 ret
= LDB_fetch_client(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
1309 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1311 if (flags
& HDB_F_GET_SERVER
) {
1312 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
1313 ret
= LDB_fetch_krbtgt(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
1314 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1316 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
1317 ret
= LDB_fetch_server(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
1318 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1320 if (flags
& HDB_F_GET_KRBTGT
) {
1321 ret
= LDB_fetch_krbtgt(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
1322 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1326 talloc_free(mem_ctx
);
1330 static krb5_error_code
LDB_store(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry_ex
*entry
)
1332 return HDB_ERR_DB_INUSE
;
1335 static krb5_error_code
LDB_remove(krb5_context context
, HDB
*db
, krb5_const_principal principal
)
1337 return HDB_ERR_DB_INUSE
;
1340 struct hdb_ldb_seq
{
1341 struct ldb_context
*ctx
;
1344 struct ldb_message
**msgs
;
1345 struct ldb_message
**realm_ref_msgs
;
1348 static krb5_error_code
LDB_seq(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry_ex
*entry
)
1350 krb5_error_code ret
;
1351 struct hdb_ldb_seq
*priv
= (struct hdb_ldb_seq
*)db
->hdb_dbc
;
1352 TALLOC_CTX
*mem_ctx
;
1353 hdb_entry_ex entry_ex
;
1354 memset(&entry_ex
, '\0', sizeof(entry_ex
));
1357 return HDB_ERR_NOENTRY
;
1360 mem_ctx
= talloc_named(priv
, 0, "LDB_seq context");
1363 krb5_set_error_string(context
, "LDB_seq: talloc_named() failed!");
1367 if (priv
->index
< priv
->count
) {
1368 ret
= LDB_message2entry(context
, db
, mem_ctx
,
1369 NULL
, HDB_SAMBA4_ENT_TYPE_ANY
,
1370 priv
->msgs
[priv
->index
++],
1371 priv
->realm_ref_msgs
[0], entry
);
1373 ret
= HDB_ERR_NOENTRY
;
1380 talloc_free(mem_ctx
);
1386 static krb5_error_code
LDB_firstkey(krb5_context context
, HDB
*db
, unsigned flags
,
1387 hdb_entry_ex
*entry
)
1389 struct ldb_context
*ldb_ctx
= (struct ldb_context
*)db
->hdb_db
;
1390 struct hdb_ldb_seq
*priv
= (struct hdb_ldb_seq
*)db
->hdb_dbc
;
1392 struct ldb_dn
*realm_dn
= NULL
;
1393 struct ldb_result
*res
= NULL
;
1394 struct ldb_message
**realm_ref_msgs
= NULL
;
1395 krb5_error_code ret
;
1396 TALLOC_CTX
*mem_ctx
;
1404 priv
= (struct hdb_ldb_seq
*) talloc(db
, struct hdb_ldb_seq
);
1406 krb5_set_error_string(context
, "talloc: out of memory");
1410 priv
->ctx
= ldb_ctx
;
1413 priv
->realm_ref_msgs
= NULL
;
1416 mem_ctx
= talloc_named(priv
, 0, "LDB_firstkey context");
1419 krb5_set_error_string(context
, "LDB_firstkey: talloc_named() failed!");
1423 ret
= krb5_get_default_realm(context
, &realm
);
1429 ret
= LDB_lookup_realm(context
, (struct ldb_context
*)db
->hdb_db
,
1430 mem_ctx
, realm
, &realm_ref_msgs
);
1436 krb5_warnx(context
, "LDB_firstkey: could not find realm\n");
1437 return HDB_ERR_NOENTRY
;
1440 realm_dn
= samdb_result_dn((struct ldb_context
*)db
->hdb_db
, mem_ctx
, realm_ref_msgs
[0], "nCName", NULL
);
1442 priv
->realm_ref_msgs
= talloc_steal(priv
, realm_ref_msgs
);
1444 lret
= ldb_search(ldb_ctx
, priv
, &res
,
1445 realm_dn
, LDB_SCOPE_SUBTREE
, user_attrs
,
1446 "(objectClass=user)");
1448 if (lret
!= LDB_SUCCESS
) {
1450 return HDB_ERR_NOENTRY
;
1453 priv
->count
= res
->count
;
1454 priv
->msgs
= talloc_steal(priv
, res
->msgs
);
1459 ret
= LDB_seq(context
, db
, flags
, entry
);
1465 talloc_free(mem_ctx
);
1470 static krb5_error_code
LDB_nextkey(krb5_context context
, HDB
*db
, unsigned flags
,
1471 hdb_entry_ex
*entry
)
1473 return LDB_seq(context
, db
, flags
, entry
);
1476 static krb5_error_code
LDB_destroy(krb5_context context
, HDB
*db
)
1482 /* This interface is to be called by the KDC, which is expecting Samba
1483 * calling conventions. It is also called by a wrapper
1484 * (hdb_ldb_create) from the kpasswdd -> krb5 -> keytab_hdb -> hdb
1487 NTSTATUS
kdc_hdb_samba4_create(TALLOC_CTX
*mem_ctx
,
1488 struct tevent_context
*ev_ctx
,
1489 struct loadparm_context
*lp_ctx
,
1490 krb5_context context
, struct HDB
**db
, const char *arg
)
1493 struct auth_session_info
*session_info
;
1494 *db
= talloc(mem_ctx
, HDB
);
1496 krb5_set_error_string(context
, "malloc: out of memory");
1497 return NT_STATUS_NO_MEMORY
;
1500 (*db
)->hdb_master_key_set
= 0;
1501 (*db
)->hdb_db
= NULL
;
1503 nt_status
= auth_system_session_info(*db
, lp_ctx
, &session_info
);
1504 if (!NT_STATUS_IS_OK(nt_status
)) {
1508 /* The idea here is very simple. Using Kerberos to
1509 * authenticate the KDC to the LDAP server is higly likely to
1512 * In future we may set this up to use EXERNAL and SSL
1513 * certificates, for now it will almost certainly be NTLMSSP
1516 cli_credentials_set_kerberos_state(session_info
->credentials
,
1517 CRED_DONT_USE_KERBEROS
);
1519 /* Setup the link to LDB */
1520 (*db
)->hdb_db
= samdb_connect(*db
, ev_ctx
, lp_ctx
, session_info
);
1521 if ((*db
)->hdb_db
== NULL
) {
1522 DEBUG(1, ("hdb_ldb_create: Cannot open samdb for KDC backend!"));
1523 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1526 (*db
)->hdb_dbc
= NULL
;
1527 (*db
)->hdb_open
= LDB_open
;
1528 (*db
)->hdb_close
= LDB_close
;
1529 (*db
)->hdb_fetch
= LDB_fetch
;
1530 (*db
)->hdb_store
= LDB_store
;
1531 (*db
)->hdb_remove
= LDB_remove
;
1532 (*db
)->hdb_firstkey
= LDB_firstkey
;
1533 (*db
)->hdb_nextkey
= LDB_nextkey
;
1534 (*db
)->hdb_lock
= LDB_lock
;
1535 (*db
)->hdb_unlock
= LDB_unlock
;
1536 (*db
)->hdb_rename
= LDB_rename
;
1537 /* we don't implement these, as we are not a lockable database */
1538 (*db
)->hdb__get
= NULL
;
1539 (*db
)->hdb__put
= NULL
;
1540 /* kadmin should not be used for deletes - use other tools instead */
1541 (*db
)->hdb__del
= NULL
;
1542 (*db
)->hdb_destroy
= LDB_destroy
;
1544 return NT_STATUS_OK
;
1547 krb5_error_code
hdb_samba4_create(krb5_context context
, struct HDB
**db
, const char *arg
)
1550 /* The global kdc_mem_ctx and kdc_lp_ctx, Disgusting, ugly hack, but it means one less private hook */
1551 nt_status
= kdc_hdb_samba4_create(kdc_mem_ctx
, kdc_ev_ctx
, kdc_lp_ctx
,
1554 if (NT_STATUS_IS_OK(nt_status
)) {