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
69 #define DBGC_CLASS DBGC_REGISTRY
72 /**********************************************************************
74 **********************************************************************/
76 static WERROR
fill_value_cache(struct registry_key
*key
)
78 if (key
->values
!= NULL
) {
79 if (!reg_values_need_update(key
->key
, key
->values
)) {
84 if (!(key
->values
= TALLOC_ZERO_P(key
, struct regval_ctr
))) {
87 if (fetch_reg_values(key
->key
, key
->values
) == -1) {
88 TALLOC_FREE(key
->values
);
95 static WERROR
fill_subkey_cache(struct registry_key
*key
)
99 if (key
->subkeys
!= NULL
) {
100 if (!reg_subkeys_need_update(key
->key
, key
->subkeys
)) {
105 werr
= regsubkey_ctr_init(key
, &(key
->subkeys
));
106 W_ERROR_NOT_OK_RETURN(werr
);
108 if (fetch_reg_keys(key
->key
, key
->subkeys
) == -1) {
109 TALLOC_FREE(key
->subkeys
);
110 return WERR_NO_MORE_ITEMS
;
116 static int regkey_destructor(struct registry_key_handle
*key
)
118 return regdb_close();
121 static WERROR
regkey_open_onelevel(TALLOC_CTX
*mem_ctx
,
122 struct registry_key
*parent
,
124 const struct nt_user_token
*token
,
125 uint32 access_desired
,
126 struct registry_key
**pregkey
)
128 WERROR result
= WERR_OK
;
129 struct registry_key
*regkey
;
130 struct registry_key_handle
*key
;
131 struct regsubkey_ctr
*subkeys
= NULL
;
133 DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name
));
135 SMB_ASSERT(strchr(name
, '\\') == NULL
);
137 if (!(regkey
= TALLOC_ZERO_P(mem_ctx
, struct registry_key
)) ||
138 !(regkey
->token
= dup_nt_token(regkey
, token
)) ||
139 !(regkey
->key
= TALLOC_ZERO_P(regkey
, struct registry_key_handle
)))
145 if ( !(W_ERROR_IS_OK(result
= regdb_open())) ) {
150 talloc_set_destructor(key
, regkey_destructor
);
154 key
->type
= REG_KEY_GENERIC
;
156 if (name
[0] == '\0') {
158 * Open a copy of the parent key
161 result
= WERR_BADFILE
;
164 key
->name
= talloc_strdup(key
, parent
->key
->name
);
170 key
->name
= talloc_asprintf(key
, "%s%s%s",
171 parent
? parent
->key
->name
: "",
176 if (key
->name
== NULL
) {
181 /* Tag this as a Performance Counter Key */
183 if( StrnCaseCmp(key
->name
, KEY_HKPD
, strlen(KEY_HKPD
)) == 0 )
184 key
->type
= REG_KEY_HKPD
;
186 /* Look up the table of registry I/O operations */
188 if ( !(key
->ops
= reghook_cache_find( key
->name
)) ) {
189 DEBUG(0,("reg_open_onelevel: Failed to assign "
190 "registry_ops to [%s]\n", key
->name
));
191 result
= WERR_BADFILE
;
195 /* check if the path really exists; failed is indicated by -1 */
196 /* if the subkey count failed, bail out */
198 result
= regsubkey_ctr_init(key
, &subkeys
);
199 if (!W_ERROR_IS_OK(result
)) {
203 if ( fetch_reg_keys( key
, subkeys
) == -1 ) {
204 result
= WERR_BADFILE
;
208 TALLOC_FREE( subkeys
);
210 if ( !regkey_access_check( key
, access_desired
, &key
->access_granted
,
212 result
= WERR_ACCESS_DENIED
;
220 if ( !W_ERROR_IS_OK(result
) ) {
227 WERROR
reg_openhive(TALLOC_CTX
*mem_ctx
, const char *hive
,
228 uint32 desired_access
,
229 const struct nt_user_token
*token
,
230 struct registry_key
**pkey
)
232 SMB_ASSERT(hive
!= NULL
);
233 SMB_ASSERT(hive
[0] != '\0');
234 SMB_ASSERT(strchr(hive
, '\\') == NULL
);
236 return regkey_open_onelevel(mem_ctx
, NULL
, hive
, token
, desired_access
,
241 /**********************************************************************
243 **********************************************************************/
245 WERROR
reg_openkey(TALLOC_CTX
*mem_ctx
, struct registry_key
*parent
,
246 const char *name
, uint32 desired_access
,
247 struct registry_key
**pkey
)
249 struct registry_key
*direct_parent
= parent
;
251 char *p
, *path
, *to_free
;
254 if (!(path
= SMB_STRDUP(name
))) {
261 if ((len
> 0) && (path
[len
-1] == '\\')) {
265 while ((p
= strchr(path
, '\\')) != NULL
) {
266 char *name_component
;
267 struct registry_key
*tmp
;
269 if (!(name_component
= SMB_STRNDUP(path
, (p
- path
)))) {
274 err
= regkey_open_onelevel(mem_ctx
, direct_parent
,
275 name_component
, parent
->token
,
276 KEY_ENUMERATE_SUB_KEYS
, &tmp
);
277 SAFE_FREE(name_component
);
279 if (!W_ERROR_IS_OK(err
)) {
282 if (direct_parent
!= parent
) {
283 TALLOC_FREE(direct_parent
);
290 err
= regkey_open_onelevel(mem_ctx
, direct_parent
, path
, parent
->token
,
291 desired_access
, pkey
);
293 if (direct_parent
!= parent
) {
294 TALLOC_FREE(direct_parent
);
300 WERROR
reg_enumkey(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
301 uint32 idx
, char **name
, NTTIME
*last_write_time
)
305 if (!(key
->key
->access_granted
& KEY_ENUMERATE_SUB_KEYS
)) {
306 return WERR_ACCESS_DENIED
;
309 if (!W_ERROR_IS_OK(err
= fill_subkey_cache(key
))) {
313 if (idx
>= regsubkey_ctr_numkeys(key
->subkeys
)) {
314 return WERR_NO_MORE_ITEMS
;
317 if (!(*name
= talloc_strdup(mem_ctx
,
318 regsubkey_ctr_specific_key(key
->subkeys
, idx
))))
323 if (last_write_time
) {
324 *last_write_time
= 0;
330 WERROR
reg_enumvalue(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
331 uint32 idx
, char **pname
, struct registry_value
**pval
)
333 struct registry_value
*val
;
336 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
337 return WERR_ACCESS_DENIED
;
340 if (!(W_ERROR_IS_OK(err
= fill_value_cache(key
)))) {
344 if (idx
>= key
->values
->num_values
) {
345 return WERR_NO_MORE_ITEMS
;
348 err
= registry_pull_value(mem_ctx
, &val
,
349 key
->values
->values
[idx
]->type
,
350 key
->values
->values
[idx
]->data_p
,
351 key
->values
->values
[idx
]->size
,
352 key
->values
->values
[idx
]->size
);
353 if (!W_ERROR_IS_OK(err
)) {
358 && !(*pname
= talloc_strdup(
359 mem_ctx
, key
->values
->values
[idx
]->valuename
))) {
368 WERROR
reg_queryvalue(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
369 const char *name
, struct registry_value
**pval
)
374 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
375 return WERR_ACCESS_DENIED
;
378 if (!(W_ERROR_IS_OK(err
= fill_value_cache(key
)))) {
382 for (i
=0; i
<key
->values
->num_values
; i
++) {
383 if (strequal(key
->values
->values
[i
]->valuename
, name
)) {
384 return reg_enumvalue(mem_ctx
, key
, i
, NULL
, pval
);
391 WERROR
reg_queryinfokey(struct registry_key
*key
, uint32_t *num_subkeys
,
392 uint32_t *max_subkeylen
, uint32_t *max_subkeysize
,
393 uint32_t *num_values
, uint32_t *max_valnamelen
,
394 uint32_t *max_valbufsize
, uint32_t *secdescsize
,
395 NTTIME
*last_changed_time
)
401 struct security_descriptor
*secdesc
;
403 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
404 return WERR_ACCESS_DENIED
;
407 if (!W_ERROR_IS_OK(fill_subkey_cache(key
)) ||
408 !W_ERROR_IS_OK(fill_value_cache(key
))) {
413 for (i
=0; i
< regsubkey_ctr_numkeys(key
->subkeys
); i
++) {
414 max_len
= MAX(max_len
,
415 strlen(regsubkey_ctr_specific_key(key
->subkeys
, i
)));
418 *num_subkeys
= regsubkey_ctr_numkeys(key
->subkeys
);
419 *max_subkeylen
= max_len
;
420 *max_subkeysize
= 0; /* Class length? */
424 for (i
=0; i
<key
->values
->num_values
; i
++) {
425 max_len
= MAX(max_len
,
426 strlen(key
->values
->values
[i
]->valuename
));
427 max_size
= MAX(max_size
, key
->values
->values
[i
]->size
);
430 *num_values
= key
->values
->num_values
;
431 *max_valnamelen
= max_len
;
432 *max_valbufsize
= max_size
;
434 if (!(mem_ctx
= talloc_new(key
))) {
438 err
= regkey_get_secdesc(mem_ctx
, key
->key
, &secdesc
);
439 if (!W_ERROR_IS_OK(err
)) {
440 TALLOC_FREE(mem_ctx
);
444 *secdescsize
= ndr_size_security_descriptor(secdesc
, NULL
, 0);
445 TALLOC_FREE(mem_ctx
);
447 *last_changed_time
= 0;
452 WERROR
reg_createkey(TALLOC_CTX
*ctx
, struct registry_key
*parent
,
453 const char *subkeypath
, uint32 desired_access
,
454 struct registry_key
**pkey
,
455 enum winreg_CreateAction
*paction
)
457 struct registry_key
*key
= parent
;
458 struct registry_key
*create_parent
;
464 * We must refuse to handle subkey-paths containing
465 * a '/' character because at a lower level, after
466 * normalization, '/' is treated as a key separator
469 if (strchr(subkeypath
, '/') != NULL
) {
470 return WERR_INVALID_PARAM
;
473 if (!(mem_ctx
= talloc_new(ctx
))) return WERR_NOMEM
;
475 if (!(path
= talloc_strdup(mem_ctx
, subkeypath
))) {
480 while ((end
= strchr(path
, '\\')) != NULL
) {
481 struct registry_key
*tmp
;
482 enum winreg_CreateAction action
;
486 err
= reg_createkey(mem_ctx
, key
, path
,
487 KEY_ENUMERATE_SUB_KEYS
, &tmp
, &action
);
488 if (!W_ERROR_IS_OK(err
)) {
501 * At this point, "path" contains the one-element subkey of "key". We
502 * can try to open it.
505 err
= reg_openkey(ctx
, key
, path
, desired_access
, pkey
);
506 if (W_ERROR_IS_OK(err
)) {
507 if (paction
!= NULL
) {
508 *paction
= REG_OPENED_EXISTING_KEY
;
513 if (!W_ERROR_EQUAL(err
, WERR_BADFILE
)) {
515 * Something but "notfound" has happened, so bail out
521 * We have to make a copy of the current key, as we opened it only
522 * with ENUM_SUBKEY access.
525 err
= reg_openkey(mem_ctx
, key
, "", KEY_CREATE_SUB_KEY
,
527 if (!W_ERROR_IS_OK(err
)) {
532 * Actually create the subkey
535 err
= fill_subkey_cache(create_parent
);
536 if (!W_ERROR_IS_OK(err
)) goto done
;
538 err
= create_reg_subkey(key
->key
, path
);
539 W_ERROR_NOT_OK_GOTO_DONE(err
);
542 * Now open the newly created key
545 err
= reg_openkey(ctx
, create_parent
, path
, desired_access
, pkey
);
546 if (W_ERROR_IS_OK(err
) && (paction
!= NULL
)) {
547 *paction
= REG_CREATED_NEW_KEY
;
551 TALLOC_FREE(mem_ctx
);
555 WERROR
reg_deletekey(struct registry_key
*parent
, const char *path
)
559 struct registry_key
*tmp_key
, *key
;
560 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
562 name
= talloc_strdup(mem_ctx
, path
);
568 /* check if the key has subkeys */
569 err
= reg_openkey(mem_ctx
, parent
, name
, REG_KEY_READ
, &key
);
570 W_ERROR_NOT_OK_GOTO_DONE(err
);
572 err
= fill_subkey_cache(key
);
573 W_ERROR_NOT_OK_GOTO_DONE(err
);
575 if (regsubkey_ctr_numkeys(key
->subkeys
) > 0) {
576 err
= WERR_ACCESS_DENIED
;
580 /* no subkeys - proceed with delete */
581 end
= strrchr(name
, '\\');
585 err
= reg_openkey(mem_ctx
, parent
, name
,
586 KEY_CREATE_SUB_KEY
, &tmp_key
);
587 W_ERROR_NOT_OK_GOTO_DONE(err
);
593 if (name
[0] == '\0') {
594 err
= WERR_INVALID_PARAM
;
598 err
= delete_reg_subkey(parent
->key
, name
);
601 TALLOC_FREE(mem_ctx
);
605 WERROR
reg_setvalue(struct registry_key
*key
, const char *name
,
606 const struct registry_value
*val
)
609 DATA_BLOB value_data
;
612 if (!(key
->key
->access_granted
& KEY_SET_VALUE
)) {
613 return WERR_ACCESS_DENIED
;
616 if (!W_ERROR_IS_OK(err
= fill_value_cache(key
))) {
620 err
= registry_push_value(key
, val
, &value_data
);
621 if (!W_ERROR_IS_OK(err
)) {
625 res
= regval_ctr_addvalue(key
->values
, name
, val
->type
,
626 (char *)value_data
.data
, value_data
.length
);
627 TALLOC_FREE(value_data
.data
);
630 TALLOC_FREE(key
->values
);
634 if (!store_reg_values(key
->key
, key
->values
)) {
635 TALLOC_FREE(key
->values
);
636 return WERR_REG_IO_FAILURE
;
642 static WERROR
reg_value_exists(struct registry_key
*key
, const char *name
)
646 for (i
=0; i
<key
->values
->num_values
; i
++) {
647 if (strequal(key
->values
->values
[i
]->valuename
, name
)) {
655 WERROR
reg_deletevalue(struct registry_key
*key
, const char *name
)
659 if (!(key
->key
->access_granted
& KEY_SET_VALUE
)) {
660 return WERR_ACCESS_DENIED
;
663 if (!W_ERROR_IS_OK(err
= fill_value_cache(key
))) {
667 err
= reg_value_exists(key
, name
);
668 if (!W_ERROR_IS_OK(err
)) {
672 regval_ctr_delvalue(key
->values
, name
);
674 if (!store_reg_values(key
->key
, key
->values
)) {
675 TALLOC_FREE(key
->values
);
676 return WERR_REG_IO_FAILURE
;
682 WERROR
reg_getkeysecurity(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
683 struct security_descriptor
**psecdesc
)
685 return regkey_get_secdesc(mem_ctx
, key
->key
, psecdesc
);
688 WERROR
reg_setkeysecurity(struct registry_key
*key
,
689 struct security_descriptor
*psecdesc
)
691 return regkey_set_secdesc(key
->key
, psecdesc
);
694 WERROR
reg_getversion(uint32_t *version
)
696 if (version
== NULL
) {
697 return WERR_INVALID_PARAM
;
700 *version
= 0x00000005; /* Windows 2000 registry API version */
704 /*******************************************************************
705 Note: topkeypat is the *full* path that this *key will be
706 loaded into (including the name of the key)
707 ********************************************************************/
709 static WERROR
reg_load_tree(REGF_FILE
*regfile
, const char *topkeypath
,
713 struct registry_key_handle registry_key
;
714 struct regval_ctr
*values
;
715 struct regsubkey_ctr
*subkeys
;
718 WERROR result
= WERR_OK
;
720 /* initialize the struct registry_key_handle structure */
722 registry_key
.ops
= reghook_cache_find(topkeypath
);
723 if (!registry_key
.ops
) {
724 DEBUG(0, ("reg_load_tree: Failed to assign registry_ops "
725 "to [%s]\n", topkeypath
));
729 registry_key
.name
= talloc_strdup(regfile
->mem_ctx
, topkeypath
);
730 if (!registry_key
.name
) {
731 DEBUG(0, ("reg_load_tree: Talloc failed for reg_key.name!\n"));
735 /* now start parsing the values and subkeys */
737 result
= regsubkey_ctr_init(regfile
->mem_ctx
, &subkeys
);
738 W_ERROR_NOT_OK_RETURN(result
);
740 values
= TALLOC_ZERO_P(subkeys
, struct regval_ctr
);
741 if (values
== NULL
) {
745 /* copy values into the struct regval_ctr */
747 for (i
=0; i
<key
->num_values
; i
++) {
748 regval_ctr_addvalue(values
, key
->values
[i
].valuename
,
750 (char*)key
->values
[i
].data
,
751 (key
->values
[i
].data_size
& ~VK_DATA_IN_OFFSET
));
754 /* copy subkeys into the struct regsubkey_ctr */
756 key
->subkey_index
= 0;
757 while ((subkey
= regfio_fetch_subkey( regfile
, key
))) {
758 result
= regsubkey_ctr_addkey(subkeys
, subkey
->keyname
);
759 if (!W_ERROR_IS_OK(result
)) {
760 TALLOC_FREE(subkeys
);
765 /* write this key and values out */
767 if (!store_reg_values(®istry_key
, values
)
768 || !store_reg_keys(®istry_key
, subkeys
))
770 DEBUG(0,("reg_load_tree: Failed to load %s!\n", topkeypath
));
771 result
= WERR_REG_IO_FAILURE
;
774 TALLOC_FREE(subkeys
);
776 if (!W_ERROR_IS_OK(result
)) {
780 /* now continue to load each subkey registry tree */
782 key
->subkey_index
= 0;
783 while ((subkey
= regfio_fetch_subkey(regfile
, key
))) {
784 path
= talloc_asprintf(regfile
->mem_ctx
,
791 result
= reg_load_tree(regfile
, path
, subkey
);
792 if (!W_ERROR_IS_OK(result
)) {
800 /*******************************************************************
801 ********************************************************************/
803 static WERROR
restore_registry_key(struct registry_key_handle
*krecord
,
807 REGF_NK_REC
*rootkey
;
810 /* open the registry file....fail if the file already exists */
812 regfile
= regfio_open(fname
, (O_RDONLY
), 0);
813 if (regfile
== NULL
) {
814 DEBUG(0, ("restore_registry_key: failed to open \"%s\" (%s)\n",
815 fname
, strerror(errno
)));
816 return ntstatus_to_werror(map_nt_error_from_unix(errno
));
819 /* get the rootkey from the regf file and then load the tree
820 via recursive calls */
822 if (!(rootkey
= regfio_rootkey(regfile
))) {
823 regfio_close(regfile
);
824 return WERR_REG_FILE_INVALID
;
827 result
= reg_load_tree(regfile
, krecord
->name
, rootkey
);
831 regfio_close(regfile
);
836 WERROR
reg_restorekey(struct registry_key
*key
, const char *fname
)
838 return restore_registry_key(key
->key
, fname
);
841 /********************************************************************
842 ********************************************************************/
844 static WERROR
reg_write_tree(REGF_FILE
*regfile
, const char *keypath
,
848 struct regval_ctr
*values
;
849 struct regsubkey_ctr
*subkeys
;
851 char *key_tmp
= NULL
;
852 char *keyname
, *parentpath
;
853 char *subkeypath
= NULL
;
855 struct registry_key_handle registry_key
;
856 WERROR result
= WERR_OK
;
857 SEC_DESC
*sec_desc
= NULL
;
860 return WERR_GENERAL_FAILURE
;
864 return WERR_OBJECT_PATH_INVALID
;
867 /* split up the registry key path */
869 key_tmp
= talloc_strdup(regfile
->mem_ctx
, keypath
);
873 if (!reg_split_key(key_tmp
, &parentpath
, &keyname
)) {
874 return WERR_OBJECT_PATH_INVALID
;
878 keyname
= parentpath
;
881 /* we need a registry_key_handle object here to enumerate subkeys and values */
883 ZERO_STRUCT(registry_key
);
885 registry_key
.name
= talloc_strdup(regfile
->mem_ctx
, keypath
);
886 if (registry_key
.name
== NULL
) {
890 registry_key
.ops
= reghook_cache_find(registry_key
.name
);
891 if (registry_key
.ops
== NULL
) {
895 /* lookup the values and subkeys */
897 result
= regsubkey_ctr_init(regfile
->mem_ctx
, &subkeys
);
898 W_ERROR_NOT_OK_RETURN(result
);
900 values
= TALLOC_ZERO_P(subkeys
, struct regval_ctr
);
901 if (values
== NULL
) {
905 fetch_reg_keys(®istry_key
, subkeys
);
906 fetch_reg_values(®istry_key
, values
);
908 result
= regkey_get_secdesc(regfile
->mem_ctx
, ®istry_key
, &sec_desc
);
909 if (!W_ERROR_IS_OK(result
)) {
913 /* write out this key */
915 key
= regfio_write_key(regfile
, keyname
, values
, subkeys
, sec_desc
,
918 result
= WERR_CAN_NOT_COMPLETE
;
922 /* write each one of the subkeys out */
924 num_subkeys
= regsubkey_ctr_numkeys(subkeys
);
925 for (i
=0; i
<num_subkeys
; i
++) {
926 subkeyname
= regsubkey_ctr_specific_key(subkeys
, i
);
927 subkeypath
= talloc_asprintf(regfile
->mem_ctx
, "%s\\%s",
928 keypath
, subkeyname
);
929 if (subkeypath
== NULL
) {
933 result
= reg_write_tree(regfile
, subkeypath
, key
);
934 if (!W_ERROR_IS_OK(result
))
938 DEBUG(6, ("reg_write_tree: wrote key [%s]\n", keypath
));
941 TALLOC_FREE(subkeys
);
942 TALLOC_FREE(registry_key
.name
);
947 static WERROR
backup_registry_key(struct registry_key_handle
*krecord
,
953 /* open the registry file....fail if the file already exists */
955 regfile
= regfio_open(fname
, (O_RDWR
|O_CREAT
|O_EXCL
),
957 if (regfile
== NULL
) {
958 DEBUG(0,("backup_registry_key: failed to open \"%s\" (%s)\n",
959 fname
, strerror(errno
) ));
960 return ntstatus_to_werror(map_nt_error_from_unix(errno
));
963 /* write the registry tree to the file */
965 result
= reg_write_tree(regfile
, krecord
->name
, NULL
);
969 regfio_close(regfile
);
974 WERROR
reg_savekey(struct registry_key
*key
, const char *fname
)
976 return backup_registry_key(key
->key
, fname
);
979 /**********************************************************************
980 * Higher level utility functions
981 **********************************************************************/
983 WERROR
reg_deleteallvalues(struct registry_key
*key
)
988 if (!(key
->key
->access_granted
& KEY_SET_VALUE
)) {
989 return WERR_ACCESS_DENIED
;
992 if (!W_ERROR_IS_OK(err
= fill_value_cache(key
))) {
996 for (i
=0; i
<key
->values
->num_values
; i
++) {
997 regval_ctr_delvalue(key
->values
, key
->values
->values
[i
]->valuename
);
1000 if (!store_reg_values(key
->key
, key
->values
)) {
1001 TALLOC_FREE(key
->values
);
1002 return WERR_REG_IO_FAILURE
;
1009 * Utility function to open a complete registry path including the hive prefix.
1012 WERROR
reg_open_path(TALLOC_CTX
*mem_ctx
, const char *orig_path
,
1013 uint32 desired_access
, const struct nt_user_token
*token
,
1014 struct registry_key
**pkey
)
1016 struct registry_key
*hive
, *key
;
1020 if (!(path
= SMB_STRDUP(orig_path
))) {
1024 p
= strchr(path
, '\\');
1026 if ((p
== NULL
) || (p
[1] == '\0')) {
1028 * No key behind the hive, just return the hive
1031 err
= reg_openhive(mem_ctx
, path
, desired_access
, token
,
1033 if (!W_ERROR_IS_OK(err
)) {
1044 err
= reg_openhive(mem_ctx
, path
, KEY_ENUMERATE_SUB_KEYS
, token
,
1046 if (!W_ERROR_IS_OK(err
)) {
1051 err
= reg_openkey(mem_ctx
, hive
, p
+1, desired_access
, &key
);
1056 if (!W_ERROR_IS_OK(err
)) {
1065 * Utility function to delete a registry key with all its subkeys.
1066 * Note that reg_deletekey returns ACCESS_DENIED when called on a
1067 * key that has subkeys.
1069 static WERROR
reg_deletekey_recursive_internal(TALLOC_CTX
*ctx
,
1070 struct registry_key
*parent
,
1074 TALLOC_CTX
*mem_ctx
= NULL
;
1075 WERROR werr
= WERR_OK
;
1076 struct registry_key
*key
;
1077 char *subkey_name
= NULL
;
1080 mem_ctx
= talloc_new(ctx
);
1081 if (mem_ctx
== NULL
) {
1086 /* recurse through subkeys first */
1087 werr
= reg_openkey(mem_ctx
, parent
, path
, REG_KEY_ALL
, &key
);
1088 if (!W_ERROR_IS_OK(werr
)) {
1092 werr
= fill_subkey_cache(key
);
1093 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1096 * loop from top to bottom for perfomance:
1097 * this way, we need to rehash the regsubkey containers less
1099 for (i
= regsubkey_ctr_numkeys(key
->subkeys
) ; i
> 0; i
--) {
1100 subkey_name
= regsubkey_ctr_specific_key(key
->subkeys
, i
-1);
1101 werr
= reg_deletekey_recursive_internal(mem_ctx
, key
,
1104 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1108 /* now delete the actual key */
1109 werr
= reg_deletekey(parent
, path
);
1113 TALLOC_FREE(mem_ctx
);
1117 static WERROR
reg_deletekey_recursive_trans(TALLOC_CTX
*ctx
,
1118 struct registry_key
*parent
,
1124 werr
= regdb_transaction_start();
1125 if (!W_ERROR_IS_OK(werr
)) {
1126 DEBUG(0, ("reg_deletekey_recursive_trans: "
1127 "error starting transaction: %s\n",
1132 werr
= reg_deletekey_recursive_internal(ctx
, parent
, path
, del_key
);
1134 if (!W_ERROR_IS_OK(werr
)) {
1135 DEBUG(1, (__location__
" failed to delete key '%s' from key "
1136 "'%s': %s\n", path
, parent
->key
->name
,
1138 werr
= regdb_transaction_cancel();
1139 if (!W_ERROR_IS_OK(werr
)) {
1140 DEBUG(0, ("reg_deletekey_recursive_trans: "
1141 "error cancelling transaction: %s\n",
1145 werr
= regdb_transaction_commit();
1146 if (!W_ERROR_IS_OK(werr
)) {
1147 DEBUG(0, ("reg_deletekey_recursive_trans: "
1148 "error committing transaction: %s\n",
1156 WERROR
reg_deletekey_recursive(TALLOC_CTX
*ctx
,
1157 struct registry_key
*parent
,
1160 return reg_deletekey_recursive_trans(ctx
, parent
, path
, true);
1163 WERROR
reg_deletesubkeys_recursive(TALLOC_CTX
*ctx
,
1164 struct registry_key
*parent
,
1167 return reg_deletekey_recursive_trans(ctx
, parent
, path
, false);
1171 /* these two functions are unused. */
1174 * Utility function to create a registry key without opening the hive
1175 * before. Assumes the hive already exists.
1178 WERROR
reg_create_path(TALLOC_CTX
*mem_ctx
, const char *orig_path
,
1179 uint32 desired_access
,
1180 const struct nt_user_token
*token
,
1181 enum winreg_CreateAction
*paction
,
1182 struct registry_key
**pkey
)
1184 struct registry_key
*hive
;
1188 if (!(path
= SMB_STRDUP(orig_path
))) {
1192 p
= strchr(path
, '\\');
1194 if ((p
== NULL
) || (p
[1] == '\0')) {
1196 * No key behind the hive, just return the hive
1199 err
= reg_openhive(mem_ctx
, path
, desired_access
, token
,
1201 if (!W_ERROR_IS_OK(err
)) {
1207 *paction
= REG_OPENED_EXISTING_KEY
;
1213 err
= reg_openhive(mem_ctx
, path
,
1214 (strchr(p
+1, '\\') != NULL
) ?
1215 KEY_ENUMERATE_SUB_KEYS
: KEY_CREATE_SUB_KEY
,
1217 if (!W_ERROR_IS_OK(err
)) {
1222 err
= reg_createkey(mem_ctx
, hive
, p
+1, desired_access
, pkey
, paction
);
1229 * Utility function to create a registry key without opening the hive
1230 * before. Will not delete a hive.
1233 WERROR
reg_delete_path(const struct nt_user_token
*token
,
1234 const char *orig_path
)
1236 struct registry_key
*hive
;
1240 if (!(path
= SMB_STRDUP(orig_path
))) {
1244 p
= strchr(path
, '\\');
1246 if ((p
== NULL
) || (p
[1] == '\0')) {
1248 return WERR_INVALID_PARAM
;
1253 err
= reg_openhive(NULL
, path
,
1254 (strchr(p
+1, '\\') != NULL
) ?
1255 KEY_ENUMERATE_SUB_KEYS
: KEY_CREATE_SUB_KEY
,
1257 if (!W_ERROR_IS_OK(err
)) {
1262 err
= reg_deletekey(hive
, p
+1);