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"
28 #include "lib/util/smb_strtox.h"
32 static struct hive_operations reg_backend_ldb
;
37 struct ldb_context
*ldb
;
39 struct ldb_message
**subkeys
, **values
;
40 unsigned int subkey_count
, value_count
;
41 const char *classname
;
44 static void reg_ldb_unpack_value(TALLOC_CTX
*mem_ctx
,
45 struct ldb_message
*msg
,
46 const char **name
, uint32_t *type
,
49 const struct ldb_val
*val
;
53 *name
= talloc_strdup(mem_ctx
,
54 ldb_msg_find_attr_as_string(msg
, "value",
58 value_type
= ldb_msg_find_attr_as_uint(msg
, "type", 0);
61 val
= ldb_msg_find_ldb_val(msg
, "data");
68 /* The data should be provided as UTF16 string */
69 convert_string_talloc(mem_ctx
, CH_UTF8
, CH_UTF16
,
70 val
->data
, val
->length
,
71 (void **)&data
->data
, &data
->length
);
79 case REG_DWORD_BIG_ENDIAN
:
82 /* The data is a plain DWORD */
85 tmp
= smb_strtoul((char *)val
->data
,
95 data
->data
= talloc_size(mem_ctx
, sizeof(uint32_t));
96 if (data
->data
!= NULL
) {
97 SIVAL(data
->data
, 0, tmp
);
99 data
->length
= sizeof(uint32_t);
109 /* The data is a plain QWORD */
112 tmp
= smb_strtoull((char *)val
->data
,
122 data
->data
= talloc_size(mem_ctx
, sizeof(uint64_t));
123 if (data
->data
!= NULL
) {
124 SBVAL(data
->data
, 0, tmp
);
126 data
->length
= sizeof(uint64_t);
136 data
->data
= talloc_memdup(mem_ctx
, val
->data
,
138 data
->length
= val
->length
;
147 static struct ldb_message
*reg_ldb_pack_value(struct ldb_context
*ctx
,
150 uint32_t type
, DATA_BLOB data
)
152 struct ldb_message
*msg
;
153 char *name_dup
, *type_str
;
156 msg
= ldb_msg_new(mem_ctx
);
161 name_dup
= talloc_strdup(msg
, name
);
162 if (name_dup
== NULL
) {
167 ret
= ldb_msg_add_string(msg
, "value", name_dup
);
168 if (ret
!= LDB_SUCCESS
) {
176 if ((data
.length
> 0) && (data
.data
!= NULL
)) {
180 val
= talloc_zero(msg
, struct ldb_val
);
186 /* The data is provided as UTF16 string */
187 ret2
= convert_string_talloc(mem_ctx
, CH_UTF16
, CH_UTF8
,
188 (void *)data
.data
, data
.length
,
189 (void **)&val
->data
, &val
->length
);
191 ret
= ldb_msg_add_value(msg
, "data", val
, NULL
);
193 /* workaround for non-standard data */
194 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
197 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
202 case REG_DWORD_BIG_ENDIAN
:
203 if ((data
.length
> 0) && (data
.data
!= NULL
)) {
204 if (data
.length
== sizeof(uint32_t)) {
207 conv_str
= talloc_asprintf(msg
, "0x%8.8x",
209 if (conv_str
== NULL
) {
213 ret
= ldb_msg_add_string(msg
, "data", conv_str
);
215 /* workaround for non-standard data */
220 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
225 if ((data
.length
> 0) && (data
.data
!= NULL
)) {
226 if (data
.length
== sizeof(uint64_t)) {
229 conv_str
= talloc_asprintf(msg
, "0x%16.16llx",
230 (unsigned long long)BVAL(data
.data
, 0));
231 if (conv_str
== NULL
) {
235 ret
= ldb_msg_add_string(msg
, "data", conv_str
);
237 /* workaround for non-standard data */
243 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
249 if ((data
.length
> 0) && (data
.data
!= NULL
)) {
250 ret
= ldb_msg_add_value(msg
, "data", &data
, NULL
);
252 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
257 if (ret
!= LDB_SUCCESS
) {
262 type_str
= talloc_asprintf(mem_ctx
, "%u", type
);
263 if (type_str
== NULL
) {
268 ret
= ldb_msg_add_string(msg
, "type", type_str
);
269 if (ret
!= LDB_SUCCESS
) {
277 static char *reg_ldb_escape(TALLOC_CTX
*mem_ctx
, const char *value
)
281 val
.data
= discard_const_p(uint8_t, value
);
282 val
.length
= strlen(value
);
284 return ldb_dn_escape_value(mem_ctx
, val
);
287 static int reg_close_ldb_key(struct ldb_key_data
*key
)
289 if (key
->subkeys
!= NULL
) {
290 talloc_free(key
->subkeys
);
294 if (key
->values
!= NULL
) {
295 talloc_free(key
->values
);
301 static struct ldb_dn
*reg_path_to_ldb(TALLOC_CTX
*mem_ctx
,
302 const struct hive_key
*from
,
303 const char *path
, const char *add
)
308 struct ldb_key_data
*kd
= talloc_get_type(from
, struct ldb_key_data
);
309 struct ldb_context
*ldb
= kd
->ldb
;
311 mypath
= talloc_strdup(mem_ctx
, path
);
312 if (mypath
== NULL
) {
316 ret
= ldb_dn_new(mem_ctx
, ldb
, add
);
317 if (!ldb_dn_validate(ret
)) {
322 if (!ldb_dn_add_base(ret
, kd
->dn
)) {
327 while (mypath
[0] != '\0') {
328 begin
= strchr(mypath
, '\\');
333 if (!ldb_dn_add_child_fmt(ret
, "key=%s",
334 reg_ldb_escape(mem_ctx
, mypath
))) {
349 static WERROR
cache_subkeys(struct ldb_key_data
*kd
)
351 struct ldb_context
*c
= kd
->ldb
;
352 struct ldb_result
*res
;
355 ret
= ldb_search(c
, c
, &res
, kd
->dn
, LDB_SCOPE_ONELEVEL
,
357 if (ret
!= LDB_SUCCESS
) {
358 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
359 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
363 kd
->subkey_count
= res
->count
;
364 kd
->subkeys
= talloc_steal(kd
, res
->msgs
);
370 static WERROR
cache_values(struct ldb_key_data
*kd
)
372 struct ldb_context
*c
= kd
->ldb
;
373 struct ldb_result
*res
;
376 ret
= ldb_search(c
, c
, &res
, kd
->dn
, LDB_SCOPE_ONELEVEL
,
378 if (ret
!= LDB_SUCCESS
) {
379 DEBUG(0, ("Error getting values for '%s': %s\n",
380 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
384 kd
->value_count
= res
->count
;
385 kd
->values
= talloc_steal(kd
, res
->msgs
);
392 static WERROR
ldb_get_subkey_by_id(TALLOC_CTX
*mem_ctx
,
393 const struct hive_key
*k
, uint32_t idx
,
395 const char **classname
,
396 NTTIME
*last_mod_time
)
398 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
403 if (classname
!= NULL
)
405 if (last_mod_time
!= NULL
)
406 *last_mod_time
= 0; /* TODO: we need to add this to the
407 ldb backend properly */
409 /* Do a search if necessary */
410 if (kd
->subkeys
== NULL
) {
411 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd
));
414 if (idx
>= kd
->subkey_count
)
415 return WERR_NO_MORE_ITEMS
;
418 *name
= talloc_strdup(mem_ctx
,
419 ldb_msg_find_attr_as_string(kd
->subkeys
[idx
], "key", NULL
));
420 if (classname
!= NULL
)
421 *classname
= talloc_strdup(mem_ctx
,
422 ldb_msg_find_attr_as_string(kd
->subkeys
[idx
], "classname", NULL
));
427 static WERROR
ldb_get_default_value(TALLOC_CTX
*mem_ctx
,
428 const struct hive_key
*k
,
429 const char **name
, uint32_t *data_type
,
432 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
433 struct ldb_context
*c
= kd
->ldb
;
434 const char* attrs
[] = { "data", "type", NULL
};
435 struct ldb_result
*res
;
438 ret
= ldb_search(c
, mem_ctx
, &res
, kd
->dn
, LDB_SCOPE_BASE
, attrs
,
441 if (ret
!= LDB_SUCCESS
) {
442 DEBUG(0, ("Error getting default value for '%s': %s\n",
443 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
447 if (res
->count
== 0 || res
->msgs
[0]->num_elements
== 0) {
449 return WERR_FILE_NOT_FOUND
;
452 if ((data_type
!= NULL
) && (data
!= NULL
)) {
453 reg_ldb_unpack_value(mem_ctx
, res
->msgs
[0], name
, data_type
,
462 static WERROR
ldb_get_value_by_id(TALLOC_CTX
*mem_ctx
, struct hive_key
*k
,
463 uint32_t idx
, const char **name
,
464 uint32_t *data_type
, DATA_BLOB
*data
)
466 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
468 /* if the default value exists, give it back */
469 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx
, k
, name
, data_type
,
477 /* Do the search if necessary */
478 if (kd
->values
== NULL
) {
479 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
482 if (idx
>= kd
->value_count
)
483 return WERR_NO_MORE_ITEMS
;
485 reg_ldb_unpack_value(mem_ctx
, kd
->values
[idx
], name
, data_type
, data
);
490 static WERROR
ldb_get_value(TALLOC_CTX
*mem_ctx
, struct hive_key
*k
,
491 const char *name
, uint32_t *data_type
,
494 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
495 const char *res_name
;
498 /* the default value was requested, give it back */
499 if (name
[0] == '\0') {
500 return ldb_get_default_value(mem_ctx
, k
, NULL
, data_type
, data
);
503 /* Do the search if necessary */
504 if (kd
->values
== NULL
) {
505 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
508 for (idx
= 0; idx
< kd
->value_count
; idx
++) {
509 res_name
= ldb_msg_find_attr_as_string(kd
->values
[idx
], "value",
511 if (ldb_attr_cmp(name
, res_name
) == 0) {
512 reg_ldb_unpack_value(mem_ctx
, kd
->values
[idx
], NULL
,
518 return WERR_FILE_NOT_FOUND
;
521 static WERROR
ldb_open_key(TALLOC_CTX
*mem_ctx
, const struct hive_key
*h
,
522 const char *name
, struct hive_key
**key
)
524 struct ldb_result
*res
;
525 struct ldb_dn
*ldb_path
;
527 struct ldb_key_data
*newkd
;
528 struct ldb_key_data
*kd
= talloc_get_type(h
, struct ldb_key_data
);
529 struct ldb_context
*c
= kd
->ldb
;
531 ldb_path
= reg_path_to_ldb(mem_ctx
, h
, name
, NULL
);
532 W_ERROR_HAVE_NO_MEMORY(ldb_path
);
534 ret
= ldb_search(c
, mem_ctx
, &res
, ldb_path
, LDB_SCOPE_BASE
, NULL
,
537 if (ret
!= LDB_SUCCESS
) {
538 DEBUG(3, ("Error opening key '%s': %s\n",
539 ldb_dn_get_linearized(ldb_path
), ldb_errstring(c
)));
541 } else if (res
->count
== 0) {
542 DEBUG(3, ("Key '%s' not found\n",
543 ldb_dn_get_linearized(ldb_path
)));
545 return WERR_FILE_NOT_FOUND
;
548 newkd
= talloc_zero(mem_ctx
, struct ldb_key_data
);
549 W_ERROR_HAVE_NO_MEMORY(newkd
);
550 newkd
->key
.ops
= ®_backend_ldb
;
551 newkd
->ldb
= talloc_reference(newkd
, kd
->ldb
);
552 newkd
->dn
= ldb_dn_copy(newkd
, res
->msgs
[0]->dn
);
553 newkd
->classname
= talloc_steal(newkd
,
554 ldb_msg_find_attr_as_string(res
->msgs
[0], "classname", NULL
));
558 *key
= (struct hive_key
*)newkd
;
563 WERROR
reg_open_ldb_file(TALLOC_CTX
*parent_ctx
, const char *location
,
564 struct auth_session_info
*session_info
,
565 struct cli_credentials
*credentials
,
566 struct tevent_context
*ev_ctx
,
567 struct loadparm_context
*lp_ctx
,
570 struct ldb_key_data
*kd
;
571 struct ldb_context
*wrap
;
572 struct ldb_message
*attrs_msg
;
574 if (location
== NULL
)
575 return WERR_INVALID_PARAMETER
;
577 wrap
= ldb_wrap_connect(parent_ctx
, ev_ctx
, lp_ctx
,
578 location
, session_info
, credentials
, 0);
581 DEBUG(1, (__FILE__
": unable to connect\n"));
585 attrs_msg
= ldb_msg_new(wrap
);
586 W_ERROR_HAVE_NO_MEMORY(attrs_msg
);
587 attrs_msg
->dn
= ldb_dn_new(attrs_msg
, wrap
, "@ATTRIBUTES");
588 W_ERROR_HAVE_NO_MEMORY(attrs_msg
->dn
);
589 ldb_msg_add_string(attrs_msg
, "key", "CASE_INSENSITIVE");
590 ldb_msg_add_string(attrs_msg
, "value", "CASE_INSENSITIVE");
592 ldb_add(wrap
, attrs_msg
);
594 ldb_set_debug_stderr(wrap
);
596 kd
= talloc_zero(parent_ctx
, struct ldb_key_data
);
597 kd
->key
.ops
= ®_backend_ldb
;
598 kd
->ldb
= talloc_reference(kd
, wrap
);
599 talloc_set_destructor (kd
, reg_close_ldb_key
);
600 kd
->dn
= ldb_dn_new(kd
, wrap
, "hive=NONE");
602 *k
= (struct hive_key
*)kd
;
607 static WERROR
ldb_add_key(TALLOC_CTX
*mem_ctx
, const struct hive_key
*parent
,
608 const char *name
, const char *classname
,
609 struct security_descriptor
*sd
,
610 struct hive_key
**newkey
)
612 struct ldb_key_data
*parentkd
= discard_const_p(struct ldb_key_data
, parent
);
613 struct ldb_dn
*ldb_path
;
614 struct ldb_message
*msg
;
615 struct ldb_key_data
*newkd
;
618 ldb_path
= reg_path_to_ldb(mem_ctx
, parent
, name
, NULL
);
619 W_ERROR_HAVE_NO_MEMORY(ldb_path
);
621 msg
= ldb_msg_new(mem_ctx
);
622 W_ERROR_HAVE_NO_MEMORY(msg
);
626 ldb_msg_add_string(msg
, "key", name
);
627 if (classname
!= NULL
) {
628 ldb_msg_add_string(msg
, "classname", classname
);
631 ret
= ldb_add(parentkd
->ldb
, msg
);
635 if (ret
== LDB_ERR_ENTRY_ALREADY_EXISTS
) {
636 return WERR_ALREADY_EXISTS
;
639 if (ret
!= LDB_SUCCESS
) {
640 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd
->ldb
)));
644 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(ldb_path
)));
646 newkd
= talloc_zero(mem_ctx
, struct ldb_key_data
);
647 W_ERROR_HAVE_NO_MEMORY(newkd
);
648 newkd
->ldb
= talloc_reference(newkd
, parentkd
->ldb
);
649 newkd
->key
.ops
= ®_backend_ldb
;
650 newkd
->dn
= talloc_steal(newkd
, ldb_path
);
651 newkd
->classname
= talloc_steal(newkd
, classname
);
653 *newkey
= (struct hive_key
*)newkd
;
656 talloc_free(parentkd
->subkeys
);
657 parentkd
->subkeys
= NULL
;
662 static WERROR
ldb_del_value(TALLOC_CTX
*mem_ctx
, struct hive_key
*key
,
666 struct ldb_key_data
*kd
= talloc_get_type(key
, struct ldb_key_data
);
667 struct ldb_message
*msg
;
668 struct ldb_dn
*childdn
;
670 if (child
[0] == '\0') {
672 msg
= ldb_msg_new(mem_ctx
);
673 W_ERROR_HAVE_NO_MEMORY(msg
);
674 msg
->dn
= ldb_dn_copy(msg
, kd
->dn
);
675 W_ERROR_HAVE_NO_MEMORY(msg
->dn
);
676 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
677 if (ret
!= LDB_SUCCESS
) {
680 ret
= ldb_msg_add_empty(msg
, "type", LDB_FLAG_MOD_DELETE
,
682 if (ret
!= LDB_SUCCESS
) {
686 ret
= ldb_modify(kd
->ldb
, msg
);
690 if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
) {
691 return WERR_FILE_NOT_FOUND
;
692 } else if (ret
!= LDB_SUCCESS
) {
693 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd
->ldb
)));
698 childdn
= ldb_dn_copy(kd
->ldb
, kd
->dn
);
699 if (!ldb_dn_add_child_fmt(childdn
, "value=%s",
700 reg_ldb_escape(childdn
, child
)))
702 talloc_free(childdn
);
706 ret
= ldb_delete(kd
->ldb
, childdn
);
708 talloc_free(childdn
);
710 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
711 return WERR_FILE_NOT_FOUND
;
712 } else if (ret
!= LDB_SUCCESS
) {
713 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd
->ldb
)));
719 talloc_free(kd
->values
);
725 static WERROR
ldb_del_key(TALLOC_CTX
*mem_ctx
, const struct hive_key
*key
,
730 struct ldb_key_data
*parentkd
= talloc_get_type(key
, struct ldb_key_data
);
731 struct ldb_dn
*ldb_path
;
732 struct ldb_context
*c
= parentkd
->ldb
;
733 struct ldb_result
*res_keys
;
734 struct ldb_result
*res_vals
;
738 /* Verify key exists by opening it */
739 werr
= ldb_open_key(mem_ctx
, key
, name
, &hk
);
740 if (!W_ERROR_IS_OK(werr
)) {
744 ldb_path
= reg_path_to_ldb(mem_ctx
, key
, name
, NULL
);
745 W_ERROR_HAVE_NO_MEMORY(ldb_path
);
747 /* Search for subkeys */
748 ret
= ldb_search(c
, mem_ctx
, &res_keys
, ldb_path
, LDB_SCOPE_ONELEVEL
,
751 if (ret
!= LDB_SUCCESS
) {
752 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
753 ldb_dn_get_linearized(ldb_path
), ldb_errstring(c
)));
757 /* Search for values */
758 ret
= ldb_search(c
, mem_ctx
, &res_vals
, ldb_path
, LDB_SCOPE_ONELEVEL
,
761 if (ret
!= LDB_SUCCESS
) {
762 DEBUG(0, ("Error getting values for '%s': %s\n",
763 ldb_dn_get_linearized(ldb_path
), ldb_errstring(c
)));
767 /* Start an explicit transaction */
768 ret
= ldb_transaction_start(c
);
770 if (ret
!= LDB_SUCCESS
) {
771 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c
)));
775 if (res_keys
->count
|| res_vals
->count
)
777 /* Delete any subkeys */
778 for (i
= 0; i
< res_keys
->count
; i
++)
780 werr
= ldb_del_key(mem_ctx
, hk
,
781 ldb_msg_find_attr_as_string(
784 if (!W_ERROR_IS_OK(werr
)) {
785 ret
= ldb_transaction_cancel(c
);
790 /* Delete any values */
791 for (i
= 0; i
< res_vals
->count
; i
++)
793 werr
= ldb_del_value(mem_ctx
, hk
,
794 ldb_msg_find_attr_as_string(
797 if (!W_ERROR_IS_OK(werr
)) {
798 ret
= ldb_transaction_cancel(c
);
803 talloc_free(res_keys
);
804 talloc_free(res_vals
);
806 /* Delete the key itself */
807 ret
= ldb_delete(c
, ldb_path
);
809 if (ret
!= LDB_SUCCESS
)
811 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c
)));
812 ret
= ldb_transaction_cancel(c
);
816 /* Commit the transaction */
817 ret
= ldb_transaction_commit(c
);
819 if (ret
!= LDB_SUCCESS
)
821 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c
)));
822 ret
= ldb_transaction_cancel(c
);
827 talloc_free(parentkd
->subkeys
);
828 parentkd
->subkeys
= NULL
;
833 static WERROR
ldb_set_value(struct hive_key
*parent
,
834 const char *name
, uint32_t type
,
835 const DATA_BLOB data
)
837 struct ldb_message
*msg
;
838 struct ldb_key_data
*kd
= talloc_get_type(parent
, struct ldb_key_data
);
841 TALLOC_CTX
*mem_ctx
= talloc_init("ldb_set_value");
843 msg
= reg_ldb_pack_value(kd
->ldb
, mem_ctx
, name
, type
, data
);
844 W_ERROR_HAVE_NO_MEMORY(msg
);
846 msg
->dn
= ldb_dn_copy(msg
, kd
->dn
);
847 W_ERROR_HAVE_NO_MEMORY(msg
->dn
);
849 if (name
[0] != '\0') {
850 /* For a default value, we add/overwrite the attributes to/of the hive.
851 For a normal value, we create a new child. */
852 if (!ldb_dn_add_child_fmt(msg
->dn
, "value=%s",
853 reg_ldb_escape(mem_ctx
, name
)))
855 talloc_free(mem_ctx
);
860 /* Try first a "modify" and if this doesn't work do try an "add" */
861 for (i
= 0; i
< msg
->num_elements
; i
++) {
862 if (LDB_FLAG_MOD_TYPE(msg
->elements
[i
].flags
) != LDB_FLAG_MOD_DELETE
) {
863 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
866 ret
= ldb_modify(kd
->ldb
, msg
);
867 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
869 while (i
< msg
->num_elements
) {
870 if (LDB_FLAG_MOD_TYPE(msg
->elements
[i
].flags
) == LDB_FLAG_MOD_DELETE
) {
871 ldb_msg_remove_element(msg
, &msg
->elements
[i
]);
876 ret
= ldb_add(kd
->ldb
, msg
);
878 if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
) {
879 /* ignore this -> the value didn't exist and also now doesn't */
885 if (ret
!= LDB_SUCCESS
) {
886 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd
->ldb
)));
887 talloc_free(mem_ctx
);
892 talloc_free(kd
->values
);
895 talloc_free(mem_ctx
);
899 static WERROR
ldb_get_key_info(TALLOC_CTX
*mem_ctx
,
900 const struct hive_key
*key
,
901 const char **classname
,
902 uint32_t *num_subkeys
,
903 uint32_t *num_values
,
904 NTTIME
*last_change_time
,
905 uint32_t *max_subkeynamelen
,
906 uint32_t *max_valnamelen
,
907 uint32_t *max_valbufsize
)
909 struct ldb_key_data
*kd
= talloc_get_type(key
, struct ldb_key_data
);
910 uint32_t default_value_type
= REG_NONE
;
911 DATA_BLOB default_value
= { NULL
, 0 };
915 if (classname
!= NULL
)
917 if (num_subkeys
!= NULL
)
919 if (num_values
!= NULL
)
921 if (last_change_time
!= NULL
)
922 *last_change_time
= 0;
923 if (max_subkeynamelen
!= NULL
)
924 *max_subkeynamelen
= 0;
925 if (max_valnamelen
!= NULL
)
927 if (max_valbufsize
!= NULL
)
930 /* We need this to get the default value (if it exists) for counting
931 * the values under the key and for finding out the longest value buffer
932 * size. If no default value exists the DATA_BLOB "default_value" will
933 * remain { NULL, 0 }. */
934 werr
= ldb_get_default_value(mem_ctx
, key
, NULL
, &default_value_type
,
936 if ((!W_ERROR_IS_OK(werr
)) && (!W_ERROR_EQUAL(werr
, WERR_FILE_NOT_FOUND
))) {
940 if (kd
->subkeys
== NULL
) {
941 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd
));
943 if (kd
->values
== NULL
) {
944 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
947 if (classname
!= NULL
) {
948 *classname
= kd
->classname
;
951 if (num_subkeys
!= NULL
) {
952 *num_subkeys
= kd
->subkey_count
;
954 if (num_values
!= NULL
) {
955 *num_values
= kd
->value_count
;
956 /* also consider the default value if it exists */
957 if (default_value
.data
!= NULL
) {
963 if (max_subkeynamelen
!= NULL
) {
965 struct ldb_message_element
*el
;
967 for (i
= 0; i
< kd
->subkey_count
; i
++) {
968 el
= ldb_msg_find_element(kd
->subkeys
[i
], "key");
969 *max_subkeynamelen
= MAX(*max_subkeynamelen
, el
->values
[0].length
);
973 if (max_valnamelen
!= NULL
|| max_valbufsize
!= NULL
) {
975 struct ldb_message_element
*el
;
976 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
978 /* also consider the default value if it exists */
979 if ((max_valbufsize
!= NULL
) && (default_value
.data
!= NULL
)) {
980 *max_valbufsize
= MAX(*max_valbufsize
,
981 default_value
.length
);
984 for (i
= 0; i
< kd
->value_count
; i
++) {
985 if (max_valnamelen
!= NULL
) {
986 el
= ldb_msg_find_element(kd
->values
[i
], "value");
987 *max_valnamelen
= MAX(*max_valnamelen
, el
->values
[0].length
);
990 if (max_valbufsize
!= NULL
) {
993 reg_ldb_unpack_value(mem_ctx
,
996 *max_valbufsize
= MAX(*max_valbufsize
, data
.length
);
997 talloc_free(data
.data
);
1002 talloc_free(default_value
.data
);
1007 static struct hive_operations reg_backend_ldb
= {
1009 .add_key
= ldb_add_key
,
1010 .del_key
= ldb_del_key
,
1011 .get_key_by_name
= ldb_open_key
,
1012 .enum_value
= ldb_get_value_by_id
,
1013 .enum_key
= ldb_get_subkey_by_id
,
1014 .set_value
= ldb_set_value
,
1015 .get_value_by_name
= ldb_get_value
,
1016 .delete_value
= ldb_del_value
,
1017 .get_key_info
= ldb_get_key_info
,