r4166: More small API fixes, keep registry structs as small as possible.
[bbaumbach/samba-autobuild/.git] / source4 / lib / registry / reg_backend_ldb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Registry interface
4    Copyright (C) Jelmer Vernooij  2004.
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "registry.h"
23 #include "lib/ldb/include/ldb.h"
24
25 struct ldb_key_data 
26 {
27         const char *dn;
28         struct ldb_message **subkeys, **values;
29         int subkey_count, value_count;
30 };
31
32 static int ldb_close_hive (void *_hive)
33 {
34         struct registry_hive *hive = _hive;
35         ldb_close (hive->backend_data);
36         return 0;
37 }
38
39
40
41 static int reg_close_ldb_key (void *data)
42 {
43         struct registry_key *key = data;
44         struct ldb_key_data *kd = key->backend_data;
45         struct ldb_context *c = key->hive->backend_data;
46
47         if (kd->subkeys) {
48                 ldb_search_free(c, kd->subkeys); 
49                 kd->subkeys = NULL;
50         }
51
52         if (kd->values) {
53                 ldb_search_free(c, kd->values); 
54                 kd->values = NULL;
55         }
56         return 0;
57 }
58
59 static char *reg_path_to_ldb(TALLOC_CTX *mem_ctx, const char *path, const char *add)
60 {
61         char *ret = talloc_strdup(mem_ctx, "");
62         char *mypath = strdup(path);
63         char *end = mypath, *begin;
64
65         if(add) 
66                 ret = talloc_asprintf_append(ret, "%s", add);
67
68         while(end) {
69                 char *keyname;
70                 begin = strrchr(end, '\\');
71
72                 if(begin) keyname = begin + 1;
73                 else keyname = mypath;
74
75                 if(strlen(keyname))
76                         ret = talloc_asprintf_append(ret, "key=%s,", keyname);
77                         
78                 if(begin) {
79                         *begin = '\0';
80                         end = begin-1;
81                 } else {
82                         end = NULL;
83                 }
84         }
85
86         SAFE_FREE(mypath);
87
88         ret[strlen(ret)-1] = '\0';
89
90         if(strlen(ret) == 0) return NULL;
91         
92         return ret;
93 }
94
95
96 static WERROR ldb_get_subkey_by_id(TALLOC_CTX *mem_ctx, struct registry_key *k, int idx, struct registry_key **subkey)
97 {
98         struct ldb_context *c = k->hive->backend_data;
99         struct ldb_message_element *el;
100         struct ldb_key_data *kd = k->backend_data, *newkd;
101
102         /* Do a search if necessary */
103         if (kd->subkeys == NULL) {
104                 kd->subkey_count = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(key=*)", NULL, &kd->subkeys);
105
106                 if(kd->subkey_count < 0) {
107                         DEBUG(0, ("Error getting subkeys for '%s': %s\n", kd->dn, ldb_errstring(c)));
108                         return WERR_FOOBAR;
109                 }
110         } 
111
112         if (idx >= kd->subkey_count) return WERR_NO_MORE_ITEMS;
113
114         el = ldb_msg_find_element(kd->subkeys[idx], "key");
115         
116         *subkey = talloc_p(mem_ctx, struct registry_key);
117         talloc_set_destructor(*subkey, reg_close_ldb_key);
118         (*subkey)->name = talloc_strdup(mem_ctx, el->values[0].data);
119         (*subkey)->backend_data = newkd = talloc_zero_p(*subkey, struct ldb_key_data);
120         newkd->dn = talloc_strdup(mem_ctx, kd->subkeys[idx]->dn);
121
122         return WERR_OK;
123 }
124
125 static WERROR ldb_get_value_by_id(TALLOC_CTX *mem_ctx, struct registry_key *k, int idx, struct registry_value **value)
126 {
127         struct ldb_context *c = k->hive->backend_data;
128         struct ldb_message_element *el;
129         struct ldb_key_data *kd = k->backend_data;
130
131         /* Do the search if necessary */
132         if (kd->values == NULL) {
133                 kd->value_count = ldb_search(c, kd->dn, LDB_SCOPE_ONELEVEL, "(value=*)", NULL,&kd->values);
134
135                 if(kd->value_count < 0) {
136                         DEBUG(0, ("Error getting values for '%s': %s\n", kd->dn, ldb_errstring(c)));
137                         return WERR_FOOBAR;
138                 }
139         }
140
141         if(idx >= kd->value_count) return WERR_NO_MORE_ITEMS;
142         
143         el = ldb_msg_find_element(kd->values[idx], "value");
144         
145         *value = talloc_p(mem_ctx, struct registry_value);
146         (*value)->name = talloc_strdup(mem_ctx, el->values[0].data);
147         /* FIXME */
148
149         return WERR_OK;
150 }
151
152 static WERROR ldb_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h, const char *name, struct registry_key **key)
153 {
154         struct ldb_context *c = h->hive->backend_data;
155         struct ldb_message **msg;
156         char *ldap_path;
157         int ret;
158         struct ldb_key_data *kd = h->backend_data, *newkd;
159         ldap_path = talloc_asprintf(mem_ctx, "%s%s%s", 
160                                                                 reg_path_to_ldb(mem_ctx, name, NULL), 
161                                                                 kd->dn?",":"",
162                                                                 kd->dn?kd->dn:"");
163
164         ret = ldb_search(c, ldap_path, LDB_SCOPE_BASE, "(key=*)", NULL,&msg);
165
166         if(ret == 0) {
167                 return WERR_NO_MORE_ITEMS;
168         } else if(ret < 0) {
169                 DEBUG(0, ("Error opening key '%s': %s\n", ldap_path, ldb_errstring(c)));
170                 return WERR_FOOBAR;
171         }
172
173         *key = talloc_p(mem_ctx, struct registry_key);
174         talloc_set_destructor(*key, reg_close_ldb_key);
175         (*key)->name = talloc_strdup(mem_ctx, strrchr(name, '\\')?strchr(name, '\\'):name);
176         (*key)->backend_data = newkd = talloc_zero_p(*key, struct ldb_key_data);
177         newkd->dn = talloc_strdup(mem_ctx, msg[0]->dn); 
178
179         ldb_search_free(c, msg);
180
181         return WERR_OK;
182 }
183
184 static WERROR ldb_open_hive(struct registry_hive *hive, struct registry_key **k)
185 {
186         struct ldb_context *c;
187         struct ldb_key_data *kd;
188
189         if (!hive->location) return WERR_INVALID_PARAM;
190         c = ldb_connect(hive->location, 0, NULL);
191
192         if(!c) {
193                 DEBUG(1, ("ldb_open_hive: %s\n", ldb_errstring(hive->backend_data)));
194                 return WERR_FOOBAR;
195         }
196         ldb_set_debug_stderr(c);
197         hive->backend_data = c;
198
199         *k = talloc_zero_p(hive, struct registry_key);
200         talloc_set_destructor (*k, reg_close_ldb_key);
201         talloc_set_destructor (hive, ldb_close_hive);
202         (*k)->name = talloc_strdup(*k, "");
203         (*k)->backend_data = kd = talloc_zero_p(*k, struct ldb_key_data);
204         kd->dn = talloc_strdup(*k, "key=root");
205         
206
207         return WERR_OK;
208 }
209
210 static WERROR ldb_add_key (TALLOC_CTX *mem_ctx, struct registry_key *parent, const char *name, uint32_t access_mask, SEC_DESC *sd, struct registry_key **newkey)
211 {
212         struct ldb_context *ctx = parent->hive->backend_data;
213         struct ldb_message msg;
214         struct ldb_key_data *newkd;
215         int ret;
216
217         ZERO_STRUCT(msg);
218
219         msg.dn = reg_path_to_ldb(mem_ctx, parent->path, talloc_asprintf(mem_ctx, "key=%s,", name));
220
221         ldb_msg_add_string(ctx, &msg, "key", talloc_strdup(mem_ctx, name));
222
223         ret = ldb_add(ctx, &msg);
224         if (ret < 0) {
225                 DEBUG(1, ("ldb_msg_add: %s\n", ldb_errstring(parent->hive->backend_data)));
226                 return WERR_FOOBAR;
227         }
228
229         *newkey = talloc_zero_p(mem_ctx, struct registry_key);
230         (*newkey)->name = talloc_strdup(mem_ctx, name);
231
232         (*newkey)->backend_data = newkd = talloc_zero_p(*newkey, struct ldb_key_data);
233         newkd->dn = msg.dn; 
234
235         return WERR_OK;
236 }
237
238 static WERROR ldb_del_key (struct registry_key *key)
239 {
240         int ret;
241         struct ldb_key_data *kd = key->backend_data;
242
243         ret = ldb_delete(key->hive->backend_data, kd->dn);
244
245         if (ret < 0) {
246                 DEBUG(1, ("ldb_del_key: %s\n", ldb_errstring(key->hive->backend_data)));
247                 return WERR_FOOBAR;
248         }
249
250         return WERR_OK;
251 }
252
253 static struct hive_operations reg_backend_ldb = {
254         .name = "ldb",
255         .add_key = ldb_add_key,
256         .del_key = ldb_del_key,
257         .open_hive = ldb_open_hive,
258         .open_key = ldb_open_key,
259         .get_value_by_index = ldb_get_value_by_id,
260         .get_subkey_by_index = ldb_get_subkey_by_id,
261 };
262
263 NTSTATUS registry_ldb_init(void)
264 {
265         return registry_register(&reg_backend_ldb);
266 }