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 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, see <http://www.gnu.org/licenses/>.
21 #include "lib/util/dlinklist.h"
22 #include "lib/registry/registry.h"
27 * @brief Main registry functions
30 /* List of available backends */
31 static struct reg_init_function_entry *backends = NULL;
33 static struct reg_init_function_entry *reg_find_backend_entry(const char *name);
35 /** Register a new backend. */
36 _PUBLIC_ NTSTATUS registry_register(const void *_hive_ops)
38 const struct hive_operations *hive_ops = _hive_ops;
39 struct reg_init_function_entry *entry = backends;
41 DEBUG(5,("Attempting to register registry backend %s\n", hive_ops->name));
43 /* Check for duplicates */
44 if (reg_find_backend_entry(hive_ops->name)) {
45 DEBUG(0,("There already is a registry backend registered with the name %s!\n", hive_ops->name));
46 return NT_STATUS_OBJECT_NAME_COLLISION;
49 entry = talloc(talloc_autofree_context(), struct reg_init_function_entry);
50 entry->hive_functions = hive_ops;
52 DLIST_ADD(backends, entry);
53 DEBUG(5,("Successfully added registry backend '%s'\n", hive_ops->name));
57 /** Find a backend in the list of available backends */
58 static struct reg_init_function_entry *reg_find_backend_entry(const char *name)
60 struct reg_init_function_entry *entry;
65 if (strcmp(entry->hive_functions->name, name) == 0) return entry;
72 /** Initialize the registry subsystem */
73 _PUBLIC_ NTSTATUS registry_init(void)
75 init_module_fn static_init[] = STATIC_registry_MODULES;
76 init_module_fn *shared_init = load_samba_modules(NULL, "registry");
78 run_init_functions(static_init);
79 run_init_functions(shared_init);
81 talloc_free(shared_init);
86 /** Check whether a certain backend is present. */
87 _PUBLIC_ BOOL reg_has_backend(const char *backend)
89 return reg_find_backend_entry(backend) != NULL?True:False;
92 const struct reg_predefined_key reg_predefined_keys[] = {
93 {HKEY_CLASSES_ROOT,"HKEY_CLASSES_ROOT" },
94 {HKEY_CURRENT_USER,"HKEY_CURRENT_USER" },
95 {HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" },
96 {HKEY_PERFORMANCE_DATA, "HKEY_PERFORMANCE_DATA" },
97 {HKEY_USERS, "HKEY_USERS" },
98 {HKEY_CURRENT_CONFIG, "HKEY_CURRENT_CONFIG" },
99 {HKEY_DYN_DATA, "HKEY_DYN_DATA" },
100 {HKEY_PERFORMANCE_TEXT, "HKEY_PERFORMANCE_TEXT" },
101 {HKEY_PERFORMANCE_NLSTEXT, "HKEY_PERFORMANCE_NLSTEXT" },
105 /** Obtain a list of predefined keys. */
106 _PUBLIC_ int reg_list_predefs(TALLOC_CTX *mem_ctx, char ***predefs, uint32_t **hkeys)
109 *predefs = talloc_array(mem_ctx, char *, ARRAY_SIZE(reg_predefined_keys));
110 *hkeys = talloc_array(mem_ctx, uint32_t, ARRAY_SIZE(reg_predefined_keys));
112 for (i = 0; reg_predefined_keys[i].name; i++) {
113 (*predefs)[i] = talloc_strdup(mem_ctx, reg_predefined_keys[i].name);
114 (*hkeys)[i] = reg_predefined_keys[i].handle;
120 /** Obtain name of specific hkey. */
121 _PUBLIC_ const char *reg_get_predef_name(uint32_t hkey)
124 for (i = 0; reg_predefined_keys[i].name; i++) {
125 if (reg_predefined_keys[i].handle == hkey) return reg_predefined_keys[i].name;
131 /** Get predefined key by name. */
132 _PUBLIC_ WERROR reg_get_predefined_key_by_name(struct registry_context *ctx, const char *name, struct registry_key **key)
136 for (i = 0; reg_predefined_keys[i].name; i++) {
137 if (!strcasecmp(reg_predefined_keys[i].name, name)) return reg_get_predefined_key(ctx, reg_predefined_keys[i].handle, key);
140 DEBUG(1, ("No predefined key with name '%s'\n", name));
145 /** Get predefined key by id. */
146 _PUBLIC_ WERROR reg_get_predefined_key(struct registry_context *ctx, uint32_t hkey, struct registry_key **key)
148 WERROR ret = ctx->get_predefined_key(ctx, hkey, key);
150 if (W_ERROR_IS_OK(ret)) {
151 (*key)->name = talloc_strdup(*key, reg_get_predef_name(hkey));
158 /** Open a registry file/host/etc */
159 _PUBLIC_ WERROR reg_open_hive(TALLOC_CTX *parent_ctx, const char *backend, const char *location, struct auth_session_info *session_info, struct cli_credentials *credentials, struct registry_key **root)
161 struct registry_hive *rethive;
162 struct registry_key *retkey = NULL;
163 struct reg_init_function_entry *entry;
166 entry = reg_find_backend_entry(backend);
169 DEBUG(0, ("No such registry backend '%s' loaded!\n", backend));
170 return WERR_GENERAL_FAILURE;
173 if(!entry->hive_functions || !entry->hive_functions->open_hive) {
174 return WERR_NOT_SUPPORTED;
177 rethive = talloc(parent_ctx, struct registry_hive);
178 rethive->location = location?talloc_strdup(rethive, location):NULL;
179 rethive->session_info = talloc_reference(rethive, session_info);
180 rethive->credentials = talloc_reference(rethive, credentials);
181 rethive->functions = entry->hive_functions;
182 rethive->backend_data = NULL;
184 werr = entry->hive_functions->open_hive(rethive, &retkey);
186 if(!W_ERROR_IS_OK(werr)) {
191 DEBUG(0, ("Backend %s didn't provide root key!\n", backend));
192 return WERR_GENERAL_FAILURE;
195 rethive->root = retkey;
197 retkey->hive = rethive;
199 retkey->path = talloc_strdup(retkey, "");
208 * First tries to use the open_key function from the backend
209 * then falls back to get_subkey_by_name and later get_subkey_by_index
211 _PUBLIC_ WERROR reg_open_key(TALLOC_CTX *mem_ctx, struct registry_key *parent, const char *name, struct registry_key **result)
216 DEBUG(0, ("Invalid parent key specified for open of '%s'\n", name));
217 return WERR_INVALID_PARAM;
220 if(!parent->hive->functions->open_key &&
221 (parent->hive->functions->get_subkey_by_name ||
222 parent->hive->functions->get_subkey_by_index)) {
223 char *orig = strdup(name),
225 *curend = strchr(orig, '\\');
226 struct registry_key *curkey = parent;
228 while(curbegin && *curbegin) {
229 if(curend)*curend = '\0';
230 error = reg_key_get_subkey_by_name(mem_ctx, curkey, curbegin, &curkey);
231 if(!W_ERROR_IS_OK(error)) {
236 curbegin = curend + 1;
237 curend = strchr(curbegin, '\\');
246 if(!parent->hive->functions->open_key) {
247 DEBUG(0, ("Registry backend doesn't have get_subkey_by_name nor open_key!\n"));
248 return WERR_NOT_SUPPORTED;
251 error = parent->hive->functions->open_key(mem_ctx, parent, name, result);
253 if(!W_ERROR_IS_OK(error)) return error;
255 (*result)->hive = parent->hive;
256 (*result)->path = ((parent->hive->root == parent)?talloc_strdup(mem_ctx, name):talloc_asprintf(mem_ctx, "%s\\%s", parent->path, name));
257 (*result)->hive = parent->hive;
265 _PUBLIC_ WERROR reg_key_get_value_by_index(TALLOC_CTX *mem_ctx, const struct registry_key *key, int idx, struct registry_value **val)
267 if(!key) return WERR_INVALID_PARAM;
269 if(key->hive->functions->get_value_by_index) {
270 WERROR status = key->hive->functions->get_value_by_index(mem_ctx, key, idx, val);
271 if(!W_ERROR_IS_OK(status))
274 return WERR_NOT_SUPPORTED;
281 * Get the number of subkeys.
283 _PUBLIC_ WERROR reg_key_num_subkeys(const struct registry_key *key, uint32_t *count)
285 if(!key) return WERR_INVALID_PARAM;
287 if(key->hive->functions->num_subkeys) {
288 return key->hive->functions->num_subkeys(key, count);
291 if(key->hive->functions->get_subkey_by_index) {
294 struct registry_key *dest = NULL;
295 TALLOC_CTX *mem_ctx = talloc_init("num_subkeys");
297 for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx, key, i, &dest)); i++);
298 talloc_free(mem_ctx);
301 if(W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) error = WERR_OK;
305 return WERR_NOT_SUPPORTED;
309 * Get the number of values of a key.
311 _PUBLIC_ WERROR reg_key_num_values(const struct registry_key *key, uint32_t *count)
314 if(!key) return WERR_INVALID_PARAM;
316 if (key->hive->functions->num_values) {
317 return key->hive->functions->num_values(key, count);
320 if(key->hive->functions->get_value_by_index) {
323 struct registry_value *dest;
324 TALLOC_CTX *mem_ctx = talloc_init("num_subkeys");
326 for(i = 0; W_ERROR_IS_OK(error = key->hive->functions->get_value_by_index(mem_ctx, key, i, &dest)); i++);
327 talloc_free(mem_ctx);
330 if(W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) error = WERR_OK;
334 return WERR_NOT_SUPPORTED;
338 * Get subkey by index.
340 _PUBLIC_ WERROR reg_key_get_subkey_by_index(TALLOC_CTX *mem_ctx, const struct registry_key *key, int idx, struct registry_key **subkey)
342 if(!key) return WERR_INVALID_PARAM;
344 if(key->hive->functions->get_subkey_by_index) {
345 WERROR status = key->hive->functions->get_subkey_by_index(mem_ctx, key, idx, subkey);
346 if(!NT_STATUS_IS_OK(status)) return status;
348 return WERR_NOT_SUPPORTED;
351 if(key->hive->root == key)
352 (*subkey)->path = talloc_strdup(mem_ctx, (*subkey)->name);
354 (*subkey)->path = talloc_asprintf(mem_ctx, "%s\\%s", key->path, (*subkey)->name);
356 (*subkey)->hive = key->hive;
361 * Get subkey by name.
363 WERROR reg_key_get_subkey_by_name(TALLOC_CTX *mem_ctx, const struct registry_key *key, const char *name, struct registry_key **subkey)
366 WERROR error = WERR_OK;
368 if(!key) return WERR_INVALID_PARAM;
370 if(key->hive->functions->get_subkey_by_name) {
371 error = key->hive->functions->get_subkey_by_name(mem_ctx, key,name,subkey);
372 } else if(key->hive->functions->open_key) {
373 error = key->hive->functions->open_key(mem_ctx, key, name, subkey);
374 } else if(key->hive->functions->get_subkey_by_index) {
375 for(i = 0; W_ERROR_IS_OK(error); i++) {
376 error = reg_key_get_subkey_by_index(mem_ctx, key, i, subkey);
377 if(W_ERROR_IS_OK(error) && !strcasecmp((*subkey)->name, name)) {
382 if (W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
383 error = WERR_DEST_NOT_FOUND;
385 return WERR_NOT_SUPPORTED;
388 if(!W_ERROR_IS_OK(error)) return error;
390 (*subkey)->path = talloc_asprintf(mem_ctx, "%s\\%s", key->path, (*subkey)->name);
391 (*subkey)->hive = key->hive;
399 _PUBLIC_ WERROR reg_key_get_value_by_name(TALLOC_CTX *mem_ctx, const struct registry_key *key, const char *name, struct registry_value **val)
402 WERROR error = WERR_OK;
404 if(!key) return WERR_INVALID_PARAM;
406 if(key->hive->functions->get_value_by_name) {
407 error = key->hive->functions->get_value_by_name(mem_ctx, key,name, val);
409 for(i = 0; W_ERROR_IS_OK(error); i++) {
410 error = reg_key_get_value_by_index(mem_ctx, key, i, val);
411 if(W_ERROR_IS_OK(error) && !strcasecmp((*val)->name, name)) {
417 if (W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
418 return WERR_DEST_NOT_FOUND;
426 _PUBLIC_ WERROR reg_key_del(struct registry_key *parent, const char *name)
429 if(!parent) return WERR_INVALID_PARAM;
432 if(!parent->hive->functions->del_key)
433 return WERR_NOT_SUPPORTED;
435 error = parent->hive->functions->del_key(parent, name);
436 if(!W_ERROR_IS_OK(error)) return error;
444 _PUBLIC_ WERROR reg_key_add_name(TALLOC_CTX *mem_ctx, const struct registry_key *parent, const char *name, uint32_t access_mask, struct security_descriptor *desc, struct registry_key **newkey)
448 if (!parent) return WERR_INVALID_PARAM;
450 if (!parent->hive->functions->add_key) {
451 DEBUG(1, ("Backend '%s' doesn't support method add_key\n", parent->hive->functions->name));
452 return WERR_NOT_SUPPORTED;
455 error = parent->hive->functions->add_key(mem_ctx, parent, name, access_mask, desc, newkey);
457 if(!W_ERROR_IS_OK(error)) return error;
460 DEBUG(0, ("Backend returned WERR_OK, but didn't specify key!\n"));
461 return WERR_GENERAL_FAILURE;
464 (*newkey)->hive = parent->hive;
472 _PUBLIC_ WERROR reg_val_set(struct registry_key *key, const char *value, uint32_t type, DATA_BLOB data)
474 /* A 'real' set function has preference */
475 if (key->hive->functions->set_value)
476 return key->hive->functions->set_value(key, value, type, data);
478 DEBUG(1, ("Backend '%s' doesn't support method set_value\n", key->hive->functions->name));
479 return WERR_NOT_SUPPORTED;
483 * Get the security descriptor on a key.
485 _PUBLIC_ WERROR reg_get_sec_desc(TALLOC_CTX *ctx, const struct registry_key *key, struct security_descriptor **secdesc)
487 /* A 'real' set function has preference */
488 if (key->hive->functions->key_get_sec_desc)
489 return key->hive->functions->key_get_sec_desc(ctx, key, secdesc);
491 DEBUG(1, ("Backend '%s' doesn't support method get_sec_desc\n", key->hive->functions->name));
492 return WERR_NOT_SUPPORTED;
498 _PUBLIC_ WERROR reg_del_value(const struct registry_key *key, const char *valname)
500 WERROR ret = WERR_OK;
501 if(!key->hive->functions->del_value)
502 return WERR_NOT_SUPPORTED;
504 ret = key->hive->functions->del_value(key, valname);
506 if(!W_ERROR_IS_OK(ret)) return ret;
512 * Flush a key to disk.
514 _PUBLIC_ WERROR reg_key_flush(const struct registry_key *key)
517 return WERR_INVALID_PARAM;
520 if (key->hive->functions->flush_key) {
521 return key->hive->functions->flush_key(key);
524 /* No need for flushing, apparently */
529 * Get the maximum name and data lengths of the subkeys.
531 _PUBLIC_ WERROR reg_key_subkeysizes(const struct registry_key *key, uint32_t *max_subkeylen, uint32_t *max_subkeysize)
534 struct registry_key *subkey;
536 TALLOC_CTX *mem_ctx = talloc_init("subkeysize");
538 *max_subkeylen = *max_subkeysize = 0;
541 error = reg_key_get_subkey_by_index(mem_ctx, key, i, &subkey);
543 if (W_ERROR_IS_OK(error)) {
544 *max_subkeysize = MAX(*max_subkeysize, 0xFF);
545 *max_subkeylen = MAX(*max_subkeylen, strlen(subkey->name));
549 } while (W_ERROR_IS_OK(error));
551 talloc_free(mem_ctx);
557 * Get the maximum name and data lengths of the values.
559 _PUBLIC_ WERROR reg_key_valuesizes(const struct registry_key *key, uint32_t *max_valnamelen, uint32_t *max_valbufsize)
562 struct registry_value *value;
564 TALLOC_CTX *mem_ctx = talloc_init("subkeysize");
566 *max_valnamelen = *max_valbufsize = 0;
569 error = reg_key_get_value_by_index(mem_ctx, key, i, &value);
571 if (W_ERROR_IS_OK(error)) {
573 *max_valnamelen = MAX(*max_valnamelen, strlen(value->name));
575 *max_valbufsize = MAX(*max_valbufsize, value->data.length);
579 } while (W_ERROR_IS_OK(error));
581 talloc_free(mem_ctx);