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"
23 #include "lib/registry/registry.h"
28 * @brief Main registry functions
31 /* List of available backends */
32 static struct reg_init_function_entry *backends = NULL;
34 static struct reg_init_function_entry *reg_find_backend_entry(const char *name);
36 /** Register a new backend. */
37 _PUBLIC_ NTSTATUS registry_register(const void *_hive_ops)
39 const struct hive_operations *hive_ops = _hive_ops;
40 struct reg_init_function_entry *entry = backends;
42 DEBUG(5,("Attempting to register registry backend %s\n", hive_ops->name));
44 /* Check for duplicates */
45 if (reg_find_backend_entry(hive_ops->name)) {
46 DEBUG(0,("There already is a registry backend registered with the name %s!\n", hive_ops->name));
47 return NT_STATUS_OBJECT_NAME_COLLISION;
50 entry = talloc(talloc_autofree_context(), struct reg_init_function_entry);
51 entry->hive_functions = hive_ops;
53 DLIST_ADD(backends, entry);
54 DEBUG(5,("Successfully added registry backend '%s'\n", hive_ops->name));
58 /** Find a backend in the list of available backends */
59 static struct reg_init_function_entry *reg_find_backend_entry(const char *name)
61 struct reg_init_function_entry *entry;
66 if (strcmp(entry->hive_functions->name, name) == 0) return entry;
73 /** Initialize the registry subsystem */
74 _PUBLIC_ NTSTATUS registry_init(void)
76 init_module_fn static_init[] = STATIC_registry_MODULES;
77 init_module_fn *shared_init = load_samba_modules(NULL, "registry");
79 run_init_functions(static_init);
80 run_init_functions(shared_init);
82 talloc_free(shared_init);
87 /** Check whether a certain backend is present. */
88 _PUBLIC_ BOOL reg_has_backend(const char *backend)
90 return reg_find_backend_entry(backend) != NULL?True:False;
98 {HKEY_CLASSES_ROOT,"HKEY_CLASSES_ROOT" },
99 {HKEY_CURRENT_USER,"HKEY_CURRENT_USER" },
100 {HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" },
101 {HKEY_PERFORMANCE_DATA, "HKEY_PERFORMANCE_DATA" },
102 {HKEY_USERS, "HKEY_USERS" },
103 {HKEY_CURRENT_CONFIG, "HKEY_CURRENT_CONFIG" },
104 {HKEY_DYN_DATA, "HKEY_DYN_DATA" },
105 {HKEY_PERFORMANCE_TEXT, "HKEY_PERFORMANCE_TEXT" },
106 {HKEY_PERFORMANCE_NLSTEXT, "HKEY_PERFORMANCE_NLSTEXT" },
110 /** Obtain a list of predefined keys. */
111 _PUBLIC_ int reg_list_predefs(TALLOC_CTX *mem_ctx, char ***predefs, uint32_t **hkeys)
114 *predefs = talloc_array(mem_ctx, char *, ARRAY_SIZE(predef_names));
115 *hkeys = talloc_array(mem_ctx, uint32_t, ARRAY_SIZE(predef_names));
117 for (i = 0; predef_names[i].name; i++) {
118 (*predefs)[i] = talloc_strdup(mem_ctx, predef_names[i].name);
119 (*hkeys)[i] = predef_names[i].handle;
125 /** Obtain name of specific hkey. */
126 _PUBLIC_ const char *reg_get_predef_name(uint32_t hkey)
129 for (i = 0; predef_names[i].name; i++) {
130 if (predef_names[i].handle == hkey) return predef_names[i].name;
136 /** Get predefined key by name. */
137 _PUBLIC_ WERROR reg_get_predefined_key_by_name(struct registry_context *ctx, const char *name, struct registry_key **key)
141 for (i = 0; predef_names[i].name; i++) {
142 if (!strcasecmp(predef_names[i].name, name)) return reg_get_predefined_key(ctx, predef_names[i].handle, key);
145 DEBUG(1, ("No predefined key with name '%s'\n", name));
150 /** Get predefined key by id. */
151 _PUBLIC_ WERROR reg_get_predefined_key(struct registry_context *ctx, uint32_t hkey, struct registry_key **key)
153 WERROR ret = ctx->get_predefined_key(ctx, hkey, key);
155 if (W_ERROR_IS_OK(ret)) {
156 (*key)->name = talloc_strdup(*key, reg_get_predef_name(hkey));
163 /** Open a registry file/host/etc */
164 _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)
166 struct registry_hive *rethive;
167 struct registry_key *retkey = NULL;
168 struct reg_init_function_entry *entry;
171 entry = reg_find_backend_entry(backend);
174 DEBUG(0, ("No such registry backend '%s' loaded!\n", backend));
175 return WERR_GENERAL_FAILURE;
178 if(!entry->hive_functions || !entry->hive_functions->open_hive) {
179 return WERR_NOT_SUPPORTED;
182 rethive = talloc(parent_ctx, struct registry_hive);
183 rethive->location = location?talloc_strdup(rethive, location):NULL;
184 rethive->session_info = talloc_reference(rethive, session_info);
185 rethive->credentials = talloc_reference(rethive, credentials);
186 rethive->functions = entry->hive_functions;
187 rethive->backend_data = NULL;
189 werr = entry->hive_functions->open_hive(rethive, &retkey);
191 if(!W_ERROR_IS_OK(werr)) {
196 DEBUG(0, ("Backend %s didn't provide root key!\n", backend));
197 return WERR_GENERAL_FAILURE;
200 rethive->root = retkey;
202 retkey->hive = rethive;
204 retkey->path = talloc_strdup(retkey, "");
213 * First tries to use the open_key function from the backend
214 * then falls back to get_subkey_by_name and later get_subkey_by_index
216 _PUBLIC_ WERROR reg_open_key(TALLOC_CTX *mem_ctx, struct registry_key *parent, const char *name, struct registry_key **result)
221 DEBUG(0, ("Invalid parent key specified for open of '%s'\n", name));
222 return WERR_INVALID_PARAM;
225 if(!parent->hive->functions->open_key &&
226 (parent->hive->functions->get_subkey_by_name ||
227 parent->hive->functions->get_subkey_by_index)) {
228 char *orig = strdup(name),
230 *curend = strchr(orig, '\\');
231 struct registry_key *curkey = parent;
233 while(curbegin && *curbegin) {
234 if(curend)*curend = '\0';
235 error = reg_key_get_subkey_by_name(mem_ctx, curkey, curbegin, &curkey);
236 if(!W_ERROR_IS_OK(error)) {
241 curbegin = curend + 1;
242 curend = strchr(curbegin, '\\');
251 if(!parent->hive->functions->open_key) {
252 DEBUG(0, ("Registry backend doesn't have get_subkey_by_name nor open_key!\n"));
253 return WERR_NOT_SUPPORTED;
256 error = parent->hive->functions->open_key(mem_ctx, parent, name, result);
258 if(!W_ERROR_IS_OK(error)) return error;
260 (*result)->hive = parent->hive;
261 (*result)->path = ((parent->hive->root == parent)?talloc_strdup(mem_ctx, name):talloc_asprintf(mem_ctx, "%s\\%s", parent->path, name));
262 (*result)->hive = parent->hive;
270 _PUBLIC_ WERROR reg_key_get_value_by_index(TALLOC_CTX *mem_ctx, const struct registry_key *key, int idx, struct registry_value **val)
272 if(!key) return WERR_INVALID_PARAM;
274 if(key->hive->functions->get_value_by_index) {
275 WERROR status = key->hive->functions->get_value_by_index(mem_ctx, key, idx, val);
276 if(!W_ERROR_IS_OK(status))
279 return WERR_NOT_SUPPORTED;
286 * Get the number of subkeys.
288 _PUBLIC_ WERROR reg_key_num_subkeys(const struct registry_key *key, uint32_t *count)
290 if(!key) return WERR_INVALID_PARAM;
292 if(key->hive->functions->num_subkeys) {
293 return key->hive->functions->num_subkeys(key, count);
296 if(key->hive->functions->get_subkey_by_index) {
299 struct registry_key *dest = NULL;
300 TALLOC_CTX *mem_ctx = talloc_init("num_subkeys");
302 for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx, key, i, &dest)); i++);
303 talloc_free(mem_ctx);
306 if(W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) error = WERR_OK;
310 return WERR_NOT_SUPPORTED;
314 * Get the number of values of a key.
316 _PUBLIC_ WERROR reg_key_num_values(const struct registry_key *key, uint32_t *count)
319 if(!key) return WERR_INVALID_PARAM;
321 if (key->hive->functions->num_values) {
322 return key->hive->functions->num_values(key, count);
325 if(key->hive->functions->get_value_by_index) {
328 struct registry_value *dest;
329 TALLOC_CTX *mem_ctx = talloc_init("num_subkeys");
331 for(i = 0; W_ERROR_IS_OK(error = key->hive->functions->get_value_by_index(mem_ctx, key, i, &dest)); i++);
332 talloc_free(mem_ctx);
335 if(W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) error = WERR_OK;
339 return WERR_NOT_SUPPORTED;
343 * Get subkey by index.
345 _PUBLIC_ WERROR reg_key_get_subkey_by_index(TALLOC_CTX *mem_ctx, const struct registry_key *key, int idx, struct registry_key **subkey)
347 if(!key) return WERR_INVALID_PARAM;
349 if(key->hive->functions->get_subkey_by_index) {
350 WERROR status = key->hive->functions->get_subkey_by_index(mem_ctx, key, idx, subkey);
351 if(!NT_STATUS_IS_OK(status)) return status;
353 return WERR_NOT_SUPPORTED;
356 if(key->hive->root == key)
357 (*subkey)->path = talloc_strdup(mem_ctx, (*subkey)->name);
359 (*subkey)->path = talloc_asprintf(mem_ctx, "%s\\%s", key->path, (*subkey)->name);
361 (*subkey)->hive = key->hive;
366 * Get subkey by name.
368 WERROR reg_key_get_subkey_by_name(TALLOC_CTX *mem_ctx, const struct registry_key *key, const char *name, struct registry_key **subkey)
371 WERROR error = WERR_OK;
373 if(!key) return WERR_INVALID_PARAM;
375 if(key->hive->functions->get_subkey_by_name) {
376 error = key->hive->functions->get_subkey_by_name(mem_ctx, key,name,subkey);
377 } else if(key->hive->functions->open_key) {
378 error = key->hive->functions->open_key(mem_ctx, key, name, subkey);
379 } else if(key->hive->functions->get_subkey_by_index) {
380 for(i = 0; W_ERROR_IS_OK(error); i++) {
381 error = reg_key_get_subkey_by_index(mem_ctx, key, i, subkey);
382 if(W_ERROR_IS_OK(error) && !strcasecmp((*subkey)->name, name)) {
387 if (W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
388 error = WERR_DEST_NOT_FOUND;
390 return WERR_NOT_SUPPORTED;
393 if(!W_ERROR_IS_OK(error)) return error;
395 (*subkey)->path = talloc_asprintf(mem_ctx, "%s\\%s", key->path, (*subkey)->name);
396 (*subkey)->hive = key->hive;
404 _PUBLIC_ WERROR reg_key_get_value_by_name(TALLOC_CTX *mem_ctx, const struct registry_key *key, const char *name, struct registry_value **val)
407 WERROR error = WERR_OK;
409 if(!key) return WERR_INVALID_PARAM;
411 if(key->hive->functions->get_value_by_name) {
412 error = key->hive->functions->get_value_by_name(mem_ctx, key,name, val);
414 for(i = 0; W_ERROR_IS_OK(error); i++) {
415 error = reg_key_get_value_by_index(mem_ctx, key, i, val);
416 if(W_ERROR_IS_OK(error) && !strcasecmp((*val)->name, name)) {
422 if (W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
423 return WERR_DEST_NOT_FOUND;
431 _PUBLIC_ WERROR reg_key_del(struct registry_key *parent, const char *name)
434 if(!parent) return WERR_INVALID_PARAM;
437 if(!parent->hive->functions->del_key)
438 return WERR_NOT_SUPPORTED;
440 error = parent->hive->functions->del_key(parent, name);
441 if(!W_ERROR_IS_OK(error)) return error;
449 _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)
453 if (!parent) return WERR_INVALID_PARAM;
455 if (!parent->hive->functions->add_key) {
456 DEBUG(1, ("Backend '%s' doesn't support method add_key\n", parent->hive->functions->name));
457 return WERR_NOT_SUPPORTED;
460 error = parent->hive->functions->add_key(mem_ctx, parent, name, access_mask, desc, newkey);
462 if(!W_ERROR_IS_OK(error)) return error;
465 DEBUG(0, ("Backend returned WERR_OK, but didn't specify key!\n"));
466 return WERR_GENERAL_FAILURE;
469 (*newkey)->hive = parent->hive;
477 _PUBLIC_ WERROR reg_val_set(struct registry_key *key, const char *value, uint32_t type, DATA_BLOB data)
479 /* A 'real' set function has preference */
480 if (key->hive->functions->set_value)
481 return key->hive->functions->set_value(key, value, type, data);
483 DEBUG(1, ("Backend '%s' doesn't support method set_value\n", key->hive->functions->name));
484 return WERR_NOT_SUPPORTED;
488 * Get the security descriptor on a key.
490 _PUBLIC_ WERROR reg_get_sec_desc(TALLOC_CTX *ctx, const struct registry_key *key, struct security_descriptor **secdesc)
492 /* A 'real' set function has preference */
493 if (key->hive->functions->key_get_sec_desc)
494 return key->hive->functions->key_get_sec_desc(ctx, key, secdesc);
496 DEBUG(1, ("Backend '%s' doesn't support method get_sec_desc\n", key->hive->functions->name));
497 return WERR_NOT_SUPPORTED;
503 _PUBLIC_ WERROR reg_del_value(const struct registry_key *key, const char *valname)
505 WERROR ret = WERR_OK;
506 if(!key->hive->functions->del_value)
507 return WERR_NOT_SUPPORTED;
509 ret = key->hive->functions->del_value(key, valname);
511 if(!W_ERROR_IS_OK(ret)) return ret;
517 * Flush a key to disk.
519 _PUBLIC_ WERROR reg_key_flush(const struct registry_key *key)
522 return WERR_INVALID_PARAM;
525 if (key->hive->functions->flush_key) {
526 return key->hive->functions->flush_key(key);
529 /* No need for flushing, apparently */
534 * Get the maximum name and data lengths of the subkeys.
536 _PUBLIC_ WERROR reg_key_subkeysizes(const struct registry_key *key, uint32_t *max_subkeylen, uint32_t *max_subkeysize)
539 struct registry_key *subkey;
541 TALLOC_CTX *mem_ctx = talloc_init("subkeysize");
543 *max_subkeylen = *max_subkeysize = 0;
546 error = reg_key_get_subkey_by_index(mem_ctx, key, i, &subkey);
548 if (W_ERROR_IS_OK(error)) {
549 *max_subkeysize = MAX(*max_subkeysize, 0xFF);
550 *max_subkeylen = MAX(*max_subkeylen, strlen(subkey->name));
554 } while (W_ERROR_IS_OK(error));
556 talloc_free(mem_ctx);
562 * Get the maximum name and data lengths of the values.
564 _PUBLIC_ WERROR reg_key_valuesizes(const struct registry_key *key, uint32_t *max_valnamelen, uint32_t *max_valbufsize)
567 struct registry_value *value;
569 TALLOC_CTX *mem_ctx = talloc_init("subkeysize");
571 *max_valnamelen = *max_valbufsize = 0;
574 error = reg_key_get_value_by_index(mem_ctx, key, i, &value);
576 if (W_ERROR_IS_OK(error)) {
578 *max_valnamelen = MAX(*max_valnamelen, strlen(value->name));
580 *max_valbufsize = MAX(*max_valbufsize, value->data.length);
584 } while (W_ERROR_IS_OK(error));
586 talloc_free(mem_ctx);