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 */
193 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
198 if ((data
.length
> 0) && (data
.data
!= NULL
)) {
199 if (data
.length
== sizeof(uint64_t)) {
202 conv_str
= talloc_asprintf(msg
, "0x%16.16llx",
204 if (conv_str
== NULL
) {
208 ret
= ldb_msg_add_string(msg
, "data", conv_str
);
210 /* workaround for non-standard data */
216 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
222 if ((data
.length
> 0) && (data
.data
!= NULL
)) {
223 ret
= ldb_msg_add_value(msg
, "data", &data
, NULL
);
225 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
230 if (ret
!= LDB_SUCCESS
) {
235 type_str
= talloc_asprintf(mem_ctx
, "%u", type
);
236 if (type_str
== NULL
) {
241 ret
= ldb_msg_add_string(msg
, "type", type_str
);
242 if (ret
!= LDB_SUCCESS
) {
250 static char *reg_ldb_escape(TALLOC_CTX
*mem_ctx
, const char *value
)
254 val
.data
= discard_const_p(uint8_t, value
);
255 val
.length
= strlen(value
);
257 return ldb_dn_escape_value(mem_ctx
, val
);
260 static int reg_close_ldb_key(struct ldb_key_data
*key
)
262 if (key
->subkeys
!= NULL
) {
263 talloc_free(key
->subkeys
);
267 if (key
->values
!= NULL
) {
268 talloc_free(key
->values
);
274 static struct ldb_dn
*reg_path_to_ldb(TALLOC_CTX
*mem_ctx
,
275 const struct hive_key
*from
,
276 const char *path
, const char *add
)
278 TALLOC_CTX
*local_ctx
;
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 local_ctx
= talloc_new(mem_ctx
);
287 ret
= ldb_dn_new(mem_ctx
, ldb
, add
);
288 if (!ldb_dn_validate(ret
)) {
290 talloc_free(local_ctx
);
297 begin
= strrchr(mypath
, '\\');
299 if (begin
) keyname
= begin
+ 1;
300 else keyname
= mypath
;
302 if (keyname
[0] != '\0') {
303 if (!ldb_dn_add_base_fmt(ret
, "key=%s",
304 reg_ldb_escape(local_ctx
,
307 talloc_free(local_ctx
);
319 ldb_dn_add_base(ret
, kd
->dn
);
321 talloc_free(local_ctx
);
326 static WERROR
cache_subkeys(struct ldb_key_data
*kd
)
328 struct ldb_context
*c
= kd
->ldb
;
329 struct ldb_result
*res
;
332 ret
= ldb_search(c
, c
, &res
, kd
->dn
, LDB_SCOPE_ONELEVEL
, NULL
, "(key=*)");
334 if (ret
!= LDB_SUCCESS
) {
335 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
336 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
340 kd
->subkey_count
= res
->count
;
341 kd
->subkeys
= talloc_steal(kd
, res
->msgs
);
347 static WERROR
cache_values(struct ldb_key_data
*kd
)
349 struct ldb_context
*c
= kd
->ldb
;
350 struct ldb_result
*res
;
353 ret
= ldb_search(c
, c
, &res
, kd
->dn
, LDB_SCOPE_ONELEVEL
,
356 if (ret
!= LDB_SUCCESS
) {
357 DEBUG(0, ("Error getting values for '%s': %s\n",
358 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
362 kd
->value_count
= res
->count
;
363 kd
->values
= talloc_steal(kd
, res
->msgs
);
370 static WERROR
ldb_get_subkey_by_id(TALLOC_CTX
*mem_ctx
,
371 const struct hive_key
*k
, uint32_t idx
,
373 const char **classname
,
374 NTTIME
*last_mod_time
)
376 struct ldb_message_element
*el
;
377 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
382 if (classname
!= NULL
)
383 *classname
= NULL
; /* TODO: Store properly */
384 if (last_mod_time
!= NULL
)
385 *last_mod_time
= 0; /* TODO: we need to add this to the
386 ldb backend properly */
388 /* Do a search if necessary */
389 if (kd
->subkeys
== NULL
) {
390 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd
));
393 if (idx
>= kd
->subkey_count
)
394 return WERR_NO_MORE_ITEMS
;
396 el
= ldb_msg_find_element(kd
->subkeys
[idx
], "key");
397 SMB_ASSERT(el
!= NULL
);
398 SMB_ASSERT(el
->num_values
!= 0);
401 *name
= talloc_strdup(mem_ctx
, (char *)el
->values
[0].data
);
406 static WERROR
ldb_get_default_value(TALLOC_CTX
*mem_ctx
,
407 const struct hive_key
*k
,
408 const char **name
, uint32_t *data_type
,
411 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
412 struct ldb_context
*c
= kd
->ldb
;
413 const char* attrs
[] = { "data", "type", NULL
};
414 struct ldb_result
*res
;
417 ret
= ldb_search(c
, mem_ctx
, &res
, kd
->dn
, LDB_SCOPE_BASE
, attrs
, "(dn=*)");
419 if (ret
!= LDB_SUCCESS
) {
420 DEBUG(0, ("Error getting default value for '%s': %s\n",
421 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
425 if (res
->count
== 0 || res
->msgs
[0]->num_elements
== 0)
428 if ((data_type
!= NULL
) && (data
!= NULL
)) {
429 reg_ldb_unpack_value(mem_ctx
, res
->msgs
[0], name
, data_type
,
438 static WERROR
ldb_get_value_by_id(TALLOC_CTX
*mem_ctx
, struct hive_key
*k
,
439 uint32_t idx
, const char **name
,
440 uint32_t *data_type
, DATA_BLOB
*data
)
442 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
444 /* if default value exists, give it back */
445 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx
, k
, name
, data_type
,
453 /* Do the search if necessary */
454 if (kd
->values
== NULL
) {
455 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
458 if (idx
>= kd
->value_count
)
459 return WERR_NO_MORE_ITEMS
;
461 reg_ldb_unpack_value(mem_ctx
, kd
->values
[idx
], name
, data_type
, data
);
466 static WERROR
ldb_get_value(TALLOC_CTX
*mem_ctx
, struct hive_key
*k
,
467 const char *name
, uint32_t *data_type
,
470 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
471 struct ldb_context
*c
= kd
->ldb
;
472 struct ldb_result
*res
;
476 return WERR_INVALID_PARAM
;
479 if (name
[0] == '\0') {
481 return ldb_get_default_value(mem_ctx
, k
, NULL
, data_type
, data
);
484 ret
= ldb_search(c
, mem_ctx
, &res
, kd
->dn
, LDB_SCOPE_ONELEVEL
,
485 NULL
, "(value=%s)", name
);
487 if (ret
!= LDB_SUCCESS
) {
488 DEBUG(0, ("Error getting values for '%s': %s\n",
489 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
496 reg_ldb_unpack_value(mem_ctx
, res
->msgs
[0], NULL
, data_type
, data
);
504 static WERROR
ldb_open_key(TALLOC_CTX
*mem_ctx
, const struct hive_key
*h
,
505 const char *name
, struct hive_key
**key
)
507 struct ldb_result
*res
;
508 struct ldb_dn
*ldap_path
;
510 struct ldb_key_data
*newkd
;
511 struct ldb_key_data
*kd
= talloc_get_type(h
, struct ldb_key_data
);
512 struct ldb_context
*c
= kd
->ldb
;
515 return WERR_INVALID_PARAM
;
518 ldap_path
= reg_path_to_ldb(mem_ctx
, h
, name
, NULL
);
519 W_ERROR_HAVE_NO_MEMORY(ldap_path
);
521 ret
= ldb_search(c
, mem_ctx
, &res
, ldap_path
, LDB_SCOPE_BASE
, NULL
, "(key=*)");
523 if (ret
!= LDB_SUCCESS
) {
524 DEBUG(3, ("Error opening key '%s': %s\n",
525 ldb_dn_get_linearized(ldap_path
), ldb_errstring(c
)));
527 } else if (res
->count
== 0) {
528 DEBUG(3, ("Key '%s' not found\n",
529 ldb_dn_get_linearized(ldap_path
)));
534 newkd
= talloc_zero(mem_ctx
, struct ldb_key_data
);
535 newkd
->key
.ops
= ®_backend_ldb
;
536 newkd
->ldb
= talloc_reference(newkd
, kd
->ldb
);
537 newkd
->dn
= ldb_dn_copy(mem_ctx
, res
->msgs
[0]->dn
);
539 *key
= (struct hive_key
*)newkd
;
544 WERROR
reg_open_ldb_file(TALLOC_CTX
*parent_ctx
, const char *location
,
545 struct auth_session_info
*session_info
,
546 struct cli_credentials
*credentials
,
547 struct tevent_context
*ev_ctx
,
548 struct loadparm_context
*lp_ctx
,
551 struct ldb_key_data
*kd
;
552 struct ldb_context
*wrap
;
553 struct ldb_message
*attrs_msg
;
555 if (location
== NULL
)
556 return WERR_INVALID_PARAM
;
558 wrap
= ldb_wrap_connect(parent_ctx
, ev_ctx
, lp_ctx
,
559 location
, session_info
, credentials
, 0);
562 DEBUG(1, (__FILE__
": unable to connect\n"));
566 attrs_msg
= ldb_msg_new(wrap
);
567 W_ERROR_HAVE_NO_MEMORY(attrs_msg
);
568 attrs_msg
->dn
= ldb_dn_new(attrs_msg
, wrap
, "@ATTRIBUTES");
569 W_ERROR_HAVE_NO_MEMORY(attrs_msg
->dn
);
570 ldb_msg_add_string(attrs_msg
, "key", "CASE_INSENSITIVE");
571 ldb_msg_add_string(attrs_msg
, "value", "CASE_INSENSITIVE");
573 ldb_add(wrap
, attrs_msg
);
575 ldb_set_debug_stderr(wrap
);
577 kd
= talloc_zero(parent_ctx
, struct ldb_key_data
);
578 kd
->key
.ops
= ®_backend_ldb
;
579 kd
->ldb
= talloc_reference(kd
, wrap
);
580 talloc_set_destructor (kd
, reg_close_ldb_key
);
581 kd
->dn
= ldb_dn_new(kd
, wrap
, "hive=NONE");
583 *k
= (struct hive_key
*)kd
;
588 static WERROR
ldb_add_key(TALLOC_CTX
*mem_ctx
, const struct hive_key
*parent
,
589 const char *name
, const char *classname
,
590 struct security_descriptor
*sd
,
591 struct hive_key
**newkey
)
593 struct ldb_key_data
*parentkd
= discard_const_p(struct ldb_key_data
, parent
);
594 struct ldb_message
*msg
;
595 struct ldb_key_data
*newkd
;
599 return WERR_INVALID_PARAM
;
602 msg
= ldb_msg_new(mem_ctx
);
603 W_ERROR_HAVE_NO_MEMORY(msg
);
605 msg
->dn
= reg_path_to_ldb(msg
, parent
, name
, NULL
);
606 W_ERROR_HAVE_NO_MEMORY(msg
->dn
);
608 ldb_msg_add_string(msg
, "key", talloc_strdup(mem_ctx
, name
));
609 if (classname
!= NULL
)
610 ldb_msg_add_string(msg
, "classname",
611 talloc_strdup(mem_ctx
, classname
));
613 ret
= ldb_add(parentkd
->ldb
, msg
);
614 if (ret
== LDB_ERR_ENTRY_ALREADY_EXISTS
) {
615 return WERR_ALREADY_EXISTS
;
618 if (ret
!= LDB_SUCCESS
) {
619 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd
->ldb
)));
623 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg
->dn
)));
625 newkd
= talloc_zero(mem_ctx
, struct ldb_key_data
);
626 W_ERROR_HAVE_NO_MEMORY(newkd
);
627 newkd
->ldb
= talloc_reference(newkd
, parentkd
->ldb
);
628 newkd
->key
.ops
= ®_backend_ldb
;
629 newkd
->dn
= talloc_steal(newkd
, msg
->dn
);
631 *newkey
= (struct hive_key
*)newkd
;
634 talloc_free(parentkd
->subkeys
);
635 parentkd
->subkeys
= NULL
;
640 static WERROR
ldb_del_value(TALLOC_CTX
*mem_ctx
, struct hive_key
*key
,
644 struct ldb_key_data
*kd
= talloc_get_type(key
, struct ldb_key_data
);
645 struct ldb_message
*msg
;
646 struct ldb_dn
*childdn
;
649 return WERR_INVALID_PARAM
;
652 if (child
[0] == '\0') {
654 msg
= talloc_zero(mem_ctx
, struct ldb_message
);
655 W_ERROR_HAVE_NO_MEMORY(msg
);
656 msg
->dn
= ldb_dn_copy(msg
, kd
->dn
);
657 W_ERROR_HAVE_NO_MEMORY(msg
->dn
);
658 ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
659 ldb_msg_add_empty(msg
, "type", LDB_FLAG_MOD_DELETE
, NULL
);
661 ret
= ldb_modify(kd
->ldb
, msg
);
662 if (ret
!= LDB_SUCCESS
) {
663 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd
->ldb
)));
668 childdn
= ldb_dn_copy(kd
->ldb
, kd
->dn
);
669 if (!ldb_dn_add_child_fmt(childdn
, "value=%s",
670 reg_ldb_escape(childdn
, child
)))
672 talloc_free(childdn
);
676 ret
= ldb_delete(kd
->ldb
, childdn
);
678 talloc_free(childdn
);
680 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
682 } else if (ret
!= LDB_SUCCESS
) {
683 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd
->ldb
)));
689 talloc_free(kd
->values
);
695 static WERROR
ldb_del_key(TALLOC_CTX
*mem_ctx
, const struct hive_key
*key
,
700 struct ldb_key_data
*parentkd
= talloc_get_type(key
, struct ldb_key_data
);
701 struct ldb_dn
*ldap_path
;
702 struct ldb_context
*c
= parentkd
->ldb
;
703 struct ldb_result
*res_keys
;
704 struct ldb_result
*res_vals
;
709 return WERR_INVALID_PARAM
;
712 /* Verify key exists by opening it */
713 werr
= ldb_open_key(mem_ctx
, key
, name
, &hk
);
714 if (!W_ERROR_IS_OK(werr
)) {
718 ldap_path
= reg_path_to_ldb(mem_ctx
, key
, name
, NULL
);
719 W_ERROR_HAVE_NO_MEMORY(ldap_path
);
721 /* Search for subkeys */
722 ret
= ldb_search(c
, mem_ctx
, &res_keys
, ldap_path
, LDB_SCOPE_ONELEVEL
,
725 if (ret
!= LDB_SUCCESS
) {
726 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
727 ldb_dn_get_linearized(ldap_path
), ldb_errstring(c
)));
731 /* Search for values */
732 ret
= ldb_search(c
, mem_ctx
, &res_vals
, ldap_path
, LDB_SCOPE_ONELEVEL
,
735 if (ret
!= LDB_SUCCESS
) {
736 DEBUG(0, ("Error getting values for '%s': %s\n",
737 ldb_dn_get_linearized(ldap_path
), ldb_errstring(c
)));
741 /* Start an explicit transaction */
742 ret
= ldb_transaction_start(c
);
744 if (ret
!= LDB_SUCCESS
) {
745 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c
)));
749 if (res_keys
->count
|| res_vals
->count
)
751 /* Delete any subkeys */
752 for (i
= 0; i
< res_keys
->count
; i
++)
754 werr
= ldb_del_key(mem_ctx
, hk
,
755 ldb_msg_find_attr_as_string(
758 if (!W_ERROR_IS_OK(werr
)) {
759 ret
= ldb_transaction_cancel(c
);
764 /* Delete any values */
765 for (i
= 0; i
< res_vals
->count
; i
++)
767 werr
= ldb_del_value(mem_ctx
, hk
,
768 ldb_msg_find_attr_as_string(
771 if (!W_ERROR_IS_OK(werr
)) {
772 ret
= ldb_transaction_cancel(c
);
778 /* Delete the key itself */
779 ret
= ldb_delete(c
, ldap_path
);
781 if (ret
!= LDB_SUCCESS
)
783 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c
)));
784 ret
= ldb_transaction_cancel(c
);
788 /* Commit the transaction */
789 ret
= ldb_transaction_commit(c
);
791 if (ret
!= LDB_SUCCESS
)
793 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c
)));
794 ret
= ldb_transaction_cancel(c
);
799 talloc_free(parentkd
->subkeys
);
800 parentkd
->subkeys
= NULL
;
805 static WERROR
ldb_set_value(struct hive_key
*parent
,
806 const char *name
, uint32_t type
,
807 const DATA_BLOB data
)
809 struct ldb_message
*msg
;
810 struct ldb_key_data
*kd
= talloc_get_type(parent
, struct ldb_key_data
);
813 TALLOC_CTX
*mem_ctx
= talloc_init("ldb_set_value");
816 return WERR_INVALID_PARAM
;
819 msg
= reg_ldb_pack_value(kd
->ldb
, mem_ctx
, name
, type
, data
);
820 W_ERROR_HAVE_NO_MEMORY(msg
);
822 msg
->dn
= ldb_dn_copy(msg
, kd
->dn
);
823 W_ERROR_HAVE_NO_MEMORY(msg
->dn
);
825 if (name
[0] != '\0') {
826 /* For a default value, we add/overwrite the attributes to/of the hive.
827 For a normal value, we create a new child. */
828 if (!ldb_dn_add_child_fmt(msg
->dn
, "value=%s",
829 reg_ldb_escape(mem_ctx
, name
)))
831 talloc_free(mem_ctx
);
836 /* Try first a "modify" and if this doesn't work do try an "add" */
837 for (i
= 0; i
< msg
->num_elements
; i
++) {
838 if (msg
->elements
[i
].flags
!= LDB_FLAG_MOD_DELETE
) {
839 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
842 ret
= ldb_modify(kd
->ldb
, msg
);
843 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
845 while (i
< msg
->num_elements
) {
846 if (msg
->elements
[i
].flags
== LDB_FLAG_MOD_DELETE
) {
847 ldb_msg_remove_element(msg
, &msg
->elements
[i
]);
852 ret
= ldb_add(kd
->ldb
, msg
);
854 if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
) {
855 /* ignore this -> the value didn't exist and also now doesn't */
859 if (ret
!= LDB_SUCCESS
) {
860 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd
->ldb
)));
861 talloc_free(mem_ctx
);
866 talloc_free(kd
->values
);
869 talloc_free(mem_ctx
);
873 static WERROR
ldb_get_key_info(TALLOC_CTX
*mem_ctx
,
874 const struct hive_key
*key
,
875 const char **classname
,
876 uint32_t *num_subkeys
,
877 uint32_t *num_values
,
878 NTTIME
*last_change_time
,
879 uint32_t *max_subkeynamelen
,
880 uint32_t *max_valnamelen
,
881 uint32_t *max_valbufsize
)
883 struct ldb_key_data
*kd
= talloc_get_type(key
, struct ldb_key_data
);
884 uint32_t default_value_type
= REG_NONE
;
885 DATA_BLOB default_value
= { NULL
, 0 };
889 if (classname
!= NULL
)
891 if (num_subkeys
!= NULL
)
893 if (num_values
!= NULL
)
895 if (last_change_time
!= NULL
)
896 *last_change_time
= 0;
897 if (max_subkeynamelen
!= NULL
)
898 *max_subkeynamelen
= 0;
899 if (max_valnamelen
!= NULL
)
901 if (max_valbufsize
!= NULL
)
904 /* We need this to get the default value (if it exists) for counting
905 * the values under the key and for finding out the longest value buffer
906 * size. If no default value exists the DATA_BLOB "default_value" will
907 * remain { NULL, 0 }. */
908 werr
= ldb_get_default_value(mem_ctx
, key
, NULL
, &default_value_type
,
910 if ((!W_ERROR_IS_OK(werr
)) && (!W_ERROR_EQUAL(werr
, WERR_BADFILE
))) {
914 if (kd
->subkeys
== NULL
) {
915 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd
));
918 if (kd
->values
== NULL
) {
919 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
922 if (num_subkeys
!= NULL
) {
923 *num_subkeys
= kd
->subkey_count
;
925 if (num_values
!= NULL
) {
926 *num_values
= kd
->value_count
;
927 /* also consider the default value if it exists */
928 if (default_value
.data
!= NULL
) {
934 if (max_subkeynamelen
!= NULL
) {
936 struct ldb_message_element
*el
;
938 for (i
= 0; i
< kd
->subkey_count
; i
++) {
939 el
= ldb_msg_find_element(kd
->subkeys
[i
], "key");
940 *max_subkeynamelen
= MAX(*max_subkeynamelen
, el
->values
[0].length
);
944 if (max_valnamelen
!= NULL
|| max_valbufsize
!= NULL
) {
946 struct ldb_message_element
*el
;
947 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
949 /* also consider the default value if it exists */
950 if ((max_valbufsize
!= NULL
) && (default_value
.data
!= NULL
)) {
951 *max_valbufsize
= MAX(*max_valbufsize
,
952 default_value
.length
);
955 for (i
= 0; i
< kd
->value_count
; i
++) {
956 if (max_valnamelen
!= NULL
) {
957 el
= ldb_msg_find_element(kd
->values
[i
], "value");
958 *max_valnamelen
= MAX(*max_valnamelen
, el
->values
[0].length
);
961 if (max_valbufsize
!= NULL
) {
964 reg_ldb_unpack_value(mem_ctx
,
967 *max_valbufsize
= MAX(*max_valbufsize
, data
.length
);
968 talloc_free(data
.data
);
973 talloc_free(default_value
.data
);
978 static struct hive_operations reg_backend_ldb
= {
980 .add_key
= ldb_add_key
,
981 .del_key
= ldb_del_key
,
982 .get_key_by_name
= ldb_open_key
,
983 .enum_value
= ldb_get_value_by_id
,
984 .enum_key
= ldb_get_subkey_by_id
,
985 .set_value
= ldb_set_value
,
986 .get_value_by_name
= ldb_get_value
,
987 .delete_value
= ldb_del_value
,
988 .get_key_info
= ldb_get_key_info
,