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';
55 printf("Doing search for : %s\n", ret);
60 * Saves the dn as private_data for every key/val
63 static WERROR ldb_open_registry(REG_HANDLE *handle, const char *location, const char *credentials)
65 struct ldb_context *c;
67 if (!location) return WERR_INVALID_PARAM;
68 c = ldb_connect(location, 0, NULL);
70 ldb_set_debug_stderr(c);
72 if(!c) return WERR_FOOBAR;
74 handle->backend_data = c;
79 static WERROR ldb_close_registry(REG_HANDLE *h)
81 ldb_close((struct ldb_context *)h->backend_data);
85 static WERROR ldb_fetch_subkeys(REG_KEY *k, int *count, REG_KEY ***subkeys)
87 struct ldb_context *c = k->handle->backend_data;
90 struct ldb_message **msg;
91 TALLOC_CTX *mem_ctx = talloc_init("ldb_path");
94 ret = ldb_search(c, (char *)k->backend_data, LDB_SCOPE_ONELEVEL, "(key=*)", NULL,&msg);
97 DEBUG(0, ("Error getting subkeys for '%s': %s\n", k->backend_data, ldb_errstring(c)));
101 *subkeys = talloc_array_p(k->mem_ctx, REG_KEY *, ret);
103 for(i = 0; i < ret; i++) {
104 struct ldb_message_element *el;
106 el = ldb_msg_find_element(msg[i], "key");
108 name = el->values[0].data;
110 /* Dirty hack to circumvent ldb_tdb bug */
111 if(k->backend_data && !strcmp(msg[i]->dn, (char *)k->backend_data)) continue;
113 (*subkeys)[j] = reg_key_new_rel(name, k, NULL);
114 (*subkeys)[j]->backend_data = talloc_strdup((*subkeys)[j]->mem_ctx, msg[i]->dn);
119 ldb_search_free(c, msg);
120 talloc_destroy(mem_ctx);
124 static WERROR ldb_get_hive(REG_HANDLE *h, int num, REG_KEY **key)
126 if(num != 0) return WERR_NO_MORE_ITEMS;
127 *key = reg_key_new_abs("", h, NULL);
128 // (*key)->backend_data = talloc_strdup((*key)->mem_ctx, "");
132 static WERROR ldb_open_key(REG_HANDLE *h, int num, const char *name, REG_KEY **key)
134 struct ldb_context *c = h->backend_data;
136 struct ldb_message **msg;
137 char *ldap_path, *new_ldap_path;
139 TALLOC_CTX *mem_ctx = talloc_init("ldb_path");
140 if(num != 0) return WERR_NO_MORE_ITEMS;
141 ldap_path = reg_path_to_ldb(mem_ctx, name, NULL);
143 ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "*", NULL,&msg);
146 return WERR_NO_MORE_ITEMS;
148 DEBUG(0, ("Error opening key '%s': %s\n", ldap_path, ldb_errstring(c)));
152 *key = reg_key_new_abs(name, h, ldap_path);
153 talloc_steal(mem_ctx, (*key)->mem_ctx, ldap_path);
154 printf("Got something!\n");
157 ldb_search_free(c, msg);
158 talloc_destroy(mem_ctx);
163 static struct registry_ops reg_backend_ldb = {
165 .open_registry = ldb_open_registry,
166 .get_hive = ldb_get_hive,
167 .close_registry = ldb_close_registry,
168 .open_key = ldb_open_key,
169 .fetch_subkeys = ldb_fetch_subkeys,
172 NTSTATUS registry_ldb_init(void)
174 return register_backend("registry", ®_backend_ldb);