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 = talloc_size(mem_ctx, sizeof(uint32_t) + 1);
77 if (data->data != NULL) {
78 SIVAL(data->data, 0, tmp);
80 data->length = sizeof(uint32_t);
90 data->data = talloc_memdup(mem_ctx, val->data,
92 data->length = val->length;
101 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
104 uint32_t type, DATA_BLOB data)
106 struct ldb_message *msg;
107 char *name_dup, *type_str;
110 msg = talloc_zero(mem_ctx, struct ldb_message);
115 name_dup = talloc_strdup(msg, name);
116 if (name_dup == NULL) {
121 ret = ldb_msg_add_string(msg, "value", name_dup);
122 if (ret != LDB_SUCCESS) {
130 if ((data.length > 0) && (data.data != NULL)
131 && (data.data[0] != '\0')) {
135 val = talloc_zero(msg, struct ldb_val);
141 ret2 = convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8,
142 (void *)data.data, data.length,
143 (void **)&val->data, &val->length,
145 ret = ldb_msg_add_value(msg, "data", val, NULL);
147 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
152 if ((data.length > 0) && (data.data != NULL)) {
155 conv_str = talloc_asprintf(msg, "0x%x", IVAL(data.data, 0));
156 if (conv_str == NULL) {
161 ret = ldb_msg_add_string(msg, "data", conv_str);
163 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
169 if ((data.length > 0) && (data.data != NULL)
170 && (data.data[0] != '\0')) {
171 ret = ldb_msg_add_value(msg, "data", &data, NULL);
173 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
178 if (ret != LDB_SUCCESS) {
183 type_str = talloc_asprintf(mem_ctx, "%u", type);
184 if (type_str == NULL) {
189 ret = ldb_msg_add_string(msg, "type", type_str);
190 if (ret != LDB_SUCCESS) {
198 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
202 val.data = discard_const_p(uint8_t, value);
203 val.length = strlen(value);
205 return ldb_dn_escape_value(mem_ctx, val);
208 static int reg_close_ldb_key(struct ldb_key_data *key)
210 if (key->subkeys != NULL) {
211 talloc_free(key->subkeys);
215 if (key->values != NULL) {
216 talloc_free(key->values);
222 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
223 const struct hive_key *from,
224 const char *path, const char *add)
226 TALLOC_CTX *local_ctx;
228 char *mypath = talloc_strdup(mem_ctx, path);
230 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
231 struct ldb_context *ldb = kd->ldb;
233 local_ctx = talloc_new(mem_ctx);
235 ret = ldb_dn_new(mem_ctx, ldb, add);
236 if (!ldb_dn_validate(ret)) {
238 talloc_free(local_ctx);
245 begin = strrchr(mypath, '\\');
247 if (begin) keyname = begin + 1;
248 else keyname = mypath;
250 if (keyname[0] != '\0') {
251 if (!ldb_dn_add_base_fmt(ret, "key=%s",
252 reg_ldb_escape(local_ctx,
255 talloc_free(local_ctx);
267 ldb_dn_add_base(ret, kd->dn);
269 talloc_free(local_ctx);
274 static WERROR cache_subkeys(struct ldb_key_data *kd)
276 struct ldb_context *c = kd->ldb;
277 struct ldb_result *res;
280 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "(key=*)");
282 if (ret != LDB_SUCCESS) {
283 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
284 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
288 kd->subkey_count = res->count;
289 kd->subkeys = talloc_steal(kd, res->msgs);
295 static WERROR cache_values(struct ldb_key_data *kd)
297 struct ldb_context *c = kd->ldb;
298 struct ldb_result *res;
301 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
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)));
310 kd->value_count = res->count;
311 kd->values = talloc_steal(kd, res->msgs);
318 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
319 const struct hive_key *k, uint32_t idx,
321 const char **classname,
322 NTTIME *last_mod_time)
324 struct ldb_message_element *el;
325 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
330 if (classname != NULL)
331 *classname = NULL; /* TODO: Store properly */
332 if (last_mod_time != NULL)
333 *last_mod_time = 0; /* TODO: we need to add this to the
334 ldb backend properly */
336 /* Do a search if necessary */
337 if (kd->subkeys == NULL) {
338 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
341 if (idx >= kd->subkey_count)
342 return WERR_NO_MORE_ITEMS;
344 el = ldb_msg_find_element(kd->subkeys[idx], "key");
345 SMB_ASSERT(el != NULL);
346 SMB_ASSERT(el->num_values != 0);
349 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
354 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
355 const char **name, uint32_t *data_type,
358 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
359 struct ldb_context *c = kd->ldb;
360 const char* attrs[] = { "data", "type", NULL };
361 struct ldb_result *res;
364 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_BASE, attrs, "(key=*)");
366 if (ret != LDB_SUCCESS) {
367 DEBUG(0, ("Error getting default value for '%s': %s\n",
368 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
372 if (res->count == 0 || res->msgs[0]->num_elements == 0)
375 reg_ldb_unpack_value(mem_ctx,
376 res->msgs[0], name, data_type, data);
383 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
384 uint32_t idx, const char **name,
385 uint32_t *data_type, DATA_BLOB *data)
387 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
389 /* if default value exists, give it back */
390 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
398 /* Do the search if necessary */
399 if (kd->values == NULL) {
400 W_ERROR_NOT_OK_RETURN(cache_values(kd));
403 if (idx >= kd->value_count)
404 return WERR_NO_MORE_ITEMS;
406 reg_ldb_unpack_value(mem_ctx, kd->values[idx], name, data_type, data);
411 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
412 const char *name, uint32_t *data_type,
415 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
416 struct ldb_context *c = kd->ldb;
417 struct ldb_result *res;
421 return WERR_INVALID_PARAM;
424 if (name[0] == '\0') {
426 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
429 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_ONELEVEL,
430 NULL, "(value=%s)", name);
432 if (ret != LDB_SUCCESS) {
433 DEBUG(0, ("Error getting values for '%s': %s\n",
434 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
441 reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
449 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
450 const char *name, struct hive_key **key)
452 struct ldb_result *res;
453 struct ldb_dn *ldap_path;
455 struct ldb_key_data *newkd;
456 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
457 struct ldb_context *c = kd->ldb;
459 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
460 W_ERROR_HAVE_NO_MEMORY(ldap_path);
462 ret = ldb_search(c, mem_ctx, &res, ldap_path, LDB_SCOPE_BASE, NULL, "(key=*)");
464 if (ret != LDB_SUCCESS) {
465 DEBUG(3, ("Error opening key '%s': %s\n",
466 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
468 } else if (res->count == 0) {
469 DEBUG(3, ("Key '%s' not found\n",
470 ldb_dn_get_linearized(ldap_path)));
475 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
476 newkd->key.ops = ®_backend_ldb;
477 newkd->ldb = talloc_reference(newkd, kd->ldb);
478 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
480 *key = (struct hive_key *)newkd;
485 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
486 struct auth_session_info *session_info,
487 struct cli_credentials *credentials,
488 struct tevent_context *ev_ctx,
489 struct loadparm_context *lp_ctx,
492 struct ldb_key_data *kd;
493 struct ldb_context *wrap;
494 struct ldb_message *attrs_msg;
496 if (location == NULL)
497 return WERR_INVALID_PARAM;
499 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
500 location, session_info, credentials, 0);
503 DEBUG(1, (__FILE__": unable to connect\n"));
507 attrs_msg = ldb_msg_new(wrap);
508 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
509 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
510 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
511 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
512 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
514 ldb_add(wrap, attrs_msg);
516 ldb_set_debug_stderr(wrap);
518 kd = talloc_zero(parent_ctx, struct ldb_key_data);
519 kd->key.ops = ®_backend_ldb;
520 kd->ldb = talloc_reference(kd, wrap);
521 talloc_set_destructor (kd, reg_close_ldb_key);
522 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
524 *k = (struct hive_key *)kd;
529 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
530 const char *name, const char *classname,
531 struct security_descriptor *sd,
532 struct hive_key **newkey)
534 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
535 struct ldb_message *msg;
536 struct ldb_key_data *newkd;
539 msg = ldb_msg_new(mem_ctx);
540 W_ERROR_HAVE_NO_MEMORY(msg);
542 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
543 W_ERROR_HAVE_NO_MEMORY(msg->dn);
545 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
546 if (classname != NULL)
547 ldb_msg_add_string(msg, "classname",
548 talloc_strdup(mem_ctx, classname));
550 ret = ldb_add(parentkd->ldb, msg);
551 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
552 return WERR_ALREADY_EXISTS;
555 if (ret != LDB_SUCCESS) {
556 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
560 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
562 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
563 W_ERROR_HAVE_NO_MEMORY(newkd);
564 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
565 newkd->key.ops = ®_backend_ldb;
566 newkd->dn = talloc_steal(newkd, msg->dn);
568 *newkey = (struct hive_key *)newkd;
571 talloc_free(parentkd->subkeys);
572 parentkd->subkeys = NULL;
577 static WERROR ldb_del_value (struct hive_key *key, const char *child)
580 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
582 struct ldb_message *msg;
583 struct ldb_dn *childdn;
585 if ((child == NULL) || (child[0] == '\0')) {
587 mem_ctx = talloc_init("ldb_del_value");
589 msg = talloc_zero(mem_ctx, struct ldb_message);
590 W_ERROR_HAVE_NO_MEMORY(msg);
591 msg->dn = ldb_dn_copy(msg, kd->dn);
592 W_ERROR_HAVE_NO_MEMORY(msg->dn);
593 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
594 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
596 ret = ldb_modify(kd->ldb, msg);
597 if (ret != LDB_SUCCESS) {
598 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
599 talloc_free(mem_ctx);
603 talloc_free(mem_ctx);
606 childdn = ldb_dn_copy(kd->ldb, kd->dn);
607 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
608 reg_ldb_escape(childdn, child)))
610 talloc_free(childdn);
614 ret = ldb_delete(kd->ldb, childdn);
616 talloc_free(childdn);
618 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
620 } else if (ret != LDB_SUCCESS) {
621 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
627 talloc_free(kd->values);
633 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
637 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
638 struct ldb_dn *ldap_path;
639 TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
640 struct ldb_context *c = parentkd->ldb;
641 struct ldb_result *res_keys;
642 struct ldb_result *res_vals;
646 /* Verify key exists by opening it */
647 werr = ldb_open_key(mem_ctx, key, name, &hk);
648 if (!W_ERROR_IS_OK(werr)) {
649 talloc_free(mem_ctx);
653 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
654 W_ERROR_HAVE_NO_MEMORY(ldap_path);
656 /* Search for subkeys */
657 ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
660 if (ret != LDB_SUCCESS) {
661 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
662 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
663 talloc_free(mem_ctx);
667 /* Search for values */
668 ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
671 if (ret != LDB_SUCCESS) {
672 DEBUG(0, ("Error getting values for '%s': %s\n",
673 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
674 talloc_free(mem_ctx);
678 /* Start an explicit transaction */
679 ret = ldb_transaction_start(c);
681 if (ret != LDB_SUCCESS) {
682 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
683 talloc_free(mem_ctx);
687 if (res_keys->count || res_vals->count)
689 /* Delete any subkeys */
690 for (i = 0; i < res_keys->count; i++)
692 werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
695 if (!W_ERROR_IS_OK(werr)) {
696 ret = ldb_transaction_cancel(c);
697 talloc_free(mem_ctx);
702 /* Delete any values */
703 for (i = 0; i < res_vals->count; i++)
705 werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
708 if (!W_ERROR_IS_OK(werr)) {
709 ret = ldb_transaction_cancel(c);
710 talloc_free(mem_ctx);
716 /* Delete the key itself */
717 ret = ldb_delete(c, ldap_path);
719 if (ret != LDB_SUCCESS)
721 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
722 ret = ldb_transaction_cancel(c);
723 talloc_free(mem_ctx);
727 /* Commit the transaction */
728 ret = ldb_transaction_commit(c);
730 if (ret != LDB_SUCCESS)
732 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
733 ret = ldb_transaction_cancel(c);
734 talloc_free(mem_ctx);
738 talloc_free(mem_ctx);
741 talloc_free(parentkd->subkeys);
742 parentkd->subkeys = NULL;
747 static WERROR ldb_set_value(struct hive_key *parent,
748 const char *name, uint32_t type,
749 const DATA_BLOB data)
751 struct ldb_message *msg;
752 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
755 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
757 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
758 W_ERROR_HAVE_NO_MEMORY(msg);
760 msg->dn = ldb_dn_copy(msg, kd->dn);
761 W_ERROR_HAVE_NO_MEMORY(msg->dn);
763 if ((name != NULL) && (name[0] != '\0')) {
764 /* For a default value, we add/overwrite the attributes to/of the hive.
765 For a normal value, we create a new child. */
766 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
767 reg_ldb_escape(mem_ctx, name)))
769 talloc_free(mem_ctx);
774 /* Try first a "modify" and if this doesn't work do try an "add" */
775 for (i = 0; i < msg->num_elements; i++) {
776 if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE) {
777 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
780 ret = ldb_modify(kd->ldb, msg);
781 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
783 while (i < msg->num_elements) {
784 if (msg->elements[i].flags == LDB_FLAG_MOD_DELETE) {
785 ldb_msg_remove_element(msg, &msg->elements[i]);
790 ret = ldb_add(kd->ldb, msg);
792 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
793 /* ignore this -> the value didn't exist and also now doesn't */
797 if (ret != LDB_SUCCESS) {
798 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
799 talloc_free(mem_ctx);
804 talloc_free(kd->values);
807 talloc_free(mem_ctx);
811 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
812 const struct hive_key *key,
813 const char **classname,
814 uint32_t *num_subkeys,
815 uint32_t *num_values,
816 NTTIME *last_change_time,
817 uint32_t *max_subkeynamelen,
818 uint32_t *max_valnamelen,
819 uint32_t *max_valbufsize)
821 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
824 if (classname != NULL)
826 if (num_subkeys != NULL)
828 if (num_values != NULL)
830 if (last_change_time != NULL)
831 *last_change_time = 0;
832 if (max_subkeynamelen != NULL)
833 *max_subkeynamelen = 0;
834 if (max_valnamelen != NULL)
836 if (max_valbufsize != NULL)
839 if (kd->subkeys == NULL) {
840 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
843 if (kd->values == NULL) {
844 W_ERROR_NOT_OK_RETURN(cache_values(kd));
847 if (num_subkeys != NULL) {
848 *num_subkeys = kd->subkey_count;
850 if (num_values != NULL) {
851 *num_values = kd->value_count;
855 if (max_subkeynamelen != NULL) {
857 struct ldb_message_element *el;
859 *max_subkeynamelen = 0;
861 for (i = 0; i < kd->subkey_count; i++) {
862 el = ldb_msg_find_element(kd->subkeys[i], "key");
863 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
867 if (max_valnamelen != NULL || max_valbufsize != NULL) {
869 struct ldb_message_element *el;
870 W_ERROR_NOT_OK_RETURN(cache_values(kd));
872 if (max_valbufsize != NULL)
875 if (max_valnamelen != NULL)
878 for (i = 0; i < kd->value_count; i++) {
879 if (max_valnamelen != NULL) {
880 el = ldb_msg_find_element(kd->values[i], "value");
881 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
884 if (max_valbufsize != NULL) {
887 reg_ldb_unpack_value(mem_ctx,
890 *max_valbufsize = MAX(*max_valbufsize, data.length);
891 talloc_free(data.data);
899 static struct hive_operations reg_backend_ldb = {
901 .add_key = ldb_add_key,
902 .del_key = ldb_del_key,
903 .get_key_by_name = ldb_open_key,
904 .enum_value = ldb_get_value_by_id,
905 .enum_key = ldb_get_subkey_by_id,
906 .set_value = ldb_set_value,
907 .get_value_by_name = ldb_get_value,
908 .delete_value = ldb_del_value,
909 .get_key_info = ldb_get_key_info,