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
, struct ldb_message
*msg
,
40 const char **name
, uint32_t *type
,
43 const struct ldb_val
*val
;
47 *name
= talloc_strdup(mem_ctx
,
48 ldb_msg_find_attr_as_string(msg
, "value",
51 value_type
= ldb_msg_find_attr_as_uint(msg
, "type", 0);
54 val
= ldb_msg_find_ldb_val(msg
, "data");
60 data
->length
= convert_string_talloc(mem_ctx
, lp_iconv_convenience(global_loadparm
), CH_UTF8
, CH_UTF16
,
61 val
->data
, val
->length
,
62 (void **)&data
->data
);
66 uint32_t tmp
= strtoul((char *)val
->data
, NULL
, 0);
67 *data
= data_blob_talloc(mem_ctx
, &tmp
, 4);
72 *data
= data_blob_talloc(mem_ctx
, val
->data
, val
->length
);
77 static struct ldb_message
*reg_ldb_pack_value(struct ldb_context
*ctx
,
80 uint32_t type
, DATA_BLOB data
)
83 struct ldb_message
*msg
= talloc_zero(mem_ctx
, struct ldb_message
);
86 ldb_msg_add_string(msg
, "value", talloc_strdup(mem_ctx
, name
));
91 val
.length
= convert_string_talloc(mem_ctx
, lp_iconv_convenience(global_loadparm
), CH_UTF16
, CH_UNIX
,
95 ldb_msg_add_value(msg
, "data", &val
, NULL
);
99 ldb_msg_add_string(msg
, "data",
100 talloc_asprintf(mem_ctx
, "0x%x",
101 IVAL(data
.data
, 0)));
104 ldb_msg_add_value(msg
, "data", &data
, NULL
);
108 type_s
= talloc_asprintf(mem_ctx
, "%u", type
);
109 ldb_msg_add_string(msg
, "type", type_s
);
114 static char *reg_ldb_escape(TALLOC_CTX
*mem_ctx
, const char *value
)
118 val
.data
= discard_const_p(uint8_t, value
);
119 val
.length
= strlen(value
);
121 return ldb_dn_escape_value(mem_ctx
, val
);
124 static int reg_close_ldb_key(struct ldb_key_data
*key
)
126 if (key
->subkeys
!= NULL
) {
127 talloc_free(key
->subkeys
);
131 if (key
->values
!= NULL
) {
132 talloc_free(key
->values
);
138 static struct ldb_dn
*reg_path_to_ldb(TALLOC_CTX
*mem_ctx
,
139 const struct hive_key
*from
,
140 const char *path
, const char *add
)
142 TALLOC_CTX
*local_ctx
;
144 char *mypath
= talloc_strdup(mem_ctx
, path
);
146 struct ldb_key_data
*kd
= talloc_get_type(from
, struct ldb_key_data
);
147 struct ldb_context
*ldb
= kd
->ldb
;
149 local_ctx
= talloc_new(mem_ctx
);
152 ret
= ldb_dn_new(mem_ctx
, ldb
, add
);
154 ret
= ldb_dn_new(mem_ctx
, ldb
, NULL
);
156 if (!ldb_dn_validate(ret
)) {
158 talloc_free(local_ctx
);
165 begin
= strrchr(mypath
, '\\');
167 if (begin
) keyname
= begin
+ 1;
168 else keyname
= mypath
;
170 if(strlen(keyname
)) {
171 if (!ldb_dn_add_base_fmt(ret
, "key=%s",
172 reg_ldb_escape(local_ctx
,
175 talloc_free(local_ctx
);
187 ldb_dn_add_base(ret
, kd
->dn
);
189 talloc_free(local_ctx
);
194 static WERROR
cache_subkeys(struct ldb_key_data
*kd
)
196 struct ldb_context
*c
= kd
->ldb
;
197 struct ldb_result
*res
;
200 ret
= ldb_search(c
, kd
->dn
, LDB_SCOPE_ONELEVEL
, "(key=*)", NULL
, &res
);
202 if (ret
!= LDB_SUCCESS
) {
203 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
204 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
208 kd
->subkey_count
= res
->count
;
209 kd
->subkeys
= talloc_steal(kd
, res
->msgs
);
215 static WERROR
cache_values(struct ldb_key_data
*kd
)
217 struct ldb_context
*c
= kd
->ldb
;
218 struct ldb_result
*res
;
221 ret
= ldb_search(c
, kd
->dn
, LDB_SCOPE_ONELEVEL
,
222 "(value=*)", NULL
, &res
);
224 if (ret
!= LDB_SUCCESS
) {
225 DEBUG(0, ("Error getting values for '%s': %s\n",
226 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
229 kd
->value_count
= res
->count
;
230 kd
->values
= talloc_steal(kd
, res
->msgs
);
236 static WERROR
ldb_get_subkey_by_id(TALLOC_CTX
*mem_ctx
,
237 const struct hive_key
*k
, uint32_t idx
,
239 const char **classname
,
240 NTTIME
*last_mod_time
)
242 struct ldb_message_element
*el
;
243 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
245 /* Do a search if necessary */
246 if (kd
->subkeys
== NULL
) {
247 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd
));
250 if (idx
>= kd
->subkey_count
)
251 return WERR_NO_MORE_ITEMS
;
253 el
= ldb_msg_find_element(kd
->subkeys
[idx
], "key");
254 SMB_ASSERT(el
!= NULL
);
255 SMB_ASSERT(el
->num_values
!= 0);
258 *name
= talloc_strdup(mem_ctx
, (char *)el
->values
[0].data
);
260 if (classname
!= NULL
)
261 *classname
= NULL
; /* TODO: Store properly */
263 if (last_mod_time
!= NULL
)
264 *last_mod_time
= 0; /* TODO: we need to add this to the
265 ldb backend properly */
270 static WERROR
ldb_get_value_by_id(TALLOC_CTX
*mem_ctx
, struct hive_key
*k
,
271 int idx
, const char **name
,
272 uint32_t *data_type
, DATA_BLOB
*data
)
274 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
276 /* Do the search if necessary */
277 if (kd
->values
== NULL
) {
278 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
281 if (idx
>= kd
->value_count
)
282 return WERR_NO_MORE_ITEMS
;
284 reg_ldb_unpack_value(mem_ctx
, kd
->values
[idx
],
285 name
, data_type
, data
);
290 static WERROR
ldb_get_value(TALLOC_CTX
*mem_ctx
, struct hive_key
*k
,
291 const char *name
, uint32_t *data_type
,
294 struct ldb_key_data
*kd
= talloc_get_type(k
, struct ldb_key_data
);
295 struct ldb_context
*c
= kd
->ldb
;
296 struct ldb_result
*res
;
298 char *query
= talloc_asprintf(mem_ctx
, "(value=%s)", name
);
300 ret
= ldb_search(c
, kd
->dn
, LDB_SCOPE_ONELEVEL
, query
, NULL
, &res
);
304 if (ret
!= LDB_SUCCESS
) {
305 DEBUG(0, ("Error getting values for '%s': %s\n",
306 ldb_dn_get_linearized(kd
->dn
), ldb_errstring(c
)));
313 reg_ldb_unpack_value(mem_ctx
, res
->msgs
[0], NULL
, data_type
, data
);
318 static WERROR
ldb_open_key(TALLOC_CTX
*mem_ctx
, const struct hive_key
*h
,
319 const char *name
, struct hive_key
**key
)
321 struct ldb_result
*res
;
322 struct ldb_dn
*ldap_path
;
324 struct ldb_key_data
*newkd
;
325 struct ldb_key_data
*kd
= talloc_get_type(h
, struct ldb_key_data
);
326 struct ldb_context
*c
= kd
->ldb
;
328 ldap_path
= reg_path_to_ldb(mem_ctx
, h
, name
, NULL
);
330 ret
= ldb_search(c
, ldap_path
, LDB_SCOPE_BASE
, "(key=*)", NULL
, &res
);
332 if (ret
!= LDB_SUCCESS
) {
333 DEBUG(3, ("Error opening key '%s': %s\n",
334 ldb_dn_get_linearized(ldap_path
), ldb_errstring(c
)));
336 } else if (res
->count
== 0) {
337 DEBUG(3, ("Key '%s' not found\n",
338 ldb_dn_get_linearized(ldap_path
)));
343 newkd
= talloc_zero(mem_ctx
, struct ldb_key_data
);
344 newkd
->key
.ops
= ®_backend_ldb
;
345 newkd
->ldb
= talloc_reference(newkd
, kd
->ldb
);
346 newkd
->dn
= ldb_dn_copy(mem_ctx
, res
->msgs
[0]->dn
);
348 *key
= (struct hive_key
*)newkd
;
355 WERROR
reg_open_ldb_file(TALLOC_CTX
*parent_ctx
, const char *location
,
356 struct auth_session_info
*session_info
,
357 struct cli_credentials
*credentials
,
358 struct loadparm_context
*lp_ctx
,
361 struct ldb_key_data
*kd
;
362 struct ldb_context
*wrap
;
363 struct ldb_message
*attrs_msg
;
365 if (location
== NULL
)
366 return WERR_INVALID_PARAM
;
368 wrap
= ldb_wrap_connect(parent_ctx
, lp_ctx
,
369 location
, session_info
, credentials
, 0, NULL
);
372 DEBUG(1, (__FILE__
": unable to connect\n"));
376 attrs_msg
= ldb_msg_new(wrap
);
377 W_ERROR_HAVE_NO_MEMORY(attrs_msg
);
378 attrs_msg
->dn
= ldb_dn_new(attrs_msg
, wrap
, "@ATTRIBUTES");
379 W_ERROR_HAVE_NO_MEMORY(attrs_msg
->dn
);
380 ldb_msg_add_string(attrs_msg
, "key", "CASE_INSENSITIVE");
381 ldb_msg_add_string(attrs_msg
, "value", "CASE_INSENSITIVE");
383 ldb_add(wrap
, attrs_msg
);
385 ldb_set_debug_stderr(wrap
);
387 kd
= talloc_zero(parent_ctx
, struct ldb_key_data
);
388 kd
->key
.ops
= ®_backend_ldb
;
389 kd
->ldb
= talloc_reference(kd
, wrap
);
390 talloc_set_destructor (kd
, reg_close_ldb_key
);
391 kd
->dn
= ldb_dn_new(kd
, wrap
, "hive=NONE");
393 *k
= (struct hive_key
*)kd
;
398 static WERROR
ldb_add_key(TALLOC_CTX
*mem_ctx
, const struct hive_key
*parent
,
399 const char *name
, const char *classname
,
400 struct security_descriptor
*sd
,
401 struct hive_key
**newkey
)
403 struct ldb_key_data
*parentkd
= discard_const_p(struct ldb_key_data
, parent
);
404 struct ldb_message
*msg
;
405 struct ldb_key_data
*newkd
;
408 msg
= ldb_msg_new(mem_ctx
);
410 msg
->dn
= reg_path_to_ldb(msg
, parent
, name
, NULL
);
412 ldb_msg_add_string(msg
, "key", talloc_strdup(mem_ctx
, name
));
413 if (classname
!= NULL
)
414 ldb_msg_add_string(msg
, "classname",
415 talloc_strdup(mem_ctx
, classname
));
417 ret
= ldb_add(parentkd
->ldb
, msg
);
418 if (ret
== LDB_ERR_ENTRY_ALREADY_EXISTS
) {
419 return WERR_ALREADY_EXISTS
;
422 if (ret
!= LDB_SUCCESS
) {
423 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd
->ldb
)));
427 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg
->dn
)));
429 newkd
= talloc_zero(mem_ctx
, struct ldb_key_data
);
430 newkd
->ldb
= talloc_reference(newkd
, parentkd
->ldb
);
431 newkd
->key
.ops
= ®_backend_ldb
;
432 newkd
->dn
= talloc_steal(newkd
, msg
->dn
);
434 *newkey
= (struct hive_key
*)newkd
;
437 talloc_free(parentkd
->subkeys
);
438 parentkd
->subkeys
= NULL
;
443 static WERROR
ldb_del_value (struct hive_key
*key
, const char *child
)
446 struct ldb_key_data
*kd
= talloc_get_type(key
, struct ldb_key_data
);
447 struct ldb_dn
*childdn
;
449 childdn
= ldb_dn_copy(kd
->ldb
, kd
->dn
);
450 if (!ldb_dn_add_child_fmt(childdn
, "value=%s",
451 reg_ldb_escape(childdn
, child
)))
453 talloc_free(childdn
);
457 ret
= ldb_delete(kd
->ldb
, childdn
);
459 talloc_free(childdn
);
461 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
463 } else if (ret
!= LDB_SUCCESS
) {
464 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd
->ldb
)));
469 talloc_free(kd
->values
);
475 static WERROR
ldb_del_key(const struct hive_key
*key
, const char *name
)
478 struct ldb_key_data
*parentkd
= talloc_get_type(key
, struct ldb_key_data
);
479 struct ldb_dn
*ldap_path
;
480 TALLOC_CTX
*mem_ctx
= talloc_init("ldb_del_key");
481 struct ldb_context
*c
= parentkd
->ldb
;
482 struct ldb_result
*res_keys
;
483 struct ldb_result
*res_vals
;
487 /* Verify key exists by opening it */
488 werr
= ldb_open_key(mem_ctx
, key
, name
, &hk
);
489 if (!W_ERROR_IS_OK(werr
)) {
490 talloc_free(mem_ctx
);
494 ldap_path
= reg_path_to_ldb(mem_ctx
, key
, name
, NULL
);
496 talloc_free(mem_ctx
);
500 /* Search for subkeys */
501 ret
= ldb_search(c
, ldap_path
, LDB_SCOPE_ONELEVEL
,
502 "(key=*)", NULL
, &res_keys
);
504 if (ret
!= LDB_SUCCESS
) {
505 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
506 ldb_dn_get_linearized(ldap_path
), ldb_errstring(c
)));
507 talloc_free(mem_ctx
);
511 /* Search for values */
512 ret
= ldb_search(c
, ldap_path
, LDB_SCOPE_ONELEVEL
,
513 "(value=*)", NULL
, &res_vals
);
515 if (ret
!= LDB_SUCCESS
) {
516 DEBUG(0, ("Error getting values for '%s': %s\n",
517 ldb_dn_get_linearized(ldap_path
), ldb_errstring(c
)));
518 talloc_free(mem_ctx
);
522 /* Start an explicit transaction */
523 ret
= ldb_transaction_start(c
);
525 if (ret
!= LDB_SUCCESS
) {
526 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c
)));
527 talloc_free(mem_ctx
);
531 if (res_keys
->count
|| res_vals
->count
)
533 /* Delete any subkeys */
534 for (i
= 0; i
< res_keys
->count
; i
++)
536 werr
= ldb_del_key(hk
, ldb_msg_find_attr_as_string(
539 if (!W_ERROR_IS_OK(werr
)) {
540 ret
= ldb_transaction_cancel(c
);
541 talloc_free(mem_ctx
);
546 /* Delete any values */
547 for (i
= 0; i
< res_vals
->count
; i
++)
549 werr
= ldb_del_value(hk
, ldb_msg_find_attr_as_string(
552 if (!W_ERROR_IS_OK(werr
)) {
553 ret
= ldb_transaction_cancel(c
);
554 talloc_free(mem_ctx
);
560 /* Delete the key itself */
561 ret
= ldb_delete(c
, ldap_path
);
563 if (ret
!= LDB_SUCCESS
)
565 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c
)));
566 ret
= ldb_transaction_cancel(c
);
567 talloc_free(mem_ctx
);
571 /* Commit the transaction */
572 ret
= ldb_transaction_commit(c
);
574 if (ret
!= LDB_SUCCESS
)
576 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c
)));
577 ret
= ldb_transaction_cancel(c
);
578 talloc_free(mem_ctx
);
582 talloc_free(mem_ctx
);
585 talloc_free(parentkd
->subkeys
);
586 parentkd
->subkeys
= NULL
;
591 static WERROR
ldb_set_value(struct hive_key
*parent
,
592 const char *name
, uint32_t type
,
593 const DATA_BLOB data
)
595 struct ldb_message
*msg
;
596 struct ldb_key_data
*kd
= talloc_get_type(parent
, struct ldb_key_data
);
598 TALLOC_CTX
*mem_ctx
= talloc_init("ldb_set_value");
600 msg
= reg_ldb_pack_value(kd
->ldb
, mem_ctx
, name
, type
, data
);
602 msg
->dn
= ldb_dn_copy(msg
, kd
->dn
);
603 if (!ldb_dn_add_child_fmt(msg
->dn
, "value=%s",
604 reg_ldb_escape(mem_ctx
, name
)))
606 talloc_free(mem_ctx
);
610 ret
= ldb_add(kd
->ldb
, msg
);
611 if (ret
== LDB_ERR_ENTRY_ALREADY_EXISTS
) {
613 for (i
= 0; i
< msg
->num_elements
; i
++) {
614 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
616 ret
= ldb_modify(kd
->ldb
, msg
);
619 if (ret
!= LDB_SUCCESS
) {
620 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(kd
->ldb
)));
621 talloc_free(mem_ctx
);
626 talloc_free(kd
->values
);
629 talloc_free(mem_ctx
);
633 static WERROR
ldb_get_key_info(TALLOC_CTX
*mem_ctx
,
634 const struct hive_key
*key
,
635 const char **classname
,
636 uint32_t *num_subkeys
,
637 uint32_t *num_values
,
638 NTTIME
*last_change_time
,
639 uint32_t *max_subkeynamelen
,
640 uint32_t *max_valnamelen
,
641 uint32_t *max_valbufsize
)
643 struct ldb_key_data
*kd
= talloc_get_type(key
, struct ldb_key_data
);
645 if (kd
->subkeys
== NULL
) {
646 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd
));
649 if (kd
->values
== NULL
) {
650 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
654 if (classname
!= NULL
)
657 if (num_subkeys
!= NULL
) {
658 *num_subkeys
= kd
->subkey_count
;
661 if (num_values
!= NULL
) {
662 *num_values
= kd
->value_count
;
665 if (last_change_time
!= NULL
)
666 *last_change_time
= 0;
668 if (max_subkeynamelen
!= NULL
) {
670 struct ldb_message_element
*el
;
672 *max_subkeynamelen
= 0;
674 for (i
= 0; i
< kd
->subkey_count
; i
++) {
675 el
= ldb_msg_find_element(kd
->subkeys
[i
], "key");
676 *max_subkeynamelen
= MAX(*max_subkeynamelen
, el
->values
[0].length
);
680 if (max_valnamelen
!= NULL
|| max_valbufsize
!= NULL
) {
682 struct ldb_message_element
*el
;
683 W_ERROR_NOT_OK_RETURN(cache_values(kd
));
685 if (max_valbufsize
!= NULL
)
688 if (max_valnamelen
!= NULL
)
691 for (i
= 0; i
< kd
->value_count
; i
++) {
692 if (max_valnamelen
!= NULL
) {
693 el
= ldb_msg_find_element(kd
->values
[i
], "value");
694 *max_valnamelen
= MAX(*max_valnamelen
, el
->values
[0].length
);
697 if (max_valbufsize
!= NULL
) {
699 reg_ldb_unpack_value(mem_ctx
, kd
->values
[i
], NULL
,
701 *max_valbufsize
= MAX(*max_valbufsize
, data
.length
);
702 talloc_free(data
.data
);
710 static struct hive_operations reg_backend_ldb
= {
712 .add_key
= ldb_add_key
,
713 .del_key
= ldb_del_key
,
714 .get_key_by_name
= ldb_open_key
,
715 .enum_value
= ldb_get_value_by_id
,
716 .enum_key
= ldb_get_subkey_by_id
,
717 .set_value
= ldb_set_value
,
718 .get_value_by_name
= ldb_get_value
,
719 .delete_value
= ldb_del_value
,
720 .get_key_info
= ldb_get_key_info
,