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"
51 { HDB_LDB_ENT_TYPE_CLIENT, HDB_LDB_ENT_TYPE_SERVER,
52 HDB_LDB_ENT_TYPE_KRBTGT, HDB_LDB_ENT_TYPE_ANY };
54 static const char * const krb5_attrs[] = {
59 "servicePrincipalName",
71 "msDS-KeyVersionNumber",
75 static const char *realm_ref_attrs[] = {
81 static KerberosTime ldb_msg_find_krb5time_ldap_time(struct ldb_message *msg, const char *attr, KerberosTime default_val)
87 gentime = ldb_msg_find_attr_as_string(msg, attr, NULL);
91 tmp = strptime(gentime, "%Y%m%d%H%M%SZ", &tm);
99 static HDBFlags uf2HDBFlags(krb5_context context, int userAccountControl, enum hdb_ldb_ent_type ent_type)
101 HDBFlags flags = int2HDBFlags(0);
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 UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
156 if (userAccountControl & UF_TEMP_DUPLICATE_ACCOUNT) {
160 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in LDB_message2entry() */
163 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
167 if (userAccountControl & UF_SMARTCARD_REQUIRED) {
168 flags.require_hwauth = 1;
170 if (userAccountControl & UF_TRUSTED_FOR_DELEGATION) {
171 flags.ok_as_delegate = 1;
173 if (!(userAccountControl & UF_NOT_DELEGATED)) {
174 flags.forwardable = 1;
178 if (userAccountControl & UF_DONT_REQUIRE_PREAUTH) {
179 flags.require_preauth = 0;
181 flags.require_preauth = 1;
187 static int hdb_ldb_destrutor(struct hdb_ldb_private *private)
189 hdb_entry_ex *entry_ex = private->entry_ex;
190 free_hdb_entry(&entry_ex->entry);
194 static void hdb_ldb_free_entry(krb5_context context, hdb_entry_ex *entry_ex)
196 talloc_free(entry_ex->ctx);
200 * Construct an hdb_entry from a directory entry.
202 static krb5_error_code LDB_message2entry(krb5_context context, HDB *db,
203 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
204 enum hdb_ldb_ent_type ent_type,
205 struct ldb_message *msg,
206 struct ldb_message *realm_ref_msg,
207 hdb_entry_ex *entry_ex)
209 unsigned int userAccountControl;
211 krb5_error_code ret = 0;
212 krb5_boolean is_computer = FALSE;
213 const char *dnsdomain = ldb_msg_find_attr_as_string(realm_ref_msg, "dnsRoot", NULL);
214 char *realm = strupper_talloc(mem_ctx, dnsdomain);
215 struct ldb_dn *domain_dn = samdb_result_dn((struct ldb_context *)db->hdb_db,
219 ldb_dn_new(mem_ctx, (struct ldb_context *)db->hdb_db, NULL));
221 struct hdb_ldb_private *private;
223 struct ldb_message_element *ldb_keys;
225 struct ldb_message_element *objectclasses;
226 struct ldb_val computer_val;
227 computer_val.data = discard_const_p(uint8_t,"computer");
228 computer_val.length = strlen((const char *)computer_val.data);
230 objectclasses = ldb_msg_find_element(msg, "objectClass");
232 if (objectclasses && ldb_msg_find_val(objectclasses, &computer_val)) {
236 memset(entry_ex, 0, sizeof(*entry_ex));
239 krb5_set_error_string(context, "talloc_strdup: out of memory");
244 private = talloc(mem_ctx, struct hdb_ldb_private);
250 private->entry_ex = entry_ex;
252 talloc_set_destructor(private, hdb_ldb_destrutor);
254 entry_ex->ctx = private;
255 entry_ex->free_entry = hdb_ldb_free_entry;
257 userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
260 entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
261 if (ent_type == HDB_LDB_ENT_TYPE_ANY && principal == NULL) {
262 const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
263 if (!samAccountName) {
264 krb5_set_error_string(context, "LDB_message2entry: no samAccountName present");
268 samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
269 krb5_make_principal(context, &entry_ex->entry.principal, realm, samAccountName, NULL);
272 ret = copy_Principal(principal, entry_ex->entry.principal);
274 krb5_clear_error_string(context);
278 /* While we have copied the client principal, tests
279 * show that Win2k3 returns the 'corrected' realm, not
280 * the client-specified realm. This code attempts to
281 * replace the client principal's realm with the one
282 * we determine from our records */
284 /* this has to be with malloc() */
285 strdup_realm = strdup(realm);
288 krb5_clear_error_string(context);
291 free(*krb5_princ_realm(context, entry_ex->entry.principal));
292 krb5_princ_set_realm(context, entry_ex->entry.principal, &strdup_realm);
295 entry_ex->entry.kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
297 entry_ex->entry.flags = uf2HDBFlags(context, userAccountControl, ent_type);
299 if (ent_type == HDB_LDB_ENT_TYPE_KRBTGT) {
300 entry_ex->entry.flags.invalid = 0;
301 entry_ex->entry.flags.server = 1;
302 entry_ex->entry.flags.forwardable = 1;
303 entry_ex->entry.flags.ok_as_delegate = 1;
306 if (lp_parm_bool(-1, "kdc", "require spn for service", True)) {
307 if (!is_computer && !ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL)) {
308 entry_ex->entry.flags.server = 0;
312 /* use 'whenCreated' */
313 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
315 entry_ex->entry.created_by.principal = NULL;
317 entry_ex->entry.modified_by = (Event *) malloc(sizeof(Event));
318 if (entry_ex->entry.modified_by == NULL) {
319 krb5_set_error_string(context, "malloc: out of memory");
324 /* use 'whenChanged' */
325 entry_ex->entry.modified_by->time = ldb_msg_find_krb5time_ldap_time(msg, "whenChanged", 0);
327 entry_ex->entry.modified_by->principal = NULL;
329 entry_ex->entry.valid_start = NULL;
331 acct_expiry = samdb_result_nttime(msg, "accountExpires", (NTTIME)-1);
332 if ((acct_expiry == (NTTIME)-1) ||
333 (acct_expiry == 0x7FFFFFFFFFFFFFFFULL)) {
334 entry_ex->entry.valid_end = NULL;
336 entry_ex->entry.valid_end = malloc(sizeof(*entry_ex->entry.valid_end));
337 if (entry_ex->entry.valid_end == NULL) {
341 *entry_ex->entry.valid_end = nt_time_to_unix(acct_expiry);
344 if (ent_type != HDB_LDB_ENT_TYPE_KRBTGT) {
345 NTTIME must_change_time
346 = samdb_result_force_password_change((struct ldb_context *)db->hdb_db, mem_ctx,
348 if (must_change_time == 0x7FFFFFFFFFFFFFFFULL) {
349 entry_ex->entry.pw_end = NULL;
351 entry_ex->entry.pw_end = malloc(sizeof(*entry_ex->entry.pw_end));
352 if (entry_ex->entry.pw_end == NULL) {
356 *entry_ex->entry.pw_end = nt_time_to_unix(must_change_time);
359 entry_ex->entry.pw_end = NULL;
362 entry_ex->entry.max_life = NULL;
364 entry_ex->entry.max_renew = NULL;
366 entry_ex->entry.generation = NULL;
368 /* Get krb5Key from the db */
370 ldb_keys = ldb_msg_find_element(msg, "krb5Key");
373 /* oh, no password. Apparently (comment in
374 * hdb-ldap.c) this violates the ASN.1, but this
375 * allows an entry with no keys (yet). */
376 entry_ex->entry.keys.val = NULL;
377 entry_ex->entry.keys.len = 0;
379 /* allocate space to decode into */
380 entry_ex->entry.keys.val = calloc(ldb_keys->num_values, sizeof(Key));
381 if (entry_ex->entry.keys.val == NULL) {
386 entry_ex->entry.keys.len = 0;
388 /* Decode Kerberos keys into the hdb structure */
389 for (i=0; i < ldb_keys->num_values; i++) {
392 ret = decode_Key(ldb_keys->values[i].data, ldb_keys->values[i].length,
395 /* Could be bougus data in the entry, or out of memory */
399 if (userAccountControl & UF_USE_DES_KEY_ONLY) {
400 switch (key.key.keytype) {
402 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
403 entry_ex->entry.keys.len++;
405 /* We must use DES keys only */
409 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
410 entry_ex->entry.keys.len++;
415 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
416 if (entry_ex->entry.etypes == NULL) {
417 krb5_clear_error_string(context);
421 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
422 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
423 if (entry_ex->entry.etypes->val == NULL) {
424 krb5_clear_error_string(context);
428 for (i=0; i < entry_ex->entry.etypes->len; i++) {
429 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
433 private->msg = talloc_steal(private, msg);
434 private->realm_ref_msg = talloc_steal(private, realm_ref_msg);
435 private->samdb = (struct ldb_context *)db->hdb_db;
439 /* This doesn't free ent itself, that is for the eventual caller to do */
440 hdb_free_entry(context, entry_ex);
442 talloc_steal(db, entry_ex->ctx);
448 static krb5_error_code LDB_lookup_principal(krb5_context context, struct ldb_context *ldb_ctx,
450 krb5_const_principal principal,
451 enum hdb_ldb_ent_type ent_type,
452 struct ldb_dn *realm_dn,
453 struct ldb_message ***pmsg)
458 const char * const *princ_attrs = krb5_attrs;
461 char *short_princ_talloc;
463 struct ldb_result *res = NULL;
465 ret = krb5_unparse_name_flags(context, principal, KRB5_PRINCIPAL_UNPARSE_NO_REALM, &short_princ);
468 krb5_set_error_string(context, "LDB_lookup_principal: could not parse principal");
469 krb5_warnx(context, "LDB_lookup_principal: could not parse principal");
473 short_princ_talloc = talloc_strdup(mem_ctx, short_princ);
475 if (!short_princ_talloc) {
476 krb5_set_error_string(context, "LDB_lookup_principal: talloc_strdup() failed!");
481 case HDB_LDB_ENT_TYPE_CLIENT:
484 case HDB_LDB_ENT_TYPE_ANY:
487 case HDB_LDB_ENT_TYPE_KRBTGT:
488 filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
491 case HDB_LDB_ENT_TYPE_SERVER:
492 filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
498 krb5_set_error_string(context, "talloc_asprintf: out of memory");
502 lret = ldb_search(ldb_ctx, realm_dn, LDB_SCOPE_SUBTREE, filter, princ_attrs, &res);
504 if (lret != LDB_SUCCESS) {
505 DEBUG(3, ("Failed to search for %s: %s\n", filter, ldb_errstring(ldb_ctx)));
506 return HDB_ERR_NOENTRY;
507 } else if (res->count == 0 || res->count > 1) {
508 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter, res->count));
510 return HDB_ERR_NOENTRY;
512 talloc_steal(mem_ctx, res->msgs);
518 static krb5_error_code LDB_lookup_realm(krb5_context context, struct ldb_context *ldb_ctx,
521 struct ldb_message ***pmsg)
524 struct ldb_result *cross_ref_res;
525 struct ldb_dn *partitions_basedn = samdb_partitions_dn(ldb_ctx, mem_ctx);
527 ret = ldb_search_exp_fmt(ldb_ctx, mem_ctx, &cross_ref_res,
528 partitions_basedn, LDB_SCOPE_SUBTREE, realm_ref_attrs,
529 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
532 if (ret != LDB_SUCCESS) {
533 DEBUG(3, ("Failed to search to lookup realm(%s): %s\n", realm, ldb_errstring(ldb_ctx)));
534 talloc_free(cross_ref_res);
535 return HDB_ERR_NOENTRY;
536 } else if (cross_ref_res->count == 0 || cross_ref_res->count > 1) {
537 DEBUG(3, ("Failed find a single entry for realm %s: got %d\n", realm, cross_ref_res->count));
538 talloc_free(cross_ref_res);
539 return HDB_ERR_NOENTRY;
543 *pmsg = cross_ref_res->msgs;
544 talloc_steal(mem_ctx, cross_ref_res->msgs);
546 talloc_free(cross_ref_res);
552 static krb5_error_code LDB_open(krb5_context context, HDB *db, int flags, mode_t mode)
554 if (db->hdb_master_key_set) {
555 krb5_warnx(context, "LDB_open: use of a master key incompatible with LDB\n");
556 krb5_set_error_string(context, "LDB_open: use of a master key incompatible with LDB\n");
557 return HDB_ERR_NOENTRY;
563 static krb5_error_code LDB_close(krb5_context context, HDB *db)
568 static krb5_error_code LDB_lock(krb5_context context, HDB *db, int operation)
573 static krb5_error_code LDB_unlock(krb5_context context, HDB *db)
578 static krb5_error_code LDB_rename(krb5_context context, HDB *db, const char *new_name)
580 return HDB_ERR_DB_INUSE;
583 static krb5_error_code LDB_fetch_client(krb5_context context, HDB *db,
585 krb5_const_principal principal,
587 hdb_entry_ex *entry_ex) {
589 char *principal_string;
591 struct ldb_message **msg = NULL;
592 struct ldb_message **realm_ref_msg = NULL;
594 ret = krb5_unparse_name(context, principal, &principal_string);
600 nt_status = sam_get_results_principal((struct ldb_context *)db->hdb_db,
601 mem_ctx, principal_string,
602 &msg, &realm_ref_msg);
603 free(principal_string);
604 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
605 return HDB_ERR_NOENTRY;
606 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
608 } else if (!NT_STATUS_IS_OK(nt_status)) {
612 ret = LDB_message2entry(context, db, mem_ctx,
613 principal, HDB_LDB_ENT_TYPE_CLIENT,
614 msg[0], realm_ref_msg[0], entry_ex);
618 static krb5_error_code LDB_fetch_krbtgt(krb5_context context, HDB *db,
620 krb5_const_principal principal,
622 hdb_entry_ex *entry_ex)
625 struct ldb_message **msg = NULL;
626 struct ldb_message **realm_ref_msg = NULL;
627 struct ldb_dn *realm_dn;
629 krb5_principal alloc_principal = NULL;
630 if (principal->name.name_string.len != 2
631 || (strcmp(principal->name.name_string.val[0], KRB5_TGS_NAME) != 0)) {
633 return HDB_ERR_NOENTRY;
636 /* krbtgt case. Either us or a trusted realm */
637 if ((LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
638 mem_ctx, principal->name.name_string.val[1], &realm_ref_msg) == 0)) {
640 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
641 * is in our db, then direct the caller at our primary
644 const char *dnsdomain = ldb_msg_find_attr_as_string(realm_ref_msg[0], "dnsRoot", NULL);
645 char *realm_fixed = strupper_talloc(mem_ctx, dnsdomain);
647 krb5_set_error_string(context, "strupper_talloc: out of memory");
651 ret = krb5_copy_principal(context, principal, &alloc_principal);
656 free(alloc_principal->name.name_string.val[1]);
657 alloc_principal->name.name_string.val[1] = strdup(realm_fixed);
658 talloc_free(realm_fixed);
659 if (!alloc_principal->name.name_string.val[1]) {
660 krb5_set_error_string(context, "LDB_fetch: strdup() failed!");
663 principal = alloc_principal;
664 realm_dn = samdb_result_dn((struct ldb_context *)db->hdb_db, mem_ctx, realm_ref_msg[0], "nCName", NULL);
666 /* we should lookup trusted domains */
667 return HDB_ERR_NOENTRY;
670 realm_dn = samdb_result_dn((struct ldb_context *)db->hdb_db, mem_ctx, realm_ref_msg[0], "nCName", NULL);
672 ret = LDB_lookup_principal(context, (struct ldb_context *)db->hdb_db,
674 principal, HDB_LDB_ENT_TYPE_KRBTGT, realm_dn, &msg);
677 krb5_warnx(context, "LDB_fetch: could not find principal in DB");
678 krb5_set_error_string(context, "LDB_fetch: could not find principal in DB");
682 ret = LDB_message2entry(context, db, mem_ctx,
683 principal, HDB_LDB_ENT_TYPE_KRBTGT,
684 msg[0], realm_ref_msg[0], entry_ex);
686 krb5_warnx(context, "LDB_fetch: message2entry failed");
691 static krb5_error_code LDB_fetch_server(krb5_context context, HDB *db,
693 krb5_const_principal principal,
695 hdb_entry_ex *entry_ex)
699 struct ldb_message **msg = NULL;
700 struct ldb_message **realm_ref_msg = NULL;
701 struct ldb_dn *partitions_basedn = samdb_partitions_dn(db->hdb_db, mem_ctx);
702 if (principal->name.name_string.len >= 2) {
703 /* 'normal server' case */
706 struct ldb_dn *user_dn, *domain_dn;
707 char *principal_string;
709 ret = krb5_unparse_name_flags(context, principal,
710 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
716 /* At this point we may find the host is known to be
717 * in a different realm, so we should generate a
718 * referral instead */
719 nt_status = crack_service_principal_name((struct ldb_context *)db->hdb_db,
720 mem_ctx, principal_string,
721 &user_dn, &domain_dn);
722 free(principal_string);
724 if (!NT_STATUS_IS_OK(nt_status)) {
725 return HDB_ERR_NOENTRY;
728 ldb_ret = gendb_search_dn((struct ldb_context *)db->hdb_db,
729 mem_ctx, user_dn, &msg, krb5_attrs);
732 return HDB_ERR_NOENTRY;
735 ldb_ret = gendb_search((struct ldb_context *)db->hdb_db,
736 mem_ctx, partitions_basedn, &realm_ref_msg, realm_ref_attrs,
737 "ncName=%s", ldb_dn_get_linearized(domain_dn));
740 return HDB_ERR_NOENTRY;
744 struct ldb_dn *realm_dn;
745 /* server as client principal case, but we must not lookup userPrincipalNames */
747 realm = krb5_principal_get_realm(context, principal);
749 ret = LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
750 mem_ctx, realm, &realm_ref_msg);
752 return HDB_ERR_NOENTRY;
755 realm_dn = samdb_result_dn((struct ldb_context *)db->hdb_db, mem_ctx, realm_ref_msg[0], "nCName", NULL);
757 ret = LDB_lookup_principal(context, (struct ldb_context *)db->hdb_db,
759 principal, HDB_LDB_ENT_TYPE_SERVER, realm_dn, &msg);
766 ret = LDB_message2entry(context, db, mem_ctx,
767 principal, HDB_LDB_ENT_TYPE_SERVER,
768 msg[0], realm_ref_msg[0], entry_ex);
770 krb5_warnx(context, "LDB_fetch: message2entry failed");
776 static krb5_error_code LDB_fetch(krb5_context context, HDB *db,
777 krb5_const_principal principal,
779 hdb_entry_ex *entry_ex)
781 krb5_error_code ret = HDB_ERR_NOENTRY;
783 TALLOC_CTX *mem_ctx = talloc_named(db, 0, "LDB_fetch context");
786 krb5_set_error_string(context, "LDB_fetch: talloc_named() failed!");
790 if (flags & HDB_F_GET_CLIENT) {
791 ret = LDB_fetch_client(context, db, mem_ctx, principal, flags, entry_ex);
792 if (ret != HDB_ERR_NOENTRY) goto done;
794 if (flags & HDB_F_GET_SERVER) {
795 ret = LDB_fetch_server(context, db, mem_ctx, principal, flags, entry_ex);
796 if (ret != HDB_ERR_NOENTRY) goto done;
797 ret = LDB_fetch_krbtgt(context, db, mem_ctx, principal, flags, entry_ex);
798 if (ret != HDB_ERR_NOENTRY) goto done;
800 if (flags & HDB_F_GET_KRBTGT) {
801 ret = LDB_fetch_krbtgt(context, db, mem_ctx, principal, flags, entry_ex);
802 if (ret != HDB_ERR_NOENTRY) goto done;
806 talloc_free(mem_ctx);
810 static krb5_error_code LDB_store(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
812 return HDB_ERR_DB_INUSE;
815 static krb5_error_code LDB_remove(krb5_context context, HDB *db, krb5_const_principal principal)
817 return HDB_ERR_DB_INUSE;
821 struct ldb_context *ctx;
824 struct ldb_message **msgs;
825 struct ldb_message **realm_ref_msgs;
828 static krb5_error_code LDB_seq(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
831 struct hdb_ldb_seq *priv = (struct hdb_ldb_seq *)db->hdb_openp;
833 hdb_entry_ex entry_ex;
834 memset(&entry_ex, '\0', sizeof(entry_ex));
837 return HDB_ERR_NOENTRY;
840 mem_ctx = talloc_named(priv, 0, "LDB_seq context");
843 krb5_set_error_string(context, "LDB_seq: talloc_named() failed!");
847 if (priv->index < priv->count) {
848 ret = LDB_message2entry(context, db, mem_ctx,
849 NULL, HDB_LDB_ENT_TYPE_ANY,
850 priv->msgs[priv->index++],
851 priv->realm_ref_msgs[0], entry);
853 ret = HDB_ERR_NOENTRY;
858 db->hdb_openp = NULL;
860 talloc_free(mem_ctx);
866 static krb5_error_code LDB_firstkey(krb5_context context, HDB *db, unsigned flags,
869 struct ldb_context *ldb_ctx = (struct ldb_context *)db->hdb_db;
870 struct hdb_ldb_seq *priv = (struct hdb_ldb_seq *)db->hdb_openp;
872 struct ldb_dn *realm_dn = NULL;
873 struct ldb_result *res = NULL;
874 struct ldb_message **realm_ref_msgs = NULL;
884 priv = (struct hdb_ldb_seq *) talloc(db, struct hdb_ldb_seq);
886 krb5_set_error_string(context, "talloc: out of memory");
893 priv->realm_ref_msgs = NULL;
896 mem_ctx = talloc_named(priv, 0, "LDB_firstkey context");
899 krb5_set_error_string(context, "LDB_firstkey: talloc_named() failed!");
903 ret = krb5_get_default_realm(context, &realm);
909 ret = LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
910 mem_ctx, realm, &realm_ref_msgs);
916 krb5_warnx(context, "LDB_firstkey: could not find realm\n");
917 return HDB_ERR_NOENTRY;
920 realm_dn = samdb_result_dn((struct ldb_context *)db->hdb_db, mem_ctx, realm_ref_msgs[0], "nCName", NULL);
922 priv->realm_ref_msgs = talloc_steal(priv, realm_ref_msgs);
924 lret = ldb_search(ldb_ctx, realm_dn,
925 LDB_SCOPE_SUBTREE, "(objectClass=user)",
928 if (lret != LDB_SUCCESS) {
930 return HDB_ERR_NOENTRY;
933 priv->count = res->count;
934 priv->msgs = talloc_steal(priv, res->msgs);
937 db->hdb_openp = priv;
939 ret = LDB_seq(context, db, flags, entry);
943 db->hdb_openp = NULL;
945 talloc_free(mem_ctx);
950 static krb5_error_code LDB_nextkey(krb5_context context, HDB *db, unsigned flags,
953 return LDB_seq(context, db, flags, entry);
956 static krb5_error_code LDB_destroy(krb5_context context, HDB *db)
962 /* This interface is to be called by the KDC, which is expecting Samba
963 * calling conventions. It is also called by a wrapper
964 * (hdb_ldb_create) from the kpasswdd -> krb5 -> keytab_hdb -> hdb
967 NTSTATUS kdc_hdb_ldb_create(TALLOC_CTX *mem_ctx,
968 krb5_context context, struct HDB **db, const char *arg)
971 struct auth_session_info *session_info;
972 *db = talloc(mem_ctx, HDB);
974 krb5_set_error_string(context, "malloc: out of memory");
975 return NT_STATUS_NO_MEMORY;
978 (*db)->hdb_master_key_set = 0;
979 (*db)->hdb_db = NULL;
981 nt_status = auth_system_session_info(*db, &session_info);
982 if (!NT_STATUS_IS_OK(nt_status)) {
986 /* The idea here is very simple. Using Kerberos to
987 * authenticate the KDC to the LDAP server is higly likely to
990 * In future we may set this up to use EXERNAL and SSL
991 * certificates, for now it will almost certainly be NTLMSSP
994 cli_credentials_set_kerberos_state(session_info->credentials,
995 CRED_DONT_USE_KERBEROS);
997 /* Setup the link to LDB */
998 (*db)->hdb_db = samdb_connect(*db, session_info);
999 if ((*db)->hdb_db == NULL) {
1000 DEBUG(1, ("hdb_ldb_create: Cannot open samdb for KDC backend!"));
1001 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1004 (*db)->hdb_openp = 0;
1005 (*db)->hdb_open = LDB_open;
1006 (*db)->hdb_close = LDB_close;
1007 (*db)->hdb_fetch = LDB_fetch;
1008 (*db)->hdb_store = LDB_store;
1009 (*db)->hdb_remove = LDB_remove;
1010 (*db)->hdb_firstkey = LDB_firstkey;
1011 (*db)->hdb_nextkey = LDB_nextkey;
1012 (*db)->hdb_lock = LDB_lock;
1013 (*db)->hdb_unlock = LDB_unlock;
1014 (*db)->hdb_rename = LDB_rename;
1015 /* we don't implement these, as we are not a lockable database */
1016 (*db)->hdb__get = NULL;
1017 (*db)->hdb__put = NULL;
1018 /* kadmin should not be used for deletes - use other tools instead */
1019 (*db)->hdb__del = NULL;
1020 (*db)->hdb_destroy = LDB_destroy;
1022 return NT_STATUS_OK;
1025 krb5_error_code hdb_ldb_create(krb5_context context, struct HDB **db, const char *arg)
1028 /* The global kdc_mem_ctx, Disgusting, ugly hack, but it means one less private hook */
1029 nt_status = kdc_hdb_ldb_create(kdc_mem_ctx, context, db, arg);
1031 if (NT_STATUS_IS_OK(nt_status)) {