2 Unix SMB/CIFS implementation.
4 Copyright (C) Jelmer Vernooij 2004.
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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "lib/ldb/include/ldb.h"
24 #include "lib/ldb/include/ldb_errors.h"
26 #include "librpc/gen_ndr/winreg.h"
30 const struct ldb_dn *dn;
31 struct ldb_message **subkeys, **values;
32 int subkey_count, value_count;
35 static int ldb_free_hive (void *_hive)
37 struct registry_hive *hive = _hive;
38 talloc_free(hive->backend_data);
39 hive->backend_data = NULL;
43 static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx, struct ldb_message *msg, char **name, uint32_t *type, DATA_BLOB *data)
45 const struct ldb_val *val;
46 *name = talloc_strdup(mem_ctx, ldb_msg_find_string(msg, "value", NULL));
47 *type = ldb_msg_find_uint(msg, "type", 0);
48 val = ldb_msg_find_ldb_val(msg, "data");
54 data->length = convert_string_talloc(mem_ctx, CH_UTF8, CH_UTF16, val->data, val->length, (void **)&data->data);
58 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
59 *data = data_blob_talloc(mem_ctx, &tmp, 4);
64 *data = data_blob_talloc(mem_ctx, val->data, val->length);
69 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx, TALLOC_CTX *mem_ctx, const char *name, uint32_t type, DATA_BLOB data)
72 struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
75 ldb_msg_add_string(msg, "value", talloc_strdup(mem_ctx, name));
80 val.length = convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8, (void *)data.data, data.length, (void **)&val.data);
81 ldb_msg_add_value(msg, "data", &val);
85 ldb_msg_add_string(msg, "data", talloc_asprintf(mem_ctx, "0x%x", IVAL(data.data, 0)));
88 ldb_msg_add_value(msg, "data", &data);
92 type_s = talloc_asprintf(mem_ctx, "%u", type);
93 ldb_msg_add_string(msg, "type", type_s);
99 static int reg_close_ldb_key (void *data)
101 struct registry_key *key = data;
102 struct ldb_key_data *kd = talloc_get_type(key->backend_data, struct ldb_key_data);
103 /* struct ldb_context *c = key->hive->backend_data; */
106 talloc_free(kd->subkeys);
111 talloc_free(kd->values);
117 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx, const struct registry_key *from, const char *path, const char *add)
119 TALLOC_CTX *local_ctx;
120 struct ldb_dn *ret = ldb_dn_new(mem_ctx);
121 char *mypath = talloc_strdup(mem_ctx, path);
123 struct ldb_key_data *kd = talloc_get_type(from->backend_data, struct ldb_key_data);
125 local_ctx = talloc_new(mem_ctx);
128 ret = ldb_dn_compose(local_ctx, ret, ldb_dn_explode(mem_ctx, add));
133 begin = strrchr(mypath, '\\');
135 if (begin) keyname = begin + 1;
136 else keyname = mypath;
138 if(strlen(keyname)) {
141 base = ldb_dn_build_child(local_ctx, "key", keyname, NULL);
142 ret = ldb_dn_compose(local_ctx, ret, base);
152 ret = ldb_dn_compose(mem_ctx, ret, kd->dn);
154 talloc_free(local_ctx);
160 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx, const struct registry_key *k, int idx, struct registry_key **subkey)
162 struct ldb_context *c = talloc_get_type(k->hive->backend_data, struct ldb_context);
163 struct ldb_message_element *el;
164 struct ldb_key_data *kd = talloc_get_type(k->backend_data, struct ldb_key_data);
165 struct ldb_key_data *newkd;
167 /* Do a search if necessary */
168 if (kd->subkeys == NULL) {
169 struct ldb_result *res;
172 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL, &res);
174 if (ret != LDB_SUCCESS) {
175 DEBUG(0, ("Error getting subkeys for '%s': %s\n", ldb_dn_linearize(mem_ctx, kd->dn), ldb_errstring(c)));
179 kd->subkey_count = res->count;
180 kd->subkeys = talloc_steal(kd, res->msgs);
184 if (idx >= kd->subkey_count) return WERR_NO_MORE_ITEMS;
186 el = ldb_msg_find_element(kd->subkeys[idx], "key");
188 *subkey = talloc(mem_ctx, struct registry_key);
189 talloc_set_destructor(*subkey, reg_close_ldb_key);
190 (*subkey)->name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
191 (*subkey)->backend_data = newkd = talloc_zero(*subkey, struct ldb_key_data);
192 (*subkey)->last_mod = 0; /* TODO: we need to add this to the
193 ldb backend properly */
194 newkd->dn = ldb_dn_copy(mem_ctx, kd->subkeys[idx]->dn);
199 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, const struct registry_key *k, int idx, struct registry_value **value)
201 struct ldb_context *c = talloc_get_type(k->hive->backend_data, struct ldb_context);
202 struct ldb_key_data *kd = talloc_get_type(k->backend_data, struct ldb_key_data);
204 /* Do the search if necessary */
205 if (kd->values == NULL) {
206 struct ldb_result *res;
209 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(value=*)", NULL, &res);
211 if (ret != LDB_SUCCESS) {
212 DEBUG(0, ("Error getting values for '%s': %s\n", ldb_dn_linearize(mem_ctx, kd->dn), ldb_errstring(c)));
215 kd->value_count = res->count;
216 kd->values = talloc_steal(kd, res->msgs);
220 if(idx >= kd->value_count) return WERR_NO_MORE_ITEMS;
222 *value = talloc(mem_ctx, struct registry_value);
224 reg_ldb_unpack_value(mem_ctx, kd->values[idx], &(*value)->name, &(*value)->data_type, &(*value)->data);
229 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct registry_key *h, const char *name, struct registry_key **key)
231 struct ldb_context *c = talloc_get_type(h->hive->backend_data, struct ldb_context);
232 struct ldb_result *res;
233 struct ldb_dn *ldap_path;
235 struct ldb_key_data *newkd;
237 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
239 ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL, &res);
241 if (ret != LDB_SUCCESS) {
242 DEBUG(0, ("Error opening key '%s': %s\n", ldb_dn_linearize(ldap_path, ldap_path), ldb_errstring(c)));
244 } else if (res->count == 0) {
248 *key = talloc(mem_ctx, struct registry_key);
249 talloc_set_destructor(*key, reg_close_ldb_key);
250 (*key)->name = talloc_strdup(mem_ctx, strrchr(name, '\\')?strchr(name, '\\'):name);
251 (*key)->backend_data = newkd = talloc_zero(*key, struct ldb_key_data);
252 newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
259 static WERROR ldb_open_hive(struct registry_hive *hive, struct registry_key **k)
261 struct ldb_key_data *kd;
262 struct ldb_context *wrap;
264 if (!hive->location) return WERR_INVALID_PARAM;
265 wrap = ldb_wrap_connect(hive, hive->location, 0, NULL);
268 DEBUG(1, ("ldb_open_hive: unable to connect\n"));
272 ldb_set_debug_stderr(wrap);
273 hive->backend_data = wrap;
275 *k = talloc_zero(hive, struct registry_key);
276 talloc_set_destructor (*k, reg_close_ldb_key);
277 talloc_set_destructor (hive, ldb_free_hive);
278 (*k)->name = talloc_strdup(*k, "");
279 (*k)->backend_data = kd = talloc_zero(*k, struct ldb_key_data);
280 kd->dn = ldb_dn_explode(*k, "hive=NONE");
286 static WERROR ldb_add_key (TALLOC_CTX *mem_ctx, const struct registry_key *parent, const char *name, uint32_t access_mask, struct security_descriptor *sd, struct registry_key **newkey)
288 struct ldb_context *ctx = talloc_get_type(parent->hive->backend_data, struct ldb_context);
289 struct ldb_message *msg;
290 struct ldb_key_data *newkd;
293 msg = ldb_msg_new(mem_ctx);
295 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
297 ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
299 ret = ldb_add(ctx, msg);
301 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(ctx)));
305 *newkey = talloc_zero(mem_ctx, struct registry_key);
306 (*newkey)->name = talloc_strdup(mem_ctx, name);
308 (*newkey)->backend_data = newkd = talloc_zero(*newkey, struct ldb_key_data);
309 newkd->dn = talloc_steal(newkd, msg->dn);
314 static WERROR ldb_del_key (const struct registry_key *key, const char *child)
316 struct ldb_context *ctx = talloc_get_type(key->hive->backend_data, struct ldb_context);
318 struct ldb_key_data *kd = talloc_get_type(key->backend_data, struct ldb_key_data);
319 struct ldb_dn *childdn = ldb_dn_build_child(ctx, "key", child, kd->dn);
321 ret = ldb_delete(ctx, childdn);
323 talloc_free(childdn);
326 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(ctx)));
333 static WERROR ldb_del_value (const struct registry_key *key, const char *child)
336 struct ldb_context *ctx = talloc_get_type(key->hive->backend_data, struct ldb_context);
337 struct ldb_key_data *kd = talloc_get_type(key->backend_data, struct ldb_key_data);
338 struct ldb_dn *childdn = ldb_dn_build_child(ctx, "value", child, kd->dn);
340 ret = ldb_delete(ctx, childdn);
342 talloc_free(childdn);
345 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(ctx)));
352 static WERROR ldb_set_value (const struct registry_key *parent, const char *name, uint32_t type, DATA_BLOB data)
354 struct ldb_context *ctx = talloc_get_type(parent->hive->backend_data, struct ldb_context);
355 struct ldb_message *msg;
356 struct ldb_key_data *kd = talloc_get_type(parent->backend_data, struct ldb_key_data);
358 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
360 msg = reg_ldb_pack_value(ctx, mem_ctx, name, type, data);
362 msg->dn = ldb_dn_build_child(msg, "value", name, kd->dn);
364 ret = ldb_add(ctx, msg);
366 ret = ldb_modify(ctx, msg);
368 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(ctx)));
369 talloc_free(mem_ctx);
374 talloc_free(mem_ctx);
378 static struct hive_operations reg_backend_ldb = {
380 .add_key = ldb_add_key,
381 .del_key = ldb_del_key,
382 .open_hive = ldb_open_hive,
383 .open_key = ldb_open_key,
384 .get_value_by_index = ldb_get_value_by_id,
385 .get_subkey_by_index = ldb_get_subkey_by_id,
386 .set_value = ldb_set_value,
387 .del_value = ldb_del_value,
390 NTSTATUS registry_ldb_init(void)
392 return registry_register(®_backend_ldb);