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