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 if (val
->data
[0] != '\0') {
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);
70 /* Provide a possibility to store also UTF8
71 * REG_SZ/REG_EXPAND_SZ values. This is done
72 * by adding a '\0' in front of the data */
73 data
->data
= talloc_size(mem_ctx
, val
->length
- 1);
74 if (data
->data
!= NULL
) {
75 memcpy(data
->data
, val
->data
+ 1,
78 data
->length
= val
->length
- 1;
87 case REG_DWORD_BIG_ENDIAN
:
89 if (val
->data
[0] != '\0') {
90 /* The data is a plain DWORD */
91 uint32_t tmp
= strtoul((char *)val
->data
, NULL
, 0);
92 data
->data
= talloc_size(mem_ctx
, sizeof(uint32_t));
93 if (data
->data
!= NULL
) {
94 SIVAL(data
->data
, 0, tmp
);
96 data
->length
= sizeof(uint32_t);
98 /* Provide a possibility to store also UTF8
99 * REG_DWORD values. This is done by adding a
100 * '\0' in front of the data */
101 data
->data
= talloc_size(mem_ctx
, val
->length
- 1);
102 if (data
->data
!= NULL
) {
103 memcpy(data
->data
, val
->data
+ 1,
106 data
->length
= val
->length
- 1;
116 if (val
->data
[0] != '\0') {
117 /* The data is a plain QWORD */
118 uint64_t tmp
= strtoull((char *)val
->data
, NULL
, 0);
119 data
->data
= talloc_size(mem_ctx
, sizeof(uint64_t));
120 if (data
->data
!= NULL
) {
121 SBVAL(data
->data
, 0, tmp
);
123 data
->length
= sizeof(uint64_t);
125 /* Provide a possibility to store also UTF8
126 * REG_QWORD values. This is done by adding a
127 * '\0' in front of the data */
128 data
->data
= talloc_size(mem_ctx
, val
->length
- 1);
129 if (data
->data
!= NULL
) {
130 memcpy(data
->data
, val
->data
+ 1,
133 data
->length
= val
->length
- 1;
144 data
->data
= talloc_memdup(mem_ctx
, val
->data
,
146 data
->length
= val
->length
;
155 static struct ldb_message
*reg_ldb_pack_value(struct ldb_context
*ctx
,
158 uint32_t type
, DATA_BLOB data
)
160 struct ldb_message
*msg
;
161 char *name_dup
, *type_str
;
164 msg
= talloc_zero(mem_ctx
, struct ldb_message
);
169 name_dup
= talloc_strdup(msg
, name
);
170 if (name_dup
== NULL
) {
175 ret
= ldb_msg_add_string(msg
, "value", name_dup
);
176 if (ret
!= LDB_SUCCESS
) {
184 if ((data
.length
> 0) && (data
.data
!= NULL
)) {
188 val
= talloc_zero(msg
, struct ldb_val
);
194 /* Only when the "data.length" is dividable by two try
195 * the charset conversion, otherwise stick with the
196 * default of the "ret2" variable set to "false" (which
197 * means binary storage and no conversion) */
198 if (data
.length
% 2 == 0) {
199 /* The data is provided as UTF16 string */
200 ret2
= convert_string_talloc(mem_ctx
, CH_UTF16
, CH_UTF8
,
201 (void *)data
.data
, data
.length
,
202 (void **)&val
->data
, &val
->length
,
206 /* Provide a possibility to store also binary
207 * UTF8 REG_SZ/REG_EXPAND_SZ values as fallback
208 * mechanism. This is done by adding a '\0' in
209 * front of the data */
210 val
->data
= talloc_size(msg
, data
.length
+ 1);
211 if (val
->data
== NULL
) {
216 memcpy(val
->data
+ 1, data
.data
, data
.length
);
217 val
->length
= data
.length
+ 1;
219 ret
= ldb_msg_add_value(msg
, "data", val
, NULL
);
221 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
226 case REG_DWORD_BIG_ENDIAN
:
227 if ((data
.length
> 0) && (data
.data
!= NULL
)) {
228 if (data
.length
== sizeof(uint32_t)) {
231 conv_str
= talloc_asprintf(msg
, "0x%8.8x",
233 if (conv_str
== NULL
) {
237 ret
= ldb_msg_add_string(msg
, "data", conv_str
);
239 /* Provide a possibility to store also UTF8
240 * REG_DWORD values. This is done by adding a
241 * '\0' in front of the data */
244 val
= talloc_zero(msg
, struct ldb_val
);
250 val
->data
= talloc_size(msg
, data
.length
+ 1);
251 if (val
->data
== NULL
) {
256 memcpy(val
->data
+ 1, data
.data
, data
.length
);
257 val
->length
= data
.length
+ 1;
258 ret
= ldb_msg_add_value(msg
, "data", val
, NULL
);
261 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
266 if ((data
.length
> 0) && (data
.data
!= NULL
)) {
267 if (data
.length
== sizeof(uint64_t)) {
270 conv_str
= talloc_asprintf(msg
, "0x%16.16llx", BVAL(data
.data
, 0));
271 if (conv_str
== NULL
) {
275 ret
= ldb_msg_add_string(msg
, "data", conv_str
);
277 /* Provide a possibility to store also UTF8
278 * REG_QWORD values. This is done by adding a
279 * '\0' in front of the data */
282 val
= talloc_zero(msg
, struct ldb_val
);
288 val
->data
= talloc_size(msg
, data
.length
+ 1);
289 if (val
->data
== NULL
) {
294 memcpy(val
->data
+ 1, data
.data
, data
.length
);
295 val
->length
= data
.length
+ 1;
296 ret
= ldb_msg_add_value(msg
, "data", val
, NULL
);
299 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
305 if ((data
.length
> 0) && (data
.data
!= NULL
)) {
306 ret
= ldb_msg_add_value(msg
, "data", &data
, NULL
);
308 ret
= ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
313 if (ret
!= LDB_SUCCESS
) {
318 type_str
= talloc_asprintf(mem_ctx
, "%u", type
);
319 if (type_str
== NULL
) {
324 ret
= ldb_msg_add_string(msg
, "type", type_str
);
325 if (ret
!= LDB_SUCCESS
) {
333 static char *reg_ldb_escape(TALLOC_CTX
*mem_ctx
, const char *value
)
337 val
.data
= discard_const_p(uint8_t, value
);
338 val
.length
= strlen(value
);
340 return ldb_dn_escape_value(mem_ctx
, val
);
343 static int reg_close_ldb_key(struct ldb_key_data
*key
)
345 if (key
->subkeys
!= NULL
) {
346 talloc_free(key
->subkeys
);
350 if (key
->values
!= NULL
) {
351 talloc_free(key
->values
);
357 static struct ldb_dn
*reg_path_to_ldb(TALLOC_CTX
*mem_ctx
,
358 const struct hive_key
*from
,
359 const char *path
, const char *add
)
361 TALLOC_CTX
*local_ctx
;
363 char *mypath
= talloc_strdup(mem_ctx
, path
);
365 struct ldb_key_data
*kd
= talloc_get_type(from
, struct ldb_key_data
);
366 struct ldb_context
*ldb
= kd
->ldb
;
368 local_ctx
= talloc_new(mem_ctx
);
370 ret
= ldb_dn_new(mem_ctx
, ldb
, add
);
371 if (!ldb_dn_validate(ret
)) {
373 talloc_free(local_ctx
);
380 begin
= strrchr(mypath
, '\\');
382 if (begin
) keyname
= begin
+ 1;
383 else keyname
= mypath
;
385 if (keyname
[0] != '\0') {
386 if (!ldb_dn_add_base_fmt(ret
, "key=%s",
387 reg_ldb_escape(local_ctx
,
390 talloc_free(local_ctx
);
402 ldb_dn_add_base(ret
, kd
->dn
);
404 talloc_free(local_ctx
);
409 static WERROR
cache_subkeys(struct ldb_key_data
*kd
)
411 struct ldb_context
*c
= kd
->ldb
;
412 struct ldb_result
*res
;
415 ret
= ldb_search(c
, c
, &res
, kd
->dn
, LDB_SCOPE_ONELEVEL
, NULL
, "(key=*)");
417 if (ret
!= LDB_SUCCESS
) {
418 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
419 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
423 kd
->subkey_count
= res
->count
;
424 kd
->subkeys
= talloc_steal(kd
, res
->msgs
);
430 static WERROR
cache_values(struct ldb_key_data
*kd
)
432 struct ldb_context
*c
= kd
->ldb
;
433 struct ldb_result
*res
;
436 ret
= ldb_search(c
, c
, &res
, kd
->dn
, LDB_SCOPE_ONELEVEL
,
439 if (ret
!= LDB_SUCCESS
) {
440 DEBUG(0, ("Error getting values for '%s': %s\n",
441 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
445 kd
->value_count
= res
->count
;
446 kd
->values
= talloc_steal(kd
, res
->msgs
);
453 static WERROR
ldb_get_subkey_by_id(TALLOC_CTX
*mem_ctx
,
454 const struct hive_key
*k
, uint32_t idx
,
456 const char **classname
,
457 NTTIME
*last_mod_time
)
459 struct ldb_message_element
*el
;
460 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
465 if (classname
!= NULL
)
466 *classname
= NULL
; /* TODO: Store properly */
467 if (last_mod_time
!= NULL
)
468 *last_mod_time
= 0; /* TODO: we need to add this to the
469 ldb backend properly */
471 /* Do a search if necessary */
472 if (kd
->subkeys
== NULL
) {
473 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd
));
476 if (idx
>= kd
->subkey_count
)
477 return WERR_NO_MORE_ITEMS
;
479 el
= ldb_msg_find_element(kd
->subkeys
[idx
], "key");
480 SMB_ASSERT(el
!= NULL
);
481 SMB_ASSERT(el
->num_values
!= 0);
484 *name
= talloc_strdup(mem_ctx
, (char *)el
->values
[0].data
);
489 static WERROR
ldb_get_default_value(TALLOC_CTX
*mem_ctx
, struct hive_key
*k
,
490 const char **name
, uint32_t *data_type
,
493 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
494 struct ldb_context
*c
= kd
->ldb
;
495 const char* attrs
[] = { "data", "type", NULL
};
496 struct ldb_result
*res
;
499 ret
= ldb_search(c
, mem_ctx
, &res
, kd
->dn
, LDB_SCOPE_BASE
, attrs
, "(key=*)");
501 if (ret
!= LDB_SUCCESS
) {
502 DEBUG(0, ("Error getting default value for '%s': %s\n",
503 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
507 if (res
->count
== 0 || res
->msgs
[0]->num_elements
== 0)
510 reg_ldb_unpack_value(mem_ctx
,
511 res
->msgs
[0], name
, data_type
, data
);
518 static WERROR
ldb_get_value_by_id(TALLOC_CTX
*mem_ctx
, struct hive_key
*k
,
519 uint32_t idx
, const char **name
,
520 uint32_t *data_type
, DATA_BLOB
*data
)
522 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
524 /* if default value exists, give it back */
525 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx
, k
, name
, data_type
,
533 /* Do the search if necessary */
534 if (kd
->values
== NULL
) {
535 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
538 if (idx
>= kd
->value_count
)
539 return WERR_NO_MORE_ITEMS
;
541 reg_ldb_unpack_value(mem_ctx
, kd
->values
[idx
], name
, data_type
, data
);
546 static WERROR
ldb_get_value(TALLOC_CTX
*mem_ctx
, struct hive_key
*k
,
547 const char *name
, uint32_t *data_type
,
550 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
551 struct ldb_context
*c
= kd
->ldb
;
552 struct ldb_result
*res
;
556 return WERR_INVALID_PARAM
;
559 if (name
[0] == '\0') {
561 return ldb_get_default_value(mem_ctx
, k
, NULL
, data_type
, data
);
564 ret
= ldb_search(c
, mem_ctx
, &res
, kd
->dn
, LDB_SCOPE_ONELEVEL
,
565 NULL
, "(value=%s)", name
);
567 if (ret
!= LDB_SUCCESS
) {
568 DEBUG(0, ("Error getting values for '%s': %s\n",
569 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
576 reg_ldb_unpack_value(mem_ctx
, res
->msgs
[0], NULL
, data_type
, data
);
584 static WERROR
ldb_open_key(TALLOC_CTX
*mem_ctx
, const struct hive_key
*h
,
585 const char *name
, struct hive_key
**key
)
587 struct ldb_result
*res
;
588 struct ldb_dn
*ldap_path
;
590 struct ldb_key_data
*newkd
;
591 struct ldb_key_data
*kd
= talloc_get_type(h
, struct ldb_key_data
);
592 struct ldb_context
*c
= kd
->ldb
;
594 ldap_path
= reg_path_to_ldb(mem_ctx
, h
, name
, NULL
);
595 W_ERROR_HAVE_NO_MEMORY(ldap_path
);
597 ret
= ldb_search(c
, mem_ctx
, &res
, ldap_path
, LDB_SCOPE_BASE
, NULL
, "(key=*)");
599 if (ret
!= LDB_SUCCESS
) {
600 DEBUG(3, ("Error opening key '%s': %s\n",
601 ldb_dn_get_linearized(ldap_path
), ldb_errstring(c
)));
603 } else if (res
->count
== 0) {
604 DEBUG(3, ("Key '%s' not found\n",
605 ldb_dn_get_linearized(ldap_path
)));
610 newkd
= talloc_zero(mem_ctx
, struct ldb_key_data
);
611 newkd
->key
.ops
= ®_backend_ldb
;
612 newkd
->ldb
= talloc_reference(newkd
, kd
->ldb
);
613 newkd
->dn
= ldb_dn_copy(mem_ctx
, res
->msgs
[0]->dn
);
615 *key
= (struct hive_key
*)newkd
;
620 WERROR
reg_open_ldb_file(TALLOC_CTX
*parent_ctx
, const char *location
,
621 struct auth_session_info
*session_info
,
622 struct cli_credentials
*credentials
,
623 struct tevent_context
*ev_ctx
,
624 struct loadparm_context
*lp_ctx
,
627 struct ldb_key_data
*kd
;
628 struct ldb_context
*wrap
;
629 struct ldb_message
*attrs_msg
;
631 if (location
== NULL
)
632 return WERR_INVALID_PARAM
;
634 wrap
= ldb_wrap_connect(parent_ctx
, ev_ctx
, lp_ctx
,
635 location
, session_info
, credentials
, 0);
638 DEBUG(1, (__FILE__
": unable to connect\n"));
642 attrs_msg
= ldb_msg_new(wrap
);
643 W_ERROR_HAVE_NO_MEMORY(attrs_msg
);
644 attrs_msg
->dn
= ldb_dn_new(attrs_msg
, wrap
, "@ATTRIBUTES");
645 W_ERROR_HAVE_NO_MEMORY(attrs_msg
->dn
);
646 ldb_msg_add_string(attrs_msg
, "key", "CASE_INSENSITIVE");
647 ldb_msg_add_string(attrs_msg
, "value", "CASE_INSENSITIVE");
649 ldb_add(wrap
, attrs_msg
);
651 ldb_set_debug_stderr(wrap
);
653 kd
= talloc_zero(parent_ctx
, struct ldb_key_data
);
654 kd
->key
.ops
= ®_backend_ldb
;
655 kd
->ldb
= talloc_reference(kd
, wrap
);
656 talloc_set_destructor (kd
, reg_close_ldb_key
);
657 kd
->dn
= ldb_dn_new(kd
, wrap
, "hive=NONE");
659 *k
= (struct hive_key
*)kd
;
664 static WERROR
ldb_add_key(TALLOC_CTX
*mem_ctx
, const struct hive_key
*parent
,
665 const char *name
, const char *classname
,
666 struct security_descriptor
*sd
,
667 struct hive_key
**newkey
)
669 struct ldb_key_data
*parentkd
= discard_const_p(struct ldb_key_data
, parent
);
670 struct ldb_message
*msg
;
671 struct ldb_key_data
*newkd
;
674 msg
= ldb_msg_new(mem_ctx
);
675 W_ERROR_HAVE_NO_MEMORY(msg
);
677 msg
->dn
= reg_path_to_ldb(msg
, parent
, name
, NULL
);
678 W_ERROR_HAVE_NO_MEMORY(msg
->dn
);
680 ldb_msg_add_string(msg
, "key", talloc_strdup(mem_ctx
, name
));
681 if (classname
!= NULL
)
682 ldb_msg_add_string(msg
, "classname",
683 talloc_strdup(mem_ctx
, classname
));
685 ret
= ldb_add(parentkd
->ldb
, msg
);
686 if (ret
== LDB_ERR_ENTRY_ALREADY_EXISTS
) {
687 return WERR_ALREADY_EXISTS
;
690 if (ret
!= LDB_SUCCESS
) {
691 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd
->ldb
)));
695 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg
->dn
)));
697 newkd
= talloc_zero(mem_ctx
, struct ldb_key_data
);
698 W_ERROR_HAVE_NO_MEMORY(newkd
);
699 newkd
->ldb
= talloc_reference(newkd
, parentkd
->ldb
);
700 newkd
->key
.ops
= ®_backend_ldb
;
701 newkd
->dn
= talloc_steal(newkd
, msg
->dn
);
703 *newkey
= (struct hive_key
*)newkd
;
706 talloc_free(parentkd
->subkeys
);
707 parentkd
->subkeys
= NULL
;
712 static WERROR
ldb_del_value (struct hive_key
*key
, const char *child
)
715 struct ldb_key_data
*kd
= talloc_get_type(key
, struct ldb_key_data
);
717 struct ldb_message
*msg
;
718 struct ldb_dn
*childdn
;
720 if ((child
== NULL
) || (child
[0] == '\0')) {
722 mem_ctx
= talloc_init("ldb_del_value");
724 msg
= talloc_zero(mem_ctx
, struct ldb_message
);
725 W_ERROR_HAVE_NO_MEMORY(msg
);
726 msg
->dn
= ldb_dn_copy(msg
, kd
->dn
);
727 W_ERROR_HAVE_NO_MEMORY(msg
->dn
);
728 ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
729 ldb_msg_add_empty(msg
, "type", LDB_FLAG_MOD_DELETE
, NULL
);
731 ret
= ldb_modify(kd
->ldb
, msg
);
732 if (ret
!= LDB_SUCCESS
) {
733 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd
->ldb
)));
734 talloc_free(mem_ctx
);
738 talloc_free(mem_ctx
);
741 childdn
= ldb_dn_copy(kd
->ldb
, kd
->dn
);
742 if (!ldb_dn_add_child_fmt(childdn
, "value=%s",
743 reg_ldb_escape(childdn
, child
)))
745 talloc_free(childdn
);
749 ret
= ldb_delete(kd
->ldb
, childdn
);
751 talloc_free(childdn
);
753 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
755 } else if (ret
!= LDB_SUCCESS
) {
756 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd
->ldb
)));
762 talloc_free(kd
->values
);
768 static WERROR
ldb_del_key(const struct hive_key
*key
, const char *name
)
772 struct ldb_key_data
*parentkd
= talloc_get_type(key
, struct ldb_key_data
);
773 struct ldb_dn
*ldap_path
;
774 TALLOC_CTX
*mem_ctx
= talloc_init("ldb_del_key");
775 struct ldb_context
*c
= parentkd
->ldb
;
776 struct ldb_result
*res_keys
;
777 struct ldb_result
*res_vals
;
781 /* Verify key exists by opening it */
782 werr
= ldb_open_key(mem_ctx
, key
, name
, &hk
);
783 if (!W_ERROR_IS_OK(werr
)) {
784 talloc_free(mem_ctx
);
788 ldap_path
= reg_path_to_ldb(mem_ctx
, key
, name
, NULL
);
789 W_ERROR_HAVE_NO_MEMORY(ldap_path
);
791 /* Search for subkeys */
792 ret
= ldb_search(c
, mem_ctx
, &res_keys
, ldap_path
, LDB_SCOPE_ONELEVEL
,
795 if (ret
!= LDB_SUCCESS
) {
796 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
797 ldb_dn_get_linearized(ldap_path
), ldb_errstring(c
)));
798 talloc_free(mem_ctx
);
802 /* Search for values */
803 ret
= ldb_search(c
, mem_ctx
, &res_vals
, ldap_path
, LDB_SCOPE_ONELEVEL
,
806 if (ret
!= LDB_SUCCESS
) {
807 DEBUG(0, ("Error getting values for '%s': %s\n",
808 ldb_dn_get_linearized(ldap_path
), ldb_errstring(c
)));
809 talloc_free(mem_ctx
);
813 /* Start an explicit transaction */
814 ret
= ldb_transaction_start(c
);
816 if (ret
!= LDB_SUCCESS
) {
817 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c
)));
818 talloc_free(mem_ctx
);
822 if (res_keys
->count
|| res_vals
->count
)
824 /* Delete any subkeys */
825 for (i
= 0; i
< res_keys
->count
; i
++)
827 werr
= ldb_del_key(hk
, ldb_msg_find_attr_as_string(
830 if (!W_ERROR_IS_OK(werr
)) {
831 ret
= ldb_transaction_cancel(c
);
832 talloc_free(mem_ctx
);
837 /* Delete any values */
838 for (i
= 0; i
< res_vals
->count
; i
++)
840 werr
= ldb_del_value(hk
, ldb_msg_find_attr_as_string(
843 if (!W_ERROR_IS_OK(werr
)) {
844 ret
= ldb_transaction_cancel(c
);
845 talloc_free(mem_ctx
);
851 /* Delete the key itself */
852 ret
= ldb_delete(c
, ldap_path
);
854 if (ret
!= LDB_SUCCESS
)
856 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c
)));
857 ret
= ldb_transaction_cancel(c
);
858 talloc_free(mem_ctx
);
862 /* Commit the transaction */
863 ret
= ldb_transaction_commit(c
);
865 if (ret
!= LDB_SUCCESS
)
867 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c
)));
868 ret
= ldb_transaction_cancel(c
);
869 talloc_free(mem_ctx
);
873 talloc_free(mem_ctx
);
876 talloc_free(parentkd
->subkeys
);
877 parentkd
->subkeys
= NULL
;
882 static WERROR
ldb_set_value(struct hive_key
*parent
,
883 const char *name
, uint32_t type
,
884 const DATA_BLOB data
)
886 struct ldb_message
*msg
;
887 struct ldb_key_data
*kd
= talloc_get_type(parent
, struct ldb_key_data
);
890 TALLOC_CTX
*mem_ctx
= talloc_init("ldb_set_value");
892 msg
= reg_ldb_pack_value(kd
->ldb
, mem_ctx
, name
, type
, data
);
893 W_ERROR_HAVE_NO_MEMORY(msg
);
895 msg
->dn
= ldb_dn_copy(msg
, kd
->dn
);
896 W_ERROR_HAVE_NO_MEMORY(msg
->dn
);
898 if ((name
!= NULL
) && (name
[0] != '\0')) {
899 /* For a default value, we add/overwrite the attributes to/of the hive.
900 For a normal value, we create a new child. */
901 if (!ldb_dn_add_child_fmt(msg
->dn
, "value=%s",
902 reg_ldb_escape(mem_ctx
, name
)))
904 talloc_free(mem_ctx
);
909 /* Try first a "modify" and if this doesn't work do try an "add" */
910 for (i
= 0; i
< msg
->num_elements
; i
++) {
911 if (msg
->elements
[i
].flags
!= LDB_FLAG_MOD_DELETE
) {
912 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
915 ret
= ldb_modify(kd
->ldb
, msg
);
916 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
918 while (i
< msg
->num_elements
) {
919 if (msg
->elements
[i
].flags
== LDB_FLAG_MOD_DELETE
) {
920 ldb_msg_remove_element(msg
, &msg
->elements
[i
]);
925 ret
= ldb_add(kd
->ldb
, msg
);
927 if (ret
== LDB_ERR_NO_SUCH_ATTRIBUTE
) {
928 /* ignore this -> the value didn't exist and also now doesn't */
932 if (ret
!= LDB_SUCCESS
) {
933 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd
->ldb
)));
934 talloc_free(mem_ctx
);
939 talloc_free(kd
->values
);
942 talloc_free(mem_ctx
);
946 static WERROR
ldb_get_key_info(TALLOC_CTX
*mem_ctx
,
947 const struct hive_key
*key
,
948 const char **classname
,
949 uint32_t *num_subkeys
,
950 uint32_t *num_values
,
951 NTTIME
*last_change_time
,
952 uint32_t *max_subkeynamelen
,
953 uint32_t *max_valnamelen
,
954 uint32_t *max_valbufsize
)
956 struct ldb_key_data
*kd
= talloc_get_type(key
, struct ldb_key_data
);
959 if (classname
!= NULL
)
961 if (num_subkeys
!= NULL
)
963 if (num_values
!= NULL
)
965 if (last_change_time
!= NULL
)
966 *last_change_time
= 0;
967 if (max_subkeynamelen
!= NULL
)
968 *max_subkeynamelen
= 0;
969 if (max_valnamelen
!= NULL
)
971 if (max_valbufsize
!= NULL
)
974 if (kd
->subkeys
== NULL
) {
975 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd
));
978 if (kd
->values
== NULL
) {
979 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
982 if (num_subkeys
!= NULL
) {
983 *num_subkeys
= kd
->subkey_count
;
985 if (num_values
!= NULL
) {
986 *num_values
= kd
->value_count
;
990 if (max_subkeynamelen
!= NULL
) {
992 struct ldb_message_element
*el
;
994 for (i
= 0; i
< kd
->subkey_count
; i
++) {
995 el
= ldb_msg_find_element(kd
->subkeys
[i
], "key");
996 *max_subkeynamelen
= MAX(*max_subkeynamelen
, el
->values
[0].length
);
999 /* for UTF16 encoding */
1000 *max_subkeynamelen
*= 2;
1003 if (max_valnamelen
!= NULL
|| max_valbufsize
!= NULL
) {
1005 struct ldb_message_element
*el
;
1006 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
1008 for (i
= 0; i
< kd
->value_count
; i
++) {
1009 if (max_valnamelen
!= NULL
) {
1010 el
= ldb_msg_find_element(kd
->values
[i
], "value");
1011 *max_valnamelen
= MAX(*max_valnamelen
, el
->values
[0].length
);
1014 if (max_valbufsize
!= NULL
) {
1017 reg_ldb_unpack_value(mem_ctx
,
1018 kd
->values
[i
], NULL
,
1020 *max_valbufsize
= MAX(*max_valbufsize
, data
.length
);
1021 talloc_free(data
.data
);
1025 if (max_valnamelen
!= NULL
) {
1026 /* for UTF16 encoding */
1027 *max_valnamelen
*= 2;
1034 static struct hive_operations reg_backend_ldb
= {
1036 .add_key
= ldb_add_key
,
1037 .del_key
= ldb_del_key
,
1038 .get_key_by_name
= ldb_open_key
,
1039 .enum_value
= ldb_get_value_by_id
,
1040 .enum_key
= ldb_get_subkey_by_id
,
1041 .set_value
= ldb_set_value
,
1042 .get_value_by_name
= ldb_get_value
,
1043 .delete_value
= ldb_del_value
,
1044 .get_key_info
= ldb_get_key_info
,