Merge commit 'release-4-0-0alpha1' into v4-0-test
[ira/wip.git] / source4 / lib / registry / local.c
1 /*
2    Unix SMB/CIFS implementation.
3    Transparent registry backend handling
4    Copyright (C) Jelmer Vernooij                        2003-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 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 "lib/util/dlinklist.h"
23 #include "lib/registry/registry.h"
24 #include "system/filesys.h"
25 #include "build.h"
26
27 struct reg_key_path {
28         uint32_t predefined_key;
29         const char **elements;
30 };
31
32 struct registry_local {
33         struct registry_context registry;
34
35         struct mountpoint {
36                 struct reg_key_path path;
37                 struct hive_key *key;
38                 struct mountpoint *prev, *next;
39         } *mountpoints;
40
41         struct auth_session_info *session_info;
42         struct cli_credentials *credentials;
43 };
44
45 struct local_key {
46         struct registry_key global;
47         struct reg_key_path path;
48         struct hive_key *hive_key;
49 };
50
51
52 struct registry_key *reg_import_hive_key(struct registry_context *ctx,
53                                          struct hive_key *hive,
54                                          uint32_t predefined_key,
55                                          const char **elements)
56 {
57         struct local_key *local_key;
58         struct reg_key_path parent_path;
59
60         parent_path.predefined_key = predefined_key;
61         parent_path.elements = elements;
62
63         local_key = talloc(ctx, struct local_key);
64         local_key->hive_key = talloc_steal(local_key, hive);
65         local_key->global.context = talloc_reference(local_key, ctx);
66         local_key->path = parent_path;
67
68         return (struct registry_key *)local_key;
69 }
70
71
72 static WERROR local_open_key(TALLOC_CTX *mem_ctx,
73                              struct registry_key *parent,
74                              const char *path,
75                              struct registry_key **result)
76 {
77         char *orig = talloc_strdup(mem_ctx, path),
78                  *curbegin = orig,
79                  *curend = strchr(orig, '\\');
80         struct local_key *local_parent = talloc_get_type(parent,
81                                                          struct local_key);
82         struct hive_key *curkey = local_parent->hive_key;
83         WERROR error;
84         const char **elements = NULL;
85         int el;
86
87         if (local_parent->path.elements != NULL) {
88                 elements = talloc_array(mem_ctx, const char *,
89                                         str_list_length(local_parent->path.elements) + 1);
90                 for (el = 0; local_parent->path.elements[el] != NULL; el++) {
91                         elements[el] = talloc_reference(elements,
92                                                         local_parent->path.elements[el]);
93                 }
94                 elements[el] = NULL;
95         } else {
96                 elements = NULL;
97                 el = 0;
98         }
99
100         while (curbegin != NULL && *curbegin) {
101                 if (curend != NULL)
102                         *curend = '\0';
103                 elements = talloc_realloc(mem_ctx, elements, const char *, el+2);
104                 elements[el] = talloc_strdup(elements, curbegin);
105                 el++;
106                 elements[el] = NULL;
107                 error = hive_get_key_by_name(mem_ctx, curkey,
108                                              curbegin, &curkey);
109                 if (!W_ERROR_IS_OK(error)) {
110                         DEBUG(2, ("Opening key %s failed: %s\n", curbegin,
111                                 win_errstr(error)));
112                         talloc_free(orig);
113                         return error;
114                 }
115                 if (curend == NULL)
116                         break;
117                 curbegin = curend + 1;
118                 curend = strchr(curbegin, '\\');
119         }
120         talloc_free(orig);
121
122         *result = reg_import_hive_key(local_parent->global.context, curkey,
123                                       local_parent->path.predefined_key,
124                                       talloc_steal(curkey, elements));
125
126         return WERR_OK;
127 }
128
129 WERROR local_get_predefined_key(const struct registry_context *ctx,
130                                 uint32_t key_id, struct registry_key **key)
131 {
132         struct registry_local *rctx = talloc_get_type(ctx,
133                                                       struct registry_local);
134         struct mountpoint *mp;
135
136         for (mp = rctx->mountpoints; mp != NULL; mp = mp->next) {
137                 if (mp->path.predefined_key == key_id &&
138                         mp->path.elements == NULL)
139                         break;
140         }
141
142         if (mp == NULL)
143                 return WERR_NOT_FOUND;
144
145         *key = reg_import_hive_key(ctx, mp->key,
146                                    mp->path.predefined_key,
147                                    mp->path.elements);
148
149         return WERR_OK;
150 }
151
152 static WERROR local_enum_key(TALLOC_CTX *mem_ctx,
153                              const struct registry_key *key, uint32_t idx,
154                              const char **name,
155                              const char **keyclass,
156                              NTTIME *last_changed_time)
157 {
158         const struct local_key *local = (const struct local_key *)key;
159
160         return hive_enum_key(mem_ctx, local->hive_key, idx, name, keyclass,
161                              last_changed_time);
162 }
163
164 static WERROR local_create_key(TALLOC_CTX *mem_ctx,
165                                struct registry_key *parent_key,
166                                const char *name,
167                                const char *key_class,
168                                struct security_descriptor *security,
169                                struct registry_key **key)
170 {
171         const struct local_key *local_parent;
172         struct hive_key *hivekey;
173         const char **elements;
174         int i;
175         const char *last_part;
176
177         last_part = strrchr(name, '\\');
178         if (last_part == NULL) {
179                 last_part = name;
180                 local_parent = (const struct local_key *)parent_key;
181         } else {
182                 W_ERROR_NOT_OK_RETURN(reg_open_key(mem_ctx, parent_key,
183                                                    talloc_strndup(mem_ctx, name, last_part-name),
184                                                    &local_parent));
185                 last_part++;
186         }
187
188         W_ERROR_NOT_OK_RETURN(hive_key_add_name(mem_ctx, local_parent->hive_key,
189                                                 last_part, key_class, security,
190                                                 &hivekey));
191
192         if (local_parent->path.elements != NULL) {
193                 elements = talloc_array(hivekey, const char *,
194                                         str_list_length(local_parent->path.elements)+2);
195                 for (i = 0; local_parent->path.elements[i] != NULL; i++) {
196                         elements[i] = talloc_reference(elements,
197                                                        local_parent->path.elements[i]);
198                 }
199         } else {
200                 elements = talloc_array(hivekey, const char *, 2);
201                 i = 0;
202         }
203
204         elements[i] = talloc_strdup(elements, name);
205         elements[i+1] = NULL;
206
207         *key = reg_import_hive_key(local_parent->global.context, hivekey,
208                                    local_parent->path.predefined_key,
209                                    elements);
210
211         return WERR_OK;
212 }
213
214 static WERROR local_set_value(struct registry_key *key, const char *name,
215                               uint32_t type, const DATA_BLOB data)
216 {
217         struct local_key *local = (struct local_key *)key;
218
219         return hive_set_value(local->hive_key, name, type, data);
220 }
221
222 static WERROR local_get_value(TALLOC_CTX *mem_ctx,
223                               const struct registry_key *key,
224                               const char *name, uint32_t *type, DATA_BLOB *data)
225 {
226         const struct local_key *local = (const struct local_key *)key;
227
228         return hive_get_value(mem_ctx, local->hive_key, name, type, data);
229 }
230
231 static WERROR local_enum_value(TALLOC_CTX *mem_ctx,
232                                const struct registry_key *key, uint32_t idx,
233                                const char **name,
234                                uint32_t *type,
235                                DATA_BLOB *data)
236 {
237         const struct local_key *local = (const struct local_key *)key;
238
239         return hive_get_value_by_index(mem_ctx, local->hive_key, idx,
240                                        name, type, data);
241 }
242
243 static WERROR local_delete_key(struct registry_key *key, const char *name)
244 {
245         const struct local_key *local = (const struct local_key *)key;
246
247         return hive_key_del(local->hive_key, name);
248 }
249
250 static WERROR local_delete_value(struct registry_key *key, const char *name)
251 {
252         const struct local_key *local = (const struct local_key *)key;
253
254         return hive_del_value(local->hive_key, name);
255 }
256
257 static WERROR local_flush_key(struct registry_key *key)
258 {
259         const struct local_key *local = (const struct local_key *)key;
260
261         return hive_key_flush(local->hive_key);
262 }
263
264 static WERROR local_get_key_info(TALLOC_CTX *mem_ctx,
265                                  const struct registry_key *key,
266                                  const char **classname,
267                                  uint32_t *num_subkeys,
268                                  uint32_t *num_values,
269                                  NTTIME *last_change_time)
270 {
271         const struct local_key *local = (const struct local_key *)key;
272
273         return hive_key_get_info(mem_ctx, local->hive_key,
274                                  classname, num_subkeys, num_values,
275                                  last_change_time);
276 }
277
278 const static struct registry_operations local_ops = {
279         .name = "local",
280         .open_key = local_open_key,
281         .get_predefined_key = local_get_predefined_key,
282         .enum_key = local_enum_key,
283         .create_key = local_create_key,
284         .set_value = local_set_value,
285         .get_value = local_get_value,
286         .enum_value = local_enum_value,
287         .delete_key = local_delete_key,
288         .delete_value = local_delete_value,
289         .flush_key = local_flush_key,
290         .get_key_info = local_get_key_info,
291 };
292
293 WERROR reg_open_local(TALLOC_CTX *mem_ctx, struct registry_context **ctx,
294                       struct auth_session_info *session_info,
295                       struct cli_credentials *credentials)
296 {
297         struct registry_local *ret = talloc_zero(mem_ctx,
298                                                  struct registry_local);
299
300         W_ERROR_HAVE_NO_MEMORY(ret);
301
302         ret->registry.ops = &local_ops;
303         ret->session_info = session_info;
304         ret->credentials = credentials;
305
306         *ctx = (struct registry_context *)ret;
307
308         return WERR_OK;
309 }
310
311 WERROR reg_mount_hive(struct registry_context *rctx,
312                       struct hive_key *hive_key,
313                       uint32_t key_id,
314                       const char **elements)
315 {
316         struct registry_local *reg_local = talloc_get_type(rctx,
317                                                            struct registry_local);
318         struct mountpoint *mp = talloc(rctx, struct mountpoint);
319         int i = 0;
320
321         mp->path.predefined_key = key_id;
322         mp->prev = mp->next = NULL;
323         mp->key = hive_key;
324         if (elements != NULL) {
325                 mp->path.elements = talloc_array(mp, const char *,
326                                                  str_list_length(elements));
327                 for (i = 0; elements[i] != NULL; i++) {
328                         mp->path.elements[i] = talloc_reference(mp->path.elements,
329                                                                 elements[i]);
330                 }
331                 mp->path.elements[i] = NULL;
332         } else {
333                 mp->path.elements = NULL;
334         }
335
336         DLIST_ADD(reg_local->mountpoints, mp);
337
338         return WERR_OK;
339 }