r6470: Remove ldb_search_free() it is not needed anymore.
[ambi/samba-autobuild/.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 "db_wrap.h"
25
26 struct ldb_key_data 
27 {
28         const char *dn;
29         struct ldb_message **subkeys, **values;
30         int subkey_count, value_count;
31 };
32
33 static int ldb_free_hive (void *_hive)
34 {
35         struct registry_hive *hive = _hive;
36         talloc_free(hive->backend_data);
37         return 0;
38 }
39
40 static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx, struct ldb_message *msg, char **name, uint32_t *type, void **data, int *len)
41 {
42         const struct ldb_val *val;
43         *name = talloc_strdup(mem_ctx, ldb_msg_find_string(msg, "value", NULL));
44         *type = ldb_msg_find_uint(msg, "type", 0);
45         val = ldb_msg_find_ldb_val(msg, "data");
46
47         switch (*type)
48         {
49         case REG_SZ:
50         case REG_EXPAND_SZ:
51                 *len = convert_string_talloc(mem_ctx, CH_UTF8, CH_UTF16, val->data, val->length, data);
52                 break;
53
54         case REG_DWORD_LE:
55                 *len = 4;
56                 *data = talloc(mem_ctx, uint32_t);
57                 SIVAL(*data, 0, strtol(val->data, NULL, 0));
58                 break;
59
60         default:
61                 *data = talloc_memdup(mem_ctx, val->data, val->length);
62                 *len = val->length;
63                 break;
64         }
65 }
66
67 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx, TALLOC_CTX *mem_ctx, const char *name, uint32_t type, void *data, int len)
68 {
69         struct ldb_val val;
70         struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
71         char *type_s;
72
73         ldb_msg_add_string(ctx, msg, "value", talloc_strdup(mem_ctx, name));
74
75         switch (type) {
76         case REG_SZ:
77         case REG_EXPAND_SZ:
78                 val.length = convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8, data, len, &val.data);
79                 ldb_msg_add_value(ctx, msg, "data", &val);
80                 break;
81         case REG_DWORD_LE:
82                 ldb_msg_add_string(ctx, msg, "data", talloc_asprintf(mem_ctx, "0x%x", IVAL(data, 0)));
83                 break;
84         default:
85                 val.length = len;
86                 val.data = data;
87                 ldb_msg_add_value(ctx, msg, "data", &val);
88         }
89
90
91         type_s = talloc_asprintf(mem_ctx, "%u", type);
92         ldb_msg_add_string(ctx, msg, "type", type_s); 
93
94         return msg;
95 }
96
97
98 static int reg_close_ldb_key (void *data)
99 {
100         struct registry_key *key = data;
101         struct ldb_key_data *kd = key->backend_data;
102         struct ldb_context *c = key->hive->backend_data;
103
104         if (kd->subkeys) {
105                 talloc_free(kd->subkeys); 
106                 kd->subkeys = NULL;
107         }
108
109         if (kd->values) {
110                 talloc_free(kd->values); 
111                 kd->values = NULL;
112         }
113         return 0;
114 }
115
116 static char *reg_path_to_ldb(TALLOC_CTX *mem_ctx, struct registry_key *from, const char *path, const char *add)
117 {
118         char *ret = talloc_strdup(mem_ctx, "");
119         char *mypath = talloc_strdup(mem_ctx, path);
120         char *begin;
121         struct ldb_key_data *kd = from->backend_data;
122
123         if(add) 
124                 ret = talloc_asprintf_append(ret, "%s", add);
125
126         while(mypath) {
127                 char *keyname;
128                 begin = strrchr(mypath, '\\');
129
130                 if(begin) keyname = begin + 1;
131                 else keyname = mypath;
132
133                 if(strlen(keyname))
134                         ret = talloc_asprintf_append(ret, "key=%s,", keyname);
135                         
136                 if(begin) {
137                         *begin = '\0';
138                 } else {
139                         break;
140                 }
141         }
142
143         ret = talloc_asprintf_append(ret, "%s", kd->dn);
144
145         return ret;
146 }
147
148
149 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx, struct registry_key *k, int idx, struct registry_key **subkey)
150 {
151         struct ldb_context *c = k->hive->backend_data;
152         struct ldb_message_element *el;
153         struct ldb_key_data *kd = k->backend_data, *newkd;
154
155         /* Do a search if necessary */
156         if (kd->subkeys == NULL) {
157                 kd->subkey_count = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL, &kd->subkeys);
158
159                 if(kd->subkey_count < 0) {
160                         DEBUG(0, ("Error getting subkeys for '%s': %s\n", kd->dn, ldb_errstring(c)));
161                         return WERR_FOOBAR;
162                 }
163         } 
164
165         if (idx >= kd->subkey_count) return WERR_NO_MORE_ITEMS;
166
167         el = ldb_msg_find_element(kd->subkeys[idx], "key");
168         
169         *subkey = talloc(mem_ctx, struct registry_key);
170         talloc_set_destructor(*subkey, reg_close_ldb_key);
171         (*subkey)->name = talloc_strdup(mem_ctx, el->values[0].data);
172         (*subkey)->backend_data = newkd = talloc_zero(*subkey, struct ldb_key_data);
173         newkd->dn = talloc_strdup(mem_ctx, kd->subkeys[idx]->dn);
174
175         return WERR_OK;
176 }
177
178 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct registry_key *k, int idx, struct registry_value **value)
179 {
180         struct ldb_context *c = k->hive->backend_data;
181         struct ldb_key_data *kd = k->backend_data;
182
183         /* Do the search if necessary */
184         if (kd->values == NULL) {
185                 kd->value_count = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(value=*)", NULL,&kd->values);
186
187                 if(kd->value_count < 0) {
188                         DEBUG(0, ("Error getting values for '%s': %s\n", kd->dn, ldb_errstring(c)));
189                         return WERR_FOOBAR;
190                 }
191         }
192
193         if(idx >= kd->value_count) return WERR_NO_MORE_ITEMS;
194
195         *value = talloc(mem_ctx, struct registry_value);
196
197         reg_ldb_unpack_value(mem_ctx, kd->values[idx], &(*value)->name, &(*value)->data_type, &(*value)->data_blk, &(*value)->data_len);
198
199         return WERR_OK;
200 }
201
202 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h, const char *name, struct registry_key **key)
203 {
204         struct ldb_context *c = h->hive->backend_data;
205         struct ldb_message **msg;
206         char *ldap_path;
207         int ret;
208         struct ldb_key_data *newkd;
209
210         ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
211
212         ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL,&msg);
213
214         if(ret == 0) {
215                 return WERR_NO_MORE_ITEMS;
216         } else if(ret < 0) {
217                 DEBUG(0, ("Error opening key '%s': %s\n", ldap_path, ldb_errstring(c)));
218                 return WERR_FOOBAR;
219         }
220
221         *key = talloc(mem_ctx, struct registry_key);
222         talloc_set_destructor(*key, reg_close_ldb_key);
223         (*key)->name = talloc_strdup(mem_ctx, strrchr(name, '\\')?strchr(name, '\\'):name);
224         (*key)->backend_data = newkd = talloc_zero(*key, struct ldb_key_data);
225         newkd->dn = talloc_strdup(mem_ctx, msg[0]->dn); 
226
227         talloc_free(msg);
228
229         return WERR_OK;
230 }
231
232 static WERROR ldb_open_hive(struct registry_hive *hive, struct registry_key **k)
233 {
234         struct ldb_key_data *kd;
235         struct ldb_context *wrap;
236
237         if (!hive->location) return WERR_INVALID_PARAM;
238         wrap = ldb_wrap_connect(hive, hive->location, 0, NULL);
239
240         if(!wrap) {
241                 DEBUG(1, ("ldb_open_hive: unable to connect\n"));
242                 return WERR_FOOBAR;
243         }
244
245         ldb_set_debug_stderr(wrap);
246         hive->backend_data = wrap;
247
248         *k = talloc_zero(hive, struct registry_key);
249         talloc_set_destructor (*k, reg_close_ldb_key);
250         talloc_set_destructor (hive, ldb_free_hive);
251         (*k)->name = talloc_strdup(*k, "");
252         (*k)->backend_data = kd = talloc_zero(*k, struct ldb_key_data);
253         kd->dn = talloc_strdup(*k, "hive=");
254         
255
256         return WERR_OK;
257 }
258
259 static WERROR ldb_add_key (TALLOC_CTX *mem_ctx, struct registry_key *parent, const char *name, uint32_t access_mask, struct security_descriptor *sd, struct registry_key **newkey)
260 {
261         struct ldb_context *ctx = parent->hive->backend_data;
262         struct ldb_message *msg;
263         struct ldb_key_data *newkd;
264         int ret;
265
266         msg = ldb_msg_new(mem_ctx);
267
268         msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
269
270         ldb_msg_add_string(ctx, msg, "key", talloc_strdup(mem_ctx, name));
271
272         ret = ldb_add(ctx, msg);
273         if (ret < 0) {
274                 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(parent->hive->backend_data)));
275                 return WERR_FOOBAR;
276         }
277
278         *newkey = talloc_zero(mem_ctx, struct registry_key);
279         (*newkey)->name = talloc_strdup(mem_ctx, name);
280
281         (*newkey)->backend_data = newkd = talloc_zero(*newkey, struct ldb_key_data);
282         newkd->dn = talloc_steal(newkd, msg->dn);
283
284         return WERR_OK;
285 }
286
287 static WERROR ldb_del_key (struct registry_key *key, const char *child)
288 {
289         int ret;
290         struct ldb_key_data *kd = key->backend_data;
291         char *childdn = talloc_asprintf(NULL, "key=%s,%s", child, kd->dn);
292
293         ret = ldb_delete(key->hive->backend_data, childdn);
294
295         talloc_free(childdn);
296
297         if (ret < 0) {
298                 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(key->hive->backend_data)));
299                 return WERR_FOOBAR;
300         }
301
302         return WERR_OK;
303 }
304
305 static WERROR ldb_del_value (struct registry_key *key, const char *child)
306 {
307         int ret;
308         struct ldb_key_data *kd = key->backend_data;
309         char *childdn = talloc_asprintf(NULL, "value=%s,%s", child, kd->dn);
310
311         ret = ldb_delete(key->hive->backend_data, childdn);
312
313         talloc_free(childdn);
314
315         if (ret < 0) {
316                 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(key->hive->backend_data)));
317                 return WERR_FOOBAR;
318         }
319
320         return WERR_OK;
321 }
322
323 static WERROR ldb_set_value (struct registry_key *parent, const char *name, uint32_t type, void *data, int len)
324 {
325         struct ldb_context *ctx = parent->hive->backend_data;
326         struct ldb_message *msg;
327         struct ldb_key_data *kd = parent->backend_data;
328         int ret;
329         TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
330
331         msg = reg_ldb_pack_value(ctx, mem_ctx, name, type, data, len);
332
333         msg->dn = talloc_asprintf(mem_ctx, "value=%s,%s", name, kd->dn);
334
335         ret = ldb_add(ctx, msg);
336         if (ret < 0) {
337                 ret = ldb_modify(ctx, msg);
338                 if (ret < 0) {
339                         DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(parent->hive->backend_data)));
340                         talloc_free(mem_ctx);
341                         return WERR_FOOBAR;
342                 }
343         }
344         
345         talloc_free(mem_ctx);
346         return WERR_OK;
347 }
348
349 static struct hive_operations reg_backend_ldb = {
350         .name = "ldb",
351         .add_key = ldb_add_key,
352         .del_key = ldb_del_key,
353         .open_hive = ldb_open_hive,
354         .open_key = ldb_open_key,
355         .get_value_by_index = ldb_get_value_by_id,
356         .get_subkey_by_index = ldb_get_subkey_by_id,
357         .set_value = ldb_set_value,
358         .del_value = ldb_del_value,
359 };
360
361 NTSTATUS registry_ldb_init(void)
362 {
363         return registry_register(&reg_backend_ldb);
364 }