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