]> git.samba.org - kamenim/samba.git/blob - source4/lib/registry/reg_backend_ldb.c
r19831: Big ldb_dn optimization and interfaces enhancement patch
[kamenim/samba.git] / source4 / lib / registry / reg_backend_ldb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Registry interface
4    Copyright (C) Jelmer Vernooij  2004.
5    
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.
10    
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.
15    
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.
19 */
20
21 #include "includes.h"
22 #include "registry.h"
23 #include "lib/ldb/include/ldb.h"
24 #include "lib/ldb/include/ldb_errors.h"
25 #include "db_wrap.h"
26 #include "librpc/gen_ndr/winreg.h"
27
28 struct ldb_key_data 
29 {
30         struct ldb_dn *dn;
31         struct ldb_message **subkeys, **values;
32         int subkey_count, value_count;
33 };
34
35 static int ldb_free_hive (struct registry_hive *hive)
36 {
37         talloc_free(hive->backend_data);
38         hive->backend_data = NULL;
39         return 0;
40 }
41
42 static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx, struct ldb_message *msg, const char **name, uint32_t *type, DATA_BLOB *data)
43 {
44         const struct ldb_val *val;
45         *name = talloc_strdup(mem_ctx, ldb_msg_find_attr_as_string(msg, "value", NULL));
46         *type = ldb_msg_find_attr_as_uint(msg, "type", 0);
47         val = ldb_msg_find_ldb_val(msg, "data");
48
49         switch (*type)
50         {
51         case REG_SZ:
52         case REG_EXPAND_SZ:
53                 data->length = convert_string_talloc(mem_ctx, CH_UTF8, CH_UTF16, val->data, val->length, (void **)&data->data);
54                 break;
55
56         case REG_DWORD: {
57                 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
58                 *data = data_blob_talloc(mem_ctx, &tmp, 4);
59                 }
60                 break;
61
62         default:
63                 *data = data_blob_talloc(mem_ctx, val->data, val->length);
64                 break;
65         }
66 }
67
68 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx, TALLOC_CTX *mem_ctx, const char *name, uint32_t type, DATA_BLOB data)
69 {
70         struct ldb_val val;
71         struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
72         char *type_s;
73
74         ldb_msg_add_string(msg, "value", talloc_strdup(mem_ctx, name));
75
76         switch (type) {
77         case REG_SZ:
78         case REG_EXPAND_SZ:
79                 val.length = convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8, (void *)data.data, data.length, (void **)&val.data);
80                 ldb_msg_add_value(msg, "data", &val, NULL);
81                 break;
82
83         case REG_DWORD:
84                 ldb_msg_add_string(msg, "data", talloc_asprintf(mem_ctx, "0x%x", IVAL(data.data, 0)));
85                 break;
86         default:
87                 ldb_msg_add_value(msg, "data", &data, NULL);
88         }
89
90
91         type_s = talloc_asprintf(mem_ctx, "%u", type);
92         ldb_msg_add_string(msg, "type", type_s); 
93
94         return msg;
95 }
96
97
98 static int reg_close_ldb_key(struct registry_key *key)
99 {
100         struct ldb_key_data *kd = talloc_get_type(key->backend_data, struct ldb_key_data);
101 /*      struct ldb_context *c = key->hive->backend_data; */
102
103         if (kd->subkeys) {
104                 talloc_free(kd->subkeys); 
105                 kd->subkeys = NULL;
106         }
107
108         if (kd->values) {
109                 talloc_free(kd->values); 
110                 kd->values = NULL;
111         }
112         return 0;
113 }
114
115 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx, const struct registry_key *from, const char *path, const char *add)
116 {
117         TALLOC_CTX *local_ctx;
118         struct ldb_dn *ret;
119         char *mypath = talloc_strdup(mem_ctx, path);
120         char *begin;
121         struct ldb_key_data *kd = talloc_get_type(from->backend_data, struct ldb_key_data);
122         struct ldb_context *ldb = talloc_get_type(from->hive->backend_data, struct ldb_context);
123
124         local_ctx = talloc_new(mem_ctx);
125
126         if (add) {
127                 ret = ldb_dn_new(mem_ctx, ldb, add);
128         } else {
129                 ret = ldb_dn_new(mem_ctx, ldb, NULL);
130         }
131         if ( ! ldb_dn_validate(ret)) {
132                 talloc_free(ret);
133                 talloc_free(local_ctx);
134                 return NULL;
135         }
136
137         while(mypath) {
138                 char *keyname;
139
140                 begin = strrchr(mypath, '\\');
141
142                 if (begin) keyname = begin + 1;
143                 else keyname = mypath;
144
145                 if(strlen(keyname)) {
146                         ldb_dn_add_base_fmt(ret, "key=%s", keyname);
147                 }
148
149                 if(begin) {
150                         *begin = '\0';
151                 } else {
152                         break;
153                 }
154         }
155
156         ldb_dn_add_base(ret, kd->dn);
157
158         talloc_free(local_ctx);
159
160         return ret;
161 }
162
163
164 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx, const struct registry_key *k, int idx, struct registry_key **subkey)
165 {
166         struct ldb_context *c = talloc_get_type(k->hive->backend_data, struct ldb_context);
167         struct ldb_message_element *el;
168         struct ldb_key_data *kd = talloc_get_type(k->backend_data, struct ldb_key_data);
169         struct ldb_key_data *newkd;
170
171         /* Do a search if necessary */
172         if (kd->subkeys == NULL) {
173                 struct ldb_result *res;
174                 int ret;
175
176                 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL, &res);
177
178                 if (ret != LDB_SUCCESS) {
179                         DEBUG(0, ("Error getting subkeys for '%s': %s\n", ldb_dn_linearize(mem_ctx, kd->dn), ldb_errstring(c)));
180                         return WERR_FOOBAR;
181                 }
182
183                 kd->subkey_count = res->count;
184                 kd->subkeys = talloc_steal(kd, res->msgs);
185                 talloc_free(res);
186         } 
187
188         if (idx >= kd->subkey_count) return WERR_NO_MORE_ITEMS;
189
190         el = ldb_msg_find_element(kd->subkeys[idx], "key");
191         
192         *subkey = talloc(mem_ctx, struct registry_key);
193         talloc_set_destructor(*subkey, reg_close_ldb_key);
194         (*subkey)->name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
195         (*subkey)->backend_data = newkd = talloc_zero(*subkey, struct ldb_key_data);
196         (*subkey)->last_mod = 0; /* TODO: we need to add this to the
197                                     ldb backend properly */
198         newkd->dn = ldb_dn_copy(mem_ctx, kd->subkeys[idx]->dn);
199
200         return WERR_OK;
201 }
202
203 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, const struct registry_key *k, int idx, struct registry_value **value)
204 {
205         struct ldb_context *c = talloc_get_type(k->hive->backend_data, struct ldb_context);
206         struct ldb_key_data *kd = talloc_get_type(k->backend_data, struct ldb_key_data);
207
208         /* Do the search if necessary */
209         if (kd->values == NULL) {
210                 struct ldb_result *res;
211                 int ret;
212
213                 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(value=*)", NULL, &res);
214
215                 if (ret != LDB_SUCCESS) {
216                         DEBUG(0, ("Error getting values for '%s': %s\n", ldb_dn_linearize(mem_ctx, kd->dn), ldb_errstring(c)));
217                         return WERR_FOOBAR;
218                 }
219                 kd->value_count = res->count;
220                 kd->values = talloc_steal(kd, res->msgs);
221                 talloc_free(res);
222         }
223
224         if(idx >= kd->value_count) return WERR_NO_MORE_ITEMS;
225
226         *value = talloc(mem_ctx, struct registry_value);
227
228         reg_ldb_unpack_value(mem_ctx, kd->values[idx], &(*value)->name, &(*value)->data_type, &(*value)->data);
229
230         return WERR_OK;
231 }
232
233 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct registry_key *h, const char *name, struct registry_key **key)
234 {
235         struct ldb_context *c = talloc_get_type(h->hive->backend_data, struct ldb_context);
236         struct ldb_result *res;
237         struct ldb_dn *ldap_path;
238         int ret;
239         struct ldb_key_data *newkd;
240
241         ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
242
243         ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL, &res);
244
245         if (ret != LDB_SUCCESS) {
246                 DEBUG(0, ("Error opening key '%s': %s\n", ldb_dn_linearize(ldap_path, ldap_path), ldb_errstring(c)));
247                 return WERR_FOOBAR;
248         } else if (res->count == 0) {
249                 talloc_free(res);
250                 return WERR_BADFILE;
251         }
252
253         *key = talloc(mem_ctx, struct registry_key);
254         talloc_set_destructor(*key, reg_close_ldb_key);
255         (*key)->name = talloc_strdup(mem_ctx, strrchr(name, '\\')?strchr(name, '\\'):name);
256         (*key)->backend_data = newkd = talloc_zero(*key, struct ldb_key_data);
257         newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn); 
258
259         talloc_free(res);
260
261         return WERR_OK;
262 }
263
264 static WERROR ldb_open_hive(struct registry_hive *hive, struct registry_key **k)
265 {
266         struct ldb_key_data *kd;
267         struct ldb_context *wrap;
268
269         if (!hive->location) return WERR_INVALID_PARAM;
270
271         wrap = ldb_wrap_connect(hive, hive->location, hive->session_info, hive->credentials, 0, NULL);
272
273         if(!wrap) {
274                 DEBUG(1, ("ldb_open_hive: unable to connect\n"));
275                 return WERR_FOOBAR;
276         }
277
278         ldb_set_debug_stderr(wrap);
279         hive->backend_data = wrap;
280
281         *k = talloc_zero(hive, struct registry_key);
282         talloc_set_destructor (*k, reg_close_ldb_key);
283         talloc_set_destructor (hive, ldb_free_hive);
284         (*k)->name = talloc_strdup(*k, "");
285         (*k)->backend_data = kd = talloc_zero(*k, struct ldb_key_data);
286         kd->dn = ldb_dn_new(*k, wrap, "hive=NONE");
287         
288
289         return WERR_OK;
290 }
291
292 static WERROR ldb_add_key (TALLOC_CTX *mem_ctx, const struct registry_key *parent, const char *name, uint32_t access_mask, struct security_descriptor *sd, struct registry_key **newkey)
293 {
294         struct ldb_context *ctx = talloc_get_type(parent->hive->backend_data, struct ldb_context);
295         struct ldb_message *msg;
296         struct ldb_key_data *newkd;
297         int ret;
298
299         msg = ldb_msg_new(mem_ctx);
300
301         msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
302
303         ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
304
305         ret = ldb_add(ctx, msg);
306         if (ret < 0) {
307                 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(ctx)));
308                 return WERR_FOOBAR;
309         }
310
311         *newkey = talloc_zero(mem_ctx, struct registry_key);
312         (*newkey)->name = talloc_strdup(mem_ctx, name);
313
314         (*newkey)->backend_data = newkd = talloc_zero(*newkey, struct ldb_key_data);
315         newkd->dn = talloc_steal(newkd, msg->dn);
316
317         return WERR_OK;
318 }
319
320 static WERROR ldb_del_key (const struct registry_key *key, const char *child)
321 {
322         struct ldb_context *ctx = talloc_get_type(key->hive->backend_data, struct ldb_context);
323         int ret;
324         struct ldb_key_data *kd = talloc_get_type(key->backend_data, struct ldb_key_data);
325         struct ldb_dn *childdn;
326
327         childdn = ldb_dn_copy(ctx, kd->dn);
328         ldb_dn_add_child_fmt(childdn, "key=%s", child);
329
330         ret = ldb_delete(ctx, childdn);
331
332         talloc_free(childdn);
333
334         if (ret < 0) {
335                 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(ctx)));
336                 return WERR_FOOBAR;
337         }
338
339         return WERR_OK;
340 }
341
342 static WERROR ldb_del_value (const struct registry_key *key, const char *child)
343 {
344         int ret;
345         struct ldb_context *ctx = talloc_get_type(key->hive->backend_data, struct ldb_context);
346         struct ldb_key_data *kd = talloc_get_type(key->backend_data, struct ldb_key_data);
347         struct ldb_dn *childdn;
348
349         childdn = ldb_dn_copy(ctx, kd->dn);
350         ldb_dn_add_child_fmt(childdn, "value=%s", child);
351
352         ret = ldb_delete(ctx, childdn);
353
354         talloc_free(childdn);
355
356         if (ret < 0) {
357                 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(ctx)));
358                 return WERR_FOOBAR;
359         }
360
361         return WERR_OK;
362 }
363
364 static WERROR ldb_set_value (const struct registry_key *parent, const char *name, uint32_t type, DATA_BLOB data)
365 {
366         struct ldb_context *ctx = talloc_get_type(parent->hive->backend_data, struct ldb_context);
367         struct ldb_message *msg;
368         struct ldb_key_data *kd = talloc_get_type(parent->backend_data, struct ldb_key_data);
369         int ret;
370         TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
371
372         msg = reg_ldb_pack_value(ctx, mem_ctx, name, type, data);
373
374         msg->dn = ldb_dn_copy(msg, kd->dn);
375         ldb_dn_add_child_fmt(msg->dn, "value=%s", name);
376
377         ret = ldb_add(ctx, msg);
378         if (ret < 0) {
379                 ret = ldb_modify(ctx, msg);
380                 if (ret < 0) {
381                         DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(ctx)));
382                         talloc_free(mem_ctx);
383                         return WERR_FOOBAR;
384                 }
385         }
386         
387         talloc_free(mem_ctx);
388         return WERR_OK;
389 }
390
391 static struct hive_operations reg_backend_ldb = {
392         .name = "ldb",
393         .add_key = ldb_add_key,
394         .del_key = ldb_del_key,
395         .open_hive = ldb_open_hive,
396         .open_key = ldb_open_key,
397         .get_value_by_index = ldb_get_value_by_id,
398         .get_subkey_by_index = ldb_get_subkey_by_id,
399         .set_value = ldb_set_value,
400         .del_value = ldb_del_value,
401 };
402
403 NTSTATUS registry_ldb_init(void)
404 {
405         return registry_register(&reg_backend_ldb);
406 }