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.
22 #include "lib/registry/common/registry.h"
24 static char *reg_path_to_ldb(TALLOC_CTX *mem_ctx, const char *path, const char *add)
26 char *ret = talloc_strdup(mem_ctx, "");
27 char *mypath = strdup(path);
28 char *end = mypath, *begin;
31 ret = talloc_asprintf_append(mem_ctx, ret, "%s", add);
35 begin = strrchr(end, '\\');
37 if(begin) keyname = begin + 1;
38 else keyname = mypath;
41 ret = talloc_asprintf_append(mem_ctx, ret, "key=%s,", keyname);
53 ret[strlen(ret)-1] = '\0';
59 * Saves the dn as private_data for every key/val
62 static WERROR ldb_open_registry(REG_HANDLE *handle, const char *location, const char *credentials)
64 struct ldb_context *c;
66 if (!location) return WERR_INVALID_PARAM;
67 c = ldb_connect(location, 0, NULL);
69 ldb_set_debug_stderr(c);
71 if(!c) return WERR_FOOBAR;
73 handle->backend_data = c;
78 static WERROR ldb_close_registry(REG_HANDLE *h)
80 ldb_close((struct ldb_context *)h->backend_data);
84 static WERROR ldb_add_key(REG_KEY *p, const char *name, uint32_t access_mask, SEC_DESC *sec, REG_KEY **new)
86 return WERR_NOT_SUPPORTED;
89 static WERROR ldb_fetch_subkeys(REG_KEY *k, int *count, REG_KEY ***subkeys)
91 struct ldb_context *c = k->handle->backend_data;
94 struct ldb_message **msg;
97 ret = ldb_search(c, (char *)k->backend_data, LDB_SCOPE_ONELEVEL, "(key=*)", NULL,&msg);
100 DEBUG(0, ("Error getting subkeys for '%s': %s\n", k->backend_data, ldb_errstring(c)));
104 *subkeys = talloc_array_p(k->mem_ctx, REG_KEY *, ret);
106 for(i = 0; i < ret; i++) {
107 struct ldb_message_element *el;
109 el = ldb_msg_find_element(msg[i], "key");
111 name = el->values[0].data;
113 /* Dirty hack to circumvent ldb_tdb bug */
114 if(k->backend_data && !strcmp(msg[i]->dn, (char *)k->backend_data)) continue;
116 (*subkeys)[j] = reg_key_new_rel(name, k, NULL);
117 (*subkeys)[j]->backend_data = talloc_strdup((*subkeys)[j]->mem_ctx, msg[i]->dn);
122 ldb_search_free(c, msg);
126 static WERROR ldb_fetch_values(REG_KEY *k, int *count, REG_VAL ***values)
128 struct ldb_context *c = k->handle->backend_data;
131 struct ldb_message **msg;
134 ret = ldb_search(c, (char *)k->backend_data, LDB_SCOPE_ONELEVEL, "(value=*)", NULL,&msg);
137 DEBUG(0, ("Error getting values for '%s': %s\n", k->backend_data, ldb_errstring(c)));
141 *values = talloc_array_p(k->mem_ctx, REG_VAL *, ret);
143 for(i = 0; i < ret; i++) {
144 struct ldb_message_element *el;
146 el = ldb_msg_find_element(msg[i], "key");
148 name = el->values[0].data;
150 /* Dirty hack to circumvent ldb_tdb bug */
151 if(k->backend_data && !strcmp(msg[i]->dn, (char *)k->backend_data)) continue;
153 (*values)[j] = reg_val_new(k, NULL);
154 (*values)[j]->backend_data = talloc_strdup((*values)[j]->mem_ctx, msg[i]->dn);
159 ldb_search_free(c, msg);
163 static WERROR ldb_get_hive(REG_HANDLE *h, int num, REG_KEY **key)
165 if(num != 0) return WERR_NO_MORE_ITEMS;
166 *key = reg_key_new_abs("", h, NULL);
170 static WERROR ldb_open_key(REG_HANDLE *h, int num, const char *name, REG_KEY **key)
172 struct ldb_context *c = h->backend_data;
174 struct ldb_message **msg;
175 char *ldap_path, *new_ldap_path;
177 TALLOC_CTX *mem_ctx = talloc_init("ldb_path");
178 if(num != 0) return WERR_NO_MORE_ITEMS;
179 ldap_path = reg_path_to_ldb(mem_ctx, name, NULL);
181 ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "*", NULL,&msg);
184 return WERR_NO_MORE_ITEMS;
186 DEBUG(0, ("Error opening key '%s': %s\n", ldap_path, ldb_errstring(c)));
190 *key = reg_key_new_abs(name, h, ldap_path);
191 talloc_steal((*key)->mem_ctx, ldap_path);
192 printf("Got something!\n");
195 ldb_search_free(c, msg);
196 talloc_destroy(mem_ctx);
201 static struct registry_ops reg_backend_ldb = {
203 .open_registry = ldb_open_registry,
204 .get_hive = ldb_get_hive,
205 .close_registry = ldb_close_registry,
206 .open_key = ldb_open_key,
207 .fetch_subkeys = ldb_fetch_subkeys,
208 .fetch_values = ldb_fetch_values,
209 .add_key = ldb_add_key,
212 NTSTATUS registry_ldb_init(void)
214 return register_backend("registry", ®_backend_ldb);