s4:lib/registry/ldb.c - fix up registry backend to be more robust
[Samba/kamenim.git] / source4 / lib / registry / ldb.c
bloba27c94e4a94f052b535e06f23445ab50cf9d7780
1 /*
2 Unix SMB/CIFS implementation.
3 Registry interface
4 Copyright (C) 2004-2007, Jelmer Vernooij, jelmer@samba.org
5 Copyright (C) 2008-2010, Matthias Dieter Wallnöfer, mdw@samba.org
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/>.
21 #include "includes.h"
22 #include "registry.h"
23 #include "lib/ldb/include/ldb.h"
24 #include "lib/ldb/include/ldb_errors.h"
25 #include "ldb_wrap.h"
26 #include "librpc/gen_ndr/winreg.h"
27 #include "param/param.h"
29 static struct hive_operations reg_backend_ldb;
31 struct ldb_key_data
33 struct hive_key key;
34 struct ldb_context *ldb;
35 struct ldb_dn *dn;
36 struct ldb_message **subkeys, **values;
37 unsigned 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,
43 DATA_BLOB *data)
45 const struct ldb_val *val;
46 uint32_t value_type;
48 if (name != NULL) {
49 *name = talloc_strdup(mem_ctx,
50 ldb_msg_find_attr_as_string(msg, "value",
51 NULL));
54 value_type = ldb_msg_find_attr_as_uint(msg, "type", 0);
55 *type = value_type;
57 val = ldb_msg_find_ldb_val(msg, "data");
59 switch (value_type)
61 case REG_SZ:
62 case REG_EXPAND_SZ:
63 if (val != NULL) {
64 convert_string_talloc(mem_ctx, CH_UTF8, CH_UTF16,
65 val->data, val->length,
66 (void **)&data->data, &data->length, false);
67 } else {
68 data->data = NULL;
69 data->length = 0;
71 break;
73 case REG_DWORD:
74 if (val != NULL) {
75 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
76 *data = data_blob_talloc(mem_ctx, NULL, 4);
77 SIVAL(data->data, 0, tmp);
78 } else {
79 data->data = NULL;
80 data->length = 0;
82 break;
84 case REG_BINARY:
85 default:
86 if (val != NULL) {
87 *data = data_blob_talloc(mem_ctx, val->data, val->length);
88 } else {
89 data->data = NULL;
90 data->length = 0;
92 break;
96 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
97 TALLOC_CTX *mem_ctx,
98 const char *name,
99 uint32_t type, DATA_BLOB data)
101 struct ldb_val val;
102 struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
103 char *type_s;
105 ldb_msg_add_string(msg, "value", talloc_strdup(mem_ctx, name));
107 switch (type) {
108 case REG_SZ:
109 case REG_EXPAND_SZ:
110 if ((data.length > 0) && (data.data != NULL)
111 && (data.data[0] != '\0')) {
112 convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8,
113 (void *)data.data,
114 data.length,
115 (void **)&val.data, &val.length, false);
116 ldb_msg_add_value(msg, "data", &val, NULL);
117 } else {
118 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
120 break;
122 case REG_DWORD:
123 if ((data.length > 0) && (data.data != NULL)) {
124 ldb_msg_add_string(msg, "data",
125 talloc_asprintf(mem_ctx, "0x%x",
126 IVAL(data.data, 0)));
127 } else {
128 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
130 break;
132 case REG_BINARY:
133 default:
134 if ((data.length > 0) && (data.data != NULL)
135 && (data.data[0] != '\0')) {
136 ldb_msg_add_value(msg, "data", &data, NULL);
137 } else {
138 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
140 break;
143 type_s = talloc_asprintf(mem_ctx, "%u", type);
144 ldb_msg_add_string(msg, "type", type_s);
146 return msg;
149 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
151 struct ldb_val val;
153 val.data = discard_const_p(uint8_t, value);
154 val.length = strlen(value);
156 return ldb_dn_escape_value(mem_ctx, val);
159 static int reg_close_ldb_key(struct ldb_key_data *key)
161 if (key->subkeys != NULL) {
162 talloc_free(key->subkeys);
163 key->subkeys = NULL;
166 if (key->values != NULL) {
167 talloc_free(key->values);
168 key->values = NULL;
170 return 0;
173 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
174 const struct hive_key *from,
175 const char *path, const char *add)
177 TALLOC_CTX *local_ctx;
178 struct ldb_dn *ret;
179 char *mypath = talloc_strdup(mem_ctx, path);
180 char *begin;
181 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
182 struct ldb_context *ldb = kd->ldb;
184 local_ctx = talloc_new(mem_ctx);
186 ret = ldb_dn_new(mem_ctx, ldb, add);
187 if (!ldb_dn_validate(ret)) {
188 talloc_free(ret);
189 talloc_free(local_ctx);
190 return NULL;
193 while (mypath) {
194 char *keyname;
196 begin = strrchr(mypath, '\\');
198 if (begin) keyname = begin + 1;
199 else keyname = mypath;
201 if (keyname[0] != '\0') {
202 if (!ldb_dn_add_base_fmt(ret, "key=%s",
203 reg_ldb_escape(local_ctx,
204 keyname)))
206 talloc_free(local_ctx);
207 return NULL;
211 if(begin) {
212 *begin = '\0';
213 } else {
214 break;
218 ldb_dn_add_base(ret, kd->dn);
220 talloc_free(local_ctx);
222 return ret;
225 static WERROR cache_subkeys(struct ldb_key_data *kd)
227 struct ldb_context *c = kd->ldb;
228 struct ldb_result *res;
229 int ret;
231 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "(key=*)");
233 if (ret != LDB_SUCCESS) {
234 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
235 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
236 return WERR_FOOBAR;
239 kd->subkey_count = res->count;
240 kd->subkeys = talloc_steal(kd, res->msgs);
241 talloc_free(res);
243 return WERR_OK;
246 static WERROR cache_values(struct ldb_key_data *kd)
248 struct ldb_context *c = kd->ldb;
249 struct ldb_result *res;
250 int ret;
252 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
253 NULL, "(value=*)");
255 if (ret != LDB_SUCCESS) {
256 DEBUG(0, ("Error getting values for '%s': %s\n",
257 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
258 return WERR_FOOBAR;
261 kd->value_count = res->count;
262 kd->values = talloc_steal(kd, res->msgs);
263 talloc_free(res);
265 return WERR_OK;
269 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
270 const struct hive_key *k, uint32_t idx,
271 const char **name,
272 const char **classname,
273 NTTIME *last_mod_time)
275 struct ldb_message_element *el;
276 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
278 /* Initialization */
279 if (name != NULL)
280 *name = NULL;
281 if (classname != NULL)
282 *classname = NULL; /* TODO: Store properly */
283 if (last_mod_time != NULL)
284 *last_mod_time = 0; /* TODO: we need to add this to the
285 ldb backend properly */
287 /* Do a search if necessary */
288 if (kd->subkeys == NULL) {
289 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
292 if (idx >= kd->subkey_count)
293 return WERR_NO_MORE_ITEMS;
295 el = ldb_msg_find_element(kd->subkeys[idx], "key");
296 SMB_ASSERT(el != NULL);
297 SMB_ASSERT(el->num_values != 0);
299 if (name != NULL)
300 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
302 return WERR_OK;
305 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
306 const char **name, uint32_t *data_type,
307 DATA_BLOB *data)
309 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
310 struct ldb_context *c = kd->ldb;
311 const char* attrs[] = { "data", "type", NULL };
312 struct ldb_result *res;
313 int ret;
315 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_BASE, attrs, "%s", "");
317 if (ret != LDB_SUCCESS) {
318 DEBUG(0, ("Error getting default value for '%s': %s\n",
319 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
320 return WERR_FOOBAR;
323 if (res->count == 0 || res->msgs[0]->num_elements == 0)
324 return WERR_BADFILE;
326 reg_ldb_unpack_value(mem_ctx,
327 res->msgs[0], name, data_type, data);
329 talloc_free(res);
331 return WERR_OK;
334 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
335 uint32_t idx, const char **name,
336 uint32_t *data_type, DATA_BLOB *data)
338 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
340 /* if default value exists, give it back */
341 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
342 data))) {
343 if (idx == 0)
344 return WERR_OK;
345 else
346 --idx;
349 /* Do the search if necessary */
350 if (kd->values == NULL) {
351 W_ERROR_NOT_OK_RETURN(cache_values(kd));
354 if (idx >= kd->value_count)
355 return WERR_NO_MORE_ITEMS;
357 reg_ldb_unpack_value(mem_ctx, kd->values[idx], name, data_type, data);
359 return WERR_OK;
362 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
363 const char *name, uint32_t *data_type,
364 DATA_BLOB *data)
366 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
367 struct ldb_context *c = kd->ldb;
368 struct ldb_result *res;
369 int ret;
370 char *query;
372 if ((name == NULL) || (name[0] == '\0')) {
373 /* default value */
374 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
375 } else {
376 /* normal value */
377 query = talloc_asprintf(mem_ctx, "(value=%s)", name);
378 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "%s", query);
379 talloc_free(query);
381 if (ret != LDB_SUCCESS) {
382 DEBUG(0, ("Error getting values for '%s': %s\n",
383 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
384 return WERR_FOOBAR;
387 if (res->count == 0)
388 return WERR_BADFILE;
390 reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
392 talloc_free(res);
395 return WERR_OK;
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;
403 int ret;
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)));
415 return WERR_FOOBAR;
416 } else if (res->count == 0) {
417 DEBUG(3, ("Key '%s' not found\n",
418 ldb_dn_get_linearized(ldap_path)));
419 talloc_free(res);
420 return WERR_BADFILE;
423 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
424 newkd->key.ops = &reg_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;
430 return WERR_OK;
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 tevent_context *ev_ctx,
437 struct loadparm_context *lp_ctx,
438 struct hive_key **k)
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);
450 if (wrap == NULL) {
451 DEBUG(1, (__FILE__": unable to connect\n"));
452 return WERR_FOOBAR;
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 = &reg_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;
474 return WERR_OK;
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;
485 int ret;
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)));
503 return WERR_FOOBAR;
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 = &reg_backend_ldb;
511 newkd->dn = talloc_steal(newkd, msg->dn);
513 *newkey = (struct hive_key *)newkd;
515 /* reset cache */
516 talloc_free(parentkd->subkeys);
517 parentkd->subkeys = NULL;
519 return WERR_OK;
522 static WERROR ldb_del_value (struct hive_key *key, const char *child)
524 int ret;
525 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
526 TALLOC_CTX *mem_ctx;
527 struct ldb_message *msg;
528 struct ldb_dn *childdn;
530 if ((child == NULL) || (child[0] == '\0')) {
531 /* default value */
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);
543 return WERR_FOOBAR;
546 talloc_free(mem_ctx);
547 } else {
548 /* normal value */
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);
554 return WERR_FOOBAR;
557 ret = ldb_delete(kd->ldb, childdn);
559 talloc_free(childdn);
561 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
562 return WERR_BADFILE;
563 } else if (ret != LDB_SUCCESS) {
564 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
565 return WERR_FOOBAR;
569 /* reset cache */
570 talloc_free(kd->values);
571 kd->values = NULL;
573 return WERR_OK;
576 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
578 unsigned int i;
579 int ret;
580 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
581 struct ldb_dn *ldap_path;
582 TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
583 struct ldb_context *c = parentkd->ldb;
584 struct ldb_result *res_keys;
585 struct ldb_result *res_vals;
586 WERROR werr;
587 struct hive_key *hk;
589 /* Verify key exists by opening it */
590 werr = ldb_open_key(mem_ctx, key, name, &hk);
591 if (!W_ERROR_IS_OK(werr)) {
592 talloc_free(mem_ctx);
593 return werr;
596 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
597 if (!ldap_path) {
598 talloc_free(mem_ctx);
599 return WERR_FOOBAR;
602 /* Search for subkeys */
603 ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
604 NULL, "(key=*)");
606 if (ret != LDB_SUCCESS) {
607 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
608 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
609 talloc_free(mem_ctx);
610 return WERR_FOOBAR;
613 /* Search for values */
614 ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
615 NULL, "(value=*)");
617 if (ret != LDB_SUCCESS) {
618 DEBUG(0, ("Error getting values for '%s': %s\n",
619 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
620 talloc_free(mem_ctx);
621 return WERR_FOOBAR;
624 /* Start an explicit transaction */
625 ret = ldb_transaction_start(c);
627 if (ret != LDB_SUCCESS) {
628 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
629 talloc_free(mem_ctx);
630 return WERR_FOOBAR;
633 if (res_keys->count || res_vals->count)
635 /* Delete any subkeys */
636 for (i = 0; i < res_keys->count; i++)
638 werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
639 res_keys->msgs[i],
640 "key", NULL));
641 if (!W_ERROR_IS_OK(werr)) {
642 ret = ldb_transaction_cancel(c);
643 talloc_free(mem_ctx);
644 return werr;
648 /* Delete any values */
649 for (i = 0; i < res_vals->count; i++)
651 werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
652 res_vals->msgs[i],
653 "value", NULL));
654 if (!W_ERROR_IS_OK(werr)) {
655 ret = ldb_transaction_cancel(c);
656 talloc_free(mem_ctx);
657 return werr;
662 /* Delete the key itself */
663 ret = ldb_delete(c, ldap_path);
665 if (ret != LDB_SUCCESS)
667 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
668 ret = ldb_transaction_cancel(c);
669 talloc_free(mem_ctx);
670 return WERR_FOOBAR;
673 /* Commit the transaction */
674 ret = ldb_transaction_commit(c);
676 if (ret != LDB_SUCCESS)
678 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
679 ret = ldb_transaction_cancel(c);
680 talloc_free(mem_ctx);
681 return WERR_FOOBAR;
684 talloc_free(mem_ctx);
686 /* reset cache */
687 talloc_free(parentkd->subkeys);
688 parentkd->subkeys = NULL;
690 return WERR_OK;
693 static WERROR ldb_set_value(struct hive_key *parent,
694 const char *name, uint32_t type,
695 const DATA_BLOB data)
697 struct ldb_message *msg;
698 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
699 unsigned int i;
700 int ret;
701 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
703 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
704 msg->dn = ldb_dn_copy(msg, kd->dn);
706 if ((name != NULL) && (name[0] != '\0')) {
707 /* For a default value, we add/overwrite the attributes to/of the hive.
708 For a normal value, we create a new child. */
709 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
710 reg_ldb_escape(mem_ctx, name)))
712 talloc_free(mem_ctx);
713 return WERR_FOOBAR;
717 /* Try first a "modify" and if this doesn't work do try an "add" */
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;
723 ret = ldb_modify(kd->ldb, msg);
724 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
725 i = 0;
726 while (i < msg->num_elements) {
727 if (msg->elements[i].flags == LDB_FLAG_MOD_DELETE) {
728 ldb_msg_remove_element(msg, &msg->elements[i]);
729 } else {
730 ++i;
733 ret = ldb_add(kd->ldb, msg);
735 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
736 /* ignore this -> the value didn't exist and also now doesn't */
737 ret = LDB_SUCCESS;
740 if (ret != LDB_SUCCESS) {
741 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
742 talloc_free(mem_ctx);
743 return WERR_FOOBAR;
746 /* reset cache */
747 talloc_free(kd->values);
748 kd->values = NULL;
750 talloc_free(mem_ctx);
751 return WERR_OK;
754 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
755 const struct hive_key *key,
756 const char **classname,
757 uint32_t *num_subkeys,
758 uint32_t *num_values,
759 NTTIME *last_change_time,
760 uint32_t *max_subkeynamelen,
761 uint32_t *max_valnamelen,
762 uint32_t *max_valbufsize)
764 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
766 /* Initialization */
767 if (classname != NULL)
768 *classname = NULL;
769 if (num_subkeys != NULL)
770 *num_subkeys = 0;
771 if (num_values != NULL)
772 *num_values = 0;
773 if (last_change_time != NULL)
774 *last_change_time = 0;
775 if (max_subkeynamelen != NULL)
776 *max_subkeynamelen = 0;
777 if (max_valnamelen != NULL)
778 *max_valnamelen = 0;
779 if (max_valbufsize != NULL)
780 *max_valbufsize = 0;
782 if (kd->subkeys == NULL) {
783 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
786 if (kd->values == NULL) {
787 W_ERROR_NOT_OK_RETURN(cache_values(kd));
790 if (num_subkeys != NULL) {
791 *num_subkeys = kd->subkey_count;
793 if (num_values != NULL) {
794 *num_values = kd->value_count;
798 if (max_subkeynamelen != NULL) {
799 unsigned int i;
800 struct ldb_message_element *el;
802 *max_subkeynamelen = 0;
804 for (i = 0; i < kd->subkey_count; i++) {
805 el = ldb_msg_find_element(kd->subkeys[i], "key");
806 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
810 if (max_valnamelen != NULL || max_valbufsize != NULL) {
811 unsigned int i;
812 struct ldb_message_element *el;
813 W_ERROR_NOT_OK_RETURN(cache_values(kd));
815 if (max_valbufsize != NULL)
816 *max_valbufsize = 0;
818 if (max_valnamelen != NULL)
819 *max_valnamelen = 0;
821 for (i = 0; i < kd->value_count; i++) {
822 if (max_valnamelen != NULL) {
823 el = ldb_msg_find_element(kd->values[i], "value");
824 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
827 if (max_valbufsize != NULL) {
828 uint32_t data_type;
829 DATA_BLOB data;
830 reg_ldb_unpack_value(mem_ctx,
831 kd->values[i], NULL,
832 &data_type, &data);
833 *max_valbufsize = MAX(*max_valbufsize, data.length);
834 talloc_free(data.data);
839 return WERR_OK;
842 static struct hive_operations reg_backend_ldb = {
843 .name = "ldb",
844 .add_key = ldb_add_key,
845 .del_key = ldb_del_key,
846 .get_key_by_name = ldb_open_key,
847 .enum_value = ldb_get_value_by_id,
848 .enum_key = ldb_get_subkey_by_id,
849 .set_value = ldb_set_value,
850 .get_value_by_name = ldb_get_value,
851 .delete_value = ldb_del_value,
852 .get_key_info = ldb_get_key_info,