2 * Copyright (c) 1999-2001, 2003, PADL Software Pty Ltd.
3 * Copyright (c) 2004, Andrew Bartlett <abartlet@samba.org>.
4 * Copyright (c) 2004, Stefan Metzmacher <metze@samba.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of PADL Software nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "system/time.h"
37 #include "dsdb/common/flags.h"
38 #include "lib/ldb/include/ldb.h"
39 #include "lib/ldb/include/ldb_errors.h"
40 #include "librpc/gen_ndr/netlogon.h"
41 #include "auth/auth.h"
42 #include "auth/credentials/credentials.h"
43 #include "auth/auth_sam.h"
44 #include "util/util_ldb.h"
45 #include "dsdb/samdb/samdb.h"
46 #include "librpc/ndr/libndr.h"
47 #include "librpc/gen_ndr/ndr_drsblobs.h"
48 #include "libcli/auth/libcli_auth.h"
49 #include "param/param.h"
50 #include "events/events.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 *realm_ref_attrs[] = {
63 static KerberosTime ldb_msg_find_krb5time_ldap_time(struct ldb_message *msg, const char *attr, KerberosTime default_val)
69 gentime = ldb_msg_find_attr_as_string(msg, attr, NULL);
73 tmp = strptime(gentime, "%Y%m%d%H%M%SZ", &tm);
81 static HDBFlags uf2HDBFlags(krb5_context context, int userAccountControl, enum hdb_ldb_ent_type ent_type)
83 HDBFlags flags = int2HDBFlags(0);
85 /* we don't allow kadmin deletes */
88 /* mark the principal as invalid to start with */
93 /* All accounts are servers, but this may be disabled again in the caller */
96 /* Account types - clear the invalid bit if it turns out to be valid */
97 if (userAccountControl & UF_NORMAL_ACCOUNT) {
98 if (ent_type == HDB_LDB_ENT_TYPE_CLIENT || ent_type == HDB_LDB_ENT_TYPE_ANY) {
104 if (userAccountControl & UF_INTERDOMAIN_TRUST_ACCOUNT) {
105 if (ent_type == HDB_LDB_ENT_TYPE_CLIENT || ent_type == HDB_LDB_ENT_TYPE_ANY) {
110 if (userAccountControl & UF_WORKSTATION_TRUST_ACCOUNT) {
111 if (ent_type == HDB_LDB_ENT_TYPE_CLIENT || ent_type == HDB_LDB_ENT_TYPE_ANY) {
116 if (userAccountControl & UF_SERVER_TRUST_ACCOUNT) {
117 if (ent_type == HDB_LDB_ENT_TYPE_CLIENT || ent_type == HDB_LDB_ENT_TYPE_ANY) {
123 /* Not permitted to act as a client if disabled */
124 if (userAccountControl & UF_ACCOUNTDISABLE) {
127 if (userAccountControl & UF_LOCKOUT) {
131 if (userAccountControl & UF_PASSWORD_NOTREQD) {
136 UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
138 if (userAccountControl & UF_TEMP_DUPLICATE_ACCOUNT) {
142 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in LDB_message2entry() */
145 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
149 if (userAccountControl & UF_SMARTCARD_REQUIRED) {
150 flags.require_hwauth = 1;
152 if (userAccountControl & UF_TRUSTED_FOR_DELEGATION) {
153 flags.ok_as_delegate = 1;
155 if (!(userAccountControl & UF_NOT_DELEGATED)) {
156 flags.forwardable = 1;
160 if (userAccountControl & UF_DONT_REQUIRE_PREAUTH) {
161 flags.require_preauth = 0;
163 flags.require_preauth = 1;
169 static int hdb_ldb_destrutor(struct hdb_ldb_private *private)
171 hdb_entry_ex *entry_ex = private->entry_ex;
172 free_hdb_entry(&entry_ex->entry);
176 static void hdb_ldb_free_entry(krb5_context context, hdb_entry_ex *entry_ex)
178 talloc_free(entry_ex->ctx);
181 static krb5_error_code LDB_message2entry_keys(krb5_context context,
182 struct smb_iconv_convenience *iconv_convenience,
184 struct ldb_message *msg,
185 unsigned int userAccountControl,
186 hdb_entry_ex *entry_ex)
188 krb5_error_code ret = 0;
189 enum ndr_err_code ndr_err;
190 struct samr_Password *hash;
191 const struct ldb_val *sc_val;
192 struct supplementalCredentialsBlob scb;
193 struct supplementalCredentialsPackage *scp = NULL;
194 struct package_PrimaryKerberosBlob _pkb;
195 struct package_PrimaryKerberosCtr3 *pkb3 = NULL;
197 uint32_t allocated_keys = 0;
199 entry_ex->entry.keys.val = NULL;
200 entry_ex->entry.keys.len = 0;
202 entry_ex->entry.kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
204 /* Get keys from the db */
206 hash = samdb_result_hash(mem_ctx, msg, "unicodePwd");
207 sc_val = ldb_msg_find_ldb_val(msg, "supplementalCredentials");
209 /* unicodePwd for enctype 0x17 (23) if present */
214 /* supplementalCredentials if present */
216 ndr_err = ndr_pull_struct_blob_all(sc_val, mem_ctx, iconv_convenience, &scb,
217 (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob);
218 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
219 dump_data(0, sc_val->data, sc_val->length);
224 for (i=0; i < scb.sub.num_packages; i++) {
225 if (scb.sub.packages[i].unknown1 != 0x00000001) {
229 if (strcmp("Primary:Kerberos", scb.sub.packages[i].name) != 0) {
233 if (!scb.sub.packages[i].data || !scb.sub.packages[i].data[0]) {
237 scp = &scb.sub.packages[i];
241 /* Primary:Kerberos element of supplementalCredentials */
245 blob = strhex_to_data_blob(scp->data);
250 talloc_steal(mem_ctx, blob.data);
252 /* TODO: use ndr_pull_struct_blob_all(), when the ndr layer handles it correct with relative pointers */
253 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, iconv_convenience, &_pkb,
254 (ndr_pull_flags_fn_t)ndr_pull_package_PrimaryKerberosBlob);
255 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
256 krb5_set_error_string(context, "LDB_message2entry_keys: could not parse package_PrimaryKerberosBlob");
257 krb5_warnx(context, "LDB_message2entry_keys: could not parse package_PrimaryKerberosBlob");
262 if (_pkb.version != 3) {
263 krb5_set_error_string(context, "LDB_message2entry_keys: could not parse PrimaryKerberos not version 3");
264 krb5_warnx(context, "LDB_message2entry_keys: could not parse PrimaryKerberos not version 3");
269 pkb3 = &_pkb.ctr.ctr3;
271 allocated_keys += pkb3->num_keys;
274 if (allocated_keys == 0) {
275 /* oh, no password. Apparently (comment in
276 * hdb-ldap.c) this violates the ASN.1, but this
277 * allows an entry with no keys (yet). */
281 /* allocate space to decode into */
282 entry_ex->entry.keys.len = 0;
283 entry_ex->entry.keys.val = calloc(allocated_keys, sizeof(Key));
284 if (entry_ex->entry.keys.val == NULL) {
289 if (hash && !(userAccountControl & UF_USE_DES_KEY_ONLY)) {
293 key.salt = NULL; /* No salt for this enc type */
295 ret = krb5_keyblock_init(context,
296 ENCTYPE_ARCFOUR_HMAC_MD5,
297 hash->hash, sizeof(hash->hash),
303 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
304 entry_ex->entry.keys.len++;
308 for (i=0; i < pkb3->num_keys; i++) {
312 if (!pkb3->keys[i].value) continue;
314 if (userAccountControl & UF_USE_DES_KEY_ONLY) {
315 switch (pkb3->keys[i].keytype) {
316 case ENCTYPE_DES_CBC_CRC:
317 case ENCTYPE_DES_CBC_MD5:
330 if (pkb3->salt.string) {
333 salt = data_blob_string_const(pkb3->salt.string);
335 key.salt = calloc(1, sizeof(*key.salt));
336 if (key.salt == NULL) {
341 key.salt->type = hdb_pw_salt;
343 ret = krb5_data_copy(&key.salt->salt, salt.data, salt.length);
351 ret = krb5_keyblock_init(context,
352 pkb3->keys[i].keytype,
353 pkb3->keys[i].value->data,
354 pkb3->keys[i].value->length,
365 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
366 entry_ex->entry.keys.len++;
372 entry_ex->entry.keys.len = 0;
374 if (entry_ex->entry.keys.len == 0 && entry_ex->entry.keys.val) {
375 free(entry_ex->entry.keys.val);
376 entry_ex->entry.keys.val = NULL;
382 * Construct an hdb_entry from a directory entry.
384 static krb5_error_code LDB_message2entry(krb5_context context, HDB *db,
385 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
386 enum hdb_ldb_ent_type ent_type,
387 struct ldb_message *msg,
388 struct ldb_message *realm_ref_msg,
389 hdb_entry_ex *entry_ex)
391 unsigned int userAccountControl;
393 krb5_error_code ret = 0;
394 krb5_boolean is_computer = FALSE;
395 const char *dnsdomain = ldb_msg_find_attr_as_string(realm_ref_msg, "dnsRoot", NULL);
396 char *realm = strupper_talloc(mem_ctx, dnsdomain);
397 struct loadparm_context *lp_ctx = ldb_get_opaque((struct ldb_context *)db->hdb_db, "loadparm");
398 struct ldb_dn *domain_dn = samdb_result_dn((struct ldb_context *)db->hdb_db,
402 ldb_dn_new(mem_ctx, (struct ldb_context *)db->hdb_db, NULL));
404 struct hdb_ldb_private *private;
407 struct ldb_message_element *objectclasses;
408 struct ldb_val computer_val;
409 computer_val.data = discard_const_p(uint8_t,"computer");
410 computer_val.length = strlen((const char *)computer_val.data);
412 objectclasses = ldb_msg_find_element(msg, "objectClass");
414 if (objectclasses && ldb_msg_find_val(objectclasses, &computer_val)) {
418 memset(entry_ex, 0, sizeof(*entry_ex));
421 krb5_set_error_string(context, "talloc_strdup: out of memory");
426 private = talloc(mem_ctx, struct hdb_ldb_private);
432 private->entry_ex = entry_ex;
433 private->iconv_convenience = lp_iconv_convenience(lp_ctx);
434 private->netbios_name = lp_netbios_name(lp_ctx);
436 talloc_set_destructor(private, hdb_ldb_destrutor);
438 entry_ex->ctx = private;
439 entry_ex->free_entry = hdb_ldb_free_entry;
441 userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
444 entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
445 if (ent_type == HDB_LDB_ENT_TYPE_ANY && principal == NULL) {
446 const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
447 if (!samAccountName) {
448 krb5_set_error_string(context, "LDB_message2entry: no samAccountName present");
452 samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
453 krb5_make_principal(context, &entry_ex->entry.principal, realm, samAccountName, NULL);
456 ret = copy_Principal(principal, entry_ex->entry.principal);
458 krb5_clear_error_string(context);
462 /* While we have copied the client principal, tests
463 * show that Win2k3 returns the 'corrected' realm, not
464 * the client-specified realm. This code attempts to
465 * replace the client principal's realm with the one
466 * we determine from our records */
468 /* this has to be with malloc() */
469 strdup_realm = strdup(realm);
472 krb5_clear_error_string(context);
475 free(*krb5_princ_realm(context, entry_ex->entry.principal));
476 krb5_princ_set_realm(context, entry_ex->entry.principal, &strdup_realm);
479 entry_ex->entry.flags = uf2HDBFlags(context, userAccountControl, ent_type);
481 if (ent_type == HDB_LDB_ENT_TYPE_KRBTGT) {
482 entry_ex->entry.flags.invalid = 0;
483 entry_ex->entry.flags.server = 1;
484 entry_ex->entry.flags.forwardable = 1;
485 entry_ex->entry.flags.ok_as_delegate = 1;
488 if (lp_parm_bool(lp_ctx, NULL, "kdc", "require spn for service", true)) {
489 if (!is_computer && !ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL)) {
490 entry_ex->entry.flags.server = 0;
494 /* use 'whenCreated' */
495 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
497 entry_ex->entry.created_by.principal = NULL;
499 entry_ex->entry.modified_by = (Event *) malloc(sizeof(Event));
500 if (entry_ex->entry.modified_by == NULL) {
501 krb5_set_error_string(context, "malloc: out of memory");
506 /* use 'whenChanged' */
507 entry_ex->entry.modified_by->time = ldb_msg_find_krb5time_ldap_time(msg, "whenChanged", 0);
509 entry_ex->entry.modified_by->principal = NULL;
511 entry_ex->entry.valid_start = NULL;
513 acct_expiry = samdb_result_account_expires(msg);
514 if (acct_expiry == 0x7FFFFFFFFFFFFFFFULL) {
515 entry_ex->entry.valid_end = NULL;
517 entry_ex->entry.valid_end = malloc(sizeof(*entry_ex->entry.valid_end));
518 if (entry_ex->entry.valid_end == NULL) {
522 *entry_ex->entry.valid_end = nt_time_to_unix(acct_expiry);
525 if (ent_type != HDB_LDB_ENT_TYPE_KRBTGT) {
526 NTTIME must_change_time
527 = samdb_result_force_password_change((struct ldb_context *)db->hdb_db, mem_ctx,
529 if (must_change_time == 0x7FFFFFFFFFFFFFFFULL) {
530 entry_ex->entry.pw_end = NULL;
532 entry_ex->entry.pw_end = malloc(sizeof(*entry_ex->entry.pw_end));
533 if (entry_ex->entry.pw_end == NULL) {
537 *entry_ex->entry.pw_end = nt_time_to_unix(must_change_time);
540 entry_ex->entry.pw_end = NULL;
543 entry_ex->entry.max_life = NULL;
545 entry_ex->entry.max_renew = NULL;
547 entry_ex->entry.generation = NULL;
549 /* Get keys from the db */
550 ret = LDB_message2entry_keys(context, private->iconv_convenience, private, msg, userAccountControl, entry_ex);
552 /* Could be bougus data in the entry, or out of memory */
556 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
557 if (entry_ex->entry.etypes == NULL) {
558 krb5_clear_error_string(context);
562 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
563 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
564 if (entry_ex->entry.etypes->val == NULL) {
565 krb5_clear_error_string(context);
569 for (i=0; i < entry_ex->entry.etypes->len; i++) {
570 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
574 private->msg = talloc_steal(private, msg);
575 private->realm_ref_msg = talloc_steal(private, realm_ref_msg);
576 private->samdb = (struct ldb_context *)db->hdb_db;
580 /* This doesn't free ent itself, that is for the eventual caller to do */
581 hdb_free_entry(context, entry_ex);
583 talloc_steal(db, entry_ex->ctx);
589 static krb5_error_code LDB_lookup_principal(krb5_context context, struct ldb_context *ldb_ctx,
591 krb5_const_principal principal,
592 enum hdb_ldb_ent_type ent_type,
593 struct ldb_dn *realm_dn,
594 struct ldb_message ***pmsg)
599 const char * const *princ_attrs = user_attrs;
602 char *short_princ_talloc;
604 struct ldb_result *res = NULL;
606 ret = krb5_unparse_name_flags(context, principal, KRB5_PRINCIPAL_UNPARSE_NO_REALM, &short_princ);
609 krb5_set_error_string(context, "LDB_lookup_principal: could not parse principal");
610 krb5_warnx(context, "LDB_lookup_principal: could not parse principal");
614 short_princ_talloc = talloc_strdup(mem_ctx, short_princ);
616 if (!short_princ_talloc) {
617 krb5_set_error_string(context, "LDB_lookup_principal: talloc_strdup() failed!");
622 case HDB_LDB_ENT_TYPE_CLIENT:
625 case HDB_LDB_ENT_TYPE_ANY:
628 case HDB_LDB_ENT_TYPE_KRBTGT:
629 filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
632 case HDB_LDB_ENT_TYPE_SERVER:
633 filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
639 krb5_set_error_string(context, "talloc_asprintf: out of memory");
643 lret = ldb_search(ldb_ctx, realm_dn, LDB_SCOPE_SUBTREE, filter, princ_attrs, &res);
645 if (lret != LDB_SUCCESS) {
646 DEBUG(3, ("Failed to search for %s: %s\n", filter, ldb_errstring(ldb_ctx)));
647 return HDB_ERR_NOENTRY;
648 } else if (res->count == 0 || res->count > 1) {
649 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter, res->count));
651 return HDB_ERR_NOENTRY;
653 talloc_steal(mem_ctx, res->msgs);
659 static krb5_error_code LDB_lookup_realm(krb5_context context, struct ldb_context *ldb_ctx,
662 struct ldb_message ***pmsg)
665 struct ldb_result *cross_ref_res;
666 struct ldb_dn *partitions_basedn = samdb_partitions_dn(ldb_ctx, mem_ctx);
668 ret = ldb_search_exp_fmt(ldb_ctx, mem_ctx, &cross_ref_res,
669 partitions_basedn, LDB_SCOPE_SUBTREE, realm_ref_attrs,
670 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
673 if (ret != LDB_SUCCESS) {
674 DEBUG(3, ("Failed to search to lookup realm(%s): %s\n", realm, ldb_errstring(ldb_ctx)));
675 talloc_free(cross_ref_res);
676 return HDB_ERR_NOENTRY;
677 } else if (cross_ref_res->count == 0 || cross_ref_res->count > 1) {
678 DEBUG(3, ("Failed find a single entry for realm %s: got %d\n", realm, cross_ref_res->count));
679 talloc_free(cross_ref_res);
680 return HDB_ERR_NOENTRY;
684 *pmsg = cross_ref_res->msgs;
685 talloc_steal(mem_ctx, cross_ref_res->msgs);
687 talloc_free(cross_ref_res);
693 static krb5_error_code LDB_open(krb5_context context, HDB *db, int flags, mode_t mode)
695 if (db->hdb_master_key_set) {
696 krb5_warnx(context, "LDB_open: use of a master key incompatible with LDB\n");
697 krb5_set_error_string(context, "LDB_open: use of a master key incompatible with LDB\n");
698 return HDB_ERR_NOENTRY;
704 static krb5_error_code LDB_close(krb5_context context, HDB *db)
709 static krb5_error_code LDB_lock(krb5_context context, HDB *db, int operation)
714 static krb5_error_code LDB_unlock(krb5_context context, HDB *db)
719 static krb5_error_code LDB_rename(krb5_context context, HDB *db, const char *new_name)
721 return HDB_ERR_DB_INUSE;
724 static krb5_error_code LDB_fetch_client(krb5_context context, HDB *db,
726 krb5_const_principal principal,
728 hdb_entry_ex *entry_ex) {
730 char *principal_string;
732 struct ldb_message **msg = NULL;
733 struct ldb_message **realm_ref_msg = NULL;
735 ret = krb5_unparse_name(context, principal, &principal_string);
741 nt_status = sam_get_results_principal((struct ldb_context *)db->hdb_db,
742 mem_ctx, principal_string,
743 &msg, &realm_ref_msg);
744 free(principal_string);
745 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
746 return HDB_ERR_NOENTRY;
747 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
749 } else if (!NT_STATUS_IS_OK(nt_status)) {
753 ret = LDB_message2entry(context, db, mem_ctx,
754 principal, HDB_LDB_ENT_TYPE_CLIENT,
755 msg[0], realm_ref_msg[0], entry_ex);
759 static krb5_error_code LDB_fetch_krbtgt(krb5_context context, HDB *db,
761 krb5_const_principal principal,
763 hdb_entry_ex *entry_ex)
766 struct ldb_message **msg = NULL;
767 struct ldb_message **realm_ref_msg = NULL;
768 struct ldb_dn *realm_dn;
770 krb5_principal alloc_principal = NULL;
771 if (principal->name.name_string.len != 2
772 || (strcmp(principal->name.name_string.val[0], KRB5_TGS_NAME) != 0)) {
774 return HDB_ERR_NOENTRY;
777 /* krbtgt case. Either us or a trusted realm */
778 if ((LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
779 mem_ctx, principal->name.name_string.val[1], &realm_ref_msg) == 0)) {
781 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
782 * is in our db, then direct the caller at our primary
785 const char *dnsdomain = ldb_msg_find_attr_as_string(realm_ref_msg[0], "dnsRoot", NULL);
786 char *realm_fixed = strupper_talloc(mem_ctx, dnsdomain);
788 krb5_set_error_string(context, "strupper_talloc: out of memory");
792 ret = krb5_copy_principal(context, principal, &alloc_principal);
797 free(alloc_principal->name.name_string.val[1]);
798 alloc_principal->name.name_string.val[1] = strdup(realm_fixed);
799 talloc_free(realm_fixed);
800 if (!alloc_principal->name.name_string.val[1]) {
801 krb5_set_error_string(context, "LDB_fetch: strdup() failed!");
804 principal = alloc_principal;
805 realm_dn = samdb_result_dn((struct ldb_context *)db->hdb_db, mem_ctx, realm_ref_msg[0], "nCName", NULL);
807 /* we should lookup trusted domains */
808 return HDB_ERR_NOENTRY;
811 realm_dn = samdb_result_dn((struct ldb_context *)db->hdb_db, mem_ctx, realm_ref_msg[0], "nCName", NULL);
813 ret = LDB_lookup_principal(context, (struct ldb_context *)db->hdb_db,
815 principal, HDB_LDB_ENT_TYPE_KRBTGT, realm_dn, &msg);
818 krb5_warnx(context, "LDB_fetch: could not find principal in DB");
819 krb5_set_error_string(context, "LDB_fetch: could not find principal in DB");
823 ret = LDB_message2entry(context, db, mem_ctx,
824 principal, HDB_LDB_ENT_TYPE_KRBTGT,
825 msg[0], realm_ref_msg[0], entry_ex);
827 krb5_warnx(context, "LDB_fetch: message2entry failed");
832 static krb5_error_code LDB_fetch_server(krb5_context context, HDB *db,
834 krb5_const_principal principal,
836 hdb_entry_ex *entry_ex)
840 struct ldb_message **msg = NULL;
841 struct ldb_message **realm_ref_msg = NULL;
842 struct ldb_dn *partitions_basedn = samdb_partitions_dn(db->hdb_db, mem_ctx);
843 if (principal->name.name_string.len >= 2) {
844 /* 'normal server' case */
847 struct ldb_dn *user_dn, *domain_dn;
848 char *principal_string;
850 ret = krb5_unparse_name_flags(context, principal,
851 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
857 /* At this point we may find the host is known to be
858 * in a different realm, so we should generate a
859 * referral instead */
860 nt_status = crack_service_principal_name((struct ldb_context *)db->hdb_db,
861 mem_ctx, principal_string,
862 &user_dn, &domain_dn);
863 free(principal_string);
865 if (!NT_STATUS_IS_OK(nt_status)) {
866 return HDB_ERR_NOENTRY;
869 ldb_ret = gendb_search_dn((struct ldb_context *)db->hdb_db,
870 mem_ctx, user_dn, &msg, user_attrs);
873 return HDB_ERR_NOENTRY;
876 ldb_ret = gendb_search((struct ldb_context *)db->hdb_db,
877 mem_ctx, partitions_basedn, &realm_ref_msg, realm_ref_attrs,
878 "ncName=%s", ldb_dn_get_linearized(domain_dn));
881 return HDB_ERR_NOENTRY;
885 struct ldb_dn *realm_dn;
886 /* server as client principal case, but we must not lookup userPrincipalNames */
888 realm = krb5_principal_get_realm(context, principal);
890 ret = LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
891 mem_ctx, realm, &realm_ref_msg);
893 return HDB_ERR_NOENTRY;
896 realm_dn = samdb_result_dn((struct ldb_context *)db->hdb_db, mem_ctx, realm_ref_msg[0], "nCName", NULL);
898 ret = LDB_lookup_principal(context, (struct ldb_context *)db->hdb_db,
900 principal, HDB_LDB_ENT_TYPE_SERVER, realm_dn, &msg);
907 ret = LDB_message2entry(context, db, mem_ctx,
908 principal, HDB_LDB_ENT_TYPE_SERVER,
909 msg[0], realm_ref_msg[0], entry_ex);
911 krb5_warnx(context, "LDB_fetch: message2entry failed");
917 static krb5_error_code LDB_fetch(krb5_context context, HDB *db,
918 krb5_const_principal principal,
920 hdb_entry_ex *entry_ex)
922 krb5_error_code ret = HDB_ERR_NOENTRY;
924 TALLOC_CTX *mem_ctx = talloc_named(db, 0, "LDB_fetch context");
927 krb5_set_error_string(context, "LDB_fetch: talloc_named() failed!");
931 if (flags & HDB_F_GET_CLIENT) {
932 ret = LDB_fetch_client(context, db, mem_ctx, principal, flags, entry_ex);
933 if (ret != HDB_ERR_NOENTRY) goto done;
935 if (flags & HDB_F_GET_SERVER) {
936 ret = LDB_fetch_server(context, db, mem_ctx, principal, flags, entry_ex);
937 if (ret != HDB_ERR_NOENTRY) goto done;
938 ret = LDB_fetch_krbtgt(context, db, mem_ctx, principal, flags, entry_ex);
939 if (ret != HDB_ERR_NOENTRY) goto done;
941 if (flags & HDB_F_GET_KRBTGT) {
942 ret = LDB_fetch_krbtgt(context, db, mem_ctx, principal, flags, entry_ex);
943 if (ret != HDB_ERR_NOENTRY) goto done;
947 talloc_free(mem_ctx);
951 static krb5_error_code LDB_store(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
953 return HDB_ERR_DB_INUSE;
956 static krb5_error_code LDB_remove(krb5_context context, HDB *db, krb5_const_principal principal)
958 return HDB_ERR_DB_INUSE;
962 struct ldb_context *ctx;
965 struct ldb_message **msgs;
966 struct ldb_message **realm_ref_msgs;
969 static krb5_error_code LDB_seq(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
972 struct hdb_ldb_seq *priv = (struct hdb_ldb_seq *)db->hdb_dbc;
974 hdb_entry_ex entry_ex;
975 memset(&entry_ex, '\0', sizeof(entry_ex));
978 return HDB_ERR_NOENTRY;
981 mem_ctx = talloc_named(priv, 0, "LDB_seq context");
984 krb5_set_error_string(context, "LDB_seq: talloc_named() failed!");
988 if (priv->index < priv->count) {
989 ret = LDB_message2entry(context, db, mem_ctx,
990 NULL, HDB_LDB_ENT_TYPE_ANY,
991 priv->msgs[priv->index++],
992 priv->realm_ref_msgs[0], entry);
994 ret = HDB_ERR_NOENTRY;
1001 talloc_free(mem_ctx);
1007 static krb5_error_code LDB_firstkey(krb5_context context, HDB *db, unsigned flags,
1008 hdb_entry_ex *entry)
1010 struct ldb_context *ldb_ctx = (struct ldb_context *)db->hdb_db;
1011 struct hdb_ldb_seq *priv = (struct hdb_ldb_seq *)db->hdb_dbc;
1013 struct ldb_dn *realm_dn = NULL;
1014 struct ldb_result *res = NULL;
1015 struct ldb_message **realm_ref_msgs = NULL;
1016 krb5_error_code ret;
1017 TALLOC_CTX *mem_ctx;
1025 priv = (struct hdb_ldb_seq *) talloc(db, struct hdb_ldb_seq);
1027 krb5_set_error_string(context, "talloc: out of memory");
1031 priv->ctx = ldb_ctx;
1034 priv->realm_ref_msgs = NULL;
1037 mem_ctx = talloc_named(priv, 0, "LDB_firstkey context");
1040 krb5_set_error_string(context, "LDB_firstkey: talloc_named() failed!");
1044 ret = krb5_get_default_realm(context, &realm);
1050 ret = LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
1051 mem_ctx, realm, &realm_ref_msgs);
1057 krb5_warnx(context, "LDB_firstkey: could not find realm\n");
1058 return HDB_ERR_NOENTRY;
1061 realm_dn = samdb_result_dn((struct ldb_context *)db->hdb_db, mem_ctx, realm_ref_msgs[0], "nCName", NULL);
1063 priv->realm_ref_msgs = talloc_steal(priv, realm_ref_msgs);
1065 lret = ldb_search(ldb_ctx, realm_dn,
1066 LDB_SCOPE_SUBTREE, "(objectClass=user)",
1069 if (lret != LDB_SUCCESS) {
1071 return HDB_ERR_NOENTRY;
1074 priv->count = res->count;
1075 priv->msgs = talloc_steal(priv, res->msgs);
1080 ret = LDB_seq(context, db, flags, entry);
1086 talloc_free(mem_ctx);
1091 static krb5_error_code LDB_nextkey(krb5_context context, HDB *db, unsigned flags,
1092 hdb_entry_ex *entry)
1094 return LDB_seq(context, db, flags, entry);
1097 static krb5_error_code LDB_destroy(krb5_context context, HDB *db)
1103 /* This interface is to be called by the KDC, which is expecting Samba
1104 * calling conventions. It is also called by a wrapper
1105 * (hdb_ldb_create) from the kpasswdd -> krb5 -> keytab_hdb -> hdb
1108 NTSTATUS kdc_hdb_ldb_create(TALLOC_CTX *mem_ctx,
1109 struct event_context *ev_ctx,
1110 struct loadparm_context *lp_ctx,
1111 krb5_context context, struct HDB **db, const char *arg)
1114 struct auth_session_info *session_info;
1115 *db = talloc(mem_ctx, HDB);
1117 krb5_set_error_string(context, "malloc: out of memory");
1118 return NT_STATUS_NO_MEMORY;
1121 (*db)->hdb_master_key_set = 0;
1122 (*db)->hdb_db = NULL;
1124 nt_status = auth_system_session_info(*db, lp_ctx, &session_info);
1125 if (!NT_STATUS_IS_OK(nt_status)) {
1129 /* The idea here is very simple. Using Kerberos to
1130 * authenticate the KDC to the LDAP server is higly likely to
1133 * In future we may set this up to use EXERNAL and SSL
1134 * certificates, for now it will almost certainly be NTLMSSP
1137 cli_credentials_set_kerberos_state(session_info->credentials,
1138 CRED_DONT_USE_KERBEROS);
1140 /* Setup the link to LDB */
1141 (*db)->hdb_db = samdb_connect(*db, ev_ctx, lp_ctx, session_info);
1142 if ((*db)->hdb_db == NULL) {
1143 DEBUG(1, ("hdb_ldb_create: Cannot open samdb for KDC backend!"));
1144 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1147 (*db)->hdb_dbc = NULL;
1148 (*db)->hdb_open = LDB_open;
1149 (*db)->hdb_close = LDB_close;
1150 (*db)->hdb_fetch = LDB_fetch;
1151 (*db)->hdb_store = LDB_store;
1152 (*db)->hdb_remove = LDB_remove;
1153 (*db)->hdb_firstkey = LDB_firstkey;
1154 (*db)->hdb_nextkey = LDB_nextkey;
1155 (*db)->hdb_lock = LDB_lock;
1156 (*db)->hdb_unlock = LDB_unlock;
1157 (*db)->hdb_rename = LDB_rename;
1158 /* we don't implement these, as we are not a lockable database */
1159 (*db)->hdb__get = NULL;
1160 (*db)->hdb__put = NULL;
1161 /* kadmin should not be used for deletes - use other tools instead */
1162 (*db)->hdb__del = NULL;
1163 (*db)->hdb_destroy = LDB_destroy;
1165 return NT_STATUS_OK;
1168 krb5_error_code hdb_ldb_create(krb5_context context, struct HDB **db, const char *arg)
1171 /* The global kdc_mem_ctx and kdc_lp_ctx, Disgusting, ugly hack, but it means one less private hook */
1172 nt_status = kdc_hdb_ldb_create(kdc_mem_ctx, event_context_find(kdc_mem_ctx), kdc_lp_ctx,
1175 if (NT_STATUS_IS_OK(nt_status)) {