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