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