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);
41 static int reg_close_ldb_key (void *data)
43 struct registry_key *key = data;
44 struct ldb_key_data *kd = key->backend_data;
45 struct ldb_context *c = key->hive->backend_data;
48 ldb_search_free(c, kd->subkeys);
53 ldb_search_free(c, kd->values);
59 static char *reg_path_to_ldb(TALLOC_CTX *mem_ctx, struct registry_key *from, const char *path, const char *add)
61 char *ret = talloc_strdup(mem_ctx, "");
62 char *mypath = talloc_strdup(mem_ctx, path);
64 struct ldb_key_data *kd = from->backend_data;
67 ret = talloc_asprintf_append(ret, "%s", add);
71 begin = strrchr(mypath, '\\');
73 if(begin) keyname = begin + 1;
74 else keyname = mypath;
77 ret = talloc_asprintf_append(ret, "key=%s,", keyname);
86 ret = talloc_asprintf_append(ret, "%s", kd->dn);
92 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx, struct registry_key *k, int idx, struct registry_key **subkey)
94 struct ldb_context *c = k->hive->backend_data;
95 struct ldb_message_element *el;
96 struct ldb_key_data *kd = k->backend_data, *newkd;
98 /* Do a search if necessary */
99 if (kd->subkeys == NULL) {
100 kd->subkey_count = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL, &kd->subkeys);
102 if(kd->subkey_count < 0) {
103 DEBUG(0, ("Error getting subkeys for '%s': %s\n", kd->dn, ldb_errstring(c)));
108 if (idx >= kd->subkey_count) return WERR_NO_MORE_ITEMS;
110 el = ldb_msg_find_element(kd->subkeys[idx], "key");
112 *subkey = talloc_p(mem_ctx, struct registry_key);
113 talloc_set_destructor(*subkey, reg_close_ldb_key);
114 (*subkey)->name = talloc_strdup(mem_ctx, el->values[0].data);
115 (*subkey)->backend_data = newkd = talloc_zero_p(*subkey, struct ldb_key_data);
116 newkd->dn = talloc_strdup(mem_ctx, kd->subkeys[idx]->dn);
121 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct registry_key *k, int idx, struct registry_value **value)
123 struct ldb_context *c = k->hive->backend_data;
124 struct ldb_message_element *el;
125 struct ldb_key_data *kd = k->backend_data;
126 const struct ldb_val *val;
128 /* Do the search if necessary */
129 if (kd->values == NULL) {
130 kd->value_count = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(value=*)", NULL,&kd->values);
132 if(kd->value_count < 0) {
133 DEBUG(0, ("Error getting values for '%s': %s\n", kd->dn, ldb_errstring(c)));
138 if(idx >= kd->value_count) return WERR_NO_MORE_ITEMS;
140 el = ldb_msg_find_element(kd->values[idx], "value");
142 *value = talloc_p(mem_ctx, struct registry_value);
143 (*value)->name = talloc_strdup(mem_ctx, el->values[0].data);
144 (*value)->data_type = ldb_msg_find_uint(kd->values[idx], "type", 0);
145 val = ldb_msg_find_ldb_val(kd->values[idx], "data");
146 (*value)->data_blk = talloc_memdup(mem_ctx, val->data, val->length);
147 (*value)->data_len = val->length;
152 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h, const char *name, struct registry_key **key)
154 struct ldb_context *c = h->hive->backend_data;
155 struct ldb_message **msg;
158 struct ldb_key_data *newkd;
160 ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
162 ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL,&msg);
165 return WERR_NO_MORE_ITEMS;
167 DEBUG(0, ("Error opening key '%s': %s\n", ldap_path, ldb_errstring(c)));
171 *key = talloc_p(mem_ctx, struct registry_key);
172 talloc_set_destructor(*key, reg_close_ldb_key);
173 (*key)->name = talloc_strdup(mem_ctx, strrchr(name, '\\')?strchr(name, '\\'):name);
174 (*key)->backend_data = newkd = talloc_zero_p(*key, struct ldb_key_data);
175 newkd->dn = talloc_strdup(mem_ctx, msg[0]->dn);
177 ldb_search_free(c, msg);
182 static WERROR ldb_open_hive(struct registry_hive *hive, struct registry_key **k)
184 struct ldb_context *c;
185 struct ldb_key_data *kd;
186 struct ldb_wrap *wrap;
188 if (!hive->location) return WERR_INVALID_PARAM;
189 wrap = ldb_wrap_connect(hive, hive->location, 0, NULL);
194 DEBUG(1, ("ldb_open_hive: %s\n", ldb_errstring(hive->backend_data)));
197 ldb_set_debug_stderr(c);
198 hive->backend_data = c;
200 *k = talloc_zero_p(hive, struct registry_key);
201 talloc_set_destructor (*k, reg_close_ldb_key);
202 talloc_set_destructor (hive, ldb_close_hive);
203 (*k)->name = talloc_strdup(*k, "");
204 (*k)->backend_data = kd = talloc_zero_p(*k, struct ldb_key_data);
205 kd->dn = talloc_strdup(*k, "hive=");
211 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)
213 struct ldb_context *ctx = parent->hive->backend_data;
214 struct ldb_message msg;
215 struct ldb_key_data *newkd;
220 msg.dn = reg_path_to_ldb(mem_ctx, parent, name, NULL);
222 ldb_msg_add_string(ctx, &msg, "key", talloc_strdup(mem_ctx, name));
224 ret = ldb_add(ctx, &msg);
226 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(parent->hive->backend_data)));
230 *newkey = talloc_zero_p(mem_ctx, struct registry_key);
231 (*newkey)->name = talloc_strdup(mem_ctx, name);
233 (*newkey)->backend_data = newkd = talloc_zero_p(*newkey, struct ldb_key_data);
239 static WERROR ldb_del_key (struct registry_key *key, const char *child)
242 struct ldb_key_data *kd = key->backend_data;
243 char *childdn = talloc_asprintf(NULL, "key=%s,%s", child, kd->dn);
245 ret = ldb_delete(key->hive->backend_data, childdn);
247 talloc_destroy(childdn);
250 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(key->hive->backend_data)));
257 static WERROR ldb_del_value (struct registry_key *key, const char *child)
260 struct ldb_key_data *kd = key->backend_data;
261 char *childdn = talloc_asprintf(NULL, "value=%s,%s", child, kd->dn);
263 ret = ldb_delete(key->hive->backend_data, childdn);
265 talloc_destroy(childdn);
268 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(key->hive->backend_data)));
275 static WERROR ldb_set_value (struct registry_key *parent, const char *name, uint32 type, void *data, int len)
277 struct ldb_context *ctx = parent->hive->backend_data;
278 struct ldb_message msg;
280 struct ldb_key_data *kd = parent->backend_data;
283 TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
287 msg.dn = talloc_asprintf(mem_ctx, "value=%s,%s", name, kd->dn);
289 ldb_msg_add_string(ctx, &msg, "value", talloc_strdup(mem_ctx, name));
292 ldb_msg_add_value(ctx, &msg, "data", &val);
294 type_s = talloc_asprintf(mem_ctx, "%u", type);
295 ldb_msg_add_string(ctx, &msg, "type", type_s);
297 ret = ldb_add(ctx, &msg);
299 ret = ldb_modify(ctx, &msg);
301 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(parent->hive->backend_data)));
302 talloc_destroy(mem_ctx);
307 talloc_destroy(mem_ctx);
311 static struct hive_operations reg_backend_ldb = {
313 .add_key = ldb_add_key,
314 .del_key = ldb_del_key,
315 .open_hive = ldb_open_hive,
316 .open_key = ldb_open_key,
317 .get_value_by_index = ldb_get_value_by_id,
318 .get_subkey_by_index = ldb_get_subkey_by_id,
319 .set_value = ldb_set_value,
320 .del_value = ldb_del_value,
323 NTSTATUS registry_ldb_init(void)
325 return registry_register(®_backend_ldb);