Merge branch 'master' of ssh://git.samba.org/data/git/samba into regsrv
[ira/wip.git] / source4 / lib / registry / ldb.c
1 /*
2    Unix SMB/CIFS implementation.
3    Registry interface
4    Copyright (C) 2004-2007, Jelmer Vernooij, jelmer@samba.org
5    Copyright (C) 2008 Matthias Dieter Wallnöfer, mwallnoefer@yahoo.de
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 "ldb_wrap.h"
26 #include "librpc/gen_ndr/winreg.h"
27 #include "param/param.h"
28
29 static struct hive_operations reg_backend_ldb;
30
31 struct ldb_key_data
32 {
33         struct hive_key key;
34         struct ldb_context *ldb;
35         struct ldb_dn *dn;
36         struct ldb_message **subkeys, **values;
37         int subkey_count, value_count;
38 };
39
40 static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx, 
41                                  struct smb_iconv_convenience *iconv_convenience,
42                                  struct ldb_message *msg,
43                                  const char **name, uint32_t *type,
44                                  DATA_BLOB *data)
45 {
46         const struct ldb_val *val;
47         uint32_t value_type;
48
49         if (name != NULL)
50                 *name = talloc_strdup(mem_ctx,
51                                       ldb_msg_find_attr_as_string(msg, "value",
52                                       NULL));
53
54         value_type = ldb_msg_find_attr_as_uint(msg, "type", 0);
55         *type = value_type; 
56
57         val = ldb_msg_find_ldb_val(msg, "data");
58
59         switch (value_type)
60         {
61         case REG_SZ:
62         case REG_EXPAND_SZ:
63                 if (val != NULL)
64                         data->length = convert_string_talloc(mem_ctx, iconv_convenience, CH_UTF8, CH_UTF16,
65                                                      val->data, val->length,
66                                                      (void **)&data->data);
67                 else {
68                         data->data = NULL;
69                         data->length = 0;
70                 }
71                 break;
72
73         case REG_BINARY:
74                 if (val != NULL)
75                         *data = data_blob_talloc(mem_ctx, val->data, val->length);
76                 else {
77                         data->data = NULL;
78                         data->length = 0;
79                 }
80                 break;
81
82         case REG_DWORD: {
83                 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
84                 *data = data_blob_talloc(mem_ctx, &tmp, 4);
85                 }
86                 break;
87
88         default:
89                 *data = data_blob_talloc(mem_ctx, val->data, val->length);
90                 break;
91         }
92 }
93
94 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
95                                               TALLOC_CTX *mem_ctx,
96                                               const char *name,
97                                               uint32_t type, DATA_BLOB data)
98 {
99         struct ldb_val val;
100         struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
101         char *type_s;
102
103         ldb_msg_add_string(msg, "value", talloc_strdup(mem_ctx, name));
104
105         switch (type) {
106         case REG_SZ:
107         case REG_EXPAND_SZ:
108                 if (data.data[0] != '\0') {
109                         val.length = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF16, CH_UTF8,
110                                                    (void *)data.data,
111                                                    data.length,
112                                                    (void **)&val.data);
113                         ldb_msg_add_value(msg, "data", &val, NULL);
114                 } else {
115                         ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
116                 }
117                 break;
118
119         case REG_BINARY:
120                 if (data.length > 0)
121                         ldb_msg_add_value(msg, "data", &data, NULL);
122                 else
123                         ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
124                 break;
125
126         case REG_DWORD:
127                 ldb_msg_add_string(msg, "data",
128                                    talloc_asprintf(mem_ctx, "0x%x",
129                                                    IVAL(data.data, 0)));
130                 break;
131         default:
132                 ldb_msg_add_value(msg, "data", &data, NULL);
133         }
134
135
136         type_s = talloc_asprintf(mem_ctx, "%u", type);
137         ldb_msg_add_string(msg, "type", type_s);
138
139         return msg;
140 }
141
142 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
143 {
144         struct ldb_val val;
145
146         val.data = discard_const_p(uint8_t, value);
147         val.length = strlen(value);
148
149         return ldb_dn_escape_value(mem_ctx, val);
150 }
151
152 static int reg_close_ldb_key(struct ldb_key_data *key)
153 {
154         if (key->subkeys != NULL) {
155                 talloc_free(key->subkeys);
156                 key->subkeys = NULL;
157         }
158
159         if (key->values != NULL) {
160                 talloc_free(key->values);
161                 key->values = NULL;
162         }
163         return 0;
164 }
165
166 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
167                                       const struct hive_key *from,
168                                       const char *path, const char *add)
169 {
170         TALLOC_CTX *local_ctx;
171         struct ldb_dn *ret;
172         char *mypath = talloc_strdup(mem_ctx, path);
173         char *begin;
174         struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
175         struct ldb_context *ldb = kd->ldb;
176
177         local_ctx = talloc_new(mem_ctx);
178
179         if (add) {
180                 ret = ldb_dn_new(mem_ctx, ldb, add);
181         } else {
182                 ret = ldb_dn_new(mem_ctx, ldb, NULL);
183         }
184         if (!ldb_dn_validate(ret)) {
185                 talloc_free(ret);
186                 talloc_free(local_ctx);
187                 return NULL;
188         }
189
190         while (mypath) {
191                 char *keyname;
192
193                 begin = strrchr(mypath, '\\');
194
195                 if (begin) keyname = begin + 1;
196                 else keyname = mypath;
197
198                 if(strlen(keyname)) {
199                         if (!ldb_dn_add_base_fmt(ret, "key=%s",
200                                                  reg_ldb_escape(local_ctx,
201                                                                 keyname)))
202                         {
203                                 talloc_free(local_ctx);
204                                 return NULL;
205                         }
206                 }
207
208                 if(begin) {
209                         *begin = '\0';
210                 } else {
211                         break;
212                 }
213         }
214
215         ldb_dn_add_base(ret, kd->dn);
216
217         talloc_free(local_ctx);
218
219         return ret;
220 }
221
222 static WERROR cache_subkeys(struct ldb_key_data *kd)
223 {
224         struct ldb_context *c = kd->ldb;
225         struct ldb_result *res;
226         int ret;
227
228         ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "(key=*)");
229
230         if (ret != LDB_SUCCESS) {
231                 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
232                         ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
233                 return WERR_FOOBAR;
234         }
235
236         kd->subkey_count = res->count;
237         kd->subkeys = talloc_steal(kd, res->msgs);
238         talloc_free(res);
239
240         return WERR_OK;
241 }
242
243 static WERROR cache_values(struct ldb_key_data *kd)
244 {
245         struct ldb_context *c = kd->ldb;
246         struct ldb_result *res;
247         int ret;
248
249         ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
250                          NULL, "(value=*)");
251
252         if (ret != LDB_SUCCESS) {
253                 DEBUG(0, ("Error getting values for '%s': %s\n",
254                         ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
255                 return WERR_FOOBAR;
256         }
257
258         kd->value_count = res->count;
259         kd->values = talloc_steal(kd, res->msgs);
260         talloc_free(res);
261
262         return WERR_OK;
263 }
264
265
266 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
267                                    const struct hive_key *k, uint32_t idx,
268                                    const char **name,
269                                    const char **classname,
270                                    NTTIME *last_mod_time)
271 {
272         struct ldb_message_element *el;
273         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
274         
275         /* Initialization */
276         if (name != NULL)
277                 *name = NULL;
278         if (classname != NULL)
279                 *classname = NULL; /* TODO: Store properly */
280         if (last_mod_time != NULL)
281                 *last_mod_time = 0; /* TODO: we need to add this to the
282                                                 ldb backend properly */
283
284         /* Do a search if necessary */
285         if (kd->subkeys == NULL) {
286                 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
287         }
288
289         if (idx >= kd->subkey_count)
290                 return WERR_NO_MORE_ITEMS;
291
292         el = ldb_msg_find_element(kd->subkeys[idx], "key");
293         SMB_ASSERT(el != NULL);
294         SMB_ASSERT(el->num_values != 0);
295
296         if (name != NULL)
297                 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
298
299         return WERR_OK;
300 }
301
302 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
303                                   const char **name, uint32_t *data_type,
304                                    DATA_BLOB *data)
305 {
306         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
307         struct ldb_context *c = kd->ldb;
308         const char* attrs[] = { "data", "type", NULL };
309         struct ldb_result *res;
310         int ret;
311
312         ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_BASE, attrs, "%s", "");
313
314         if (ret != LDB_SUCCESS) {
315                 DEBUG(0, ("Error getting default value for '%s': %s\n",
316                         ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
317                 return WERR_FOOBAR;
318         }
319
320         if (res->count == 0 || res->msgs[0]->num_elements == 0)
321                 return WERR_BADFILE;
322
323         reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
324                  res->msgs[0], name, data_type, data);
325
326         talloc_free(res);
327
328         return WERR_OK;
329 }
330
331 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
332                                   int idx, const char **name,
333                                   uint32_t *data_type, DATA_BLOB *data)
334 {
335         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
336
337         /* if default value exists, give it back */
338         if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
339                 data))) {
340                 if (idx == 0)
341                         return WERR_OK;
342                 else
343                         --idx;
344         }
345
346         /* Do the search if necessary */
347         if (kd->values == NULL) {
348                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
349         }
350
351         if (idx >= kd->value_count)
352                 return WERR_NO_MORE_ITEMS;
353
354         reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
355                          kd->values[idx], name, data_type, data);
356
357         return WERR_OK;
358 }
359
360 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
361                             const char *name, uint32_t *data_type,
362                             DATA_BLOB *data)
363 {
364         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
365         struct ldb_context *c = kd->ldb;
366         struct ldb_result *res;
367         int ret;
368         char *query;
369
370         if (strlen(name) == 0) {
371                 /* default value */
372                 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
373         } else {
374                 /* normal value */
375                 query = talloc_asprintf(mem_ctx, "(value=%s)", name);
376                 ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_ONELEVEL, NULL, "%s", query);
377                 talloc_free(query);
378
379                 if (ret != LDB_SUCCESS) {
380                         DEBUG(0, ("Error getting values for '%s': %s\n",
381                                 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
382                         return WERR_FOOBAR;
383                 }
384
385                 if (res->count == 0)
386                         return WERR_BADFILE;
387
388                 reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
389                          res->msgs[0], NULL, data_type, data);
390
391                 talloc_free(res);
392         }
393
394         return WERR_OK;
395 }
396
397 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
398                            const char *name, struct hive_key **key)
399 {
400         struct ldb_result *res;
401         struct ldb_dn *ldap_path;
402         int ret;
403         struct ldb_key_data *newkd;
404         struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
405         struct ldb_context *c = kd->ldb;
406
407         ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
408
409         ret = ldb_search(c, mem_ctx, &res, ldap_path, LDB_SCOPE_BASE, NULL, "(key=*)");
410
411         if (ret != LDB_SUCCESS) {
412                 DEBUG(3, ("Error opening key '%s': %s\n",
413                         ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
414                 return WERR_FOOBAR;
415         } else if (res->count == 0) {
416                 DEBUG(3, ("Key '%s' not found\n",
417                         ldb_dn_get_linearized(ldap_path)));
418                 talloc_free(res);
419                 return WERR_BADFILE;
420         }
421
422         newkd = talloc_zero(mem_ctx, struct ldb_key_data);
423         newkd->key.ops = &reg_backend_ldb;
424         newkd->ldb = talloc_reference(newkd, kd->ldb);
425         newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
426
427         *key = (struct hive_key *)newkd;
428
429         return WERR_OK;
430 }
431
432 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
433                          struct auth_session_info *session_info,
434                          struct cli_credentials *credentials,
435                          struct event_context *ev_ctx,
436                          struct loadparm_context *lp_ctx,
437                          struct hive_key **k)
438 {
439         struct ldb_key_data *kd;
440         struct ldb_context *wrap;
441         struct ldb_message *attrs_msg;
442
443         if (location == NULL)
444                 return WERR_INVALID_PARAM;
445
446         wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
447                                 location, session_info, credentials, 0, NULL);
448
449         if (wrap == NULL) {
450                 DEBUG(1, (__FILE__": unable to connect\n"));
451                 return WERR_FOOBAR;
452         }
453
454         attrs_msg = ldb_msg_new(wrap);
455         W_ERROR_HAVE_NO_MEMORY(attrs_msg);
456         attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
457         W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
458         ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
459         ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
460
461         ldb_add(wrap, attrs_msg);
462
463         ldb_set_debug_stderr(wrap);
464
465         kd = talloc_zero(parent_ctx, struct ldb_key_data);
466         kd->key.ops = &reg_backend_ldb;
467         kd->ldb = talloc_reference(kd, wrap);
468         talloc_set_destructor (kd, reg_close_ldb_key);
469         kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
470
471         *k = (struct hive_key *)kd;
472
473         return WERR_OK;
474 }
475
476 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
477                           const char *name, const char *classname,
478                           struct security_descriptor *sd,
479                           struct hive_key **newkey)
480 {
481         struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
482         struct ldb_message *msg;
483         struct ldb_key_data *newkd;
484         int ret;
485
486         msg = ldb_msg_new(mem_ctx);
487
488         msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
489
490         ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
491         if (classname != NULL)
492                 ldb_msg_add_string(msg, "classname",
493                                    talloc_strdup(mem_ctx, classname));
494
495         ret = ldb_add(parentkd->ldb, msg);
496         if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
497                 return WERR_ALREADY_EXISTS;
498         }
499
500         if (ret != LDB_SUCCESS) {
501                 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
502                 return WERR_FOOBAR;
503         }
504
505         DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
506
507         newkd = talloc_zero(mem_ctx, struct ldb_key_data);
508         newkd->ldb = talloc_reference(newkd, parentkd->ldb);
509         newkd->key.ops = &reg_backend_ldb;
510         newkd->dn = talloc_steal(newkd, msg->dn);
511
512         *newkey = (struct hive_key *)newkd;
513
514         /* reset cache */
515         talloc_free(parentkd->subkeys);
516         parentkd->subkeys = NULL;
517
518         return WERR_OK;
519 }
520
521 static WERROR ldb_del_value (struct hive_key *key, const char *child)
522 {
523         int ret;
524         struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
525         TALLOC_CTX *mem_ctx;
526         struct ldb_message *msg;
527         struct ldb_dn *childdn;
528
529         if (strlen(child) == 0) {
530                 /* default value */
531                 mem_ctx = talloc_init("ldb_del_value");
532
533                 msg = talloc_zero(mem_ctx, struct ldb_message);
534                 msg->dn = ldb_dn_copy(msg, kd->dn);
535                 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
536                 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
537
538                 ret = ldb_modify(kd->ldb, msg);
539                 if (ret != LDB_SUCCESS) {
540                         DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
541                         talloc_free(mem_ctx);
542                         return WERR_FOOBAR;
543                 }
544
545                 talloc_free(mem_ctx);
546         } else {
547                 /* normal value */
548                 childdn = ldb_dn_copy(kd->ldb, kd->dn);
549                 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
550                                   reg_ldb_escape(childdn, child)))
551                 {
552                         talloc_free(childdn);
553                         return WERR_FOOBAR;
554                 }
555
556                 ret = ldb_delete(kd->ldb, childdn);
557
558                 talloc_free(childdn);
559
560                 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
561                         return WERR_BADFILE;
562                 } else if (ret != LDB_SUCCESS) {
563                         DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
564                         return WERR_FOOBAR;
565                 }
566         }
567
568         /* reset cache */
569         talloc_free(kd->values);
570         kd->values = NULL;
571
572         return WERR_OK;
573 }
574
575 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
576 {
577         int i, ret;
578         struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
579         struct ldb_dn *ldap_path;
580         TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
581         struct ldb_context *c = parentkd->ldb;
582         struct ldb_result *res_keys;
583         struct ldb_result *res_vals;
584         WERROR werr;
585         struct hive_key *hk;
586
587         /* Verify key exists by opening it */
588         werr = ldb_open_key(mem_ctx, key, name, &hk);
589         if (!W_ERROR_IS_OK(werr)) {
590                 talloc_free(mem_ctx);
591                 return werr;
592         }
593
594         ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
595         if (!ldap_path) {
596                 talloc_free(mem_ctx);
597                 return WERR_FOOBAR;
598         }
599
600         /* Search for subkeys */
601         ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
602                          NULL, "(key=*)");
603
604         if (ret != LDB_SUCCESS) {
605                 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
606                       ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
607                 talloc_free(mem_ctx);
608                 return WERR_FOOBAR;
609         }
610
611         /* Search for values */
612         ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
613                          NULL, "(value=*)");
614
615         if (ret != LDB_SUCCESS) {
616                 DEBUG(0, ("Error getting values for '%s': %s\n",
617                       ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
618                 talloc_free(mem_ctx);
619                 return WERR_FOOBAR;
620         }
621
622         /* Start an explicit transaction */
623         ret = ldb_transaction_start(c);
624
625         if (ret != LDB_SUCCESS) {
626                 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
627                 talloc_free(mem_ctx);
628                 return WERR_FOOBAR;
629         }
630
631         if (res_keys->count || res_vals->count)
632         {
633                 /* Delete any subkeys */
634                 for (i = 0; i < res_keys->count; i++)
635                 {
636                         werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
637                                                         res_keys->msgs[i],
638                                                         "key", NULL));
639                         if (!W_ERROR_IS_OK(werr)) {
640                                 ret = ldb_transaction_cancel(c);
641                                 talloc_free(mem_ctx);
642                                 return werr;
643                         }
644                 }
645
646                 /* Delete any values */
647                 for (i = 0; i < res_vals->count; i++)
648                 {
649                         werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
650                                                         res_vals->msgs[i],
651                                                         "value", NULL));
652                         if (!W_ERROR_IS_OK(werr)) {
653                                 ret = ldb_transaction_cancel(c);
654                                 talloc_free(mem_ctx);
655                                 return werr;
656                         }
657                 }
658         }
659
660         /* Delete the key itself */
661         ret = ldb_delete(c, ldap_path);
662
663         if (ret != LDB_SUCCESS)
664         {
665                 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
666                 ret = ldb_transaction_cancel(c);
667                 talloc_free(mem_ctx);
668                 return WERR_FOOBAR;
669         }
670
671         /* Commit the transaction */
672         ret = ldb_transaction_commit(c);
673
674         if (ret != LDB_SUCCESS)
675         {
676                 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
677                 ret = ldb_transaction_cancel(c);
678                 talloc_free(mem_ctx);
679                 return WERR_FOOBAR;
680         }
681
682         talloc_free(mem_ctx);
683
684         /* reset cache */
685         talloc_free(parentkd->subkeys);
686         parentkd->subkeys = NULL;
687
688         return WERR_OK;
689 }
690
691 static WERROR ldb_set_value(struct hive_key *parent,
692                             const char *name, uint32_t type,
693                             const DATA_BLOB data)
694 {
695         struct ldb_message *msg;
696         struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
697         int ret;
698         TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
699
700         msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
701         msg->dn = ldb_dn_copy(msg, kd->dn);
702         
703         if (strlen(name) > 0) {
704                 /* For a default value, we add/overwrite the attributes to/of the hive.
705                    For a normal value, we create new childs. */
706                 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
707                                   reg_ldb_escape(mem_ctx, name)))
708                 {
709                         talloc_free(mem_ctx);
710                         return WERR_FOOBAR;
711                 }
712         }
713
714         ret = ldb_add(kd->ldb, msg);
715         if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
716                 int i;
717                 for (i = 0; i < msg->num_elements; i++) {
718                         if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE)
719                                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
720                 }
721                 ret = ldb_modify(kd->ldb, msg);
722         }
723
724         if (ret != LDB_SUCCESS) {
725                 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
726                 talloc_free(mem_ctx);
727                 return WERR_FOOBAR;
728         }
729
730         /* reset cache */
731         talloc_free(kd->values);
732         kd->values = NULL;
733
734         talloc_free(mem_ctx);
735         return WERR_OK;
736 }
737
738 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
739                                const struct hive_key *key,
740                                const char **classname,
741                                uint32_t *num_subkeys,
742                                uint32_t *num_values,
743                                NTTIME *last_change_time,
744                                uint32_t *max_subkeynamelen,
745                                uint32_t *max_valnamelen,
746                                uint32_t *max_valbufsize)
747 {
748         struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
749
750         /* Initialization */
751         if (classname != NULL)
752                 *classname = NULL;
753         if (num_subkeys != NULL)
754                 *num_subkeys = 0;
755         if (num_values != NULL)
756                 *num_values = 0;
757         if (last_change_time != NULL)
758                 *last_change_time = 0;
759         if (max_subkeynamelen != NULL)
760                 *max_subkeynamelen = 0;
761         if (max_valnamelen != NULL)
762                 *max_valnamelen = 0;
763         if (max_valbufsize != NULL)
764                 *max_valbufsize = 0;
765
766         if (kd->subkeys == NULL) {
767                 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
768         }
769
770         if (kd->values == NULL) {
771                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
772         }
773
774         if (num_subkeys != NULL) {
775                 *num_subkeys = kd->subkey_count;
776         }
777         if (num_values != NULL) {
778                 *num_values = kd->value_count;
779         }
780
781
782         if (max_subkeynamelen != NULL) {
783                 int i;
784                 struct ldb_message_element *el;
785
786                 *max_subkeynamelen = 0;
787
788                 for (i = 0; i < kd->subkey_count; i++) {
789                         el = ldb_msg_find_element(kd->subkeys[i], "key");
790                         *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
791                 }
792         }
793
794         if (max_valnamelen != NULL || max_valbufsize != NULL) {
795                 int i;
796                 struct ldb_message_element *el;
797                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
798
799                 if (max_valbufsize != NULL)
800                         *max_valbufsize = 0;
801
802                 if (max_valnamelen != NULL)
803                         *max_valnamelen = 0;
804
805                 for (i = 0; i < kd->value_count; i++) {
806                         if (max_valnamelen != NULL) {
807                                 el = ldb_msg_find_element(kd->values[i], "value");
808                                 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
809                         }
810
811                         if (max_valbufsize != NULL) {
812                                 uint32_t data_type;
813                                 DATA_BLOB data;
814                                 reg_ldb_unpack_value(mem_ctx, 
815                                                      lp_iconv_convenience(global_loadparm),
816                                                      kd->values[i], NULL, 
817                                                      &data_type, &data);
818                                 *max_valbufsize = MAX(*max_valbufsize, data.length);
819                                 talloc_free(data.data);
820                         }
821                 }
822         }
823
824         return WERR_OK;
825 }
826
827 static struct hive_operations reg_backend_ldb = {
828         .name = "ldb",
829         .add_key = ldb_add_key,
830         .del_key = ldb_del_key,
831         .get_key_by_name = ldb_open_key,
832         .enum_value = ldb_get_value_by_id,
833         .enum_key = ldb_get_subkey_by_id,
834         .set_value = ldb_set_value,
835         .get_value_by_name = ldb_get_value,
836         .delete_value = ldb_del_value,
837         .get_key_info = ldb_get_key_info,
838 };