s4:lib/ldb/registry.c - handle the classname in the right way
[Samba/gebeck_regimport.git] / source4 / lib / registry / ldb.c
blobb2b1e745f344c150d0762b72a0e9892c9758c33f
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;
38 const char *classname;
41 static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx,
42 struct ldb_message *msg,
43 const char **name, uint32_t *type,
44 DATA_BLOB *data)
46 const struct ldb_val *val;
47 uint32_t value_type;
49 if (name != NULL) {
50 *name = talloc_strdup(mem_ctx,
51 ldb_msg_find_attr_as_string(msg, "value",
52 ""));
55 value_type = ldb_msg_find_attr_as_uint(msg, "type", 0);
56 *type = value_type;
58 val = ldb_msg_find_ldb_val(msg, "data");
60 switch (value_type)
62 case REG_SZ:
63 case REG_EXPAND_SZ:
64 if (val != NULL) {
65 /* The data should be provided as UTF16 string */
66 convert_string_talloc(mem_ctx, CH_UTF8, CH_UTF16,
67 val->data, val->length,
68 (void **)&data->data, &data->length, false);
69 } else {
70 data->data = NULL;
71 data->length = 0;
73 break;
75 case REG_DWORD:
76 case REG_DWORD_BIG_ENDIAN:
77 if (val != NULL) {
78 /* The data is a plain DWORD */
79 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
80 data->data = talloc_size(mem_ctx, sizeof(uint32_t));
81 if (data->data != NULL) {
82 SIVAL(data->data, 0, tmp);
84 data->length = sizeof(uint32_t);
85 } else {
86 data->data = NULL;
87 data->length = 0;
89 break;
91 case REG_QWORD:
92 if (val != NULL) {
93 /* The data is a plain QWORD */
94 uint64_t tmp = strtoull((char *)val->data, NULL, 0);
95 data->data = talloc_size(mem_ctx, sizeof(uint64_t));
96 if (data->data != NULL) {
97 SBVAL(data->data, 0, tmp);
99 data->length = sizeof(uint64_t);
100 } else {
101 data->data = NULL;
102 data->length = 0;
104 break;
106 case REG_BINARY:
107 default:
108 if (val != NULL) {
109 data->data = talloc_memdup(mem_ctx, val->data,
110 val->length);
111 data->length = val->length;
112 } else {
113 data->data = NULL;
114 data->length = 0;
116 break;
120 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
121 TALLOC_CTX *mem_ctx,
122 const char *name,
123 uint32_t type, DATA_BLOB data)
125 struct ldb_message *msg;
126 char *name_dup, *type_str;
127 int ret;
129 msg = talloc_zero(mem_ctx, struct ldb_message);
130 if (msg == NULL) {
131 return NULL;
134 name_dup = talloc_strdup(msg, name);
135 if (name_dup == NULL) {
136 talloc_free(msg);
137 return NULL;
140 ret = ldb_msg_add_string(msg, "value", name_dup);
141 if (ret != LDB_SUCCESS) {
142 talloc_free(msg);
143 return NULL;
146 switch (type) {
147 case REG_SZ:
148 case REG_EXPAND_SZ:
149 if ((data.length > 0) && (data.data != NULL)) {
150 struct ldb_val *val;
151 bool ret2 = false;
153 val = talloc_zero(msg, struct ldb_val);
154 if (val == NULL) {
155 talloc_free(msg);
156 return NULL;
159 /* The data is provided as UTF16 string */
160 ret2 = convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8,
161 (void *)data.data, data.length,
162 (void **)&val->data, &val->length,
163 false);
164 if (ret2) {
165 ret = ldb_msg_add_value(msg, "data", val, NULL);
166 } else {
167 /* workaround for non-standard data */
168 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
170 } else {
171 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
173 break;
175 case REG_DWORD:
176 case REG_DWORD_BIG_ENDIAN:
177 if ((data.length > 0) && (data.data != NULL)) {
178 if (data.length == sizeof(uint32_t)) {
179 char *conv_str;
181 conv_str = talloc_asprintf(msg, "0x%8.8x",
182 IVAL(data.data, 0));
183 if (conv_str == NULL) {
184 talloc_free(msg);
185 return NULL;
187 ret = ldb_msg_add_string(msg, "data", conv_str);
188 } else {
189 /* workaround for non-standard data */
190 talloc_free(msg);
191 return NULL;
193 } else {
194 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
196 break;
198 case REG_QWORD:
199 if ((data.length > 0) && (data.data != NULL)) {
200 if (data.length == sizeof(uint64_t)) {
201 char *conv_str;
203 conv_str = talloc_asprintf(msg, "0x%16.16llx",
204 BVAL(data.data, 0));
205 if (conv_str == NULL) {
206 talloc_free(msg);
207 return NULL;
209 ret = ldb_msg_add_string(msg, "data", conv_str);
210 } else {
211 /* workaround for non-standard data */
212 talloc_free(msg);
213 return NULL;
216 } else {
217 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
219 break;
221 case REG_BINARY:
222 default:
223 if ((data.length > 0) && (data.data != NULL)) {
224 ret = ldb_msg_add_value(msg, "data", &data, NULL);
225 } else {
226 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
228 break;
231 if (ret != LDB_SUCCESS) {
232 talloc_free(msg);
233 return NULL;
236 type_str = talloc_asprintf(mem_ctx, "%u", type);
237 if (type_str == NULL) {
238 talloc_free(msg);
239 return NULL;
242 ret = ldb_msg_add_string(msg, "type", type_str);
243 if (ret != LDB_SUCCESS) {
244 talloc_free(msg);
245 return NULL;
248 return msg;
251 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
253 struct ldb_val val;
255 val.data = discard_const_p(uint8_t, value);
256 val.length = strlen(value);
258 return ldb_dn_escape_value(mem_ctx, val);
261 static int reg_close_ldb_key(struct ldb_key_data *key)
263 if (key->subkeys != NULL) {
264 talloc_free(key->subkeys);
265 key->subkeys = NULL;
268 if (key->values != NULL) {
269 talloc_free(key->values);
270 key->values = NULL;
272 return 0;
275 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
276 const struct hive_key *from,
277 const char *path, const char *add)
279 struct ldb_dn *ret;
280 char *mypath = talloc_strdup(mem_ctx, path);
281 char *begin;
282 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
283 struct ldb_context *ldb = kd->ldb;
285 ret = ldb_dn_new(mem_ctx, ldb, add);
286 if (!ldb_dn_validate(ret)) {
287 talloc_free(ret);
288 return NULL;
291 while (mypath) {
292 char *keyname;
294 begin = strrchr(mypath, '\\');
296 if (begin) keyname = begin + 1;
297 else keyname = mypath;
299 if (keyname[0] != '\0') {
300 if (!ldb_dn_add_base_fmt(ret, "key=%s",
301 reg_ldb_escape(mem_ctx,
302 keyname)))
304 talloc_free(ret);
305 return NULL;
309 if(begin) {
310 *begin = '\0';
311 } else {
312 break;
316 ldb_dn_add_base(ret, kd->dn);
318 return ret;
321 static WERROR cache_subkeys(struct ldb_key_data *kd)
323 struct ldb_context *c = kd->ldb;
324 struct ldb_result *res;
325 int ret;
327 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
328 NULL, "(key=*)");
329 if (ret != LDB_SUCCESS) {
330 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
331 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
332 return WERR_FOOBAR;
335 kd->subkey_count = res->count;
336 kd->subkeys = talloc_steal(kd, res->msgs);
337 talloc_free(res);
339 return WERR_OK;
342 static WERROR cache_values(struct ldb_key_data *kd)
344 struct ldb_context *c = kd->ldb;
345 struct ldb_result *res;
346 int ret;
348 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
349 NULL, "(value=*)");
350 if (ret != LDB_SUCCESS) {
351 DEBUG(0, ("Error getting values for '%s': %s\n",
352 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
353 return WERR_FOOBAR;
356 kd->value_count = res->count;
357 kd->values = talloc_steal(kd, res->msgs);
358 talloc_free(res);
360 return WERR_OK;
364 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
365 const struct hive_key *k, uint32_t idx,
366 const char **name,
367 const char **classname,
368 NTTIME *last_mod_time)
370 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
372 /* Initialization */
373 if (name != NULL)
374 *name = NULL;
375 if (classname != NULL)
376 *classname = NULL;
377 if (last_mod_time != NULL)
378 *last_mod_time = 0; /* TODO: we need to add this to the
379 ldb backend properly */
381 /* Do a search if necessary */
382 if (kd->subkeys == NULL) {
383 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
386 if (idx >= kd->subkey_count)
387 return WERR_NO_MORE_ITEMS;
389 if (name != NULL)
390 *name = talloc_strdup(mem_ctx,
391 ldb_msg_find_attr_as_string(kd->subkeys[idx], "key", NULL));
392 if (classname != NULL)
393 *classname = talloc_strdup(mem_ctx,
394 ldb_msg_find_attr_as_string(kd->subkeys[idx], "classname", NULL));
396 return WERR_OK;
399 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx,
400 const struct hive_key *k,
401 const char **name, uint32_t *data_type,
402 DATA_BLOB *data)
404 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
405 struct ldb_context *c = kd->ldb;
406 const char* attrs[] = { "data", "type", NULL };
407 struct ldb_result *res;
408 int ret;
410 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_BASE, attrs, "(dn=*)");
412 if (ret != LDB_SUCCESS) {
413 DEBUG(0, ("Error getting default value for '%s': %s\n",
414 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
415 return WERR_FOOBAR;
418 if (res->count == 0 || res->msgs[0]->num_elements == 0)
419 return WERR_BADFILE;
421 if ((data_type != NULL) && (data != NULL)) {
422 reg_ldb_unpack_value(mem_ctx, res->msgs[0], name, data_type,
423 data);
426 talloc_free(res);
428 return WERR_OK;
431 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
432 uint32_t idx, const char **name,
433 uint32_t *data_type, DATA_BLOB *data)
435 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
437 /* if the default value exists, give it back */
438 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
439 data))) {
440 if (idx == 0)
441 return WERR_OK;
442 else
443 --idx;
446 /* Do the search if necessary */
447 if (kd->values == NULL) {
448 W_ERROR_NOT_OK_RETURN(cache_values(kd));
451 if (idx >= kd->value_count)
452 return WERR_NO_MORE_ITEMS;
454 reg_ldb_unpack_value(mem_ctx, kd->values[idx], name, data_type, data);
456 return WERR_OK;
459 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
460 const char *name, uint32_t *data_type,
461 DATA_BLOB *data)
463 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
464 const char *res_name;
465 uint32_t idx;
467 if (name == NULL) {
468 return WERR_INVALID_PARAM;
471 /* the default value was requested, give it back */
472 if (name[0] == '\0') {
473 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
476 /* Do the search if necessary */
477 if (kd->values == NULL) {
478 W_ERROR_NOT_OK_RETURN(cache_values(kd));
481 for (idx = 0; idx < kd->value_count; idx++) {
482 res_name = ldb_msg_find_attr_as_string(kd->values[idx], "value",
483 "");
484 if (ldb_attr_cmp(name, res_name) == 0) {
485 reg_ldb_unpack_value(mem_ctx, kd->values[idx], NULL,
486 data_type, data);
487 return WERR_OK;
491 return WERR_BADFILE;
494 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
495 const char *name, struct hive_key **key)
497 struct ldb_result *res;
498 struct ldb_dn *ldap_path;
499 int ret;
500 struct ldb_key_data *newkd;
501 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
502 struct ldb_context *c = kd->ldb;
504 if (name == NULL) {
505 return WERR_INVALID_PARAM;
508 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
509 W_ERROR_HAVE_NO_MEMORY(ldap_path);
511 ret = ldb_search(c, mem_ctx, &res, ldap_path, LDB_SCOPE_BASE, NULL, "(key=*)");
513 if (ret != LDB_SUCCESS) {
514 DEBUG(3, ("Error opening key '%s': %s\n",
515 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
516 return WERR_FOOBAR;
517 } else if (res->count == 0) {
518 DEBUG(3, ("Key '%s' not found\n",
519 ldb_dn_get_linearized(ldap_path)));
520 talloc_free(res);
521 return WERR_BADFILE;
524 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
525 newkd->key.ops = &reg_backend_ldb;
526 newkd->ldb = talloc_reference(newkd, kd->ldb);
527 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
528 newkd->classname = talloc_steal(newkd,
529 ldb_msg_find_attr_as_string(res->msgs[0], "classname", NULL);
531 *key = (struct hive_key *)newkd;
533 return WERR_OK;
536 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
537 struct auth_session_info *session_info,
538 struct cli_credentials *credentials,
539 struct tevent_context *ev_ctx,
540 struct loadparm_context *lp_ctx,
541 struct hive_key **k)
543 struct ldb_key_data *kd;
544 struct ldb_context *wrap;
545 struct ldb_message *attrs_msg;
547 if (location == NULL)
548 return WERR_INVALID_PARAM;
550 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
551 location, session_info, credentials, 0);
553 if (wrap == NULL) {
554 DEBUG(1, (__FILE__": unable to connect\n"));
555 return WERR_FOOBAR;
558 attrs_msg = ldb_msg_new(wrap);
559 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
560 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
561 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
562 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
563 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
565 ldb_add(wrap, attrs_msg);
567 ldb_set_debug_stderr(wrap);
569 kd = talloc_zero(parent_ctx, struct ldb_key_data);
570 kd->key.ops = &reg_backend_ldb;
571 kd->ldb = talloc_reference(kd, wrap);
572 talloc_set_destructor (kd, reg_close_ldb_key);
573 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
575 *k = (struct hive_key *)kd;
577 return WERR_OK;
580 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
581 const char *name, const char *classname,
582 struct security_descriptor *sd,
583 struct hive_key **newkey)
585 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
586 struct ldb_message *msg;
587 struct ldb_key_data *newkd;
588 int ret;
590 if (name == NULL) {
591 return WERR_INVALID_PARAM;
594 msg = ldb_msg_new(mem_ctx);
595 W_ERROR_HAVE_NO_MEMORY(msg);
597 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
598 W_ERROR_HAVE_NO_MEMORY(msg->dn);
600 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
601 if (classname != NULL)
602 ldb_msg_add_string(msg, "classname",
603 talloc_strdup(mem_ctx, classname));
605 ret = ldb_add(parentkd->ldb, msg);
606 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
607 return WERR_ALREADY_EXISTS;
610 if (ret != LDB_SUCCESS) {
611 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
612 return WERR_FOOBAR;
615 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
617 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
618 W_ERROR_HAVE_NO_MEMORY(newkd);
619 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
620 newkd->key.ops = &reg_backend_ldb;
621 newkd->dn = talloc_steal(newkd, msg->dn);
622 newkd->classname = talloc_steal(newkd, classname);
624 *newkey = (struct hive_key *)newkd;
626 /* reset cache */
627 talloc_free(parentkd->subkeys);
628 parentkd->subkeys = NULL;
630 return WERR_OK;
633 static WERROR ldb_del_value(TALLOC_CTX *mem_ctx, struct hive_key *key,
634 const char *child)
636 int ret;
637 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
638 struct ldb_message *msg;
639 struct ldb_dn *childdn;
641 if (child == NULL) {
642 return WERR_INVALID_PARAM;
645 if (child[0] == '\0') {
646 /* default value */
647 msg = talloc_zero(mem_ctx, struct ldb_message);
648 W_ERROR_HAVE_NO_MEMORY(msg);
649 msg->dn = ldb_dn_copy(msg, kd->dn);
650 W_ERROR_HAVE_NO_MEMORY(msg->dn);
651 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
652 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
654 ret = ldb_modify(kd->ldb, msg);
655 if (ret != LDB_SUCCESS) {
656 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
657 return WERR_FOOBAR;
659 } else {
660 /* normal value */
661 childdn = ldb_dn_copy(kd->ldb, kd->dn);
662 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
663 reg_ldb_escape(childdn, child)))
665 talloc_free(childdn);
666 return WERR_FOOBAR;
669 ret = ldb_delete(kd->ldb, childdn);
671 talloc_free(childdn);
673 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
674 return WERR_BADFILE;
675 } else if (ret != LDB_SUCCESS) {
676 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
677 return WERR_FOOBAR;
681 /* reset cache */
682 talloc_free(kd->values);
683 kd->values = NULL;
685 return WERR_OK;
688 static WERROR ldb_del_key(TALLOC_CTX *mem_ctx, const struct hive_key *key,
689 const char *name)
691 unsigned int i;
692 int ret;
693 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
694 struct ldb_dn *ldap_path;
695 struct ldb_context *c = parentkd->ldb;
696 struct ldb_result *res_keys;
697 struct ldb_result *res_vals;
698 WERROR werr;
699 struct hive_key *hk;
701 if (name == NULL) {
702 return WERR_INVALID_PARAM;
705 /* Verify key exists by opening it */
706 werr = ldb_open_key(mem_ctx, key, name, &hk);
707 if (!W_ERROR_IS_OK(werr)) {
708 return werr;
711 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
712 W_ERROR_HAVE_NO_MEMORY(ldap_path);
714 /* Search for subkeys */
715 ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
716 NULL, "(key=*)");
718 if (ret != LDB_SUCCESS) {
719 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
720 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
721 return WERR_FOOBAR;
724 /* Search for values */
725 ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
726 NULL, "(value=*)");
728 if (ret != LDB_SUCCESS) {
729 DEBUG(0, ("Error getting values for '%s': %s\n",
730 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
731 return WERR_FOOBAR;
734 /* Start an explicit transaction */
735 ret = ldb_transaction_start(c);
737 if (ret != LDB_SUCCESS) {
738 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
739 return WERR_FOOBAR;
742 if (res_keys->count || res_vals->count)
744 /* Delete any subkeys */
745 for (i = 0; i < res_keys->count; i++)
747 werr = ldb_del_key(mem_ctx, hk,
748 ldb_msg_find_attr_as_string(
749 res_keys->msgs[i],
750 "key", NULL));
751 if (!W_ERROR_IS_OK(werr)) {
752 ret = ldb_transaction_cancel(c);
753 return werr;
757 /* Delete any values */
758 for (i = 0; i < res_vals->count; i++)
760 werr = ldb_del_value(mem_ctx, hk,
761 ldb_msg_find_attr_as_string(
762 res_vals->msgs[i],
763 "value", NULL));
764 if (!W_ERROR_IS_OK(werr)) {
765 ret = ldb_transaction_cancel(c);
766 return werr;
771 /* Delete the key itself */
772 ret = ldb_delete(c, ldap_path);
774 if (ret != LDB_SUCCESS)
776 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
777 ret = ldb_transaction_cancel(c);
778 return WERR_FOOBAR;
781 /* Commit the transaction */
782 ret = ldb_transaction_commit(c);
784 if (ret != LDB_SUCCESS)
786 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
787 ret = ldb_transaction_cancel(c);
788 return WERR_FOOBAR;
791 /* reset cache */
792 talloc_free(parentkd->subkeys);
793 parentkd->subkeys = NULL;
795 return WERR_OK;
798 static WERROR ldb_set_value(struct hive_key *parent,
799 const char *name, uint32_t type,
800 const DATA_BLOB data)
802 struct ldb_message *msg;
803 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
804 unsigned int i;
805 int ret;
806 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
808 if (name == NULL) {
809 return WERR_INVALID_PARAM;
812 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
813 W_ERROR_HAVE_NO_MEMORY(msg);
815 msg->dn = ldb_dn_copy(msg, kd->dn);
816 W_ERROR_HAVE_NO_MEMORY(msg->dn);
818 if (name[0] != '\0') {
819 /* For a default value, we add/overwrite the attributes to/of the hive.
820 For a normal value, we create a new child. */
821 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
822 reg_ldb_escape(mem_ctx, name)))
824 talloc_free(mem_ctx);
825 return WERR_FOOBAR;
829 /* Try first a "modify" and if this doesn't work do try an "add" */
830 for (i = 0; i < msg->num_elements; i++) {
831 if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE) {
832 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
835 ret = ldb_modify(kd->ldb, msg);
836 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
837 i = 0;
838 while (i < msg->num_elements) {
839 if (msg->elements[i].flags == LDB_FLAG_MOD_DELETE) {
840 ldb_msg_remove_element(msg, &msg->elements[i]);
841 } else {
842 ++i;
845 ret = ldb_add(kd->ldb, msg);
847 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
848 /* ignore this -> the value didn't exist and also now doesn't */
849 ret = LDB_SUCCESS;
852 if (ret != LDB_SUCCESS) {
853 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
854 talloc_free(mem_ctx);
855 return WERR_FOOBAR;
858 /* reset cache */
859 talloc_free(kd->values);
860 kd->values = NULL;
862 talloc_free(mem_ctx);
863 return WERR_OK;
866 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
867 const struct hive_key *key,
868 const char **classname,
869 uint32_t *num_subkeys,
870 uint32_t *num_values,
871 NTTIME *last_change_time,
872 uint32_t *max_subkeynamelen,
873 uint32_t *max_valnamelen,
874 uint32_t *max_valbufsize)
876 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
877 uint32_t default_value_type = REG_NONE;
878 DATA_BLOB default_value = { NULL, 0 };
879 WERROR werr;
881 /* Initialization */
882 if (classname != NULL)
883 *classname = NULL;
884 if (num_subkeys != NULL)
885 *num_subkeys = 0;
886 if (num_values != NULL)
887 *num_values = 0;
888 if (last_change_time != NULL)
889 *last_change_time = 0;
890 if (max_subkeynamelen != NULL)
891 *max_subkeynamelen = 0;
892 if (max_valnamelen != NULL)
893 *max_valnamelen = 0;
894 if (max_valbufsize != NULL)
895 *max_valbufsize = 0;
897 /* We need this to get the default value (if it exists) for counting
898 * the values under the key and for finding out the longest value buffer
899 * size. If no default value exists the DATA_BLOB "default_value" will
900 * remain { NULL, 0 }. */
901 werr = ldb_get_default_value(mem_ctx, key, NULL, &default_value_type,
902 &default_value);
903 if ((!W_ERROR_IS_OK(werr)) && (!W_ERROR_EQUAL(werr, WERR_BADFILE))) {
904 return werr;
907 if (kd->subkeys == NULL) {
908 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
910 if (kd->values == NULL) {
911 W_ERROR_NOT_OK_RETURN(cache_values(kd));
914 if (classname != NULL) {
915 *classname = kd->classname;
918 if (num_subkeys != NULL) {
919 *num_subkeys = kd->subkey_count;
921 if (num_values != NULL) {
922 *num_values = kd->value_count;
923 /* also consider the default value if it exists */
924 if (default_value.data != NULL) {
925 ++(*num_values);
930 if (max_subkeynamelen != NULL) {
931 unsigned int i;
932 struct ldb_message_element *el;
934 for (i = 0; i < kd->subkey_count; i++) {
935 el = ldb_msg_find_element(kd->subkeys[i], "key");
936 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
940 if (max_valnamelen != NULL || max_valbufsize != NULL) {
941 unsigned int i;
942 struct ldb_message_element *el;
943 W_ERROR_NOT_OK_RETURN(cache_values(kd));
945 /* also consider the default value if it exists */
946 if ((max_valbufsize != NULL) && (default_value.data != NULL)) {
947 *max_valbufsize = MAX(*max_valbufsize,
948 default_value.length);
951 for (i = 0; i < kd->value_count; i++) {
952 if (max_valnamelen != NULL) {
953 el = ldb_msg_find_element(kd->values[i], "value");
954 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
957 if (max_valbufsize != NULL) {
958 uint32_t data_type;
959 DATA_BLOB data;
960 reg_ldb_unpack_value(mem_ctx,
961 kd->values[i], NULL,
962 &data_type, &data);
963 *max_valbufsize = MAX(*max_valbufsize, data.length);
964 talloc_free(data.data);
969 talloc_free(default_value.data);
971 return WERR_OK;
974 static struct hive_operations reg_backend_ldb = {
975 .name = "ldb",
976 .add_key = ldb_add_key,
977 .del_key = ldb_del_key,
978 .get_key_by_name = ldb_open_key,
979 .enum_value = ldb_get_value_by_id,
980 .enum_key = ldb_get_subkey_by_id,
981 .set_value = ldb_set_value,
982 .get_value_by_name = ldb_get_value,
983 .delete_value = ldb_del_value,
984 .get_key_info = ldb_get_key_info,