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 "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
*private)
191 hdb_entry_ex
*entry_ex
= private->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(scpk
->data
);
285 talloc_steal(mem_ctx
, blob
.data
);
287 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
288 ndr_err
= ndr_pull_struct_blob(&blob
, mem_ctx
, iconv_convenience
, &_pkb
,
289 (ndr_pull_flags_fn_t
)ndr_pull_package_PrimaryKerberosBlob
);
290 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
291 krb5_set_error_string(context
, "LDB_message2entry_keys: could not parse package_PrimaryKerberosBlob");
292 krb5_warnx(context
, "LDB_message2entry_keys: could not parse package_PrimaryKerberosBlob");
297 if (newer_keys
&& _pkb
.version
!= 4) {
298 krb5_set_error_string(context
, "LDB_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
299 krb5_warnx(context
, "LDB_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
304 if (!newer_keys
&& _pkb
.version
!= 3) {
305 krb5_set_error_string(context
, "LDB_message2entry_keys: could not parse Primary:Kerberos not version 3");
306 krb5_warnx(context
, "LDB_message2entry_keys: could not parse Primary:Kerberos not version 3");
311 if (_pkb
.version
== 4) {
312 pkb4
= &_pkb
.ctr
.ctr4
;
313 allocated_keys
+= pkb4
->num_keys
;
314 } else if (_pkb
.version
== 3) {
315 pkb3
= &_pkb
.ctr
.ctr3
;
316 allocated_keys
+= pkb3
->num_keys
;
320 if (allocated_keys
== 0) {
321 /* oh, no password. Apparently (comment in
322 * hdb-ldap.c) this violates the ASN.1, but this
323 * allows an entry with no keys (yet). */
327 /* allocate space to decode into */
328 entry_ex
->entry
.keys
.len
= 0;
329 entry_ex
->entry
.keys
.val
= calloc(allocated_keys
, sizeof(Key
));
330 if (entry_ex
->entry
.keys
.val
== NULL
) {
335 if (hash
&& !(userAccountControl
& UF_USE_DES_KEY_ONLY
)) {
339 key
.salt
= NULL
; /* No salt for this enc type */
341 ret
= krb5_keyblock_init(context
,
342 ENCTYPE_ARCFOUR_HMAC_MD5
,
343 hash
->hash
, sizeof(hash
->hash
),
349 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
350 entry_ex
->entry
.keys
.len
++;
354 for (i
=0; i
< pkb4
->num_keys
; i
++) {
358 if (!pkb4
->keys
[i
].value
) continue;
360 if (userAccountControl
& UF_USE_DES_KEY_ONLY
) {
361 switch (pkb4
->keys
[i
].keytype
) {
362 case ENCTYPE_DES_CBC_CRC
:
363 case ENCTYPE_DES_CBC_MD5
:
376 if (pkb4
->salt
.string
) {
379 salt
= data_blob_string_const(pkb4
->salt
.string
);
381 key
.salt
= calloc(1, sizeof(*key
.salt
));
382 if (key
.salt
== NULL
) {
387 key
.salt
->type
= hdb_pw_salt
;
389 ret
= krb5_data_copy(&key
.salt
->salt
, salt
.data
, salt
.length
);
397 /* TODO: maybe pass the iteration_count somehow... */
399 ret
= krb5_keyblock_init(context
,
400 pkb4
->keys
[i
].keytype
,
401 pkb4
->keys
[i
].value
->data
,
402 pkb4
->keys
[i
].value
->length
,
413 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
414 entry_ex
->entry
.keys
.len
++;
417 for (i
=0; i
< pkb3
->num_keys
; i
++) {
421 if (!pkb3
->keys
[i
].value
) continue;
423 if (userAccountControl
& UF_USE_DES_KEY_ONLY
) {
424 switch (pkb3
->keys
[i
].keytype
) {
425 case ENCTYPE_DES_CBC_CRC
:
426 case ENCTYPE_DES_CBC_MD5
:
439 if (pkb3
->salt
.string
) {
442 salt
= data_blob_string_const(pkb3
->salt
.string
);
444 key
.salt
= calloc(1, sizeof(*key
.salt
));
445 if (key
.salt
== NULL
) {
450 key
.salt
->type
= hdb_pw_salt
;
452 ret
= krb5_data_copy(&key
.salt
->salt
, salt
.data
, salt
.length
);
460 ret
= krb5_keyblock_init(context
,
461 pkb3
->keys
[i
].keytype
,
462 pkb3
->keys
[i
].value
->data
,
463 pkb3
->keys
[i
].value
->length
,
474 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
475 entry_ex
->entry
.keys
.len
++;
481 entry_ex
->entry
.keys
.len
= 0;
483 if (entry_ex
->entry
.keys
.len
== 0 && entry_ex
->entry
.keys
.val
) {
484 free(entry_ex
->entry
.keys
.val
);
485 entry_ex
->entry
.keys
.val
= NULL
;
491 * Construct an hdb_entry from a directory entry.
493 static krb5_error_code
LDB_message2entry(krb5_context context
, HDB
*db
,
494 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
495 enum hdb_ldb_ent_type ent_type
,
496 struct ldb_message
*msg
,
497 struct ldb_message
*realm_ref_msg
,
498 hdb_entry_ex
*entry_ex
)
500 unsigned int userAccountControl
;
502 krb5_error_code ret
= 0;
503 krb5_boolean is_computer
= FALSE
;
504 const char *dnsdomain
= ldb_msg_find_attr_as_string(realm_ref_msg
, "dnsRoot", NULL
);
505 char *realm
= strupper_talloc(mem_ctx
, dnsdomain
);
506 struct loadparm_context
*lp_ctx
= ldb_get_opaque((struct ldb_context
*)db
->hdb_db
, "loadparm");
507 struct ldb_dn
*domain_dn
= samdb_result_dn((struct ldb_context
*)db
->hdb_db
,
511 ldb_dn_new(mem_ctx
, (struct ldb_context
*)db
->hdb_db
, NULL
));
513 struct hdb_ldb_private
*private;
516 struct ldb_message_element
*objectclasses
;
517 struct ldb_val computer_val
;
518 computer_val
.data
= discard_const_p(uint8_t,"computer");
519 computer_val
.length
= strlen((const char *)computer_val
.data
);
521 objectclasses
= ldb_msg_find_element(msg
, "objectClass");
523 if (objectclasses
&& ldb_msg_find_val(objectclasses
, &computer_val
)) {
527 memset(entry_ex
, 0, sizeof(*entry_ex
));
530 krb5_set_error_string(context
, "talloc_strdup: out of memory");
535 private = talloc(mem_ctx
, struct hdb_ldb_private
);
541 private->entry_ex
= entry_ex
;
542 private->iconv_convenience
= lp_iconv_convenience(lp_ctx
);
543 private->netbios_name
= lp_netbios_name(lp_ctx
);
545 talloc_set_destructor(private, hdb_ldb_destructor
);
547 entry_ex
->ctx
= private;
548 entry_ex
->free_entry
= hdb_ldb_free_entry
;
550 userAccountControl
= ldb_msg_find_attr_as_uint(msg
, "userAccountControl", 0);
553 entry_ex
->entry
.principal
= malloc(sizeof(*(entry_ex
->entry
.principal
)));
554 if (ent_type
== HDB_SAMBA4_ENT_TYPE_ANY
&& principal
== NULL
) {
555 const char *samAccountName
= ldb_msg_find_attr_as_string(msg
, "samAccountName", NULL
);
556 if (!samAccountName
) {
557 krb5_set_error_string(context
, "LDB_message2entry: no samAccountName present");
561 samAccountName
= ldb_msg_find_attr_as_string(msg
, "samAccountName", NULL
);
562 krb5_make_principal(context
, &entry_ex
->entry
.principal
, realm
, samAccountName
, NULL
);
565 ret
= copy_Principal(principal
, entry_ex
->entry
.principal
);
567 krb5_clear_error_string(context
);
571 /* While we have copied the client principal, tests
572 * show that Win2k3 returns the 'corrected' realm, not
573 * the client-specified realm. This code attempts to
574 * replace the client principal's realm with the one
575 * we determine from our records */
577 /* this has to be with malloc() */
578 strdup_realm
= strdup(realm
);
581 krb5_clear_error_string(context
);
584 free(*krb5_princ_realm(context
, entry_ex
->entry
.principal
));
585 krb5_princ_set_realm(context
, entry_ex
->entry
.principal
, &strdup_realm
);
588 entry_ex
->entry
.flags
= uf2HDBFlags(context
, userAccountControl
, ent_type
);
590 if (ent_type
== HDB_SAMBA4_ENT_TYPE_KRBTGT
) {
591 entry_ex
->entry
.flags
.invalid
= 0;
592 entry_ex
->entry
.flags
.server
= 1;
593 entry_ex
->entry
.flags
.forwardable
= 1;
594 entry_ex
->entry
.flags
.ok_as_delegate
= 1;
597 if (lp_parm_bool(lp_ctx
, NULL
, "kdc", "require spn for service", true)) {
598 if (!is_computer
&& !ldb_msg_find_attr_as_string(msg
, "servicePrincipalName", NULL
)) {
599 entry_ex
->entry
.flags
.server
= 0;
603 /* use 'whenCreated' */
604 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
606 entry_ex
->entry
.created_by
.principal
= NULL
;
608 entry_ex
->entry
.modified_by
= (Event
*) malloc(sizeof(Event
));
609 if (entry_ex
->entry
.modified_by
== NULL
) {
610 krb5_set_error_string(context
, "malloc: out of memory");
615 /* use 'whenChanged' */
616 entry_ex
->entry
.modified_by
->time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenChanged", 0);
618 entry_ex
->entry
.modified_by
->principal
= NULL
;
620 entry_ex
->entry
.valid_start
= NULL
;
622 acct_expiry
= samdb_result_account_expires(msg
);
623 if (acct_expiry
== 0x7FFFFFFFFFFFFFFFULL
) {
624 entry_ex
->entry
.valid_end
= NULL
;
626 entry_ex
->entry
.valid_end
= malloc(sizeof(*entry_ex
->entry
.valid_end
));
627 if (entry_ex
->entry
.valid_end
== NULL
) {
631 *entry_ex
->entry
.valid_end
= nt_time_to_unix(acct_expiry
);
634 if (ent_type
!= HDB_SAMBA4_ENT_TYPE_KRBTGT
) {
635 NTTIME must_change_time
636 = samdb_result_force_password_change((struct ldb_context
*)db
->hdb_db
, mem_ctx
,
638 if (must_change_time
== 0x7FFFFFFFFFFFFFFFULL
) {
639 entry_ex
->entry
.pw_end
= NULL
;
641 entry_ex
->entry
.pw_end
= malloc(sizeof(*entry_ex
->entry
.pw_end
));
642 if (entry_ex
->entry
.pw_end
== NULL
) {
646 *entry_ex
->entry
.pw_end
= nt_time_to_unix(must_change_time
);
649 entry_ex
->entry
.pw_end
= NULL
;
652 entry_ex
->entry
.max_life
= NULL
;
654 entry_ex
->entry
.max_renew
= NULL
;
656 entry_ex
->entry
.generation
= NULL
;
658 /* Get keys from the db */
659 ret
= LDB_message2entry_keys(context
, private->iconv_convenience
, private, msg
, userAccountControl
, entry_ex
);
661 /* Could be bougus data in the entry, or out of memory */
665 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
666 if (entry_ex
->entry
.etypes
== NULL
) {
667 krb5_clear_error_string(context
);
671 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
672 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
673 if (entry_ex
->entry
.etypes
->val
== NULL
) {
674 krb5_clear_error_string(context
);
678 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
679 entry_ex
->entry
.etypes
->val
[i
] = entry_ex
->entry
.keys
.val
[i
].key
.keytype
;
683 private->msg
= talloc_steal(private, msg
);
684 private->realm_ref_msg
= talloc_steal(private, realm_ref_msg
);
685 private->samdb
= (struct ldb_context
*)db
->hdb_db
;
689 /* This doesn't free ent itself, that is for the eventual caller to do */
690 hdb_free_entry(context
, entry_ex
);
692 talloc_steal(db
, entry_ex
->ctx
);
699 * Construct an hdb_entry from a directory entry.
701 static krb5_error_code
LDB_trust_message2entry(krb5_context context
, HDB
*db
,
702 struct loadparm_context
*lp_ctx
,
703 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
704 enum trust_direction direction
,
705 struct ldb_message
*msg
,
706 hdb_entry_ex
*entry_ex
)
709 const char *dnsdomain
;
712 DATA_BLOB password_utf16
;
713 struct samr_Password password_hash
;
714 const struct ldb_val
*password_val
;
715 struct trustAuthInOutBlob password_blob
;
716 struct hdb_ldb_private
*private;
718 enum ndr_err_code ndr_err
;
719 int i
, ret
, trust_direction_flags
;
721 private = talloc(mem_ctx
, struct hdb_ldb_private
);
727 private->entry_ex
= entry_ex
;
728 private->iconv_convenience
= lp_iconv_convenience(lp_ctx
);
729 private->netbios_name
= lp_netbios_name(lp_ctx
);
731 talloc_set_destructor(private, hdb_ldb_destructor
);
733 entry_ex
->ctx
= private;
734 entry_ex
->free_entry
= hdb_ldb_free_entry
;
736 /* use 'whenCreated' */
737 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
739 entry_ex
->entry
.created_by
.principal
= NULL
;
741 entry_ex
->entry
.valid_start
= NULL
;
743 trust_direction_flags
= ldb_msg_find_attr_as_int(msg
, "trustDirection", 0);
745 if (direction
== INBOUND
) {
746 realm
= strupper_talloc(mem_ctx
, lp_realm(lp_ctx
));
747 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthIncoming");
749 } else { /* OUTBOUND */
750 dnsdomain
= ldb_msg_find_attr_as_string(msg
, "trustPartner", NULL
);
751 realm
= strupper_talloc(mem_ctx
, dnsdomain
);
752 password_val
= ldb_msg_find_ldb_val(msg
, "trustAuthOutgoing");
755 if (!password_val
|| !(trust_direction_flags
& direction
)) {
760 ndr_err
= ndr_pull_struct_blob_all(password_val
, mem_ctx
, private->iconv_convenience
, &password_blob
,
761 (ndr_pull_flags_fn_t
)ndr_pull_trustAuthInOutBlob
);
762 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
767 for (i
=0; i
< password_blob
.count
; i
++) {
768 if (password_blob
.current
->array
[i
].AuthType
== TRUST_AUTH_TYPE_CLEAR
) {
769 password_utf16
= data_blob_const(password_blob
.current
->array
[i
].AuthInfo
.clear
.password
,
770 password_blob
.current
->array
[i
].AuthInfo
.clear
.size
);
771 /* In the future, generate all sorts of
772 * hashes, but for now we can't safely convert
773 * the random strings windows uses into
776 /* but as it is utf16 already, we can get the NT password/arcfour-hmac-md5 key */
777 mdfour(password_hash
.hash
, password_utf16
.data
, password_utf16
.length
);
779 } else if (password_blob
.current
->array
[i
].AuthType
== TRUST_AUTH_TYPE_NT4OWF
) {
780 password_hash
= password_blob
.current
->array
[i
].AuthInfo
.nt4owf
.password
;
784 entry_ex
->entry
.keys
.len
= 0;
785 entry_ex
->entry
.keys
.val
= NULL
;
787 if (i
< password_blob
.count
) {
789 /* Must have found a cleartext or MD4 password */
790 entry_ex
->entry
.keys
.val
= calloc(1, sizeof(Key
));
793 key
.salt
= NULL
; /* No salt for this enc type */
795 if (entry_ex
->entry
.keys
.val
== NULL
) {
800 ret
= krb5_keyblock_init(context
,
801 ENCTYPE_ARCFOUR_HMAC_MD5
,
802 password_hash
.hash
, sizeof(password_hash
.hash
),
805 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
806 entry_ex
->entry
.keys
.len
++;
809 ret
= copy_Principal(principal
, entry_ex
->entry
.principal
);
811 krb5_clear_error_string(context
);
815 /* While we have copied the client principal, tests
816 * show that Win2k3 returns the 'corrected' realm, not
817 * the client-specified realm. This code attempts to
818 * replace the client principal's realm with the one
819 * we determine from our records */
821 /* this has to be with malloc() */
822 strdup_realm
= strdup(realm
);
825 krb5_clear_error_string(context
);
828 free(*krb5_princ_realm(context
, entry_ex
->entry
.principal
));
829 krb5_princ_set_realm(context
, entry_ex
->entry
.principal
, &strdup_realm
);
831 entry_ex
->entry
.flags
= int2HDBFlags(0);
832 entry_ex
->entry
.flags
.immutable
= 1;
833 entry_ex
->entry
.flags
.invalid
= 0;
834 entry_ex
->entry
.flags
.server
= 1;
835 entry_ex
->entry
.flags
.require_preauth
= 1;
837 entry_ex
->entry
.pw_end
= NULL
;
839 entry_ex
->entry
.max_life
= NULL
;
841 entry_ex
->entry
.max_renew
= NULL
;
843 entry_ex
->entry
.generation
= NULL
;
845 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
846 if (entry_ex
->entry
.etypes
== NULL
) {
847 krb5_clear_error_string(context
);
851 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
852 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
853 if (entry_ex
->entry
.etypes
->val
== NULL
) {
854 krb5_clear_error_string(context
);
858 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
859 entry_ex
->entry
.etypes
->val
[i
] = entry_ex
->entry
.keys
.val
[i
].key
.keytype
;
863 private->msg
= talloc_steal(private, msg
);
864 private->realm_ref_msg
= NULL
;
865 private->samdb
= (struct ldb_context
*)db
->hdb_db
;
869 /* This doesn't free ent itself, that is for the eventual caller to do */
870 hdb_free_entry(context
, entry_ex
);
872 talloc_steal(db
, entry_ex
->ctx
);
879 static krb5_error_code
LDB_lookup_principal(krb5_context context
, struct ldb_context
*ldb_ctx
,
881 krb5_const_principal principal
,
882 enum hdb_ldb_ent_type ent_type
,
883 struct ldb_dn
*realm_dn
,
884 struct ldb_message
***pmsg
)
889 const char * const *princ_attrs
= user_attrs
;
892 char *short_princ_talloc
;
894 struct ldb_result
*res
= NULL
;
896 ret
= krb5_unparse_name_flags(context
, principal
, KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &short_princ
);
899 krb5_set_error_string(context
, "LDB_lookup_principal: could not parse principal");
900 krb5_warnx(context
, "LDB_lookup_principal: could not parse principal");
904 short_princ_talloc
= talloc_strdup(mem_ctx
, short_princ
);
906 if (!short_princ_talloc
) {
907 krb5_set_error_string(context
, "LDB_lookup_principal: talloc_strdup() failed!");
912 case HDB_SAMBA4_ENT_TYPE_CLIENT
:
913 case HDB_SAMBA4_ENT_TYPE_TRUST
:
914 case HDB_SAMBA4_ENT_TYPE_ANY
:
917 case HDB_SAMBA4_ENT_TYPE_KRBTGT
:
918 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=user)(samAccountName=%s))",
921 case HDB_SAMBA4_ENT_TYPE_SERVER
:
922 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=user)(samAccountName=%s))",
928 krb5_set_error_string(context
, "talloc_asprintf: out of memory");
932 lret
= ldb_search(ldb_ctx
, mem_ctx
, &res
, realm_dn
,
933 LDB_SCOPE_SUBTREE
, princ_attrs
, "%s", filter
);
934 if (lret
!= LDB_SUCCESS
) {
935 DEBUG(3, ("Failed to search for %s: %s\n", filter
, ldb_errstring(ldb_ctx
)));
936 return HDB_ERR_NOENTRY
;
937 } else if (res
->count
== 0 || res
->count
> 1) {
938 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter
, res
->count
));
940 return HDB_ERR_NOENTRY
;
942 talloc_steal(mem_ctx
, res
->msgs
);
948 static krb5_error_code
LDB_lookup_trust(krb5_context context
, struct ldb_context
*ldb_ctx
,
951 struct ldb_dn
*realm_dn
,
952 struct ldb_message
***pmsg
)
956 const char * const *attrs
= trust_attrs
;
958 struct ldb_result
*res
= NULL
;
959 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=trustedDomain)(|(flatname=%s)(trustPartner=%s)))", realm
, realm
);
962 krb5_set_error_string(context
, "talloc_asprintf: out of memory");
966 lret
= ldb_search(ldb_ctx
, mem_ctx
, &res
,
967 ldb_get_default_basedn(ldb_ctx
),
968 LDB_SCOPE_SUBTREE
, attrs
, "%s", filter
);
969 if (lret
!= LDB_SUCCESS
) {
970 DEBUG(3, ("Failed to search for %s: %s\n", filter
, ldb_errstring(ldb_ctx
)));
971 return HDB_ERR_NOENTRY
;
972 } else if (res
->count
== 0 || res
->count
> 1) {
973 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter
, res
->count
));
975 return HDB_ERR_NOENTRY
;
977 talloc_steal(mem_ctx
, res
->msgs
);
983 static krb5_error_code
LDB_lookup_realm(krb5_context context
, struct ldb_context
*ldb_ctx
,
986 struct ldb_message
***pmsg
)
989 struct ldb_result
*cross_ref_res
;
990 struct ldb_dn
*partitions_basedn
= samdb_partitions_dn(ldb_ctx
, mem_ctx
);
992 ret
= ldb_search(ldb_ctx
, mem_ctx
, &cross_ref_res
,
993 partitions_basedn
, LDB_SCOPE_SUBTREE
, realm_ref_attrs
,
994 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
997 if (ret
!= LDB_SUCCESS
) {
998 DEBUG(3, ("Failed to search to lookup realm(%s): %s\n", realm
, ldb_errstring(ldb_ctx
)));
999 talloc_free(cross_ref_res
);
1000 return HDB_ERR_NOENTRY
;
1001 } else if (cross_ref_res
->count
== 0 || cross_ref_res
->count
> 1) {
1002 DEBUG(3, ("Failed find a single entry for realm %s: got %d\n", realm
, cross_ref_res
->count
));
1003 talloc_free(cross_ref_res
);
1004 return HDB_ERR_NOENTRY
;
1008 *pmsg
= cross_ref_res
->msgs
;
1009 talloc_steal(mem_ctx
, cross_ref_res
->msgs
);
1011 talloc_free(cross_ref_res
);
1017 static krb5_error_code
LDB_open(krb5_context context
, HDB
*db
, int flags
, mode_t mode
)
1019 if (db
->hdb_master_key_set
) {
1020 krb5_warnx(context
, "LDB_open: use of a master key incompatible with LDB\n");
1021 krb5_set_error_string(context
, "LDB_open: use of a master key incompatible with LDB\n");
1022 return HDB_ERR_NOENTRY
;
1028 static krb5_error_code
LDB_close(krb5_context context
, HDB
*db
)
1033 static krb5_error_code
LDB_lock(krb5_context context
, HDB
*db
, int operation
)
1038 static krb5_error_code
LDB_unlock(krb5_context context
, HDB
*db
)
1043 static krb5_error_code
LDB_rename(krb5_context context
, HDB
*db
, const char *new_name
)
1045 return HDB_ERR_DB_INUSE
;
1048 static krb5_error_code
LDB_fetch_client(krb5_context context
, HDB
*db
,
1049 TALLOC_CTX
*mem_ctx
,
1050 krb5_const_principal principal
,
1052 hdb_entry_ex
*entry_ex
) {
1054 char *principal_string
;
1055 krb5_error_code ret
;
1056 struct ldb_message
**msg
= NULL
;
1057 struct ldb_message
**realm_ref_msg
= NULL
;
1059 ret
= krb5_unparse_name(context
, principal
, &principal_string
);
1065 nt_status
= sam_get_results_principal((struct ldb_context
*)db
->hdb_db
,
1066 mem_ctx
, principal_string
,
1067 &msg
, &realm_ref_msg
);
1068 free(principal_string
);
1069 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
1070 return HDB_ERR_NOENTRY
;
1071 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_MEMORY
)) {
1073 } else if (!NT_STATUS_IS_OK(nt_status
)) {
1077 ret
= LDB_message2entry(context
, db
, mem_ctx
,
1078 principal
, HDB_SAMBA4_ENT_TYPE_CLIENT
,
1079 msg
[0], realm_ref_msg
[0], entry_ex
);
1083 static krb5_error_code
LDB_fetch_krbtgt(krb5_context context
, HDB
*db
,
1084 TALLOC_CTX
*mem_ctx
,
1085 krb5_const_principal principal
,
1087 hdb_entry_ex
*entry_ex
)
1089 krb5_error_code ret
;
1090 struct ldb_message
**msg
= NULL
;
1091 struct ldb_message
**realm_ref_msg_1
= NULL
;
1092 struct ldb_message
**realm_ref_msg_2
= NULL
;
1093 struct ldb_dn
*realm_dn
;
1096 krb5_principal alloc_principal
= NULL
;
1097 if (principal
->name
.name_string
.len
!= 2
1098 || (strcmp(principal
->name
.name_string
.val
[0], KRB5_TGS_NAME
) != 0)) {
1100 return HDB_ERR_NOENTRY
;
1103 /* krbtgt case. Either us or a trusted realm */
1105 if ((LDB_lookup_realm(context
, (struct ldb_context
*)db
->hdb_db
,
1106 mem_ctx
, principal
->realm
, &realm_ref_msg_1
) == 0)
1107 && (LDB_lookup_realm(context
, (struct ldb_context
*)db
->hdb_db
,
1108 mem_ctx
, principal
->name
.name_string
.val
[1], &realm_ref_msg_2
) == 0)
1109 && (ldb_dn_compare(realm_ref_msg_1
[0]->dn
, realm_ref_msg_1
[0]->dn
) == 0)) {
1111 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1112 * is in our db, then direct the caller at our primary
1115 const char *dnsdomain
= ldb_msg_find_attr_as_string(realm_ref_msg_1
[0], "dnsRoot", NULL
);
1116 char *realm_fixed
= strupper_talloc(mem_ctx
, dnsdomain
);
1118 krb5_set_error_string(context
, "strupper_talloc: out of memory");
1122 ret
= krb5_copy_principal(context
, principal
, &alloc_principal
);
1127 free(alloc_principal
->name
.name_string
.val
[1]);
1128 alloc_principal
->name
.name_string
.val
[1] = strdup(realm_fixed
);
1129 talloc_free(realm_fixed
);
1130 if (!alloc_principal
->name
.name_string
.val
[1]) {
1131 krb5_set_error_string(context
, "LDB_fetch: strdup() failed!");
1134 principal
= alloc_principal
;
1135 realm_dn
= samdb_result_dn((struct ldb_context
*)db
->hdb_db
, mem_ctx
, realm_ref_msg_1
[0], "nCName", NULL
);
1137 ret
= LDB_lookup_principal(context
, (struct ldb_context
*)db
->hdb_db
,
1139 principal
, HDB_SAMBA4_ENT_TYPE_KRBTGT
, realm_dn
, &msg
);
1142 krb5_warnx(context
, "LDB_fetch: could not find principal in DB");
1143 krb5_set_error_string(context
, "LDB_fetch: could not find principal in DB");
1147 ret
= LDB_message2entry(context
, db
, mem_ctx
,
1148 principal
, HDB_SAMBA4_ENT_TYPE_KRBTGT
,
1149 msg
[0], realm_ref_msg_1
[0], entry_ex
);
1151 krb5_warnx(context
, "LDB_fetch: message2entry failed");
1156 enum trust_direction direction
= UNKNOWN
;
1158 struct loadparm_context
*lp_ctx
= talloc_get_type(ldb_get_opaque(db
->hdb_db
, "loadparm"), struct loadparm_context
);
1159 /* Either an inbound or outbound trust */
1161 if (strcasecmp(lp_realm(lp_ctx
), principal
->realm
) == 0) {
1162 /* look for inbound trust */
1163 direction
= INBOUND
;
1164 realm
= principal
->name
.name_string
.val
[1];
1167 if (strcasecmp(lp_realm(lp_ctx
), principal
->name
.name_string
.val
[1]) == 0) {
1168 /* look for outbound trust */
1169 direction
= OUTBOUND
;
1170 realm
= principal
->realm
;
1173 /* Trusted domains are under CN=system */
1175 ret
= LDB_lookup_trust(context
, (struct ldb_context
*)db
->hdb_db
,
1177 realm
, realm_dn
, &msg
);
1180 krb5_warnx(context
, "LDB_fetch: could not find principal in DB");
1181 krb5_set_error_string(context
, "LDB_fetch: could not find principal in DB");
1185 ret
= LDB_trust_message2entry(context
, db
, lp_ctx
, mem_ctx
,
1186 principal
, direction
,
1189 krb5_warnx(context
, "LDB_fetch: message2entry failed");
1194 /* we should lookup trusted domains */
1195 return HDB_ERR_NOENTRY
;
1200 static krb5_error_code
LDB_fetch_server(krb5_context context
, HDB
*db
,
1201 TALLOC_CTX
*mem_ctx
,
1202 krb5_const_principal principal
,
1204 hdb_entry_ex
*entry_ex
)
1206 krb5_error_code ret
;
1208 struct ldb_message
**msg
= NULL
;
1209 struct ldb_message
**realm_ref_msg
= NULL
;
1210 struct ldb_dn
*partitions_basedn
= samdb_partitions_dn(db
->hdb_db
, mem_ctx
);
1211 if (principal
->name
.name_string
.len
>= 2) {
1212 /* 'normal server' case */
1215 struct ldb_dn
*user_dn
, *domain_dn
;
1216 char *principal_string
;
1218 ret
= krb5_unparse_name_flags(context
, principal
,
1219 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
1225 /* At this point we may find the host is known to be
1226 * in a different realm, so we should generate a
1227 * referral instead */
1228 nt_status
= crack_service_principal_name((struct ldb_context
*)db
->hdb_db
,
1229 mem_ctx
, principal_string
,
1230 &user_dn
, &domain_dn
);
1231 free(principal_string
);
1233 if (!NT_STATUS_IS_OK(nt_status
)) {
1234 return HDB_ERR_NOENTRY
;
1237 ldb_ret
= gendb_search_dn((struct ldb_context
*)db
->hdb_db
,
1238 mem_ctx
, user_dn
, &msg
, user_attrs
);
1241 return HDB_ERR_NOENTRY
;
1244 ldb_ret
= gendb_search((struct ldb_context
*)db
->hdb_db
,
1245 mem_ctx
, partitions_basedn
, &realm_ref_msg
, realm_ref_attrs
,
1246 "ncName=%s", ldb_dn_get_linearized(domain_dn
));
1249 return HDB_ERR_NOENTRY
;
1253 struct ldb_dn
*realm_dn
;
1254 /* server as client principal case, but we must not lookup userPrincipalNames */
1256 realm
= krb5_principal_get_realm(context
, principal
);
1258 ret
= LDB_lookup_realm(context
, (struct ldb_context
*)db
->hdb_db
,
1259 mem_ctx
, realm
, &realm_ref_msg
);
1261 return HDB_ERR_NOENTRY
;
1264 realm_dn
= samdb_result_dn((struct ldb_context
*)db
->hdb_db
, mem_ctx
, realm_ref_msg
[0], "nCName", NULL
);
1266 ret
= LDB_lookup_principal(context
, (struct ldb_context
*)db
->hdb_db
,
1268 principal
, HDB_SAMBA4_ENT_TYPE_SERVER
, realm_dn
, &msg
);
1275 ret
= LDB_message2entry(context
, db
, mem_ctx
,
1276 principal
, HDB_SAMBA4_ENT_TYPE_SERVER
,
1277 msg
[0], realm_ref_msg
[0], entry_ex
);
1279 krb5_warnx(context
, "LDB_fetch: message2entry failed");
1285 static krb5_error_code
LDB_fetch(krb5_context context
, HDB
*db
,
1286 krb5_const_principal principal
,
1288 hdb_entry_ex
*entry_ex
)
1290 krb5_error_code ret
= HDB_ERR_NOENTRY
;
1292 TALLOC_CTX
*mem_ctx
= talloc_named(db
, 0, "LDB_fetch context");
1295 krb5_set_error_string(context
, "LDB_fetch: talloc_named() failed!");
1299 if (flags
& HDB_F_GET_CLIENT
) {
1300 ret
= LDB_fetch_client(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
1301 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1303 if (flags
& HDB_F_GET_SERVER
) {
1304 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
1305 ret
= LDB_fetch_krbtgt(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
1306 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1308 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
1309 ret
= LDB_fetch_server(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
1310 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1312 if (flags
& HDB_F_GET_KRBTGT
) {
1313 ret
= LDB_fetch_krbtgt(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
1314 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1318 talloc_free(mem_ctx
);
1322 static krb5_error_code
LDB_store(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry_ex
*entry
)
1324 return HDB_ERR_DB_INUSE
;
1327 static krb5_error_code
LDB_remove(krb5_context context
, HDB
*db
, krb5_const_principal principal
)
1329 return HDB_ERR_DB_INUSE
;
1332 struct hdb_ldb_seq
{
1333 struct ldb_context
*ctx
;
1336 struct ldb_message
**msgs
;
1337 struct ldb_message
**realm_ref_msgs
;
1340 static krb5_error_code
LDB_seq(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry_ex
*entry
)
1342 krb5_error_code ret
;
1343 struct hdb_ldb_seq
*priv
= (struct hdb_ldb_seq
*)db
->hdb_dbc
;
1344 TALLOC_CTX
*mem_ctx
;
1345 hdb_entry_ex entry_ex
;
1346 memset(&entry_ex
, '\0', sizeof(entry_ex
));
1349 return HDB_ERR_NOENTRY
;
1352 mem_ctx
= talloc_named(priv
, 0, "LDB_seq context");
1355 krb5_set_error_string(context
, "LDB_seq: talloc_named() failed!");
1359 if (priv
->index
< priv
->count
) {
1360 ret
= LDB_message2entry(context
, db
, mem_ctx
,
1361 NULL
, HDB_SAMBA4_ENT_TYPE_ANY
,
1362 priv
->msgs
[priv
->index
++],
1363 priv
->realm_ref_msgs
[0], entry
);
1365 ret
= HDB_ERR_NOENTRY
;
1372 talloc_free(mem_ctx
);
1378 static krb5_error_code
LDB_firstkey(krb5_context context
, HDB
*db
, unsigned flags
,
1379 hdb_entry_ex
*entry
)
1381 struct ldb_context
*ldb_ctx
= (struct ldb_context
*)db
->hdb_db
;
1382 struct hdb_ldb_seq
*priv
= (struct hdb_ldb_seq
*)db
->hdb_dbc
;
1384 struct ldb_dn
*realm_dn
= NULL
;
1385 struct ldb_result
*res
= NULL
;
1386 struct ldb_message
**realm_ref_msgs
= NULL
;
1387 krb5_error_code ret
;
1388 TALLOC_CTX
*mem_ctx
;
1396 priv
= (struct hdb_ldb_seq
*) talloc(db
, struct hdb_ldb_seq
);
1398 krb5_set_error_string(context
, "talloc: out of memory");
1402 priv
->ctx
= ldb_ctx
;
1405 priv
->realm_ref_msgs
= NULL
;
1408 mem_ctx
= talloc_named(priv
, 0, "LDB_firstkey context");
1411 krb5_set_error_string(context
, "LDB_firstkey: talloc_named() failed!");
1415 ret
= krb5_get_default_realm(context
, &realm
);
1421 ret
= LDB_lookup_realm(context
, (struct ldb_context
*)db
->hdb_db
,
1422 mem_ctx
, realm
, &realm_ref_msgs
);
1428 krb5_warnx(context
, "LDB_firstkey: could not find realm\n");
1429 return HDB_ERR_NOENTRY
;
1432 realm_dn
= samdb_result_dn((struct ldb_context
*)db
->hdb_db
, mem_ctx
, realm_ref_msgs
[0], "nCName", NULL
);
1434 priv
->realm_ref_msgs
= talloc_steal(priv
, realm_ref_msgs
);
1436 lret
= ldb_search(ldb_ctx
, priv
, &res
,
1437 realm_dn
, LDB_SCOPE_SUBTREE
, user_attrs
,
1438 "(objectClass=user)");
1440 if (lret
!= LDB_SUCCESS
) {
1442 return HDB_ERR_NOENTRY
;
1445 priv
->count
= res
->count
;
1446 priv
->msgs
= talloc_steal(priv
, res
->msgs
);
1451 ret
= LDB_seq(context
, db
, flags
, entry
);
1457 talloc_free(mem_ctx
);
1462 static krb5_error_code
LDB_nextkey(krb5_context context
, HDB
*db
, unsigned flags
,
1463 hdb_entry_ex
*entry
)
1465 return LDB_seq(context
, db
, flags
, entry
);
1468 static krb5_error_code
LDB_destroy(krb5_context context
, HDB
*db
)
1474 /* This interface is to be called by the KDC, which is expecting Samba
1475 * calling conventions. It is also called by a wrapper
1476 * (hdb_ldb_create) from the kpasswdd -> krb5 -> keytab_hdb -> hdb
1479 NTSTATUS
kdc_hdb_samba4_create(TALLOC_CTX
*mem_ctx
,
1480 struct event_context
*ev_ctx
,
1481 struct loadparm_context
*lp_ctx
,
1482 krb5_context context
, struct HDB
**db
, const char *arg
)
1485 struct auth_session_info
*session_info
;
1486 *db
= talloc(mem_ctx
, HDB
);
1488 krb5_set_error_string(context
, "malloc: out of memory");
1489 return NT_STATUS_NO_MEMORY
;
1492 (*db
)->hdb_master_key_set
= 0;
1493 (*db
)->hdb_db
= NULL
;
1495 nt_status
= auth_system_session_info(*db
, lp_ctx
, &session_info
);
1496 if (!NT_STATUS_IS_OK(nt_status
)) {
1500 /* The idea here is very simple. Using Kerberos to
1501 * authenticate the KDC to the LDAP server is higly likely to
1504 * In future we may set this up to use EXERNAL and SSL
1505 * certificates, for now it will almost certainly be NTLMSSP
1508 cli_credentials_set_kerberos_state(session_info
->credentials
,
1509 CRED_DONT_USE_KERBEROS
);
1511 /* Setup the link to LDB */
1512 (*db
)->hdb_db
= samdb_connect(*db
, ev_ctx
, lp_ctx
, session_info
);
1513 if ((*db
)->hdb_db
== NULL
) {
1514 DEBUG(1, ("hdb_ldb_create: Cannot open samdb for KDC backend!"));
1515 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1518 (*db
)->hdb_dbc
= NULL
;
1519 (*db
)->hdb_open
= LDB_open
;
1520 (*db
)->hdb_close
= LDB_close
;
1521 (*db
)->hdb_fetch
= LDB_fetch
;
1522 (*db
)->hdb_store
= LDB_store
;
1523 (*db
)->hdb_remove
= LDB_remove
;
1524 (*db
)->hdb_firstkey
= LDB_firstkey
;
1525 (*db
)->hdb_nextkey
= LDB_nextkey
;
1526 (*db
)->hdb_lock
= LDB_lock
;
1527 (*db
)->hdb_unlock
= LDB_unlock
;
1528 (*db
)->hdb_rename
= LDB_rename
;
1529 /* we don't implement these, as we are not a lockable database */
1530 (*db
)->hdb__get
= NULL
;
1531 (*db
)->hdb__put
= NULL
;
1532 /* kadmin should not be used for deletes - use other tools instead */
1533 (*db
)->hdb__del
= NULL
;
1534 (*db
)->hdb_destroy
= LDB_destroy
;
1536 return NT_STATUS_OK
;
1539 krb5_error_code
hdb_samba4_create(krb5_context context
, struct HDB
**db
, const char *arg
)
1542 /* The global kdc_mem_ctx and kdc_lp_ctx, Disgusting, ugly hack, but it means one less private hook */
1543 nt_status
= kdc_hdb_samba4_create(kdc_mem_ctx
, event_context_find(kdc_mem_ctx
), kdc_lp_ctx
,
1546 if (NT_STATUS_IS_OK(nt_status
)) {