2 Unix SMB/CIFS implementation.
4 Copyright (C) Jelmer Vernooij 2004-2007.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "lib/ldb/include/ldb.h"
23 #include "lib/ldb/include/ldb_errors.h"
25 #include "librpc/gen_ndr/winreg.h"
26 #include "param/param.h"
28 static struct hive_operations reg_backend_ldb
;
33 struct ldb_context
*ldb
;
35 struct ldb_message
**subkeys
, **values
;
36 int subkey_count
, value_count
;
39 static void reg_ldb_unpack_value(TALLOC_CTX
*mem_ctx
,
40 struct smb_iconv_convenience
*iconv_convenience
,
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",
53 value_type
= ldb_msg_find_attr_as_uint(msg
, "type", 0);
56 val
= ldb_msg_find_ldb_val(msg
, "data");
62 data
->length
= convert_string_talloc(mem_ctx
, iconv_convenience
, CH_UNIX
, CH_UTF16
,
63 val
->data
, val
->length
,
64 (void **)&data
->data
);
69 *data
= strhex_to_data_blob((char *)val
->data
);
77 uint32_t tmp
= strtoul((char *)val
->data
, NULL
, 0);
78 *data
= data_blob_talloc(mem_ctx
, &tmp
, 4);
83 *data
= data_blob_talloc(mem_ctx
, val
->data
, val
->length
);
88 static struct ldb_message
*reg_ldb_pack_value(struct ldb_context
*ctx
,
91 uint32_t type
, DATA_BLOB data
)
94 struct ldb_message
*msg
= talloc_zero(mem_ctx
, struct ldb_message
);
97 ldb_msg_add_string(msg
, "value", talloc_strdup(mem_ctx
, name
));
102 val
.length
= convert_string_talloc(mem_ctx
, lp_iconv_convenience(global_loadparm
), CH_UTF16
, CH_UNIX
,
106 ldb_msg_add_value(msg
, "data", &val
, NULL
);
110 ldb_msg_add_string(msg
, "data",
111 data_blob_hex_string(mem_ctx
, &data
));
115 ldb_msg_add_string(msg
, "data",
116 talloc_asprintf(mem_ctx
, "0x%x",
117 IVAL(data
.data
, 0)));
120 ldb_msg_add_value(msg
, "data", &data
, NULL
);
124 type_s
= talloc_asprintf(mem_ctx
, "%u", type
);
125 ldb_msg_add_string(msg
, "type", type_s
);
130 static char *reg_ldb_escape(TALLOC_CTX
*mem_ctx
, const char *value
)
134 val
.data
= discard_const_p(uint8_t, value
);
135 val
.length
= strlen(value
);
137 return ldb_dn_escape_value(mem_ctx
, val
);
140 static int reg_close_ldb_key(struct ldb_key_data
*key
)
142 if (key
->subkeys
!= NULL
) {
143 talloc_free(key
->subkeys
);
147 if (key
->values
!= NULL
) {
148 talloc_free(key
->values
);
154 static struct ldb_dn
*reg_path_to_ldb(TALLOC_CTX
*mem_ctx
,
155 const struct hive_key
*from
,
156 const char *path
, const char *add
)
158 TALLOC_CTX
*local_ctx
;
160 char *mypath
= talloc_strdup(mem_ctx
, path
);
162 struct ldb_key_data
*kd
= talloc_get_type(from
, struct ldb_key_data
);
163 struct ldb_context
*ldb
= kd
->ldb
;
165 local_ctx
= talloc_new(mem_ctx
);
168 ret
= ldb_dn_new(mem_ctx
, ldb
, add
);
170 ret
= ldb_dn_new(mem_ctx
, ldb
, NULL
);
172 if (!ldb_dn_validate(ret
)) {
174 talloc_free(local_ctx
);
181 begin
= strrchr(mypath
, '\\');
183 if (begin
) keyname
= begin
+ 1;
184 else keyname
= mypath
;
186 if(strlen(keyname
)) {
187 if (!ldb_dn_add_base_fmt(ret
, "key=%s",
188 reg_ldb_escape(local_ctx
,
191 talloc_free(local_ctx
);
203 ldb_dn_add_base(ret
, kd
->dn
);
205 talloc_free(local_ctx
);
210 static WERROR
cache_subkeys(struct ldb_key_data
*kd
)
212 struct ldb_context
*c
= kd
->ldb
;
213 struct ldb_result
*res
;
216 ret
= ldb_search(c
, kd
->dn
, LDB_SCOPE_ONELEVEL
, "(key=*)", NULL
, &res
);
218 if (ret
!= LDB_SUCCESS
) {
219 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
220 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
224 kd
->subkey_count
= res
->count
;
225 kd
->subkeys
= talloc_steal(kd
, res
->msgs
);
231 static WERROR
cache_values(struct ldb_key_data
*kd
)
233 struct ldb_context
*c
= kd
->ldb
;
234 struct ldb_result
*res
;
237 ret
= ldb_search(c
, kd
->dn
, LDB_SCOPE_ONELEVEL
,
238 "(value=*)", NULL
, &res
);
240 if (ret
!= LDB_SUCCESS
) {
241 DEBUG(0, ("Error getting values for '%s': %s\n",
242 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
245 kd
->value_count
= res
->count
;
246 kd
->values
= talloc_steal(kd
, res
->msgs
);
252 static WERROR
ldb_get_subkey_by_id(TALLOC_CTX
*mem_ctx
,
253 const struct hive_key
*k
, uint32_t idx
,
255 const char **classname
,
256 NTTIME
*last_mod_time
)
258 struct ldb_message_element
*el
;
259 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
261 /* Do a search if necessary */
262 if (kd
->subkeys
== NULL
) {
263 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd
));
266 if (idx
>= kd
->subkey_count
)
267 return WERR_NO_MORE_ITEMS
;
269 el
= ldb_msg_find_element(kd
->subkeys
[idx
], "key");
270 SMB_ASSERT(el
!= NULL
);
271 SMB_ASSERT(el
->num_values
!= 0);
274 *name
= talloc_strdup(mem_ctx
, (char *)el
->values
[0].data
);
276 if (classname
!= NULL
)
277 *classname
= NULL
; /* TODO: Store properly */
279 if (last_mod_time
!= NULL
)
280 *last_mod_time
= 0; /* TODO: we need to add this to the
281 ldb backend properly */
286 static WERROR
ldb_get_default_value(TALLOC_CTX
*mem_ctx
, struct hive_key
*k
,
287 const char** name
, uint32_t *data_type
,
290 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
291 struct ldb_context
*c
= kd
->ldb
;
292 const char* attrs
[] = { "data", "type", NULL
};
293 struct ldb_result
*res
;
296 ret
= ldb_search(c
, kd
->dn
, LDB_SCOPE_BASE
, "", attrs
, &res
);
298 if (ret
!= LDB_SUCCESS
) {
299 DEBUG(0, ("Error getting default value for '%s': %s\n",
300 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
304 if (res
->count
== 0 || res
->msgs
[0]->num_elements
== 0)
307 reg_ldb_unpack_value(mem_ctx
, lp_iconv_convenience(global_loadparm
),
308 res
->msgs
[0], name
, data_type
, data
);
315 static WERROR
ldb_get_value_by_id(TALLOC_CTX
*mem_ctx
, struct hive_key
*k
,
316 int idx
, const char **name
,
317 uint32_t *data_type
, DATA_BLOB
*data
)
319 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
323 return ldb_get_default_value(mem_ctx
, k
, name
, data_type
, data
);
327 /* Do the search if necessary */
328 if (kd
->values
== NULL
) {
329 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
332 if (idx
>= kd
->value_count
)
333 return WERR_NO_MORE_ITEMS
;
335 reg_ldb_unpack_value(mem_ctx
, lp_iconv_convenience(global_loadparm
),
336 kd
->values
[idx
], name
, data_type
, data
);
342 static WERROR
ldb_get_value(TALLOC_CTX
*mem_ctx
, struct hive_key
*k
,
343 const char *name
, uint32_t *data_type
,
346 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
347 struct ldb_context
*c
= kd
->ldb
;
348 struct ldb_result
*res
;
352 if (strlen(name
) == 0) {
354 return ldb_get_default_value(mem_ctx
, k
, NULL
, data_type
, data
);
357 query
= talloc_asprintf(mem_ctx
, "(value=%s)", name
);
358 ret
= ldb_search(c
, kd
->dn
, LDB_SCOPE_ONELEVEL
, query
, NULL
, &res
);
361 if (ret
!= LDB_SUCCESS
) {
362 DEBUG(0, ("Error getting values for '%s': %s\n",
363 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
370 reg_ldb_unpack_value(mem_ctx
, lp_iconv_convenience(global_loadparm
),
371 res
->msgs
[0], NULL
, data_type
, data
);
379 static WERROR
ldb_open_key(TALLOC_CTX
*mem_ctx
, const struct hive_key
*h
,
380 const char *name
, struct hive_key
**key
)
382 struct ldb_result
*res
;
383 struct ldb_dn
*ldap_path
;
385 struct ldb_key_data
*newkd
;
386 struct ldb_key_data
*kd
= talloc_get_type(h
, struct ldb_key_data
);
387 struct ldb_context
*c
= kd
->ldb
;
389 ldap_path
= reg_path_to_ldb(mem_ctx
, h
, name
, NULL
);
391 ret
= ldb_search(c
, ldap_path
, LDB_SCOPE_BASE
, "(key=*)", NULL
, &res
);
393 if (ret
!= LDB_SUCCESS
) {
394 DEBUG(3, ("Error opening key '%s': %s\n",
395 ldb_dn_get_linearized(ldap_path
), ldb_errstring(c
)));
397 } else if (res
->count
== 0) {
398 DEBUG(3, ("Key '%s' not found\n",
399 ldb_dn_get_linearized(ldap_path
)));
404 newkd
= talloc_zero(mem_ctx
, struct ldb_key_data
);
405 newkd
->key
.ops
= ®_backend_ldb
;
406 newkd
->ldb
= talloc_reference(newkd
, kd
->ldb
);
407 newkd
->dn
= ldb_dn_copy(mem_ctx
, res
->msgs
[0]->dn
);
409 *key
= (struct hive_key
*)newkd
;
414 WERROR
reg_open_ldb_file(TALLOC_CTX
*parent_ctx
, const char *location
,
415 struct auth_session_info
*session_info
,
416 struct cli_credentials
*credentials
,
417 struct event_context
*ev_ctx
,
418 struct loadparm_context
*lp_ctx
,
421 struct ldb_key_data
*kd
;
422 struct ldb_context
*wrap
;
423 struct ldb_message
*attrs_msg
;
425 if (location
== NULL
)
426 return WERR_INVALID_PARAM
;
428 wrap
= ldb_wrap_connect(parent_ctx
, ev_ctx
, lp_ctx
,
429 location
, session_info
, credentials
, 0, NULL
);
432 DEBUG(1, (__FILE__
": unable to connect\n"));
436 attrs_msg
= ldb_msg_new(wrap
);
437 W_ERROR_HAVE_NO_MEMORY(attrs_msg
);
438 attrs_msg
->dn
= ldb_dn_new(attrs_msg
, wrap
, "@ATTRIBUTES");
439 W_ERROR_HAVE_NO_MEMORY(attrs_msg
->dn
);
440 ldb_msg_add_string(attrs_msg
, "key", "CASE_INSENSITIVE");
441 ldb_msg_add_string(attrs_msg
, "value", "CASE_INSENSITIVE");
443 ldb_add(wrap
, attrs_msg
);
445 ldb_set_debug_stderr(wrap
);
447 kd
= talloc_zero(parent_ctx
, struct ldb_key_data
);
448 kd
->key
.ops
= ®_backend_ldb
;
449 kd
->ldb
= talloc_reference(kd
, wrap
);
450 talloc_set_destructor (kd
, reg_close_ldb_key
);
451 kd
->dn
= ldb_dn_new(kd
, wrap
, "hive=NONE");
453 *k
= (struct hive_key
*)kd
;
458 static WERROR
ldb_add_key(TALLOC_CTX
*mem_ctx
, const struct hive_key
*parent
,
459 const char *name
, const char *classname
,
460 struct security_descriptor
*sd
,
461 struct hive_key
**newkey
)
463 struct ldb_key_data
*parentkd
= discard_const_p(struct ldb_key_data
, parent
);
464 struct ldb_message
*msg
;
465 struct ldb_key_data
*newkd
;
468 msg
= ldb_msg_new(mem_ctx
);
470 msg
->dn
= reg_path_to_ldb(msg
, parent
, name
, NULL
);
472 ldb_msg_add_string(msg
, "key", talloc_strdup(mem_ctx
, name
));
473 if (classname
!= NULL
)
474 ldb_msg_add_string(msg
, "classname",
475 talloc_strdup(mem_ctx
, classname
));
477 ret
= ldb_add(parentkd
->ldb
, msg
);
478 if (ret
== LDB_ERR_ENTRY_ALREADY_EXISTS
) {
479 return WERR_ALREADY_EXISTS
;
482 if (ret
!= LDB_SUCCESS
) {
483 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd
->ldb
)));
487 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg
->dn
)));
489 newkd
= talloc_zero(mem_ctx
, struct ldb_key_data
);
490 newkd
->ldb
= talloc_reference(newkd
, parentkd
->ldb
);
491 newkd
->key
.ops
= ®_backend_ldb
;
492 newkd
->dn
= talloc_steal(newkd
, msg
->dn
);
494 *newkey
= (struct hive_key
*)newkd
;
497 talloc_free(parentkd
->subkeys
);
498 parentkd
->subkeys
= NULL
;
503 static WERROR
ldb_del_value (struct hive_key
*key
, const char *child
)
506 struct ldb_key_data
*kd
= talloc_get_type(key
, struct ldb_key_data
);
508 struct ldb_message
*msg
;
509 struct ldb_dn
*childdn
;
511 if (strlen(child
) == 0) {
513 mem_ctx
= talloc_init("ldb_del_value");
515 msg
= talloc_zero(mem_ctx
, struct ldb_message
);
516 msg
->dn
= ldb_dn_copy(msg
, kd
->dn
);
517 ldb_msg_add_empty(msg
, "data", LDB_FLAG_MOD_DELETE
, NULL
);
518 ldb_msg_add_empty(msg
, "type", LDB_FLAG_MOD_DELETE
, NULL
);
520 ret
= ldb_modify(kd
->ldb
, msg
);
521 if (ret
!= LDB_SUCCESS
) {
522 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd
->ldb
)));
523 talloc_free(mem_ctx
);
527 talloc_free(mem_ctx
);
530 childdn
= ldb_dn_copy(kd
->ldb
, kd
->dn
);
531 if (!ldb_dn_add_child_fmt(childdn
, "value=%s",
532 reg_ldb_escape(childdn
, child
)))
534 talloc_free(childdn
);
538 ret
= ldb_delete(kd
->ldb
, childdn
);
540 talloc_free(childdn
);
542 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
544 } else if (ret
!= LDB_SUCCESS
) {
545 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd
->ldb
)));
551 talloc_free(kd
->values
);
557 static WERROR
ldb_del_key(const struct hive_key
*key
, const char *name
)
560 struct ldb_key_data
*parentkd
= talloc_get_type(key
, struct ldb_key_data
);
561 struct ldb_dn
*ldap_path
;
562 TALLOC_CTX
*mem_ctx
= talloc_init("ldb_del_key");
563 struct ldb_context
*c
= parentkd
->ldb
;
564 struct ldb_result
*res_keys
;
565 struct ldb_result
*res_vals
;
569 /* Verify key exists by opening it */
570 werr
= ldb_open_key(mem_ctx
, key
, name
, &hk
);
571 if (!W_ERROR_IS_OK(werr
)) {
572 talloc_free(mem_ctx
);
576 ldap_path
= reg_path_to_ldb(mem_ctx
, key
, name
, NULL
);
578 talloc_free(mem_ctx
);
582 /* Search for subkeys */
583 ret
= ldb_search(c
, ldap_path
, LDB_SCOPE_ONELEVEL
,
584 "(key=*)", NULL
, &res_keys
);
586 if (ret
!= LDB_SUCCESS
) {
587 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
588 ldb_dn_get_linearized(ldap_path
), ldb_errstring(c
)));
589 talloc_free(mem_ctx
);
593 /* Search for values */
594 ret
= ldb_search(c
, ldap_path
, LDB_SCOPE_ONELEVEL
,
595 "(value=*)", NULL
, &res_vals
);
597 if (ret
!= LDB_SUCCESS
) {
598 DEBUG(0, ("Error getting values for '%s': %s\n",
599 ldb_dn_get_linearized(ldap_path
), ldb_errstring(c
)));
600 talloc_free(mem_ctx
);
604 /* Start an explicit transaction */
605 ret
= ldb_transaction_start(c
);
607 if (ret
!= LDB_SUCCESS
) {
608 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c
)));
609 talloc_free(mem_ctx
);
613 if (res_keys
->count
|| res_vals
->count
)
615 /* Delete any subkeys */
616 for (i
= 0; i
< res_keys
->count
; i
++)
618 werr
= ldb_del_key(hk
, ldb_msg_find_attr_as_string(
621 if (!W_ERROR_IS_OK(werr
)) {
622 ret
= ldb_transaction_cancel(c
);
623 talloc_free(mem_ctx
);
628 /* Delete any values */
629 for (i
= 0; i
< res_vals
->count
; i
++)
631 werr
= ldb_del_value(hk
, ldb_msg_find_attr_as_string(
634 if (!W_ERROR_IS_OK(werr
)) {
635 ret
= ldb_transaction_cancel(c
);
636 talloc_free(mem_ctx
);
642 /* Delete the key itself */
643 ret
= ldb_delete(c
, ldap_path
);
645 if (ret
!= LDB_SUCCESS
)
647 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c
)));
648 ret
= ldb_transaction_cancel(c
);
649 talloc_free(mem_ctx
);
653 /* Commit the transaction */
654 ret
= ldb_transaction_commit(c
);
656 if (ret
!= LDB_SUCCESS
)
658 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c
)));
659 ret
= ldb_transaction_cancel(c
);
660 talloc_free(mem_ctx
);
664 talloc_free(mem_ctx
);
667 talloc_free(parentkd
->subkeys
);
668 parentkd
->subkeys
= NULL
;
673 static WERROR
ldb_set_value(struct hive_key
*parent
,
674 const char *name
, uint32_t type
,
675 const DATA_BLOB data
)
677 struct ldb_message
*msg
;
678 struct ldb_key_data
*kd
= talloc_get_type(parent
, struct ldb_key_data
);
680 TALLOC_CTX
*mem_ctx
= talloc_init("ldb_set_value");
682 msg
= reg_ldb_pack_value(kd
->ldb
, mem_ctx
, name
, type
, data
);
683 msg
->dn
= ldb_dn_copy(msg
, kd
->dn
);
685 if (strlen(name
) > 0) {
686 /* For a default value, we add/overwrite the attributes to/of the hive.
687 For a normal value, we create new childs. */
688 if (!ldb_dn_add_child_fmt(msg
->dn
, "value=%s",
689 reg_ldb_escape(mem_ctx
, name
)))
691 talloc_free(mem_ctx
);
696 ret
= ldb_add(kd
->ldb
, msg
);
697 if (ret
== LDB_ERR_ENTRY_ALREADY_EXISTS
) {
699 for (i
= 0; i
< msg
->num_elements
; i
++) {
700 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
702 ret
= ldb_modify(kd
->ldb
, msg
);
705 if (ret
!= LDB_SUCCESS
) {
706 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd
->ldb
)));
707 talloc_free(mem_ctx
);
712 talloc_free(kd
->values
);
715 talloc_free(mem_ctx
);
719 static WERROR
ldb_get_key_info(TALLOC_CTX
*mem_ctx
,
720 const struct hive_key
*key
,
721 const char **classname
,
722 uint32_t *num_subkeys
,
723 uint32_t *num_values
,
724 NTTIME
*last_change_time
,
725 uint32_t *max_subkeynamelen
,
726 uint32_t *max_valnamelen
,
727 uint32_t *max_valbufsize
)
729 struct ldb_key_data
*kd
= talloc_get_type(key
, struct ldb_key_data
);
731 if (kd
->subkeys
== NULL
) {
732 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd
));
735 if (kd
->values
== NULL
) {
736 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
740 if (classname
!= NULL
)
743 if (num_subkeys
!= NULL
) {
744 *num_subkeys
= kd
->subkey_count
;
747 if (num_values
!= NULL
) {
748 *num_values
= kd
->value_count
;
751 if (last_change_time
!= NULL
)
752 *last_change_time
= 0;
754 if (max_subkeynamelen
!= NULL
) {
756 struct ldb_message_element
*el
;
758 *max_subkeynamelen
= 0;
760 for (i
= 0; i
< kd
->subkey_count
; i
++) {
761 el
= ldb_msg_find_element(kd
->subkeys
[i
], "key");
762 *max_subkeynamelen
= MAX(*max_subkeynamelen
, el
->values
[0].length
);
766 if (max_valnamelen
!= NULL
|| max_valbufsize
!= NULL
) {
768 struct ldb_message_element
*el
;
769 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
771 if (max_valbufsize
!= NULL
)
774 if (max_valnamelen
!= NULL
)
777 for (i
= 0; i
< kd
->value_count
; i
++) {
778 if (max_valnamelen
!= NULL
) {
779 el
= ldb_msg_find_element(kd
->values
[i
], "value");
780 *max_valnamelen
= MAX(*max_valnamelen
, el
->values
[0].length
);
783 if (max_valbufsize
!= NULL
) {
785 reg_ldb_unpack_value(mem_ctx
,
786 lp_iconv_convenience(global_loadparm
),
789 *max_valbufsize
= MAX(*max_valbufsize
, data
.length
);
790 talloc_free(data
.data
);
798 static struct hive_operations reg_backend_ldb
= {
800 .add_key
= ldb_add_key
,
801 .del_key
= ldb_del_key
,
802 .get_key_by_name
= ldb_open_key
,
803 .enum_value
= ldb_get_value_by_id
,
804 .enum_key
= ldb_get_subkey_by_id
,
805 .set_value
= ldb_set_value
,
806 .get_value_by_name
= ldb_get_value
,
807 .delete_value
= ldb_del_value
,
808 .get_key_info
= ldb_get_key_info
,