2 Unix SMB/CIFS implementation.
4 Copyright (C) 2004-2007, Jelmer Vernooij, jelmer@samba.org
5 Copyright (C) 2008 Matthias Dieter Wallnöfer, mwallnoefer@yahoo.de
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 int subkey_count, value_count;
40 static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx,
41 struct smb_iconv_convenience *iconv_convenience,
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",
54 value_type = ldb_msg_find_attr_as_uint(msg, "type", 0);
57 val = ldb_msg_find_ldb_val(msg, "data");
63 data->length = convert_string_talloc(mem_ctx, iconv_convenience, CH_UNIX, CH_UTF16,
64 val->data, val->length,
65 (void **)&data->data);
70 *data = strhex_to_data_blob((char *)val->data);
78 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
79 *data = data_blob_talloc(mem_ctx, &tmp, 4);
84 *data = data_blob_talloc(mem_ctx, val->data, val->length);
89 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
92 uint32_t type, DATA_BLOB data)
95 struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
98 ldb_msg_add_string(msg, "value", talloc_strdup(mem_ctx, name));
103 val.length = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF16, CH_UNIX,
107 ldb_msg_add_value(msg, "data", &val, NULL);
111 ldb_msg_add_string(msg, "data",
112 data_blob_hex_string(mem_ctx, &data));
116 ldb_msg_add_string(msg, "data",
117 talloc_asprintf(mem_ctx, "0x%x",
118 IVAL(data.data, 0)));
121 ldb_msg_add_value(msg, "data", &data, NULL);
125 type_s = talloc_asprintf(mem_ctx, "%u", type);
126 ldb_msg_add_string(msg, "type", type_s);
131 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
135 val.data = discard_const_p(uint8_t, value);
136 val.length = strlen(value);
138 return ldb_dn_escape_value(mem_ctx, val);
141 static int reg_close_ldb_key(struct ldb_key_data *key)
143 if (key->subkeys != NULL) {
144 talloc_free(key->subkeys);
148 if (key->values != NULL) {
149 talloc_free(key->values);
155 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
156 const struct hive_key *from,
157 const char *path, const char *add)
159 TALLOC_CTX *local_ctx;
161 char *mypath = talloc_strdup(mem_ctx, path);
163 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
164 struct ldb_context *ldb = kd->ldb;
166 local_ctx = talloc_new(mem_ctx);
169 ret = ldb_dn_new(mem_ctx, ldb, add);
171 ret = ldb_dn_new(mem_ctx, ldb, NULL);
173 if (!ldb_dn_validate(ret)) {
175 talloc_free(local_ctx);
182 begin = strrchr(mypath, '\\');
184 if (begin) keyname = begin + 1;
185 else keyname = mypath;
187 if(strlen(keyname)) {
188 if (!ldb_dn_add_base_fmt(ret, "key=%s",
189 reg_ldb_escape(local_ctx,
192 talloc_free(local_ctx);
204 ldb_dn_add_base(ret, kd->dn);
206 talloc_free(local_ctx);
211 static WERROR cache_subkeys(struct ldb_key_data *kd)
213 struct ldb_context *c = kd->ldb;
214 struct ldb_result *res;
217 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL, &res);
219 if (ret != LDB_SUCCESS) {
220 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
221 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
225 kd->subkey_count = res->count;
226 kd->subkeys = talloc_steal(kd, res->msgs);
232 static WERROR cache_values(struct ldb_key_data *kd)
234 struct ldb_context *c = kd->ldb;
235 struct ldb_result *res;
238 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL,
239 "(value=*)", NULL, &res);
241 if (ret != LDB_SUCCESS) {
242 DEBUG(0, ("Error getting values for '%s': %s\n",
243 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
246 kd->value_count = res->count;
247 kd->values = talloc_steal(kd, res->msgs);
253 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
254 const struct hive_key *k, uint32_t idx,
256 const char **classname,
257 NTTIME *last_mod_time)
259 struct ldb_message_element *el;
260 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
265 if (classname != NULL)
266 *classname = NULL; /* TODO: Store properly */
267 if (last_mod_time != NULL)
268 *last_mod_time = 0; /* TODO: we need to add this to the
269 ldb backend properly */
271 /* Do a search if necessary */
272 if (kd->subkeys == NULL) {
273 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
276 if (idx >= kd->subkey_count)
277 return WERR_NO_MORE_ITEMS;
279 el = ldb_msg_find_element(kd->subkeys[idx], "key");
280 SMB_ASSERT(el != NULL);
281 SMB_ASSERT(el->num_values != 0);
284 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
289 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
290 const char** name, uint32_t *data_type,
293 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
294 struct ldb_context *c = kd->ldb;
295 const char* attrs[] = { "data", "type", NULL };
296 struct ldb_result *res;
299 ret = ldb_search(c, kd->dn, LDB_SCOPE_BASE, "", attrs, &res);
301 if (ret != LDB_SUCCESS) {
302 DEBUG(0, ("Error getting default value for '%s': %s\n",
303 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
307 if (res->count == 0 || res->msgs[0]->num_elements == 0)
310 reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
311 res->msgs[0], name, data_type, data);
318 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
319 int idx, const char **name,
320 uint32_t *data_type, DATA_BLOB *data)
322 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
324 /* if default value exists, give it back */
325 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type, data)))
331 /* Do the search if necessary */
332 if (kd->values == NULL) {
333 W_ERROR_NOT_OK_RETURN(cache_values(kd));
336 if (idx >= kd->value_count)
337 return WERR_NO_MORE_ITEMS;
339 reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
340 kd->values[idx], name, data_type, data);
345 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
346 const char *name, uint32_t *data_type,
349 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
350 struct ldb_context *c = kd->ldb;
351 struct ldb_result *res;
355 if (strlen(name) == 0) {
357 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
360 query = talloc_asprintf(mem_ctx, "(value=%s)", name);
361 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, query, NULL, &res);
364 if (ret != LDB_SUCCESS) {
365 DEBUG(0, ("Error getting values for '%s': %s\n",
366 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
373 reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
374 res->msgs[0], NULL, data_type, data);
382 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
383 const char *name, struct hive_key **key)
385 struct ldb_result *res;
386 struct ldb_dn *ldap_path;
388 struct ldb_key_data *newkd;
389 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
390 struct ldb_context *c = kd->ldb;
392 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
394 ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL, &res);
396 if (ret != LDB_SUCCESS) {
397 DEBUG(3, ("Error opening key '%s': %s\n",
398 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
400 } else if (res->count == 0) {
401 DEBUG(3, ("Key '%s' not found\n",
402 ldb_dn_get_linearized(ldap_path)));
407 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
408 newkd->key.ops = ®_backend_ldb;
409 newkd->ldb = talloc_reference(newkd, kd->ldb);
410 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
412 *key = (struct hive_key *)newkd;
417 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
418 struct auth_session_info *session_info,
419 struct cli_credentials *credentials,
420 struct event_context *ev_ctx,
421 struct loadparm_context *lp_ctx,
424 struct ldb_key_data *kd;
425 struct ldb_context *wrap;
426 struct ldb_message *attrs_msg;
428 if (location == NULL)
429 return WERR_INVALID_PARAM;
431 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
432 location, session_info, credentials, 0, NULL);
435 DEBUG(1, (__FILE__": unable to connect\n"));
439 attrs_msg = ldb_msg_new(wrap);
440 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
441 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
442 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
443 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
444 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
446 ldb_add(wrap, attrs_msg);
448 ldb_set_debug_stderr(wrap);
450 kd = talloc_zero(parent_ctx, struct ldb_key_data);
451 kd->key.ops = ®_backend_ldb;
452 kd->ldb = talloc_reference(kd, wrap);
453 talloc_set_destructor (kd, reg_close_ldb_key);
454 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
456 *k = (struct hive_key *)kd;
461 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
462 const char *name, const char *classname,
463 struct security_descriptor *sd,
464 struct hive_key **newkey)
466 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
467 struct ldb_message *msg;
468 struct ldb_key_data *newkd;
471 msg = ldb_msg_new(mem_ctx);
473 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
475 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
476 if (classname != NULL)
477 ldb_msg_add_string(msg, "classname",
478 talloc_strdup(mem_ctx, classname));
480 ret = ldb_add(parentkd->ldb, msg);
481 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
482 return WERR_ALREADY_EXISTS;
485 if (ret != LDB_SUCCESS) {
486 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
490 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
492 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
493 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
494 newkd->key.ops = ®_backend_ldb;
495 newkd->dn = talloc_steal(newkd, msg->dn);
497 *newkey = (struct hive_key *)newkd;
500 talloc_free(parentkd->subkeys);
501 parentkd->subkeys = NULL;
506 static WERROR ldb_del_value (struct hive_key *key, const char *child)
509 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
511 struct ldb_message *msg;
512 struct ldb_dn *childdn;
514 if (strlen(child) == 0) {
516 mem_ctx = talloc_init("ldb_del_value");
518 msg = talloc_zero(mem_ctx, struct ldb_message);
519 msg->dn = ldb_dn_copy(msg, kd->dn);
520 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
521 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
523 ret = ldb_modify(kd->ldb, msg);
524 if (ret != LDB_SUCCESS) {
525 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
526 talloc_free(mem_ctx);
530 talloc_free(mem_ctx);
533 childdn = ldb_dn_copy(kd->ldb, kd->dn);
534 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
535 reg_ldb_escape(childdn, child)))
537 talloc_free(childdn);
541 ret = ldb_delete(kd->ldb, childdn);
543 talloc_free(childdn);
545 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
547 } else if (ret != LDB_SUCCESS) {
548 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
554 talloc_free(kd->values);
560 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
563 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
564 struct ldb_dn *ldap_path;
565 TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
566 struct ldb_context *c = parentkd->ldb;
567 struct ldb_result *res_keys;
568 struct ldb_result *res_vals;
572 /* Verify key exists by opening it */
573 werr = ldb_open_key(mem_ctx, key, name, &hk);
574 if (!W_ERROR_IS_OK(werr)) {
575 talloc_free(mem_ctx);
579 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
581 talloc_free(mem_ctx);
585 /* Search for subkeys */
586 ret = ldb_search(c, ldap_path, LDB_SCOPE_ONELEVEL,
587 "(key=*)", NULL, &res_keys);
589 if (ret != LDB_SUCCESS) {
590 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
591 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
592 talloc_free(mem_ctx);
596 /* Search for values */
597 ret = ldb_search(c, ldap_path, LDB_SCOPE_ONELEVEL,
598 "(value=*)", NULL, &res_vals);
600 if (ret != LDB_SUCCESS) {
601 DEBUG(0, ("Error getting values for '%s': %s\n",
602 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
603 talloc_free(mem_ctx);
607 /* Start an explicit transaction */
608 ret = ldb_transaction_start(c);
610 if (ret != LDB_SUCCESS) {
611 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
612 talloc_free(mem_ctx);
616 if (res_keys->count || res_vals->count)
618 /* Delete any subkeys */
619 for (i = 0; i < res_keys->count; i++)
621 werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
624 if (!W_ERROR_IS_OK(werr)) {
625 ret = ldb_transaction_cancel(c);
626 talloc_free(mem_ctx);
631 /* Delete any values */
632 for (i = 0; i < res_vals->count; i++)
634 werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
637 if (!W_ERROR_IS_OK(werr)) {
638 ret = ldb_transaction_cancel(c);
639 talloc_free(mem_ctx);
645 /* Delete the key itself */
646 ret = ldb_delete(c, ldap_path);
648 if (ret != LDB_SUCCESS)
650 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
651 ret = ldb_transaction_cancel(c);
652 talloc_free(mem_ctx);
656 /* Commit the transaction */
657 ret = ldb_transaction_commit(c);
659 if (ret != LDB_SUCCESS)
661 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
662 ret = ldb_transaction_cancel(c);
663 talloc_free(mem_ctx);
667 talloc_free(mem_ctx);
670 talloc_free(parentkd->subkeys);
671 parentkd->subkeys = NULL;
676 static WERROR ldb_set_value(struct hive_key *parent,
677 const char *name, uint32_t type,
678 const DATA_BLOB data)
680 struct ldb_message *msg;
681 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
683 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
685 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
686 msg->dn = ldb_dn_copy(msg, kd->dn);
688 if (strlen(name) > 0) {
689 /* For a default value, we add/overwrite the attributes to/of the hive.
690 For a normal value, we create new childs. */
691 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
692 reg_ldb_escape(mem_ctx, name)))
694 talloc_free(mem_ctx);
699 ret = ldb_add(kd->ldb, msg);
700 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
702 for (i = 0; i < msg->num_elements; i++) {
703 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
705 ret = ldb_modify(kd->ldb, msg);
708 if (ret != LDB_SUCCESS) {
709 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
710 talloc_free(mem_ctx);
715 talloc_free(kd->values);
718 talloc_free(mem_ctx);
722 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
723 const struct hive_key *key,
724 const char **classname,
725 uint32_t *num_subkeys,
726 uint32_t *num_values,
727 NTTIME *last_change_time,
728 uint32_t *max_subkeynamelen,
729 uint32_t *max_valnamelen,
730 uint32_t *max_valbufsize)
732 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
735 if (classname != NULL)
737 if (num_subkeys != NULL)
739 if (num_values != NULL)
741 if (last_change_time != NULL)
742 *last_change_time = 0;
743 if (max_subkeynamelen != NULL)
744 *max_subkeynamelen = 0;
745 if (max_valnamelen != NULL)
747 if (max_valbufsize != NULL)
750 if (kd->subkeys == NULL) {
751 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
754 if (kd->values == NULL) {
755 W_ERROR_NOT_OK_RETURN(cache_values(kd));
758 if (num_subkeys != NULL) {
759 *num_subkeys = kd->subkey_count;
761 if (num_values != NULL) {
762 *num_values = kd->value_count;
766 if (max_subkeynamelen != NULL) {
768 struct ldb_message_element *el;
770 *max_subkeynamelen = 0;
772 for (i = 0; i < kd->subkey_count; i++) {
773 el = ldb_msg_find_element(kd->subkeys[i], "key");
774 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
778 if (max_valnamelen != NULL || max_valbufsize != NULL) {
780 struct ldb_message_element *el;
781 W_ERROR_NOT_OK_RETURN(cache_values(kd));
783 if (max_valbufsize != NULL)
786 if (max_valnamelen != NULL)
789 for (i = 0; i < kd->value_count; i++) {
790 if (max_valnamelen != NULL) {
791 el = ldb_msg_find_element(kd->values[i], "value");
792 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
795 if (max_valbufsize != NULL) {
797 reg_ldb_unpack_value(mem_ctx,
798 lp_iconv_convenience(global_loadparm),
801 *max_valbufsize = MAX(*max_valbufsize, data.length);
802 talloc_free(data.data);
810 static struct hive_operations reg_backend_ldb = {
812 .add_key = ldb_add_key,
813 .del_key = ldb_del_key,
814 .get_key_by_name = ldb_open_key,
815 .enum_value = ldb_get_value_by_id,
816 .enum_key = ldb_get_subkey_by_id,
817 .set_value = ldb_set_value,
818 .get_value_by_name = ldb_get_value,
819 .delete_value = ldb_del_value,
820 .get_key_info = ldb_get_key_info,