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