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