registry: Fix warning.
[samba.git] / source4 / 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, struct ldb_message *msg,
40                                  const char **name, uint32_t *type,
41                                  DATA_BLOB *data)
42 {
43         const struct ldb_val *val;
44         uint32_t value_type;
45
46         if (name != NULL)
47                 *name = talloc_strdup(mem_ctx,
48                                       ldb_msg_find_attr_as_string(msg, "value",
49                                       NULL));
50
51         value_type = ldb_msg_find_attr_as_uint(msg, "type", 0);
52         if (type != NULL)
53                 *type = value_type; 
54         val = ldb_msg_find_ldb_val(msg, "data");
55
56         switch (value_type)
57         {
58         case REG_SZ:
59         case REG_EXPAND_SZ:
60                 data->length = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF8, CH_UTF16,
61                                                      val->data, val->length,
62                                                      (void **)&data->data);
63                 break;
64
65         case REG_DWORD: {
66                 uint32_t tmp = strtoul((char *)val->data, NULL, 0);
67                 *data = data_blob_talloc(mem_ctx, &tmp, 4);
68                 }
69                 break;
70
71         default:
72                 *data = data_blob_talloc(mem_ctx, val->data, val->length);
73                 break;
74         }
75 }
76
77 static struct ldb_message *reg_ldb_pack_value(struct ldb_context *ctx,
78                                               TALLOC_CTX *mem_ctx,
79                                               const char *name,
80                                               uint32_t type, DATA_BLOB data)
81 {
82         struct ldb_val val;
83         struct ldb_message *msg = talloc_zero(mem_ctx, struct ldb_message);
84         char *type_s;
85
86         ldb_msg_add_string(msg, "value", talloc_strdup(mem_ctx, name));
87
88         switch (type) {
89         case REG_SZ:
90         case REG_EXPAND_SZ:
91                 val.length = convert_string_talloc(mem_ctx, lp_iconv_convenience(global_loadparm), CH_UTF16, CH_UNIX,
92                                                    (void *)data.data,
93                                                    data.length,
94                                                    (void **)&val.data);
95                 ldb_msg_add_value(msg, "data", &val, NULL);
96                 break;
97
98         case REG_DWORD:
99                 ldb_msg_add_string(msg, "data",
100                                    talloc_asprintf(mem_ctx, "0x%x",
101                                                    IVAL(data.data, 0)));
102                 break;
103         default:
104                 ldb_msg_add_value(msg, "data", &data, NULL);
105         }
106
107
108         type_s = talloc_asprintf(mem_ctx, "%u", type);
109         ldb_msg_add_string(msg, "type", type_s);
110
111         return msg;
112 }
113
114 static char *reg_ldb_escape(TALLOC_CTX *mem_ctx, const char *value)
115 {
116         struct ldb_val val;
117
118         val.data = discard_const_p(uint8_t, value);
119         val.length = strlen(value);
120
121         return ldb_dn_escape_value(mem_ctx, val);
122 }
123
124 static int reg_close_ldb_key(struct ldb_key_data *key)
125 {
126         if (key->subkeys != NULL) {
127                 talloc_free(key->subkeys);
128                 key->subkeys = NULL;
129         }
130
131         if (key->values != NULL) {
132                 talloc_free(key->values);
133                 key->values = NULL;
134         }
135         return 0;
136 }
137
138 static struct ldb_dn *reg_path_to_ldb(TALLOC_CTX *mem_ctx,
139                                       const struct hive_key *from,
140                                       const char *path, const char *add)
141 {
142         TALLOC_CTX *local_ctx;
143         struct ldb_dn *ret;
144         char *mypath = talloc_strdup(mem_ctx, path);
145         char *begin;
146         struct ldb_key_data *kd = talloc_get_type(from, struct ldb_key_data);
147         struct ldb_context *ldb = kd->ldb;
148
149         local_ctx = talloc_new(mem_ctx);
150
151         if (add) {
152                 ret = ldb_dn_new(mem_ctx, ldb, add);
153         } else {
154                 ret = ldb_dn_new(mem_ctx, ldb, NULL);
155         }
156         if (!ldb_dn_validate(ret)) {
157                 talloc_free(ret);
158                 talloc_free(local_ctx);
159                 return NULL;
160         }
161
162         while (mypath) {
163                 char *keyname;
164
165                 begin = strrchr(mypath, '\\');
166
167                 if (begin) keyname = begin + 1;
168                 else keyname = mypath;
169
170                 if(strlen(keyname)) {
171                         if (!ldb_dn_add_base_fmt(ret, "key=%s",
172                                                  reg_ldb_escape(local_ctx,
173                                                                 keyname)))
174                         {
175                                 talloc_free(local_ctx);
176                                 return NULL;
177                         }
178                 }
179
180                 if(begin) {
181                         *begin = '\0';
182                 } else {
183                         break;
184                 }
185         }
186
187         ldb_dn_add_base(ret, kd->dn);
188
189         talloc_free(local_ctx);
190
191         return ret;
192 }
193
194 static WERROR cache_subkeys(struct ldb_key_data *kd)
195 {
196         struct ldb_context *c = kd->ldb;
197         struct ldb_result *res;
198         int ret;
199
200         ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL, &res);
201
202         if (ret != LDB_SUCCESS) {
203                 DEBUG(0, ("Error getting subkeys for '%s': %s\n",
204                         ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
205                 return WERR_FOOBAR;
206         }
207
208         kd->subkey_count = res->count;
209         kd->subkeys = talloc_steal(kd, res->msgs);
210         talloc_free(res);
211
212         return WERR_OK;
213 }
214
215 static WERROR cache_values(struct ldb_key_data *kd)
216 {
217         struct ldb_context *c = kd->ldb;
218         struct ldb_result *res;
219         int ret;
220
221         ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL,
222                          "(value=*)", NULL, &res);
223
224         if (ret != LDB_SUCCESS) {
225                 DEBUG(0, ("Error getting values for '%s': %s\n",
226                         ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
227                 return WERR_FOOBAR;
228         }
229         kd->value_count = res->count;
230         kd->values = talloc_steal(kd, res->msgs);
231         talloc_free(res);
232         return WERR_OK;
233 }
234
235
236 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx,
237                                    const struct hive_key *k, uint32_t idx,
238                                    const char **name,
239                                    const char **classname,
240                                    NTTIME *last_mod_time)
241 {
242         struct ldb_message_element *el;
243         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
244
245         /* Do a search if necessary */
246         if (kd->subkeys == NULL) {
247                 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
248         }
249
250         if (idx >= kd->subkey_count)
251                 return WERR_NO_MORE_ITEMS;
252
253         el = ldb_msg_find_element(kd->subkeys[idx], "key");
254         SMB_ASSERT(el != NULL);
255         SMB_ASSERT(el->num_values != 0);
256
257         if (name != NULL)
258                 *name = talloc_strdup(mem_ctx, (char *)el->values[0].data);
259
260         if (classname != NULL)
261                 *classname = NULL; /* TODO: Store properly */
262
263         if (last_mod_time != NULL)
264                 *last_mod_time = 0; /* TODO: we need to add this to the
265                                                 ldb backend properly */
266
267         return WERR_OK;
268 }
269
270 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct hive_key *k,
271                                   int idx, const char **name,
272                                   uint32_t *data_type, DATA_BLOB *data)
273 {
274         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
275
276         /* Do the search if necessary */
277         if (kd->values == NULL) {
278                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
279         }
280
281         if (idx >= kd->value_count)
282                 return WERR_NO_MORE_ITEMS;
283
284         reg_ldb_unpack_value(mem_ctx, kd->values[idx],
285                              name, data_type, data);
286
287         return WERR_OK;
288 }
289
290 static WERROR ldb_get_value(TALLOC_CTX *mem_ctx, struct hive_key *k,
291                             const char *name, uint32_t *data_type,
292                             DATA_BLOB *data)
293 {
294         struct ldb_key_data *kd = talloc_get_type(k, struct ldb_key_data);
295         struct ldb_context *c = kd->ldb;
296         struct ldb_result *res;
297         int ret;
298         char *query = talloc_asprintf(mem_ctx, "(value=%s)", name);
299
300         ret = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, query, NULL, &res);
301
302         talloc_free(query);
303
304         if (ret != LDB_SUCCESS) {
305                 DEBUG(0, ("Error getting values for '%s': %s\n",
306                         ldb_dn_get_linearized(kd->dn), ldb_errstring(c)));
307                 return WERR_FOOBAR;
308         }
309
310         if (res->count == 0)
311                 return WERR_BADFILE;
312
313         reg_ldb_unpack_value(mem_ctx, res->msgs[0], NULL, data_type, data);
314
315         return WERR_OK;
316 }
317
318 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, const struct hive_key *h,
319                            const char *name, struct hive_key **key)
320 {
321         struct ldb_result *res;
322         struct ldb_dn *ldap_path;
323         int ret;
324         struct ldb_key_data *newkd;
325         struct ldb_key_data *kd = talloc_get_type(h, struct ldb_key_data);
326         struct ldb_context *c = kd->ldb;
327
328         ldap_path = reg_path_to_ldb(mem_ctx, h, name, NULL);
329
330         ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL, &res);
331
332         if (ret != LDB_SUCCESS) {
333                 DEBUG(3, ("Error opening key '%s': %s\n",
334                         ldb_dn_get_linearized(ldap_path), ldb_errstring(c)));
335                 return WERR_FOOBAR;
336         } else if (res->count == 0) {
337                 DEBUG(3, ("Key '%s' not found\n",
338                         ldb_dn_get_linearized(ldap_path)));
339                 talloc_free(res);
340                 return WERR_BADFILE;
341         }
342
343         newkd = talloc_zero(mem_ctx, struct ldb_key_data);
344         newkd->key.ops = &reg_backend_ldb;
345         newkd->ldb = talloc_reference(newkd, kd->ldb);
346         newkd->dn = ldb_dn_copy(mem_ctx, res->msgs[0]->dn);
347
348         *key = (struct hive_key *)newkd;
349
350         talloc_free(res);
351
352         return WERR_OK;
353 }
354
355 WERROR reg_open_ldb_file(TALLOC_CTX *parent_ctx, const char *location,
356                          struct auth_session_info *session_info,
357                          struct cli_credentials *credentials,
358                          struct loadparm_context *lp_ctx,
359                          struct hive_key **k)
360 {
361         struct ldb_key_data *kd;
362         struct ldb_context *wrap;
363         struct ldb_message *attrs_msg;
364
365         if (location == NULL)
366                 return WERR_INVALID_PARAM;
367
368         wrap = ldb_wrap_connect(parent_ctx, lp_ctx,
369                                 location, session_info, credentials, 0, NULL);
370
371         if (wrap == NULL) {
372                 DEBUG(1, (__FILE__": unable to connect\n"));
373                 return WERR_FOOBAR;
374         }
375
376         attrs_msg = ldb_msg_new(wrap);
377         W_ERROR_HAVE_NO_MEMORY(attrs_msg);
378         attrs_msg->dn = ldb_dn_new(attrs_msg, wrap, "@ATTRIBUTES");
379         W_ERROR_HAVE_NO_MEMORY(attrs_msg->dn);
380         ldb_msg_add_string(attrs_msg, "key", "CASE_INSENSITIVE");
381         ldb_msg_add_string(attrs_msg, "value", "CASE_INSENSITIVE");
382
383         ldb_add(wrap, attrs_msg);
384
385         ldb_set_debug_stderr(wrap);
386
387         kd = talloc_zero(parent_ctx, struct ldb_key_data);
388         kd->key.ops = &reg_backend_ldb;
389         kd->ldb = talloc_reference(kd, wrap);
390         talloc_set_destructor (kd, reg_close_ldb_key);
391         kd->dn = ldb_dn_new(kd, wrap, "hive=NONE");
392
393         *k = (struct hive_key *)kd;
394
395         return WERR_OK;
396 }
397
398 static WERROR ldb_add_key(TALLOC_CTX *mem_ctx, const struct hive_key *parent,
399                           const char *name, const char *classname,
400                           struct security_descriptor *sd,
401                           struct hive_key **newkey)
402 {
403         struct ldb_key_data *parentkd = discard_const_p(struct ldb_key_data, parent);
404         struct ldb_message *msg;
405         struct ldb_key_data *newkd;
406         int ret;
407
408         msg = ldb_msg_new(mem_ctx);
409
410         msg->dn = reg_path_to_ldb(msg, parent, name, NULL);
411
412         ldb_msg_add_string(msg, "key", talloc_strdup(mem_ctx, name));
413         if (classname != NULL)
414                 ldb_msg_add_string(msg, "classname",
415                                    talloc_strdup(mem_ctx, classname));
416
417         ret = ldb_add(parentkd->ldb, msg);
418         if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
419                 return WERR_ALREADY_EXISTS;
420         }
421
422         if (ret != LDB_SUCCESS) {
423                 DEBUG(1, ("ldb_add: %s\n", ldb_errstring(parentkd->ldb)));
424                 return WERR_FOOBAR;
425         }
426
427         DEBUG(2, ("key added: %s\n", ldb_dn_get_linearized(msg->dn)));
428
429         newkd = talloc_zero(mem_ctx, struct ldb_key_data);
430         newkd->ldb = talloc_reference(newkd, parentkd->ldb);
431         newkd->key.ops = &reg_backend_ldb;
432         newkd->dn = talloc_steal(newkd, msg->dn);
433
434         *newkey = (struct hive_key *)newkd;
435
436         /* reset cache */
437         talloc_free(parentkd->subkeys);
438         parentkd->subkeys = NULL;
439
440         return WERR_OK;
441 }
442
443 static WERROR ldb_del_key(const struct hive_key *key, const char *name)
444 {
445         int ret;
446         struct ldb_key_data *parentkd = talloc_get_type(key, struct ldb_key_data);
447         struct ldb_dn *ldap_path;
448         TALLOC_CTX *mem_ctx = talloc_init("ldb_del_key");
449
450         ldap_path = reg_path_to_ldb(mem_ctx, key, name, NULL);
451
452         ret = ldb_delete(parentkd->ldb, ldap_path);
453
454         talloc_free(mem_ctx);
455
456         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
457                 return WERR_BADFILE;
458         } else if (ret != LDB_SUCCESS) {
459                 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(parentkd->ldb)));
460                 return WERR_FOOBAR;
461         }
462
463         /* reset cache */
464         talloc_free(parentkd->subkeys);
465         parentkd->subkeys = NULL;
466
467         return WERR_OK;
468 }
469
470 static WERROR ldb_del_value (struct hive_key *key, const char *child)
471 {
472         int ret;
473         struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
474         struct ldb_dn *childdn;
475
476         childdn = ldb_dn_copy(kd->ldb, kd->dn);
477         if (!ldb_dn_add_child_fmt(childdn, "value=%s",
478                                   reg_ldb_escape(childdn, child)))
479         {
480                 talloc_free(childdn);
481                 return WERR_FOOBAR;
482         }
483
484         ret = ldb_delete(kd->ldb, childdn);
485
486         talloc_free(childdn);
487
488         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
489                 return WERR_BADFILE;
490         } else if (ret != LDB_SUCCESS) {
491                 DEBUG(1, ("ldb_del_value: %s\n", ldb_errstring(kd->ldb)));
492                 return WERR_FOOBAR;
493         }
494
495         /* reset cache */
496         talloc_free(kd->values);
497         kd->values = NULL;
498
499         return WERR_OK;
500 }
501
502 static WERROR ldb_set_value(struct hive_key *parent,
503                             const char *name, uint32_t type,
504                             const DATA_BLOB data)
505 {
506         struct ldb_message *msg;
507         struct ldb_key_data *kd = talloc_get_type(parent, struct ldb_key_data);
508         int ret;
509         TALLOC_CTX *mem_ctx = talloc_init("ldb_set_value");
510
511         msg = reg_ldb_pack_value(kd->ldb, mem_ctx, name, type, data);
512
513         msg->dn = ldb_dn_copy(msg, kd->dn);
514         if (!ldb_dn_add_child_fmt(msg->dn, "value=%s",
515                                   reg_ldb_escape(mem_ctx, name)))
516         {
517                 talloc_free(mem_ctx);
518                 return WERR_FOOBAR;
519         }
520
521         ret = ldb_add(kd->ldb, msg);
522         if (ret == LDB_ERR_ENTRY_ALREADY_EXISTS) {
523                 int i;
524                 for (i = 0; i < msg->num_elements; i++) {
525                         msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
526                 }
527                 ret = ldb_modify(kd->ldb, msg);
528         }
529
530         if (ret != LDB_SUCCESS) {
531                 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(kd->ldb)));
532                 talloc_free(mem_ctx);
533                 return WERR_FOOBAR;
534         }
535
536         /* reset cache */
537         talloc_free(kd->values);
538         kd->values = NULL;
539
540         talloc_free(mem_ctx);
541         return WERR_OK;
542 }
543
544 static WERROR ldb_get_key_info(TALLOC_CTX *mem_ctx,
545                                const struct hive_key *key,
546                                const char **classname,
547                                uint32_t *num_subkeys,
548                                uint32_t *num_values,
549                                NTTIME *last_change_time,
550                                uint32_t *max_subkeynamelen,
551                                uint32_t *max_valnamelen,
552                                uint32_t *max_valbufsize)
553 {
554         struct ldb_key_data *kd = talloc_get_type(key, struct ldb_key_data);
555
556         if (kd->subkeys == NULL) {
557                 W_ERROR_NOT_OK_RETURN(cache_subkeys(kd));
558         }
559
560         if (kd->values == NULL) {
561                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
562         }
563
564         /* FIXME */
565         if (classname != NULL)
566                 *classname = NULL;
567
568         if (num_subkeys != NULL) {
569                 *num_subkeys = kd->subkey_count;
570         }
571
572         if (num_values != NULL) {
573                 *num_values = kd->value_count;
574         }
575
576         if (last_change_time != NULL)
577                 *last_change_time = 0;
578
579         if (max_subkeynamelen != NULL) {
580                 int i;
581                 struct ldb_message_element *el;
582
583                 *max_subkeynamelen = 0;
584
585                 for (i = 0; i < kd->subkey_count; i++) {
586                         el = ldb_msg_find_element(kd->subkeys[i], "key");
587                         *max_subkeynamelen = MAX(*max_subkeynamelen, el->values[0].length);
588                 }
589         }
590
591         if (max_valnamelen != NULL || max_valbufsize != NULL) {
592                 int i;
593                 struct ldb_message_element *el;
594                 W_ERROR_NOT_OK_RETURN(cache_values(kd));
595
596                 if (max_valbufsize != NULL)
597                         *max_valbufsize = 0;
598
599                 if (max_valnamelen != NULL)
600                         *max_valnamelen = 0;
601
602                 for (i = 0; i < kd->value_count; i++) {
603                         if (max_valnamelen != NULL) {
604                                 el = ldb_msg_find_element(kd->values[i], "value");
605                                 *max_valnamelen = MAX(*max_valnamelen, el->values[0].length);
606                         }
607
608                         if (max_valbufsize != NULL) {
609                                 DATA_BLOB data;
610                                 reg_ldb_unpack_value(mem_ctx, kd->values[i], NULL, 
611                                                      NULL, &data);
612                                 *max_valbufsize = MAX(*max_valbufsize, data.length);
613                                 talloc_free(data.data);
614                         }
615                 }
616         }
617
618         return WERR_OK;
619 }
620
621 static struct hive_operations reg_backend_ldb = {
622         .name = "ldb",
623         .add_key = ldb_add_key,
624         .del_key = ldb_del_key,
625         .get_key_by_name = ldb_open_key,
626         .enum_value = ldb_get_value_by_id,
627         .enum_key = ldb_get_subkey_by_id,
628         .set_value = ldb_set_value,
629         .get_value_by_name = ldb_get_value,
630         .delete_value = ldb_del_value,
631         .get_key_info = ldb_get_key_info,
632 };