dsdb: Use WERR_DS_ATT_NOT_DEF_IN_SCHEMA for failed schema lookups
[Samba.git] / source3 / registry / reg_api.c
blobca990e22f85d5ab040ffd4e7c21a002ce64f0a69
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"
73 #include "reg_parse_internal.h"
75 #undef DBGC_CLASS
76 #define DBGC_CLASS DBGC_REGISTRY
79 /**********************************************************************
80 * Helper functions
81 **********************************************************************/
83 static WERROR fill_value_cache(struct registry_key *key)
85 WERROR werr;
87 if (key->values != NULL) {
88 if (!reg_values_need_update(key->key, key->values)) {
89 return WERR_OK;
93 TALLOC_FREE(key->values);
94 werr = regval_ctr_init(key, &(key->values));
95 W_ERROR_NOT_OK_RETURN(werr);
97 if (fetch_reg_values(key->key, key->values) == -1) {
98 TALLOC_FREE(key->values);
99 return WERR_BADFILE;
102 return WERR_OK;
105 static WERROR fill_subkey_cache(struct registry_key *key)
107 WERROR werr;
109 if (key->subkeys != NULL) {
110 if (!reg_subkeys_need_update(key->key, key->subkeys)) {
111 return WERR_OK;
115 TALLOC_FREE(key->subkeys);
116 werr = regsubkey_ctr_init(key, &(key->subkeys));
117 W_ERROR_NOT_OK_RETURN(werr);
119 if (fetch_reg_keys(key->key, key->subkeys) == -1) {
120 TALLOC_FREE(key->subkeys);
121 return WERR_BADFILE;
124 return WERR_OK;
127 static int regkey_destructor(struct registry_key_handle *key)
129 return regdb_close();
132 static WERROR regkey_open_onelevel(TALLOC_CTX *mem_ctx,
133 struct registry_key *parent,
134 const char *name,
135 const struct security_token *token,
136 uint32 access_desired,
137 struct registry_key **pregkey)
139 WERROR result = WERR_OK;
140 struct registry_key *regkey;
141 struct registry_key_handle *key;
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)))
151 result = WERR_NOMEM;
152 goto done;
155 result = regdb_open();
156 if (!(W_ERROR_IS_OK(result))) {
157 goto done;
160 key = regkey->key;
161 talloc_set_destructor(key, regkey_destructor);
163 /* initialization */
165 key->type = REG_KEY_GENERIC;
167 if (name[0] == '\0') {
169 * Open a copy of the parent key
171 if (!parent) {
172 result = WERR_BADFILE;
173 goto done;
175 key->name = talloc_strdup(key, parent->key->name);
177 else {
179 * Normal subkey open
181 key->name = talloc_asprintf(key, "%s%s%s",
182 parent ? parent->key->name : "",
183 parent ? "\\": "",
184 name);
187 if (key->name == NULL) {
188 result = WERR_NOMEM;
189 goto done;
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 key->ops = reghook_cache_find( key->name );
200 if (key->ops == NULL) {
201 DEBUG(0,("reg_open_onelevel: Failed to assign "
202 "registry_ops to [%s]\n", key->name ));
203 result = WERR_BADFILE;
204 goto done;
207 /* FIXME: Existence is currently checked by fetching the subkeys */
209 result = fill_subkey_cache(regkey);
210 if (!W_ERROR_IS_OK(result)) {
211 goto done;
214 if ( !regkey_access_check( key, access_desired, &key->access_granted,
215 token ) ) {
216 result = WERR_ACCESS_DENIED;
217 goto done;
220 *pregkey = regkey;
221 result = WERR_OK;
223 done:
224 if ( !W_ERROR_IS_OK(result) ) {
225 TALLOC_FREE(regkey);
228 return result;
231 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
232 uint32 desired_access,
233 const struct security_token *token,
234 struct registry_key **pkey)
236 const struct hive_info *hi;
237 SMB_ASSERT(hive != NULL);
238 SMB_ASSERT(strchr(hive, '\\') == NULL);
240 hi = hive_info(hive);
241 if (hi == NULL) {
242 return WERR_BADFILE;
245 return regkey_open_onelevel(mem_ctx, NULL, hi->short_name, token,
246 desired_access, 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;
261 size_t len;
262 TALLOC_CTX *frame = talloc_stackframe();
264 path = talloc_strdup(frame, name);
265 if (path == NULL) {
266 err = WERR_NOMEM;
267 goto error;
270 len = strlen(path);
272 if ((len > 0) && (path[len-1] == '\\')) {
273 path[len-1] = '\0';
276 while ((p = strchr(path, '\\')) != NULL) {
277 char *name_component;
278 struct registry_key *tmp;
280 name_component = talloc_strndup(frame, path, (p - path));
281 if (name_component == NULL) {
282 err = WERR_NOMEM;
283 goto error;
286 err = regkey_open_onelevel(frame, direct_parent,
287 name_component, parent->token,
288 KEY_ENUMERATE_SUB_KEYS, &tmp);
290 if (!W_ERROR_IS_OK(err)) {
291 goto error;
294 direct_parent = tmp;
295 path = p+1;
298 err = regkey_open_onelevel(mem_ctx, direct_parent, path, parent->token,
299 desired_access, pkey);
301 error:
302 talloc_free(frame);
303 return err;
306 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
307 uint32 idx, char **name, NTTIME *last_write_time)
309 WERROR err;
311 if (!(key->key->access_granted & KEY_ENUMERATE_SUB_KEYS)) {
312 return WERR_ACCESS_DENIED;
315 err = fill_subkey_cache(key);
316 if (!W_ERROR_IS_OK(err)) {
317 return err;
320 if (idx >= regsubkey_ctr_numkeys(key->subkeys)) {
321 return WERR_NO_MORE_ITEMS;
324 if (!(*name = talloc_strdup(mem_ctx,
325 regsubkey_ctr_specific_key(key->subkeys, idx))))
327 return WERR_NOMEM;
330 if (last_write_time) {
331 *last_write_time = 0;
334 return WERR_OK;
337 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
338 uint32 idx, char **pname, struct registry_value **pval)
340 struct registry_value *val;
341 struct regval_blob *blob;
342 WERROR err;
344 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
345 return WERR_ACCESS_DENIED;
348 err = fill_value_cache(key);
349 if (!(W_ERROR_IS_OK(err))) {
350 return err;
353 if (idx >= regval_ctr_numvals(key->values)) {
354 return WERR_NO_MORE_ITEMS;
357 blob = regval_ctr_specific_value(key->values, idx);
359 val = talloc_zero(mem_ctx, struct registry_value);
360 if (val == NULL) {
361 return WERR_NOMEM;
364 val->type = regval_type(blob);
365 val->data = data_blob_talloc(mem_ctx, regval_data_p(blob), regval_size(blob));
367 if (pname
368 && !(*pname = talloc_strdup(
369 mem_ctx, regval_name(blob)))) {
370 TALLOC_FREE(val);
371 return WERR_NOMEM;
374 *pval = val;
375 return WERR_OK;
378 static WERROR reg_enumvalue_nocachefill(TALLOC_CTX *mem_ctx,
379 struct registry_key *key,
380 uint32 idx, char **pname,
381 struct registry_value **pval)
383 struct registry_value *val;
384 struct regval_blob *blob;
386 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
387 return WERR_ACCESS_DENIED;
390 if (idx >= regval_ctr_numvals(key->values)) {
391 return WERR_NO_MORE_ITEMS;
394 blob = regval_ctr_specific_value(key->values, idx);
396 val = talloc_zero(mem_ctx, struct registry_value);
397 if (val == NULL) {
398 return WERR_NOMEM;
401 val->type = regval_type(blob);
402 val->data = data_blob_talloc(mem_ctx, regval_data_p(blob), regval_size(blob));
404 if (pname
405 && !(*pname = talloc_strdup(
406 mem_ctx, regval_name(blob)))) {
407 TALLOC_FREE(val);
408 return WERR_NOMEM;
411 *pval = val;
412 return WERR_OK;
415 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
416 const char *name, struct registry_value **pval)
418 WERROR err;
419 uint32 i;
421 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
422 return WERR_ACCESS_DENIED;
425 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
426 return err;
429 for (i=0; i < regval_ctr_numvals(key->values); i++) {
430 struct regval_blob *blob;
431 blob = regval_ctr_specific_value(key->values, i);
432 if (strequal(regval_name(blob), name)) {
434 * don't use reg_enumvalue here:
435 * re-reading the values from the disk
436 * would change the indexing and break
437 * this function.
439 return reg_enumvalue_nocachefill(mem_ctx, key, i,
440 NULL, pval);
444 return WERR_BADFILE;
447 WERROR reg_querymultiplevalues(TALLOC_CTX *mem_ctx,
448 struct registry_key *key,
449 uint32_t num_names,
450 const char **names,
451 uint32_t *pnum_vals,
452 struct registry_value **pvals)
454 WERROR err;
455 uint32_t i, n, found = 0;
456 struct registry_value *vals;
458 if (num_names == 0) {
459 return WERR_OK;
462 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
463 return WERR_ACCESS_DENIED;
466 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
467 return err;
470 vals = talloc_zero_array(mem_ctx, struct registry_value, num_names);
471 if (vals == NULL) {
472 return WERR_NOMEM;
475 for (n=0; n < num_names; n++) {
476 for (i=0; i < regval_ctr_numvals(key->values); i++) {
477 struct regval_blob *blob;
478 blob = regval_ctr_specific_value(key->values, i);
479 if (strequal(regval_name(blob), names[n])) {
480 struct registry_value *v;
481 err = reg_enumvalue(mem_ctx, key, i, NULL, &v);
482 if (!W_ERROR_IS_OK(err)) {
483 return err;
485 vals[n] = *v;
486 found++;
491 *pvals = vals;
492 *pnum_vals = found;
494 return WERR_OK;
497 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
498 uint32_t *max_subkeylen, uint32_t *max_subkeysize,
499 uint32_t *num_values, uint32_t *max_valnamelen,
500 uint32_t *max_valbufsize, uint32_t *secdescsize,
501 NTTIME *last_changed_time)
503 uint32 i, max_size;
504 size_t max_len;
505 TALLOC_CTX *mem_ctx;
506 WERROR err;
507 struct security_descriptor *secdesc;
509 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
510 return WERR_ACCESS_DENIED;
513 if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
514 !W_ERROR_IS_OK(fill_value_cache(key))) {
515 return WERR_BADFILE;
518 max_len = 0;
519 for (i=0; i< regsubkey_ctr_numkeys(key->subkeys); i++) {
520 max_len = MAX(max_len,
521 strlen(regsubkey_ctr_specific_key(key->subkeys, i)));
524 *num_subkeys = regsubkey_ctr_numkeys(key->subkeys);
525 *max_subkeylen = max_len;
526 *max_subkeysize = 0; /* Class length? */
528 max_len = 0;
529 max_size = 0;
530 for (i=0; i < regval_ctr_numvals(key->values); i++) {
531 struct regval_blob *blob;
532 blob = regval_ctr_specific_value(key->values, i);
533 max_len = MAX(max_len, strlen(regval_name(blob)));
534 max_size = MAX(max_size, regval_size(blob));
537 *num_values = regval_ctr_numvals(key->values);
538 *max_valnamelen = max_len;
539 *max_valbufsize = max_size;
541 if (!(mem_ctx = talloc_new(key))) {
542 return WERR_NOMEM;
545 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
546 if (!W_ERROR_IS_OK(err)) {
547 TALLOC_FREE(mem_ctx);
548 return err;
551 *secdescsize = ndr_size_security_descriptor(secdesc, 0);
552 TALLOC_FREE(mem_ctx);
554 *last_changed_time = 0;
556 return WERR_OK;
559 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
560 const char *subkeypath, uint32 desired_access,
561 struct registry_key **pkey,
562 enum winreg_CreateAction *paction)
564 struct registry_key *key = parent;
565 TALLOC_CTX *mem_ctx;
566 char *path, *end;
567 WERROR err;
568 uint32_t access_granted;
570 mem_ctx = talloc_new(ctx);
571 if (mem_ctx == NULL) {
572 return WERR_NOMEM;
575 path = talloc_strdup(mem_ctx, subkeypath);
576 if (path == NULL) {
577 err = WERR_NOMEM;
578 goto done;
581 err = regdb_transaction_start();
582 if (!W_ERROR_IS_OK(err)) {
583 DEBUG(0, ("reg_createkey: failed to start transaction: %s\n",
584 win_errstr(err)));
585 goto done;
588 while ((end = strchr(path, '\\')) != NULL) {
589 struct registry_key *tmp;
590 enum winreg_CreateAction action;
592 *end = '\0';
594 err = reg_createkey(mem_ctx, key, path,
595 KEY_ENUMERATE_SUB_KEYS, &tmp, &action);
596 if (!W_ERROR_IS_OK(err)) {
597 goto trans_done;
600 if (key != parent) {
601 TALLOC_FREE(key);
604 key = tmp;
605 path = end+1;
609 * At this point, "path" contains the one-element subkey of "key". We
610 * can try to open it.
613 err = reg_openkey(ctx, key, path, desired_access, pkey);
614 if (W_ERROR_IS_OK(err)) {
615 if (paction != NULL) {
616 *paction = REG_OPENED_EXISTING_KEY;
618 goto trans_done;
621 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
623 * Something but "notfound" has happened, so bail out
625 goto trans_done;
629 * We may (e.g. in the iteration) have opened the key with ENUM_SUBKEY.
630 * Instead of re-opening the key with CREATE_SUB_KEY, we simply
631 * duplicate the access check here and skip the expensive full open.
633 if (!regkey_access_check(key->key, KEY_CREATE_SUB_KEY, &access_granted,
634 key->token))
636 err = WERR_ACCESS_DENIED;
637 goto done;
641 * Actually create the subkey
644 err = create_reg_subkey(key->key, path);
645 if (!W_ERROR_IS_OK(err)) {
646 goto trans_done;
650 * Now open the newly created key
653 err = reg_openkey(ctx, key, path, desired_access, pkey);
654 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
655 *paction = REG_CREATED_NEW_KEY;
658 trans_done:
659 if (W_ERROR_IS_OK(err)) {
660 err = regdb_transaction_commit();
661 if (!W_ERROR_IS_OK(err)) {
662 DEBUG(0, ("reg_createkey: Error committing transaction: %s\n", win_errstr(err)));
664 } else {
665 WERROR err1 = regdb_transaction_cancel();
666 if (!W_ERROR_IS_OK(err1)) {
667 DEBUG(0, ("reg_createkey: Error cancelling transaction: %s\n", win_errstr(err1)));
671 done:
672 TALLOC_FREE(mem_ctx);
673 return err;
676 static WERROR reg_deletekey_internal(TALLOC_CTX *mem_ctx,
677 struct registry_key *parent,
678 const char *path, bool lazy)
680 WERROR err;
681 char *name, *end;
682 struct registry_key *key;
683 name = talloc_strdup(mem_ctx, path);
684 if (name == NULL) {
685 err = WERR_NOMEM;
686 goto done;
689 /* no subkeys - proceed with delete */
690 end = strrchr(name, '\\');
691 if (end != NULL) {
692 *end = '\0';
694 err = reg_openkey(mem_ctx, parent, name,
695 KEY_CREATE_SUB_KEY, &key);
696 W_ERROR_NOT_OK_GOTO_DONE(err);
698 parent = key;
699 name = end+1;
702 if (name[0] == '\0') {
703 err = WERR_INVALID_PARAM;
704 goto done;
707 err = delete_reg_subkey(parent->key, name, lazy);
709 done:
710 return err;
713 WERROR reg_deletekey(struct registry_key *parent, const char *path)
715 WERROR err;
716 struct registry_key *key;
717 TALLOC_CTX *mem_ctx = talloc_stackframe();
719 /* check if the key has subkeys */
720 err = reg_openkey(mem_ctx, parent, path, REG_KEY_READ, &key);
721 W_ERROR_NOT_OK_GOTO_DONE(err);
723 err = regdb_transaction_start();
724 if (!W_ERROR_IS_OK(err)) {
725 DEBUG(0, ("reg_deletekey: Error starting transaction: %s\n",
726 win_errstr(err)));
727 goto done;
730 err = fill_subkey_cache(key);
731 if (!W_ERROR_IS_OK(err)) {
732 goto trans_done;
735 if (regsubkey_ctr_numkeys(key->subkeys) > 0) {
736 err = WERR_ACCESS_DENIED;
737 goto trans_done;
739 err = reg_deletekey_internal(mem_ctx, parent, path, false);
741 trans_done:
742 if (W_ERROR_IS_OK(err)) {
743 err = regdb_transaction_commit();
744 if (!W_ERROR_IS_OK(err)) {
745 DEBUG(0, ("reg_deletekey: Error committing transaction: %s\n", win_errstr(err)));
747 } else {
748 WERROR err1 = regdb_transaction_cancel();
749 if (!W_ERROR_IS_OK(err1)) {
750 DEBUG(0, ("reg_deletekey: Error cancelling transaction: %s\n", win_errstr(err1)));
754 done:
755 TALLOC_FREE(mem_ctx);
756 return err;
760 WERROR reg_setvalue(struct registry_key *key, const char *name,
761 const struct registry_value *val)
763 struct regval_blob *existing;
764 WERROR err;
765 int res;
767 if (!(key->key->access_granted & KEY_SET_VALUE)) {
768 return WERR_ACCESS_DENIED;
771 err = regdb_transaction_start();
772 if (!W_ERROR_IS_OK(err)) {
773 DEBUG(0, ("reg_setvalue: Failed to start transaction: %s\n",
774 win_errstr(err)));
775 return err;
778 err = fill_value_cache(key);
779 if (!W_ERROR_IS_OK(err)) {
780 DEBUG(0, ("reg_setvalue: Error filling value cache: %s\n", win_errstr(err)));
781 goto done;
784 existing = regval_ctr_getvalue(key->values, name);
786 if ((existing != NULL) &&
787 (regval_size(existing) == val->data.length) &&
788 (memcmp(regval_data_p(existing), val->data.data,
789 val->data.length) == 0))
791 err = WERR_OK;
792 goto done;
795 res = regval_ctr_addvalue(key->values, name, val->type,
796 val->data.data, val->data.length);
798 if (res == 0) {
799 TALLOC_FREE(key->values);
800 err = WERR_NOMEM;
801 goto done;
804 if (!store_reg_values(key->key, key->values)) {
805 TALLOC_FREE(key->values);
806 DEBUG(0, ("reg_setvalue: store_reg_values failed\n"));
807 err = WERR_REG_IO_FAILURE;
808 goto done;
811 err = WERR_OK;
813 done:
814 if (W_ERROR_IS_OK(err)) {
815 err = regdb_transaction_commit();
816 if (!W_ERROR_IS_OK(err)) {
817 DEBUG(0, ("reg_setvalue: Error committing transaction: %s\n", win_errstr(err)));
819 } else {
820 WERROR err1 = regdb_transaction_cancel();
821 if (!W_ERROR_IS_OK(err1)) {
822 DEBUG(0, ("reg_setvalue: Error cancelling transaction: %s\n", win_errstr(err1)));
826 return err;
829 static WERROR reg_value_exists(struct registry_key *key, const char *name)
831 struct regval_blob *blob;
833 blob = regval_ctr_getvalue(key->values, name);
835 if (blob == NULL) {
836 return WERR_BADFILE;
837 } else {
838 return WERR_OK;
842 WERROR reg_deletevalue(struct registry_key *key, const char *name)
844 WERROR err;
846 if (!(key->key->access_granted & KEY_SET_VALUE)) {
847 return WERR_ACCESS_DENIED;
850 err = regdb_transaction_start();
851 if (!W_ERROR_IS_OK(err)) {
852 DEBUG(0, ("reg_deletevalue: Failed to start transaction: %s\n",
853 win_errstr(err)));
854 return err;
857 err = fill_value_cache(key);
858 if (!W_ERROR_IS_OK(err)) {
859 DEBUG(0, ("reg_deletevalue; Error filling value cache: %s\n",
860 win_errstr(err)));
861 goto done;
864 err = reg_value_exists(key, name);
865 if (!W_ERROR_IS_OK(err)) {
866 goto done;
869 regval_ctr_delvalue(key->values, name);
871 if (!store_reg_values(key->key, key->values)) {
872 TALLOC_FREE(key->values);
873 err = WERR_REG_IO_FAILURE;
874 DEBUG(0, ("reg_deletevalue: store_reg_values failed\n"));
875 goto done;
878 err = WERR_OK;
880 done:
881 if (W_ERROR_IS_OK(err)) {
882 err = regdb_transaction_commit();
883 if (!W_ERROR_IS_OK(err)) {
884 DEBUG(0, ("reg_deletevalue: Error committing transaction: %s\n", win_errstr(err)));
886 } else {
887 WERROR err1 = regdb_transaction_cancel();
888 if (!W_ERROR_IS_OK(err1)) {
889 DEBUG(0, ("reg_deletevalue: Error cancelling transaction: %s\n", win_errstr(err1)));
893 return err;
896 WERROR reg_getkeysecurity(TALLOC_CTX *mem_ctx, struct registry_key *key,
897 struct security_descriptor **psecdesc)
899 return regkey_get_secdesc(mem_ctx, key->key, psecdesc);
902 WERROR reg_setkeysecurity(struct registry_key *key,
903 struct security_descriptor *psecdesc)
905 return regkey_set_secdesc(key->key, psecdesc);
908 WERROR reg_getversion(uint32_t *version)
910 if (version == NULL) {
911 return WERR_INVALID_PARAM;
914 *version = 0x00000005; /* Windows 2000 registry API version */
915 return WERR_OK;
918 /**********************************************************************
919 * Higher level utility functions
920 **********************************************************************/
922 WERROR reg_deleteallvalues(struct registry_key *key)
924 WERROR err;
925 int i;
927 if (!(key->key->access_granted & KEY_SET_VALUE)) {
928 return WERR_ACCESS_DENIED;
931 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
932 return err;
935 for (i=0; i < regval_ctr_numvals(key->values); i++) {
936 struct regval_blob *blob;
937 blob = regval_ctr_specific_value(key->values, i);
938 regval_ctr_delvalue(key->values, regval_name(blob));
941 if (!store_reg_values(key->key, key->values)) {
942 TALLOC_FREE(key->values);
943 return WERR_REG_IO_FAILURE;
946 return WERR_OK;
950 * Utility function to delete a registry key with all its subkeys.
951 * Note that reg_deletekey returns ACCESS_DENIED when called on a
952 * key that has subkeys.
954 static WERROR reg_deletekey_recursive_internal(struct registry_key *parent,
955 const char *path,
956 bool del_key, bool lazy)
958 WERROR werr = WERR_OK;
959 struct registry_key *key;
960 char *subkey_name = NULL;
961 uint32 i;
962 TALLOC_CTX *mem_ctx = talloc_stackframe();
964 DEBUG(5, ("reg_deletekey_recursive_internal: deleting '%s' from '%s'\n",
965 path, parent->key->name));
967 /* recurse through subkeys first */
968 werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
969 if (!W_ERROR_IS_OK(werr)) {
970 DEBUG(3, ("reg_deletekey_recursive_internal: error opening "
971 "subkey '%s' of '%s': '%s'\n",
972 path, parent->key->name, win_errstr(werr)));
973 goto done;
976 werr = fill_subkey_cache(key);
977 W_ERROR_NOT_OK_GOTO_DONE(werr);
980 * loop from top to bottom for perfomance:
981 * this way, we need to rehash the regsubkey containers less
983 for (i = regsubkey_ctr_numkeys(key->subkeys) ; i > 0; i--) {
984 subkey_name = regsubkey_ctr_specific_key(key->subkeys, i-1);
985 werr = reg_deletekey_recursive_internal(key, subkey_name, true, del_key);
986 W_ERROR_NOT_OK_GOTO_DONE(werr);
989 if (del_key) {
990 /* now delete the actual key */
991 werr = reg_deletekey_internal(mem_ctx, parent, path, lazy);
994 done:
996 DEBUG(5, ("reg_deletekey_recursive_internal: done deleting '%s' from "
997 "'%s': %s\n",
998 path, parent->key->name, win_errstr(werr)));
999 TALLOC_FREE(mem_ctx);
1000 return werr;
1003 static WERROR reg_deletekey_recursive_trans(struct registry_key *parent,
1004 const char *path,
1005 bool del_key)
1007 WERROR werr;
1009 werr = regdb_transaction_start();
1010 if (!W_ERROR_IS_OK(werr)) {
1011 DEBUG(0, ("reg_deletekey_recursive_trans: "
1012 "error starting transaction: %s\n",
1013 win_errstr(werr)));
1014 return werr;
1017 werr = reg_deletekey_recursive_internal(parent, path, del_key, false);
1019 if (!W_ERROR_IS_OK(werr)) {
1020 WERROR werr2;
1021 DEBUG(W_ERROR_EQUAL(werr, WERR_BADFILE) ? 5 : 1,
1022 (__location__ ": failed to delete key '%s' from key "
1023 "'%s': %s\n", path, parent->key->name,
1024 win_errstr(werr)));
1026 werr2 = regdb_transaction_cancel();
1027 if (!W_ERROR_IS_OK(werr2)) {
1028 DEBUG(0, ("reg_deletekey_recursive_trans: "
1029 "error cancelling transaction: %s\n",
1030 win_errstr(werr2)));
1032 * return the original werr or the
1033 * error from cancelling the transaction?
1036 } else {
1037 werr = regdb_transaction_commit();
1038 if (!W_ERROR_IS_OK(werr)) {
1039 DEBUG(0, ("reg_deletekey_recursive_trans: "
1040 "error committing transaction: %s\n",
1041 win_errstr(werr)));
1042 } else {
1043 DEBUG(5, ("reg_reletekey_recursive_trans: deleted key '%s' from '%s'\n",
1044 path, parent->key->name));
1049 return werr;
1052 WERROR reg_deletekey_recursive(struct registry_key *parent,
1053 const char *path)
1055 return reg_deletekey_recursive_trans(parent, path, true);
1058 WERROR reg_deletesubkeys_recursive(struct registry_key *parent,
1059 const char *path)
1061 return reg_deletekey_recursive_trans(parent, path, false);