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"
25 static char *reg_path_to_ldb(TALLOC_CTX *mem_ctx, const char *path, const char *add)
27 char *ret = talloc_strdup(mem_ctx, "");
28 char *mypath = strdup(path);
29 char *end = mypath, *begin;
32 ret = talloc_asprintf_append(ret, "%s", add);
36 begin = strrchr(end, '\\');
38 if(begin) keyname = begin + 1;
39 else keyname = mypath;
42 ret = talloc_asprintf_append(ret, "key=%s,", keyname);
54 ret[strlen(ret)-1] = '\0';
56 if(strlen(ret) == 0) return NULL;
62 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx, struct registry_key *k, int idx, struct registry_key **subkey)
64 struct ldb_context *c = k->hive->backend_data;
66 struct ldb_message **msg;
67 struct ldb_message_element *el;
69 ret = ldb_search(c, (char *)k->backend_data, LDB_SCOPE_ONELEVEL, "(key=*)", NULL,&msg);
72 DEBUG(0, ("Error getting subkeys for '%s': %s\n", (char *)k->backend_data, ldb_errstring(c)));
76 if(idx >= ret) return WERR_NO_MORE_ITEMS;
78 el = ldb_msg_find_element(msg[idx], "key");
80 *subkey = talloc_p(mem_ctx, struct registry_key);
81 (*subkey)->name = talloc_strdup(mem_ctx, el->values[0].data);
82 (*subkey)->backend_data = talloc_strdup(mem_ctx, msg[idx]->dn);
84 ldb_search_free(c, msg);
88 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct registry_key *k, int idx, struct registry_value **value)
90 struct ldb_context *c = k->hive->backend_data;
92 struct ldb_message **msg;
93 struct ldb_message_element *el;
95 ret = ldb_search(c, (char *)k->backend_data, LDB_SCOPE_ONELEVEL, "(value=*)", NULL,&msg);
98 DEBUG(0, ("Error getting values for '%s': %s\n", (char *)k->backend_data, ldb_errstring(c)));
102 if(idx >= ret) return WERR_NO_MORE_ITEMS;
104 el = ldb_msg_find_element(msg[idx], "value");
106 *value = talloc_p(mem_ctx, struct registry_value);
107 (*value)->name = talloc_strdup(mem_ctx, el->values[0].data);
108 (*value)->backend_data = talloc_strdup(mem_ctx, msg[idx]->dn);
110 ldb_search_free(c, msg);
114 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, struct registry_hive *h, const char *name, struct registry_key **key)
116 struct ldb_context *c = h->backend_data;
117 struct ldb_message **msg;
120 ldap_path = reg_path_to_ldb(mem_ctx, name, NULL);
122 ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL,&msg);
125 return WERR_NO_MORE_ITEMS;
127 DEBUG(0, ("Error opening key '%s': %s\n", ldap_path, ldb_errstring(c)));
131 *key = talloc_p(mem_ctx, struct registry_key);
132 (*key)->name = talloc_strdup(mem_ctx, strrchr(name, '\\'));
133 (*key)->backend_data = talloc_strdup(mem_ctx, msg[0]->dn);
135 ldb_search_free(c, msg);
140 static WERROR ldb_open_hive(TALLOC_CTX *mem_ctx, struct registry_hive *hive, struct registry_key **k)
142 struct ldb_context *c;
144 if (!hive->location) return WERR_INVALID_PARAM;
145 c = ldb_connect(hive->location, 0, NULL);
148 DEBUG(1, ("ldb_open_hive: %s\n", ldb_errstring(hive->backend_data)));
151 ldb_set_debug_stderr(c);
152 hive->backend_data = c;
154 hive->root = talloc_zero_p(mem_ctx, struct registry_key);
155 hive->root->name = talloc_strdup(mem_ctx, "");
160 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)
162 struct ldb_context *ctx = parent->hive->backend_data;
163 struct ldb_message msg;
168 msg.dn = reg_path_to_ldb(mem_ctx, parent->path, talloc_asprintf(mem_ctx, "key=%s,", name));
170 ldb_msg_add_string(ctx, &msg, "key", talloc_strdup(mem_ctx, name));
172 ret = ldb_add(ctx, &msg);
174 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(parent->hive->backend_data)));
178 *newkey = talloc_zero_p(mem_ctx, struct registry_key);
179 (*newkey)->backend_data = msg.dn;
180 (*newkey)->name = talloc_strdup(mem_ctx, name);
185 static WERROR ldb_del_key (struct registry_key *key)
189 ret = ldb_delete(key->hive->backend_data, key->backend_data);
192 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(key->hive->backend_data)));
199 static WERROR ldb_close_hive (struct registry_hive *hive)
201 ldb_close (hive->backend_data);
205 static struct registry_operations reg_backend_ldb = {
207 .add_key = ldb_add_key,
208 .del_key = ldb_del_key,
209 .open_hive = ldb_open_hive,
210 .close_hive = ldb_close_hive,
211 .open_key = ldb_open_key,
212 .get_value_by_index = ldb_get_value_by_id,
213 .get_subkey_by_index = ldb_get_subkey_by_id,
216 NTSTATUS registry_ldb_init(void)
218 return registry_register(®_backend_ldb);