s3:smb2_sesssetup: make use of SMBD_SMB2_* macros
[Samba/bb.git] / source3 / registry / reg_api.c
blobc263174bc23bbc504540d939aa5f3b43ff6371f9
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 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_BADFILE;
123 return WERR_OK;
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,
133 const char *name,
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)))
150 result = WERR_NOMEM;
151 goto done;
154 result = regdb_open();
155 if (!(W_ERROR_IS_OK(result))) {
156 goto done;
159 key = regkey->key;
160 talloc_set_destructor(key, regkey_destructor);
162 /* initialization */
164 key->type = REG_KEY_GENERIC;
166 if (name[0] == '\0') {
168 * Open a copy of the parent key
170 if (!parent) {
171 result = WERR_BADFILE;
172 goto done;
174 key->name = talloc_strdup(key, parent->key->name);
176 else {
178 * Normal subkey open
180 key->name = talloc_asprintf(key, "%s%s%s",
181 parent ? parent->key->name : "",
182 parent ? "\\": "",
183 name);
186 if (key->name == NULL) {
187 result = WERR_NOMEM;
188 goto done;
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;
203 goto done;
206 /* FIXME: Existence is currently checked by fetching the subkeys */
208 result = fill_subkey_cache(regkey);
209 if (!W_ERROR_IS_OK(result)) {
210 goto done;
213 if ( !regkey_access_check( key, access_desired, &key->access_granted,
214 token ) ) {
215 result = WERR_ACCESS_DENIED;
216 goto done;
219 *pregkey = regkey;
220 result = WERR_OK;
222 done:
223 if ( !W_ERROR_IS_OK(result) ) {
224 TALLOC_FREE(regkey);
227 return 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,
240 pkey);
244 /**********************************************************************
245 * The API functions
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;
253 WERROR err;
254 char *p, *path;
255 size_t len;
256 TALLOC_CTX *frame = talloc_stackframe();
258 path = talloc_strdup(frame, name);
259 if (path == NULL) {
260 err = WERR_NOMEM;
261 goto error;
264 len = strlen(path);
266 if ((len > 0) && (path[len-1] == '\\')) {
267 path[len-1] = '\0';
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) {
276 err = WERR_NOMEM;
277 goto error;
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)) {
285 goto error;
288 direct_parent = tmp;
289 path = p+1;
292 err = regkey_open_onelevel(mem_ctx, direct_parent, path, parent->token,
293 desired_access, pkey);
295 error:
296 talloc_free(frame);
297 return err;
300 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
301 uint32 idx, char **name, NTTIME *last_write_time)
303 WERROR err;
305 if (!(key->key->access_granted & KEY_ENUMERATE_SUB_KEYS)) {
306 return WERR_ACCESS_DENIED;
309 err = fill_subkey_cache(key);
310 if (!W_ERROR_IS_OK(err)) {
311 return err;
314 if (idx >= regsubkey_ctr_numkeys(key->subkeys)) {
315 return WERR_NO_MORE_ITEMS;
318 if (!(*name = talloc_strdup(mem_ctx,
319 regsubkey_ctr_specific_key(key->subkeys, idx))))
321 return WERR_NOMEM;
324 if (last_write_time) {
325 *last_write_time = 0;
328 return WERR_OK;
331 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
332 uint32 idx, char **pname, struct registry_value **pval)
334 struct registry_value *val;
335 struct regval_blob *blob;
336 WERROR err;
338 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
339 return WERR_ACCESS_DENIED;
342 err = fill_value_cache(key);
343 if (!(W_ERROR_IS_OK(err))) {
344 return err;
347 if (idx >= regval_ctr_numvals(key->values)) {
348 return WERR_NO_MORE_ITEMS;
351 blob = regval_ctr_specific_value(key->values, idx);
353 val = talloc_zero(mem_ctx, struct registry_value);
354 if (val == NULL) {
355 return WERR_NOMEM;
358 val->type = regval_type(blob);
359 val->data = data_blob_talloc(mem_ctx, regval_data_p(blob), regval_size(blob));
361 if (pname
362 && !(*pname = talloc_strdup(
363 mem_ctx, regval_name(blob)))) {
364 TALLOC_FREE(val);
365 return WERR_NOMEM;
368 *pval = val;
369 return WERR_OK;
372 static WERROR reg_enumvalue_nocachefill(TALLOC_CTX *mem_ctx,
373 struct registry_key *key,
374 uint32 idx, char **pname,
375 struct registry_value **pval)
377 struct registry_value *val;
378 struct regval_blob *blob;
380 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
381 return WERR_ACCESS_DENIED;
384 if (idx >= regval_ctr_numvals(key->values)) {
385 return WERR_NO_MORE_ITEMS;
388 blob = regval_ctr_specific_value(key->values, idx);
390 val = talloc_zero(mem_ctx, struct registry_value);
391 if (val == NULL) {
392 return WERR_NOMEM;
395 val->type = regval_type(blob);
396 val->data = data_blob_talloc(mem_ctx, regval_data_p(blob), regval_size(blob));
398 if (pname
399 && !(*pname = talloc_strdup(
400 mem_ctx, regval_name(blob)))) {
401 TALLOC_FREE(val);
402 return WERR_NOMEM;
405 *pval = val;
406 return WERR_OK;
409 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
410 const char *name, struct registry_value **pval)
412 WERROR err;
413 uint32 i;
415 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
416 return WERR_ACCESS_DENIED;
419 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
420 return err;
423 for (i=0; i < regval_ctr_numvals(key->values); i++) {
424 struct regval_blob *blob;
425 blob = regval_ctr_specific_value(key->values, i);
426 if (strequal(regval_name(blob), name)) {
428 * don't use reg_enumvalue here:
429 * re-reading the values from the disk
430 * would change the indexing and break
431 * this function.
433 return reg_enumvalue_nocachefill(mem_ctx, key, i,
434 NULL, pval);
438 return WERR_BADFILE;
441 WERROR reg_querymultiplevalues(TALLOC_CTX *mem_ctx,
442 struct registry_key *key,
443 uint32_t num_names,
444 const char **names,
445 uint32_t *pnum_vals,
446 struct registry_value **pvals)
448 WERROR err;
449 uint32_t i, n, found = 0;
450 struct registry_value *vals;
452 if (num_names == 0) {
453 return WERR_OK;
456 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
457 return WERR_ACCESS_DENIED;
460 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
461 return err;
464 vals = talloc_zero_array(mem_ctx, struct registry_value, num_names);
465 if (vals == NULL) {
466 return WERR_NOMEM;
469 for (n=0; n < num_names; n++) {
470 for (i=0; i < regval_ctr_numvals(key->values); i++) {
471 struct regval_blob *blob;
472 blob = regval_ctr_specific_value(key->values, i);
473 if (strequal(regval_name(blob), names[n])) {
474 struct registry_value *v;
475 err = reg_enumvalue(mem_ctx, key, i, NULL, &v);
476 if (!W_ERROR_IS_OK(err)) {
477 return err;
479 vals[n] = *v;
480 found++;
485 *pvals = vals;
486 *pnum_vals = found;
488 return WERR_OK;
491 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
492 uint32_t *max_subkeylen, uint32_t *max_subkeysize,
493 uint32_t *num_values, uint32_t *max_valnamelen,
494 uint32_t *max_valbufsize, uint32_t *secdescsize,
495 NTTIME *last_changed_time)
497 uint32 i, max_size;
498 size_t max_len;
499 TALLOC_CTX *mem_ctx;
500 WERROR err;
501 struct security_descriptor *secdesc;
503 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
504 return WERR_ACCESS_DENIED;
507 if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
508 !W_ERROR_IS_OK(fill_value_cache(key))) {
509 return WERR_BADFILE;
512 max_len = 0;
513 for (i=0; i< regsubkey_ctr_numkeys(key->subkeys); i++) {
514 max_len = MAX(max_len,
515 strlen(regsubkey_ctr_specific_key(key->subkeys, i)));
518 *num_subkeys = regsubkey_ctr_numkeys(key->subkeys);
519 *max_subkeylen = max_len;
520 *max_subkeysize = 0; /* Class length? */
522 max_len = 0;
523 max_size = 0;
524 for (i=0; i < regval_ctr_numvals(key->values); i++) {
525 struct regval_blob *blob;
526 blob = regval_ctr_specific_value(key->values, i);
527 max_len = MAX(max_len, strlen(regval_name(blob)));
528 max_size = MAX(max_size, regval_size(blob));
531 *num_values = regval_ctr_numvals(key->values);
532 *max_valnamelen = max_len;
533 *max_valbufsize = max_size;
535 if (!(mem_ctx = talloc_new(key))) {
536 return WERR_NOMEM;
539 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
540 if (!W_ERROR_IS_OK(err)) {
541 TALLOC_FREE(mem_ctx);
542 return err;
545 *secdescsize = ndr_size_security_descriptor(secdesc, 0);
546 TALLOC_FREE(mem_ctx);
548 *last_changed_time = 0;
550 return WERR_OK;
553 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
554 const char *subkeypath, uint32 desired_access,
555 struct registry_key **pkey,
556 enum winreg_CreateAction *paction)
558 struct registry_key *key = parent;
559 TALLOC_CTX *mem_ctx;
560 char *path, *end;
561 WERROR err;
562 uint32_t access_granted;
564 mem_ctx = talloc_new(ctx);
565 if (mem_ctx == NULL) {
566 return WERR_NOMEM;
569 path = talloc_strdup(mem_ctx, subkeypath);
570 if (path == NULL) {
571 err = WERR_NOMEM;
572 goto done;
575 err = regdb_transaction_start();
576 if (!W_ERROR_IS_OK(err)) {
577 DEBUG(0, ("reg_createkey: failed to start transaction: %s\n",
578 win_errstr(err)));
579 goto done;
582 while ((end = strchr(path, '\\')) != NULL) {
583 struct registry_key *tmp;
584 enum winreg_CreateAction action;
586 *end = '\0';
588 err = reg_createkey(mem_ctx, key, path,
589 KEY_ENUMERATE_SUB_KEYS, &tmp, &action);
590 if (!W_ERROR_IS_OK(err)) {
591 goto trans_done;
594 if (key != parent) {
595 TALLOC_FREE(key);
598 key = tmp;
599 path = end+1;
603 * At this point, "path" contains the one-element subkey of "key". We
604 * can try to open it.
607 err = reg_openkey(ctx, key, path, desired_access, pkey);
608 if (W_ERROR_IS_OK(err)) {
609 if (paction != NULL) {
610 *paction = REG_OPENED_EXISTING_KEY;
612 goto trans_done;
615 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
617 * Something but "notfound" has happened, so bail out
619 goto trans_done;
623 * We may (e.g. in the iteration) have opened the key with ENUM_SUBKEY.
624 * Instead of re-opening the key with CREATE_SUB_KEY, we simply
625 * duplicate the access check here and skip the expensive full open.
627 if (!regkey_access_check(key->key, KEY_CREATE_SUB_KEY, &access_granted,
628 key->token))
630 err = WERR_ACCESS_DENIED;
631 goto done;
635 * Actually create the subkey
638 err = create_reg_subkey(key->key, path);
639 if (!W_ERROR_IS_OK(err)) {
640 goto trans_done;
644 * Now open the newly created key
647 err = reg_openkey(ctx, key, path, desired_access, pkey);
648 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
649 *paction = REG_CREATED_NEW_KEY;
652 trans_done:
653 if (W_ERROR_IS_OK(err)) {
654 err = regdb_transaction_commit();
655 if (!W_ERROR_IS_OK(err)) {
656 DEBUG(0, ("reg_createkey: Error committing transaction: %s\n", win_errstr(err)));
658 } else {
659 WERROR err1 = regdb_transaction_cancel();
660 if (!W_ERROR_IS_OK(err1)) {
661 DEBUG(0, ("reg_createkey: Error cancelling transaction: %s\n", win_errstr(err1)));
665 done:
666 TALLOC_FREE(mem_ctx);
667 return err;
670 static WERROR reg_deletekey_internal(TALLOC_CTX *mem_ctx,
671 struct registry_key *parent,
672 const char *path, bool lazy)
674 WERROR err;
675 char *name, *end;
676 struct registry_key *key;
677 name = talloc_strdup(mem_ctx, path);
678 if (name == NULL) {
679 err = WERR_NOMEM;
680 goto done;
683 /* no subkeys - proceed with delete */
684 end = strrchr(name, '\\');
685 if (end != NULL) {
686 *end = '\0';
688 err = reg_openkey(mem_ctx, parent, name,
689 KEY_CREATE_SUB_KEY, &key);
690 W_ERROR_NOT_OK_GOTO_DONE(err);
692 parent = key;
693 name = end+1;
696 if (name[0] == '\0') {
697 err = WERR_INVALID_PARAM;
698 goto done;
701 err = delete_reg_subkey(parent->key, name, lazy);
703 done:
704 return err;
707 WERROR reg_deletekey(struct registry_key *parent, const char *path)
709 WERROR err;
710 struct registry_key *key;
711 TALLOC_CTX *mem_ctx = talloc_stackframe();
713 /* check if the key has subkeys */
714 err = reg_openkey(mem_ctx, parent, path, REG_KEY_READ, &key);
715 W_ERROR_NOT_OK_GOTO_DONE(err);
717 err = regdb_transaction_start();
718 if (!W_ERROR_IS_OK(err)) {
719 DEBUG(0, ("reg_deletekey: Error starting transaction: %s\n",
720 win_errstr(err)));
721 goto done;
724 err = fill_subkey_cache(key);
725 if (!W_ERROR_IS_OK(err)) {
726 goto trans_done;
729 if (regsubkey_ctr_numkeys(key->subkeys) > 0) {
730 err = WERR_ACCESS_DENIED;
731 goto trans_done;
733 err = reg_deletekey_internal(mem_ctx, parent, path, false);
735 trans_done:
736 if (W_ERROR_IS_OK(err)) {
737 err = regdb_transaction_commit();
738 if (!W_ERROR_IS_OK(err)) {
739 DEBUG(0, ("reg_deletekey: Error committing transaction: %s\n", win_errstr(err)));
741 } else {
742 WERROR err1 = regdb_transaction_cancel();
743 if (!W_ERROR_IS_OK(err1)) {
744 DEBUG(0, ("reg_deletekey: Error cancelling transaction: %s\n", win_errstr(err1)));
748 done:
749 TALLOC_FREE(mem_ctx);
750 return err;
754 WERROR reg_setvalue(struct registry_key *key, const char *name,
755 const struct registry_value *val)
757 struct regval_blob *existing;
758 WERROR err;
759 int res;
761 if (!(key->key->access_granted & KEY_SET_VALUE)) {
762 return WERR_ACCESS_DENIED;
765 err = regdb_transaction_start();
766 if (!W_ERROR_IS_OK(err)) {
767 DEBUG(0, ("reg_setvalue: Failed to start transaction: %s\n",
768 win_errstr(err)));
769 return err;
772 err = fill_value_cache(key);
773 if (!W_ERROR_IS_OK(err)) {
774 DEBUG(0, ("reg_setvalue: Error filling value cache: %s\n", win_errstr(err)));
775 goto done;
778 existing = regval_ctr_getvalue(key->values, name);
780 if ((existing != NULL) &&
781 (regval_size(existing) == val->data.length) &&
782 (memcmp(regval_data_p(existing), val->data.data,
783 val->data.length) == 0))
785 err = WERR_OK;
786 goto done;
789 res = regval_ctr_addvalue(key->values, name, val->type,
790 val->data.data, val->data.length);
792 if (res == 0) {
793 TALLOC_FREE(key->values);
794 err = WERR_NOMEM;
795 goto done;
798 if (!store_reg_values(key->key, key->values)) {
799 TALLOC_FREE(key->values);
800 DEBUG(0, ("reg_setvalue: store_reg_values failed\n"));
801 err = WERR_REG_IO_FAILURE;
802 goto done;
805 err = WERR_OK;
807 done:
808 if (W_ERROR_IS_OK(err)) {
809 err = regdb_transaction_commit();
810 if (!W_ERROR_IS_OK(err)) {
811 DEBUG(0, ("reg_setvalue: Error committing transaction: %s\n", win_errstr(err)));
813 } else {
814 WERROR err1 = regdb_transaction_cancel();
815 if (!W_ERROR_IS_OK(err1)) {
816 DEBUG(0, ("reg_setvalue: Error cancelling transaction: %s\n", win_errstr(err1)));
820 return err;
823 static WERROR reg_value_exists(struct registry_key *key, const char *name)
825 struct regval_blob *blob;
827 blob = regval_ctr_getvalue(key->values, name);
829 if (blob == NULL) {
830 return WERR_BADFILE;
831 } else {
832 return WERR_OK;
836 WERROR reg_deletevalue(struct registry_key *key, const char *name)
838 WERROR err;
840 if (!(key->key->access_granted & KEY_SET_VALUE)) {
841 return WERR_ACCESS_DENIED;
844 err = regdb_transaction_start();
845 if (!W_ERROR_IS_OK(err)) {
846 DEBUG(0, ("reg_deletevalue: Failed to start transaction: %s\n",
847 win_errstr(err)));
848 return err;
851 err = fill_value_cache(key);
852 if (!W_ERROR_IS_OK(err)) {
853 DEBUG(0, ("reg_deletevalue; Error filling value cache: %s\n",
854 win_errstr(err)));
855 goto done;
858 err = reg_value_exists(key, name);
859 if (!W_ERROR_IS_OK(err)) {
860 goto done;
863 regval_ctr_delvalue(key->values, name);
865 if (!store_reg_values(key->key, key->values)) {
866 TALLOC_FREE(key->values);
867 err = WERR_REG_IO_FAILURE;
868 DEBUG(0, ("reg_deletevalue: store_reg_values failed\n"));
869 goto done;
872 err = WERR_OK;
874 done:
875 if (W_ERROR_IS_OK(err)) {
876 err = regdb_transaction_commit();
877 if (!W_ERROR_IS_OK(err)) {
878 DEBUG(0, ("reg_deletevalue: Error committing transaction: %s\n", win_errstr(err)));
880 } else {
881 WERROR err1 = regdb_transaction_cancel();
882 if (!W_ERROR_IS_OK(err1)) {
883 DEBUG(0, ("reg_deletevalue: Error cancelling transaction: %s\n", win_errstr(err1)));
887 return err;
890 WERROR reg_getkeysecurity(TALLOC_CTX *mem_ctx, struct registry_key *key,
891 struct security_descriptor **psecdesc)
893 return regkey_get_secdesc(mem_ctx, key->key, psecdesc);
896 WERROR reg_setkeysecurity(struct registry_key *key,
897 struct security_descriptor *psecdesc)
899 return regkey_set_secdesc(key->key, psecdesc);
902 WERROR reg_getversion(uint32_t *version)
904 if (version == NULL) {
905 return WERR_INVALID_PARAM;
908 *version = 0x00000005; /* Windows 2000 registry API version */
909 return WERR_OK;
912 /**********************************************************************
913 * Higher level utility functions
914 **********************************************************************/
916 WERROR reg_deleteallvalues(struct registry_key *key)
918 WERROR err;
919 int i;
921 if (!(key->key->access_granted & KEY_SET_VALUE)) {
922 return WERR_ACCESS_DENIED;
925 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
926 return err;
929 for (i=0; i < regval_ctr_numvals(key->values); i++) {
930 struct regval_blob *blob;
931 blob = regval_ctr_specific_value(key->values, i);
932 regval_ctr_delvalue(key->values, regval_name(blob));
935 if (!store_reg_values(key->key, key->values)) {
936 TALLOC_FREE(key->values);
937 return WERR_REG_IO_FAILURE;
940 return WERR_OK;
944 * Utility function to delete a registry key with all its subkeys.
945 * Note that reg_deletekey returns ACCESS_DENIED when called on a
946 * key that has subkeys.
948 static WERROR reg_deletekey_recursive_internal(struct registry_key *parent,
949 const char *path,
950 bool del_key, bool lazy)
952 WERROR werr = WERR_OK;
953 struct registry_key *key;
954 char *subkey_name = NULL;
955 uint32 i;
956 TALLOC_CTX *mem_ctx = talloc_stackframe();
958 DEBUG(5, ("reg_deletekey_recursive_internal: deleting '%s' from '%s'\n",
959 path, parent->key->name));
961 /* recurse through subkeys first */
962 werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
963 if (!W_ERROR_IS_OK(werr)) {
964 DEBUG(3, ("reg_deletekey_recursive_internal: error opening "
965 "subkey '%s' of '%s': '%s'\n",
966 path, parent->key->name, win_errstr(werr)));
967 goto done;
970 werr = fill_subkey_cache(key);
971 W_ERROR_NOT_OK_GOTO_DONE(werr);
974 * loop from top to bottom for perfomance:
975 * this way, we need to rehash the regsubkey containers less
977 for (i = regsubkey_ctr_numkeys(key->subkeys) ; i > 0; i--) {
978 subkey_name = regsubkey_ctr_specific_key(key->subkeys, i-1);
979 werr = reg_deletekey_recursive_internal(key, subkey_name, true, del_key);
980 W_ERROR_NOT_OK_GOTO_DONE(werr);
983 if (del_key) {
984 /* now delete the actual key */
985 werr = reg_deletekey_internal(mem_ctx, parent, path, lazy);
988 done:
990 DEBUG(5, ("reg_deletekey_recursive_internal: done deleting '%s' from "
991 "'%s': %s\n",
992 path, parent->key->name, win_errstr(werr)));
993 TALLOC_FREE(mem_ctx);
994 return werr;
997 static WERROR reg_deletekey_recursive_trans(struct registry_key *parent,
998 const char *path,
999 bool del_key)
1001 WERROR werr;
1003 werr = regdb_transaction_start();
1004 if (!W_ERROR_IS_OK(werr)) {
1005 DEBUG(0, ("reg_deletekey_recursive_trans: "
1006 "error starting transaction: %s\n",
1007 win_errstr(werr)));
1008 return werr;
1011 werr = reg_deletekey_recursive_internal(parent, path, del_key, false);
1013 if (!W_ERROR_IS_OK(werr)) {
1014 WERROR werr2;
1015 DEBUG(W_ERROR_EQUAL(werr, WERR_BADFILE) ? 5 : 1,
1016 (__location__ ": failed to delete key '%s' from key "
1017 "'%s': %s\n", path, parent->key->name,
1018 win_errstr(werr)));
1020 werr2 = regdb_transaction_cancel();
1021 if (!W_ERROR_IS_OK(werr2)) {
1022 DEBUG(0, ("reg_deletekey_recursive_trans: "
1023 "error cancelling transaction: %s\n",
1024 win_errstr(werr2)));
1026 * return the original werr or the
1027 * error from cancelling the transaction?
1030 } else {
1031 werr = regdb_transaction_commit();
1032 if (!W_ERROR_IS_OK(werr)) {
1033 DEBUG(0, ("reg_deletekey_recursive_trans: "
1034 "error committing transaction: %s\n",
1035 win_errstr(werr)));
1036 } else {
1037 DEBUG(5, ("reg_reletekey_recursive_trans: deleted key '%s' from '%s'\n",
1038 path, parent->key->name));
1043 return werr;
1046 WERROR reg_deletekey_recursive(struct registry_key *parent,
1047 const char *path)
1049 return reg_deletekey_recursive_trans(parent, path, true);
1052 WERROR reg_deletesubkeys_recursive(struct registry_key *parent,
1053 const char *path)
1055 return reg_deletekey_recursive_trans(parent, path, false);