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_p(NULL, 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_PT, "HKEY_PT" },
89 {HKEY_PN, "HKEY_PN" },
93 int reg_list_hives(TALLOC_CTX *mem_ctx, char ***hives, uint32_t **hkeys)
96 *hives = talloc_array_p(mem_ctx, char *, ARRAY_SIZE(hkey_names));
97 *hkeys = talloc_array_p(mem_ctx, uint32_t, ARRAY_SIZE(hkey_names));
99 for (i = 0; hkey_names[i].name; i++) {
100 (*hives)[i] = talloc_strdup(mem_ctx, hkey_names[i].name);
101 (*hkeys)[i] = hkey_names[i].hkey;
107 const char *reg_get_hkey_name(uint32_t hkey)
110 for (i = 0; hkey_names[i].name; i++) {
111 if (hkey_names[i].hkey == hkey) return hkey_names[i].name;
117 WERROR reg_get_hive_by_name(struct registry_context *ctx, const char *name, struct registry_key **key)
121 for (i = 0; hkey_names[i].name; i++) {
122 if (!strcmp(hkey_names[i].name, name)) return reg_get_hive(ctx, hkey_names[i].hkey, key);
125 DEBUG(1, ("No hive with name '%s'\n", name));
130 WERROR reg_close (struct registry_context *ctx)
137 WERROR reg_get_hive(struct registry_context *ctx, uint32_t hkey, struct registry_key **key)
139 WERROR ret = ctx->get_hive(ctx, hkey, key);
141 if (W_ERROR_IS_OK(ret)) {
142 (*key)->name = talloc_strdup(*key, reg_get_hkey_name(hkey));
149 /* Open a registry file/host/etc */
150 WERROR reg_open_hive(struct registry_context *parent_ctx, const char *backend, const char *location, const char *credentials, struct registry_key **root)
152 struct registry_hive *ret;
153 struct reg_init_function_entry *entry;
156 entry = reg_find_backend_entry(backend);
159 DEBUG(0, ("No such registry backend '%s' loaded!\n", backend));
160 return WERR_GENERAL_FAILURE;
163 if(!entry->hive_functions || !entry->hive_functions->open_hive) {
164 return WERR_NOT_SUPPORTED;
167 ret = talloc_p(parent_ctx, struct registry_hive);
168 ret->location = location?talloc_strdup(ret, location):NULL;
169 ret->functions = entry->hive_functions;
170 ret->backend_data = NULL;
171 ret->reg_ctx = parent_ctx;
173 werr = entry->hive_functions->open_hive(ret, &ret->root);
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 ret->root->hive = ret;
185 ret->root->name = NULL;
186 ret->root->path = talloc_strdup(ret, "");
193 /* Open a key by name (including the hive name!) */
194 WERROR reg_open_key_abs(TALLOC_CTX *mem_ctx, struct registry_context *handle, const char *name, struct registry_key **result)
196 struct registry_key *hive;
201 if(strchr(name, '\\')) hivelength = strchr(name, '\\')-name;
202 else hivelength = strlen(name);
204 hivename = strndup(name, hivelength);
205 error = reg_get_hive_by_name(handle, hivename, &hive);
208 if(!W_ERROR_IS_OK(error)) {
212 return reg_open_key(mem_ctx, hive, name, result);
216 * First tries to use the open_key function from the backend
217 * then falls back to get_subkey_by_name and later get_subkey_by_index
219 WERROR reg_open_key(TALLOC_CTX *mem_ctx, struct registry_key *parent, const char *name, struct registry_key **result)
225 DEBUG(0, ("Invalid parent key specified"));
226 return WERR_INVALID_PARAM;
229 if(!parent->hive->functions->open_key &&
230 (parent->hive->functions->get_subkey_by_name ||
231 parent->hive->functions->get_subkey_by_index)) {
232 char *orig = strdup(name),
234 *curend = strchr(orig, '\\');
235 struct registry_key *curkey = parent;
237 while(curbegin && *curbegin) {
238 if(curend)*curend = '\0';
239 error = reg_key_get_subkey_by_name(mem_ctx, curkey, curbegin, &curkey);
240 if(!W_ERROR_IS_OK(error)) {
245 curbegin = curend + 1;
246 curend = strchr(curbegin, '\\');
255 if(!parent->hive->functions->open_key) {
256 DEBUG(0, ("Registry backend doesn't have get_subkey_by_name nor open_key!\n"));
257 return WERR_NOT_SUPPORTED;
261 fullname = ((parent->hive->root == parent)?talloc_strdup(mem_ctx, name):talloc_asprintf(mem_ctx, "%s\\%s", parent->path, name));
263 error = parent->hive->functions->open_key(mem_ctx, parent->hive, fullname, result);
265 if(!W_ERROR_IS_OK(error)) return error;
267 (*result)->hive = parent->hive;
268 (*result)->path = fullname;
269 (*result)->hive = parent->hive;
274 WERROR reg_key_get_value_by_index(TALLOC_CTX *mem_ctx, struct registry_key *key, int idx, struct registry_value **val)
276 if(!key) return WERR_INVALID_PARAM;
278 if(key->hive->functions->get_value_by_index) {
279 WERROR status = key->hive->functions->get_value_by_index(mem_ctx, key, idx, val);
280 if(!W_ERROR_IS_OK(status))
283 return WERR_NOT_SUPPORTED;
286 (*val)->parent = key;
287 (*val)->hive = key->hive;
291 WERROR reg_key_num_subkeys(struct registry_key *key, int *count)
293 if(!key) return WERR_INVALID_PARAM;
295 if(key->hive->functions->num_subkeys) {
296 return key->hive->functions->num_subkeys(key, count);
299 if(key->hive->functions->get_subkey_by_index) {
302 struct registry_key *dest;
303 TALLOC_CTX *mem_ctx = talloc_init("num_subkeys");
305 for(i = 0; W_ERROR_IS_OK(error = key->hive->functions->get_subkey_by_index(mem_ctx, key, i, &dest)); i++);
306 talloc_destroy(mem_ctx);
309 if(W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) error = WERR_OK;
313 return WERR_NOT_SUPPORTED;
316 WERROR reg_key_num_values(struct registry_key *key, int *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_destroy(mem_ctx);
335 if(W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) error = WERR_OK;
339 return WERR_NOT_SUPPORTED;
342 WERROR reg_key_get_subkey_by_index(TALLOC_CTX *mem_ctx, struct registry_key *key, int idx, struct registry_key **subkey)
344 if(!key) return WERR_INVALID_PARAM;
346 if(key->hive->functions->get_subkey_by_index) {
347 WERROR status = key->hive->functions->get_subkey_by_index(mem_ctx, key, idx, subkey);
348 if(!NT_STATUS_IS_OK(status)) return status;
350 return WERR_NOT_SUPPORTED;
353 if(key->hive->root == key)
354 (*subkey)->path = talloc_strdup(mem_ctx, (*subkey)->name);
356 (*subkey)->path = talloc_asprintf(mem_ctx, "%s\\%s", key->path, (*subkey)->name);
358 (*subkey)->hive = key->hive;
362 WERROR reg_key_get_subkey_by_name(TALLOC_CTX *mem_ctx, struct registry_key *key, const char *name, struct registry_key **subkey)
365 WERROR error = WERR_OK;
367 if(!key) return WERR_INVALID_PARAM;
369 if(key->hive->functions->get_subkey_by_name) {
370 error = key->hive->functions->get_subkey_by_name(mem_ctx, key,name,subkey);
371 } else if(key->hive->functions->open_key) {
372 error = key->hive->functions->open_key(mem_ctx, key->hive, talloc_asprintf(mem_ctx, "%s\\%s", key->path, name), subkey);
373 } else if(key->hive->functions->get_subkey_by_index) {
374 for(i = 0; W_ERROR_IS_OK(error); i++) {
375 error = reg_key_get_subkey_by_index(mem_ctx, key, i, subkey);
376 if(W_ERROR_IS_OK(error) && !strcmp((*subkey)->name, name)) {
381 return WERR_NOT_SUPPORTED;
384 if(!W_ERROR_IS_OK(error)) return error;
386 (*subkey)->path = talloc_asprintf(mem_ctx, "%s\\%s", key->path, (*subkey)->name);
387 (*subkey)->hive = key->hive;
392 WERROR reg_key_get_value_by_name(TALLOC_CTX *mem_ctx, struct registry_key *key, const char *name, struct registry_value **val)
395 WERROR error = WERR_OK;
397 if(!key) return WERR_INVALID_PARAM;
399 if(key->hive->functions->get_value_by_name) {
400 error = key->hive->functions->get_value_by_name(mem_ctx, key,name, val);
402 for(i = 0; W_ERROR_IS_OK(error); i++) {
403 error = reg_key_get_value_by_index(mem_ctx, key, i, val);
404 if(W_ERROR_IS_OK(error) && StrCaseCmp((*val)->name, name)) {
410 if(!W_ERROR_IS_OK(error) && !W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
413 (*val)->parent = key;
414 (*val)->hive = key->hive;
419 WERROR reg_key_del(struct registry_key *key)
422 if(!key) return WERR_INVALID_PARAM;
425 if(!key->hive->functions->del_key)
426 return WERR_NOT_SUPPORTED;
428 error = key->hive->functions->del_key(key);
429 if(!W_ERROR_IS_OK(error)) return error;
434 WERROR reg_key_del_recursive(struct registry_key *key)
436 WERROR error = WERR_OK;
439 TALLOC_CTX *mem_ctx = talloc_init("del_recursive");
441 /* Delete all values for specified key */
442 for(i = 0; W_ERROR_IS_OK(error); i++) {
443 struct registry_value *val;
444 error = reg_key_get_value_by_index(mem_ctx, key, i, &val);
445 if(!W_ERROR_IS_OK(error) && !W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
447 talloc_destroy(mem_ctx);
451 if(W_ERROR_IS_OK(error)) {
452 error = reg_del_value(val);
453 if(!W_ERROR_IS_OK(error)) {
454 talloc_destroy(mem_ctx);
462 /* Delete all keys below this one */
463 for(i = 0; W_ERROR_IS_OK(error); i++) {
464 struct registry_key *subkey;
466 error = reg_key_get_subkey_by_index(mem_ctx, key, i, &subkey);
467 if(!W_ERROR_IS_OK(error)) { talloc_destroy(mem_ctx); return error; }
469 error = reg_key_del_recursive(subkey);
470 if(!W_ERROR_IS_OK(error)) { talloc_destroy(mem_ctx); return error; }
473 talloc_destroy(mem_ctx);
474 return reg_key_del(key);
477 WERROR reg_key_add_name_recursive_abs(struct registry_context *handle, const char *name)
479 struct registry_key *hive;
484 if(strchr(name, '\\')) hivelength = strchr(name, '\\')-name;
485 else hivelength = strlen(name);
487 hivename = strndup(name, hivelength);
488 error = reg_get_hive_by_name(handle, hivename, &hive);
491 if(!W_ERROR_IS_OK(error)) return error;
493 return reg_key_add_name_recursive(hive, name);
496 WERROR reg_key_add_name_recursive(struct registry_key *parent, const char *path)
498 struct registry_key *cur, *prevcur = parent;
499 WERROR error = WERR_OK;
500 char *dups, *begin, *end;
501 TALLOC_CTX *mem_ctx = talloc_init("add_recursive");
503 begin = dups = strdup(path);
506 end = strchr(begin, '\\');
509 error = reg_key_get_subkey_by_name(mem_ctx, prevcur, begin, &cur);
511 /* Key is not there, add it */
512 if(W_ERROR_EQUAL(error, WERR_DEST_NOT_FOUND)) {
513 error = reg_key_add_name(mem_ctx, prevcur, begin, 0, NULL, &cur);
514 if(!W_ERROR_IS_OK(error)) break;
517 if(!W_ERROR_IS_OK(error)) {
532 talloc_destroy(mem_ctx);
536 WERROR reg_key_add_name(TALLOC_CTX *mem_ctx, struct registry_key *parent, const char *name, uint32_t access_mask, SEC_DESC *desc, struct registry_key **newkey)
540 if (!parent) return WERR_INVALID_PARAM;
542 if (!parent->hive->functions->add_key) {
543 DEBUG(1, ("Backend '%s' doesn't support method add_key\n", parent->hive->functions->name));
544 return WERR_NOT_SUPPORTED;
547 error = parent->hive->functions->add_key(mem_ctx, parent, name, access_mask, desc, newkey);
549 if(!W_ERROR_IS_OK(error)) return error;
551 (*newkey)->hive = parent->hive;
552 (*newkey)->backend_data = talloc_asprintf(mem_ctx, "%s\\%s", parent->path, name);
557 WERROR reg_val_set(struct registry_key *key, const char *value, int type, void *data, int len)
559 /* A 'real' set function has preference */
560 if (key->hive->functions->set_value)
561 return key->hive->functions->set_value(key, value, type, data, len);
563 DEBUG(1, ("Backend '%s' doesn't support method set_value\n", key->hive->functions->name));
564 return WERR_NOT_SUPPORTED;
569 WERROR reg_del_value(struct registry_value *val)
571 WERROR ret = WERR_OK;
572 if(!val->hive->functions->del_value)
573 return WERR_NOT_SUPPORTED;
575 ret = val->hive->functions->del_value(val);
577 if(!W_ERROR_IS_OK(ret)) return ret;
582 WERROR reg_save (struct registry_context *ctx, const char *location)
584 return WERR_NOT_SUPPORTED;
587 WERROR reg_key_get_parent(TALLOC_CTX *mem_ctx, struct registry_key *key, struct registry_key **parent)
591 struct registry_key *root = NULL;
594 parent_name = strdup(key->path);
595 last = strrchr(parent_name, '\\');
598 SAFE_FREE(parent_name);
603 error = reg_open_key(mem_ctx, root, parent_name, parent);
604 SAFE_FREE(parent_name);
608 WERROR reg_key_flush(struct registry_key *key)
611 return WERR_INVALID_PARAM;
614 if (key->hive->functions->flush_key) {
615 return key->hive->functions->flush_key(key);
618 /* No need for flushing, apparently */
622 WERROR reg_key_subkeysizes(struct registry_key *key, uint32 *max_subkeylen, uint32 *max_subkeysize)
625 struct registry_key *subkey;
627 TALLOC_CTX *mem_ctx = talloc_init("subkeysize");
629 *max_subkeylen = *max_subkeysize = 0;
632 error = reg_key_get_subkey_by_index(mem_ctx, key, i, &subkey);
634 if (W_ERROR_IS_OK(error)) {
635 *max_subkeysize = MAX(*max_subkeysize, 0xFF);
636 *max_subkeylen = MAX(*max_subkeylen, strlen(subkey->name));
640 } while (W_ERROR_IS_OK(error));
642 talloc_destroy(mem_ctx);
647 WERROR reg_key_valuesizes(struct registry_key *key, uint32 *max_valnamelen, uint32 *max_valbufsize)
650 struct registry_value *value;
652 TALLOC_CTX *mem_ctx = talloc_init("subkeysize");
654 *max_valnamelen = *max_valbufsize = 0;
657 error = reg_key_get_value_by_index(mem_ctx, key, i, &value);
659 if (W_ERROR_IS_OK(error)) {
661 *max_valnamelen = MAX(*max_valnamelen, strlen(value->name));
663 *max_valbufsize = MAX(*max_valbufsize, value->data_len);
667 } while (W_ERROR_IS_OK(error));
669 talloc_destroy(mem_ctx);