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 "lib/registry/common/registry.h"
25 #define DBGC_CLASS DBGC_REGISTRY
27 /* List of available backends */
28 static struct reg_init_function_entry *backends = NULL;
30 static struct reg_init_function_entry *reg_find_backend_entry(const char *name);
32 /* Register new backend */
33 NTSTATUS registry_register(void *_function)
35 struct registry_ops *functions = _function;
36 struct reg_init_function_entry *entry = backends;
38 if (!functions || !functions->name) {
39 return NT_STATUS_INVALID_PARAMETER;
42 DEBUG(5,("Attempting to register registry backend %s\n", functions->name));
44 /* Check for duplicates */
45 if (reg_find_backend_entry(functions->name)) {
46 DEBUG(0,("There already is a registry backend registered with the name %s!\n", functions->name));
47 return NT_STATUS_OBJECT_NAME_COLLISION;
50 entry = malloc(sizeof(struct reg_init_function_entry));
51 entry->functions = functions;
53 DLIST_ADD(backends, entry);
54 DEBUG(5,("Successfully added registry backend '%s'\n", functions->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;
62 static BOOL reg_first_init = True;
66 status = register_subsystem("registry", registry_register);
67 if (!NT_STATUS_IS_OK(status))
71 reg_first_init = False;
77 if (strcmp(entry->functions->name, name)==0) return entry;
84 BOOL reg_has_backend(const char *backend)
86 return reg_find_backend_entry(backend)?True:False;
89 /* Open a registry file/host/etc */
90 WERROR reg_open(const char *backend, const char *location, const char *credentials, REG_HANDLE **h)
92 struct reg_init_function_entry *entry;
98 entry = reg_find_backend_entry(backend);
101 DEBUG(0, ("No such registry backend '%s' loaded!\n", backend));
102 return WERR_GENERAL_FAILURE;
105 mem_ctx = talloc_init(backend);
106 ret = talloc(mem_ctx, sizeof(REG_HANDLE));
108 ret->location = location?talloc_strdup(mem_ctx, location):NULL;
109 ret->credentials = credentials?talloc_strdup(mem_ctx, credentials):NULL;
110 ret->functions = entry->functions;
111 ret->backend_data = NULL;
112 ret->mem_ctx = mem_ctx;
115 if(!entry->functions->open_registry) {
119 werr = entry->functions->open_registry(ret, location, credentials);
121 if(W_ERROR_IS_OK(werr))
124 talloc_destroy(mem_ctx);
128 WERROR reg_open_key_abs(REG_HANDLE *handle, const char *name, REG_KEY **result)
134 if(strchr(name, '\\')) hivelength = strchr(name, '\\')-name;
135 else hivelength = strlen(name);
137 for(i = 0; W_ERROR_IS_OK(error); i++) {
138 error = reg_get_hive(handle, i, &hive);
139 if(W_ERROR_IS_OK(error) && !strncmp(reg_key_name(hive), name, hivelength)) {
140 return reg_open_key(hive, name, result);
148 * First tries to use the open_key function from the backend
149 * then falls back to get_subkey_by_name and later get_subkey_by_index
151 WERROR reg_open_key(REG_KEY *parent, const char *name, REG_KEY **result)
158 DEBUG(0, ("Invalid parent key specified"));
159 return WERR_INVALID_PARAM;
162 if(!parent->handle->functions->open_key &&
163 (parent->handle->functions->get_subkey_by_name ||
164 parent->handle->functions->get_subkey_by_index)) {
165 char *orig = strdup(name),
167 *curend = strchr(orig, '\\');
168 REG_KEY *curkey = parent;
170 while(curbegin && *curbegin) {
171 if(curend)*curend = '\0';
172 error = reg_key_get_subkey_by_name(curkey, curbegin, result);
173 if(!W_ERROR_IS_OK(error)) {
178 curbegin = curend + 1;
179 curend = strchr(curbegin, '\\');
187 if(!parent->handle->functions->open_key) {
188 DEBUG(0, ("Registry backend doesn't have get_subkey_by_name nor open_key!\n"));
189 return WERR_NOT_SUPPORTED;
192 mem_ctx = talloc_init("mem_ctx");
194 fullname = talloc_asprintf(mem_ctx, "%s%s%s", reg_key_get_path(parent), strlen(reg_key_get_path(parent))?"\\":"", name);
196 error = parent->handle->functions->open_key(parent->handle, parent->hive, fullname, result);
198 if(!W_ERROR_IS_OK(error)) {
199 talloc_destroy(mem_ctx);
203 (*result)->handle = parent->handle;
204 (*result)->path = talloc_asprintf((*result)->mem_ctx, "%s\\%s", reg_key_get_path_abs(parent), (*result)->name);
205 (*result)->hive = parent->hive;
206 talloc_steal(mem_ctx, (*result)->mem_ctx, fullname);
208 talloc_destroy(mem_ctx);
213 WERROR reg_key_get_value_by_index(REG_KEY *key, int idx, REG_VAL **val)
215 if(!key) return WERR_INVALID_PARAM;
217 if(key->handle->functions->get_value_by_index) {
218 WERROR status = key->handle->functions->get_value_by_index(key, idx, val);
219 if(!W_ERROR_IS_OK(status))
222 } else if(key->handle->functions->fetch_values) {
223 if(!key->cache_values)
224 key->handle->functions->fetch_values(key, &key->cache_values_count, &key->cache_values);
226 if(idx < key->cache_values_count && idx >= 0) {
227 *val = reg_val_dup(key->cache_values[idx]);
229 return WERR_NO_MORE_ITEMS;
232 return WERR_NOT_SUPPORTED;
235 (*val)->parent = key;
236 (*val)->handle = key->handle;
240 WERROR reg_key_num_subkeys(REG_KEY *key, int *count)
242 if(!key) return WERR_INVALID_PARAM;
244 if(key->handle->functions->num_subkeys) {
245 return key->handle->functions->num_subkeys(key, count);
248 if(key->handle->functions->fetch_subkeys) {
249 if(!key->cache_subkeys)
250 key->handle->functions->fetch_subkeys(key, &key->cache_subkeys_count, &key->cache_subkeys);
252 *count = key->cache_subkeys_count;
256 if(key->handle->functions->get_subkey_by_index) {
260 for(i = 0; W_ERROR_IS_OK(error = key->handle->functions->get_subkey_by_index(key, i, &dest)); i++) {
265 if(W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) return WERR_OK;
269 return WERR_NOT_SUPPORTED;
272 WERROR reg_key_num_values(REG_KEY *key, int *count)
275 if(!key) return WERR_INVALID_PARAM;
277 if(!key->handle->functions->num_values) {
278 if(!key->handle->functions->fetch_values) {
279 DEBUG(1, ("Backend '%s' doesn't support enumerating values\n", key->handle->functions->name));
280 return WERR_NOT_SUPPORTED;
283 if(!key->cache_values)
284 key->handle->functions->fetch_values(key, &key->cache_values_count, &key->cache_values);
286 *count = key->cache_values_count;
291 return key->handle->functions->num_values(key, count);
294 WERROR reg_key_get_subkey_by_index(REG_KEY *key, int idx, REG_KEY **subkey)
296 if(!key) return WERR_INVALID_PARAM;
298 if(key->handle->functions->get_subkey_by_index) {
299 WERROR status = key->handle->functions->get_subkey_by_index(key, idx, subkey);
300 if(!NT_STATUS_IS_OK(status)) return status;
301 } else if(key->handle->functions->fetch_subkeys) {
302 if(!key->cache_subkeys)
303 key->handle->functions->fetch_subkeys(key, &key->cache_subkeys_count, &key->cache_subkeys);
305 if(idx < key->cache_subkeys_count) {
306 *subkey = reg_key_dup(key->cache_subkeys[idx]);
308 return WERR_NO_MORE_ITEMS;
311 return WERR_NOT_SUPPORTED;
314 (*subkey)->path = talloc_asprintf((*subkey)->mem_ctx, "%s\\%s", reg_key_get_path_abs(key), (*subkey)->name);
315 (*subkey)->handle = key->handle;
316 (*subkey)->hive = key->hive;
322 WERROR reg_key_get_subkey_by_name(REG_KEY *key, const char *name, REG_KEY **subkey)
325 WERROR error = WERR_OK;
327 if(!key) return WERR_INVALID_PARAM;
329 if(key->handle->functions->get_subkey_by_name) {
330 error = key->handle->functions->get_subkey_by_name(key,name,subkey);
331 } else if(key->handle->functions->get_subkey_by_index || key->handle->functions->fetch_subkeys) {
332 for(i = 0; W_ERROR_IS_OK(error); i++) {
333 error = reg_key_get_subkey_by_index(key, i, subkey);
334 if(W_ERROR_IS_OK(error) && !strcmp((*subkey)->name, name)) {
337 reg_key_free(*subkey);
340 return WERR_NOT_SUPPORTED;
343 if(!W_ERROR_IS_OK(error)) return error;
345 (*subkey)->path = talloc_asprintf((*subkey)->mem_ctx, "%s\\%s", reg_key_get_path_abs(key), (*subkey)->name);
346 (*subkey)->handle = key->handle;
347 (*subkey)->hive = key->hive;
352 WERROR reg_key_get_value_by_name(REG_KEY *key, const char *name, REG_VAL **val)
355 WERROR error = WERR_OK;
357 if(!key) return WERR_INVALID_PARAM;
359 if(key->handle->functions->get_value_by_name) {
360 error = key->handle->functions->get_value_by_name(key,name, val);
362 for(i = 0; W_ERROR_IS_OK(error); i++) {
363 error = reg_key_get_value_by_index(key, i, val);
364 if(W_ERROR_IS_OK(error) && StrCaseCmp((*val)->name, name)) {
371 if(!W_ERROR_IS_OK(error) && !W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
374 (*val)->parent = key;
375 (*val)->handle = key->handle;
380 WERROR reg_key_del(REG_KEY *key)
383 if(!key) return WERR_INVALID_PARAM;
386 if(!key->handle->functions->del_key)
387 return WERR_NOT_SUPPORTED;
389 error = key->handle->functions->del_key(key);
390 if(!W_ERROR_IS_OK(error)) return error;
392 /* Invalidate cache */
393 key->cache_subkeys = NULL;
394 key->cache_subkeys_count = 0;
398 WERROR reg_sync(REG_KEY *h, const char *location)
400 if(!h->handle->functions->sync_key)
403 return h->handle->functions->sync_key(h, location);
406 WERROR reg_key_del_recursive(REG_KEY *key)
408 WERROR error = WERR_OK;
411 /* Delete all values for specified key */
412 for(i = 0; W_ERROR_IS_OK(error); i++) {
414 error = reg_key_get_value_by_index(key, i, &val);
415 if(!W_ERROR_IS_OK(error) && !W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
418 if(W_ERROR_IS_OK(error)) {
419 error = reg_val_del(val);
420 if(!W_ERROR_IS_OK(error)) return error;
426 /* Delete all keys below this one */
427 for(i = 0; W_ERROR_IS_OK(error); i++) {
430 error = reg_key_get_subkey_by_index(key, i, &subkey);
431 if(!W_ERROR_IS_OK(error)) return error;
433 error = reg_key_del_recursive(subkey);
434 if(!W_ERROR_IS_OK(error)) return error;
437 return reg_key_del(key);
440 WERROR reg_val_del(REG_VAL *val)
443 if (!val) return WERR_INVALID_PARAM;
445 if (!val->handle->functions->del_value) {
446 DEBUG(1, ("Backend '%s' doesn't support method del_value\n", val->handle->functions->name));
447 return WERR_NOT_SUPPORTED;
450 error = val->handle->functions->del_value(val);
452 if(!W_ERROR_IS_OK(error)) return error;
454 val->parent->cache_values = NULL;
455 val->parent->cache_values_count = 0;
460 WERROR reg_key_add_name_recursive_abs(REG_HANDLE *handle, const char *name)
466 if(strchr(name, '\\')) hivelength = strchr(name, '\\')-name;
467 else hivelength = strlen(name);
469 for(i = 0; W_ERROR_IS_OK(error); i++) {
470 error = reg_get_hive(handle, i, &hive);
471 if(W_ERROR_IS_OK(error) && !strncmp(reg_key_name(hive), name, hivelength)) {
472 return reg_key_add_name_recursive(hive, name);
479 WERROR reg_key_add_name_recursive(REG_KEY *parent, const char *path)
481 REG_KEY *cur, *prevcur = parent;
483 /* FIXME: we should never write to a 'const char *' !!! --metze */
484 char *begin = (char *)path, *end;
487 end = strchr(begin, '\\');
490 error = reg_key_get_subkey_by_name(prevcur, begin, &cur);
492 /* Key is not there, add it */
493 if(W_ERROR_EQUAL(error, WERR_DEST_NOT_FOUND)) {
494 error = reg_key_add_name(prevcur, begin, 0, NULL, &cur);
495 if(!W_ERROR_IS_OK(error)) return error;
498 if(!W_ERROR_IS_OK(error)) {
511 WERROR reg_key_add_name(REG_KEY *parent, const char *name, uint32_t access_mask, SEC_DESC *desc, REG_KEY **newkey)
515 if (!parent) return WERR_INVALID_PARAM;
517 if (!parent->handle->functions->add_key) {
518 DEBUG(1, ("Backend '%s' doesn't support method add_key\n", parent->handle->functions->name));
519 return WERR_NOT_SUPPORTED;
522 error = parent->handle->functions->add_key(parent, name, access_mask, desc, newkey);
524 if(!W_ERROR_IS_OK(error)) return error;
526 (*newkey)->handle = parent->handle;
527 (*newkey)->backend_data = talloc_asprintf((*newkey)->mem_ctx, "%s\\%s", reg_key_get_path(parent), name);
529 parent->cache_subkeys = NULL;
530 parent->cache_subkeys_count = 0;
534 WERROR reg_val_update(REG_VAL *val, int type, void *data, int len)
538 /* A 'real' update function has preference */
539 if (val->handle->functions->update_value)
540 return val->handle->functions->update_value(val, type, data, len);
542 /* Otherwise, just remove and add again */
543 if (val->handle->functions->add_value &&
544 val->handle->functions->del_value) {
546 if(!W_ERROR_IS_OK(error = val->handle->functions->del_value(val)))
549 error = val->handle->functions->add_value(val->parent, val->name, type, data, len);
550 if(!W_ERROR_IS_OK(error)) return error;
551 memcpy(val, new, sizeof(REG_VAL));
552 val->parent->cache_values = NULL;
553 val->parent->cache_values_count = 0;
557 DEBUG(1, ("Backend '%s' doesn't support method update_value\n", val->handle->functions->name));
558 return WERR_NOT_SUPPORTED;
561 void reg_free(REG_HANDLE *h)
563 if(!h->functions->close_registry) return;
565 h->functions->close_registry(h);
568 WERROR reg_get_hive(REG_HANDLE *h, int hivenum, REG_KEY **key)
572 if(h->functions->get_hive) {
573 ret = h->functions->get_hive(h, hivenum, key);
574 } else if(h->functions->open_key) {
575 if(hivenum == 0) ret = h->functions->open_key(h, hivenum, "", key);
576 else ret = WERR_NO_MORE_ITEMS;
578 DEBUG(0, ("Backend '%s' has neither open_root_key nor open_key or get_hive method implemented\n", h->functions->name));
579 ret = WERR_NOT_SUPPORTED;
582 if(W_ERROR_IS_OK(ret)) {
585 (*key)->path = talloc_strdup((*key)->mem_ctx, (*key)->name);
587 (*key)->hive = hivenum;
593 WERROR reg_key_add_value(REG_KEY *key, const char *name, int type, void *value, size_t vallen)
595 WERROR ret = WERR_OK;
596 if(!key->handle->functions->add_value)
597 return WERR_NOT_SUPPORTED;
599 ret = key->handle->functions->add_value(key, name, type, value, vallen);
601 if(!W_ERROR_IS_OK(ret)) return ret;
603 /* Invalidate the cache */
604 key->cache_values = NULL;
605 key->cache_values_count = 0;
609 WERROR reg_save(REG_HANDLE *h, const char *location)
612 return WERR_NOT_SUPPORTED;
615 WERROR reg_key_get_parent(REG_KEY *key, REG_KEY **parent)
622 error = reg_get_hive(key->handle, key->hive, &root);
623 if(!W_ERROR_IS_OK(error)) return error;
625 parent_name = strdup(reg_key_get_path(key));
626 last = strrchr(parent_name, '\\');
629 SAFE_FREE(parent_name);
634 error = reg_open_key(root, parent_name, parent);
635 SAFE_FREE(parent_name);