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"
75 #define DBGC_CLASS DBGC_REGISTRY
78 /**********************************************************************
80 **********************************************************************/
82 static WERROR
fill_value_cache(struct registry_key
*key
)
86 if (key
->values
!= NULL
) {
87 if (!reg_values_need_update(key
->key
, key
->values
)) {
92 TALLOC_FREE(key
->values
);
93 werr
= regval_ctr_init(key
, &(key
->values
));
94 W_ERROR_NOT_OK_RETURN(werr
);
96 if (fetch_reg_values(key
->key
, key
->values
) == -1) {
97 TALLOC_FREE(key
->values
);
104 static WERROR
fill_subkey_cache(struct registry_key
*key
)
108 if (key
->subkeys
!= NULL
) {
109 if (!reg_subkeys_need_update(key
->key
, key
->subkeys
)) {
114 TALLOC_FREE(key
->subkeys
);
115 werr
= regsubkey_ctr_init(key
, &(key
->subkeys
));
116 W_ERROR_NOT_OK_RETURN(werr
);
118 if (fetch_reg_keys(key
->key
, key
->subkeys
) == -1) {
119 TALLOC_FREE(key
->subkeys
);
126 static int regkey_destructor(struct registry_key_handle
*key
)
128 return regdb_close();
131 static WERROR
regkey_open_onelevel(TALLOC_CTX
*mem_ctx
,
132 struct registry_key
*parent
,
134 const struct security_token
*token
,
135 uint32 access_desired
,
136 struct registry_key
**pregkey
)
138 WERROR result
= WERR_OK
;
139 struct registry_key
*regkey
;
140 struct registry_key_handle
*key
;
142 DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name
));
144 SMB_ASSERT(strchr(name
, '\\') == NULL
);
146 if (!(regkey
= talloc_zero(mem_ctx
, struct registry_key
)) ||
147 !(regkey
->token
= dup_nt_token(regkey
, token
)) ||
148 !(regkey
->key
= talloc_zero(regkey
, struct registry_key_handle
)))
154 result
= regdb_open();
155 if (!(W_ERROR_IS_OK(result
))) {
160 talloc_set_destructor(key
, regkey_destructor
);
164 key
->type
= REG_KEY_GENERIC
;
166 if (name
[0] == '\0') {
168 * Open a copy of the parent key
171 result
= WERR_BADFILE
;
174 key
->name
= talloc_strdup(key
, parent
->key
->name
);
180 key
->name
= talloc_asprintf(key
, "%s%s%s",
181 parent
? parent
->key
->name
: "",
186 if (key
->name
== NULL
) {
191 /* Tag this as a Performance Counter Key */
193 if( strncasecmp_m(key
->name
, KEY_HKPD
, strlen(KEY_HKPD
)) == 0 )
194 key
->type
= REG_KEY_HKPD
;
196 /* Look up the table of registry I/O operations */
198 key
->ops
= reghook_cache_find( key
->name
);
199 if (key
->ops
== NULL
) {
200 DEBUG(0,("reg_open_onelevel: Failed to assign "
201 "registry_ops to [%s]\n", key
->name
));
202 result
= WERR_BADFILE
;
206 /* FIXME: Existence is currently checked by fetching the subkeys */
208 result
= fill_subkey_cache(regkey
);
209 if (!W_ERROR_IS_OK(result
)) {
213 if ( !regkey_access_check( key
, access_desired
, &key
->access_granted
,
215 result
= WERR_ACCESS_DENIED
;
223 if ( !W_ERROR_IS_OK(result
) ) {
230 WERROR
reg_openhive(TALLOC_CTX
*mem_ctx
, const char *hive
,
231 uint32 desired_access
,
232 const struct security_token
*token
,
233 struct registry_key
**pkey
)
235 SMB_ASSERT(hive
!= NULL
);
236 SMB_ASSERT(hive
[0] != '\0');
237 SMB_ASSERT(strchr(hive
, '\\') == NULL
);
239 return regkey_open_onelevel(mem_ctx
, NULL
, hive
, token
, desired_access
,
244 /**********************************************************************
246 **********************************************************************/
248 WERROR
reg_openkey(TALLOC_CTX
*mem_ctx
, struct registry_key
*parent
,
249 const char *name
, uint32 desired_access
,
250 struct registry_key
**pkey
)
252 struct registry_key
*direct_parent
= parent
;
256 TALLOC_CTX
*frame
= talloc_stackframe();
258 path
= talloc_strdup(frame
, name
);
266 if ((len
> 0) && (path
[len
-1] == '\\')) {
270 while ((p
= strchr(path
, '\\')) != NULL
) {
271 char *name_component
;
272 struct registry_key
*tmp
;
274 name_component
= talloc_strndup(frame
, path
, (p
- path
));
275 if (name_component
== NULL
) {
280 err
= regkey_open_onelevel(frame
, direct_parent
,
281 name_component
, parent
->token
,
282 KEY_ENUMERATE_SUB_KEYS
, &tmp
);
284 if (!W_ERROR_IS_OK(err
)) {
292 err
= regkey_open_onelevel(mem_ctx
, direct_parent
, path
, parent
->token
,
293 desired_access
, pkey
);
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
;
334 struct regval_blob
*blob
;
337 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
338 return WERR_ACCESS_DENIED
;
341 if (!(W_ERROR_IS_OK(err
= fill_value_cache(key
)))) {
345 if (idx
>= regval_ctr_numvals(key
->values
)) {
346 return WERR_NO_MORE_ITEMS
;
349 blob
= regval_ctr_specific_value(key
->values
, idx
);
351 val
= talloc_zero(mem_ctx
, struct registry_value
);
356 val
->type
= regval_type(blob
);
357 val
->data
= data_blob_talloc(mem_ctx
, regval_data_p(blob
), regval_size(blob
));
360 && !(*pname
= talloc_strdup(
361 mem_ctx
, regval_name(blob
)))) {
370 static WERROR
reg_enumvalue_nocachefill(TALLOC_CTX
*mem_ctx
,
371 struct registry_key
*key
,
372 uint32 idx
, char **pname
,
373 struct registry_value
**pval
)
375 struct registry_value
*val
;
376 struct regval_blob
*blob
;
378 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
379 return WERR_ACCESS_DENIED
;
382 if (idx
>= regval_ctr_numvals(key
->values
)) {
383 return WERR_NO_MORE_ITEMS
;
386 blob
= regval_ctr_specific_value(key
->values
, idx
);
388 val
= talloc_zero(mem_ctx
, struct registry_value
);
393 val
->type
= regval_type(blob
);
394 val
->data
= data_blob_talloc(mem_ctx
, regval_data_p(blob
), regval_size(blob
));
397 && !(*pname
= talloc_strdup(
398 mem_ctx
, regval_name(blob
)))) {
407 WERROR
reg_queryvalue(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
408 const char *name
, struct registry_value
**pval
)
413 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
414 return WERR_ACCESS_DENIED
;
417 if (!(W_ERROR_IS_OK(err
= fill_value_cache(key
)))) {
421 for (i
=0; i
< regval_ctr_numvals(key
->values
); i
++) {
422 struct regval_blob
*blob
;
423 blob
= regval_ctr_specific_value(key
->values
, i
);
424 if (strequal(regval_name(blob
), name
)) {
426 * don't use reg_enumvalue here:
427 * re-reading the values from the disk
428 * would change the indexing and break
431 return reg_enumvalue_nocachefill(mem_ctx
, key
, i
,
439 WERROR
reg_querymultiplevalues(TALLOC_CTX
*mem_ctx
,
440 struct registry_key
*key
,
444 struct registry_value
**pvals
)
447 uint32_t i
, n
, found
= 0;
448 struct registry_value
*vals
;
450 if (num_names
== 0) {
454 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
455 return WERR_ACCESS_DENIED
;
458 if (!(W_ERROR_IS_OK(err
= fill_value_cache(key
)))) {
462 vals
= talloc_zero_array(mem_ctx
, struct registry_value
, num_names
);
467 for (n
=0; n
< num_names
; n
++) {
468 for (i
=0; i
< regval_ctr_numvals(key
->values
); i
++) {
469 struct regval_blob
*blob
;
470 blob
= regval_ctr_specific_value(key
->values
, i
);
471 if (strequal(regval_name(blob
), names
[n
])) {
472 struct registry_value
*v
;
473 err
= reg_enumvalue(mem_ctx
, key
, i
, NULL
, &v
);
474 if (!W_ERROR_IS_OK(err
)) {
489 WERROR
reg_queryinfokey(struct registry_key
*key
, uint32_t *num_subkeys
,
490 uint32_t *max_subkeylen
, uint32_t *max_subkeysize
,
491 uint32_t *num_values
, uint32_t *max_valnamelen
,
492 uint32_t *max_valbufsize
, uint32_t *secdescsize
,
493 NTTIME
*last_changed_time
)
499 struct security_descriptor
*secdesc
;
501 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
502 return WERR_ACCESS_DENIED
;
505 if (!W_ERROR_IS_OK(fill_subkey_cache(key
)) ||
506 !W_ERROR_IS_OK(fill_value_cache(key
))) {
511 for (i
=0; i
< regsubkey_ctr_numkeys(key
->subkeys
); i
++) {
512 max_len
= MAX(max_len
,
513 strlen(regsubkey_ctr_specific_key(key
->subkeys
, i
)));
516 *num_subkeys
= regsubkey_ctr_numkeys(key
->subkeys
);
517 *max_subkeylen
= max_len
;
518 *max_subkeysize
= 0; /* Class length? */
522 for (i
=0; i
< regval_ctr_numvals(key
->values
); i
++) {
523 struct regval_blob
*blob
;
524 blob
= regval_ctr_specific_value(key
->values
, i
);
525 max_len
= MAX(max_len
, strlen(regval_name(blob
)));
526 max_size
= MAX(max_size
, regval_size(blob
));
529 *num_values
= regval_ctr_numvals(key
->values
);
530 *max_valnamelen
= max_len
;
531 *max_valbufsize
= max_size
;
533 if (!(mem_ctx
= talloc_new(key
))) {
537 err
= regkey_get_secdesc(mem_ctx
, key
->key
, &secdesc
);
538 if (!W_ERROR_IS_OK(err
)) {
539 TALLOC_FREE(mem_ctx
);
543 *secdescsize
= ndr_size_security_descriptor(secdesc
, 0);
544 TALLOC_FREE(mem_ctx
);
546 *last_changed_time
= 0;
551 WERROR
reg_createkey(TALLOC_CTX
*ctx
, struct registry_key
*parent
,
552 const char *subkeypath
, uint32 desired_access
,
553 struct registry_key
**pkey
,
554 enum winreg_CreateAction
*paction
)
556 struct registry_key
*key
= parent
;
560 uint32_t access_granted
;
562 mem_ctx
= talloc_new(ctx
);
563 if (mem_ctx
== NULL
) {
567 path
= talloc_strdup(mem_ctx
, subkeypath
);
573 err
= regdb_transaction_start();
574 if (!W_ERROR_IS_OK(err
)) {
575 DEBUG(0, ("reg_createkey: failed to start transaction: %s\n",
580 while ((end
= strchr(path
, '\\')) != NULL
) {
581 struct registry_key
*tmp
;
582 enum winreg_CreateAction action
;
586 err
= reg_createkey(mem_ctx
, key
, path
,
587 KEY_ENUMERATE_SUB_KEYS
, &tmp
, &action
);
588 if (!W_ERROR_IS_OK(err
)) {
601 * At this point, "path" contains the one-element subkey of "key". We
602 * can try to open it.
605 err
= reg_openkey(ctx
, key
, path
, desired_access
, pkey
);
606 if (W_ERROR_IS_OK(err
)) {
607 if (paction
!= NULL
) {
608 *paction
= REG_OPENED_EXISTING_KEY
;
613 if (!W_ERROR_EQUAL(err
, WERR_BADFILE
)) {
615 * Something but "notfound" has happened, so bail out
621 * We may (e.g. in the iteration) have opened the key with ENUM_SUBKEY.
622 * Instead of re-opening the key with CREATE_SUB_KEY, we simply
623 * duplicate the access check here and skip the expensive full open.
625 if (!regkey_access_check(key
->key
, KEY_CREATE_SUB_KEY
, &access_granted
,
628 err
= WERR_ACCESS_DENIED
;
633 * Actually create the subkey
636 err
= create_reg_subkey(key
->key
, path
);
637 if (!W_ERROR_IS_OK(err
)) {
642 * Now open the newly created key
645 err
= reg_openkey(ctx
, key
, path
, desired_access
, pkey
);
646 if (W_ERROR_IS_OK(err
) && (paction
!= NULL
)) {
647 *paction
= REG_CREATED_NEW_KEY
;
651 if (W_ERROR_IS_OK(err
)) {
652 err
= regdb_transaction_commit();
653 if (!W_ERROR_IS_OK(err
)) {
654 DEBUG(0, ("reg_createkey: Error committing transaction: %s\n", win_errstr(err
)));
657 WERROR err1
= regdb_transaction_cancel();
658 if (!W_ERROR_IS_OK(err1
)) {
659 DEBUG(0, ("reg_createkey: Error cancelling transaction: %s\n", win_errstr(err1
)));
664 TALLOC_FREE(mem_ctx
);
668 static WERROR
reg_deletekey_internal(TALLOC_CTX
*mem_ctx
,
669 struct registry_key
*parent
,
670 const char *path
, bool lazy
)
674 struct registry_key
*key
;
675 name
= talloc_strdup(mem_ctx
, path
);
681 /* no subkeys - proceed with delete */
682 end
= strrchr(name
, '\\');
686 err
= reg_openkey(mem_ctx
, parent
, name
,
687 KEY_CREATE_SUB_KEY
, &key
);
688 W_ERROR_NOT_OK_GOTO_DONE(err
);
694 if (name
[0] == '\0') {
695 err
= WERR_INVALID_PARAM
;
699 err
= delete_reg_subkey(parent
->key
, name
, lazy
);
705 WERROR
reg_deletekey(struct registry_key
*parent
, const char *path
)
708 struct registry_key
*key
;
709 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
711 /* check if the key has subkeys */
712 err
= reg_openkey(mem_ctx
, parent
, path
, REG_KEY_READ
, &key
);
713 W_ERROR_NOT_OK_GOTO_DONE(err
);
715 err
= regdb_transaction_start();
716 if (!W_ERROR_IS_OK(err
)) {
717 DEBUG(0, ("reg_deletekey: Error starting transaction: %s\n",
722 err
= fill_subkey_cache(key
);
723 if (!W_ERROR_IS_OK(err
)) {
727 if (regsubkey_ctr_numkeys(key
->subkeys
) > 0) {
728 err
= WERR_ACCESS_DENIED
;
731 err
= reg_deletekey_internal(mem_ctx
, parent
, path
, false);
734 if (W_ERROR_IS_OK(err
)) {
735 err
= regdb_transaction_commit();
736 if (!W_ERROR_IS_OK(err
)) {
737 DEBUG(0, ("reg_deletekey: Error committing transaction: %s\n", win_errstr(err
)));
740 WERROR err1
= regdb_transaction_cancel();
741 if (!W_ERROR_IS_OK(err1
)) {
742 DEBUG(0, ("reg_deletekey: Error cancelling transaction: %s\n", win_errstr(err1
)));
747 TALLOC_FREE(mem_ctx
);
752 WERROR
reg_setvalue(struct registry_key
*key
, const char *name
,
753 const struct registry_value
*val
)
755 struct regval_blob
*existing
;
759 if (!(key
->key
->access_granted
& KEY_SET_VALUE
)) {
760 return WERR_ACCESS_DENIED
;
763 err
= regdb_transaction_start();
764 if (!W_ERROR_IS_OK(err
)) {
765 DEBUG(0, ("reg_setvalue: Failed to start transaction: %s\n",
770 err
= fill_value_cache(key
);
771 if (!W_ERROR_IS_OK(err
)) {
772 DEBUG(0, ("reg_setvalue: Error filling value cache: %s\n", win_errstr(err
)));
776 existing
= regval_ctr_getvalue(key
->values
, name
);
778 if ((existing
!= NULL
) &&
779 (regval_size(existing
) == val
->data
.length
) &&
780 (memcmp(regval_data_p(existing
), val
->data
.data
,
781 val
->data
.length
) == 0))
787 res
= regval_ctr_addvalue(key
->values
, name
, val
->type
,
788 val
->data
.data
, val
->data
.length
);
791 TALLOC_FREE(key
->values
);
796 if (!store_reg_values(key
->key
, key
->values
)) {
797 TALLOC_FREE(key
->values
);
798 DEBUG(0, ("reg_setvalue: store_reg_values failed\n"));
799 err
= WERR_REG_IO_FAILURE
;
806 if (W_ERROR_IS_OK(err
)) {
807 err
= regdb_transaction_commit();
808 if (!W_ERROR_IS_OK(err
)) {
809 DEBUG(0, ("reg_setvalue: Error committing transaction: %s\n", win_errstr(err
)));
812 WERROR err1
= regdb_transaction_cancel();
813 if (!W_ERROR_IS_OK(err1
)) {
814 DEBUG(0, ("reg_setvalue: Error cancelling transaction: %s\n", win_errstr(err1
)));
821 static WERROR
reg_value_exists(struct registry_key
*key
, const char *name
)
823 struct regval_blob
*blob
;
825 blob
= regval_ctr_getvalue(key
->values
, name
);
834 WERROR
reg_deletevalue(struct registry_key
*key
, const char *name
)
838 if (!(key
->key
->access_granted
& KEY_SET_VALUE
)) {
839 return WERR_ACCESS_DENIED
;
842 err
= regdb_transaction_start();
843 if (!W_ERROR_IS_OK(err
)) {
844 DEBUG(0, ("reg_deletevalue: Failed to start transaction: %s\n",
849 err
= fill_value_cache(key
);
850 if (!W_ERROR_IS_OK(err
)) {
851 DEBUG(0, ("reg_deletevalue; Error filling value cache: %s\n",
856 err
= reg_value_exists(key
, name
);
857 if (!W_ERROR_IS_OK(err
)) {
861 regval_ctr_delvalue(key
->values
, name
);
863 if (!store_reg_values(key
->key
, key
->values
)) {
864 TALLOC_FREE(key
->values
);
865 err
= WERR_REG_IO_FAILURE
;
866 DEBUG(0, ("reg_deletevalue: store_reg_values failed\n"));
873 if (W_ERROR_IS_OK(err
)) {
874 err
= regdb_transaction_commit();
875 if (!W_ERROR_IS_OK(err
)) {
876 DEBUG(0, ("reg_deletevalue: Error committing transaction: %s\n", win_errstr(err
)));
879 WERROR err1
= regdb_transaction_cancel();
880 if (!W_ERROR_IS_OK(err1
)) {
881 DEBUG(0, ("reg_deletevalue: Error cancelling transaction: %s\n", win_errstr(err1
)));
888 WERROR
reg_getkeysecurity(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
889 struct security_descriptor
**psecdesc
)
891 return regkey_get_secdesc(mem_ctx
, key
->key
, psecdesc
);
894 WERROR
reg_setkeysecurity(struct registry_key
*key
,
895 struct security_descriptor
*psecdesc
)
897 return regkey_set_secdesc(key
->key
, psecdesc
);
900 WERROR
reg_getversion(uint32_t *version
)
902 if (version
== NULL
) {
903 return WERR_INVALID_PARAM
;
906 *version
= 0x00000005; /* Windows 2000 registry API version */
910 /**********************************************************************
911 * Higher level utility functions
912 **********************************************************************/
914 WERROR
reg_deleteallvalues(struct registry_key
*key
)
919 if (!(key
->key
->access_granted
& KEY_SET_VALUE
)) {
920 return WERR_ACCESS_DENIED
;
923 if (!W_ERROR_IS_OK(err
= fill_value_cache(key
))) {
927 for (i
=0; i
< regval_ctr_numvals(key
->values
); i
++) {
928 struct regval_blob
*blob
;
929 blob
= regval_ctr_specific_value(key
->values
, i
);
930 regval_ctr_delvalue(key
->values
, regval_name(blob
));
933 if (!store_reg_values(key
->key
, key
->values
)) {
934 TALLOC_FREE(key
->values
);
935 return WERR_REG_IO_FAILURE
;
942 * Utility function to delete a registry key with all its subkeys.
943 * Note that reg_deletekey returns ACCESS_DENIED when called on a
944 * key that has subkeys.
946 static WERROR
reg_deletekey_recursive_internal(struct registry_key
*parent
,
948 bool del_key
, bool lazy
)
950 WERROR werr
= WERR_OK
;
951 struct registry_key
*key
;
952 char *subkey_name
= NULL
;
954 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
956 DEBUG(5, ("reg_deletekey_recursive_internal: deleting '%s' from '%s'\n",
957 path
, parent
->key
->name
));
959 /* recurse through subkeys first */
960 werr
= reg_openkey(mem_ctx
, parent
, path
, REG_KEY_ALL
, &key
);
961 if (!W_ERROR_IS_OK(werr
)) {
962 DEBUG(3, ("reg_deletekey_recursive_internal: error opening "
963 "subkey '%s' of '%s': '%s'\n",
964 path
, parent
->key
->name
, win_errstr(werr
)));
968 werr
= fill_subkey_cache(key
);
969 W_ERROR_NOT_OK_GOTO_DONE(werr
);
972 * loop from top to bottom for perfomance:
973 * this way, we need to rehash the regsubkey containers less
975 for (i
= regsubkey_ctr_numkeys(key
->subkeys
) ; i
> 0; i
--) {
976 subkey_name
= regsubkey_ctr_specific_key(key
->subkeys
, i
-1);
977 werr
= reg_deletekey_recursive_internal(key
, subkey_name
, true, del_key
);
978 W_ERROR_NOT_OK_GOTO_DONE(werr
);
982 /* now delete the actual key */
983 werr
= reg_deletekey_internal(mem_ctx
, parent
, path
, lazy
);
988 DEBUG(5, ("reg_deletekey_recursive_internal: done deleting '%s' from "
990 path
, parent
->key
->name
, win_errstr(werr
)));
991 TALLOC_FREE(mem_ctx
);
995 static WERROR
reg_deletekey_recursive_trans(struct registry_key
*parent
,
1001 werr
= regdb_transaction_start();
1002 if (!W_ERROR_IS_OK(werr
)) {
1003 DEBUG(0, ("reg_deletekey_recursive_trans: "
1004 "error starting transaction: %s\n",
1009 werr
= reg_deletekey_recursive_internal(parent
, path
, del_key
, false);
1011 if (!W_ERROR_IS_OK(werr
)) {
1014 DEBUG(1, (__location__
" failed to delete key '%s' from key "
1015 "'%s': %s\n", path
, parent
->key
->name
,
1018 werr2
= regdb_transaction_cancel();
1019 if (!W_ERROR_IS_OK(werr2
)) {
1020 DEBUG(0, ("reg_deletekey_recursive_trans: "
1021 "error cancelling transaction: %s\n",
1022 win_errstr(werr2
)));
1024 * return the original werr or the
1025 * error from cancelling the transaction?
1029 werr
= regdb_transaction_commit();
1030 if (!W_ERROR_IS_OK(werr
)) {
1031 DEBUG(0, ("reg_deletekey_recursive_trans: "
1032 "error committing transaction: %s\n",
1035 DEBUG(5, ("reg_reletekey_recursive_trans: deleted key '%s' from '%s'\n",
1036 path
, parent
->key
->name
));
1044 WERROR
reg_deletekey_recursive(struct registry_key
*parent
,
1047 return reg_deletekey_recursive_trans(parent
, path
, true);
1050 WERROR
reg_deletesubkeys_recursive(struct registry_key
*parent
,
1053 return reg_deletekey_recursive_trans(parent
, path
, false);