Merge branch 'upstream'
[jelmer/samba4-debian.git] / source / lib / registry / ldb.c
1 /*
2    Unix SMB/CIFS implementation.
3    Registry interface
4    Copyright (C) Jelmer Vernooij  2004-2007.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "registry.h"
22 #include "lib/ldb/include/ldb.h"
23 #include "lib/ldb/include/ldb_errors.h"
24 #include "ldb_wrap.h"
25 #include "librpc/gen_ndr/winreg.h"
26 #include "param/param.h"
27
28 static struct hive_operations reg_backend_ldb;
29
30 struct ldb_key_data
31 {
32         struct hive_key key;
33         struct ldb_context *ldb;
34         struct ldb_dn *dn;
35         struct ldb_message **subkeys, **values;
36         int subkey_count, value_count;
37 };
38
39 static void reg_ldb_unpack_value(TALLOC_CTX *mem_ctx, 
40                                  struct smb_iconv_convenience *iconv_convenience,
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         value_type = ldb_msg_find_attr_as_uint(msg, "type", 0);
54         if (type != NULL)
55                 *type = value_type; 
56         val = ldb_msg_find_ldb_val(msg, "data");
57
58         switch (value_type)
59         {
60         case REG_SZ:
61         case REG_EXPAND_SZ:
62                 data->length = convert_string_talloc(mem_ctx, iconv_convenience, CH_UTF8, CH_UTF16,
63                                                      val->data, val->length,
64                                                      (void **)&data->data);
65                 break;
66
67         case REG_DWORD: {
68                 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
69                 *data = data_blob_talloc(mem_ctx, &tmp, 4);
70                 }
71                 break;
72
73         default:
74                 *data = data_blob_talloc(mem_ctx, val->data, val->length);
75                 break;
76         }
77 }
78
79 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
80                                               TALLOC_CTX *mem_ctx,
81                                               const char *name,
82                                               uint32_t type, DATA_BLOB data)
83 {
84         struct ldb_val val;
85         struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
86         char *type_s;
87
88         ldb_msg_add_string(msg, "value", talloc_strdup(mem_ctx, name));
89
90         switch (type) {
91         case REG_SZ:
92         case REG_EXPAND_SZ:
93                 val.length = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF16, CH_UNIX,
94                                                    (void *)data.data,
95                                                    data.length,
96                                                    (void **)&val.data);
97                 ldb_msg_add_value(msg, "data", &val, NULL);
98                 break;
99
100         case REG_DWORD:
101                 ldb_msg_add_string(msg, "data",
102                                    talloc_asprintf(mem_ctx, "0x%x",
103                                                    IVAL(data.data, 0)));
104                 break;
105         default:
106                 ldb_msg_add_value(msg, "data", &data, NULL);
107         }
108
109
110         type_s = talloc_asprintf(mem_ctx, "%u", type);
111         ldb_msg_add_string(msg, "type", type_s);
112
113         return msg;
114 }
115
116 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
117 {
118         struct ldb_val val;
119
120         val.data = discard_const_p(uint8_t, value);
121         val.length = strlen(value);
122
123         return ldb_dn_escape_value(mem_ctx, val);
124 }
125
126 static int reg_close_ldb_key(struct ldb_key_data *key)
127 {
128         if (key->subkeys != NULL) {
129                 talloc_free(key->subkeys);
130                 key->subkeys = NULL;
131         }
132
133         if (key->values != NULL) {
134                 talloc_free(key->values);
135                 key->values = NULL;
136         }
137         return 0;
138 }
139
140 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
141                                       const struct hive_key *from,
142                                       const char *path, const char *add)
143 {
144         TALLOC_CTX *local_ctx;
145         struct ldb_dn *ret;
146         char *mypath = talloc_strdup(mem_ctx, path);
147         char *begin;
148         struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
149         struct ldb_context *ldb = kd->ldb;
150
151         local_ctx = talloc_new(mem_ctx);
152
153         if (add) {
154                 ret = ldb_dn_new(mem_ctx, ldb, add);
155         } else {
156                 ret = ldb_dn_new(mem_ctx, ldb, NULL);
157         }
158         if (!ldb_dn_validate(ret)) {
159                 talloc_free(ret);
160                 talloc_free(local_ctx);
161                 return NULL;
162         }
163
164         while (mypath) {
165                 char *keyname;
166
167                 begin = strrchr(mypath, '\\');
168
169                 if (begin) keyname = begin + 1;
170                 else keyname = mypath;
171
172                 if(strlen(keyname)) {
173                         if (!ldb_dn_add_base_fmt(ret, "key=%s",
174                                                  reg_ldb_escape(local_ctx,
175                                                                 keyname)))
176                         {
177                                 talloc_free(local_ctx);
178                                 return NULL;
179                         }
180                 }
181
182                 if(begin) {
183                         *begin = '\0';
184                 } else {
185                         break;
186                 }
187         }
188
189         ldb_dn_add_base(ret, kd->dn);
190
191         talloc_free(local_ctx);
192
193         return ret;
194 }
195
196 static WERROR cache_subkeys(struct ldb_key_data *kd)
197 {
198         struct ldb_context *c = kd->ldb;
199         struct ldb_result *res;
200         int ret;
201
202         ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL, &res);
203
204         if (ret != LDB_SUCCESS) {
205                 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
206                         ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
207                 return WERR_FOOBAR;
208         }
209
210         kd->subkey_count = res->count;
211         kd->subkeys = talloc_steal(kd, res->msgs);
212         talloc_free(res);
213
214         return WERR_OK;
215 }
216
217 static WERROR cache_values(struct ldb_key_data *kd)
218 {
219         struct ldb_context *c = kd->ldb;
220         struct ldb_result *res;
221         int ret;
222
223         ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL,
224                          "(value=*)", NULL, &res);
225
226         if (ret != LDB_SUCCESS) {
227                 DEBUG(0, ("Error getting values for '%s': %s\n",
228                         ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
229                 return WERR_FOOBAR;
230         }
231         kd->value_count = res->count;
232         kd->values = talloc_steal(kd, res->msgs);
233         talloc_free(res);
234         return WERR_OK;
235 }
236
237
238 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
239                                    const struct hive_key *k, uint32_t idx,
240                                    const char **name,
241                                    const char **classname,
242                                    NTTIME *last_mod_time)
243 {
244         struct ldb_message_element *el;
245         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
246
247         /* Do a search if necessary */
248         if (kd->subkeys == NULL) {
249                 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
250         }
251
252         if (idx >= kd->subkey_count)
253                 return WERR_NO_MORE_ITEMS;
254
255         el = ldb_msg_find_element(kd->subkeys[idx], "key");
256         SMB_ASSERT(el != NULL);
257         SMB_ASSERT(el->num_values != 0);
258
259         if (name != NULL)
260                 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
261
262         if (classname != NULL)
263                 *classname = NULL; /* TODO: Store properly */
264
265         if (last_mod_time != NULL)
266                 *last_mod_time = 0; /* TODO: we need to add this to the
267                                                 ldb backend properly */
268
269         return WERR_OK;
270 }
271
272 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
273                                   int idx, const char **name,
274                                   uint32_t *data_type, DATA_BLOB *data)
275 {
276         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
277
278         /* Do the search if necessary */
279         if (kd->values == NULL) {
280                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
281         }
282
283         if (idx >= kd->value_count)
284                 return WERR_NO_MORE_ITEMS;
285
286         reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm), kd->values[idx],
287                              name, data_type, data);
288
289         return WERR_OK;
290 }
291
292 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
293                             const char *name, uint32_t *data_type,
294                             DATA_BLOB *data)
295 {
296         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
297         struct ldb_context *c = kd->ldb;
298         struct ldb_result *res;
299         int ret;
300         char *query = talloc_asprintf(mem_ctx, "(value=%s)", name);
301
302         ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, query, NULL, &res);
303
304         talloc_free(query);
305
306         if (ret != LDB_SUCCESS) {
307                 DEBUG(0, ("Error getting values for '%s': %s\n",
308                         ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
309                 return WERR_FOOBAR;
310         }
311
312         if (res->count == 0)
313                 return WERR_BADFILE;
314
315         reg_ldb_unpack_value(mem_ctx, lp_iconv_convenience(global_loadparm), res->msgs[0], NULL, data_type, data);
316
317         return WERR_OK;
318 }
319
320 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
321                            const char *name, struct hive_key **key)
322 {
323         struct ldb_result *res;
324         struct ldb_dn *ldap_path;
325         int ret;
326         struct ldb_key_data *newkd;
327         struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
328         struct ldb_context *c = kd->ldb;
329
330         ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
331
332         ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL, &res);
333
334         if (ret != LDB_SUCCESS) {
335                 DEBUG(3, ("Error opening key '%s': %s\n",
336                         ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
337                 return WERR_FOOBAR;
338         } else if (res->count == 0) {
339                 DEBUG(3, ("Key '%s' not found\n",
340                         ldb_dn_get_linearized(ldap_path)));
341                 talloc_free(res);
342                 return WERR_BADFILE;
343         }
344
345         newkd = talloc_zero(mem_ctx, struct ldb_key_data);
346         newkd->key.ops = &reg_backend_ldb;
347         newkd->ldb = talloc_reference(newkd, kd->ldb);
348         newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
349
350         *key = (struct hive_key *)newkd;
351
352         talloc_free(res);
353
354         return WERR_OK;
355 }
356
357 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
358                          struct auth_session_info *session_info,
359                          struct cli_credentials *credentials,
360                          struct loadparm_context *lp_ctx,
361                          struct hive_key **k)
362 {
363         struct ldb_key_data *kd;
364         struct ldb_context *wrap;
365         struct ldb_message *attrs_msg;
366
367         if (location == NULL)
368                 return WERR_INVALID_PARAM;
369
370         wrap = ldb_wrap_connect(parent_ctx, lp_ctx,
371                                 location, session_info, credentials, 0, NULL);
372
373         if (wrap == NULL) {
374                 DEBUG(1, (__FILE__": unable to connect\n"));
375                 return WERR_FOOBAR;
376         }
377
378         attrs_msg = ldb_msg_new(wrap);
379         W_ERROR_HAVE_NO_MEMORY(attrs_msg);
380         attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
381         W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
382         ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
383         ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
384
385         ldb_add(wrap, attrs_msg);
386
387         ldb_set_debug_stderr(wrap);
388
389         kd = talloc_zero(parent_ctx, struct ldb_key_data);
390         kd->key.ops = &reg_backend_ldb;
391         kd->ldb = talloc_reference(kd, wrap);
392         talloc_set_destructor (kd, reg_close_ldb_key);
393         kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
394
395         *k = (struct hive_key *)kd;
396
397         return WERR_OK;
398 }
399
400 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
401                           const char *name, const char *classname,
402                           struct security_descriptor *sd,
403                           struct hive_key **newkey)
404 {
405         struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
406         struct ldb_message *msg;
407         struct ldb_key_data *newkd;
408         int ret;
409
410         msg = ldb_msg_new(mem_ctx);
411
412         msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
413
414         ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
415         if (classname != NULL)
416                 ldb_msg_add_string(msg, "classname",
417                                    talloc_strdup(mem_ctx, classname));
418
419         ret = ldb_add(parentkd->ldb, msg);
420         if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
421                 return WERR_ALREADY_EXISTS;
422         }
423
424         if (ret != LDB_SUCCESS) {
425                 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
426                 return WERR_FOOBAR;
427         }
428
429         DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
430
431         newkd = talloc_zero(mem_ctx, struct ldb_key_data);
432         newkd->ldb = talloc_reference(newkd, parentkd->ldb);
433         newkd->key.ops = &reg_backend_ldb;
434         newkd->dn = talloc_steal(newkd, msg->dn);
435
436         *newkey = (struct hive_key *)newkd;
437
438         /* reset cache */
439         talloc_free(parentkd->subkeys);
440         parentkd->subkeys = NULL;
441
442         return WERR_OK;
443 }
444
445 static WERROR ldb_del_value (struct hive_key *key, const char *child)
446 {
447         int ret;
448         struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
449         struct ldb_dn *childdn;
450
451         childdn = ldb_dn_copy(kd->ldb, kd->dn);
452         if (!ldb_dn_add_child_fmt(childdn, "value=%s",
453                                   reg_ldb_escape(childdn, child)))
454         {
455                 talloc_free(childdn);
456                 return WERR_FOOBAR;
457         }
458
459         ret = ldb_delete(kd->ldb, childdn);
460
461         talloc_free(childdn);
462
463         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
464                 return WERR_BADFILE;
465         } else if (ret != LDB_SUCCESS) {
466                 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
467                 return WERR_FOOBAR;
468         }
469
470         /* reset cache */
471         talloc_free(kd->values);
472         kd->values = NULL;
473
474         return WERR_OK;
475 }
476
477 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
478 {
479         int i, ret;
480         struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
481         struct ldb_dn *ldap_path;
482         TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
483         struct ldb_context *c = parentkd->ldb;
484         struct ldb_result *res_keys;
485         struct ldb_result *res_vals;
486         WERROR werr;
487         struct hive_key *hk;
488
489         /* Verify key exists by opening it */
490         werr = ldb_open_key(mem_ctx, key, name, &hk);
491         if (!W_ERROR_IS_OK(werr)) {
492                 talloc_free(mem_ctx);
493                 return werr;
494         }
495
496         ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
497         if (!ldap_path) {
498                 talloc_free(mem_ctx);
499                 return WERR_FOOBAR;
500         }
501
502         /* Search for subkeys */
503         ret = ldb_search(c, ldap_path, LDB_SCOPE_ONELEVEL,
504                          "(key=*)", NULL, &res_keys);
505
506         if (ret != LDB_SUCCESS) {
507                 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
508                       ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
509                 talloc_free(mem_ctx);
510                 return WERR_FOOBAR;
511         }
512
513         /* Search for values */
514         ret = ldb_search(c, ldap_path, LDB_SCOPE_ONELEVEL,
515                          "(value=*)", NULL, &res_vals);
516
517         if (ret != LDB_SUCCESS) {
518                 DEBUG(0, ("Error getting values for '%s': %s\n",
519                       ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
520                 talloc_free(mem_ctx);
521                 return WERR_FOOBAR;
522         }
523
524         /* Start an explicit transaction */
525         ret = ldb_transaction_start(c);
526
527         if (ret != LDB_SUCCESS) {
528                 DEBUG(0, ("ldb_transaction_start: %s\n", ldb_errstring(c)));
529                 talloc_free(mem_ctx);
530                 return WERR_FOOBAR;
531         }
532
533         if (res_keys->count || res_vals->count)
534         {
535                 /* Delete any subkeys */
536                 for (i = 0; i < res_keys->count; i++)
537                 {
538                         werr = ldb_del_key(hk, ldb_msg_find_attr_as_string(
539                                                         res_keys->msgs[i],
540                                                         "key", NULL));
541                         if (!W_ERROR_IS_OK(werr)) {
542                                 ret = ldb_transaction_cancel(c);
543                                 talloc_free(mem_ctx);
544                                 return werr;
545                         }
546                 }
547
548                 /* Delete any values */
549                 for (i = 0; i < res_vals->count; i++)
550                 {
551                         werr = ldb_del_value(hk, ldb_msg_find_attr_as_string(
552                                                         res_vals->msgs[i],
553                                                         "value", NULL));
554                         if (!W_ERROR_IS_OK(werr)) {
555                                 ret = ldb_transaction_cancel(c);
556                                 talloc_free(mem_ctx);
557                                 return werr;
558                         }
559                 }
560         }
561
562         /* Delete the key itself */
563         ret = ldb_delete(c, ldap_path);
564
565         if (ret != LDB_SUCCESS)
566         {
567                 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(c)));
568                 ret = ldb_transaction_cancel(c);
569                 talloc_free(mem_ctx);
570                 return WERR_FOOBAR;
571         }
572
573         /* Commit the transaction */
574         ret = ldb_transaction_commit(c);
575
576         if (ret != LDB_SUCCESS)
577         {
578                 DEBUG(0, ("ldb_transaction_commit: %s\n", ldb_errstring(c)));
579                 ret = ldb_transaction_cancel(c);
580                 talloc_free(mem_ctx);
581                 return WERR_FOOBAR;
582         }
583
584         talloc_free(mem_ctx);
585
586         /* reset cache */
587         talloc_free(parentkd->subkeys);
588         parentkd->subkeys = NULL;
589
590         return WERR_OK;
591 }
592
593 static WERROR ldb_set_value(struct hive_key *parent,
594                             const char *name, uint32_t type,
595                             const DATA_BLOB data)
596 {
597         struct ldb_message *msg;
598         struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
599         int ret;
600         TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
601
602         msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
603
604         msg->dn = ldb_dn_copy(msg, kd->dn);
605         if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
606                                   reg_ldb_escape(mem_ctx, name)))
607         {
608                 talloc_free(mem_ctx);
609                 return WERR_FOOBAR;
610         }
611
612         ret = ldb_add(kd->ldb, msg);
613         if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
614                 int i;
615                 for (i = 0; i < msg->num_elements; i++) {
616                         msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
617                 }
618                 ret = ldb_modify(kd->ldb, msg);
619         }
620
621         if (ret != LDB_SUCCESS) {
622                 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(kd->ldb)));
623                 talloc_free(mem_ctx);
624                 return WERR_FOOBAR;
625         }
626
627         /* reset cache */
628         talloc_free(kd->values);
629         kd->values = NULL;
630
631         talloc_free(mem_ctx);
632         return WERR_OK;
633 }
634
635 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
636                                const struct hive_key *key,
637                                const char **classname,
638                                uint32_t *num_subkeys,
639                                uint32_t *num_values,
640                                NTTIME *last_change_time,
641                                uint32_t *max_subkeynamelen,
642                                uint32_t *max_valnamelen,
643                                uint32_t *max_valbufsize)
644 {
645         struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
646
647         if (kd->subkeys == NULL) {
648                 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
649         }
650
651         if (kd->values == NULL) {
652                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
653         }
654
655         /* FIXME */
656         if (classname != NULL)
657                 *classname = NULL;
658
659         if (num_subkeys != NULL) {
660                 *num_subkeys = kd->subkey_count;
661         }
662
663         if (num_values != NULL) {
664                 *num_values = kd->value_count;
665         }
666
667         if (last_change_time != NULL)
668                 *last_change_time = 0;
669
670         if (max_subkeynamelen != NULL) {
671                 int i;
672                 struct ldb_message_element *el;
673
674                 *max_subkeynamelen = 0;
675
676                 for (i = 0; i < kd->subkey_count; i++) {
677                         el = ldb_msg_find_element(kd->subkeys[i], "key");
678                         *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
679                 }
680         }
681
682         if (max_valnamelen != NULL || max_valbufsize != NULL) {
683                 int i;
684                 struct ldb_message_element *el;
685                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
686
687                 if (max_valbufsize != NULL)
688                         *max_valbufsize = 0;
689
690                 if (max_valnamelen != NULL)
691                         *max_valnamelen = 0;
692
693                 for (i = 0; i < kd->value_count; i++) {
694                         if (max_valnamelen != NULL) {
695                                 el = ldb_msg_find_element(kd->values[i], "value");
696                                 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
697                         }
698
699                         if (max_valbufsize != NULL) {
700                                 DATA_BLOB data;
701                                 reg_ldb_unpack_value(mem_ctx, 
702                                                      lp_iconv_convenience(global_loadparm),
703                                                      kd->values[i], NULL, 
704                                                      NULL, &data);
705                                 *max_valbufsize = MAX(*max_valbufsize, data.length);
706                                 talloc_free(data.data);
707                         }
708                 }
709         }
710
711         return WERR_OK;
712 }
713
714 static struct hive_operations reg_backend_ldb = {
715         .name = "ldb",
716         .add_key = ldb_add_key,
717         .del_key = ldb_del_key,
718         .get_key_by_name = ldb_open_key,
719         .enum_value = ldb_get_value_by_id,
720         .enum_key = ldb_get_subkey_by_id,
721         .set_value = ldb_set_value,
722         .get_value_by_name = ldb_get_value,
723         .delete_value = ldb_del_value,
724         .get_key_info = ldb_get_key_info,
725 };