s4:registry - ldb.c - check more for possible "Out of memory" circumstances
[Samba/nascimento.git] / source4 / lib / registry / ldb.c
blobe168d8fd02a075b6b74a69fc1713739a0ee1ad4b
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;
415 if (name == NULL) {
416 return WERR_INVALID_PARAM;
419 if (name[0] == '\0') {
420 /* default value */
421 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
422 } else {
423 /* normal value */
424 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_ONELEVEL,
425 NULL, "(value=%s)", name);
427 if (ret != LDB_SUCCESS) {
428 DEBUG(0, ("Error getting values for '%s': %s\n",
429 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
430 return WERR_FOOBAR;
433 if (res->count == 0)
434 return WERR_BADFILE;
436 reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
438 talloc_free(res);
441 return WERR_OK;
444 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
445 const char *name, struct hive_key **key)
447 struct ldb_result *res;
448 struct ldb_dn *ldap_path;
449 int ret;
450 struct ldb_key_data *newkd;
451 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
452 struct ldb_context *c = kd->ldb;
454 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
455 W_ERROR_HAVE_NO_MEMORY(ldap_path);
457 ret = ldb_search(c, mem_ctx, &res, ldap_path, LDB_SCOPE_BASE, NULL, "(key=*)");
459 if (ret != LDB_SUCCESS) {
460 DEBUG(3, ("Error opening key '%s': %s\n",
461 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
462 return WERR_FOOBAR;
463 } else if (res->count == 0) {
464 DEBUG(3, ("Key '%s' not found\n",
465 ldb_dn_get_linearized(ldap_path)));
466 talloc_free(res);
467 return WERR_BADFILE;
470 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
471 newkd->key.ops = &reg_backend_ldb;
472 newkd->ldb = talloc_reference(newkd, kd->ldb);
473 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
475 *key = (struct hive_key *)newkd;
477 return WERR_OK;
480 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
481 struct auth_session_info *session_info,
482 struct cli_credentials *credentials,
483 struct tevent_context *ev_ctx,
484 struct loadparm_context *lp_ctx,
485 struct hive_key **k)
487 struct ldb_key_data *kd;
488 struct ldb_context *wrap;
489 struct ldb_message *attrs_msg;
491 if (location == NULL)
492 return WERR_INVALID_PARAM;
494 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
495 location, session_info, credentials, 0);
497 if (wrap == NULL) {
498 DEBUG(1, (__FILE__": unable to connect\n"));
499 return WERR_FOOBAR;
502 attrs_msg = ldb_msg_new(wrap);
503 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
504 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
505 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
506 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
507 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
509 ldb_add(wrap, attrs_msg);
511 ldb_set_debug_stderr(wrap);
513 kd = talloc_zero(parent_ctx, struct ldb_key_data);
514 kd->key.ops = &reg_backend_ldb;
515 kd->ldb = talloc_reference(kd, wrap);
516 talloc_set_destructor (kd, reg_close_ldb_key);
517 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
519 *k = (struct hive_key *)kd;
521 return WERR_OK;
524 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
525 const char *name, const char *classname,
526 struct security_descriptor *sd,
527 struct hive_key **newkey)
529 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
530 struct ldb_message *msg;
531 struct ldb_key_data *newkd;
532 int ret;
534 msg = ldb_msg_new(mem_ctx);
535 W_ERROR_HAVE_NO_MEMORY(msg);
537 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
538 W_ERROR_HAVE_NO_MEMORY(msg->dn);
540 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
541 if (classname != NULL)
542 ldb_msg_add_string(msg, "classname",
543 talloc_strdup(mem_ctx, classname));
545 ret = ldb_add(parentkd->ldb, msg);
546 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
547 return WERR_ALREADY_EXISTS;
550 if (ret != LDB_SUCCESS) {
551 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
552 return WERR_FOOBAR;
555 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
557 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
558 W_ERROR_HAVE_NO_MEMORY(newkd);
559 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
560 newkd->key.ops = &reg_backend_ldb;
561 newkd->dn = talloc_steal(newkd, msg->dn);
563 *newkey = (struct hive_key *)newkd;
565 /* reset cache */
566 talloc_free(parentkd->subkeys);
567 parentkd->subkeys = NULL;
569 return WERR_OK;
572 static WERROR ldb_del_value (struct hive_key *key, const char *child)
574 int ret;
575 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
576 TALLOC_CTX *mem_ctx;
577 struct ldb_message *msg;
578 struct ldb_dn *childdn;
580 if ((child == NULL) || (child[0] == '\0')) {
581 /* default value */
582 mem_ctx = talloc_init("ldb_del_value");
584 msg = talloc_zero(mem_ctx, struct ldb_message);
585 W_ERROR_HAVE_NO_MEMORY(msg);
586 msg->dn = ldb_dn_copy(msg, kd->dn);
587 W_ERROR_HAVE_NO_MEMORY(msg->dn);
588 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
589 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
591 ret = ldb_modify(kd->ldb, msg);
592 if (ret != LDB_SUCCESS) {
593 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
594 talloc_free(mem_ctx);
595 return WERR_FOOBAR;
598 talloc_free(mem_ctx);
599 } else {
600 /* normal value */
601 childdn = ldb_dn_copy(kd->ldb, kd->dn);
602 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
603 reg_ldb_escape(childdn, child)))
605 talloc_free(childdn);
606 return WERR_FOOBAR;
609 ret = ldb_delete(kd->ldb, childdn);
611 talloc_free(childdn);
613 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
614 return WERR_BADFILE;
615 } else if (ret != LDB_SUCCESS) {
616 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
617 return WERR_FOOBAR;
621 /* reset cache */
622 talloc_free(kd->values);
623 kd->values = NULL;
625 return WERR_OK;
628 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
630 unsigned int i;
631 int ret;
632 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
633 struct ldb_dn *ldap_path;
634 TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
635 struct ldb_context *c = parentkd->ldb;
636 struct ldb_result *res_keys;
637 struct ldb_result *res_vals;
638 WERROR werr;
639 struct hive_key *hk;
641 /* Verify key exists by opening it */
642 werr = ldb_open_key(mem_ctx, key, name, &hk);
643 if (!W_ERROR_IS_OK(werr)) {
644 talloc_free(mem_ctx);
645 return werr;
648 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
649 W_ERROR_HAVE_NO_MEMORY(ldap_path);
651 /* Search for subkeys */
652 ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
653 NULL, "(key=*)");
655 if (ret != LDB_SUCCESS) {
656 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
657 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
658 talloc_free(mem_ctx);
659 return WERR_FOOBAR;
662 /* Search for values */
663 ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
664 NULL, "(value=*)");
666 if (ret != LDB_SUCCESS) {
667 DEBUG(0, ("Error getting values for '%s': %s\n",
668 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
669 talloc_free(mem_ctx);
670 return WERR_FOOBAR;
673 /* Start an explicit transaction */
674 ret = ldb_transaction_start(c);
676 if (ret != LDB_SUCCESS) {
677 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
678 talloc_free(mem_ctx);
679 return WERR_FOOBAR;
682 if (res_keys->count || res_vals->count)
684 /* Delete any subkeys */
685 for (i = 0; i < res_keys->count; i++)
687 werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
688 res_keys->msgs[i],
689 "key", NULL));
690 if (!W_ERROR_IS_OK(werr)) {
691 ret = ldb_transaction_cancel(c);
692 talloc_free(mem_ctx);
693 return werr;
697 /* Delete any values */
698 for (i = 0; i < res_vals->count; i++)
700 werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
701 res_vals->msgs[i],
702 "value", NULL));
703 if (!W_ERROR_IS_OK(werr)) {
704 ret = ldb_transaction_cancel(c);
705 talloc_free(mem_ctx);
706 return werr;
711 /* Delete the key itself */
712 ret = ldb_delete(c, ldap_path);
714 if (ret != LDB_SUCCESS)
716 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
717 ret = ldb_transaction_cancel(c);
718 talloc_free(mem_ctx);
719 return WERR_FOOBAR;
722 /* Commit the transaction */
723 ret = ldb_transaction_commit(c);
725 if (ret != LDB_SUCCESS)
727 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
728 ret = ldb_transaction_cancel(c);
729 talloc_free(mem_ctx);
730 return WERR_FOOBAR;
733 talloc_free(mem_ctx);
735 /* reset cache */
736 talloc_free(parentkd->subkeys);
737 parentkd->subkeys = NULL;
739 return WERR_OK;
742 static WERROR ldb_set_value(struct hive_key *parent,
743 const char *name, uint32_t type,
744 const DATA_BLOB data)
746 struct ldb_message *msg;
747 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
748 unsigned int i;
749 int ret;
750 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
752 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
753 W_ERROR_HAVE_NO_MEMORY(msg);
755 msg->dn = ldb_dn_copy(msg, kd->dn);
756 W_ERROR_HAVE_NO_MEMORY(msg->dn);
758 if ((name != NULL) && (name[0] != '\0')) {
759 /* For a default value, we add/overwrite the attributes to/of the hive.
760 For a normal value, we create a new child. */
761 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
762 reg_ldb_escape(mem_ctx, name)))
764 talloc_free(mem_ctx);
765 return WERR_FOOBAR;
769 /* Try first a "modify" and if this doesn't work do try an "add" */
770 for (i = 0; i < msg->num_elements; i++) {
771 if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE) {
772 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
775 ret = ldb_modify(kd->ldb, msg);
776 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
777 i = 0;
778 while (i < msg->num_elements) {
779 if (msg->elements[i].flags == LDB_FLAG_MOD_DELETE) {
780 ldb_msg_remove_element(msg, &msg->elements[i]);
781 } else {
782 ++i;
785 ret = ldb_add(kd->ldb, msg);
787 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
788 /* ignore this -> the value didn't exist and also now doesn't */
789 ret = LDB_SUCCESS;
792 if (ret != LDB_SUCCESS) {
793 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
794 talloc_free(mem_ctx);
795 return WERR_FOOBAR;
798 /* reset cache */
799 talloc_free(kd->values);
800 kd->values = NULL;
802 talloc_free(mem_ctx);
803 return WERR_OK;
806 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
807 const struct hive_key *key,
808 const char **classname,
809 uint32_t *num_subkeys,
810 uint32_t *num_values,
811 NTTIME *last_change_time,
812 uint32_t *max_subkeynamelen,
813 uint32_t *max_valnamelen,
814 uint32_t *max_valbufsize)
816 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
818 /* Initialization */
819 if (classname != NULL)
820 *classname = NULL;
821 if (num_subkeys != NULL)
822 *num_subkeys = 0;
823 if (num_values != NULL)
824 *num_values = 0;
825 if (last_change_time != NULL)
826 *last_change_time = 0;
827 if (max_subkeynamelen != NULL)
828 *max_subkeynamelen = 0;
829 if (max_valnamelen != NULL)
830 *max_valnamelen = 0;
831 if (max_valbufsize != NULL)
832 *max_valbufsize = 0;
834 if (kd->subkeys == NULL) {
835 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
838 if (kd->values == NULL) {
839 W_ERROR_NOT_OK_RETURN(cache_values(kd));
842 if (num_subkeys != NULL) {
843 *num_subkeys = kd->subkey_count;
845 if (num_values != NULL) {
846 *num_values = kd->value_count;
850 if (max_subkeynamelen != NULL) {
851 unsigned int i;
852 struct ldb_message_element *el;
854 *max_subkeynamelen = 0;
856 for (i = 0; i < kd->subkey_count; i++) {
857 el = ldb_msg_find_element(kd->subkeys[i], "key");
858 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
862 if (max_valnamelen != NULL || max_valbufsize != NULL) {
863 unsigned int i;
864 struct ldb_message_element *el;
865 W_ERROR_NOT_OK_RETURN(cache_values(kd));
867 if (max_valbufsize != NULL)
868 *max_valbufsize = 0;
870 if (max_valnamelen != NULL)
871 *max_valnamelen = 0;
873 for (i = 0; i < kd->value_count; i++) {
874 if (max_valnamelen != NULL) {
875 el = ldb_msg_find_element(kd->values[i], "value");
876 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
879 if (max_valbufsize != NULL) {
880 uint32_t data_type;
881 DATA_BLOB data;
882 reg_ldb_unpack_value(mem_ctx,
883 kd->values[i], NULL,
884 &data_type, &data);
885 *max_valbufsize = MAX(*max_valbufsize, data.length);
886 talloc_free(data.data);
891 return WERR_OK;
894 static struct hive_operations reg_backend_ldb = {
895 .name = "ldb",
896 .add_key = ldb_add_key,
897 .del_key = ldb_del_key,
898 .get_key_by_name = ldb_open_key,
899 .enum_value = ldb_get_value_by_id,
900 .enum_key = ldb_get_subkey_by_id,
901 .set_value = ldb_set_value,
902 .get_value_by_name = ldb_get_value,
903 .delete_value = ldb_del_value,
904 .get_key_info = ldb_get_key_info,