s4:lib/registry/ldb.c - use "ldb_path" rather than "ldap_path" as LDB key varibale...
[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 "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 = talloc_strdup(mem_ctx, path);
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         ret = ldb_dn_new(mem_ctx, ldb, add);
286         if (!ldb_dn_validate(ret)) {
287                 talloc_free(ret);
288                 return NULL;
289         }
290
291         while (mypath) {
292                 char *keyname;
293
294                 begin = strrchr(mypath, '\\');
295
296                 if (begin) keyname = begin + 1;
297                 else keyname = mypath;
298
299                 if (keyname[0] != '\0') {
300                         if (!ldb_dn_add_base_fmt(ret, "key=%s",
301                                                  reg_ldb_escape(mem_ctx,
302                                                                 keyname)))
303                         {
304                                 talloc_free(ret);
305                                 return NULL;
306                         }
307                 }
308
309                 if(begin) {
310                         *begin = '\0';
311                 } else {
312                         break;
313                 }
314         }
315
316         ldb_dn_add_base(ret, kd->dn);
317
318         return ret;
319 }
320
321 static WERROR cache_subkeys(struct ldb_key_data *kd)
322 {
323         struct ldb_context *c = kd->ldb;
324         struct ldb_result *res;
325         int ret;
326
327         ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
328                          NULL, "(key=*)");
329         if (ret != LDB_SUCCESS) {
330                 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
331                         ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
332                 return WERR_FOOBAR;
333         }
334
335         kd->subkey_count = res->count;
336         kd->subkeys = talloc_steal(kd, res->msgs);
337         talloc_free(res);
338
339         return WERR_OK;
340 }
341
342 static WERROR cache_values(struct ldb_key_data *kd)
343 {
344         struct ldb_context *c = kd->ldb;
345         struct ldb_result *res;
346         int ret;
347
348         ret = ldb_search(c, c, &res, kd->dn, LDB_SCOPE_ONELEVEL,
349                          NULL, "(value=*)");
350         if (ret != LDB_SUCCESS) {
351                 DEBUG(0, ("Error getting values for '%s': %s\n",
352                         ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
353                 return WERR_FOOBAR;
354         }
355
356         kd->value_count = res->count;
357         kd->values = talloc_steal(kd, res->msgs);
358         talloc_free(res);
359
360         return WERR_OK;
361 }
362
363
364 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
365                                    const struct hive_key *k, uint32_t idx,
366                                    const char **name,
367                                    const char **classname,
368                                    NTTIME *last_mod_time)
369 {
370         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
371
372         /* Initialization */
373         if (name != NULL)
374                 *name = NULL;
375         if (classname != NULL)
376                 *classname = NULL;
377         if (last_mod_time != NULL)
378                 *last_mod_time = 0; /* TODO: we need to add this to the
379                                                 ldb backend properly */
380
381         /* Do a search if necessary */
382         if (kd->subkeys == NULL) {
383                 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
384         }
385
386         if (idx >= kd->subkey_count)
387                 return WERR_NO_MORE_ITEMS;
388
389         if (name != NULL)
390                 *name = talloc_strdup(mem_ctx,
391                                       ldb_msg_find_attr_as_string(kd->subkeys[idx], "key", NULL));
392         if (classname != NULL)
393                 *classname = talloc_strdup(mem_ctx,
394                                            ldb_msg_find_attr_as_string(kd->subkeys[idx], "classname", NULL));
395
396         return WERR_OK;
397 }
398
399 static WERROR ldb_get_default_value(TALLOC_CTX *mem_ctx,
400                                     const struct hive_key *k,
401                                     const char **name, uint32_t *data_type,
402                                     DATA_BLOB *data)
403 {
404         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
405         struct ldb_context *c = kd->ldb;
406         const char* attrs[] = { "data", "type", NULL };
407         struct ldb_result *res;
408         int ret;
409
410         ret = ldb_search(c, mem_ctx, &res, kd->dn, LDB_SCOPE_BASE, attrs, "(dn=*)");
411
412         if (ret != LDB_SUCCESS) {
413                 DEBUG(0, ("Error getting default value for '%s': %s\n",
414                         ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
415                 return WERR_FOOBAR;
416         }
417
418         if (res->count == 0 || res->msgs[0]->num_elements == 0)
419                 return WERR_BADFILE;
420
421         if ((data_type != NULL) && (data != NULL)) {
422                 reg_ldb_unpack_value(mem_ctx, res->msgs[0], name, data_type,
423                                      data);
424         }
425
426         talloc_free(res);
427
428         return WERR_OK;
429 }
430
431 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
432                                   uint32_t idx, const char **name,
433                                   uint32_t *data_type, DATA_BLOB *data)
434 {
435         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
436
437         /* if the default value exists, give it back */
438         if (W_ERROR_IS_OK(ldb_get_default_value(mem_ctx, k, name, data_type,
439                 data))) {
440                 if (idx == 0)
441                         return WERR_OK;
442                 else
443                         --idx;
444         }
445
446         /* Do the search if necessary */
447         if (kd->values == NULL) {
448                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
449         }
450
451         if (idx >= kd->value_count)
452                 return WERR_NO_MORE_ITEMS;
453
454         reg_ldb_unpack_value(mem_ctx, kd->values[idx], name, data_type, data);
455
456         return WERR_OK;
457 }
458
459 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
460                             const char *name, uint32_t *data_type,
461                             DATA_BLOB *data)
462 {
463         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
464         const char *res_name;
465         uint32_t idx;
466
467         if (name == NULL) {
468                 return WERR_INVALID_PARAM;
469         }
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         if (name == NULL) {
505                 return WERR_INVALID_PARAM;
506         }
507
508         ldb_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
509         W_ERROR_HAVE_NO_MEMORY(ldb_path);
510
511         ret = ldb_search(c, mem_ctx, &res, ldb_path, LDB_SCOPE_BASE, NULL, "(key=*)");
512
513         if (ret != LDB_SUCCESS) {
514                 DEBUG(3, ("Error opening key '%s': %s\n",
515                         ldb_dn_get_linearized(ldb_path), ldb_errstring(c)));
516                 return WERR_FOOBAR;
517         } else if (res->count == 0) {
518                 DEBUG(3, ("Key '%s' not found\n",
519                         ldb_dn_get_linearized(ldb_path)));
520                 talloc_free(res);
521                 return WERR_BADFILE;
522         }
523
524         newkd = talloc_zero(mem_ctx, struct ldb_key_data);
525         W_ERROR_HAVE_NO_MEMORY(newkd);
526         newkd->key.ops = &reg_backend_ldb;
527         newkd->ldb = talloc_reference(newkd, kd->ldb);
528         newkd->dn = ldb_dn_copy(newkd, res->msgs[0]->dn);
529         newkd->classname = talloc_steal(newkd,
530                                         ldb_msg_find_attr_as_string(res->msgs[0], "classname", NULL));
531
532         talloc_free(res);
533
534         *key = (struct hive_key *)newkd;
535
536         return WERR_OK;
537 }
538
539 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
540                          struct auth_session_info *session_info,
541                          struct cli_credentials *credentials,
542                          struct tevent_context *ev_ctx,
543                          struct loadparm_context *lp_ctx,
544                          struct hive_key **k)
545 {
546         struct ldb_key_data *kd;
547         struct ldb_context *wrap;
548         struct ldb_message *attrs_msg;
549
550         if (location == NULL)
551                 return WERR_INVALID_PARAM;
552
553         wrap = ldb_wrap_connect(parent_ctx, ev_ctx, lp_ctx,
554                                 location, session_info, credentials, 0);
555
556         if (wrap == NULL) {
557                 DEBUG(1, (__FILE__": unable to connect\n"));
558                 return WERR_FOOBAR;
559         }
560
561         attrs_msg = ldb_msg_new(wrap);
562         W_ERROR_HAVE_NO_MEMORY(attrs_msg);
563         attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
564         W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
565         ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
566         ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
567
568         ldb_add(wrap, attrs_msg);
569
570         ldb_set_debug_stderr(wrap);
571
572         kd = talloc_zero(parent_ctx, struct ldb_key_data);
573         kd->key.ops = &reg_backend_ldb;
574         kd->ldb = talloc_reference(kd, wrap);
575         talloc_set_destructor (kd, reg_close_ldb_key);
576         kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
577
578         *k = (struct hive_key *)kd;
579
580         return WERR_OK;
581 }
582
583 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
584                           const char *name, const char *classname,
585                           struct security_descriptor *sd,
586                           struct hive_key **newkey)
587 {
588         struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
589         struct ldb_dn *ldb_path;
590         struct ldb_message *msg;
591         struct ldb_key_data *newkd;
592         int ret;
593
594         if (name == NULL) {
595                 return WERR_INVALID_PARAM;
596         }
597
598         ldb_path = reg_path_to_ldb(mem_ctx, parent, name, NULL);
599         W_ERROR_HAVE_NO_MEMORY(ldb_path);
600
601         msg = ldb_msg_new(mem_ctx);
602         W_ERROR_HAVE_NO_MEMORY(msg);
603
604         msg->dn = ldb_path;
605
606         ldb_msg_add_string(msg, "key", name);
607         if (classname != NULL) {
608                 ldb_msg_add_string(msg, "classname", classname);
609         }
610
611         ret = ldb_add(parentkd->ldb, msg);
612
613         talloc_free(msg);
614
615         if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
616                 return WERR_ALREADY_EXISTS;
617         }
618
619         if (ret != LDB_SUCCESS) {
620                 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
621                 return WERR_FOOBAR;
622         }
623
624         DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(ldb_path)));
625
626         newkd = talloc_zero(mem_ctx, struct ldb_key_data);
627         W_ERROR_HAVE_NO_MEMORY(newkd);
628         newkd->ldb = talloc_reference(newkd, parentkd->ldb);
629         newkd->key.ops = &reg_backend_ldb;
630         newkd->dn = talloc_steal(newkd, ldb_path);
631         newkd->classname = talloc_steal(newkd, classname);
632
633         *newkey = (struct hive_key *)newkd;
634
635         /* reset cache */
636         talloc_free(parentkd->subkeys);
637         parentkd->subkeys = NULL;
638
639         return WERR_OK;
640 }
641
642 static WERROR ldb_del_value(TALLOC_CTX *mem_ctx, struct hive_key *key,
643                             const char *child)
644 {
645         int ret;
646         struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
647         struct ldb_message *msg;
648         struct ldb_dn *childdn;
649
650         if (child == NULL) {
651                 return WERR_INVALID_PARAM;
652         }
653
654         if (child[0] == '\0') {
655                 /* default value */
656                 msg = talloc_zero(mem_ctx, struct ldb_message);
657                 W_ERROR_HAVE_NO_MEMORY(msg);
658                 msg->dn = ldb_dn_copy(msg, kd->dn);
659                 W_ERROR_HAVE_NO_MEMORY(msg->dn);
660                 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
661                 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
662
663                 ret = ldb_modify(kd->ldb, msg);
664                 if (ret != LDB_SUCCESS) {
665                         DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
666                         return WERR_FOOBAR;
667                 }
668         } else {
669                 /* normal value */
670                 childdn = ldb_dn_copy(kd->ldb, kd->dn);
671                 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
672                                   reg_ldb_escape(childdn, child)))
673                 {
674                         talloc_free(childdn);
675                         return WERR_FOOBAR;
676                 }
677
678                 ret = ldb_delete(kd->ldb, childdn);
679
680                 talloc_free(childdn);
681
682                 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
683                         return WERR_BADFILE;
684                 } else if (ret != LDB_SUCCESS) {
685                         DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
686                         return WERR_FOOBAR;
687                 }
688         }
689
690         /* reset cache */
691         talloc_free(kd->values);
692         kd->values = NULL;
693
694         return WERR_OK;
695 }
696
697 static WERROR ldb_del_key(TALLOC_CTX *mem_ctx, const struct hive_key *key,
698                           const char *name)
699 {
700         unsigned int i;
701         int ret;
702         struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
703         struct ldb_dn *ldb_path;
704         struct ldb_context *c = parentkd->ldb;
705         struct ldb_result *res_keys;
706         struct ldb_result *res_vals;
707         WERROR werr;
708         struct hive_key *hk;
709
710         if (name == NULL) {
711                 return WERR_INVALID_PARAM;
712         }
713
714         /* Verify key exists by opening it */
715         werr = ldb_open_key(mem_ctx, key, name, &hk);
716         if (!W_ERROR_IS_OK(werr)) {
717                 return werr;
718         }
719
720         ldb_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
721         W_ERROR_HAVE_NO_MEMORY(ldb_path);
722
723         /* Search for subkeys */
724         ret = ldb_search(c, mem_ctx, &res_keys, ldb_path, LDB_SCOPE_ONELEVEL,
725                          NULL, "(key=*)");
726
727         if (ret != LDB_SUCCESS) {
728                 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
729                       ldb_dn_get_linearized(ldb_path), ldb_errstring(c)));
730                 return WERR_FOOBAR;
731         }
732
733         /* Search for values */
734         ret = ldb_search(c, mem_ctx, &res_vals, ldb_path, LDB_SCOPE_ONELEVEL,
735                          NULL, "(value=*)");
736
737         if (ret != LDB_SUCCESS) {
738                 DEBUG(0, ("Error getting values for '%s': %s\n",
739                       ldb_dn_get_linearized(ldb_path), ldb_errstring(c)));
740                 return WERR_FOOBAR;
741         }
742
743         /* Start an explicit transaction */
744         ret = ldb_transaction_start(c);
745
746         if (ret != LDB_SUCCESS) {
747                 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
748                 return WERR_FOOBAR;
749         }
750
751         if (res_keys->count || res_vals->count)
752         {
753                 /* Delete any subkeys */
754                 for (i = 0; i < res_keys->count; i++)
755                 {
756                         werr = ldb_del_key(mem_ctx, hk,
757                                            ldb_msg_find_attr_as_string(
758                                                         res_keys->msgs[i],
759                                                         "key", NULL));
760                         if (!W_ERROR_IS_OK(werr)) {
761                                 ret = ldb_transaction_cancel(c);
762                                 return werr;
763                         }
764                 }
765
766                 /* Delete any values */
767                 for (i = 0; i < res_vals->count; i++)
768                 {
769                         werr = ldb_del_value(mem_ctx, hk,
770                                              ldb_msg_find_attr_as_string(
771                                                         res_vals->msgs[i],
772                                                         "value", NULL));
773                         if (!W_ERROR_IS_OK(werr)) {
774                                 ret = ldb_transaction_cancel(c);
775                                 return werr;
776                         }
777                 }
778         }
779
780         /* Delete the key itself */
781         ret = ldb_delete(c, ldb_path);
782
783         if (ret != LDB_SUCCESS)
784         {
785                 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
786                 ret = ldb_transaction_cancel(c);
787                 return WERR_FOOBAR;
788         }
789
790         /* Commit the transaction */
791         ret = ldb_transaction_commit(c);
792
793         if (ret != LDB_SUCCESS)
794         {
795                 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
796                 ret = ldb_transaction_cancel(c);
797                 return WERR_FOOBAR;
798         }
799
800         /* reset cache */
801         talloc_free(parentkd->subkeys);
802         parentkd->subkeys = NULL;
803
804         return WERR_OK;
805 }
806
807 static WERROR ldb_set_value(struct hive_key *parent,
808                             const char *name, uint32_t type,
809                             const DATA_BLOB data)
810 {
811         struct ldb_message *msg;
812         struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
813         unsigned int i;
814         int ret;
815         TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
816
817         if (name == NULL) {
818                 return WERR_INVALID_PARAM;
819         }
820
821         msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
822         W_ERROR_HAVE_NO_MEMORY(msg);
823
824         msg->dn = ldb_dn_copy(msg, kd->dn);
825         W_ERROR_HAVE_NO_MEMORY(msg->dn);
826
827         if (name[0] != '\0') {
828                 /* For a default value, we add/overwrite the attributes to/of the hive.
829                    For a normal value, we create a new child. */
830                 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
831                                   reg_ldb_escape(mem_ctx, name)))
832                 {
833                         talloc_free(mem_ctx);
834                         return WERR_FOOBAR;
835                 }
836         }
837
838         /* Try first a "modify" and if this doesn't work do try an "add" */
839         for (i = 0; i < msg->num_elements; i++) {
840                 if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE) {
841                         msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
842                 }
843         }
844         ret = ldb_modify(kd->ldb, msg);
845         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
846                 i = 0;
847                 while (i < msg->num_elements) {
848                         if (msg->elements[i].flags == LDB_FLAG_MOD_DELETE) {
849                                 ldb_msg_remove_element(msg, &msg->elements[i]);
850                         } else {
851                                 ++i;
852                         }
853                 }
854                 ret = ldb_add(kd->ldb, msg);
855         }
856         if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
857                 /* ignore this -> the value didn't exist and also now doesn't */
858                 ret = LDB_SUCCESS;
859         }
860
861         if (ret != LDB_SUCCESS) {
862                 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
863                 talloc_free(mem_ctx);
864                 return WERR_FOOBAR;
865         }
866
867         /* reset cache */
868         talloc_free(kd->values);
869         kd->values = NULL;
870
871         talloc_free(mem_ctx);
872         return WERR_OK;
873 }
874
875 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
876                                const struct hive_key *key,
877                                const char **classname,
878                                uint32_t *num_subkeys,
879                                uint32_t *num_values,
880                                NTTIME *last_change_time,
881                                uint32_t *max_subkeynamelen,
882                                uint32_t *max_valnamelen,
883                                uint32_t *max_valbufsize)
884 {
885         struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
886         uint32_t default_value_type = REG_NONE;
887         DATA_BLOB default_value = { NULL, 0 };
888         WERROR werr;
889
890         /* Initialization */
891         if (classname != NULL)
892                 *classname = NULL;
893         if (num_subkeys != NULL)
894                 *num_subkeys = 0;
895         if (num_values != NULL)
896                 *num_values = 0;
897         if (last_change_time != NULL)
898                 *last_change_time = 0;
899         if (max_subkeynamelen != NULL)
900                 *max_subkeynamelen = 0;
901         if (max_valnamelen != NULL)
902                 *max_valnamelen = 0;
903         if (max_valbufsize != NULL)
904                 *max_valbufsize = 0;
905
906         /* We need this to get the default value (if it exists) for counting
907          * the values under the key and for finding out the longest value buffer
908          * size. If no default value exists the DATA_BLOB "default_value" will
909          * remain { NULL, 0 }. */
910         werr = ldb_get_default_value(mem_ctx, key, NULL, &default_value_type,
911                                      &default_value);
912         if ((!W_ERROR_IS_OK(werr)) && (!W_ERROR_EQUAL(werr, WERR_BADFILE))) {
913                 return werr;
914         }
915
916         if (kd->subkeys == NULL) {
917                 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
918         }
919         if (kd->values == NULL) {
920                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
921         }
922
923         if (classname != NULL) {
924                 *classname = kd->classname;
925         }
926
927         if (num_subkeys != NULL) {
928                 *num_subkeys = kd->subkey_count;
929         }
930         if (num_values != NULL) {
931                 *num_values = kd->value_count;
932                 /* also consider the default value if it exists */
933                 if (default_value.data != NULL) {
934                         ++(*num_values);
935                 }
936         }
937
938
939         if (max_subkeynamelen != NULL) {
940                 unsigned int i;
941                 struct ldb_message_element *el;
942
943                 for (i = 0; i < kd->subkey_count; i++) {
944                         el = ldb_msg_find_element(kd->subkeys[i], "key");
945                         *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
946                 }
947         }
948
949         if (max_valnamelen != NULL || max_valbufsize != NULL) {
950                 unsigned int i;
951                 struct ldb_message_element *el;
952                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
953
954                 /* also consider the default value if it exists */
955                 if ((max_valbufsize != NULL) && (default_value.data != NULL)) {
956                                 *max_valbufsize = MAX(*max_valbufsize,
957                                                       default_value.length);
958                 }
959
960                 for (i = 0; i < kd->value_count; i++) {
961                         if (max_valnamelen != NULL) {
962                                 el = ldb_msg_find_element(kd->values[i], "value");
963                                 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
964                         }
965
966                         if (max_valbufsize != NULL) {
967                                 uint32_t data_type;
968                                 DATA_BLOB data;
969                                 reg_ldb_unpack_value(mem_ctx,
970                                                      kd->values[i], NULL,
971                                                      &data_type, &data);
972                                 *max_valbufsize = MAX(*max_valbufsize, data.length);
973                                 talloc_free(data.data);
974                         }
975                 }
976         }
977
978         talloc_free(default_value.data);
979
980         return WERR_OK;
981 }
982
983 static struct hive_operations reg_backend_ldb = {
984         .name = "ldb",
985         .add_key = ldb_add_key,
986         .del_key = ldb_del_key,
987         .get_key_by_name = ldb_open_key,
988         .enum_value = ldb_get_value_by_id,
989         .enum_key = ldb_get_subkey_by_id,
990         .set_value = ldb_set_value,
991         .get_value_by_name = ldb_get_value,
992         .delete_value = ldb_del_value,
993         .get_key_info = ldb_get_key_info,
994 };