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 = backends;
64 if (strcmp(entry->functions->name, name)==0) return entry;
71 /* Open a registry file/host/etc */
72 WERROR reg_open(const char *backend, const char *location, const char *credentials, REG_HANDLE **h)
74 struct reg_init_function_entry *entry;
75 static BOOL reg_first_init = True;
82 status = register_subsystem("registry", registry_register);
83 if (!NT_STATUS_IS_OK(status))
84 return WERR_GENERAL_FAILURE;
87 reg_first_init = False;
90 entry = reg_find_backend_entry(backend);
93 DEBUG(0, ("No such registry backend '%s' loaded!\n", backend));
94 return WERR_GENERAL_FAILURE;
97 mem_ctx = talloc_init(backend);
98 ret = talloc(mem_ctx, sizeof(REG_HANDLE));
100 ret->location = location?talloc_strdup(mem_ctx, location):NULL;
101 ret->credentials = credentials?talloc_strdup(mem_ctx, credentials):NULL;
102 ret->functions = entry->functions;
103 ret->backend_data = NULL;
104 ret->mem_ctx = mem_ctx;
107 if(!entry->functions->open_registry) {
111 werr = entry->functions->open_registry(ret, location, credentials);
113 if(W_ERROR_IS_OK(werr))
116 talloc_destroy(mem_ctx);
120 WERROR reg_open_key_abs(REG_HANDLE *handle, const char *name, REG_KEY **result)
126 if(strchr(name, '\\')) hivelength = strchr(name, '\\')-name;
127 else hivelength = strlen(name);
129 for(i = 0; W_ERROR_IS_OK(error); i++) {
130 error = reg_get_hive(handle, i, &hive);
131 if(W_ERROR_IS_OK(error) && !strncmp(reg_key_name(hive), name, hivelength)) {
132 return reg_open_key(hive, name, result);
140 * First tries to use the open_key function from the backend
141 * then falls back to get_subkey_by_name and later get_subkey_by_index
143 WERROR reg_open_key(REG_KEY *parent, const char *name, REG_KEY **result)
150 DEBUG(0, ("Invalid parent key specified"));
151 return WERR_INVALID_PARAM;
154 if(!parent->handle->functions->open_key &&
155 (parent->handle->functions->get_subkey_by_name ||
156 parent->handle->functions->get_subkey_by_index)) {
157 char *orig = strdup(name),
159 *curend = strchr(orig, '\\');
160 REG_KEY *curkey = parent;
162 while(curbegin && *curbegin) {
163 if(curend)*curend = '\0';
164 error = reg_key_get_subkey_by_name(curkey, curbegin, result);
165 if(!W_ERROR_IS_OK(error)) {
170 curbegin = curend + 1;
171 curend = strchr(curbegin, '\\');
179 if(!parent->handle->functions->open_key) {
180 DEBUG(0, ("Registry backend doesn't have get_subkey_by_name nor open_key!\n"));
181 return WERR_NOT_SUPPORTED;
184 mem_ctx = talloc_init("mem_ctx");
186 fullname = talloc_asprintf(mem_ctx, "%s%s%s", reg_key_get_path(parent), strlen(reg_key_get_path(parent))?"\\":"", name);
188 error = parent->handle->functions->open_key(parent->handle, parent->hive, fullname, result);
190 if(!W_ERROR_IS_OK(error)) {
191 talloc_destroy(mem_ctx);
195 (*result)->handle = parent->handle;
196 (*result)->path = talloc_asprintf((*result)->mem_ctx, "%s\\%s", reg_key_get_path_abs(parent), (*result)->name);
197 (*result)->hive = parent->hive;
198 talloc_steal(mem_ctx, (*result)->mem_ctx, fullname);
200 talloc_destroy(mem_ctx);
205 WERROR reg_key_get_value_by_index(REG_KEY *key, int idx, REG_VAL **val)
207 if(!key) return WERR_INVALID_PARAM;
209 if(key->handle->functions->get_value_by_index) {
210 WERROR status = key->handle->functions->get_value_by_index(key, idx, val);
211 if(!W_ERROR_IS_OK(status))
214 } else if(key->handle->functions->fetch_values) {
215 if(!key->cache_values)
216 key->handle->functions->fetch_values(key, &key->cache_values_count, &key->cache_values);
218 if(idx < key->cache_values_count && idx >= 0) {
219 *val = reg_val_dup(key->cache_values[idx]);
221 return WERR_NO_MORE_ITEMS;
224 return WERR_NOT_SUPPORTED;
227 (*val)->parent = key;
228 (*val)->handle = key->handle;
232 WERROR reg_key_num_subkeys(REG_KEY *key, int *count)
234 if(!key) return WERR_INVALID_PARAM;
236 if(key->handle->functions->num_subkeys) {
237 return key->handle->functions->num_subkeys(key, count);
240 if(key->handle->functions->fetch_subkeys) {
241 if(!key->cache_subkeys)
242 key->handle->functions->fetch_subkeys(key, &key->cache_subkeys_count, &key->cache_subkeys);
244 *count = key->cache_subkeys_count;
248 if(key->handle->functions->get_subkey_by_index) {
252 for(i = 0; W_ERROR_IS_OK(error = key->handle->functions->get_subkey_by_index(key, i, &dest)); i++) {
257 if(W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) return WERR_OK;
261 return WERR_NOT_SUPPORTED;
264 WERROR reg_key_num_values(REG_KEY *key, int *count)
267 if(!key) return WERR_INVALID_PARAM;
269 if(!key->handle->functions->num_values) {
270 if(!key->handle->functions->fetch_values) {
271 DEBUG(1, ("Backend '%s' doesn't support enumerating values\n", key->handle->functions->name));
272 return WERR_NOT_SUPPORTED;
275 if(!key->cache_values)
276 key->handle->functions->fetch_values(key, &key->cache_values_count, &key->cache_values);
278 *count = key->cache_values_count;
283 return key->handle->functions->num_values(key, count);
286 WERROR reg_key_get_subkey_by_index(REG_KEY *key, int idx, REG_KEY **subkey)
288 if(!key) return WERR_INVALID_PARAM;
290 if(key->handle->functions->get_subkey_by_index) {
291 WERROR status = key->handle->functions->get_subkey_by_index(key, idx, subkey);
292 if(!NT_STATUS_IS_OK(status)) return status;
293 } else if(key->handle->functions->fetch_subkeys) {
294 if(!key->cache_subkeys)
295 key->handle->functions->fetch_subkeys(key, &key->cache_subkeys_count, &key->cache_subkeys);
297 if(idx < key->cache_subkeys_count) {
298 *subkey = reg_key_dup(key->cache_subkeys[idx]);
300 return WERR_NO_MORE_ITEMS;
303 return WERR_NOT_SUPPORTED;
306 (*subkey)->path = talloc_asprintf((*subkey)->mem_ctx, "%s\\%s", reg_key_get_path_abs(key), (*subkey)->name);
307 (*subkey)->handle = key->handle;
308 (*subkey)->hive = key->hive;
314 WERROR reg_key_get_subkey_by_name(REG_KEY *key, const char *name, REG_KEY **subkey)
317 WERROR error = WERR_OK;
319 if(!key) return WERR_INVALID_PARAM;
321 if(key->handle->functions->get_subkey_by_name) {
322 error = key->handle->functions->get_subkey_by_name(key,name,subkey);
323 } else if(key->handle->functions->get_subkey_by_index || key->handle->functions->fetch_subkeys) {
324 for(i = 0; W_ERROR_IS_OK(error); i++) {
325 error = reg_key_get_subkey_by_index(key, i, subkey);
326 if(W_ERROR_IS_OK(error) && !strcmp((*subkey)->name, name)) {
329 reg_key_free(*subkey);
332 return WERR_NOT_SUPPORTED;
335 if(!W_ERROR_IS_OK(error)) return error;
337 (*subkey)->path = talloc_asprintf((*subkey)->mem_ctx, "%s\\%s", reg_key_get_path_abs(key), (*subkey)->name);
338 (*subkey)->handle = key->handle;
339 (*subkey)->hive = key->hive;
344 WERROR reg_key_get_value_by_name(REG_KEY *key, const char *name, REG_VAL **val)
347 WERROR error = WERR_OK;
349 if(!key) return WERR_INVALID_PARAM;
351 if(key->handle->functions->get_value_by_name) {
352 error = key->handle->functions->get_value_by_name(key,name, val);
354 for(i = 0; W_ERROR_IS_OK(error); i++) {
355 error = reg_key_get_value_by_index(key, i, val);
356 if(W_ERROR_IS_OK(error) && StrCaseCmp((*val)->name, name)) {
363 if(!W_ERROR_IS_OK(error) && !W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
366 (*val)->parent = key;
367 (*val)->handle = key->handle;
372 WERROR reg_key_del(REG_KEY *key)
375 if(!key) return WERR_INVALID_PARAM;
378 if(!key->handle->functions->del_key)
379 return WERR_NOT_SUPPORTED;
381 error = key->handle->functions->del_key(key);
382 if(!W_ERROR_IS_OK(error)) return error;
384 /* Invalidate cache */
385 key->cache_subkeys = NULL;
386 key->cache_subkeys_count = 0;
390 WERROR reg_sync(REG_KEY *h, const char *location)
392 if(!h->handle->functions->sync_key)
395 return h->handle->functions->sync_key(h, location);
398 WERROR reg_key_del_recursive(REG_KEY *key)
400 WERROR error = WERR_OK;
403 /* Delete all values for specified key */
404 for(i = 0; W_ERROR_IS_OK(error); i++) {
406 error = reg_key_get_value_by_index(key, i, &val);
407 if(!W_ERROR_IS_OK(error) && !W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
410 if(W_ERROR_IS_OK(error)) {
411 error = reg_val_del(val);
412 if(!W_ERROR_IS_OK(error)) return error;
418 /* Delete all keys below this one */
419 for(i = 0; W_ERROR_IS_OK(error); i++) {
422 error = reg_key_get_subkey_by_index(key, i, &subkey);
423 if(!W_ERROR_IS_OK(error)) return error;
425 error = reg_key_del_recursive(subkey);
426 if(!W_ERROR_IS_OK(error)) return error;
429 return reg_key_del(key);
432 WERROR reg_val_del(REG_VAL *val)
435 if (!val) return WERR_INVALID_PARAM;
437 if (!val->handle->functions->del_value) {
438 DEBUG(1, ("Backend '%s' doesn't support method del_value\n", val->handle->functions->name));
439 return WERR_NOT_SUPPORTED;
442 error = val->handle->functions->del_value(val);
444 if(!W_ERROR_IS_OK(error)) return error;
446 val->parent->cache_values = NULL;
447 val->parent->cache_values_count = 0;
452 WERROR reg_key_add_name_recursive_abs(REG_HANDLE *handle, const char *name)
458 if(strchr(name, '\\')) hivelength = strchr(name, '\\')-name;
459 else hivelength = strlen(name);
461 for(i = 0; W_ERROR_IS_OK(error); i++) {
462 error = reg_get_hive(handle, i, &hive);
463 if(W_ERROR_IS_OK(error) && !strncmp(reg_key_name(hive), name, hivelength)) {
464 return reg_key_add_name_recursive(hive, name);
471 WERROR reg_key_add_name_recursive(REG_KEY *parent, const char *path)
473 REG_KEY *cur, *prevcur = parent;
475 /* FIXME: we should never write to a 'const char *' !!! --metze */
476 char *begin = (char *)path, *end;
479 end = strchr(begin, '\\');
482 error = reg_key_get_subkey_by_name(prevcur, begin, &cur);
484 /* Key is not there, add it */
485 if(W_ERROR_EQUAL(error, WERR_DEST_NOT_FOUND)) {
486 error = reg_key_add_name(prevcur, begin, 0, NULL, &cur);
487 if(!W_ERROR_IS_OK(error)) return error;
490 if(!W_ERROR_IS_OK(error)) {
503 WERROR reg_key_add_name(REG_KEY *parent, const char *name, uint32 access_mask, SEC_DESC *desc, REG_KEY **newkey)
507 if (!parent) return WERR_INVALID_PARAM;
509 if (!parent->handle->functions->add_key) {
510 DEBUG(1, ("Backend '%s' doesn't support method add_key\n", parent->handle->functions->name));
511 return WERR_NOT_SUPPORTED;
514 error = parent->handle->functions->add_key(parent, name, access_mask, desc, newkey);
516 if(!W_ERROR_IS_OK(error)) return error;
518 (*newkey)->handle = parent->handle;
519 (*newkey)->backend_data = talloc_asprintf((*newkey)->mem_ctx, "%s\\%s", reg_key_get_path(parent), name);
521 parent->cache_subkeys = NULL;
522 parent->cache_subkeys_count = 0;
526 WERROR reg_val_update(REG_VAL *val, int type, void *data, int len)
530 /* A 'real' update function has preference */
531 if (val->handle->functions->update_value)
532 return val->handle->functions->update_value(val, type, data, len);
534 /* Otherwise, just remove and add again */
535 if (val->handle->functions->add_value &&
536 val->handle->functions->del_value) {
538 if(!W_ERROR_IS_OK(error = val->handle->functions->del_value(val)))
541 error = val->handle->functions->add_value(val->parent, val->name, type, data, len);
542 if(!W_ERROR_IS_OK(error)) return error;
543 memcpy(val, new, sizeof(REG_VAL));
544 val->parent->cache_values = NULL;
545 val->parent->cache_values_count = 0;
549 DEBUG(1, ("Backend '%s' doesn't support method update_value\n", val->handle->functions->name));
550 return WERR_NOT_SUPPORTED;
553 void reg_free(REG_HANDLE *h)
555 if(!h->functions->close_registry) return;
557 h->functions->close_registry(h);
560 WERROR reg_get_hive(REG_HANDLE *h, int hivenum, REG_KEY **key)
564 if(h->functions->get_hive) {
565 ret = h->functions->get_hive(h, hivenum, key);
566 } else if(h->functions->open_key) {
567 if(hivenum == 0) ret = h->functions->open_key(h, hivenum, "", key);
568 else ret = WERR_NO_MORE_ITEMS;
570 DEBUG(0, ("Backend '%s' has neither open_root_key nor open_key or get_hive method implemented\n", h->functions->name));
571 ret = WERR_NOT_SUPPORTED;
574 if(W_ERROR_IS_OK(ret)) {
577 (*key)->path = talloc_strdup((*key)->mem_ctx, (*key)->name);
579 (*key)->hive = hivenum;
585 WERROR reg_key_add_value(REG_KEY *key, const char *name, int type, void *value, size_t vallen)
587 WERROR ret = WERR_OK;
588 if(!key->handle->functions->add_value)
589 return WERR_NOT_SUPPORTED;
591 ret = key->handle->functions->add_value(key, name, type, value, vallen);
593 if(!W_ERROR_IS_OK(ret)) return ret;
595 /* Invalidate the cache */
596 key->cache_values = NULL;
597 key->cache_values_count = 0;
601 WERROR reg_save(REG_HANDLE *h, const char *location)
604 return WERR_NOT_SUPPORTED;
607 WERROR reg_key_get_parent(REG_KEY *key, REG_KEY **parent)
614 error = reg_get_hive(key->handle, key->hive, &root);
615 if(!W_ERROR_IS_OK(error)) return error;
617 parent_name = strdup(reg_key_get_path(key));
618 last = strrchr(parent_name, '\\');
621 SAFE_FREE(parent_name);
626 error = reg_open_key(root, parent_name, parent);
627 SAFE_FREE(parent_name);