2 Unix SMB/CIFS implementation.
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/>.
22 #include "lib/ldb/include/ldb.h"
23 #include "lib/ldb/include/ldb_errors.h"
25 #include "librpc/gen_ndr/winreg.h"
26 #include "param/param.h"
28 static struct hive_operations reg_backend_ldb;
33 struct ldb_context *ldb;
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,
43 const struct ldb_val *val;
47 *name = talloc_strdup(mem_ctx,
48 ldb_msg_find_attr_as_string(msg, "value",
51 value_type = ldb_msg_find_attr_as_uint(msg, "type", 0);
54 val = ldb_msg_find_ldb_val(msg, "data");
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);
66 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
67 *data = data_blob_talloc(mem_ctx, &tmp, 4);
72 *data = data_blob_talloc(mem_ctx, val->data, val->length);
77 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
80 uint32_t type, DATA_BLOB data)
83 struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
86 ldb_msg_add_string(msg, "value", talloc_strdup(mem_ctx, name));
91 val.length = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF16, CH_UNIX,
95 ldb_msg_add_value(msg, "data", &val, NULL);
99 ldb_msg_add_string(msg, "data",
100 talloc_asprintf(mem_ctx, "0x%x",
101 IVAL(data.data, 0)));
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);
114 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
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);
131 if (key->values != NULL) {
132 talloc_free(key->values);
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;
144 char *mypath = talloc_strdup(mem_ctx, path);
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);
152 ret = ldb_dn_new(mem_ctx, ldb, add);
154 ret = ldb_dn_new(mem_ctx, ldb, NULL);
156 if (!ldb_dn_validate(ret)) {
158 talloc_free(local_ctx);
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,
175 talloc_free(local_ctx);
187 ldb_dn_add_base(ret, kd->dn);
189 talloc_free(local_ctx);
194 static WERROR cache_subkeys(struct ldb_key_data *kd)
196 struct ldb_context *c = kd->ldb;
197 struct ldb_result *res;
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)));
208 kd->subkey_count = res->count;
209 kd->subkeys = talloc_steal(kd, res->msgs);
215 static WERROR cache_values(struct ldb_key_data *kd)
217 struct ldb_context *c = kd->ldb;
218 struct ldb_result *res;
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)));
229 kd->value_count = res->count;
230 kd->values = talloc_steal(kd, res->msgs);
236 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
237 const struct hive_key *k, uint32_t idx,
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);
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 */
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);
290 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
291 const char *name, uint32_t *data_type,
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;
298 char *query = talloc_asprintf(mem_ctx, "(value=%s)", name);
300 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, query, NULL, &res);
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)));
313 reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
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;
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)));
336 } else if (res->count == 0) {
337 DEBUG(3, ("Key '%s' not found\n",
338 ldb_dn_get_linearized(ldap_path)));
343 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
344 newkd->key.ops = ®_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;
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,
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);
372 DEBUG(1, (__FILE__": unable to connect\n"));
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 = ®_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;
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;
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)));
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 = ®_backend_ldb;
432 newkd->dn = talloc_steal(newkd, msg->dn);
434 *newkey = (struct hive_key *)newkd;
437 talloc_free(parentkd->subkeys);
438 parentkd->subkeys = NULL;
443 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
446 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
447 struct ldb_dn *ldap_path;
448 TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
450 ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
452 ret = ldb_delete(parentkd->ldb, ldap_path);
454 talloc_free(mem_ctx);
456 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
458 } else if (ret != LDB_SUCCESS) {
459 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(parentkd->ldb)));
464 talloc_free(parentkd->subkeys);
465 parentkd->subkeys = NULL;
470 static WERROR ldb_del_value (struct hive_key *key, const char *child)
473 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
474 struct ldb_dn *childdn;
476 childdn = ldb_dn_copy(kd->ldb, kd->dn);
477 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
478 reg_ldb_escape(childdn, child)))
480 talloc_free(childdn);
484 ret = ldb_delete(kd->ldb, childdn);
486 talloc_free(childdn);
488 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
490 } else if (ret != LDB_SUCCESS) {
491 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
496 talloc_free(kd->values);
502 static WERROR ldb_set_value(struct hive_key *parent,
503 const char *name, uint32_t type,
504 const DATA_BLOB data)
506 struct ldb_message *msg;
507 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
509 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
511 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
513 msg->dn = ldb_dn_copy(msg, kd->dn);
514 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
515 reg_ldb_escape(mem_ctx, name)))
517 talloc_free(mem_ctx);
521 ret = ldb_add(kd->ldb, msg);
522 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
524 for (i = 0; i < msg->num_elements; i++) {
525 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
527 ret = ldb_modify(kd->ldb, msg);
530 if (ret != LDB_SUCCESS) {
531 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(kd->ldb)));
532 talloc_free(mem_ctx);
537 talloc_free(kd->values);
540 talloc_free(mem_ctx);
544 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
545 const struct hive_key *key,
546 const char **classname,
547 uint32_t *num_subkeys,
548 uint32_t *num_values,
549 NTTIME *last_change_time,
550 uint32_t *max_subkeynamelen,
551 uint32_t *max_valnamelen,
552 uint32_t *max_valbufsize)
554 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
556 if (kd->subkeys == NULL) {
557 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
560 if (kd->values == NULL) {
561 W_ERROR_NOT_OK_RETURN(cache_values(kd));
565 if (classname != NULL)
568 if (num_subkeys != NULL) {
569 *num_subkeys = kd->subkey_count;
572 if (num_values != NULL) {
573 *num_values = kd->value_count;
576 if (last_change_time != NULL)
577 *last_change_time = 0;
579 if (max_subkeynamelen != NULL) {
581 struct ldb_message_element *el;
583 *max_subkeynamelen = 0;
585 for (i = 0; i < kd->subkey_count; i++) {
586 el = ldb_msg_find_element(kd->subkeys[i], "key");
587 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
591 if (max_valnamelen != NULL || max_valbufsize != NULL) {
593 struct ldb_message_element *el;
594 W_ERROR_NOT_OK_RETURN(cache_values(kd));
596 if (max_valbufsize != NULL)
599 if (max_valnamelen != NULL)
602 for (i = 0; i < kd->value_count; i++) {
603 if (max_valnamelen != NULL) {
604 el = ldb_msg_find_element(kd->values[i], "value");
605 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
608 if (max_valbufsize != NULL) {
610 reg_ldb_unpack_value(mem_ctx, kd->values[i], NULL,
612 *max_valbufsize = MAX(*max_valbufsize, data.length);
613 talloc_free(data.data);
621 static struct hive_operations reg_backend_ldb = {
623 .add_key = ldb_add_key,
624 .del_key = ldb_del_key,
625 .get_key_by_name = ldb_open_key,
626 .enum_value = ldb_get_value_by_id,
627 .enum_key = ldb_get_subkey_by_id,
628 .set_value = ldb_set_value,
629 .get_value_by_name = ldb_get_value,
630 .delete_value = ldb_del_value,
631 .get_key_info = ldb_get_key_info,