2 Unix SMB/CIFS implementation.
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/>.
23 #include "lib/ldb/include/ldb.h"
24 #include "lib/ldb/include/ldb_errors.h"
26 #include "librpc/gen_ndr/winreg.h"
27 #include "param/param.h"
29 static struct hive_operations reg_backend_ldb;
34 struct ldb_context *ldb;
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,
45 const struct ldb_val *val;
49 *name = talloc_strdup(mem_ctx,
50 ldb_msg_find_attr_as_string(msg, "value",
54 value_type = ldb_msg_find_attr_as_uint(msg, "type", 0);
57 val = ldb_msg_find_ldb_val(msg, "data");
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);
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,
78 data->length = val->length - 1;
87 case REG_DWORD_BIG_ENDIAN:
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);
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,
106 data->length = val->length - 1;
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);
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,
133 data->length = val->length - 1;
144 data->data = talloc_memdup(mem_ctx, val->data,
146 data->length = val->length;
155 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
158 uint32_t type, DATA_BLOB data)
160 struct ldb_message *msg;
161 char *name_dup, *type_str;
164 msg = talloc_zero(mem_ctx, struct ldb_message);
169 name_dup = talloc_strdup(msg, name);
170 if (name_dup == NULL) {
175 ret = ldb_msg_add_string(msg, "value", name_dup);
176 if (ret != LDB_SUCCESS) {
184 if ((data.length > 0) && (data.data != NULL)) {
188 val = talloc_zero(msg, struct ldb_val);
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,
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) {
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);
221 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
226 case REG_DWORD_BIG_ENDIAN:
227 if ((data.length > 0) && (data.data != NULL)) {
228 if (data.length == sizeof(uint32_t)) {
231 conv_str = talloc_asprintf(msg, "0x%8.8x",
233 if (conv_str == NULL) {
237 ret = ldb_msg_add_string(msg, "data", conv_str);
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 */
244 val = talloc_zero(msg, struct ldb_val);
250 val->data = talloc_size(msg, data.length + 1);
251 if (val->data == NULL) {
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);
261 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
266 if ((data.length > 0) && (data.data != NULL)) {
267 if (data.length == sizeof(uint64_t)) {
270 conv_str = talloc_asprintf(msg, "0x%16.16llx", BVAL(data.data, 0));
271 if (conv_str == NULL) {
275 ret = ldb_msg_add_string(msg, "data", conv_str);
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 */
282 val = talloc_zero(msg, struct ldb_val);
288 val->data = talloc_size(msg, data.length + 1);
289 if (val->data == NULL) {
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);
299 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
305 if ((data.length > 0) && (data.data != NULL)) {
306 ret = ldb_msg_add_value(msg, "data", &data, NULL);
308 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
313 if (ret != LDB_SUCCESS) {
318 type_str = talloc_asprintf(mem_ctx, "%u", type);
319 if (type_str == NULL) {
324 ret = ldb_msg_add_string(msg, "type", type_str);
325 if (ret != LDB_SUCCESS) {
333 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
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);
350 if (key->values != NULL) {
351 talloc_free(key->values);
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;
363 char *mypath = talloc_strdup(mem_ctx, path);
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)) {
373 talloc_free(local_ctx);
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,
390 talloc_free(local_ctx);
402 ldb_dn_add_base(ret, kd->dn);
404 talloc_free(local_ctx);
409 static WERROR cache_subkeys(struct ldb_key_data *kd)
411 struct ldb_context *c = kd->ldb;
412 struct ldb_result *res;
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)));
423 kd->subkey_count = res->count;
424 kd->subkeys = talloc_steal(kd, res->msgs);
430 static WERROR cache_values(struct ldb_key_data *kd)
432 struct ldb_context *c = kd->ldb;
433 struct ldb_result *res;
436 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
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)));
445 kd->value_count = res->count;
446 kd->values = talloc_steal(kd, res->msgs);
453 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
454 const struct hive_key *k, uint32_t idx,
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);
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);
484 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
489 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx,
490 const struct hive_key *k,
491 const char **name, uint32_t *data_type,
494 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
495 struct ldb_context *c = kd->ldb;
496 const char* attrs[] = { "data", "type", NULL };
497 struct ldb_result *res;
500 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_BASE, attrs, "(dn=*)");
502 if (ret != LDB_SUCCESS) {
503 DEBUG(0, ("Error getting default value for '%s': %s\n",
504 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
508 if (res->count == 0 || res->msgs[0]->num_elements == 0)
511 if ((data_type != NULL) && (data != NULL)) {
512 reg_ldb_unpack_value(mem_ctx, res->msgs[0], name, data_type,
521 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
522 uint32_t idx, const char **name,
523 uint32_t *data_type, DATA_BLOB *data)
525 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
527 /* if default value exists, give it back */
528 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
536 /* Do the search if necessary */
537 if (kd->values == NULL) {
538 W_ERROR_NOT_OK_RETURN(cache_values(kd));
541 if (idx >= kd->value_count)
542 return WERR_NO_MORE_ITEMS;
544 reg_ldb_unpack_value(mem_ctx, kd->values[idx], name, data_type, data);
549 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
550 const char *name, uint32_t *data_type,
553 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
554 struct ldb_context *c = kd->ldb;
555 struct ldb_result *res;
559 return WERR_INVALID_PARAM;
562 if (name[0] == '\0') {
564 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
567 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_ONELEVEL,
568 NULL, "(value=%s)", name);
570 if (ret != LDB_SUCCESS) {
571 DEBUG(0, ("Error getting values for '%s': %s\n",
572 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
579 reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
587 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
588 const char *name, struct hive_key **key)
590 struct ldb_result *res;
591 struct ldb_dn *ldap_path;
593 struct ldb_key_data *newkd;
594 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
595 struct ldb_context *c = kd->ldb;
597 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
598 W_ERROR_HAVE_NO_MEMORY(ldap_path);
600 ret = ldb_search(c, mem_ctx, &res, ldap_path, LDB_SCOPE_BASE, NULL, "(key=*)");
602 if (ret != LDB_SUCCESS) {
603 DEBUG(3, ("Error opening key '%s': %s\n",
604 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
606 } else if (res->count == 0) {
607 DEBUG(3, ("Key '%s' not found\n",
608 ldb_dn_get_linearized(ldap_path)));
613 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
614 newkd->key.ops = ®_backend_ldb;
615 newkd->ldb = talloc_reference(newkd, kd->ldb);
616 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
618 *key = (struct hive_key *)newkd;
623 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
624 struct auth_session_info *session_info,
625 struct cli_credentials *credentials,
626 struct tevent_context *ev_ctx,
627 struct loadparm_context *lp_ctx,
630 struct ldb_key_data *kd;
631 struct ldb_context *wrap;
632 struct ldb_message *attrs_msg;
634 if (location == NULL)
635 return WERR_INVALID_PARAM;
637 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
638 location, session_info, credentials, 0);
641 DEBUG(1, (__FILE__": unable to connect\n"));
645 attrs_msg = ldb_msg_new(wrap);
646 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
647 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
648 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
649 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
650 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
652 ldb_add(wrap, attrs_msg);
654 ldb_set_debug_stderr(wrap);
656 kd = talloc_zero(parent_ctx, struct ldb_key_data);
657 kd->key.ops = ®_backend_ldb;
658 kd->ldb = talloc_reference(kd, wrap);
659 talloc_set_destructor (kd, reg_close_ldb_key);
660 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
662 *k = (struct hive_key *)kd;
667 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
668 const char *name, const char *classname,
669 struct security_descriptor *sd,
670 struct hive_key **newkey)
672 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
673 struct ldb_message *msg;
674 struct ldb_key_data *newkd;
677 msg = ldb_msg_new(mem_ctx);
678 W_ERROR_HAVE_NO_MEMORY(msg);
680 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
681 W_ERROR_HAVE_NO_MEMORY(msg->dn);
683 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
684 if (classname != NULL)
685 ldb_msg_add_string(msg, "classname",
686 talloc_strdup(mem_ctx, classname));
688 ret = ldb_add(parentkd->ldb, msg);
689 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
690 return WERR_ALREADY_EXISTS;
693 if (ret != LDB_SUCCESS) {
694 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
698 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
700 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
701 W_ERROR_HAVE_NO_MEMORY(newkd);
702 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
703 newkd->key.ops = ®_backend_ldb;
704 newkd->dn = talloc_steal(newkd, msg->dn);
706 *newkey = (struct hive_key *)newkd;
709 talloc_free(parentkd->subkeys);
710 parentkd->subkeys = NULL;
715 static WERROR ldb_del_value(TALLOC_CTX *mem_ctx, struct hive_key *key,
719 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
720 struct ldb_message *msg;
721 struct ldb_dn *childdn;
723 if ((child == NULL) || (child[0] == '\0')) {
725 msg = talloc_zero(mem_ctx, struct ldb_message);
726 W_ERROR_HAVE_NO_MEMORY(msg);
727 msg->dn = ldb_dn_copy(msg, kd->dn);
728 W_ERROR_HAVE_NO_MEMORY(msg->dn);
729 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
730 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
732 ret = ldb_modify(kd->ldb, msg);
733 if (ret != LDB_SUCCESS) {
734 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
739 childdn = ldb_dn_copy(kd->ldb, kd->dn);
740 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
741 reg_ldb_escape(childdn, child)))
743 talloc_free(childdn);
747 ret = ldb_delete(kd->ldb, childdn);
749 talloc_free(childdn);
751 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
753 } else if (ret != LDB_SUCCESS) {
754 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
760 talloc_free(kd->values);
766 static WERROR ldb_del_key(TALLOC_CTX *mem_ctx, const struct hive_key *key,
771 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
772 struct ldb_dn *ldap_path;
773 struct ldb_context *c = parentkd->ldb;
774 struct ldb_result *res_keys;
775 struct ldb_result *res_vals;
779 /* Verify key exists by opening it */
780 werr = ldb_open_key(mem_ctx, key, name, &hk);
781 if (!W_ERROR_IS_OK(werr)) {
785 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
786 W_ERROR_HAVE_NO_MEMORY(ldap_path);
788 /* Search for subkeys */
789 ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
792 if (ret != LDB_SUCCESS) {
793 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
794 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
798 /* Search for values */
799 ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
802 if (ret != LDB_SUCCESS) {
803 DEBUG(0, ("Error getting values for '%s': %s\n",
804 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
808 /* Start an explicit transaction */
809 ret = ldb_transaction_start(c);
811 if (ret != LDB_SUCCESS) {
812 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
816 if (res_keys->count || res_vals->count)
818 /* Delete any subkeys */
819 for (i = 0; i < res_keys->count; i++)
821 werr = ldb_del_key(mem_ctx, hk,
822 ldb_msg_find_attr_as_string(
825 if (!W_ERROR_IS_OK(werr)) {
826 ret = ldb_transaction_cancel(c);
831 /* Delete any values */
832 for (i = 0; i < res_vals->count; i++)
834 werr = ldb_del_value(mem_ctx, hk,
835 ldb_msg_find_attr_as_string(
838 if (!W_ERROR_IS_OK(werr)) {
839 ret = ldb_transaction_cancel(c);
845 /* Delete the key itself */
846 ret = ldb_delete(c, ldap_path);
848 if (ret != LDB_SUCCESS)
850 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
851 ret = ldb_transaction_cancel(c);
855 /* Commit the transaction */
856 ret = ldb_transaction_commit(c);
858 if (ret != LDB_SUCCESS)
860 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
861 ret = ldb_transaction_cancel(c);
866 talloc_free(parentkd->subkeys);
867 parentkd->subkeys = NULL;
872 static WERROR ldb_set_value(struct hive_key *parent,
873 const char *name, uint32_t type,
874 const DATA_BLOB data)
876 struct ldb_message *msg;
877 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
880 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
882 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
883 W_ERROR_HAVE_NO_MEMORY(msg);
885 msg->dn = ldb_dn_copy(msg, kd->dn);
886 W_ERROR_HAVE_NO_MEMORY(msg->dn);
888 if ((name != NULL) && (name[0] != '\0')) {
889 /* For a default value, we add/overwrite the attributes to/of the hive.
890 For a normal value, we create a new child. */
891 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
892 reg_ldb_escape(mem_ctx, name)))
894 talloc_free(mem_ctx);
899 /* Try first a "modify" and if this doesn't work do try an "add" */
900 for (i = 0; i < msg->num_elements; i++) {
901 if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE) {
902 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
905 ret = ldb_modify(kd->ldb, msg);
906 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
908 while (i < msg->num_elements) {
909 if (msg->elements[i].flags == LDB_FLAG_MOD_DELETE) {
910 ldb_msg_remove_element(msg, &msg->elements[i]);
915 ret = ldb_add(kd->ldb, msg);
917 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
918 /* ignore this -> the value didn't exist and also now doesn't */
922 if (ret != LDB_SUCCESS) {
923 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
924 talloc_free(mem_ctx);
929 talloc_free(kd->values);
932 talloc_free(mem_ctx);
936 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
937 const struct hive_key *key,
938 const char **classname,
939 uint32_t *num_subkeys,
940 uint32_t *num_values,
941 NTTIME *last_change_time,
942 uint32_t *max_subkeynamelen,
943 uint32_t *max_valnamelen,
944 uint32_t *max_valbufsize)
946 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
947 uint32_t default_value_type = REG_NONE;
948 DATA_BLOB default_value = { NULL, 0 };
952 if (classname != NULL)
954 if (num_subkeys != NULL)
956 if (num_values != NULL)
958 if (last_change_time != NULL)
959 *last_change_time = 0;
960 if (max_subkeynamelen != NULL)
961 *max_subkeynamelen = 0;
962 if (max_valnamelen != NULL)
964 if (max_valbufsize != NULL)
967 /* We need this to get the default value (if it exists) for counting
968 * the values under the key and for finding out the longest value buffer
969 * size. If no default value exists the DATA_BLOB "default_value" will
970 * remain { NULL, 0 }. */
971 werr = ldb_get_default_value(mem_ctx, key, NULL, &default_value_type,
973 if ((!W_ERROR_IS_OK(werr)) && (!W_ERROR_EQUAL(werr, WERR_BADFILE))) {
977 if (kd->subkeys == NULL) {
978 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
981 if (kd->values == NULL) {
982 W_ERROR_NOT_OK_RETURN(cache_values(kd));
985 if (num_subkeys != NULL) {
986 *num_subkeys = kd->subkey_count;
988 if (num_values != NULL) {
989 *num_values = kd->value_count;
990 /* also consider the default value if it exists */
991 if (default_value.data != NULL) {
997 if (max_subkeynamelen != NULL) {
999 struct ldb_message_element *el;
1001 for (i = 0; i < kd->subkey_count; i++) {
1002 el = ldb_msg_find_element(kd->subkeys[i], "key");
1003 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
1006 /* for UTF16 encoding */
1007 *max_subkeynamelen *= 2;
1010 if (max_valnamelen != NULL || max_valbufsize != NULL) {
1012 struct ldb_message_element *el;
1013 W_ERROR_NOT_OK_RETURN(cache_values(kd));
1015 /* also consider the default value if it exists */
1016 if ((max_valbufsize != NULL) && (default_value.data != NULL)) {
1017 *max_valbufsize = MAX(*max_valbufsize,
1018 default_value.length);
1021 for (i = 0; i < kd->value_count; i++) {
1022 if (max_valnamelen != NULL) {
1023 el = ldb_msg_find_element(kd->values[i], "value");
1024 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
1027 if (max_valbufsize != NULL) {
1030 reg_ldb_unpack_value(mem_ctx,
1031 kd->values[i], NULL,
1033 *max_valbufsize = MAX(*max_valbufsize, data.length);
1034 talloc_free(data.data);
1038 if (max_valnamelen != NULL) {
1039 /* for UTF16 encoding */
1040 *max_valnamelen *= 2;
1044 talloc_free(default_value.data);
1049 static struct hive_operations reg_backend_ldb = {
1051 .add_key = ldb_add_key,
1052 .del_key = ldb_del_key,
1053 .get_key_by_name = ldb_open_key,
1054 .enum_value = ldb_get_value_by_id,
1055 .enum_key = ldb_get_subkey_by_id,
1056 .set_value = ldb_set_value,
1057 .get_value_by_name = ldb_get_value,
1058 .delete_value = ldb_del_value,
1059 .get_key_info = ldb_get_key_info,