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
39 #include "lib/ldb/include/ldb.h"
40 #include "lib/ldb/include/ldb_errors.h"
41 #include "system/iconv.h"
42 #include "librpc/gen_ndr/netlogon.h"
43 #include "auth/auth.h"
44 #include "auth/auth_sam.h"
46 #include "dsdb/samdb/samdb.h"
49 { HDB_LDB_ENT_TYPE_CLIENT, HDB_LDB_ENT_TYPE_SERVER,
50 HDB_LDB_ENT_TYPE_KRBTGT, HDB_LDB_ENT_TYPE_ANY };
52 static const char * const krb5_attrs[] = {
57 "servicePrincipalName",
69 "msDS-KeyVersionNumber",
73 static const char *realm_ref_attrs[] = {
79 static KerberosTime ldb_msg_find_krb5time_ldap_time(struct ldb_message *msg, const char *attr, KerberosTime default_val)
85 gentime = ldb_msg_find_string(msg, attr, NULL);
89 tmp = strptime(gentime, "%Y%m%d%H%M%SZ", &tm);
97 static HDBFlags uf2HDBFlags(krb5_context context, int userAccountControl, enum hdb_ldb_ent_type ent_type)
99 HDBFlags flags = int2HDBFlags(0);
101 krb5_warnx(context, "uf2HDBFlags: userAccountControl: %08x\n", userAccountControl);
103 /* we don't allow kadmin deletes */
106 /* mark the principal as invalid to start with */
111 /* All accounts are servers, but this may be disabled again in the caller */
114 /* Account types - clear the invalid bit if it turns out to be valid */
115 if (userAccountControl & UF_NORMAL_ACCOUNT) {
116 if (ent_type == HDB_LDB_ENT_TYPE_CLIENT || ent_type == HDB_LDB_ENT_TYPE_ANY) {
122 if (userAccountControl & UF_INTERDOMAIN_TRUST_ACCOUNT) {
123 if (ent_type == HDB_LDB_ENT_TYPE_CLIENT || ent_type == HDB_LDB_ENT_TYPE_ANY) {
128 if (userAccountControl & UF_WORKSTATION_TRUST_ACCOUNT) {
129 if (ent_type == HDB_LDB_ENT_TYPE_CLIENT || ent_type == HDB_LDB_ENT_TYPE_ANY) {
134 if (userAccountControl & UF_SERVER_TRUST_ACCOUNT) {
135 if (ent_type == HDB_LDB_ENT_TYPE_CLIENT || ent_type == HDB_LDB_ENT_TYPE_ANY) {
141 /* Not permitted to act as a client if disabled */
142 if (userAccountControl & UF_ACCOUNTDISABLE) {
145 if (userAccountControl & UF_LOCKOUT) {
149 if (userAccountControl & UF_PASSWORD_NOTREQD) {
154 if (userAccountControl & UF_PASSWORD_CANT_CHANGE) {
159 if (userAccountControl & UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED) {
163 if (userAccountControl & UF_TEMP_DUPLICATE_ACCOUNT) {
167 /* UF_DONT_EXPIRE_PASSWD handled in LDB_message2entry() */
170 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
174 if (userAccountControl & UF_SMARTCARD_REQUIRED) {
175 flags.require_hwauth = 1;
177 if (userAccountControl & UF_TRUSTED_FOR_DELEGATION) {
178 flags.ok_as_delegate = 1;
180 if (!(userAccountControl & UF_NOT_DELEGATED)) {
181 flags.forwardable = 1;
186 if (userAccountControl & UF_SMARTCARD_USE_DES_KEY_ONLY) {
190 if (userAccountControl & UF_DONT_REQUIRE_PREAUTH) {
191 flags.require_preauth = 0;
193 flags.require_preauth = 1;
197 krb5_warnx(context, "uf2HDBFlags: HDBFlags: %08x\n", HDBFlags2int(flags));
202 static int hdb_ldb_destrutor(void *ptr)
204 struct hdb_ldb_private *private = ptr;
205 hdb_entry_ex *entry_ex = private->entry_ex;
206 free_hdb_entry(&entry_ex->entry);
210 static void hdb_ldb_free_entry(krb5_context context, hdb_entry_ex *entry_ex)
212 talloc_free(entry_ex->ctx);
216 * Construct an hdb_entry from a directory entry.
218 static krb5_error_code LDB_message2entry(krb5_context context, HDB *db,
219 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
220 enum hdb_ldb_ent_type ent_type,
221 struct ldb_message *msg,
222 struct ldb_message *realm_ref_msg,
223 hdb_entry_ex *entry_ex)
225 unsigned int userAccountControl;
227 krb5_error_code ret = 0;
228 krb5_boolean is_computer = FALSE;
229 const char *dnsdomain = ldb_msg_find_string(realm_ref_msg, "dnsRoot", NULL);
230 char *realm = strupper_talloc(mem_ctx, dnsdomain);
231 struct ldb_dn *domain_dn = samdb_result_dn(mem_ctx, realm_ref_msg, "nCName", ldb_dn_new(mem_ctx));
233 struct hdb_ldb_private *private;
235 struct ldb_message_element *ldb_keys;
237 struct ldb_message_element *objectclasses;
238 struct ldb_val computer_val;
239 computer_val.data = discard_const_p(uint8_t,"computer");
240 computer_val.length = strlen((const char *)computer_val.data);
242 objectclasses = ldb_msg_find_element(msg, "objectClass");
244 if (objectclasses && ldb_msg_find_val(objectclasses, &computer_val)) {
248 memset(entry_ex, 0, sizeof(*entry_ex));
250 krb5_warnx(context, "LDB_message2entry:\n");
253 krb5_set_error_string(context, "talloc_strdup: out of memory");
258 private = talloc(mem_ctx, struct hdb_ldb_private);
264 private->entry_ex = entry_ex;
266 talloc_set_destructor(private, hdb_ldb_destrutor);
268 entry_ex->ctx = private;
269 entry_ex->free_entry = hdb_ldb_free_entry;
271 userAccountControl = ldb_msg_find_uint(msg, "userAccountControl", 0);
274 entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
275 if (ent_type == HDB_LDB_ENT_TYPE_ANY && principal == NULL) {
276 const char *samAccountName = ldb_msg_find_string(msg, "samAccountName", NULL);
277 if (!samAccountName) {
278 krb5_set_error_string(context, "LDB_message2entry: no samAccountName present");
282 samAccountName = ldb_msg_find_string(msg, "samAccountName", NULL);
283 krb5_make_principal(context, &entry_ex->entry.principal, realm, samAccountName, NULL);
286 ret = copy_Principal(principal, entry_ex->entry.principal);
288 krb5_clear_error_string(context);
292 /* While we have copied the client principal, tests
293 * show that Win2k3 returns the 'corrected' realm, not
294 * the client-specified realm. This code attempts to
295 * replace the client principal's realm with the one
296 * we determine from our records */
298 /* this has to be with malloc() */
299 strdup_realm = strdup(realm);
302 krb5_clear_error_string(context);
305 free(*krb5_princ_realm(context, entry_ex->entry.principal));
306 krb5_princ_set_realm(context, entry_ex->entry.principal, &strdup_realm);
309 entry_ex->entry.kvno = ldb_msg_find_int(msg, "msDS-KeyVersionNumber", 0);
311 entry_ex->entry.flags = uf2HDBFlags(context, userAccountControl, ent_type);
313 if (ent_type == HDB_LDB_ENT_TYPE_KRBTGT) {
314 entry_ex->entry.flags.invalid = 0;
315 entry_ex->entry.flags.server = 1;
316 entry_ex->entry.flags.forwardable = 1;
317 entry_ex->entry.flags.ok_as_delegate = 1;
320 if (lp_parm_bool(-1, "kdc", "require spn for service", True)) {
321 if (!is_computer && !ldb_msg_find_string(msg, "servicePrincipalName", NULL)) {
322 entry_ex->entry.flags.server = 0;
326 /* use 'whenCreated' */
327 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
329 entry_ex->entry.created_by.principal = NULL;
331 entry_ex->entry.modified_by = (Event *) malloc(sizeof(Event));
332 if (entry_ex->entry.modified_by == NULL) {
333 krb5_set_error_string(context, "malloc: out of memory");
338 /* use 'whenChanged' */
339 entry_ex->entry.modified_by->time = ldb_msg_find_krb5time_ldap_time(msg, "whenChanged", 0);
341 entry_ex->entry.modified_by->principal = NULL;
343 entry_ex->entry.valid_start = NULL;
345 acct_expiry = samdb_result_nttime(msg, "accountExpires", (NTTIME)-1);
346 if ((acct_expiry == (NTTIME)-1) ||
347 (acct_expiry == 0x7FFFFFFFFFFFFFFFULL)) {
348 entry_ex->entry.valid_end = NULL;
350 entry_ex->entry.valid_end = malloc(sizeof(*entry_ex->entry.valid_end));
351 if (entry_ex->entry.valid_end == NULL) {
355 *entry_ex->entry.valid_end = nt_time_to_unix(acct_expiry);
358 if (ent_type != HDB_LDB_ENT_TYPE_KRBTGT) {
359 NTTIME must_change_time
360 = samdb_result_force_password_change((struct ldb_context *)db->hdb_db, mem_ctx,
362 if (must_change_time == 0x7FFFFFFFFFFFFFFFULL) {
363 entry_ex->entry.pw_end = NULL;
365 entry_ex->entry.pw_end = malloc(sizeof(*entry_ex->entry.pw_end));
366 if (entry_ex->entry.pw_end == NULL) {
370 *entry_ex->entry.pw_end = nt_time_to_unix(must_change_time);
373 entry_ex->entry.pw_end = NULL;
376 entry_ex->entry.max_life = NULL;
378 entry_ex->entry.max_renew = NULL;
380 entry_ex->entry.generation = NULL;
382 /* Get krb5Key from the db */
384 ldb_keys = ldb_msg_find_element(msg, "krb5Key");
387 /* oh, no password. Apparently (comment in
388 * hdb-ldap.c) this violates the ASN.1, but this
389 * allows an entry with no keys (yet). */
390 entry_ex->entry.keys.val = NULL;
391 entry_ex->entry.keys.len = 0;
393 /* allocate space to decode into */
394 entry_ex->entry.keys.val = calloc(ldb_keys->num_values, sizeof(Key));
395 if (entry_ex->entry.keys.val == NULL) {
399 entry_ex->entry.keys.len = ldb_keys->num_values;
401 /* Decode Kerberos keys into the hdb structure */
402 for (i=0; i < entry_ex->entry.keys.len; i++) {
404 ret = decode_Key(ldb_keys->values[i].data, ldb_keys->values[i].length,
405 &entry_ex->entry.keys.val[i], &decode_len);
407 /* Could be bougus data in the entry, or out of memory */
413 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
414 if (entry_ex->entry.etypes == NULL) {
415 krb5_clear_error_string(context);
419 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
420 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
421 if (entry_ex->entry.etypes->val == NULL) {
422 krb5_clear_error_string(context);
426 for (i=0; i < entry_ex->entry.etypes->len; i++) {
427 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
431 private->msg = talloc_steal(private, msg);
432 private->realm_ref_msg = talloc_steal(private, realm_ref_msg);
433 private->samdb = (struct ldb_context *)db->hdb_db;
435 entry_ex->check_client_access = hdb_ldb_check_client_access;
436 entry_ex->authz_data_tgs_req = hdb_ldb_authz_data_tgs_req;
437 entry_ex->authz_data_as_req = hdb_ldb_authz_data_as_req;
441 /* This doesn't free ent itself, that is for the eventual caller to do */
442 hdb_free_entry(context, entry_ex);
444 talloc_steal(db, entry_ex->ctx);
450 static krb5_error_code LDB_lookup_principal(krb5_context context, struct ldb_context *ldb_ctx,
452 krb5_const_principal principal,
453 enum hdb_ldb_ent_type ent_type,
454 const struct ldb_dn *realm_dn,
455 struct ldb_message ***pmsg)
460 const char * const *princ_attrs = krb5_attrs;
463 char *short_princ_talloc;
467 struct ldb_result *res = NULL;
469 ret = krb5_unparse_name_norealm(context, principal, &short_princ);
472 krb5_set_error_string(context, "LDB_lookup_principal: could not parse principal");
473 krb5_warnx(context, "LDB_lookup_principal: could not parse principal");
477 short_princ_talloc = talloc_strdup(mem_ctx, short_princ);
479 if (!short_princ_talloc) {
480 krb5_set_error_string(context, "LDB_lookup_principal: talloc_strdup() failed!");
485 case HDB_LDB_ENT_TYPE_CLIENT:
488 case HDB_LDB_ENT_TYPE_ANY:
491 case HDB_LDB_ENT_TYPE_KRBTGT:
492 filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
495 case HDB_LDB_ENT_TYPE_SERVER:
496 filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
502 krb5_set_error_string(context, "talloc_asprintf: out of memory");
506 lret = ldb_search(ldb_ctx, realm_dn, LDB_SCOPE_SUBTREE, filter, princ_attrs, &res);
508 realm_dn_str = ldb_dn_linearize(mem_ctx, realm_dn);
510 if (lret != LDB_SUCCESS) {
511 DEBUG(3, ("Failed to search for %s: %s\n", filter, ldb_errstring(ldb_ctx)));
512 return HDB_ERR_NOENTRY;
513 } else if (res->count == 0 || res->count > 1) {
514 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter, res->count));
515 return HDB_ERR_NOENTRY;
517 talloc_steal(mem_ctx, res->msgs);
523 static krb5_error_code LDB_lookup_realm(krb5_context context, struct ldb_context *ldb_ctx,
526 struct ldb_message ***pmsg)
529 char *cross_ref_filter;
530 struct ldb_result *cross_ref_res;
532 cross_ref_filter = talloc_asprintf(mem_ctx,
533 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
535 if (!cross_ref_filter) {
536 krb5_set_error_string(context, "asprintf: out of memory");
540 ret = ldb_search(ldb_ctx, NULL, LDB_SCOPE_SUBTREE, cross_ref_filter, realm_ref_attrs, &cross_ref_res);
542 if (ret != LDB_SUCCESS) {
543 DEBUG(3, ("Failed to search for %s: %s\n", cross_ref_filter, ldb_errstring(ldb_ctx)));
544 talloc_free(cross_ref_res);
545 return HDB_ERR_NOENTRY;
546 } else if (cross_ref_res->count == 0 || cross_ref_res->count > 1) {
547 DEBUG(3, ("Failed find a single entry for %s: got %d\n", cross_ref_filter, cross_ref_res->count));
548 talloc_free(cross_ref_res);
549 return HDB_ERR_NOENTRY;
553 *pmsg = cross_ref_res->msgs;
554 talloc_steal(mem_ctx, cross_ref_res->msgs);
556 talloc_free(cross_ref_res);
562 static krb5_error_code LDB_open(krb5_context context, HDB *db, int flags, mode_t mode)
564 if (db->hdb_master_key_set) {
565 krb5_warnx(context, "LDB_open: use of a master key incompatible with LDB\n");
566 krb5_set_error_string(context, "LDB_open: use of a master key incompatible with LDB\n");
567 return HDB_ERR_NOENTRY;
573 static krb5_error_code LDB_close(krb5_context context, HDB *db)
578 static krb5_error_code LDB_lock(krb5_context context, HDB *db, int operation)
583 static krb5_error_code LDB_unlock(krb5_context context, HDB *db)
588 static krb5_error_code LDB_rename(krb5_context context, HDB *db, const char *new_name)
590 return HDB_ERR_DB_INUSE;
593 static krb5_error_code LDB_fetch(krb5_context context, HDB *db, unsigned flags,
594 krb5_const_principal principal,
595 enum hdb_ent_type ent_type,
596 hdb_entry_ex *entry_ex)
598 struct ldb_message **msg = NULL;
599 struct ldb_message **realm_ref_msg = NULL;
600 struct ldb_message **realm_fixed_msg = NULL;
601 enum hdb_ldb_ent_type ldb_ent_type;
605 const struct ldb_dn *realm_dn;
606 TALLOC_CTX *mem_ctx = talloc_named(db, 0, "LDB_fetch context");
609 krb5_set_error_string(context, "LDB_fetch: talloc_named() failed!");
614 case HDB_ENT_TYPE_CLIENT:
617 char *principal_string;
618 ldb_ent_type = HDB_LDB_ENT_TYPE_CLIENT;
620 ret = krb5_unparse_name(context, principal, &principal_string);
623 talloc_free(mem_ctx);
627 nt_status = sam_get_results_principal((struct ldb_context *)db->hdb_db,
628 mem_ctx, principal_string,
629 &msg, &realm_ref_msg);
630 free(principal_string);
631 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
632 talloc_free(mem_ctx);
633 return HDB_ERR_NOENTRY;
634 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
635 talloc_free(mem_ctx);
637 } else if (!NT_STATUS_IS_OK(nt_status)) {
638 talloc_free(mem_ctx);
642 ret = LDB_message2entry(context, db, mem_ctx,
643 principal, ldb_ent_type,
644 msg[0], realm_ref_msg[0], entry_ex);
646 talloc_free(mem_ctx);
649 case HDB_ENT_TYPE_SERVER:
650 if (principal->name.name_string.len == 2
651 && (strcmp(principal->name.name_string.val[0], KRB5_TGS_NAME) == 0)) {
652 /* krbtgt case. Either us or a trusted realm */
653 if ((LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
654 mem_ctx, principal->name.name_string.val[1], &realm_fixed_msg) == 0)) {
656 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
657 * is in our db, then direct the caller at our primary
660 const char *dnsdomain = ldb_msg_find_string(realm_fixed_msg[0], "dnsRoot", NULL);
661 char *realm_fixed = strupper_talloc(mem_ctx, dnsdomain);
663 krb5_set_error_string(context, "strupper_talloc: out of memory");
664 talloc_free(mem_ctx);
668 free(principal->name.name_string.val[1]);
669 principal->name.name_string.val[1] = strdup(realm_fixed);
670 talloc_free(realm_fixed);
671 if (!principal->name.name_string.val[1]) {
672 krb5_set_error_string(context, "LDB_fetch: strdup() failed!");
673 talloc_free(mem_ctx);
676 ldb_ent_type = HDB_LDB_ENT_TYPE_KRBTGT;
679 /* we should lookup trusted domains */
680 talloc_free(mem_ctx);
681 return HDB_ERR_NOENTRY;
684 } else if (principal->name.name_string.len >= 2) {
685 /* 'normal server' case */
688 struct ldb_dn *user_dn, *domain_dn;
689 char *principal_string;
690 ldb_ent_type = HDB_LDB_ENT_TYPE_SERVER;
692 ret = krb5_unparse_name_norealm(context, principal, &principal_string);
695 talloc_free(mem_ctx);
699 /* At this point we may find the host is known to be
700 * in a different realm, so we should generate a
701 * referral instead */
702 nt_status = crack_service_principal_name((struct ldb_context *)db->hdb_db,
703 mem_ctx, principal_string,
704 &user_dn, &domain_dn);
705 free(principal_string);
707 if (!NT_STATUS_IS_OK(nt_status)) {
708 talloc_free(mem_ctx);
709 return HDB_ERR_NOENTRY;
712 ldb_ret = gendb_search_dn((struct ldb_context *)db->hdb_db,
713 mem_ctx, user_dn, &msg, krb5_attrs);
716 talloc_free(mem_ctx);
717 return HDB_ERR_NOENTRY;
720 ldb_ret = gendb_search((struct ldb_context *)db->hdb_db,
721 mem_ctx, NULL, &realm_ref_msg, realm_ref_attrs,
722 "ncName=%s", ldb_dn_linearize(mem_ctx, domain_dn));
725 talloc_free(mem_ctx);
726 return HDB_ERR_NOENTRY;
729 ret = LDB_message2entry(context, db, mem_ctx,
730 principal, ldb_ent_type,
731 msg[0], realm_ref_msg[0], entry_ex);
732 talloc_free(mem_ctx);
736 ldb_ent_type = HDB_LDB_ENT_TYPE_SERVER;
737 /* server as client principal case, but we must not lookup userPrincipalNames */
740 case HDB_ENT_TYPE_ANY:
741 krb5_warnx(context, "LDB_fetch: ENT_TYPE_ANY is not valid in hdb-ldb!");
742 talloc_free(mem_ctx);
743 return HDB_ERR_NOENTRY;
745 krb5_warnx(context, "LDB_fetch: invalid ent_type specified!");
746 talloc_free(mem_ctx);
747 return HDB_ERR_NOENTRY;
751 realm = krb5_principal_get_realm(context, principal);
753 ret = LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
754 mem_ctx, realm, &realm_ref_msg);
756 krb5_warnx(context, "LDB_fetch: could not find realm");
757 talloc_free(mem_ctx);
758 return HDB_ERR_NOENTRY;
761 realm_dn = samdb_result_dn(mem_ctx, realm_ref_msg[0], "nCName", NULL);
763 ret = LDB_lookup_principal(context, (struct ldb_context *)db->hdb_db,
765 principal, ldb_ent_type, realm_dn, &msg);
768 krb5_warnx(context, "LDB_fetch: could not find principal in DB");
769 krb5_set_error_string(context, "LDB_fetch: could not find principal in DB");
770 talloc_free(mem_ctx);
773 ret = LDB_message2entry(context, db, mem_ctx,
774 principal, ldb_ent_type,
775 msg[0], realm_ref_msg[0], entry_ex);
777 krb5_warnx(context, "LDB_fetch: message2entry failed\n");
781 talloc_free(mem_ctx);
785 static krb5_error_code LDB_store(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
787 return HDB_ERR_DB_INUSE;
790 static krb5_error_code LDB_remove(krb5_context context, HDB *db, hdb_entry_ex *entry)
792 return HDB_ERR_DB_INUSE;
796 struct ldb_context *ctx;
799 struct ldb_message **msgs;
800 struct ldb_message **realm_ref_msgs;
803 static krb5_error_code LDB_seq(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
806 struct hdb_ldb_seq *priv = (struct hdb_ldb_seq *)db->hdb_openp;
808 hdb_entry_ex entry_ex;
809 memset(&entry_ex, '\0', sizeof(entry_ex));
812 return HDB_ERR_NOENTRY;
815 mem_ctx = talloc_named(priv, 0, "LDB_seq context");
818 krb5_set_error_string(context, "LDB_seq: talloc_named() failed!");
822 if (priv->index < priv->count) {
823 ret = LDB_message2entry(context, db, mem_ctx,
824 NULL, HDB_LDB_ENT_TYPE_ANY,
825 priv->msgs[priv->index++],
826 priv->realm_ref_msgs[0], entry);
828 ret = HDB_ERR_NOENTRY;
833 db->hdb_openp = NULL;
835 talloc_free(mem_ctx);
841 static krb5_error_code LDB_firstkey(krb5_context context, HDB *db, unsigned flags,
844 struct ldb_context *ldb_ctx = (struct ldb_context *)db->hdb_db;
845 struct hdb_ldb_seq *priv = (struct hdb_ldb_seq *)db->hdb_openp;
847 struct ldb_dn *realm_dn = NULL;
848 struct ldb_result *res = NULL;
849 struct ldb_message **realm_ref_msgs = NULL;
859 priv = (struct hdb_ldb_seq *) talloc(db, struct hdb_ldb_seq);
861 krb5_set_error_string(context, "talloc: out of memory");
868 priv->realm_ref_msgs = NULL;
871 mem_ctx = talloc_named(priv, 0, "LDB_firstkey context");
874 krb5_set_error_string(context, "LDB_firstkey: talloc_named() failed!");
878 ret = krb5_get_default_realm(context, &realm);
884 ret = LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
885 mem_ctx, realm, &realm_ref_msgs);
891 krb5_warnx(context, "LDB_firstkey: could not find realm\n");
892 return HDB_ERR_NOENTRY;
895 realm_dn = samdb_result_dn(mem_ctx, realm_ref_msgs[0], "nCName", NULL);
897 priv->realm_ref_msgs = talloc_steal(priv, realm_ref_msgs);
899 krb5_warnx(context, "LDB_firstkey: realm ok\n");
901 lret = ldb_search(ldb_ctx, realm_dn,
902 LDB_SCOPE_SUBTREE, "(objectClass=user)",
905 if (lret != LDB_SUCCESS) {
907 return HDB_ERR_NOENTRY;
910 priv->count = res->count;
911 priv->msgs = talloc_steal(priv, res->msgs);
914 db->hdb_openp = priv;
916 ret = LDB_seq(context, db, flags, entry);
920 db->hdb_openp = NULL;
922 talloc_free(mem_ctx);
927 static krb5_error_code LDB_nextkey(krb5_context context, HDB *db, unsigned flags,
930 return LDB_seq(context, db, flags, entry);
933 static krb5_error_code LDB_destroy(krb5_context context, HDB *db)
939 /* This interface is to be called by the KDC, which is expecting Samba
940 * calling conventions. It is also called by a wrapper
941 * (hdb_ldb_create) from the kpasswdd -> krb5 -> keytab_hdb -> hdb
944 NTSTATUS kdc_hdb_ldb_create(TALLOC_CTX *mem_ctx,
945 krb5_context context, struct HDB **db, const char *arg)
948 struct auth_session_info *session_info;
949 *db = talloc(mem_ctx, HDB);
951 krb5_set_error_string(context, "malloc: out of memory");
952 return NT_STATUS_NO_MEMORY;
955 (*db)->hdb_master_key_set = 0;
956 (*db)->hdb_db = NULL;
958 nt_status = auth_system_session_info(*db, &session_info);
959 if (!NT_STATUS_IS_OK(nt_status)) {
963 /* The idea here is very simple. Using Kerberos to
964 * authenticate the KDC to the LDAP server is higly likely to
967 * In future we may set this up to use EXERNAL and SSL
968 * certificates, for now it will almost certainly be NTLMSSP
971 cli_credentials_set_kerberos_state(session_info->credentials,
972 CRED_DONT_USE_KERBEROS);
974 /* Setup the link to LDB */
975 (*db)->hdb_db = samdb_connect(*db, session_info);
976 if ((*db)->hdb_db == NULL) {
977 DEBUG(1, ("hdb_ldb_create: Cannot open samdb for KDC backend!"));
978 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
981 (*db)->hdb_openp = 0;
982 (*db)->hdb_open = LDB_open;
983 (*db)->hdb_close = LDB_close;
984 (*db)->hdb_fetch = LDB_fetch;
985 (*db)->hdb_store = LDB_store;
986 (*db)->hdb_remove = LDB_remove;
987 (*db)->hdb_firstkey = LDB_firstkey;
988 (*db)->hdb_nextkey = LDB_nextkey;
989 (*db)->hdb_lock = LDB_lock;
990 (*db)->hdb_unlock = LDB_unlock;
991 (*db)->hdb_rename = LDB_rename;
992 /* we don't implement these, as we are not a lockable database */
993 (*db)->hdb__get = NULL;
994 (*db)->hdb__put = NULL;
995 /* kadmin should not be used for deletes - use other tools instead */
996 (*db)->hdb__del = NULL;
997 (*db)->hdb_destroy = LDB_destroy;
1002 krb5_error_code hdb_ldb_create(krb5_context context, struct HDB **db, const char *arg)
1005 /* Disgusting, ugly hack, but it means one less private hook */
1006 nt_status = kdc_hdb_ldb_create(context->mem_ctx, context, db, arg);
1008 if (NT_STATUS_IS_OK(nt_status)) {