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 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",
53 value_type = ldb_msg_find_attr_as_uint(msg, "type", 0);
56 val = ldb_msg_find_ldb_val(msg, "data");
63 data->length = convert_string_talloc(mem_ctx, CH_UTF8, CH_UTF16,
64 val->data, val->length,
65 (void **)&data->data);
74 *data = data_blob_talloc(mem_ctx, val->data, val->length);
82 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
83 *data = data_blob_talloc(mem_ctx, &tmp, 4);
88 *data = data_blob_talloc(mem_ctx, val->data, val->length);
93 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
96 uint32_t type, DATA_BLOB data)
99 struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
102 ldb_msg_add_string(msg, "value", talloc_strdup(mem_ctx, name));
107 if (data.data[0] != '\0') {
108 val.length = convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8,
112 ldb_msg_add_value(msg, "data", &val, NULL);
114 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
120 ldb_msg_add_value(msg, "data", &data, NULL);
122 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
126 ldb_msg_add_string(msg, "data",
127 talloc_asprintf(mem_ctx, "0x%x",
128 IVAL(data.data, 0)));
131 ldb_msg_add_value(msg, "data", &data, NULL);
135 type_s = talloc_asprintf(mem_ctx, "%u", type);
136 ldb_msg_add_string(msg, "type", type_s);
141 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
145 val.data = discard_const_p(uint8_t, value);
146 val.length = strlen(value);
148 return ldb_dn_escape_value(mem_ctx, val);
151 static int reg_close_ldb_key(struct ldb_key_data *key)
153 if (key->subkeys != NULL) {
154 talloc_free(key->subkeys);
158 if (key->values != NULL) {
159 talloc_free(key->values);
165 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
166 const struct hive_key *from,
167 const char *path, const char *add)
169 TALLOC_CTX *local_ctx;
171 char *mypath = talloc_strdup(mem_ctx, path);
173 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
174 struct ldb_context *ldb = kd->ldb;
176 local_ctx = talloc_new(mem_ctx);
179 ret = ldb_dn_new(mem_ctx, ldb, add);
181 ret = ldb_dn_new(mem_ctx, ldb, NULL);
183 if (!ldb_dn_validate(ret)) {
185 talloc_free(local_ctx);
192 begin = strrchr(mypath, '\\');
194 if (begin) keyname = begin + 1;
195 else keyname = mypath;
197 if(strlen(keyname)) {
198 if (!ldb_dn_add_base_fmt(ret, "key=%s",
199 reg_ldb_escape(local_ctx,
202 talloc_free(local_ctx);
214 ldb_dn_add_base(ret, kd->dn);
216 talloc_free(local_ctx);
221 static WERROR cache_subkeys(struct ldb_key_data *kd)
223 struct ldb_context *c = kd->ldb;
224 struct ldb_result *res;
227 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "(key=*)");
229 if (ret != LDB_SUCCESS) {
230 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
231 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
235 kd->subkey_count = res->count;
236 kd->subkeys = talloc_steal(kd, res->msgs);
242 static WERROR cache_values(struct ldb_key_data *kd)
244 struct ldb_context *c = kd->ldb;
245 struct ldb_result *res;
248 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
251 if (ret != LDB_SUCCESS) {
252 DEBUG(0, ("Error getting values for '%s': %s\n",
253 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
257 kd->value_count = res->count;
258 kd->values = talloc_steal(kd, res->msgs);
265 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
266 const struct hive_key *k, uint32_t idx,
268 const char **classname,
269 NTTIME *last_mod_time)
271 struct ldb_message_element *el;
272 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
277 if (classname != NULL)
278 *classname = NULL; /* TODO: Store properly */
279 if (last_mod_time != NULL)
280 *last_mod_time = 0; /* TODO: we need to add this to the
281 ldb backend properly */
283 /* Do a search if necessary */
284 if (kd->subkeys == NULL) {
285 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
288 if (idx >= kd->subkey_count)
289 return WERR_NO_MORE_ITEMS;
291 el = ldb_msg_find_element(kd->subkeys[idx], "key");
292 SMB_ASSERT(el != NULL);
293 SMB_ASSERT(el->num_values != 0);
296 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
301 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
302 const char **name, uint32_t *data_type,
305 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
306 struct ldb_context *c = kd->ldb;
307 const char* attrs[] = { "data", "type", NULL };
308 struct ldb_result *res;
311 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_BASE, attrs, "%s", "");
313 if (ret != LDB_SUCCESS) {
314 DEBUG(0, ("Error getting default value for '%s': %s\n",
315 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
319 if (res->count == 0 || res->msgs[0]->num_elements == 0)
322 reg_ldb_unpack_value(mem_ctx,
323 res->msgs[0], name, data_type, data);
330 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
331 int idx, const char **name,
332 uint32_t *data_type, DATA_BLOB *data)
334 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
336 /* if default value exists, give it back */
337 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
345 /* Do the search if necessary */
346 if (kd->values == NULL) {
347 W_ERROR_NOT_OK_RETURN(cache_values(kd));
350 if (idx >= kd->value_count)
351 return WERR_NO_MORE_ITEMS;
353 reg_ldb_unpack_value(mem_ctx, kd->values[idx], name, data_type, data);
358 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
359 const char *name, uint32_t *data_type,
362 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
363 struct ldb_context *c = kd->ldb;
364 struct ldb_result *res;
368 if (strlen(name) == 0) {
370 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
373 query = talloc_asprintf(mem_ctx, "(value=%s)", name);
374 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "%s", query);
377 if (ret != LDB_SUCCESS) {
378 DEBUG(0, ("Error getting values for '%s': %s\n",
379 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
386 reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
394 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
395 const char *name, struct hive_key **key)
397 struct ldb_result *res;
398 struct ldb_dn *ldap_path;
400 struct ldb_key_data *newkd;
401 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
402 struct ldb_context *c = kd->ldb;
404 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
406 ret = ldb_search(c, mem_ctx, &res, ldap_path, LDB_SCOPE_BASE, NULL, "(key=*)");
408 if (ret != LDB_SUCCESS) {
409 DEBUG(3, ("Error opening key '%s': %s\n",
410 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
412 } else if (res->count == 0) {
413 DEBUG(3, ("Key '%s' not found\n",
414 ldb_dn_get_linearized(ldap_path)));
419 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
420 newkd->key.ops = ®_backend_ldb;
421 newkd->ldb = talloc_reference(newkd, kd->ldb);
422 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
424 *key = (struct hive_key *)newkd;
429 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
430 struct auth_session_info *session_info,
431 struct cli_credentials *credentials,
432 struct tevent_context *ev_ctx,
433 struct loadparm_context *lp_ctx,
436 struct ldb_key_data *kd;
437 struct ldb_context *wrap;
438 struct ldb_message *attrs_msg;
440 if (location == NULL)
441 return WERR_INVALID_PARAM;
443 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
444 location, session_info, credentials, 0, NULL);
447 DEBUG(1, (__FILE__": unable to connect\n"));
451 attrs_msg = ldb_msg_new(wrap);
452 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
453 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
454 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
455 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
456 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
458 ldb_add(wrap, attrs_msg);
460 ldb_set_debug_stderr(wrap);
462 kd = talloc_zero(parent_ctx, struct ldb_key_data);
463 kd->key.ops = ®_backend_ldb;
464 kd->ldb = talloc_reference(kd, wrap);
465 talloc_set_destructor (kd, reg_close_ldb_key);
466 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
468 *k = (struct hive_key *)kd;
473 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
474 const char *name, const char *classname,
475 struct security_descriptor *sd,
476 struct hive_key **newkey)
478 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
479 struct ldb_message *msg;
480 struct ldb_key_data *newkd;
483 msg = ldb_msg_new(mem_ctx);
485 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
487 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
488 if (classname != NULL)
489 ldb_msg_add_string(msg, "classname",
490 talloc_strdup(mem_ctx, classname));
492 ret = ldb_add(parentkd->ldb, msg);
493 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
494 return WERR_ALREADY_EXISTS;
497 if (ret != LDB_SUCCESS) {
498 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
502 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
504 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
505 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
506 newkd->key.ops = ®_backend_ldb;
507 newkd->dn = talloc_steal(newkd, msg->dn);
509 *newkey = (struct hive_key *)newkd;
512 talloc_free(parentkd->subkeys);
513 parentkd->subkeys = NULL;
518 static WERROR ldb_del_value (struct hive_key *key, const char *child)
521 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
523 struct ldb_message *msg;
524 struct ldb_dn *childdn;
526 if (strlen(child) == 0) {
528 mem_ctx = talloc_init("ldb_del_value");
530 msg = talloc_zero(mem_ctx, struct ldb_message);
531 msg->dn = ldb_dn_copy(msg, kd->dn);
532 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
533 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
535 ret = ldb_modify(kd->ldb, msg);
536 if (ret != LDB_SUCCESS) {
537 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
538 talloc_free(mem_ctx);
542 talloc_free(mem_ctx);
545 childdn = ldb_dn_copy(kd->ldb, kd->dn);
546 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
547 reg_ldb_escape(childdn, child)))
549 talloc_free(childdn);
553 ret = ldb_delete(kd->ldb, childdn);
555 talloc_free(childdn);
557 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
559 } else if (ret != LDB_SUCCESS) {
560 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
566 talloc_free(kd->values);
572 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
575 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
576 struct ldb_dn *ldap_path;
577 TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
578 struct ldb_context *c = parentkd->ldb;
579 struct ldb_result *res_keys;
580 struct ldb_result *res_vals;
584 /* Verify key exists by opening it */
585 werr = ldb_open_key(mem_ctx, key, name, &hk);
586 if (!W_ERROR_IS_OK(werr)) {
587 talloc_free(mem_ctx);
591 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
593 talloc_free(mem_ctx);
597 /* Search for subkeys */
598 ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
601 if (ret != LDB_SUCCESS) {
602 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
603 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
604 talloc_free(mem_ctx);
608 /* Search for values */
609 ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
612 if (ret != LDB_SUCCESS) {
613 DEBUG(0, ("Error getting values for '%s': %s\n",
614 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
615 talloc_free(mem_ctx);
619 /* Start an explicit transaction */
620 ret = ldb_transaction_start(c);
622 if (ret != LDB_SUCCESS) {
623 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
624 talloc_free(mem_ctx);
628 if (res_keys->count || res_vals->count)
630 /* Delete any subkeys */
631 for (i = 0; i < res_keys->count; i++)
633 werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
636 if (!W_ERROR_IS_OK(werr)) {
637 ret = ldb_transaction_cancel(c);
638 talloc_free(mem_ctx);
643 /* Delete any values */
644 for (i = 0; i < res_vals->count; i++)
646 werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
649 if (!W_ERROR_IS_OK(werr)) {
650 ret = ldb_transaction_cancel(c);
651 talloc_free(mem_ctx);
657 /* Delete the key itself */
658 ret = ldb_delete(c, ldap_path);
660 if (ret != LDB_SUCCESS)
662 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
663 ret = ldb_transaction_cancel(c);
664 talloc_free(mem_ctx);
668 /* Commit the transaction */
669 ret = ldb_transaction_commit(c);
671 if (ret != LDB_SUCCESS)
673 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
674 ret = ldb_transaction_cancel(c);
675 talloc_free(mem_ctx);
679 talloc_free(mem_ctx);
682 talloc_free(parentkd->subkeys);
683 parentkd->subkeys = NULL;
688 static WERROR ldb_set_value(struct hive_key *parent,
689 const char *name, uint32_t type,
690 const DATA_BLOB data)
692 struct ldb_message *msg;
693 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
695 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
697 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
698 msg->dn = ldb_dn_copy(msg, kd->dn);
700 if (strlen(name) > 0) {
701 /* For a default value, we add/overwrite the attributes to/of the hive.
702 For a normal value, we create new childs. */
703 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
704 reg_ldb_escape(mem_ctx, name)))
706 talloc_free(mem_ctx);
711 ret = ldb_add(kd->ldb, msg);
712 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
714 for (i = 0; i < msg->num_elements; i++) {
715 if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE)
716 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
718 ret = ldb_modify(kd->ldb, msg);
721 if (ret != LDB_SUCCESS) {
722 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
723 talloc_free(mem_ctx);
728 talloc_free(kd->values);
731 talloc_free(mem_ctx);
735 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
736 const struct hive_key *key,
737 const char **classname,
738 uint32_t *num_subkeys,
739 uint32_t *num_values,
740 NTTIME *last_change_time,
741 uint32_t *max_subkeynamelen,
742 uint32_t *max_valnamelen,
743 uint32_t *max_valbufsize)
745 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
748 if (classname != NULL)
750 if (num_subkeys != NULL)
752 if (num_values != NULL)
754 if (last_change_time != NULL)
755 *last_change_time = 0;
756 if (max_subkeynamelen != NULL)
757 *max_subkeynamelen = 0;
758 if (max_valnamelen != NULL)
760 if (max_valbufsize != NULL)
763 if (kd->subkeys == NULL) {
764 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
767 if (kd->values == NULL) {
768 W_ERROR_NOT_OK_RETURN(cache_values(kd));
771 if (num_subkeys != NULL) {
772 *num_subkeys = kd->subkey_count;
774 if (num_values != NULL) {
775 *num_values = kd->value_count;
779 if (max_subkeynamelen != NULL) {
781 struct ldb_message_element *el;
783 *max_subkeynamelen = 0;
785 for (i = 0; i < kd->subkey_count; i++) {
786 el = ldb_msg_find_element(kd->subkeys[i], "key");
787 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
791 if (max_valnamelen != NULL || max_valbufsize != NULL) {
793 struct ldb_message_element *el;
794 W_ERROR_NOT_OK_RETURN(cache_values(kd));
796 if (max_valbufsize != NULL)
799 if (max_valnamelen != NULL)
802 for (i = 0; i < kd->value_count; i++) {
803 if (max_valnamelen != NULL) {
804 el = ldb_msg_find_element(kd->values[i], "value");
805 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
808 if (max_valbufsize != NULL) {
811 reg_ldb_unpack_value(mem_ctx,
814 *max_valbufsize = MAX(*max_valbufsize, data.length);
815 talloc_free(data.data);
823 static struct hive_operations reg_backend_ldb = {
825 .add_key = ldb_add_key,
826 .del_key = ldb_del_key,
827 .get_key_by_name = ldb_open_key,
828 .enum_value = ldb_get_value_by_id,
829 .enum_key = ldb_get_subkey_by_id,
830 .set_value = ldb_set_value,
831 .get_value_by_name = ldb_get_value,
832 .delete_value = ldb_del_value,
833 .get_key_info = ldb_get_key_info,