s4:registry - adaptions for "add memory contexts for delete value/key functions"
[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                         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(TALLOC_CTX *mem_ctx, struct hive_key *key,
716                             const char *child)
717 {
718         int ret;
719         struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
720         struct ldb_message *msg;
721         struct ldb_dn *childdn;
722
723         if ((child == NULL) || (child[0] == '\0')) {
724                 /* default value */
725                 msg = talloc_zero(mem_ctx, struct ldb_message);
726                 W_ERROR_HAVE_NO_MEMORY(msg);
727                 msg->dn = ldb_dn_copy(msg, kd->dn);
728                 W_ERROR_HAVE_NO_MEMORY(msg->dn);
729                 ldb_msg_add_empty(msg, "data", LDB_FLAG_MOD_DELETE, NULL);
730                 ldb_msg_add_empty(msg, "type", LDB_FLAG_MOD_DELETE, NULL);
731
732                 ret = ldb_modify(kd->ldb, msg);
733                 if (ret != LDB_SUCCESS) {
734                         DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
735                         return WERR_FOOBAR;
736                 }
737         } else {
738                 /* normal value */
739                 childdn = ldb_dn_copy(kd->ldb, kd->dn);
740                 if (!ldb_dn_add_child_fmt(childdn, "value=%s",
741                                   reg_ldb_escape(childdn, child)))
742                 {
743                         talloc_free(childdn);
744                         return WERR_FOOBAR;
745                 }
746
747                 ret = ldb_delete(kd->ldb, childdn);
748
749                 talloc_free(childdn);
750
751                 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
752                         return WERR_BADFILE;
753                 } else if (ret != LDB_SUCCESS) {
754                         DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
755                         return WERR_FOOBAR;
756                 }
757         }
758
759         /* reset cache */
760         talloc_free(kd->values);
761         kd->values = NULL;
762
763         return WERR_OK;
764 }
765
766 static WERROR ldb_del_key(TALLOC_CTX *mem_ctx, const struct hive_key *key,
767                           const char *name)
768 {
769         unsigned int i;
770         int ret;
771         struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
772         struct ldb_dn *ldap_path;
773         struct ldb_context *c = parentkd->ldb;
774         struct ldb_result *res_keys;
775         struct ldb_result *res_vals;
776         WERROR werr;
777         struct hive_key *hk;
778
779         /* Verify key exists by opening it */
780         werr = ldb_open_key(mem_ctx, key, name, &hk);
781         if (!W_ERROR_IS_OK(werr)) {
782                 return werr;
783         }
784
785         ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
786         W_ERROR_HAVE_NO_MEMORY(ldap_path);
787
788         /* Search for subkeys */
789         ret = ldb_search(c, mem_ctx, &res_keys, ldap_path, LDB_SCOPE_ONELEVEL,
790                          NULL, "(key=*)");
791
792         if (ret != LDB_SUCCESS) {
793                 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
794                       ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
795                 return WERR_FOOBAR;
796         }
797
798         /* Search for values */
799         ret = ldb_search(c, mem_ctx, &res_vals, ldap_path, LDB_SCOPE_ONELEVEL,
800                          NULL, "(value=*)");
801
802         if (ret != LDB_SUCCESS) {
803                 DEBUG(0, ("Error getting values for '%s': %s\n",
804                       ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
805                 return WERR_FOOBAR;
806         }
807
808         /* Start an explicit transaction */
809         ret = ldb_transaction_start(c);
810
811         if (ret != LDB_SUCCESS) {
812                 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
813                 return WERR_FOOBAR;
814         }
815
816         if (res_keys->count || res_vals->count)
817         {
818                 /* Delete any subkeys */
819                 for (i = 0; i < res_keys->count; i++)
820                 {
821                         werr = ldb_del_key(mem_ctx, hk,
822                                            ldb_msg_find_attr_as_string(
823                                                         res_keys->msgs[i],
824                                                         "key", NULL));
825                         if (!W_ERROR_IS_OK(werr)) {
826                                 ret = ldb_transaction_cancel(c);
827                                 return werr;
828                         }
829                 }
830
831                 /* Delete any values */
832                 for (i = 0; i < res_vals->count; i++)
833                 {
834                         werr = ldb_del_value(mem_ctx, hk,
835                                              ldb_msg_find_attr_as_string(
836                                                         res_vals->msgs[i],
837                                                         "value", NULL));
838                         if (!W_ERROR_IS_OK(werr)) {
839                                 ret = ldb_transaction_cancel(c);
840                                 return werr;
841                         }
842                 }
843         }
844
845         /* Delete the key itself */
846         ret = ldb_delete(c, ldap_path);
847
848         if (ret != LDB_SUCCESS)
849         {
850                 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
851                 ret = ldb_transaction_cancel(c);
852                 return WERR_FOOBAR;
853         }
854
855         /* Commit the transaction */
856         ret = ldb_transaction_commit(c);
857
858         if (ret != LDB_SUCCESS)
859         {
860                 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
861                 ret = ldb_transaction_cancel(c);
862                 return WERR_FOOBAR;
863         }
864
865         /* reset cache */
866         talloc_free(parentkd->subkeys);
867         parentkd->subkeys = NULL;
868
869         return WERR_OK;
870 }
871
872 static WERROR ldb_set_value(struct hive_key *parent,
873                             const char *name, uint32_t type,
874                             const DATA_BLOB data)
875 {
876         struct ldb_message *msg;
877         struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
878         unsigned int i;
879         int ret;
880         TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
881
882         msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
883         W_ERROR_HAVE_NO_MEMORY(msg);
884
885         msg->dn = ldb_dn_copy(msg, kd->dn);
886         W_ERROR_HAVE_NO_MEMORY(msg->dn);
887
888         if ((name != NULL) && (name[0] != '\0')) {
889                 /* For a default value, we add/overwrite the attributes to/of the hive.
890                    For a normal value, we create a new child. */
891                 if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
892                                   reg_ldb_escape(mem_ctx, name)))
893                 {
894                         talloc_free(mem_ctx);
895                         return WERR_FOOBAR;
896                 }
897         }
898
899         /* Try first a "modify" and if this doesn't work do try an "add" */
900         for (i = 0; i < msg->num_elements; i++) {
901                 if (msg->elements[i].flags != LDB_FLAG_MOD_DELETE) {
902                         msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
903                 }
904         }
905         ret = ldb_modify(kd->ldb, msg);
906         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
907                 i = 0;
908                 while (i < msg->num_elements) {
909                         if (msg->elements[i].flags == LDB_FLAG_MOD_DELETE) {
910                                 ldb_msg_remove_element(msg, &msg->elements[i]);
911                         } else {
912                                 ++i;
913                         }
914                 }
915                 ret = ldb_add(kd->ldb, msg);
916         }
917         if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
918                 /* ignore this -> the value didn't exist and also now doesn't */
919                 ret = LDB_SUCCESS;
920         }
921
922         if (ret != LDB_SUCCESS) {
923                 DEBUG(1, ("ldb_set_value: %s\n", ldb_errstring(kd->ldb)));
924                 talloc_free(mem_ctx);
925                 return WERR_FOOBAR;
926         }
927
928         /* reset cache */
929         talloc_free(kd->values);
930         kd->values = NULL;
931
932         talloc_free(mem_ctx);
933         return WERR_OK;
934 }
935
936 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
937                                const struct hive_key *key,
938                                const char **classname,
939                                uint32_t *num_subkeys,
940                                uint32_t *num_values,
941                                NTTIME *last_change_time,
942                                uint32_t *max_subkeynamelen,
943                                uint32_t *max_valnamelen,
944                                uint32_t *max_valbufsize)
945 {
946         struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
947         uint32_t default_value_type = REG_NONE;
948         DATA_BLOB default_value = { NULL, 0 };
949         WERROR werr;
950
951         /* Initialization */
952         if (classname != NULL)
953                 *classname = NULL;
954         if (num_subkeys != NULL)
955                 *num_subkeys = 0;
956         if (num_values != NULL)
957                 *num_values = 0;
958         if (last_change_time != NULL)
959                 *last_change_time = 0;
960         if (max_subkeynamelen != NULL)
961                 *max_subkeynamelen = 0;
962         if (max_valnamelen != NULL)
963                 *max_valnamelen = 0;
964         if (max_valbufsize != NULL)
965                 *max_valbufsize = 0;
966
967         /* We need this to get the default value (if it exists) for counting
968          * the values under the key and for finding out the longest value buffer
969          * size. If no default value exists the DATA_BLOB "default_value" will
970          * remain { NULL, 0 }. */
971         werr = ldb_get_default_value(mem_ctx, key, NULL, &default_value_type,
972                                      &default_value);
973         if ((!W_ERROR_IS_OK(werr)) && (!W_ERROR_EQUAL(werr, WERR_BADFILE))) {
974                 return werr;
975         }
976
977         if (kd->subkeys == NULL) {
978                 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
979         }
980
981         if (kd->values == NULL) {
982                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
983         }
984
985         if (num_subkeys != NULL) {
986                 *num_subkeys = kd->subkey_count;
987         }
988         if (num_values != NULL) {
989                 *num_values = kd->value_count;
990                 /* also consider the default value if it exists */
991                 if (default_value.data != NULL) {
992                         ++(*num_values);
993                 }
994         }
995
996
997         if (max_subkeynamelen != NULL) {
998                 unsigned int i;
999                 struct ldb_message_element *el;
1000
1001                 for (i = 0; i < kd->subkey_count; i++) {
1002                         el = ldb_msg_find_element(kd->subkeys[i], "key");
1003                         *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
1004                 }
1005
1006                 /* for UTF16 encoding */
1007                 *max_subkeynamelen *= 2;
1008         }
1009
1010         if (max_valnamelen != NULL || max_valbufsize != NULL) {
1011                 unsigned int i;
1012                 struct ldb_message_element *el;
1013                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
1014
1015                 /* also consider the default value if it exists */
1016                 if ((max_valbufsize != NULL) && (default_value.data != NULL)) {
1017                                 *max_valbufsize = MAX(*max_valbufsize,
1018                                                       default_value.length);
1019                 }
1020
1021                 for (i = 0; i < kd->value_count; i++) {
1022                         if (max_valnamelen != NULL) {
1023                                 el = ldb_msg_find_element(kd->values[i], "value");
1024                                 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
1025                         }
1026
1027                         if (max_valbufsize != NULL) {
1028                                 uint32_t data_type;
1029                                 DATA_BLOB data;
1030                                 reg_ldb_unpack_value(mem_ctx,
1031                                                      kd->values[i], NULL,
1032                                                      &data_type, &data);
1033                                 *max_valbufsize = MAX(*max_valbufsize, data.length);
1034                                 talloc_free(data.data);
1035                         }
1036                 }
1037
1038                 if (max_valnamelen != NULL) {
1039                         /* for UTF16 encoding */
1040                         *max_valnamelen *= 2;
1041                 }
1042         }
1043
1044         talloc_free(default_value.data);
1045
1046         return WERR_OK;
1047 }
1048
1049 static struct hive_operations reg_backend_ldb = {
1050         .name = "ldb",
1051         .add_key = ldb_add_key,
1052         .del_key = ldb_del_key,
1053         .get_key_by_name = ldb_open_key,
1054         .enum_value = ldb_get_value_by_id,
1055         .enum_key = ldb_get_subkey_by_id,
1056         .set_value = ldb_set_value,
1057         .get_value_by_name = ldb_get_value,
1058         .delete_value = ldb_del_value,
1059         .get_key_info = ldb_get_key_info,
1060 };