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