r25000: Fix some more C++ compatibility warnings.
[tprouty/samba.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, struct local_key);
81         struct hive_key *curkey = local_parent->hive_key;
82         WERROR error;
83         const char **elements = NULL;
84         int el;
85
86         if (local_parent->path.elements != NULL) {
87                 elements = talloc_array(mem_ctx, const char *, 
88                                                         str_list_length(local_parent->path.elements) + 1);
89                 for (el = 0; local_parent->path.elements[el] != NULL; el++) {
90                         elements[el] = talloc_reference(elements, 
91                                                                                         local_parent->path.elements[el]);
92                 }
93                 elements[el] = NULL;
94         } else {
95                 elements = NULL;
96                 el = 0;
97         }
98
99         while (curbegin != NULL && *curbegin) {
100                 if (curend != NULL)
101                         *curend = '\0';
102                 elements = talloc_realloc(mem_ctx, elements, const char *, el+2);
103                 elements[el] = talloc_strdup(elements, curbegin);
104                 el++;
105                 elements[el] = NULL;
106                 error = hive_get_key_by_name(mem_ctx, curkey, curbegin, &curkey);
107                 if (!W_ERROR_IS_OK(error)) {
108                         DEBUG(2, ("Opening key %s failed: %s\n", curbegin, win_errstr(error)));
109                         talloc_free(orig);
110                         return error;
111                 }
112                 if (curend == NULL) 
113                         break;
114                 curbegin = curend + 1;
115                 curend = strchr(curbegin, '\\');
116         }
117         talloc_free(orig);
118
119         *result = reg_import_hive_key(local_parent->global.context, curkey, 
120                                                                   local_parent->path.predefined_key,
121                                                                   talloc_steal(curkey, elements));
122         
123         return WERR_OK;
124 }
125
126 WERROR local_get_predefined_key (const struct registry_context *ctx, 
127           uint32_t key_id, struct registry_key **key) 
128 {       
129         struct registry_local *rctx = talloc_get_type(ctx, struct registry_local);
130         struct mountpoint *mp;
131
132         for (mp = rctx->mountpoints; mp != NULL; mp = mp->next) {
133                 if (mp->path.predefined_key == key_id && 
134                         mp->path.elements == NULL)
135                         break;
136         }
137
138         if (mp == NULL)
139                 return WERR_NOT_FOUND;
140         
141         *key = reg_import_hive_key(ctx, mp->key, 
142                                    mp->path.predefined_key, 
143                                    mp->path.elements);
144
145         return WERR_OK;
146 }
147
148 static WERROR local_enum_key(TALLOC_CTX *mem_ctx,
149                                           const struct registry_key *key, uint32_t idx,
150                                           const char **name,
151                                           const char **keyclass,
152                                           NTTIME *last_changed_time)
153 {
154         const struct local_key *local = (const struct local_key *)key;
155
156         return hive_enum_key(mem_ctx, local->hive_key, idx, name, keyclass, 
157                                                   last_changed_time);
158 }
159
160 static WERROR local_create_key (TALLOC_CTX *mem_ctx, 
161                                                                 struct registry_key *parent_key, 
162                                                                 const char *name,
163                                                                 const char *key_class,
164                                                                 struct security_descriptor *security,
165                                                                 struct registry_key **key)
166 {
167         const struct local_key *local_parent;
168         struct hive_key *hivekey;
169         const char **elements;
170         int i;
171         const char *last_part;
172
173         last_part = strrchr(name, '\\');
174         if (last_part == NULL) {
175                 last_part = name;
176                 local_parent = (const struct local_key *)parent_key;
177         } else {
178                 W_ERROR_NOT_OK_RETURN(reg_open_key(mem_ctx, parent_key, 
179                                                           talloc_strndup(mem_ctx, name, last_part-name),
180                                                           &local_parent));
181                 last_part++;
182         }
183
184         W_ERROR_NOT_OK_RETURN(hive_key_add_name(mem_ctx, local_parent->hive_key, 
185                                                                 last_part, key_class, security, &hivekey));
186
187         if (local_parent->path.elements != NULL) {
188                 elements = talloc_array(hivekey, const char *, 
189                                                                 str_list_length(local_parent->path.elements)+2);
190                 for (i = 0; local_parent->path.elements[i] != NULL; i++) {
191                         elements[i] = talloc_reference(elements, 
192                                                                                    local_parent->path.elements[i]);
193                 }
194         } else {
195                 elements = talloc_array(hivekey, const char *, 2);
196                 i = 0;
197         }
198
199         elements[i] = talloc_strdup(elements, name);
200         elements[i+1] = NULL;
201
202         *key = reg_import_hive_key(local_parent->global.context, hivekey, 
203                                                            local_parent->path.predefined_key,
204                                                            elements);
205
206         return WERR_OK;
207 }
208
209 static WERROR local_set_value (struct registry_key *key, const char *name,
210                                                 uint32_t type, const DATA_BLOB data)
211 {
212         struct local_key *local = (struct local_key *)key;
213
214         return hive_set_value(local->hive_key, name, type, data);
215 }
216
217 static WERROR local_get_value (TALLOC_CTX *mem_ctx,
218                                                  const struct registry_key *key,
219                                                  const char *name, uint32_t *type, DATA_BLOB *data)
220 {
221         const struct local_key *local = (const struct local_key *)key;
222
223         return hive_get_value(mem_ctx, local->hive_key, name, type, data);
224 }
225
226 static WERROR local_enum_value (TALLOC_CTX *mem_ctx,
227                                                   const struct registry_key *key, uint32_t idx,
228                                                   const char **name,
229                                                   uint32_t *type,
230                                                   DATA_BLOB *data)
231 {
232         const struct local_key *local = (const struct local_key *)key;
233
234         return hive_get_value_by_index(mem_ctx, local->hive_key, idx, 
235                                                                    name, type, data);
236 }
237
238 static WERROR local_delete_key (struct registry_key *key, const char *name)
239 {
240         const struct local_key *local = (const struct local_key *)key;
241
242         return hive_key_del(local->hive_key, name);
243 }
244
245 static WERROR local_delete_value (struct registry_key *key, const char *name)
246 {
247         const struct local_key *local = (const struct local_key *)key;
248
249         return hive_del_value(local->hive_key, name);
250 }
251
252 static WERROR local_flush_key (struct registry_key *key)
253 {
254         const struct local_key *local = (const struct local_key *)key;
255
256         return hive_key_flush(local->hive_key);
257 }
258
259 static WERROR local_get_key_info (TALLOC_CTX *mem_ctx,
260                                                 const struct registry_key *key,
261                                                 const char **classname,
262                                                 uint32_t *num_subkeys,
263                                                 uint32_t *num_values,
264                                                 NTTIME *last_change_time)
265 {
266         const struct local_key *local = (const struct local_key *)key;
267
268         return hive_key_get_info(mem_ctx, local->hive_key, 
269                                                          classname, num_subkeys, num_values, 
270                                                          last_change_time);
271 }
272
273 const static struct registry_operations local_ops = {
274         .name = "local",
275         .open_key = local_open_key,
276         .get_predefined_key = local_get_predefined_key,
277         .enum_key = local_enum_key,
278         .create_key = local_create_key,
279         .set_value = local_set_value,
280         .get_value = local_get_value,
281         .enum_value = local_enum_value,
282         .delete_key = local_delete_key,
283         .delete_value = local_delete_value,
284         .flush_key = local_flush_key,
285         .get_key_info = local_get_key_info,
286 };
287
288 WERROR reg_open_local(TALLOC_CTX *mem_ctx, struct registry_context **ctx, 
289                                 struct auth_session_info *session_info, 
290                                 struct cli_credentials *credentials)
291 {
292         struct registry_local *ret = talloc_zero(mem_ctx, struct registry_local);
293
294         W_ERROR_HAVE_NO_MEMORY(ret);
295
296         ret->registry.ops = &local_ops;
297         ret->session_info = session_info;
298         ret->credentials = credentials;
299
300         *ctx = (struct registry_context *)ret;
301         
302         return WERR_OK;
303 }
304
305 WERROR reg_mount_hive(struct registry_context *rctx, 
306                                           struct hive_key *hive_key,
307                                           uint32_t key_id,
308                                           const char **elements) 
309 {
310         struct registry_local *reg_local = talloc_get_type(rctx, struct registry_local);
311         struct mountpoint *mp = talloc(rctx, struct mountpoint);
312         int i = 0;
313
314         mp->path.predefined_key = key_id;
315         mp->prev = mp->next = NULL;
316         mp->key = hive_key;
317         if (elements != NULL) {
318                 mp->path.elements = talloc_array(mp, const char *, 
319                                                                                  str_list_length(elements));
320                 for (i = 0; elements[i] != NULL; i++) {
321                         mp->path.elements[i] = talloc_reference(mp->path.elements, 
322                                                                                                         elements[i]);
323                 }
324                 mp->path.elements[i] = NULL;
325         } else {
326                 mp->path.elements = NULL;
327         }
328
329         DLIST_ADD(reg_local->mountpoints, mp);
330
331         return WERR_OK;
332 }