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 char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
119 val.data = discard_const_p(uint8_t, value);
120 val.length = strlen(value);
122 return ldb_dn_escape_value(mem_ctx, val);
125 static int reg_close_ldb_key(struct ldb_key_data *key)
127 if (key->subkeys != NULL) {
128 talloc_free(key->subkeys);
132 if (key->values != NULL) {
133 talloc_free(key->values);
139 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
140 const struct hive_key *from,
141 const char *path, const char *add)
143 TALLOC_CTX *local_ctx;
145 char *mypath = talloc_strdup(mem_ctx, path);
147 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
148 struct ldb_context *ldb = kd->ldb;
150 local_ctx = talloc_new(mem_ctx);
153 ret = ldb_dn_new(mem_ctx, ldb, add);
155 ret = ldb_dn_new(mem_ctx, ldb, NULL);
157 if (!ldb_dn_validate(ret)) {
159 talloc_free(local_ctx);
166 begin = strrchr(mypath, '\\');
168 if (begin) keyname = begin + 1;
169 else keyname = mypath;
171 if(strlen(keyname)) {
172 ldb_dn_add_base_fmt(ret, "key=%s", keyname);
182 ldb_dn_add_base(ret, kd->dn);
184 talloc_free(local_ctx);
189 static WERROR cache_subkeys(struct ldb_key_data *kd)
191 struct ldb_context *c = kd->ldb;
192 struct ldb_result *res;
195 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL, &res);
197 if (ret != LDB_SUCCESS) {
198 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
199 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
203 kd->subkey_count = res->count;
204 kd->subkeys = talloc_steal(kd, res->msgs);
210 static WERROR cache_values(struct ldb_key_data *kd)
212 struct ldb_context *c = kd->ldb;
213 struct ldb_result *res;
216 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL,
217 "(value=*)", NULL, &res);
219 if (ret != LDB_SUCCESS) {
220 DEBUG(0, ("Error getting values for '%s': %s\n",
221 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
224 kd->value_count = res->count;
225 kd->values = talloc_steal(kd, res->msgs);
231 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
232 const struct hive_key *k, uint32_t idx,
234 const char **classname,
235 NTTIME *last_mod_time)
237 struct ldb_message_element *el;
238 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
240 /* Do a search if necessary */
241 if (kd->subkeys == NULL) {
242 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
245 if (idx >= kd->subkey_count)
246 return WERR_NO_MORE_ITEMS;
248 el = ldb_msg_find_element(kd->subkeys[idx], "key");
249 SMB_ASSERT(el != NULL);
250 SMB_ASSERT(el->num_values != 0);
253 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
255 if (classname != NULL)
256 *classname = NULL; /* TODO: Store properly */
258 if (last_mod_time != NULL)
259 *last_mod_time = 0; /* TODO: we need to add this to the
260 ldb backend properly */
265 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
266 int idx, const char **name,
267 uint32_t *data_type, DATA_BLOB *data)
269 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
271 /* Do the search if necessary */
272 if (kd->values == NULL) {
273 W_ERROR_NOT_OK_RETURN(cache_values(kd));
276 if (idx >= kd->value_count)
277 return WERR_NO_MORE_ITEMS;
279 reg_ldb_unpack_value(mem_ctx, kd->values[idx],
280 name, data_type, data);
285 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
286 const char *name, uint32_t *data_type,
289 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
290 struct ldb_context *c = kd->ldb;
291 struct ldb_result *res;
293 char *query = talloc_asprintf(mem_ctx, "(value=%s)", name);
295 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, query, NULL, &res);
299 if (ret != LDB_SUCCESS) {
300 DEBUG(0, ("Error getting values for '%s': %s\n",
301 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
306 return WERR_NOT_FOUND;
308 reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
313 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
314 const char *name, struct hive_key **key)
316 struct ldb_result *res;
317 struct ldb_dn *ldap_path;
319 struct ldb_key_data *newkd;
320 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
321 struct ldb_context *c = kd->ldb;
323 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
325 ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL, &res);
327 if (ret != LDB_SUCCESS) {
328 DEBUG(3, ("Error opening key '%s': %s\n",
329 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
331 } else if (res->count == 0) {
332 DEBUG(3, ("Key '%s' not found\n",
333 ldb_dn_get_linearized(ldap_path)));
335 return WERR_NOT_FOUND;
338 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
339 newkd->key.ops = ®_backend_ldb;
340 newkd->ldb = talloc_reference(newkd, kd->ldb);
341 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
343 *key = (struct hive_key *)newkd;
350 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
351 struct auth_session_info *session_info,
352 struct cli_credentials *credentials,
353 struct loadparm_context *lp_ctx,
356 struct ldb_key_data *kd;
357 struct ldb_context *wrap;
358 struct ldb_message *attrs_msg;
360 if (location == NULL)
361 return WERR_INVALID_PARAM;
363 wrap = ldb_wrap_connect(parent_ctx, lp_ctx,
364 location, session_info, credentials, 0, NULL);
367 DEBUG(1, (__FILE__": unable to connect\n"));
371 attrs_msg = ldb_msg_new(wrap);
372 W_ERROR_HAVE_NO_MEMORY(attrs_msg);
373 attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
374 W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
375 ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
376 ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
378 ldb_add(wrap, attrs_msg);
380 ldb_set_debug_stderr(wrap);
382 kd = talloc_zero(parent_ctx, struct ldb_key_data);
383 kd->key.ops = ®_backend_ldb;
384 kd->ldb = talloc_reference(kd, wrap);
385 talloc_set_destructor (kd, reg_close_ldb_key);
386 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
388 *k = (struct hive_key *)kd;
393 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
394 const char *name, const char *classname,
395 struct security_descriptor *sd,
396 struct hive_key **newkey)
398 const struct ldb_key_data *parentkd = (const struct ldb_key_data *)parent;
399 struct ldb_message *msg;
400 struct ldb_key_data *newkd;
403 msg = ldb_msg_new(mem_ctx);
405 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
407 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
408 if (classname != NULL)
409 ldb_msg_add_string(msg, "classname",
410 talloc_strdup(mem_ctx, classname));
412 ret = ldb_add(parentkd->ldb, msg);
413 if (ret != LDB_SUCCESS) {
414 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(parentkd->ldb)));
418 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
420 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
421 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
422 newkd->key.ops = ®_backend_ldb;
423 newkd->dn = talloc_steal(newkd, msg->dn);
425 *newkey = (struct hive_key *)newkd;
430 static WERROR ldb_del_key(const struct hive_key *key, const char *child)
433 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
434 struct ldb_dn *childdn;
436 childdn = ldb_dn_copy(parentkd->ldb, parentkd->dn);
437 ldb_dn_add_child_fmt(childdn, "key=%s", child);
439 ret = ldb_delete(parentkd->ldb, childdn);
441 talloc_free(childdn);
443 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
444 return WERR_NOT_FOUND;
445 } else if (ret != LDB_SUCCESS) {
446 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(parentkd->ldb)));
453 static WERROR ldb_del_value (struct hive_key *key, const char *child)
456 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
457 struct ldb_dn *childdn;
459 childdn = ldb_dn_copy(kd->ldb, kd->dn);
460 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
461 reg_ldb_escape(childdn, child)))
463 talloc_free(childdn);
467 ret = ldb_delete(kd->ldb, childdn);
469 talloc_free(childdn);
471 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
472 return WERR_NOT_FOUND;
473 } else if (ret != LDB_SUCCESS) {
474 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
481 static WERROR ldb_set_value(struct hive_key *parent,
482 const char *name, uint32_t type,
483 const DATA_BLOB data)
485 struct ldb_message *msg;
486 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
488 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
490 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
492 msg->dn = ldb_dn_copy(msg, kd->dn);
493 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
494 reg_ldb_escape(mem_ctx, name)))
496 talloc_free(mem_ctx);
500 ret = ldb_add(kd->ldb, msg);
501 if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
503 for (i = 0; i < msg->num_elements; i++) {
504 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
506 ret = ldb_modify(kd->ldb, msg);
509 if (ret != LDB_SUCCESS) {
510 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(kd->ldb)));
511 talloc_free(mem_ctx);
515 talloc_free(mem_ctx);
519 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
520 const struct hive_key *key,
521 const char **classname,
522 uint32_t *num_subkeys,
523 uint32_t *num_values,
524 NTTIME *last_change_time,
525 uint32_t *max_subkeynamelen,
526 uint32_t *max_valnamelen,
527 uint32_t *max_valbufsize)
529 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
532 if (classname != NULL)
535 if (num_subkeys != NULL) {
536 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
537 *num_subkeys = kd->subkey_count;
540 if (num_values != NULL) {
541 W_ERROR_NOT_OK_RETURN(cache_values(kd));
542 *num_values = kd->value_count;
545 if (last_change_time != NULL)
546 *last_change_time = 0;
548 if (max_subkeynamelen != NULL) {
550 struct ldb_message_element *el;
551 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
553 *max_subkeynamelen = 0;
555 for (i = 0; i < kd->subkey_count; i++) {
556 el = ldb_msg_find_element(kd->subkeys[i], "key");
557 *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
561 if (max_valnamelen != NULL || max_valbufsize != NULL) {
563 struct ldb_message_element *el;
564 W_ERROR_NOT_OK_RETURN(cache_values(kd));
566 if (max_valbufsize != NULL)
569 if (max_valnamelen != NULL)
572 for (i = 0; i < kd->value_count; i++) {
573 if (max_valnamelen != NULL) {
574 el = ldb_msg_find_element(kd->values[i], "value");
575 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
578 if (max_valbufsize != NULL) {
580 reg_ldb_unpack_value(mem_ctx, kd->values[i], NULL,
582 *max_valbufsize = MAX(*max_valbufsize, data.length);
583 talloc_free(data.data);
591 static struct hive_operations reg_backend_ldb = {
593 .add_key = ldb_add_key,
594 .del_key = ldb_del_key,
595 .get_key_by_name = ldb_open_key,
596 .enum_value = ldb_get_value_by_id,
597 .enum_key = ldb_get_subkey_by_id,
598 .set_value = ldb_set_value,
599 .get_value_by_name = ldb_get_value,
600 .delete_value = ldb_del_value,
601 .get_key_info = ldb_get_key_info,