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 static char *reg_path_to_ldb(TALLOC_CTX *mem_ctx, const char *path, const char *add)
25 char *ret = talloc_strdup(mem_ctx, "");
26 char *mypath = strdup(path);
27 char *end = mypath, *begin;
30 ret = talloc_asprintf_append(ret, "%s", add);
34 begin = strrchr(end, '\\');
36 if(begin) keyname = begin + 1;
37 else keyname = mypath;
40 ret = talloc_asprintf_append(ret, "key=%s,", keyname);
52 ret[strlen(ret)-1] = '\0';
54 if(strlen(ret) == 0) return NULL;
60 static int ldb_close_registry(void *data)
62 ldb_close((struct ldb_context *)data);
66 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, struct registry_key *p, const char *name, uint32_t access_mask, SEC_DESC *sec, struct registry_key **new)
68 return WERR_NOT_SUPPORTED;
71 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx, struct registry_key *k, int idx, struct registry_key **subkey)
73 struct ldb_context *c = k->hive->backend_data;
75 struct ldb_message **msg;
76 struct ldb_message_element *el;
78 ret = ldb_search(c, (char *)k->backend_data, LDB_SCOPE_ONELEVEL, "(key=*)", NULL,&msg);
81 DEBUG(0, ("Error getting subkeys for '%s': %s\n", (char *)k->backend_data, ldb_errstring(c)));
85 if(idx >= ret) return WERR_NO_MORE_ITEMS;
87 el = ldb_msg_find_element(msg[idx], "key");
89 *subkey = talloc_p(mem_ctx, struct registry_key);
90 (*subkey)->name = talloc_strdup(mem_ctx, el->values[0].data);
91 (*subkey)->backend_data = talloc_strdup(mem_ctx, msg[idx]->dn);
93 ldb_search_free(c, msg);
98 static WERROR ldb_fetch_values(struct registry_key *k, int *count, REG_VAL ***values)
100 struct ldb_context *c = k->hive->backend_data;
102 struct ldb_message **msg;
104 ret = ldb_search(c, (char *)k->backend_data, LDB_SCOPE_ONELEVEL, "(value=*)", NULL,&msg);
107 DEBUG(0, ("Error getting values for '%s': %s\n", (char *)k->backend_data, ldb_errstring(c)));
111 *values = talloc_array_p(k->mem_ctx, REG_VAL *, ret);
113 for(i = 0; i < ret; i++) {
114 struct ldb_message_element *el;
116 el = ldb_msg_find_element(msg[i], "key");
118 name = el->values[0].data;
120 /* Dirty hack to circumvent ldb_tdb bug */
121 if(k->backend_data && !strcmp(msg[i]->dn, (char *)k->backend_data)) continue;
123 (*values)[j] = reg_val_new(k, NULL);
124 (*values)[j]->backend_data = talloc_strdup((*values)[j]->mem_ctx, msg[i]->dn);
129 ldb_search_free(c, msg);
135 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, struct registry_hive *h, const char *name, struct registry_key **key)
137 struct ldb_context *c = h->backend_data;
138 struct ldb_message **msg;
141 ldap_path = reg_path_to_ldb(mem_ctx, name, NULL);
143 ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL,&msg);
146 return WERR_NO_MORE_ITEMS;
148 DEBUG(0, ("Error opening key '%s': %s\n", ldap_path, ldb_errstring(c)));
152 *key = talloc_p(mem_ctx, struct registry_key);
153 (*key)->name = talloc_strdup(mem_ctx, strrchr(name, '\\'));
154 (*key)->backend_data = talloc_strdup(mem_ctx, msg[0]->dn);
156 ldb_search_free(c, msg);
161 static WERROR ldb_open_hive(TALLOC_CTX *mem_ctx, struct registry_hive *hive, struct registry_key **k)
163 struct ldb_context *c;
165 if (!hive->location) return WERR_INVALID_PARAM;
166 c = ldb_connect(hive->location, 0, NULL);
168 if(!c) return WERR_FOOBAR;
169 ldb_set_debug_stderr(c);
170 hive->backend_data = c;
172 return ldb_open_key(mem_ctx, hive, "", k);
175 static struct registry_operations reg_backend_ldb = {
177 .open_hive = ldb_open_hive,
178 .open_key = ldb_open_key,
180 .fetch_values = ldb_fetch_values,*/
181 .get_subkey_by_index = ldb_get_subkey_by_id,
182 .add_key = ldb_add_key,
185 NTSTATUS registry_ldb_init(void)
187 return register_backend("registry", ®_backend_ldb);