pyldb: avoid segfault when adding an element with no name
[kai/samba-autobuild/.git] / source4 / lib / registry / ldb.c
1 /*
2    Unix SMB/CIFS implementation.
3    Registry interface
4    Copyright (C) 2004-2007, Jelmer Vernooij, jelmer@samba.org
5    Copyright (C) 2008-2010, Matthias Dieter Wallnöfer, mdw@samba.org
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 <ldb.h>
24 #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         unsigned int subkey_count, value_count;
38         const char *classname;
39 };
40
41 static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx,
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                                       ""));
53         }
54
55         value_type = ldb_msg_find_attr_as_uint(msg, "type", 0);
56         *type = value_type;
57
58         val = ldb_msg_find_ldb_val(msg, "data");
59
60         switch (value_type)
61         {
62         case REG_SZ:
63         case REG_EXPAND_SZ:
64                 if (val != NULL) {
65                         /* The data should be provided as UTF16 string */
66                         convert_string_talloc(mem_ctx, CH_UTF8, CH_UTF16,
67                                               val->data, val->length,
68                                               (void **)&data->data, &data->length);
69                 } else {
70                         data->data = NULL;
71                         data->length = 0;
72                 }
73                 break;
74
75         case REG_DWORD:
76         case REG_DWORD_BIG_ENDIAN:
77                 if (val != NULL) {
78                         int error = 0;
79                         /* The data is a plain DWORD */
80                         uint32_t tmp;
81
82                         tmp = strtoul_err((char *)val->data, NULL, 0, &error);
83                         if (error != 0) {
84                                 data->data = NULL;
85                                 data->length = 0;
86                                 break;
87                         }
88                         data->data = talloc_size(mem_ctx, sizeof(uint32_t));
89                         if (data->data != NULL) {
90                                 SIVAL(data->data, 0, tmp);
91                         }
92                         data->length = sizeof(uint32_t);
93                 } else {
94                         data->data = NULL;
95                         data->length = 0;
96                 }
97                 break;
98
99         case REG_QWORD:
100                 if (val != NULL) {
101                         int error = 0;
102                         /* The data is a plain QWORD */
103                         uint64_t tmp;
104
105                         tmp = strtoull_err((char *)val->data, NULL, 0, &error);
106                         if (error != 0) {
107                                 data->data = NULL;
108                                 data->length = 0;
109                                 break;
110                         }
111                         data->data = talloc_size(mem_ctx, sizeof(uint64_t));
112                         if (data->data != NULL) {
113                                 SBVAL(data->data, 0, tmp);
114                         }
115                         data->length = sizeof(uint64_t);
116                 } else {
117                         data->data = NULL;
118                         data->length = 0;
119                 }
120                 break;
121
122         case REG_BINARY:
123         default:
124                 if (val != NULL) {
125                         data->data = talloc_memdup(mem_ctx, val->data,
126                                                    val->length);
127                         data->length = val->length;
128                 } else {
129                         data->data = NULL;
130                         data->length = 0;
131                 }
132                 break;
133         }
134 }
135
136 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
137                                               TALLOC_CTX *mem_ctx,
138                                               const char *name,
139                                               uint32_t type, DATA_BLOB data)
140 {
141         struct ldb_message *msg;
142         char *name_dup, *type_str;
143         int ret;
144
145         msg = talloc_zero(mem_ctx, struct ldb_message);
146         if (msg == NULL) {
147                 return NULL;
148         }
149
150         name_dup = talloc_strdup(msg, name);
151         if (name_dup == NULL) {
152                 talloc_free(msg);
153                 return NULL;
154         }
155
156         ret = ldb_msg_add_string(msg, "value", name_dup);
157         if (ret != LDB_SUCCESS) {
158                 talloc_free(msg);
159                 return NULL;
160         }
161
162         switch (type) {
163         case REG_SZ:
164         case REG_EXPAND_SZ:
165                 if ((data.length > 0) && (data.data != NULL)) {
166                         struct ldb_val *val;
167                         bool ret2 = false;
168
169                         val = talloc_zero(msg, struct ldb_val);
170                         if (val == NULL) {
171                                 talloc_free(msg);
172                                 return NULL;
173                         }
174
175                         /* The data is provided as UTF16 string */
176                         ret2 = convert_string_talloc(mem_ctx, CH_UTF16, CH_UTF8,
177                                                      (void *)data.data, data.length,
178                                                      (void **)&val->data, &val->length);
179                         if (ret2) {
180                                 ret = ldb_msg_add_value(msg, "data", val, NULL);
181                         } else {
182                                 /* workaround for non-standard data */
183                                 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
184                         }
185                 } else {
186                         ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
187                 }
188                 break;
189
190         case REG_DWORD:
191         case REG_DWORD_BIG_ENDIAN:
192                 if ((data.length > 0) && (data.data != NULL)) {
193                         if (data.length == sizeof(uint32_t)) {
194                                 char *conv_str;
195
196                                 conv_str = talloc_asprintf(msg, "0x%8.8x",
197                                                            IVAL(data.data, 0));
198                                 if (conv_str == NULL) {
199                                         talloc_free(msg);
200                                         return NULL;
201                                 }
202                                 ret = ldb_msg_add_string(msg, "data", conv_str);
203                         } else {
204                                 /* workaround for non-standard data */
205                                 talloc_free(msg);
206                                 return NULL;
207                         }
208                 } else {
209                         ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
210                 }
211                 break;
212
213         case REG_QWORD:
214                 if ((data.length > 0) && (data.data != NULL)) {
215                         if (data.length == sizeof(uint64_t)) {
216                                 char *conv_str;
217
218                                 conv_str = talloc_asprintf(msg, "0x%16.16llx",
219                                                            (unsigned long long)BVAL(data.data, 0));
220                                 if (conv_str == NULL) {
221                                         talloc_free(msg);
222                                         return NULL;
223                                 }
224                                 ret = ldb_msg_add_string(msg, "data", conv_str);
225                         } else {
226                                 /* workaround for non-standard data */
227                                 talloc_free(msg);
228                                 return NULL;
229
230                         }
231                 } else {
232                         ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
233                 }
234                 break;
235
236         case REG_BINARY:
237         default:
238                 if ((data.length > 0) && (data.data != NULL)) {
239                         ret = ldb_msg_add_value(msg, "data", &data, NULL);
240                 } else {
241                         ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
242                 }
243                 break;
244         }
245
246         if (ret != LDB_SUCCESS) {
247                 talloc_free(msg);
248                 return NULL;
249         }
250
251         type_str = talloc_asprintf(mem_ctx, "%u", type);
252         if (type_str == NULL) {
253                 talloc_free(msg);
254                 return NULL;
255         }
256
257         ret = ldb_msg_add_string(msg, "type", type_str);
258         if (ret != LDB_SUCCESS) {
259                 talloc_free(msg);
260                 return NULL;
261         }
262
263         return msg;
264 }
265
266 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
267 {
268         struct ldb_val val;
269
270         val.data = discard_const_p(uint8_t, value);
271         val.length = strlen(value);
272
273         return ldb_dn_escape_value(mem_ctx, val);
274 }
275
276 static int reg_close_ldb_key(struct ldb_key_data *key)
277 {
278         if (key->subkeys != NULL) {
279                 talloc_free(key->subkeys);
280                 key->subkeys = NULL;
281         }
282
283         if (key->values != NULL) {
284                 talloc_free(key->values);
285                 key->values = NULL;
286         }
287         return 0;
288 }
289
290 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
291                                       const struct hive_key *from,
292                                       const char *path, const char *add)
293 {
294         struct ldb_dn *ret;
295         char *mypath;
296         char *begin;
297         struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
298         struct ldb_context *ldb = kd->ldb;
299
300         mypath = talloc_strdup(mem_ctx, path);
301         if (mypath == NULL) {
302                 return NULL;
303         }
304
305         ret = ldb_dn_new(mem_ctx, ldb, add);
306         if (!ldb_dn_validate(ret)) {
307                 talloc_free(ret);
308                 return NULL;
309         }
310
311         if (!ldb_dn_add_base(ret, kd->dn)) {
312                 talloc_free(ret);
313                 return NULL;
314         }
315
316         while (mypath[0] != '\0') {
317                 begin = strchr(mypath, '\\');
318                 if (begin != NULL) {
319                         *begin = '\0';
320                 }
321
322                 if (!ldb_dn_add_child_fmt(ret, "key=%s",
323                                           reg_ldb_escape(mem_ctx, mypath))) {
324                         talloc_free(ret);
325                         return NULL;
326                 }
327
328                 if (begin != NULL) {
329                         mypath = begin + 1;
330                 } else {
331                         break;
332                 }
333         }
334
335         return ret;
336 }
337
338 static WERROR cache_subkeys(struct ldb_key_data *kd)
339 {
340         struct ldb_context *c = kd->ldb;
341         struct ldb_result *res;
342         int ret;
343
344         ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
345                          NULL, "(key=*)");
346         if (ret != LDB_SUCCESS) {
347                 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
348                         ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
349                 return WERR_FOOBAR;
350         }
351
352         kd->subkey_count = res->count;
353         kd->subkeys = talloc_steal(kd, res->msgs);
354         talloc_free(res);
355
356         return WERR_OK;
357 }
358
359 static WERROR cache_values(struct ldb_key_data *kd)
360 {
361         struct ldb_context *c = kd->ldb;
362         struct ldb_result *res;
363         int ret;
364
365         ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
366                          NULL, "(value=*)");
367         if (ret != LDB_SUCCESS) {
368                 DEBUG(0, ("Error getting values for '%s': %s\n",
369                         ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
370                 return WERR_FOOBAR;
371         }
372
373         kd->value_count = res->count;
374         kd->values = talloc_steal(kd, res->msgs);
375         talloc_free(res);
376
377         return WERR_OK;
378 }
379
380
381 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
382                                    const struct hive_key *k, uint32_t idx,
383                                    const char **name,
384                                    const char **classname,
385                                    NTTIME *last_mod_time)
386 {
387         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
388
389         /* Initialization */
390         if (name != NULL)
391                 *name = NULL;
392         if (classname != NULL)
393                 *classname = NULL;
394         if (last_mod_time != NULL)
395                 *last_mod_time = 0; /* TODO: we need to add this to the
396                                                 ldb backend properly */
397
398         /* Do a search if necessary */
399         if (kd->subkeys == NULL) {
400                 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
401         }
402
403         if (idx >= kd->subkey_count)
404                 return WERR_NO_MORE_ITEMS;
405
406         if (name != NULL)
407                 *name = talloc_strdup(mem_ctx,
408                                       ldb_msg_find_attr_as_string(kd->subkeys[idx], "key", NULL));
409         if (classname != NULL)
410                 *classname = talloc_strdup(mem_ctx,
411                                            ldb_msg_find_attr_as_string(kd->subkeys[idx], "classname", NULL));
412
413         return WERR_OK;
414 }
415
416 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx,
417                                     const struct hive_key *k,
418                                     const char **name, uint32_t *data_type,
419                                     DATA_BLOB *data)
420 {
421         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
422         struct ldb_context *c = kd->ldb;
423         const char* attrs[] = { "data", "type", NULL };
424         struct ldb_result *res;
425         int ret;
426
427         ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_BASE, attrs,
428                          NULL);
429
430         if (ret != LDB_SUCCESS) {
431                 DEBUG(0, ("Error getting default value for '%s': %s\n",
432                         ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
433                 return WERR_FOOBAR;
434         }
435
436         if (res->count == 0 || res->msgs[0]->num_elements == 0) {
437                 talloc_free(res);
438                 return WERR_FILE_NOT_FOUND;
439         }
440
441         if ((data_type != NULL) && (data != NULL)) {
442                 reg_ldb_unpack_value(mem_ctx, res->msgs[0], name, data_type,
443                                      data);
444         }
445
446         talloc_free(res);
447
448         return WERR_OK;
449 }
450
451 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
452                                   uint32_t idx, const char **name,
453                                   uint32_t *data_type, DATA_BLOB *data)
454 {
455         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
456
457         /* if the default value exists, give it back */
458         if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
459                 data))) {
460                 if (idx == 0)
461                         return WERR_OK;
462                 else
463                         --idx;
464         }
465
466         /* Do the search if necessary */
467         if (kd->values == NULL) {
468                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
469         }
470
471         if (idx >= kd->value_count)
472                 return WERR_NO_MORE_ITEMS;
473
474         reg_ldb_unpack_value(mem_ctx, kd->values[idx], name, data_type, data);
475
476         return WERR_OK;
477 }
478
479 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
480                             const char *name, uint32_t *data_type,
481                             DATA_BLOB *data)
482 {
483         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
484         const char *res_name;
485         uint32_t idx;
486
487         /* the default value was requested, give it back */
488         if (name[0] == '\0') {
489                 return ldb_get_default_value(mem_ctx, k, NULL, data_type, data);
490         }
491
492         /* Do the search if necessary */
493         if (kd->values == NULL) {
494                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
495         }
496
497         for (idx = 0; idx < kd->value_count; idx++) {
498                 res_name = ldb_msg_find_attr_as_string(kd->values[idx], "value",
499                                                        "");
500                 if (ldb_attr_cmp(name, res_name) == 0) {
501                         reg_ldb_unpack_value(mem_ctx, kd->values[idx], NULL,
502                                              data_type, data);
503                         return WERR_OK;
504                 }
505         }
506
507         return WERR_FILE_NOT_FOUND;
508 }
509
510 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
511                            const char *name, struct hive_key **key)
512 {
513         struct ldb_result *res;
514         struct ldb_dn *ldb_path;
515         int ret;
516         struct ldb_key_data *newkd;
517         struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
518         struct ldb_context *c = kd->ldb;
519
520         ldb_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
521         W_ERROR_HAVE_NO_MEMORY(ldb_path);
522
523         ret = ldb_search(c, mem_ctx, &res, ldb_path, LDB_SCOPE_BASE, NULL,
524                          NULL);
525
526         if (ret != LDB_SUCCESS) {
527                 DEBUG(3, ("Error opening key '%s': %s\n",
528                         ldb_dn_get_linearized(ldb_path), ldb_errstring(c)));
529                 return WERR_FOOBAR;
530         } else if (res->count == 0) {
531                 DEBUG(3, ("Key '%s' not found\n",
532                         ldb_dn_get_linearized(ldb_path)));
533                 talloc_free(res);
534                 return WERR_FILE_NOT_FOUND;
535         }
536
537         newkd = talloc_zero(mem_ctx, struct ldb_key_data);
538         W_ERROR_HAVE_NO_MEMORY(newkd);
539         newkd->key.ops = &reg_backend_ldb;
540         newkd->ldb = talloc_reference(newkd, kd->ldb);
541         newkd->dn = ldb_dn_copy(newkd, res->msgs[0]->dn);
542         newkd->classname = talloc_steal(newkd,
543                                         ldb_msg_find_attr_as_string(res->msgs[0], "classname", NULL));
544
545         talloc_free(res);
546
547         *key = (struct hive_key *)newkd;
548
549         return WERR_OK;
550 }
551
552 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
553                          struct auth_session_info *session_info,
554                          struct cli_credentials *credentials,
555                          struct tevent_context *ev_ctx,
556                          struct loadparm_context *lp_ctx,
557                          struct hive_key **k)
558 {
559         struct ldb_key_data *kd;
560         struct ldb_context *wrap;
561         struct ldb_message *attrs_msg;
562
563         if (location == NULL)
564                 return WERR_INVALID_PARAMETER;
565
566         wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
567                                 location, session_info, credentials, 0);
568
569         if (wrap == NULL) {
570                 DEBUG(1, (__FILE__": unable to connect\n"));
571                 return WERR_FOOBAR;
572         }
573
574         attrs_msg = ldb_msg_new(wrap);
575         W_ERROR_HAVE_NO_MEMORY(attrs_msg);
576         attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
577         W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
578         ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
579         ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
580
581         ldb_add(wrap, attrs_msg);
582
583         ldb_set_debug_stderr(wrap);
584
585         kd = talloc_zero(parent_ctx, struct ldb_key_data);
586         kd->key.ops = &reg_backend_ldb;
587         kd->ldb = talloc_reference(kd, wrap);
588         talloc_set_destructor (kd, reg_close_ldb_key);
589         kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
590
591         *k = (struct hive_key *)kd;
592
593         return WERR_OK;
594 }
595
596 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
597                           const char *name, const char *classname,
598                           struct security_descriptor *sd,
599                           struct hive_key **newkey)
600 {
601         struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
602         struct ldb_dn *ldb_path;
603         struct ldb_message *msg;
604         struct ldb_key_data *newkd;
605         int ret;
606
607         ldb_path = reg_path_to_ldb(mem_ctx, parent, name, NULL);
608         W_ERROR_HAVE_NO_MEMORY(ldb_path);
609
610         msg = ldb_msg_new(mem_ctx);
611         W_ERROR_HAVE_NO_MEMORY(msg);
612
613         msg->dn = ldb_path;
614
615         ldb_msg_add_string(msg, "key", name);
616         if (classname != NULL) {
617                 ldb_msg_add_string(msg, "classname", classname);
618         }
619
620         ret = ldb_add(parentkd->ldb, msg);
621
622         talloc_free(msg);
623
624         if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
625                 return WERR_ALREADY_EXISTS;
626         }
627
628         if (ret != LDB_SUCCESS) {
629                 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
630                 return WERR_FOOBAR;
631         }
632
633         DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(ldb_path)));
634
635         newkd = talloc_zero(mem_ctx, struct ldb_key_data);
636         W_ERROR_HAVE_NO_MEMORY(newkd);
637         newkd->ldb = talloc_reference(newkd, parentkd->ldb);
638         newkd->key.ops = &reg_backend_ldb;
639         newkd->dn = talloc_steal(newkd, ldb_path);
640         newkd->classname = talloc_steal(newkd, classname);
641
642         *newkey = (struct hive_key *)newkd;
643
644         /* reset cache */
645         talloc_free(parentkd->subkeys);
646         parentkd->subkeys = NULL;
647
648         return WERR_OK;
649 }
650
651 static WERROR ldb_del_value(TALLOC_CTX *mem_ctx, struct hive_key *key,
652                             const char *child)
653 {
654         int ret;
655         struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
656         struct ldb_message *msg;
657         struct ldb_dn *childdn;
658
659         if (child[0] == '\0') {
660                 /* default value */
661                 msg = talloc_zero(mem_ctx, struct ldb_message);
662                 W_ERROR_HAVE_NO_MEMORY(msg);
663                 msg->dn = ldb_dn_copy(msg, kd->dn);
664                 W_ERROR_HAVE_NO_MEMORY(msg->dn);
665                 ret = ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
666                 if (ret != LDB_SUCCESS) {
667                         return WERR_FOOBAR;
668                 }
669                 ret = ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE,
670                                         NULL);
671                 if (ret != LDB_SUCCESS) {
672                         return WERR_FOOBAR;
673                 }
674
675                 ret = ldb_modify(kd->ldb, msg);
676
677                 talloc_free(msg);
678
679                 if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
680                         return WERR_FILE_NOT_FOUND;
681                 } else if (ret != LDB_SUCCESS) {
682                         DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
683                         return WERR_FOOBAR;
684                 }
685         } else {
686                 /* normal value */
687                 childdn = ldb_dn_copy(kd->ldb, kd->dn);
688                 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
689                                   reg_ldb_escape(childdn, child)))
690                 {
691                         talloc_free(childdn);
692                         return WERR_FOOBAR;
693                 }
694
695                 ret = ldb_delete(kd->ldb, childdn);
696
697                 talloc_free(childdn);
698
699                 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
700                         return WERR_FILE_NOT_FOUND;
701                 } else if (ret != LDB_SUCCESS) {
702                         DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
703                         return WERR_FOOBAR;
704                 }
705         }
706
707         /* reset cache */
708         talloc_free(kd->values);
709         kd->values = NULL;
710
711         return WERR_OK;
712 }
713
714 static WERROR ldb_del_key(TALLOC_CTX *mem_ctx, const struct hive_key *key,
715                           const char *name)
716 {
717         unsigned int i;
718         int ret;
719         struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
720         struct ldb_dn *ldb_path;
721         struct ldb_context *c = parentkd->ldb;
722         struct ldb_result *res_keys;
723         struct ldb_result *res_vals;
724         WERROR werr;
725         struct hive_key *hk;
726
727         /* Verify key exists by opening it */
728         werr = ldb_open_key(mem_ctx, key, name, &hk);
729         if (!W_ERROR_IS_OK(werr)) {
730                 return werr;
731         }
732
733         ldb_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
734         W_ERROR_HAVE_NO_MEMORY(ldb_path);
735
736         /* Search for subkeys */
737         ret = ldb_search(c, mem_ctx, &res_keys, ldb_path, LDB_SCOPE_ONELEVEL,
738                          NULL, "(key=*)");
739
740         if (ret != LDB_SUCCESS) {
741                 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
742                       ldb_dn_get_linearized(ldb_path), ldb_errstring(c)));
743                 return WERR_FOOBAR;
744         }
745
746         /* Search for values */
747         ret = ldb_search(c, mem_ctx, &res_vals, ldb_path, LDB_SCOPE_ONELEVEL,
748                          NULL, "(value=*)");
749
750         if (ret != LDB_SUCCESS) {
751                 DEBUG(0, ("Error getting values for '%s': %s\n",
752                       ldb_dn_get_linearized(ldb_path), ldb_errstring(c)));
753                 return WERR_FOOBAR;
754         }
755
756         /* Start an explicit transaction */
757         ret = ldb_transaction_start(c);
758
759         if (ret != LDB_SUCCESS) {
760                 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
761                 return WERR_FOOBAR;
762         }
763
764         if (res_keys->count || res_vals->count)
765         {
766                 /* Delete any subkeys */
767                 for (i = 0; i < res_keys->count; i++)
768                 {
769                         werr = ldb_del_key(mem_ctx, hk,
770                                            ldb_msg_find_attr_as_string(
771                                                         res_keys->msgs[i],
772                                                         "key", NULL));
773                         if (!W_ERROR_IS_OK(werr)) {
774                                 ret = ldb_transaction_cancel(c);
775                                 return werr;
776                         }
777                 }
778
779                 /* Delete any values */
780                 for (i = 0; i < res_vals->count; i++)
781                 {
782                         werr = ldb_del_value(mem_ctx, hk,
783                                              ldb_msg_find_attr_as_string(
784                                                         res_vals->msgs[i],
785                                                         "value", NULL));
786                         if (!W_ERROR_IS_OK(werr)) {
787                                 ret = ldb_transaction_cancel(c);
788                                 return werr;
789                         }
790                 }
791         }
792         talloc_free(res_keys);
793         talloc_free(res_vals);
794
795         /* Delete the key itself */
796         ret = ldb_delete(c, ldb_path);
797
798         if (ret != LDB_SUCCESS)
799         {
800                 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
801                 ret = ldb_transaction_cancel(c);
802                 return WERR_FOOBAR;
803         }
804
805         /* Commit the transaction */
806         ret = ldb_transaction_commit(c);
807
808         if (ret != LDB_SUCCESS)
809         {
810                 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
811                 ret = ldb_transaction_cancel(c);
812                 return WERR_FOOBAR;
813         }
814
815         /* reset cache */
816         talloc_free(parentkd->subkeys);
817         parentkd->subkeys = NULL;
818
819         return WERR_OK;
820 }
821
822 static WERROR ldb_set_value(struct hive_key *parent,
823                             const char *name, uint32_t type,
824                             const DATA_BLOB data)
825 {
826         struct ldb_message *msg;
827         struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
828         unsigned int i;
829         int ret;
830         TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
831
832         msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
833         W_ERROR_HAVE_NO_MEMORY(msg);
834
835         msg->dn = ldb_dn_copy(msg, kd->dn);
836         W_ERROR_HAVE_NO_MEMORY(msg->dn);
837
838         if (name[0] != '\0') {
839                 /* For a default value, we add/overwrite the attributes to/of the hive.
840                    For a normal value, we create a new child. */
841                 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
842                                   reg_ldb_escape(mem_ctx, name)))
843                 {
844                         talloc_free(mem_ctx);
845                         return WERR_FOOBAR;
846                 }
847         }
848
849         /* Try first a "modify" and if this doesn't work do try an "add" */
850         for (i = 0; i < msg->num_elements; i++) {
851                 if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE) {
852                         msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
853                 }
854         }
855         ret = ldb_modify(kd->ldb, msg);
856         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
857                 i = 0;
858                 while (i < msg->num_elements) {
859                         if (LDB_FLAG_MOD_TYPE(msg->elements[i].flags) == LDB_FLAG_MOD_DELETE) {
860                                 ldb_msg_remove_element(msg, &msg->elements[i]);
861                         } else {
862                                 ++i;
863                         }
864                 }
865                 ret = ldb_add(kd->ldb, msg);
866         }
867         if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
868                 /* ignore this -> the value didn't exist and also now doesn't */
869                 ret = LDB_SUCCESS;
870         }
871
872         talloc_free(msg);
873
874         if (ret != LDB_SUCCESS) {
875                 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
876                 talloc_free(mem_ctx);
877                 return WERR_FOOBAR;
878         }
879
880         /* reset cache */
881         talloc_free(kd->values);
882         kd->values = NULL;
883
884         talloc_free(mem_ctx);
885         return WERR_OK;
886 }
887
888 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
889                                const struct hive_key *key,
890                                const char **classname,
891                                uint32_t *num_subkeys,
892                                uint32_t *num_values,
893                                NTTIME *last_change_time,
894                                uint32_t *max_subkeynamelen,
895                                uint32_t *max_valnamelen,
896                                uint32_t *max_valbufsize)
897 {
898         struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
899         uint32_t default_value_type = REG_NONE;
900         DATA_BLOB default_value = { NULL, 0 };
901         WERROR werr;
902
903         /* Initialization */
904         if (classname != NULL)
905                 *classname = NULL;
906         if (num_subkeys != NULL)
907                 *num_subkeys = 0;
908         if (num_values != NULL)
909                 *num_values = 0;
910         if (last_change_time != NULL)
911                 *last_change_time = 0;
912         if (max_subkeynamelen != NULL)
913                 *max_subkeynamelen = 0;
914         if (max_valnamelen != NULL)
915                 *max_valnamelen = 0;
916         if (max_valbufsize != NULL)
917                 *max_valbufsize = 0;
918
919         /* We need this to get the default value (if it exists) for counting
920          * the values under the key and for finding out the longest value buffer
921          * size. If no default value exists the DATA_BLOB "default_value" will
922          * remain { NULL, 0 }. */
923         werr = ldb_get_default_value(mem_ctx, key, NULL, &default_value_type,
924                                      &default_value);
925         if ((!W_ERROR_IS_OK(werr)) && (!W_ERROR_EQUAL(werr, WERR_FILE_NOT_FOUND))) {
926                 return werr;
927         }
928
929         if (kd->subkeys == NULL) {
930                 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
931         }
932         if (kd->values == NULL) {
933                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
934         }
935
936         if (classname != NULL) {
937                 *classname = kd->classname;
938         }
939
940         if (num_subkeys != NULL) {
941                 *num_subkeys = kd->subkey_count;
942         }
943         if (num_values != NULL) {
944                 *num_values = kd->value_count;
945                 /* also consider the default value if it exists */
946                 if (default_value.data != NULL) {
947                         ++(*num_values);
948                 }
949         }
950
951
952         if (max_subkeynamelen != NULL) {
953                 unsigned int i;
954                 struct ldb_message_element *el;
955
956                 for (i = 0; i < kd->subkey_count; i++) {
957                         el = ldb_msg_find_element(kd->subkeys[i], "key");
958                         *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
959                 }
960         }
961
962         if (max_valnamelen != NULL || max_valbufsize != NULL) {
963                 unsigned int i;
964                 struct ldb_message_element *el;
965                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
966
967                 /* also consider the default value if it exists */
968                 if ((max_valbufsize != NULL) && (default_value.data != NULL)) {
969                                 *max_valbufsize = MAX(*max_valbufsize,
970                                                       default_value.length);
971                 }
972
973                 for (i = 0; i < kd->value_count; i++) {
974                         if (max_valnamelen != NULL) {
975                                 el = ldb_msg_find_element(kd->values[i], "value");
976                                 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
977                         }
978
979                         if (max_valbufsize != NULL) {
980                                 uint32_t data_type;
981                                 DATA_BLOB data;
982                                 reg_ldb_unpack_value(mem_ctx,
983                                                      kd->values[i], NULL,
984                                                      &data_type, &data);
985                                 *max_valbufsize = MAX(*max_valbufsize, data.length);
986                                 talloc_free(data.data);
987                         }
988                 }
989         }
990
991         talloc_free(default_value.data);
992
993         return WERR_OK;
994 }
995
996 static struct hive_operations reg_backend_ldb = {
997         .name = "ldb",
998         .add_key = ldb_add_key,
999         .del_key = ldb_del_key,
1000         .get_key_by_name = ldb_open_key,
1001         .enum_value = ldb_get_value_by_id,
1002         .enum_key = ldb_get_subkey_by_id,
1003         .set_value = ldb_set_value,
1004         .get_value_by_name = ldb_get_value,
1005         .delete_value = ldb_del_value,
1006         .get_key_info = ldb_get_key_info,
1007 };