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_ERR(status)) {
68 DEBUG(0, ("Error registering registry subsystem: %s\n", nt_errstr(status)));
69 /* Don't try the initialisation again */
70 reg_first_init = False;
75 reg_first_init = False;
81 if (strcmp(entry->functions->name, name)==0) return entry;
88 BOOL reg_has_backend(const char *backend)
90 return reg_find_backend_entry(backend) != NULL?True:False;
93 /* Open a registry file/host/etc */
94 WERROR reg_open(const char *backend, const char *location, const char *credentials, REG_HANDLE **h)
96 struct reg_init_function_entry *entry;
102 entry = reg_find_backend_entry(backend);
105 DEBUG(0, ("No such registry backend '%s' loaded!\n", backend));
106 return WERR_GENERAL_FAILURE;
109 mem_ctx = talloc_init(backend);
110 ret = talloc(mem_ctx, sizeof(REG_HANDLE));
112 ret->location = location?talloc_strdup(mem_ctx, location):NULL;
113 ret->credentials = credentials?talloc_strdup(mem_ctx, credentials):NULL;
114 ret->functions = entry->functions;
115 ret->backend_data = NULL;
116 ret->mem_ctx = mem_ctx;
119 if(!entry->functions->open_registry) {
123 werr = entry->functions->open_registry(ret, location, credentials);
125 if(W_ERROR_IS_OK(werr))
128 talloc_destroy(mem_ctx);
132 WERROR reg_open_key_abs(REG_HANDLE *handle, const char *name, REG_KEY **result)
138 if(strchr(name, '\\')) hivelength = strchr(name, '\\')-name;
139 else hivelength = strlen(name);
141 for(i = 0; W_ERROR_IS_OK(error); i++) {
142 error = reg_get_hive(handle, i, &hive);
143 if(W_ERROR_IS_OK(error) && !strncmp(reg_key_name(hive), name, hivelength)) {
144 return reg_open_key(hive, name, result);
152 * First tries to use the open_key function from the backend
153 * then falls back to get_subkey_by_name and later get_subkey_by_index
155 WERROR reg_open_key(REG_KEY *parent, const char *name, REG_KEY **result)
162 DEBUG(0, ("Invalid parent key specified"));
163 return WERR_INVALID_PARAM;
166 if(!parent->handle->functions->open_key &&
167 (parent->handle->functions->get_subkey_by_name ||
168 parent->handle->functions->get_subkey_by_index)) {
169 char *orig = strdup(name),
171 *curend = strchr(orig, '\\');
172 REG_KEY *curkey = parent;
174 while(curbegin && *curbegin) {
175 if(curend)*curend = '\0';
176 error = reg_key_get_subkey_by_name(curkey, curbegin, result);
177 if(!W_ERROR_IS_OK(error)) {
182 curbegin = curend + 1;
183 curend = strchr(curbegin, '\\');
191 if(!parent->handle->functions->open_key) {
192 DEBUG(0, ("Registry backend doesn't have get_subkey_by_name nor open_key!\n"));
193 return WERR_NOT_SUPPORTED;
196 mem_ctx = talloc_init("mem_ctx");
198 fullname = talloc_asprintf(mem_ctx, "%s%s%s", reg_key_get_path(parent), strlen(reg_key_get_path(parent))?"\\":"", name);
200 error = parent->handle->functions->open_key(parent->handle, parent->hive, fullname, result);
202 if(!W_ERROR_IS_OK(error)) {
203 talloc_destroy(mem_ctx);
207 (*result)->handle = parent->handle;
208 (*result)->path = talloc_asprintf((*result)->mem_ctx, "%s\\%s", reg_key_get_path_abs(parent), (*result)->name);
209 (*result)->hive = parent->hive;
210 talloc_steal(mem_ctx, (*result)->mem_ctx, fullname);
212 talloc_destroy(mem_ctx);
217 WERROR reg_key_get_value_by_index(REG_KEY *key, int idx, REG_VAL **val)
219 if(!key) return WERR_INVALID_PARAM;
221 if(key->handle->functions->get_value_by_index) {
222 WERROR status = key->handle->functions->get_value_by_index(key, idx, val);
223 if(!W_ERROR_IS_OK(status))
226 } else if(key->handle->functions->fetch_values) {
227 if(!key->cache_values)
228 key->handle->functions->fetch_values(key, &key->cache_values_count, &key->cache_values);
230 if(idx < key->cache_values_count && idx >= 0) {
231 *val = reg_val_dup(key->cache_values[idx]);
233 return WERR_NO_MORE_ITEMS;
236 return WERR_NOT_SUPPORTED;
239 (*val)->parent = key;
240 (*val)->handle = key->handle;
244 WERROR reg_key_num_subkeys(REG_KEY *key, int *count)
246 if(!key) return WERR_INVALID_PARAM;
248 if(key->handle->functions->num_subkeys) {
249 return key->handle->functions->num_subkeys(key, count);
252 if(key->handle->functions->fetch_subkeys) {
253 if(!key->cache_subkeys)
254 key->handle->functions->fetch_subkeys(key, &key->cache_subkeys_count, &key->cache_subkeys);
256 *count = key->cache_subkeys_count;
260 if(key->handle->functions->get_subkey_by_index) {
264 for(i = 0; W_ERROR_IS_OK(error = key->handle->functions->get_subkey_by_index(key, i, &dest)); i++) {
269 if(W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) return WERR_OK;
273 return WERR_NOT_SUPPORTED;
276 WERROR reg_key_num_values(REG_KEY *key, int *count)
279 if(!key) return WERR_INVALID_PARAM;
281 if(!key->handle->functions->num_values) {
282 if(!key->handle->functions->fetch_values) {
283 DEBUG(1, ("Backend '%s' doesn't support enumerating values\n", key->handle->functions->name));
284 return WERR_NOT_SUPPORTED;
287 if(!key->cache_values)
288 key->handle->functions->fetch_values(key, &key->cache_values_count, &key->cache_values);
290 *count = key->cache_values_count;
295 return key->handle->functions->num_values(key, count);
298 WERROR reg_key_get_subkey_by_index(REG_KEY *key, int idx, REG_KEY **subkey)
300 if(!key) return WERR_INVALID_PARAM;
302 if(key->handle->functions->get_subkey_by_index) {
303 WERROR status = key->handle->functions->get_subkey_by_index(key, idx, subkey);
304 if(!NT_STATUS_IS_OK(status)) return status;
305 } else if(key->handle->functions->fetch_subkeys) {
306 if(!key->cache_subkeys)
307 key->handle->functions->fetch_subkeys(key, &key->cache_subkeys_count, &key->cache_subkeys);
309 if(idx < key->cache_subkeys_count) {
310 *subkey = reg_key_dup(key->cache_subkeys[idx]);
312 return WERR_NO_MORE_ITEMS;
315 return WERR_NOT_SUPPORTED;
318 (*subkey)->path = talloc_asprintf((*subkey)->mem_ctx, "%s\\%s", reg_key_get_path_abs(key), (*subkey)->name);
319 (*subkey)->handle = key->handle;
320 (*subkey)->hive = key->hive;
326 WERROR reg_key_get_subkey_by_name(REG_KEY *key, const char *name, REG_KEY **subkey)
329 WERROR error = WERR_OK;
331 if(!key) return WERR_INVALID_PARAM;
333 if(key->handle->functions->get_subkey_by_name) {
334 error = key->handle->functions->get_subkey_by_name(key,name,subkey);
335 } else if(key->handle->functions->get_subkey_by_index || key->handle->functions->fetch_subkeys) {
336 for(i = 0; W_ERROR_IS_OK(error); i++) {
337 error = reg_key_get_subkey_by_index(key, i, subkey);
338 if(W_ERROR_IS_OK(error) && !strcmp((*subkey)->name, name)) {
341 reg_key_free(*subkey);
344 return WERR_NOT_SUPPORTED;
347 if(!W_ERROR_IS_OK(error)) return error;
349 (*subkey)->path = talloc_asprintf((*subkey)->mem_ctx, "%s\\%s", reg_key_get_path_abs(key), (*subkey)->name);
350 (*subkey)->handle = key->handle;
351 (*subkey)->hive = key->hive;
356 WERROR reg_key_get_value_by_name(REG_KEY *key, const char *name, REG_VAL **val)
359 WERROR error = WERR_OK;
361 if(!key) return WERR_INVALID_PARAM;
363 if(key->handle->functions->get_value_by_name) {
364 error = key->handle->functions->get_value_by_name(key,name, val);
366 for(i = 0; W_ERROR_IS_OK(error); i++) {
367 error = reg_key_get_value_by_index(key, i, val);
368 if(W_ERROR_IS_OK(error) && StrCaseCmp((*val)->name, name)) {
375 if(!W_ERROR_IS_OK(error) && !W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
378 (*val)->parent = key;
379 (*val)->handle = key->handle;
384 WERROR reg_key_del(REG_KEY *key)
387 if(!key) return WERR_INVALID_PARAM;
390 if(!key->handle->functions->del_key)
391 return WERR_NOT_SUPPORTED;
393 error = key->handle->functions->del_key(key);
394 if(!W_ERROR_IS_OK(error)) return error;
396 /* Invalidate cache */
397 key->cache_subkeys = NULL;
398 key->cache_subkeys_count = 0;
402 WERROR reg_sync(REG_KEY *h, const char *location)
404 if(!h->handle->functions->sync_key)
407 return h->handle->functions->sync_key(h, location);
410 WERROR reg_key_del_recursive(REG_KEY *key)
412 WERROR error = WERR_OK;
415 /* Delete all values for specified key */
416 for(i = 0; W_ERROR_IS_OK(error); i++) {
418 error = reg_key_get_value_by_index(key, i, &val);
419 if(!W_ERROR_IS_OK(error) && !W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
422 if(W_ERROR_IS_OK(error)) {
423 error = reg_val_del(val);
424 if(!W_ERROR_IS_OK(error)) return error;
430 /* Delete all keys below this one */
431 for(i = 0; W_ERROR_IS_OK(error); i++) {
434 error = reg_key_get_subkey_by_index(key, i, &subkey);
435 if(!W_ERROR_IS_OK(error)) return error;
437 error = reg_key_del_recursive(subkey);
438 if(!W_ERROR_IS_OK(error)) return error;
441 return reg_key_del(key);
444 WERROR reg_val_del(REG_VAL *val)
447 if (!val) return WERR_INVALID_PARAM;
449 if (!val->handle->functions->del_value) {
450 DEBUG(1, ("Backend '%s' doesn't support method del_value\n", val->handle->functions->name));
451 return WERR_NOT_SUPPORTED;
454 error = val->handle->functions->del_value(val);
456 if(!W_ERROR_IS_OK(error)) return error;
458 val->parent->cache_values = NULL;
459 val->parent->cache_values_count = 0;
464 WERROR reg_key_add_name_recursive_abs(REG_HANDLE *handle, const char *name)
470 if(strchr(name, '\\')) hivelength = strchr(name, '\\')-name;
471 else hivelength = strlen(name);
473 for(i = 0; W_ERROR_IS_OK(error); i++) {
474 error = reg_get_hive(handle, i, &hive);
475 if(W_ERROR_IS_OK(error) && !strncmp(reg_key_name(hive), name, hivelength)) {
476 return reg_key_add_name_recursive(hive, name);
483 WERROR reg_key_add_name_recursive(REG_KEY *parent, const char *path)
485 REG_KEY *cur, *prevcur = parent;
487 /* FIXME: we should never write to a 'const char *' !!! --metze */
488 char *begin = (char *)path, *end;
491 end = strchr(begin, '\\');
494 error = reg_key_get_subkey_by_name(prevcur, begin, &cur);
496 /* Key is not there, add it */
497 if(W_ERROR_EQUAL(error, WERR_DEST_NOT_FOUND)) {
498 error = reg_key_add_name(prevcur, begin, 0, NULL, &cur);
499 if(!W_ERROR_IS_OK(error)) return error;
502 if(!W_ERROR_IS_OK(error)) {
515 WERROR reg_key_add_name(REG_KEY *parent, const char *name, uint32_t access_mask, SEC_DESC *desc, REG_KEY **newkey)
519 if (!parent) return WERR_INVALID_PARAM;
521 if (!parent->handle->functions->add_key) {
522 DEBUG(1, ("Backend '%s' doesn't support method add_key\n", parent->handle->functions->name));
523 return WERR_NOT_SUPPORTED;
526 error = parent->handle->functions->add_key(parent, name, access_mask, desc, newkey);
528 if(!W_ERROR_IS_OK(error)) return error;
530 (*newkey)->handle = parent->handle;
531 (*newkey)->backend_data = talloc_asprintf((*newkey)->mem_ctx, "%s\\%s", reg_key_get_path(parent), name);
533 parent->cache_subkeys = NULL;
534 parent->cache_subkeys_count = 0;
538 WERROR reg_val_update(REG_VAL *val, int type, void *data, int len)
542 /* A 'real' update function has preference */
543 if (val->handle->functions->update_value)
544 return val->handle->functions->update_value(val, type, data, len);
546 /* Otherwise, just remove and add again */
547 if (val->handle->functions->add_value &&
548 val->handle->functions->del_value) {
550 if(!W_ERROR_IS_OK(error = val->handle->functions->del_value(val)))
553 error = val->handle->functions->add_value(val->parent, val->name, type, data, len);
554 if(!W_ERROR_IS_OK(error)) return error;
555 memcpy(val, new, sizeof(REG_VAL));
556 val->parent->cache_values = NULL;
557 val->parent->cache_values_count = 0;
561 DEBUG(1, ("Backend '%s' doesn't support method update_value\n", val->handle->functions->name));
562 return WERR_NOT_SUPPORTED;
565 void reg_free(REG_HANDLE *h)
567 if(!h->functions->close_registry) return;
569 h->functions->close_registry(h);
572 WERROR reg_get_hive(REG_HANDLE *h, int hivenum, REG_KEY **key)
576 if(h->functions->get_hive) {
577 ret = h->functions->get_hive(h, hivenum, key);
578 } else if(h->functions->open_key) {
579 if(hivenum == 0) ret = h->functions->open_key(h, hivenum, "", key);
580 else ret = WERR_NO_MORE_ITEMS;
582 DEBUG(0, ("Backend '%s' has neither open_root_key nor open_key or get_hive method implemented\n", h->functions->name));
583 ret = WERR_NOT_SUPPORTED;
586 if(W_ERROR_IS_OK(ret)) {
589 (*key)->path = talloc_strdup((*key)->mem_ctx, (*key)->name);
591 (*key)->hive = hivenum;
597 WERROR reg_key_add_value(REG_KEY *key, const char *name, int type, void *value, size_t vallen)
599 WERROR ret = WERR_OK;
600 if(!key->handle->functions->add_value)
601 return WERR_NOT_SUPPORTED;
603 ret = key->handle->functions->add_value(key, name, type, value, vallen);
605 if(!W_ERROR_IS_OK(ret)) return ret;
607 /* Invalidate the cache */
608 key->cache_values = NULL;
609 key->cache_values_count = 0;
613 WERROR reg_save(REG_HANDLE *h, const char *location)
616 return WERR_NOT_SUPPORTED;
619 WERROR reg_key_get_parent(REG_KEY *key, REG_KEY **parent)
626 error = reg_get_hive(key->handle, key->hive, &root);
627 if(!W_ERROR_IS_OK(error)) return error;
629 parent_name = strdup(reg_key_get_path(key));
630 last = strrchr(parent_name, '\\');
633 SAFE_FREE(parent_name);
638 error = reg_open_key(root, parent_name, parent);
639 SAFE_FREE(parent_name);