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