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"
28 struct ldb_message **subkeys, **values;
29 int subkey_count, value_count;
32 static int ldb_close_hive (void *_hive)
34 struct registry_hive *hive = _hive;
35 ldb_close (hive->backend_data);
39 static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx, struct ldb_message *msg, char **name, uint32 *type, void **data, int *len)
41 const struct ldb_val *val;
42 *name = talloc_strdup(mem_ctx, ldb_msg_find_string(msg, "value", NULL));
43 *type = ldb_msg_find_uint(msg, "type", 0);
44 val = ldb_msg_find_ldb_val(msg, "data");
50 *len = convert_string_talloc(mem_ctx, CH_UTF8, CH_UTF16, val->data, val->length, data);
55 *data = talloc_p(mem_ctx, uint32);
56 SIVAL(*data, 0, strtol(val->data, NULL, 0));
60 *data = talloc_memdup(mem_ctx, val->data, val->length);
66 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx, TALLOC_CTX *mem_ctx, const char *name, uint32 type, void *data, int len)
69 struct ldb_message *msg = talloc_zero_p(mem_ctx, struct ldb_message);
72 ldb_msg_add_string(ctx, msg, "value", talloc_strdup(mem_ctx, name));
77 val.length = convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8, data, len, &val.data);
78 ldb_msg_add_value(ctx, msg, "data", &val);
81 ldb_msg_add_string(ctx, msg, "data", talloc_asprintf(mem_ctx, "0x%x", IVAL(data, 0)));
86 ldb_msg_add_value(ctx, msg, "data", &val);
90 type_s = talloc_asprintf(mem_ctx, "%u", type);
91 ldb_msg_add_string(ctx, msg, "type", type_s);
97 static int reg_close_ldb_key (void *data)
99 struct registry_key *key = data;
100 struct ldb_key_data *kd = key->backend_data;
101 struct ldb_context *c = key->hive->backend_data;
104 ldb_search_free(c, kd->subkeys);
109 ldb_search_free(c, kd->values);
115 static char *reg_path_to_ldb(TALLOC_CTX *mem_ctx, struct registry_key *from, const char *path, const char *add)
117 char *ret = talloc_strdup(mem_ctx, "");
118 char *mypath = talloc_strdup(mem_ctx, path);
120 struct ldb_key_data *kd = from->backend_data;
123 ret = talloc_asprintf_append(ret, "%s", add);
127 begin = strrchr(mypath, '\\');
129 if(begin) keyname = begin + 1;
130 else keyname = mypath;
133 ret = talloc_asprintf_append(ret, "key=%s,", keyname);
142 ret = talloc_asprintf_append(ret, "%s", kd->dn);
148 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx, struct registry_key *k, int idx, struct registry_key **subkey)
150 struct ldb_context *c = k->hive->backend_data;
151 struct ldb_message_element *el;
152 struct ldb_key_data *kd = k->backend_data, *newkd;
154 /* Do a search if necessary */
155 if (kd->subkeys == NULL) {
156 kd->subkey_count = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL, &kd->subkeys);
158 if(kd->subkey_count < 0) {
159 DEBUG(0, ("Error getting subkeys for '%s': %s\n", kd->dn, ldb_errstring(c)));
164 if (idx >= kd->subkey_count) return WERR_NO_MORE_ITEMS;
166 el = ldb_msg_find_element(kd->subkeys[idx], "key");
168 *subkey = talloc_p(mem_ctx, struct registry_key);
169 talloc_set_destructor(*subkey, reg_close_ldb_key);
170 (*subkey)->name = talloc_strdup(mem_ctx, el->values[0].data);
171 (*subkey)->backend_data = newkd = talloc_zero_p(*subkey, struct ldb_key_data);
172 newkd->dn = talloc_strdup(mem_ctx, kd->subkeys[idx]->dn);
177 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct registry_key *k, int idx, struct registry_value **value)
179 struct ldb_context *c = k->hive->backend_data;
180 struct ldb_key_data *kd = k->backend_data;
182 /* Do the search if necessary */
183 if (kd->values == NULL) {
184 kd->value_count = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(value=*)", NULL,&kd->values);
186 if(kd->value_count < 0) {
187 DEBUG(0, ("Error getting values for '%s': %s\n", kd->dn, ldb_errstring(c)));
192 if(idx >= kd->value_count) return WERR_NO_MORE_ITEMS;
194 *value = talloc_p(mem_ctx, struct registry_value);
196 reg_ldb_unpack_value(mem_ctx, kd->values[idx], &(*value)->name, &(*value)->data_type, &(*value)->data_blk, &(*value)->data_len);
201 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h, const char *name, struct registry_key **key)
203 struct ldb_context *c = h->hive->backend_data;
204 struct ldb_message **msg;
207 struct ldb_key_data *newkd;
209 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
211 ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL,&msg);
214 return WERR_NO_MORE_ITEMS;
216 DEBUG(0, ("Error opening key '%s': %s\n", ldap_path, ldb_errstring(c)));
220 *key = talloc_p(mem_ctx, struct registry_key);
221 talloc_set_destructor(*key, reg_close_ldb_key);
222 (*key)->name = talloc_strdup(mem_ctx, strrchr(name, '\\')?strchr(name, '\\'):name);
223 (*key)->backend_data = newkd = talloc_zero_p(*key, struct ldb_key_data);
224 newkd->dn = talloc_strdup(mem_ctx, msg[0]->dn);
226 ldb_search_free(c, msg);
231 static WERROR ldb_open_hive(struct registry_hive *hive, struct registry_key **k)
233 struct ldb_context *c;
234 struct ldb_key_data *kd;
235 struct ldb_wrap *wrap;
237 if (!hive->location) return WERR_INVALID_PARAM;
238 wrap = ldb_wrap_connect(hive, hive->location, 0, NULL);
241 DEBUG(1, ("ldb_open_hive: unable to connect\n"));
247 ldb_set_debug_stderr(c);
248 hive->backend_data = c;
250 *k = talloc_zero_p(hive, struct registry_key);
251 talloc_set_destructor (*k, reg_close_ldb_key);
252 talloc_set_destructor (hive, ldb_close_hive);
253 (*k)->name = talloc_strdup(*k, "");
254 (*k)->backend_data = kd = talloc_zero_p(*k, struct ldb_key_data);
255 kd->dn = talloc_strdup(*k, "hive=");
261 static WERROR ldb_add_key (TALLOC_CTX *mem_ctx, struct registry_key *parent, const char *name, uint32_t access_mask, struct security_descriptor *sd, struct registry_key **newkey)
263 struct ldb_context *ctx = parent->hive->backend_data;
264 struct ldb_message *msg;
265 struct ldb_key_data *newkd;
268 msg = ldb_msg_new(mem_ctx);
270 msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
272 ldb_msg_add_string(ctx, msg, "key", talloc_strdup(mem_ctx, name));
274 ret = ldb_add(ctx, msg);
276 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(parent->hive->backend_data)));
280 *newkey = talloc_zero_p(mem_ctx, struct registry_key);
281 (*newkey)->name = talloc_strdup(mem_ctx, name);
283 (*newkey)->backend_data = newkd = talloc_zero_p(*newkey, struct ldb_key_data);
284 newkd->dn = talloc_steal(newkd, msg->dn);
289 static WERROR ldb_del_key (struct registry_key *key, const char *child)
292 struct ldb_key_data *kd = key->backend_data;
293 char *childdn = talloc_asprintf(NULL, "key=%s,%s", child, kd->dn);
295 ret = ldb_delete(key->hive->backend_data, childdn);
297 talloc_destroy(childdn);
300 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(key->hive->backend_data)));
307 static WERROR ldb_del_value (struct registry_key *key, const char *child)
310 struct ldb_key_data *kd = key->backend_data;
311 char *childdn = talloc_asprintf(NULL, "value=%s,%s", child, kd->dn);
313 ret = ldb_delete(key->hive->backend_data, childdn);
315 talloc_destroy(childdn);
318 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(key->hive->backend_data)));
325 static WERROR ldb_set_value (struct registry_key *parent, const char *name, uint32 type, void *data, int len)
327 struct ldb_context *ctx = parent->hive->backend_data;
328 struct ldb_message *msg;
329 struct ldb_key_data *kd = parent->backend_data;
331 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
333 msg = reg_ldb_pack_value(ctx, mem_ctx, name, type, data, len);
335 msg->dn = talloc_asprintf(mem_ctx, "value=%s,%s", name, kd->dn);
337 ret = ldb_add(ctx, msg);
339 ret = ldb_modify(ctx, msg);
341 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(parent->hive->backend_data)));
342 talloc_destroy(mem_ctx);
347 talloc_destroy(mem_ctx);
351 static struct hive_operations reg_backend_ldb = {
353 .add_key = ldb_add_key,
354 .del_key = ldb_del_key,
355 .open_hive = ldb_open_hive,
356 .open_key = ldb_open_key,
357 .get_value_by_index = ldb_get_value_by_id,
358 .get_subkey_by_index = ldb_get_subkey_by_id,
359 .set_value = ldb_set_value,
360 .del_value = ldb_del_value,
363 NTSTATUS registry_ldb_init(void)
365 return registry_register(®_backend_ldb);