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