s3-lsa: Fix static list of luids in our privileges implementation.
[Samba/ekacnet.git] / source4 / lib / registry / ldb.c
blob2310babd8de1f10c1f71065a451d634d5b1f25b5
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 ""));
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 /* The data should be provided as UTF16 string */
65 convert_string_talloc(mem_ctx, CH_UTF8, CH_UTF16,
66 val->data, val->length,
67 (void **)&data->data, &data->length, false);
68 } else {
69 data->data = NULL;
70 data->length = 0;
72 break;
74 case REG_DWORD:
75 case REG_DWORD_BIG_ENDIAN:
76 if (val != NULL) {
77 /* The data is a plain DWORD */
78 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
79 data->data = talloc_size(mem_ctx, sizeof(uint32_t));
80 if (data->data != NULL) {
81 SIVAL(data->data, 0, tmp);
83 data->length = sizeof(uint32_t);
84 } else {
85 data->data = NULL;
86 data->length = 0;
88 break;
90 case REG_QWORD:
91 if (val != NULL) {
92 /* The data is a plain QWORD */
93 uint64_t tmp = strtoull((char *)val->data, NULL, 0);
94 data->data = talloc_size(mem_ctx, sizeof(uint64_t));
95 if (data->data != NULL) {
96 SBVAL(data->data, 0, tmp);
98 data->length = sizeof(uint64_t);
99 } else {
100 data->data = NULL;
101 data->length = 0;
103 break;
105 case REG_BINARY:
106 default:
107 if (val != NULL) {
108 data->data = talloc_memdup(mem_ctx, val->data,
109 val->length);
110 data->length = val->length;
111 } else {
112 data->data = NULL;
113 data->length = 0;
115 break;
119 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
120 TALLOC_CTX *mem_ctx,
121 const char *name,
122 uint32_t type, DATA_BLOB data)
124 struct ldb_message *msg;
125 char *name_dup, *type_str;
126 int ret;
128 msg = talloc_zero(mem_ctx, struct ldb_message);
129 if (msg == NULL) {
130 return NULL;
133 name_dup = talloc_strdup(msg, name);
134 if (name_dup == NULL) {
135 talloc_free(msg);
136 return NULL;
139 ret = ldb_msg_add_string(msg, "value", name_dup);
140 if (ret != LDB_SUCCESS) {
141 talloc_free(msg);
142 return NULL;
145 switch (type) {
146 case REG_SZ:
147 case REG_EXPAND_SZ:
148 if ((data.length > 0) && (data.data != NULL)) {
149 struct ldb_val *val;
150 bool ret2 = false;
152 val = talloc_zero(msg, struct ldb_val);
153 if (val == NULL) {
154 talloc_free(msg);
155 return NULL;
158 /* The data is provided as UTF16 string */
159 ret2 = convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8,
160 (void *)data.data, data.length,
161 (void **)&val->data, &val->length,
162 false);
163 if (ret2) {
164 ret = ldb_msg_add_value(msg, "data", val, NULL);
165 } else {
166 /* workaround for non-standard data */
167 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
169 } else {
170 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
172 break;
174 case REG_DWORD:
175 case REG_DWORD_BIG_ENDIAN:
176 if ((data.length > 0) && (data.data != NULL)) {
177 if (data.length == sizeof(uint32_t)) {
178 char *conv_str;
180 conv_str = talloc_asprintf(msg, "0x%8.8x",
181 IVAL(data.data, 0));
182 if (conv_str == NULL) {
183 talloc_free(msg);
184 return NULL;
186 ret = ldb_msg_add_string(msg, "data", conv_str);
187 } else {
188 /* workaround for non-standard data */
189 talloc_free(msg);
190 return NULL;
192 } else {
193 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
195 break;
197 case REG_QWORD:
198 if ((data.length > 0) && (data.data != NULL)) {
199 if (data.length == sizeof(uint64_t)) {
200 char *conv_str;
202 conv_str = talloc_asprintf(msg, "0x%16.16llx",
203 BVAL(data.data, 0));
204 if (conv_str == NULL) {
205 talloc_free(msg);
206 return NULL;
208 ret = ldb_msg_add_string(msg, "data", conv_str);
209 } else {
210 /* workaround for non-standard data */
211 talloc_free(msg);
212 return NULL;
215 } else {
216 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
218 break;
220 case REG_BINARY:
221 default:
222 if ((data.length > 0) && (data.data != NULL)) {
223 ret = ldb_msg_add_value(msg, "data", &data, NULL);
224 } else {
225 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
227 break;
230 if (ret != LDB_SUCCESS) {
231 talloc_free(msg);
232 return NULL;
235 type_str = talloc_asprintf(mem_ctx, "%u", type);
236 if (type_str == NULL) {
237 talloc_free(msg);
238 return NULL;
241 ret = ldb_msg_add_string(msg, "type", type_str);
242 if (ret != LDB_SUCCESS) {
243 talloc_free(msg);
244 return NULL;
247 return msg;
250 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
252 struct ldb_val val;
254 val.data = discard_const_p(uint8_t, value);
255 val.length = strlen(value);
257 return ldb_dn_escape_value(mem_ctx, val);
260 static int reg_close_ldb_key(struct ldb_key_data *key)
262 if (key->subkeys != NULL) {
263 talloc_free(key->subkeys);
264 key->subkeys = NULL;
267 if (key->values != NULL) {
268 talloc_free(key->values);
269 key->values = NULL;
271 return 0;
274 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
275 const struct hive_key *from,
276 const char *path, const char *add)
278 TALLOC_CTX *local_ctx;
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 local_ctx = talloc_new(mem_ctx);
287 ret = ldb_dn_new(mem_ctx, ldb, add);
288 if (!ldb_dn_validate(ret)) {
289 talloc_free(ret);
290 talloc_free(local_ctx);
291 return NULL;
294 while (mypath) {
295 char *keyname;
297 begin = strrchr(mypath, '\\');
299 if (begin) keyname = begin + 1;
300 else keyname = mypath;
302 if (keyname[0] != '\0') {
303 if (!ldb_dn_add_base_fmt(ret, "key=%s",
304 reg_ldb_escape(local_ctx,
305 keyname)))
307 talloc_free(local_ctx);
308 return NULL;
312 if(begin) {
313 *begin = '\0';
314 } else {
315 break;
319 ldb_dn_add_base(ret, kd->dn);
321 talloc_free(local_ctx);
323 return ret;
326 static WERROR cache_subkeys(struct ldb_key_data *kd)
328 struct ldb_context *c = kd->ldb;
329 struct ldb_result *res;
330 int ret;
332 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "(key=*)");
334 if (ret != LDB_SUCCESS) {
335 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
336 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
337 return WERR_FOOBAR;
340 kd->subkey_count = res->count;
341 kd->subkeys = talloc_steal(kd, res->msgs);
342 talloc_free(res);
344 return WERR_OK;
347 static WERROR cache_values(struct ldb_key_data *kd)
349 struct ldb_context *c = kd->ldb;
350 struct ldb_result *res;
351 int ret;
353 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
354 NULL, "(value=*)");
356 if (ret != LDB_SUCCESS) {
357 DEBUG(0, ("Error getting values for '%s': %s\n",
358 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
359 return WERR_FOOBAR;
362 kd->value_count = res->count;
363 kd->values = talloc_steal(kd, res->msgs);
364 talloc_free(res);
366 return WERR_OK;
370 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
371 const struct hive_key *k, uint32_t idx,
372 const char **name,
373 const char **classname,
374 NTTIME *last_mod_time)
376 struct ldb_message_element *el;
377 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
379 /* Initialization */
380 if (name != NULL)
381 *name = NULL;
382 if (classname != NULL)
383 *classname = NULL; /* TODO: Store properly */
384 if (last_mod_time != NULL)
385 *last_mod_time = 0; /* TODO: we need to add this to the
386 ldb backend properly */
388 /* Do a search if necessary */
389 if (kd->subkeys == NULL) {
390 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
393 if (idx >= kd->subkey_count)
394 return WERR_NO_MORE_ITEMS;
396 el = ldb_msg_find_element(kd->subkeys[idx], "key");
397 SMB_ASSERT(el != NULL);
398 SMB_ASSERT(el->num_values != 0);
400 if (name != NULL)
401 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
403 return WERR_OK;
406 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx,
407 const struct hive_key *k,
408 const char **name, uint32_t *data_type,
409 DATA_BLOB *data)
411 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
412 struct ldb_context *c = kd->ldb;
413 const char* attrs[] = { "data", "type", NULL };
414 struct ldb_result *res;
415 int ret;
417 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_BASE, attrs, "(dn=*)");
419 if (ret != LDB_SUCCESS) {
420 DEBUG(0, ("Error getting default value for '%s': %s\n",
421 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
422 return WERR_FOOBAR;
425 if (res->count == 0 || res->msgs[0]->num_elements == 0)
426 return WERR_BADFILE;
428 if ((data_type != NULL) && (data != NULL)) {
429 reg_ldb_unpack_value(mem_ctx, res->msgs[0], name, data_type,
430 data);
433 talloc_free(res);
435 return WERR_OK;
438 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
439 uint32_t idx, const char **name,
440 uint32_t *data_type, DATA_BLOB *data)
442 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
444 /* if default value exists, give it back */
445 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
446 data))) {
447 if (idx == 0)
448 return WERR_OK;
449 else
450 --idx;
453 /* Do the search if necessary */
454 if (kd->values == NULL) {
455 W_ERROR_NOT_OK_RETURN(cache_values(kd));
458 if (idx >= kd->value_count)
459 return WERR_NO_MORE_ITEMS;
461 reg_ldb_unpack_value(mem_ctx, kd->values[idx], name, data_type, data);
463 return WERR_OK;
466 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
467 const char *name, uint32_t *data_type,
468 DATA_BLOB *data)
470 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
471 struct ldb_context *c = kd->ldb;
472 struct ldb_result *res;
473 int ret;
475 if (name == NULL) {
476 return WERR_INVALID_PARAM;
479 if (name[0] == '\0') {
480 /* default value */
481 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
482 } else {
483 /* normal value */
484 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_ONELEVEL,
485 NULL, "(value=%s)", name);
487 if (ret != LDB_SUCCESS) {
488 DEBUG(0, ("Error getting values for '%s': %s\n",
489 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
490 return WERR_FOOBAR;
493 if (res->count == 0)
494 return WERR_BADFILE;
496 reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
498 talloc_free(res);
501 return WERR_OK;
504 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
505 const char *name, struct hive_key **key)
507 struct ldb_result *res;
508 struct ldb_dn *ldap_path;
509 int ret;
510 struct ldb_key_data *newkd;
511 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
512 struct ldb_context *c = kd->ldb;
514 if (name == NULL) {
515 return WERR_INVALID_PARAM;
518 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
519 W_ERROR_HAVE_NO_MEMORY(ldap_path);
521 ret = ldb_search(c, mem_ctx, &res, ldap_path, LDB_SCOPE_BASE, NULL, "(key=*)");
523 if (ret != LDB_SUCCESS) {
524 DEBUG(3, ("Error opening key '%s': %s\n",
525 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
526 return WERR_FOOBAR;
527 } else if (res->count == 0) {
528 DEBUG(3, ("Key '%s' not found\n",
529 ldb_dn_get_linearized(ldap_path)));
530 talloc_free(res);
531 return WERR_BADFILE;
534 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
535 newkd->key.ops = &reg_backend_ldb;
536 newkd->ldb = talloc_reference(newkd, kd->ldb);
537 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
539 *key = (struct hive_key *)newkd;
541 return WERR_OK;
544 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
545 struct auth_session_info *session_info,
546 struct cli_credentials *credentials,
547 struct tevent_context *ev_ctx,
548 struct loadparm_context *lp_ctx,
549 struct hive_key **k)
551 struct ldb_key_data *kd;
552 struct ldb_context *wrap;
553 struct ldb_message *attrs_msg;
555 if (location == NULL)
556 return WERR_INVALID_PARAM;
558 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
559 location, session_info, credentials, 0);
561 if (wrap == NULL) {
562 DEBUG(1, (__FILE__": unable to connect\n"));
563 return WERR_FOOBAR;
566 attrs_msg = ldb_msg_new(wrap);
567 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
568 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
569 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
570 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
571 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
573 ldb_add(wrap, attrs_msg);
575 ldb_set_debug_stderr(wrap);
577 kd = talloc_zero(parent_ctx, struct ldb_key_data);
578 kd->key.ops = &reg_backend_ldb;
579 kd->ldb = talloc_reference(kd, wrap);
580 talloc_set_destructor (kd, reg_close_ldb_key);
581 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
583 *k = (struct hive_key *)kd;
585 return WERR_OK;
588 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
589 const char *name, const char *classname,
590 struct security_descriptor *sd,
591 struct hive_key **newkey)
593 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
594 struct ldb_message *msg;
595 struct ldb_key_data *newkd;
596 int ret;
598 if (name == NULL) {
599 return WERR_INVALID_PARAM;
602 msg = ldb_msg_new(mem_ctx);
603 W_ERROR_HAVE_NO_MEMORY(msg);
605 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
606 W_ERROR_HAVE_NO_MEMORY(msg->dn);
608 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
609 if (classname != NULL)
610 ldb_msg_add_string(msg, "classname",
611 talloc_strdup(mem_ctx, classname));
613 ret = ldb_add(parentkd->ldb, msg);
614 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
615 return WERR_ALREADY_EXISTS;
618 if (ret != LDB_SUCCESS) {
619 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
620 return WERR_FOOBAR;
623 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
625 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
626 W_ERROR_HAVE_NO_MEMORY(newkd);
627 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
628 newkd->key.ops = &reg_backend_ldb;
629 newkd->dn = talloc_steal(newkd, msg->dn);
631 *newkey = (struct hive_key *)newkd;
633 /* reset cache */
634 talloc_free(parentkd->subkeys);
635 parentkd->subkeys = NULL;
637 return WERR_OK;
640 static WERROR ldb_del_value(TALLOC_CTX *mem_ctx, struct hive_key *key,
641 const char *child)
643 int ret;
644 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
645 struct ldb_message *msg;
646 struct ldb_dn *childdn;
648 if (child == NULL) {
649 return WERR_INVALID_PARAM;
652 if (child[0] == '\0') {
653 /* default value */
654 msg = talloc_zero(mem_ctx, struct ldb_message);
655 W_ERROR_HAVE_NO_MEMORY(msg);
656 msg->dn = ldb_dn_copy(msg, kd->dn);
657 W_ERROR_HAVE_NO_MEMORY(msg->dn);
658 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
659 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
661 ret = ldb_modify(kd->ldb, msg);
662 if (ret != LDB_SUCCESS) {
663 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
664 return WERR_FOOBAR;
666 } else {
667 /* normal value */
668 childdn = ldb_dn_copy(kd->ldb, kd->dn);
669 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
670 reg_ldb_escape(childdn, child)))
672 talloc_free(childdn);
673 return WERR_FOOBAR;
676 ret = ldb_delete(kd->ldb, childdn);
678 talloc_free(childdn);
680 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
681 return WERR_BADFILE;
682 } else if (ret != LDB_SUCCESS) {
683 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
684 return WERR_FOOBAR;
688 /* reset cache */
689 talloc_free(kd->values);
690 kd->values = NULL;
692 return WERR_OK;
695 static WERROR ldb_del_key(TALLOC_CTX *mem_ctx, const struct hive_key *key,
696 const char *name)
698 unsigned int i;
699 int ret;
700 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
701 struct ldb_dn *ldap_path;
702 struct ldb_context *c = parentkd->ldb;
703 struct ldb_result *res_keys;
704 struct ldb_result *res_vals;
705 WERROR werr;
706 struct hive_key *hk;
708 if (name == NULL) {
709 return WERR_INVALID_PARAM;
712 /* Verify key exists by opening it */
713 werr = ldb_open_key(mem_ctx, key, name, &hk);
714 if (!W_ERROR_IS_OK(werr)) {
715 return werr;
718 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
719 W_ERROR_HAVE_NO_MEMORY(ldap_path);
721 /* Search for subkeys */
722 ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
723 NULL, "(key=*)");
725 if (ret != LDB_SUCCESS) {
726 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
727 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
728 return WERR_FOOBAR;
731 /* Search for values */
732 ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
733 NULL, "(value=*)");
735 if (ret != LDB_SUCCESS) {
736 DEBUG(0, ("Error getting values for '%s': %s\n",
737 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
738 return WERR_FOOBAR;
741 /* Start an explicit transaction */
742 ret = ldb_transaction_start(c);
744 if (ret != LDB_SUCCESS) {
745 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
746 return WERR_FOOBAR;
749 if (res_keys->count || res_vals->count)
751 /* Delete any subkeys */
752 for (i = 0; i < res_keys->count; i++)
754 werr = ldb_del_key(mem_ctx, hk,
755 ldb_msg_find_attr_as_string(
756 res_keys->msgs[i],
757 "key", NULL));
758 if (!W_ERROR_IS_OK(werr)) {
759 ret = ldb_transaction_cancel(c);
760 return werr;
764 /* Delete any values */
765 for (i = 0; i < res_vals->count; i++)
767 werr = ldb_del_value(mem_ctx, hk,
768 ldb_msg_find_attr_as_string(
769 res_vals->msgs[i],
770 "value", NULL));
771 if (!W_ERROR_IS_OK(werr)) {
772 ret = ldb_transaction_cancel(c);
773 return werr;
778 /* Delete the key itself */
779 ret = ldb_delete(c, ldap_path);
781 if (ret != LDB_SUCCESS)
783 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
784 ret = ldb_transaction_cancel(c);
785 return WERR_FOOBAR;
788 /* Commit the transaction */
789 ret = ldb_transaction_commit(c);
791 if (ret != LDB_SUCCESS)
793 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
794 ret = ldb_transaction_cancel(c);
795 return WERR_FOOBAR;
798 /* reset cache */
799 talloc_free(parentkd->subkeys);
800 parentkd->subkeys = NULL;
802 return WERR_OK;
805 static WERROR ldb_set_value(struct hive_key *parent,
806 const char *name, uint32_t type,
807 const DATA_BLOB data)
809 struct ldb_message *msg;
810 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
811 unsigned int i;
812 int ret;
813 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
815 if (name == NULL) {
816 return WERR_INVALID_PARAM;
819 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
820 W_ERROR_HAVE_NO_MEMORY(msg);
822 msg->dn = ldb_dn_copy(msg, kd->dn);
823 W_ERROR_HAVE_NO_MEMORY(msg->dn);
825 if (name[0] != '\0') {
826 /* For a default value, we add/overwrite the attributes to/of the hive.
827 For a normal value, we create a new child. */
828 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
829 reg_ldb_escape(mem_ctx, name)))
831 talloc_free(mem_ctx);
832 return WERR_FOOBAR;
836 /* Try first a "modify" and if this doesn't work do try an "add" */
837 for (i = 0; i < msg->num_elements; i++) {
838 if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE) {
839 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
842 ret = ldb_modify(kd->ldb, msg);
843 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
844 i = 0;
845 while (i < msg->num_elements) {
846 if (msg->elements[i].flags == LDB_FLAG_MOD_DELETE) {
847 ldb_msg_remove_element(msg, &msg->elements[i]);
848 } else {
849 ++i;
852 ret = ldb_add(kd->ldb, msg);
854 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
855 /* ignore this -> the value didn't exist and also now doesn't */
856 ret = LDB_SUCCESS;
859 if (ret != LDB_SUCCESS) {
860 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
861 talloc_free(mem_ctx);
862 return WERR_FOOBAR;
865 /* reset cache */
866 talloc_free(kd->values);
867 kd->values = NULL;
869 talloc_free(mem_ctx);
870 return WERR_OK;
873 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
874 const struct hive_key *key,
875 const char **classname,
876 uint32_t *num_subkeys,
877 uint32_t *num_values,
878 NTTIME *last_change_time,
879 uint32_t *max_subkeynamelen,
880 uint32_t *max_valnamelen,
881 uint32_t *max_valbufsize)
883 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
884 uint32_t default_value_type = REG_NONE;
885 DATA_BLOB default_value = { NULL, 0 };
886 WERROR werr;
888 /* Initialization */
889 if (classname != NULL)
890 *classname = NULL;
891 if (num_subkeys != NULL)
892 *num_subkeys = 0;
893 if (num_values != NULL)
894 *num_values = 0;
895 if (last_change_time != NULL)
896 *last_change_time = 0;
897 if (max_subkeynamelen != NULL)
898 *max_subkeynamelen = 0;
899 if (max_valnamelen != NULL)
900 *max_valnamelen = 0;
901 if (max_valbufsize != NULL)
902 *max_valbufsize = 0;
904 /* We need this to get the default value (if it exists) for counting
905 * the values under the key and for finding out the longest value buffer
906 * size. If no default value exists the DATA_BLOB "default_value" will
907 * remain { NULL, 0 }. */
908 werr = ldb_get_default_value(mem_ctx, key, NULL, &default_value_type,
909 &default_value);
910 if ((!W_ERROR_IS_OK(werr)) && (!W_ERROR_EQUAL(werr, WERR_BADFILE))) {
911 return werr;
914 if (kd->subkeys == NULL) {
915 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
918 if (kd->values == NULL) {
919 W_ERROR_NOT_OK_RETURN(cache_values(kd));
922 if (num_subkeys != NULL) {
923 *num_subkeys = kd->subkey_count;
925 if (num_values != NULL) {
926 *num_values = kd->value_count;
927 /* also consider the default value if it exists */
928 if (default_value.data != NULL) {
929 ++(*num_values);
934 if (max_subkeynamelen != NULL) {
935 unsigned int i;
936 struct ldb_message_element *el;
938 for (i = 0; i < kd->subkey_count; i++) {
939 el = ldb_msg_find_element(kd->subkeys[i], "key");
940 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
944 if (max_valnamelen != NULL || max_valbufsize != NULL) {
945 unsigned int i;
946 struct ldb_message_element *el;
947 W_ERROR_NOT_OK_RETURN(cache_values(kd));
949 /* also consider the default value if it exists */
950 if ((max_valbufsize != NULL) && (default_value.data != NULL)) {
951 *max_valbufsize = MAX(*max_valbufsize,
952 default_value.length);
955 for (i = 0; i < kd->value_count; i++) {
956 if (max_valnamelen != NULL) {
957 el = ldb_msg_find_element(kd->values[i], "value");
958 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
961 if (max_valbufsize != NULL) {
962 uint32_t data_type;
963 DATA_BLOB data;
964 reg_ldb_unpack_value(mem_ctx,
965 kd->values[i], NULL,
966 &data_type, &data);
967 *max_valbufsize = MAX(*max_valbufsize, data.length);
968 talloc_free(data.data);
973 talloc_free(default_value.data);
975 return WERR_OK;
978 static struct hive_operations reg_backend_ldb = {
979 .name = "ldb",
980 .add_key = ldb_add_key,
981 .del_key = ldb_del_key,
982 .get_key_by_name = ldb_open_key,
983 .enum_value = ldb_get_value_by_id,
984 .enum_key = ldb_get_subkey_by_id,
985 .set_value = ldb_set_value,
986 .get_value_by_name = ldb_get_value,
987 .delete_value = ldb_del_value,
988 .get_key_info = ldb_get_key_info,