]> git.samba.org - kamenim/samba.git/blob - source4/lib/registry/reg_backend_ldb.c
b9bfe8a230641b569d38a2b53b5af208295189be
[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 #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 (struct registry_hive *hive)
36 {
37         talloc_free(hive->backend_data);
38         hive->backend_data = NULL;
39         return 0;
40 }
41
42 static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx, struct ldb_message *msg, const char **name, uint32_t *type, DATA_BLOB *data)
43 {
44         const struct ldb_val *val;
45         *name = talloc_strdup(mem_ctx, ldb_msg_find_attr_as_string(msg, "value", NULL));
46         *type = ldb_msg_find_attr_as_uint(msg, "type", 0);
47         val = ldb_msg_find_ldb_val(msg, "data");
48
49         switch (*type)
50         {
51         case REG_SZ:
52         case REG_EXPAND_SZ:
53                 data->length = convert_string_talloc(mem_ctx, CH_UTF8, CH_UTF16, val->data, val->length, (void **)&data->data);
54                 break;
55
56         case REG_DWORD: {
57                 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
58                 *data = data_blob_talloc(mem_ctx, &tmp, 4);
59                 }
60                 break;
61
62         default:
63                 *data = data_blob_talloc(mem_ctx, val->data, val->length);
64                 break;
65         }
66 }
67
68 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)
69 {
70         struct ldb_val val;
71         struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
72         char *type_s;
73
74         ldb_msg_add_string(msg, "value", talloc_strdup(mem_ctx, name));
75
76         switch (type) {
77         case REG_SZ:
78         case REG_EXPAND_SZ:
79                 val.length = convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8, (void *)data.data, data.length, (void **)&val.data);
80                 ldb_msg_add_value(msg, "data", &val, NULL);
81                 break;
82
83         case REG_DWORD:
84                 ldb_msg_add_string(msg, "data", talloc_asprintf(mem_ctx, "0x%x", IVAL(data.data, 0)));
85                 break;
86         default:
87                 ldb_msg_add_value(msg, "data", &data, NULL);
88         }
89
90
91         type_s = talloc_asprintf(mem_ctx, "%u", type);
92         ldb_msg_add_string(msg, "type", type_s); 
93
94         return msg;
95 }
96
97
98 static int reg_close_ldb_key(struct registry_key *key)
99 {
100         struct ldb_key_data *kd = talloc_get_type(key->backend_data, struct ldb_key_data);
101 /*      struct ldb_context *c = key->hive->backend_data; */
102
103         if (kd->subkeys) {
104                 talloc_free(kd->subkeys); 
105                 kd->subkeys = NULL;
106         }
107
108         if (kd->values) {
109                 talloc_free(kd->values); 
110                 kd->values = NULL;
111         }
112         return 0;
113 }
114
115 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx, const struct registry_key *from, const char *path, const char *add)
116 {
117         TALLOC_CTX *local_ctx;
118         struct ldb_dn *ret = ldb_dn_new(mem_ctx);
119         char *mypath = talloc_strdup(mem_ctx, path);
120         char *begin;
121         struct ldb_key_data *kd = talloc_get_type(from->backend_data, struct ldb_key_data);
122
123         local_ctx = talloc_new(mem_ctx);
124
125         if (add) 
126                 ret = ldb_dn_compose(local_ctx, ret, ldb_dn_explode(mem_ctx, add));
127
128         while(mypath) {
129                 char *keyname;
130
131                 begin = strrchr(mypath, '\\');
132
133                 if (begin) keyname = begin + 1;
134                 else keyname = mypath;
135
136                 if(strlen(keyname)) {
137                         struct ldb_dn *base;
138
139                         base = ldb_dn_build_child(local_ctx, "key", keyname, NULL);
140                         ret = ldb_dn_compose(local_ctx, ret, base);
141                 }
142
143                 if(begin) {
144                         *begin = '\0';
145                 } else {
146                         break;
147                 }
148         }
149
150         ret = ldb_dn_compose(mem_ctx, ret, kd->dn);
151
152         talloc_free(local_ctx);
153
154         return ret;
155 }
156
157
158 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx, const struct registry_key *k, int idx, struct registry_key **subkey)
159 {
160         struct ldb_context *c = talloc_get_type(k->hive->backend_data, struct ldb_context);
161         struct ldb_message_element *el;
162         struct ldb_key_data *kd = talloc_get_type(k->backend_data, struct ldb_key_data);
163         struct ldb_key_data *newkd;
164
165         /* Do a search if necessary */
166         if (kd->subkeys == NULL) {
167                 struct ldb_result *res;
168                 int ret;
169
170                 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL, &res);
171
172                 if (ret != LDB_SUCCESS) {
173                         DEBUG(0, ("Error getting subkeys for '%s': %s\n", ldb_dn_linearize(mem_ctx, kd->dn), ldb_errstring(c)));
174                         return WERR_FOOBAR;
175                 }
176
177                 kd->subkey_count = res->count;
178                 kd->subkeys = talloc_steal(kd, res->msgs);
179                 talloc_free(res);
180         } 
181
182         if (idx >= kd->subkey_count) return WERR_NO_MORE_ITEMS;
183
184         el = ldb_msg_find_element(kd->subkeys[idx], "key");
185         
186         *subkey = talloc(mem_ctx, struct registry_key);
187         talloc_set_destructor(*subkey, reg_close_ldb_key);
188         (*subkey)->name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
189         (*subkey)->backend_data = newkd = talloc_zero(*subkey, struct ldb_key_data);
190         (*subkey)->last_mod = 0; /* TODO: we need to add this to the
191                                     ldb backend properly */
192         newkd->dn = ldb_dn_copy(mem_ctx, kd->subkeys[idx]->dn);
193
194         return WERR_OK;
195 }
196
197 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, const struct registry_key *k, int idx, struct registry_value **value)
198 {
199         struct ldb_context *c = talloc_get_type(k->hive->backend_data, struct ldb_context);
200         struct ldb_key_data *kd = talloc_get_type(k->backend_data, struct ldb_key_data);
201
202         /* Do the search if necessary */
203         if (kd->values == NULL) {
204                 struct ldb_result *res;
205                 int ret;
206
207                 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(value=*)", NULL, &res);
208
209                 if (ret != LDB_SUCCESS) {
210                         DEBUG(0, ("Error getting values for '%s': %s\n", ldb_dn_linearize(mem_ctx, kd->dn), ldb_errstring(c)));
211                         return WERR_FOOBAR;
212                 }
213                 kd->value_count = res->count;
214                 kd->values = talloc_steal(kd, res->msgs);
215                 talloc_free(res);
216         }
217
218         if(idx >= kd->value_count) return WERR_NO_MORE_ITEMS;
219
220         *value = talloc(mem_ctx, struct registry_value);
221
222         reg_ldb_unpack_value(mem_ctx, kd->values[idx], &(*value)->name, &(*value)->data_type, &(*value)->data);
223
224         return WERR_OK;
225 }
226
227 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct registry_key *h, const char *name, struct registry_key **key)
228 {
229         struct ldb_context *c = talloc_get_type(h->hive->backend_data, struct ldb_context);
230         struct ldb_result *res;
231         struct ldb_dn *ldap_path;
232         int ret;
233         struct ldb_key_data *newkd;
234
235         ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
236
237         ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL, &res);
238
239         if (ret != LDB_SUCCESS) {
240                 DEBUG(0, ("Error opening key '%s': %s\n", ldb_dn_linearize(ldap_path, ldap_path), ldb_errstring(c)));
241                 return WERR_FOOBAR;
242         } else if (res->count == 0) {
243                 talloc_free(res);
244                 return WERR_BADFILE;
245         }
246
247         *key = talloc(mem_ctx, struct registry_key);
248         talloc_set_destructor(*key, reg_close_ldb_key);
249         (*key)->name = talloc_strdup(mem_ctx, strrchr(name, '\\')?strchr(name, '\\'):name);
250         (*key)->backend_data = newkd = talloc_zero(*key, struct ldb_key_data);
251         newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn); 
252
253         talloc_free(res);
254
255         return WERR_OK;
256 }
257
258 static WERROR ldb_open_hive(struct registry_hive *hive, struct registry_key **k)
259 {
260         struct ldb_key_data *kd;
261         struct ldb_context *wrap;
262
263         if (!hive->location) return WERR_INVALID_PARAM;
264
265         wrap = ldb_wrap_connect(hive, hive->location, hive->session_info, hive->credentials, 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 }