95851dace097608e4fffd74e2cdf576578584773
[kai/samba-autobuild/.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                 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
247         kd->value_count = res->count;
248         kd->values = talloc_steal(kd, res->msgs);
249         talloc_free(res);
250
251         return WERR_OK;
252 }
253
254
255 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
256                                    const struct hive_key *k, uint32_t idx,
257                                    const char **name,
258                                    const char **classname,
259                                    NTTIME *last_mod_time)
260 {
261         struct ldb_message_element *el;
262         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
263         
264         /* Initialization */
265         if (name != NULL)
266                 *name = NULL;
267         if (classname != NULL)
268                 *classname = NULL; /* TODO: Store properly */
269         if (last_mod_time != NULL)
270                 *last_mod_time = 0; /* TODO: we need to add this to the
271                                                 ldb backend properly */
272
273         /* Do a search if necessary */
274         if (kd->subkeys == NULL) {
275                 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
276         }
277
278         if (idx >= kd->subkey_count)
279                 return WERR_NO_MORE_ITEMS;
280
281         el = ldb_msg_find_element(kd->subkeys[idx], "key");
282         SMB_ASSERT(el != NULL);
283         SMB_ASSERT(el->num_values != 0);
284
285         if (name != NULL)
286                 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
287
288         return WERR_OK;
289 }
290
291 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
292                                   const char** name, uint32_t *data_type,
293                                    DATA_BLOB *data)
294 {
295         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
296         struct ldb_context *c = kd->ldb;
297         const char* attrs[] = { "data", "type", NULL };
298         struct ldb_result *res;
299         int ret;
300
301         ret = ldb_search(c, kd->dn, LDB_SCOPE_BASE, "", attrs, &res);
302
303         if (ret != LDB_SUCCESS) {
304                 DEBUG(0, ("Error getting default value for '%s': %s\n",
305                         ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
306                 return WERR_FOOBAR;
307         }
308
309         if (res->count == 0 || res->msgs[0]->num_elements == 0)
310                 return WERR_BADFILE;
311
312         reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
313                  res->msgs[0], name, data_type, data);
314
315         talloc_free(res);
316
317         return WERR_OK;
318 }
319
320 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
321                                   int idx, const char **name,
322                                   uint32_t *data_type, DATA_BLOB *data)
323 {
324         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
325
326         /* if default value exists, give it back */
327         if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
328                 data))) {
329                 if (idx == 0)
330                         return WERR_OK;
331                 else
332                         --idx;
333         }
334
335         /* Do the search if necessary */
336         if (kd->values == NULL) {
337                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
338         }
339
340         if (idx >= kd->value_count)
341                 return WERR_NO_MORE_ITEMS;
342
343         reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
344                          kd->values[idx], name, data_type, data);
345
346         return WERR_OK;
347 }
348
349 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
350                             const char *name, uint32_t *data_type,
351                             DATA_BLOB *data)
352 {
353         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
354         struct ldb_context *c = kd->ldb;
355         struct ldb_result *res;
356         int ret;
357         char *query;
358
359         if (strlen(name) == 0) {
360                 /* default value */
361                 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
362         } else {
363                 /* normal value */
364                 query = talloc_asprintf(mem_ctx, "(value=%s)", name);
365                 ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, query, NULL, &res);
366                 talloc_free(query);
367
368                 if (ret != LDB_SUCCESS) {
369                         DEBUG(0, ("Error getting values for '%s': %s\n",
370                                 ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
371                         return WERR_FOOBAR;
372                 }
373
374                 if (res->count == 0)
375                         return WERR_BADFILE;
376
377                 reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm),
378                          res->msgs[0], NULL, data_type, data);
379
380                 talloc_free(res);
381         }
382
383         return WERR_OK;
384 }
385
386 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
387                            const char *name, struct hive_key **key)
388 {
389         struct ldb_result *res;
390         struct ldb_dn *ldap_path;
391         int ret;
392         struct ldb_key_data *newkd;
393         struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
394         struct ldb_context *c = kd->ldb;
395
396         ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
397
398         ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL, &res);
399
400         if (ret != LDB_SUCCESS) {
401                 DEBUG(3, ("Error opening key '%s': %s\n",
402                         ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
403                 return WERR_FOOBAR;
404         } else if (res->count == 0) {
405                 DEBUG(3, ("Key '%s' not found\n",
406                         ldb_dn_get_linearized(ldap_path)));
407                 talloc_free(res);
408                 return WERR_BADFILE;
409         }
410
411         newkd = talloc_zero(mem_ctx, struct ldb_key_data);
412         newkd->key.ops = &reg_backend_ldb;
413         newkd->ldb = talloc_reference(newkd, kd->ldb);
414         newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
415
416         *key = (struct hive_key *)newkd;
417
418         return WERR_OK;
419 }
420
421 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
422                          struct auth_session_info *session_info,
423                          struct cli_credentials *credentials,
424                          struct event_context *ev_ctx,
425                          struct loadparm_context *lp_ctx,
426                          struct hive_key **k)
427 {
428         struct ldb_key_data *kd;
429         struct ldb_context *wrap;
430         struct ldb_message *attrs_msg;
431
432         if (location == NULL)
433                 return WERR_INVALID_PARAM;
434
435         wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
436                                 location, session_info, credentials, 0, NULL);
437
438         if (wrap == NULL) {
439                 DEBUG(1, (__FILE__": unable to connect\n"));
440                 return WERR_FOOBAR;
441         }
442
443         attrs_msg = ldb_msg_new(wrap);
444         W_ERROR_HAVE_NO_MEMORY(attrs_msg);
445         attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
446         W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
447         ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
448         ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
449
450         ldb_add(wrap, attrs_msg);
451
452         ldb_set_debug_stderr(wrap);
453
454         kd = talloc_zero(parent_ctx, struct ldb_key_data);
455         kd->key.ops = &reg_backend_ldb;
456         kd->ldb = talloc_reference(kd, wrap);
457         talloc_set_destructor (kd, reg_close_ldb_key);
458         kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
459
460         *k = (struct hive_key *)kd;
461
462         return WERR_OK;
463 }
464
465 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
466                           const char *name, const char *classname,
467                           struct security_descriptor *sd,
468                           struct hive_key **newkey)
469 {
470         struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
471         struct ldb_message *msg;
472         struct ldb_key_data *newkd;
473         int ret;
474
475         msg = ldb_msg_new(mem_ctx);
476
477         msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
478
479         ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
480         if (classname != NULL)
481                 ldb_msg_add_string(msg, "classname",
482                                    talloc_strdup(mem_ctx, classname));
483
484         ret = ldb_add(parentkd->ldb, msg);
485         if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
486                 return WERR_ALREADY_EXISTS;
487         }
488
489         if (ret != LDB_SUCCESS) {
490                 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
491                 return WERR_FOOBAR;
492         }
493
494         DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
495
496         newkd = talloc_zero(mem_ctx, struct ldb_key_data);
497         newkd->ldb = talloc_reference(newkd, parentkd->ldb);
498         newkd->key.ops = &reg_backend_ldb;
499         newkd->dn = talloc_steal(newkd, msg->dn);
500
501         *newkey = (struct hive_key *)newkd;
502
503         /* reset cache */
504         talloc_free(parentkd->subkeys);
505         parentkd->subkeys = NULL;
506
507         return WERR_OK;
508 }
509
510 static WERROR ldb_del_value (struct hive_key *key, const char *child)
511 {
512         int ret;
513         struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
514         TALLOC_CTX *mem_ctx;
515         struct ldb_message *msg;
516         struct ldb_dn *childdn;
517
518         if (strlen(child) == 0) {
519                 /* default value */
520                 mem_ctx = talloc_init("ldb_del_value");
521
522                 msg = talloc_zero(mem_ctx, struct ldb_message);
523                 msg->dn = ldb_dn_copy(msg, kd->dn);
524                 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
525                 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
526
527                 ret = ldb_modify(kd->ldb, msg);
528                 if (ret != LDB_SUCCESS) {
529                         DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
530                         talloc_free(mem_ctx);
531                         return WERR_FOOBAR;
532                 }
533
534                 talloc_free(mem_ctx);
535         } else {
536                 /* normal value */
537                 childdn = ldb_dn_copy(kd->ldb, kd->dn);
538                 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
539                                   reg_ldb_escape(childdn, child)))
540                 {
541                         talloc_free(childdn);
542                         return WERR_FOOBAR;
543                 }
544
545                 ret = ldb_delete(kd->ldb, childdn);
546
547                 talloc_free(childdn);
548
549                 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
550                         return WERR_BADFILE;
551                 } else if (ret != LDB_SUCCESS) {
552                         DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
553                         return WERR_FOOBAR;
554                 }
555         }
556
557         /* reset cache */
558         talloc_free(kd->values);
559         kd->values = NULL;
560
561         return WERR_OK;
562 }
563
564 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
565 {
566         int i, ret;
567         struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
568         struct ldb_dn *ldap_path;
569         TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
570         struct ldb_context *c = parentkd->ldb;
571         struct ldb_result *res_keys;
572         struct ldb_result *res_vals;
573         WERROR werr;
574         struct hive_key *hk;
575
576         /* Verify key exists by opening it */
577         werr = ldb_open_key(mem_ctx, key, name, &hk);
578         if (!W_ERROR_IS_OK(werr)) {
579                 talloc_free(mem_ctx);
580                 return werr;
581         }
582
583         ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
584         if (!ldap_path) {
585                 talloc_free(mem_ctx);
586                 return WERR_FOOBAR;
587         }
588
589         /* Search for subkeys */
590         ret = ldb_search(c, ldap_path, LDB_SCOPE_ONELEVEL,
591                          "(key=*)", NULL, &res_keys);
592
593         if (ret != LDB_SUCCESS) {
594                 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
595                       ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
596                 talloc_free(mem_ctx);
597                 return WERR_FOOBAR;
598         }
599
600         /* Search for values */
601         ret = ldb_search(c, ldap_path, LDB_SCOPE_ONELEVEL,
602                          "(value=*)", NULL, &res_vals);
603
604         if (ret != LDB_SUCCESS) {
605                 DEBUG(0, ("Error getting values 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         /* Start an explicit transaction */
612         ret = ldb_transaction_start(c);
613
614         if (ret != LDB_SUCCESS) {
615                 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
616                 talloc_free(mem_ctx);
617                 return WERR_FOOBAR;
618         }
619
620         if (res_keys->count || res_vals->count)
621         {
622                 /* Delete any subkeys */
623                 for (i = 0; i < res_keys->count; i++)
624                 {
625                         werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
626                                                         res_keys->msgs[i],
627                                                         "key", NULL));
628                         if (!W_ERROR_IS_OK(werr)) {
629                                 ret = ldb_transaction_cancel(c);
630                                 talloc_free(mem_ctx);
631                                 return werr;
632                         }
633                 }
634
635                 /* Delete any values */
636                 for (i = 0; i < res_vals->count; i++)
637                 {
638                         werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
639                                                         res_vals->msgs[i],
640                                                         "value", NULL));
641                         if (!W_ERROR_IS_OK(werr)) {
642                                 ret = ldb_transaction_cancel(c);
643                                 talloc_free(mem_ctx);
644                                 return werr;
645                         }
646                 }
647         }
648
649         /* Delete the key itself */
650         ret = ldb_delete(c, ldap_path);
651
652         if (ret != LDB_SUCCESS)
653         {
654                 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
655                 ret = ldb_transaction_cancel(c);
656                 talloc_free(mem_ctx);
657                 return WERR_FOOBAR;
658         }
659
660         /* Commit the transaction */
661         ret = ldb_transaction_commit(c);
662
663         if (ret != LDB_SUCCESS)
664         {
665                 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
666                 ret = ldb_transaction_cancel(c);
667                 talloc_free(mem_ctx);
668                 return WERR_FOOBAR;
669         }
670
671         talloc_free(mem_ctx);
672
673         /* reset cache */
674         talloc_free(parentkd->subkeys);
675         parentkd->subkeys = NULL;
676
677         return WERR_OK;
678 }
679
680 static WERROR ldb_set_value(struct hive_key *parent,
681                             const char *name, uint32_t type,
682                             const DATA_BLOB data)
683 {
684         struct ldb_message *msg;
685         struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
686         int ret;
687         TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
688
689         msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
690         msg->dn = ldb_dn_copy(msg, kd->dn);
691         
692         if (strlen(name) > 0) {
693                 /* For a default value, we add/overwrite the attributes to/of the hive.
694                    For a normal value, we create new childs. */
695                 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
696                                   reg_ldb_escape(mem_ctx, name)))
697                 {
698                         talloc_free(mem_ctx);
699                         return WERR_FOOBAR;
700                 }
701         }
702
703         ret = ldb_add(kd->ldb, msg);
704         if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
705                 int i;
706                 for (i = 0; i < msg->num_elements; i++) {
707                         msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
708                 }
709                 ret = ldb_modify(kd->ldb, msg);
710         }
711
712         if (ret != LDB_SUCCESS) {
713                 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
714                 talloc_free(mem_ctx);
715                 return WERR_FOOBAR;
716         }
717
718         /* reset cache */
719         talloc_free(kd->values);
720         kd->values = NULL;
721
722         talloc_free(mem_ctx);
723         return WERR_OK;
724 }
725
726 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
727                                const struct hive_key *key,
728                                const char **classname,
729                                uint32_t *num_subkeys,
730                                uint32_t *num_values,
731                                NTTIME *last_change_time,
732                                uint32_t *max_subkeynamelen,
733                                uint32_t *max_valnamelen,
734                                uint32_t *max_valbufsize)
735 {
736         struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
737
738         /* Initialization */
739         if (classname != NULL)
740                 *classname = NULL;
741         if (num_subkeys != NULL)
742                 *num_subkeys = 0;
743         if (num_values != NULL)
744                 *num_values = 0;
745         if (last_change_time != NULL)
746                 *last_change_time = 0;
747         if (max_subkeynamelen != NULL)
748                 *max_subkeynamelen = 0;
749         if (max_valnamelen != NULL)
750                 *max_valnamelen = 0;
751         if (max_valbufsize != NULL)
752                 *max_valbufsize = 0;
753
754         if (kd->subkeys == NULL) {
755                 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
756         }
757
758         if (kd->values == NULL) {
759                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
760         }
761
762         if (num_subkeys != NULL) {
763                 *num_subkeys = kd->subkey_count;
764         }
765         if (num_values != NULL) {
766                 *num_values = kd->value_count;
767         }
768
769
770         if (max_subkeynamelen != NULL) {
771                 int i;
772                 struct ldb_message_element *el;
773
774                 *max_subkeynamelen = 0;
775
776                 for (i = 0; i < kd->subkey_count; i++) {
777                         el = ldb_msg_find_element(kd->subkeys[i], "key");
778                         *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
779                 }
780         }
781
782         if (max_valnamelen != NULL || max_valbufsize != NULL) {
783                 int i;
784                 struct ldb_message_element *el;
785                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
786
787                 if (max_valbufsize != NULL)
788                         *max_valbufsize = 0;
789
790                 if (max_valnamelen != NULL)
791                         *max_valnamelen = 0;
792
793                 for (i = 0; i < kd->value_count; i++) {
794                         if (max_valnamelen != NULL) {
795                                 el = ldb_msg_find_element(kd->values[i], "value");
796                                 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
797                         }
798
799                         if (max_valbufsize != NULL) {
800                                 DATA_BLOB data;
801                                 reg_ldb_unpack_value(mem_ctx, 
802                                                      lp_iconv_convenience(global_loadparm),
803                                                      kd->values[i], NULL, 
804                                                      NULL, &data);
805                                 *max_valbufsize = MAX(*max_valbufsize, data.length);
806                                 talloc_free(data.data);
807                         }
808                 }
809         }
810
811         return WERR_OK;
812 }
813
814 static struct hive_operations reg_backend_ldb = {
815         .name = "ldb",
816         .add_key = ldb_add_key,
817         .del_key = ldb_del_key,
818         .get_key_by_name = ldb_open_key,
819         .enum_value = ldb_get_value_by_id,
820         .enum_key = ldb_get_subkey_by_id,
821         .set_value = ldb_set_value,
822         .get_value_by_name = ldb_get_value,
823         .delete_value = ldb_del_value,
824         .get_key_info = ldb_get_key_info,
825 };