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) {
119 if (ent_type == HDB_LDB_ENT_TYPE_SERVER || ent_type == HDB_LDB_ENT_TYPE_ANY) {
125 if (userAccountControl & UF_INTERDOMAIN_TRUST_ACCOUNT) {
126 if (ent_type == HDB_LDB_ENT_TYPE_CLIENT || ent_type == HDB_LDB_ENT_TYPE_ANY) {
131 if (userAccountControl & UF_WORKSTATION_TRUST_ACCOUNT) {
132 if (ent_type == HDB_LDB_ENT_TYPE_CLIENT || ent_type == HDB_LDB_ENT_TYPE_ANY) {
135 if (ent_type == HDB_LDB_ENT_TYPE_SERVER || ent_type == HDB_LDB_ENT_TYPE_ANY) {
140 if (userAccountControl & UF_SERVER_TRUST_ACCOUNT) {
141 if (ent_type == HDB_LDB_ENT_TYPE_CLIENT || ent_type == HDB_LDB_ENT_TYPE_ANY) {
144 if (ent_type == HDB_LDB_ENT_TYPE_SERVER || ent_type == HDB_LDB_ENT_TYPE_ANY) {
150 /* Not permitted to act as a client if disabled */
151 if (userAccountControl & UF_ACCOUNTDISABLE) {
154 if (userAccountControl & UF_LOCKOUT) {
158 if (userAccountControl & UF_PASSWORD_NOTREQD) {
163 if (userAccountControl & UF_PASSWORD_CANT_CHANGE) {
168 if (userAccountControl & UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED) {
172 if (userAccountControl & UF_TEMP_DUPLICATE_ACCOUNT) {
176 /* UF_DONT_EXPIRE_PASSWD handled in LDB_message2entry() */
179 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
183 if (userAccountControl & UF_SMARTCARD_REQUIRED) {
184 flags.require_hwauth = 1;
186 if (flags.server && (userAccountControl & UF_TRUSTED_FOR_DELEGATION)) {
187 flags.forwardable = 1;
189 } else if (flags.client && (userAccountControl & UF_NOT_DELEGATED)) {
190 flags.forwardable = 0;
193 flags.forwardable = 1;
198 if (userAccountControl & UF_SMARTCARD_USE_DES_KEY_ONLY) {
202 if (userAccountControl & UF_DONT_REQUIRE_PREAUTH) {
203 flags.require_preauth = 0;
205 flags.require_preauth = 1;
209 krb5_warnx(context, "uf2HDBFlags: HDBFlags: %08x\n", HDBFlags2int(flags));
215 * Construct an hdb_entry from a directory entry.
217 static krb5_error_code LDB_message2entry(krb5_context context, HDB *db,
218 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
219 enum hdb_ldb_ent_type ent_type, struct ldb_message *realm_msg,
220 struct ldb_message *msg,
223 const char *unicodePwd;
224 int userAccountControl;
226 krb5_error_code ret = 0;
227 const char *dnsdomain = ldb_msg_find_string(realm_msg, "dnsDomain", NULL);
228 char *realm = strupper_talloc(mem_ctx, dnsdomain);
231 krb5_set_error_string(context, "talloc_strdup: out of memory");
236 krb5_warnx(context, "LDB_message2entry:\n");
238 memset(ent, 0, sizeof(*ent));
240 userAccountControl = ldb_msg_find_int(msg, "userAccountControl", 0);
242 ent->principal = malloc(sizeof(*(ent->principal)));
243 if (ent_type == HDB_LDB_ENT_TYPE_ANY && principal == NULL) {
244 const char *samAccountName = ldb_msg_find_string(msg, "samAccountName", NULL);
245 if (!samAccountName) {
246 krb5_set_error_string(context, "LDB_message2entry: no samAccountName present");
250 samAccountName = ldb_msg_find_string(msg, "samAccountName", NULL);
251 krb5_make_principal(context, &ent->principal, realm, samAccountName, NULL);
254 ret = copy_Principal(principal, ent->principal);
256 krb5_clear_error_string(context);
260 /* While we have copied the client principal, tests
261 * show that Win2k3 returns the 'corrected' realm, not
262 * the client-specified realm. This code attempts to
263 * replace the client principal's realm with the one
264 * we determine from our records */
267 free(*krb5_princ_realm(context, ent->principal));
269 /* this has to be with malloc() */
270 strdup_realm = strdup(realm);
273 krb5_clear_error_string(context);
276 krb5_princ_set_realm(context, ent->principal, &strdup_realm);
279 ent->kvno = ldb_msg_find_int(msg, "msDS-KeyVersionNumber", 0);
281 ent->flags = uf2HDBFlags(context, userAccountControl, ent_type);
283 if (ent_type == HDB_LDB_ENT_TYPE_KRBTGT) {
284 ent->flags.invalid = 0;
285 ent->flags.server = 1;
288 /* use 'whenCreated' */
289 ent->created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
291 ent->created_by.principal = NULL;
293 ent->modified_by = (Event *) malloc(sizeof(Event));
294 if (ent->modified_by == NULL) {
295 krb5_set_error_string(context, "malloc: out of memory");
300 /* use 'whenChanged' */
301 ent->modified_by->time = ldb_msg_find_krb5time_ldap_time(msg, "whenChanged", 0);
303 ent->modified_by->principal = NULL;
305 ent->valid_start = NULL;
307 ent->valid_end = NULL;
310 ent->max_life = NULL;
312 ent->max_renew = NULL;
314 ent->generation = NULL;
316 /* create the keys and enctypes */
317 unicodePwd = ldb_msg_find_string(msg, "unicodePwd", NULL);
319 /* Many, many thanks to lukeh@padl.com for this
320 * algorithm, described in his Nov 10 2004 mail to
321 * samba-technical@samba.org */
323 Principal *salt_principal;
324 const char *user_principal_name = ldb_msg_find_string(msg, "userPrincipalName", NULL);
325 struct ldb_message_element *objectclasses;
326 struct ldb_val computer_val;
327 computer_val.data = discard_const_p(uint8_t,"computer");
328 computer_val.length = strlen((const char *)computer_val.data);
330 objectclasses = ldb_msg_find_element(msg, "objectClass");
332 if (objectclasses && ldb_msg_find_val(objectclasses, &computer_val)) {
333 /* Determine a salting principal */
334 char *samAccountName = talloc_strdup(mem_ctx, ldb_msg_find_string(msg, "samAccountName", NULL));
336 if (!samAccountName) {
337 krb5_set_error_string(context, "LDB_message2entry: no samAccountName present");
341 if (samAccountName[strlen(samAccountName)-1] == '$') {
342 samAccountName[strlen(samAccountName)-1] = '\0';
344 saltbody = talloc_asprintf(mem_ctx, "%s.%s", samAccountName, dnsdomain);
346 ret = krb5_make_principal(context, &salt_principal, realm, "host", saltbody, NULL);
347 } else if (user_principal_name) {
349 user_principal_name = talloc_strdup(mem_ctx, user_principal_name);
350 if (!user_principal_name) {
354 p = strchr(user_principal_name, '@');
358 ret = krb5_make_principal(context, &salt_principal, realm, user_principal_name, NULL);
361 const char *samAccountName = ldb_msg_find_string(msg, "samAccountName", NULL);
362 ret = krb5_make_principal(context, &salt_principal, realm, samAccountName, NULL);
366 size_t num_keys = ent->keys.len;
368 * create keys from unicodePwd
370 ret = hdb_generate_key_set_password(context, salt_principal,
372 &ent->keys.val, &num_keys);
373 ent->keys.len = num_keys;
374 krb5_free_principal(context, salt_principal);
378 krb5_warnx(context, "could not generate keys from unicodePwd\n");
379 ent->keys.val = NULL;
384 const struct ldb_val *val;
387 val = ldb_msg_find_ldb_val(msg, "ntPwdHash");
389 krb5_warnx(context, "neither type of key available for this account\n");
390 ent->keys.val = NULL;
392 } else if (val->length < 16) {
393 ent->keys.val = NULL;
395 krb5_warnx(context, "ntPwdHash has invalid length: %d\n",
398 ret = krb5_data_alloc (&keyvalue, 16);
400 krb5_set_error_string(context, "malloc: out of memory");
405 memcpy(keyvalue.data, val->data, 16);
407 ent->keys.val = malloc(sizeof(ent->keys.val[0]));
408 if (ent->keys.val == NULL) {
409 krb5_data_free(&keyvalue);
410 krb5_set_error_string(context, "malloc: out of memory");
415 memset(&ent->keys.val[0], 0, sizeof(Key));
416 ent->keys.val[0].key.keytype = ETYPE_ARCFOUR_HMAC_MD5;
417 ent->keys.val[0].key.keyvalue = keyvalue;
424 ent->etypes = malloc(sizeof(*(ent->etypes)));
425 if (ent->etypes == NULL) {
426 krb5_set_error_string(context, "malloc: out of memory");
430 ent->etypes->len = ent->keys.len;
431 ent->etypes->val = calloc(ent->etypes->len, sizeof(int));
432 if (ent->etypes->val == NULL) {
433 krb5_set_error_string(context, "malloc: out of memory");
437 for (i=0; i < ent->etypes->len; i++) {
438 ent->etypes->val[i] = ent->keys.val[i].key.keytype;
443 /* I don't think this frees ent itself. */
444 hdb_free_entry(context, ent);
450 static krb5_error_code LDB_lookup_principal(krb5_context context, struct ldb_context *ldb_ctx,
452 krb5_const_principal principal,
453 enum hdb_ldb_ent_type ent_type,
454 const struct ldb_dn *realm_dn,
455 struct ldb_message ***pmsg)
460 const char * const *princ_attrs = krb5_attrs;
463 char *princ_str_talloc;
465 char *short_princ_talloc;
469 struct ldb_message **msg = NULL;
471 /* Structure assignment, so we don't mess with the source parameter */
472 struct Principal princ = *principal;
474 /* Allow host/dns.name/realm@REALM, just convert into host/dns.name@REALM */
475 if (princ.name.name_string.len == 3
476 && strcasecmp_m(princ.name.name_string.val[2], princ.realm) == 0) {
477 princ.name.name_string.len = 2;
480 ret = krb5_unparse_name(context, &princ, &princ_str);
483 krb5_set_error_string(context, "LDB_lookup_principal: could not parse principal");
484 krb5_warnx(context, "LDB_lookup_principal: could not parse principal");
488 ret = krb5_unparse_name_norealm(context, &princ, &short_princ);
492 krb5_set_error_string(context, "LDB_lookup_principal: could not parse principal");
493 krb5_warnx(context, "LDB_lookup_principal: could not parse principal");
497 princ_str_talloc = talloc_strdup(mem_ctx, princ_str);
498 short_princ_talloc = talloc_strdup(mem_ctx, short_princ);
501 if (!short_princ || !princ_str_talloc) {
502 krb5_set_error_string(context, "LDB_lookup_principal: talloc_strdup() failed!");
507 case HDB_LDB_ENT_TYPE_KRBTGT:
508 filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
511 case HDB_LDB_ENT_TYPE_CLIENT:
512 filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(|(samAccountName=%s)(userPrincipalName=%s)))",
513 short_princ_talloc, princ_str_talloc);
515 case HDB_LDB_ENT_TYPE_SERVER:
516 filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(|(samAccountName=%s)(servicePrincipalName=%s)))",
517 short_princ_talloc, short_princ_talloc);
519 case HDB_LDB_ENT_TYPE_ANY:
520 filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(|(|(samAccountName=%s)(servicePrincipalName=%s))(userPrincipalName=%s)))",
521 short_princ_talloc, short_princ_talloc, princ_str_talloc);
526 krb5_set_error_string(context, "talloc_asprintf: out of memory");
530 count = ldb_search(ldb_ctx, realm_dn, LDB_SCOPE_SUBTREE, filter,
533 realm_dn_str = ldb_dn_linearize(mem_ctx, realm_dn);
536 krb5_warnx(context, "ldb_search: basedn: '%s' filter: '%s' failed: %d",
537 realm_dn_str, filter, count);
538 krb5_set_error_string(context, "ldb_search: basedn: '%s' filter: '%s' failed: %d",
539 realm_dn_str, filter, count);
540 return HDB_ERR_NOENTRY;
541 } else if (count > 1) {
543 krb5_warnx(context, "ldb_search: basedn: '%s' filter: '%s' more than 1 entry: %d",
544 realm_dn_str, filter, count);
545 krb5_set_error_string(context, "ldb_search: basedn: '%s' filter: '%s' more than 1 entry: %d",
546 realm_dn_str, filter, count);
547 return HDB_ERR_NOENTRY;
549 *pmsg = talloc_steal(mem_ctx, msg);
553 static krb5_error_code LDB_lookup_realm(krb5_context context, struct ldb_context *ldb_ctx,
556 struct ldb_message ***pmsg)
559 struct ldb_dn *realm_dn;
560 const char *realm_dn_str;
561 char *cross_ref_filter;
562 struct ldb_message **cross_ref_msg;
563 struct ldb_message **msg;
565 const char *cross_ref_attrs[] = {
570 const char *realm_attrs[] = {
576 cross_ref_filter = talloc_asprintf(mem_ctx,
577 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
579 if (!cross_ref_filter) {
580 krb5_set_error_string(context, "asprintf: out of memory");
584 count = ldb_search(ldb_ctx, NULL, LDB_SCOPE_SUBTREE, cross_ref_filter,
585 cross_ref_attrs, &cross_ref_msg);
588 krb5_warnx(context, "ldb_search: filter: '%s' failed: %d", cross_ref_filter, count);
589 krb5_set_error_string(context, "ldb_search: filter: '%s' failed: %d", cross_ref_filter, count);
591 talloc_free(cross_ref_msg);
592 return HDB_ERR_NOENTRY;
593 } else if (count > 1) {
594 krb5_warnx(context, "ldb_search: filter: '%s' more than 1 entry: %d", cross_ref_filter, count);
595 krb5_set_error_string(context, "ldb_search: filter: '%s' more than 1 entry: %d", cross_ref_filter, count);
597 talloc_free(cross_ref_msg);
598 return HDB_ERR_NOENTRY;
601 realm_dn_str = ldb_msg_find_string(cross_ref_msg[0], "nCName", NULL);
602 realm_dn = ldb_dn_explode(mem_ctx, realm_dn_str);
604 count = ldb_search(ldb_ctx, realm_dn, LDB_SCOPE_BASE, "(objectClass=domain)",
607 *pmsg = talloc_steal(mem_ctx, msg);
613 krb5_warnx(context, "ldb_search: dn: %s not found: %d", realm_dn_str, count);
614 krb5_set_error_string(context, "ldb_search: dn: %s not found: %d", realm_dn_str, count);
615 return HDB_ERR_NOENTRY;
616 } else if (count > 1) {
617 krb5_warnx(context, "ldb_search: dn: '%s' more than 1 entry: %d", realm_dn_str, count);
618 krb5_set_error_string(context, "ldb_search: dn: %s more than 1 entry: %d", realm_dn_str, count);
619 return HDB_ERR_NOENTRY;
625 static krb5_error_code LDB_lookup_spn_alias(krb5_context context, struct ldb_context *ldb_ctx,
627 const struct ldb_dn *realm_dn,
628 const char *alias_from,
633 struct ldb_message **msg;
634 struct ldb_message_element *spnmappings;
635 struct ldb_dn *service_dn = ldb_dn_string_compose(mem_ctx, realm_dn,
636 "CN=Directory Service,CN=Windows NT"
637 ",CN=Services,CN=Configuration");
638 char *service_dn_str = ldb_dn_linearize(mem_ctx, service_dn);
639 const char *directory_attrs[] = {
644 count = ldb_search(ldb_ctx, service_dn, LDB_SCOPE_BASE, "(objectClass=nTDSService)",
645 directory_attrs, &msg);
646 talloc_steal(mem_ctx, msg);
649 krb5_warnx(context, "ldb_search: dn: %s not found: %d", service_dn_str, count);
650 krb5_set_error_string(context, "ldb_search: dn: %s not found: %d", service_dn_str, count);
651 return HDB_ERR_NOENTRY;
652 } else if (count > 1) {
653 krb5_warnx(context, "ldb_search: dn: %s found %d times!", service_dn_str, count);
654 krb5_set_error_string(context, "ldb_search: dn: %s found %d times!", service_dn_str, count);
655 return HDB_ERR_NOENTRY;
658 spnmappings = ldb_msg_find_element(msg[0], "sPNMappings");
659 if (!spnmappings || spnmappings->num_values == 0) {
660 krb5_warnx(context, "ldb_search: dn: %s no sPNMappings attribute", service_dn_str);
661 krb5_set_error_string(context, "ldb_search: dn: %s no sPNMappings attribute", service_dn_str);
662 return HDB_ERR_NOENTRY;
665 for (i = 0; i < spnmappings->num_values; i++) {
666 char *mapping, *p, *str;
667 mapping = talloc_strdup(mem_ctx,
668 (const char *)spnmappings->values[i].data);
670 krb5_warnx(context, "LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping", service_dn_str);
671 krb5_set_error_string(context, "LDB_lookup_spn_alias: ldb_search: dn: %s did not have an sPNMapping", service_dn_str);
672 return HDB_ERR_NOENTRY;
675 /* C string manipulation sucks */
677 p = strchr(mapping, '=');
679 krb5_warnx(context, "ldb_search: dn: %s sPNMapping malformed: %s",
680 service_dn_str, mapping);
681 krb5_set_error_string(context, "ldb_search: dn: %s sPNMapping malformed: %s",
682 service_dn_str, mapping);
683 return HDB_ERR_NOENTRY;
694 if (strcasecmp(str, alias_from) == 0) {
700 krb5_warnx(context, "LDB_lookup_spn_alias: no alias for service %s applicable", alias_from);
701 return HDB_ERR_NOENTRY;
704 static krb5_error_code LDB_open(krb5_context context, HDB *db, int flags, mode_t mode)
706 if (db->hdb_master_key_set) {
707 krb5_warnx(context, "LDB_open: use of a master key incompatible with LDB\n");
708 krb5_set_error_string(context, "LDB_open: use of a master key incompatible with LDB\n");
709 return HDB_ERR_NOENTRY;
715 static krb5_error_code LDB_close(krb5_context context, HDB *db)
720 static krb5_error_code LDB_lock(krb5_context context, HDB *db, int operation)
725 static krb5_error_code LDB_unlock(krb5_context context, HDB *db)
730 static krb5_error_code LDB_rename(krb5_context context, HDB *db, const char *new_name)
732 return HDB_ERR_DB_INUSE;
735 static krb5_error_code LDB_fetch(krb5_context context, HDB *db, unsigned flags,
736 krb5_const_principal principal,
737 enum hdb_ent_type ent_type,
740 struct ldb_message **msg = NULL;
741 struct ldb_message **realm_msg = NULL;
742 struct ldb_message **realm_fixed_msg = NULL;
743 enum hdb_ldb_ent_type ldb_ent_type;
747 const struct ldb_dn *realm_dn;
748 TALLOC_CTX *mem_ctx = talloc_named(NULL, 0, "LDB_fetch context");
751 krb5_set_error_string(context, "LDB_fetch: talloc_named() failed!");
755 realm = krb5_principal_get_realm(context, principal);
757 ret = LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
758 mem_ctx, realm, &realm_msg);
760 krb5_warnx(context, "LDB_fetch: could not find realm");
761 talloc_free(mem_ctx);
762 return HDB_ERR_NOENTRY;
765 realm_dn = realm_msg[0]->dn;
767 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
768 * is in our db, then direct the caller at our primary
772 case HDB_ENT_TYPE_SERVER:
773 if (principal->name.name_string.len == 2
774 && (strcmp(principal->name.name_string.val[0], KRB5_TGS_NAME) == 0)
775 && (LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
776 mem_ctx, principal->name.name_string.val[1], &realm_fixed_msg) == 0)) {
777 const char *dnsdomain = ldb_msg_find_string(realm_fixed_msg[0], "dnsDomain", NULL);
778 char *realm_fixed = strupper_talloc(mem_ctx, dnsdomain);
780 krb5_set_error_string(context, "strupper_talloc: out of memory");
781 talloc_free(mem_ctx);
785 free(principal->name.name_string.val[1]);
786 principal->name.name_string.val[1] = strdup(realm_fixed);
787 talloc_free(realm_fixed);
788 if (!principal->name.name_string.val[1]) {
789 krb5_set_error_string(context, "LDB_fetch: strdup() failed!");
790 talloc_free(mem_ctx);
793 ldb_ent_type = HDB_LDB_ENT_TYPE_KRBTGT;
795 ldb_ent_type = HDB_LDB_ENT_TYPE_SERVER;
798 case HDB_ENT_TYPE_CLIENT:
799 ldb_ent_type = HDB_LDB_ENT_TYPE_CLIENT;
801 case HDB_ENT_TYPE_ANY:
802 ldb_ent_type = HDB_LDB_ENT_TYPE_ANY;
805 krb5_warnx(context, "LDB_fetch: invalid ent_type specified!");
806 talloc_free(mem_ctx);
807 return HDB_ERR_NOENTRY;
810 ret = LDB_lookup_principal(context, (struct ldb_context *)db->hdb_db,
812 principal, ldb_ent_type, realm_dn, &msg);
815 char *alias_from = principal->name.name_string.val[0];
817 Principal alias_principal;
819 /* Try again with a servicePrincipal alias */
820 if (ent_type != HDB_LDB_ENT_TYPE_SERVER && ent_type != HDB_LDB_ENT_TYPE_ANY) {
821 talloc_free(mem_ctx);
824 if (principal->name.name_string.len < 2) {
825 krb5_warnx(context, "LDB_fetch: could not find principal in DB, alias not applicable");
826 krb5_set_error_string(context, "LDB_fetch: could not find principal in DB, alias not applicable");
827 talloc_free(mem_ctx);
831 /* Look for the list of aliases */
832 ret = LDB_lookup_spn_alias(context,
833 (struct ldb_context *)db->hdb_db, mem_ctx,
834 realm_dn, alias_from,
837 talloc_free(mem_ctx);
841 ret = copy_Principal(principal, &alias_principal);
843 krb5_warnx(context, "LDB_fetch: could not copy principal");
844 krb5_set_error_string(context, "LDB_fetch: could not copy principal");
845 talloc_free(mem_ctx);
849 /* ooh, very nasty playing around in the Principal... */
850 free(alias_principal.name.name_string.val[0]);
851 alias_principal.name.name_string.val[0] = strdup(alias_to);
852 if (!alias_principal.name.name_string.val[0]) {
853 krb5_warnx(context, "LDB_fetch: strdup() failed");
854 krb5_set_error_string(context, "LDB_fetch: strdup() failed");
856 talloc_free(mem_ctx);
857 free_Principal(&alias_principal);
861 ret = LDB_lookup_principal(context, (struct ldb_context *)db->hdb_db,
863 &alias_principal, ent_type, realm_dn, &msg);
864 free_Principal(&alias_principal);
867 krb5_warnx(context, "LDB_fetch: could not find alias principal in DB");
868 krb5_set_error_string(context, "LDB_fetch: could not find alias principal in DB");
869 talloc_free(mem_ctx);
875 ret = LDB_message2entry(context, db, mem_ctx,
876 principal, ldb_ent_type,
877 realm_msg[0], msg[0], entry);
879 krb5_warnx(context, "LDB_fetch: message2entry failed\n");
883 talloc_free(mem_ctx);
887 static krb5_error_code LDB_store(krb5_context context, HDB *db, unsigned flags, hdb_entry *entry)
889 return HDB_ERR_DB_INUSE;
892 static krb5_error_code LDB_remove(krb5_context context, HDB *db, hdb_entry *entry)
894 return HDB_ERR_DB_INUSE;
898 struct ldb_context *ctx;
901 struct ldb_message **msgs;
902 struct ldb_message **realm_msgs;
905 static krb5_error_code LDB_seq(krb5_context context, HDB *db, unsigned flags, hdb_entry *entry)
908 struct hdb_ldb_seq *priv = (struct hdb_ldb_seq *)db->hdb_openp;
911 return HDB_ERR_NOENTRY;
914 mem_ctx = talloc_named(priv, 0, "LDB_seq context");
917 krb5_set_error_string(context, "LDB_seq: talloc_named() failed!");
921 if (priv->index < priv->count) {
922 ret = LDB_message2entry(context, db, mem_ctx,
923 NULL, HDB_LDB_ENT_TYPE_ANY,
924 priv->realm_msgs[0], priv->msgs[priv->index++], entry);
926 ret = HDB_ERR_NOENTRY;
931 db->hdb_openp = NULL;
933 talloc_free(mem_ctx);
939 static krb5_error_code LDB_firstkey(krb5_context context, HDB *db, unsigned flags,
942 struct ldb_context *ldb_ctx = (struct ldb_context *)db->hdb_db;
943 struct hdb_ldb_seq *priv = (struct hdb_ldb_seq *)db->hdb_openp;
945 struct ldb_dn *realm_dn = NULL;
946 struct ldb_message **msgs = NULL;
947 struct ldb_message **realm_msgs = NULL;
956 priv = (struct hdb_ldb_seq *) talloc(db, struct hdb_ldb_seq);
958 krb5_set_error_string(context, "talloc: out of memory");
965 priv->realm_msgs = NULL;
968 mem_ctx = talloc_named(priv, 0, "LDB_firstkey context");
971 krb5_set_error_string(context, "LDB_firstkey: talloc_named() failed!");
975 ret = krb5_get_default_realm(context, &realm);
981 ret = LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
982 mem_ctx, realm, &realm_msgs);
988 krb5_warnx(context, "LDB_firstkey: could not find realm\n");
989 return HDB_ERR_NOENTRY;
992 realm_dn = realm_msgs[0]->dn;
994 priv->realm_msgs = talloc_steal(priv, realm_msgs);
996 krb5_warnx(context, "LDB_firstkey: realm ok\n");
998 priv->count = ldb_search(ldb_ctx, realm_dn,
999 LDB_SCOPE_SUBTREE, "(objectClass=user)",
1002 priv->msgs = talloc_steal(priv, msgs);
1004 if (priv->count <= 0) {
1006 return HDB_ERR_NOENTRY;
1009 db->hdb_openp = priv;
1011 ret = LDB_seq(context, db, flags, entry);
1015 db->hdb_openp = NULL;
1017 talloc_free(mem_ctx);
1022 static krb5_error_code LDB_nextkey(krb5_context context, HDB *db, unsigned flags,
1025 return LDB_seq(context, db, flags, entry);
1028 static krb5_error_code LDB_destroy(krb5_context context, HDB *db)
1034 krb5_error_code hdb_ldb_create(TALLOC_CTX *mem_ctx,
1035 krb5_context context, struct HDB **db, const char *arg)
1037 *db = talloc(mem_ctx, HDB);
1039 krb5_set_error_string(context, "malloc: out of memory");
1043 (*db)->hdb_master_key_set = 0;
1044 (*db)->hdb_db = NULL;
1046 /* Setup the link to LDB */
1047 (*db)->hdb_db = samdb_connect(db, system_session(db));
1048 if ((*db)->hdb_db == NULL) {
1049 krb5_warnx(context, "hdb_ldb_create: samdb_connect failed!");
1050 krb5_set_error_string(context, "samdb_connect failed!");
1052 return HDB_ERR_NOENTRY;
1055 (*db)->hdb_openp = 0;
1056 (*db)->hdb_open = LDB_open;
1057 (*db)->hdb_close = LDB_close;
1058 (*db)->hdb_fetch = LDB_fetch;
1059 (*db)->hdb_store = LDB_store;
1060 (*db)->hdb_remove = LDB_remove;
1061 (*db)->hdb_firstkey = LDB_firstkey;
1062 (*db)->hdb_nextkey = LDB_nextkey;
1063 (*db)->hdb_lock = LDB_lock;
1064 (*db)->hdb_unlock = LDB_unlock;
1065 (*db)->hdb_rename = LDB_rename;
1066 /* we don't implement these, as we are not a lockable database */
1067 (*db)->hdb__get = NULL;
1068 (*db)->hdb__put = NULL;
1069 /* kadmin should not be used for deletes - use other tools instead */
1070 (*db)->hdb__del = NULL;
1071 (*db)->hdb_destroy = LDB_destroy;