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/>.
24 #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
);
76 case REG_DWORD_BIG_ENDIAN
:
79 /* The data is a plain DWORD */
82 tmp
= strtoul_err((char *)val
->data
, NULL
, 0, &error
);
88 data
->data
= talloc_size(mem_ctx
, sizeof(uint32_t));
89 if (data
->data
!= NULL
) {
90 SIVAL(data
->data
, 0, tmp
);
92 data
->length
= sizeof(uint32_t);
102 /* The data is a plain QWORD */
105 tmp
= strtoull_err((char *)val
->data
, NULL
, 0, &error
);
111 data
->data
= talloc_size(mem_ctx
, sizeof(uint64_t));
112 if (data
->data
!= NULL
) {
113 SBVAL(data
->data
, 0, tmp
);
115 data
->length
= sizeof(uint64_t);
125 data
->data
= talloc_memdup(mem_ctx
, val
->data
,
127 data
->length
= val
->length
;
136 static struct ldb_message
*reg_ldb_pack_value(struct ldb_context
*ctx
,
139 uint32_t type
, DATA_BLOB data
)
141 struct ldb_message
*msg
;
142 char *name_dup
, *type_str
;
145 msg
= ldb_msg_new(mem_ctx
);
150 name_dup
= talloc_strdup(msg
, name
);
151 if (name_dup
== NULL
) {
156 ret
= ldb_msg_add_string(msg
, "value", name_dup
);
157 if (ret
!= LDB_SUCCESS
) {
165 if ((data
.length
> 0) && (data
.data
!= NULL
)) {
169 val
= talloc_zero(msg
, struct ldb_val
);
175 /* The data is provided as UTF16 string */
176 ret2
= convert_string_talloc(mem_ctx
, CH_UTF16
, CH_UTF8
,
177 (void *)data
.data
, data
.length
,
178 (void **)&val
->data
, &val
->length
);
180 ret
= ldb_msg_add_value(msg
, "data", val
, NULL
);
182 /* workaround for non-standard data */
183 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
186 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
191 case REG_DWORD_BIG_ENDIAN
:
192 if ((data
.length
> 0) && (data
.data
!= NULL
)) {
193 if (data
.length
== sizeof(uint32_t)) {
196 conv_str
= talloc_asprintf(msg
, "0x%8.8x",
198 if (conv_str
== NULL
) {
202 ret
= ldb_msg_add_string(msg
, "data", conv_str
);
204 /* workaround for non-standard data */
209 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
214 if ((data
.length
> 0) && (data
.data
!= NULL
)) {
215 if (data
.length
== sizeof(uint64_t)) {
218 conv_str
= talloc_asprintf(msg
, "0x%16.16llx",
219 (unsigned long long)BVAL(data
.data
, 0));
220 if (conv_str
== NULL
) {
224 ret
= ldb_msg_add_string(msg
, "data", conv_str
);
226 /* workaround for non-standard data */
232 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
238 if ((data
.length
> 0) && (data
.data
!= NULL
)) {
239 ret
= ldb_msg_add_value(msg
, "data", &data
, NULL
);
241 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
246 if (ret
!= LDB_SUCCESS
) {
251 type_str
= talloc_asprintf(mem_ctx
, "%u", type
);
252 if (type_str
== NULL
) {
257 ret
= ldb_msg_add_string(msg
, "type", type_str
);
258 if (ret
!= LDB_SUCCESS
) {
266 static char *reg_ldb_escape(TALLOC_CTX
*mem_ctx
, const char *value
)
270 val
.data
= discard_const_p(uint8_t, value
);
271 val
.length
= strlen(value
);
273 return ldb_dn_escape_value(mem_ctx
, val
);
276 static int reg_close_ldb_key(struct ldb_key_data
*key
)
278 if (key
->subkeys
!= NULL
) {
279 talloc_free(key
->subkeys
);
283 if (key
->values
!= NULL
) {
284 talloc_free(key
->values
);
290 static struct ldb_dn
*reg_path_to_ldb(TALLOC_CTX
*mem_ctx
,
291 const struct hive_key
*from
,
292 const char *path
, const char *add
)
297 struct ldb_key_data
*kd
= talloc_get_type(from
, struct ldb_key_data
);
298 struct ldb_context
*ldb
= kd
->ldb
;
300 mypath
= talloc_strdup(mem_ctx
, path
);
301 if (mypath
== NULL
) {
305 ret
= ldb_dn_new(mem_ctx
, ldb
, add
);
306 if (!ldb_dn_validate(ret
)) {
311 if (!ldb_dn_add_base(ret
, kd
->dn
)) {
316 while (mypath
[0] != '\0') {
317 begin
= strchr(mypath
, '\\');
322 if (!ldb_dn_add_child_fmt(ret
, "key=%s",
323 reg_ldb_escape(mem_ctx
, mypath
))) {
338 static WERROR
cache_subkeys(struct ldb_key_data
*kd
)
340 struct ldb_context
*c
= kd
->ldb
;
341 struct ldb_result
*res
;
344 ret
= ldb_search(c
, c
, &res
, kd
->dn
, LDB_SCOPE_ONELEVEL
,
346 if (ret
!= LDB_SUCCESS
) {
347 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
348 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
352 kd
->subkey_count
= res
->count
;
353 kd
->subkeys
= talloc_steal(kd
, res
->msgs
);
359 static WERROR
cache_values(struct ldb_key_data
*kd
)
361 struct ldb_context
*c
= kd
->ldb
;
362 struct ldb_result
*res
;
365 ret
= ldb_search(c
, c
, &res
, kd
->dn
, LDB_SCOPE_ONELEVEL
,
367 if (ret
!= LDB_SUCCESS
) {
368 DEBUG(0, ("Error getting values for '%s': %s\n",
369 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
373 kd
->value_count
= res
->count
;
374 kd
->values
= talloc_steal(kd
, res
->msgs
);
381 static WERROR
ldb_get_subkey_by_id(TALLOC_CTX
*mem_ctx
,
382 const struct hive_key
*k
, uint32_t idx
,
384 const char **classname
,
385 NTTIME
*last_mod_time
)
387 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
392 if (classname
!= NULL
)
394 if (last_mod_time
!= NULL
)
395 *last_mod_time
= 0; /* TODO: we need to add this to the
396 ldb backend properly */
398 /* Do a search if necessary */
399 if (kd
->subkeys
== NULL
) {
400 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd
));
403 if (idx
>= kd
->subkey_count
)
404 return WERR_NO_MORE_ITEMS
;
407 *name
= talloc_strdup(mem_ctx
,
408 ldb_msg_find_attr_as_string(kd
->subkeys
[idx
], "key", NULL
));
409 if (classname
!= NULL
)
410 *classname
= talloc_strdup(mem_ctx
,
411 ldb_msg_find_attr_as_string(kd
->subkeys
[idx
], "classname", NULL
));
416 static WERROR
ldb_get_default_value(TALLOC_CTX
*mem_ctx
,
417 const struct hive_key
*k
,
418 const char **name
, uint32_t *data_type
,
421 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
422 struct ldb_context
*c
= kd
->ldb
;
423 const char* attrs
[] = { "data", "type", NULL
};
424 struct ldb_result
*res
;
427 ret
= ldb_search(c
, mem_ctx
, &res
, kd
->dn
, LDB_SCOPE_BASE
, attrs
,
430 if (ret
!= LDB_SUCCESS
) {
431 DEBUG(0, ("Error getting default value for '%s': %s\n",
432 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
436 if (res
->count
== 0 || res
->msgs
[0]->num_elements
== 0) {
438 return WERR_FILE_NOT_FOUND
;
441 if ((data_type
!= NULL
) && (data
!= NULL
)) {
442 reg_ldb_unpack_value(mem_ctx
, res
->msgs
[0], name
, data_type
,
451 static WERROR
ldb_get_value_by_id(TALLOC_CTX
*mem_ctx
, struct hive_key
*k
,
452 uint32_t idx
, const char **name
,
453 uint32_t *data_type
, DATA_BLOB
*data
)
455 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
457 /* if the default value exists, give it back */
458 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx
, k
, name
, data_type
,
466 /* Do the search if necessary */
467 if (kd
->values
== NULL
) {
468 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
471 if (idx
>= kd
->value_count
)
472 return WERR_NO_MORE_ITEMS
;
474 reg_ldb_unpack_value(mem_ctx
, kd
->values
[idx
], name
, data_type
, data
);
479 static WERROR
ldb_get_value(TALLOC_CTX
*mem_ctx
, struct hive_key
*k
,
480 const char *name
, uint32_t *data_type
,
483 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
484 const char *res_name
;
487 /* the default value was requested, give it back */
488 if (name
[0] == '\0') {
489 return ldb_get_default_value(mem_ctx
, k
, NULL
, data_type
, data
);
492 /* Do the search if necessary */
493 if (kd
->values
== NULL
) {
494 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
497 for (idx
= 0; idx
< kd
->value_count
; idx
++) {
498 res_name
= ldb_msg_find_attr_as_string(kd
->values
[idx
], "value",
500 if (ldb_attr_cmp(name
, res_name
) == 0) {
501 reg_ldb_unpack_value(mem_ctx
, kd
->values
[idx
], NULL
,
507 return WERR_FILE_NOT_FOUND
;
510 static WERROR
ldb_open_key(TALLOC_CTX
*mem_ctx
, const struct hive_key
*h
,
511 const char *name
, struct hive_key
**key
)
513 struct ldb_result
*res
;
514 struct ldb_dn
*ldb_path
;
516 struct ldb_key_data
*newkd
;
517 struct ldb_key_data
*kd
= talloc_get_type(h
, struct ldb_key_data
);
518 struct ldb_context
*c
= kd
->ldb
;
520 ldb_path
= reg_path_to_ldb(mem_ctx
, h
, name
, NULL
);
521 W_ERROR_HAVE_NO_MEMORY(ldb_path
);
523 ret
= ldb_search(c
, mem_ctx
, &res
, ldb_path
, LDB_SCOPE_BASE
, NULL
,
526 if (ret
!= LDB_SUCCESS
) {
527 DEBUG(3, ("Error opening key '%s': %s\n",
528 ldb_dn_get_linearized(ldb_path
), ldb_errstring(c
)));
530 } else if (res
->count
== 0) {
531 DEBUG(3, ("Key '%s' not found\n",
532 ldb_dn_get_linearized(ldb_path
)));
534 return WERR_FILE_NOT_FOUND
;
537 newkd
= talloc_zero(mem_ctx
, struct ldb_key_data
);
538 W_ERROR_HAVE_NO_MEMORY(newkd
);
539 newkd
->key
.ops
= ®_backend_ldb
;
540 newkd
->ldb
= talloc_reference(newkd
, kd
->ldb
);
541 newkd
->dn
= ldb_dn_copy(newkd
, res
->msgs
[0]->dn
);
542 newkd
->classname
= talloc_steal(newkd
,
543 ldb_msg_find_attr_as_string(res
->msgs
[0], "classname", NULL
));
547 *key
= (struct hive_key
*)newkd
;
552 WERROR
reg_open_ldb_file(TALLOC_CTX
*parent_ctx
, const char *location
,
553 struct auth_session_info
*session_info
,
554 struct cli_credentials
*credentials
,
555 struct tevent_context
*ev_ctx
,
556 struct loadparm_context
*lp_ctx
,
559 struct ldb_key_data
*kd
;
560 struct ldb_context
*wrap
;
561 struct ldb_message
*attrs_msg
;
563 if (location
== NULL
)
564 return WERR_INVALID_PARAMETER
;
566 wrap
= ldb_wrap_connect(parent_ctx
, ev_ctx
, lp_ctx
,
567 location
, session_info
, credentials
, 0);
570 DEBUG(1, (__FILE__
": unable to connect\n"));
574 attrs_msg
= ldb_msg_new(wrap
);
575 W_ERROR_HAVE_NO_MEMORY(attrs_msg
);
576 attrs_msg
->dn
= ldb_dn_new(attrs_msg
, wrap
, "@ATTRIBUTES");
577 W_ERROR_HAVE_NO_MEMORY(attrs_msg
->dn
);
578 ldb_msg_add_string(attrs_msg
, "key", "CASE_INSENSITIVE");
579 ldb_msg_add_string(attrs_msg
, "value", "CASE_INSENSITIVE");
581 ldb_add(wrap
, attrs_msg
);
583 ldb_set_debug_stderr(wrap
);
585 kd
= talloc_zero(parent_ctx
, struct ldb_key_data
);
586 kd
->key
.ops
= ®_backend_ldb
;
587 kd
->ldb
= talloc_reference(kd
, wrap
);
588 talloc_set_destructor (kd
, reg_close_ldb_key
);
589 kd
->dn
= ldb_dn_new(kd
, wrap
, "hive=NONE");
591 *k
= (struct hive_key
*)kd
;
596 static WERROR
ldb_add_key(TALLOC_CTX
*mem_ctx
, const struct hive_key
*parent
,
597 const char *name
, const char *classname
,
598 struct security_descriptor
*sd
,
599 struct hive_key
**newkey
)
601 struct ldb_key_data
*parentkd
= discard_const_p(struct ldb_key_data
, parent
);
602 struct ldb_dn
*ldb_path
;
603 struct ldb_message
*msg
;
604 struct ldb_key_data
*newkd
;
607 ldb_path
= reg_path_to_ldb(mem_ctx
, parent
, name
, NULL
);
608 W_ERROR_HAVE_NO_MEMORY(ldb_path
);
610 msg
= ldb_msg_new(mem_ctx
);
611 W_ERROR_HAVE_NO_MEMORY(msg
);
615 ldb_msg_add_string(msg
, "key", name
);
616 if (classname
!= NULL
) {
617 ldb_msg_add_string(msg
, "classname", classname
);
620 ret
= ldb_add(parentkd
->ldb
, msg
);
624 if (ret
== LDB_ERR_ENTRY_ALREADY_EXISTS
) {
625 return WERR_ALREADY_EXISTS
;
628 if (ret
!= LDB_SUCCESS
) {
629 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd
->ldb
)));
633 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(ldb_path
)));
635 newkd
= talloc_zero(mem_ctx
, struct ldb_key_data
);
636 W_ERROR_HAVE_NO_MEMORY(newkd
);
637 newkd
->ldb
= talloc_reference(newkd
, parentkd
->ldb
);
638 newkd
->key
.ops
= ®_backend_ldb
;
639 newkd
->dn
= talloc_steal(newkd
, ldb_path
);
640 newkd
->classname
= talloc_steal(newkd
, classname
);
642 *newkey
= (struct hive_key
*)newkd
;
645 talloc_free(parentkd
->subkeys
);
646 parentkd
->subkeys
= NULL
;
651 static WERROR
ldb_del_value(TALLOC_CTX
*mem_ctx
, struct hive_key
*key
,
655 struct ldb_key_data
*kd
= talloc_get_type(key
, struct ldb_key_data
);
656 struct ldb_message
*msg
;
657 struct ldb_dn
*childdn
;
659 if (child
[0] == '\0') {
661 msg
= ldb_msg_new(mem_ctx
);
662 W_ERROR_HAVE_NO_MEMORY(msg
);
663 msg
->dn
= ldb_dn_copy(msg
, kd
->dn
);
664 W_ERROR_HAVE_NO_MEMORY(msg
->dn
);
665 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
666 if (ret
!= LDB_SUCCESS
) {
669 ret
= ldb_msg_add_empty(msg
, "type", LDB_FLAG_MOD_DELETE
,
671 if (ret
!= LDB_SUCCESS
) {
675 ret
= ldb_modify(kd
->ldb
, msg
);
679 if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
) {
680 return WERR_FILE_NOT_FOUND
;
681 } else if (ret
!= LDB_SUCCESS
) {
682 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd
->ldb
)));
687 childdn
= ldb_dn_copy(kd
->ldb
, kd
->dn
);
688 if (!ldb_dn_add_child_fmt(childdn
, "value=%s",
689 reg_ldb_escape(childdn
, child
)))
691 talloc_free(childdn
);
695 ret
= ldb_delete(kd
->ldb
, childdn
);
697 talloc_free(childdn
);
699 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
700 return WERR_FILE_NOT_FOUND
;
701 } else if (ret
!= LDB_SUCCESS
) {
702 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd
->ldb
)));
708 talloc_free(kd
->values
);
714 static WERROR
ldb_del_key(TALLOC_CTX
*mem_ctx
, const struct hive_key
*key
,
719 struct ldb_key_data
*parentkd
= talloc_get_type(key
, struct ldb_key_data
);
720 struct ldb_dn
*ldb_path
;
721 struct ldb_context
*c
= parentkd
->ldb
;
722 struct ldb_result
*res_keys
;
723 struct ldb_result
*res_vals
;
727 /* Verify key exists by opening it */
728 werr
= ldb_open_key(mem_ctx
, key
, name
, &hk
);
729 if (!W_ERROR_IS_OK(werr
)) {
733 ldb_path
= reg_path_to_ldb(mem_ctx
, key
, name
, NULL
);
734 W_ERROR_HAVE_NO_MEMORY(ldb_path
);
736 /* Search for subkeys */
737 ret
= ldb_search(c
, mem_ctx
, &res_keys
, ldb_path
, LDB_SCOPE_ONELEVEL
,
740 if (ret
!= LDB_SUCCESS
) {
741 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
742 ldb_dn_get_linearized(ldb_path
), ldb_errstring(c
)));
746 /* Search for values */
747 ret
= ldb_search(c
, mem_ctx
, &res_vals
, ldb_path
, LDB_SCOPE_ONELEVEL
,
750 if (ret
!= LDB_SUCCESS
) {
751 DEBUG(0, ("Error getting values for '%s': %s\n",
752 ldb_dn_get_linearized(ldb_path
), ldb_errstring(c
)));
756 /* Start an explicit transaction */
757 ret
= ldb_transaction_start(c
);
759 if (ret
!= LDB_SUCCESS
) {
760 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c
)));
764 if (res_keys
->count
|| res_vals
->count
)
766 /* Delete any subkeys */
767 for (i
= 0; i
< res_keys
->count
; i
++)
769 werr
= ldb_del_key(mem_ctx
, hk
,
770 ldb_msg_find_attr_as_string(
773 if (!W_ERROR_IS_OK(werr
)) {
774 ret
= ldb_transaction_cancel(c
);
779 /* Delete any values */
780 for (i
= 0; i
< res_vals
->count
; i
++)
782 werr
= ldb_del_value(mem_ctx
, hk
,
783 ldb_msg_find_attr_as_string(
786 if (!W_ERROR_IS_OK(werr
)) {
787 ret
= ldb_transaction_cancel(c
);
792 talloc_free(res_keys
);
793 talloc_free(res_vals
);
795 /* Delete the key itself */
796 ret
= ldb_delete(c
, ldb_path
);
798 if (ret
!= LDB_SUCCESS
)
800 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c
)));
801 ret
= ldb_transaction_cancel(c
);
805 /* Commit the transaction */
806 ret
= ldb_transaction_commit(c
);
808 if (ret
!= LDB_SUCCESS
)
810 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c
)));
811 ret
= ldb_transaction_cancel(c
);
816 talloc_free(parentkd
->subkeys
);
817 parentkd
->subkeys
= NULL
;
822 static WERROR
ldb_set_value(struct hive_key
*parent
,
823 const char *name
, uint32_t type
,
824 const DATA_BLOB data
)
826 struct ldb_message
*msg
;
827 struct ldb_key_data
*kd
= talloc_get_type(parent
, struct ldb_key_data
);
830 TALLOC_CTX
*mem_ctx
= talloc_init("ldb_set_value");
832 msg
= reg_ldb_pack_value(kd
->ldb
, mem_ctx
, name
, type
, data
);
833 W_ERROR_HAVE_NO_MEMORY(msg
);
835 msg
->dn
= ldb_dn_copy(msg
, kd
->dn
);
836 W_ERROR_HAVE_NO_MEMORY(msg
->dn
);
838 if (name
[0] != '\0') {
839 /* For a default value, we add/overwrite the attributes to/of the hive.
840 For a normal value, we create a new child. */
841 if (!ldb_dn_add_child_fmt(msg
->dn
, "value=%s",
842 reg_ldb_escape(mem_ctx
, name
)))
844 talloc_free(mem_ctx
);
849 /* Try first a "modify" and if this doesn't work do try an "add" */
850 for (i
= 0; i
< msg
->num_elements
; i
++) {
851 if (msg
->elements
[i
].flags
!= LDB_FLAG_MOD_DELETE
) {
852 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
855 ret
= ldb_modify(kd
->ldb
, msg
);
856 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
858 while (i
< msg
->num_elements
) {
859 if (LDB_FLAG_MOD_TYPE(msg
->elements
[i
].flags
) == LDB_FLAG_MOD_DELETE
) {
860 ldb_msg_remove_element(msg
, &msg
->elements
[i
]);
865 ret
= ldb_add(kd
->ldb
, msg
);
867 if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
) {
868 /* ignore this -> the value didn't exist and also now doesn't */
874 if (ret
!= LDB_SUCCESS
) {
875 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd
->ldb
)));
876 talloc_free(mem_ctx
);
881 talloc_free(kd
->values
);
884 talloc_free(mem_ctx
);
888 static WERROR
ldb_get_key_info(TALLOC_CTX
*mem_ctx
,
889 const struct hive_key
*key
,
890 const char **classname
,
891 uint32_t *num_subkeys
,
892 uint32_t *num_values
,
893 NTTIME
*last_change_time
,
894 uint32_t *max_subkeynamelen
,
895 uint32_t *max_valnamelen
,
896 uint32_t *max_valbufsize
)
898 struct ldb_key_data
*kd
= talloc_get_type(key
, struct ldb_key_data
);
899 uint32_t default_value_type
= REG_NONE
;
900 DATA_BLOB default_value
= { NULL
, 0 };
904 if (classname
!= NULL
)
906 if (num_subkeys
!= NULL
)
908 if (num_values
!= NULL
)
910 if (last_change_time
!= NULL
)
911 *last_change_time
= 0;
912 if (max_subkeynamelen
!= NULL
)
913 *max_subkeynamelen
= 0;
914 if (max_valnamelen
!= NULL
)
916 if (max_valbufsize
!= NULL
)
919 /* We need this to get the default value (if it exists) for counting
920 * the values under the key and for finding out the longest value buffer
921 * size. If no default value exists the DATA_BLOB "default_value" will
922 * remain { NULL, 0 }. */
923 werr
= ldb_get_default_value(mem_ctx
, key
, NULL
, &default_value_type
,
925 if ((!W_ERROR_IS_OK(werr
)) && (!W_ERROR_EQUAL(werr
, WERR_FILE_NOT_FOUND
))) {
929 if (kd
->subkeys
== NULL
) {
930 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd
));
932 if (kd
->values
== NULL
) {
933 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
936 if (classname
!= NULL
) {
937 *classname
= kd
->classname
;
940 if (num_subkeys
!= NULL
) {
941 *num_subkeys
= kd
->subkey_count
;
943 if (num_values
!= NULL
) {
944 *num_values
= kd
->value_count
;
945 /* also consider the default value if it exists */
946 if (default_value
.data
!= NULL
) {
952 if (max_subkeynamelen
!= NULL
) {
954 struct ldb_message_element
*el
;
956 for (i
= 0; i
< kd
->subkey_count
; i
++) {
957 el
= ldb_msg_find_element(kd
->subkeys
[i
], "key");
958 *max_subkeynamelen
= MAX(*max_subkeynamelen
, el
->values
[0].length
);
962 if (max_valnamelen
!= NULL
|| max_valbufsize
!= NULL
) {
964 struct ldb_message_element
*el
;
965 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
967 /* also consider the default value if it exists */
968 if ((max_valbufsize
!= NULL
) && (default_value
.data
!= NULL
)) {
969 *max_valbufsize
= MAX(*max_valbufsize
,
970 default_value
.length
);
973 for (i
= 0; i
< kd
->value_count
; i
++) {
974 if (max_valnamelen
!= NULL
) {
975 el
= ldb_msg_find_element(kd
->values
[i
], "value");
976 *max_valnamelen
= MAX(*max_valnamelen
, el
->values
[0].length
);
979 if (max_valbufsize
!= NULL
) {
982 reg_ldb_unpack_value(mem_ctx
,
985 *max_valbufsize
= MAX(*max_valbufsize
, data
.length
);
986 talloc_free(data
.data
);
991 talloc_free(default_value
.data
);
996 static struct hive_operations reg_backend_ldb
= {
998 .add_key
= ldb_add_key
,
999 .del_key
= ldb_del_key
,
1000 .get_key_by_name
= ldb_open_key
,
1001 .enum_value
= ldb_get_value_by_id
,
1002 .enum_key
= ldb_get_subkey_by_id
,
1003 .set_value
= ldb_set_value
,
1004 .get_value_by_name
= ldb_get_value
,
1005 .delete_value
= ldb_del_value
,
1006 .get_key_info
= ldb_get_key_info
,