registry: Implement recursive deletes for ldb-backed registry.
[Samba/aatanasov.git] / source4 / lib / registry / ldb.c
blob31b78d82463aaf9e6eb72112b5e943b6b15778dd
1 /*
2 Unix SMB/CIFS implementation.
3 Registry interface
4 Copyright (C) Jelmer Vernooij 2004-2007.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "registry.h"
22 #include "lib/ldb/include/ldb.h"
23 #include "lib/ldb/include/ldb_errors.h"
24 #include "ldb_wrap.h"
25 #include "librpc/gen_ndr/winreg.h"
26 #include "param/param.h"
28 static struct hive_operations reg_backend_ldb;
30 struct ldb_key_data
32 struct hive_key key;
33 struct ldb_context *ldb;
34 struct ldb_dn *dn;
35 struct ldb_message **subkeys, **values;
36 int subkey_count, value_count;
39 static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx, struct ldb_message *msg,
40 const char **name, uint32_t *type,
41 DATA_BLOB *data)
43 const struct ldb_val *val;
44 uint32_t value_type;
46 if (name != NULL)
47 *name = talloc_strdup(mem_ctx,
48 ldb_msg_find_attr_as_string(msg, "value",
49 NULL));
51 value_type = ldb_msg_find_attr_as_uint(msg, "type", 0);
52 if (type != NULL)
53 *type = value_type;
54 val = ldb_msg_find_ldb_val(msg, "data");
56 switch (value_type)
58 case REG_SZ:
59 case REG_EXPAND_SZ:
60 data->length = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF8, CH_UTF16,
61 val->data, val->length,
62 (void **)&data->data);
63 break;
65 case REG_DWORD: {
66 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
67 *data = data_blob_talloc(mem_ctx, &tmp, 4);
69 break;
71 default:
72 *data = data_blob_talloc(mem_ctx, val->data, val->length);
73 break;
77 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
78 TALLOC_CTX *mem_ctx,
79 const char *name,
80 uint32_t type, DATA_BLOB data)
82 struct ldb_val val;
83 struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
84 char *type_s;
86 ldb_msg_add_string(msg, "value", talloc_strdup(mem_ctx, name));
88 switch (type) {
89 case REG_SZ:
90 case REG_EXPAND_SZ:
91 val.length = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF16, CH_UNIX,
92 (void *)data.data,
93 data.length,
94 (void **)&val.data);
95 ldb_msg_add_value(msg, "data", &val, NULL);
96 break;
98 case REG_DWORD:
99 ldb_msg_add_string(msg, "data",
100 talloc_asprintf(mem_ctx, "0x%x",
101 IVAL(data.data, 0)));
102 break;
103 default:
104 ldb_msg_add_value(msg, "data", &data, NULL);
108 type_s = talloc_asprintf(mem_ctx, "%u", type);
109 ldb_msg_add_string(msg, "type", type_s);
111 return msg;
114 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
116 struct ldb_val val;
118 val.data = discard_const_p(uint8_t, value);
119 val.length = strlen(value);
121 return ldb_dn_escape_value(mem_ctx, val);
124 static int reg_close_ldb_key(struct ldb_key_data *key)
126 if (key->subkeys != NULL) {
127 talloc_free(key->subkeys);
128 key->subkeys = NULL;
131 if (key->values != NULL) {
132 talloc_free(key->values);
133 key->values = NULL;
135 return 0;
138 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
139 const struct hive_key *from,
140 const char *path, const char *add)
142 TALLOC_CTX *local_ctx;
143 struct ldb_dn *ret;
144 char *mypath = talloc_strdup(mem_ctx, path);
145 char *begin;
146 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
147 struct ldb_context *ldb = kd->ldb;
149 local_ctx = talloc_new(mem_ctx);
151 if (add) {
152 ret = ldb_dn_new(mem_ctx, ldb, add);
153 } else {
154 ret = ldb_dn_new(mem_ctx, ldb, NULL);
156 if (!ldb_dn_validate(ret)) {
157 talloc_free(ret);
158 talloc_free(local_ctx);
159 return NULL;
162 while (mypath) {
163 char *keyname;
165 begin = strrchr(mypath, '\\');
167 if (begin) keyname = begin + 1;
168 else keyname = mypath;
170 if(strlen(keyname)) {
171 if (!ldb_dn_add_base_fmt(ret, "key=%s",
172 reg_ldb_escape(local_ctx,
173 keyname)))
175 talloc_free(local_ctx);
176 return NULL;
180 if(begin) {
181 *begin = '\0';
182 } else {
183 break;
187 ldb_dn_add_base(ret, kd->dn);
189 talloc_free(local_ctx);
191 return ret;
194 static WERROR cache_subkeys(struct ldb_key_data *kd)
196 struct ldb_context *c = kd->ldb;
197 struct ldb_result *res;
198 int ret;
200 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL, &res);
202 if (ret != LDB_SUCCESS) {
203 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
204 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
205 return WERR_FOOBAR;
208 kd->subkey_count = res->count;
209 kd->subkeys = talloc_steal(kd, res->msgs);
210 talloc_free(res);
212 return WERR_OK;
215 static WERROR cache_values(struct ldb_key_data *kd)
217 struct ldb_context *c = kd->ldb;
218 struct ldb_result *res;
219 int ret;
221 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL,
222 "(value=*)", NULL, &res);
224 if (ret != LDB_SUCCESS) {
225 DEBUG(0, ("Error getting values for '%s': %s\n",
226 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
227 return WERR_FOOBAR;
229 kd->value_count = res->count;
230 kd->values = talloc_steal(kd, res->msgs);
231 talloc_free(res);
232 return WERR_OK;
236 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
237 const struct hive_key *k, uint32_t idx,
238 const char **name,
239 const char **classname,
240 NTTIME *last_mod_time)
242 struct ldb_message_element *el;
243 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
245 /* Do a search if necessary */
246 if (kd->subkeys == NULL) {
247 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
250 if (idx >= kd->subkey_count)
251 return WERR_NO_MORE_ITEMS;
253 el = ldb_msg_find_element(kd->subkeys[idx], "key");
254 SMB_ASSERT(el != NULL);
255 SMB_ASSERT(el->num_values != 0);
257 if (name != NULL)
258 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
260 if (classname != NULL)
261 *classname = NULL; /* TODO: Store properly */
263 if (last_mod_time != NULL)
264 *last_mod_time = 0; /* TODO: we need to add this to the
265 ldb backend properly */
267 return WERR_OK;
270 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
271 int idx, const char **name,
272 uint32_t *data_type, DATA_BLOB *data)
274 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
276 /* Do the search if necessary */
277 if (kd->values == NULL) {
278 W_ERROR_NOT_OK_RETURN(cache_values(kd));
281 if (idx >= kd->value_count)
282 return WERR_NO_MORE_ITEMS;
284 reg_ldb_unpack_value(mem_ctx, kd->values[idx],
285 name, data_type, data);
287 return WERR_OK;
290 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
291 const char *name, uint32_t *data_type,
292 DATA_BLOB *data)
294 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
295 struct ldb_context *c = kd->ldb;
296 struct ldb_result *res;
297 int ret;
298 char *query = talloc_asprintf(mem_ctx, "(value=%s)", name);
300 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, query, NULL, &res);
302 talloc_free(query);
304 if (ret != LDB_SUCCESS) {
305 DEBUG(0, ("Error getting values for '%s': %s\n",
306 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
307 return WERR_FOOBAR;
310 if (res->count == 0)
311 return WERR_BADFILE;
313 reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
315 return WERR_OK;
318 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
319 const char *name, struct hive_key **key)
321 struct ldb_result *res;
322 struct ldb_dn *ldap_path;
323 int ret;
324 struct ldb_key_data *newkd;
325 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
326 struct ldb_context *c = kd->ldb;
328 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
330 ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL, &res);
332 if (ret != LDB_SUCCESS) {
333 DEBUG(3, ("Error opening key '%s': %s\n",
334 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
335 return WERR_FOOBAR;
336 } else if (res->count == 0) {
337 DEBUG(3, ("Key '%s' not found\n",
338 ldb_dn_get_linearized(ldap_path)));
339 talloc_free(res);
340 return WERR_BADFILE;
343 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
344 newkd->key.ops = &reg_backend_ldb;
345 newkd->ldb = talloc_reference(newkd, kd->ldb);
346 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
348 *key = (struct hive_key *)newkd;
350 talloc_free(res);
352 return WERR_OK;
355 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
356 struct auth_session_info *session_info,
357 struct cli_credentials *credentials,
358 struct loadparm_context *lp_ctx,
359 struct hive_key **k)
361 struct ldb_key_data *kd;
362 struct ldb_context *wrap;
363 struct ldb_message *attrs_msg;
365 if (location == NULL)
366 return WERR_INVALID_PARAM;
368 wrap = ldb_wrap_connect(parent_ctx, lp_ctx,
369 location, session_info, credentials, 0, NULL);
371 if (wrap == NULL) {
372 DEBUG(1, (__FILE__": unable to connect\n"));
373 return WERR_FOOBAR;
376 attrs_msg = ldb_msg_new(wrap);
377 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
378 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
379 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
380 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
381 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
383 ldb_add(wrap, attrs_msg);
385 ldb_set_debug_stderr(wrap);
387 kd = talloc_zero(parent_ctx, struct ldb_key_data);
388 kd->key.ops = &reg_backend_ldb;
389 kd->ldb = talloc_reference(kd, wrap);
390 talloc_set_destructor (kd, reg_close_ldb_key);
391 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
393 *k = (struct hive_key *)kd;
395 return WERR_OK;
398 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
399 const char *name, const char *classname,
400 struct security_descriptor *sd,
401 struct hive_key **newkey)
403 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
404 struct ldb_message *msg;
405 struct ldb_key_data *newkd;
406 int ret;
408 msg = ldb_msg_new(mem_ctx);
410 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
412 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
413 if (classname != NULL)
414 ldb_msg_add_string(msg, "classname",
415 talloc_strdup(mem_ctx, classname));
417 ret = ldb_add(parentkd->ldb, msg);
418 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
419 return WERR_ALREADY_EXISTS;
422 if (ret != LDB_SUCCESS) {
423 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
424 return WERR_FOOBAR;
427 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
429 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
430 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
431 newkd->key.ops = &reg_backend_ldb;
432 newkd->dn = talloc_steal(newkd, msg->dn);
434 *newkey = (struct hive_key *)newkd;
436 /* reset cache */
437 talloc_free(parentkd->subkeys);
438 parentkd->subkeys = NULL;
440 return WERR_OK;
443 static WERROR ldb_del_value (struct hive_key *key, const char *child)
445 int ret;
446 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
447 struct ldb_dn *childdn;
449 childdn = ldb_dn_copy(kd->ldb, kd->dn);
450 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
451 reg_ldb_escape(childdn, child)))
453 talloc_free(childdn);
454 return WERR_FOOBAR;
457 ret = ldb_delete(kd->ldb, childdn);
459 talloc_free(childdn);
461 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
462 return WERR_BADFILE;
463 } else if (ret != LDB_SUCCESS) {
464 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
465 return WERR_FOOBAR;
468 /* reset cache */
469 talloc_free(kd->values);
470 kd->values = NULL;
472 return WERR_OK;
475 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
477 int i, ret;
478 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
479 struct ldb_dn *ldap_path;
480 TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
481 struct ldb_context *c = parentkd->ldb;
482 struct ldb_result *res_keys;
483 struct ldb_result *res_vals;
484 WERROR werr;
485 struct hive_key *hk;
487 /* Verify key exists by opening it */
488 werr = ldb_open_key(mem_ctx, key, name, &hk);
489 if (!W_ERROR_IS_OK(werr)) {
490 talloc_free(mem_ctx);
491 return werr;
494 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
495 if (!ldap_path) {
496 talloc_free(mem_ctx);
497 return WERR_FOOBAR;
500 /* Search for subkeys */
501 ret = ldb_search(c, ldap_path, LDB_SCOPE_ONELEVEL,
502 "(key=*)", NULL, &res_keys);
504 if (ret != LDB_SUCCESS) {
505 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
506 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
507 talloc_free(mem_ctx);
508 return WERR_FOOBAR;
511 /* Search for values */
512 ret = ldb_search(c, ldap_path, LDB_SCOPE_ONELEVEL,
513 "(value=*)", NULL, &res_vals);
515 if (ret != LDB_SUCCESS) {
516 DEBUG(0, ("Error getting values for '%s': %s\n",
517 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
518 talloc_free(mem_ctx);
519 return WERR_FOOBAR;
522 /* Start an explicit transaction */
523 ret = ldb_transaction_start(c);
525 if (ret != LDB_SUCCESS) {
526 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
527 talloc_free(mem_ctx);
528 return WERR_FOOBAR;
531 if (res_keys->count || res_vals->count)
533 /* Delete any subkeys */
534 for (i = 0; i < res_keys->count; i++)
536 werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
537 res_keys->msgs[i],
538 "key", NULL));
539 if (!W_ERROR_IS_OK(werr)) {
540 ret = ldb_transaction_cancel(c);
541 talloc_free(mem_ctx);
542 return werr;
546 /* Delete any values */
547 for (i = 0; i < res_vals->count; i++)
549 werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
550 res_vals->msgs[i],
551 "value", NULL));
552 if (!W_ERROR_IS_OK(werr)) {
553 ret = ldb_transaction_cancel(c);
554 talloc_free(mem_ctx);
555 return werr;
560 /* Delete the key itself */
561 ret = ldb_delete(c, ldap_path);
563 if (ret != LDB_SUCCESS)
565 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
566 ret = ldb_transaction_cancel(c);
567 talloc_free(mem_ctx);
568 return WERR_FOOBAR;
571 /* Commit the transaction */
572 ret = ldb_transaction_commit(c);
574 if (ret != LDB_SUCCESS)
576 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
577 ret = ldb_transaction_cancel(c);
578 talloc_free(mem_ctx);
579 return WERR_FOOBAR;
582 talloc_free(mem_ctx);
584 /* reset cache */
585 talloc_free(parentkd->subkeys);
586 parentkd->subkeys = NULL;
588 return WERR_OK;
591 static WERROR ldb_set_value(struct hive_key *parent,
592 const char *name, uint32_t type,
593 const DATA_BLOB data)
595 struct ldb_message *msg;
596 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
597 int ret;
598 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
600 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
602 msg->dn = ldb_dn_copy(msg, kd->dn);
603 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
604 reg_ldb_escape(mem_ctx, name)))
606 talloc_free(mem_ctx);
607 return WERR_FOOBAR;
610 ret = ldb_add(kd->ldb, msg);
611 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
612 int i;
613 for (i = 0; i < msg->num_elements; i++) {
614 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
616 ret = ldb_modify(kd->ldb, msg);
619 if (ret != LDB_SUCCESS) {
620 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(kd->ldb)));
621 talloc_free(mem_ctx);
622 return WERR_FOOBAR;
625 /* reset cache */
626 talloc_free(kd->values);
627 kd->values = NULL;
629 talloc_free(mem_ctx);
630 return WERR_OK;
633 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
634 const struct hive_key *key,
635 const char **classname,
636 uint32_t *num_subkeys,
637 uint32_t *num_values,
638 NTTIME *last_change_time,
639 uint32_t *max_subkeynamelen,
640 uint32_t *max_valnamelen,
641 uint32_t *max_valbufsize)
643 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
645 if (kd->subkeys == NULL) {
646 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
649 if (kd->values == NULL) {
650 W_ERROR_NOT_OK_RETURN(cache_values(kd));
653 /* FIXME */
654 if (classname != NULL)
655 *classname = NULL;
657 if (num_subkeys != NULL) {
658 *num_subkeys = kd->subkey_count;
661 if (num_values != NULL) {
662 *num_values = kd->value_count;
665 if (last_change_time != NULL)
666 *last_change_time = 0;
668 if (max_subkeynamelen != NULL) {
669 int i;
670 struct ldb_message_element *el;
672 *max_subkeynamelen = 0;
674 for (i = 0; i < kd->subkey_count; i++) {
675 el = ldb_msg_find_element(kd->subkeys[i], "key");
676 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
680 if (max_valnamelen != NULL || max_valbufsize != NULL) {
681 int i;
682 struct ldb_message_element *el;
683 W_ERROR_NOT_OK_RETURN(cache_values(kd));
685 if (max_valbufsize != NULL)
686 *max_valbufsize = 0;
688 if (max_valnamelen != NULL)
689 *max_valnamelen = 0;
691 for (i = 0; i < kd->value_count; i++) {
692 if (max_valnamelen != NULL) {
693 el = ldb_msg_find_element(kd->values[i], "value");
694 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
697 if (max_valbufsize != NULL) {
698 DATA_BLOB data;
699 reg_ldb_unpack_value(mem_ctx, kd->values[i], NULL,
700 NULL, &data);
701 *max_valbufsize = MAX(*max_valbufsize, data.length);
702 talloc_free(data.data);
707 return WERR_OK;
710 static struct hive_operations reg_backend_ldb = {
711 .name = "ldb",
712 .add_key = ldb_add_key,
713 .del_key = ldb_del_key,
714 .get_key_by_name = ldb_open_key,
715 .enum_value = ldb_get_value_by_id,
716 .enum_key = ldb_get_subkey_by_id,
717 .set_value = ldb_set_value,
718 .get_value_by_name = ldb_get_value,
719 .delete_value = ldb_del_value,
720 .get_key_info = ldb_get_key_info,