tevent: Added a description for tevent queue.
[Samba/ekacnet.git] / source4 / lib / registry / local.c
blobbe48729f79f77115cc2f8eff9c9f211f279d7294
1 /*
2 Unix SMB/CIFS implementation.
3 Transparent registry backend handling
4 Copyright (C) Jelmer Vernooij 2003-2007.
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.
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.
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.
21 #include "includes.h"
22 #include "../lib/util/dlinklist.h"
23 #include "lib/registry/registry.h"
24 #include "system/filesys.h"
26 struct reg_key_path {
27 uint32_t predefined_key;
28 const char **elements;
31 struct registry_local {
32 const struct registry_operations *ops;
34 struct mountpoint {
35 struct reg_key_path path;
36 struct hive_key *key;
37 struct mountpoint *prev, *next;
38 } *mountpoints;
41 struct local_key {
42 struct registry_key global;
43 struct reg_key_path path;
44 struct hive_key *hive_key;
48 struct registry_key *reg_import_hive_key(struct registry_context *ctx,
49 struct hive_key *hive,
50 uint32_t predefined_key,
51 const char **elements)
53 struct local_key *local_key;
54 struct reg_key_path parent_path;
56 parent_path.predefined_key = predefined_key;
57 parent_path.elements = elements;
59 local_key = talloc(ctx, struct local_key);
60 if (local_key != NULL) {
61 local_key->hive_key = talloc_reference(local_key, hive);
62 local_key->global.context = talloc_reference(local_key, ctx);
63 local_key->path = parent_path;
66 return (struct registry_key *)local_key;
70 static WERROR local_open_key(TALLOC_CTX *mem_ctx,
71 struct registry_key *parent,
72 const char *path,
73 struct registry_key **result)
75 char *orig, *curbegin, *curend;
76 struct local_key *local_parent = talloc_get_type(parent,
77 struct local_key);
78 struct hive_key *curkey = local_parent->hive_key;
79 WERROR error;
80 const char **elements = NULL;
81 int el;
83 orig = talloc_strdup(mem_ctx, path);
84 W_ERROR_HAVE_NO_MEMORY(orig);
85 curbegin = orig;
86 curend = strchr(orig, '\\');
88 if (local_parent->path.elements != NULL) {
89 elements = talloc_array(mem_ctx, const char *,
90 str_list_length(local_parent->path.elements) + 1);
91 W_ERROR_HAVE_NO_MEMORY(elements);
92 for (el = 0; local_parent->path.elements[el] != NULL; el++) {
93 elements[el] = talloc_reference(elements,
94 local_parent->path.elements[el]);
96 elements[el] = NULL;
97 } else {
98 elements = NULL;
99 el = 0;
102 while (curbegin != NULL && *curbegin) {
103 if (curend != NULL)
104 *curend = '\0';
105 elements = talloc_realloc(mem_ctx, elements, const char *, el+2);
106 W_ERROR_HAVE_NO_MEMORY(elements);
107 elements[el] = talloc_strdup(elements, curbegin);
108 W_ERROR_HAVE_NO_MEMORY(elements[el]);
109 el++;
110 elements[el] = NULL;
111 error = hive_get_key_by_name(mem_ctx, curkey,
112 curbegin, &curkey);
113 if (!W_ERROR_IS_OK(error)) {
114 DEBUG(2, ("Opening key %s failed: %s\n", curbegin,
115 win_errstr(error)));
116 talloc_free(orig);
117 return error;
119 if (curend == NULL)
120 break;
121 curbegin = curend + 1;
122 curend = strchr(curbegin, '\\');
124 talloc_free(orig);
126 *result = reg_import_hive_key(local_parent->global.context, curkey,
127 local_parent->path.predefined_key,
128 talloc_steal(curkey, elements));
130 return WERR_OK;
133 WERROR local_get_predefined_key(struct registry_context *ctx,
134 uint32_t key_id, struct registry_key **key)
136 struct registry_local *rctx = talloc_get_type(ctx,
137 struct registry_local);
138 struct mountpoint *mp;
140 for (mp = rctx->mountpoints; mp != NULL; mp = mp->next) {
141 if (mp->path.predefined_key == key_id &&
142 mp->path.elements == NULL)
143 break;
146 if (mp == NULL)
147 return WERR_BADFILE;
149 *key = reg_import_hive_key(ctx, mp->key,
150 mp->path.predefined_key,
151 mp->path.elements);
153 return WERR_OK;
156 static WERROR local_enum_key(TALLOC_CTX *mem_ctx,
157 const struct registry_key *key, uint32_t idx,
158 const char **name,
159 const char **keyclass,
160 NTTIME *last_changed_time)
162 const struct local_key *local = (const struct local_key *)key;
164 return hive_enum_key(mem_ctx, local->hive_key, idx, name, keyclass,
165 last_changed_time);
168 static WERROR local_create_key(TALLOC_CTX *mem_ctx,
169 struct registry_key *parent_key,
170 const char *name,
171 const char *key_class,
172 struct security_descriptor *security,
173 struct registry_key **key)
175 struct local_key *local_parent;
176 struct hive_key *hivekey;
177 const char **elements;
178 unsigned int i;
179 const char *last_part;
180 char *trunc_name;
182 last_part = strrchr(name, '\\');
183 if (last_part == NULL) {
184 last_part = name;
185 local_parent = (struct local_key *)parent_key;
186 } else {
187 trunc_name = talloc_strndup(mem_ctx, name, last_part - name);
188 W_ERROR_HAVE_NO_MEMORY(trunc_name);
189 W_ERROR_NOT_OK_RETURN(reg_open_key(mem_ctx, parent_key,
190 trunc_name,
191 (struct registry_key **)&local_parent));
192 talloc_free(trunc_name);
193 last_part++;
196 W_ERROR_NOT_OK_RETURN(hive_key_add_name(mem_ctx, local_parent->hive_key,
197 last_part, key_class, security,
198 &hivekey));
200 if (local_parent->path.elements != NULL) {
201 elements = talloc_array(hivekey, const char *,
202 str_list_length(local_parent->path.elements)+2);
203 W_ERROR_HAVE_NO_MEMORY(elements);
204 for (i = 0; local_parent->path.elements[i] != NULL; i++) {
205 elements[i] = talloc_reference(elements,
206 local_parent->path.elements[i]);
208 } else {
209 elements = talloc_array(hivekey, const char *, 2);
210 W_ERROR_HAVE_NO_MEMORY(elements);
211 i = 0;
214 elements[i] = talloc_strdup(elements, name);
215 W_ERROR_HAVE_NO_MEMORY(elements[i]);
216 elements[i+1] = NULL;
218 *key = reg_import_hive_key(local_parent->global.context, hivekey,
219 local_parent->path.predefined_key,
220 elements);
222 return WERR_OK;
225 static WERROR local_set_value(struct registry_key *key, const char *name,
226 uint32_t type, const DATA_BLOB data)
228 struct local_key *local = (struct local_key *)key;
230 return hive_key_set_value(local->hive_key, name, type, data);
233 static WERROR local_get_value(TALLOC_CTX *mem_ctx,
234 const struct registry_key *key,
235 const char *name, uint32_t *type, DATA_BLOB *data)
237 const struct local_key *local = (const struct local_key *)key;
239 return hive_get_value(mem_ctx, local->hive_key, name, type, data);
242 static WERROR local_enum_value(TALLOC_CTX *mem_ctx,
243 const struct registry_key *key, uint32_t idx,
244 const char **name,
245 uint32_t *type,
246 DATA_BLOB *data)
248 const struct local_key *local = (const struct local_key *)key;
250 return hive_get_value_by_index(mem_ctx, local->hive_key, idx,
251 name, type, data);
254 static WERROR local_delete_key(TALLOC_CTX *mem_ctx, struct registry_key *key,
255 const char *name)
257 const struct local_key *local = (const struct local_key *)key;
259 return hive_key_del(mem_ctx, local->hive_key, name);
262 static WERROR local_delete_value(TALLOC_CTX *mem_ctx, struct registry_key *key,
263 const char *name)
265 const struct local_key *local = (const struct local_key *)key;
267 return hive_key_del_value(mem_ctx, local->hive_key, name);
270 static WERROR local_flush_key(struct registry_key *key)
272 const struct local_key *local = (const struct local_key *)key;
274 return hive_key_flush(local->hive_key);
277 static WERROR local_get_key_info(TALLOC_CTX *mem_ctx,
278 const struct registry_key *key,
279 const char **classname,
280 uint32_t *num_subkeys,
281 uint32_t *num_values,
282 NTTIME *last_change_time,
283 uint32_t *max_subkeynamelen,
284 uint32_t *max_valnamelen,
285 uint32_t *max_valbufsize)
287 const struct local_key *local = (const struct local_key *)key;
289 return hive_key_get_info(mem_ctx, local->hive_key,
290 classname, num_subkeys, num_values,
291 last_change_time, max_subkeynamelen,
292 max_valnamelen, max_valbufsize);
294 static WERROR local_get_sec_desc(TALLOC_CTX *mem_ctx,
295 const struct registry_key *key,
296 struct security_descriptor **security)
298 const struct local_key *local = (const struct local_key *)key;
300 return hive_get_sec_desc(mem_ctx, local->hive_key, security);
302 static WERROR local_set_sec_desc(struct registry_key *key,
303 const struct security_descriptor *security)
305 const struct local_key *local = (const struct local_key *)key;
307 return hive_set_sec_desc(local->hive_key, security);
309 const static struct registry_operations local_ops = {
310 .name = "local",
311 .open_key = local_open_key,
312 .get_predefined_key = local_get_predefined_key,
313 .enum_key = local_enum_key,
314 .create_key = local_create_key,
315 .set_value = local_set_value,
316 .get_value = local_get_value,
317 .enum_value = local_enum_value,
318 .delete_key = local_delete_key,
319 .delete_value = local_delete_value,
320 .flush_key = local_flush_key,
321 .get_key_info = local_get_key_info,
322 .get_sec_desc = local_get_sec_desc,
323 .set_sec_desc = local_set_sec_desc,
326 WERROR reg_open_local(TALLOC_CTX *mem_ctx, struct registry_context **ctx)
328 struct registry_local *ret = talloc_zero(mem_ctx,
329 struct registry_local);
331 W_ERROR_HAVE_NO_MEMORY(ret);
333 ret->ops = &local_ops;
335 *ctx = (struct registry_context *)ret;
337 return WERR_OK;
340 WERROR reg_mount_hive(struct registry_context *rctx,
341 struct hive_key *hive_key,
342 uint32_t key_id,
343 const char **elements)
345 struct registry_local *reg_local = talloc_get_type(rctx,
346 struct registry_local);
347 struct mountpoint *mp;
348 unsigned int i = 0;
350 mp = talloc(rctx, struct mountpoint);
351 W_ERROR_HAVE_NO_MEMORY(mp);
352 mp->path.predefined_key = key_id;
353 mp->prev = mp->next = NULL;
354 mp->key = hive_key;
355 if (elements != NULL && str_list_length(elements) != 0) {
356 mp->path.elements = talloc_array(mp, const char *,
357 str_list_length(elements));
358 W_ERROR_HAVE_NO_MEMORY(mp->path.elements);
359 for (i = 0; elements[i] != NULL; i++) {
360 mp->path.elements[i] = talloc_reference(mp->path.elements,
361 elements[i]);
363 mp->path.elements[i] = NULL;
364 } else {
365 mp->path.elements = NULL;
368 DLIST_ADD(reg_local->mountpoints, mp);
370 return WERR_OK;