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 "system/iconv.h"
43 { HDB_LDB_ENT_TYPE_CLIENT, HDB_LDB_ENT_TYPE_SERVER,
44 HDB_LDB_ENT_TYPE_KRBTGT, HDB_LDB_ENT_TYPE_ANY };
46 static const char * const krb5_attrs[] = {
52 "servicePrincipalName",
78 "msDS-KeyVersionNumber",
82 static KerberosTime ldb_msg_find_krb5time_ldap_time(struct ldb_message *msg, const char *attr, KerberosTime default_val)
88 gentime = ldb_msg_find_string(msg, attr, NULL);
92 tmp = strptime(gentime, "%Y%m%d%H%M%SZ", &tm);
100 static HDBFlags uf2HDBFlags(krb5_context context, int userAccountControl, enum hdb_ldb_ent_type ent_type)
102 HDBFlags flags = int2HDBFlags(0);
104 krb5_warnx(context, "uf2HDBFlags: userAccountControl: %08x\n", userAccountControl);
106 /* we don't allow kadmin deletes */
109 /* mark the principal as invalid to start with */
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) {
132 if (ent_type == HDB_LDB_ENT_TYPE_SERVER || ent_type == HDB_LDB_ENT_TYPE_ANY) {
137 if (userAccountControl & UF_SERVER_TRUST_ACCOUNT) {
138 if (ent_type == HDB_LDB_ENT_TYPE_CLIENT || ent_type == HDB_LDB_ENT_TYPE_ANY) {
141 if (ent_type == HDB_LDB_ENT_TYPE_SERVER || ent_type == HDB_LDB_ENT_TYPE_ANY) {
147 if (userAccountControl & UF_ACCOUNTDISABLE) {
150 if (userAccountControl & UF_LOCKOUT) {
154 if (userAccountControl & UF_PASSWORD_NOTREQD) {
159 if (userAccountControl & UF_PASSWORD_CANT_CHANGE) {
164 if (userAccountControl & UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED) {
168 if (userAccountControl & UF_TEMP_DUPLICATE_ACCOUNT) {
172 /* UF_DONT_EXPIRE_PASSWD handled in LDB_message2entry() */
175 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
179 if (userAccountControl & UF_SMARTCARD_REQUIRED) {
180 flags.require_hwauth = 1;
182 if (flags.server && (userAccountControl & UF_TRUSTED_FOR_DELEGATION)) {
183 flags.forwardable = 1;
185 } else if (flags.client && (userAccountControl & UF_NOT_DELEGATED)) {
186 flags.forwardable = 0;
189 flags.forwardable = 1;
194 if (userAccountControl & UF_SMARTCARD_USE_DES_KEY_ONLY) {
198 if (userAccountControl & UF_DONT_REQUIRE_PREAUTH) {
199 flags.require_preauth = 0;
201 flags.require_preauth = 1;
205 krb5_warnx(context, "uf2HDBFlags: HDBFlags: %08x\n", HDBFlags2int(flags));
211 * Construct an hdb_entry from a directory entry.
213 static krb5_error_code LDB_message2entry(krb5_context context, HDB *db,
214 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
215 enum hdb_ldb_ent_type ent_type, struct ldb_message *realm_msg,
216 struct ldb_message *msg,
219 const char *unicodePwd;
220 int userAccountControl;
222 krb5_error_code ret = 0;
223 const char *dnsdomain = ldb_msg_find_string(realm_msg, "dnsDomain", NULL);
224 char *realm = strupper_talloc(mem_ctx, dnsdomain);
227 krb5_set_error_string(context, "talloc_strdup: out of memory");
232 krb5_warnx(context, "LDB_message2entry:\n");
234 memset(ent, 0, sizeof(*ent));
236 userAccountControl = ldb_msg_find_int(msg, "userAccountControl", 0);
238 ent->principal = malloc(sizeof(*(ent->principal)));
239 if (ent_type == HDB_LDB_ENT_TYPE_ANY && principal == NULL) {
240 const char *samAccountName = ldb_msg_find_string(msg, "samAccountName", NULL);
241 if (!samAccountName) {
242 krb5_set_error_string(context, "LDB_message2entry: no samAccountName present");
246 samAccountName = ldb_msg_find_string(msg, "samAccountName", NULL);
247 krb5_make_principal(context, &ent->principal, realm, samAccountName, NULL);
250 ret = copy_Principal(principal, ent->principal);
252 krb5_clear_error_string(context);
256 /* While we have copied the client principal, tests
257 * show that Win2k3 returns the 'corrected' realm, not
258 * the client-specified realm. This code attempts to
259 * replace the client principal's realm with the one
260 * we determine from our records */
263 free(*krb5_princ_realm(context, ent->principal));
265 /* this has to be with malloc() */
266 strdup_realm = strdup(realm);
269 krb5_clear_error_string(context);
272 krb5_princ_set_realm(context, ent->principal, &strdup_realm);
275 ent->kvno = ldb_msg_find_int(msg, "msDS-KeyVersionNumber", 0);
277 ent->flags = uf2HDBFlags(context, userAccountControl, ent_type);
279 if (ent_type == HDB_LDB_ENT_TYPE_KRBTGT) {
280 ent->flags.invalid = 0;
281 ent->flags.server = 1;
284 /* use 'whenCreated' */
285 ent->created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
287 ent->created_by.principal = NULL;
289 ent->modified_by = (Event *) malloc(sizeof(Event));
290 if (ent->modified_by == NULL) {
291 krb5_set_error_string(context, "malloc: out of memory");
296 /* use 'whenChanged' */
297 ent->modified_by->time = ldb_msg_find_krb5time_ldap_time(msg, "whenChanged", 0);
299 ent->modified_by->principal = NULL;
301 ent->valid_start = NULL;
303 ent->valid_end = NULL;
306 ent->max_life = NULL;
308 ent->max_renew = NULL;
310 ent->generation = NULL;
312 /* create the keys and enctypes */
313 unicodePwd = ldb_msg_find_string(msg, "unicodePwd", NULL);
315 /* Many, many thanks to lukeh@padl.com for this
316 * algorithm, described in his Nov 10 2004 mail to
317 * samba-technical@samba.org */
319 Principal *salt_principal;
320 const char *user_principal_name = ldb_msg_find_string(msg, "userPrincipalName", NULL);
321 struct ldb_message_element *objectclasses;
322 struct ldb_val computer_val;
323 computer_val.data = discard_const_p(uint8_t,"computer");
324 computer_val.length = strlen((const char *)computer_val.data);
326 objectclasses = ldb_msg_find_element(msg, "objectClass");
328 if (objectclasses && ldb_msg_find_val(objectclasses, &computer_val)) {
329 /* Determine a salting principal */
330 char *samAccountName = talloc_strdup(mem_ctx, ldb_msg_find_string(msg, "samAccountName", NULL));
332 if (!samAccountName) {
333 krb5_set_error_string(context, "LDB_message2entry: no samAccountName present");
337 if (samAccountName[strlen(samAccountName)-1] == '$') {
338 samAccountName[strlen(samAccountName)-1] = '\0';
340 saltbody = talloc_asprintf(mem_ctx, "%s.%s", samAccountName, dnsdomain);
342 ret = krb5_make_principal(context, &salt_principal, realm, "host", saltbody, NULL);
343 } else if (user_principal_name) {
345 user_principal_name = talloc_strdup(mem_ctx, user_principal_name);
346 if (!user_principal_name) {
350 p = strchr(user_principal_name, '@');
354 ret = krb5_make_principal(context, &salt_principal, realm, user_principal_name, NULL);
357 const char *samAccountName = ldb_msg_find_string(msg, "samAccountName", NULL);
358 ret = krb5_make_principal(context, &salt_principal, realm, samAccountName, NULL);
362 size_t num_keys = ent->keys.len;
364 * create keys from unicodePwd
366 ret = hdb_generate_key_set_password(context, salt_principal,
368 &ent->keys.val, &num_keys);
369 ent->keys.len = num_keys;
370 krb5_free_principal(context, salt_principal);
374 krb5_warnx(context, "could not generate keys from unicodePwd\n");
375 ent->keys.val = NULL;
380 const struct ldb_val *val;
383 val = ldb_msg_find_ldb_val(msg, "ntPwdHash");
385 krb5_warnx(context, "neither type of key available for this account\n");
386 ent->keys.val = NULL;
388 } else if (val->length < 16) {
389 ent->keys.val = NULL;
391 krb5_warnx(context, "ntPwdHash has invalid length: %d\n",
394 ret = krb5_data_alloc (&keyvalue, 16);
396 krb5_set_error_string(context, "malloc: out of memory");
401 memcpy(keyvalue.data, val->data, 16);
403 ent->keys.val = malloc(sizeof(ent->keys.val[0]));
404 if (ent->keys.val == NULL) {
405 krb5_data_free(&keyvalue);
406 krb5_set_error_string(context, "malloc: out of memory");
411 memset(&ent->keys.val[0], 0, sizeof(Key));
412 ent->keys.val[0].key.keytype = ETYPE_ARCFOUR_HMAC_MD5;
413 ent->keys.val[0].key.keyvalue = keyvalue;
420 ent->etypes = malloc(sizeof(*(ent->etypes)));
421 if (ent->etypes == NULL) {
422 krb5_set_error_string(context, "malloc: out of memory");
426 ent->etypes->len = ent->keys.len;
427 ent->etypes->val = calloc(ent->etypes->len, sizeof(int));
428 if (ent->etypes->val == NULL) {
429 krb5_set_error_string(context, "malloc: out of memory");
433 for (i=0; i < ent->etypes->len; i++) {
434 ent->etypes->val[i] = ent->keys.val[i].key.keytype;
439 /* I don't think this frees ent itself. */
440 hdb_free_entry(context, ent);
446 static krb5_error_code LDB_lookup_principal(krb5_context context, struct ldb_context *ldb_ctx,
448 krb5_const_principal principal,
449 enum hdb_ldb_ent_type ent_type,
450 const char *realm_dn,
451 struct ldb_message ***pmsg)
456 const char * const *princ_attrs = krb5_attrs;
460 char *princ_str_talloc;
463 struct ldb_message **msg = NULL;
465 /* Structure assignment, so we don't mess with the source parameter */
466 struct Principal princ = *principal;
468 /* Allow host/dns.name/realm@REALM, just convert into host/dns.name@REALM */
469 if (princ.name.name_string.len == 3
470 && StrCaseCmp(princ.name.name_string.val[2], princ.realm) == 0) {
471 princ.name.name_string.len = 2;
474 ret = krb5_unparse_name(context, &princ, &princ_str);
477 krb5_set_error_string(context, "LDB_lookup_principal: could not parse principal");
478 krb5_warnx(context, "LDB_lookup_principal: could not parse principal");
482 princ_str_talloc = talloc_strdup(mem_ctx, princ_str);
483 short_princ = talloc_strdup(mem_ctx, princ_str);
485 if (!short_princ || !princ_str_talloc) {
486 krb5_set_error_string(context, "LDB_lookup_principal: talloc_strdup() failed!");
490 p = strchr(short_princ, '@');
497 case HDB_LDB_ENT_TYPE_KRBTGT:
498 filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
501 case HDB_LDB_ENT_TYPE_CLIENT:
502 filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(|(samAccountName=%s)(userPrincipalName=%s)))",
503 short_princ, princ_str_talloc);
505 case HDB_LDB_ENT_TYPE_SERVER:
506 filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(|(samAccountName=%s)(servicePrincipalName=%s)))",
507 short_princ, short_princ);
509 case HDB_LDB_ENT_TYPE_ANY:
510 filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(|(|(samAccountName=%s)(servicePrincipalName=%s))(userPrincipalName=%s)))",
511 short_princ, short_princ, princ_str_talloc);
516 krb5_set_error_string(context, "talloc_asprintf: out of memory");
520 count = ldb_search(ldb_ctx, realm_dn, LDB_SCOPE_SUBTREE, filter,
524 krb5_warnx(context, "ldb_search: basedn: '%s' filter: '%s' failed: %d",
525 realm_dn, filter, count);
526 krb5_set_error_string(context, "ldb_search: basedn: '%s' filter: '%s' failed: %d",
527 realm_dn, filter, count);
528 return HDB_ERR_NOENTRY;
529 } else if (count > 1) {
531 krb5_warnx(context, "ldb_search: basedn: '%s' filter: '%s' more than 1 entry: %d",
532 realm_dn, filter, count);
533 krb5_set_error_string(context, "ldb_search: basedn: '%s' filter: '%s' more than 1 entry: %d",
534 realm_dn, filter, count);
535 return HDB_ERR_NOENTRY;
537 *pmsg = talloc_steal(mem_ctx, msg);
541 static krb5_error_code LDB_lookup_realm(krb5_context context, struct ldb_context *ldb_ctx,
544 struct ldb_message ***pmsg)
547 const char *realm_dn;
548 char *cross_ref_filter;
549 struct ldb_message **cross_ref_msg;
550 struct ldb_message **msg;
552 const char *cross_ref_attrs[] = {
557 const char *realm_attrs[] = {
563 cross_ref_filter = talloc_asprintf(mem_ctx,
564 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
566 if (!cross_ref_filter) {
567 krb5_set_error_string(context, "asprintf: out of memory");
571 count = ldb_search(ldb_ctx, NULL, LDB_SCOPE_SUBTREE, cross_ref_filter,
572 cross_ref_attrs, &cross_ref_msg);
575 krb5_warnx(context, "ldb_search: filter: '%s' failed: %d", cross_ref_filter, count);
576 krb5_set_error_string(context, "ldb_search: filter: '%s' failed: %d", cross_ref_filter, count);
578 talloc_free(cross_ref_msg);
579 return HDB_ERR_NOENTRY;
580 } else if (count > 1) {
581 krb5_warnx(context, "ldb_search: filter: '%s' more than 1 entry: %d", cross_ref_filter, count);
582 krb5_set_error_string(context, "ldb_search: filter: '%s' more than 1 entry: %d", cross_ref_filter, count);
584 talloc_free(cross_ref_msg);
585 return HDB_ERR_NOENTRY;
588 realm_dn = ldb_msg_find_string(cross_ref_msg[0], "nCName", NULL);
590 count = ldb_search(ldb_ctx, realm_dn, LDB_SCOPE_BASE, "(objectClass=domain)",
593 *pmsg = talloc_steal(mem_ctx, msg);
599 krb5_warnx(context, "ldb_search: dn: %s not found: %d", realm_dn, count);
600 krb5_set_error_string(context, "ldb_search: dn: %s not found: %d", realm_dn, count);
601 return HDB_ERR_NOENTRY;
602 } else if (count > 1) {
603 krb5_warnx(context, "ldb_search: dn: '%s' more than 1 entry: %d", realm_dn, count);
604 krb5_set_error_string(context, "ldb_search: dn: %s more than 1 entry: %d", realm_dn, count);
605 return HDB_ERR_NOENTRY;
611 static krb5_error_code LDB_lookup_spn_alias(krb5_context context, struct ldb_context *ldb_ctx,
613 const char *realm_dn,
614 const char *alias_from,
619 struct ldb_message **msg;
620 struct ldb_message_element *spnmappings;
621 char *service_dn = talloc_asprintf(mem_ctx,
622 "CN=Directory Service,CN=Windows NT,CN=Services,CN=Configuration,%s",
624 const char *directory_attrs[] = {
629 count = ldb_search(ldb_ctx, service_dn, LDB_SCOPE_BASE, "(objectClass=nTDSService)",
630 directory_attrs, &msg);
631 talloc_steal(mem_ctx, msg);
634 krb5_warnx(context, "ldb_search: dn: %s not found: %d", service_dn, count);
635 krb5_set_error_string(context, "ldb_search: dn: %s not found: %d", service_dn, count);
636 return HDB_ERR_NOENTRY;
637 } else if (count > 1) {
638 krb5_warnx(context, "ldb_search: dn: %s found %d times!", service_dn, count);
639 krb5_set_error_string(context, "ldb_search: dn: %s found %d times!", service_dn, count);
640 return HDB_ERR_NOENTRY;
643 spnmappings = ldb_msg_find_element(msg[0], "sPNMappings");
644 if (!spnmappings || spnmappings->num_values == 0) {
645 krb5_warnx(context, "ldb_search: dn: %s no sPNMappings attribute", service_dn);
646 krb5_set_error_string(context, "ldb_search: dn: %s no sPNMappings attribute", service_dn);
649 for (i = 0; i < spnmappings->num_values; i++) {
650 char *mapping, *p, *str;
651 mapping = talloc_strdup(mem_ctx,
652 (const char *)spnmappings->values[i].data);
654 krb5_warnx(context, "LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping", service_dn);
655 krb5_set_error_string(context, "LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping", service_dn);
656 return HDB_ERR_NOENTRY;
659 /* C string manipulation sucks */
661 p = strchr(mapping, '=');
663 krb5_warnx(context, "ldb_search: dn: %s sPNMapping malformed: %s",
664 service_dn, mapping);
665 krb5_set_error_string(context, "ldb_search: dn: %s sPNMapping malformed: %s",
666 service_dn, mapping);
677 if (strcasecmp(str, alias_from) == 0) {
683 krb5_warnx(context, "LDB_lookup_spn_alias: no alias for service %s applicable", alias_from);
684 return HDB_ERR_NOENTRY;
687 static krb5_error_code LDB_open(krb5_context context, HDB *db, int flags, mode_t mode)
689 if (db->hdb_master_key_set) {
690 krb5_warnx(context, "LDB_open: use of a master key incompatible with LDB\n");
691 krb5_set_error_string(context, "LDB_open: use of a master key incompatible with LDB\n");
692 return HDB_ERR_NOENTRY;
698 static krb5_error_code LDB_close(krb5_context context, HDB *db)
703 static krb5_error_code LDB_lock(krb5_context context, HDB *db, int operation)
708 static krb5_error_code LDB_unlock(krb5_context context, HDB *db)
713 static krb5_error_code LDB_rename(krb5_context context, HDB *db, const char *new_name)
715 return HDB_ERR_DB_INUSE;
718 static krb5_error_code LDB_fetch(krb5_context context, HDB *db, unsigned flags,
719 krb5_const_principal principal,
720 enum hdb_ent_type ent_type,
723 struct ldb_message **msg = NULL;
724 struct ldb_message **realm_msg = NULL;
725 struct ldb_message **realm_fixed_msg = NULL;
726 enum hdb_ldb_ent_type ldb_ent_type;
730 const char *realm_dn;
731 TALLOC_CTX *mem_ctx = talloc_named(NULL, 0, "LDB_fetch context");
734 krb5_set_error_string(context, "LDB_fetch: talloc_named() failed!");
738 realm = krb5_principal_get_realm(context, principal);
740 ret = LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
741 mem_ctx, realm, &realm_msg);
743 krb5_warnx(context, "LDB_fetch: could not find realm");
744 talloc_free(mem_ctx);
745 return HDB_ERR_NOENTRY;
748 realm_dn = realm_msg[0]->dn;
750 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
751 * is in our db, then direct the caller at our primary
755 case HDB_ENT_TYPE_SERVER:
756 if (principal->name.name_string.len == 2
757 && (strcmp(principal->name.name_string.val[0], KRB5_TGS_NAME) == 0)
758 && (LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
759 mem_ctx, principal->name.name_string.val[1], &realm_fixed_msg) == 0)) {
760 const char *dnsdomain = ldb_msg_find_string(realm_fixed_msg[0], "dnsDomain", NULL);
761 char *realm_fixed = strupper_talloc(mem_ctx, dnsdomain);
763 krb5_set_error_string(context, "strupper_talloc: out of memory");
764 talloc_free(mem_ctx);
768 free(principal->name.name_string.val[1]);
769 principal->name.name_string.val[1] = strdup(realm_fixed);
770 talloc_free(realm_fixed);
771 if (!principal->name.name_string.val[1]) {
772 krb5_set_error_string(context, "LDB_fetch: strdup() failed!");
773 talloc_free(mem_ctx);
776 ldb_ent_type = HDB_LDB_ENT_TYPE_KRBTGT;
778 ldb_ent_type = HDB_LDB_ENT_TYPE_SERVER;
781 case HDB_ENT_TYPE_CLIENT:
782 ldb_ent_type = HDB_LDB_ENT_TYPE_CLIENT;
784 case HDB_ENT_TYPE_ANY:
785 ldb_ent_type = HDB_LDB_ENT_TYPE_ANY;
788 krb5_warnx(context, "LDB_fetch: invalid ent_type specified!");
789 talloc_free(mem_ctx);
790 return HDB_ERR_NOENTRY;
793 ret = LDB_lookup_principal(context, (struct ldb_context *)db->hdb_db,
795 principal, ldb_ent_type, realm_dn, &msg);
798 char *alias_from = principal->name.name_string.val[0];
800 Principal alias_principal;
802 /* Try again with a servicePrincipal alias */
803 if (ent_type != HDB_LDB_ENT_TYPE_SERVER && ent_type != HDB_LDB_ENT_TYPE_ANY) {
804 talloc_free(mem_ctx);
807 if (principal->name.name_string.len < 2) {
808 krb5_warnx(context, "LDB_fetch: could not find principal in DB, alias not applicable");
809 krb5_set_error_string(context, "LDB_fetch: could not find principal in DB, alias not applicable");
810 talloc_free(mem_ctx);
814 /* Look for the list of aliases */
815 ret = LDB_lookup_spn_alias(context,
816 (struct ldb_context *)db->hdb_db, mem_ctx,
817 realm_dn, alias_from,
820 talloc_free(mem_ctx);
824 ret = copy_Principal(principal, &alias_principal);
826 krb5_warnx(context, "LDB_fetch: could not copy principal");
827 krb5_set_error_string(context, "LDB_fetch: could not copy principal");
828 talloc_free(mem_ctx);
832 /* ooh, very nasty playing around in the Principal... */
833 free(alias_principal.name.name_string.val[0]);
834 alias_principal.name.name_string.val[0] = strdup(alias_to);
835 if (!alias_principal.name.name_string.val[0]) {
836 krb5_warnx(context, "LDB_fetch: strdup() failed");
837 krb5_set_error_string(context, "LDB_fetch: strdup() failed");
839 talloc_free(mem_ctx);
840 free_Principal(&alias_principal);
844 ret = LDB_lookup_principal(context, (struct ldb_context *)db->hdb_db,
846 &alias_principal, ent_type, realm_dn, &msg);
847 free_Principal(&alias_principal);
850 krb5_warnx(context, "LDB_fetch: could not find alias principal in DB");
851 krb5_set_error_string(context, "LDB_fetch: could not find alias principal in DB");
852 talloc_free(mem_ctx);
858 ret = LDB_message2entry(context, db, mem_ctx,
859 principal, ldb_ent_type,
860 realm_msg[0], msg[0], entry);
862 krb5_warnx(context, "LDB_fetch: message2entry failed\n");
866 talloc_free(mem_ctx);
870 static krb5_error_code LDB_store(krb5_context context, HDB *db, unsigned flags, hdb_entry *entry)
872 return HDB_ERR_DB_INUSE;
875 static krb5_error_code LDB_remove(krb5_context context, HDB *db, hdb_entry *entry)
877 return HDB_ERR_DB_INUSE;
881 struct ldb_context *ctx;
884 struct ldb_message **msgs;
885 struct ldb_message **realm_msgs;
888 static krb5_error_code LDB_seq(krb5_context context, HDB *db, unsigned flags, hdb_entry *entry)
891 struct hdb_ldb_seq *priv = (struct hdb_ldb_seq *)db->hdb_openp;
894 return HDB_ERR_NOENTRY;
897 mem_ctx = talloc_named(priv, 0, "LDB_seq context");
900 krb5_set_error_string(context, "LDB_seq: talloc_named() failed!");
904 if (priv->index < priv->count) {
905 ret = LDB_message2entry(context, db, mem_ctx,
906 NULL, HDB_LDB_ENT_TYPE_ANY,
907 priv->realm_msgs[0], priv->msgs[priv->index++], entry);
909 ret = HDB_ERR_NOENTRY;
914 db->hdb_openp = NULL;
916 talloc_free(mem_ctx);
922 static krb5_error_code LDB_firstkey(krb5_context context, HDB *db, unsigned flags,
925 struct ldb_context *ldb_ctx = (struct ldb_context *)db->hdb_db;
926 struct hdb_ldb_seq *priv = (struct hdb_ldb_seq *)db->hdb_openp;
928 char *realm_dn = NULL;
929 struct ldb_message **msgs = NULL;
930 struct ldb_message **realm_msgs = NULL;
939 priv = (struct hdb_ldb_seq *) talloc(db, struct hdb_ldb_seq);
941 krb5_set_error_string(context, "talloc: out of memory");
948 priv->realm_msgs = NULL;
951 mem_ctx = talloc_named(priv, 0, "LDB_firstkey context");
954 krb5_set_error_string(context, "LDB_firstkey: talloc_named() failed!");
958 ret = krb5_get_default_realm(context, &realm);
964 ret = LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
965 mem_ctx, realm, &realm_msgs);
971 krb5_warnx(context, "LDB_firstkey: could not find realm\n");
972 return HDB_ERR_NOENTRY;
975 realm_dn = realm_msgs[0]->dn;
977 priv->realm_msgs = talloc_steal(priv, realm_msgs);
979 krb5_warnx(context, "LDB_firstkey: realm ok\n");
981 priv->count = ldb_search(ldb_ctx, realm_dn,
982 LDB_SCOPE_SUBTREE, "(objectClass=user)",
985 priv->msgs = talloc_steal(priv, msgs);
987 if (priv->count <= 0) {
989 return HDB_ERR_NOENTRY;
992 db->hdb_openp = priv;
994 ret = LDB_seq(context, db, flags, entry);
998 db->hdb_openp = NULL;
1000 talloc_free(mem_ctx);
1005 static krb5_error_code LDB_nextkey(krb5_context context, HDB *db, unsigned flags,
1008 return LDB_seq(context, db, flags, entry);
1011 static krb5_error_code LDB_destroy(krb5_context context, HDB *db)
1017 krb5_error_code hdb_ldb_create(TALLOC_CTX *mem_ctx,
1018 krb5_context context, struct HDB **db, const char *arg)
1020 *db = talloc(mem_ctx, HDB);
1022 krb5_set_error_string(context, "malloc: out of memory");
1026 (*db)->hdb_master_key_set = 0;
1027 (*db)->hdb_db = NULL;
1029 /* Setup the link to LDB */
1030 (*db)->hdb_db = samdb_connect(db);
1031 if ((*db)->hdb_db == NULL) {
1032 krb5_warnx(context, "hdb_ldb_create: samdb_connect failed!");
1033 krb5_set_error_string(context, "samdb_connect failed!");
1035 return HDB_ERR_NOENTRY;
1038 (*db)->hdb_openp = 0;
1039 (*db)->hdb_open = LDB_open;
1040 (*db)->hdb_close = LDB_close;
1041 (*db)->hdb_fetch = LDB_fetch;
1042 (*db)->hdb_store = LDB_store;
1043 (*db)->hdb_remove = LDB_remove;
1044 (*db)->hdb_firstkey = LDB_firstkey;
1045 (*db)->hdb_nextkey = LDB_nextkey;
1046 (*db)->hdb_lock = LDB_lock;
1047 (*db)->hdb_unlock = LDB_unlock;
1048 (*db)->hdb_rename = LDB_rename;
1049 /* we don't implement these, as we are not a lockable database */
1050 (*db)->hdb__get = NULL;
1051 (*db)->hdb__put = NULL;
1052 /* kadmin should not be used for deletes - use other tools instead */
1053 (*db)->hdb__del = NULL;
1054 (*db)->hdb_destroy = LDB_destroy;