Merge ldb_search() and ldb_search_exp_fmt() into a simgle function.
[Samba.git] / source4 / lib / registry / ldb.c
blob25b8c583ed2f4c27650e36fc65f56092e2439472
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,
40 struct smb_iconv_convenience *iconv_convenience,
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 if (type != NULL)
55 *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 data->length = convert_string_talloc(mem_ctx, iconv_convenience, CH_UTF8, CH_UTF16,
63 val->data, val->length,
64 (void **)&data->data);
65 break;
67 case REG_DWORD: {
68 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
69 *data = data_blob_talloc(mem_ctx, &tmp, 4);
71 break;
73 default:
74 *data = data_blob_talloc(mem_ctx, val->data, val->length);
75 break;
79 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
80 TALLOC_CTX *mem_ctx,
81 const char *name,
82 uint32_t type, DATA_BLOB data)
84 struct ldb_val val;
85 struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
86 char *type_s;
88 ldb_msg_add_string(msg, "value", talloc_strdup(mem_ctx, name));
90 switch (type) {
91 case REG_SZ:
92 case REG_EXPAND_SZ:
93 val.length = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF16, CH_UNIX,
94 (void *)data.data,
95 data.length,
96 (void **)&val.data);
97 ldb_msg_add_value(msg, "data", &val, NULL);
98 break;
100 case REG_DWORD:
101 ldb_msg_add_string(msg, "data",
102 talloc_asprintf(mem_ctx, "0x%x",
103 IVAL(data.data, 0)));
104 break;
105 default:
106 ldb_msg_add_value(msg, "data", &data, NULL);
110 type_s = talloc_asprintf(mem_ctx, "%u", type);
111 ldb_msg_add_string(msg, "type", type_s);
113 return msg;
116 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
118 struct ldb_val val;
120 val.data = discard_const_p(uint8_t, value);
121 val.length = strlen(value);
123 return ldb_dn_escape_value(mem_ctx, val);
126 static int reg_close_ldb_key(struct ldb_key_data *key)
128 if (key->subkeys != NULL) {
129 talloc_free(key->subkeys);
130 key->subkeys = NULL;
133 if (key->values != NULL) {
134 talloc_free(key->values);
135 key->values = NULL;
137 return 0;
140 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
141 const struct hive_key *from,
142 const char *path, const char *add)
144 TALLOC_CTX *local_ctx;
145 struct ldb_dn *ret;
146 char *mypath = talloc_strdup(mem_ctx, path);
147 char *begin;
148 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
149 struct ldb_context *ldb = kd->ldb;
151 local_ctx = talloc_new(mem_ctx);
153 if (add) {
154 ret = ldb_dn_new(mem_ctx, ldb, add);
155 } else {
156 ret = ldb_dn_new(mem_ctx, ldb, NULL);
158 if (!ldb_dn_validate(ret)) {
159 talloc_free(ret);
160 talloc_free(local_ctx);
161 return NULL;
164 while (mypath) {
165 char *keyname;
167 begin = strrchr(mypath, '\\');
169 if (begin) keyname = begin + 1;
170 else keyname = mypath;
172 if(strlen(keyname)) {
173 if (!ldb_dn_add_base_fmt(ret, "key=%s",
174 reg_ldb_escape(local_ctx,
175 keyname)))
177 talloc_free(local_ctx);
178 return NULL;
182 if(begin) {
183 *begin = '\0';
184 } else {
185 break;
189 ldb_dn_add_base(ret, kd->dn);
191 talloc_free(local_ctx);
193 return ret;
196 static WERROR cache_subkeys(struct ldb_key_data *kd)
198 struct ldb_context *c = kd->ldb;
199 struct ldb_result *res;
200 int ret;
202 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "(key=*)");
204 if (ret != LDB_SUCCESS) {
205 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
206 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
207 return WERR_FOOBAR;
210 kd->subkey_count = res->count;
211 kd->subkeys = talloc_steal(kd, res->msgs);
212 talloc_free(res);
214 return WERR_OK;
217 static WERROR cache_values(struct ldb_key_data *kd)
219 struct ldb_context *c = kd->ldb;
220 struct ldb_result *res;
221 int ret;
223 ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
224 NULL, "(value=*)");
226 if (ret != LDB_SUCCESS) {
227 DEBUG(0, ("Error getting values for '%s': %s\n",
228 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
229 return WERR_FOOBAR;
231 kd->value_count = res->count;
232 kd->values = talloc_steal(kd, res->msgs);
233 talloc_free(res);
234 return WERR_OK;
238 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
239 const struct hive_key *k, uint32_t idx,
240 const char **name,
241 const char **classname,
242 NTTIME *last_mod_time)
244 struct ldb_message_element *el;
245 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
247 /* Do a search if necessary */
248 if (kd->subkeys == NULL) {
249 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
252 if (idx >= kd->subkey_count)
253 return WERR_NO_MORE_ITEMS;
255 el = ldb_msg_find_element(kd->subkeys[idx], "key");
256 SMB_ASSERT(el != NULL);
257 SMB_ASSERT(el->num_values != 0);
259 if (name != NULL)
260 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
262 if (classname != NULL)
263 *classname = NULL; /* TODO: Store properly */
265 if (last_mod_time != NULL)
266 *last_mod_time = 0; /* TODO: we need to add this to the
267 ldb backend properly */
269 return WERR_OK;
272 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
273 int idx, const char **name,
274 uint32_t *data_type, DATA_BLOB *data)
276 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
278 /* Do the search if necessary */
279 if (kd->values == NULL) {
280 W_ERROR_NOT_OK_RETURN(cache_values(kd));
283 if (idx >= kd->value_count)
284 return WERR_NO_MORE_ITEMS;
286 reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm), kd->values[idx],
287 name, data_type, data);
289 return WERR_OK;
292 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
293 const char *name, uint32_t *data_type,
294 DATA_BLOB *data)
296 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
297 struct ldb_context *c = kd->ldb;
298 struct ldb_result *res;
299 int ret;
300 char *query = talloc_asprintf(mem_ctx, "(value=%s)", name);
302 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "%s", query);
304 talloc_free(query);
306 if (ret != LDB_SUCCESS) {
307 DEBUG(0, ("Error getting values for '%s': %s\n",
308 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
309 talloc_free(res);
310 return WERR_FOOBAR;
313 if (res->count == 0) {
314 talloc_free(res);
315 return WERR_BADFILE;
318 reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm), res->msgs[0], NULL, data_type, data);
320 talloc_free(res);
321 return WERR_OK;
324 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
325 const char *name, struct hive_key **key)
327 struct ldb_result *res;
328 struct ldb_dn *ldap_path;
329 int ret;
330 struct ldb_key_data *newkd;
331 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
332 struct ldb_context *c = kd->ldb;
334 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
336 ret = ldb_search(c, mem_ctx, &res, ldap_path, LDB_SCOPE_BASE, NULL, "(key=*)");
338 if (ret != LDB_SUCCESS) {
339 DEBUG(3, ("Error opening key '%s': %s\n",
340 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
341 return WERR_FOOBAR;
342 } else if (res->count == 0) {
343 DEBUG(3, ("Key '%s' not found\n",
344 ldb_dn_get_linearized(ldap_path)));
345 talloc_free(res);
346 return WERR_BADFILE;
349 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
350 newkd->key.ops = &reg_backend_ldb;
351 newkd->ldb = talloc_reference(newkd, kd->ldb);
352 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
354 *key = (struct hive_key *)newkd;
356 talloc_free(res);
358 return WERR_OK;
361 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
362 struct auth_session_info *session_info,
363 struct cli_credentials *credentials,
364 struct event_context *ev_ctx,
365 struct loadparm_context *lp_ctx,
366 struct hive_key **k)
368 struct ldb_key_data *kd;
369 struct ldb_context *wrap;
370 struct ldb_message *attrs_msg;
372 if (location == NULL)
373 return WERR_INVALID_PARAM;
375 wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
376 location, session_info, credentials, 0, NULL);
378 if (wrap == NULL) {
379 DEBUG(1, (__FILE__": unable to connect\n"));
380 return WERR_FOOBAR;
383 attrs_msg = ldb_msg_new(wrap);
384 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
385 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
386 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
387 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
388 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
390 ldb_add(wrap, attrs_msg);
392 ldb_set_debug_stderr(wrap);
394 kd = talloc_zero(parent_ctx, struct ldb_key_data);
395 kd->key.ops = &reg_backend_ldb;
396 kd->ldb = talloc_reference(kd, wrap);
397 talloc_set_destructor (kd, reg_close_ldb_key);
398 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
400 *k = (struct hive_key *)kd;
402 return WERR_OK;
405 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
406 const char *name, const char *classname,
407 struct security_descriptor *sd,
408 struct hive_key **newkey)
410 struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
411 struct ldb_message *msg;
412 struct ldb_key_data *newkd;
413 int ret;
415 msg = ldb_msg_new(mem_ctx);
417 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
419 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
420 if (classname != NULL)
421 ldb_msg_add_string(msg, "classname",
422 talloc_strdup(mem_ctx, classname));
424 ret = ldb_add(parentkd->ldb, msg);
425 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
426 return WERR_ALREADY_EXISTS;
429 if (ret != LDB_SUCCESS) {
430 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
431 return WERR_FOOBAR;
434 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
436 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
437 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
438 newkd->key.ops = &reg_backend_ldb;
439 newkd->dn = talloc_steal(newkd, msg->dn);
441 *newkey = (struct hive_key *)newkd;
443 /* reset cache */
444 talloc_free(parentkd->subkeys);
445 parentkd->subkeys = NULL;
447 return WERR_OK;
450 static WERROR ldb_del_value (struct hive_key *key, const char *child)
452 int ret;
453 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
454 struct ldb_dn *childdn;
456 childdn = ldb_dn_copy(kd->ldb, kd->dn);
457 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
458 reg_ldb_escape(childdn, child)))
460 talloc_free(childdn);
461 return WERR_FOOBAR;
464 ret = ldb_delete(kd->ldb, childdn);
466 talloc_free(childdn);
468 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
469 return WERR_BADFILE;
470 } else if (ret != LDB_SUCCESS) {
471 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
472 return WERR_FOOBAR;
475 /* reset cache */
476 talloc_free(kd->values);
477 kd->values = NULL;
479 return WERR_OK;
482 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
484 int i, ret;
485 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
486 struct ldb_dn *ldap_path;
487 TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
488 struct ldb_context *c = parentkd->ldb;
489 struct ldb_result *res_keys;
490 struct ldb_result *res_vals;
491 WERROR werr;
492 struct hive_key *hk;
494 /* Verify key exists by opening it */
495 werr = ldb_open_key(mem_ctx, key, name, &hk);
496 if (!W_ERROR_IS_OK(werr)) {
497 talloc_free(mem_ctx);
498 return werr;
501 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
502 if (!ldap_path) {
503 talloc_free(mem_ctx);
504 return WERR_FOOBAR;
507 /* Search for subkeys */
508 ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
509 NULL, "(key=*)");
511 if (ret != LDB_SUCCESS) {
512 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
513 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
514 talloc_free(mem_ctx);
515 return WERR_FOOBAR;
518 /* Search for values */
519 ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
520 NULL, "(value=*)");
522 if (ret != LDB_SUCCESS) {
523 DEBUG(0, ("Error getting values for '%s': %s\n",
524 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
525 talloc_free(mem_ctx);
526 return WERR_FOOBAR;
529 /* Start an explicit transaction */
530 ret = ldb_transaction_start(c);
532 if (ret != LDB_SUCCESS) {
533 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
534 talloc_free(mem_ctx);
535 return WERR_FOOBAR;
538 if (res_keys->count || res_vals->count)
540 /* Delete any subkeys */
541 for (i = 0; i < res_keys->count; i++)
543 werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
544 res_keys->msgs[i],
545 "key", NULL));
546 if (!W_ERROR_IS_OK(werr)) {
547 ret = ldb_transaction_cancel(c);
548 talloc_free(mem_ctx);
549 return werr;
553 /* Delete any values */
554 for (i = 0; i < res_vals->count; i++)
556 werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
557 res_vals->msgs[i],
558 "value", NULL));
559 if (!W_ERROR_IS_OK(werr)) {
560 ret = ldb_transaction_cancel(c);
561 talloc_free(mem_ctx);
562 return werr;
567 /* Delete the key itself */
568 ret = ldb_delete(c, ldap_path);
570 if (ret != LDB_SUCCESS)
572 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
573 ret = ldb_transaction_cancel(c);
574 talloc_free(mem_ctx);
575 return WERR_FOOBAR;
578 /* Commit the transaction */
579 ret = ldb_transaction_commit(c);
581 if (ret != LDB_SUCCESS)
583 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
584 ret = ldb_transaction_cancel(c);
585 talloc_free(mem_ctx);
586 return WERR_FOOBAR;
589 talloc_free(mem_ctx);
591 /* reset cache */
592 talloc_free(parentkd->subkeys);
593 parentkd->subkeys = NULL;
595 return WERR_OK;
598 static WERROR ldb_set_value(struct hive_key *parent,
599 const char *name, uint32_t type,
600 const DATA_BLOB data)
602 struct ldb_message *msg;
603 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
604 int ret;
605 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
607 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
609 msg->dn = ldb_dn_copy(msg, kd->dn);
610 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
611 reg_ldb_escape(mem_ctx, name)))
613 talloc_free(mem_ctx);
614 return WERR_FOOBAR;
617 ret = ldb_add(kd->ldb, msg);
618 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
619 int i;
620 for (i = 0; i < msg->num_elements; i++) {
621 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
623 ret = ldb_modify(kd->ldb, msg);
626 if (ret != LDB_SUCCESS) {
627 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(kd->ldb)));
628 talloc_free(mem_ctx);
629 return WERR_FOOBAR;
632 /* reset cache */
633 talloc_free(kd->values);
634 kd->values = NULL;
636 talloc_free(mem_ctx);
637 return WERR_OK;
640 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
641 const struct hive_key *key,
642 const char **classname,
643 uint32_t *num_subkeys,
644 uint32_t *num_values,
645 NTTIME *last_change_time,
646 uint32_t *max_subkeynamelen,
647 uint32_t *max_valnamelen,
648 uint32_t *max_valbufsize)
650 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
652 if (kd->subkeys == NULL) {
653 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
656 if (kd->values == NULL) {
657 W_ERROR_NOT_OK_RETURN(cache_values(kd));
660 /* FIXME */
661 if (classname != NULL)
662 *classname = NULL;
664 if (num_subkeys != NULL) {
665 *num_subkeys = kd->subkey_count;
668 if (num_values != NULL) {
669 *num_values = kd->value_count;
672 if (last_change_time != NULL)
673 *last_change_time = 0;
675 if (max_subkeynamelen != NULL) {
676 int i;
677 struct ldb_message_element *el;
679 *max_subkeynamelen = 0;
681 for (i = 0; i < kd->subkey_count; i++) {
682 el = ldb_msg_find_element(kd->subkeys[i], "key");
683 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
687 if (max_valnamelen != NULL || max_valbufsize != NULL) {
688 int i;
689 struct ldb_message_element *el;
690 W_ERROR_NOT_OK_RETURN(cache_values(kd));
692 if (max_valbufsize != NULL)
693 *max_valbufsize = 0;
695 if (max_valnamelen != NULL)
696 *max_valnamelen = 0;
698 for (i = 0; i < kd->value_count; i++) {
699 if (max_valnamelen != NULL) {
700 el = ldb_msg_find_element(kd->values[i], "value");
701 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
704 if (max_valbufsize != NULL) {
705 DATA_BLOB data;
706 reg_ldb_unpack_value(mem_ctx,
707 lp_iconv_convenience(global_loadparm),
708 kd->values[i], NULL,
709 NULL, &data);
710 *max_valbufsize = MAX(*max_valbufsize, data.length);
711 talloc_free(data.data);
716 return WERR_OK;
719 static struct hive_operations reg_backend_ldb = {
720 .name = "ldb",
721 .add_key = ldb_add_key,
722 .del_key = ldb_del_key,
723 .get_key_by_name = ldb_open_key,
724 .enum_value = ldb_get_value_by_id,
725 .enum_key = ldb_get_subkey_by_id,
726 .set_value = ldb_set_value,
727 .get_value_by_name = ldb_get_value,
728 .delete_value = ldb_del_value,
729 .get_key_info = ldb_get_key_info,