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);
262 /* Do a search if necessary */
263 if (kd->subkeys == NULL) {
264 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
267 if (idx >= kd->subkey_count)
268 return WERR_NO_MORE_ITEMS;
270 el = ldb_msg_find_element(kd->subkeys[idx], "key");
271 SMB_ASSERT(el != NULL);
272 SMB_ASSERT(el->num_values != 0);
275 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
277 if (classname != NULL)
278 *classname = NULL; /* TODO: Store properly */
280 if (last_mod_time != NULL)
281 *last_mod_time = 0; /* TODO: we need to add this to the
282 ldb backend properly */
287 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
288 const char** name, uint32_t *data_type,
291 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
292 struct ldb_context *c = kd->ldb;
293 const char* attrs[] = { "data", "type", NULL };
294 struct ldb_result *res;
297 ret = ldb_search(c, kd->dn, LDB_SCOPE_BASE, "", attrs, &res);
299 if (ret != LDB_SUCCESS) {
300 DEBUG(0, ("Error getting default value for '%s': %s\n",
301 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
305 if (res->count == 0 || res->msgs[0]->num_elements == 0)
308 reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
309 res->msgs[0], name, data_type, data);
316 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
317 int idx, const char **name,
318 uint32_t *data_type, DATA_BLOB *data)
320 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
322 /* if default value exists, give it back */
323 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type, data)))
329 /* Do the search if necessary */
330 if (kd->values == NULL) {
331 W_ERROR_NOT_OK_RETURN(cache_values(kd));
334 if (idx >= kd->value_count)
335 return WERR_NO_MORE_ITEMS;
337 reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
338 kd->values[idx], name, data_type, data);
343 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
344 const char *name, uint32_t *data_type,
347 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
348 struct ldb_context *c = kd->ldb;
349 struct ldb_result *res;
353 if (strlen(name) == 0) {
355 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
358 query = talloc_asprintf(mem_ctx, "(value=%s)", name);
359 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, query, NULL, &res);
362 if (ret != LDB_SUCCESS) {
363 DEBUG(0, ("Error getting values for '%s': %s\n",
364 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
371 reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
372 res->msgs[0], NULL, data_type, data);
380 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
381 const char *name, struct hive_key **key)
383 struct ldb_result *res;
384 struct ldb_dn *ldap_path;
386 struct ldb_key_data *newkd;
387 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
388 struct ldb_context *c = kd->ldb;
390 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
392 ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL, &res);
394 if (ret != LDB_SUCCESS) {
395 DEBUG(3, ("Error opening key '%s': %s\n",
396 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
398 } else if (res->count == 0) {
399 DEBUG(3, ("Key '%s' not found\n",
400 ldb_dn_get_linearized(ldap_path)));
405 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
406 newkd->key.ops = ®_backend_ldb;
407 newkd->ldb = talloc_reference(newkd, kd->ldb);
408 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
410 *key = (struct hive_key *)newkd;
415 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
416 struct auth_session_info *session_info,
417 struct cli_credentials *credentials,
418 struct event_context *ev_ctx,
419 struct loadparm_context *lp_ctx,
422 struct ldb_key_data *kd;
423 struct ldb_context *wrap;
424 struct ldb_message *attrs_msg;
426 if (location == NULL)
427 return WERR_INVALID_PARAM;
429 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
430 location, session_info, credentials, 0, NULL);
433 DEBUG(1, (__FILE__": unable to connect\n"));
437 attrs_msg = ldb_msg_new(wrap);
438 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
439 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
440 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
441 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
442 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
444 ldb_add(wrap, attrs_msg);
446 ldb_set_debug_stderr(wrap);
448 kd = talloc_zero(parent_ctx, struct ldb_key_data);
449 kd->key.ops = ®_backend_ldb;
450 kd->ldb = talloc_reference(kd, wrap);
451 talloc_set_destructor (kd, reg_close_ldb_key);
452 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
454 *k = (struct hive_key *)kd;
459 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
460 const char *name, const char *classname,
461 struct security_descriptor *sd,
462 struct hive_key **newkey)
464 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
465 struct ldb_message *msg;
466 struct ldb_key_data *newkd;
469 msg = ldb_msg_new(mem_ctx);
471 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
473 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
474 if (classname != NULL)
475 ldb_msg_add_string(msg, "classname",
476 talloc_strdup(mem_ctx, classname));
478 ret = ldb_add(parentkd->ldb, msg);
479 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
480 return WERR_ALREADY_EXISTS;
483 if (ret != LDB_SUCCESS) {
484 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
488 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
490 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
491 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
492 newkd->key.ops = ®_backend_ldb;
493 newkd->dn = talloc_steal(newkd, msg->dn);
495 *newkey = (struct hive_key *)newkd;
498 talloc_free(parentkd->subkeys);
499 parentkd->subkeys = NULL;
504 static WERROR ldb_del_value (struct hive_key *key, const char *child)
507 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
509 struct ldb_message *msg;
510 struct ldb_dn *childdn;
512 if (strlen(child) == 0) {
514 mem_ctx = talloc_init("ldb_del_value");
516 msg = talloc_zero(mem_ctx, struct ldb_message);
517 msg->dn = ldb_dn_copy(msg, kd->dn);
518 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
519 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
521 ret = ldb_modify(kd->ldb, msg);
522 if (ret != LDB_SUCCESS) {
523 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
524 talloc_free(mem_ctx);
528 talloc_free(mem_ctx);
531 childdn = ldb_dn_copy(kd->ldb, kd->dn);
532 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
533 reg_ldb_escape(childdn, child)))
535 talloc_free(childdn);
539 ret = ldb_delete(kd->ldb, childdn);
541 talloc_free(childdn);
543 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
545 } else if (ret != LDB_SUCCESS) {
546 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
552 talloc_free(kd->values);
558 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
561 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
562 struct ldb_dn *ldap_path;
563 TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
564 struct ldb_context *c = parentkd->ldb;
565 struct ldb_result *res_keys;
566 struct ldb_result *res_vals;
570 /* Verify key exists by opening it */
571 werr = ldb_open_key(mem_ctx, key, name, &hk);
572 if (!W_ERROR_IS_OK(werr)) {
573 talloc_free(mem_ctx);
577 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
579 talloc_free(mem_ctx);
583 /* Search for subkeys */
584 ret = ldb_search(c, ldap_path, LDB_SCOPE_ONELEVEL,
585 "(key=*)", NULL, &res_keys);
587 if (ret != LDB_SUCCESS) {
588 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
589 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
590 talloc_free(mem_ctx);
594 /* Search for values */
595 ret = ldb_search(c, ldap_path, LDB_SCOPE_ONELEVEL,
596 "(value=*)", NULL, &res_vals);
598 if (ret != LDB_SUCCESS) {
599 DEBUG(0, ("Error getting values for '%s': %s\n",
600 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
601 talloc_free(mem_ctx);
605 /* Start an explicit transaction */
606 ret = ldb_transaction_start(c);
608 if (ret != LDB_SUCCESS) {
609 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
610 talloc_free(mem_ctx);
614 if (res_keys->count || res_vals->count)
616 /* Delete any subkeys */
617 for (i = 0; i < res_keys->count; i++)
619 werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
622 if (!W_ERROR_IS_OK(werr)) {
623 ret = ldb_transaction_cancel(c);
624 talloc_free(mem_ctx);
629 /* Delete any values */
630 for (i = 0; i < res_vals->count; i++)
632 werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
635 if (!W_ERROR_IS_OK(werr)) {
636 ret = ldb_transaction_cancel(c);
637 talloc_free(mem_ctx);
643 /* Delete the key itself */
644 ret = ldb_delete(c, ldap_path);
646 if (ret != LDB_SUCCESS)
648 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
649 ret = ldb_transaction_cancel(c);
650 talloc_free(mem_ctx);
654 /* Commit the transaction */
655 ret = ldb_transaction_commit(c);
657 if (ret != LDB_SUCCESS)
659 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
660 ret = ldb_transaction_cancel(c);
661 talloc_free(mem_ctx);
665 talloc_free(mem_ctx);
668 talloc_free(parentkd->subkeys);
669 parentkd->subkeys = NULL;
674 static WERROR ldb_set_value(struct hive_key *parent,
675 const char *name, uint32_t type,
676 const DATA_BLOB data)
678 struct ldb_message *msg;
679 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
681 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
683 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
684 msg->dn = ldb_dn_copy(msg, kd->dn);
686 if (strlen(name) > 0) {
687 /* For a default value, we add/overwrite the attributes to/of the hive.
688 For a normal value, we create new childs. */
689 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
690 reg_ldb_escape(mem_ctx, name)))
692 talloc_free(mem_ctx);
697 ret = ldb_add(kd->ldb, msg);
698 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
700 for (i = 0; i < msg->num_elements; i++) {
701 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
703 ret = ldb_modify(kd->ldb, msg);
706 if (ret != LDB_SUCCESS) {
707 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
708 talloc_free(mem_ctx);
713 talloc_free(kd->values);
716 talloc_free(mem_ctx);
720 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
721 const struct hive_key *key,
722 const char **classname,
723 uint32_t *num_subkeys,
724 uint32_t *num_values,
725 NTTIME *last_change_time,
726 uint32_t *max_subkeynamelen,
727 uint32_t *max_valnamelen,
728 uint32_t *max_valbufsize)
730 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
732 if (kd->subkeys == NULL) {
733 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
736 if (kd->values == NULL) {
737 W_ERROR_NOT_OK_RETURN(cache_values(kd));
741 if (classname != NULL)
744 if (num_subkeys != NULL) {
745 *num_subkeys = kd->subkey_count;
748 if (num_values != NULL) {
749 *num_values = kd->value_count;
752 if (last_change_time != NULL)
753 *last_change_time = 0;
755 if (max_subkeynamelen != NULL) {
757 struct ldb_message_element *el;
759 *max_subkeynamelen = 0;
761 for (i = 0; i < kd->subkey_count; i++) {
762 el = ldb_msg_find_element(kd->subkeys[i], "key");
763 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
767 if (max_valnamelen != NULL || max_valbufsize != NULL) {
769 struct ldb_message_element *el;
770 W_ERROR_NOT_OK_RETURN(cache_values(kd));
772 if (max_valbufsize != NULL)
775 if (max_valnamelen != NULL)
778 for (i = 0; i < kd->value_count; i++) {
779 if (max_valnamelen != NULL) {
780 el = ldb_msg_find_element(kd->values[i], "value");
781 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
784 if (max_valbufsize != NULL) {
786 reg_ldb_unpack_value(mem_ctx,
787 lp_iconv_convenience(global_loadparm),
790 *max_valbufsize = MAX(*max_valbufsize, data.length);
791 talloc_free(data.data);
799 static struct hive_operations reg_backend_ldb = {
801 .add_key = ldb_add_key,
802 .del_key = ldb_del_key,
803 .get_key_by_name = ldb_open_key,
804 .enum_value = ldb_get_value_by_id,
805 .enum_key = ldb_get_subkey_by_id,
806 .set_value = ldb_set_value,
807 .get_value_by_name = ldb_get_value,
808 .delete_value = ldb_del_value,
809 .get_key_info = ldb_get_key_info,