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, const char *path, const char *add)
61 char *ret = talloc_strdup(mem_ctx, "");
62 char *mypath = strdup(path);
63 char *end = mypath, *begin;
66 ret = talloc_asprintf_append(ret, "%s", add);
70 begin = strrchr(end, '\\');
72 if(begin) keyname = begin + 1;
73 else keyname = mypath;
76 ret = talloc_asprintf_append(ret, "key=%s,", keyname);
88 ret[strlen(ret)-1] = '\0';
90 if(strlen(ret) == 0) return NULL;
96 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx, struct registry_key *k, int idx, struct registry_key **subkey)
98 struct ldb_context *c = k->hive->backend_data;
99 struct ldb_message_element *el;
100 struct ldb_key_data *kd = k->backend_data, *newkd;
102 /* Do a search if necessary */
103 if (kd->subkeys == NULL) {
104 kd->subkey_count = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL, &kd->subkeys);
106 if(kd->subkey_count < 0) {
107 DEBUG(0, ("Error getting subkeys for '%s': %s\n", kd->dn, ldb_errstring(c)));
112 if (idx >= kd->subkey_count) return WERR_NO_MORE_ITEMS;
114 el = ldb_msg_find_element(kd->subkeys[idx], "key");
116 *subkey = talloc_p(mem_ctx, struct registry_key);
117 talloc_set_destructor(*subkey, reg_close_ldb_key);
118 (*subkey)->name = talloc_strdup(mem_ctx, el->values[0].data);
119 (*subkey)->backend_data = newkd = talloc_zero_p(*subkey, struct ldb_key_data);
120 newkd->dn = talloc_strdup(mem_ctx, kd->subkeys[idx]->dn);
125 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct registry_key *k, int idx, struct registry_value **value)
127 struct ldb_context *c = k->hive->backend_data;
128 struct ldb_message_element *el;
129 struct ldb_key_data *kd = k->backend_data;
131 /* Do the search if necessary */
132 if (kd->values == NULL) {
133 kd->value_count = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(value=*)", NULL,&kd->values);
135 if(kd->value_count < 0) {
136 DEBUG(0, ("Error getting values for '%s': %s\n", kd->dn, ldb_errstring(c)));
141 if(idx >= kd->value_count) return WERR_NO_MORE_ITEMS;
143 el = ldb_msg_find_element(kd->values[idx], "value");
145 *value = talloc_p(mem_ctx, struct registry_value);
146 (*value)->name = talloc_strdup(mem_ctx, el->values[0].data);
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 *kd = h->backend_data, *newkd;
159 ldap_path = talloc_asprintf(mem_ctx, "%s%s%s",
160 reg_path_to_ldb(mem_ctx, name, NULL),
164 ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL,&msg);
167 return WERR_NO_MORE_ITEMS;
169 DEBUG(0, ("Error opening key '%s': %s\n", ldap_path, ldb_errstring(c)));
173 *key = talloc_p(mem_ctx, struct registry_key);
174 talloc_set_destructor(*key, reg_close_ldb_key);
175 (*key)->name = talloc_strdup(mem_ctx, strrchr(name, '\\')?strchr(name, '\\'):name);
176 (*key)->backend_data = newkd = talloc_zero_p(*key, struct ldb_key_data);
177 newkd->dn = talloc_strdup(mem_ctx, msg[0]->dn);
179 ldb_search_free(c, msg);
184 static WERROR ldb_open_hive(struct registry_hive *hive, struct registry_key **k)
186 struct ldb_context *c;
187 struct ldb_key_data *kd;
189 if (!hive->location) return WERR_INVALID_PARAM;
190 c = ldb_connect(hive->location, 0, NULL);
193 DEBUG(1, ("ldb_open_hive: %s\n", ldb_errstring(hive->backend_data)));
196 ldb_set_debug_stderr(c);
197 hive->backend_data = c;
199 *k = talloc_zero_p(hive, struct registry_key);
200 talloc_set_destructor (*k, reg_close_ldb_key);
201 talloc_set_destructor (hive, ldb_close_hive);
202 (*k)->name = talloc_strdup(*k, "");
203 (*k)->backend_data = kd = talloc_zero_p(*k, struct ldb_key_data);
204 kd->dn = talloc_strdup(*k, "key=root");
210 static WERROR ldb_add_key (TALLOC_CTX *mem_ctx, struct registry_key *parent, const char *name, uint32_t access_mask, SEC_DESC *sd, struct registry_key **newkey)
212 struct ldb_context *ctx = parent->hive->backend_data;
213 struct ldb_message msg;
214 struct ldb_key_data *newkd;
219 msg.dn = reg_path_to_ldb(mem_ctx, parent->path, talloc_asprintf(mem_ctx, "key=%s,", name));
221 ldb_msg_add_string(ctx, &msg, "key", talloc_strdup(mem_ctx, name));
223 ret = ldb_add(ctx, &msg);
225 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(parent->hive->backend_data)));
229 *newkey = talloc_zero_p(mem_ctx, struct registry_key);
230 (*newkey)->name = talloc_strdup(mem_ctx, name);
232 (*newkey)->backend_data = newkd = talloc_zero_p(*newkey, struct ldb_key_data);
238 static WERROR ldb_del_key (struct registry_key *key)
241 struct ldb_key_data *kd = key->backend_data;
243 ret = ldb_delete(key->hive->backend_data, kd->dn);
246 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(key->hive->backend_data)));
253 static struct hive_operations reg_backend_ldb = {
255 .add_key = ldb_add_key,
256 .del_key = ldb_del_key,
257 .open_hive = ldb_open_hive,
258 .open_key = ldb_open_key,
259 .get_value_by_index = ldb_get_value_by_id,
260 .get_subkey_by_index = ldb_get_subkey_by_id,
263 NTSTATUS registry_ldb_init(void)
265 return registry_register(®_backend_ldb);