2 Unix SMB/CIFS implementation.
4 Copyright (C) 2004-2007, Jelmer Vernooij, jelmer@samba.org
5 Copyright (C) 2008-2010, Matthias Dieter Wallnöfer, mdw@samba.org
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "lib/ldb/include/ldb.h"
24 #include "lib/ldb/include/ldb_errors.h"
26 #include "librpc/gen_ndr/winreg.h"
27 #include "param/param.h"
29 static struct hive_operations reg_backend_ldb;
34 struct ldb_context *ldb;
36 struct ldb_message **subkeys, **values;
37 unsigned int subkey_count, value_count;
40 static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx,
41 struct ldb_message *msg,
42 const char **name, uint32_t *type,
45 const struct ldb_val *val;
49 *name = talloc_strdup(mem_ctx,
50 ldb_msg_find_attr_as_string(msg, "value",
54 value_type = ldb_msg_find_attr_as_uint(msg, "type", 0);
57 val = ldb_msg_find_ldb_val(msg, "data");
64 /* The data should be provided as UTF16 string */
65 convert_string_talloc(mem_ctx, CH_UTF8, CH_UTF16,
66 val->data, val->length,
67 (void **)&data->data, &data->length, false);
75 case REG_DWORD_BIG_ENDIAN:
77 /* The data is a plain DWORD */
78 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
79 data->data = talloc_size(mem_ctx, sizeof(uint32_t));
80 if (data->data != NULL) {
81 SIVAL(data->data, 0, tmp);
83 data->length = sizeof(uint32_t);
92 /* The data is a plain QWORD */
93 uint64_t tmp = strtoull((char *)val->data, NULL, 0);
94 data->data = talloc_size(mem_ctx, sizeof(uint64_t));
95 if (data->data != NULL) {
96 SBVAL(data->data, 0, tmp);
98 data->length = sizeof(uint64_t);
108 data->data = talloc_memdup(mem_ctx, val->data,
110 data->length = val->length;
119 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
122 uint32_t type, DATA_BLOB data)
124 struct ldb_message *msg;
125 char *name_dup, *type_str;
128 msg = talloc_zero(mem_ctx, struct ldb_message);
133 name_dup = talloc_strdup(msg, name);
134 if (name_dup == NULL) {
139 ret = ldb_msg_add_string(msg, "value", name_dup);
140 if (ret != LDB_SUCCESS) {
148 if ((data.length > 0) && (data.data != NULL)) {
152 val = talloc_zero(msg, struct ldb_val);
158 /* The data is provided as UTF16 string */
159 ret2 = convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8,
160 (void *)data.data, data.length,
161 (void **)&val->data, &val->length,
164 ret = ldb_msg_add_value(msg, "data", val, NULL);
166 /* workaround for non-standard data */
167 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
170 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
175 case REG_DWORD_BIG_ENDIAN:
176 if ((data.length > 0) && (data.data != NULL)) {
177 if (data.length == sizeof(uint32_t)) {
180 conv_str = talloc_asprintf(msg, "0x%8.8x",
182 if (conv_str == NULL) {
186 ret = ldb_msg_add_string(msg, "data", conv_str);
188 /* workaround for non-standard data */
189 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
192 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
197 if ((data.length > 0) && (data.data != NULL)) {
198 if (data.length == sizeof(uint64_t)) {
201 conv_str = talloc_asprintf(msg, "0x%16.16llx",
203 if (conv_str == NULL) {
207 ret = ldb_msg_add_string(msg, "data", conv_str);
209 /* workaround for non-standard data */
210 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
213 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
219 if ((data.length > 0) && (data.data != NULL)) {
220 ret = ldb_msg_add_value(msg, "data", &data, NULL);
222 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
227 if (ret != LDB_SUCCESS) {
232 type_str = talloc_asprintf(mem_ctx, "%u", type);
233 if (type_str == NULL) {
238 ret = ldb_msg_add_string(msg, "type", type_str);
239 if (ret != LDB_SUCCESS) {
247 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
251 val.data = discard_const_p(uint8_t, value);
252 val.length = strlen(value);
254 return ldb_dn_escape_value(mem_ctx, val);
257 static int reg_close_ldb_key(struct ldb_key_data *key)
259 if (key->subkeys != NULL) {
260 talloc_free(key->subkeys);
264 if (key->values != NULL) {
265 talloc_free(key->values);
271 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
272 const struct hive_key *from,
273 const char *path, const char *add)
275 TALLOC_CTX *local_ctx;
277 char *mypath = talloc_strdup(mem_ctx, path);
279 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
280 struct ldb_context *ldb = kd->ldb;
282 local_ctx = talloc_new(mem_ctx);
284 ret = ldb_dn_new(mem_ctx, ldb, add);
285 if (!ldb_dn_validate(ret)) {
287 talloc_free(local_ctx);
294 begin = strrchr(mypath, '\\');
296 if (begin) keyname = begin + 1;
297 else keyname = mypath;
299 if (keyname[0] != '\0') {
300 if (!ldb_dn_add_base_fmt(ret, "key=%s",
301 reg_ldb_escape(local_ctx,
304 talloc_free(local_ctx);
316 ldb_dn_add_base(ret, kd->dn);
318 talloc_free(local_ctx);
323 static WERROR cache_subkeys(struct ldb_key_data *kd)
325 struct ldb_context *c = kd->ldb;
326 struct ldb_result *res;
329 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "(key=*)");
331 if (ret != LDB_SUCCESS) {
332 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
333 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
337 kd->subkey_count = res->count;
338 kd->subkeys = talloc_steal(kd, res->msgs);
344 static WERROR cache_values(struct ldb_key_data *kd)
346 struct ldb_context *c = kd->ldb;
347 struct ldb_result *res;
350 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
353 if (ret != LDB_SUCCESS) {
354 DEBUG(0, ("Error getting values for '%s': %s\n",
355 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
359 kd->value_count = res->count;
360 kd->values = talloc_steal(kd, res->msgs);
367 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
368 const struct hive_key *k, uint32_t idx,
370 const char **classname,
371 NTTIME *last_mod_time)
373 struct ldb_message_element *el;
374 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
379 if (classname != NULL)
380 *classname = NULL; /* TODO: Store properly */
381 if (last_mod_time != NULL)
382 *last_mod_time = 0; /* TODO: we need to add this to the
383 ldb backend properly */
385 /* Do a search if necessary */
386 if (kd->subkeys == NULL) {
387 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
390 if (idx >= kd->subkey_count)
391 return WERR_NO_MORE_ITEMS;
393 el = ldb_msg_find_element(kd->subkeys[idx], "key");
394 SMB_ASSERT(el != NULL);
395 SMB_ASSERT(el->num_values != 0);
398 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
403 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx,
404 const struct hive_key *k,
405 const char **name, uint32_t *data_type,
408 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
409 struct ldb_context *c = kd->ldb;
410 const char* attrs[] = { "data", "type", NULL };
411 struct ldb_result *res;
414 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_BASE, attrs, "(dn=*)");
416 if (ret != LDB_SUCCESS) {
417 DEBUG(0, ("Error getting default value for '%s': %s\n",
418 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
422 if (res->count == 0 || res->msgs[0]->num_elements == 0)
425 if ((data_type != NULL) && (data != NULL)) {
426 reg_ldb_unpack_value(mem_ctx, res->msgs[0], name, data_type,
435 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
436 uint32_t idx, const char **name,
437 uint32_t *data_type, DATA_BLOB *data)
439 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
441 /* if default value exists, give it back */
442 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
450 /* Do the search if necessary */
451 if (kd->values == NULL) {
452 W_ERROR_NOT_OK_RETURN(cache_values(kd));
455 if (idx >= kd->value_count)
456 return WERR_NO_MORE_ITEMS;
458 reg_ldb_unpack_value(mem_ctx, kd->values[idx], name, data_type, data);
463 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
464 const char *name, uint32_t *data_type,
467 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
468 struct ldb_context *c = kd->ldb;
469 struct ldb_result *res;
473 return WERR_INVALID_PARAM;
476 if (name[0] == '\0') {
478 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
481 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_ONELEVEL,
482 NULL, "(value=%s)", name);
484 if (ret != LDB_SUCCESS) {
485 DEBUG(0, ("Error getting values for '%s': %s\n",
486 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
493 reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
501 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
502 const char *name, struct hive_key **key)
504 struct ldb_result *res;
505 struct ldb_dn *ldap_path;
507 struct ldb_key_data *newkd;
508 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
509 struct ldb_context *c = kd->ldb;
511 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
512 W_ERROR_HAVE_NO_MEMORY(ldap_path);
514 ret = ldb_search(c, mem_ctx, &res, ldap_path, LDB_SCOPE_BASE, NULL, "(key=*)");
516 if (ret != LDB_SUCCESS) {
517 DEBUG(3, ("Error opening key '%s': %s\n",
518 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
520 } else if (res->count == 0) {
521 DEBUG(3, ("Key '%s' not found\n",
522 ldb_dn_get_linearized(ldap_path)));
527 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
528 newkd->key.ops = ®_backend_ldb;
529 newkd->ldb = talloc_reference(newkd, kd->ldb);
530 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
532 *key = (struct hive_key *)newkd;
537 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
538 struct auth_session_info *session_info,
539 struct cli_credentials *credentials,
540 struct tevent_context *ev_ctx,
541 struct loadparm_context *lp_ctx,
544 struct ldb_key_data *kd;
545 struct ldb_context *wrap;
546 struct ldb_message *attrs_msg;
548 if (location == NULL)
549 return WERR_INVALID_PARAM;
551 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
552 location, session_info, credentials, 0);
555 DEBUG(1, (__FILE__": unable to connect\n"));
559 attrs_msg = ldb_msg_new(wrap);
560 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
561 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
562 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
563 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
564 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
566 ldb_add(wrap, attrs_msg);
568 ldb_set_debug_stderr(wrap);
570 kd = talloc_zero(parent_ctx, struct ldb_key_data);
571 kd->key.ops = ®_backend_ldb;
572 kd->ldb = talloc_reference(kd, wrap);
573 talloc_set_destructor (kd, reg_close_ldb_key);
574 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
576 *k = (struct hive_key *)kd;
581 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
582 const char *name, const char *classname,
583 struct security_descriptor *sd,
584 struct hive_key **newkey)
586 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
587 struct ldb_message *msg;
588 struct ldb_key_data *newkd;
591 msg = ldb_msg_new(mem_ctx);
592 W_ERROR_HAVE_NO_MEMORY(msg);
594 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
595 W_ERROR_HAVE_NO_MEMORY(msg->dn);
597 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
598 if (classname != NULL)
599 ldb_msg_add_string(msg, "classname",
600 talloc_strdup(mem_ctx, classname));
602 ret = ldb_add(parentkd->ldb, msg);
603 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
604 return WERR_ALREADY_EXISTS;
607 if (ret != LDB_SUCCESS) {
608 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
612 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
614 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
615 W_ERROR_HAVE_NO_MEMORY(newkd);
616 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
617 newkd->key.ops = ®_backend_ldb;
618 newkd->dn = talloc_steal(newkd, msg->dn);
620 *newkey = (struct hive_key *)newkd;
623 talloc_free(parentkd->subkeys);
624 parentkd->subkeys = NULL;
629 static WERROR ldb_del_value(TALLOC_CTX *mem_ctx, struct hive_key *key,
633 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
634 struct ldb_message *msg;
635 struct ldb_dn *childdn;
637 if ((child == NULL) || (child[0] == '\0')) {
639 msg = talloc_zero(mem_ctx, struct ldb_message);
640 W_ERROR_HAVE_NO_MEMORY(msg);
641 msg->dn = ldb_dn_copy(msg, kd->dn);
642 W_ERROR_HAVE_NO_MEMORY(msg->dn);
643 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
644 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
646 ret = ldb_modify(kd->ldb, msg);
647 if (ret != LDB_SUCCESS) {
648 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
653 childdn = ldb_dn_copy(kd->ldb, kd->dn);
654 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
655 reg_ldb_escape(childdn, child)))
657 talloc_free(childdn);
661 ret = ldb_delete(kd->ldb, childdn);
663 talloc_free(childdn);
665 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
667 } else if (ret != LDB_SUCCESS) {
668 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
674 talloc_free(kd->values);
680 static WERROR ldb_del_key(TALLOC_CTX *mem_ctx, const struct hive_key *key,
685 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
686 struct ldb_dn *ldap_path;
687 struct ldb_context *c = parentkd->ldb;
688 struct ldb_result *res_keys;
689 struct ldb_result *res_vals;
693 /* Verify key exists by opening it */
694 werr = ldb_open_key(mem_ctx, key, name, &hk);
695 if (!W_ERROR_IS_OK(werr)) {
699 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
700 W_ERROR_HAVE_NO_MEMORY(ldap_path);
702 /* Search for subkeys */
703 ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
706 if (ret != LDB_SUCCESS) {
707 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
708 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
712 /* Search for values */
713 ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
716 if (ret != LDB_SUCCESS) {
717 DEBUG(0, ("Error getting values for '%s': %s\n",
718 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
722 /* Start an explicit transaction */
723 ret = ldb_transaction_start(c);
725 if (ret != LDB_SUCCESS) {
726 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
730 if (res_keys->count || res_vals->count)
732 /* Delete any subkeys */
733 for (i = 0; i < res_keys->count; i++)
735 werr = ldb_del_key(mem_ctx, hk,
736 ldb_msg_find_attr_as_string(
739 if (!W_ERROR_IS_OK(werr)) {
740 ret = ldb_transaction_cancel(c);
745 /* Delete any values */
746 for (i = 0; i < res_vals->count; i++)
748 werr = ldb_del_value(mem_ctx, hk,
749 ldb_msg_find_attr_as_string(
752 if (!W_ERROR_IS_OK(werr)) {
753 ret = ldb_transaction_cancel(c);
759 /* Delete the key itself */
760 ret = ldb_delete(c, ldap_path);
762 if (ret != LDB_SUCCESS)
764 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
765 ret = ldb_transaction_cancel(c);
769 /* Commit the transaction */
770 ret = ldb_transaction_commit(c);
772 if (ret != LDB_SUCCESS)
774 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
775 ret = ldb_transaction_cancel(c);
780 talloc_free(parentkd->subkeys);
781 parentkd->subkeys = NULL;
786 static WERROR ldb_set_value(struct hive_key *parent,
787 const char *name, uint32_t type,
788 const DATA_BLOB data)
790 struct ldb_message *msg;
791 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
794 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
796 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
797 W_ERROR_HAVE_NO_MEMORY(msg);
799 msg->dn = ldb_dn_copy(msg, kd->dn);
800 W_ERROR_HAVE_NO_MEMORY(msg->dn);
802 if ((name != NULL) && (name[0] != '\0')) {
803 /* For a default value, we add/overwrite the attributes to/of the hive.
804 For a normal value, we create a new child. */
805 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
806 reg_ldb_escape(mem_ctx, name)))
808 talloc_free(mem_ctx);
813 /* Try first a "modify" and if this doesn't work do try an "add" */
814 for (i = 0; i < msg->num_elements; i++) {
815 if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE) {
816 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
819 ret = ldb_modify(kd->ldb, msg);
820 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
822 while (i < msg->num_elements) {
823 if (msg->elements[i].flags == LDB_FLAG_MOD_DELETE) {
824 ldb_msg_remove_element(msg, &msg->elements[i]);
829 ret = ldb_add(kd->ldb, msg);
831 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
832 /* ignore this -> the value didn't exist and also now doesn't */
836 if (ret != LDB_SUCCESS) {
837 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
838 talloc_free(mem_ctx);
843 talloc_free(kd->values);
846 talloc_free(mem_ctx);
850 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
851 const struct hive_key *key,
852 const char **classname,
853 uint32_t *num_subkeys,
854 uint32_t *num_values,
855 NTTIME *last_change_time,
856 uint32_t *max_subkeynamelen,
857 uint32_t *max_valnamelen,
858 uint32_t *max_valbufsize)
860 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
861 uint32_t default_value_type = REG_NONE;
862 DATA_BLOB default_value = { NULL, 0 };
866 if (classname != NULL)
868 if (num_subkeys != NULL)
870 if (num_values != NULL)
872 if (last_change_time != NULL)
873 *last_change_time = 0;
874 if (max_subkeynamelen != NULL)
875 *max_subkeynamelen = 0;
876 if (max_valnamelen != NULL)
878 if (max_valbufsize != NULL)
881 /* We need this to get the default value (if it exists) for counting
882 * the values under the key and for finding out the longest value buffer
883 * size. If no default value exists the DATA_BLOB "default_value" will
884 * remain { NULL, 0 }. */
885 werr = ldb_get_default_value(mem_ctx, key, NULL, &default_value_type,
887 if ((!W_ERROR_IS_OK(werr)) && (!W_ERROR_EQUAL(werr, WERR_BADFILE))) {
891 if (kd->subkeys == NULL) {
892 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
895 if (kd->values == NULL) {
896 W_ERROR_NOT_OK_RETURN(cache_values(kd));
899 if (num_subkeys != NULL) {
900 *num_subkeys = kd->subkey_count;
902 if (num_values != NULL) {
903 *num_values = kd->value_count;
904 /* also consider the default value if it exists */
905 if (default_value.data != NULL) {
911 if (max_subkeynamelen != NULL) {
913 struct ldb_message_element *el;
915 for (i = 0; i < kd->subkey_count; i++) {
916 el = ldb_msg_find_element(kd->subkeys[i], "key");
917 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
921 if (max_valnamelen != NULL || max_valbufsize != NULL) {
923 struct ldb_message_element *el;
924 W_ERROR_NOT_OK_RETURN(cache_values(kd));
926 /* also consider the default value if it exists */
927 if ((max_valbufsize != NULL) && (default_value.data != NULL)) {
928 *max_valbufsize = MAX(*max_valbufsize,
929 default_value.length);
932 for (i = 0; i < kd->value_count; i++) {
933 if (max_valnamelen != NULL) {
934 el = ldb_msg_find_element(kd->values[i], "value");
935 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
938 if (max_valbufsize != NULL) {
941 reg_ldb_unpack_value(mem_ctx,
944 *max_valbufsize = MAX(*max_valbufsize, data.length);
945 talloc_free(data.data);
950 talloc_free(default_value.data);
955 static struct hive_operations reg_backend_ldb = {
957 .add_key = ldb_add_key,
958 .del_key = ldb_del_key,
959 .get_key_by_name = ldb_open_key,
960 .enum_value = ldb_get_value_by_id,
961 .enum_key = ldb_get_subkey_by_id,
962 .set_value = ldb_set_value,
963 .get_value_by_name = ldb_get_value,
964 .delete_value = ldb_del_value,
965 .get_key_info = ldb_get_key_info,