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