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)
101 struct ldb_message *msg;
102 char *name_dup, *type_str;
105 msg = talloc_zero(mem_ctx, struct ldb_message);
110 name_dup = talloc_strdup(msg, name);
111 if (name_dup == NULL) {
116 ret = ldb_msg_add_string(msg, "value", name_dup);
117 if (ret != LDB_SUCCESS) {
125 if ((data.length > 0) && (data.data != NULL)
126 && (data.data[0] != '\0')) {
130 val = talloc_zero(msg, struct ldb_val);
136 ret2 = convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8,
137 (void *)data.data, data.length,
138 (void **)&val->data, &val->length,
140 ret = ldb_msg_add_value(msg, "data", val, NULL);
142 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
147 if ((data.length > 0) && (data.data != NULL)) {
150 conv_str = talloc_asprintf(msg, "0x%x", IVAL(data.data, 0));
151 if (conv_str == NULL) {
156 ret = ldb_msg_add_string(msg, "data", conv_str);
158 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
164 if ((data.length > 0) && (data.data != NULL)
165 && (data.data[0] != '\0')) {
166 ret = ldb_msg_add_value(msg, "data", &data, NULL);
168 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
173 if (ret != LDB_SUCCESS) {
178 type_str = talloc_asprintf(mem_ctx, "%u", type);
179 if (type_str == NULL) {
184 ret = ldb_msg_add_string(msg, "type", type_str);
185 if (ret != LDB_SUCCESS) {
193 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
197 val.data = discard_const_p(uint8_t, value);
198 val.length = strlen(value);
200 return ldb_dn_escape_value(mem_ctx, val);
203 static int reg_close_ldb_key(struct ldb_key_data *key)
205 if (key->subkeys != NULL) {
206 talloc_free(key->subkeys);
210 if (key->values != NULL) {
211 talloc_free(key->values);
217 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
218 const struct hive_key *from,
219 const char *path, const char *add)
221 TALLOC_CTX *local_ctx;
223 char *mypath = talloc_strdup(mem_ctx, path);
225 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
226 struct ldb_context *ldb = kd->ldb;
228 local_ctx = talloc_new(mem_ctx);
230 ret = ldb_dn_new(mem_ctx, ldb, add);
231 if (!ldb_dn_validate(ret)) {
233 talloc_free(local_ctx);
240 begin = strrchr(mypath, '\\');
242 if (begin) keyname = begin + 1;
243 else keyname = mypath;
245 if (keyname[0] != '\0') {
246 if (!ldb_dn_add_base_fmt(ret, "key=%s",
247 reg_ldb_escape(local_ctx,
250 talloc_free(local_ctx);
262 ldb_dn_add_base(ret, kd->dn);
264 talloc_free(local_ctx);
269 static WERROR cache_subkeys(struct ldb_key_data *kd)
271 struct ldb_context *c = kd->ldb;
272 struct ldb_result *res;
275 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "(key=*)");
277 if (ret != LDB_SUCCESS) {
278 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
279 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
283 kd->subkey_count = res->count;
284 kd->subkeys = talloc_steal(kd, res->msgs);
290 static WERROR cache_values(struct ldb_key_data *kd)
292 struct ldb_context *c = kd->ldb;
293 struct ldb_result *res;
296 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
299 if (ret != LDB_SUCCESS) {
300 DEBUG(0, ("Error getting values for '%s': %s\n",
301 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
305 kd->value_count = res->count;
306 kd->values = talloc_steal(kd, res->msgs);
313 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
314 const struct hive_key *k, uint32_t idx,
316 const char **classname,
317 NTTIME *last_mod_time)
319 struct ldb_message_element *el;
320 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
325 if (classname != NULL)
326 *classname = NULL; /* TODO: Store properly */
327 if (last_mod_time != NULL)
328 *last_mod_time = 0; /* TODO: we need to add this to the
329 ldb backend properly */
331 /* Do a search if necessary */
332 if (kd->subkeys == NULL) {
333 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
336 if (idx >= kd->subkey_count)
337 return WERR_NO_MORE_ITEMS;
339 el = ldb_msg_find_element(kd->subkeys[idx], "key");
340 SMB_ASSERT(el != NULL);
341 SMB_ASSERT(el->num_values != 0);
344 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
349 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
350 const char **name, uint32_t *data_type,
353 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
354 struct ldb_context *c = kd->ldb;
355 const char* attrs[] = { "data", "type", NULL };
356 struct ldb_result *res;
359 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_BASE, attrs, "%s", "");
361 if (ret != LDB_SUCCESS) {
362 DEBUG(0, ("Error getting default value for '%s': %s\n",
363 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
367 if (res->count == 0 || res->msgs[0]->num_elements == 0)
370 reg_ldb_unpack_value(mem_ctx,
371 res->msgs[0], name, data_type, data);
378 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
379 uint32_t idx, const char **name,
380 uint32_t *data_type, DATA_BLOB *data)
382 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
384 /* if default value exists, give it back */
385 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
393 /* Do the search if necessary */
394 if (kd->values == NULL) {
395 W_ERROR_NOT_OK_RETURN(cache_values(kd));
398 if (idx >= kd->value_count)
399 return WERR_NO_MORE_ITEMS;
401 reg_ldb_unpack_value(mem_ctx, kd->values[idx], name, data_type, data);
406 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
407 const char *name, uint32_t *data_type,
410 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
411 struct ldb_context *c = kd->ldb;
412 struct ldb_result *res;
416 return WERR_INVALID_PARAM;
419 if (name[0] == '\0') {
421 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
424 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_ONELEVEL,
425 NULL, "(value=%s)", name);
427 if (ret != LDB_SUCCESS) {
428 DEBUG(0, ("Error getting values for '%s': %s\n",
429 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
436 reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
444 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
445 const char *name, struct hive_key **key)
447 struct ldb_result *res;
448 struct ldb_dn *ldap_path;
450 struct ldb_key_data *newkd;
451 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
452 struct ldb_context *c = kd->ldb;
454 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
455 W_ERROR_HAVE_NO_MEMORY(ldap_path);
457 ret = ldb_search(c, mem_ctx, &res, ldap_path, LDB_SCOPE_BASE, NULL, "(key=*)");
459 if (ret != LDB_SUCCESS) {
460 DEBUG(3, ("Error opening key '%s': %s\n",
461 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
463 } else if (res->count == 0) {
464 DEBUG(3, ("Key '%s' not found\n",
465 ldb_dn_get_linearized(ldap_path)));
470 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
471 newkd->key.ops = ®_backend_ldb;
472 newkd->ldb = talloc_reference(newkd, kd->ldb);
473 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
475 *key = (struct hive_key *)newkd;
480 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
481 struct auth_session_info *session_info,
482 struct cli_credentials *credentials,
483 struct tevent_context *ev_ctx,
484 struct loadparm_context *lp_ctx,
487 struct ldb_key_data *kd;
488 struct ldb_context *wrap;
489 struct ldb_message *attrs_msg;
491 if (location == NULL)
492 return WERR_INVALID_PARAM;
494 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
495 location, session_info, credentials, 0);
498 DEBUG(1, (__FILE__": unable to connect\n"));
502 attrs_msg = ldb_msg_new(wrap);
503 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
504 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
505 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
506 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
507 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
509 ldb_add(wrap, attrs_msg);
511 ldb_set_debug_stderr(wrap);
513 kd = talloc_zero(parent_ctx, struct ldb_key_data);
514 kd->key.ops = ®_backend_ldb;
515 kd->ldb = talloc_reference(kd, wrap);
516 talloc_set_destructor (kd, reg_close_ldb_key);
517 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
519 *k = (struct hive_key *)kd;
524 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
525 const char *name, const char *classname,
526 struct security_descriptor *sd,
527 struct hive_key **newkey)
529 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
530 struct ldb_message *msg;
531 struct ldb_key_data *newkd;
534 msg = ldb_msg_new(mem_ctx);
535 W_ERROR_HAVE_NO_MEMORY(msg);
537 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
538 W_ERROR_HAVE_NO_MEMORY(msg->dn);
540 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
541 if (classname != NULL)
542 ldb_msg_add_string(msg, "classname",
543 talloc_strdup(mem_ctx, classname));
545 ret = ldb_add(parentkd->ldb, msg);
546 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
547 return WERR_ALREADY_EXISTS;
550 if (ret != LDB_SUCCESS) {
551 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
555 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
557 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
558 W_ERROR_HAVE_NO_MEMORY(newkd);
559 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
560 newkd->key.ops = ®_backend_ldb;
561 newkd->dn = talloc_steal(newkd, msg->dn);
563 *newkey = (struct hive_key *)newkd;
566 talloc_free(parentkd->subkeys);
567 parentkd->subkeys = NULL;
572 static WERROR ldb_del_value (struct hive_key *key, const char *child)
575 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
577 struct ldb_message *msg;
578 struct ldb_dn *childdn;
580 if ((child == NULL) || (child[0] == '\0')) {
582 mem_ctx = talloc_init("ldb_del_value");
584 msg = talloc_zero(mem_ctx, struct ldb_message);
585 W_ERROR_HAVE_NO_MEMORY(msg);
586 msg->dn = ldb_dn_copy(msg, kd->dn);
587 W_ERROR_HAVE_NO_MEMORY(msg->dn);
588 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
589 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
591 ret = ldb_modify(kd->ldb, msg);
592 if (ret != LDB_SUCCESS) {
593 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
594 talloc_free(mem_ctx);
598 talloc_free(mem_ctx);
601 childdn = ldb_dn_copy(kd->ldb, kd->dn);
602 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
603 reg_ldb_escape(childdn, child)))
605 talloc_free(childdn);
609 ret = ldb_delete(kd->ldb, childdn);
611 talloc_free(childdn);
613 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
615 } else if (ret != LDB_SUCCESS) {
616 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
622 talloc_free(kd->values);
628 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
632 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
633 struct ldb_dn *ldap_path;
634 TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
635 struct ldb_context *c = parentkd->ldb;
636 struct ldb_result *res_keys;
637 struct ldb_result *res_vals;
641 /* Verify key exists by opening it */
642 werr = ldb_open_key(mem_ctx, key, name, &hk);
643 if (!W_ERROR_IS_OK(werr)) {
644 talloc_free(mem_ctx);
648 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
649 W_ERROR_HAVE_NO_MEMORY(ldap_path);
651 /* Search for subkeys */
652 ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
655 if (ret != LDB_SUCCESS) {
656 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
657 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
658 talloc_free(mem_ctx);
662 /* Search for values */
663 ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
666 if (ret != LDB_SUCCESS) {
667 DEBUG(0, ("Error getting values for '%s': %s\n",
668 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
669 talloc_free(mem_ctx);
673 /* Start an explicit transaction */
674 ret = ldb_transaction_start(c);
676 if (ret != LDB_SUCCESS) {
677 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
678 talloc_free(mem_ctx);
682 if (res_keys->count || res_vals->count)
684 /* Delete any subkeys */
685 for (i = 0; i < res_keys->count; i++)
687 werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
690 if (!W_ERROR_IS_OK(werr)) {
691 ret = ldb_transaction_cancel(c);
692 talloc_free(mem_ctx);
697 /* Delete any values */
698 for (i = 0; i < res_vals->count; i++)
700 werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
703 if (!W_ERROR_IS_OK(werr)) {
704 ret = ldb_transaction_cancel(c);
705 talloc_free(mem_ctx);
711 /* Delete the key itself */
712 ret = ldb_delete(c, ldap_path);
714 if (ret != LDB_SUCCESS)
716 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
717 ret = ldb_transaction_cancel(c);
718 talloc_free(mem_ctx);
722 /* Commit the transaction */
723 ret = ldb_transaction_commit(c);
725 if (ret != LDB_SUCCESS)
727 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
728 ret = ldb_transaction_cancel(c);
729 talloc_free(mem_ctx);
733 talloc_free(mem_ctx);
736 talloc_free(parentkd->subkeys);
737 parentkd->subkeys = NULL;
742 static WERROR ldb_set_value(struct hive_key *parent,
743 const char *name, uint32_t type,
744 const DATA_BLOB data)
746 struct ldb_message *msg;
747 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
750 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
752 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
753 W_ERROR_HAVE_NO_MEMORY(msg);
755 msg->dn = ldb_dn_copy(msg, kd->dn);
756 W_ERROR_HAVE_NO_MEMORY(msg->dn);
758 if ((name != NULL) && (name[0] != '\0')) {
759 /* For a default value, we add/overwrite the attributes to/of the hive.
760 For a normal value, we create a new child. */
761 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
762 reg_ldb_escape(mem_ctx, name)))
764 talloc_free(mem_ctx);
769 /* Try first a "modify" and if this doesn't work do try an "add" */
770 for (i = 0; i < msg->num_elements; i++) {
771 if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE) {
772 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
775 ret = ldb_modify(kd->ldb, msg);
776 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
778 while (i < msg->num_elements) {
779 if (msg->elements[i].flags == LDB_FLAG_MOD_DELETE) {
780 ldb_msg_remove_element(msg, &msg->elements[i]);
785 ret = ldb_add(kd->ldb, msg);
787 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
788 /* ignore this -> the value didn't exist and also now doesn't */
792 if (ret != LDB_SUCCESS) {
793 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
794 talloc_free(mem_ctx);
799 talloc_free(kd->values);
802 talloc_free(mem_ctx);
806 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
807 const struct hive_key *key,
808 const char **classname,
809 uint32_t *num_subkeys,
810 uint32_t *num_values,
811 NTTIME *last_change_time,
812 uint32_t *max_subkeynamelen,
813 uint32_t *max_valnamelen,
814 uint32_t *max_valbufsize)
816 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
819 if (classname != NULL)
821 if (num_subkeys != NULL)
823 if (num_values != NULL)
825 if (last_change_time != NULL)
826 *last_change_time = 0;
827 if (max_subkeynamelen != NULL)
828 *max_subkeynamelen = 0;
829 if (max_valnamelen != NULL)
831 if (max_valbufsize != NULL)
834 if (kd->subkeys == NULL) {
835 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
838 if (kd->values == NULL) {
839 W_ERROR_NOT_OK_RETURN(cache_values(kd));
842 if (num_subkeys != NULL) {
843 *num_subkeys = kd->subkey_count;
845 if (num_values != NULL) {
846 *num_values = kd->value_count;
850 if (max_subkeynamelen != NULL) {
852 struct ldb_message_element *el;
854 *max_subkeynamelen = 0;
856 for (i = 0; i < kd->subkey_count; i++) {
857 el = ldb_msg_find_element(kd->subkeys[i], "key");
858 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
862 if (max_valnamelen != NULL || max_valbufsize != NULL) {
864 struct ldb_message_element *el;
865 W_ERROR_NOT_OK_RETURN(cache_values(kd));
867 if (max_valbufsize != NULL)
870 if (max_valnamelen != NULL)
873 for (i = 0; i < kd->value_count; i++) {
874 if (max_valnamelen != NULL) {
875 el = ldb_msg_find_element(kd->values[i], "value");
876 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
879 if (max_valbufsize != NULL) {
882 reg_ldb_unpack_value(mem_ctx,
885 *max_valbufsize = MAX(*max_valbufsize, data.length);
886 talloc_free(data.data);
894 static struct hive_operations reg_backend_ldb = {
896 .add_key = ldb_add_key,
897 .del_key = ldb_del_key,
898 .get_key_by_name = ldb_open_key,
899 .enum_value = ldb_get_value_by_id,
900 .enum_key = ldb_get_subkey_by_id,
901 .set_value = ldb_set_value,
902 .get_value_by_name = ldb_get_value,
903 .delete_value = ldb_del_value,
904 .get_key_info = ldb_get_key_info,