s4:registry - ldb.c - Consider result values in "reg_ldb_pack_value"
[Samba/nascimento.git] / source4 / lib / registry / ldb.c
blob0fab8472a9d8d47c85fd9616bff2bc162eebc16c
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_message *msg;
102 char *name_dup, *type_str;
103 int ret;
105 msg = talloc_zero(mem_ctx, struct ldb_message);
106 if (msg == NULL) {
107 return NULL;
110 name_dup = talloc_strdup(msg, name);
111 if (name_dup == NULL) {
112 talloc_free(msg);
113 return NULL;
116 ret = ldb_msg_add_string(msg, "value", name_dup);
117 if (ret != LDB_SUCCESS) {
118 talloc_free(msg);
119 return NULL;
122 switch (type) {
123 case REG_SZ:
124 case REG_EXPAND_SZ:
125 if ((data.length > 0) && (data.data != NULL)
126 && (data.data[0] != '\0')) {
127 struct ldb_val *val;
128 bool ret2;
130 val = talloc_zero(msg, struct ldb_val);
131 if (val == NULL) {
132 talloc_free(msg);
133 return NULL;
136 ret2 = convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8,
137 (void *)data.data, data.length,
138 (void **)&val->data, &val->length,
139 false);
140 ret = ldb_msg_add_value(msg, "data", val, NULL);
141 } else {
142 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
144 break;
146 case REG_DWORD:
147 if ((data.length > 0) && (data.data != NULL)) {
148 char *conv_str;
150 conv_str = talloc_asprintf(msg, "0x%x", IVAL(data.data, 0));
151 if (conv_str == NULL) {
152 talloc_free(msg);
153 return NULL;
156 ret = ldb_msg_add_string(msg, "data", conv_str);
157 } else {
158 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
160 break;
162 case REG_BINARY:
163 default:
164 if ((data.length > 0) && (data.data != NULL)
165 && (data.data[0] != '\0')) {
166 ret = ldb_msg_add_value(msg, "data", &data, NULL);
167 } else {
168 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
170 break;
173 if (ret != LDB_SUCCESS) {
174 talloc_free(msg);
175 return NULL;
178 type_str = talloc_asprintf(mem_ctx, "%u", type);
179 if (type_str == NULL) {
180 talloc_free(msg);
181 return NULL;
184 ret = ldb_msg_add_string(msg, "type", type_str);
185 if (ret != LDB_SUCCESS) {
186 talloc_free(msg);
187 return NULL;
190 return msg;
193 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
195 struct ldb_val val;
197 val.data = discard_const_p(uint8_t, value);
198 val.length = strlen(value);
200 return ldb_dn_escape_value(mem_ctx, val);
203 static int reg_close_ldb_key(struct ldb_key_data *key)
205 if (key->subkeys != NULL) {
206 talloc_free(key->subkeys);
207 key->subkeys = NULL;
210 if (key->values != NULL) {
211 talloc_free(key->values);
212 key->values = NULL;
214 return 0;
217 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
218 const struct hive_key *from,
219 const char *path, const char *add)
221 TALLOC_CTX *local_ctx;
222 struct ldb_dn *ret;
223 char *mypath = talloc_strdup(mem_ctx, path);
224 char *begin;
225 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
226 struct ldb_context *ldb = kd->ldb;
228 local_ctx = talloc_new(mem_ctx);
230 ret = ldb_dn_new(mem_ctx, ldb, add);
231 if (!ldb_dn_validate(ret)) {
232 talloc_free(ret);
233 talloc_free(local_ctx);
234 return NULL;
237 while (mypath) {
238 char *keyname;
240 begin = strrchr(mypath, '\\');
242 if (begin) keyname = begin + 1;
243 else keyname = mypath;
245 if (keyname[0] != '\0') {
246 if (!ldb_dn_add_base_fmt(ret, "key=%s",
247 reg_ldb_escape(local_ctx,
248 keyname)))
250 talloc_free(local_ctx);
251 return NULL;
255 if(begin) {
256 *begin = '\0';
257 } else {
258 break;
262 ldb_dn_add_base(ret, kd->dn);
264 talloc_free(local_ctx);
266 return ret;
269 static WERROR cache_subkeys(struct ldb_key_data *kd)
271 struct ldb_context *c = kd->ldb;
272 struct ldb_result *res;
273 int ret;
275 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "(key=*)");
277 if (ret != LDB_SUCCESS) {
278 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
279 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
280 return WERR_FOOBAR;
283 kd->subkey_count = res->count;
284 kd->subkeys = talloc_steal(kd, res->msgs);
285 talloc_free(res);
287 return WERR_OK;
290 static WERROR cache_values(struct ldb_key_data *kd)
292 struct ldb_context *c = kd->ldb;
293 struct ldb_result *res;
294 int ret;
296 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
297 NULL, "(value=*)");
299 if (ret != LDB_SUCCESS) {
300 DEBUG(0, ("Error getting values for '%s': %s\n",
301 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
302 return WERR_FOOBAR;
305 kd->value_count = res->count;
306 kd->values = talloc_steal(kd, res->msgs);
307 talloc_free(res);
309 return WERR_OK;
313 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
314 const struct hive_key *k, uint32_t idx,
315 const char **name,
316 const char **classname,
317 NTTIME *last_mod_time)
319 struct ldb_message_element *el;
320 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
322 /* Initialization */
323 if (name != NULL)
324 *name = NULL;
325 if (classname != NULL)
326 *classname = NULL; /* TODO: Store properly */
327 if (last_mod_time != NULL)
328 *last_mod_time = 0; /* TODO: we need to add this to the
329 ldb backend properly */
331 /* Do a search if necessary */
332 if (kd->subkeys == NULL) {
333 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
336 if (idx >= kd->subkey_count)
337 return WERR_NO_MORE_ITEMS;
339 el = ldb_msg_find_element(kd->subkeys[idx], "key");
340 SMB_ASSERT(el != NULL);
341 SMB_ASSERT(el->num_values != 0);
343 if (name != NULL)
344 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
346 return WERR_OK;
349 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
350 const char **name, uint32_t *data_type,
351 DATA_BLOB *data)
353 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
354 struct ldb_context *c = kd->ldb;
355 const char* attrs[] = { "data", "type", NULL };
356 struct ldb_result *res;
357 int ret;
359 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_BASE, attrs, "%s", "");
361 if (ret != LDB_SUCCESS) {
362 DEBUG(0, ("Error getting default value for '%s': %s\n",
363 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
364 return WERR_FOOBAR;
367 if (res->count == 0 || res->msgs[0]->num_elements == 0)
368 return WERR_BADFILE;
370 reg_ldb_unpack_value(mem_ctx,
371 res->msgs[0], name, data_type, data);
373 talloc_free(res);
375 return WERR_OK;
378 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
379 uint32_t idx, const char **name,
380 uint32_t *data_type, DATA_BLOB *data)
382 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
384 /* if default value exists, give it back */
385 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
386 data))) {
387 if (idx == 0)
388 return WERR_OK;
389 else
390 --idx;
393 /* Do the search if necessary */
394 if (kd->values == NULL) {
395 W_ERROR_NOT_OK_RETURN(cache_values(kd));
398 if (idx >= kd->value_count)
399 return WERR_NO_MORE_ITEMS;
401 reg_ldb_unpack_value(mem_ctx, kd->values[idx], name, data_type, data);
403 return WERR_OK;
406 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
407 const char *name, uint32_t *data_type,
408 DATA_BLOB *data)
410 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
411 struct ldb_context *c = kd->ldb;
412 struct ldb_result *res;
413 int ret;
414 char *query;
416 if (name == NULL) {
417 return WERR_INVALID_PARAM;
420 if (name[0] == '\0') {
421 /* default value */
422 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
423 } else {
424 /* normal value */
425 query = talloc_asprintf(mem_ctx, "(value=%s)", name);
426 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "%s", query);
427 talloc_free(query);
429 if (ret != LDB_SUCCESS) {
430 DEBUG(0, ("Error getting values for '%s': %s\n",
431 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
432 return WERR_FOOBAR;
435 if (res->count == 0)
436 return WERR_BADFILE;
438 reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
440 talloc_free(res);
443 return WERR_OK;
446 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
447 const char *name, struct hive_key **key)
449 struct ldb_result *res;
450 struct ldb_dn *ldap_path;
451 int ret;
452 struct ldb_key_data *newkd;
453 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
454 struct ldb_context *c = kd->ldb;
456 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
458 ret = ldb_search(c, mem_ctx, &res, ldap_path, LDB_SCOPE_BASE, NULL, "(key=*)");
460 if (ret != LDB_SUCCESS) {
461 DEBUG(3, ("Error opening key '%s': %s\n",
462 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
463 return WERR_FOOBAR;
464 } else if (res->count == 0) {
465 DEBUG(3, ("Key '%s' not found\n",
466 ldb_dn_get_linearized(ldap_path)));
467 talloc_free(res);
468 return WERR_BADFILE;
471 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
472 newkd->key.ops = &reg_backend_ldb;
473 newkd->ldb = talloc_reference(newkd, kd->ldb);
474 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
476 *key = (struct hive_key *)newkd;
478 return WERR_OK;
481 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
482 struct auth_session_info *session_info,
483 struct cli_credentials *credentials,
484 struct tevent_context *ev_ctx,
485 struct loadparm_context *lp_ctx,
486 struct hive_key **k)
488 struct ldb_key_data *kd;
489 struct ldb_context *wrap;
490 struct ldb_message *attrs_msg;
492 if (location == NULL)
493 return WERR_INVALID_PARAM;
495 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
496 location, session_info, credentials, 0);
498 if (wrap == NULL) {
499 DEBUG(1, (__FILE__": unable to connect\n"));
500 return WERR_FOOBAR;
503 attrs_msg = ldb_msg_new(wrap);
504 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
505 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
506 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
507 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
508 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
510 ldb_add(wrap, attrs_msg);
512 ldb_set_debug_stderr(wrap);
514 kd = talloc_zero(parent_ctx, struct ldb_key_data);
515 kd->key.ops = &reg_backend_ldb;
516 kd->ldb = talloc_reference(kd, wrap);
517 talloc_set_destructor (kd, reg_close_ldb_key);
518 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
520 *k = (struct hive_key *)kd;
522 return WERR_OK;
525 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
526 const char *name, const char *classname,
527 struct security_descriptor *sd,
528 struct hive_key **newkey)
530 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
531 struct ldb_message *msg;
532 struct ldb_key_data *newkd;
533 int ret;
535 msg = ldb_msg_new(mem_ctx);
537 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
539 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
540 if (classname != NULL)
541 ldb_msg_add_string(msg, "classname",
542 talloc_strdup(mem_ctx, classname));
544 ret = ldb_add(parentkd->ldb, msg);
545 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
546 return WERR_ALREADY_EXISTS;
549 if (ret != LDB_SUCCESS) {
550 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
551 return WERR_FOOBAR;
554 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
556 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
557 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
558 newkd->key.ops = &reg_backend_ldb;
559 newkd->dn = talloc_steal(newkd, msg->dn);
561 *newkey = (struct hive_key *)newkd;
563 /* reset cache */
564 talloc_free(parentkd->subkeys);
565 parentkd->subkeys = NULL;
567 return WERR_OK;
570 static WERROR ldb_del_value (struct hive_key *key, const char *child)
572 int ret;
573 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
574 TALLOC_CTX *mem_ctx;
575 struct ldb_message *msg;
576 struct ldb_dn *childdn;
578 if ((child == NULL) || (child[0] == '\0')) {
579 /* default value */
580 mem_ctx = talloc_init("ldb_del_value");
582 msg = talloc_zero(mem_ctx, struct ldb_message);
583 msg->dn = ldb_dn_copy(msg, kd->dn);
584 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
585 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
587 ret = ldb_modify(kd->ldb, msg);
588 if (ret != LDB_SUCCESS) {
589 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
590 talloc_free(mem_ctx);
591 return WERR_FOOBAR;
594 talloc_free(mem_ctx);
595 } else {
596 /* normal value */
597 childdn = ldb_dn_copy(kd->ldb, kd->dn);
598 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
599 reg_ldb_escape(childdn, child)))
601 talloc_free(childdn);
602 return WERR_FOOBAR;
605 ret = ldb_delete(kd->ldb, childdn);
607 talloc_free(childdn);
609 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
610 return WERR_BADFILE;
611 } else if (ret != LDB_SUCCESS) {
612 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
613 return WERR_FOOBAR;
617 /* reset cache */
618 talloc_free(kd->values);
619 kd->values = NULL;
621 return WERR_OK;
624 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
626 unsigned int i;
627 int ret;
628 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
629 struct ldb_dn *ldap_path;
630 TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
631 struct ldb_context *c = parentkd->ldb;
632 struct ldb_result *res_keys;
633 struct ldb_result *res_vals;
634 WERROR werr;
635 struct hive_key *hk;
637 /* Verify key exists by opening it */
638 werr = ldb_open_key(mem_ctx, key, name, &hk);
639 if (!W_ERROR_IS_OK(werr)) {
640 talloc_free(mem_ctx);
641 return werr;
644 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
645 if (!ldap_path) {
646 talloc_free(mem_ctx);
647 return WERR_FOOBAR;
650 /* Search for subkeys */
651 ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
652 NULL, "(key=*)");
654 if (ret != LDB_SUCCESS) {
655 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
656 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
657 talloc_free(mem_ctx);
658 return WERR_FOOBAR;
661 /* Search for values */
662 ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
663 NULL, "(value=*)");
665 if (ret != LDB_SUCCESS) {
666 DEBUG(0, ("Error getting values for '%s': %s\n",
667 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
668 talloc_free(mem_ctx);
669 return WERR_FOOBAR;
672 /* Start an explicit transaction */
673 ret = ldb_transaction_start(c);
675 if (ret != LDB_SUCCESS) {
676 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
677 talloc_free(mem_ctx);
678 return WERR_FOOBAR;
681 if (res_keys->count || res_vals->count)
683 /* Delete any subkeys */
684 for (i = 0; i < res_keys->count; i++)
686 werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
687 res_keys->msgs[i],
688 "key", NULL));
689 if (!W_ERROR_IS_OK(werr)) {
690 ret = ldb_transaction_cancel(c);
691 talloc_free(mem_ctx);
692 return werr;
696 /* Delete any values */
697 for (i = 0; i < res_vals->count; i++)
699 werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
700 res_vals->msgs[i],
701 "value", NULL));
702 if (!W_ERROR_IS_OK(werr)) {
703 ret = ldb_transaction_cancel(c);
704 talloc_free(mem_ctx);
705 return werr;
710 /* Delete the key itself */
711 ret = ldb_delete(c, ldap_path);
713 if (ret != LDB_SUCCESS)
715 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
716 ret = ldb_transaction_cancel(c);
717 talloc_free(mem_ctx);
718 return WERR_FOOBAR;
721 /* Commit the transaction */
722 ret = ldb_transaction_commit(c);
724 if (ret != LDB_SUCCESS)
726 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
727 ret = ldb_transaction_cancel(c);
728 talloc_free(mem_ctx);
729 return WERR_FOOBAR;
732 talloc_free(mem_ctx);
734 /* reset cache */
735 talloc_free(parentkd->subkeys);
736 parentkd->subkeys = NULL;
738 return WERR_OK;
741 static WERROR ldb_set_value(struct hive_key *parent,
742 const char *name, uint32_t type,
743 const DATA_BLOB data)
745 struct ldb_message *msg;
746 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
747 unsigned int i;
748 int ret;
749 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
751 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
752 msg->dn = ldb_dn_copy(msg, kd->dn);
754 if ((name != NULL) && (name[0] != '\0')) {
755 /* For a default value, we add/overwrite the attributes to/of the hive.
756 For a normal value, we create a new child. */
757 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
758 reg_ldb_escape(mem_ctx, name)))
760 talloc_free(mem_ctx);
761 return WERR_FOOBAR;
765 /* Try first a "modify" and if this doesn't work do try an "add" */
766 for (i = 0; i < msg->num_elements; i++) {
767 if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE) {
768 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
771 ret = ldb_modify(kd->ldb, msg);
772 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
773 i = 0;
774 while (i < msg->num_elements) {
775 if (msg->elements[i].flags == LDB_FLAG_MOD_DELETE) {
776 ldb_msg_remove_element(msg, &msg->elements[i]);
777 } else {
778 ++i;
781 ret = ldb_add(kd->ldb, msg);
783 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
784 /* ignore this -> the value didn't exist and also now doesn't */
785 ret = LDB_SUCCESS;
788 if (ret != LDB_SUCCESS) {
789 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
790 talloc_free(mem_ctx);
791 return WERR_FOOBAR;
794 /* reset cache */
795 talloc_free(kd->values);
796 kd->values = NULL;
798 talloc_free(mem_ctx);
799 return WERR_OK;
802 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
803 const struct hive_key *key,
804 const char **classname,
805 uint32_t *num_subkeys,
806 uint32_t *num_values,
807 NTTIME *last_change_time,
808 uint32_t *max_subkeynamelen,
809 uint32_t *max_valnamelen,
810 uint32_t *max_valbufsize)
812 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
814 /* Initialization */
815 if (classname != NULL)
816 *classname = NULL;
817 if (num_subkeys != NULL)
818 *num_subkeys = 0;
819 if (num_values != NULL)
820 *num_values = 0;
821 if (last_change_time != NULL)
822 *last_change_time = 0;
823 if (max_subkeynamelen != NULL)
824 *max_subkeynamelen = 0;
825 if (max_valnamelen != NULL)
826 *max_valnamelen = 0;
827 if (max_valbufsize != NULL)
828 *max_valbufsize = 0;
830 if (kd->subkeys == NULL) {
831 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
834 if (kd->values == NULL) {
835 W_ERROR_NOT_OK_RETURN(cache_values(kd));
838 if (num_subkeys != NULL) {
839 *num_subkeys = kd->subkey_count;
841 if (num_values != NULL) {
842 *num_values = kd->value_count;
846 if (max_subkeynamelen != NULL) {
847 unsigned int i;
848 struct ldb_message_element *el;
850 *max_subkeynamelen = 0;
852 for (i = 0; i < kd->subkey_count; i++) {
853 el = ldb_msg_find_element(kd->subkeys[i], "key");
854 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
858 if (max_valnamelen != NULL || max_valbufsize != NULL) {
859 unsigned int i;
860 struct ldb_message_element *el;
861 W_ERROR_NOT_OK_RETURN(cache_values(kd));
863 if (max_valbufsize != NULL)
864 *max_valbufsize = 0;
866 if (max_valnamelen != NULL)
867 *max_valnamelen = 0;
869 for (i = 0; i < kd->value_count; i++) {
870 if (max_valnamelen != NULL) {
871 el = ldb_msg_find_element(kd->values[i], "value");
872 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
875 if (max_valbufsize != NULL) {
876 uint32_t data_type;
877 DATA_BLOB data;
878 reg_ldb_unpack_value(mem_ctx,
879 kd->values[i], NULL,
880 &data_type, &data);
881 *max_valbufsize = MAX(*max_valbufsize, data.length);
882 talloc_free(data.data);
887 return WERR_OK;
890 static struct hive_operations reg_backend_ldb = {
891 .name = "ldb",
892 .add_key = ldb_add_key,
893 .del_key = ldb_del_key,
894 .get_key_by_name = ldb_open_key,
895 .enum_value = ldb_get_value_by_id,
896 .enum_key = ldb_get_subkey_by_id,
897 .set_value = ldb_set_value,
898 .get_value_by_name = ldb_get_value,
899 .delete_value = ldb_del_value,
900 .get_key_info = ldb_get_key_info,