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 "dsdb/samdb/samdb.h"
47 { HDB_LDB_ENT_TYPE_CLIENT, HDB_LDB_ENT_TYPE_SERVER,
48 HDB_LDB_ENT_TYPE_KRBTGT, HDB_LDB_ENT_TYPE_ANY };
50 static const char * const krb5_attrs[] = {
55 "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 const char *unicodePwd;
226 unsigned int userAccountControl;
228 krb5_error_code ret = 0;
229 krb5_boolean is_computer = FALSE;
230 const char *dnsdomain = ldb_msg_find_string(realm_ref_msg, "dnsRoot", NULL);
231 char *realm = strupper_talloc(mem_ctx, dnsdomain);
232 struct ldb_dn *domain_dn = samdb_result_dn(mem_ctx, realm_ref_msg, "nCName", ldb_dn_new(mem_ctx));
234 struct hdb_ldb_private *private;
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 */
299 free(*krb5_princ_realm(context, entry_ex->entry.principal));
301 /* this has to be with malloc() */
302 strdup_realm = strdup(realm);
305 krb5_clear_error_string(context);
308 krb5_princ_set_realm(context, entry_ex->entry.principal, &strdup_realm);
311 entry_ex->entry.kvno = ldb_msg_find_int(msg, "msDS-KeyVersionNumber", 0);
313 entry_ex->entry.flags = uf2HDBFlags(context, userAccountControl, ent_type);
315 if (ent_type == HDB_LDB_ENT_TYPE_KRBTGT) {
316 entry_ex->entry.flags.invalid = 0;
317 entry_ex->entry.flags.server = 1;
318 entry_ex->entry.flags.forwardable = 1;
319 entry_ex->entry.flags.ok_as_delegate = 1;
322 if (lp_parm_bool(-1, "kdc", "require spn for service", True)) {
323 if (!is_computer && !ldb_msg_find_string(msg, "servicePrincipalName", NULL)) {
324 entry_ex->entry.flags.server = 0;
328 /* use 'whenCreated' */
329 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
331 entry_ex->entry.created_by.principal = NULL;
333 entry_ex->entry.modified_by = (Event *) malloc(sizeof(Event));
334 if (entry_ex->entry.modified_by == NULL) {
335 krb5_set_error_string(context, "malloc: out of memory");
340 /* use 'whenChanged' */
341 entry_ex->entry.modified_by->time = ldb_msg_find_krb5time_ldap_time(msg, "whenChanged", 0);
343 entry_ex->entry.modified_by->principal = NULL;
345 entry_ex->entry.valid_start = NULL;
347 acct_expiry = samdb_result_nttime(msg, "accountExpires", (NTTIME)-1);
348 if ((acct_expiry == (NTTIME)-1) ||
349 (acct_expiry == 0x7FFFFFFFFFFFFFFFULL)) {
350 entry_ex->entry.valid_end = NULL;
352 entry_ex->entry.valid_end = malloc(sizeof(*entry_ex->entry.valid_end));
353 if (entry_ex->entry.valid_end == NULL) {
357 *entry_ex->entry.valid_end = nt_time_to_unix(acct_expiry);
360 if ((ent_type != HDB_LDB_ENT_TYPE_KRBTGT) && (!(userAccountControl & UF_DONT_EXPIRE_PASSWD))) {
361 NTTIME must_change_time
362 = samdb_result_force_password_change((struct ldb_context *)db->hdb_db, mem_ctx,
365 if (must_change_time != 0) {
366 entry_ex->entry.pw_end = malloc(sizeof(*entry_ex->entry.pw_end));
367 if (entry_ex->entry.pw_end == NULL) {
371 *entry_ex->entry.pw_end = nt_time_to_unix(must_change_time);
373 entry_ex->entry.pw_end = NULL;
376 entry_ex->entry.pw_end = NULL;
379 entry_ex->entry.max_life = NULL;
381 entry_ex->entry.max_renew = NULL;
383 entry_ex->entry.generation = NULL;
385 /* create the keys and enctypes */
386 unicodePwd = ldb_msg_find_string(msg, "unicodePwd", NULL);
388 /* Many, many thanks to lukeh@padl.com for this
389 * algorithm, described in his Nov 10 2004 mail to
390 * samba-technical@samba.org */
392 Principal *salt_principal;
393 const char *user_principal_name = ldb_msg_find_string(msg, "userPrincipalName", NULL);
395 /* Determine a salting principal */
396 char *samAccountName = talloc_strdup(mem_ctx, ldb_msg_find_string(msg, "samAccountName", NULL));
398 if (!samAccountName) {
399 krb5_set_error_string(context, "LDB_message2entry: no samAccountName present");
403 if (samAccountName[strlen(samAccountName)-1] == '$') {
404 samAccountName[strlen(samAccountName)-1] = '\0';
406 saltbody = talloc_asprintf(mem_ctx, "%s.%s", samAccountName, dnsdomain);
408 ret = krb5_make_principal(context, &salt_principal, realm, "host", saltbody, NULL);
409 } else if (user_principal_name) {
411 user_principal_name = talloc_strdup(mem_ctx, user_principal_name);
412 if (!user_principal_name) {
416 p = strchr(user_principal_name, '@');
420 ret = krb5_make_principal(context, &salt_principal, realm, user_principal_name, NULL);
423 const char *samAccountName = ldb_msg_find_string(msg, "samAccountName", NULL);
424 ret = krb5_make_principal(context, &salt_principal, realm, samAccountName, NULL);
428 size_t num_keys = entry_ex->entry.keys.len;
430 * create keys from unicodePwd
432 ret = hdb_generate_key_set_password(context, salt_principal,
434 &entry_ex->entry.keys.val, &num_keys);
435 entry_ex->entry.keys.len = num_keys;
436 krb5_free_principal(context, salt_principal);
440 krb5_warnx(context, "could not generate keys from unicodePwd\n");
441 entry_ex->entry.keys.val = NULL;
442 entry_ex->entry.keys.len = 0;
446 const struct ldb_val *val;
449 val = ldb_msg_find_ldb_val(msg, "ntPwdHash");
451 krb5_warnx(context, "neither type of key available for this account\n");
452 entry_ex->entry.keys.val = NULL;
453 entry_ex->entry.keys.len = 0;
454 } else if (val->length < 16) {
455 entry_ex->entry.keys.val = NULL;
456 entry_ex->entry.keys.len = 0;
457 krb5_warnx(context, "ntPwdHash has invalid length: %d\n",
460 ret = krb5_data_alloc (&keyvalue, 16);
462 krb5_clear_error_string(context);
467 memcpy(keyvalue.data, val->data, 16);
469 entry_ex->entry.keys.val = malloc(sizeof(entry_ex->entry.keys.val[0]));
470 if (entry_ex->entry.keys.val == NULL) {
471 krb5_data_free(&keyvalue);
472 krb5_clear_error_string(context);
477 memset(&entry_ex->entry.keys.val[0], 0, sizeof(Key));
478 entry_ex->entry.keys.val[0].key.keytype = ETYPE_ARCFOUR_HMAC_MD5;
479 entry_ex->entry.keys.val[0].key.keyvalue = keyvalue;
481 entry_ex->entry.keys.len = 1;
486 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
487 if (entry_ex->entry.etypes == NULL) {
488 krb5_clear_error_string(context);
492 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
493 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
494 if (entry_ex->entry.etypes->val == NULL) {
495 krb5_clear_error_string(context);
499 for (i=0; i < entry_ex->entry.etypes->len; i++) {
500 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
504 private->msg = talloc_steal(private, msg);
505 private->realm_ref_msg = talloc_steal(private, realm_ref_msg);
506 private->samdb = (struct ldb_context *)db->hdb_db;
508 entry_ex->check_client_access = hdb_ldb_check_client_access;
509 entry_ex->authz_data_tgs_req = hdb_ldb_authz_data_tgs_req;
510 entry_ex->authz_data_as_req = hdb_ldb_authz_data_as_req;
514 /* This doesn't free ent itself, that is for the eventual caller to do */
515 hdb_free_entry(context, entry_ex);
517 talloc_steal(db, entry_ex->ctx);
523 static krb5_error_code LDB_lookup_principal(krb5_context context, struct ldb_context *ldb_ctx,
525 krb5_const_principal principal,
526 enum hdb_ldb_ent_type ent_type,
527 const struct ldb_dn *realm_dn,
528 struct ldb_message ***pmsg)
533 const char * const *princ_attrs = krb5_attrs;
536 char *short_princ_talloc;
540 struct ldb_result *res = NULL;
542 ret = krb5_unparse_name_norealm(context, principal, &short_princ);
545 krb5_set_error_string(context, "LDB_lookup_principal: could not parse principal");
546 krb5_warnx(context, "LDB_lookup_principal: could not parse principal");
550 short_princ_talloc = talloc_strdup(mem_ctx, short_princ);
552 if (!short_princ || !short_princ_talloc) {
553 krb5_set_error_string(context, "LDB_lookup_principal: talloc_strdup() failed!");
558 case HDB_LDB_ENT_TYPE_CLIENT:
561 case HDB_LDB_ENT_TYPE_ANY:
564 case HDB_LDB_ENT_TYPE_KRBTGT:
565 filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
568 case HDB_LDB_ENT_TYPE_SERVER:
569 filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
575 krb5_set_error_string(context, "talloc_asprintf: out of memory");
579 lret = ldb_search(ldb_ctx, realm_dn, LDB_SCOPE_SUBTREE, filter, princ_attrs, &res);
581 realm_dn_str = ldb_dn_linearize(mem_ctx, realm_dn);
583 if (lret != LDB_SUCCESS || res->count == 0) {
584 krb5_warnx(context, "ldb_search: basedn: '%s' filter: '%s' failed: %s",
585 realm_dn_str, filter, ldb_errstring(ldb_ctx));
586 krb5_set_error_string(context, "ldb_search: basedn: '%s' filter: '%s' failed: %s",
587 realm_dn_str, filter, ldb_errstring(ldb_ctx));
588 return HDB_ERR_NOENTRY;
589 } else if (res->count > 1) {
590 krb5_warnx(context, "ldb_search: basedn: '%s' filter: '%s' more than 1 entry: %d",
591 realm_dn_str, filter, res->count);
592 krb5_set_error_string(context, "ldb_search: basedn: '%s' filter: '%s' more than 1 entry: %d",
593 realm_dn_str, filter, res->count);
595 return HDB_ERR_NOENTRY;
597 talloc_steal(mem_ctx, res->msgs);
603 static krb5_error_code LDB_lookup_realm(krb5_context context, struct ldb_context *ldb_ctx,
606 struct ldb_message ***pmsg)
609 char *cross_ref_filter;
610 struct ldb_result *cross_ref_res;
612 cross_ref_filter = talloc_asprintf(mem_ctx,
613 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
615 if (!cross_ref_filter) {
616 krb5_set_error_string(context, "asprintf: out of memory");
620 ret = ldb_search(ldb_ctx, NULL, LDB_SCOPE_SUBTREE, cross_ref_filter, realm_ref_attrs, &cross_ref_res);
622 if (ret != LDB_SUCCESS || cross_ref_res->count == 0) {
623 krb5_warnx(context, "ldb_search: filter: '%s' failed: %s", cross_ref_filter, ldb_errstring(ldb_ctx));
624 krb5_set_error_string(context, "ldb_search: filter: '%s' failed: %s", cross_ref_filter, ldb_errstring(ldb_ctx));
626 talloc_free(cross_ref_res);
627 return HDB_ERR_NOENTRY;
628 } else if (cross_ref_res->count > 1) {
629 krb5_warnx(context, "ldb_search: filter: '%s' more than 1 entry: %d", cross_ref_filter, cross_ref_res->count);
630 krb5_set_error_string(context, "ldb_search: filter: '%s' more than 1 entry: %d", cross_ref_filter, cross_ref_res->count);
632 talloc_free(cross_ref_res);
633 return HDB_ERR_NOENTRY;
637 *pmsg = talloc_steal(mem_ctx, cross_ref_res->msgs);
639 talloc_free(cross_ref_res);
646 static krb5_error_code LDB_open(krb5_context context, HDB *db, int flags, mode_t mode)
648 if (db->hdb_master_key_set) {
649 krb5_warnx(context, "LDB_open: use of a master key incompatible with LDB\n");
650 krb5_set_error_string(context, "LDB_open: use of a master key incompatible with LDB\n");
651 return HDB_ERR_NOENTRY;
657 static krb5_error_code LDB_close(krb5_context context, HDB *db)
662 static krb5_error_code LDB_lock(krb5_context context, HDB *db, int operation)
667 static krb5_error_code LDB_unlock(krb5_context context, HDB *db)
672 static krb5_error_code LDB_rename(krb5_context context, HDB *db, const char *new_name)
674 return HDB_ERR_DB_INUSE;
677 static krb5_error_code LDB_fetch(krb5_context context, HDB *db, unsigned flags,
678 krb5_const_principal principal,
679 enum hdb_ent_type ent_type,
680 hdb_entry_ex *entry_ex)
682 struct ldb_message **msg = NULL;
683 struct ldb_message **realm_ref_msg = NULL;
684 struct ldb_message **realm_fixed_msg = NULL;
685 enum hdb_ldb_ent_type ldb_ent_type;
689 const struct ldb_dn *realm_dn;
690 TALLOC_CTX *mem_ctx = talloc_named(db, 0, "LDB_fetch context");
693 krb5_set_error_string(context, "LDB_fetch: talloc_named() failed!");
698 case HDB_ENT_TYPE_CLIENT:
701 char *principal_string;
702 ldb_ent_type = HDB_LDB_ENT_TYPE_CLIENT;
704 ret = krb5_unparse_name(context, principal, &principal_string);
707 talloc_free(mem_ctx);
711 nt_status = sam_get_results_principal((struct ldb_context *)db->hdb_db,
712 mem_ctx, principal_string,
713 &msg, &realm_ref_msg);
714 free(principal_string);
715 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
716 talloc_free(mem_ctx);
717 return HDB_ERR_NOENTRY;
718 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
719 talloc_free(mem_ctx);
721 } else if (!NT_STATUS_IS_OK(nt_status)) {
722 talloc_free(mem_ctx);
726 ret = LDB_message2entry(context, db, mem_ctx,
727 principal, ldb_ent_type,
728 msg[0], realm_ref_msg[0], entry_ex);
730 talloc_free(mem_ctx);
733 case HDB_ENT_TYPE_SERVER:
734 if (principal->name.name_string.len == 2
735 && (strcmp(principal->name.name_string.val[0], KRB5_TGS_NAME) == 0)) {
736 /* krbtgt case. Either us or a trusted realm */
737 if ((LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
738 mem_ctx, principal->name.name_string.val[1], &realm_fixed_msg) == 0)) {
740 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
741 * is in our db, then direct the caller at our primary
744 const char *dnsdomain = ldb_msg_find_string(realm_fixed_msg[0], "dnsRoot", NULL);
745 char *realm_fixed = strupper_talloc(mem_ctx, dnsdomain);
747 krb5_set_error_string(context, "strupper_talloc: out of memory");
748 talloc_free(mem_ctx);
752 free(principal->name.name_string.val[1]);
753 principal->name.name_string.val[1] = strdup(realm_fixed);
754 talloc_free(realm_fixed);
755 if (!principal->name.name_string.val[1]) {
756 krb5_set_error_string(context, "LDB_fetch: strdup() failed!");
757 talloc_free(mem_ctx);
760 ldb_ent_type = HDB_LDB_ENT_TYPE_KRBTGT;
763 /* we should lookup trusted domains */
764 talloc_free(mem_ctx);
765 return HDB_ERR_NOENTRY;
768 } else if (principal->name.name_string.len >= 2) {
769 /* 'normal server' case */
772 struct ldb_dn *user_dn, *domain_dn;
773 char *principal_string;
774 ldb_ent_type = HDB_LDB_ENT_TYPE_SERVER;
776 ret = krb5_unparse_name_norealm(context, principal, &principal_string);
779 talloc_free(mem_ctx);
783 /* At this point we may find the host is known to be
784 * in a different realm, so we should generate a
785 * referral instead */
786 nt_status = crack_service_principal_name((struct ldb_context *)db->hdb_db,
787 mem_ctx, principal_string,
788 &user_dn, &domain_dn);
789 free(principal_string);
791 if (!NT_STATUS_IS_OK(nt_status)) {
792 talloc_free(mem_ctx);
793 return HDB_ERR_NOENTRY;
796 ldb_ret = gendb_search_dn((struct ldb_context *)db->hdb_db,
797 mem_ctx, user_dn, &msg, krb5_attrs);
800 talloc_free(mem_ctx);
801 return HDB_ERR_NOENTRY;
804 ldb_ret = gendb_search((struct ldb_context *)db->hdb_db,
805 mem_ctx, NULL, &realm_ref_msg, realm_ref_attrs,
806 "ncName=%s", ldb_dn_linearize(mem_ctx, domain_dn));
809 talloc_free(mem_ctx);
810 return HDB_ERR_NOENTRY;
813 ret = LDB_message2entry(context, db, mem_ctx,
814 principal, ldb_ent_type,
815 msg[0], realm_ref_msg[0], entry_ex);
816 talloc_free(mem_ctx);
820 ldb_ent_type = HDB_LDB_ENT_TYPE_SERVER;
821 /* server as client principal case, but we must not lookup userPrincipalNames */
824 case HDB_ENT_TYPE_ANY:
825 krb5_warnx(context, "LDB_fetch: ENT_TYPE_ANY is not valid in hdb-ldb!");
826 talloc_free(mem_ctx);
827 return HDB_ERR_NOENTRY;
829 krb5_warnx(context, "LDB_fetch: invalid ent_type specified!");
830 talloc_free(mem_ctx);
831 return HDB_ERR_NOENTRY;
835 realm = krb5_principal_get_realm(context, principal);
837 ret = LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
838 mem_ctx, realm, &realm_ref_msg);
840 krb5_warnx(context, "LDB_fetch: could not find realm");
841 talloc_free(mem_ctx);
842 return HDB_ERR_NOENTRY;
845 realm_dn = samdb_result_dn(mem_ctx, realm_ref_msg[0], "nCName", NULL);
847 ret = LDB_lookup_principal(context, (struct ldb_context *)db->hdb_db,
849 principal, ldb_ent_type, realm_dn, &msg);
852 krb5_warnx(context, "LDB_fetch: could not find principal in DB");
853 krb5_set_error_string(context, "LDB_fetch: could not find principal in DB");
854 talloc_free(mem_ctx);
857 ret = LDB_message2entry(context, db, mem_ctx,
858 principal, ldb_ent_type,
859 msg[0], realm_ref_msg[0], entry_ex);
861 krb5_warnx(context, "LDB_fetch: message2entry failed\n");
865 talloc_free(mem_ctx);
869 static krb5_error_code LDB_store(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
871 return HDB_ERR_DB_INUSE;
874 static krb5_error_code LDB_remove(krb5_context context, HDB *db, hdb_entry_ex *entry)
876 return HDB_ERR_DB_INUSE;
880 struct ldb_context *ctx;
883 struct ldb_message **msgs;
884 struct ldb_message **realm_ref_msgs;
887 static krb5_error_code LDB_seq(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
890 struct hdb_ldb_seq *priv = (struct hdb_ldb_seq *)db->hdb_openp;
892 hdb_entry_ex entry_ex;
893 memset(&entry_ex, '\0', sizeof(entry_ex));
896 return HDB_ERR_NOENTRY;
899 mem_ctx = talloc_named(priv, 0, "LDB_seq context");
902 krb5_set_error_string(context, "LDB_seq: talloc_named() failed!");
906 if (priv->index < priv->count) {
907 ret = LDB_message2entry(context, db, mem_ctx,
908 NULL, HDB_LDB_ENT_TYPE_ANY,
909 priv->msgs[priv->index++],
910 priv->realm_ref_msgs[0], entry);
912 ret = HDB_ERR_NOENTRY;
917 db->hdb_openp = NULL;
919 talloc_free(mem_ctx);
925 static krb5_error_code LDB_firstkey(krb5_context context, HDB *db, unsigned flags,
928 struct ldb_context *ldb_ctx = (struct ldb_context *)db->hdb_db;
929 struct hdb_ldb_seq *priv = (struct hdb_ldb_seq *)db->hdb_openp;
931 struct ldb_dn *realm_dn = NULL;
932 struct ldb_result *res = NULL;
933 struct ldb_message **realm_ref_msgs = NULL;
943 priv = (struct hdb_ldb_seq *) talloc(db, struct hdb_ldb_seq);
945 krb5_set_error_string(context, "talloc: out of memory");
952 priv->realm_ref_msgs = NULL;
955 mem_ctx = talloc_named(priv, 0, "LDB_firstkey context");
958 krb5_set_error_string(context, "LDB_firstkey: talloc_named() failed!");
962 ret = krb5_get_default_realm(context, &realm);
968 ret = LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
969 mem_ctx, realm, &realm_ref_msgs);
975 krb5_warnx(context, "LDB_firstkey: could not find realm\n");
976 return HDB_ERR_NOENTRY;
979 realm_dn = samdb_result_dn(mem_ctx, realm_ref_msgs[0], "nCName", NULL);
981 priv->realm_ref_msgs = talloc_steal(priv, realm_ref_msgs);
983 krb5_warnx(context, "LDB_firstkey: realm ok\n");
985 lret = ldb_search(ldb_ctx, realm_dn,
986 LDB_SCOPE_SUBTREE, "(objectClass=user)",
989 if (lret != LDB_SUCCESS) {
991 return HDB_ERR_NOENTRY;
994 priv->count = res->count;
995 priv->msgs = talloc_steal(priv, res->msgs);
997 db->hdb_openp = priv;
999 ret = LDB_seq(context, db, flags, entry);
1003 db->hdb_openp = NULL;
1005 talloc_free(mem_ctx);
1010 static krb5_error_code LDB_nextkey(krb5_context context, HDB *db, unsigned flags,
1011 hdb_entry_ex *entry)
1013 return LDB_seq(context, db, flags, entry);
1016 static krb5_error_code LDB_destroy(krb5_context context, HDB *db)
1022 NTSTATUS hdb_ldb_create(TALLOC_CTX *mem_ctx,
1023 krb5_context context, struct HDB **db, const char *arg)
1026 struct auth_session_info *session_info;
1027 *db = talloc(mem_ctx, HDB);
1029 krb5_set_error_string(context, "malloc: out of memory");
1030 return NT_STATUS_NO_MEMORY;
1033 (*db)->hdb_master_key_set = 0;
1034 (*db)->hdb_db = NULL;
1036 nt_status = auth_system_session_info(*db, &session_info);
1037 if (!NT_STATUS_IS_OK(nt_status)) {
1041 /* The idea here is very simple. Using Kerberos to
1042 * authenticate the KDC to the LDAP server is higly likely to
1045 * In future we may set this up to use EXERNAL and SSL
1046 * certificates, for now it will almost certainly be NTLMSSP
1049 nt_status = cli_credentials_gensec_remove_oid(session_info->credentials,
1050 GENSEC_OID_KERBEROS5);
1051 if (!NT_STATUS_IS_OK(nt_status)) {
1055 /* Setup the link to LDB */
1056 (*db)->hdb_db = samdb_connect(*db, session_info);
1057 if ((*db)->hdb_db == NULL) {
1058 DEBUG(1, ("hdb_ldb_create: Cannot open samdb for KDC backend!"));
1059 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1062 (*db)->hdb_openp = 0;
1063 (*db)->hdb_open = LDB_open;
1064 (*db)->hdb_close = LDB_close;
1065 (*db)->hdb_fetch = LDB_fetch;
1066 (*db)->hdb_store = LDB_store;
1067 (*db)->hdb_remove = LDB_remove;
1068 (*db)->hdb_firstkey = LDB_firstkey;
1069 (*db)->hdb_nextkey = LDB_nextkey;
1070 (*db)->hdb_lock = LDB_lock;
1071 (*db)->hdb_unlock = LDB_unlock;
1072 (*db)->hdb_rename = LDB_rename;
1073 /* we don't implement these, as we are not a lockable database */
1074 (*db)->hdb__get = NULL;
1075 (*db)->hdb__put = NULL;
1076 /* kadmin should not be used for deletes - use other tools instead */
1077 (*db)->hdb__del = NULL;
1078 (*db)->hdb_destroy = LDB_destroy;
1080 return NT_STATUS_OK;