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