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);
59 val = ldb_msg_find_ldb_val(msg, "data");
65 data->length = convert_string_talloc(mem_ctx,
66 iconv_convenience, CH_UNIX, CH_UTF16,
67 val->data, val->length,
68 (void **)&data->data);
73 *data = strhex_to_data_blob((char *)val->data);
81 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
82 *data = data_blob_talloc(mem_ctx, &tmp, 4);
87 *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 val.length = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF16, CH_UNIX,
111 ldb_msg_add_value(msg, "data", &val, NULL);
115 ldb_msg_add_string(msg, "data",
116 data_blob_hex_string(mem_ctx, &data));
120 ldb_msg_add_string(msg, "data",
121 talloc_asprintf(mem_ctx, "0x%x",
122 IVAL(data.data, 0)));
125 ldb_msg_add_value(msg, "data", &data, NULL);
129 type_s = talloc_asprintf(mem_ctx, "%u", type);
130 ldb_msg_add_string(msg, "type", type_s);
135 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
139 val.data = discard_const_p(uint8_t, value);
140 val.length = strlen(value);
142 return ldb_dn_escape_value(mem_ctx, val);
145 static int reg_close_ldb_key(struct ldb_key_data *key)
147 if (key->subkeys != NULL) {
148 talloc_free(key->subkeys);
152 if (key->values != NULL) {
153 talloc_free(key->values);
159 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
160 const struct hive_key *from,
161 const char *path, const char *add)
163 TALLOC_CTX *local_ctx;
165 char *mypath = talloc_strdup(mem_ctx, path);
167 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
168 struct ldb_context *ldb = kd->ldb;
170 local_ctx = talloc_new(mem_ctx);
173 ret = ldb_dn_new(mem_ctx, ldb, add);
175 ret = ldb_dn_new(mem_ctx, ldb, NULL);
177 if (!ldb_dn_validate(ret)) {
179 talloc_free(local_ctx);
186 begin = strrchr(mypath, '\\');
188 if (begin) keyname = begin + 1;
189 else keyname = mypath;
191 if(strlen(keyname)) {
192 if (!ldb_dn_add_base_fmt(ret, "key=%s",
193 reg_ldb_escape(local_ctx,
196 talloc_free(local_ctx);
208 ldb_dn_add_base(ret, kd->dn);
210 talloc_free(local_ctx);
215 static WERROR cache_subkeys(struct ldb_key_data *kd)
217 struct ldb_context *c = kd->ldb;
218 struct ldb_result *res;
221 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL, &res);
223 if (ret != LDB_SUCCESS) {
224 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
225 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
229 kd->subkey_count = res->count;
230 kd->subkeys = talloc_steal(kd, res->msgs);
236 static WERROR cache_values(struct ldb_key_data *kd)
238 struct ldb_context *c = kd->ldb;
239 struct ldb_result *res;
242 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL,
243 "(value=*)", NULL, &res);
245 if (ret != LDB_SUCCESS) {
246 DEBUG(0, ("Error getting values for '%s': %s\n",
247 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
251 kd->value_count = res->count;
252 kd->values = talloc_steal(kd, res->msgs);
259 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
260 const struct hive_key *k, uint32_t idx,
262 const char **classname,
263 NTTIME *last_mod_time)
265 struct ldb_message_element *el;
266 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
271 if (classname != NULL)
272 *classname = NULL; /* TODO: Store properly */
273 if (last_mod_time != NULL)
274 *last_mod_time = 0; /* TODO: we need to add this to the
275 ldb backend properly */
277 /* Do a search if necessary */
278 if (kd->subkeys == NULL) {
279 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
282 if (idx >= kd->subkey_count)
283 return WERR_NO_MORE_ITEMS;
285 el = ldb_msg_find_element(kd->subkeys[idx], "key");
286 SMB_ASSERT(el != NULL);
287 SMB_ASSERT(el->num_values != 0);
290 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
295 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
296 const char** name, uint32_t *data_type,
299 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
300 struct ldb_context *c = kd->ldb;
301 const char* attrs[] = { "data", "type", NULL };
302 struct ldb_result *res;
305 ret = ldb_search(c, kd->dn, LDB_SCOPE_BASE, "", attrs, &res);
307 if (ret != LDB_SUCCESS) {
308 DEBUG(0, ("Error getting default value for '%s': %s\n",
309 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
313 if (res->count == 0 || res->msgs[0]->num_elements == 0)
316 reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
317 res->msgs[0], name, data_type, data);
324 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
325 int idx, const char **name,
326 uint32_t *data_type, DATA_BLOB *data)
328 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
330 /* if default value exists, give it back */
331 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
339 /* Do the search if necessary */
340 if (kd->values == NULL) {
341 W_ERROR_NOT_OK_RETURN(cache_values(kd));
344 if (idx >= kd->value_count)
345 return WERR_NO_MORE_ITEMS;
347 reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
348 kd->values[idx], name, data_type, data);
353 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
354 const char *name, uint32_t *data_type,
357 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
358 struct ldb_context *c = kd->ldb;
359 struct ldb_result *res;
363 if (strlen(name) == 0) {
365 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
368 query = talloc_asprintf(mem_ctx, "(value=%s)", name);
369 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, query, NULL, &res);
372 if (ret != LDB_SUCCESS) {
373 DEBUG(0, ("Error getting values for '%s': %s\n",
374 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
381 reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
382 res->msgs[0], NULL, data_type, data);
390 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
391 const char *name, struct hive_key **key)
393 struct ldb_result *res;
394 struct ldb_dn *ldap_path;
396 struct ldb_key_data *newkd;
397 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
398 struct ldb_context *c = kd->ldb;
400 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
402 ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL, &res);
404 if (ret != LDB_SUCCESS) {
405 DEBUG(3, ("Error opening key '%s': %s\n",
406 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
408 } else if (res->count == 0) {
409 DEBUG(3, ("Key '%s' not found\n",
410 ldb_dn_get_linearized(ldap_path)));
415 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
416 newkd->key.ops = ®_backend_ldb;
417 newkd->ldb = talloc_reference(newkd, kd->ldb);
418 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
420 *key = (struct hive_key *)newkd;
425 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
426 struct auth_session_info *session_info,
427 struct cli_credentials *credentials,
428 struct event_context *ev_ctx,
429 struct loadparm_context *lp_ctx,
432 struct ldb_key_data *kd;
433 struct ldb_context *wrap;
434 struct ldb_message *attrs_msg;
436 if (location == NULL)
437 return WERR_INVALID_PARAM;
439 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
440 location, session_info, credentials, 0, NULL);
443 DEBUG(1, (__FILE__": unable to connect\n"));
447 attrs_msg = ldb_msg_new(wrap);
448 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
449 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
450 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
451 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
452 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
454 ldb_add(wrap, attrs_msg);
456 ldb_set_debug_stderr(wrap);
458 kd = talloc_zero(parent_ctx, struct ldb_key_data);
459 kd->key.ops = ®_backend_ldb;
460 kd->ldb = talloc_reference(kd, wrap);
461 talloc_set_destructor (kd, reg_close_ldb_key);
462 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
464 *k = (struct hive_key *)kd;
469 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
470 const char *name, const char *classname,
471 struct security_descriptor *sd,
472 struct hive_key **newkey)
474 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
475 struct ldb_message *msg;
476 struct ldb_key_data *newkd;
479 msg = ldb_msg_new(mem_ctx);
481 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
483 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
484 if (classname != NULL)
485 ldb_msg_add_string(msg, "classname",
486 talloc_strdup(mem_ctx, classname));
488 ret = ldb_add(parentkd->ldb, msg);
489 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
490 return WERR_ALREADY_EXISTS;
493 if (ret != LDB_SUCCESS) {
494 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
498 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
500 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
501 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
502 newkd->key.ops = ®_backend_ldb;
503 newkd->dn = talloc_steal(newkd, msg->dn);
505 *newkey = (struct hive_key *)newkd;
508 talloc_free(parentkd->subkeys);
509 parentkd->subkeys = NULL;
514 static WERROR ldb_del_value (struct hive_key *key, const char *child)
517 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
519 struct ldb_message *msg;
520 struct ldb_dn *childdn;
522 if (strlen(child) == 0) {
524 mem_ctx = talloc_init("ldb_del_value");
526 msg = talloc_zero(mem_ctx, struct ldb_message);
527 msg->dn = ldb_dn_copy(msg, kd->dn);
528 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
529 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
531 ret = ldb_modify(kd->ldb, msg);
532 if (ret != LDB_SUCCESS) {
533 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
534 talloc_free(mem_ctx);
538 talloc_free(mem_ctx);
541 childdn = ldb_dn_copy(kd->ldb, kd->dn);
542 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
543 reg_ldb_escape(childdn, child)))
545 talloc_free(childdn);
549 ret = ldb_delete(kd->ldb, childdn);
551 talloc_free(childdn);
553 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
555 } else if (ret != LDB_SUCCESS) {
556 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
562 talloc_free(kd->values);
568 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
571 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
572 struct ldb_dn *ldap_path;
573 TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
574 struct ldb_context *c = parentkd->ldb;
575 struct ldb_result *res_keys;
576 struct ldb_result *res_vals;
580 /* Verify key exists by opening it */
581 werr = ldb_open_key(mem_ctx, key, name, &hk);
582 if (!W_ERROR_IS_OK(werr)) {
583 talloc_free(mem_ctx);
587 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
589 talloc_free(mem_ctx);
593 /* Search for subkeys */
594 ret = ldb_search(c, ldap_path, LDB_SCOPE_ONELEVEL,
595 "(key=*)", NULL, &res_keys);
597 if (ret != LDB_SUCCESS) {
598 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
599 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
600 talloc_free(mem_ctx);
604 /* Search for values */
605 ret = ldb_search(c, ldap_path, LDB_SCOPE_ONELEVEL,
606 "(value=*)", NULL, &res_vals);
608 if (ret != LDB_SUCCESS) {
609 DEBUG(0, ("Error getting values for '%s': %s\n",
610 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
611 talloc_free(mem_ctx);
615 /* Start an explicit transaction */
616 ret = ldb_transaction_start(c);
618 if (ret != LDB_SUCCESS) {
619 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
620 talloc_free(mem_ctx);
624 if (res_keys->count || res_vals->count)
626 /* Delete any subkeys */
627 for (i = 0; i < res_keys->count; i++)
629 werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
632 if (!W_ERROR_IS_OK(werr)) {
633 ret = ldb_transaction_cancel(c);
634 talloc_free(mem_ctx);
639 /* Delete any values */
640 for (i = 0; i < res_vals->count; i++)
642 werr = ldb_del_value(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);
653 /* Delete the key itself */
654 ret = ldb_delete(c, ldap_path);
656 if (ret != LDB_SUCCESS)
658 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
659 ret = ldb_transaction_cancel(c);
660 talloc_free(mem_ctx);
664 /* Commit the transaction */
665 ret = ldb_transaction_commit(c);
667 if (ret != LDB_SUCCESS)
669 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
670 ret = ldb_transaction_cancel(c);
671 talloc_free(mem_ctx);
675 talloc_free(mem_ctx);
678 talloc_free(parentkd->subkeys);
679 parentkd->subkeys = NULL;
684 static WERROR ldb_set_value(struct hive_key *parent,
685 const char *name, uint32_t type,
686 const DATA_BLOB data)
688 struct ldb_message *msg;
689 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
691 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
693 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
694 msg->dn = ldb_dn_copy(msg, kd->dn);
696 if (strlen(name) > 0) {
697 /* For a default value, we add/overwrite the attributes to/of the hive.
698 For a normal value, we create new childs. */
699 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
700 reg_ldb_escape(mem_ctx, name)))
702 talloc_free(mem_ctx);
707 ret = ldb_add(kd->ldb, msg);
708 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
710 for (i = 0; i < msg->num_elements; i++) {
711 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
713 ret = ldb_modify(kd->ldb, msg);
716 if (ret != LDB_SUCCESS) {
717 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
718 talloc_free(mem_ctx);
723 talloc_free(kd->values);
726 talloc_free(mem_ctx);
730 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
731 const struct hive_key *key,
732 const char **classname,
733 uint32_t *num_subkeys,
734 uint32_t *num_values,
735 NTTIME *last_change_time,
736 uint32_t *max_subkeynamelen,
737 uint32_t *max_valnamelen,
738 uint32_t *max_valbufsize)
740 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
743 if (classname != NULL)
745 if (num_subkeys != NULL)
747 if (num_values != NULL)
749 if (last_change_time != NULL)
750 *last_change_time = 0;
751 if (max_subkeynamelen != NULL)
752 *max_subkeynamelen = 0;
753 if (max_valnamelen != NULL)
755 if (max_valbufsize != NULL)
758 if (kd->subkeys == NULL) {
759 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
762 if (kd->values == NULL) {
763 W_ERROR_NOT_OK_RETURN(cache_values(kd));
766 if (num_subkeys != NULL) {
767 *num_subkeys = kd->subkey_count;
769 if (num_values != NULL) {
770 *num_values = kd->value_count;
774 if (max_subkeynamelen != NULL) {
776 struct ldb_message_element *el;
778 *max_subkeynamelen = 0;
780 for (i = 0; i < kd->subkey_count; i++) {
781 el = ldb_msg_find_element(kd->subkeys[i], "key");
782 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
786 if (max_valnamelen != NULL || max_valbufsize != NULL) {
788 struct ldb_message_element *el;
789 W_ERROR_NOT_OK_RETURN(cache_values(kd));
791 if (max_valbufsize != NULL)
794 if (max_valnamelen != NULL)
797 for (i = 0; i < kd->value_count; i++) {
798 if (max_valnamelen != NULL) {
799 el = ldb_msg_find_element(kd->values[i], "value");
800 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
803 if (max_valbufsize != NULL) {
805 reg_ldb_unpack_value(mem_ctx,
806 lp_iconv_convenience(global_loadparm),
809 *max_valbufsize = MAX(*max_valbufsize, data.length);
810 talloc_free(data.data);
818 static struct hive_operations reg_backend_ldb = {
820 .add_key = ldb_add_key,
821 .del_key = ldb_del_key,
822 .get_key_by_name = ldb_open_key,
823 .enum_value = ldb_get_value_by_id,
824 .enum_key = ldb_get_subkey_by_id,
825 .set_value = ldb_set_value,
826 .get_value_by_name = ldb_get_value,
827 .delete_value = ldb_del_value,
828 .get_key_info = ldb_get_key_info,