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