remove needless rpath stuff for default paths as early as possible
[Samba/gebeck_regimport.git] / source4 / lib / registry / local.c
bloba3aee06b1337f63a54e0020b47d22fa3c7bd1a24
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 local_key->hive_key = talloc_steal(local_key, hive);
61 local_key->global.context = talloc_reference(local_key, ctx);
62 local_key->path = parent_path;
64 return (struct registry_key *)local_key;
68 static WERROR local_open_key(TALLOC_CTX *mem_ctx,
69 struct registry_key *parent,
70 const char *path,
71 struct registry_key **result)
73 char *orig = talloc_strdup(mem_ctx, path),
74 *curbegin = orig,
75 *curend = strchr(orig, '\\');
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 if (local_parent->path.elements != NULL) {
84 elements = talloc_array(mem_ctx, const char *,
85 str_list_length(local_parent->path.elements) + 1);
86 for (el = 0; local_parent->path.elements[el] != NULL; el++) {
87 elements[el] = talloc_reference(elements,
88 local_parent->path.elements[el]);
90 elements[el] = NULL;
91 } else {
92 elements = NULL;
93 el = 0;
96 while (curbegin != NULL && *curbegin) {
97 if (curend != NULL)
98 *curend = '\0';
99 elements = talloc_realloc(mem_ctx, elements, const char *, el+2);
100 elements[el] = talloc_strdup(elements, curbegin);
101 el++;
102 elements[el] = NULL;
103 error = hive_get_key_by_name(mem_ctx, curkey,
104 curbegin, &curkey);
105 if (!W_ERROR_IS_OK(error)) {
106 DEBUG(2, ("Opening key %s failed: %s\n", curbegin,
107 win_errstr(error)));
108 talloc_free(orig);
109 return error;
111 if (curend == NULL)
112 break;
113 curbegin = curend + 1;
114 curend = strchr(curbegin, '\\');
116 talloc_free(orig);
118 *result = reg_import_hive_key(local_parent->global.context, curkey,
119 local_parent->path.predefined_key,
120 talloc_steal(curkey, elements));
122 return WERR_OK;
125 WERROR local_get_predefined_key(struct registry_context *ctx,
126 uint32_t key_id, struct registry_key **key)
128 struct registry_local *rctx = talloc_get_type(ctx,
129 struct registry_local);
130 struct mountpoint *mp;
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;
138 if (mp == NULL)
139 return WERR_BADFILE;
141 *key = reg_import_hive_key(ctx, mp->key,
142 mp->path.predefined_key,
143 mp->path.elements);
145 return WERR_OK;
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)
154 const struct local_key *local = (const struct local_key *)key;
156 return hive_enum_key(mem_ctx, local->hive_key, idx, name, keyclass,
157 last_changed_time);
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)
167 struct local_key *local_parent;
168 struct hive_key *hivekey;
169 const char **elements;
170 int i;
171 const char *last_part;
173 last_part = strrchr(name, '\\');
174 if (last_part == NULL) {
175 last_part = name;
176 local_parent = (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 (struct registry_key **)&local_parent));
181 last_part++;
184 W_ERROR_NOT_OK_RETURN(hive_key_add_name(mem_ctx, local_parent->hive_key,
185 last_part, key_class, security,
186 &hivekey));
188 if (local_parent->path.elements != NULL) {
189 elements = talloc_array(hivekey, const char *,
190 str_list_length(local_parent->path.elements)+2);
191 for (i = 0; local_parent->path.elements[i] != NULL; i++) {
192 elements[i] = talloc_reference(elements,
193 local_parent->path.elements[i]);
195 } else {
196 elements = talloc_array(hivekey, const char *, 2);
197 i = 0;
200 elements[i] = talloc_strdup(elements, name);
201 elements[i+1] = NULL;
203 *key = reg_import_hive_key(local_parent->global.context, hivekey,
204 local_parent->path.predefined_key,
205 elements);
207 return WERR_OK;
210 static WERROR local_set_value(struct registry_key *key, const char *name,
211 uint32_t type, const DATA_BLOB data)
213 struct local_key *local = (struct local_key *)key;
215 return hive_key_set_value(local->hive_key, name, type, data);
218 static WERROR local_get_value(TALLOC_CTX *mem_ctx,
219 const struct registry_key *key,
220 const char *name, uint32_t *type, DATA_BLOB *data)
222 const struct local_key *local = (const struct local_key *)key;
224 return hive_get_value(mem_ctx, local->hive_key, name, type, data);
227 static WERROR local_enum_value(TALLOC_CTX *mem_ctx,
228 const struct registry_key *key, uint32_t idx,
229 const char **name,
230 uint32_t *type,
231 DATA_BLOB *data)
233 const struct local_key *local = (const struct local_key *)key;
235 return hive_get_value_by_index(mem_ctx, local->hive_key, idx,
236 name, type, data);
239 static WERROR local_delete_key(struct registry_key *key, const char *name)
241 const struct local_key *local = (const struct local_key *)key;
243 return hive_key_del(local->hive_key, name);
246 static WERROR local_delete_value(struct registry_key *key, const char *name)
248 const struct local_key *local = (const struct local_key *)key;
250 return hive_key_del_value(local->hive_key, name);
253 static WERROR local_flush_key(struct registry_key *key)
255 const struct local_key *local = (const struct local_key *)key;
257 return hive_key_flush(local->hive_key);
260 static WERROR local_get_key_info(TALLOC_CTX *mem_ctx,
261 const struct registry_key *key,
262 const char **classname,
263 uint32_t *num_subkeys,
264 uint32_t *num_values,
265 NTTIME *last_change_time,
266 uint32_t *max_subkeynamelen,
267 uint32_t *max_valnamelen,
268 uint32_t *max_valbufsize)
270 const struct local_key *local = (const struct local_key *)key;
272 return hive_key_get_info(mem_ctx, local->hive_key,
273 classname, num_subkeys, num_values,
274 last_change_time, max_subkeynamelen,
275 max_valnamelen, max_valbufsize);
277 static WERROR local_get_sec_desc(TALLOC_CTX *mem_ctx,
278 const struct registry_key *key,
279 struct security_descriptor **security)
281 const struct local_key *local = (const struct local_key *)key;
283 return hive_get_sec_desc(mem_ctx, local->hive_key, security);
285 static WERROR local_set_sec_desc(struct registry_key *key,
286 const struct security_descriptor *security)
288 const struct local_key *local = (const struct local_key *)key;
290 return hive_set_sec_desc(local->hive_key, security);
292 const static struct registry_operations local_ops = {
293 .name = "local",
294 .open_key = local_open_key,
295 .get_predefined_key = local_get_predefined_key,
296 .enum_key = local_enum_key,
297 .create_key = local_create_key,
298 .set_value = local_set_value,
299 .get_value = local_get_value,
300 .enum_value = local_enum_value,
301 .delete_key = local_delete_key,
302 .delete_value = local_delete_value,
303 .flush_key = local_flush_key,
304 .get_key_info = local_get_key_info,
305 .get_sec_desc = local_get_sec_desc,
306 .set_sec_desc = local_set_sec_desc,
309 WERROR reg_open_local(TALLOC_CTX *mem_ctx, struct registry_context **ctx)
311 struct registry_local *ret = talloc_zero(mem_ctx,
312 struct registry_local);
314 W_ERROR_HAVE_NO_MEMORY(ret);
316 ret->ops = &local_ops;
318 *ctx = (struct registry_context *)ret;
320 return WERR_OK;
323 WERROR reg_mount_hive(struct registry_context *rctx,
324 struct hive_key *hive_key,
325 uint32_t key_id,
326 const char **elements)
328 struct registry_local *reg_local = talloc_get_type(rctx,
329 struct registry_local);
330 struct mountpoint *mp = talloc(rctx, struct mountpoint);
331 int i = 0;
333 mp->path.predefined_key = key_id;
334 mp->prev = mp->next = NULL;
335 mp->key = hive_key;
336 if (elements != NULL && str_list_length(elements) != 0) {
337 mp->path.elements = talloc_array(mp, const char *,
338 str_list_length(elements));
339 for (i = 0; elements[i] != NULL; i++) {
340 mp->path.elements[i] = talloc_reference(mp->path.elements,
341 elements[i]);
343 mp->path.elements[i] = NULL;
344 } else {
345 mp->path.elements = NULL;
348 DLIST_ADD(reg_local->mountpoints, mp);
350 return WERR_OK;