s4:libregistry - change counters to be "unsigned"
[Samba.git] / source4 / lib / registry / ldb.c
blob8ed4b5d847ddd1200fc10e63f00268575a6a4866
1 /*
2 Unix SMB/CIFS implementation.
3 Registry interface
4 Copyright (C) 2004-2007, Jelmer Vernooij, jelmer@samba.org
5 Copyright (C) 2008 Matthias Dieter Wallnöfer, mwallnoefer@yahoo.de
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));
53 value_type = ldb_msg_find_attr_as_uint(msg, "type", 0);
54 *type = value_type;
56 val = ldb_msg_find_ldb_val(msg, "data");
58 switch (value_type)
60 case REG_SZ:
61 case REG_EXPAND_SZ:
62 if (val != NULL)
63 convert_string_talloc(mem_ctx, CH_UTF8, CH_UTF16,
64 val->data, val->length,
65 (void **)&data->data, &data->length, false);
66 else {
67 data->data = NULL;
68 data->length = 0;
70 break;
72 case REG_BINARY:
73 if (val != NULL)
74 *data = data_blob_talloc(mem_ctx, val->data, val->length);
75 else {
76 data->data = NULL;
77 data->length = 0;
79 break;
81 case REG_DWORD: {
82 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
83 *data = data_blob_talloc(mem_ctx, NULL, 4);
84 SIVAL(data->data, 0, tmp);
86 break;
88 default:
89 *data = data_blob_talloc(mem_ctx, val->data, val->length);
90 break;
94 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
95 TALLOC_CTX *mem_ctx,
96 const char *name,
97 uint32_t type, DATA_BLOB data)
99 struct ldb_val val;
100 struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
101 char *type_s;
103 ldb_msg_add_string(msg, "value", talloc_strdup(mem_ctx, name));
105 switch (type) {
106 case REG_SZ:
107 case REG_EXPAND_SZ:
108 if (data.data[0] != '\0') {
109 convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8,
110 (void *)data.data,
111 data.length,
112 (void **)&val.data, &val.length, false);
113 ldb_msg_add_value(msg, "data", &val, NULL);
114 } else {
115 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
117 break;
119 case REG_BINARY:
120 if (data.length > 0)
121 ldb_msg_add_value(msg, "data", &data, NULL);
122 else
123 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
124 break;
126 case REG_DWORD:
127 ldb_msg_add_string(msg, "data",
128 talloc_asprintf(mem_ctx, "0x%x",
129 IVAL(data.data, 0)));
130 break;
131 default:
132 ldb_msg_add_value(msg, "data", &data, NULL);
136 type_s = talloc_asprintf(mem_ctx, "%u", type);
137 ldb_msg_add_string(msg, "type", type_s);
139 return msg;
142 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
144 struct ldb_val val;
146 val.data = discard_const_p(uint8_t, value);
147 val.length = strlen(value);
149 return ldb_dn_escape_value(mem_ctx, val);
152 static int reg_close_ldb_key(struct ldb_key_data *key)
154 if (key->subkeys != NULL) {
155 talloc_free(key->subkeys);
156 key->subkeys = NULL;
159 if (key->values != NULL) {
160 talloc_free(key->values);
161 key->values = NULL;
163 return 0;
166 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
167 const struct hive_key *from,
168 const char *path, const char *add)
170 TALLOC_CTX *local_ctx;
171 struct ldb_dn *ret;
172 char *mypath = talloc_strdup(mem_ctx, path);
173 char *begin;
174 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
175 struct ldb_context *ldb = kd->ldb;
177 local_ctx = talloc_new(mem_ctx);
179 if (add) {
180 ret = ldb_dn_new(mem_ctx, ldb, add);
181 } else {
182 ret = ldb_dn_new(mem_ctx, ldb, NULL);
184 if (!ldb_dn_validate(ret)) {
185 talloc_free(ret);
186 talloc_free(local_ctx);
187 return NULL;
190 while (mypath) {
191 char *keyname;
193 begin = strrchr(mypath, '\\');
195 if (begin) keyname = begin + 1;
196 else keyname = mypath;
198 if(strlen(keyname)) {
199 if (!ldb_dn_add_base_fmt(ret, "key=%s",
200 reg_ldb_escape(local_ctx,
201 keyname)))
203 talloc_free(local_ctx);
204 return NULL;
208 if(begin) {
209 *begin = '\0';
210 } else {
211 break;
215 ldb_dn_add_base(ret, kd->dn);
217 talloc_free(local_ctx);
219 return ret;
222 static WERROR cache_subkeys(struct ldb_key_data *kd)
224 struct ldb_context *c = kd->ldb;
225 struct ldb_result *res;
226 int ret;
228 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "(key=*)");
230 if (ret != LDB_SUCCESS) {
231 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
232 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
233 return WERR_FOOBAR;
236 kd->subkey_count = res->count;
237 kd->subkeys = talloc_steal(kd, res->msgs);
238 talloc_free(res);
240 return WERR_OK;
243 static WERROR cache_values(struct ldb_key_data *kd)
245 struct ldb_context *c = kd->ldb;
246 struct ldb_result *res;
247 int ret;
249 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
250 NULL, "(value=*)");
252 if (ret != LDB_SUCCESS) {
253 DEBUG(0, ("Error getting values for '%s': %s\n",
254 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
255 return WERR_FOOBAR;
258 kd->value_count = res->count;
259 kd->values = talloc_steal(kd, res->msgs);
260 talloc_free(res);
262 return WERR_OK;
266 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
267 const struct hive_key *k, uint32_t idx,
268 const char **name,
269 const char **classname,
270 NTTIME *last_mod_time)
272 struct ldb_message_element *el;
273 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
275 /* Initialization */
276 if (name != NULL)
277 *name = NULL;
278 if (classname != NULL)
279 *classname = NULL; /* TODO: Store properly */
280 if (last_mod_time != NULL)
281 *last_mod_time = 0; /* TODO: we need to add this to the
282 ldb backend properly */
284 /* Do a search if necessary */
285 if (kd->subkeys == NULL) {
286 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
289 if (idx >= kd->subkey_count)
290 return WERR_NO_MORE_ITEMS;
292 el = ldb_msg_find_element(kd->subkeys[idx], "key");
293 SMB_ASSERT(el != NULL);
294 SMB_ASSERT(el->num_values != 0);
296 if (name != NULL)
297 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
299 return WERR_OK;
302 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
303 const char **name, uint32_t *data_type,
304 DATA_BLOB *data)
306 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
307 struct ldb_context *c = kd->ldb;
308 const char* attrs[] = { "data", "type", NULL };
309 struct ldb_result *res;
310 int ret;
312 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_BASE, attrs, "%s", "");
314 if (ret != LDB_SUCCESS) {
315 DEBUG(0, ("Error getting default value for '%s': %s\n",
316 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
317 return WERR_FOOBAR;
320 if (res->count == 0 || res->msgs[0]->num_elements == 0)
321 return WERR_BADFILE;
323 reg_ldb_unpack_value(mem_ctx,
324 res->msgs[0], name, data_type, data);
326 talloc_free(res);
328 return WERR_OK;
331 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
332 uint32_t idx, const char **name,
333 uint32_t *data_type, DATA_BLOB *data)
335 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
337 /* if default value exists, give it back */
338 if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
339 data))) {
340 if (idx == 0)
341 return WERR_OK;
342 else
343 --idx;
346 /* Do the search if necessary */
347 if (kd->values == NULL) {
348 W_ERROR_NOT_OK_RETURN(cache_values(kd));
351 if (idx >= kd->value_count)
352 return WERR_NO_MORE_ITEMS;
354 reg_ldb_unpack_value(mem_ctx, kd->values[idx], name, data_type, data);
356 return WERR_OK;
359 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
360 const char *name, uint32_t *data_type,
361 DATA_BLOB *data)
363 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
364 struct ldb_context *c = kd->ldb;
365 struct ldb_result *res;
366 int ret;
367 char *query;
369 if (strlen(name) == 0) {
370 /* default value */
371 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
372 } else {
373 /* normal value */
374 query = talloc_asprintf(mem_ctx, "(value=%s)", name);
375 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "%s", query);
376 talloc_free(query);
378 if (ret != LDB_SUCCESS) {
379 DEBUG(0, ("Error getting values for '%s': %s\n",
380 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
381 return WERR_FOOBAR;
384 if (res->count == 0)
385 return WERR_BADFILE;
387 reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
389 talloc_free(res);
392 return WERR_OK;
395 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
396 const char *name, struct hive_key **key)
398 struct ldb_result *res;
399 struct ldb_dn *ldap_path;
400 int ret;
401 struct ldb_key_data *newkd;
402 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
403 struct ldb_context *c = kd->ldb;
405 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
407 ret = ldb_search(c, mem_ctx, &res, ldap_path, LDB_SCOPE_BASE, NULL, "(key=*)");
409 if (ret != LDB_SUCCESS) {
410 DEBUG(3, ("Error opening key '%s': %s\n",
411 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
412 return WERR_FOOBAR;
413 } else if (res->count == 0) {
414 DEBUG(3, ("Key '%s' not found\n",
415 ldb_dn_get_linearized(ldap_path)));
416 talloc_free(res);
417 return WERR_BADFILE;
420 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
421 newkd->key.ops = &reg_backend_ldb;
422 newkd->ldb = talloc_reference(newkd, kd->ldb);
423 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
425 *key = (struct hive_key *)newkd;
427 return WERR_OK;
430 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
431 struct auth_session_info *session_info,
432 struct cli_credentials *credentials,
433 struct tevent_context *ev_ctx,
434 struct loadparm_context *lp_ctx,
435 struct hive_key **k)
437 struct ldb_key_data *kd;
438 struct ldb_context *wrap;
439 struct ldb_message *attrs_msg;
441 if (location == NULL)
442 return WERR_INVALID_PARAM;
444 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
445 location, session_info, credentials, 0);
447 if (wrap == NULL) {
448 DEBUG(1, (__FILE__": unable to connect\n"));
449 return WERR_FOOBAR;
452 attrs_msg = ldb_msg_new(wrap);
453 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
454 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
455 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
456 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
457 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
459 ldb_add(wrap, attrs_msg);
461 ldb_set_debug_stderr(wrap);
463 kd = talloc_zero(parent_ctx, struct ldb_key_data);
464 kd->key.ops = &reg_backend_ldb;
465 kd->ldb = talloc_reference(kd, wrap);
466 talloc_set_destructor (kd, reg_close_ldb_key);
467 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
469 *k = (struct hive_key *)kd;
471 return WERR_OK;
474 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
475 const char *name, const char *classname,
476 struct security_descriptor *sd,
477 struct hive_key **newkey)
479 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
480 struct ldb_message *msg;
481 struct ldb_key_data *newkd;
482 int ret;
484 msg = ldb_msg_new(mem_ctx);
486 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
488 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
489 if (classname != NULL)
490 ldb_msg_add_string(msg, "classname",
491 talloc_strdup(mem_ctx, classname));
493 ret = ldb_add(parentkd->ldb, msg);
494 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
495 return WERR_ALREADY_EXISTS;
498 if (ret != LDB_SUCCESS) {
499 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
500 return WERR_FOOBAR;
503 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
505 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
506 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
507 newkd->key.ops = &reg_backend_ldb;
508 newkd->dn = talloc_steal(newkd, msg->dn);
510 *newkey = (struct hive_key *)newkd;
512 /* reset cache */
513 talloc_free(parentkd->subkeys);
514 parentkd->subkeys = NULL;
516 return WERR_OK;
519 static WERROR ldb_del_value (struct hive_key *key, const char *child)
521 int ret;
522 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
523 TALLOC_CTX *mem_ctx;
524 struct ldb_message *msg;
525 struct ldb_dn *childdn;
527 if (strlen(child) == 0) {
528 /* default value */
529 mem_ctx = talloc_init("ldb_del_value");
531 msg = talloc_zero(mem_ctx, struct ldb_message);
532 msg->dn = ldb_dn_copy(msg, kd->dn);
533 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
534 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
536 ret = ldb_modify(kd->ldb, msg);
537 if (ret != LDB_SUCCESS) {
538 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
539 talloc_free(mem_ctx);
540 return WERR_FOOBAR;
543 talloc_free(mem_ctx);
544 } else {
545 /* normal value */
546 childdn = ldb_dn_copy(kd->ldb, kd->dn);
547 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
548 reg_ldb_escape(childdn, child)))
550 talloc_free(childdn);
551 return WERR_FOOBAR;
554 ret = ldb_delete(kd->ldb, childdn);
556 talloc_free(childdn);
558 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
559 return WERR_BADFILE;
560 } else if (ret != LDB_SUCCESS) {
561 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
562 return WERR_FOOBAR;
566 /* reset cache */
567 talloc_free(kd->values);
568 kd->values = NULL;
570 return WERR_OK;
573 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
575 unsigned int i;
576 int ret;
577 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
578 struct ldb_dn *ldap_path;
579 TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
580 struct ldb_context *c = parentkd->ldb;
581 struct ldb_result *res_keys;
582 struct ldb_result *res_vals;
583 WERROR werr;
584 struct hive_key *hk;
586 /* Verify key exists by opening it */
587 werr = ldb_open_key(mem_ctx, key, name, &hk);
588 if (!W_ERROR_IS_OK(werr)) {
589 talloc_free(mem_ctx);
590 return werr;
593 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
594 if (!ldap_path) {
595 talloc_free(mem_ctx);
596 return WERR_FOOBAR;
599 /* Search for subkeys */
600 ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
601 NULL, "(key=*)");
603 if (ret != LDB_SUCCESS) {
604 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
605 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
606 talloc_free(mem_ctx);
607 return WERR_FOOBAR;
610 /* Search for values */
611 ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
612 NULL, "(value=*)");
614 if (ret != LDB_SUCCESS) {
615 DEBUG(0, ("Error getting values for '%s': %s\n",
616 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
617 talloc_free(mem_ctx);
618 return WERR_FOOBAR;
621 /* Start an explicit transaction */
622 ret = ldb_transaction_start(c);
624 if (ret != LDB_SUCCESS) {
625 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
626 talloc_free(mem_ctx);
627 return WERR_FOOBAR;
630 if (res_keys->count || res_vals->count)
632 /* Delete any subkeys */
633 for (i = 0; i < res_keys->count; i++)
635 werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
636 res_keys->msgs[i],
637 "key", NULL));
638 if (!W_ERROR_IS_OK(werr)) {
639 ret = ldb_transaction_cancel(c);
640 talloc_free(mem_ctx);
641 return werr;
645 /* Delete any values */
646 for (i = 0; i < res_vals->count; i++)
648 werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
649 res_vals->msgs[i],
650 "value", NULL));
651 if (!W_ERROR_IS_OK(werr)) {
652 ret = ldb_transaction_cancel(c);
653 talloc_free(mem_ctx);
654 return werr;
659 /* Delete the key itself */
660 ret = ldb_delete(c, ldap_path);
662 if (ret != LDB_SUCCESS)
664 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
665 ret = ldb_transaction_cancel(c);
666 talloc_free(mem_ctx);
667 return WERR_FOOBAR;
670 /* Commit the transaction */
671 ret = ldb_transaction_commit(c);
673 if (ret != LDB_SUCCESS)
675 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
676 ret = ldb_transaction_cancel(c);
677 talloc_free(mem_ctx);
678 return WERR_FOOBAR;
681 talloc_free(mem_ctx);
683 /* reset cache */
684 talloc_free(parentkd->subkeys);
685 parentkd->subkeys = NULL;
687 return WERR_OK;
690 static WERROR ldb_set_value(struct hive_key *parent,
691 const char *name, uint32_t type,
692 const DATA_BLOB data)
694 struct ldb_message *msg;
695 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
696 int ret;
697 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
699 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
700 msg->dn = ldb_dn_copy(msg, kd->dn);
702 if (strlen(name) > 0) {
703 /* For a default value, we add/overwrite the attributes to/of the hive.
704 For a normal value, we create a new child. */
705 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
706 reg_ldb_escape(mem_ctx, name)))
708 talloc_free(mem_ctx);
709 return WERR_FOOBAR;
713 ret = ldb_add(kd->ldb, msg);
714 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
715 unsigned int i;
716 for (i = 0; i < msg->num_elements; i++) {
717 if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE)
718 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
720 ret = ldb_modify(kd->ldb, msg);
723 if (ret != LDB_SUCCESS) {
724 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
725 talloc_free(mem_ctx);
726 return WERR_FOOBAR;
729 /* reset cache */
730 talloc_free(kd->values);
731 kd->values = NULL;
733 talloc_free(mem_ctx);
734 return WERR_OK;
737 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
738 const struct hive_key *key,
739 const char **classname,
740 uint32_t *num_subkeys,
741 uint32_t *num_values,
742 NTTIME *last_change_time,
743 uint32_t *max_subkeynamelen,
744 uint32_t *max_valnamelen,
745 uint32_t *max_valbufsize)
747 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
749 /* Initialization */
750 if (classname != NULL)
751 *classname = NULL;
752 if (num_subkeys != NULL)
753 *num_subkeys = 0;
754 if (num_values != NULL)
755 *num_values = 0;
756 if (last_change_time != NULL)
757 *last_change_time = 0;
758 if (max_subkeynamelen != NULL)
759 *max_subkeynamelen = 0;
760 if (max_valnamelen != NULL)
761 *max_valnamelen = 0;
762 if (max_valbufsize != NULL)
763 *max_valbufsize = 0;
765 if (kd->subkeys == NULL) {
766 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
769 if (kd->values == NULL) {
770 W_ERROR_NOT_OK_RETURN(cache_values(kd));
773 if (num_subkeys != NULL) {
774 *num_subkeys = kd->subkey_count;
776 if (num_values != NULL) {
777 *num_values = kd->value_count;
781 if (max_subkeynamelen != NULL) {
782 unsigned int i;
783 struct ldb_message_element *el;
785 *max_subkeynamelen = 0;
787 for (i = 0; i < kd->subkey_count; i++) {
788 el = ldb_msg_find_element(kd->subkeys[i], "key");
789 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
793 if (max_valnamelen != NULL || max_valbufsize != NULL) {
794 unsigned int i;
795 struct ldb_message_element *el;
796 W_ERROR_NOT_OK_RETURN(cache_values(kd));
798 if (max_valbufsize != NULL)
799 *max_valbufsize = 0;
801 if (max_valnamelen != NULL)
802 *max_valnamelen = 0;
804 for (i = 0; i < kd->value_count; i++) {
805 if (max_valnamelen != NULL) {
806 el = ldb_msg_find_element(kd->values[i], "value");
807 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
810 if (max_valbufsize != NULL) {
811 uint32_t data_type;
812 DATA_BLOB data;
813 reg_ldb_unpack_value(mem_ctx,
814 kd->values[i], NULL,
815 &data_type, &data);
816 *max_valbufsize = MAX(*max_valbufsize, data.length);
817 talloc_free(data.data);
822 return WERR_OK;
825 static struct hive_operations reg_backend_ldb = {
826 .name = "ldb",
827 .add_key = ldb_add_key,
828 .del_key = ldb_del_key,
829 .get_key_by_name = ldb_open_key,
830 .enum_value = ldb_get_value_by_id,
831 .enum_key = ldb_get_subkey_by_id,
832 .set_value = ldb_set_value,
833 .get_value_by_name = ldb_get_value,
834 .delete_value = ldb_del_value,
835 .get_key_info = ldb_get_key_info,