s4:registry - "LDB backend" - fix indentation
[Samba.git] / source4 / lib / registry / ldb.c
blobdd94cf24ef637f29c7b39899c1b104f67cafd7ab
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 if (val->data[0] != '\0') {
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 /* Provide a possibility to store also UTF8
71 * REG_SZ/REG_EXPAND_SZ values. This is done
72 * by adding a '\0' in front of the data */
73 data->data = talloc_size(mem_ctx, val->length - 1);
74 if (data->data != NULL) {
75 memcpy(data->data, val->data + 1,
76 val->length - 1);
78 data->length = val->length - 1;
80 } else {
81 data->data = NULL;
82 data->length = 0;
84 break;
86 case REG_DWORD:
87 case REG_DWORD_BIG_ENDIAN:
88 if (val != NULL) {
89 if (val->data[0] != '\0') {
90 /* The data is a plain DWORD */
91 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
92 data->data = talloc_size(mem_ctx, sizeof(uint32_t));
93 if (data->data != NULL) {
94 SIVAL(data->data, 0, tmp);
96 data->length = sizeof(uint32_t);
97 } else {
98 /* Provide a possibility to store also UTF8
99 * REG_DWORD values. This is done by adding a
100 * '\0' in front of the data */
101 data->data = talloc_size(mem_ctx, val->length - 1);
102 if (data->data != NULL) {
103 memcpy(data->data, val->data + 1,
104 val->length - 1);
106 data->length = val->length - 1;
108 } else {
109 data->data = NULL;
110 data->length = 0;
112 break;
114 case REG_QWORD:
115 if (val != NULL) {
116 if (val->data[0] != '\0') {
117 /* The data is a plain QWORD */
118 uint64_t tmp = strtoull((char *)val->data, NULL, 0);
119 data->data = talloc_size(mem_ctx, sizeof(uint64_t));
120 if (data->data != NULL) {
121 SBVAL(data->data, 0, tmp);
123 data->length = sizeof(uint64_t);
124 } else {
125 /* Provide a possibility to store also UTF8
126 * REG_QWORD values. This is done by adding a
127 * '\0' in front of the data */
128 data->data = talloc_size(mem_ctx, val->length - 1);
129 if (data->data != NULL) {
130 memcpy(data->data, val->data + 1,
131 val->length - 1);
133 data->length = val->length - 1;
135 } else {
136 data->data = NULL;
137 data->length = 0;
139 break;
141 case REG_BINARY:
142 default:
143 if (val != NULL) {
144 data->data = talloc_memdup(mem_ctx, val->data,
145 val->length);
146 data->length = val->length;
147 } else {
148 data->data = NULL;
149 data->length = 0;
151 break;
155 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
156 TALLOC_CTX *mem_ctx,
157 const char *name,
158 uint32_t type, DATA_BLOB data)
160 struct ldb_message *msg;
161 char *name_dup, *type_str;
162 int ret;
164 msg = talloc_zero(mem_ctx, struct ldb_message);
165 if (msg == NULL) {
166 return NULL;
169 name_dup = talloc_strdup(msg, name);
170 if (name_dup == NULL) {
171 talloc_free(msg);
172 return NULL;
175 ret = ldb_msg_add_string(msg, "value", name_dup);
176 if (ret != LDB_SUCCESS) {
177 talloc_free(msg);
178 return NULL;
181 switch (type) {
182 case REG_SZ:
183 case REG_EXPAND_SZ:
184 if ((data.length > 0) && (data.data != NULL)) {
185 struct ldb_val *val;
186 bool ret2 = false;
188 val = talloc_zero(msg, struct ldb_val);
189 if (val == NULL) {
190 talloc_free(msg);
191 return NULL;
194 /* Only when the "data.length" is dividable by two try
195 * the charset conversion, otherwise stick with the
196 * default of the "ret2" variable set to "false" (which
197 * means binary storage and no conversion) */
198 if (data.length % 2 == 0) {
199 /* The data is provided as UTF16 string */
200 ret2 = convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8,
201 (void *)data.data, data.length,
202 (void **)&val->data, &val->length,
203 false);
205 if (!ret2) {
206 /* Provide a possibility to store also binary
207 * UTF8 REG_SZ/REG_EXPAND_SZ values as fallback
208 * mechanism. This is done by adding a '\0' in
209 * front of the data */
210 val->data = talloc_size(msg, data.length + 1);
211 if (val->data == NULL) {
212 talloc_free(msg);
213 return NULL;
215 val->data[0] = '\0';
216 memcpy(val->data + 1, data.data, data.length);
217 val->length = data.length + 1;
219 ret = ldb_msg_add_value(msg, "data", val, NULL);
220 } else {
221 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
223 break;
225 case REG_DWORD:
226 case REG_DWORD_BIG_ENDIAN:
227 if ((data.length > 0) && (data.data != NULL)) {
228 if (data.length == sizeof(uint32_t)) {
229 char *conv_str;
231 conv_str = talloc_asprintf(msg, "0x%8.8x",
232 IVAL(data.data, 0));
233 if (conv_str == NULL) {
234 talloc_free(msg);
235 return NULL;
237 ret = ldb_msg_add_string(msg, "data", conv_str);
238 } else {
239 /* Provide a possibility to store also UTF8
240 * REG_DWORD values. This is done by adding a
241 * '\0' in front of the data */
242 struct ldb_val *val;
244 val = talloc_zero(msg, struct ldb_val);
245 if (val == NULL) {
246 talloc_free(msg);
247 return NULL;
250 val->data = talloc_size(msg, data.length + 1);
251 if (val->data == NULL) {
252 talloc_free(msg);
253 return NULL;
255 val->data[0] = '\0';
256 memcpy(val->data + 1, data.data, data.length);
257 val->length = data.length + 1;
258 ret = ldb_msg_add_value(msg, "data", val, NULL);
260 } else {
261 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
263 break;
265 case REG_QWORD:
266 if ((data.length > 0) && (data.data != NULL)) {
267 if (data.length == sizeof(uint64_t)) {
268 char *conv_str;
270 conv_str = talloc_asprintf(msg, "0x%16.16llx", BVAL(data.data, 0));
271 if (conv_str == NULL) {
272 talloc_free(msg);
273 return NULL;
275 ret = ldb_msg_add_string(msg, "data", conv_str);
276 } else {
277 /* Provide a possibility to store also UTF8
278 * REG_QWORD values. This is done by adding a
279 * '\0' in front of the data */
280 struct ldb_val *val;
282 val = talloc_zero(msg, struct ldb_val);
283 if (val == NULL) {
284 talloc_free(msg);
285 return NULL;
288 val->data = talloc_size(msg, data.length + 1);
289 if (val->data == NULL) {
290 talloc_free(msg);
291 return NULL;
293 val->data[0] = '\0';
294 memcpy(val->data + 1, data.data, data.length);
295 val->length = data.length + 1;
296 ret = ldb_msg_add_value(msg, "data", val, NULL);
298 } else {
299 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
301 break;
303 case REG_BINARY:
304 default:
305 if ((data.length > 0) && (data.data != NULL)) {
306 ret = ldb_msg_add_value(msg, "data", &data, NULL);
307 } else {
308 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
310 break;
313 if (ret != LDB_SUCCESS) {
314 talloc_free(msg);
315 return NULL;
318 type_str = talloc_asprintf(mem_ctx, "%u", type);
319 if (type_str == NULL) {
320 talloc_free(msg);
321 return NULL;
324 ret = ldb_msg_add_string(msg, "type", type_str);
325 if (ret != LDB_SUCCESS) {
326 talloc_free(msg);
327 return NULL;
330 return msg;
333 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
335 struct ldb_val val;
337 val.data = discard_const_p(uint8_t, value);
338 val.length = strlen(value);
340 return ldb_dn_escape_value(mem_ctx, val);
343 static int reg_close_ldb_key(struct ldb_key_data *key)
345 if (key->subkeys != NULL) {
346 talloc_free(key->subkeys);
347 key->subkeys = NULL;
350 if (key->values != NULL) {
351 talloc_free(key->values);
352 key->values = NULL;
354 return 0;
357 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
358 const struct hive_key *from,
359 const char *path, const char *add)
361 TALLOC_CTX *local_ctx;
362 struct ldb_dn *ret;
363 char *mypath = talloc_strdup(mem_ctx, path);
364 char *begin;
365 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
366 struct ldb_context *ldb = kd->ldb;
368 local_ctx = talloc_new(mem_ctx);
370 ret = ldb_dn_new(mem_ctx, ldb, add);
371 if (!ldb_dn_validate(ret)) {
372 talloc_free(ret);
373 talloc_free(local_ctx);
374 return NULL;
377 while (mypath) {
378 char *keyname;
380 begin = strrchr(mypath, '\\');
382 if (begin) keyname = begin + 1;
383 else keyname = mypath;
385 if (keyname[0] != '\0') {
386 if (!ldb_dn_add_base_fmt(ret, "key=%s",
387 reg_ldb_escape(local_ctx,
388 keyname)))
390 talloc_free(local_ctx);
391 return NULL;
395 if(begin) {
396 *begin = '\0';
397 } else {
398 break;
402 ldb_dn_add_base(ret, kd->dn);
404 talloc_free(local_ctx);
406 return ret;
409 static WERROR cache_subkeys(struct ldb_key_data *kd)
411 struct ldb_context *c = kd->ldb;
412 struct ldb_result *res;
413 int ret;
415 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "(key=*)");
417 if (ret != LDB_SUCCESS) {
418 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
419 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
420 return WERR_FOOBAR;
423 kd->subkey_count = res->count;
424 kd->subkeys = talloc_steal(kd, res->msgs);
425 talloc_free(res);
427 return WERR_OK;
430 static WERROR cache_values(struct ldb_key_data *kd)
432 struct ldb_context *c = kd->ldb;
433 struct ldb_result *res;
434 int ret;
436 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
437 NULL, "(value=*)");
439 if (ret != LDB_SUCCESS) {
440 DEBUG(0, ("Error getting values for '%s': %s\n",
441 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
442 return WERR_FOOBAR;
445 kd->value_count = res->count;
446 kd->values = talloc_steal(kd, res->msgs);
447 talloc_free(res);
449 return WERR_OK;
453 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
454 const struct hive_key *k, uint32_t idx,
455 const char **name,
456 const char **classname,
457 NTTIME *last_mod_time)
459 struct ldb_message_element *el;
460 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
462 /* Initialization */
463 if (name != NULL)
464 *name = NULL;
465 if (classname != NULL)
466 *classname = NULL; /* TODO: Store properly */
467 if (last_mod_time != NULL)
468 *last_mod_time = 0; /* TODO: we need to add this to the
469 ldb backend properly */
471 /* Do a search if necessary */
472 if (kd->subkeys == NULL) {
473 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
476 if (idx >= kd->subkey_count)
477 return WERR_NO_MORE_ITEMS;
479 el = ldb_msg_find_element(kd->subkeys[idx], "key");
480 SMB_ASSERT(el != NULL);
481 SMB_ASSERT(el->num_values != 0);
483 if (name != NULL)
484 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
486 return WERR_OK;
489 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
490 const char **name, uint32_t *data_type,
491 DATA_BLOB *data)
493 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
494 struct ldb_context *c = kd->ldb;
495 const char* attrs[] = { "data", "type", NULL };
496 struct ldb_result *res;
497 int ret;
499 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_BASE, attrs, "(key=*)");
501 if (ret != LDB_SUCCESS) {
502 DEBUG(0, ("Error getting default value for '%s': %s\n",
503 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
504 return WERR_FOOBAR;
507 if (res->count == 0 || res->msgs[0]->num_elements == 0)
508 return WERR_BADFILE;
510 reg_ldb_unpack_value(mem_ctx,
511 res->msgs[0], name, data_type, data);
513 talloc_free(res);
515 return WERR_OK;
518 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
519 uint32_t idx, const char **name,
520 uint32_t *data_type, DATA_BLOB *data)
522 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
524 /* if default value exists, give it back */
525 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
526 data))) {
527 if (idx == 0)
528 return WERR_OK;
529 else
530 --idx;
533 /* Do the search if necessary */
534 if (kd->values == NULL) {
535 W_ERROR_NOT_OK_RETURN(cache_values(kd));
538 if (idx >= kd->value_count)
539 return WERR_NO_MORE_ITEMS;
541 reg_ldb_unpack_value(mem_ctx, kd->values[idx], name, data_type, data);
543 return WERR_OK;
546 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
547 const char *name, uint32_t *data_type,
548 DATA_BLOB *data)
550 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
551 struct ldb_context *c = kd->ldb;
552 struct ldb_result *res;
553 int ret;
555 if (name == NULL) {
556 return WERR_INVALID_PARAM;
559 if (name[0] == '\0') {
560 /* default value */
561 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
562 } else {
563 /* normal value */
564 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_ONELEVEL,
565 NULL, "(value=%s)", name);
567 if (ret != LDB_SUCCESS) {
568 DEBUG(0, ("Error getting values for '%s': %s\n",
569 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
570 return WERR_FOOBAR;
573 if (res->count == 0)
574 return WERR_BADFILE;
576 reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
578 talloc_free(res);
581 return WERR_OK;
584 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
585 const char *name, struct hive_key **key)
587 struct ldb_result *res;
588 struct ldb_dn *ldap_path;
589 int ret;
590 struct ldb_key_data *newkd;
591 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
592 struct ldb_context *c = kd->ldb;
594 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
595 W_ERROR_HAVE_NO_MEMORY(ldap_path);
597 ret = ldb_search(c, mem_ctx, &res, ldap_path, LDB_SCOPE_BASE, NULL, "(key=*)");
599 if (ret != LDB_SUCCESS) {
600 DEBUG(3, ("Error opening key '%s': %s\n",
601 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
602 return WERR_FOOBAR;
603 } else if (res->count == 0) {
604 DEBUG(3, ("Key '%s' not found\n",
605 ldb_dn_get_linearized(ldap_path)));
606 talloc_free(res);
607 return WERR_BADFILE;
610 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
611 newkd->key.ops = &reg_backend_ldb;
612 newkd->ldb = talloc_reference(newkd, kd->ldb);
613 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
615 *key = (struct hive_key *)newkd;
617 return WERR_OK;
620 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
621 struct auth_session_info *session_info,
622 struct cli_credentials *credentials,
623 struct tevent_context *ev_ctx,
624 struct loadparm_context *lp_ctx,
625 struct hive_key **k)
627 struct ldb_key_data *kd;
628 struct ldb_context *wrap;
629 struct ldb_message *attrs_msg;
631 if (location == NULL)
632 return WERR_INVALID_PARAM;
634 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
635 location, session_info, credentials, 0);
637 if (wrap == NULL) {
638 DEBUG(1, (__FILE__": unable to connect\n"));
639 return WERR_FOOBAR;
642 attrs_msg = ldb_msg_new(wrap);
643 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
644 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
645 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
646 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
647 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
649 ldb_add(wrap, attrs_msg);
651 ldb_set_debug_stderr(wrap);
653 kd = talloc_zero(parent_ctx, struct ldb_key_data);
654 kd->key.ops = &reg_backend_ldb;
655 kd->ldb = talloc_reference(kd, wrap);
656 talloc_set_destructor (kd, reg_close_ldb_key);
657 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
659 *k = (struct hive_key *)kd;
661 return WERR_OK;
664 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
665 const char *name, const char *classname,
666 struct security_descriptor *sd,
667 struct hive_key **newkey)
669 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
670 struct ldb_message *msg;
671 struct ldb_key_data *newkd;
672 int ret;
674 msg = ldb_msg_new(mem_ctx);
675 W_ERROR_HAVE_NO_MEMORY(msg);
677 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
678 W_ERROR_HAVE_NO_MEMORY(msg->dn);
680 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
681 if (classname != NULL)
682 ldb_msg_add_string(msg, "classname",
683 talloc_strdup(mem_ctx, classname));
685 ret = ldb_add(parentkd->ldb, msg);
686 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
687 return WERR_ALREADY_EXISTS;
690 if (ret != LDB_SUCCESS) {
691 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
692 return WERR_FOOBAR;
695 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
697 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
698 W_ERROR_HAVE_NO_MEMORY(newkd);
699 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
700 newkd->key.ops = &reg_backend_ldb;
701 newkd->dn = talloc_steal(newkd, msg->dn);
703 *newkey = (struct hive_key *)newkd;
705 /* reset cache */
706 talloc_free(parentkd->subkeys);
707 parentkd->subkeys = NULL;
709 return WERR_OK;
712 static WERROR ldb_del_value (struct hive_key *key, const char *child)
714 int ret;
715 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
716 TALLOC_CTX *mem_ctx;
717 struct ldb_message *msg;
718 struct ldb_dn *childdn;
720 if ((child == NULL) || (child[0] == '\0')) {
721 /* default value */
722 mem_ctx = talloc_init("ldb_del_value");
724 msg = talloc_zero(mem_ctx, struct ldb_message);
725 W_ERROR_HAVE_NO_MEMORY(msg);
726 msg->dn = ldb_dn_copy(msg, kd->dn);
727 W_ERROR_HAVE_NO_MEMORY(msg->dn);
728 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
729 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
731 ret = ldb_modify(kd->ldb, msg);
732 if (ret != LDB_SUCCESS) {
733 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
734 talloc_free(mem_ctx);
735 return WERR_FOOBAR;
738 talloc_free(mem_ctx);
739 } else {
740 /* normal value */
741 childdn = ldb_dn_copy(kd->ldb, kd->dn);
742 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
743 reg_ldb_escape(childdn, child)))
745 talloc_free(childdn);
746 return WERR_FOOBAR;
749 ret = ldb_delete(kd->ldb, childdn);
751 talloc_free(childdn);
753 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
754 return WERR_BADFILE;
755 } else if (ret != LDB_SUCCESS) {
756 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
757 return WERR_FOOBAR;
761 /* reset cache */
762 talloc_free(kd->values);
763 kd->values = NULL;
765 return WERR_OK;
768 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
770 unsigned int i;
771 int ret;
772 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
773 struct ldb_dn *ldap_path;
774 TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
775 struct ldb_context *c = parentkd->ldb;
776 struct ldb_result *res_keys;
777 struct ldb_result *res_vals;
778 WERROR werr;
779 struct hive_key *hk;
781 /* Verify key exists by opening it */
782 werr = ldb_open_key(mem_ctx, key, name, &hk);
783 if (!W_ERROR_IS_OK(werr)) {
784 talloc_free(mem_ctx);
785 return werr;
788 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
789 W_ERROR_HAVE_NO_MEMORY(ldap_path);
791 /* Search for subkeys */
792 ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
793 NULL, "(key=*)");
795 if (ret != LDB_SUCCESS) {
796 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
797 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
798 talloc_free(mem_ctx);
799 return WERR_FOOBAR;
802 /* Search for values */
803 ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
804 NULL, "(value=*)");
806 if (ret != LDB_SUCCESS) {
807 DEBUG(0, ("Error getting values for '%s': %s\n",
808 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
809 talloc_free(mem_ctx);
810 return WERR_FOOBAR;
813 /* Start an explicit transaction */
814 ret = ldb_transaction_start(c);
816 if (ret != LDB_SUCCESS) {
817 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
818 talloc_free(mem_ctx);
819 return WERR_FOOBAR;
822 if (res_keys->count || res_vals->count)
824 /* Delete any subkeys */
825 for (i = 0; i < res_keys->count; i++)
827 werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
828 res_keys->msgs[i],
829 "key", NULL));
830 if (!W_ERROR_IS_OK(werr)) {
831 ret = ldb_transaction_cancel(c);
832 talloc_free(mem_ctx);
833 return werr;
837 /* Delete any values */
838 for (i = 0; i < res_vals->count; i++)
840 werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
841 res_vals->msgs[i],
842 "value", NULL));
843 if (!W_ERROR_IS_OK(werr)) {
844 ret = ldb_transaction_cancel(c);
845 talloc_free(mem_ctx);
846 return werr;
851 /* Delete the key itself */
852 ret = ldb_delete(c, ldap_path);
854 if (ret != LDB_SUCCESS)
856 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
857 ret = ldb_transaction_cancel(c);
858 talloc_free(mem_ctx);
859 return WERR_FOOBAR;
862 /* Commit the transaction */
863 ret = ldb_transaction_commit(c);
865 if (ret != LDB_SUCCESS)
867 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
868 ret = ldb_transaction_cancel(c);
869 talloc_free(mem_ctx);
870 return WERR_FOOBAR;
873 talloc_free(mem_ctx);
875 /* reset cache */
876 talloc_free(parentkd->subkeys);
877 parentkd->subkeys = NULL;
879 return WERR_OK;
882 static WERROR ldb_set_value(struct hive_key *parent,
883 const char *name, uint32_t type,
884 const DATA_BLOB data)
886 struct ldb_message *msg;
887 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
888 unsigned int i;
889 int ret;
890 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
892 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
893 W_ERROR_HAVE_NO_MEMORY(msg);
895 msg->dn = ldb_dn_copy(msg, kd->dn);
896 W_ERROR_HAVE_NO_MEMORY(msg->dn);
898 if ((name != NULL) && (name[0] != '\0')) {
899 /* For a default value, we add/overwrite the attributes to/of the hive.
900 For a normal value, we create a new child. */
901 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
902 reg_ldb_escape(mem_ctx, name)))
904 talloc_free(mem_ctx);
905 return WERR_FOOBAR;
909 /* Try first a "modify" and if this doesn't work do try an "add" */
910 for (i = 0; i < msg->num_elements; i++) {
911 if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE) {
912 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
915 ret = ldb_modify(kd->ldb, msg);
916 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
917 i = 0;
918 while (i < msg->num_elements) {
919 if (msg->elements[i].flags == LDB_FLAG_MOD_DELETE) {
920 ldb_msg_remove_element(msg, &msg->elements[i]);
921 } else {
922 ++i;
925 ret = ldb_add(kd->ldb, msg);
927 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
928 /* ignore this -> the value didn't exist and also now doesn't */
929 ret = LDB_SUCCESS;
932 if (ret != LDB_SUCCESS) {
933 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
934 talloc_free(mem_ctx);
935 return WERR_FOOBAR;
938 /* reset cache */
939 talloc_free(kd->values);
940 kd->values = NULL;
942 talloc_free(mem_ctx);
943 return WERR_OK;
946 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
947 const struct hive_key *key,
948 const char **classname,
949 uint32_t *num_subkeys,
950 uint32_t *num_values,
951 NTTIME *last_change_time,
952 uint32_t *max_subkeynamelen,
953 uint32_t *max_valnamelen,
954 uint32_t *max_valbufsize)
956 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
958 /* Initialization */
959 if (classname != NULL)
960 *classname = NULL;
961 if (num_subkeys != NULL)
962 *num_subkeys = 0;
963 if (num_values != NULL)
964 *num_values = 0;
965 if (last_change_time != NULL)
966 *last_change_time = 0;
967 if (max_subkeynamelen != NULL)
968 *max_subkeynamelen = 0;
969 if (max_valnamelen != NULL)
970 *max_valnamelen = 0;
971 if (max_valbufsize != NULL)
972 *max_valbufsize = 0;
974 if (kd->subkeys == NULL) {
975 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
978 if (kd->values == NULL) {
979 W_ERROR_NOT_OK_RETURN(cache_values(kd));
982 if (num_subkeys != NULL) {
983 *num_subkeys = kd->subkey_count;
985 if (num_values != NULL) {
986 *num_values = kd->value_count;
990 if (max_subkeynamelen != NULL) {
991 unsigned int i;
992 struct ldb_message_element *el;
994 for (i = 0; i < kd->subkey_count; i++) {
995 el = ldb_msg_find_element(kd->subkeys[i], "key");
996 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
999 /* for UTF16 encoding */
1000 *max_subkeynamelen *= 2;
1003 if (max_valnamelen != NULL || max_valbufsize != NULL) {
1004 unsigned int i;
1005 struct ldb_message_element *el;
1006 W_ERROR_NOT_OK_RETURN(cache_values(kd));
1008 for (i = 0; i < kd->value_count; i++) {
1009 if (max_valnamelen != NULL) {
1010 el = ldb_msg_find_element(kd->values[i], "value");
1011 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
1014 if (max_valbufsize != NULL) {
1015 uint32_t data_type;
1016 DATA_BLOB data;
1017 reg_ldb_unpack_value(mem_ctx,
1018 kd->values[i], NULL,
1019 &data_type, &data);
1020 *max_valbufsize = MAX(*max_valbufsize, data.length);
1021 talloc_free(data.data);
1025 if (max_valnamelen != NULL) {
1026 /* for UTF16 encoding */
1027 *max_valnamelen *= 2;
1031 return WERR_OK;
1034 static struct hive_operations reg_backend_ldb = {
1035 .name = "ldb",
1036 .add_key = ldb_add_key,
1037 .del_key = ldb_del_key,
1038 .get_key_by_name = ldb_open_key,
1039 .enum_value = ldb_get_value_by_id,
1040 .enum_key = ldb_get_subkey_by_id,
1041 .set_value = ldb_set_value,
1042 .get_value_by_name = ldb_get_value,
1043 .delete_value = ldb_del_value,
1044 .get_key_info = ldb_get_key_info,