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;
38 const char *classname;
41 static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx,
42 struct ldb_message *msg,
43 const char **name, uint32_t *type,
46 const struct ldb_val *val;
50 *name = talloc_strdup(mem_ctx,
51 ldb_msg_find_attr_as_string(msg, "value",
55 value_type = ldb_msg_find_attr_as_uint(msg, "type", 0);
58 val = ldb_msg_find_ldb_val(msg, "data");
65 /* The data should be provided as UTF16 string */
66 convert_string_talloc(mem_ctx, CH_UTF8, CH_UTF16,
67 val->data, val->length,
68 (void **)&data->data, &data->length, false);
76 case REG_DWORD_BIG_ENDIAN:
78 /* The data is a plain DWORD */
79 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
80 data->data = talloc_size(mem_ctx, sizeof(uint32_t));
81 if (data->data != NULL) {
82 SIVAL(data->data, 0, tmp);
84 data->length = sizeof(uint32_t);
93 /* The data is a plain QWORD */
94 uint64_t tmp = strtoull((char *)val->data, NULL, 0);
95 data->data = talloc_size(mem_ctx, sizeof(uint64_t));
96 if (data->data != NULL) {
97 SBVAL(data->data, 0, tmp);
99 data->length = sizeof(uint64_t);
109 data->data = talloc_memdup(mem_ctx, val->data,
111 data->length = val->length;
120 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
123 uint32_t type, DATA_BLOB data)
125 struct ldb_message *msg;
126 char *name_dup, *type_str;
129 msg = talloc_zero(mem_ctx, struct ldb_message);
134 name_dup = talloc_strdup(msg, name);
135 if (name_dup == NULL) {
140 ret = ldb_msg_add_string(msg, "value", name_dup);
141 if (ret != LDB_SUCCESS) {
149 if ((data.length > 0) && (data.data != NULL)) {
153 val = talloc_zero(msg, struct ldb_val);
159 /* The data is provided as UTF16 string */
160 ret2 = convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8,
161 (void *)data.data, data.length,
162 (void **)&val->data, &val->length,
165 ret = ldb_msg_add_value(msg, "data", val, NULL);
167 /* workaround for non-standard data */
168 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
171 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
176 case REG_DWORD_BIG_ENDIAN:
177 if ((data.length > 0) && (data.data != NULL)) {
178 if (data.length == sizeof(uint32_t)) {
181 conv_str = talloc_asprintf(msg, "0x%8.8x",
183 if (conv_str == NULL) {
187 ret = ldb_msg_add_string(msg, "data", conv_str);
189 /* workaround for non-standard data */
194 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
199 if ((data.length > 0) && (data.data != NULL)) {
200 if (data.length == sizeof(uint64_t)) {
203 conv_str = talloc_asprintf(msg, "0x%16.16llx",
205 if (conv_str == NULL) {
209 ret = ldb_msg_add_string(msg, "data", conv_str);
211 /* workaround for non-standard data */
217 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
223 if ((data.length > 0) && (data.data != NULL)) {
224 ret = ldb_msg_add_value(msg, "data", &data, NULL);
226 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
231 if (ret != LDB_SUCCESS) {
236 type_str = talloc_asprintf(mem_ctx, "%u", type);
237 if (type_str == NULL) {
242 ret = ldb_msg_add_string(msg, "type", type_str);
243 if (ret != LDB_SUCCESS) {
251 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
255 val.data = discard_const_p(uint8_t, value);
256 val.length = strlen(value);
258 return ldb_dn_escape_value(mem_ctx, val);
261 static int reg_close_ldb_key(struct ldb_key_data *key)
263 if (key->subkeys != NULL) {
264 talloc_free(key->subkeys);
268 if (key->values != NULL) {
269 talloc_free(key->values);
275 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
276 const struct hive_key *from,
277 const char *path, const char *add)
280 char *mypath = talloc_strdup(mem_ctx, path);
282 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
283 struct ldb_context *ldb = kd->ldb;
285 ret = ldb_dn_new(mem_ctx, ldb, add);
286 if (!ldb_dn_validate(ret)) {
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(mem_ctx,
316 ldb_dn_add_base(ret, kd->dn);
321 static WERROR cache_subkeys(struct ldb_key_data *kd)
323 struct ldb_context *c = kd->ldb;
324 struct ldb_result *res;
327 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
329 if (ret != LDB_SUCCESS) {
330 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
331 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
335 kd->subkey_count = res->count;
336 kd->subkeys = talloc_steal(kd, res->msgs);
342 static WERROR cache_values(struct ldb_key_data *kd)
344 struct ldb_context *c = kd->ldb;
345 struct ldb_result *res;
348 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
350 if (ret != LDB_SUCCESS) {
351 DEBUG(0, ("Error getting values for '%s': %s\n",
352 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
356 kd->value_count = res->count;
357 kd->values = talloc_steal(kd, res->msgs);
364 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
365 const struct hive_key *k, uint32_t idx,
367 const char **classname,
368 NTTIME *last_mod_time)
370 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
375 if (classname != NULL)
377 if (last_mod_time != NULL)
378 *last_mod_time = 0; /* TODO: we need to add this to the
379 ldb backend properly */
381 /* Do a search if necessary */
382 if (kd->subkeys == NULL) {
383 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
386 if (idx >= kd->subkey_count)
387 return WERR_NO_MORE_ITEMS;
390 *name = talloc_strdup(mem_ctx,
391 ldb_msg_find_attr_as_string(kd->subkeys[idx], "key", NULL));
392 if (classname != NULL)
393 *classname = talloc_strdup(mem_ctx,
394 ldb_msg_find_attr_as_string(kd->subkeys[idx], "classname", NULL));
399 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx,
400 const struct hive_key *k,
401 const char **name, uint32_t *data_type,
404 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
405 struct ldb_context *c = kd->ldb;
406 const char* attrs[] = { "data", "type", NULL };
407 struct ldb_result *res;
410 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_BASE, attrs, "(dn=*)");
412 if (ret != LDB_SUCCESS) {
413 DEBUG(0, ("Error getting default value for '%s': %s\n",
414 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
418 if (res->count == 0 || res->msgs[0]->num_elements == 0)
421 if ((data_type != NULL) && (data != NULL)) {
422 reg_ldb_unpack_value(mem_ctx, res->msgs[0], name, data_type,
431 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
432 uint32_t idx, const char **name,
433 uint32_t *data_type, DATA_BLOB *data)
435 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
437 /* if the default value exists, give it back */
438 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
446 /* Do the search if necessary */
447 if (kd->values == NULL) {
448 W_ERROR_NOT_OK_RETURN(cache_values(kd));
451 if (idx >= kd->value_count)
452 return WERR_NO_MORE_ITEMS;
454 reg_ldb_unpack_value(mem_ctx, kd->values[idx], name, data_type, data);
459 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
460 const char *name, uint32_t *data_type,
463 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
464 const char *res_name;
468 return WERR_INVALID_PARAM;
471 /* the default value was requested, give it back */
472 if (name[0] == '\0') {
473 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
476 /* Do the search if necessary */
477 if (kd->values == NULL) {
478 W_ERROR_NOT_OK_RETURN(cache_values(kd));
481 for (idx = 0; idx < kd->value_count; idx++) {
482 res_name = ldb_msg_find_attr_as_string(kd->values[idx], "value",
484 if (ldb_attr_cmp(name, res_name) == 0) {
485 reg_ldb_unpack_value(mem_ctx, kd->values[idx], NULL,
494 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
495 const char *name, struct hive_key **key)
497 struct ldb_result *res;
498 struct ldb_dn *ldap_path;
500 struct ldb_key_data *newkd;
501 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
502 struct ldb_context *c = kd->ldb;
505 return WERR_INVALID_PARAM;
508 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
509 W_ERROR_HAVE_NO_MEMORY(ldap_path);
511 ret = ldb_search(c, mem_ctx, &res, ldap_path, LDB_SCOPE_BASE, NULL, "(key=*)");
513 if (ret != LDB_SUCCESS) {
514 DEBUG(3, ("Error opening key '%s': %s\n",
515 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
517 } else if (res->count == 0) {
518 DEBUG(3, ("Key '%s' not found\n",
519 ldb_dn_get_linearized(ldap_path)));
524 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
525 W_ERROR_HAVE_NO_MEMORY(newkd);
526 newkd->key.ops = ®_backend_ldb;
527 newkd->ldb = talloc_reference(newkd, kd->ldb);
528 newkd->dn = ldb_dn_copy(newkd, res->msgs[0]->dn);
529 newkd->classname = talloc_steal(newkd,
530 ldb_msg_find_attr_as_string(res->msgs[0], "classname", NULL));
534 *key = (struct hive_key *)newkd;
539 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
540 struct auth_session_info *session_info,
541 struct cli_credentials *credentials,
542 struct tevent_context *ev_ctx,
543 struct loadparm_context *lp_ctx,
546 struct ldb_key_data *kd;
547 struct ldb_context *wrap;
548 struct ldb_message *attrs_msg;
550 if (location == NULL)
551 return WERR_INVALID_PARAM;
553 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
554 location, session_info, credentials, 0);
557 DEBUG(1, (__FILE__": unable to connect\n"));
561 attrs_msg = ldb_msg_new(wrap);
562 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
563 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
564 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
565 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
566 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
568 ldb_add(wrap, attrs_msg);
570 ldb_set_debug_stderr(wrap);
572 kd = talloc_zero(parent_ctx, struct ldb_key_data);
573 kd->key.ops = ®_backend_ldb;
574 kd->ldb = talloc_reference(kd, wrap);
575 talloc_set_destructor (kd, reg_close_ldb_key);
576 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
578 *k = (struct hive_key *)kd;
583 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
584 const char *name, const char *classname,
585 struct security_descriptor *sd,
586 struct hive_key **newkey)
588 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
589 struct ldb_message *msg;
590 struct ldb_key_data *newkd;
594 return WERR_INVALID_PARAM;
597 msg = ldb_msg_new(mem_ctx);
598 W_ERROR_HAVE_NO_MEMORY(msg);
600 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
601 W_ERROR_HAVE_NO_MEMORY(msg->dn);
603 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
604 if (classname != NULL)
605 ldb_msg_add_string(msg, "classname",
606 talloc_strdup(mem_ctx, classname));
608 ret = ldb_add(parentkd->ldb, msg);
609 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
610 return WERR_ALREADY_EXISTS;
613 if (ret != LDB_SUCCESS) {
614 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
618 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
620 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
621 W_ERROR_HAVE_NO_MEMORY(newkd);
622 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
623 newkd->key.ops = ®_backend_ldb;
624 newkd->dn = talloc_steal(newkd, msg->dn);
625 newkd->classname = talloc_steal(newkd, classname);
627 *newkey = (struct hive_key *)newkd;
630 talloc_free(parentkd->subkeys);
631 parentkd->subkeys = NULL;
636 static WERROR ldb_del_value(TALLOC_CTX *mem_ctx, struct hive_key *key,
640 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
641 struct ldb_message *msg;
642 struct ldb_dn *childdn;
645 return WERR_INVALID_PARAM;
648 if (child[0] == '\0') {
650 msg = talloc_zero(mem_ctx, struct ldb_message);
651 W_ERROR_HAVE_NO_MEMORY(msg);
652 msg->dn = ldb_dn_copy(msg, kd->dn);
653 W_ERROR_HAVE_NO_MEMORY(msg->dn);
654 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
655 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
657 ret = ldb_modify(kd->ldb, msg);
658 if (ret != LDB_SUCCESS) {
659 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
664 childdn = ldb_dn_copy(kd->ldb, kd->dn);
665 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
666 reg_ldb_escape(childdn, child)))
668 talloc_free(childdn);
672 ret = ldb_delete(kd->ldb, childdn);
674 talloc_free(childdn);
676 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
678 } else if (ret != LDB_SUCCESS) {
679 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
685 talloc_free(kd->values);
691 static WERROR ldb_del_key(TALLOC_CTX *mem_ctx, const struct hive_key *key,
696 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
697 struct ldb_dn *ldap_path;
698 struct ldb_context *c = parentkd->ldb;
699 struct ldb_result *res_keys;
700 struct ldb_result *res_vals;
705 return WERR_INVALID_PARAM;
708 /* Verify key exists by opening it */
709 werr = ldb_open_key(mem_ctx, key, name, &hk);
710 if (!W_ERROR_IS_OK(werr)) {
714 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
715 W_ERROR_HAVE_NO_MEMORY(ldap_path);
717 /* Search for subkeys */
718 ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
721 if (ret != LDB_SUCCESS) {
722 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
723 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
727 /* Search for values */
728 ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
731 if (ret != LDB_SUCCESS) {
732 DEBUG(0, ("Error getting values for '%s': %s\n",
733 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
737 /* Start an explicit transaction */
738 ret = ldb_transaction_start(c);
740 if (ret != LDB_SUCCESS) {
741 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
745 if (res_keys->count || res_vals->count)
747 /* Delete any subkeys */
748 for (i = 0; i < res_keys->count; i++)
750 werr = ldb_del_key(mem_ctx, hk,
751 ldb_msg_find_attr_as_string(
754 if (!W_ERROR_IS_OK(werr)) {
755 ret = ldb_transaction_cancel(c);
760 /* Delete any values */
761 for (i = 0; i < res_vals->count; i++)
763 werr = ldb_del_value(mem_ctx, hk,
764 ldb_msg_find_attr_as_string(
767 if (!W_ERROR_IS_OK(werr)) {
768 ret = ldb_transaction_cancel(c);
774 /* Delete the key itself */
775 ret = ldb_delete(c, ldap_path);
777 if (ret != LDB_SUCCESS)
779 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
780 ret = ldb_transaction_cancel(c);
784 /* Commit the transaction */
785 ret = ldb_transaction_commit(c);
787 if (ret != LDB_SUCCESS)
789 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
790 ret = ldb_transaction_cancel(c);
795 talloc_free(parentkd->subkeys);
796 parentkd->subkeys = NULL;
801 static WERROR ldb_set_value(struct hive_key *parent,
802 const char *name, uint32_t type,
803 const DATA_BLOB data)
805 struct ldb_message *msg;
806 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
809 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
812 return WERR_INVALID_PARAM;
815 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
816 W_ERROR_HAVE_NO_MEMORY(msg);
818 msg->dn = ldb_dn_copy(msg, kd->dn);
819 W_ERROR_HAVE_NO_MEMORY(msg->dn);
821 if (name[0] != '\0') {
822 /* For a default value, we add/overwrite the attributes to/of the hive.
823 For a normal value, we create a new child. */
824 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
825 reg_ldb_escape(mem_ctx, name)))
827 talloc_free(mem_ctx);
832 /* Try first a "modify" and if this doesn't work do try an "add" */
833 for (i = 0; i < msg->num_elements; i++) {
834 if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE) {
835 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
838 ret = ldb_modify(kd->ldb, msg);
839 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
841 while (i < msg->num_elements) {
842 if (msg->elements[i].flags == LDB_FLAG_MOD_DELETE) {
843 ldb_msg_remove_element(msg, &msg->elements[i]);
848 ret = ldb_add(kd->ldb, msg);
850 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
851 /* ignore this -> the value didn't exist and also now doesn't */
855 if (ret != LDB_SUCCESS) {
856 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
857 talloc_free(mem_ctx);
862 talloc_free(kd->values);
865 talloc_free(mem_ctx);
869 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
870 const struct hive_key *key,
871 const char **classname,
872 uint32_t *num_subkeys,
873 uint32_t *num_values,
874 NTTIME *last_change_time,
875 uint32_t *max_subkeynamelen,
876 uint32_t *max_valnamelen,
877 uint32_t *max_valbufsize)
879 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
880 uint32_t default_value_type = REG_NONE;
881 DATA_BLOB default_value = { NULL, 0 };
885 if (classname != NULL)
887 if (num_subkeys != NULL)
889 if (num_values != NULL)
891 if (last_change_time != NULL)
892 *last_change_time = 0;
893 if (max_subkeynamelen != NULL)
894 *max_subkeynamelen = 0;
895 if (max_valnamelen != NULL)
897 if (max_valbufsize != NULL)
900 /* We need this to get the default value (if it exists) for counting
901 * the values under the key and for finding out the longest value buffer
902 * size. If no default value exists the DATA_BLOB "default_value" will
903 * remain { NULL, 0 }. */
904 werr = ldb_get_default_value(mem_ctx, key, NULL, &default_value_type,
906 if ((!W_ERROR_IS_OK(werr)) && (!W_ERROR_EQUAL(werr, WERR_BADFILE))) {
910 if (kd->subkeys == NULL) {
911 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
913 if (kd->values == NULL) {
914 W_ERROR_NOT_OK_RETURN(cache_values(kd));
917 if (classname != NULL) {
918 *classname = kd->classname;
921 if (num_subkeys != NULL) {
922 *num_subkeys = kd->subkey_count;
924 if (num_values != NULL) {
925 *num_values = kd->value_count;
926 /* also consider the default value if it exists */
927 if (default_value.data != NULL) {
933 if (max_subkeynamelen != NULL) {
935 struct ldb_message_element *el;
937 for (i = 0; i < kd->subkey_count; i++) {
938 el = ldb_msg_find_element(kd->subkeys[i], "key");
939 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
943 if (max_valnamelen != NULL || max_valbufsize != NULL) {
945 struct ldb_message_element *el;
946 W_ERROR_NOT_OK_RETURN(cache_values(kd));
948 /* also consider the default value if it exists */
949 if ((max_valbufsize != NULL) && (default_value.data != NULL)) {
950 *max_valbufsize = MAX(*max_valbufsize,
951 default_value.length);
954 for (i = 0; i < kd->value_count; i++) {
955 if (max_valnamelen != NULL) {
956 el = ldb_msg_find_element(kd->values[i], "value");
957 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
960 if (max_valbufsize != NULL) {
963 reg_ldb_unpack_value(mem_ctx,
966 *max_valbufsize = MAX(*max_valbufsize, data.length);
967 talloc_free(data.data);
972 talloc_free(default_value.data);
977 static struct hive_operations reg_backend_ldb = {
979 .add_key = ldb_add_key,
980 .del_key = ldb_del_key,
981 .get_key_by_name = ldb_open_key,
982 .enum_value = ldb_get_value_by_id,
983 .enum_key = ldb_get_subkey_by_id,
984 .set_value = ldb_set_value,
985 .get_value_by_name = ldb_get_value,
986 .delete_value = ldb_del_value,
987 .get_key_info = ldb_get_key_info,