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 convert_string_talloc(mem_ctx
, CH_UTF8
, CH_UTF16
,
65 val
->data
, val
->length
,
66 (void **)&data
->data
, &data
->length
, false);
75 uint32_t tmp
= strtoul((char *)val
->data
, NULL
, 0);
76 *data
= data_blob_talloc(mem_ctx
, NULL
, 4);
77 SIVAL(data
->data
, 0, tmp
);
87 *data
= data_blob_talloc(mem_ctx
, val
->data
, val
->length
);
96 static struct ldb_message
*reg_ldb_pack_value(struct ldb_context
*ctx
,
99 uint32_t type
, DATA_BLOB data
)
101 struct ldb_message
*msg
;
102 char *name_dup
, *type_str
;
105 msg
= talloc_zero(mem_ctx
, struct ldb_message
);
110 name_dup
= talloc_strdup(msg
, name
);
111 if (name_dup
== NULL
) {
116 ret
= ldb_msg_add_string(msg
, "value", name_dup
);
117 if (ret
!= LDB_SUCCESS
) {
125 if ((data
.length
> 0) && (data
.data
!= NULL
)
126 && (data
.data
[0] != '\0')) {
130 val
= talloc_zero(msg
, struct ldb_val
);
136 ret2
= convert_string_talloc(mem_ctx
, CH_UTF16
, CH_UTF8
,
137 (void *)data
.data
, data
.length
,
138 (void **)&val
->data
, &val
->length
,
140 ret
= ldb_msg_add_value(msg
, "data", val
, NULL
);
142 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
147 if ((data
.length
> 0) && (data
.data
!= NULL
)) {
150 conv_str
= talloc_asprintf(msg
, "0x%x", IVAL(data
.data
, 0));
151 if (conv_str
== NULL
) {
156 ret
= ldb_msg_add_string(msg
, "data", conv_str
);
158 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
164 if ((data
.length
> 0) && (data
.data
!= NULL
)
165 && (data
.data
[0] != '\0')) {
166 ret
= ldb_msg_add_value(msg
, "data", &data
, NULL
);
168 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
173 if (ret
!= LDB_SUCCESS
) {
178 type_str
= talloc_asprintf(mem_ctx
, "%u", type
);
179 if (type_str
== NULL
) {
184 ret
= ldb_msg_add_string(msg
, "type", type_str
);
185 if (ret
!= LDB_SUCCESS
) {
193 static char *reg_ldb_escape(TALLOC_CTX
*mem_ctx
, const char *value
)
197 val
.data
= discard_const_p(uint8_t, value
);
198 val
.length
= strlen(value
);
200 return ldb_dn_escape_value(mem_ctx
, val
);
203 static int reg_close_ldb_key(struct ldb_key_data
*key
)
205 if (key
->subkeys
!= NULL
) {
206 talloc_free(key
->subkeys
);
210 if (key
->values
!= NULL
) {
211 talloc_free(key
->values
);
217 static struct ldb_dn
*reg_path_to_ldb(TALLOC_CTX
*mem_ctx
,
218 const struct hive_key
*from
,
219 const char *path
, const char *add
)
221 TALLOC_CTX
*local_ctx
;
223 char *mypath
= talloc_strdup(mem_ctx
, path
);
225 struct ldb_key_data
*kd
= talloc_get_type(from
, struct ldb_key_data
);
226 struct ldb_context
*ldb
= kd
->ldb
;
228 local_ctx
= talloc_new(mem_ctx
);
230 ret
= ldb_dn_new(mem_ctx
, ldb
, add
);
231 if (!ldb_dn_validate(ret
)) {
233 talloc_free(local_ctx
);
240 begin
= strrchr(mypath
, '\\');
242 if (begin
) keyname
= begin
+ 1;
243 else keyname
= mypath
;
245 if (keyname
[0] != '\0') {
246 if (!ldb_dn_add_base_fmt(ret
, "key=%s",
247 reg_ldb_escape(local_ctx
,
250 talloc_free(local_ctx
);
262 ldb_dn_add_base(ret
, kd
->dn
);
264 talloc_free(local_ctx
);
269 static WERROR
cache_subkeys(struct ldb_key_data
*kd
)
271 struct ldb_context
*c
= kd
->ldb
;
272 struct ldb_result
*res
;
275 ret
= ldb_search(c
, c
, &res
, kd
->dn
, LDB_SCOPE_ONELEVEL
, NULL
, "(key=*)");
277 if (ret
!= LDB_SUCCESS
) {
278 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
279 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
283 kd
->subkey_count
= res
->count
;
284 kd
->subkeys
= talloc_steal(kd
, res
->msgs
);
290 static WERROR
cache_values(struct ldb_key_data
*kd
)
292 struct ldb_context
*c
= kd
->ldb
;
293 struct ldb_result
*res
;
296 ret
= ldb_search(c
, c
, &res
, kd
->dn
, LDB_SCOPE_ONELEVEL
,
299 if (ret
!= LDB_SUCCESS
) {
300 DEBUG(0, ("Error getting values for '%s': %s\n",
301 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
305 kd
->value_count
= res
->count
;
306 kd
->values
= talloc_steal(kd
, res
->msgs
);
313 static WERROR
ldb_get_subkey_by_id(TALLOC_CTX
*mem_ctx
,
314 const struct hive_key
*k
, uint32_t idx
,
316 const char **classname
,
317 NTTIME
*last_mod_time
)
319 struct ldb_message_element
*el
;
320 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
325 if (classname
!= NULL
)
326 *classname
= NULL
; /* TODO: Store properly */
327 if (last_mod_time
!= NULL
)
328 *last_mod_time
= 0; /* TODO: we need to add this to the
329 ldb backend properly */
331 /* Do a search if necessary */
332 if (kd
->subkeys
== NULL
) {
333 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd
));
336 if (idx
>= kd
->subkey_count
)
337 return WERR_NO_MORE_ITEMS
;
339 el
= ldb_msg_find_element(kd
->subkeys
[idx
], "key");
340 SMB_ASSERT(el
!= NULL
);
341 SMB_ASSERT(el
->num_values
!= 0);
344 *name
= talloc_strdup(mem_ctx
, (char *)el
->values
[0].data
);
349 static WERROR
ldb_get_default_value(TALLOC_CTX
*mem_ctx
, struct hive_key
*k
,
350 const char **name
, uint32_t *data_type
,
353 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
354 struct ldb_context
*c
= kd
->ldb
;
355 const char* attrs
[] = { "data", "type", NULL
};
356 struct ldb_result
*res
;
359 ret
= ldb_search(c
, mem_ctx
, &res
, kd
->dn
, LDB_SCOPE_BASE
, attrs
, "%s", "");
361 if (ret
!= LDB_SUCCESS
) {
362 DEBUG(0, ("Error getting default value for '%s': %s\n",
363 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
367 if (res
->count
== 0 || res
->msgs
[0]->num_elements
== 0)
370 reg_ldb_unpack_value(mem_ctx
,
371 res
->msgs
[0], name
, data_type
, data
);
378 static WERROR
ldb_get_value_by_id(TALLOC_CTX
*mem_ctx
, struct hive_key
*k
,
379 uint32_t idx
, const char **name
,
380 uint32_t *data_type
, DATA_BLOB
*data
)
382 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
384 /* if default value exists, give it back */
385 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx
, k
, name
, data_type
,
393 /* Do the search if necessary */
394 if (kd
->values
== NULL
) {
395 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
398 if (idx
>= kd
->value_count
)
399 return WERR_NO_MORE_ITEMS
;
401 reg_ldb_unpack_value(mem_ctx
, kd
->values
[idx
], name
, data_type
, data
);
406 static WERROR
ldb_get_value(TALLOC_CTX
*mem_ctx
, struct hive_key
*k
,
407 const char *name
, uint32_t *data_type
,
410 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
411 struct ldb_context
*c
= kd
->ldb
;
412 struct ldb_result
*res
;
417 return WERR_INVALID_PARAM
;
420 if (name
[0] == '\0') {
422 return ldb_get_default_value(mem_ctx
, k
, NULL
, data_type
, data
);
425 query
= talloc_asprintf(mem_ctx
, "(value=%s)", name
);
426 ret
= ldb_search(c
, mem_ctx
, &res
, kd
->dn
, LDB_SCOPE_ONELEVEL
, NULL
, "%s", query
);
429 if (ret
!= LDB_SUCCESS
) {
430 DEBUG(0, ("Error getting values for '%s': %s\n",
431 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
438 reg_ldb_unpack_value(mem_ctx
, res
->msgs
[0], NULL
, data_type
, data
);
446 static WERROR
ldb_open_key(TALLOC_CTX
*mem_ctx
, const struct hive_key
*h
,
447 const char *name
, struct hive_key
**key
)
449 struct ldb_result
*res
;
450 struct ldb_dn
*ldap_path
;
452 struct ldb_key_data
*newkd
;
453 struct ldb_key_data
*kd
= talloc_get_type(h
, struct ldb_key_data
);
454 struct ldb_context
*c
= kd
->ldb
;
456 ldap_path
= reg_path_to_ldb(mem_ctx
, h
, name
, NULL
);
458 ret
= ldb_search(c
, mem_ctx
, &res
, ldap_path
, LDB_SCOPE_BASE
, NULL
, "(key=*)");
460 if (ret
!= LDB_SUCCESS
) {
461 DEBUG(3, ("Error opening key '%s': %s\n",
462 ldb_dn_get_linearized(ldap_path
), ldb_errstring(c
)));
464 } else if (res
->count
== 0) {
465 DEBUG(3, ("Key '%s' not found\n",
466 ldb_dn_get_linearized(ldap_path
)));
471 newkd
= talloc_zero(mem_ctx
, struct ldb_key_data
);
472 newkd
->key
.ops
= ®_backend_ldb
;
473 newkd
->ldb
= talloc_reference(newkd
, kd
->ldb
);
474 newkd
->dn
= ldb_dn_copy(mem_ctx
, res
->msgs
[0]->dn
);
476 *key
= (struct hive_key
*)newkd
;
481 WERROR
reg_open_ldb_file(TALLOC_CTX
*parent_ctx
, const char *location
,
482 struct auth_session_info
*session_info
,
483 struct cli_credentials
*credentials
,
484 struct tevent_context
*ev_ctx
,
485 struct loadparm_context
*lp_ctx
,
488 struct ldb_key_data
*kd
;
489 struct ldb_context
*wrap
;
490 struct ldb_message
*attrs_msg
;
492 if (location
== NULL
)
493 return WERR_INVALID_PARAM
;
495 wrap
= ldb_wrap_connect(parent_ctx
, ev_ctx
, lp_ctx
,
496 location
, session_info
, credentials
, 0);
499 DEBUG(1, (__FILE__
": unable to connect\n"));
503 attrs_msg
= ldb_msg_new(wrap
);
504 W_ERROR_HAVE_NO_MEMORY(attrs_msg
);
505 attrs_msg
->dn
= ldb_dn_new(attrs_msg
, wrap
, "@ATTRIBUTES");
506 W_ERROR_HAVE_NO_MEMORY(attrs_msg
->dn
);
507 ldb_msg_add_string(attrs_msg
, "key", "CASE_INSENSITIVE");
508 ldb_msg_add_string(attrs_msg
, "value", "CASE_INSENSITIVE");
510 ldb_add(wrap
, attrs_msg
);
512 ldb_set_debug_stderr(wrap
);
514 kd
= talloc_zero(parent_ctx
, struct ldb_key_data
);
515 kd
->key
.ops
= ®_backend_ldb
;
516 kd
->ldb
= talloc_reference(kd
, wrap
);
517 talloc_set_destructor (kd
, reg_close_ldb_key
);
518 kd
->dn
= ldb_dn_new(kd
, wrap
, "hive=NONE");
520 *k
= (struct hive_key
*)kd
;
525 static WERROR
ldb_add_key(TALLOC_CTX
*mem_ctx
, const struct hive_key
*parent
,
526 const char *name
, const char *classname
,
527 struct security_descriptor
*sd
,
528 struct hive_key
**newkey
)
530 struct ldb_key_data
*parentkd
= discard_const_p(struct ldb_key_data
, parent
);
531 struct ldb_message
*msg
;
532 struct ldb_key_data
*newkd
;
535 msg
= ldb_msg_new(mem_ctx
);
537 msg
->dn
= reg_path_to_ldb(msg
, parent
, name
, NULL
);
539 ldb_msg_add_string(msg
, "key", talloc_strdup(mem_ctx
, name
));
540 if (classname
!= NULL
)
541 ldb_msg_add_string(msg
, "classname",
542 talloc_strdup(mem_ctx
, classname
));
544 ret
= ldb_add(parentkd
->ldb
, msg
);
545 if (ret
== LDB_ERR_ENTRY_ALREADY_EXISTS
) {
546 return WERR_ALREADY_EXISTS
;
549 if (ret
!= LDB_SUCCESS
) {
550 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd
->ldb
)));
554 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg
->dn
)));
556 newkd
= talloc_zero(mem_ctx
, struct ldb_key_data
);
557 newkd
->ldb
= talloc_reference(newkd
, parentkd
->ldb
);
558 newkd
->key
.ops
= ®_backend_ldb
;
559 newkd
->dn
= talloc_steal(newkd
, msg
->dn
);
561 *newkey
= (struct hive_key
*)newkd
;
564 talloc_free(parentkd
->subkeys
);
565 parentkd
->subkeys
= NULL
;
570 static WERROR
ldb_del_value (struct hive_key
*key
, const char *child
)
573 struct ldb_key_data
*kd
= talloc_get_type(key
, struct ldb_key_data
);
575 struct ldb_message
*msg
;
576 struct ldb_dn
*childdn
;
578 if ((child
== NULL
) || (child
[0] == '\0')) {
580 mem_ctx
= talloc_init("ldb_del_value");
582 msg
= talloc_zero(mem_ctx
, struct ldb_message
);
583 msg
->dn
= ldb_dn_copy(msg
, kd
->dn
);
584 ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
585 ldb_msg_add_empty(msg
, "type", LDB_FLAG_MOD_DELETE
, NULL
);
587 ret
= ldb_modify(kd
->ldb
, msg
);
588 if (ret
!= LDB_SUCCESS
) {
589 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd
->ldb
)));
590 talloc_free(mem_ctx
);
594 talloc_free(mem_ctx
);
597 childdn
= ldb_dn_copy(kd
->ldb
, kd
->dn
);
598 if (!ldb_dn_add_child_fmt(childdn
, "value=%s",
599 reg_ldb_escape(childdn
, child
)))
601 talloc_free(childdn
);
605 ret
= ldb_delete(kd
->ldb
, childdn
);
607 talloc_free(childdn
);
609 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
611 } else if (ret
!= LDB_SUCCESS
) {
612 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd
->ldb
)));
618 talloc_free(kd
->values
);
624 static WERROR
ldb_del_key(const struct hive_key
*key
, const char *name
)
628 struct ldb_key_data
*parentkd
= talloc_get_type(key
, struct ldb_key_data
);
629 struct ldb_dn
*ldap_path
;
630 TALLOC_CTX
*mem_ctx
= talloc_init("ldb_del_key");
631 struct ldb_context
*c
= parentkd
->ldb
;
632 struct ldb_result
*res_keys
;
633 struct ldb_result
*res_vals
;
637 /* Verify key exists by opening it */
638 werr
= ldb_open_key(mem_ctx
, key
, name
, &hk
);
639 if (!W_ERROR_IS_OK(werr
)) {
640 talloc_free(mem_ctx
);
644 ldap_path
= reg_path_to_ldb(mem_ctx
, key
, name
, NULL
);
646 talloc_free(mem_ctx
);
650 /* Search for subkeys */
651 ret
= ldb_search(c
, mem_ctx
, &res_keys
, ldap_path
, LDB_SCOPE_ONELEVEL
,
654 if (ret
!= LDB_SUCCESS
) {
655 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
656 ldb_dn_get_linearized(ldap_path
), ldb_errstring(c
)));
657 talloc_free(mem_ctx
);
661 /* Search for values */
662 ret
= ldb_search(c
, mem_ctx
, &res_vals
, ldap_path
, LDB_SCOPE_ONELEVEL
,
665 if (ret
!= LDB_SUCCESS
) {
666 DEBUG(0, ("Error getting values for '%s': %s\n",
667 ldb_dn_get_linearized(ldap_path
), ldb_errstring(c
)));
668 talloc_free(mem_ctx
);
672 /* Start an explicit transaction */
673 ret
= ldb_transaction_start(c
);
675 if (ret
!= LDB_SUCCESS
) {
676 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c
)));
677 talloc_free(mem_ctx
);
681 if (res_keys
->count
|| res_vals
->count
)
683 /* Delete any subkeys */
684 for (i
= 0; i
< res_keys
->count
; i
++)
686 werr
= ldb_del_key(hk
, ldb_msg_find_attr_as_string(
689 if (!W_ERROR_IS_OK(werr
)) {
690 ret
= ldb_transaction_cancel(c
);
691 talloc_free(mem_ctx
);
696 /* Delete any values */
697 for (i
= 0; i
< res_vals
->count
; i
++)
699 werr
= ldb_del_value(hk
, ldb_msg_find_attr_as_string(
702 if (!W_ERROR_IS_OK(werr
)) {
703 ret
= ldb_transaction_cancel(c
);
704 talloc_free(mem_ctx
);
710 /* Delete the key itself */
711 ret
= ldb_delete(c
, ldap_path
);
713 if (ret
!= LDB_SUCCESS
)
715 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c
)));
716 ret
= ldb_transaction_cancel(c
);
717 talloc_free(mem_ctx
);
721 /* Commit the transaction */
722 ret
= ldb_transaction_commit(c
);
724 if (ret
!= LDB_SUCCESS
)
726 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c
)));
727 ret
= ldb_transaction_cancel(c
);
728 talloc_free(mem_ctx
);
732 talloc_free(mem_ctx
);
735 talloc_free(parentkd
->subkeys
);
736 parentkd
->subkeys
= NULL
;
741 static WERROR
ldb_set_value(struct hive_key
*parent
,
742 const char *name
, uint32_t type
,
743 const DATA_BLOB data
)
745 struct ldb_message
*msg
;
746 struct ldb_key_data
*kd
= talloc_get_type(parent
, struct ldb_key_data
);
749 TALLOC_CTX
*mem_ctx
= talloc_init("ldb_set_value");
751 msg
= reg_ldb_pack_value(kd
->ldb
, mem_ctx
, name
, type
, data
);
752 msg
->dn
= ldb_dn_copy(msg
, kd
->dn
);
754 if ((name
!= NULL
) && (name
[0] != '\0')) {
755 /* For a default value, we add/overwrite the attributes to/of the hive.
756 For a normal value, we create a new child. */
757 if (!ldb_dn_add_child_fmt(msg
->dn
, "value=%s",
758 reg_ldb_escape(mem_ctx
, name
)))
760 talloc_free(mem_ctx
);
765 /* Try first a "modify" and if this doesn't work do try an "add" */
766 for (i
= 0; i
< msg
->num_elements
; i
++) {
767 if (msg
->elements
[i
].flags
!= LDB_FLAG_MOD_DELETE
) {
768 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
771 ret
= ldb_modify(kd
->ldb
, msg
);
772 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
774 while (i
< msg
->num_elements
) {
775 if (msg
->elements
[i
].flags
== LDB_FLAG_MOD_DELETE
) {
776 ldb_msg_remove_element(msg
, &msg
->elements
[i
]);
781 ret
= ldb_add(kd
->ldb
, msg
);
783 if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
) {
784 /* ignore this -> the value didn't exist and also now doesn't */
788 if (ret
!= LDB_SUCCESS
) {
789 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd
->ldb
)));
790 talloc_free(mem_ctx
);
795 talloc_free(kd
->values
);
798 talloc_free(mem_ctx
);
802 static WERROR
ldb_get_key_info(TALLOC_CTX
*mem_ctx
,
803 const struct hive_key
*key
,
804 const char **classname
,
805 uint32_t *num_subkeys
,
806 uint32_t *num_values
,
807 NTTIME
*last_change_time
,
808 uint32_t *max_subkeynamelen
,
809 uint32_t *max_valnamelen
,
810 uint32_t *max_valbufsize
)
812 struct ldb_key_data
*kd
= talloc_get_type(key
, struct ldb_key_data
);
815 if (classname
!= NULL
)
817 if (num_subkeys
!= NULL
)
819 if (num_values
!= NULL
)
821 if (last_change_time
!= NULL
)
822 *last_change_time
= 0;
823 if (max_subkeynamelen
!= NULL
)
824 *max_subkeynamelen
= 0;
825 if (max_valnamelen
!= NULL
)
827 if (max_valbufsize
!= NULL
)
830 if (kd
->subkeys
== NULL
) {
831 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd
));
834 if (kd
->values
== NULL
) {
835 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
838 if (num_subkeys
!= NULL
) {
839 *num_subkeys
= kd
->subkey_count
;
841 if (num_values
!= NULL
) {
842 *num_values
= kd
->value_count
;
846 if (max_subkeynamelen
!= NULL
) {
848 struct ldb_message_element
*el
;
850 *max_subkeynamelen
= 0;
852 for (i
= 0; i
< kd
->subkey_count
; i
++) {
853 el
= ldb_msg_find_element(kd
->subkeys
[i
], "key");
854 *max_subkeynamelen
= MAX(*max_subkeynamelen
, el
->values
[0].length
);
858 if (max_valnamelen
!= NULL
|| max_valbufsize
!= NULL
) {
860 struct ldb_message_element
*el
;
861 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
863 if (max_valbufsize
!= NULL
)
866 if (max_valnamelen
!= NULL
)
869 for (i
= 0; i
< kd
->value_count
; i
++) {
870 if (max_valnamelen
!= NULL
) {
871 el
= ldb_msg_find_element(kd
->values
[i
], "value");
872 *max_valnamelen
= MAX(*max_valnamelen
, el
->values
[0].length
);
875 if (max_valbufsize
!= NULL
) {
878 reg_ldb_unpack_value(mem_ctx
,
881 *max_valbufsize
= MAX(*max_valbufsize
, data
.length
);
882 talloc_free(data
.data
);
890 static struct hive_operations reg_backend_ldb
= {
892 .add_key
= ldb_add_key
,
893 .del_key
= ldb_del_key
,
894 .get_key_by_name
= ldb_open_key
,
895 .enum_value
= ldb_get_value_by_id
,
896 .enum_key
= ldb_get_subkey_by_id
,
897 .set_value
= ldb_set_value
,
898 .get_value_by_name
= ldb_get_value
,
899 .delete_value
= ldb_del_value
,
900 .get_key_info
= ldb_get_key_info
,