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;
65 status = register_subsystem("registry", registry_register);
66 if (!NT_STATUS_IS_OK(status))
67 return WERR_GENERAL_FAILURE;
70 reg_first_init = False;
76 if (strcmp(entry->functions->name, name)==0) return entry;
83 BOOL reg_has_backend(const char *backend)
85 return reg_find_backend_entry(backend)?True:False;
88 /* Open a registry file/host/etc */
89 WERROR reg_open(const char *backend, const char *location, const char *credentials, REG_HANDLE **h)
91 struct reg_init_function_entry *entry;
97 entry = reg_find_backend_entry(backend);
100 DEBUG(0, ("No such registry backend '%s' loaded!\n", backend));
101 return WERR_GENERAL_FAILURE;
104 mem_ctx = talloc_init(backend);
105 ret = talloc(mem_ctx, sizeof(REG_HANDLE));
107 ret->location = location?talloc_strdup(mem_ctx, location):NULL;
108 ret->credentials = credentials?talloc_strdup(mem_ctx, credentials):NULL;
109 ret->functions = entry->functions;
110 ret->backend_data = NULL;
111 ret->mem_ctx = mem_ctx;
114 if(!entry->functions->open_registry) {
118 werr = entry->functions->open_registry(ret, location, credentials);
120 if(W_ERROR_IS_OK(werr))
123 talloc_destroy(mem_ctx);
127 WERROR reg_open_key_abs(REG_HANDLE *handle, const char *name, REG_KEY **result)
133 if(strchr(name, '\\')) hivelength = strchr(name, '\\')-name;
134 else hivelength = strlen(name);
136 for(i = 0; W_ERROR_IS_OK(error); i++) {
137 error = reg_get_hive(handle, i, &hive);
138 if(W_ERROR_IS_OK(error) && !strncmp(reg_key_name(hive), name, hivelength)) {
139 return reg_open_key(hive, name, result);
147 * First tries to use the open_key function from the backend
148 * then falls back to get_subkey_by_name and later get_subkey_by_index
150 WERROR reg_open_key(REG_KEY *parent, const char *name, REG_KEY **result)
157 DEBUG(0, ("Invalid parent key specified"));
158 return WERR_INVALID_PARAM;
161 if(!parent->handle->functions->open_key &&
162 (parent->handle->functions->get_subkey_by_name ||
163 parent->handle->functions->get_subkey_by_index)) {
164 char *orig = strdup(name),
166 *curend = strchr(orig, '\\');
167 REG_KEY *curkey = parent;
169 while(curbegin && *curbegin) {
170 if(curend)*curend = '\0';
171 error = reg_key_get_subkey_by_name(curkey, curbegin, result);
172 if(!W_ERROR_IS_OK(error)) {
177 curbegin = curend + 1;
178 curend = strchr(curbegin, '\\');
186 if(!parent->handle->functions->open_key) {
187 DEBUG(0, ("Registry backend doesn't have get_subkey_by_name nor open_key!\n"));
188 return WERR_NOT_SUPPORTED;
191 mem_ctx = talloc_init("mem_ctx");
193 fullname = talloc_asprintf(mem_ctx, "%s%s%s", reg_key_get_path(parent), strlen(reg_key_get_path(parent))?"\\":"", name);
195 error = parent->handle->functions->open_key(parent->handle, parent->hive, fullname, result);
197 if(!W_ERROR_IS_OK(error)) {
198 talloc_destroy(mem_ctx);
202 (*result)->handle = parent->handle;
203 (*result)->path = talloc_asprintf((*result)->mem_ctx, "%s\\%s", reg_key_get_path_abs(parent), (*result)->name);
204 (*result)->hive = parent->hive;
205 talloc_steal(mem_ctx, (*result)->mem_ctx, fullname);
207 talloc_destroy(mem_ctx);
212 WERROR reg_key_get_value_by_index(REG_KEY *key, int idx, REG_VAL **val)
214 if(!key) return WERR_INVALID_PARAM;
216 if(key->handle->functions->get_value_by_index) {
217 WERROR status = key->handle->functions->get_value_by_index(key, idx, val);
218 if(!W_ERROR_IS_OK(status))
221 } else if(key->handle->functions->fetch_values) {
222 if(!key->cache_values)
223 key->handle->functions->fetch_values(key, &key->cache_values_count, &key->cache_values);
225 if(idx < key->cache_values_count && idx >= 0) {
226 *val = reg_val_dup(key->cache_values[idx]);
228 return WERR_NO_MORE_ITEMS;
231 return WERR_NOT_SUPPORTED;
234 (*val)->parent = key;
235 (*val)->handle = key->handle;
239 WERROR reg_key_num_subkeys(REG_KEY *key, int *count)
241 if(!key) return WERR_INVALID_PARAM;
243 if(key->handle->functions->num_subkeys) {
244 return key->handle->functions->num_subkeys(key, count);
247 if(key->handle->functions->fetch_subkeys) {
248 if(!key->cache_subkeys)
249 key->handle->functions->fetch_subkeys(key, &key->cache_subkeys_count, &key->cache_subkeys);
251 *count = key->cache_subkeys_count;
255 if(key->handle->functions->get_subkey_by_index) {
259 for(i = 0; W_ERROR_IS_OK(error = key->handle->functions->get_subkey_by_index(key, i, &dest)); i++) {
264 if(W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) return WERR_OK;
268 return WERR_NOT_SUPPORTED;
271 WERROR reg_key_num_values(REG_KEY *key, int *count)
274 if(!key) return WERR_INVALID_PARAM;
276 if(!key->handle->functions->num_values) {
277 if(!key->handle->functions->fetch_values) {
278 DEBUG(1, ("Backend '%s' doesn't support enumerating values\n", key->handle->functions->name));
279 return WERR_NOT_SUPPORTED;
282 if(!key->cache_values)
283 key->handle->functions->fetch_values(key, &key->cache_values_count, &key->cache_values);
285 *count = key->cache_values_count;
290 return key->handle->functions->num_values(key, count);
293 WERROR reg_key_get_subkey_by_index(REG_KEY *key, int idx, REG_KEY **subkey)
295 if(!key) return WERR_INVALID_PARAM;
297 if(key->handle->functions->get_subkey_by_index) {
298 WERROR status = key->handle->functions->get_subkey_by_index(key, idx, subkey);
299 if(!NT_STATUS_IS_OK(status)) return status;
300 } else if(key->handle->functions->fetch_subkeys) {
301 if(!key->cache_subkeys)
302 key->handle->functions->fetch_subkeys(key, &key->cache_subkeys_count, &key->cache_subkeys);
304 if(idx < key->cache_subkeys_count) {
305 *subkey = reg_key_dup(key->cache_subkeys[idx]);
307 return WERR_NO_MORE_ITEMS;
310 return WERR_NOT_SUPPORTED;
313 (*subkey)->path = talloc_asprintf((*subkey)->mem_ctx, "%s\\%s", reg_key_get_path_abs(key), (*subkey)->name);
314 (*subkey)->handle = key->handle;
315 (*subkey)->hive = key->hive;
321 WERROR reg_key_get_subkey_by_name(REG_KEY *key, const char *name, REG_KEY **subkey)
324 WERROR error = WERR_OK;
326 if(!key) return WERR_INVALID_PARAM;
328 if(key->handle->functions->get_subkey_by_name) {
329 error = key->handle->functions->get_subkey_by_name(key,name,subkey);
330 } else if(key->handle->functions->get_subkey_by_index || key->handle->functions->fetch_subkeys) {
331 for(i = 0; W_ERROR_IS_OK(error); i++) {
332 error = reg_key_get_subkey_by_index(key, i, subkey);
333 if(W_ERROR_IS_OK(error) && !strcmp((*subkey)->name, name)) {
336 reg_key_free(*subkey);
339 return WERR_NOT_SUPPORTED;
342 if(!W_ERROR_IS_OK(error)) return error;
344 (*subkey)->path = talloc_asprintf((*subkey)->mem_ctx, "%s\\%s", reg_key_get_path_abs(key), (*subkey)->name);
345 (*subkey)->handle = key->handle;
346 (*subkey)->hive = key->hive;
351 WERROR reg_key_get_value_by_name(REG_KEY *key, const char *name, REG_VAL **val)
354 WERROR error = WERR_OK;
356 if(!key) return WERR_INVALID_PARAM;
358 if(key->handle->functions->get_value_by_name) {
359 error = key->handle->functions->get_value_by_name(key,name, val);
361 for(i = 0; W_ERROR_IS_OK(error); i++) {
362 error = reg_key_get_value_by_index(key, i, val);
363 if(W_ERROR_IS_OK(error) && StrCaseCmp((*val)->name, name)) {
370 if(!W_ERROR_IS_OK(error) && !W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
373 (*val)->parent = key;
374 (*val)->handle = key->handle;
379 WERROR reg_key_del(REG_KEY *key)
382 if(!key) return WERR_INVALID_PARAM;
385 if(!key->handle->functions->del_key)
386 return WERR_NOT_SUPPORTED;
388 error = key->handle->functions->del_key(key);
389 if(!W_ERROR_IS_OK(error)) return error;
391 /* Invalidate cache */
392 key->cache_subkeys = NULL;
393 key->cache_subkeys_count = 0;
397 WERROR reg_sync(REG_KEY *h, const char *location)
399 if(!h->handle->functions->sync_key)
402 return h->handle->functions->sync_key(h, location);
405 WERROR reg_key_del_recursive(REG_KEY *key)
407 WERROR error = WERR_OK;
410 /* Delete all values for specified key */
411 for(i = 0; W_ERROR_IS_OK(error); i++) {
413 error = reg_key_get_value_by_index(key, i, &val);
414 if(!W_ERROR_IS_OK(error) && !W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
417 if(W_ERROR_IS_OK(error)) {
418 error = reg_val_del(val);
419 if(!W_ERROR_IS_OK(error)) return error;
425 /* Delete all keys below this one */
426 for(i = 0; W_ERROR_IS_OK(error); i++) {
429 error = reg_key_get_subkey_by_index(key, i, &subkey);
430 if(!W_ERROR_IS_OK(error)) return error;
432 error = reg_key_del_recursive(subkey);
433 if(!W_ERROR_IS_OK(error)) return error;
436 return reg_key_del(key);
439 WERROR reg_val_del(REG_VAL *val)
442 if (!val) return WERR_INVALID_PARAM;
444 if (!val->handle->functions->del_value) {
445 DEBUG(1, ("Backend '%s' doesn't support method del_value\n", val->handle->functions->name));
446 return WERR_NOT_SUPPORTED;
449 error = val->handle->functions->del_value(val);
451 if(!W_ERROR_IS_OK(error)) return error;
453 val->parent->cache_values = NULL;
454 val->parent->cache_values_count = 0;
459 WERROR reg_key_add_name_recursive_abs(REG_HANDLE *handle, const char *name)
465 if(strchr(name, '\\')) hivelength = strchr(name, '\\')-name;
466 else hivelength = strlen(name);
468 for(i = 0; W_ERROR_IS_OK(error); i++) {
469 error = reg_get_hive(handle, i, &hive);
470 if(W_ERROR_IS_OK(error) && !strncmp(reg_key_name(hive), name, hivelength)) {
471 return reg_key_add_name_recursive(hive, name);
478 WERROR reg_key_add_name_recursive(REG_KEY *parent, const char *path)
480 REG_KEY *cur, *prevcur = parent;
482 /* FIXME: we should never write to a 'const char *' !!! --metze */
483 char *begin = (char *)path, *end;
486 end = strchr(begin, '\\');
489 error = reg_key_get_subkey_by_name(prevcur, begin, &cur);
491 /* Key is not there, add it */
492 if(W_ERROR_EQUAL(error, WERR_DEST_NOT_FOUND)) {
493 error = reg_key_add_name(prevcur, begin, 0, NULL, &cur);
494 if(!W_ERROR_IS_OK(error)) return error;
497 if(!W_ERROR_IS_OK(error)) {
510 WERROR reg_key_add_name(REG_KEY *parent, const char *name, uint32 access_mask, SEC_DESC *desc, REG_KEY **newkey)
514 if (!parent) return WERR_INVALID_PARAM;
516 if (!parent->handle->functions->add_key) {
517 DEBUG(1, ("Backend '%s' doesn't support method add_key\n", parent->handle->functions->name));
518 return WERR_NOT_SUPPORTED;
521 error = parent->handle->functions->add_key(parent, name, access_mask, desc, newkey);
523 if(!W_ERROR_IS_OK(error)) return error;
525 (*newkey)->handle = parent->handle;
526 (*newkey)->backend_data = talloc_asprintf((*newkey)->mem_ctx, "%s\\%s", reg_key_get_path(parent), name);
528 parent->cache_subkeys = NULL;
529 parent->cache_subkeys_count = 0;
533 WERROR reg_val_update(REG_VAL *val, int type, void *data, int len)
537 /* A 'real' update function has preference */
538 if (val->handle->functions->update_value)
539 return val->handle->functions->update_value(val, type, data, len);
541 /* Otherwise, just remove and add again */
542 if (val->handle->functions->add_value &&
543 val->handle->functions->del_value) {
545 if(!W_ERROR_IS_OK(error = val->handle->functions->del_value(val)))
548 error = val->handle->functions->add_value(val->parent, val->name, type, data, len);
549 if(!W_ERROR_IS_OK(error)) return error;
550 memcpy(val, new, sizeof(REG_VAL));
551 val->parent->cache_values = NULL;
552 val->parent->cache_values_count = 0;
556 DEBUG(1, ("Backend '%s' doesn't support method update_value\n", val->handle->functions->name));
557 return WERR_NOT_SUPPORTED;
560 void reg_free(REG_HANDLE *h)
562 if(!h->functions->close_registry) return;
564 h->functions->close_registry(h);
567 WERROR reg_get_hive(REG_HANDLE *h, int hivenum, REG_KEY **key)
571 if(h->functions->get_hive) {
572 ret = h->functions->get_hive(h, hivenum, key);
573 } else if(h->functions->open_key) {
574 if(hivenum == 0) ret = h->functions->open_key(h, hivenum, "", key);
575 else ret = WERR_NO_MORE_ITEMS;
577 DEBUG(0, ("Backend '%s' has neither open_root_key nor open_key or get_hive method implemented\n", h->functions->name));
578 ret = WERR_NOT_SUPPORTED;
581 if(W_ERROR_IS_OK(ret)) {
584 (*key)->path = talloc_strdup((*key)->mem_ctx, (*key)->name);
586 (*key)->hive = hivenum;
592 WERROR reg_key_add_value(REG_KEY *key, const char *name, int type, void *value, size_t vallen)
594 WERROR ret = WERR_OK;
595 if(!key->handle->functions->add_value)
596 return WERR_NOT_SUPPORTED;
598 ret = key->handle->functions->add_value(key, name, type, value, vallen);
600 if(!W_ERROR_IS_OK(ret)) return ret;
602 /* Invalidate the cache */
603 key->cache_values = NULL;
604 key->cache_values_count = 0;
608 WERROR reg_save(REG_HANDLE *h, const char *location)
611 return WERR_NOT_SUPPORTED;
614 WERROR reg_key_get_parent(REG_KEY *key, REG_KEY **parent)
621 error = reg_get_hive(key->handle, key->hive, &root);
622 if(!W_ERROR_IS_OK(error)) return error;
624 parent_name = strdup(reg_key_get_path(key));
625 last = strrchr(parent_name, '\\');
628 SAFE_FREE(parent_name);
633 error = reg_open_key(root, parent_name, parent);
634 SAFE_FREE(parent_name);