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);
115 static int reg_close_ldb_key(struct ldb_key_data *key)
117 if (key->subkeys != NULL) {
118 talloc_free(key->subkeys);
122 if (key->values != NULL) {
123 talloc_free(key->values);
129 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
130 const struct hive_key *from,
131 const char *path, const char *add)
133 TALLOC_CTX *local_ctx;
135 char *mypath = talloc_strdup(mem_ctx, path);
137 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
138 struct ldb_context *ldb = kd->ldb;
140 local_ctx = talloc_new(mem_ctx);
143 ret = ldb_dn_new(mem_ctx, ldb, add);
145 ret = ldb_dn_new(mem_ctx, ldb, NULL);
147 if (!ldb_dn_validate(ret)) {
149 talloc_free(local_ctx);
156 begin = strrchr(mypath, '\\');
158 if (begin) keyname = begin + 1;
159 else keyname = mypath;
161 if(strlen(keyname)) {
162 ldb_dn_add_base_fmt(ret, "key=%s", keyname);
172 ldb_dn_add_base(ret, kd->dn);
174 talloc_free(local_ctx);
179 static WERROR cache_subkeys(struct ldb_key_data *kd)
181 struct ldb_context *c = kd->ldb;
182 struct ldb_result *res;
185 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL, &res);
187 if (ret != LDB_SUCCESS) {
188 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
189 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
193 kd->subkey_count = res->count;
194 kd->subkeys = talloc_steal(kd, res->msgs);
200 static WERROR cache_values(struct ldb_key_data *kd)
202 struct ldb_context *c = kd->ldb;
203 struct ldb_result *res;
206 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL,
207 "(value=*)", NULL, &res);
209 if (ret != LDB_SUCCESS) {
210 DEBUG(0, ("Error getting values for '%s': %s\n",
211 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
214 kd->value_count = res->count;
215 kd->values = talloc_steal(kd, res->msgs);
221 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
222 const struct hive_key *k, uint32_t idx,
224 const char **classname,
225 NTTIME *last_mod_time)
227 struct ldb_message_element *el;
228 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
230 /* Do a search if necessary */
231 if (kd->subkeys == NULL) {
232 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
235 if (idx >= kd->subkey_count)
236 return WERR_NO_MORE_ITEMS;
238 el = ldb_msg_find_element(kd->subkeys[idx], "key");
239 SMB_ASSERT(el != NULL);
240 SMB_ASSERT(el->num_values != 0);
243 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
245 if (classname != NULL)
246 *classname = NULL; /* TODO: Store properly */
248 if (last_mod_time != NULL)
249 *last_mod_time = 0; /* TODO: we need to add this to the
250 ldb backend properly */
255 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
256 int idx, const char **name,
257 uint32_t *data_type, DATA_BLOB *data)
259 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
261 /* Do the search if necessary */
262 if (kd->values == NULL) {
263 W_ERROR_NOT_OK_RETURN(cache_values(kd));
266 if (idx >= kd->value_count)
267 return WERR_NO_MORE_ITEMS;
269 reg_ldb_unpack_value(mem_ctx, kd->values[idx],
270 name, data_type, data);
275 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
276 const char *name, uint32_t *data_type,
279 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
280 struct ldb_context *c = kd->ldb;
281 struct ldb_result *res;
283 char *query = talloc_asprintf(mem_ctx, "(value=%s)", name);
285 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, query, NULL, &res);
289 if (ret != LDB_SUCCESS) {
290 DEBUG(0, ("Error getting values for '%s': %s\n",
291 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
296 return WERR_NOT_FOUND;
298 reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
303 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
304 const char *name, struct hive_key **key)
306 struct ldb_result *res;
307 struct ldb_dn *ldap_path;
309 struct ldb_key_data *newkd;
310 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
311 struct ldb_context *c = kd->ldb;
313 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
315 ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL, &res);
317 if (ret != LDB_SUCCESS) {
318 DEBUG(3, ("Error opening key '%s': %s\n",
319 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
321 } else if (res->count == 0) {
322 DEBUG(3, ("Key '%s' not found\n",
323 ldb_dn_get_linearized(ldap_path)));
325 return WERR_NOT_FOUND;
328 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
329 newkd->key.ops = ®_backend_ldb;
330 newkd->ldb = talloc_reference(newkd, kd->ldb);
331 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
333 *key = (struct hive_key *)newkd;
340 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
341 struct auth_session_info *session_info,
342 struct cli_credentials *credentials,
343 struct loadparm_context *lp_ctx,
346 struct ldb_key_data *kd;
347 struct ldb_context *wrap;
348 struct ldb_message *attrs_msg;
350 if (location == NULL)
351 return WERR_INVALID_PARAM;
353 wrap = ldb_wrap_connect(parent_ctx, lp_ctx,
354 location, session_info, credentials, 0, NULL);
357 DEBUG(1, (__FILE__": unable to connect\n"));
361 attrs_msg = ldb_msg_new(wrap);
362 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
363 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
364 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
365 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
366 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
368 ldb_add(wrap, attrs_msg);
370 ldb_set_debug_stderr(wrap);
372 kd = talloc_zero(parent_ctx, struct ldb_key_data);
373 kd->key.ops = ®_backend_ldb;
374 kd->ldb = talloc_reference(kd, wrap);
375 talloc_set_destructor (kd, reg_close_ldb_key);
376 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
378 *k = (struct hive_key *)kd;
383 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
384 const char *name, const char *classname,
385 struct security_descriptor *sd,
386 struct hive_key **newkey)
388 const struct ldb_key_data *parentkd = (const struct ldb_key_data *)parent;
389 struct ldb_message *msg;
390 struct ldb_key_data *newkd;
393 msg = ldb_msg_new(mem_ctx);
395 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
397 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
398 if (classname != NULL)
399 ldb_msg_add_string(msg, "classname",
400 talloc_strdup(mem_ctx, classname));
402 ret = ldb_add(parentkd->ldb, msg);
404 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(parentkd->ldb)));
408 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
410 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
411 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
412 newkd->key.ops = ®_backend_ldb;
413 newkd->dn = talloc_steal(newkd, msg->dn);
415 *newkey = (struct hive_key *)newkd;
420 static WERROR ldb_del_key(const struct hive_key *key, const char *child)
423 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
424 struct ldb_dn *childdn;
426 childdn = ldb_dn_copy(parentkd->ldb, parentkd->dn);
427 ldb_dn_add_child_fmt(childdn, "key=%s", child);
429 ret = ldb_delete(parentkd->ldb, childdn);
431 talloc_free(childdn);
433 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
434 return WERR_NOT_FOUND;
435 } else if (ret < 0) {
436 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(parentkd->ldb)));
443 static WERROR ldb_del_value (struct hive_key *key, const char *child)
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 ldb_dn_add_child_fmt(childdn, "value=%s", child);
452 ret = ldb_delete(kd->ldb, childdn);
454 talloc_free(childdn);
456 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
457 return WERR_NOT_FOUND;
458 } else if (ret < 0) {
459 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
466 static WERROR ldb_set_value(struct hive_key *parent,
467 const char *name, uint32_t type,
468 const DATA_BLOB data)
470 struct ldb_message *msg;
471 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
473 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
475 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
477 msg->dn = ldb_dn_copy(msg, kd->dn);
478 ldb_dn_add_child_fmt(msg->dn, "value=%s", name);
480 ret = ldb_add(kd->ldb, msg);
481 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
483 for (i = 0; i < msg->num_elements; i++) {
484 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
486 ret = ldb_modify(kd->ldb, msg);
489 if (ret != LDB_SUCCESS) {
490 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(kd->ldb)));
491 talloc_free(mem_ctx);
495 talloc_free(mem_ctx);
499 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
500 const struct hive_key *key,
501 const char **classname,
502 uint32_t *num_subkeys,
503 uint32_t *num_values,
504 NTTIME *last_change_time,
505 uint32_t *max_subkeynamelen,
506 uint32_t *max_valnamelen,
507 uint32_t *max_valbufsize)
509 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
512 if (classname != NULL)
515 if (num_subkeys != NULL) {
516 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
517 *num_subkeys = kd->subkey_count;
520 if (num_values != NULL) {
521 W_ERROR_NOT_OK_RETURN(cache_values(kd));
522 *num_values = kd->value_count;
525 if (last_change_time != NULL)
526 *last_change_time = 0;
528 if (max_subkeynamelen != NULL) {
530 struct ldb_message_element *el;
531 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
533 *max_subkeynamelen = 0;
535 for (i = 0; i < kd->subkey_count; i++) {
536 el = ldb_msg_find_element(kd->subkeys[i], "key");
537 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
541 if (max_valnamelen != NULL || max_valbufsize != NULL) {
543 struct ldb_message_element *el;
544 W_ERROR_NOT_OK_RETURN(cache_values(kd));
546 if (max_valbufsize != NULL)
549 if (max_valnamelen != NULL)
552 for (i = 0; i < kd->value_count; i++) {
553 if (max_valnamelen != NULL) {
554 el = ldb_msg_find_element(kd->values[i], "value");
555 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
558 if (max_valbufsize != NULL) {
560 reg_ldb_unpack_value(mem_ctx, kd->values[i], NULL,
562 *max_valbufsize = MAX(*max_valbufsize, data.length);
563 talloc_free(data.data);
571 static struct hive_operations reg_backend_ldb = {
573 .add_key = ldb_add_key,
574 .del_key = ldb_del_key,
575 .get_key_by_name = ldb_open_key,
576 .enum_value = ldb_get_value_by_id,
577 .enum_key = ldb_get_subkey_by_id,
578 .set_value = ldb_set_value,
579 .get_value_by_name = ldb_get_value,
580 .delete_value = ldb_del_value,
581 .get_key_info = ldb_get_key_info,