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
27 * 0x00 winreg_OpenHKCR
28 * 0x01 winreg_OpenHKCU
29 * 0x02 winreg_OpenHKLM
30 * 0x03 winreg_OpenHKPD
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
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
67 #include "reg_cachehook.h"
70 #include "reg_backend_db.h"
73 #define DBGC_CLASS DBGC_REGISTRY
76 /**********************************************************************
78 **********************************************************************/
80 static WERROR
fill_value_cache(struct registry_key
*key
)
82 if (key
->values
!= NULL
) {
83 if (!reg_values_need_update(key
->key
, key
->values
)) {
88 if (!(key
->values
= TALLOC_ZERO_P(key
, struct regval_ctr
))) {
91 if (fetch_reg_values(key
->key
, key
->values
) == -1) {
92 TALLOC_FREE(key
->values
);
99 static WERROR
fill_subkey_cache(struct registry_key
*key
)
103 if (key
->subkeys
!= NULL
) {
104 if (!reg_subkeys_need_update(key
->key
, key
->subkeys
)) {
109 werr
= regsubkey_ctr_init(key
, &(key
->subkeys
));
110 W_ERROR_NOT_OK_RETURN(werr
);
112 if (fetch_reg_keys(key
->key
, key
->subkeys
) == -1) {
113 TALLOC_FREE(key
->subkeys
);
114 return WERR_NO_MORE_ITEMS
;
120 static int regkey_destructor(struct registry_key_handle
*key
)
122 return regdb_close();
125 static WERROR
regkey_open_onelevel(TALLOC_CTX
*mem_ctx
,
126 struct registry_key
*parent
,
128 const struct nt_user_token
*token
,
129 uint32 access_desired
,
130 struct registry_key
**pregkey
)
132 WERROR result
= WERR_OK
;
133 struct registry_key
*regkey
;
134 struct registry_key_handle
*key
;
135 struct regsubkey_ctr
*subkeys
= NULL
;
137 DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name
));
139 SMB_ASSERT(strchr(name
, '\\') == NULL
);
141 if (!(regkey
= TALLOC_ZERO_P(mem_ctx
, struct registry_key
)) ||
142 !(regkey
->token
= dup_nt_token(regkey
, token
)) ||
143 !(regkey
->key
= TALLOC_ZERO_P(regkey
, struct registry_key_handle
)))
149 if ( !(W_ERROR_IS_OK(result
= regdb_open())) ) {
154 talloc_set_destructor(key
, regkey_destructor
);
158 key
->type
= REG_KEY_GENERIC
;
160 if (name
[0] == '\0') {
162 * Open a copy of the parent key
165 result
= WERR_BADFILE
;
168 key
->name
= talloc_strdup(key
, parent
->key
->name
);
174 key
->name
= talloc_asprintf(key
, "%s%s%s",
175 parent
? parent
->key
->name
: "",
180 if (key
->name
== NULL
) {
185 /* Tag this as a Performance Counter Key */
187 if( StrnCaseCmp(key
->name
, KEY_HKPD
, strlen(KEY_HKPD
)) == 0 )
188 key
->type
= REG_KEY_HKPD
;
190 /* Look up the table of registry I/O operations */
192 if ( !(key
->ops
= reghook_cache_find( key
->name
)) ) {
193 DEBUG(0,("reg_open_onelevel: Failed to assign "
194 "registry_ops to [%s]\n", key
->name
));
195 result
= WERR_BADFILE
;
199 /* check if the path really exists; failed is indicated by -1 */
200 /* if the subkey count failed, bail out */
202 result
= regsubkey_ctr_init(key
, &subkeys
);
203 if (!W_ERROR_IS_OK(result
)) {
207 if ( fetch_reg_keys( key
, subkeys
) == -1 ) {
208 result
= WERR_BADFILE
;
212 TALLOC_FREE( subkeys
);
214 if ( !regkey_access_check( key
, access_desired
, &key
->access_granted
,
216 result
= WERR_ACCESS_DENIED
;
224 if ( !W_ERROR_IS_OK(result
) ) {
231 WERROR
reg_openhive(TALLOC_CTX
*mem_ctx
, const char *hive
,
232 uint32 desired_access
,
233 const struct nt_user_token
*token
,
234 struct registry_key
**pkey
)
236 SMB_ASSERT(hive
!= NULL
);
237 SMB_ASSERT(hive
[0] != '\0');
238 SMB_ASSERT(strchr(hive
, '\\') == NULL
);
240 return regkey_open_onelevel(mem_ctx
, NULL
, hive
, token
, desired_access
,
245 /**********************************************************************
247 **********************************************************************/
249 WERROR
reg_openkey(TALLOC_CTX
*mem_ctx
, struct registry_key
*parent
,
250 const char *name
, uint32 desired_access
,
251 struct registry_key
**pkey
)
253 struct registry_key
*direct_parent
= parent
;
255 char *p
, *path
, *to_free
;
258 if (!(path
= SMB_STRDUP(name
))) {
265 if ((len
> 0) && (path
[len
-1] == '\\')) {
269 while ((p
= strchr(path
, '\\')) != NULL
) {
270 char *name_component
;
271 struct registry_key
*tmp
;
273 if (!(name_component
= SMB_STRNDUP(path
, (p
- path
)))) {
278 err
= regkey_open_onelevel(mem_ctx
, direct_parent
,
279 name_component
, parent
->token
,
280 KEY_ENUMERATE_SUB_KEYS
, &tmp
);
281 SAFE_FREE(name_component
);
283 if (!W_ERROR_IS_OK(err
)) {
286 if (direct_parent
!= parent
) {
287 TALLOC_FREE(direct_parent
);
294 err
= regkey_open_onelevel(mem_ctx
, direct_parent
, path
, parent
->token
,
295 desired_access
, pkey
);
297 if (direct_parent
!= parent
) {
298 TALLOC_FREE(direct_parent
);
304 WERROR
reg_enumkey(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
305 uint32 idx
, char **name
, NTTIME
*last_write_time
)
309 if (!(key
->key
->access_granted
& KEY_ENUMERATE_SUB_KEYS
)) {
310 return WERR_ACCESS_DENIED
;
313 if (!W_ERROR_IS_OK(err
= fill_subkey_cache(key
))) {
317 if (idx
>= regsubkey_ctr_numkeys(key
->subkeys
)) {
318 return WERR_NO_MORE_ITEMS
;
321 if (!(*name
= talloc_strdup(mem_ctx
,
322 regsubkey_ctr_specific_key(key
->subkeys
, idx
))))
327 if (last_write_time
) {
328 *last_write_time
= 0;
334 WERROR
reg_enumvalue(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
335 uint32 idx
, char **pname
, struct registry_value
**pval
)
337 struct registry_value
*val
;
340 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
341 return WERR_ACCESS_DENIED
;
344 if (!(W_ERROR_IS_OK(err
= fill_value_cache(key
)))) {
348 if (idx
>= key
->values
->num_values
) {
349 return WERR_NO_MORE_ITEMS
;
352 err
= registry_pull_value(mem_ctx
, &val
,
353 key
->values
->values
[idx
]->type
,
354 key
->values
->values
[idx
]->data_p
,
355 key
->values
->values
[idx
]->size
,
356 key
->values
->values
[idx
]->size
);
357 if (!W_ERROR_IS_OK(err
)) {
362 && !(*pname
= talloc_strdup(
363 mem_ctx
, key
->values
->values
[idx
]->valuename
))) {
372 WERROR
reg_queryvalue(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
373 const char *name
, struct registry_value
**pval
)
378 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
379 return WERR_ACCESS_DENIED
;
382 if (!(W_ERROR_IS_OK(err
= fill_value_cache(key
)))) {
386 for (i
=0; i
<key
->values
->num_values
; i
++) {
387 if (strequal(key
->values
->values
[i
]->valuename
, name
)) {
388 return reg_enumvalue(mem_ctx
, key
, i
, NULL
, pval
);
395 WERROR
reg_queryinfokey(struct registry_key
*key
, uint32_t *num_subkeys
,
396 uint32_t *max_subkeylen
, uint32_t *max_subkeysize
,
397 uint32_t *num_values
, uint32_t *max_valnamelen
,
398 uint32_t *max_valbufsize
, uint32_t *secdescsize
,
399 NTTIME
*last_changed_time
)
405 struct security_descriptor
*secdesc
;
407 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
408 return WERR_ACCESS_DENIED
;
411 if (!W_ERROR_IS_OK(fill_subkey_cache(key
)) ||
412 !W_ERROR_IS_OK(fill_value_cache(key
))) {
417 for (i
=0; i
< regsubkey_ctr_numkeys(key
->subkeys
); i
++) {
418 max_len
= MAX(max_len
,
419 strlen(regsubkey_ctr_specific_key(key
->subkeys
, i
)));
422 *num_subkeys
= regsubkey_ctr_numkeys(key
->subkeys
);
423 *max_subkeylen
= max_len
;
424 *max_subkeysize
= 0; /* Class length? */
428 for (i
=0; i
<key
->values
->num_values
; i
++) {
429 max_len
= MAX(max_len
,
430 strlen(key
->values
->values
[i
]->valuename
));
431 max_size
= MAX(max_size
, key
->values
->values
[i
]->size
);
434 *num_values
= key
->values
->num_values
;
435 *max_valnamelen
= max_len
;
436 *max_valbufsize
= max_size
;
438 if (!(mem_ctx
= talloc_new(key
))) {
442 err
= regkey_get_secdesc(mem_ctx
, key
->key
, &secdesc
);
443 if (!W_ERROR_IS_OK(err
)) {
444 TALLOC_FREE(mem_ctx
);
448 *secdescsize
= ndr_size_security_descriptor(secdesc
, 0);
449 TALLOC_FREE(mem_ctx
);
451 *last_changed_time
= 0;
456 WERROR
reg_createkey(TALLOC_CTX
*ctx
, struct registry_key
*parent
,
457 const char *subkeypath
, uint32 desired_access
,
458 struct registry_key
**pkey
,
459 enum winreg_CreateAction
*paction
)
461 struct registry_key
*key
= parent
;
462 struct registry_key
*create_parent
;
468 * We must refuse to handle subkey-paths containing
469 * a '/' character because at a lower level, after
470 * normalization, '/' is treated as a key separator
473 if (strchr(subkeypath
, '/') != NULL
) {
474 return WERR_INVALID_PARAM
;
477 if (!(mem_ctx
= talloc_new(ctx
))) return WERR_NOMEM
;
479 if (!(path
= talloc_strdup(mem_ctx
, subkeypath
))) {
484 while ((end
= strchr(path
, '\\')) != NULL
) {
485 struct registry_key
*tmp
;
486 enum winreg_CreateAction action
;
490 err
= reg_createkey(mem_ctx
, key
, path
,
491 KEY_ENUMERATE_SUB_KEYS
, &tmp
, &action
);
492 if (!W_ERROR_IS_OK(err
)) {
505 * At this point, "path" contains the one-element subkey of "key". We
506 * can try to open it.
509 err
= reg_openkey(ctx
, key
, path
, desired_access
, pkey
);
510 if (W_ERROR_IS_OK(err
)) {
511 if (paction
!= NULL
) {
512 *paction
= REG_OPENED_EXISTING_KEY
;
517 if (!W_ERROR_EQUAL(err
, WERR_BADFILE
)) {
519 * Something but "notfound" has happened, so bail out
525 * We have to make a copy of the current key, as we opened it only
526 * with ENUM_SUBKEY access.
529 err
= reg_openkey(mem_ctx
, key
, "", KEY_CREATE_SUB_KEY
,
531 if (!W_ERROR_IS_OK(err
)) {
536 * Actually create the subkey
539 err
= fill_subkey_cache(create_parent
);
540 if (!W_ERROR_IS_OK(err
)) goto done
;
542 err
= create_reg_subkey(key
->key
, path
);
543 W_ERROR_NOT_OK_GOTO_DONE(err
);
546 * Now open the newly created key
549 err
= reg_openkey(ctx
, create_parent
, path
, desired_access
, pkey
);
550 if (W_ERROR_IS_OK(err
) && (paction
!= NULL
)) {
551 *paction
= REG_CREATED_NEW_KEY
;
555 TALLOC_FREE(mem_ctx
);
559 WERROR
reg_deletekey(struct registry_key
*parent
, const char *path
)
563 struct registry_key
*tmp_key
, *key
;
564 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
566 name
= talloc_strdup(mem_ctx
, path
);
572 /* check if the key has subkeys */
573 err
= reg_openkey(mem_ctx
, parent
, name
, REG_KEY_READ
, &key
);
574 W_ERROR_NOT_OK_GOTO_DONE(err
);
576 err
= fill_subkey_cache(key
);
577 W_ERROR_NOT_OK_GOTO_DONE(err
);
579 if (regsubkey_ctr_numkeys(key
->subkeys
) > 0) {
580 err
= WERR_ACCESS_DENIED
;
584 /* no subkeys - proceed with delete */
585 end
= strrchr(name
, '\\');
589 err
= reg_openkey(mem_ctx
, parent
, name
,
590 KEY_CREATE_SUB_KEY
, &tmp_key
);
591 W_ERROR_NOT_OK_GOTO_DONE(err
);
597 if (name
[0] == '\0') {
598 err
= WERR_INVALID_PARAM
;
602 err
= delete_reg_subkey(parent
->key
, name
);
605 TALLOC_FREE(mem_ctx
);
609 WERROR
reg_setvalue(struct registry_key
*key
, const char *name
,
610 const struct registry_value
*val
)
613 DATA_BLOB value_data
;
616 if (!(key
->key
->access_granted
& KEY_SET_VALUE
)) {
617 return WERR_ACCESS_DENIED
;
620 if (!W_ERROR_IS_OK(err
= fill_value_cache(key
))) {
624 err
= registry_push_value(key
, val
, &value_data
);
625 if (!W_ERROR_IS_OK(err
)) {
629 res
= regval_ctr_addvalue(key
->values
, name
, val
->type
,
630 value_data
.data
, value_data
.length
);
631 TALLOC_FREE(value_data
.data
);
634 TALLOC_FREE(key
->values
);
638 if (!store_reg_values(key
->key
, key
->values
)) {
639 TALLOC_FREE(key
->values
);
640 return WERR_REG_IO_FAILURE
;
646 static WERROR
reg_value_exists(struct registry_key
*key
, const char *name
)
650 for (i
=0; i
<key
->values
->num_values
; i
++) {
651 if (strequal(key
->values
->values
[i
]->valuename
, name
)) {
659 WERROR
reg_deletevalue(struct registry_key
*key
, const char *name
)
663 if (!(key
->key
->access_granted
& KEY_SET_VALUE
)) {
664 return WERR_ACCESS_DENIED
;
667 if (!W_ERROR_IS_OK(err
= fill_value_cache(key
))) {
671 err
= reg_value_exists(key
, name
);
672 if (!W_ERROR_IS_OK(err
)) {
676 regval_ctr_delvalue(key
->values
, name
);
678 if (!store_reg_values(key
->key
, key
->values
)) {
679 TALLOC_FREE(key
->values
);
680 return WERR_REG_IO_FAILURE
;
686 WERROR
reg_getkeysecurity(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
687 struct security_descriptor
**psecdesc
)
689 return regkey_get_secdesc(mem_ctx
, key
->key
, psecdesc
);
692 WERROR
reg_setkeysecurity(struct registry_key
*key
,
693 struct security_descriptor
*psecdesc
)
695 return regkey_set_secdesc(key
->key
, psecdesc
);
698 WERROR
reg_getversion(uint32_t *version
)
700 if (version
== NULL
) {
701 return WERR_INVALID_PARAM
;
704 *version
= 0x00000005; /* Windows 2000 registry API version */
708 /*******************************************************************
709 Note: topkeypat is the *full* path that this *key will be
710 loaded into (including the name of the key)
711 ********************************************************************/
713 static WERROR
reg_load_tree(REGF_FILE
*regfile
, const char *topkeypath
,
717 struct registry_key_handle registry_key
;
718 struct regval_ctr
*values
;
719 struct regsubkey_ctr
*subkeys
;
722 WERROR result
= WERR_OK
;
724 /* initialize the struct registry_key_handle structure */
726 registry_key
.ops
= reghook_cache_find(topkeypath
);
727 if (!registry_key
.ops
) {
728 DEBUG(0, ("reg_load_tree: Failed to assign registry_ops "
729 "to [%s]\n", topkeypath
));
733 registry_key
.name
= talloc_strdup(regfile
->mem_ctx
, topkeypath
);
734 if (!registry_key
.name
) {
735 DEBUG(0, ("reg_load_tree: Talloc failed for reg_key.name!\n"));
739 /* now start parsing the values and subkeys */
741 result
= regsubkey_ctr_init(regfile
->mem_ctx
, &subkeys
);
742 W_ERROR_NOT_OK_RETURN(result
);
744 values
= TALLOC_ZERO_P(subkeys
, struct regval_ctr
);
745 if (values
== NULL
) {
749 /* copy values into the struct regval_ctr */
751 for (i
=0; i
<key
->num_values
; i
++) {
752 regval_ctr_addvalue(values
, key
->values
[i
].valuename
,
755 (key
->values
[i
].data_size
& ~VK_DATA_IN_OFFSET
));
758 /* copy subkeys into the struct regsubkey_ctr */
760 key
->subkey_index
= 0;
761 while ((subkey
= regfio_fetch_subkey( regfile
, key
))) {
762 result
= regsubkey_ctr_addkey(subkeys
, subkey
->keyname
);
763 if (!W_ERROR_IS_OK(result
)) {
764 TALLOC_FREE(subkeys
);
769 /* write this key and values out */
771 if (!store_reg_values(®istry_key
, values
)
772 || !store_reg_keys(®istry_key
, subkeys
))
774 DEBUG(0,("reg_load_tree: Failed to load %s!\n", topkeypath
));
775 result
= WERR_REG_IO_FAILURE
;
778 TALLOC_FREE(subkeys
);
780 if (!W_ERROR_IS_OK(result
)) {
784 /* now continue to load each subkey registry tree */
786 key
->subkey_index
= 0;
787 while ((subkey
= regfio_fetch_subkey(regfile
, key
))) {
788 path
= talloc_asprintf(regfile
->mem_ctx
,
795 result
= reg_load_tree(regfile
, path
, subkey
);
796 if (!W_ERROR_IS_OK(result
)) {
804 /*******************************************************************
805 ********************************************************************/
807 static WERROR
restore_registry_key(struct registry_key_handle
*krecord
,
811 REGF_NK_REC
*rootkey
;
814 /* open the registry file....fail if the file already exists */
816 regfile
= regfio_open(fname
, (O_RDONLY
), 0);
817 if (regfile
== NULL
) {
818 DEBUG(0, ("restore_registry_key: failed to open \"%s\" (%s)\n",
819 fname
, strerror(errno
)));
820 return ntstatus_to_werror(map_nt_error_from_unix(errno
));
823 /* get the rootkey from the regf file and then load the tree
824 via recursive calls */
826 if (!(rootkey
= regfio_rootkey(regfile
))) {
827 regfio_close(regfile
);
828 return WERR_REG_FILE_INVALID
;
831 result
= reg_load_tree(regfile
, krecord
->name
, rootkey
);
835 regfio_close(regfile
);
840 WERROR
reg_restorekey(struct registry_key
*key
, const char *fname
)
842 return restore_registry_key(key
->key
, fname
);
845 /********************************************************************
846 ********************************************************************/
848 static WERROR
reg_write_tree(REGF_FILE
*regfile
, const char *keypath
,
852 struct regval_ctr
*values
;
853 struct regsubkey_ctr
*subkeys
;
855 char *key_tmp
= NULL
;
856 char *keyname
, *parentpath
;
857 char *subkeypath
= NULL
;
859 struct registry_key_handle registry_key
;
860 WERROR result
= WERR_OK
;
861 struct security_descriptor
*sec_desc
= NULL
;
864 return WERR_GENERAL_FAILURE
;
868 return WERR_OBJECT_PATH_INVALID
;
871 /* split up the registry key path */
873 key_tmp
= talloc_strdup(regfile
->mem_ctx
, keypath
);
877 if (!reg_split_key(key_tmp
, &parentpath
, &keyname
)) {
878 return WERR_OBJECT_PATH_INVALID
;
882 keyname
= parentpath
;
885 /* we need a registry_key_handle object here to enumerate subkeys and values */
887 ZERO_STRUCT(registry_key
);
889 registry_key
.name
= talloc_strdup(regfile
->mem_ctx
, keypath
);
890 if (registry_key
.name
== NULL
) {
894 registry_key
.ops
= reghook_cache_find(registry_key
.name
);
895 if (registry_key
.ops
== NULL
) {
899 /* lookup the values and subkeys */
901 result
= regsubkey_ctr_init(regfile
->mem_ctx
, &subkeys
);
902 W_ERROR_NOT_OK_RETURN(result
);
904 values
= TALLOC_ZERO_P(subkeys
, struct regval_ctr
);
905 if (values
== NULL
) {
909 fetch_reg_keys(®istry_key
, subkeys
);
910 fetch_reg_values(®istry_key
, values
);
912 result
= regkey_get_secdesc(regfile
->mem_ctx
, ®istry_key
, &sec_desc
);
913 if (!W_ERROR_IS_OK(result
)) {
917 /* write out this key */
919 key
= regfio_write_key(regfile
, keyname
, values
, subkeys
, sec_desc
,
922 result
= WERR_CAN_NOT_COMPLETE
;
926 /* write each one of the subkeys out */
928 num_subkeys
= regsubkey_ctr_numkeys(subkeys
);
929 for (i
=0; i
<num_subkeys
; i
++) {
930 subkeyname
= regsubkey_ctr_specific_key(subkeys
, i
);
931 subkeypath
= talloc_asprintf(regfile
->mem_ctx
, "%s\\%s",
932 keypath
, subkeyname
);
933 if (subkeypath
== NULL
) {
937 result
= reg_write_tree(regfile
, subkeypath
, key
);
938 if (!W_ERROR_IS_OK(result
))
942 DEBUG(6, ("reg_write_tree: wrote key [%s]\n", keypath
));
945 TALLOC_FREE(subkeys
);
946 TALLOC_FREE(registry_key
.name
);
951 static WERROR
backup_registry_key(struct registry_key_handle
*krecord
,
957 /* open the registry file....fail if the file already exists */
959 regfile
= regfio_open(fname
, (O_RDWR
|O_CREAT
|O_EXCL
),
961 if (regfile
== NULL
) {
962 DEBUG(0,("backup_registry_key: failed to open \"%s\" (%s)\n",
963 fname
, strerror(errno
) ));
964 return ntstatus_to_werror(map_nt_error_from_unix(errno
));
967 /* write the registry tree to the file */
969 result
= reg_write_tree(regfile
, krecord
->name
, NULL
);
973 regfio_close(regfile
);
978 WERROR
reg_savekey(struct registry_key
*key
, const char *fname
)
980 return backup_registry_key(key
->key
, fname
);
983 /**********************************************************************
984 * Higher level utility functions
985 **********************************************************************/
987 WERROR
reg_deleteallvalues(struct registry_key
*key
)
992 if (!(key
->key
->access_granted
& KEY_SET_VALUE
)) {
993 return WERR_ACCESS_DENIED
;
996 if (!W_ERROR_IS_OK(err
= fill_value_cache(key
))) {
1000 for (i
=0; i
<key
->values
->num_values
; i
++) {
1001 regval_ctr_delvalue(key
->values
, key
->values
->values
[i
]->valuename
);
1004 if (!store_reg_values(key
->key
, key
->values
)) {
1005 TALLOC_FREE(key
->values
);
1006 return WERR_REG_IO_FAILURE
;
1013 * Utility function to open a complete registry path including the hive prefix.
1016 WERROR
reg_open_path(TALLOC_CTX
*mem_ctx
, const char *orig_path
,
1017 uint32 desired_access
, const struct nt_user_token
*token
,
1018 struct registry_key
**pkey
)
1020 struct registry_key
*hive
, *key
;
1024 if (!(path
= SMB_STRDUP(orig_path
))) {
1028 p
= strchr(path
, '\\');
1030 if ((p
== NULL
) || (p
[1] == '\0')) {
1032 * No key behind the hive, just return the hive
1035 err
= reg_openhive(mem_ctx
, path
, desired_access
, token
,
1037 if (!W_ERROR_IS_OK(err
)) {
1048 err
= reg_openhive(mem_ctx
, path
, KEY_ENUMERATE_SUB_KEYS
, token
,
1050 if (!W_ERROR_IS_OK(err
)) {
1055 err
= reg_openkey(mem_ctx
, hive
, p
+1, desired_access
, &key
);
1060 if (!W_ERROR_IS_OK(err
)) {
1069 * Utility function to delete a registry key with all its subkeys.
1070 * Note that reg_deletekey returns ACCESS_DENIED when called on a
1071 * key that has subkeys.
1073 static WERROR
reg_deletekey_recursive_internal(TALLOC_CTX
*ctx
,
1074 struct registry_key
*parent
,
1078 TALLOC_CTX
*mem_ctx
= NULL
;
1079 WERROR werr
= WERR_OK
;
1080 struct registry_key
*key
;
1081 char *subkey_name
= NULL
;
1084 mem_ctx
= talloc_new(ctx
);
1085 if (mem_ctx
== NULL
) {
1090 /* recurse through subkeys first */
1091 werr
= reg_openkey(mem_ctx
, parent
, path
, REG_KEY_ALL
, &key
);
1092 if (!W_ERROR_IS_OK(werr
)) {
1096 werr
= fill_subkey_cache(key
);
1097 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1100 * loop from top to bottom for perfomance:
1101 * this way, we need to rehash the regsubkey containers less
1103 for (i
= regsubkey_ctr_numkeys(key
->subkeys
) ; i
> 0; i
--) {
1104 subkey_name
= regsubkey_ctr_specific_key(key
->subkeys
, i
-1);
1105 werr
= reg_deletekey_recursive_internal(mem_ctx
, key
,
1108 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1112 /* now delete the actual key */
1113 werr
= reg_deletekey(parent
, path
);
1117 TALLOC_FREE(mem_ctx
);
1121 static WERROR
reg_deletekey_recursive_trans(TALLOC_CTX
*ctx
,
1122 struct registry_key
*parent
,
1128 werr
= regdb_transaction_start();
1129 if (!W_ERROR_IS_OK(werr
)) {
1130 DEBUG(0, ("reg_deletekey_recursive_trans: "
1131 "error starting transaction: %s\n",
1136 werr
= reg_deletekey_recursive_internal(ctx
, parent
, path
, del_key
);
1138 if (!W_ERROR_IS_OK(werr
)) {
1139 DEBUG(1, (__location__
" failed to delete key '%s' from key "
1140 "'%s': %s\n", path
, parent
->key
->name
,
1142 werr
= regdb_transaction_cancel();
1143 if (!W_ERROR_IS_OK(werr
)) {
1144 DEBUG(0, ("reg_deletekey_recursive_trans: "
1145 "error cancelling transaction: %s\n",
1149 werr
= regdb_transaction_commit();
1150 if (!W_ERROR_IS_OK(werr
)) {
1151 DEBUG(0, ("reg_deletekey_recursive_trans: "
1152 "error committing transaction: %s\n",
1160 WERROR
reg_deletekey_recursive(TALLOC_CTX
*ctx
,
1161 struct registry_key
*parent
,
1164 return reg_deletekey_recursive_trans(ctx
, parent
, path
, true);
1167 WERROR
reg_deletesubkeys_recursive(TALLOC_CTX
*ctx
,
1168 struct registry_key
*parent
,
1171 return reg_deletekey_recursive_trans(ctx
, parent
, path
, false);
1175 /* these two functions are unused. */
1178 * Utility function to create a registry key without opening the hive
1179 * before. Assumes the hive already exists.
1182 WERROR
reg_create_path(TALLOC_CTX
*mem_ctx
, const char *orig_path
,
1183 uint32 desired_access
,
1184 const struct nt_user_token
*token
,
1185 enum winreg_CreateAction
*paction
,
1186 struct registry_key
**pkey
)
1188 struct registry_key
*hive
;
1192 if (!(path
= SMB_STRDUP(orig_path
))) {
1196 p
= strchr(path
, '\\');
1198 if ((p
== NULL
) || (p
[1] == '\0')) {
1200 * No key behind the hive, just return the hive
1203 err
= reg_openhive(mem_ctx
, path
, desired_access
, token
,
1205 if (!W_ERROR_IS_OK(err
)) {
1211 *paction
= REG_OPENED_EXISTING_KEY
;
1217 err
= reg_openhive(mem_ctx
, path
,
1218 (strchr(p
+1, '\\') != NULL
) ?
1219 KEY_ENUMERATE_SUB_KEYS
: KEY_CREATE_SUB_KEY
,
1221 if (!W_ERROR_IS_OK(err
)) {
1226 err
= reg_createkey(mem_ctx
, hive
, p
+1, desired_access
, pkey
, paction
);
1233 * Utility function to create a registry key without opening the hive
1234 * before. Will not delete a hive.
1237 WERROR
reg_delete_path(const struct nt_user_token
*token
,
1238 const char *orig_path
)
1240 struct registry_key
*hive
;
1244 if (!(path
= SMB_STRDUP(orig_path
))) {
1248 p
= strchr(path
, '\\');
1250 if ((p
== NULL
) || (p
[1] == '\0')) {
1252 return WERR_INVALID_PARAM
;
1257 err
= reg_openhive(NULL
, path
,
1258 (strchr(p
+1, '\\') != NULL
) ?
1259 KEY_ENUMERATE_SUB_KEYS
: KEY_CREATE_SUB_KEY
,
1261 if (!W_ERROR_IS_OK(err
)) {
1266 err
= reg_deletekey(hive
, p
+1);