2 Unix SMB/CIFS implementation.
4 Copyright (C) Jelmer Vernooij 2004-2007.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "lib/ldb/include/ldb.h"
23 #include "lib/ldb/include/ldb_errors.h"
25 #include "librpc/gen_ndr/winreg.h"
26 #include "param/param.h"
28 static struct hive_operations reg_backend_ldb;
33 struct ldb_context *ldb;
35 struct ldb_message **subkeys, **values;
36 int subkey_count, value_count;
39 static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx,
40 struct smb_iconv_convenience *iconv_convenience,
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");
62 data->length = convert_string_talloc(mem_ctx, iconv_convenience, CH_UTF8, CH_UTF16,
63 val->data, val->length,
64 (void **)&data->data);
68 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
69 *data = data_blob_talloc(mem_ctx, &tmp, 4);
74 *data = data_blob_talloc(mem_ctx, val->data, val->length);
79 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
82 uint32_t type, DATA_BLOB data)
85 struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
88 ldb_msg_add_string(msg, "value", talloc_strdup(mem_ctx, name));
93 val.length = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF16, CH_UNIX,
97 ldb_msg_add_value(msg, "data", &val, NULL);
101 ldb_msg_add_string(msg, "data",
102 talloc_asprintf(mem_ctx, "0x%x",
103 IVAL(data.data, 0)));
106 ldb_msg_add_value(msg, "data", &data, NULL);
110 type_s = talloc_asprintf(mem_ctx, "%u", type);
111 ldb_msg_add_string(msg, "type", type_s);
116 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
120 val.data = discard_const_p(uint8_t, value);
121 val.length = strlen(value);
123 return ldb_dn_escape_value(mem_ctx, val);
126 static int reg_close_ldb_key(struct ldb_key_data *key)
128 if (key->subkeys != NULL) {
129 talloc_free(key->subkeys);
133 if (key->values != NULL) {
134 talloc_free(key->values);
140 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
141 const struct hive_key *from,
142 const char *path, const char *add)
144 TALLOC_CTX *local_ctx;
146 char *mypath = talloc_strdup(mem_ctx, path);
148 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
149 struct ldb_context *ldb = kd->ldb;
151 local_ctx = talloc_new(mem_ctx);
154 ret = ldb_dn_new(mem_ctx, ldb, add);
156 ret = ldb_dn_new(mem_ctx, ldb, NULL);
158 if (!ldb_dn_validate(ret)) {
160 talloc_free(local_ctx);
167 begin = strrchr(mypath, '\\');
169 if (begin) keyname = begin + 1;
170 else keyname = mypath;
172 if(strlen(keyname)) {
173 if (!ldb_dn_add_base_fmt(ret, "key=%s",
174 reg_ldb_escape(local_ctx,
177 talloc_free(local_ctx);
189 ldb_dn_add_base(ret, kd->dn);
191 talloc_free(local_ctx);
196 static WERROR cache_subkeys(struct ldb_key_data *kd)
198 struct ldb_context *c = kd->ldb;
199 struct ldb_result *res;
202 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL, &res);
204 if (ret != LDB_SUCCESS) {
205 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
206 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
210 kd->subkey_count = res->count;
211 kd->subkeys = talloc_steal(kd, res->msgs);
217 static WERROR cache_values(struct ldb_key_data *kd)
219 struct ldb_context *c = kd->ldb;
220 struct ldb_result *res;
223 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL,
224 "(value=*)", NULL, &res);
226 if (ret != LDB_SUCCESS) {
227 DEBUG(0, ("Error getting values for '%s': %s\n",
228 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
231 kd->value_count = res->count;
232 kd->values = talloc_steal(kd, res->msgs);
238 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
239 const struct hive_key *k, uint32_t idx,
241 const char **classname,
242 NTTIME *last_mod_time)
244 struct ldb_message_element *el;
245 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
247 /* Do a search if necessary */
248 if (kd->subkeys == NULL) {
249 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
252 if (idx >= kd->subkey_count)
253 return WERR_NO_MORE_ITEMS;
255 el = ldb_msg_find_element(kd->subkeys[idx], "key");
256 SMB_ASSERT(el != NULL);
257 SMB_ASSERT(el->num_values != 0);
260 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
262 if (classname != NULL)
263 *classname = NULL; /* TODO: Store properly */
265 if (last_mod_time != NULL)
266 *last_mod_time = 0; /* TODO: we need to add this to the
267 ldb backend properly */
272 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
273 int idx, const char **name,
274 uint32_t *data_type, DATA_BLOB *data)
276 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
278 /* Do the search if necessary */
279 if (kd->values == NULL) {
280 W_ERROR_NOT_OK_RETURN(cache_values(kd));
283 if (idx >= kd->value_count)
284 return WERR_NO_MORE_ITEMS;
286 reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm), kd->values[idx],
287 name, data_type, data);
292 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
293 const char *name, uint32_t *data_type,
296 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
297 struct ldb_context *c = kd->ldb;
298 struct ldb_result *res;
300 char *query = talloc_asprintf(mem_ctx, "(value=%s)", name);
302 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, query, NULL, &res);
306 if (ret != LDB_SUCCESS) {
307 DEBUG(0, ("Error getting values for '%s': %s\n",
308 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
315 reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm), res->msgs[0], NULL, data_type, data);
320 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
321 const char *name, struct hive_key **key)
323 struct ldb_result *res;
324 struct ldb_dn *ldap_path;
326 struct ldb_key_data *newkd;
327 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
328 struct ldb_context *c = kd->ldb;
330 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
332 ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL, &res);
334 if (ret != LDB_SUCCESS) {
335 DEBUG(3, ("Error opening key '%s': %s\n",
336 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
338 } else if (res->count == 0) {
339 DEBUG(3, ("Key '%s' not found\n",
340 ldb_dn_get_linearized(ldap_path)));
345 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
346 newkd->key.ops = ®_backend_ldb;
347 newkd->ldb = talloc_reference(newkd, kd->ldb);
348 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
350 *key = (struct hive_key *)newkd;
357 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
358 struct auth_session_info *session_info,
359 struct cli_credentials *credentials,
360 struct event_context *ev_ctx,
361 struct loadparm_context *lp_ctx,
364 struct ldb_key_data *kd;
365 struct ldb_context *wrap;
366 struct ldb_message *attrs_msg;
368 if (location == NULL)
369 return WERR_INVALID_PARAM;
371 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
372 location, session_info, credentials, 0, NULL);
375 DEBUG(1, (__FILE__": unable to connect\n"));
379 attrs_msg = ldb_msg_new(wrap);
380 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
381 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
382 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
383 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
384 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
386 ldb_add(wrap, attrs_msg);
388 ldb_set_debug_stderr(wrap);
390 kd = talloc_zero(parent_ctx, struct ldb_key_data);
391 kd->key.ops = ®_backend_ldb;
392 kd->ldb = talloc_reference(kd, wrap);
393 talloc_set_destructor (kd, reg_close_ldb_key);
394 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
396 *k = (struct hive_key *)kd;
401 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
402 const char *name, const char *classname,
403 struct security_descriptor *sd,
404 struct hive_key **newkey)
406 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
407 struct ldb_message *msg;
408 struct ldb_key_data *newkd;
411 msg = ldb_msg_new(mem_ctx);
413 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
415 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
416 if (classname != NULL)
417 ldb_msg_add_string(msg, "classname",
418 talloc_strdup(mem_ctx, classname));
420 ret = ldb_add(parentkd->ldb, msg);
421 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
422 return WERR_ALREADY_EXISTS;
425 if (ret != LDB_SUCCESS) {
426 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
430 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
432 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
433 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
434 newkd->key.ops = ®_backend_ldb;
435 newkd->dn = talloc_steal(newkd, msg->dn);
437 *newkey = (struct hive_key *)newkd;
440 talloc_free(parentkd->subkeys);
441 parentkd->subkeys = NULL;
446 static WERROR ldb_del_value (struct hive_key *key, const char *child)
449 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
450 struct ldb_dn *childdn;
452 childdn = ldb_dn_copy(kd->ldb, kd->dn);
453 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
454 reg_ldb_escape(childdn, child)))
456 talloc_free(childdn);
460 ret = ldb_delete(kd->ldb, childdn);
462 talloc_free(childdn);
464 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
466 } else if (ret != LDB_SUCCESS) {
467 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
472 talloc_free(kd->values);
478 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
481 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
482 struct ldb_dn *ldap_path;
483 TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
484 struct ldb_context *c = parentkd->ldb;
485 struct ldb_result *res_keys;
486 struct ldb_result *res_vals;
490 /* Verify key exists by opening it */
491 werr = ldb_open_key(mem_ctx, key, name, &hk);
492 if (!W_ERROR_IS_OK(werr)) {
493 talloc_free(mem_ctx);
497 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
499 talloc_free(mem_ctx);
503 /* Search for subkeys */
504 ret = ldb_search(c, ldap_path, LDB_SCOPE_ONELEVEL,
505 "(key=*)", NULL, &res_keys);
507 if (ret != LDB_SUCCESS) {
508 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
509 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
510 talloc_free(mem_ctx);
514 /* Search for values */
515 ret = ldb_search(c, ldap_path, LDB_SCOPE_ONELEVEL,
516 "(value=*)", NULL, &res_vals);
518 if (ret != LDB_SUCCESS) {
519 DEBUG(0, ("Error getting values for '%s': %s\n",
520 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
521 talloc_free(mem_ctx);
525 /* Start an explicit transaction */
526 ret = ldb_transaction_start(c);
528 if (ret != LDB_SUCCESS) {
529 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
530 talloc_free(mem_ctx);
534 if (res_keys->count || res_vals->count)
536 /* Delete any subkeys */
537 for (i = 0; i < res_keys->count; i++)
539 werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
542 if (!W_ERROR_IS_OK(werr)) {
543 ret = ldb_transaction_cancel(c);
544 talloc_free(mem_ctx);
549 /* Delete any values */
550 for (i = 0; i < res_vals->count; i++)
552 werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
555 if (!W_ERROR_IS_OK(werr)) {
556 ret = ldb_transaction_cancel(c);
557 talloc_free(mem_ctx);
563 /* Delete the key itself */
564 ret = ldb_delete(c, ldap_path);
566 if (ret != LDB_SUCCESS)
568 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
569 ret = ldb_transaction_cancel(c);
570 talloc_free(mem_ctx);
574 /* Commit the transaction */
575 ret = ldb_transaction_commit(c);
577 if (ret != LDB_SUCCESS)
579 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
580 ret = ldb_transaction_cancel(c);
581 talloc_free(mem_ctx);
585 talloc_free(mem_ctx);
588 talloc_free(parentkd->subkeys);
589 parentkd->subkeys = NULL;
594 static WERROR ldb_set_value(struct hive_key *parent,
595 const char *name, uint32_t type,
596 const DATA_BLOB data)
598 struct ldb_message *msg;
599 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
601 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
603 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
605 msg->dn = ldb_dn_copy(msg, kd->dn);
606 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
607 reg_ldb_escape(mem_ctx, name)))
609 talloc_free(mem_ctx);
613 ret = ldb_add(kd->ldb, msg);
614 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
616 for (i = 0; i < msg->num_elements; i++) {
617 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
619 ret = ldb_modify(kd->ldb, msg);
622 if (ret != LDB_SUCCESS) {
623 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(kd->ldb)));
624 talloc_free(mem_ctx);
629 talloc_free(kd->values);
632 talloc_free(mem_ctx);
636 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
637 const struct hive_key *key,
638 const char **classname,
639 uint32_t *num_subkeys,
640 uint32_t *num_values,
641 NTTIME *last_change_time,
642 uint32_t *max_subkeynamelen,
643 uint32_t *max_valnamelen,
644 uint32_t *max_valbufsize)
646 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
648 if (kd->subkeys == NULL) {
649 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
652 if (kd->values == NULL) {
653 W_ERROR_NOT_OK_RETURN(cache_values(kd));
657 if (classname != NULL)
660 if (num_subkeys != NULL) {
661 *num_subkeys = kd->subkey_count;
664 if (num_values != NULL) {
665 *num_values = kd->value_count;
668 if (last_change_time != NULL)
669 *last_change_time = 0;
671 if (max_subkeynamelen != NULL) {
673 struct ldb_message_element *el;
675 *max_subkeynamelen = 0;
677 for (i = 0; i < kd->subkey_count; i++) {
678 el = ldb_msg_find_element(kd->subkeys[i], "key");
679 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
683 if (max_valnamelen != NULL || max_valbufsize != NULL) {
685 struct ldb_message_element *el;
686 W_ERROR_NOT_OK_RETURN(cache_values(kd));
688 if (max_valbufsize != NULL)
691 if (max_valnamelen != NULL)
694 for (i = 0; i < kd->value_count; i++) {
695 if (max_valnamelen != NULL) {
696 el = ldb_msg_find_element(kd->values[i], "value");
697 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
700 if (max_valbufsize != NULL) {
702 reg_ldb_unpack_value(mem_ctx,
703 lp_iconv_convenience(global_loadparm),
706 *max_valbufsize = MAX(*max_valbufsize, data.length);
707 talloc_free(data.data);
715 static struct hive_operations reg_backend_ldb = {
717 .add_key = ldb_add_key,
718 .del_key = ldb_del_key,
719 .get_key_by_name = ldb_open_key,
720 .enum_value = ldb_get_value_by_id,
721 .enum_key = ldb_get_subkey_by_id,
722 .set_value = ldb_set_value,
723 .get_value_by_name = ldb_get_value,
724 .delete_value = ldb_del_value,
725 .get_key_info = ldb_get_key_info,