s3:registry: fix race in reg_setvalue that could lead to data corruption
[Samba.git] / source3 / registry / reg_api.c
blobbd3cabb86e5f803013e9bda76c38aab3e3bd29b8
1 /*
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
25 * provided here:
27 * 0x00 winreg_OpenHKCR
28 * 0x01 winreg_OpenHKCU
29 * 0x02 winreg_OpenHKLM
30 * 0x03 winreg_OpenHKPD
31 * 0x04 winreg_OpenHKU
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
40 * 0x0d winreg_LoadKey
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
65 #include "includes.h"
66 #include "registry.h"
67 #include "reg_api.h"
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"
74 #undef DBGC_CLASS
75 #define DBGC_CLASS DBGC_REGISTRY
78 /**********************************************************************
79 * Helper functions
80 **********************************************************************/
82 static WERROR fill_value_cache(struct registry_key *key)
84 WERROR werr;
86 if (key->values != NULL) {
87 if (!reg_values_need_update(key->key, key->values)) {
88 return WERR_OK;
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);
98 return WERR_BADFILE;
101 return WERR_OK;
104 static WERROR fill_subkey_cache(struct registry_key *key)
106 WERROR werr;
108 if (key->subkeys != NULL) {
109 if (!reg_subkeys_need_update(key->key, key->subkeys)) {
110 return WERR_OK;
114 werr = regsubkey_ctr_init(key, &(key->subkeys));
115 W_ERROR_NOT_OK_RETURN(werr);
117 if (fetch_reg_keys(key->key, key->subkeys) == -1) {
118 TALLOC_FREE(key->subkeys);
119 return WERR_NO_MORE_ITEMS;
122 return WERR_OK;
125 static int regkey_destructor(struct registry_key_handle *key)
127 return regdb_close();
130 static WERROR regkey_open_onelevel(TALLOC_CTX *mem_ctx,
131 struct registry_key *parent,
132 const char *name,
133 const struct security_token *token,
134 uint32 access_desired,
135 struct registry_key **pregkey)
137 WERROR result = WERR_OK;
138 struct registry_key *regkey;
139 struct registry_key_handle *key;
140 struct regsubkey_ctr *subkeys = NULL;
142 DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name));
144 SMB_ASSERT(strchr(name, '\\') == NULL);
146 if (!(regkey = TALLOC_ZERO_P(mem_ctx, struct registry_key)) ||
147 !(regkey->token = dup_nt_token(regkey, token)) ||
148 !(regkey->key = TALLOC_ZERO_P(regkey, struct registry_key_handle)))
150 result = WERR_NOMEM;
151 goto done;
154 if ( !(W_ERROR_IS_OK(result = regdb_open())) ) {
155 goto done;
158 key = regkey->key;
159 talloc_set_destructor(key, regkey_destructor);
161 /* initialization */
163 key->type = REG_KEY_GENERIC;
165 if (name[0] == '\0') {
167 * Open a copy of the parent key
169 if (!parent) {
170 result = WERR_BADFILE;
171 goto done;
173 key->name = talloc_strdup(key, parent->key->name);
175 else {
177 * Normal subkey open
179 key->name = talloc_asprintf(key, "%s%s%s",
180 parent ? parent->key->name : "",
181 parent ? "\\": "",
182 name);
185 if (key->name == NULL) {
186 result = WERR_NOMEM;
187 goto done;
190 /* Tag this as a Performance Counter Key */
192 if( StrnCaseCmp(key->name, KEY_HKPD, strlen(KEY_HKPD)) == 0 )
193 key->type = REG_KEY_HKPD;
195 /* Look up the table of registry I/O operations */
197 if ( !(key->ops = reghook_cache_find( key->name )) ) {
198 DEBUG(0,("reg_open_onelevel: Failed to assign "
199 "registry_ops to [%s]\n", key->name ));
200 result = WERR_BADFILE;
201 goto done;
204 /* check if the path really exists; failed is indicated by -1 */
205 /* if the subkey count failed, bail out */
207 result = regsubkey_ctr_init(key, &subkeys);
208 if (!W_ERROR_IS_OK(result)) {
209 goto done;
212 if ( fetch_reg_keys( key, subkeys ) == -1 ) {
213 result = WERR_BADFILE;
214 goto done;
217 TALLOC_FREE( subkeys );
219 if ( !regkey_access_check( key, access_desired, &key->access_granted,
220 token ) ) {
221 result = WERR_ACCESS_DENIED;
222 goto done;
225 *pregkey = regkey;
226 result = WERR_OK;
228 done:
229 if ( !W_ERROR_IS_OK(result) ) {
230 TALLOC_FREE(regkey);
233 return result;
236 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
237 uint32 desired_access,
238 const struct security_token *token,
239 struct registry_key **pkey)
241 SMB_ASSERT(hive != NULL);
242 SMB_ASSERT(hive[0] != '\0');
243 SMB_ASSERT(strchr(hive, '\\') == NULL);
245 return regkey_open_onelevel(mem_ctx, NULL, hive, token, desired_access,
246 pkey);
250 /**********************************************************************
251 * The API functions
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;
259 WERROR err;
260 char *p, *path, *to_free;
261 size_t len;
263 if (!(path = SMB_STRDUP(name))) {
264 return WERR_NOMEM;
266 to_free = path;
268 len = strlen(path);
270 if ((len > 0) && (path[len-1] == '\\')) {
271 path[len-1] = '\0';
274 while ((p = strchr(path, '\\')) != NULL) {
275 char *name_component;
276 struct registry_key *tmp;
278 if (!(name_component = SMB_STRNDUP(path, (p - path)))) {
279 err = WERR_NOMEM;
280 goto error;
283 err = regkey_open_onelevel(mem_ctx, direct_parent,
284 name_component, parent->token,
285 KEY_ENUMERATE_SUB_KEYS, &tmp);
286 SAFE_FREE(name_component);
288 if (!W_ERROR_IS_OK(err)) {
289 goto error;
291 if (direct_parent != parent) {
292 TALLOC_FREE(direct_parent);
295 direct_parent = tmp;
296 path = p+1;
299 err = regkey_open_onelevel(mem_ctx, direct_parent, path, parent->token,
300 desired_access, pkey);
301 error:
302 if (direct_parent != parent) {
303 TALLOC_FREE(direct_parent);
305 SAFE_FREE(to_free);
306 return err;
309 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
310 uint32 idx, char **name, NTTIME *last_write_time)
312 WERROR err;
314 if (!(key->key->access_granted & KEY_ENUMERATE_SUB_KEYS)) {
315 return WERR_ACCESS_DENIED;
318 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
319 return err;
322 if (idx >= regsubkey_ctr_numkeys(key->subkeys)) {
323 return WERR_NO_MORE_ITEMS;
326 if (!(*name = talloc_strdup(mem_ctx,
327 regsubkey_ctr_specific_key(key->subkeys, idx))))
329 return WERR_NOMEM;
332 if (last_write_time) {
333 *last_write_time = 0;
336 return WERR_OK;
339 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
340 uint32 idx, char **pname, struct registry_value **pval)
342 struct registry_value *val;
343 struct regval_blob *blob;
344 WERROR err;
346 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
347 return WERR_ACCESS_DENIED;
350 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
351 return err;
354 if (idx >= regval_ctr_numvals(key->values)) {
355 return WERR_NO_MORE_ITEMS;
358 blob = regval_ctr_specific_value(key->values, idx);
360 val = talloc_zero(mem_ctx, struct registry_value);
361 if (val == NULL) {
362 return WERR_NOMEM;
365 val->type = regval_type(blob);
366 val->data = data_blob_talloc(mem_ctx, regval_data_p(blob), regval_size(blob));
368 if (pname
369 && !(*pname = talloc_strdup(
370 mem_ctx, regval_name(blob)))) {
371 TALLOC_FREE(val);
372 return WERR_NOMEM;
375 *pval = val;
376 return WERR_OK;
379 static WERROR reg_enumvalue_nocachefill(TALLOC_CTX *mem_ctx,
380 struct registry_key *key,
381 uint32 idx, char **pname,
382 struct registry_value **pval)
384 struct registry_value *val;
385 struct regval_blob *blob;
387 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
388 return WERR_ACCESS_DENIED;
391 if (idx >= regval_ctr_numvals(key->values)) {
392 return WERR_NO_MORE_ITEMS;
395 blob = regval_ctr_specific_value(key->values, idx);
397 val = talloc_zero(mem_ctx, struct registry_value);
398 if (val == NULL) {
399 return WERR_NOMEM;
402 val->type = regval_type(blob);
403 val->data = data_blob_talloc(mem_ctx, regval_data_p(blob), regval_size(blob));
405 if (pname
406 && !(*pname = talloc_strdup(
407 mem_ctx, regval_name(blob)))) {
408 TALLOC_FREE(val);
409 return WERR_NOMEM;
412 *pval = val;
413 return WERR_OK;
416 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
417 const char *name, struct registry_value **pval)
419 WERROR err;
420 uint32 i;
422 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
423 return WERR_ACCESS_DENIED;
426 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
427 return err;
430 for (i=0; i < regval_ctr_numvals(key->values); i++) {
431 struct regval_blob *blob;
432 blob = regval_ctr_specific_value(key->values, i);
433 if (strequal(regval_name(blob), name)) {
435 * don't use reg_enumvalue here:
436 * re-reading the values from the disk
437 * would change the indexing and break
438 * this function.
440 return reg_enumvalue_nocachefill(mem_ctx, key, i,
441 NULL, pval);
445 return WERR_BADFILE;
448 WERROR reg_querymultiplevalues(TALLOC_CTX *mem_ctx,
449 struct registry_key *key,
450 uint32_t num_names,
451 const char **names,
452 uint32_t *pnum_vals,
453 struct registry_value **pvals)
455 WERROR err;
456 uint32_t i, n, found = 0;
457 struct registry_value *vals;
459 if (num_names == 0) {
460 return WERR_OK;
463 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
464 return WERR_ACCESS_DENIED;
467 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
468 return err;
471 vals = talloc_zero_array(mem_ctx, struct registry_value, num_names);
472 if (vals == NULL) {
473 return WERR_NOMEM;
476 for (n=0; n < num_names; n++) {
477 for (i=0; i < regval_ctr_numvals(key->values); i++) {
478 struct regval_blob *blob;
479 blob = regval_ctr_specific_value(key->values, i);
480 if (strequal(regval_name(blob), names[n])) {
481 struct registry_value *v;
482 err = reg_enumvalue(mem_ctx, key, i, NULL, &v);
483 if (!W_ERROR_IS_OK(err)) {
484 return err;
486 vals[n] = *v;
487 found++;
492 *pvals = vals;
493 *pnum_vals = found;
495 return WERR_OK;
498 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
499 uint32_t *max_subkeylen, uint32_t *max_subkeysize,
500 uint32_t *num_values, uint32_t *max_valnamelen,
501 uint32_t *max_valbufsize, uint32_t *secdescsize,
502 NTTIME *last_changed_time)
504 uint32 i, max_size;
505 size_t max_len;
506 TALLOC_CTX *mem_ctx;
507 WERROR err;
508 struct security_descriptor *secdesc;
510 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
511 return WERR_ACCESS_DENIED;
514 if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
515 !W_ERROR_IS_OK(fill_value_cache(key))) {
516 return WERR_BADFILE;
519 max_len = 0;
520 for (i=0; i< regsubkey_ctr_numkeys(key->subkeys); i++) {
521 max_len = MAX(max_len,
522 strlen(regsubkey_ctr_specific_key(key->subkeys, i)));
525 *num_subkeys = regsubkey_ctr_numkeys(key->subkeys);
526 *max_subkeylen = max_len;
527 *max_subkeysize = 0; /* Class length? */
529 max_len = 0;
530 max_size = 0;
531 for (i=0; i < regval_ctr_numvals(key->values); i++) {
532 struct regval_blob *blob;
533 blob = regval_ctr_specific_value(key->values, i);
534 max_len = MAX(max_len, strlen(regval_name(blob)));
535 max_size = MAX(max_size, regval_size(blob));
538 *num_values = regval_ctr_numvals(key->values);
539 *max_valnamelen = max_len;
540 *max_valbufsize = max_size;
542 if (!(mem_ctx = talloc_new(key))) {
543 return WERR_NOMEM;
546 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
547 if (!W_ERROR_IS_OK(err)) {
548 TALLOC_FREE(mem_ctx);
549 return err;
552 *secdescsize = ndr_size_security_descriptor(secdesc, 0);
553 TALLOC_FREE(mem_ctx);
555 *last_changed_time = 0;
557 return WERR_OK;
560 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
561 const char *subkeypath, uint32 desired_access,
562 struct registry_key **pkey,
563 enum winreg_CreateAction *paction)
565 struct registry_key *key = parent;
566 struct registry_key *create_parent;
567 TALLOC_CTX *mem_ctx;
568 char *path, *end;
569 WERROR err;
571 if (!(mem_ctx = talloc_new(ctx))) return WERR_NOMEM;
573 if (!(path = talloc_strdup(mem_ctx, subkeypath))) {
574 err = WERR_NOMEM;
575 goto done;
578 while ((end = strchr(path, '\\')) != NULL) {
579 struct registry_key *tmp;
580 enum winreg_CreateAction action;
582 *end = '\0';
584 err = reg_createkey(mem_ctx, key, path,
585 KEY_ENUMERATE_SUB_KEYS, &tmp, &action);
586 if (!W_ERROR_IS_OK(err)) {
587 goto done;
590 if (key != parent) {
591 TALLOC_FREE(key);
594 key = tmp;
595 path = end+1;
599 * At this point, "path" contains the one-element subkey of "key". We
600 * can try to open it.
603 err = reg_openkey(ctx, key, path, desired_access, pkey);
604 if (W_ERROR_IS_OK(err)) {
605 if (paction != NULL) {
606 *paction = REG_OPENED_EXISTING_KEY;
608 goto done;
611 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
613 * Something but "notfound" has happened, so bail out
615 goto done;
619 * We have to make a copy of the current key, as we opened it only
620 * with ENUM_SUBKEY access.
623 err = reg_openkey(mem_ctx, key, "", KEY_CREATE_SUB_KEY,
624 &create_parent);
625 if (!W_ERROR_IS_OK(err)) {
626 goto done;
630 * Actually create the subkey
633 err = fill_subkey_cache(create_parent);
634 if (!W_ERROR_IS_OK(err)) goto done;
636 err = create_reg_subkey(key->key, path);
637 W_ERROR_NOT_OK_GOTO_DONE(err);
640 * Now open the newly created key
643 err = reg_openkey(ctx, create_parent, path, desired_access, pkey);
644 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
645 *paction = REG_CREATED_NEW_KEY;
648 done:
649 TALLOC_FREE(mem_ctx);
650 return err;
653 WERROR reg_deletekey(struct registry_key *parent, const char *path)
655 WERROR err;
656 char *name, *end;
657 struct registry_key *tmp_key, *key;
658 TALLOC_CTX *mem_ctx = talloc_stackframe();
660 name = talloc_strdup(mem_ctx, path);
661 if (name == NULL) {
662 err = WERR_NOMEM;
663 goto done;
666 /* check if the key has subkeys */
667 err = reg_openkey(mem_ctx, parent, name, REG_KEY_READ, &key);
668 W_ERROR_NOT_OK_GOTO_DONE(err);
670 err = fill_subkey_cache(key);
671 W_ERROR_NOT_OK_GOTO_DONE(err);
673 if (regsubkey_ctr_numkeys(key->subkeys) > 0) {
674 err = WERR_ACCESS_DENIED;
675 goto done;
678 /* no subkeys - proceed with delete */
679 end = strrchr(name, '\\');
680 if (end != NULL) {
681 *end = '\0';
683 err = reg_openkey(mem_ctx, parent, name,
684 KEY_CREATE_SUB_KEY, &tmp_key);
685 W_ERROR_NOT_OK_GOTO_DONE(err);
687 parent = tmp_key;
688 name = end+1;
691 if (name[0] == '\0') {
692 err = WERR_INVALID_PARAM;
693 goto done;
696 err = delete_reg_subkey(parent->key, name);
698 done:
699 TALLOC_FREE(mem_ctx);
700 return err;
703 WERROR reg_setvalue(struct registry_key *key, const char *name,
704 const struct registry_value *val)
706 struct regval_blob *existing;
707 WERROR err;
708 int res;
710 if (!(key->key->access_granted & KEY_SET_VALUE)) {
711 return WERR_ACCESS_DENIED;
714 err = regdb_transaction_start();
715 if (!W_ERROR_IS_OK(err)) {
716 DEBUG(0, ("reg_setvalue: Failed to start transaction: %s\n",
717 win_errstr(err)));
718 return err;
721 err = fill_value_cache(key);
722 if (!W_ERROR_IS_OK(err)) {
723 DEBUG(0, ("reg_setvalue: Error filling value cache: %s\n", win_errstr(err)));
724 goto done;
727 existing = regval_ctr_getvalue(key->values, name);
729 if ((existing != NULL) &&
730 (regval_size(existing) == val->data.length) &&
731 (memcmp(regval_data_p(existing), val->data.data,
732 val->data.length) == 0))
734 err = WERR_OK;
735 goto done;
738 res = regval_ctr_addvalue(key->values, name, val->type,
739 val->data.data, val->data.length);
741 if (res == 0) {
742 TALLOC_FREE(key->values);
743 err = WERR_NOMEM;
744 goto done;
747 if (!store_reg_values(key->key, key->values)) {
748 TALLOC_FREE(key->values);
749 DEBUG(0, ("reg_setvalue: store_reg_values failed\n"));
750 err = WERR_REG_IO_FAILURE;
751 goto done;
754 err = WERR_OK;
756 done:
757 if (W_ERROR_IS_OK(err)) {
758 err = regdb_transaction_commit();
759 if (!W_ERROR_IS_OK(err)) {
760 DEBUG(0, ("reg_setvalue: Error committing transaction: %s\n", win_errstr(err)));
762 } else {
763 WERROR err1 = regdb_transaction_cancel();
764 if (!W_ERROR_IS_OK(err1)) {
765 DEBUG(0, ("reg_setvalue: Error cancelling transaction: %s\n", win_errstr(err1)));
769 return err;
772 static WERROR reg_value_exists(struct registry_key *key, const char *name)
774 struct regval_blob *blob;
776 blob = regval_ctr_getvalue(key->values, name);
778 if (blob == NULL) {
779 return WERR_BADFILE;
780 } else {
781 return WERR_OK;
785 WERROR reg_deletevalue(struct registry_key *key, const char *name)
787 WERROR err;
789 if (!(key->key->access_granted & KEY_SET_VALUE)) {
790 return WERR_ACCESS_DENIED;
793 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
794 return err;
797 err = reg_value_exists(key, name);
798 if (!W_ERROR_IS_OK(err)) {
799 return err;
802 regval_ctr_delvalue(key->values, name);
804 if (!store_reg_values(key->key, key->values)) {
805 TALLOC_FREE(key->values);
806 return WERR_REG_IO_FAILURE;
809 return WERR_OK;
812 WERROR reg_getkeysecurity(TALLOC_CTX *mem_ctx, struct registry_key *key,
813 struct security_descriptor **psecdesc)
815 return regkey_get_secdesc(mem_ctx, key->key, psecdesc);
818 WERROR reg_setkeysecurity(struct registry_key *key,
819 struct security_descriptor *psecdesc)
821 return regkey_set_secdesc(key->key, psecdesc);
824 WERROR reg_getversion(uint32_t *version)
826 if (version == NULL) {
827 return WERR_INVALID_PARAM;
830 *version = 0x00000005; /* Windows 2000 registry API version */
831 return WERR_OK;
834 /**********************************************************************
835 * Higher level utility functions
836 **********************************************************************/
838 WERROR reg_deleteallvalues(struct registry_key *key)
840 WERROR err;
841 int i;
843 if (!(key->key->access_granted & KEY_SET_VALUE)) {
844 return WERR_ACCESS_DENIED;
847 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
848 return err;
851 for (i=0; i < regval_ctr_numvals(key->values); i++) {
852 struct regval_blob *blob;
853 blob = regval_ctr_specific_value(key->values, i);
854 regval_ctr_delvalue(key->values, regval_name(blob));
857 if (!store_reg_values(key->key, key->values)) {
858 TALLOC_FREE(key->values);
859 return WERR_REG_IO_FAILURE;
862 return WERR_OK;
866 * Utility function to delete a registry key with all its subkeys.
867 * Note that reg_deletekey returns ACCESS_DENIED when called on a
868 * key that has subkeys.
870 static WERROR reg_deletekey_recursive_internal(struct registry_key *parent,
871 const char *path,
872 bool del_key)
874 WERROR werr = WERR_OK;
875 struct registry_key *key;
876 char *subkey_name = NULL;
877 uint32 i;
878 TALLOC_CTX *mem_ctx = talloc_stackframe();
880 /* recurse through subkeys first */
881 werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
882 if (!W_ERROR_IS_OK(werr)) {
883 goto done;
886 werr = fill_subkey_cache(key);
887 W_ERROR_NOT_OK_GOTO_DONE(werr);
890 * loop from top to bottom for perfomance:
891 * this way, we need to rehash the regsubkey containers less
893 for (i = regsubkey_ctr_numkeys(key->subkeys) ; i > 0; i--) {
894 subkey_name = regsubkey_ctr_specific_key(key->subkeys, i-1);
895 werr = reg_deletekey_recursive_internal(key, subkey_name, true);
896 W_ERROR_NOT_OK_GOTO_DONE(werr);
899 if (del_key) {
900 /* now delete the actual key */
901 werr = reg_deletekey(parent, path);
904 done:
905 TALLOC_FREE(mem_ctx);
906 return werr;
909 static WERROR reg_deletekey_recursive_trans(struct registry_key *parent,
910 const char *path,
911 bool del_key)
913 WERROR werr;
915 werr = regdb_transaction_start();
916 if (!W_ERROR_IS_OK(werr)) {
917 DEBUG(0, ("reg_deletekey_recursive_trans: "
918 "error starting transaction: %s\n",
919 win_errstr(werr)));
920 return werr;
923 werr = reg_deletekey_recursive_internal(parent, path, del_key);
925 if (!W_ERROR_IS_OK(werr)) {
926 WERROR werr2;
928 DEBUG(1, (__location__ " failed to delete key '%s' from key "
929 "'%s': %s\n", path, parent->key->name,
930 win_errstr(werr)));
932 werr2 = regdb_transaction_cancel();
933 if (!W_ERROR_IS_OK(werr2)) {
934 DEBUG(0, ("reg_deletekey_recursive_trans: "
935 "error cancelling transaction: %s\n",
936 win_errstr(werr2)));
938 * return the original werr or the
939 * error from cancelling the transaction?
942 } else {
943 werr = regdb_transaction_commit();
944 if (!W_ERROR_IS_OK(werr)) {
945 DEBUG(0, ("reg_deletekey_recursive_trans: "
946 "error committing transaction: %s\n",
947 win_errstr(werr)));
951 return werr;
954 WERROR reg_deletekey_recursive(struct registry_key *parent,
955 const char *path)
957 return reg_deletekey_recursive_trans(parent, path, true);
960 WERROR reg_deletesubkeys_recursive(struct registry_key *parent,
961 const char *path)
963 return reg_deletekey_recursive_trans(parent, path, false);