s3:registry: user regval_ctr/blob accessor functions in reg_backend_db.c
[Samba/ekacnet.git] / source3 / registry / reg_api.c
blob32759ed6e1e6ebff58c41d64818cc4cb791ad1d2
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Volker Lendecke 2006
5 * Copyright (C) Michael Adam 2007-2008
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 /* Attempt to wrap the existing API in a more winreg.idl-like way */
24 * Here is a list of winreg.idl functions and corresponding implementations
25 * provided here:
27 * 0x00 winreg_OpenHKCR
28 * 0x01 winreg_OpenHKCU
29 * 0x02 winreg_OpenHKLM
30 * 0x03 winreg_OpenHKPD
31 * 0x04 winreg_OpenHKU
32 * 0x05 winreg_CloseKey
33 * 0x06 winreg_CreateKey reg_createkey
34 * 0x07 winreg_DeleteKey reg_deletekey
35 * 0x08 winreg_DeleteValue reg_deletevalue
36 * 0x09 winreg_EnumKey reg_enumkey
37 * 0x0a winreg_EnumValue reg_enumvalue
38 * 0x0b winreg_FlushKey
39 * 0x0c winreg_GetKeySecurity reg_getkeysecurity
40 * 0x0d winreg_LoadKey
41 * 0x0e winreg_NotifyChangeKeyValue
42 * 0x0f winreg_OpenKey reg_openkey
43 * 0x10 winreg_QueryInfoKey reg_queryinfokey
44 * 0x11 winreg_QueryValue reg_queryvalue
45 * 0x12 winreg_ReplaceKey
46 * 0x13 winreg_RestoreKey reg_restorekey
47 * 0x14 winreg_SaveKey reg_savekey
48 * 0x15 winreg_SetKeySecurity reg_setkeysecurity
49 * 0x16 winreg_SetValue reg_setvalue
50 * 0x17 winreg_UnLoadKey
51 * 0x18 winreg_InitiateSystemShutdown
52 * 0x19 winreg_AbortSystemShutdown
53 * 0x1a winreg_GetVersion reg_getversion
54 * 0x1b winreg_OpenHKCC
55 * 0x1c winreg_OpenHKDD
56 * 0x1d winreg_QueryMultipleValues
57 * 0x1e winreg_InitiateSystemShutdownEx
58 * 0x1f winreg_SaveKeyEx
59 * 0x20 winreg_OpenHKPT
60 * 0x21 winreg_OpenHKPN
61 * 0x22 winreg_QueryMultipleValues2
65 #include "includes.h"
66 #include "registry.h"
67 #include "reg_cachehook.h"
68 #include "regfio.h"
69 #include "reg_util_internal.h"
70 #include "reg_backend_db.h"
71 #include "reg_dispatcher.h"
72 #include "reg_util_marshalling.h"
74 #undef DBGC_CLASS
75 #define DBGC_CLASS DBGC_REGISTRY
78 /**********************************************************************
79 * Helper functions
80 **********************************************************************/
82 static WERROR fill_value_cache(struct registry_key *key)
84 WERROR werr;
86 if (key->values != NULL) {
87 if (!reg_values_need_update(key->key, key->values)) {
88 return WERR_OK;
92 werr = regval_ctr_init(key, &(key->values));
93 W_ERROR_NOT_OK_RETURN(werr);
95 if (fetch_reg_values(key->key, key->values) == -1) {
96 TALLOC_FREE(key->values);
97 return WERR_BADFILE;
100 return WERR_OK;
103 static WERROR fill_subkey_cache(struct registry_key *key)
105 WERROR werr;
107 if (key->subkeys != NULL) {
108 if (!reg_subkeys_need_update(key->key, key->subkeys)) {
109 return WERR_OK;
113 werr = regsubkey_ctr_init(key, &(key->subkeys));
114 W_ERROR_NOT_OK_RETURN(werr);
116 if (fetch_reg_keys(key->key, key->subkeys) == -1) {
117 TALLOC_FREE(key->subkeys);
118 return WERR_NO_MORE_ITEMS;
121 return WERR_OK;
124 static int regkey_destructor(struct registry_key_handle *key)
126 return regdb_close();
129 static WERROR regkey_open_onelevel(TALLOC_CTX *mem_ctx,
130 struct registry_key *parent,
131 const char *name,
132 const struct nt_user_token *token,
133 uint32 access_desired,
134 struct registry_key **pregkey)
136 WERROR result = WERR_OK;
137 struct registry_key *regkey;
138 struct registry_key_handle *key;
139 struct regsubkey_ctr *subkeys = NULL;
141 DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name));
143 SMB_ASSERT(strchr(name, '\\') == NULL);
145 if (!(regkey = TALLOC_ZERO_P(mem_ctx, struct registry_key)) ||
146 !(regkey->token = dup_nt_token(regkey, token)) ||
147 !(regkey->key = TALLOC_ZERO_P(regkey, struct registry_key_handle)))
149 result = WERR_NOMEM;
150 goto done;
153 if ( !(W_ERROR_IS_OK(result = regdb_open())) ) {
154 goto done;
157 key = regkey->key;
158 talloc_set_destructor(key, regkey_destructor);
160 /* initialization */
162 key->type = REG_KEY_GENERIC;
164 if (name[0] == '\0') {
166 * Open a copy of the parent key
168 if (!parent) {
169 result = WERR_BADFILE;
170 goto done;
172 key->name = talloc_strdup(key, parent->key->name);
174 else {
176 * Normal subkey open
178 key->name = talloc_asprintf(key, "%s%s%s",
179 parent ? parent->key->name : "",
180 parent ? "\\": "",
181 name);
184 if (key->name == NULL) {
185 result = WERR_NOMEM;
186 goto done;
189 /* Tag this as a Performance Counter Key */
191 if( StrnCaseCmp(key->name, KEY_HKPD, strlen(KEY_HKPD)) == 0 )
192 key->type = REG_KEY_HKPD;
194 /* Look up the table of registry I/O operations */
196 if ( !(key->ops = reghook_cache_find( key->name )) ) {
197 DEBUG(0,("reg_open_onelevel: Failed to assign "
198 "registry_ops to [%s]\n", key->name ));
199 result = WERR_BADFILE;
200 goto done;
203 /* check if the path really exists; failed is indicated by -1 */
204 /* if the subkey count failed, bail out */
206 result = regsubkey_ctr_init(key, &subkeys);
207 if (!W_ERROR_IS_OK(result)) {
208 goto done;
211 if ( fetch_reg_keys( key, subkeys ) == -1 ) {
212 result = WERR_BADFILE;
213 goto done;
216 TALLOC_FREE( subkeys );
218 if ( !regkey_access_check( key, access_desired, &key->access_granted,
219 token ) ) {
220 result = WERR_ACCESS_DENIED;
221 goto done;
224 *pregkey = regkey;
225 result = WERR_OK;
227 done:
228 if ( !W_ERROR_IS_OK(result) ) {
229 TALLOC_FREE(regkey);
232 return result;
235 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
236 uint32 desired_access,
237 const struct nt_user_token *token,
238 struct registry_key **pkey)
240 SMB_ASSERT(hive != NULL);
241 SMB_ASSERT(hive[0] != '\0');
242 SMB_ASSERT(strchr(hive, '\\') == NULL);
244 return regkey_open_onelevel(mem_ctx, NULL, hive, token, desired_access,
245 pkey);
249 /**********************************************************************
250 * The API functions
251 **********************************************************************/
253 WERROR reg_openkey(TALLOC_CTX *mem_ctx, struct registry_key *parent,
254 const char *name, uint32 desired_access,
255 struct registry_key **pkey)
257 struct registry_key *direct_parent = parent;
258 WERROR err;
259 char *p, *path, *to_free;
260 size_t len;
262 if (!(path = SMB_STRDUP(name))) {
263 return WERR_NOMEM;
265 to_free = path;
267 len = strlen(path);
269 if ((len > 0) && (path[len-1] == '\\')) {
270 path[len-1] = '\0';
273 while ((p = strchr(path, '\\')) != NULL) {
274 char *name_component;
275 struct registry_key *tmp;
277 if (!(name_component = SMB_STRNDUP(path, (p - path)))) {
278 err = WERR_NOMEM;
279 goto error;
282 err = regkey_open_onelevel(mem_ctx, direct_parent,
283 name_component, parent->token,
284 KEY_ENUMERATE_SUB_KEYS, &tmp);
285 SAFE_FREE(name_component);
287 if (!W_ERROR_IS_OK(err)) {
288 goto error;
290 if (direct_parent != parent) {
291 TALLOC_FREE(direct_parent);
294 direct_parent = tmp;
295 path = p+1;
298 err = regkey_open_onelevel(mem_ctx, direct_parent, path, parent->token,
299 desired_access, pkey);
300 error:
301 if (direct_parent != parent) {
302 TALLOC_FREE(direct_parent);
304 SAFE_FREE(to_free);
305 return err;
308 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
309 uint32 idx, char **name, NTTIME *last_write_time)
311 WERROR err;
313 if (!(key->key->access_granted & KEY_ENUMERATE_SUB_KEYS)) {
314 return WERR_ACCESS_DENIED;
317 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
318 return err;
321 if (idx >= regsubkey_ctr_numkeys(key->subkeys)) {
322 return WERR_NO_MORE_ITEMS;
325 if (!(*name = talloc_strdup(mem_ctx,
326 regsubkey_ctr_specific_key(key->subkeys, idx))))
328 return WERR_NOMEM;
331 if (last_write_time) {
332 *last_write_time = 0;
335 return WERR_OK;
338 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
339 uint32 idx, char **pname, struct registry_value **pval)
341 struct registry_value *val;
342 struct regval_blob *blob;
343 WERROR err;
345 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
346 return WERR_ACCESS_DENIED;
349 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
350 return err;
353 if (idx >= regval_ctr_numvals(key->values)) {
354 return WERR_NO_MORE_ITEMS;
357 blob = regval_ctr_specific_value(key->values, idx);
358 err = registry_pull_value(mem_ctx, &val,
359 regval_type(blob),
360 regval_data_p(blob),
361 regval_size(blob),
362 regval_size(blob));
363 if (!W_ERROR_IS_OK(err)) {
364 return err;
367 if (pname
368 && !(*pname = talloc_strdup(
369 mem_ctx, regval_name(blob)))) {
370 SAFE_FREE(val);
371 return WERR_NOMEM;
374 *pval = val;
375 return WERR_OK;
378 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
379 const char *name, struct registry_value **pval)
381 WERROR err;
382 uint32 i;
384 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
385 return WERR_ACCESS_DENIED;
388 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
389 return err;
392 for (i=0; i < regval_ctr_numvals(key->values); i++) {
393 struct regval_blob *blob;
394 blob = regval_ctr_specific_value(key->values, i);
395 if (strequal(regval_name(blob), name)) {
396 return reg_enumvalue(mem_ctx, key, i, NULL, pval);
400 return WERR_BADFILE;
403 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
404 uint32_t *max_subkeylen, uint32_t *max_subkeysize,
405 uint32_t *num_values, uint32_t *max_valnamelen,
406 uint32_t *max_valbufsize, uint32_t *secdescsize,
407 NTTIME *last_changed_time)
409 uint32 i, max_size;
410 size_t max_len;
411 TALLOC_CTX *mem_ctx;
412 WERROR err;
413 struct security_descriptor *secdesc;
415 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
416 return WERR_ACCESS_DENIED;
419 if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
420 !W_ERROR_IS_OK(fill_value_cache(key))) {
421 return WERR_BADFILE;
424 max_len = 0;
425 for (i=0; i< regsubkey_ctr_numkeys(key->subkeys); i++) {
426 max_len = MAX(max_len,
427 strlen(regsubkey_ctr_specific_key(key->subkeys, i)));
430 *num_subkeys = regsubkey_ctr_numkeys(key->subkeys);
431 *max_subkeylen = max_len;
432 *max_subkeysize = 0; /* Class length? */
434 max_len = 0;
435 max_size = 0;
436 for (i=0; i < regval_ctr_numvals(key->values); i++) {
437 struct regval_blob *blob;
438 blob = regval_ctr_specific_value(key->values, i);
439 max_len = MAX(max_len, strlen(regval_name(blob)));
440 max_size = MAX(max_size, regval_size(blob));
443 *num_values = regval_ctr_numvals(key->values);
444 *max_valnamelen = max_len;
445 *max_valbufsize = max_size;
447 if (!(mem_ctx = talloc_new(key))) {
448 return WERR_NOMEM;
451 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
452 if (!W_ERROR_IS_OK(err)) {
453 TALLOC_FREE(mem_ctx);
454 return err;
457 *secdescsize = ndr_size_security_descriptor(secdesc, 0);
458 TALLOC_FREE(mem_ctx);
460 *last_changed_time = 0;
462 return WERR_OK;
465 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
466 const char *subkeypath, uint32 desired_access,
467 struct registry_key **pkey,
468 enum winreg_CreateAction *paction)
470 struct registry_key *key = parent;
471 struct registry_key *create_parent;
472 TALLOC_CTX *mem_ctx;
473 char *path, *end;
474 WERROR err;
477 * We must refuse to handle subkey-paths containing
478 * a '/' character because at a lower level, after
479 * normalization, '/' is treated as a key separator
480 * just like '\\'.
482 if (strchr(subkeypath, '/') != NULL) {
483 return WERR_INVALID_PARAM;
486 if (!(mem_ctx = talloc_new(ctx))) return WERR_NOMEM;
488 if (!(path = talloc_strdup(mem_ctx, subkeypath))) {
489 err = WERR_NOMEM;
490 goto done;
493 while ((end = strchr(path, '\\')) != NULL) {
494 struct registry_key *tmp;
495 enum winreg_CreateAction action;
497 *end = '\0';
499 err = reg_createkey(mem_ctx, key, path,
500 KEY_ENUMERATE_SUB_KEYS, &tmp, &action);
501 if (!W_ERROR_IS_OK(err)) {
502 goto done;
505 if (key != parent) {
506 TALLOC_FREE(key);
509 key = tmp;
510 path = end+1;
514 * At this point, "path" contains the one-element subkey of "key". We
515 * can try to open it.
518 err = reg_openkey(ctx, key, path, desired_access, pkey);
519 if (W_ERROR_IS_OK(err)) {
520 if (paction != NULL) {
521 *paction = REG_OPENED_EXISTING_KEY;
523 goto done;
526 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
528 * Something but "notfound" has happened, so bail out
530 goto done;
534 * We have to make a copy of the current key, as we opened it only
535 * with ENUM_SUBKEY access.
538 err = reg_openkey(mem_ctx, key, "", KEY_CREATE_SUB_KEY,
539 &create_parent);
540 if (!W_ERROR_IS_OK(err)) {
541 goto done;
545 * Actually create the subkey
548 err = fill_subkey_cache(create_parent);
549 if (!W_ERROR_IS_OK(err)) goto done;
551 err = create_reg_subkey(key->key, path);
552 W_ERROR_NOT_OK_GOTO_DONE(err);
555 * Now open the newly created key
558 err = reg_openkey(ctx, create_parent, path, desired_access, pkey);
559 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
560 *paction = REG_CREATED_NEW_KEY;
563 done:
564 TALLOC_FREE(mem_ctx);
565 return err;
568 WERROR reg_deletekey(struct registry_key *parent, const char *path)
570 WERROR err;
571 char *name, *end;
572 struct registry_key *tmp_key, *key;
573 TALLOC_CTX *mem_ctx = talloc_stackframe();
575 name = talloc_strdup(mem_ctx, path);
576 if (name == NULL) {
577 err = WERR_NOMEM;
578 goto done;
581 /* check if the key has subkeys */
582 err = reg_openkey(mem_ctx, parent, name, REG_KEY_READ, &key);
583 W_ERROR_NOT_OK_GOTO_DONE(err);
585 err = fill_subkey_cache(key);
586 W_ERROR_NOT_OK_GOTO_DONE(err);
588 if (regsubkey_ctr_numkeys(key->subkeys) > 0) {
589 err = WERR_ACCESS_DENIED;
590 goto done;
593 /* no subkeys - proceed with delete */
594 end = strrchr(name, '\\');
595 if (end != NULL) {
596 *end = '\0';
598 err = reg_openkey(mem_ctx, parent, name,
599 KEY_CREATE_SUB_KEY, &tmp_key);
600 W_ERROR_NOT_OK_GOTO_DONE(err);
602 parent = tmp_key;
603 name = end+1;
606 if (name[0] == '\0') {
607 err = WERR_INVALID_PARAM;
608 goto done;
611 err = delete_reg_subkey(parent->key, name);
613 done:
614 TALLOC_FREE(mem_ctx);
615 return err;
618 WERROR reg_setvalue(struct registry_key *key, const char *name,
619 const struct registry_value *val)
621 WERROR err;
622 DATA_BLOB value_data;
623 int res;
625 if (!(key->key->access_granted & KEY_SET_VALUE)) {
626 return WERR_ACCESS_DENIED;
629 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
630 return err;
633 err = registry_push_value(key, val, &value_data);
634 if (!W_ERROR_IS_OK(err)) {
635 return err;
638 res = regval_ctr_addvalue(key->values, name, val->type,
639 value_data.data, value_data.length);
640 TALLOC_FREE(value_data.data);
642 if (res == 0) {
643 TALLOC_FREE(key->values);
644 return WERR_NOMEM;
647 if (!store_reg_values(key->key, key->values)) {
648 TALLOC_FREE(key->values);
649 return WERR_REG_IO_FAILURE;
652 return WERR_OK;
655 static WERROR reg_value_exists(struct registry_key *key, const char *name)
657 struct regval_blob *blob;
659 blob = regval_ctr_getvalue(key->values, name);
661 if (blob == NULL) {
662 return WERR_BADFILE;
663 } else {
664 return WERR_OK;
668 WERROR reg_deletevalue(struct registry_key *key, const char *name)
670 WERROR err;
672 if (!(key->key->access_granted & KEY_SET_VALUE)) {
673 return WERR_ACCESS_DENIED;
676 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
677 return err;
680 err = reg_value_exists(key, name);
681 if (!W_ERROR_IS_OK(err)) {
682 return err;
685 regval_ctr_delvalue(key->values, name);
687 if (!store_reg_values(key->key, key->values)) {
688 TALLOC_FREE(key->values);
689 return WERR_REG_IO_FAILURE;
692 return WERR_OK;
695 WERROR reg_getkeysecurity(TALLOC_CTX *mem_ctx, struct registry_key *key,
696 struct security_descriptor **psecdesc)
698 return regkey_get_secdesc(mem_ctx, key->key, psecdesc);
701 WERROR reg_setkeysecurity(struct registry_key *key,
702 struct security_descriptor *psecdesc)
704 return regkey_set_secdesc(key->key, psecdesc);
707 WERROR reg_getversion(uint32_t *version)
709 if (version == NULL) {
710 return WERR_INVALID_PARAM;
713 *version = 0x00000005; /* Windows 2000 registry API version */
714 return WERR_OK;
717 /*******************************************************************
718 Note: topkeypat is the *full* path that this *key will be
719 loaded into (including the name of the key)
720 ********************************************************************/
722 static WERROR reg_load_tree(REGF_FILE *regfile, const char *topkeypath,
723 REGF_NK_REC *key)
725 REGF_NK_REC *subkey;
726 struct registry_key_handle registry_key;
727 struct regval_ctr *values;
728 struct regsubkey_ctr *subkeys;
729 int i;
730 char *path = NULL;
731 WERROR result = WERR_OK;
733 /* initialize the struct registry_key_handle structure */
735 registry_key.ops = reghook_cache_find(topkeypath);
736 if (!registry_key.ops) {
737 DEBUG(0, ("reg_load_tree: Failed to assign registry_ops "
738 "to [%s]\n", topkeypath));
739 return WERR_BADFILE;
742 registry_key.name = talloc_strdup(regfile->mem_ctx, topkeypath);
743 if (!registry_key.name) {
744 DEBUG(0, ("reg_load_tree: Talloc failed for reg_key.name!\n"));
745 return WERR_NOMEM;
748 /* now start parsing the values and subkeys */
750 result = regsubkey_ctr_init(regfile->mem_ctx, &subkeys);
751 W_ERROR_NOT_OK_RETURN(result);
753 result = regval_ctr_init(subkeys, &values);
754 W_ERROR_NOT_OK_RETURN(result);
756 /* copy values into the struct regval_ctr */
758 for (i=0; i<key->num_values; i++) {
759 regval_ctr_addvalue(values, key->values[i].valuename,
760 key->values[i].type,
761 key->values[i].data,
762 (key->values[i].data_size & ~VK_DATA_IN_OFFSET));
765 /* copy subkeys into the struct regsubkey_ctr */
767 key->subkey_index = 0;
768 while ((subkey = regfio_fetch_subkey( regfile, key ))) {
769 result = regsubkey_ctr_addkey(subkeys, subkey->keyname);
770 if (!W_ERROR_IS_OK(result)) {
771 TALLOC_FREE(subkeys);
772 return result;
776 /* write this key and values out */
778 if (!store_reg_values(&registry_key, values)
779 || !store_reg_keys(&registry_key, subkeys))
781 DEBUG(0,("reg_load_tree: Failed to load %s!\n", topkeypath));
782 result = WERR_REG_IO_FAILURE;
785 TALLOC_FREE(subkeys);
787 if (!W_ERROR_IS_OK(result)) {
788 return result;
791 /* now continue to load each subkey registry tree */
793 key->subkey_index = 0;
794 while ((subkey = regfio_fetch_subkey(regfile, key))) {
795 path = talloc_asprintf(regfile->mem_ctx,
796 "%s\\%s",
797 topkeypath,
798 subkey->keyname);
799 if (path == NULL) {
800 return WERR_NOMEM;
802 result = reg_load_tree(regfile, path, subkey);
803 if (!W_ERROR_IS_OK(result)) {
804 break;
808 return result;
811 /*******************************************************************
812 ********************************************************************/
814 static WERROR restore_registry_key(struct registry_key_handle *krecord,
815 const char *fname)
817 REGF_FILE *regfile;
818 REGF_NK_REC *rootkey;
819 WERROR result;
821 /* open the registry file....fail if the file already exists */
823 regfile = regfio_open(fname, (O_RDONLY), 0);
824 if (regfile == NULL) {
825 DEBUG(0, ("restore_registry_key: failed to open \"%s\" (%s)\n",
826 fname, strerror(errno)));
827 return ntstatus_to_werror(map_nt_error_from_unix(errno));
830 /* get the rootkey from the regf file and then load the tree
831 via recursive calls */
833 if (!(rootkey = regfio_rootkey(regfile))) {
834 regfio_close(regfile);
835 return WERR_REG_FILE_INVALID;
838 result = reg_load_tree(regfile, krecord->name, rootkey);
840 /* cleanup */
842 regfio_close(regfile);
844 return result;
847 WERROR reg_restorekey(struct registry_key *key, const char *fname)
849 return restore_registry_key(key->key, fname);
852 /********************************************************************
853 ********************************************************************/
855 static WERROR reg_write_tree(REGF_FILE *regfile, const char *keypath,
856 REGF_NK_REC *parent)
858 REGF_NK_REC *key;
859 struct regval_ctr *values;
860 struct regsubkey_ctr *subkeys;
861 int i, num_subkeys;
862 char *key_tmp = NULL;
863 char *keyname, *parentpath;
864 char *subkeypath = NULL;
865 char *subkeyname;
866 struct registry_key_handle registry_key;
867 WERROR result = WERR_OK;
868 struct security_descriptor *sec_desc = NULL;
870 if (!regfile) {
871 return WERR_GENERAL_FAILURE;
874 if (!keypath) {
875 return WERR_OBJECT_PATH_INVALID;
878 /* split up the registry key path */
880 key_tmp = talloc_strdup(regfile->mem_ctx, keypath);
881 if (!key_tmp) {
882 return WERR_NOMEM;
884 if (!reg_split_key(key_tmp, &parentpath, &keyname)) {
885 return WERR_OBJECT_PATH_INVALID;
888 if (!keyname) {
889 keyname = parentpath;
892 /* we need a registry_key_handle object here to enumerate subkeys and values */
894 ZERO_STRUCT(registry_key);
896 registry_key.name = talloc_strdup(regfile->mem_ctx, keypath);
897 if (registry_key.name == NULL) {
898 return WERR_NOMEM;
901 registry_key.ops = reghook_cache_find(registry_key.name);
902 if (registry_key.ops == NULL) {
903 return WERR_BADFILE;
906 /* lookup the values and subkeys */
908 result = regsubkey_ctr_init(regfile->mem_ctx, &subkeys);
909 W_ERROR_NOT_OK_RETURN(result);
911 result = regval_ctr_init(subkeys, &values);
912 W_ERROR_NOT_OK_RETURN(result);
914 fetch_reg_keys(&registry_key, subkeys);
915 fetch_reg_values(&registry_key, values);
917 result = regkey_get_secdesc(regfile->mem_ctx, &registry_key, &sec_desc);
918 if (!W_ERROR_IS_OK(result)) {
919 goto done;
922 /* write out this key */
924 key = regfio_write_key(regfile, keyname, values, subkeys, sec_desc,
925 parent);
926 if (key == NULL) {
927 result = WERR_CAN_NOT_COMPLETE;
928 goto done;
931 /* write each one of the subkeys out */
933 num_subkeys = regsubkey_ctr_numkeys(subkeys);
934 for (i=0; i<num_subkeys; i++) {
935 subkeyname = regsubkey_ctr_specific_key(subkeys, i);
936 subkeypath = talloc_asprintf(regfile->mem_ctx, "%s\\%s",
937 keypath, subkeyname);
938 if (subkeypath == NULL) {
939 result = WERR_NOMEM;
940 goto done;
942 result = reg_write_tree(regfile, subkeypath, key);
943 if (!W_ERROR_IS_OK(result))
944 goto done;
947 DEBUG(6, ("reg_write_tree: wrote key [%s]\n", keypath));
949 done:
950 TALLOC_FREE(subkeys);
951 TALLOC_FREE(registry_key.name);
953 return result;
956 static WERROR backup_registry_key(struct registry_key_handle *krecord,
957 const char *fname)
959 REGF_FILE *regfile;
960 WERROR result;
962 /* open the registry file....fail if the file already exists */
964 regfile = regfio_open(fname, (O_RDWR|O_CREAT|O_EXCL),
965 (S_IREAD|S_IWRITE));
966 if (regfile == NULL) {
967 DEBUG(0,("backup_registry_key: failed to open \"%s\" (%s)\n",
968 fname, strerror(errno) ));
969 return ntstatus_to_werror(map_nt_error_from_unix(errno));
972 /* write the registry tree to the file */
974 result = reg_write_tree(regfile, krecord->name, NULL);
976 /* cleanup */
978 regfio_close(regfile);
980 return result;
983 WERROR reg_savekey(struct registry_key *key, const char *fname)
985 return backup_registry_key(key->key, fname);
988 /**********************************************************************
989 * Higher level utility functions
990 **********************************************************************/
992 WERROR reg_deleteallvalues(struct registry_key *key)
994 WERROR err;
995 int i;
997 if (!(key->key->access_granted & KEY_SET_VALUE)) {
998 return WERR_ACCESS_DENIED;
1001 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
1002 return err;
1005 for (i=0; i < regval_ctr_numvals(key->values); i++) {
1006 struct regval_blob *blob;
1007 blob = regval_ctr_specific_value(key->values, i);
1008 regval_ctr_delvalue(key->values, regval_name(blob));
1011 if (!store_reg_values(key->key, key->values)) {
1012 TALLOC_FREE(key->values);
1013 return WERR_REG_IO_FAILURE;
1016 return WERR_OK;
1020 * Utility function to open a complete registry path including the hive prefix.
1023 WERROR reg_open_path(TALLOC_CTX *mem_ctx, const char *orig_path,
1024 uint32 desired_access, const struct nt_user_token *token,
1025 struct registry_key **pkey)
1027 struct registry_key *hive, *key;
1028 char *path, *p;
1029 WERROR err;
1031 if (!(path = SMB_STRDUP(orig_path))) {
1032 return WERR_NOMEM;
1035 p = strchr(path, '\\');
1037 if ((p == NULL) || (p[1] == '\0')) {
1039 * No key behind the hive, just return the hive
1042 err = reg_openhive(mem_ctx, path, desired_access, token,
1043 &hive);
1044 if (!W_ERROR_IS_OK(err)) {
1045 SAFE_FREE(path);
1046 return err;
1048 SAFE_FREE(path);
1049 *pkey = hive;
1050 return WERR_OK;
1053 *p = '\0';
1055 err = reg_openhive(mem_ctx, path, KEY_ENUMERATE_SUB_KEYS, token,
1056 &hive);
1057 if (!W_ERROR_IS_OK(err)) {
1058 SAFE_FREE(path);
1059 return err;
1062 err = reg_openkey(mem_ctx, hive, p+1, desired_access, &key);
1064 TALLOC_FREE(hive);
1065 SAFE_FREE(path);
1067 if (!W_ERROR_IS_OK(err)) {
1068 return err;
1071 *pkey = key;
1072 return WERR_OK;
1076 * Utility function to delete a registry key with all its subkeys.
1077 * Note that reg_deletekey returns ACCESS_DENIED when called on a
1078 * key that has subkeys.
1080 static WERROR reg_deletekey_recursive_internal(TALLOC_CTX *ctx,
1081 struct registry_key *parent,
1082 const char *path,
1083 bool del_key)
1085 TALLOC_CTX *mem_ctx = NULL;
1086 WERROR werr = WERR_OK;
1087 struct registry_key *key;
1088 char *subkey_name = NULL;
1089 uint32 i;
1091 mem_ctx = talloc_new(ctx);
1092 if (mem_ctx == NULL) {
1093 werr = WERR_NOMEM;
1094 goto done;
1097 /* recurse through subkeys first */
1098 werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
1099 if (!W_ERROR_IS_OK(werr)) {
1100 goto done;
1103 werr = fill_subkey_cache(key);
1104 W_ERROR_NOT_OK_GOTO_DONE(werr);
1107 * loop from top to bottom for perfomance:
1108 * this way, we need to rehash the regsubkey containers less
1110 for (i = regsubkey_ctr_numkeys(key->subkeys) ; i > 0; i--) {
1111 subkey_name = regsubkey_ctr_specific_key(key->subkeys, i-1);
1112 werr = reg_deletekey_recursive_internal(mem_ctx, key,
1113 subkey_name,
1114 true);
1115 W_ERROR_NOT_OK_GOTO_DONE(werr);
1118 if (del_key) {
1119 /* now delete the actual key */
1120 werr = reg_deletekey(parent, path);
1123 done:
1124 TALLOC_FREE(mem_ctx);
1125 return werr;
1128 static WERROR reg_deletekey_recursive_trans(TALLOC_CTX *ctx,
1129 struct registry_key *parent,
1130 const char *path,
1131 bool del_key)
1133 WERROR werr;
1135 werr = regdb_transaction_start();
1136 if (!W_ERROR_IS_OK(werr)) {
1137 DEBUG(0, ("reg_deletekey_recursive_trans: "
1138 "error starting transaction: %s\n",
1139 win_errstr(werr)));
1140 return werr;
1143 werr = reg_deletekey_recursive_internal(ctx, parent, path, del_key);
1145 if (!W_ERROR_IS_OK(werr)) {
1146 DEBUG(1, (__location__ " failed to delete key '%s' from key "
1147 "'%s': %s\n", path, parent->key->name,
1148 win_errstr(werr)));
1149 werr = regdb_transaction_cancel();
1150 if (!W_ERROR_IS_OK(werr)) {
1151 DEBUG(0, ("reg_deletekey_recursive_trans: "
1152 "error cancelling transaction: %s\n",
1153 win_errstr(werr)));
1155 } else {
1156 werr = regdb_transaction_commit();
1157 if (!W_ERROR_IS_OK(werr)) {
1158 DEBUG(0, ("reg_deletekey_recursive_trans: "
1159 "error committing transaction: %s\n",
1160 win_errstr(werr)));
1164 return werr;
1167 WERROR reg_deletekey_recursive(TALLOC_CTX *ctx,
1168 struct registry_key *parent,
1169 const char *path)
1171 return reg_deletekey_recursive_trans(ctx, parent, path, true);
1174 WERROR reg_deletesubkeys_recursive(TALLOC_CTX *ctx,
1175 struct registry_key *parent,
1176 const char *path)
1178 return reg_deletekey_recursive_trans(ctx, parent, path, false);
1181 #if 0
1182 /* these two functions are unused. */
1185 * Utility function to create a registry key without opening the hive
1186 * before. Assumes the hive already exists.
1189 WERROR reg_create_path(TALLOC_CTX *mem_ctx, const char *orig_path,
1190 uint32 desired_access,
1191 const struct nt_user_token *token,
1192 enum winreg_CreateAction *paction,
1193 struct registry_key **pkey)
1195 struct registry_key *hive;
1196 char *path, *p;
1197 WERROR err;
1199 if (!(path = SMB_STRDUP(orig_path))) {
1200 return WERR_NOMEM;
1203 p = strchr(path, '\\');
1205 if ((p == NULL) || (p[1] == '\0')) {
1207 * No key behind the hive, just return the hive
1210 err = reg_openhive(mem_ctx, path, desired_access, token,
1211 &hive);
1212 if (!W_ERROR_IS_OK(err)) {
1213 SAFE_FREE(path);
1214 return err;
1216 SAFE_FREE(path);
1217 *pkey = hive;
1218 *paction = REG_OPENED_EXISTING_KEY;
1219 return WERR_OK;
1222 *p = '\0';
1224 err = reg_openhive(mem_ctx, path,
1225 (strchr(p+1, '\\') != NULL) ?
1226 KEY_ENUMERATE_SUB_KEYS : KEY_CREATE_SUB_KEY,
1227 token, &hive);
1228 if (!W_ERROR_IS_OK(err)) {
1229 SAFE_FREE(path);
1230 return err;
1233 err = reg_createkey(mem_ctx, hive, p+1, desired_access, pkey, paction);
1234 SAFE_FREE(path);
1235 TALLOC_FREE(hive);
1236 return err;
1240 * Utility function to create a registry key without opening the hive
1241 * before. Will not delete a hive.
1244 WERROR reg_delete_path(const struct nt_user_token *token,
1245 const char *orig_path)
1247 struct registry_key *hive;
1248 char *path, *p;
1249 WERROR err;
1251 if (!(path = SMB_STRDUP(orig_path))) {
1252 return WERR_NOMEM;
1255 p = strchr(path, '\\');
1257 if ((p == NULL) || (p[1] == '\0')) {
1258 SAFE_FREE(path);
1259 return WERR_INVALID_PARAM;
1262 *p = '\0';
1264 err = reg_openhive(NULL, path,
1265 (strchr(p+1, '\\') != NULL) ?
1266 KEY_ENUMERATE_SUB_KEYS : KEY_CREATE_SUB_KEY,
1267 token, &hive);
1268 if (!W_ERROR_IS_OK(err)) {
1269 SAFE_FREE(path);
1270 return err;
1273 err = reg_deletekey(hive, p+1);
1274 SAFE_FREE(path);
1275 TALLOC_FREE(hive);
1276 return err;
1278 #endif /* #if 0 */