2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Volker Lendecke 2006
5 * Copyright (C) Michael Adam 2007-2010
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 reg_querymultiplevalues
57 * 0x1e winreg_InitiateSystemShutdownEx
58 * 0x1f winreg_SaveKeyEx
59 * 0x20 winreg_OpenHKPT
60 * 0x21 winreg_OpenHKPN
61 * 0x22 winreg_QueryMultipleValues2 reg_querymultiplevalues
68 #include "reg_cachehook.h"
69 #include "reg_backend_db.h"
70 #include "reg_dispatcher.h"
71 #include "reg_objects.h"
72 #include "../librpc/gen_ndr/ndr_security.h"
73 #include "reg_parse_internal.h"
76 #define DBGC_CLASS DBGC_REGISTRY
79 /**********************************************************************
81 **********************************************************************/
83 static WERROR
fill_value_cache(struct registry_key
*key
)
87 if (key
->values
!= NULL
) {
88 if (!reg_values_need_update(key
->key
, key
->values
)) {
93 TALLOC_FREE(key
->values
);
94 werr
= regval_ctr_init(key
, &(key
->values
));
95 W_ERROR_NOT_OK_RETURN(werr
);
97 if (fetch_reg_values(key
->key
, key
->values
) == -1) {
98 TALLOC_FREE(key
->values
);
105 static WERROR
fill_subkey_cache(struct registry_key
*key
)
109 if (key
->subkeys
!= NULL
) {
110 if (!reg_subkeys_need_update(key
->key
, key
->subkeys
)) {
115 TALLOC_FREE(key
->subkeys
);
116 werr
= regsubkey_ctr_init(key
, &(key
->subkeys
));
117 W_ERROR_NOT_OK_RETURN(werr
);
119 if (fetch_reg_keys(key
->key
, key
->subkeys
) == -1) {
120 TALLOC_FREE(key
->subkeys
);
127 static int regkey_destructor(struct registry_key_handle
*key
)
129 return regdb_close();
132 static WERROR
regkey_open_onelevel(TALLOC_CTX
*mem_ctx
,
133 struct registry_key
*parent
,
135 const struct security_token
*token
,
136 uint32 access_desired
,
137 struct registry_key
**pregkey
)
139 WERROR result
= WERR_OK
;
140 struct registry_key
*regkey
;
141 struct registry_key_handle
*key
;
143 DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name
));
145 SMB_ASSERT(strchr(name
, '\\') == NULL
);
147 if (!(regkey
= talloc_zero(mem_ctx
, struct registry_key
)) ||
148 !(regkey
->token
= dup_nt_token(regkey
, token
)) ||
149 !(regkey
->key
= talloc_zero(regkey
, struct registry_key_handle
)))
155 result
= regdb_open();
156 if (!(W_ERROR_IS_OK(result
))) {
161 talloc_set_destructor(key
, regkey_destructor
);
165 key
->type
= REG_KEY_GENERIC
;
167 if (name
[0] == '\0') {
169 * Open a copy of the parent key
172 result
= WERR_BADFILE
;
175 key
->name
= talloc_strdup(key
, parent
->key
->name
);
181 key
->name
= talloc_asprintf(key
, "%s%s%s",
182 parent
? parent
->key
->name
: "",
187 if (key
->name
== NULL
) {
192 /* Tag this as a Performance Counter Key */
194 if( strncasecmp_m(key
->name
, KEY_HKPD
, strlen(KEY_HKPD
)) == 0 )
195 key
->type
= REG_KEY_HKPD
;
197 /* Look up the table of registry I/O operations */
199 key
->ops
= reghook_cache_find( key
->name
);
200 if (key
->ops
== NULL
) {
201 DEBUG(0,("reg_open_onelevel: Failed to assign "
202 "registry_ops to [%s]\n", key
->name
));
203 result
= WERR_BADFILE
;
207 /* FIXME: Existence is currently checked by fetching the subkeys */
209 result
= fill_subkey_cache(regkey
);
210 if (!W_ERROR_IS_OK(result
)) {
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 security_token
*token
,
234 struct registry_key
**pkey
)
236 const struct hive_info
*hi
;
237 SMB_ASSERT(hive
!= NULL
);
238 SMB_ASSERT(strchr(hive
, '\\') == NULL
);
240 hi
= hive_info(hive
);
245 return regkey_open_onelevel(mem_ctx
, NULL
, hi
->short_name
, token
,
246 desired_access
, pkey
);
250 /**********************************************************************
252 **********************************************************************/
254 WERROR
reg_openkey(TALLOC_CTX
*mem_ctx
, struct registry_key
*parent
,
255 const char *name
, uint32 desired_access
,
256 struct registry_key
**pkey
)
258 struct registry_key
*direct_parent
= parent
;
262 TALLOC_CTX
*frame
= talloc_stackframe();
264 path
= talloc_strdup(frame
, name
);
272 if ((len
> 0) && (path
[len
-1] == '\\')) {
276 while ((p
= strchr(path
, '\\')) != NULL
) {
277 char *name_component
;
278 struct registry_key
*tmp
;
280 name_component
= talloc_strndup(frame
, path
, (p
- path
));
281 if (name_component
== NULL
) {
286 err
= regkey_open_onelevel(frame
, direct_parent
,
287 name_component
, parent
->token
,
288 KEY_ENUMERATE_SUB_KEYS
, &tmp
);
290 if (!W_ERROR_IS_OK(err
)) {
298 err
= regkey_open_onelevel(mem_ctx
, direct_parent
, path
, parent
->token
,
299 desired_access
, pkey
);
306 WERROR
reg_enumkey(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
307 uint32 idx
, char **name
, NTTIME
*last_write_time
)
311 if (!(key
->key
->access_granted
& KEY_ENUMERATE_SUB_KEYS
)) {
312 return WERR_ACCESS_DENIED
;
315 err
= fill_subkey_cache(key
);
316 if (!W_ERROR_IS_OK(err
)) {
320 if (idx
>= regsubkey_ctr_numkeys(key
->subkeys
)) {
321 return WERR_NO_MORE_ITEMS
;
324 if (!(*name
= talloc_strdup(mem_ctx
,
325 regsubkey_ctr_specific_key(key
->subkeys
, idx
))))
330 if (last_write_time
) {
331 *last_write_time
= 0;
337 WERROR
reg_enumvalue(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
338 uint32 idx
, char **pname
, struct registry_value
**pval
)
340 struct registry_value
*val
;
341 struct regval_blob
*blob
;
344 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
345 return WERR_ACCESS_DENIED
;
348 err
= fill_value_cache(key
);
349 if (!(W_ERROR_IS_OK(err
))) {
353 if (idx
>= regval_ctr_numvals(key
->values
)) {
354 return WERR_NO_MORE_ITEMS
;
357 blob
= regval_ctr_specific_value(key
->values
, idx
);
359 val
= talloc_zero(mem_ctx
, struct registry_value
);
364 val
->type
= regval_type(blob
);
365 val
->data
= data_blob_talloc(mem_ctx
, regval_data_p(blob
), regval_size(blob
));
368 && !(*pname
= talloc_strdup(
369 mem_ctx
, regval_name(blob
)))) {
378 static WERROR
reg_enumvalue_nocachefill(TALLOC_CTX
*mem_ctx
,
379 struct registry_key
*key
,
380 uint32 idx
, char **pname
,
381 struct registry_value
**pval
)
383 struct registry_value
*val
;
384 struct regval_blob
*blob
;
386 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
387 return WERR_ACCESS_DENIED
;
390 if (idx
>= regval_ctr_numvals(key
->values
)) {
391 return WERR_NO_MORE_ITEMS
;
394 blob
= regval_ctr_specific_value(key
->values
, idx
);
396 val
= talloc_zero(mem_ctx
, struct registry_value
);
401 val
->type
= regval_type(blob
);
402 val
->data
= data_blob_talloc(mem_ctx
, regval_data_p(blob
), regval_size(blob
));
405 && !(*pname
= talloc_strdup(
406 mem_ctx
, regval_name(blob
)))) {
415 WERROR
reg_queryvalue(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
416 const char *name
, struct registry_value
**pval
)
421 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
422 return WERR_ACCESS_DENIED
;
425 if (!(W_ERROR_IS_OK(err
= fill_value_cache(key
)))) {
429 for (i
=0; i
< regval_ctr_numvals(key
->values
); i
++) {
430 struct regval_blob
*blob
;
431 blob
= regval_ctr_specific_value(key
->values
, i
);
432 if (strequal(regval_name(blob
), name
)) {
434 * don't use reg_enumvalue here:
435 * re-reading the values from the disk
436 * would change the indexing and break
439 return reg_enumvalue_nocachefill(mem_ctx
, key
, i
,
447 WERROR
reg_querymultiplevalues(TALLOC_CTX
*mem_ctx
,
448 struct registry_key
*key
,
452 struct registry_value
**pvals
)
455 uint32_t i
, n
, found
= 0;
456 struct registry_value
*vals
;
458 if (num_names
== 0) {
462 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
463 return WERR_ACCESS_DENIED
;
466 if (!(W_ERROR_IS_OK(err
= fill_value_cache(key
)))) {
470 vals
= talloc_zero_array(mem_ctx
, struct registry_value
, num_names
);
475 for (n
=0; n
< num_names
; n
++) {
476 for (i
=0; i
< regval_ctr_numvals(key
->values
); i
++) {
477 struct regval_blob
*blob
;
478 blob
= regval_ctr_specific_value(key
->values
, i
);
479 if (strequal(regval_name(blob
), names
[n
])) {
480 struct registry_value
*v
;
481 err
= reg_enumvalue(mem_ctx
, key
, i
, NULL
, &v
);
482 if (!W_ERROR_IS_OK(err
)) {
497 WERROR
reg_queryinfokey(struct registry_key
*key
, uint32_t *num_subkeys
,
498 uint32_t *max_subkeylen
, uint32_t *max_subkeysize
,
499 uint32_t *num_values
, uint32_t *max_valnamelen
,
500 uint32_t *max_valbufsize
, uint32_t *secdescsize
,
501 NTTIME
*last_changed_time
)
507 struct security_descriptor
*secdesc
;
509 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
510 return WERR_ACCESS_DENIED
;
513 if (!W_ERROR_IS_OK(fill_subkey_cache(key
)) ||
514 !W_ERROR_IS_OK(fill_value_cache(key
))) {
519 for (i
=0; i
< regsubkey_ctr_numkeys(key
->subkeys
); i
++) {
520 max_len
= MAX(max_len
,
521 strlen(regsubkey_ctr_specific_key(key
->subkeys
, i
)));
524 *num_subkeys
= regsubkey_ctr_numkeys(key
->subkeys
);
525 *max_subkeylen
= max_len
;
526 *max_subkeysize
= 0; /* Class length? */
530 for (i
=0; i
< regval_ctr_numvals(key
->values
); i
++) {
531 struct regval_blob
*blob
;
532 blob
= regval_ctr_specific_value(key
->values
, i
);
533 max_len
= MAX(max_len
, strlen(regval_name(blob
)));
534 max_size
= MAX(max_size
, regval_size(blob
));
537 *num_values
= regval_ctr_numvals(key
->values
);
538 *max_valnamelen
= max_len
;
539 *max_valbufsize
= max_size
;
541 if (!(mem_ctx
= talloc_new(key
))) {
545 err
= regkey_get_secdesc(mem_ctx
, key
->key
, &secdesc
);
546 if (!W_ERROR_IS_OK(err
)) {
547 TALLOC_FREE(mem_ctx
);
551 *secdescsize
= ndr_size_security_descriptor(secdesc
, 0);
552 TALLOC_FREE(mem_ctx
);
554 *last_changed_time
= 0;
559 WERROR
reg_createkey(TALLOC_CTX
*ctx
, struct registry_key
*parent
,
560 const char *subkeypath
, uint32 desired_access
,
561 struct registry_key
**pkey
,
562 enum winreg_CreateAction
*paction
)
564 struct registry_key
*key
= parent
;
568 uint32_t access_granted
;
570 mem_ctx
= talloc_new(ctx
);
571 if (mem_ctx
== NULL
) {
575 path
= talloc_strdup(mem_ctx
, subkeypath
);
581 err
= regdb_transaction_start();
582 if (!W_ERROR_IS_OK(err
)) {
583 DEBUG(0, ("reg_createkey: failed to start transaction: %s\n",
588 while ((end
= strchr(path
, '\\')) != NULL
) {
589 struct registry_key
*tmp
;
590 enum winreg_CreateAction action
;
594 err
= reg_createkey(mem_ctx
, key
, path
,
595 KEY_ENUMERATE_SUB_KEYS
, &tmp
, &action
);
596 if (!W_ERROR_IS_OK(err
)) {
609 * At this point, "path" contains the one-element subkey of "key". We
610 * can try to open it.
613 err
= reg_openkey(ctx
, key
, path
, desired_access
, pkey
);
614 if (W_ERROR_IS_OK(err
)) {
615 if (paction
!= NULL
) {
616 *paction
= REG_OPENED_EXISTING_KEY
;
621 if (!W_ERROR_EQUAL(err
, WERR_BADFILE
)) {
623 * Something but "notfound" has happened, so bail out
629 * We may (e.g. in the iteration) have opened the key with ENUM_SUBKEY.
630 * Instead of re-opening the key with CREATE_SUB_KEY, we simply
631 * duplicate the access check here and skip the expensive full open.
633 if (!regkey_access_check(key
->key
, KEY_CREATE_SUB_KEY
, &access_granted
,
636 err
= WERR_ACCESS_DENIED
;
641 * Actually create the subkey
644 err
= create_reg_subkey(key
->key
, path
);
645 if (!W_ERROR_IS_OK(err
)) {
650 * Now open the newly created key
653 err
= reg_openkey(ctx
, key
, path
, desired_access
, pkey
);
654 if (W_ERROR_IS_OK(err
) && (paction
!= NULL
)) {
655 *paction
= REG_CREATED_NEW_KEY
;
659 if (W_ERROR_IS_OK(err
)) {
660 err
= regdb_transaction_commit();
661 if (!W_ERROR_IS_OK(err
)) {
662 DEBUG(0, ("reg_createkey: Error committing transaction: %s\n", win_errstr(err
)));
665 WERROR err1
= regdb_transaction_cancel();
666 if (!W_ERROR_IS_OK(err1
)) {
667 DEBUG(0, ("reg_createkey: Error cancelling transaction: %s\n", win_errstr(err1
)));
672 TALLOC_FREE(mem_ctx
);
676 static WERROR
reg_deletekey_internal(TALLOC_CTX
*mem_ctx
,
677 struct registry_key
*parent
,
678 const char *path
, bool lazy
)
682 struct registry_key
*key
;
683 name
= talloc_strdup(mem_ctx
, path
);
689 /* no subkeys - proceed with delete */
690 end
= strrchr(name
, '\\');
694 err
= reg_openkey(mem_ctx
, parent
, name
,
695 KEY_CREATE_SUB_KEY
, &key
);
696 W_ERROR_NOT_OK_GOTO_DONE(err
);
702 if (name
[0] == '\0') {
703 err
= WERR_INVALID_PARAM
;
707 err
= delete_reg_subkey(parent
->key
, name
, lazy
);
713 WERROR
reg_deletekey(struct registry_key
*parent
, const char *path
)
716 struct registry_key
*key
;
717 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
719 /* check if the key has subkeys */
720 err
= reg_openkey(mem_ctx
, parent
, path
, REG_KEY_READ
, &key
);
721 W_ERROR_NOT_OK_GOTO_DONE(err
);
723 err
= regdb_transaction_start();
724 if (!W_ERROR_IS_OK(err
)) {
725 DEBUG(0, ("reg_deletekey: Error starting transaction: %s\n",
730 err
= fill_subkey_cache(key
);
731 if (!W_ERROR_IS_OK(err
)) {
735 if (regsubkey_ctr_numkeys(key
->subkeys
) > 0) {
736 err
= WERR_ACCESS_DENIED
;
739 err
= reg_deletekey_internal(mem_ctx
, parent
, path
, false);
742 if (W_ERROR_IS_OK(err
)) {
743 err
= regdb_transaction_commit();
744 if (!W_ERROR_IS_OK(err
)) {
745 DEBUG(0, ("reg_deletekey: Error committing transaction: %s\n", win_errstr(err
)));
748 WERROR err1
= regdb_transaction_cancel();
749 if (!W_ERROR_IS_OK(err1
)) {
750 DEBUG(0, ("reg_deletekey: Error cancelling transaction: %s\n", win_errstr(err1
)));
755 TALLOC_FREE(mem_ctx
);
760 WERROR
reg_setvalue(struct registry_key
*key
, const char *name
,
761 const struct registry_value
*val
)
763 struct regval_blob
*existing
;
767 if (!(key
->key
->access_granted
& KEY_SET_VALUE
)) {
768 return WERR_ACCESS_DENIED
;
771 err
= regdb_transaction_start();
772 if (!W_ERROR_IS_OK(err
)) {
773 DEBUG(0, ("reg_setvalue: Failed to start transaction: %s\n",
778 err
= fill_value_cache(key
);
779 if (!W_ERROR_IS_OK(err
)) {
780 DEBUG(0, ("reg_setvalue: Error filling value cache: %s\n", win_errstr(err
)));
784 existing
= regval_ctr_getvalue(key
->values
, name
);
786 if ((existing
!= NULL
) &&
787 (regval_size(existing
) == val
->data
.length
) &&
788 (memcmp(regval_data_p(existing
), val
->data
.data
,
789 val
->data
.length
) == 0))
795 res
= regval_ctr_addvalue(key
->values
, name
, val
->type
,
796 val
->data
.data
, val
->data
.length
);
799 TALLOC_FREE(key
->values
);
804 if (!store_reg_values(key
->key
, key
->values
)) {
805 TALLOC_FREE(key
->values
);
806 DEBUG(0, ("reg_setvalue: store_reg_values failed\n"));
807 err
= WERR_REG_IO_FAILURE
;
814 if (W_ERROR_IS_OK(err
)) {
815 err
= regdb_transaction_commit();
816 if (!W_ERROR_IS_OK(err
)) {
817 DEBUG(0, ("reg_setvalue: Error committing transaction: %s\n", win_errstr(err
)));
820 WERROR err1
= regdb_transaction_cancel();
821 if (!W_ERROR_IS_OK(err1
)) {
822 DEBUG(0, ("reg_setvalue: Error cancelling transaction: %s\n", win_errstr(err1
)));
829 static WERROR
reg_value_exists(struct registry_key
*key
, const char *name
)
831 struct regval_blob
*blob
;
833 blob
= regval_ctr_getvalue(key
->values
, name
);
842 WERROR
reg_deletevalue(struct registry_key
*key
, const char *name
)
846 if (!(key
->key
->access_granted
& KEY_SET_VALUE
)) {
847 return WERR_ACCESS_DENIED
;
850 err
= regdb_transaction_start();
851 if (!W_ERROR_IS_OK(err
)) {
852 DEBUG(0, ("reg_deletevalue: Failed to start transaction: %s\n",
857 err
= fill_value_cache(key
);
858 if (!W_ERROR_IS_OK(err
)) {
859 DEBUG(0, ("reg_deletevalue; Error filling value cache: %s\n",
864 err
= reg_value_exists(key
, name
);
865 if (!W_ERROR_IS_OK(err
)) {
869 regval_ctr_delvalue(key
->values
, name
);
871 if (!store_reg_values(key
->key
, key
->values
)) {
872 TALLOC_FREE(key
->values
);
873 err
= WERR_REG_IO_FAILURE
;
874 DEBUG(0, ("reg_deletevalue: store_reg_values failed\n"));
881 if (W_ERROR_IS_OK(err
)) {
882 err
= regdb_transaction_commit();
883 if (!W_ERROR_IS_OK(err
)) {
884 DEBUG(0, ("reg_deletevalue: Error committing transaction: %s\n", win_errstr(err
)));
887 WERROR err1
= regdb_transaction_cancel();
888 if (!W_ERROR_IS_OK(err1
)) {
889 DEBUG(0, ("reg_deletevalue: Error cancelling transaction: %s\n", win_errstr(err1
)));
896 WERROR
reg_getkeysecurity(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
897 struct security_descriptor
**psecdesc
)
899 return regkey_get_secdesc(mem_ctx
, key
->key
, psecdesc
);
902 WERROR
reg_setkeysecurity(struct registry_key
*key
,
903 struct security_descriptor
*psecdesc
)
905 return regkey_set_secdesc(key
->key
, psecdesc
);
908 WERROR
reg_getversion(uint32_t *version
)
910 if (version
== NULL
) {
911 return WERR_INVALID_PARAM
;
914 *version
= 0x00000005; /* Windows 2000 registry API version */
918 /**********************************************************************
919 * Higher level utility functions
920 **********************************************************************/
922 WERROR
reg_deleteallvalues(struct registry_key
*key
)
927 if (!(key
->key
->access_granted
& KEY_SET_VALUE
)) {
928 return WERR_ACCESS_DENIED
;
931 if (!W_ERROR_IS_OK(err
= fill_value_cache(key
))) {
935 for (i
=0; i
< regval_ctr_numvals(key
->values
); i
++) {
936 struct regval_blob
*blob
;
937 blob
= regval_ctr_specific_value(key
->values
, i
);
938 regval_ctr_delvalue(key
->values
, regval_name(blob
));
941 if (!store_reg_values(key
->key
, key
->values
)) {
942 TALLOC_FREE(key
->values
);
943 return WERR_REG_IO_FAILURE
;
950 * Utility function to delete a registry key with all its subkeys.
951 * Note that reg_deletekey returns ACCESS_DENIED when called on a
952 * key that has subkeys.
954 static WERROR
reg_deletekey_recursive_internal(struct registry_key
*parent
,
956 bool del_key
, bool lazy
)
958 WERROR werr
= WERR_OK
;
959 struct registry_key
*key
;
960 char *subkey_name
= NULL
;
962 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
964 DEBUG(5, ("reg_deletekey_recursive_internal: deleting '%s' from '%s'\n",
965 path
, parent
->key
->name
));
967 /* recurse through subkeys first */
968 werr
= reg_openkey(mem_ctx
, parent
, path
, REG_KEY_ALL
, &key
);
969 if (!W_ERROR_IS_OK(werr
)) {
970 DEBUG(3, ("reg_deletekey_recursive_internal: error opening "
971 "subkey '%s' of '%s': '%s'\n",
972 path
, parent
->key
->name
, win_errstr(werr
)));
976 werr
= fill_subkey_cache(key
);
977 W_ERROR_NOT_OK_GOTO_DONE(werr
);
980 * loop from top to bottom for perfomance:
981 * this way, we need to rehash the regsubkey containers less
983 for (i
= regsubkey_ctr_numkeys(key
->subkeys
) ; i
> 0; i
--) {
984 subkey_name
= regsubkey_ctr_specific_key(key
->subkeys
, i
-1);
985 werr
= reg_deletekey_recursive_internal(key
, subkey_name
, true, del_key
);
986 W_ERROR_NOT_OK_GOTO_DONE(werr
);
990 /* now delete the actual key */
991 werr
= reg_deletekey_internal(mem_ctx
, parent
, path
, lazy
);
996 DEBUG(5, ("reg_deletekey_recursive_internal: done deleting '%s' from "
998 path
, parent
->key
->name
, win_errstr(werr
)));
999 TALLOC_FREE(mem_ctx
);
1003 static WERROR
reg_deletekey_recursive_trans(struct registry_key
*parent
,
1009 werr
= regdb_transaction_start();
1010 if (!W_ERROR_IS_OK(werr
)) {
1011 DEBUG(0, ("reg_deletekey_recursive_trans: "
1012 "error starting transaction: %s\n",
1017 werr
= reg_deletekey_recursive_internal(parent
, path
, del_key
, false);
1019 if (!W_ERROR_IS_OK(werr
)) {
1021 DEBUG(W_ERROR_EQUAL(werr
, WERR_BADFILE
) ? 5 : 1,
1022 (__location__
": failed to delete key '%s' from key "
1023 "'%s': %s\n", path
, parent
->key
->name
,
1026 werr2
= regdb_transaction_cancel();
1027 if (!W_ERROR_IS_OK(werr2
)) {
1028 DEBUG(0, ("reg_deletekey_recursive_trans: "
1029 "error cancelling transaction: %s\n",
1030 win_errstr(werr2
)));
1032 * return the original werr or the
1033 * error from cancelling the transaction?
1037 werr
= regdb_transaction_commit();
1038 if (!W_ERROR_IS_OK(werr
)) {
1039 DEBUG(0, ("reg_deletekey_recursive_trans: "
1040 "error committing transaction: %s\n",
1043 DEBUG(5, ("reg_reletekey_recursive_trans: deleted key '%s' from '%s'\n",
1044 path
, parent
->key
->name
));
1052 WERROR
reg_deletekey_recursive(struct registry_key
*parent
,
1055 return reg_deletekey_recursive_trans(parent
, path
, true);
1058 WERROR
reg_deletesubkeys_recursive(struct registry_key
*parent
,
1061 return reg_deletekey_recursive_trans(parent
, path
, false);