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;
45 *name = talloc_strdup(mem_ctx,
46 ldb_msg_find_attr_as_string(msg, "value",
50 *type = ldb_msg_find_attr_as_uint(msg, "type", 0);
51 val = ldb_msg_find_ldb_val(msg, "data");
57 data->length = convert_string_talloc(mem_ctx, global_smb_iconv_convenience, CH_UTF8, CH_UTF16,
58 val->data, val->length,
59 (void **)&data->data);
63 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
64 *data = data_blob_talloc(mem_ctx, &tmp, 4);
69 *data = data_blob_talloc(mem_ctx, val->data, val->length);
74 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
77 uint32_t type, DATA_BLOB data)
80 struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
83 ldb_msg_add_string(msg, "value", talloc_strdup(mem_ctx, name));
88 val.length = convert_string_talloc(mem_ctx, global_smb_iconv_convenience, CH_UTF16, CH_UNIX,
92 ldb_msg_add_value(msg, "data", &val, NULL);
96 ldb_msg_add_string(msg, "data",
97 talloc_asprintf(mem_ctx, "0x%x",
101 ldb_msg_add_value(msg, "data", &data, NULL);
105 type_s = talloc_asprintf(mem_ctx, "%u", type);
106 ldb_msg_add_string(msg, "type", type_s);
112 static int reg_close_ldb_key(struct ldb_key_data *key)
114 if (key->subkeys != NULL) {
115 talloc_free(key->subkeys);
119 if (key->values != NULL) {
120 talloc_free(key->values);
126 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
127 const struct hive_key *from,
128 const char *path, const char *add)
130 TALLOC_CTX *local_ctx;
132 char *mypath = talloc_strdup(mem_ctx, path);
134 struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
135 struct ldb_context *ldb = kd->ldb;
137 local_ctx = talloc_new(mem_ctx);
140 ret = ldb_dn_new(mem_ctx, ldb, add);
142 ret = ldb_dn_new(mem_ctx, ldb, NULL);
144 if (!ldb_dn_validate(ret)) {
146 talloc_free(local_ctx);
153 begin = strrchr(mypath, '\\');
155 if (begin) keyname = begin + 1;
156 else keyname = mypath;
158 if(strlen(keyname)) {
159 ldb_dn_add_base_fmt(ret, "key=%s", keyname);
169 ldb_dn_add_base(ret, kd->dn);
171 talloc_free(local_ctx);
176 static WERROR cache_subkeys(struct ldb_key_data *kd)
178 struct ldb_context *c = kd->ldb;
179 struct ldb_result *res;
182 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL, &res);
184 if (ret != LDB_SUCCESS) {
185 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
186 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
190 kd->subkey_count = res->count;
191 kd->subkeys = talloc_steal(kd, res->msgs);
197 static WERROR cache_values(struct ldb_key_data *kd)
199 struct ldb_context *c = kd->ldb;
200 struct ldb_result *res;
203 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL,
204 "(value=*)", NULL, &res);
206 if (ret != LDB_SUCCESS) {
207 DEBUG(0, ("Error getting values for '%s': %s\n",
208 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
211 kd->value_count = res->count;
212 kd->values = talloc_steal(kd, res->msgs);
218 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
219 const struct hive_key *k, uint32_t idx,
221 const char **classname,
222 NTTIME *last_mod_time)
224 struct ldb_message_element *el;
225 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
227 /* Do a search if necessary */
228 if (kd->subkeys == NULL) {
229 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
232 if (idx >= kd->subkey_count)
233 return WERR_NO_MORE_ITEMS;
235 el = ldb_msg_find_element(kd->subkeys[idx], "key");
236 SMB_ASSERT(el != NULL);
237 SMB_ASSERT(el->num_values != 0);
240 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
242 if (classname != NULL)
243 *classname = NULL; /* TODO: Store properly */
245 if (last_mod_time != NULL)
246 *last_mod_time = 0; /* TODO: we need to add this to the
247 ldb backend properly */
252 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, const struct hive_key *k,
253 int idx, const char **name,
254 uint32_t *data_type, DATA_BLOB *data)
256 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
258 /* Do the search if necessary */
259 if (kd->values == NULL) {
260 W_ERROR_NOT_OK_RETURN(cache_values(kd));
263 if (idx >= kd->value_count)
264 return WERR_NO_MORE_ITEMS;
266 reg_ldb_unpack_value(mem_ctx, kd->values[idx],
267 name, data_type, data);
272 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
273 const char *name, uint32_t *data_type,
276 struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
277 struct ldb_context *c = kd->ldb;
278 struct ldb_result *res;
280 char *query = talloc_asprintf(mem_ctx, "(value=%s)", name);
282 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, query, NULL, &res);
286 if (ret != LDB_SUCCESS) {
287 DEBUG(0, ("Error getting values for '%s': %s\n",
288 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
293 return WERR_NOT_FOUND;
295 reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
300 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
301 const char *name, struct hive_key **key)
303 struct ldb_result *res;
304 struct ldb_dn *ldap_path;
306 struct ldb_key_data *newkd;
307 struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
308 struct ldb_context *c = kd->ldb;
310 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
312 ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL, &res);
314 if (ret != LDB_SUCCESS) {
315 DEBUG(3, ("Error opening key '%s': %s\n",
316 ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
318 } else if (res->count == 0) {
319 DEBUG(3, ("Key '%s' not found\n",
320 ldb_dn_get_linearized(ldap_path)));
322 return WERR_NOT_FOUND;
325 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
326 newkd->key.ops = ®_backend_ldb;
327 newkd->ldb = talloc_reference(newkd, kd->ldb);
328 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
330 *key = (struct hive_key *)newkd;
337 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
338 struct auth_session_info *session_info,
339 struct cli_credentials *credentials,
342 struct ldb_key_data *kd;
343 struct ldb_context *wrap;
345 if (location == NULL)
346 return WERR_INVALID_PARAM;
348 wrap = ldb_wrap_connect(parent_ctx, global_loadparm,
349 location, session_info, credentials, 0, NULL);
352 DEBUG(1, (__FILE__": unable to connect\n"));
356 ldb_set_debug_stderr(wrap);
358 kd = talloc_zero(parent_ctx, struct ldb_key_data);
359 kd->key.ops = ®_backend_ldb;
360 kd->ldb = talloc_reference(kd, wrap);
361 talloc_set_destructor (kd, reg_close_ldb_key);
362 kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
364 *k = (struct hive_key *)kd;
369 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
370 const char *name, const char *classname,
371 struct security_descriptor *sd,
372 struct hive_key **newkey)
374 const struct ldb_key_data *parentkd = (const struct ldb_key_data *)parent;
375 struct ldb_message *msg;
376 struct ldb_key_data *newkd;
379 msg = ldb_msg_new(mem_ctx);
381 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
383 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
384 if (classname != NULL)
385 ldb_msg_add_string(msg, "classname",
386 talloc_strdup(mem_ctx, classname));
388 ret = ldb_add(parentkd->ldb, msg);
390 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(parentkd->ldb)));
394 DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
396 newkd = talloc_zero(mem_ctx, struct ldb_key_data);
397 newkd->ldb = talloc_reference(newkd, parentkd->ldb);
398 newkd->key.ops = ®_backend_ldb;
399 newkd->dn = talloc_steal(newkd, msg->dn);
401 *newkey = (struct hive_key *)newkd;
406 static WERROR ldb_del_key(const struct hive_key *key, const char *child)
409 struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
410 struct ldb_dn *childdn;
412 childdn = ldb_dn_copy(parentkd->ldb, parentkd->dn);
413 ldb_dn_add_child_fmt(childdn, "key=%s", child);
415 ret = ldb_delete(parentkd->ldb, childdn);
417 talloc_free(childdn);
419 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
420 return WERR_NOT_FOUND;
421 } else if (ret < 0) {
422 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(parentkd->ldb)));
429 static WERROR ldb_del_value (struct hive_key *key, const char *child)
432 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
433 struct ldb_dn *childdn;
435 childdn = ldb_dn_copy(kd->ldb, kd->dn);
436 ldb_dn_add_child_fmt(childdn, "value=%s", child);
438 ret = ldb_delete(kd->ldb, childdn);
440 talloc_free(childdn);
442 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
443 return WERR_NOT_FOUND;
444 } else if (ret < 0) {
445 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
452 static WERROR ldb_set_value(struct hive_key *parent,
453 const char *name, uint32_t type,
454 const DATA_BLOB data)
456 struct ldb_message *msg;
457 struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
459 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
461 msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
463 msg->dn = ldb_dn_copy(msg, kd->dn);
464 ldb_dn_add_child_fmt(msg->dn, "value=%s", name);
466 ret = ldb_add(kd->ldb, msg);
468 ret = ldb_modify(kd->ldb, msg);
470 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(kd->ldb)));
471 talloc_free(mem_ctx);
476 talloc_free(mem_ctx);
480 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
481 const struct hive_key *key,
482 const char **classname,
483 uint32_t *num_subkeys,
484 uint32_t *num_values,
485 NTTIME *last_change_time)
487 struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
490 if (classname != NULL)
493 if (num_subkeys != NULL) {
494 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
495 *num_subkeys = kd->subkey_count;
498 if (num_values != NULL) {
499 W_ERROR_NOT_OK_RETURN(cache_values(kd));
500 *num_values = kd->value_count;
503 if (last_change_time != NULL)
504 *last_change_time = 0;
509 static struct hive_operations reg_backend_ldb = {
511 .add_key = ldb_add_key,
512 .del_key = ldb_del_key,
513 .get_key_by_name = ldb_open_key,
514 .enum_value = ldb_get_value_by_id,
515 .enum_key = ldb_get_subkey_by_id,
516 .set_value = ldb_set_value,
517 .get_value_by_name = ldb_get_value,
518 .delete_value = ldb_del_value,
519 .get_key_info = ldb_get_key_info,