s3: Simplify check_reduced_name a bit
[Samba/gebeck_regimport.git] / source3 / registry / reg_api.c
blob372f2d396e94de7427bef5718a9a8ea1b4fc7a8f
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 werr = regval_ctr_init(key, &(key->values));
93 W_ERROR_NOT_OK_RETURN(werr);
95 if (fetch_reg_values(key->key, key->values) == -1) {
96 TALLOC_FREE(key->values);
97 return WERR_BADFILE;
100 return WERR_OK;
103 static WERROR fill_subkey_cache(struct registry_key *key)
105 WERROR werr;
107 if (key->subkeys != NULL) {
108 if (!reg_subkeys_need_update(key->key, key->subkeys)) {
109 return WERR_OK;
113 TALLOC_FREE(key->subkeys);
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(mem_ctx, struct registry_key)) ||
147 !(regkey->token = dup_nt_token(regkey, token)) ||
148 !(regkey->key = talloc_zero(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_m(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 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
380 const char *name, struct registry_value **pval)
382 WERROR err;
383 uint32 i;
385 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
386 return WERR_ACCESS_DENIED;
389 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
390 return err;
393 for (i=0; i < regval_ctr_numvals(key->values); i++) {
394 struct regval_blob *blob;
395 blob = regval_ctr_specific_value(key->values, i);
396 if (strequal(regval_name(blob), name)) {
397 return reg_enumvalue(mem_ctx, key, i, NULL, pval);
401 return WERR_BADFILE;
404 WERROR reg_querymultiplevalues(TALLOC_CTX *mem_ctx,
405 struct registry_key *key,
406 uint32_t num_names,
407 const char **names,
408 uint32_t *pnum_vals,
409 struct registry_value **pvals)
411 WERROR err;
412 uint32_t i, n, found = 0;
413 struct registry_value *vals;
415 if (num_names == 0) {
416 return WERR_OK;
419 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
420 return WERR_ACCESS_DENIED;
423 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
424 return err;
427 vals = talloc_zero_array(mem_ctx, struct registry_value, num_names);
428 if (vals == NULL) {
429 return WERR_NOMEM;
432 for (n=0; n < num_names; n++) {
433 for (i=0; i < regval_ctr_numvals(key->values); i++) {
434 struct regval_blob *blob;
435 blob = regval_ctr_specific_value(key->values, i);
436 if (strequal(regval_name(blob), names[n])) {
437 struct registry_value *v;
438 err = reg_enumvalue(mem_ctx, key, i, NULL, &v);
439 if (!W_ERROR_IS_OK(err)) {
440 return err;
442 vals[n] = *v;
443 found++;
448 *pvals = vals;
449 *pnum_vals = found;
451 return WERR_OK;
454 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
455 uint32_t *max_subkeylen, uint32_t *max_subkeysize,
456 uint32_t *num_values, uint32_t *max_valnamelen,
457 uint32_t *max_valbufsize, uint32_t *secdescsize,
458 NTTIME *last_changed_time)
460 uint32 i, max_size;
461 size_t max_len;
462 TALLOC_CTX *mem_ctx;
463 WERROR err;
464 struct security_descriptor *secdesc;
466 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
467 return WERR_ACCESS_DENIED;
470 if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
471 !W_ERROR_IS_OK(fill_value_cache(key))) {
472 return WERR_BADFILE;
475 max_len = 0;
476 for (i=0; i< regsubkey_ctr_numkeys(key->subkeys); i++) {
477 max_len = MAX(max_len,
478 strlen(regsubkey_ctr_specific_key(key->subkeys, i)));
481 *num_subkeys = regsubkey_ctr_numkeys(key->subkeys);
482 *max_subkeylen = max_len;
483 *max_subkeysize = 0; /* Class length? */
485 max_len = 0;
486 max_size = 0;
487 for (i=0; i < regval_ctr_numvals(key->values); i++) {
488 struct regval_blob *blob;
489 blob = regval_ctr_specific_value(key->values, i);
490 max_len = MAX(max_len, strlen(regval_name(blob)));
491 max_size = MAX(max_size, regval_size(blob));
494 *num_values = regval_ctr_numvals(key->values);
495 *max_valnamelen = max_len;
496 *max_valbufsize = max_size;
498 if (!(mem_ctx = talloc_new(key))) {
499 return WERR_NOMEM;
502 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
503 if (!W_ERROR_IS_OK(err)) {
504 TALLOC_FREE(mem_ctx);
505 return err;
508 *secdescsize = ndr_size_security_descriptor(secdesc, 0);
509 TALLOC_FREE(mem_ctx);
511 *last_changed_time = 0;
513 return WERR_OK;
516 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
517 const char *subkeypath, uint32 desired_access,
518 struct registry_key **pkey,
519 enum winreg_CreateAction *paction)
521 struct registry_key *key = parent;
522 struct registry_key *create_parent;
523 TALLOC_CTX *mem_ctx;
524 char *path, *end;
525 WERROR err;
527 if (!(mem_ctx = talloc_new(ctx))) return WERR_NOMEM;
529 if (!(path = talloc_strdup(mem_ctx, subkeypath))) {
530 err = WERR_NOMEM;
531 goto done;
534 while ((end = strchr(path, '\\')) != NULL) {
535 struct registry_key *tmp;
536 enum winreg_CreateAction action;
538 *end = '\0';
540 err = reg_createkey(mem_ctx, key, path,
541 KEY_ENUMERATE_SUB_KEYS, &tmp, &action);
542 if (!W_ERROR_IS_OK(err)) {
543 goto done;
546 if (key != parent) {
547 TALLOC_FREE(key);
550 key = tmp;
551 path = end+1;
555 * At this point, "path" contains the one-element subkey of "key". We
556 * can try to open it.
559 err = reg_openkey(ctx, key, path, desired_access, pkey);
560 if (W_ERROR_IS_OK(err)) {
561 if (paction != NULL) {
562 *paction = REG_OPENED_EXISTING_KEY;
564 goto done;
567 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
569 * Something but "notfound" has happened, so bail out
571 goto done;
575 * We have to make a copy of the current key, as we opened it only
576 * with ENUM_SUBKEY access.
579 err = reg_openkey(mem_ctx, key, "", KEY_CREATE_SUB_KEY,
580 &create_parent);
581 if (!W_ERROR_IS_OK(err)) {
582 goto done;
586 * Actually create the subkey
589 err = fill_subkey_cache(create_parent);
590 if (!W_ERROR_IS_OK(err)) goto done;
592 err = create_reg_subkey(key->key, path);
593 W_ERROR_NOT_OK_GOTO_DONE(err);
596 * Now open the newly created key
599 err = reg_openkey(ctx, create_parent, path, desired_access, pkey);
600 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
601 *paction = REG_CREATED_NEW_KEY;
604 done:
605 TALLOC_FREE(mem_ctx);
606 return err;
609 static WERROR reg_deletekey_internal(TALLOC_CTX *mem_ctx,
610 struct registry_key *parent,
611 const char *path, bool lazy)
613 WERROR err;
614 char *name, *end;
615 struct registry_key *key;
616 name = talloc_strdup(mem_ctx, path);
617 if (name == NULL) {
618 err = WERR_NOMEM;
619 goto done;
622 /* no subkeys - proceed with delete */
623 end = strrchr(name, '\\');
624 if (end != NULL) {
625 *end = '\0';
627 err = reg_openkey(mem_ctx, parent, name,
628 KEY_CREATE_SUB_KEY, &key);
629 W_ERROR_NOT_OK_GOTO_DONE(err);
631 parent = key;
632 name = end+1;
635 if (name[0] == '\0') {
636 err = WERR_INVALID_PARAM;
637 goto done;
640 err = delete_reg_subkey(parent->key, name, lazy);
642 done:
643 return err;
646 WERROR reg_deletekey(struct registry_key *parent, const char *path)
648 WERROR err;
649 struct registry_key *key;
650 TALLOC_CTX *mem_ctx = talloc_stackframe();
652 /* check if the key has subkeys */
653 err = reg_openkey(mem_ctx, parent, path, REG_KEY_READ, &key);
654 W_ERROR_NOT_OK_GOTO_DONE(err);
656 err = fill_subkey_cache(key);
657 W_ERROR_NOT_OK_GOTO_DONE(err);
659 if (regsubkey_ctr_numkeys(key->subkeys) > 0) {
660 err = WERR_ACCESS_DENIED;
661 goto done;
663 err = reg_deletekey_internal(mem_ctx, parent, path, false);
664 done:
665 TALLOC_FREE(mem_ctx);
666 return err;
670 WERROR reg_setvalue(struct registry_key *key, const char *name,
671 const struct registry_value *val)
673 struct regval_blob *existing;
674 WERROR err;
675 int res;
677 if (!(key->key->access_granted & KEY_SET_VALUE)) {
678 return WERR_ACCESS_DENIED;
681 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
682 return err;
685 existing = regval_ctr_getvalue(key->values, name);
687 if ((existing != NULL) &&
688 (regval_size(existing) == val->data.length) &&
689 (memcmp(regval_data_p(existing), val->data.data,
690 val->data.length) == 0)) {
691 return WERR_OK;
694 res = regval_ctr_addvalue(key->values, name, val->type,
695 val->data.data, val->data.length);
697 if (res == 0) {
698 TALLOC_FREE(key->values);
699 return WERR_NOMEM;
702 if (!store_reg_values(key->key, key->values)) {
703 TALLOC_FREE(key->values);
704 return WERR_REG_IO_FAILURE;
707 return WERR_OK;
710 static WERROR reg_value_exists(struct registry_key *key, const char *name)
712 struct regval_blob *blob;
714 blob = regval_ctr_getvalue(key->values, name);
716 if (blob == NULL) {
717 return WERR_BADFILE;
718 } else {
719 return WERR_OK;
723 WERROR reg_deletevalue(struct registry_key *key, const char *name)
725 WERROR err;
727 if (!(key->key->access_granted & KEY_SET_VALUE)) {
728 return WERR_ACCESS_DENIED;
731 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
732 return err;
735 err = reg_value_exists(key, name);
736 if (!W_ERROR_IS_OK(err)) {
737 return err;
740 regval_ctr_delvalue(key->values, name);
742 if (!store_reg_values(key->key, key->values)) {
743 TALLOC_FREE(key->values);
744 return WERR_REG_IO_FAILURE;
747 return WERR_OK;
750 WERROR reg_getkeysecurity(TALLOC_CTX *mem_ctx, struct registry_key *key,
751 struct security_descriptor **psecdesc)
753 return regkey_get_secdesc(mem_ctx, key->key, psecdesc);
756 WERROR reg_setkeysecurity(struct registry_key *key,
757 struct security_descriptor *psecdesc)
759 return regkey_set_secdesc(key->key, psecdesc);
762 WERROR reg_getversion(uint32_t *version)
764 if (version == NULL) {
765 return WERR_INVALID_PARAM;
768 *version = 0x00000005; /* Windows 2000 registry API version */
769 return WERR_OK;
772 /**********************************************************************
773 * Higher level utility functions
774 **********************************************************************/
776 WERROR reg_deleteallvalues(struct registry_key *key)
778 WERROR err;
779 int i;
781 if (!(key->key->access_granted & KEY_SET_VALUE)) {
782 return WERR_ACCESS_DENIED;
785 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
786 return err;
789 for (i=0; i < regval_ctr_numvals(key->values); i++) {
790 struct regval_blob *blob;
791 blob = regval_ctr_specific_value(key->values, i);
792 regval_ctr_delvalue(key->values, regval_name(blob));
795 if (!store_reg_values(key->key, key->values)) {
796 TALLOC_FREE(key->values);
797 return WERR_REG_IO_FAILURE;
800 return WERR_OK;
804 * Utility function to delete a registry key with all its subkeys.
805 * Note that reg_deletekey returns ACCESS_DENIED when called on a
806 * key that has subkeys.
808 static WERROR reg_deletekey_recursive_internal(struct registry_key *parent,
809 const char *path,
810 bool del_key, bool lazy)
812 WERROR werr = WERR_OK;
813 struct registry_key *key;
814 char *subkey_name = NULL;
815 uint32 i;
816 TALLOC_CTX *mem_ctx = talloc_stackframe();
818 DEBUG(5, ("reg_deletekey_recursive_internal: deleting '%s' from '%s'\n",
819 path, parent->key->name));
821 /* recurse through subkeys first */
822 werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
823 if (!W_ERROR_IS_OK(werr)) {
824 DEBUG(3, ("reg_deletekey_recursive_internal: error opening "
825 "subkey '%s' of '%s': '%s'\n",
826 path, parent->key->name, win_errstr(werr)));
827 goto done;
830 werr = fill_subkey_cache(key);
831 W_ERROR_NOT_OK_GOTO_DONE(werr);
834 * loop from top to bottom for perfomance:
835 * this way, we need to rehash the regsubkey containers less
837 for (i = regsubkey_ctr_numkeys(key->subkeys) ; i > 0; i--) {
838 subkey_name = regsubkey_ctr_specific_key(key->subkeys, i-1);
839 werr = reg_deletekey_recursive_internal(key, subkey_name, true, del_key);
840 W_ERROR_NOT_OK_GOTO_DONE(werr);
843 if (del_key) {
844 /* now delete the actual key */
845 werr = reg_deletekey_internal(mem_ctx, parent, path, lazy);
848 done:
850 DEBUG(5, ("reg_deletekey_recursive_internal: done deleting '%s' from "
851 "'%s': %s\n",
852 path, parent->key->name, win_errstr(werr)));
853 TALLOC_FREE(mem_ctx);
854 return werr;
857 static WERROR reg_deletekey_recursive_trans(struct registry_key *parent,
858 const char *path,
859 bool del_key)
861 WERROR werr;
863 werr = regdb_transaction_start();
864 if (!W_ERROR_IS_OK(werr)) {
865 DEBUG(0, ("reg_deletekey_recursive_trans: "
866 "error starting transaction: %s\n",
867 win_errstr(werr)));
868 return werr;
871 werr = reg_deletekey_recursive_internal(parent, path, del_key, false);
873 if (!W_ERROR_IS_OK(werr)) {
874 WERROR werr2;
876 DEBUG(1, (__location__ " failed to delete key '%s' from key "
877 "'%s': %s\n", path, parent->key->name,
878 win_errstr(werr)));
880 werr2 = regdb_transaction_cancel();
881 if (!W_ERROR_IS_OK(werr2)) {
882 DEBUG(0, ("reg_deletekey_recursive_trans: "
883 "error cancelling transaction: %s\n",
884 win_errstr(werr2)));
886 * return the original werr or the
887 * error from cancelling the transaction?
890 } else {
891 werr = regdb_transaction_commit();
892 if (!W_ERROR_IS_OK(werr)) {
893 DEBUG(0, ("reg_deletekey_recursive_trans: "
894 "error committing transaction: %s\n",
895 win_errstr(werr)));
896 } else {
897 DEBUG(5, ("reg_reletekey_recursive_trans: deleted key '%s' from '%s'\n",
898 path, parent->key->name));
903 return werr;
906 WERROR reg_deletekey_recursive(struct registry_key *parent,
907 const char *path)
909 return reg_deletekey_recursive_trans(parent, path, true);
912 WERROR reg_deletesubkeys_recursive(struct registry_key *parent,
913 const char *path)
915 return reg_deletekey_recursive_trans(parent, path, false);