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
);
120 return WERR_NO_MORE_ITEMS
;
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
;
141 struct regsubkey_ctr
*subkeys
= NULL
;
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 if ( !(key
->ops
= reghook_cache_find( key
->name
)) ) {
200 DEBUG(0,("reg_open_onelevel: Failed to assign "
201 "registry_ops to [%s]\n", key
->name
));
202 result
= WERR_BADFILE
;
206 /* check if the path really exists; failed is indicated by -1 */
207 /* if the subkey count failed, bail out */
209 result
= regsubkey_ctr_init(key
, &subkeys
);
210 if (!W_ERROR_IS_OK(result
)) {
214 if ( fetch_reg_keys( key
, subkeys
) == -1 ) {
215 result
= WERR_BADFILE
;
219 TALLOC_FREE( subkeys
);
221 if ( !regkey_access_check( key
, access_desired
, &key
->access_granted
,
223 result
= WERR_ACCESS_DENIED
;
231 if ( !W_ERROR_IS_OK(result
) ) {
238 WERROR
reg_openhive(TALLOC_CTX
*mem_ctx
, const char *hive
,
239 uint32 desired_access
,
240 const struct security_token
*token
,
241 struct registry_key
**pkey
)
243 SMB_ASSERT(hive
!= NULL
);
244 SMB_ASSERT(hive
[0] != '\0');
245 SMB_ASSERT(strchr(hive
, '\\') == NULL
);
247 return regkey_open_onelevel(mem_ctx
, NULL
, hive
, token
, desired_access
,
252 /**********************************************************************
254 **********************************************************************/
256 WERROR
reg_openkey(TALLOC_CTX
*mem_ctx
, struct registry_key
*parent
,
257 const char *name
, uint32 desired_access
,
258 struct registry_key
**pkey
)
260 struct registry_key
*direct_parent
= parent
;
262 char *p
, *path
, *to_free
;
265 if (!(path
= SMB_STRDUP(name
))) {
272 if ((len
> 0) && (path
[len
-1] == '\\')) {
276 while ((p
= strchr(path
, '\\')) != NULL
) {
277 char *name_component
;
278 struct registry_key
*tmp
;
280 if (!(name_component
= SMB_STRNDUP(path
, (p
- path
)))) {
285 err
= regkey_open_onelevel(mem_ctx
, direct_parent
,
286 name_component
, parent
->token
,
287 KEY_ENUMERATE_SUB_KEYS
, &tmp
);
288 SAFE_FREE(name_component
);
290 if (!W_ERROR_IS_OK(err
)) {
293 if (direct_parent
!= parent
) {
294 TALLOC_FREE(direct_parent
);
301 err
= regkey_open_onelevel(mem_ctx
, direct_parent
, path
, parent
->token
,
302 desired_access
, pkey
);
304 if (direct_parent
!= parent
) {
305 TALLOC_FREE(direct_parent
);
311 WERROR
reg_enumkey(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
312 uint32 idx
, char **name
, NTTIME
*last_write_time
)
316 if (!(key
->key
->access_granted
& KEY_ENUMERATE_SUB_KEYS
)) {
317 return WERR_ACCESS_DENIED
;
320 if (!W_ERROR_IS_OK(err
= fill_subkey_cache(key
))) {
324 if (idx
>= regsubkey_ctr_numkeys(key
->subkeys
)) {
325 return WERR_NO_MORE_ITEMS
;
328 if (!(*name
= talloc_strdup(mem_ctx
,
329 regsubkey_ctr_specific_key(key
->subkeys
, idx
))))
334 if (last_write_time
) {
335 *last_write_time
= 0;
341 WERROR
reg_enumvalue(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
342 uint32 idx
, char **pname
, struct registry_value
**pval
)
344 struct registry_value
*val
;
345 struct regval_blob
*blob
;
348 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
349 return WERR_ACCESS_DENIED
;
352 if (!(W_ERROR_IS_OK(err
= fill_value_cache(key
)))) {
356 if (idx
>= regval_ctr_numvals(key
->values
)) {
357 return WERR_NO_MORE_ITEMS
;
360 blob
= regval_ctr_specific_value(key
->values
, idx
);
362 val
= talloc_zero(mem_ctx
, struct registry_value
);
367 val
->type
= regval_type(blob
);
368 val
->data
= data_blob_talloc(mem_ctx
, regval_data_p(blob
), regval_size(blob
));
371 && !(*pname
= talloc_strdup(
372 mem_ctx
, regval_name(blob
)))) {
381 static WERROR
reg_enumvalue_nocachefill(TALLOC_CTX
*mem_ctx
,
382 struct registry_key
*key
,
383 uint32 idx
, char **pname
,
384 struct registry_value
**pval
)
386 struct registry_value
*val
;
387 struct regval_blob
*blob
;
389 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
390 return WERR_ACCESS_DENIED
;
393 if (idx
>= regval_ctr_numvals(key
->values
)) {
394 return WERR_NO_MORE_ITEMS
;
397 blob
= regval_ctr_specific_value(key
->values
, idx
);
399 val
= talloc_zero(mem_ctx
, struct registry_value
);
404 val
->type
= regval_type(blob
);
405 val
->data
= data_blob_talloc(mem_ctx
, regval_data_p(blob
), regval_size(blob
));
408 && !(*pname
= talloc_strdup(
409 mem_ctx
, regval_name(blob
)))) {
418 WERROR
reg_queryvalue(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
419 const char *name
, struct registry_value
**pval
)
424 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
425 return WERR_ACCESS_DENIED
;
428 if (!(W_ERROR_IS_OK(err
= fill_value_cache(key
)))) {
432 for (i
=0; i
< regval_ctr_numvals(key
->values
); i
++) {
433 struct regval_blob
*blob
;
434 blob
= regval_ctr_specific_value(key
->values
, i
);
435 if (strequal(regval_name(blob
), name
)) {
437 * don't use reg_enumvalue here:
438 * re-reading the values from the disk
439 * would change the indexing and break
442 return reg_enumvalue_nocachefill(mem_ctx
, key
, i
,
450 WERROR
reg_querymultiplevalues(TALLOC_CTX
*mem_ctx
,
451 struct registry_key
*key
,
455 struct registry_value
**pvals
)
458 uint32_t i
, n
, found
= 0;
459 struct registry_value
*vals
;
461 if (num_names
== 0) {
465 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
466 return WERR_ACCESS_DENIED
;
469 if (!(W_ERROR_IS_OK(err
= fill_value_cache(key
)))) {
473 vals
= talloc_zero_array(mem_ctx
, struct registry_value
, num_names
);
478 for (n
=0; n
< num_names
; n
++) {
479 for (i
=0; i
< regval_ctr_numvals(key
->values
); i
++) {
480 struct regval_blob
*blob
;
481 blob
= regval_ctr_specific_value(key
->values
, i
);
482 if (strequal(regval_name(blob
), names
[n
])) {
483 struct registry_value
*v
;
484 err
= reg_enumvalue(mem_ctx
, key
, i
, NULL
, &v
);
485 if (!W_ERROR_IS_OK(err
)) {
500 WERROR
reg_queryinfokey(struct registry_key
*key
, uint32_t *num_subkeys
,
501 uint32_t *max_subkeylen
, uint32_t *max_subkeysize
,
502 uint32_t *num_values
, uint32_t *max_valnamelen
,
503 uint32_t *max_valbufsize
, uint32_t *secdescsize
,
504 NTTIME
*last_changed_time
)
510 struct security_descriptor
*secdesc
;
512 if (!(key
->key
->access_granted
& KEY_QUERY_VALUE
)) {
513 return WERR_ACCESS_DENIED
;
516 if (!W_ERROR_IS_OK(fill_subkey_cache(key
)) ||
517 !W_ERROR_IS_OK(fill_value_cache(key
))) {
522 for (i
=0; i
< regsubkey_ctr_numkeys(key
->subkeys
); i
++) {
523 max_len
= MAX(max_len
,
524 strlen(regsubkey_ctr_specific_key(key
->subkeys
, i
)));
527 *num_subkeys
= regsubkey_ctr_numkeys(key
->subkeys
);
528 *max_subkeylen
= max_len
;
529 *max_subkeysize
= 0; /* Class length? */
533 for (i
=0; i
< regval_ctr_numvals(key
->values
); i
++) {
534 struct regval_blob
*blob
;
535 blob
= regval_ctr_specific_value(key
->values
, i
);
536 max_len
= MAX(max_len
, strlen(regval_name(blob
)));
537 max_size
= MAX(max_size
, regval_size(blob
));
540 *num_values
= regval_ctr_numvals(key
->values
);
541 *max_valnamelen
= max_len
;
542 *max_valbufsize
= max_size
;
544 if (!(mem_ctx
= talloc_new(key
))) {
548 err
= regkey_get_secdesc(mem_ctx
, key
->key
, &secdesc
);
549 if (!W_ERROR_IS_OK(err
)) {
550 TALLOC_FREE(mem_ctx
);
554 *secdescsize
= ndr_size_security_descriptor(secdesc
, 0);
555 TALLOC_FREE(mem_ctx
);
557 *last_changed_time
= 0;
562 WERROR
reg_createkey(TALLOC_CTX
*ctx
, struct registry_key
*parent
,
563 const char *subkeypath
, uint32 desired_access
,
564 struct registry_key
**pkey
,
565 enum winreg_CreateAction
*paction
)
567 struct registry_key
*key
= parent
;
568 struct registry_key
*create_parent
;
573 mem_ctx
= talloc_new(ctx
);
574 if (mem_ctx
== NULL
) {
578 path
= talloc_strdup(mem_ctx
, subkeypath
);
584 err
= regdb_transaction_start();
585 if (!W_ERROR_IS_OK(err
)) {
586 DEBUG(0, ("reg_createkey: failed to start transaction: %s\n",
591 while ((end
= strchr(path
, '\\')) != NULL
) {
592 struct registry_key
*tmp
;
593 enum winreg_CreateAction action
;
597 err
= reg_createkey(mem_ctx
, key
, path
,
598 KEY_ENUMERATE_SUB_KEYS
, &tmp
, &action
);
599 if (!W_ERROR_IS_OK(err
)) {
612 * At this point, "path" contains the one-element subkey of "key". We
613 * can try to open it.
616 err
= reg_openkey(ctx
, key
, path
, desired_access
, pkey
);
617 if (W_ERROR_IS_OK(err
)) {
618 if (paction
!= NULL
) {
619 *paction
= REG_OPENED_EXISTING_KEY
;
624 if (!W_ERROR_EQUAL(err
, WERR_BADFILE
)) {
626 * Something but "notfound" has happened, so bail out
632 * We have to make a copy of the current key, as we opened it only
633 * with ENUM_SUBKEY access.
636 err
= reg_openkey(mem_ctx
, key
, "", KEY_CREATE_SUB_KEY
,
638 if (!W_ERROR_IS_OK(err
)) {
643 * Actually create the subkey
646 err
= fill_subkey_cache(create_parent
);
647 if (!W_ERROR_IS_OK(err
)) {
651 err
= create_reg_subkey(key
->key
, path
);
652 if (!W_ERROR_IS_OK(err
)) {
657 * Now open the newly created key
660 err
= reg_openkey(ctx
, create_parent
, path
, desired_access
, pkey
);
661 if (W_ERROR_IS_OK(err
) && (paction
!= NULL
)) {
662 *paction
= REG_CREATED_NEW_KEY
;
666 if (W_ERROR_IS_OK(err
)) {
667 err
= regdb_transaction_commit();
668 if (!W_ERROR_IS_OK(err
)) {
669 DEBUG(0, ("reg_createkey: Error committing transaction: %s\n", win_errstr(err
)));
672 WERROR err1
= regdb_transaction_cancel();
673 if (!W_ERROR_IS_OK(err1
)) {
674 DEBUG(0, ("reg_createkey: Error cancelling transaction: %s\n", win_errstr(err1
)));
679 TALLOC_FREE(mem_ctx
);
683 static WERROR
reg_deletekey_internal(TALLOC_CTX
*mem_ctx
,
684 struct registry_key
*parent
,
685 const char *path
, bool lazy
)
689 struct registry_key
*key
;
690 name
= talloc_strdup(mem_ctx
, path
);
696 /* no subkeys - proceed with delete */
697 end
= strrchr(name
, '\\');
701 err
= reg_openkey(mem_ctx
, parent
, name
,
702 KEY_CREATE_SUB_KEY
, &key
);
703 W_ERROR_NOT_OK_GOTO_DONE(err
);
709 if (name
[0] == '\0') {
710 err
= WERR_INVALID_PARAM
;
714 err
= delete_reg_subkey(parent
->key
, name
, lazy
);
720 WERROR
reg_deletekey(struct registry_key
*parent
, const char *path
)
723 struct registry_key
*key
;
724 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
726 /* check if the key has subkeys */
727 err
= reg_openkey(mem_ctx
, parent
, path
, REG_KEY_READ
, &key
);
728 W_ERROR_NOT_OK_GOTO_DONE(err
);
730 err
= regdb_transaction_start();
731 if (!W_ERROR_IS_OK(err
)) {
732 DEBUG(0, ("reg_deletekey: Error starting transaction: %s\n",
737 err
= fill_subkey_cache(key
);
738 if (!W_ERROR_IS_OK(err
)) {
742 if (regsubkey_ctr_numkeys(key
->subkeys
) > 0) {
743 err
= WERR_ACCESS_DENIED
;
746 err
= reg_deletekey_internal(mem_ctx
, parent
, path
, false);
749 if (W_ERROR_IS_OK(err
)) {
750 err
= regdb_transaction_commit();
751 if (!W_ERROR_IS_OK(err
)) {
752 DEBUG(0, ("reg_deletekey: Error committing transaction: %s\n", win_errstr(err
)));
755 WERROR err1
= regdb_transaction_cancel();
756 if (!W_ERROR_IS_OK(err1
)) {
757 DEBUG(0, ("reg_deletekey: Error cancelling transaction: %s\n", win_errstr(err1
)));
762 TALLOC_FREE(mem_ctx
);
767 WERROR
reg_setvalue(struct registry_key
*key
, const char *name
,
768 const struct registry_value
*val
)
770 struct regval_blob
*existing
;
774 if (!(key
->key
->access_granted
& KEY_SET_VALUE
)) {
775 return WERR_ACCESS_DENIED
;
778 err
= regdb_transaction_start();
779 if (!W_ERROR_IS_OK(err
)) {
780 DEBUG(0, ("reg_setvalue: Failed to start transaction: %s\n",
785 err
= fill_value_cache(key
);
786 if (!W_ERROR_IS_OK(err
)) {
787 DEBUG(0, ("reg_setvalue: Error filling value cache: %s\n", win_errstr(err
)));
791 existing
= regval_ctr_getvalue(key
->values
, name
);
793 if ((existing
!= NULL
) &&
794 (regval_size(existing
) == val
->data
.length
) &&
795 (memcmp(regval_data_p(existing
), val
->data
.data
,
796 val
->data
.length
) == 0))
802 res
= regval_ctr_addvalue(key
->values
, name
, val
->type
,
803 val
->data
.data
, val
->data
.length
);
806 TALLOC_FREE(key
->values
);
811 if (!store_reg_values(key
->key
, key
->values
)) {
812 TALLOC_FREE(key
->values
);
813 DEBUG(0, ("reg_setvalue: store_reg_values failed\n"));
814 err
= WERR_REG_IO_FAILURE
;
821 if (W_ERROR_IS_OK(err
)) {
822 err
= regdb_transaction_commit();
823 if (!W_ERROR_IS_OK(err
)) {
824 DEBUG(0, ("reg_setvalue: Error committing transaction: %s\n", win_errstr(err
)));
827 WERROR err1
= regdb_transaction_cancel();
828 if (!W_ERROR_IS_OK(err1
)) {
829 DEBUG(0, ("reg_setvalue: Error cancelling transaction: %s\n", win_errstr(err1
)));
836 static WERROR
reg_value_exists(struct registry_key
*key
, const char *name
)
838 struct regval_blob
*blob
;
840 blob
= regval_ctr_getvalue(key
->values
, name
);
849 WERROR
reg_deletevalue(struct registry_key
*key
, const char *name
)
853 if (!(key
->key
->access_granted
& KEY_SET_VALUE
)) {
854 return WERR_ACCESS_DENIED
;
857 err
= regdb_transaction_start();
858 if (!W_ERROR_IS_OK(err
)) {
859 DEBUG(0, ("reg_deletevalue: Failed to start transaction: %s\n",
864 err
= fill_value_cache(key
);
865 if (!W_ERROR_IS_OK(err
)) {
866 DEBUG(0, ("reg_deletevalue; Error filling value cache: %s\n",
871 err
= reg_value_exists(key
, name
);
872 if (!W_ERROR_IS_OK(err
)) {
876 regval_ctr_delvalue(key
->values
, name
);
878 if (!store_reg_values(key
->key
, key
->values
)) {
879 TALLOC_FREE(key
->values
);
880 err
= WERR_REG_IO_FAILURE
;
881 DEBUG(0, ("reg_deletevalue: store_reg_values failed\n"));
888 if (W_ERROR_IS_OK(err
)) {
889 err
= regdb_transaction_commit();
890 if (!W_ERROR_IS_OK(err
)) {
891 DEBUG(0, ("reg_deletevalue: Error committing transaction: %s\n", win_errstr(err
)));
894 WERROR err1
= regdb_transaction_cancel();
895 if (!W_ERROR_IS_OK(err1
)) {
896 DEBUG(0, ("reg_deletevalue: Error cancelling transaction: %s\n", win_errstr(err1
)));
903 WERROR
reg_getkeysecurity(TALLOC_CTX
*mem_ctx
, struct registry_key
*key
,
904 struct security_descriptor
**psecdesc
)
906 return regkey_get_secdesc(mem_ctx
, key
->key
, psecdesc
);
909 WERROR
reg_setkeysecurity(struct registry_key
*key
,
910 struct security_descriptor
*psecdesc
)
912 return regkey_set_secdesc(key
->key
, psecdesc
);
915 WERROR
reg_getversion(uint32_t *version
)
917 if (version
== NULL
) {
918 return WERR_INVALID_PARAM
;
921 *version
= 0x00000005; /* Windows 2000 registry API version */
925 /**********************************************************************
926 * Higher level utility functions
927 **********************************************************************/
929 WERROR
reg_deleteallvalues(struct registry_key
*key
)
934 if (!(key
->key
->access_granted
& KEY_SET_VALUE
)) {
935 return WERR_ACCESS_DENIED
;
938 if (!W_ERROR_IS_OK(err
= fill_value_cache(key
))) {
942 for (i
=0; i
< regval_ctr_numvals(key
->values
); i
++) {
943 struct regval_blob
*blob
;
944 blob
= regval_ctr_specific_value(key
->values
, i
);
945 regval_ctr_delvalue(key
->values
, regval_name(blob
));
948 if (!store_reg_values(key
->key
, key
->values
)) {
949 TALLOC_FREE(key
->values
);
950 return WERR_REG_IO_FAILURE
;
957 * Utility function to delete a registry key with all its subkeys.
958 * Note that reg_deletekey returns ACCESS_DENIED when called on a
959 * key that has subkeys.
961 static WERROR
reg_deletekey_recursive_internal(struct registry_key
*parent
,
963 bool del_key
, bool lazy
)
965 WERROR werr
= WERR_OK
;
966 struct registry_key
*key
;
967 char *subkey_name
= NULL
;
969 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
971 DEBUG(5, ("reg_deletekey_recursive_internal: deleting '%s' from '%s'\n",
972 path
, parent
->key
->name
));
974 /* recurse through subkeys first */
975 werr
= reg_openkey(mem_ctx
, parent
, path
, REG_KEY_ALL
, &key
);
976 if (!W_ERROR_IS_OK(werr
)) {
977 DEBUG(3, ("reg_deletekey_recursive_internal: error opening "
978 "subkey '%s' of '%s': '%s'\n",
979 path
, parent
->key
->name
, win_errstr(werr
)));
983 werr
= fill_subkey_cache(key
);
984 W_ERROR_NOT_OK_GOTO_DONE(werr
);
987 * loop from top to bottom for perfomance:
988 * this way, we need to rehash the regsubkey containers less
990 for (i
= regsubkey_ctr_numkeys(key
->subkeys
) ; i
> 0; i
--) {
991 subkey_name
= regsubkey_ctr_specific_key(key
->subkeys
, i
-1);
992 werr
= reg_deletekey_recursive_internal(key
, subkey_name
, true, del_key
);
993 W_ERROR_NOT_OK_GOTO_DONE(werr
);
997 /* now delete the actual key */
998 werr
= reg_deletekey_internal(mem_ctx
, parent
, path
, lazy
);
1003 DEBUG(5, ("reg_deletekey_recursive_internal: done deleting '%s' from "
1005 path
, parent
->key
->name
, win_errstr(werr
)));
1006 TALLOC_FREE(mem_ctx
);
1010 static WERROR
reg_deletekey_recursive_trans(struct registry_key
*parent
,
1016 werr
= regdb_transaction_start();
1017 if (!W_ERROR_IS_OK(werr
)) {
1018 DEBUG(0, ("reg_deletekey_recursive_trans: "
1019 "error starting transaction: %s\n",
1024 werr
= reg_deletekey_recursive_internal(parent
, path
, del_key
, false);
1026 if (!W_ERROR_IS_OK(werr
)) {
1029 DEBUG(1, (__location__
" failed to delete key '%s' from key "
1030 "'%s': %s\n", path
, parent
->key
->name
,
1033 werr2
= regdb_transaction_cancel();
1034 if (!W_ERROR_IS_OK(werr2
)) {
1035 DEBUG(0, ("reg_deletekey_recursive_trans: "
1036 "error cancelling transaction: %s\n",
1037 win_errstr(werr2
)));
1039 * return the original werr or the
1040 * error from cancelling the transaction?
1044 werr
= regdb_transaction_commit();
1045 if (!W_ERROR_IS_OK(werr
)) {
1046 DEBUG(0, ("reg_deletekey_recursive_trans: "
1047 "error committing transaction: %s\n",
1050 DEBUG(5, ("reg_reletekey_recursive_trans: deleted key '%s' from '%s'\n",
1051 path
, parent
->key
->name
));
1059 WERROR
reg_deletekey_recursive(struct registry_key
*parent
,
1062 return reg_deletekey_recursive_trans(parent
, path
, true);
1065 WERROR
reg_deletesubkeys_recursive(struct registry_key
*parent
,
1068 return reg_deletekey_recursive_trans(parent
, path
, false);