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