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 convert_string_talloc(mem_ctx, CH_UTF8, CH_UTF16,
65 val->data, val->length,
66 (void **)&data->data, &data->length, false);
75 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
76 *data = data_blob_talloc(mem_ctx, NULL, 4);
77 SIVAL(data->data, 0, tmp);
87 *data = data_blob_talloc(mem_ctx, val->data, val->length);
96 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
99 uint32_t type, DATA_BLOB data)
102 struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
105 ldb_msg_add_string(msg, "value", talloc_strdup(mem_ctx, name));
110 if ((data.length > 0) && (data.data != NULL)
111 && (data.data[0] != '\0')) {
112 convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8,
115 (void **)&val.data, &val.length, false);
116 ldb_msg_add_value(msg, "data", &val, NULL);
118 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
123 if ((data.length > 0) && (data.data != NULL)) {
124 ldb_msg_add_string(msg, "data",
125 talloc_asprintf(mem_ctx, "0x%x",
126 IVAL(data.data, 0)));
128 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
134 if ((data.length > 0) && (data.data != NULL)
135 && (data.data[0] != '\0')) {
136 ldb_msg_add_value(msg, "data", &data, NULL);
138 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
143 type_s = talloc_asprintf(mem_ctx, "%u", type);
144 ldb_msg_add_string(msg, "type", type_s);
149 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
153 val.data = discard_const_p(uint8_t, value);
154 val.length = strlen(value);
156 return ldb_dn_escape_value(mem_ctx, val);
159 static int reg_close_ldb_key(struct ldb_key_data *key)
161 if (key->subkeys != NULL) {
162 talloc_free(key->subkeys);
166 if (key->values != NULL) {
167 talloc_free(key->values);
173 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
174 const struct hive_key *from,
175 const char *path, const char *add)
177 TALLOC_CTX *local_ctx;
179 char *mypath = talloc_strdup(mem_ctx, path);
181 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
182 struct ldb_context *ldb = kd->ldb;
184 local_ctx = talloc_new(mem_ctx);
187 ret = ldb_dn_new(mem_ctx, ldb, add);
189 ret = ldb_dn_new(mem_ctx, ldb, NULL);
191 if (!ldb_dn_validate(ret)) {
193 talloc_free(local_ctx);
200 begin = strrchr(mypath, '\\');
202 if (begin) keyname = begin + 1;
203 else keyname = mypath;
205 if(strlen(keyname)) {
206 if (!ldb_dn_add_base_fmt(ret, "key=%s",
207 reg_ldb_escape(local_ctx,
210 talloc_free(local_ctx);
222 ldb_dn_add_base(ret, kd->dn);
224 talloc_free(local_ctx);
229 static WERROR cache_subkeys(struct ldb_key_data *kd)
231 struct ldb_context *c = kd->ldb;
232 struct ldb_result *res;
235 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "(key=*)");
237 if (ret != LDB_SUCCESS) {
238 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
239 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
243 kd->subkey_count = res->count;
244 kd->subkeys = talloc_steal(kd, res->msgs);
250 static WERROR cache_values(struct ldb_key_data *kd)
252 struct ldb_context *c = kd->ldb;
253 struct ldb_result *res;
256 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
259 if (ret != LDB_SUCCESS) {
260 DEBUG(0, ("Error getting values for '%s': %s\n",
261 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
265 kd->value_count = res->count;
266 kd->values = talloc_steal(kd, res->msgs);
273 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
274 const struct hive_key *k, uint32_t idx,
276 const char **classname,
277 NTTIME *last_mod_time)
279 struct ldb_message_element *el;
280 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
285 if (classname != NULL)
286 *classname = NULL; /* TODO: Store properly */
287 if (last_mod_time != NULL)
288 *last_mod_time = 0; /* TODO: we need to add this to the
289 ldb backend properly */
291 /* Do a search if necessary */
292 if (kd->subkeys == NULL) {
293 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
296 if (idx >= kd->subkey_count)
297 return WERR_NO_MORE_ITEMS;
299 el = ldb_msg_find_element(kd->subkeys[idx], "key");
300 SMB_ASSERT(el != NULL);
301 SMB_ASSERT(el->num_values != 0);
304 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
309 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
310 const char **name, uint32_t *data_type,
313 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
314 struct ldb_context *c = kd->ldb;
315 const char* attrs[] = { "data", "type", NULL };
316 struct ldb_result *res;
319 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_BASE, attrs, "%s", "");
321 if (ret != LDB_SUCCESS) {
322 DEBUG(0, ("Error getting default value for '%s': %s\n",
323 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
327 if (res->count == 0 || res->msgs[0]->num_elements == 0)
330 reg_ldb_unpack_value(mem_ctx,
331 res->msgs[0], name, data_type, data);
338 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
339 uint32_t idx, const char **name,
340 uint32_t *data_type, DATA_BLOB *data)
342 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
344 /* if default value exists, give it back */
345 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
353 /* Do the search if necessary */
354 if (kd->values == NULL) {
355 W_ERROR_NOT_OK_RETURN(cache_values(kd));
358 if (idx >= kd->value_count)
359 return WERR_NO_MORE_ITEMS;
361 reg_ldb_unpack_value(mem_ctx, kd->values[idx], name, data_type, data);
366 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
367 const char *name, uint32_t *data_type,
370 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
371 struct ldb_context *c = kd->ldb;
372 struct ldb_result *res;
376 if (strlen(name) == 0) {
378 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
381 query = talloc_asprintf(mem_ctx, "(value=%s)", name);
382 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "%s", query);
385 if (ret != LDB_SUCCESS) {
386 DEBUG(0, ("Error getting values for '%s': %s\n",
387 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
394 reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
402 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
403 const char *name, struct hive_key **key)
405 struct ldb_result *res;
406 struct ldb_dn *ldap_path;
408 struct ldb_key_data *newkd;
409 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
410 struct ldb_context *c = kd->ldb;
412 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
414 ret = ldb_search(c, mem_ctx, &res, ldap_path, LDB_SCOPE_BASE, NULL, "(key=*)");
416 if (ret != LDB_SUCCESS) {
417 DEBUG(3, ("Error opening key '%s': %s\n",
418 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
420 } else if (res->count == 0) {
421 DEBUG(3, ("Key '%s' not found\n",
422 ldb_dn_get_linearized(ldap_path)));
427 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
428 newkd->key.ops = ®_backend_ldb;
429 newkd->ldb = talloc_reference(newkd, kd->ldb);
430 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
432 *key = (struct hive_key *)newkd;
437 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
438 struct auth_session_info *session_info,
439 struct cli_credentials *credentials,
440 struct tevent_context *ev_ctx,
441 struct loadparm_context *lp_ctx,
444 struct ldb_key_data *kd;
445 struct ldb_context *wrap;
446 struct ldb_message *attrs_msg;
448 if (location == NULL)
449 return WERR_INVALID_PARAM;
451 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
452 location, session_info, credentials, 0);
455 DEBUG(1, (__FILE__": unable to connect\n"));
459 attrs_msg = ldb_msg_new(wrap);
460 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
461 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
462 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
463 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
464 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
466 ldb_add(wrap, attrs_msg);
468 ldb_set_debug_stderr(wrap);
470 kd = talloc_zero(parent_ctx, struct ldb_key_data);
471 kd->key.ops = ®_backend_ldb;
472 kd->ldb = talloc_reference(kd, wrap);
473 talloc_set_destructor (kd, reg_close_ldb_key);
474 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
476 *k = (struct hive_key *)kd;
481 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
482 const char *name, const char *classname,
483 struct security_descriptor *sd,
484 struct hive_key **newkey)
486 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
487 struct ldb_message *msg;
488 struct ldb_key_data *newkd;
491 msg = ldb_msg_new(mem_ctx);
493 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
495 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
496 if (classname != NULL)
497 ldb_msg_add_string(msg, "classname",
498 talloc_strdup(mem_ctx, classname));
500 ret = ldb_add(parentkd->ldb, msg);
501 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
502 return WERR_ALREADY_EXISTS;
505 if (ret != LDB_SUCCESS) {
506 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
510 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
512 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
513 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
514 newkd->key.ops = ®_backend_ldb;
515 newkd->dn = talloc_steal(newkd, msg->dn);
517 *newkey = (struct hive_key *)newkd;
520 talloc_free(parentkd->subkeys);
521 parentkd->subkeys = NULL;
526 static WERROR ldb_del_value (struct hive_key *key, const char *child)
529 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
531 struct ldb_message *msg;
532 struct ldb_dn *childdn;
534 if (strlen(child) == 0) {
536 mem_ctx = talloc_init("ldb_del_value");
538 msg = talloc_zero(mem_ctx, struct ldb_message);
539 msg->dn = ldb_dn_copy(msg, kd->dn);
540 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
541 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
543 ret = ldb_modify(kd->ldb, msg);
544 if (ret != LDB_SUCCESS) {
545 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
546 talloc_free(mem_ctx);
550 talloc_free(mem_ctx);
553 childdn = ldb_dn_copy(kd->ldb, kd->dn);
554 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
555 reg_ldb_escape(childdn, child)))
557 talloc_free(childdn);
561 ret = ldb_delete(kd->ldb, childdn);
563 talloc_free(childdn);
565 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
567 } else if (ret != LDB_SUCCESS) {
568 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
574 talloc_free(kd->values);
580 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
584 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
585 struct ldb_dn *ldap_path;
586 TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
587 struct ldb_context *c = parentkd->ldb;
588 struct ldb_result *res_keys;
589 struct ldb_result *res_vals;
593 /* Verify key exists by opening it */
594 werr = ldb_open_key(mem_ctx, key, name, &hk);
595 if (!W_ERROR_IS_OK(werr)) {
596 talloc_free(mem_ctx);
600 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
602 talloc_free(mem_ctx);
606 /* Search for subkeys */
607 ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
610 if (ret != LDB_SUCCESS) {
611 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
612 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
613 talloc_free(mem_ctx);
617 /* Search for values */
618 ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
621 if (ret != LDB_SUCCESS) {
622 DEBUG(0, ("Error getting values for '%s': %s\n",
623 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
624 talloc_free(mem_ctx);
628 /* Start an explicit transaction */
629 ret = ldb_transaction_start(c);
631 if (ret != LDB_SUCCESS) {
632 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
633 talloc_free(mem_ctx);
637 if (res_keys->count || res_vals->count)
639 /* Delete any subkeys */
640 for (i = 0; i < res_keys->count; i++)
642 werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
645 if (!W_ERROR_IS_OK(werr)) {
646 ret = ldb_transaction_cancel(c);
647 talloc_free(mem_ctx);
652 /* Delete any values */
653 for (i = 0; i < res_vals->count; i++)
655 werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
658 if (!W_ERROR_IS_OK(werr)) {
659 ret = ldb_transaction_cancel(c);
660 talloc_free(mem_ctx);
666 /* Delete the key itself */
667 ret = ldb_delete(c, ldap_path);
669 if (ret != LDB_SUCCESS)
671 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
672 ret = ldb_transaction_cancel(c);
673 talloc_free(mem_ctx);
677 /* Commit the transaction */
678 ret = ldb_transaction_commit(c);
680 if (ret != LDB_SUCCESS)
682 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
683 ret = ldb_transaction_cancel(c);
684 talloc_free(mem_ctx);
688 talloc_free(mem_ctx);
691 talloc_free(parentkd->subkeys);
692 parentkd->subkeys = NULL;
697 static WERROR ldb_set_value(struct hive_key *parent,
698 const char *name, uint32_t type,
699 const DATA_BLOB data)
701 struct ldb_message *msg;
702 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
705 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
707 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
708 msg->dn = ldb_dn_copy(msg, kd->dn);
710 if (name[0] != '\0') {
711 /* For a default value, we add/overwrite the attributes to/of the hive.
712 For a normal value, we create a new child. */
713 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
714 reg_ldb_escape(mem_ctx, name)))
716 talloc_free(mem_ctx);
721 /* Try first a "modify" and if this doesn't work do try an "add" */
722 for (i = 0; i < msg->num_elements; i++) {
723 if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE) {
724 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
727 ret = ldb_modify(kd->ldb, msg);
728 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
730 while (i < msg->num_elements) {
731 if (msg->elements[i].flags == LDB_FLAG_MOD_DELETE) {
732 ldb_msg_remove_element(msg, &msg->elements[i]);
737 ret = ldb_add(kd->ldb, msg);
739 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
740 /* ignore this -> the value didn't exist and also now doesn't */
744 if (ret != LDB_SUCCESS) {
745 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
746 talloc_free(mem_ctx);
751 talloc_free(kd->values);
754 talloc_free(mem_ctx);
758 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
759 const struct hive_key *key,
760 const char **classname,
761 uint32_t *num_subkeys,
762 uint32_t *num_values,
763 NTTIME *last_change_time,
764 uint32_t *max_subkeynamelen,
765 uint32_t *max_valnamelen,
766 uint32_t *max_valbufsize)
768 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
771 if (classname != NULL)
773 if (num_subkeys != NULL)
775 if (num_values != NULL)
777 if (last_change_time != NULL)
778 *last_change_time = 0;
779 if (max_subkeynamelen != NULL)
780 *max_subkeynamelen = 0;
781 if (max_valnamelen != NULL)
783 if (max_valbufsize != NULL)
786 if (kd->subkeys == NULL) {
787 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
790 if (kd->values == NULL) {
791 W_ERROR_NOT_OK_RETURN(cache_values(kd));
794 if (num_subkeys != NULL) {
795 *num_subkeys = kd->subkey_count;
797 if (num_values != NULL) {
798 *num_values = kd->value_count;
802 if (max_subkeynamelen != NULL) {
804 struct ldb_message_element *el;
806 *max_subkeynamelen = 0;
808 for (i = 0; i < kd->subkey_count; i++) {
809 el = ldb_msg_find_element(kd->subkeys[i], "key");
810 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
814 if (max_valnamelen != NULL || max_valbufsize != NULL) {
816 struct ldb_message_element *el;
817 W_ERROR_NOT_OK_RETURN(cache_values(kd));
819 if (max_valbufsize != NULL)
822 if (max_valnamelen != NULL)
825 for (i = 0; i < kd->value_count; i++) {
826 if (max_valnamelen != NULL) {
827 el = ldb_msg_find_element(kd->values[i], "value");
828 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
831 if (max_valbufsize != NULL) {
834 reg_ldb_unpack_value(mem_ctx,
837 *max_valbufsize = MAX(*max_valbufsize, data.length);
838 talloc_free(data.data);
846 static struct hive_operations reg_backend_ldb = {
848 .add_key = ldb_add_key,
849 .del_key = ldb_del_key,
850 .get_key_by_name = ldb_open_key,
851 .enum_value = ldb_get_value_by_id,
852 .enum_key = ldb_get_subkey_by_id,
853 .set_value = ldb_set_value,
854 .get_value_by_name = ldb_get_value,
855 .delete_value = ldb_del_value,
856 .get_key_info = ldb_get_key_info,