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