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");
64 data->length = convert_string_talloc(mem_ctx, iconv_convenience, CH_UTF8, CH_UTF16,
65 val->data, val->length,
66 (void **)&data->data);
83 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
84 *data = data_blob_talloc(mem_ctx, &tmp, 4);
94 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
97 uint32_t type, DATA_BLOB data)
100 struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
103 ldb_msg_add_string(msg, "value", talloc_strdup(mem_ctx, name));
108 if (data.data[0] != '\0') {
109 val.length = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF16, CH_UTF8,
113 ldb_msg_add_value(msg, "data", &val, NULL);
115 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
121 ldb_msg_add_value(msg, "data", &data, NULL);
123 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
127 ldb_msg_add_string(msg, "data",
128 talloc_asprintf(mem_ctx, "0x%x",
129 IVAL(data.data, 0)));
132 ldb_msg_add_value(msg, "data", &data, NULL);
136 type_s = talloc_asprintf(mem_ctx, "%u", type);
137 ldb_msg_add_string(msg, "type", type_s);
142 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
146 val.data = discard_const_p(uint8_t, value);
147 val.length = strlen(value);
149 return ldb_dn_escape_value(mem_ctx, val);
152 static int reg_close_ldb_key(struct ldb_key_data *key)
154 if (key->subkeys != NULL) {
155 talloc_free(key->subkeys);
159 if (key->values != NULL) {
160 talloc_free(key->values);
166 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
167 const struct hive_key *from,
168 const char *path, const char *add)
170 TALLOC_CTX *local_ctx;
172 char *mypath = talloc_strdup(mem_ctx, path);
174 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
175 struct ldb_context *ldb = kd->ldb;
177 local_ctx = talloc_new(mem_ctx);
180 ret = ldb_dn_new(mem_ctx, ldb, add);
182 ret = ldb_dn_new(mem_ctx, ldb, NULL);
184 if (!ldb_dn_validate(ret)) {
186 talloc_free(local_ctx);
193 begin = strrchr(mypath, '\\');
195 if (begin) keyname = begin + 1;
196 else keyname = mypath;
198 if(strlen(keyname)) {
199 if (!ldb_dn_add_base_fmt(ret, "key=%s",
200 reg_ldb_escape(local_ctx,
203 talloc_free(local_ctx);
215 ldb_dn_add_base(ret, kd->dn);
217 talloc_free(local_ctx);
222 static WERROR cache_subkeys(struct ldb_key_data *kd)
224 struct ldb_context *c = kd->ldb;
225 struct ldb_result *res;
228 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "(key=*)");
230 if (ret != LDB_SUCCESS) {
231 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
232 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
236 kd->subkey_count = res->count;
237 kd->subkeys = talloc_steal(kd, res->msgs);
243 static WERROR cache_values(struct ldb_key_data *kd)
245 struct ldb_context *c = kd->ldb;
246 struct ldb_result *res;
249 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
252 if (ret != LDB_SUCCESS) {
253 DEBUG(0, ("Error getting values for '%s': %s\n",
254 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
258 kd->value_count = res->count;
259 kd->values = talloc_steal(kd, res->msgs);
266 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
267 const struct hive_key *k, uint32_t idx,
269 const char **classname,
270 NTTIME *last_mod_time)
272 struct ldb_message_element *el;
273 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
278 if (classname != NULL)
279 *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 */
284 /* Do a search if necessary */
285 if (kd->subkeys == NULL) {
286 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
289 if (idx >= kd->subkey_count)
290 return WERR_NO_MORE_ITEMS;
292 el = ldb_msg_find_element(kd->subkeys[idx], "key");
293 SMB_ASSERT(el != NULL);
294 SMB_ASSERT(el->num_values != 0);
297 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
302 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
303 const char **name, uint32_t *data_type,
306 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
307 struct ldb_context *c = kd->ldb;
308 const char* attrs[] = { "data", "type", NULL };
309 struct ldb_result *res;
312 ret = ldb_search(c, kd->dn, LDB_SCOPE_BASE, "", attrs, &res);
314 if (ret != LDB_SUCCESS) {
315 DEBUG(0, ("Error getting default value for '%s': %s\n",
316 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
320 if (res->count == 0 || res->msgs[0]->num_elements == 0)
323 reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
324 res->msgs[0], name, data_type, data);
331 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
332 int idx, const char **name,
333 uint32_t *data_type, DATA_BLOB *data)
335 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
337 /* if default value exists, give it back */
338 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
346 /* Do the search if necessary */
347 if (kd->values == NULL) {
348 W_ERROR_NOT_OK_RETURN(cache_values(kd));
351 if (idx >= kd->value_count)
352 return WERR_NO_MORE_ITEMS;
354 reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
355 kd->values[idx], name, data_type, data);
360 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
361 const char *name, uint32_t *data_type,
364 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
365 struct ldb_context *c = kd->ldb;
366 struct ldb_result *res;
370 if (strlen(name) == 0) {
372 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
375 query = talloc_asprintf(mem_ctx, "(value=%s)", name);
376 ret = ldb_search(c, kd->dn, &res, LDB_SCOPE_ONELEVEL, query, NULL, "%s", query);
379 if (ret != LDB_SUCCESS) {
380 DEBUG(0, ("Error getting values for '%s': %s\n",
381 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
388 reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
389 res->msgs[0], NULL, data_type, data);
398 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
399 const char *name, struct hive_key **key)
401 struct ldb_result *res;
402 struct ldb_dn *ldap_path;
404 struct ldb_key_data *newkd;
405 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
406 struct ldb_context *c = kd->ldb;
408 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
410 ret = ldb_search(c, mem_ctx, &res, ldap_path, LDB_SCOPE_BASE, NULL, "(key=*)");
412 if (ret != LDB_SUCCESS) {
413 DEBUG(3, ("Error opening key '%s': %s\n",
414 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
416 } else if (res->count == 0) {
417 DEBUG(3, ("Key '%s' not found\n",
418 ldb_dn_get_linearized(ldap_path)));
423 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
424 newkd->key.ops = ®_backend_ldb;
425 newkd->ldb = talloc_reference(newkd, kd->ldb);
426 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
428 *key = (struct hive_key *)newkd;
433 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
434 struct auth_session_info *session_info,
435 struct cli_credentials *credentials,
436 struct event_context *ev_ctx,
437 struct loadparm_context *lp_ctx,
440 struct ldb_key_data *kd;
441 struct ldb_context *wrap;
442 struct ldb_message *attrs_msg;
444 if (location == NULL)
445 return WERR_INVALID_PARAM;
447 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
448 location, session_info, credentials, 0, NULL);
451 DEBUG(1, (__FILE__": unable to connect\n"));
455 attrs_msg = ldb_msg_new(wrap);
456 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
457 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
458 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
459 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
460 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
462 ldb_add(wrap, attrs_msg);
464 ldb_set_debug_stderr(wrap);
466 kd = talloc_zero(parent_ctx, struct ldb_key_data);
467 kd->key.ops = ®_backend_ldb;
468 kd->ldb = talloc_reference(kd, wrap);
469 talloc_set_destructor (kd, reg_close_ldb_key);
470 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
472 *k = (struct hive_key *)kd;
477 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
478 const char *name, const char *classname,
479 struct security_descriptor *sd,
480 struct hive_key **newkey)
482 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
483 struct ldb_message *msg;
484 struct ldb_key_data *newkd;
487 msg = ldb_msg_new(mem_ctx);
489 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
491 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
492 if (classname != NULL)
493 ldb_msg_add_string(msg, "classname",
494 talloc_strdup(mem_ctx, classname));
496 ret = ldb_add(parentkd->ldb, msg);
497 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
498 return WERR_ALREADY_EXISTS;
501 if (ret != LDB_SUCCESS) {
502 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
506 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
508 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
509 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
510 newkd->key.ops = ®_backend_ldb;
511 newkd->dn = talloc_steal(newkd, msg->dn);
513 *newkey = (struct hive_key *)newkd;
516 talloc_free(parentkd->subkeys);
517 parentkd->subkeys = NULL;
522 static WERROR ldb_del_value (struct hive_key *key, const char *child)
525 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
527 struct ldb_message *msg;
528 struct ldb_dn *childdn;
530 if (strlen(child) == 0) {
532 mem_ctx = talloc_init("ldb_del_value");
534 msg = talloc_zero(mem_ctx, struct ldb_message);
535 msg->dn = ldb_dn_copy(msg, kd->dn);
536 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
537 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
539 ret = ldb_modify(kd->ldb, msg);
540 if (ret != LDB_SUCCESS) {
541 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
542 talloc_free(mem_ctx);
546 talloc_free(mem_ctx);
549 childdn = ldb_dn_copy(kd->ldb, kd->dn);
550 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
551 reg_ldb_escape(childdn, child)))
553 talloc_free(childdn);
557 ret = ldb_delete(kd->ldb, childdn);
559 talloc_free(childdn);
561 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
563 } else if (ret != LDB_SUCCESS) {
564 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
570 talloc_free(kd->values);
576 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
579 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
580 struct ldb_dn *ldap_path;
581 TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
582 struct ldb_context *c = parentkd->ldb;
583 struct ldb_result *res_keys;
584 struct ldb_result *res_vals;
588 /* Verify key exists by opening it */
589 werr = ldb_open_key(mem_ctx, key, name, &hk);
590 if (!W_ERROR_IS_OK(werr)) {
591 talloc_free(mem_ctx);
595 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
597 talloc_free(mem_ctx);
601 /* Search for subkeys */
602 ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
605 if (ret != LDB_SUCCESS) {
606 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
607 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
608 talloc_free(mem_ctx);
612 /* Search for values */
613 ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
616 if (ret != LDB_SUCCESS) {
617 DEBUG(0, ("Error getting values for '%s': %s\n",
618 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
619 talloc_free(mem_ctx);
623 /* Start an explicit transaction */
624 ret = ldb_transaction_start(c);
626 if (ret != LDB_SUCCESS) {
627 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
628 talloc_free(mem_ctx);
632 if (res_keys->count || res_vals->count)
634 /* Delete any subkeys */
635 for (i = 0; i < res_keys->count; i++)
637 werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
640 if (!W_ERROR_IS_OK(werr)) {
641 ret = ldb_transaction_cancel(c);
642 talloc_free(mem_ctx);
647 /* Delete any values */
648 for (i = 0; i < res_vals->count; i++)
650 werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
653 if (!W_ERROR_IS_OK(werr)) {
654 ret = ldb_transaction_cancel(c);
655 talloc_free(mem_ctx);
661 /* Delete the key itself */
662 ret = ldb_delete(c, ldap_path);
664 if (ret != LDB_SUCCESS)
666 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
667 ret = ldb_transaction_cancel(c);
668 talloc_free(mem_ctx);
672 /* Commit the transaction */
673 ret = ldb_transaction_commit(c);
675 if (ret != LDB_SUCCESS)
677 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
678 ret = ldb_transaction_cancel(c);
679 talloc_free(mem_ctx);
683 talloc_free(mem_ctx);
686 talloc_free(parentkd->subkeys);
687 parentkd->subkeys = NULL;
692 static WERROR ldb_set_value(struct hive_key *parent,
693 const char *name, uint32_t type,
694 const DATA_BLOB data)
696 struct ldb_message *msg;
697 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
699 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
701 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
702 msg->dn = ldb_dn_copy(msg, kd->dn);
704 if (strlen(name) > 0) {
705 /* For a default value, we add/overwrite the attributes to/of the hive.
706 For a normal value, we create new childs. */
707 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
708 reg_ldb_escape(mem_ctx, name)))
710 talloc_free(mem_ctx);
715 ret = ldb_add(kd->ldb, msg);
716 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
718 for (i = 0; i < msg->num_elements; i++) {
719 if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE)
720 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
722 ret = ldb_modify(kd->ldb, msg);
725 if (ret != LDB_SUCCESS) {
726 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
727 talloc_free(mem_ctx);
732 talloc_free(kd->values);
735 talloc_free(mem_ctx);
739 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
740 const struct hive_key *key,
741 const char **classname,
742 uint32_t *num_subkeys,
743 uint32_t *num_values,
744 NTTIME *last_change_time,
745 uint32_t *max_subkeynamelen,
746 uint32_t *max_valnamelen,
747 uint32_t *max_valbufsize)
749 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
752 if (classname != NULL)
754 if (num_subkeys != NULL)
756 if (num_values != NULL)
758 if (last_change_time != NULL)
759 *last_change_time = 0;
760 if (max_subkeynamelen != NULL)
761 *max_subkeynamelen = 0;
762 if (max_valnamelen != NULL)
764 if (max_valbufsize != NULL)
767 if (kd->subkeys == NULL) {
768 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
771 if (kd->values == NULL) {
772 W_ERROR_NOT_OK_RETURN(cache_values(kd));
775 if (num_subkeys != NULL) {
776 *num_subkeys = kd->subkey_count;
778 if (num_values != NULL) {
779 *num_values = kd->value_count;
783 if (max_subkeynamelen != NULL) {
785 struct ldb_message_element *el;
787 *max_subkeynamelen = 0;
789 for (i = 0; i < kd->subkey_count; i++) {
790 el = ldb_msg_find_element(kd->subkeys[i], "key");
791 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
795 if (max_valnamelen != NULL || max_valbufsize != NULL) {
797 struct ldb_message_element *el;
798 W_ERROR_NOT_OK_RETURN(cache_values(kd));
800 if (max_valbufsize != NULL)
803 if (max_valnamelen != NULL)
806 for (i = 0; i < kd->value_count; i++) {
807 if (max_valnamelen != NULL) {
808 el = ldb_msg_find_element(kd->values[i], "value");
809 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
812 if (max_valbufsize != NULL) {
815 reg_ldb_unpack_value(mem_ctx,
816 lp_iconv_convenience(global_loadparm),
819 *max_valbufsize = MAX(*max_valbufsize, data.length);
820 talloc_free(data.data);
828 static struct hive_operations reg_backend_ldb = {
830 .add_key = ldb_add_key,
831 .del_key = ldb_del_key,
832 .get_key_by_name = ldb_open_key,
833 .enum_value = ldb_get_value_by_id,
834 .enum_key = ldb_get_subkey_by_id,
835 .set_value = ldb_set_value,
836 .get_value_by_name = ldb_get_value,
837 .delete_value = ldb_del_value,
838 .get_key_info = ldb_get_key_info,