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