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
*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(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 entry_ex
->entry
.kvno
= -1;
768 for (i
=0; i
< password_blob
.count
; i
++) {
769 if (password_blob
.current
->array
[i
].AuthType
== TRUST_AUTH_TYPE_VERSION
) {
770 entry_ex
->entry
.kvno
= password_blob
.current
->array
[i
].AuthInfo
.version
.version
;
774 for (i
=0; i
< password_blob
.count
; i
++) {
775 if (password_blob
.current
->array
[i
].AuthType
== TRUST_AUTH_TYPE_CLEAR
) {
776 password_utf16
= data_blob_const(password_blob
.current
->array
[i
].AuthInfo
.clear
.password
,
777 password_blob
.current
->array
[i
].AuthInfo
.clear
.size
);
778 /* In the future, generate all sorts of
779 * hashes, but for now we can't safely convert
780 * the random strings windows uses into
783 /* but as it is utf16 already, we can get the NT password/arcfour-hmac-md5 key */
784 mdfour(password_hash
.hash
, password_utf16
.data
, password_utf16
.length
);
786 } else if (password_blob
.current
->array
[i
].AuthType
== TRUST_AUTH_TYPE_NT4OWF
) {
787 password_hash
= password_blob
.current
->array
[i
].AuthInfo
.nt4owf
.password
;
791 entry_ex
->entry
.keys
.len
= 0;
792 entry_ex
->entry
.keys
.val
= NULL
;
794 if (i
< password_blob
.count
) {
796 /* Must have found a cleartext or MD4 password */
797 entry_ex
->entry
.keys
.val
= calloc(1, sizeof(Key
));
800 key
.salt
= NULL
; /* No salt for this enc type */
802 if (entry_ex
->entry
.keys
.val
== NULL
) {
807 ret
= krb5_keyblock_init(context
,
808 ENCTYPE_ARCFOUR_HMAC_MD5
,
809 password_hash
.hash
, sizeof(password_hash
.hash
),
812 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
813 entry_ex
->entry
.keys
.len
++;
816 entry_ex
->entry
.principal
= malloc(sizeof(*(entry_ex
->entry
.principal
)));
818 ret
= copy_Principal(principal
, entry_ex
->entry
.principal
);
820 krb5_clear_error_string(context
);
824 /* While we have copied the client principal, tests
825 * show that Win2k3 returns the 'corrected' realm, not
826 * the client-specified realm. This code attempts to
827 * replace the client principal's realm with the one
828 * we determine from our records */
830 /* this has to be with malloc() */
831 strdup_realm
= strdup(realm
);
834 krb5_clear_error_string(context
);
837 free(*krb5_princ_realm(context
, entry_ex
->entry
.principal
));
838 krb5_princ_set_realm(context
, entry_ex
->entry
.principal
, &strdup_realm
);
840 entry_ex
->entry
.flags
= int2HDBFlags(0);
841 entry_ex
->entry
.flags
.immutable
= 1;
842 entry_ex
->entry
.flags
.invalid
= 0;
843 entry_ex
->entry
.flags
.server
= 1;
844 entry_ex
->entry
.flags
.require_preauth
= 1;
846 entry_ex
->entry
.pw_end
= NULL
;
848 entry_ex
->entry
.max_life
= NULL
;
850 entry_ex
->entry
.max_renew
= NULL
;
852 entry_ex
->entry
.generation
= NULL
;
854 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
855 if (entry_ex
->entry
.etypes
== NULL
) {
856 krb5_clear_error_string(context
);
860 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
861 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
862 if (entry_ex
->entry
.etypes
->val
== NULL
) {
863 krb5_clear_error_string(context
);
867 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
868 entry_ex
->entry
.etypes
->val
[i
] = entry_ex
->entry
.keys
.val
[i
].key
.keytype
;
872 private->msg
= talloc_steal(private, msg
);
873 private->realm_ref_msg
= NULL
;
874 private->samdb
= (struct ldb_context
*)db
->hdb_db
;
878 /* This doesn't free ent itself, that is for the eventual caller to do */
879 hdb_free_entry(context
, entry_ex
);
881 talloc_steal(db
, entry_ex
->ctx
);
888 static krb5_error_code
LDB_lookup_principal(krb5_context context
, struct ldb_context
*ldb_ctx
,
890 krb5_const_principal principal
,
891 enum hdb_ldb_ent_type ent_type
,
892 struct ldb_dn
*realm_dn
,
893 struct ldb_message
***pmsg
)
898 const char * const *princ_attrs
= user_attrs
;
901 char *short_princ_talloc
;
903 struct ldb_result
*res
= NULL
;
905 ret
= krb5_unparse_name_flags(context
, principal
, KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &short_princ
);
908 krb5_set_error_string(context
, "LDB_lookup_principal: could not parse principal");
909 krb5_warnx(context
, "LDB_lookup_principal: could not parse principal");
913 short_princ_talloc
= talloc_strdup(mem_ctx
, short_princ
);
915 if (!short_princ_talloc
) {
916 krb5_set_error_string(context
, "LDB_lookup_principal: talloc_strdup() failed!");
921 case HDB_SAMBA4_ENT_TYPE_CLIENT
:
922 case HDB_SAMBA4_ENT_TYPE_TRUST
:
923 case HDB_SAMBA4_ENT_TYPE_ANY
:
926 case HDB_SAMBA4_ENT_TYPE_KRBTGT
:
927 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=user)(samAccountName=%s))",
930 case HDB_SAMBA4_ENT_TYPE_SERVER
:
931 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=user)(samAccountName=%s))",
937 krb5_set_error_string(context
, "talloc_asprintf: out of memory");
941 lret
= ldb_search(ldb_ctx
, mem_ctx
, &res
, realm_dn
,
942 LDB_SCOPE_SUBTREE
, princ_attrs
, "%s", filter
);
943 if (lret
!= LDB_SUCCESS
) {
944 DEBUG(3, ("Failed to search for %s: %s\n", filter
, ldb_errstring(ldb_ctx
)));
945 return HDB_ERR_NOENTRY
;
946 } else if (res
->count
== 0 || res
->count
> 1) {
947 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter
, res
->count
));
949 return HDB_ERR_NOENTRY
;
951 talloc_steal(mem_ctx
, res
->msgs
);
957 static krb5_error_code
LDB_lookup_trust(krb5_context context
, struct ldb_context
*ldb_ctx
,
960 struct ldb_dn
*realm_dn
,
961 struct ldb_message
***pmsg
)
965 const char * const *attrs
= trust_attrs
;
967 struct ldb_result
*res
= NULL
;
968 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=trustedDomain)(|(flatname=%s)(trustPartner=%s)))", realm
, realm
);
971 krb5_set_error_string(context
, "talloc_asprintf: out of memory");
975 lret
= ldb_search(ldb_ctx
, mem_ctx
, &res
,
976 ldb_get_default_basedn(ldb_ctx
),
977 LDB_SCOPE_SUBTREE
, attrs
, "%s", filter
);
978 if (lret
!= LDB_SUCCESS
) {
979 DEBUG(3, ("Failed to search for %s: %s\n", filter
, ldb_errstring(ldb_ctx
)));
980 return HDB_ERR_NOENTRY
;
981 } else if (res
->count
== 0 || res
->count
> 1) {
982 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter
, res
->count
));
984 return HDB_ERR_NOENTRY
;
986 talloc_steal(mem_ctx
, res
->msgs
);
992 static krb5_error_code
LDB_lookup_realm(krb5_context context
, struct ldb_context
*ldb_ctx
,
995 struct ldb_message
***pmsg
)
998 struct ldb_result
*cross_ref_res
;
999 struct ldb_dn
*partitions_basedn
= samdb_partitions_dn(ldb_ctx
, mem_ctx
);
1001 ret
= ldb_search(ldb_ctx
, mem_ctx
, &cross_ref_res
,
1002 partitions_basedn
, LDB_SCOPE_SUBTREE
, realm_ref_attrs
,
1003 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
1006 if (ret
!= LDB_SUCCESS
) {
1007 DEBUG(3, ("Failed to search to lookup realm(%s): %s\n", realm
, ldb_errstring(ldb_ctx
)));
1008 talloc_free(cross_ref_res
);
1009 return HDB_ERR_NOENTRY
;
1010 } else if (cross_ref_res
->count
== 0 || cross_ref_res
->count
> 1) {
1011 DEBUG(3, ("Failed find a single entry for realm %s: got %d\n", realm
, cross_ref_res
->count
));
1012 talloc_free(cross_ref_res
);
1013 return HDB_ERR_NOENTRY
;
1017 *pmsg
= cross_ref_res
->msgs
;
1018 talloc_steal(mem_ctx
, cross_ref_res
->msgs
);
1020 talloc_free(cross_ref_res
);
1026 static krb5_error_code
LDB_open(krb5_context context
, HDB
*db
, int flags
, mode_t mode
)
1028 if (db
->hdb_master_key_set
) {
1029 krb5_warnx(context
, "LDB_open: use of a master key incompatible with LDB\n");
1030 krb5_set_error_string(context
, "LDB_open: use of a master key incompatible with LDB\n");
1031 return HDB_ERR_NOENTRY
;
1037 static krb5_error_code
LDB_close(krb5_context context
, HDB
*db
)
1042 static krb5_error_code
LDB_lock(krb5_context context
, HDB
*db
, int operation
)
1047 static krb5_error_code
LDB_unlock(krb5_context context
, HDB
*db
)
1052 static krb5_error_code
LDB_rename(krb5_context context
, HDB
*db
, const char *new_name
)
1054 return HDB_ERR_DB_INUSE
;
1057 static krb5_error_code
LDB_fetch_client(krb5_context context
, HDB
*db
,
1058 TALLOC_CTX
*mem_ctx
,
1059 krb5_const_principal principal
,
1061 hdb_entry_ex
*entry_ex
) {
1063 char *principal_string
;
1064 krb5_error_code ret
;
1065 struct ldb_message
**msg
= NULL
;
1066 struct ldb_message
**realm_ref_msg
= NULL
;
1068 ret
= krb5_unparse_name(context
, principal
, &principal_string
);
1074 nt_status
= sam_get_results_principal((struct ldb_context
*)db
->hdb_db
,
1075 mem_ctx
, principal_string
,
1076 &msg
, &realm_ref_msg
);
1077 free(principal_string
);
1078 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
1079 return HDB_ERR_NOENTRY
;
1080 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_MEMORY
)) {
1082 } else if (!NT_STATUS_IS_OK(nt_status
)) {
1086 ret
= LDB_message2entry(context
, db
, mem_ctx
,
1087 principal
, HDB_SAMBA4_ENT_TYPE_CLIENT
,
1088 msg
[0], realm_ref_msg
[0], entry_ex
);
1092 static krb5_error_code
LDB_fetch_krbtgt(krb5_context context
, HDB
*db
,
1093 TALLOC_CTX
*mem_ctx
,
1094 krb5_const_principal principal
,
1096 hdb_entry_ex
*entry_ex
)
1098 krb5_error_code ret
;
1099 struct ldb_message
**msg
= NULL
;
1100 struct ldb_message
**realm_ref_msg_1
= NULL
;
1101 struct ldb_message
**realm_ref_msg_2
= NULL
;
1102 struct ldb_dn
*realm_dn
;
1105 krb5_principal alloc_principal
= NULL
;
1106 if (principal
->name
.name_string
.len
!= 2
1107 || (strcmp(principal
->name
.name_string
.val
[0], KRB5_TGS_NAME
) != 0)) {
1109 return HDB_ERR_NOENTRY
;
1112 /* krbtgt case. Either us or a trusted realm */
1114 if ((LDB_lookup_realm(context
, (struct ldb_context
*)db
->hdb_db
,
1115 mem_ctx
, principal
->realm
, &realm_ref_msg_1
) == 0)
1116 && (LDB_lookup_realm(context
, (struct ldb_context
*)db
->hdb_db
,
1117 mem_ctx
, principal
->name
.name_string
.val
[1], &realm_ref_msg_2
) == 0)
1118 && (ldb_dn_compare(realm_ref_msg_1
[0]->dn
, realm_ref_msg_1
[0]->dn
) == 0)) {
1120 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1121 * is in our db, then direct the caller at our primary
1124 const char *dnsdomain
= ldb_msg_find_attr_as_string(realm_ref_msg_1
[0], "dnsRoot", NULL
);
1125 char *realm_fixed
= strupper_talloc(mem_ctx
, dnsdomain
);
1127 krb5_set_error_string(context
, "strupper_talloc: out of memory");
1131 ret
= krb5_copy_principal(context
, principal
, &alloc_principal
);
1136 free(alloc_principal
->name
.name_string
.val
[1]);
1137 alloc_principal
->name
.name_string
.val
[1] = strdup(realm_fixed
);
1138 talloc_free(realm_fixed
);
1139 if (!alloc_principal
->name
.name_string
.val
[1]) {
1140 krb5_set_error_string(context
, "LDB_fetch: strdup() failed!");
1143 principal
= alloc_principal
;
1144 realm_dn
= samdb_result_dn((struct ldb_context
*)db
->hdb_db
, mem_ctx
, realm_ref_msg_1
[0], "nCName", NULL
);
1146 ret
= LDB_lookup_principal(context
, (struct ldb_context
*)db
->hdb_db
,
1148 principal
, HDB_SAMBA4_ENT_TYPE_KRBTGT
, realm_dn
, &msg
);
1151 krb5_warnx(context
, "LDB_fetch: could not find principal in DB");
1152 krb5_set_error_string(context
, "LDB_fetch: could not find principal in DB");
1156 ret
= LDB_message2entry(context
, db
, mem_ctx
,
1157 principal
, HDB_SAMBA4_ENT_TYPE_KRBTGT
,
1158 msg
[0], realm_ref_msg_1
[0], entry_ex
);
1160 krb5_warnx(context
, "LDB_fetch: self krbtgt message2entry failed");
1165 enum trust_direction direction
= UNKNOWN
;
1167 struct loadparm_context
*lp_ctx
= talloc_get_type(ldb_get_opaque(db
->hdb_db
, "loadparm"), struct loadparm_context
);
1168 /* Either an inbound or outbound trust */
1170 if (strcasecmp(lp_realm(lp_ctx
), principal
->realm
) == 0) {
1171 /* look for inbound trust */
1172 direction
= INBOUND
;
1173 realm
= principal
->name
.name_string
.val
[1];
1176 if (strcasecmp(lp_realm(lp_ctx
), principal
->name
.name_string
.val
[1]) == 0) {
1177 /* look for outbound trust */
1178 direction
= OUTBOUND
;
1179 realm
= principal
->realm
;
1182 /* Trusted domains are under CN=system */
1184 ret
= LDB_lookup_trust(context
, (struct ldb_context
*)db
->hdb_db
,
1186 realm
, realm_dn
, &msg
);
1189 krb5_warnx(context
, "LDB_fetch: could not find principal in DB");
1190 krb5_set_error_string(context
, "LDB_fetch: could not find principal in DB");
1194 ret
= LDB_trust_message2entry(context
, db
, lp_ctx
, mem_ctx
,
1195 principal
, direction
,
1198 krb5_warnx(context
, "LDB_fetch: trust_message2entry failed");
1203 /* we should lookup trusted domains */
1204 return HDB_ERR_NOENTRY
;
1209 static krb5_error_code
LDB_fetch_server(krb5_context context
, HDB
*db
,
1210 TALLOC_CTX
*mem_ctx
,
1211 krb5_const_principal principal
,
1213 hdb_entry_ex
*entry_ex
)
1215 krb5_error_code ret
;
1217 struct ldb_message
**msg
= NULL
;
1218 struct ldb_message
**realm_ref_msg
= NULL
;
1219 struct ldb_dn
*partitions_basedn
= samdb_partitions_dn(db
->hdb_db
, mem_ctx
);
1220 if (principal
->name
.name_string
.len
>= 2) {
1221 /* 'normal server' case */
1224 struct ldb_dn
*user_dn
, *domain_dn
;
1225 char *principal_string
;
1227 ret
= krb5_unparse_name_flags(context
, principal
,
1228 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
1234 /* At this point we may find the host is known to be
1235 * in a different realm, so we should generate a
1236 * referral instead */
1237 nt_status
= crack_service_principal_name((struct ldb_context
*)db
->hdb_db
,
1238 mem_ctx
, principal_string
,
1239 &user_dn
, &domain_dn
);
1240 free(principal_string
);
1242 if (!NT_STATUS_IS_OK(nt_status
)) {
1243 return HDB_ERR_NOENTRY
;
1246 ldb_ret
= gendb_search_dn((struct ldb_context
*)db
->hdb_db
,
1247 mem_ctx
, user_dn
, &msg
, user_attrs
);
1250 return HDB_ERR_NOENTRY
;
1253 ldb_ret
= gendb_search((struct ldb_context
*)db
->hdb_db
,
1254 mem_ctx
, partitions_basedn
, &realm_ref_msg
, realm_ref_attrs
,
1255 "ncName=%s", ldb_dn_get_linearized(domain_dn
));
1258 return HDB_ERR_NOENTRY
;
1262 struct ldb_dn
*realm_dn
;
1263 /* server as client principal case, but we must not lookup userPrincipalNames */
1265 realm
= krb5_principal_get_realm(context
, principal
);
1267 ret
= LDB_lookup_realm(context
, (struct ldb_context
*)db
->hdb_db
,
1268 mem_ctx
, realm
, &realm_ref_msg
);
1270 return HDB_ERR_NOENTRY
;
1273 realm_dn
= samdb_result_dn((struct ldb_context
*)db
->hdb_db
, mem_ctx
, realm_ref_msg
[0], "nCName", NULL
);
1275 ret
= LDB_lookup_principal(context
, (struct ldb_context
*)db
->hdb_db
,
1277 principal
, HDB_SAMBA4_ENT_TYPE_SERVER
, realm_dn
, &msg
);
1284 ret
= LDB_message2entry(context
, db
, mem_ctx
,
1285 principal
, HDB_SAMBA4_ENT_TYPE_SERVER
,
1286 msg
[0], realm_ref_msg
[0], entry_ex
);
1288 krb5_warnx(context
, "LDB_fetch: message2entry failed");
1294 static krb5_error_code
LDB_fetch(krb5_context context
, HDB
*db
,
1295 krb5_const_principal principal
,
1297 hdb_entry_ex
*entry_ex
)
1299 krb5_error_code ret
= HDB_ERR_NOENTRY
;
1301 TALLOC_CTX
*mem_ctx
= talloc_named(db
, 0, "LDB_fetch context");
1304 krb5_set_error_string(context
, "LDB_fetch: talloc_named() failed!");
1308 if (flags
& HDB_F_GET_CLIENT
) {
1309 ret
= LDB_fetch_client(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
1310 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1312 if (flags
& HDB_F_GET_SERVER
) {
1313 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
1314 ret
= LDB_fetch_krbtgt(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
1315 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1317 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
1318 ret
= LDB_fetch_server(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
1319 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1321 if (flags
& HDB_F_GET_KRBTGT
) {
1322 ret
= LDB_fetch_krbtgt(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
1323 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
1327 talloc_free(mem_ctx
);
1331 static krb5_error_code
LDB_store(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry_ex
*entry
)
1333 return HDB_ERR_DB_INUSE
;
1336 static krb5_error_code
LDB_remove(krb5_context context
, HDB
*db
, krb5_const_principal principal
)
1338 return HDB_ERR_DB_INUSE
;
1341 struct hdb_ldb_seq
{
1342 struct ldb_context
*ctx
;
1345 struct ldb_message
**msgs
;
1346 struct ldb_message
**realm_ref_msgs
;
1349 static krb5_error_code
LDB_seq(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry_ex
*entry
)
1351 krb5_error_code ret
;
1352 struct hdb_ldb_seq
*priv
= (struct hdb_ldb_seq
*)db
->hdb_dbc
;
1353 TALLOC_CTX
*mem_ctx
;
1354 hdb_entry_ex entry_ex
;
1355 memset(&entry_ex
, '\0', sizeof(entry_ex
));
1358 return HDB_ERR_NOENTRY
;
1361 mem_ctx
= talloc_named(priv
, 0, "LDB_seq context");
1364 krb5_set_error_string(context
, "LDB_seq: talloc_named() failed!");
1368 if (priv
->index
< priv
->count
) {
1369 ret
= LDB_message2entry(context
, db
, mem_ctx
,
1370 NULL
, HDB_SAMBA4_ENT_TYPE_ANY
,
1371 priv
->msgs
[priv
->index
++],
1372 priv
->realm_ref_msgs
[0], entry
);
1374 ret
= HDB_ERR_NOENTRY
;
1381 talloc_free(mem_ctx
);
1387 static krb5_error_code
LDB_firstkey(krb5_context context
, HDB
*db
, unsigned flags
,
1388 hdb_entry_ex
*entry
)
1390 struct ldb_context
*ldb_ctx
= (struct ldb_context
*)db
->hdb_db
;
1391 struct hdb_ldb_seq
*priv
= (struct hdb_ldb_seq
*)db
->hdb_dbc
;
1393 struct ldb_dn
*realm_dn
= NULL
;
1394 struct ldb_result
*res
= NULL
;
1395 struct ldb_message
**realm_ref_msgs
= NULL
;
1396 krb5_error_code ret
;
1397 TALLOC_CTX
*mem_ctx
;
1405 priv
= (struct hdb_ldb_seq
*) talloc(db
, struct hdb_ldb_seq
);
1407 krb5_set_error_string(context
, "talloc: out of memory");
1411 priv
->ctx
= ldb_ctx
;
1414 priv
->realm_ref_msgs
= NULL
;
1417 mem_ctx
= talloc_named(priv
, 0, "LDB_firstkey context");
1420 krb5_set_error_string(context
, "LDB_firstkey: talloc_named() failed!");
1424 ret
= krb5_get_default_realm(context
, &realm
);
1430 ret
= LDB_lookup_realm(context
, (struct ldb_context
*)db
->hdb_db
,
1431 mem_ctx
, realm
, &realm_ref_msgs
);
1437 krb5_warnx(context
, "LDB_firstkey: could not find realm\n");
1438 return HDB_ERR_NOENTRY
;
1441 realm_dn
= samdb_result_dn((struct ldb_context
*)db
->hdb_db
, mem_ctx
, realm_ref_msgs
[0], "nCName", NULL
);
1443 priv
->realm_ref_msgs
= talloc_steal(priv
, realm_ref_msgs
);
1445 lret
= ldb_search(ldb_ctx
, priv
, &res
,
1446 realm_dn
, LDB_SCOPE_SUBTREE
, user_attrs
,
1447 "(objectClass=user)");
1449 if (lret
!= LDB_SUCCESS
) {
1451 return HDB_ERR_NOENTRY
;
1454 priv
->count
= res
->count
;
1455 priv
->msgs
= talloc_steal(priv
, res
->msgs
);
1460 ret
= LDB_seq(context
, db
, flags
, entry
);
1466 talloc_free(mem_ctx
);
1471 static krb5_error_code
LDB_nextkey(krb5_context context
, HDB
*db
, unsigned flags
,
1472 hdb_entry_ex
*entry
)
1474 return LDB_seq(context
, db
, flags
, entry
);
1477 static krb5_error_code
LDB_destroy(krb5_context context
, HDB
*db
)
1483 /* This interface is to be called by the KDC, which is expecting Samba
1484 * calling conventions. It is also called by a wrapper
1485 * (hdb_ldb_create) from the kpasswdd -> krb5 -> keytab_hdb -> hdb
1488 NTSTATUS
kdc_hdb_samba4_create(TALLOC_CTX
*mem_ctx
,
1489 struct event_context
*ev_ctx
,
1490 struct loadparm_context
*lp_ctx
,
1491 krb5_context context
, struct HDB
**db
, const char *arg
)
1494 struct auth_session_info
*session_info
;
1495 *db
= talloc(mem_ctx
, HDB
);
1497 krb5_set_error_string(context
, "malloc: out of memory");
1498 return NT_STATUS_NO_MEMORY
;
1501 (*db
)->hdb_master_key_set
= 0;
1502 (*db
)->hdb_db
= NULL
;
1504 nt_status
= auth_system_session_info(*db
, lp_ctx
, &session_info
);
1505 if (!NT_STATUS_IS_OK(nt_status
)) {
1509 /* The idea here is very simple. Using Kerberos to
1510 * authenticate the KDC to the LDAP server is higly likely to
1513 * In future we may set this up to use EXERNAL and SSL
1514 * certificates, for now it will almost certainly be NTLMSSP
1517 cli_credentials_set_kerberos_state(session_info
->credentials
,
1518 CRED_DONT_USE_KERBEROS
);
1520 /* Setup the link to LDB */
1521 (*db
)->hdb_db
= samdb_connect(*db
, ev_ctx
, lp_ctx
, session_info
);
1522 if ((*db
)->hdb_db
== NULL
) {
1523 DEBUG(1, ("hdb_ldb_create: Cannot open samdb for KDC backend!"));
1524 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1527 (*db
)->hdb_dbc
= NULL
;
1528 (*db
)->hdb_open
= LDB_open
;
1529 (*db
)->hdb_close
= LDB_close
;
1530 (*db
)->hdb_fetch
= LDB_fetch
;
1531 (*db
)->hdb_store
= LDB_store
;
1532 (*db
)->hdb_remove
= LDB_remove
;
1533 (*db
)->hdb_firstkey
= LDB_firstkey
;
1534 (*db
)->hdb_nextkey
= LDB_nextkey
;
1535 (*db
)->hdb_lock
= LDB_lock
;
1536 (*db
)->hdb_unlock
= LDB_unlock
;
1537 (*db
)->hdb_rename
= LDB_rename
;
1538 /* we don't implement these, as we are not a lockable database */
1539 (*db
)->hdb__get
= NULL
;
1540 (*db
)->hdb__put
= NULL
;
1541 /* kadmin should not be used for deletes - use other tools instead */
1542 (*db
)->hdb__del
= NULL
;
1543 (*db
)->hdb_destroy
= LDB_destroy
;
1545 return NT_STATUS_OK
;
1548 krb5_error_code
hdb_samba4_create(krb5_context context
, struct HDB
**db
, const char *arg
)
1551 /* The global kdc_mem_ctx and kdc_lp_ctx, Disgusting, ugly hack, but it means one less private hook */
1552 nt_status
= kdc_hdb_samba4_create(kdc_mem_ctx
, event_context_find(kdc_mem_ctx
), kdc_lp_ctx
,
1555 if (NT_STATUS_IS_OK(nt_status
)) {