2 Unix SMB/CIFS implementation.
3 Transparent registry backend handling
4 Copyright (C) Jelmer Vernooij 2003-2004.
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.
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.
22 #include "dlinklist.h"
26 #define DBGC_CLASS DBGC_REGISTRY
28 /* List of available backends */
29 static struct reg_init_function_entry *backends = NULL;
31 static struct reg_init_function_entry *reg_find_backend_entry(const char *name);
33 /* Register new backend */
34 NTSTATUS registry_register(const void *_hive_ops)
36 const struct hive_operations *hive_ops = _hive_ops;
37 struct reg_init_function_entry *entry = backends;
39 DEBUG(5,("Attempting to register registry backend %s\n", hive_ops->name));
41 /* Check for duplicates */
42 if (reg_find_backend_entry(hive_ops->name)) {
43 DEBUG(0,("There already is a registry backend registered with the name %s!\n", hive_ops->name));
44 return NT_STATUS_OBJECT_NAME_COLLISION;
47 entry = talloc(talloc_autofree_context(), struct reg_init_function_entry);
48 entry->hive_functions = hive_ops;
50 DLIST_ADD(backends, entry);
51 DEBUG(5,("Successfully added registry backend '%s'\n", hive_ops->name));
55 /* Find a backend in the list of available backends */
56 static struct reg_init_function_entry *reg_find_backend_entry(const char *name)
58 struct reg_init_function_entry *entry;
63 if (strcmp(entry->hive_functions->name, name) == 0) return entry;
70 /* Check whether a certain backend is present */
71 BOOL reg_has_backend(const char *backend)
73 return reg_find_backend_entry(backend) != NULL?True:False;
81 {HKEY_CLASSES_ROOT,"HKEY_CLASSES_ROOT" },
82 {HKEY_CURRENT_USER,"HKEY_CURRENT_USER" },
83 {HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" },
84 {HKEY_PERFORMANCE_DATA, "HKEY_PERFORMANCE_DATA" },
85 {HKEY_USERS, "HKEY_USERS" },
86 {HKEY_CURRENT_CONFIG, "HKEY_CURRENT_CONFIG" },
87 {HKEY_DYN_DATA, "HKEY_DYN_DATA" },
88 {HKEY_PERFORMANCE_TEXT, "HKEY_PERFORMANCE_TEXT" },
89 {HKEY_PERFORMANCE_NLSTEXT, "HKEY_PERFORMANCE_NLSTEXT" },
93 int reg_list_predefs(TALLOC_CTX *mem_ctx, char ***predefs, uint32_t **hkeys)
96 *predefs = talloc_array(mem_ctx, char *, ARRAY_SIZE(predef_names));
97 *hkeys = talloc_array(mem_ctx, uint32_t, ARRAY_SIZE(predef_names));
99 for (i = 0; predef_names[i].name; i++) {
100 (*predefs)[i] = talloc_strdup(mem_ctx, predef_names[i].name);
101 (*hkeys)[i] = predef_names[i].handle;
107 const char *reg_get_predef_name(uint32_t hkey)
110 for (i = 0; predef_names[i].name; i++) {
111 if (predef_names[i].handle == hkey) return predef_names[i].name;
117 WERROR reg_get_predefined_key_by_name(struct registry_context *ctx, const char *name, struct registry_key **key)
121 for (i = 0; predef_names[i].name; i++) {
122 if (!strcasecmp(predef_names[i].name, name)) return reg_get_predefined_key(ctx, predef_names[i].handle, key);
125 DEBUG(1, ("No predefined key with name '%s'\n", name));
130 WERROR reg_close (struct registry_context *ctx)
137 WERROR reg_get_predefined_key(struct registry_context *ctx, uint32_t hkey, struct registry_key **key)
139 WERROR ret = ctx->get_predefined_key(ctx, hkey, key);
141 if (W_ERROR_IS_OK(ret)) {
142 (*key)->name = talloc_strdup(*key, reg_get_predef_name(hkey));
149 /* Open a registry file/host/etc */
150 WERROR reg_open_hive(TALLOC_CTX *parent_ctx, const char *backend, const char *location, const char *credentials, struct registry_key **root)
152 struct registry_hive *rethive;
153 struct registry_key *retkey = NULL;
154 struct reg_init_function_entry *entry;
157 entry = reg_find_backend_entry(backend);
160 DEBUG(0, ("No such registry backend '%s' loaded!\n", backend));
161 return WERR_GENERAL_FAILURE;
164 if(!entry->hive_functions || !entry->hive_functions->open_hive) {
165 return WERR_NOT_SUPPORTED;
168 rethive = talloc(parent_ctx, struct registry_hive);
169 rethive->location = location?talloc_strdup(rethive, location):NULL;
170 rethive->functions = entry->hive_functions;
171 rethive->backend_data = NULL;
173 werr = entry->hive_functions->open_hive(rethive, &retkey);
175 if(!W_ERROR_IS_OK(werr)) {
180 DEBUG(0, ("Backend %s didn't provide root key!\n", backend));
181 return WERR_GENERAL_FAILURE;
184 rethive->root = retkey;
186 retkey->hive = rethive;
188 retkey->path = talloc_strdup(retkey, "");
196 * First tries to use the open_key function from the backend
197 * then falls back to get_subkey_by_name and later get_subkey_by_index
199 WERROR reg_open_key(TALLOC_CTX *mem_ctx, struct registry_key *parent, const char *name, struct registry_key **result)
204 DEBUG(0, ("Invalid parent key specified for open of '%s'\n", name));
205 return WERR_INVALID_PARAM;
208 if(!parent->hive->functions->open_key &&
209 (parent->hive->functions->get_subkey_by_name ||
210 parent->hive->functions->get_subkey_by_index)) {
211 char *orig = strdup(name),
213 *curend = strchr(orig, '\\');
214 struct registry_key *curkey = parent;
216 while(curbegin && *curbegin) {
217 if(curend)*curend = '\0';
218 error = reg_key_get_subkey_by_name(mem_ctx, curkey, curbegin, &curkey);
219 if(!W_ERROR_IS_OK(error)) {
224 curbegin = curend + 1;
225 curend = strchr(curbegin, '\\');
234 if(!parent->hive->functions->open_key) {
235 DEBUG(0, ("Registry backend doesn't have get_subkey_by_name nor open_key!\n"));
236 return WERR_NOT_SUPPORTED;
239 error = parent->hive->functions->open_key(mem_ctx, parent, name, result);
241 if(!W_ERROR_IS_OK(error)) return error;
243 (*result)->hive = parent->hive;
244 (*result)->path = ((parent->hive->root == parent)?talloc_strdup(mem_ctx, name):talloc_asprintf(mem_ctx, "%s\\%s", parent->path, name));
245 (*result)->hive = parent->hive;
250 WERROR reg_key_get_value_by_index(TALLOC_CTX *mem_ctx, struct registry_key *key, int idx, struct registry_value **val)
252 if(!key) return WERR_INVALID_PARAM;
254 if(key->hive->functions->get_value_by_index) {
255 WERROR status = key->hive->functions->get_value_by_index(mem_ctx, key, idx, val);
256 if(!W_ERROR_IS_OK(status))
259 return WERR_NOT_SUPPORTED;
265 WERROR reg_key_num_subkeys(struct registry_key *key, int *count)
267 if(!key) return WERR_INVALID_PARAM;
269 if(key->hive->functions->num_subkeys) {
270 return key->hive->functions->num_subkeys(key, count);
273 if(key->hive->functions->get_subkey_by_index) {
276 struct registry_key *dest = NULL;
277 TALLOC_CTX *mem_ctx = talloc_init("num_subkeys");
279 for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx, key, i, &dest)); i++);
280 talloc_free(mem_ctx);
283 if(W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) error = WERR_OK;
287 return WERR_NOT_SUPPORTED;
290 WERROR reg_key_num_values(struct registry_key *key, int *count)
293 if(!key) return WERR_INVALID_PARAM;
295 if (key->hive->functions->num_values) {
296 return key->hive->functions->num_values(key, count);
299 if(key->hive->functions->get_value_by_index) {
302 struct registry_value *dest;
303 TALLOC_CTX *mem_ctx = talloc_init("num_subkeys");
305 for(i = 0; W_ERROR_IS_OK(error = key->hive->functions->get_value_by_index(mem_ctx, key, i, &dest)); i++);
306 talloc_free(mem_ctx);
309 if(W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) error = WERR_OK;
313 return WERR_NOT_SUPPORTED;
316 WERROR reg_key_get_subkey_by_index(TALLOC_CTX *mem_ctx, struct registry_key *key, int idx, struct registry_key **subkey)
318 if(!key) return WERR_INVALID_PARAM;
320 if(key->hive->functions->get_subkey_by_index) {
321 WERROR status = key->hive->functions->get_subkey_by_index(mem_ctx, key, idx, subkey);
322 if(!NT_STATUS_IS_OK(status)) return status;
324 return WERR_NOT_SUPPORTED;
327 if(key->hive->root == key)
328 (*subkey)->path = talloc_strdup(mem_ctx, (*subkey)->name);
330 (*subkey)->path = talloc_asprintf(mem_ctx, "%s\\%s", key->path, (*subkey)->name);
332 (*subkey)->hive = key->hive;
336 WERROR reg_key_get_subkey_by_name(TALLOC_CTX *mem_ctx, struct registry_key *key, const char *name, struct registry_key **subkey)
339 WERROR error = WERR_OK;
341 if(!key) return WERR_INVALID_PARAM;
343 if(key->hive->functions->get_subkey_by_name) {
344 error = key->hive->functions->get_subkey_by_name(mem_ctx, key,name,subkey);
345 } else if(key->hive->functions->open_key) {
346 error = key->hive->functions->open_key(mem_ctx, key, name, subkey);
347 } else if(key->hive->functions->get_subkey_by_index) {
348 for(i = 0; W_ERROR_IS_OK(error); i++) {
349 error = reg_key_get_subkey_by_index(mem_ctx, key, i, subkey);
350 if(W_ERROR_IS_OK(error) && !strcasecmp((*subkey)->name, name)) {
355 if (W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
356 error = WERR_DEST_NOT_FOUND;
358 return WERR_NOT_SUPPORTED;
361 if(!W_ERROR_IS_OK(error)) return error;
363 (*subkey)->path = talloc_asprintf(mem_ctx, "%s\\%s", key->path, (*subkey)->name);
364 (*subkey)->hive = key->hive;
369 WERROR reg_key_get_value_by_name(TALLOC_CTX *mem_ctx, struct registry_key *key, const char *name, struct registry_value **val)
372 WERROR error = WERR_OK;
374 if(!key) return WERR_INVALID_PARAM;
376 if(key->hive->functions->get_value_by_name) {
377 error = key->hive->functions->get_value_by_name(mem_ctx, key,name, val);
379 for(i = 0; W_ERROR_IS_OK(error); i++) {
380 error = reg_key_get_value_by_index(mem_ctx, key, i, val);
381 if(W_ERROR_IS_OK(error) && !strcasecmp((*val)->name, name)) {
387 if(!W_ERROR_IS_OK(error) && !W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
393 WERROR reg_key_del(struct registry_key *parent, const char *name)
396 if(!parent) return WERR_INVALID_PARAM;
399 if(!parent->hive->functions->del_key)
400 return WERR_NOT_SUPPORTED;
402 error = parent->hive->functions->del_key(parent, name);
403 if(!W_ERROR_IS_OK(error)) return error;
408 WERROR reg_key_add_name(TALLOC_CTX *mem_ctx, struct registry_key *parent, const char *name, uint32_t access_mask, struct security_descriptor *desc, struct registry_key **newkey)
412 if (!parent) return WERR_INVALID_PARAM;
414 if (!parent->hive->functions->add_key) {
415 DEBUG(1, ("Backend '%s' doesn't support method add_key\n", parent->hive->functions->name));
416 return WERR_NOT_SUPPORTED;
419 error = parent->hive->functions->add_key(mem_ctx, parent, name, access_mask, desc, newkey);
421 if(!W_ERROR_IS_OK(error)) return error;
423 (*newkey)->hive = parent->hive;
424 (*newkey)->backend_data = talloc_asprintf(mem_ctx, "%s\\%s", parent->path, name);
429 WERROR reg_val_set(struct registry_key *key, const char *value, uint32 type, void *data, int len)
431 /* A 'real' set function has preference */
432 if (key->hive->functions->set_value)
433 return key->hive->functions->set_value(key, value, type, data, len);
435 DEBUG(1, ("Backend '%s' doesn't support method set_value\n", key->hive->functions->name));
436 return WERR_NOT_SUPPORTED;
441 WERROR reg_del_value(struct registry_key *key, const char *valname)
443 WERROR ret = WERR_OK;
444 if(!key->hive->functions->del_value)
445 return WERR_NOT_SUPPORTED;
447 ret = key->hive->functions->del_value(key, valname);
449 if(!W_ERROR_IS_OK(ret)) return ret;
454 WERROR reg_save (struct registry_context *ctx, const char *location)
456 return WERR_NOT_SUPPORTED;
459 WERROR reg_key_flush(struct registry_key *key)
462 return WERR_INVALID_PARAM;
465 if (key->hive->functions->flush_key) {
466 return key->hive->functions->flush_key(key);
469 /* No need for flushing, apparently */
473 WERROR reg_key_subkeysizes(struct registry_key *key, uint32 *max_subkeylen, uint32 *max_subkeysize)
476 struct registry_key *subkey;
478 TALLOC_CTX *mem_ctx = talloc_init("subkeysize");
480 *max_subkeylen = *max_subkeysize = 0;
483 error = reg_key_get_subkey_by_index(mem_ctx, key, i, &subkey);
485 if (W_ERROR_IS_OK(error)) {
486 *max_subkeysize = MAX(*max_subkeysize, 0xFF);
487 *max_subkeylen = MAX(*max_subkeylen, strlen(subkey->name));
491 } while (W_ERROR_IS_OK(error));
493 talloc_free(mem_ctx);
498 WERROR reg_key_valuesizes(struct registry_key *key, uint32 *max_valnamelen, uint32 *max_valbufsize)
501 struct registry_value *value;
503 TALLOC_CTX *mem_ctx = talloc_init("subkeysize");
505 *max_valnamelen = *max_valbufsize = 0;
508 error = reg_key_get_value_by_index(mem_ctx, key, i, &value);
510 if (W_ERROR_IS_OK(error)) {
512 *max_valnamelen = MAX(*max_valnamelen, strlen(value->name));
514 *max_valbufsize = MAX(*max_valbufsize, value->data_len);
518 } while (W_ERROR_IS_OK(error));
520 talloc_free(mem_ctx);