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"
38 #include "dsdb/common/flags.h"
40 #include "krb5_locl.h"
41 #include "lib/ldb/include/ldb.h"
42 #include "lib/ldb/include/ldb_errors.h"
43 #include "librpc/gen_ndr/netlogon.h"
44 #include "auth/auth.h"
45 #include "auth/credentials/credentials.h"
46 #include "auth/auth_sam.h"
48 #include "dsdb/samdb/samdb.h"
49 #include "librpc/ndr/libndr.h"
50 #include "librpc/gen_ndr/ndr_drsblobs.h"
51 #include "libcli/auth/libcli_auth.h"
54 { HDB_LDB_ENT_TYPE_CLIENT
, HDB_LDB_ENT_TYPE_SERVER
,
55 HDB_LDB_ENT_TYPE_KRBTGT
, HDB_LDB_ENT_TYPE_ANY
};
57 static const char * const krb5_attrs
[] = {
62 "servicePrincipalName",
72 "msDS-KeyVersionNumber",
75 "supplementalCredentials",
80 static const char *realm_ref_attrs
[] = {
86 static KerberosTime
ldb_msg_find_krb5time_ldap_time(struct ldb_message
*msg
, const char *attr
, KerberosTime default_val
)
92 gentime
= ldb_msg_find_attr_as_string(msg
, attr
, NULL
);
96 tmp
= strptime(gentime
, "%Y%m%d%H%M%SZ", &tm
);
104 static HDBFlags
uf2HDBFlags(krb5_context context
, int userAccountControl
, enum hdb_ldb_ent_type ent_type
)
106 HDBFlags flags
= int2HDBFlags(0);
108 /* we don't allow kadmin deletes */
111 /* mark the principal as invalid to start with */
116 /* All accounts are servers, but this may be disabled again in the caller */
119 /* Account types - clear the invalid bit if it turns out to be valid */
120 if (userAccountControl
& UF_NORMAL_ACCOUNT
) {
121 if (ent_type
== HDB_LDB_ENT_TYPE_CLIENT
|| ent_type
== HDB_LDB_ENT_TYPE_ANY
) {
127 if (userAccountControl
& UF_INTERDOMAIN_TRUST_ACCOUNT
) {
128 if (ent_type
== HDB_LDB_ENT_TYPE_CLIENT
|| ent_type
== HDB_LDB_ENT_TYPE_ANY
) {
133 if (userAccountControl
& UF_WORKSTATION_TRUST_ACCOUNT
) {
134 if (ent_type
== HDB_LDB_ENT_TYPE_CLIENT
|| ent_type
== HDB_LDB_ENT_TYPE_ANY
) {
139 if (userAccountControl
& UF_SERVER_TRUST_ACCOUNT
) {
140 if (ent_type
== HDB_LDB_ENT_TYPE_CLIENT
|| ent_type
== HDB_LDB_ENT_TYPE_ANY
) {
146 /* Not permitted to act as a client if disabled */
147 if (userAccountControl
& UF_ACCOUNTDISABLE
) {
150 if (userAccountControl
& UF_LOCKOUT
) {
154 if (userAccountControl & UF_PASSWORD_NOTREQD) {
159 UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
161 if (userAccountControl
& UF_TEMP_DUPLICATE_ACCOUNT
) {
165 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in LDB_message2entry() */
168 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
172 if (userAccountControl
& UF_SMARTCARD_REQUIRED
) {
173 flags
.require_hwauth
= 1;
175 if (userAccountControl
& UF_TRUSTED_FOR_DELEGATION
) {
176 flags
.ok_as_delegate
= 1;
178 if (!(userAccountControl
& UF_NOT_DELEGATED
)) {
179 flags
.forwardable
= 1;
183 if (userAccountControl
& UF_DONT_REQUIRE_PREAUTH
) {
184 flags
.require_preauth
= 0;
186 flags
.require_preauth
= 1;
192 static int hdb_ldb_destrutor(struct hdb_ldb_private
*private)
194 hdb_entry_ex
*entry_ex
= private->entry_ex
;
195 free_hdb_entry(&entry_ex
->entry
);
199 static void hdb_ldb_free_entry(krb5_context context
, hdb_entry_ex
*entry_ex
)
201 talloc_free(entry_ex
->ctx
);
204 static krb5_error_code
LDB_message2entry_keys(krb5_context context
,
206 struct ldb_message
*msg
,
207 unsigned int userAccountControl
,
208 hdb_entry_ex
*entry_ex
)
210 krb5_error_code ret
= 0;
212 struct samr_Password
*hash
;
213 const struct ldb_val
*sc_val
;
214 struct supplementalCredentialsBlob scb
;
215 struct supplementalCredentialsPackage
*scp
= NULL
;
216 struct package_PrimaryKerberosBlob _pkb
;
217 struct package_PrimaryKerberosCtr3
*pkb3
= 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 status
= ndr_pull_struct_blob_all(sc_val
, mem_ctx
, &scb
,
239 (ndr_pull_flags_fn_t
)ndr_pull_supplementalCredentialsBlob
);
240 if (!NT_STATUS_IS_OK(status
)) {
241 dump_data(0, sc_val
->data
, sc_val
->length
);
246 for (i
=0; i
< scb
.sub
.num_packages
; i
++) {
247 if (scb
.sub
.packages
[i
].unknown1
!= 0x00000001) {
251 if (strcmp("Primary:Kerberos", scb
.sub
.packages
[i
].name
) != 0) {
255 if (!scb
.sub
.packages
[i
].data
|| !scb
.sub
.packages
[i
].data
[0]) {
259 scp
= &scb
.sub
.packages
[i
];
263 /* Primary:Kerberos element of supplementalCredentials */
267 blob
= strhex_to_data_blob(scp
->data
);
272 talloc_steal(mem_ctx
, blob
.data
);
274 /* TODO: use ndr_pull_struct_blob_all(), when the ndr layer handles it correct with relative pointers */
275 status
= ndr_pull_struct_blob(&blob
, mem_ctx
, &_pkb
,
276 (ndr_pull_flags_fn_t
)ndr_pull_package_PrimaryKerberosBlob
);
277 if (!NT_STATUS_IS_OK(status
)) {
278 krb5_set_error_string(context
, "LDB_message2entry_keys: could not parse package_PrimaryKerberosBlob");
279 krb5_warnx(context
, "LDB_message2entry_keys: could not parse package_PrimaryKerberosBlob");
284 if (_pkb
.version
!= 3) {
285 krb5_set_error_string(context
, "LDB_message2entry_keys: could not parse PrimaryKerberos not version 3");
286 krb5_warnx(context
, "LDB_message2entry_keys: could not parse PrimaryKerberos not version 3");
291 pkb3
= &_pkb
.ctr
.ctr3
;
293 allocated_keys
+= pkb3
->num_keys
;
296 if (allocated_keys
== 0) {
297 /* oh, no password. Apparently (comment in
298 * hdb-ldap.c) this violates the ASN.1, but this
299 * allows an entry with no keys (yet). */
303 /* allocate space to decode into */
304 entry_ex
->entry
.keys
.len
= 0;
305 entry_ex
->entry
.keys
.val
= calloc(allocated_keys
, sizeof(Key
));
306 if (entry_ex
->entry
.keys
.val
== NULL
) {
311 if (hash
&& !(userAccountControl
& UF_USE_DES_KEY_ONLY
)) {
315 key
.salt
= NULL
; /* No salt for this enc type */
317 ret
= krb5_keyblock_init(context
,
318 ENCTYPE_ARCFOUR_HMAC_MD5
,
319 hash
->hash
, sizeof(hash
->hash
),
325 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
326 entry_ex
->entry
.keys
.len
++;
330 for (i
=0; i
< pkb3
->num_keys
; i
++) {
334 if (!pkb3
->keys
[i
].value
) continue;
336 if (userAccountControl
& UF_USE_DES_KEY_ONLY
) {
337 switch (pkb3
->keys
[i
].keytype
) {
338 case ENCTYPE_DES_CBC_CRC
:
339 case ENCTYPE_DES_CBC_MD5
:
351 if (pkb3
->salt
.string
) {
354 salt
= data_blob_string_const(pkb3
->salt
.string
);
356 key
.salt
= calloc(1, sizeof(*key
.salt
));
357 if (key
.salt
== NULL
) {
362 key
.salt
->type
= hdb_pw_salt
;
364 ret
= krb5_data_copy(&key
.salt
->salt
, salt
.data
, salt
.length
);
372 ret
= krb5_keyblock_init(context
,
373 pkb3
->keys
[i
].keytype
,
374 pkb3
->keys
[i
].value
->data
,
375 pkb3
->keys
[i
].value
->length
,
386 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
387 entry_ex
->entry
.keys
.len
++;
393 entry_ex
->entry
.keys
.len
= 0;
395 if (entry_ex
->entry
.keys
.len
== 0 && entry_ex
->entry
.keys
.val
) {
396 free(entry_ex
->entry
.keys
.val
);
397 entry_ex
->entry
.keys
.val
= NULL
;
403 * Construct an hdb_entry from a directory entry.
405 static krb5_error_code
LDB_message2entry(krb5_context context
, HDB
*db
,
406 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
407 enum hdb_ldb_ent_type ent_type
,
408 struct ldb_message
*msg
,
409 struct ldb_message
*realm_ref_msg
,
410 hdb_entry_ex
*entry_ex
)
412 unsigned int userAccountControl
;
414 krb5_error_code ret
= 0;
415 krb5_boolean is_computer
= FALSE
;
416 const char *dnsdomain
= ldb_msg_find_attr_as_string(realm_ref_msg
, "dnsRoot", NULL
);
417 char *realm
= strupper_talloc(mem_ctx
, dnsdomain
);
418 struct ldb_dn
*domain_dn
= samdb_result_dn((struct ldb_context
*)db
->hdb_db
,
422 ldb_dn_new(mem_ctx
, (struct ldb_context
*)db
->hdb_db
, NULL
));
424 struct hdb_ldb_private
*private;
427 struct ldb_message_element
*objectclasses
;
428 struct ldb_val computer_val
;
429 computer_val
.data
= discard_const_p(uint8_t,"computer");
430 computer_val
.length
= strlen((const char *)computer_val
.data
);
432 objectclasses
= ldb_msg_find_element(msg
, "objectClass");
434 if (objectclasses
&& ldb_msg_find_val(objectclasses
, &computer_val
)) {
438 memset(entry_ex
, 0, sizeof(*entry_ex
));
441 krb5_set_error_string(context
, "talloc_strdup: out of memory");
446 private = talloc(mem_ctx
, struct hdb_ldb_private
);
452 private->entry_ex
= entry_ex
;
454 talloc_set_destructor(private, hdb_ldb_destrutor
);
456 entry_ex
->ctx
= private;
457 entry_ex
->free_entry
= hdb_ldb_free_entry
;
459 userAccountControl
= ldb_msg_find_attr_as_uint(msg
, "userAccountControl", 0);
462 entry_ex
->entry
.principal
= malloc(sizeof(*(entry_ex
->entry
.principal
)));
463 if (ent_type
== HDB_LDB_ENT_TYPE_ANY
&& principal
== NULL
) {
464 const char *samAccountName
= ldb_msg_find_attr_as_string(msg
, "samAccountName", NULL
);
465 if (!samAccountName
) {
466 krb5_set_error_string(context
, "LDB_message2entry: no samAccountName present");
470 samAccountName
= ldb_msg_find_attr_as_string(msg
, "samAccountName", NULL
);
471 krb5_make_principal(context
, &entry_ex
->entry
.principal
, realm
, samAccountName
, NULL
);
474 ret
= copy_Principal(principal
, entry_ex
->entry
.principal
);
476 krb5_clear_error_string(context
);
480 /* While we have copied the client principal, tests
481 * show that Win2k3 returns the 'corrected' realm, not
482 * the client-specified realm. This code attempts to
483 * replace the client principal's realm with the one
484 * we determine from our records */
486 /* this has to be with malloc() */
487 strdup_realm
= strdup(realm
);
490 krb5_clear_error_string(context
);
493 free(*krb5_princ_realm(context
, entry_ex
->entry
.principal
));
494 krb5_princ_set_realm(context
, entry_ex
->entry
.principal
, &strdup_realm
);
497 entry_ex
->entry
.flags
= uf2HDBFlags(context
, userAccountControl
, ent_type
);
499 if (ent_type
== HDB_LDB_ENT_TYPE_KRBTGT
) {
500 entry_ex
->entry
.flags
.invalid
= 0;
501 entry_ex
->entry
.flags
.server
= 1;
502 entry_ex
->entry
.flags
.forwardable
= 1;
503 entry_ex
->entry
.flags
.ok_as_delegate
= 1;
506 if (lp_parm_bool(-1, "kdc", "require spn for service", True
)) {
507 if (!is_computer
&& !ldb_msg_find_attr_as_string(msg
, "servicePrincipalName", NULL
)) {
508 entry_ex
->entry
.flags
.server
= 0;
512 /* use 'whenCreated' */
513 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
515 entry_ex
->entry
.created_by
.principal
= NULL
;
517 entry_ex
->entry
.modified_by
= (Event
*) malloc(sizeof(Event
));
518 if (entry_ex
->entry
.modified_by
== NULL
) {
519 krb5_set_error_string(context
, "malloc: out of memory");
524 /* use 'whenChanged' */
525 entry_ex
->entry
.modified_by
->time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenChanged", 0);
527 entry_ex
->entry
.modified_by
->principal
= NULL
;
529 entry_ex
->entry
.valid_start
= NULL
;
531 acct_expiry
= samdb_result_nttime(msg
, "accountExpires", (NTTIME
)-1);
532 if ((acct_expiry
== (NTTIME
)-1) ||
533 (acct_expiry
== 0x7FFFFFFFFFFFFFFFULL
)) {
534 entry_ex
->entry
.valid_end
= NULL
;
536 entry_ex
->entry
.valid_end
= malloc(sizeof(*entry_ex
->entry
.valid_end
));
537 if (entry_ex
->entry
.valid_end
== NULL
) {
541 *entry_ex
->entry
.valid_end
= nt_time_to_unix(acct_expiry
);
544 if (ent_type
!= HDB_LDB_ENT_TYPE_KRBTGT
) {
545 NTTIME must_change_time
546 = samdb_result_force_password_change((struct ldb_context
*)db
->hdb_db
, mem_ctx
,
548 if (must_change_time
== 0x7FFFFFFFFFFFFFFFULL
) {
549 entry_ex
->entry
.pw_end
= NULL
;
551 entry_ex
->entry
.pw_end
= malloc(sizeof(*entry_ex
->entry
.pw_end
));
552 if (entry_ex
->entry
.pw_end
== NULL
) {
556 *entry_ex
->entry
.pw_end
= nt_time_to_unix(must_change_time
);
559 entry_ex
->entry
.pw_end
= NULL
;
562 entry_ex
->entry
.max_life
= NULL
;
564 entry_ex
->entry
.max_renew
= NULL
;
566 entry_ex
->entry
.generation
= NULL
;
568 /* Get keys from the db */
569 ret
= LDB_message2entry_keys(context
, private, msg
, userAccountControl
, entry_ex
);
571 /* Could be bougus data in the entry, or out of memory */
575 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
576 if (entry_ex
->entry
.etypes
== NULL
) {
577 krb5_clear_error_string(context
);
581 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
582 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
583 if (entry_ex
->entry
.etypes
->val
== NULL
) {
584 krb5_clear_error_string(context
);
588 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
589 entry_ex
->entry
.etypes
->val
[i
] = entry_ex
->entry
.keys
.val
[i
].key
.keytype
;
593 private->msg
= talloc_steal(private, msg
);
594 private->realm_ref_msg
= talloc_steal(private, realm_ref_msg
);
595 private->samdb
= (struct ldb_context
*)db
->hdb_db
;
599 /* This doesn't free ent itself, that is for the eventual caller to do */
600 hdb_free_entry(context
, entry_ex
);
602 talloc_steal(db
, entry_ex
->ctx
);
608 static krb5_error_code
LDB_lookup_principal(krb5_context context
, struct ldb_context
*ldb_ctx
,
610 krb5_const_principal principal
,
611 enum hdb_ldb_ent_type ent_type
,
612 struct ldb_dn
*realm_dn
,
613 struct ldb_message
***pmsg
)
618 const char * const *princ_attrs
= krb5_attrs
;
621 char *short_princ_talloc
;
623 struct ldb_result
*res
= NULL
;
625 ret
= krb5_unparse_name_flags(context
, principal
, KRB5_PRINCIPAL_UNPARSE_NO_REALM
, &short_princ
);
628 krb5_set_error_string(context
, "LDB_lookup_principal: could not parse principal");
629 krb5_warnx(context
, "LDB_lookup_principal: could not parse principal");
633 short_princ_talloc
= talloc_strdup(mem_ctx
, short_princ
);
635 if (!short_princ_talloc
) {
636 krb5_set_error_string(context
, "LDB_lookup_principal: talloc_strdup() failed!");
641 case HDB_LDB_ENT_TYPE_CLIENT
:
644 case HDB_LDB_ENT_TYPE_ANY
:
647 case HDB_LDB_ENT_TYPE_KRBTGT
:
648 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=user)(samAccountName=%s))",
651 case HDB_LDB_ENT_TYPE_SERVER
:
652 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=user)(samAccountName=%s))",
658 krb5_set_error_string(context
, "talloc_asprintf: out of memory");
662 lret
= ldb_search(ldb_ctx
, realm_dn
, LDB_SCOPE_SUBTREE
, filter
, princ_attrs
, &res
);
664 if (lret
!= LDB_SUCCESS
) {
665 DEBUG(3, ("Failed to search for %s: %s\n", filter
, ldb_errstring(ldb_ctx
)));
666 return HDB_ERR_NOENTRY
;
667 } else if (res
->count
== 0 || res
->count
> 1) {
668 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter
, res
->count
));
670 return HDB_ERR_NOENTRY
;
672 talloc_steal(mem_ctx
, res
->msgs
);
678 static krb5_error_code
LDB_lookup_realm(krb5_context context
, struct ldb_context
*ldb_ctx
,
681 struct ldb_message
***pmsg
)
684 struct ldb_result
*cross_ref_res
;
685 struct ldb_dn
*partitions_basedn
= samdb_partitions_dn(ldb_ctx
, mem_ctx
);
687 ret
= ldb_search_exp_fmt(ldb_ctx
, mem_ctx
, &cross_ref_res
,
688 partitions_basedn
, LDB_SCOPE_SUBTREE
, realm_ref_attrs
,
689 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
692 if (ret
!= LDB_SUCCESS
) {
693 DEBUG(3, ("Failed to search to lookup realm(%s): %s\n", realm
, ldb_errstring(ldb_ctx
)));
694 talloc_free(cross_ref_res
);
695 return HDB_ERR_NOENTRY
;
696 } else if (cross_ref_res
->count
== 0 || cross_ref_res
->count
> 1) {
697 DEBUG(3, ("Failed find a single entry for realm %s: got %d\n", realm
, cross_ref_res
->count
));
698 talloc_free(cross_ref_res
);
699 return HDB_ERR_NOENTRY
;
703 *pmsg
= cross_ref_res
->msgs
;
704 talloc_steal(mem_ctx
, cross_ref_res
->msgs
);
706 talloc_free(cross_ref_res
);
712 static krb5_error_code
LDB_open(krb5_context context
, HDB
*db
, int flags
, mode_t mode
)
714 if (db
->hdb_master_key_set
) {
715 krb5_warnx(context
, "LDB_open: use of a master key incompatible with LDB\n");
716 krb5_set_error_string(context
, "LDB_open: use of a master key incompatible with LDB\n");
717 return HDB_ERR_NOENTRY
;
723 static krb5_error_code
LDB_close(krb5_context context
, HDB
*db
)
728 static krb5_error_code
LDB_lock(krb5_context context
, HDB
*db
, int operation
)
733 static krb5_error_code
LDB_unlock(krb5_context context
, HDB
*db
)
738 static krb5_error_code
LDB_rename(krb5_context context
, HDB
*db
, const char *new_name
)
740 return HDB_ERR_DB_INUSE
;
743 static krb5_error_code
LDB_fetch_client(krb5_context context
, HDB
*db
,
745 krb5_const_principal principal
,
747 hdb_entry_ex
*entry_ex
) {
749 char *principal_string
;
751 struct ldb_message
**msg
= NULL
;
752 struct ldb_message
**realm_ref_msg
= NULL
;
754 ret
= krb5_unparse_name(context
, principal
, &principal_string
);
760 nt_status
= sam_get_results_principal((struct ldb_context
*)db
->hdb_db
,
761 mem_ctx
, principal_string
,
762 &msg
, &realm_ref_msg
);
763 free(principal_string
);
764 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
765 return HDB_ERR_NOENTRY
;
766 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_MEMORY
)) {
768 } else if (!NT_STATUS_IS_OK(nt_status
)) {
772 ret
= LDB_message2entry(context
, db
, mem_ctx
,
773 principal
, HDB_LDB_ENT_TYPE_CLIENT
,
774 msg
[0], realm_ref_msg
[0], entry_ex
);
778 static krb5_error_code
LDB_fetch_krbtgt(krb5_context context
, HDB
*db
,
780 krb5_const_principal principal
,
782 hdb_entry_ex
*entry_ex
)
785 struct ldb_message
**msg
= NULL
;
786 struct ldb_message
**realm_ref_msg
= NULL
;
787 struct ldb_dn
*realm_dn
;
789 krb5_principal alloc_principal
= NULL
;
790 if (principal
->name
.name_string
.len
!= 2
791 || (strcmp(principal
->name
.name_string
.val
[0], KRB5_TGS_NAME
) != 0)) {
793 return HDB_ERR_NOENTRY
;
796 /* krbtgt case. Either us or a trusted realm */
797 if ((LDB_lookup_realm(context
, (struct ldb_context
*)db
->hdb_db
,
798 mem_ctx
, principal
->name
.name_string
.val
[1], &realm_ref_msg
) == 0)) {
800 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
801 * is in our db, then direct the caller at our primary
804 const char *dnsdomain
= ldb_msg_find_attr_as_string(realm_ref_msg
[0], "dnsRoot", NULL
);
805 char *realm_fixed
= strupper_talloc(mem_ctx
, dnsdomain
);
807 krb5_set_error_string(context
, "strupper_talloc: out of memory");
811 ret
= krb5_copy_principal(context
, principal
, &alloc_principal
);
816 free(alloc_principal
->name
.name_string
.val
[1]);
817 alloc_principal
->name
.name_string
.val
[1] = strdup(realm_fixed
);
818 talloc_free(realm_fixed
);
819 if (!alloc_principal
->name
.name_string
.val
[1]) {
820 krb5_set_error_string(context
, "LDB_fetch: strdup() failed!");
823 principal
= alloc_principal
;
824 realm_dn
= samdb_result_dn((struct ldb_context
*)db
->hdb_db
, mem_ctx
, realm_ref_msg
[0], "nCName", NULL
);
826 /* we should lookup trusted domains */
827 return HDB_ERR_NOENTRY
;
830 realm_dn
= samdb_result_dn((struct ldb_context
*)db
->hdb_db
, mem_ctx
, realm_ref_msg
[0], "nCName", NULL
);
832 ret
= LDB_lookup_principal(context
, (struct ldb_context
*)db
->hdb_db
,
834 principal
, HDB_LDB_ENT_TYPE_KRBTGT
, realm_dn
, &msg
);
837 krb5_warnx(context
, "LDB_fetch: could not find principal in DB");
838 krb5_set_error_string(context
, "LDB_fetch: could not find principal in DB");
842 ret
= LDB_message2entry(context
, db
, mem_ctx
,
843 principal
, HDB_LDB_ENT_TYPE_KRBTGT
,
844 msg
[0], realm_ref_msg
[0], entry_ex
);
846 krb5_warnx(context
, "LDB_fetch: message2entry failed");
851 static krb5_error_code
LDB_fetch_server(krb5_context context
, HDB
*db
,
853 krb5_const_principal principal
,
855 hdb_entry_ex
*entry_ex
)
859 struct ldb_message
**msg
= NULL
;
860 struct ldb_message
**realm_ref_msg
= NULL
;
861 struct ldb_dn
*partitions_basedn
= samdb_partitions_dn(db
->hdb_db
, mem_ctx
);
862 if (principal
->name
.name_string
.len
>= 2) {
863 /* 'normal server' case */
866 struct ldb_dn
*user_dn
, *domain_dn
;
867 char *principal_string
;
869 ret
= krb5_unparse_name_flags(context
, principal
,
870 KRB5_PRINCIPAL_UNPARSE_NO_REALM
,
876 /* At this point we may find the host is known to be
877 * in a different realm, so we should generate a
878 * referral instead */
879 nt_status
= crack_service_principal_name((struct ldb_context
*)db
->hdb_db
,
880 mem_ctx
, principal_string
,
881 &user_dn
, &domain_dn
);
882 free(principal_string
);
884 if (!NT_STATUS_IS_OK(nt_status
)) {
885 return HDB_ERR_NOENTRY
;
888 ldb_ret
= gendb_search_dn((struct ldb_context
*)db
->hdb_db
,
889 mem_ctx
, user_dn
, &msg
, krb5_attrs
);
892 return HDB_ERR_NOENTRY
;
895 ldb_ret
= gendb_search((struct ldb_context
*)db
->hdb_db
,
896 mem_ctx
, partitions_basedn
, &realm_ref_msg
, realm_ref_attrs
,
897 "ncName=%s", ldb_dn_get_linearized(domain_dn
));
900 return HDB_ERR_NOENTRY
;
904 struct ldb_dn
*realm_dn
;
905 /* server as client principal case, but we must not lookup userPrincipalNames */
907 realm
= krb5_principal_get_realm(context
, principal
);
909 ret
= LDB_lookup_realm(context
, (struct ldb_context
*)db
->hdb_db
,
910 mem_ctx
, realm
, &realm_ref_msg
);
912 return HDB_ERR_NOENTRY
;
915 realm_dn
= samdb_result_dn((struct ldb_context
*)db
->hdb_db
, mem_ctx
, realm_ref_msg
[0], "nCName", NULL
);
917 ret
= LDB_lookup_principal(context
, (struct ldb_context
*)db
->hdb_db
,
919 principal
, HDB_LDB_ENT_TYPE_SERVER
, realm_dn
, &msg
);
926 ret
= LDB_message2entry(context
, db
, mem_ctx
,
927 principal
, HDB_LDB_ENT_TYPE_SERVER
,
928 msg
[0], realm_ref_msg
[0], entry_ex
);
930 krb5_warnx(context
, "LDB_fetch: message2entry failed");
936 static krb5_error_code
LDB_fetch(krb5_context context
, HDB
*db
,
937 krb5_const_principal principal
,
939 hdb_entry_ex
*entry_ex
)
941 krb5_error_code ret
= HDB_ERR_NOENTRY
;
943 TALLOC_CTX
*mem_ctx
= talloc_named(db
, 0, "LDB_fetch context");
946 krb5_set_error_string(context
, "LDB_fetch: talloc_named() failed!");
950 if (flags
& HDB_F_GET_CLIENT
) {
951 ret
= LDB_fetch_client(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
952 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
954 if (flags
& HDB_F_GET_SERVER
) {
955 ret
= LDB_fetch_server(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
956 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
957 ret
= LDB_fetch_krbtgt(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
958 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
960 if (flags
& HDB_F_GET_KRBTGT
) {
961 ret
= LDB_fetch_krbtgt(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
962 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
966 talloc_free(mem_ctx
);
970 static krb5_error_code
LDB_store(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry_ex
*entry
)
972 return HDB_ERR_DB_INUSE
;
975 static krb5_error_code
LDB_remove(krb5_context context
, HDB
*db
, krb5_const_principal principal
)
977 return HDB_ERR_DB_INUSE
;
981 struct ldb_context
*ctx
;
984 struct ldb_message
**msgs
;
985 struct ldb_message
**realm_ref_msgs
;
988 static krb5_error_code
LDB_seq(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry_ex
*entry
)
991 struct hdb_ldb_seq
*priv
= (struct hdb_ldb_seq
*)db
->hdb_openp
;
993 hdb_entry_ex entry_ex
;
994 memset(&entry_ex
, '\0', sizeof(entry_ex
));
997 return HDB_ERR_NOENTRY
;
1000 mem_ctx
= talloc_named(priv
, 0, "LDB_seq context");
1003 krb5_set_error_string(context
, "LDB_seq: talloc_named() failed!");
1007 if (priv
->index
< priv
->count
) {
1008 ret
= LDB_message2entry(context
, db
, mem_ctx
,
1009 NULL
, HDB_LDB_ENT_TYPE_ANY
,
1010 priv
->msgs
[priv
->index
++],
1011 priv
->realm_ref_msgs
[0], entry
);
1013 ret
= HDB_ERR_NOENTRY
;
1018 db
->hdb_openp
= NULL
;
1020 talloc_free(mem_ctx
);
1026 static krb5_error_code
LDB_firstkey(krb5_context context
, HDB
*db
, unsigned flags
,
1027 hdb_entry_ex
*entry
)
1029 struct ldb_context
*ldb_ctx
= (struct ldb_context
*)db
->hdb_db
;
1030 struct hdb_ldb_seq
*priv
= (struct hdb_ldb_seq
*)db
->hdb_openp
;
1032 struct ldb_dn
*realm_dn
= NULL
;
1033 struct ldb_result
*res
= NULL
;
1034 struct ldb_message
**realm_ref_msgs
= NULL
;
1035 krb5_error_code ret
;
1036 TALLOC_CTX
*mem_ctx
;
1044 priv
= (struct hdb_ldb_seq
*) talloc(db
, struct hdb_ldb_seq
);
1046 krb5_set_error_string(context
, "talloc: out of memory");
1050 priv
->ctx
= ldb_ctx
;
1053 priv
->realm_ref_msgs
= NULL
;
1056 mem_ctx
= talloc_named(priv
, 0, "LDB_firstkey context");
1059 krb5_set_error_string(context
, "LDB_firstkey: talloc_named() failed!");
1063 ret
= krb5_get_default_realm(context
, &realm
);
1069 ret
= LDB_lookup_realm(context
, (struct ldb_context
*)db
->hdb_db
,
1070 mem_ctx
, realm
, &realm_ref_msgs
);
1076 krb5_warnx(context
, "LDB_firstkey: could not find realm\n");
1077 return HDB_ERR_NOENTRY
;
1080 realm_dn
= samdb_result_dn((struct ldb_context
*)db
->hdb_db
, mem_ctx
, realm_ref_msgs
[0], "nCName", NULL
);
1082 priv
->realm_ref_msgs
= talloc_steal(priv
, realm_ref_msgs
);
1084 lret
= ldb_search(ldb_ctx
, realm_dn
,
1085 LDB_SCOPE_SUBTREE
, "(objectClass=user)",
1088 if (lret
!= LDB_SUCCESS
) {
1090 return HDB_ERR_NOENTRY
;
1093 priv
->count
= res
->count
;
1094 priv
->msgs
= talloc_steal(priv
, res
->msgs
);
1097 db
->hdb_openp
= priv
;
1099 ret
= LDB_seq(context
, db
, flags
, entry
);
1103 db
->hdb_openp
= NULL
;
1105 talloc_free(mem_ctx
);
1110 static krb5_error_code
LDB_nextkey(krb5_context context
, HDB
*db
, unsigned flags
,
1111 hdb_entry_ex
*entry
)
1113 return LDB_seq(context
, db
, flags
, entry
);
1116 static krb5_error_code
LDB_destroy(krb5_context context
, HDB
*db
)
1122 /* This interface is to be called by the KDC, which is expecting Samba
1123 * calling conventions. It is also called by a wrapper
1124 * (hdb_ldb_create) from the kpasswdd -> krb5 -> keytab_hdb -> hdb
1127 NTSTATUS
kdc_hdb_ldb_create(TALLOC_CTX
*mem_ctx
,
1128 krb5_context context
, struct HDB
**db
, const char *arg
)
1131 struct auth_session_info
*session_info
;
1132 *db
= talloc(mem_ctx
, HDB
);
1134 krb5_set_error_string(context
, "malloc: out of memory");
1135 return NT_STATUS_NO_MEMORY
;
1138 (*db
)->hdb_master_key_set
= 0;
1139 (*db
)->hdb_db
= NULL
;
1141 nt_status
= auth_system_session_info(*db
, &session_info
);
1142 if (!NT_STATUS_IS_OK(nt_status
)) {
1146 /* The idea here is very simple. Using Kerberos to
1147 * authenticate the KDC to the LDAP server is higly likely to
1150 * In future we may set this up to use EXERNAL and SSL
1151 * certificates, for now it will almost certainly be NTLMSSP
1154 cli_credentials_set_kerberos_state(session_info
->credentials
,
1155 CRED_DONT_USE_KERBEROS
);
1157 /* Setup the link to LDB */
1158 (*db
)->hdb_db
= samdb_connect(*db
, session_info
);
1159 if ((*db
)->hdb_db
== NULL
) {
1160 DEBUG(1, ("hdb_ldb_create: Cannot open samdb for KDC backend!"));
1161 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1164 (*db
)->hdb_openp
= 0;
1165 (*db
)->hdb_open
= LDB_open
;
1166 (*db
)->hdb_close
= LDB_close
;
1167 (*db
)->hdb_fetch
= LDB_fetch
;
1168 (*db
)->hdb_store
= LDB_store
;
1169 (*db
)->hdb_remove
= LDB_remove
;
1170 (*db
)->hdb_firstkey
= LDB_firstkey
;
1171 (*db
)->hdb_nextkey
= LDB_nextkey
;
1172 (*db
)->hdb_lock
= LDB_lock
;
1173 (*db
)->hdb_unlock
= LDB_unlock
;
1174 (*db
)->hdb_rename
= LDB_rename
;
1175 /* we don't implement these, as we are not a lockable database */
1176 (*db
)->hdb__get
= NULL
;
1177 (*db
)->hdb__put
= NULL
;
1178 /* kadmin should not be used for deletes - use other tools instead */
1179 (*db
)->hdb__del
= NULL
;
1180 (*db
)->hdb_destroy
= LDB_destroy
;
1182 return NT_STATUS_OK
;
1185 krb5_error_code
hdb_ldb_create(krb5_context context
, struct HDB
**db
, const char *arg
)
1188 /* The global kdc_mem_ctx, Disgusting, ugly hack, but it means one less private hook */
1189 nt_status
= kdc_hdb_ldb_create(kdc_mem_ctx
, context
, db
, arg
);
1191 if (NT_STATUS_IS_OK(nt_status
)) {