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
, 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(REGISTRY_KEY
*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
;
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
, REGISTRY_KEY
))) {
144 if ( !(W_ERROR_IS_OK(result
= regdb_open())) ) {
149 talloc_set_destructor(key
, regkey_destructor
);
153 key
->type
= REG_KEY_GENERIC
;
155 if (name
[0] == '\0') {
157 * Open a copy of the parent key
160 result
= WERR_BADFILE
;
163 key
->name
= talloc_strdup(key
, parent
->key
->name
);
169 key
->name
= talloc_asprintf(key
, "%s%s%s",
170 parent
? parent
->key
->name
: "",
175 if (key
->name
== NULL
) {
180 /* Tag this as a Performance Counter Key */
182 if( StrnCaseCmp(key
->name
, KEY_HKPD
, strlen(KEY_HKPD
)) == 0 )
183 key
->type
= REG_KEY_HKPD
;
185 /* Look up the table of registry I/O operations */
187 if ( !(key
->ops
= reghook_cache_find( key
->name
)) ) {
188 DEBUG(0,("reg_open_onelevel: Failed to assign "
189 "REGISTRY_OPS to [%s]\n", key
->name
));
190 result
= WERR_BADFILE
;
194 /* check if the path really exists; failed is indicated by -1 */
195 /* if the subkey count failed, bail out */
197 result
= regsubkey_ctr_init(key
, &subkeys
);
198 if (!W_ERROR_IS_OK(result
)) {
202 if ( fetch_reg_keys( key
, subkeys
) == -1 ) {
203 result
= WERR_BADFILE
;
207 TALLOC_FREE( subkeys
);
209 if ( !regkey_access_check( key
, access_desired
, &key
->access_granted
,
211 result
= WERR_ACCESS_DENIED
;
219 if ( !W_ERROR_IS_OK(result
) ) {
226 WERROR
reg_openhive(TALLOC_CTX
*mem_ctx
, const char *hive
,
227 uint32 desired_access
,
228 const struct nt_user_token
*token
,
229 struct registry_key
**pkey
)
231 SMB_ASSERT(hive
!= NULL
);
232 SMB_ASSERT(hive
[0] != '\0');
233 SMB_ASSERT(strchr(hive
, '\\') == NULL
);
235 return regkey_open_onelevel(mem_ctx
, NULL
, hive
, token
, desired_access
,
240 /**********************************************************************
242 **********************************************************************/
244 WERROR
reg_openkey(TALLOC_CTX
*mem_ctx
, struct registry_key
*parent
,
245 const char *name
, uint32 desired_access
,
246 struct registry_key
**pkey
)
248 struct registry_key
*direct_parent
= parent
;
250 char *p
, *path
, *to_free
;
253 if (!(path
= SMB_STRDUP(name
))) {
260 if ((len
> 0) && (path
[len
-1] == '\\')) {
264 while ((p
= strchr(path
, '\\')) != NULL
) {
265 char *name_component
;
266 struct registry_key
*tmp
;
268 if (!(name_component
= SMB_STRNDUP(path
, (p
- path
)))) {
273 err
= regkey_open_onelevel(mem_ctx
, direct_parent
,
274 name_component
, parent
->token
,
275 SEC_RIGHTS_ENUM_SUBKEYS
, &tmp
);
276 SAFE_FREE(name_component
);
278 if (!W_ERROR_IS_OK(err
)) {
281 if (direct_parent
!= parent
) {
282 TALLOC_FREE(direct_parent
);
289 err
= regkey_open_onelevel(mem_ctx
, direct_parent
, path
, parent
->token
,
290 desired_access
, pkey
);
292 if (direct_parent
!= parent
) {
293 TALLOC_FREE(direct_parent
);
299 WERROR
reg_enumkey(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
300 uint32 idx
, char **name
, NTTIME
*last_write_time
)
304 if (!(key
->key
->access_granted
& SEC_RIGHTS_ENUM_SUBKEYS
)) {
305 return WERR_ACCESS_DENIED
;
308 if (!W_ERROR_IS_OK(err
= fill_subkey_cache(key
))) {
312 if (idx
>= regsubkey_ctr_numkeys(key
->subkeys
)) {
313 return WERR_NO_MORE_ITEMS
;
316 if (!(*name
= talloc_strdup(mem_ctx
,
317 regsubkey_ctr_specific_key(key
->subkeys
, idx
))))
322 if (last_write_time
) {
323 *last_write_time
= 0;
329 WERROR
reg_enumvalue(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
330 uint32 idx
, char **pname
, struct registry_value
**pval
)
332 struct registry_value
*val
;
335 if (!(key
->key
->access_granted
& SEC_RIGHTS_QUERY_VALUE
)) {
336 return WERR_ACCESS_DENIED
;
339 if (!(W_ERROR_IS_OK(err
= fill_value_cache(key
)))) {
343 if (idx
>= key
->values
->num_values
) {
344 return WERR_NO_MORE_ITEMS
;
347 err
= registry_pull_value(mem_ctx
, &val
,
348 key
->values
->values
[idx
]->type
,
349 key
->values
->values
[idx
]->data_p
,
350 key
->values
->values
[idx
]->size
,
351 key
->values
->values
[idx
]->size
);
352 if (!W_ERROR_IS_OK(err
)) {
357 && !(*pname
= talloc_strdup(
358 mem_ctx
, key
->values
->values
[idx
]->valuename
))) {
367 WERROR
reg_queryvalue(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
368 const char *name
, struct registry_value
**pval
)
373 if (!(key
->key
->access_granted
& SEC_RIGHTS_QUERY_VALUE
)) {
374 return WERR_ACCESS_DENIED
;
377 if (!(W_ERROR_IS_OK(err
= fill_value_cache(key
)))) {
381 for (i
=0; i
<key
->values
->num_values
; i
++) {
382 if (strequal(key
->values
->values
[i
]->valuename
, name
)) {
383 return reg_enumvalue(mem_ctx
, key
, i
, NULL
, pval
);
390 WERROR
reg_queryinfokey(struct registry_key
*key
, uint32_t *num_subkeys
,
391 uint32_t *max_subkeylen
, uint32_t *max_subkeysize
,
392 uint32_t *num_values
, uint32_t *max_valnamelen
,
393 uint32_t *max_valbufsize
, uint32_t *secdescsize
,
394 NTTIME
*last_changed_time
)
400 struct security_descriptor
*secdesc
;
402 if (!(key
->key
->access_granted
& SEC_RIGHTS_QUERY_VALUE
)) {
403 return WERR_ACCESS_DENIED
;
406 if (!W_ERROR_IS_OK(fill_subkey_cache(key
)) ||
407 !W_ERROR_IS_OK(fill_value_cache(key
))) {
412 for (i
=0; i
< regsubkey_ctr_numkeys(key
->subkeys
); i
++) {
413 max_len
= MAX(max_len
,
414 strlen(regsubkey_ctr_specific_key(key
->subkeys
, i
)));
417 *num_subkeys
= regsubkey_ctr_numkeys(key
->subkeys
);
418 *max_subkeylen
= max_len
;
419 *max_subkeysize
= 0; /* Class length? */
423 for (i
=0; i
<key
->values
->num_values
; i
++) {
424 max_len
= MAX(max_len
,
425 strlen(key
->values
->values
[i
]->valuename
));
426 max_size
= MAX(max_size
, key
->values
->values
[i
]->size
);
429 *num_values
= key
->values
->num_values
;
430 *max_valnamelen
= max_len
;
431 *max_valbufsize
= max_size
;
433 if (!(mem_ctx
= talloc_new(key
))) {
437 err
= regkey_get_secdesc(mem_ctx
, key
->key
, &secdesc
);
438 if (!W_ERROR_IS_OK(err
)) {
439 TALLOC_FREE(mem_ctx
);
443 *secdescsize
= ndr_size_security_descriptor(secdesc
, 0);
444 TALLOC_FREE(mem_ctx
);
446 *last_changed_time
= 0;
451 WERROR
reg_createkey(TALLOC_CTX
*ctx
, struct registry_key
*parent
,
452 const char *subkeypath
, uint32 desired_access
,
453 struct registry_key
**pkey
,
454 enum winreg_CreateAction
*paction
)
456 struct registry_key
*key
= parent
;
457 struct registry_key
*create_parent
;
463 * We must refuse to handle subkey-paths containing
464 * a '/' character because at a lower level, after
465 * normalization, '/' is treated as a key separator
468 if (strchr(subkeypath
, '/') != NULL
) {
469 return WERR_INVALID_PARAM
;
472 if (!(mem_ctx
= talloc_new(ctx
))) return WERR_NOMEM
;
474 if (!(path
= talloc_strdup(mem_ctx
, subkeypath
))) {
479 while ((end
= strchr(path
, '\\')) != NULL
) {
480 struct registry_key
*tmp
;
481 enum winreg_CreateAction action
;
485 err
= reg_createkey(mem_ctx
, key
, path
,
486 SEC_RIGHTS_ENUM_SUBKEYS
, &tmp
, &action
);
487 if (!W_ERROR_IS_OK(err
)) {
500 * At this point, "path" contains the one-element subkey of "key". We
501 * can try to open it.
504 err
= reg_openkey(ctx
, key
, path
, desired_access
, pkey
);
505 if (W_ERROR_IS_OK(err
)) {
506 if (paction
!= NULL
) {
507 *paction
= REG_OPENED_EXISTING_KEY
;
512 if (!W_ERROR_EQUAL(err
, WERR_BADFILE
)) {
514 * Something but "notfound" has happened, so bail out
520 * We have to make a copy of the current key, as we opened it only
521 * with ENUM_SUBKEY access.
524 err
= reg_openkey(mem_ctx
, key
, "", SEC_RIGHTS_CREATE_SUBKEY
,
526 if (!W_ERROR_IS_OK(err
)) {
531 * Actually create the subkey
534 err
= fill_subkey_cache(create_parent
);
535 if (!W_ERROR_IS_OK(err
)) goto done
;
537 err
= create_reg_subkey(key
->key
, path
);
538 W_ERROR_NOT_OK_GOTO_DONE(err
);
541 * Now open the newly created key
544 err
= reg_openkey(ctx
, create_parent
, path
, desired_access
, pkey
);
545 if (W_ERROR_IS_OK(err
) && (paction
!= NULL
)) {
546 *paction
= REG_CREATED_NEW_KEY
;
550 TALLOC_FREE(mem_ctx
);
554 WERROR
reg_deletekey(struct registry_key
*parent
, const char *path
)
558 struct registry_key
*tmp_key
, *key
;
559 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
561 name
= talloc_strdup(mem_ctx
, path
);
567 /* check if the key has subkeys */
568 err
= reg_openkey(mem_ctx
, parent
, name
, REG_KEY_READ
, &key
);
569 W_ERROR_NOT_OK_GOTO_DONE(err
);
571 err
= fill_subkey_cache(key
);
572 W_ERROR_NOT_OK_GOTO_DONE(err
);
574 if (regsubkey_ctr_numkeys(key
->subkeys
) > 0) {
575 err
= WERR_ACCESS_DENIED
;
579 /* no subkeys - proceed with delete */
580 end
= strrchr(name
, '\\');
584 err
= reg_openkey(mem_ctx
, parent
, name
,
585 SEC_RIGHTS_CREATE_SUBKEY
, &tmp_key
);
586 W_ERROR_NOT_OK_GOTO_DONE(err
);
592 if (name
[0] == '\0') {
593 err
= WERR_INVALID_PARAM
;
597 err
= delete_reg_subkey(parent
->key
, name
);
600 TALLOC_FREE(mem_ctx
);
604 WERROR
reg_setvalue(struct registry_key
*key
, const char *name
,
605 const struct registry_value
*val
)
608 DATA_BLOB value_data
;
611 if (!(key
->key
->access_granted
& SEC_RIGHTS_SET_VALUE
)) {
612 return WERR_ACCESS_DENIED
;
615 if (!W_ERROR_IS_OK(err
= fill_value_cache(key
))) {
619 err
= registry_push_value(key
, val
, &value_data
);
620 if (!W_ERROR_IS_OK(err
)) {
624 res
= regval_ctr_addvalue(key
->values
, name
, val
->type
,
625 (char *)value_data
.data
, value_data
.length
);
626 TALLOC_FREE(value_data
.data
);
629 TALLOC_FREE(key
->values
);
633 if (!store_reg_values(key
->key
, key
->values
)) {
634 TALLOC_FREE(key
->values
);
635 return WERR_REG_IO_FAILURE
;
641 static WERROR
reg_value_exists(struct registry_key
*key
, const char *name
)
645 for (i
=0; i
<key
->values
->num_values
; i
++) {
646 if (strequal(key
->values
->values
[i
]->valuename
, name
)) {
654 WERROR
reg_deletevalue(struct registry_key
*key
, const char *name
)
658 if (!(key
->key
->access_granted
& SEC_RIGHTS_SET_VALUE
)) {
659 return WERR_ACCESS_DENIED
;
662 if (!W_ERROR_IS_OK(err
= fill_value_cache(key
))) {
666 err
= reg_value_exists(key
, name
);
667 if (!W_ERROR_IS_OK(err
)) {
671 regval_ctr_delvalue(key
->values
, name
);
673 if (!store_reg_values(key
->key
, key
->values
)) {
674 TALLOC_FREE(key
->values
);
675 return WERR_REG_IO_FAILURE
;
681 WERROR
reg_getkeysecurity(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
682 struct security_descriptor
**psecdesc
)
684 return regkey_get_secdesc(mem_ctx
, key
->key
, psecdesc
);
687 WERROR
reg_setkeysecurity(struct registry_key
*key
,
688 struct security_descriptor
*psecdesc
)
690 return regkey_set_secdesc(key
->key
, psecdesc
);
693 WERROR
reg_getversion(uint32_t *version
)
695 if (version
== NULL
) {
696 return WERR_INVALID_PARAM
;
699 *version
= 0x00000005; /* Windows 2000 registry API version */
703 /*******************************************************************
704 Note: topkeypat is the *full* path that this *key will be
705 loaded into (including the name of the key)
706 ********************************************************************/
708 static WERROR
reg_load_tree(REGF_FILE
*regfile
, const char *topkeypath
,
712 REGISTRY_KEY registry_key
;
714 struct regsubkey_ctr
*subkeys
;
717 WERROR result
= WERR_OK
;
719 /* initialize the REGISTRY_KEY structure */
721 registry_key
.ops
= reghook_cache_find(topkeypath
);
722 if (!registry_key
.ops
) {
723 DEBUG(0, ("reg_load_tree: Failed to assign REGISTRY_OPS "
724 "to [%s]\n", topkeypath
));
728 registry_key
.name
= talloc_strdup(regfile
->mem_ctx
, topkeypath
);
729 if (!registry_key
.name
) {
730 DEBUG(0, ("reg_load_tree: Talloc failed for reg_key.name!\n"));
734 /* now start parsing the values and subkeys */
736 result
= regsubkey_ctr_init(regfile
->mem_ctx
, &subkeys
);
737 W_ERROR_NOT_OK_RETURN(result
);
739 values
= TALLOC_ZERO_P(subkeys
, REGVAL_CTR
);
740 if (values
== NULL
) {
744 /* copy values into the REGVAL_CTR */
746 for (i
=0; i
<key
->num_values
; i
++) {
747 regval_ctr_addvalue(values
, key
->values
[i
].valuename
,
749 (char*)key
->values
[i
].data
,
750 (key
->values
[i
].data_size
& ~VK_DATA_IN_OFFSET
));
753 /* copy subkeys into the struct regsubkey_ctr */
755 key
->subkey_index
= 0;
756 while ((subkey
= regfio_fetch_subkey( regfile
, key
))) {
757 result
= regsubkey_ctr_addkey(subkeys
, subkey
->keyname
);
758 if (!W_ERROR_IS_OK(result
)) {
759 TALLOC_FREE(subkeys
);
764 /* write this key and values out */
766 if (!store_reg_values(®istry_key
, values
)
767 || !store_reg_keys(®istry_key
, subkeys
))
769 DEBUG(0,("reg_load_tree: Failed to load %s!\n", topkeypath
));
770 result
= WERR_REG_IO_FAILURE
;
773 TALLOC_FREE(subkeys
);
775 if (!W_ERROR_IS_OK(result
)) {
779 /* now continue to load each subkey registry tree */
781 key
->subkey_index
= 0;
782 while ((subkey
= regfio_fetch_subkey(regfile
, key
))) {
783 path
= talloc_asprintf(regfile
->mem_ctx
,
790 result
= reg_load_tree(regfile
, path
, subkey
);
791 if (!W_ERROR_IS_OK(result
)) {
799 /*******************************************************************
800 ********************************************************************/
802 static WERROR
restore_registry_key(REGISTRY_KEY
*krecord
, const char *fname
)
805 REGF_NK_REC
*rootkey
;
808 /* open the registry file....fail if the file already exists */
810 regfile
= regfio_open(fname
, (O_RDONLY
), 0);
811 if (regfile
== NULL
) {
812 DEBUG(0, ("restore_registry_key: failed to open \"%s\" (%s)\n",
813 fname
, strerror(errno
)));
814 return ntstatus_to_werror(map_nt_error_from_unix(errno
));
817 /* get the rootkey from the regf file and then load the tree
818 via recursive calls */
820 if (!(rootkey
= regfio_rootkey(regfile
))) {
821 regfio_close(regfile
);
822 return WERR_REG_FILE_INVALID
;
825 result
= reg_load_tree(regfile
, krecord
->name
, rootkey
);
829 regfio_close(regfile
);
834 WERROR
reg_restorekey(struct registry_key
*key
, const char *fname
)
836 return restore_registry_key(key
->key
, fname
);
839 /********************************************************************
840 ********************************************************************/
842 static WERROR
reg_write_tree(REGF_FILE
*regfile
, const char *keypath
,
847 struct regsubkey_ctr
*subkeys
;
849 char *key_tmp
= NULL
;
850 char *keyname
, *parentpath
;
851 char *subkeypath
= NULL
;
853 REGISTRY_KEY registry_key
;
854 WERROR result
= WERR_OK
;
855 SEC_DESC
*sec_desc
= NULL
;
858 return WERR_GENERAL_FAILURE
;
862 return WERR_OBJECT_PATH_INVALID
;
865 /* split up the registry key path */
867 key_tmp
= talloc_strdup(regfile
->mem_ctx
, keypath
);
871 if (!reg_split_key(key_tmp
, &parentpath
, &keyname
)) {
872 return WERR_OBJECT_PATH_INVALID
;
876 keyname
= parentpath
;
879 /* we need a REGISTRY_KEY object here to enumerate subkeys and values */
881 ZERO_STRUCT(registry_key
);
883 registry_key
.name
= talloc_strdup(regfile
->mem_ctx
, keypath
);
884 if (registry_key
.name
== NULL
) {
888 registry_key
.ops
= reghook_cache_find(registry_key
.name
);
889 if (registry_key
.ops
== NULL
) {
893 /* lookup the values and subkeys */
895 result
= regsubkey_ctr_init(regfile
->mem_ctx
, &subkeys
);
896 W_ERROR_NOT_OK_RETURN(result
);
898 values
= TALLOC_ZERO_P(subkeys
, REGVAL_CTR
);
899 if (values
== NULL
) {
903 fetch_reg_keys(®istry_key
, subkeys
);
904 fetch_reg_values(®istry_key
, values
);
906 result
= regkey_get_secdesc(regfile
->mem_ctx
, ®istry_key
, &sec_desc
);
907 if (!W_ERROR_IS_OK(result
)) {
911 /* write out this key */
913 key
= regfio_write_key(regfile
, keyname
, values
, subkeys
, sec_desc
,
916 result
= WERR_CAN_NOT_COMPLETE
;
920 /* write each one of the subkeys out */
922 num_subkeys
= regsubkey_ctr_numkeys(subkeys
);
923 for (i
=0; i
<num_subkeys
; i
++) {
924 subkeyname
= regsubkey_ctr_specific_key(subkeys
, i
);
925 subkeypath
= talloc_asprintf(regfile
->mem_ctx
, "%s\\%s",
926 keypath
, subkeyname
);
927 if (subkeypath
== NULL
) {
931 result
= reg_write_tree(regfile
, subkeypath
, key
);
932 if (!W_ERROR_IS_OK(result
))
936 DEBUG(6, ("reg_write_tree: wrote key [%s]\n", keypath
));
939 TALLOC_FREE(subkeys
);
940 TALLOC_FREE(registry_key
.name
);
945 static WERROR
backup_registry_key(REGISTRY_KEY
*krecord
, const char *fname
)
950 /* open the registry file....fail if the file already exists */
952 regfile
= regfio_open(fname
, (O_RDWR
|O_CREAT
|O_EXCL
),
954 if (regfile
== NULL
) {
955 DEBUG(0,("backup_registry_key: failed to open \"%s\" (%s)\n",
956 fname
, strerror(errno
) ));
957 return ntstatus_to_werror(map_nt_error_from_unix(errno
));
960 /* write the registry tree to the file */
962 result
= reg_write_tree(regfile
, krecord
->name
, NULL
);
966 regfio_close(regfile
);
971 WERROR
reg_savekey(struct registry_key
*key
, const char *fname
)
973 return backup_registry_key(key
->key
, fname
);
976 /**********************************************************************
977 * Higher level utility functions
978 **********************************************************************/
980 WERROR
reg_deleteallvalues(struct registry_key
*key
)
985 if (!(key
->key
->access_granted
& SEC_RIGHTS_SET_VALUE
)) {
986 return WERR_ACCESS_DENIED
;
989 if (!W_ERROR_IS_OK(err
= fill_value_cache(key
))) {
993 for (i
=0; i
<key
->values
->num_values
; i
++) {
994 regval_ctr_delvalue(key
->values
, key
->values
->values
[i
]->valuename
);
997 if (!store_reg_values(key
->key
, key
->values
)) {
998 TALLOC_FREE(key
->values
);
999 return WERR_REG_IO_FAILURE
;
1006 * Utility function to open a complete registry path including the hive prefix.
1009 WERROR
reg_open_path(TALLOC_CTX
*mem_ctx
, const char *orig_path
,
1010 uint32 desired_access
, const struct nt_user_token
*token
,
1011 struct registry_key
**pkey
)
1013 struct registry_key
*hive
, *key
;
1017 if (!(path
= SMB_STRDUP(orig_path
))) {
1021 p
= strchr(path
, '\\');
1023 if ((p
== NULL
) || (p
[1] == '\0')) {
1025 * No key behind the hive, just return the hive
1028 err
= reg_openhive(mem_ctx
, path
, desired_access
, token
,
1030 if (!W_ERROR_IS_OK(err
)) {
1041 err
= reg_openhive(mem_ctx
, path
, SEC_RIGHTS_ENUM_SUBKEYS
, token
,
1043 if (!W_ERROR_IS_OK(err
)) {
1048 err
= reg_openkey(mem_ctx
, hive
, p
+1, desired_access
, &key
);
1053 if (!W_ERROR_IS_OK(err
)) {
1062 * Utility function to delete a registry key with all its subkeys.
1063 * Note that reg_deletekey returns ACCESS_DENIED when called on a
1064 * key that has subkeys.
1066 static WERROR
reg_deletekey_recursive_internal(TALLOC_CTX
*ctx
,
1067 struct registry_key
*parent
,
1071 TALLOC_CTX
*mem_ctx
= NULL
;
1072 WERROR werr
= WERR_OK
;
1073 struct registry_key
*key
;
1074 char *subkey_name
= NULL
;
1077 mem_ctx
= talloc_new(ctx
);
1078 if (mem_ctx
== NULL
) {
1083 /* recurse through subkeys first */
1084 werr
= reg_openkey(mem_ctx
, parent
, path
, REG_KEY_ALL
, &key
);
1085 if (!W_ERROR_IS_OK(werr
)) {
1089 werr
= fill_subkey_cache(key
);
1090 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1093 * loop from top to bottom for perfomance:
1094 * this way, we need to rehash the regsubkey containers less
1096 for (i
= regsubkey_ctr_numkeys(key
->subkeys
) ; i
> 0; i
--) {
1097 subkey_name
= regsubkey_ctr_specific_key(key
->subkeys
, i
-1);
1098 werr
= reg_deletekey_recursive_internal(mem_ctx
, key
,
1101 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1105 /* now delete the actual key */
1106 werr
= reg_deletekey(parent
, path
);
1110 TALLOC_FREE(mem_ctx
);
1114 static WERROR
reg_deletekey_recursive_trans(TALLOC_CTX
*ctx
,
1115 struct registry_key
*parent
,
1121 werr
= regdb_transaction_start();
1122 if (!W_ERROR_IS_OK(werr
)) {
1123 DEBUG(0, ("reg_deletekey_recursive_trans: "
1124 "error starting transaction: %s\n",
1129 werr
= reg_deletekey_recursive_internal(ctx
, parent
, path
, del_key
);
1131 if (!W_ERROR_IS_OK(werr
)) {
1132 DEBUG(1, (__location__
" failed to delete key '%s' from key "
1133 "'%s': %s\n", path
, parent
->key
->name
,
1135 werr
= regdb_transaction_cancel();
1136 if (!W_ERROR_IS_OK(werr
)) {
1137 DEBUG(0, ("reg_deletekey_recursive_trans: "
1138 "error cancelling transaction: %s\n",
1142 werr
= regdb_transaction_commit();
1143 if (!W_ERROR_IS_OK(werr
)) {
1144 DEBUG(0, ("reg_deletekey_recursive_trans: "
1145 "error committing transaction: %s\n",
1153 WERROR
reg_deletekey_recursive(TALLOC_CTX
*ctx
,
1154 struct registry_key
*parent
,
1157 return reg_deletekey_recursive_trans(ctx
, parent
, path
, true);
1160 WERROR
reg_deletesubkeys_recursive(TALLOC_CTX
*ctx
,
1161 struct registry_key
*parent
,
1164 return reg_deletekey_recursive_trans(ctx
, parent
, path
, false);
1168 /* these two functions are unused. */
1171 * Utility function to create a registry key without opening the hive
1172 * before. Assumes the hive already exists.
1175 WERROR
reg_create_path(TALLOC_CTX
*mem_ctx
, const char *orig_path
,
1176 uint32 desired_access
,
1177 const struct nt_user_token
*token
,
1178 enum winreg_CreateAction
*paction
,
1179 struct registry_key
**pkey
)
1181 struct registry_key
*hive
;
1185 if (!(path
= SMB_STRDUP(orig_path
))) {
1189 p
= strchr(path
, '\\');
1191 if ((p
== NULL
) || (p
[1] == '\0')) {
1193 * No key behind the hive, just return the hive
1196 err
= reg_openhive(mem_ctx
, path
, desired_access
, token
,
1198 if (!W_ERROR_IS_OK(err
)) {
1204 *paction
= REG_OPENED_EXISTING_KEY
;
1210 err
= reg_openhive(mem_ctx
, path
,
1211 (strchr(p
+1, '\\') != NULL
) ?
1212 SEC_RIGHTS_ENUM_SUBKEYS
: SEC_RIGHTS_CREATE_SUBKEY
,
1214 if (!W_ERROR_IS_OK(err
)) {
1219 err
= reg_createkey(mem_ctx
, hive
, p
+1, desired_access
, pkey
, paction
);
1226 * Utility function to create a registry key without opening the hive
1227 * before. Will not delete a hive.
1230 WERROR
reg_delete_path(const struct nt_user_token
*token
,
1231 const char *orig_path
)
1233 struct registry_key
*hive
;
1237 if (!(path
= SMB_STRDUP(orig_path
))) {
1241 p
= strchr(path
, '\\');
1243 if ((p
== NULL
) || (p
[1] == '\0')) {
1245 return WERR_INVALID_PARAM
;
1250 err
= reg_openhive(NULL
, path
,
1251 (strchr(p
+1, '\\') != NULL
) ?
1252 SEC_RIGHTS_ENUM_SUBKEYS
: SEC_RIGHTS_CREATE_SUBKEY
,
1254 if (!W_ERROR_IS_OK(err
)) {
1259 err
= reg_deletekey(hive
, p
+1);