s3:registry untangle an assignment from the check in regkey_open_onelevel()
[Samba.git] / source3 / registry / reg_api.c
blobff70016ae25cde0996f3ed8f033fdf6d4d98985b
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_NO_MORE_ITEMS;
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;
141 struct regsubkey_ctr *subkeys = NULL;
143 DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name));
145 SMB_ASSERT(strchr(name, '\\') == NULL);
147 if (!(regkey = talloc_zero(mem_ctx, struct registry_key)) ||
148 !(regkey->token = dup_nt_token(regkey, token)) ||
149 !(regkey->key = talloc_zero(regkey, struct registry_key_handle)))
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 /* check if the path really exists; failed is indicated by -1 */
208 /* if the subkey count failed, bail out */
210 result = regsubkey_ctr_init(key, &subkeys);
211 if (!W_ERROR_IS_OK(result)) {
212 goto done;
215 if ( fetch_reg_keys( key, subkeys ) == -1 ) {
216 result = WERR_BADFILE;
217 goto done;
220 TALLOC_FREE( subkeys );
222 if ( !regkey_access_check( key, access_desired, &key->access_granted,
223 token ) ) {
224 result = WERR_ACCESS_DENIED;
225 goto done;
228 *pregkey = regkey;
229 result = WERR_OK;
231 done:
232 if ( !W_ERROR_IS_OK(result) ) {
233 TALLOC_FREE(regkey);
236 return result;
239 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
240 uint32 desired_access,
241 const struct security_token *token,
242 struct registry_key **pkey)
244 SMB_ASSERT(hive != NULL);
245 SMB_ASSERT(hive[0] != '\0');
246 SMB_ASSERT(strchr(hive, '\\') == NULL);
248 return regkey_open_onelevel(mem_ctx, NULL, hive, token, desired_access,
249 pkey);
253 /**********************************************************************
254 * The API functions
255 **********************************************************************/
257 WERROR reg_openkey(TALLOC_CTX *mem_ctx, struct registry_key *parent,
258 const char *name, uint32 desired_access,
259 struct registry_key **pkey)
261 struct registry_key *direct_parent = parent;
262 WERROR err;
263 char *p, *path, *to_free;
264 size_t len;
266 if (!(path = SMB_STRDUP(name))) {
267 return WERR_NOMEM;
269 to_free = path;
271 len = strlen(path);
273 if ((len > 0) && (path[len-1] == '\\')) {
274 path[len-1] = '\0';
277 while ((p = strchr(path, '\\')) != NULL) {
278 char *name_component;
279 struct registry_key *tmp;
281 if (!(name_component = SMB_STRNDUP(path, (p - path)))) {
282 err = WERR_NOMEM;
283 goto error;
286 err = regkey_open_onelevel(mem_ctx, direct_parent,
287 name_component, parent->token,
288 KEY_ENUMERATE_SUB_KEYS, &tmp);
289 SAFE_FREE(name_component);
291 if (!W_ERROR_IS_OK(err)) {
292 goto error;
294 if (direct_parent != parent) {
295 TALLOC_FREE(direct_parent);
298 direct_parent = tmp;
299 path = p+1;
302 err = regkey_open_onelevel(mem_ctx, direct_parent, path, parent->token,
303 desired_access, pkey);
304 error:
305 if (direct_parent != parent) {
306 TALLOC_FREE(direct_parent);
308 SAFE_FREE(to_free);
309 return err;
312 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
313 uint32 idx, char **name, NTTIME *last_write_time)
315 WERROR err;
317 if (!(key->key->access_granted & KEY_ENUMERATE_SUB_KEYS)) {
318 return WERR_ACCESS_DENIED;
321 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
322 return err;
325 if (idx >= regsubkey_ctr_numkeys(key->subkeys)) {
326 return WERR_NO_MORE_ITEMS;
329 if (!(*name = talloc_strdup(mem_ctx,
330 regsubkey_ctr_specific_key(key->subkeys, idx))))
332 return WERR_NOMEM;
335 if (last_write_time) {
336 *last_write_time = 0;
339 return WERR_OK;
342 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
343 uint32 idx, char **pname, struct registry_value **pval)
345 struct registry_value *val;
346 struct regval_blob *blob;
347 WERROR err;
349 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
350 return WERR_ACCESS_DENIED;
353 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
354 return err;
357 if (idx >= regval_ctr_numvals(key->values)) {
358 return WERR_NO_MORE_ITEMS;
361 blob = regval_ctr_specific_value(key->values, idx);
363 val = talloc_zero(mem_ctx, struct registry_value);
364 if (val == NULL) {
365 return WERR_NOMEM;
368 val->type = regval_type(blob);
369 val->data = data_blob_talloc(mem_ctx, regval_data_p(blob), regval_size(blob));
371 if (pname
372 && !(*pname = talloc_strdup(
373 mem_ctx, regval_name(blob)))) {
374 TALLOC_FREE(val);
375 return WERR_NOMEM;
378 *pval = val;
379 return WERR_OK;
382 static WERROR reg_enumvalue_nocachefill(TALLOC_CTX *mem_ctx,
383 struct registry_key *key,
384 uint32 idx, char **pname,
385 struct registry_value **pval)
387 struct registry_value *val;
388 struct regval_blob *blob;
390 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
391 return WERR_ACCESS_DENIED;
394 if (idx >= regval_ctr_numvals(key->values)) {
395 return WERR_NO_MORE_ITEMS;
398 blob = regval_ctr_specific_value(key->values, idx);
400 val = talloc_zero(mem_ctx, struct registry_value);
401 if (val == NULL) {
402 return WERR_NOMEM;
405 val->type = regval_type(blob);
406 val->data = data_blob_talloc(mem_ctx, regval_data_p(blob), regval_size(blob));
408 if (pname
409 && !(*pname = talloc_strdup(
410 mem_ctx, regval_name(blob)))) {
411 TALLOC_FREE(val);
412 return WERR_NOMEM;
415 *pval = val;
416 return WERR_OK;
419 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
420 const char *name, struct registry_value **pval)
422 WERROR err;
423 uint32 i;
425 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
426 return WERR_ACCESS_DENIED;
429 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
430 return err;
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), name)) {
438 * don't use reg_enumvalue here:
439 * re-reading the values from the disk
440 * would change the indexing and break
441 * this function.
443 return reg_enumvalue_nocachefill(mem_ctx, key, i,
444 NULL, pval);
448 return WERR_BADFILE;
451 WERROR reg_querymultiplevalues(TALLOC_CTX *mem_ctx,
452 struct registry_key *key,
453 uint32_t num_names,
454 const char **names,
455 uint32_t *pnum_vals,
456 struct registry_value **pvals)
458 WERROR err;
459 uint32_t i, n, found = 0;
460 struct registry_value *vals;
462 if (num_names == 0) {
463 return WERR_OK;
466 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
467 return WERR_ACCESS_DENIED;
470 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
471 return err;
474 vals = talloc_zero_array(mem_ctx, struct registry_value, num_names);
475 if (vals == NULL) {
476 return WERR_NOMEM;
479 for (n=0; n < num_names; n++) {
480 for (i=0; i < regval_ctr_numvals(key->values); i++) {
481 struct regval_blob *blob;
482 blob = regval_ctr_specific_value(key->values, i);
483 if (strequal(regval_name(blob), names[n])) {
484 struct registry_value *v;
485 err = reg_enumvalue(mem_ctx, key, i, NULL, &v);
486 if (!W_ERROR_IS_OK(err)) {
487 return err;
489 vals[n] = *v;
490 found++;
495 *pvals = vals;
496 *pnum_vals = found;
498 return WERR_OK;
501 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
502 uint32_t *max_subkeylen, uint32_t *max_subkeysize,
503 uint32_t *num_values, uint32_t *max_valnamelen,
504 uint32_t *max_valbufsize, uint32_t *secdescsize,
505 NTTIME *last_changed_time)
507 uint32 i, max_size;
508 size_t max_len;
509 TALLOC_CTX *mem_ctx;
510 WERROR err;
511 struct security_descriptor *secdesc;
513 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
514 return WERR_ACCESS_DENIED;
517 if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
518 !W_ERROR_IS_OK(fill_value_cache(key))) {
519 return WERR_BADFILE;
522 max_len = 0;
523 for (i=0; i< regsubkey_ctr_numkeys(key->subkeys); i++) {
524 max_len = MAX(max_len,
525 strlen(regsubkey_ctr_specific_key(key->subkeys, i)));
528 *num_subkeys = regsubkey_ctr_numkeys(key->subkeys);
529 *max_subkeylen = max_len;
530 *max_subkeysize = 0; /* Class length? */
532 max_len = 0;
533 max_size = 0;
534 for (i=0; i < regval_ctr_numvals(key->values); i++) {
535 struct regval_blob *blob;
536 blob = regval_ctr_specific_value(key->values, i);
537 max_len = MAX(max_len, strlen(regval_name(blob)));
538 max_size = MAX(max_size, regval_size(blob));
541 *num_values = regval_ctr_numvals(key->values);
542 *max_valnamelen = max_len;
543 *max_valbufsize = max_size;
545 if (!(mem_ctx = talloc_new(key))) {
546 return WERR_NOMEM;
549 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
550 if (!W_ERROR_IS_OK(err)) {
551 TALLOC_FREE(mem_ctx);
552 return err;
555 *secdescsize = ndr_size_security_descriptor(secdesc, 0);
556 TALLOC_FREE(mem_ctx);
558 *last_changed_time = 0;
560 return WERR_OK;
563 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
564 const char *subkeypath, uint32 desired_access,
565 struct registry_key **pkey,
566 enum winreg_CreateAction *paction)
568 struct registry_key *key = parent;
569 struct registry_key *create_parent;
570 TALLOC_CTX *mem_ctx;
571 char *path, *end;
572 WERROR err;
574 mem_ctx = talloc_new(ctx);
575 if (mem_ctx == NULL) {
576 return WERR_NOMEM;
579 path = talloc_strdup(mem_ctx, subkeypath);
580 if (path == NULL) {
581 err = WERR_NOMEM;
582 goto done;
585 err = regdb_transaction_start();
586 if (!W_ERROR_IS_OK(err)) {
587 DEBUG(0, ("reg_createkey: failed to start transaction: %s\n",
588 win_errstr(err)));
589 goto done;
592 while ((end = strchr(path, '\\')) != NULL) {
593 struct registry_key *tmp;
594 enum winreg_CreateAction action;
596 *end = '\0';
598 err = reg_createkey(mem_ctx, key, path,
599 KEY_ENUMERATE_SUB_KEYS, &tmp, &action);
600 if (!W_ERROR_IS_OK(err)) {
601 goto trans_done;
604 if (key != parent) {
605 TALLOC_FREE(key);
608 key = tmp;
609 path = end+1;
613 * At this point, "path" contains the one-element subkey of "key". We
614 * can try to open it.
617 err = reg_openkey(ctx, key, path, desired_access, pkey);
618 if (W_ERROR_IS_OK(err)) {
619 if (paction != NULL) {
620 *paction = REG_OPENED_EXISTING_KEY;
622 goto trans_done;
625 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
627 * Something but "notfound" has happened, so bail out
629 goto trans_done;
633 * We have to make a copy of the current key, as we opened it only
634 * with ENUM_SUBKEY access.
637 err = reg_openkey(mem_ctx, key, "", KEY_CREATE_SUB_KEY,
638 &create_parent);
639 if (!W_ERROR_IS_OK(err)) {
640 goto trans_done;
644 * Actually create the subkey
647 err = fill_subkey_cache(create_parent);
648 if (!W_ERROR_IS_OK(err)) {
649 goto trans_done;
652 err = create_reg_subkey(key->key, path);
653 if (!W_ERROR_IS_OK(err)) {
654 goto trans_done;
658 * Now open the newly created key
661 err = reg_openkey(ctx, create_parent, path, desired_access, pkey);
662 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
663 *paction = REG_CREATED_NEW_KEY;
666 trans_done:
667 if (W_ERROR_IS_OK(err)) {
668 err = regdb_transaction_commit();
669 if (!W_ERROR_IS_OK(err)) {
670 DEBUG(0, ("reg_createkey: Error committing transaction: %s\n", win_errstr(err)));
672 } else {
673 WERROR err1 = regdb_transaction_cancel();
674 if (!W_ERROR_IS_OK(err1)) {
675 DEBUG(0, ("reg_createkey: Error cancelling transaction: %s\n", win_errstr(err1)));
679 done:
680 TALLOC_FREE(mem_ctx);
681 return err;
684 static WERROR reg_deletekey_internal(TALLOC_CTX *mem_ctx,
685 struct registry_key *parent,
686 const char *path, bool lazy)
688 WERROR err;
689 char *name, *end;
690 struct registry_key *key;
691 name = talloc_strdup(mem_ctx, path);
692 if (name == NULL) {
693 err = WERR_NOMEM;
694 goto done;
697 /* no subkeys - proceed with delete */
698 end = strrchr(name, '\\');
699 if (end != NULL) {
700 *end = '\0';
702 err = reg_openkey(mem_ctx, parent, name,
703 KEY_CREATE_SUB_KEY, &key);
704 W_ERROR_NOT_OK_GOTO_DONE(err);
706 parent = key;
707 name = end+1;
710 if (name[0] == '\0') {
711 err = WERR_INVALID_PARAM;
712 goto done;
715 err = delete_reg_subkey(parent->key, name, lazy);
717 done:
718 return err;
721 WERROR reg_deletekey(struct registry_key *parent, const char *path)
723 WERROR err;
724 struct registry_key *key;
725 TALLOC_CTX *mem_ctx = talloc_stackframe();
727 /* check if the key has subkeys */
728 err = reg_openkey(mem_ctx, parent, path, REG_KEY_READ, &key);
729 W_ERROR_NOT_OK_GOTO_DONE(err);
731 err = regdb_transaction_start();
732 if (!W_ERROR_IS_OK(err)) {
733 DEBUG(0, ("reg_deletekey: Error starting transaction: %s\n",
734 win_errstr(err)));
735 goto done;
738 err = fill_subkey_cache(key);
739 if (!W_ERROR_IS_OK(err)) {
740 goto trans_done;
743 if (regsubkey_ctr_numkeys(key->subkeys) > 0) {
744 err = WERR_ACCESS_DENIED;
745 goto trans_done;
747 err = reg_deletekey_internal(mem_ctx, parent, path, false);
749 trans_done:
750 if (W_ERROR_IS_OK(err)) {
751 err = regdb_transaction_commit();
752 if (!W_ERROR_IS_OK(err)) {
753 DEBUG(0, ("reg_deletekey: Error committing transaction: %s\n", win_errstr(err)));
755 } else {
756 WERROR err1 = regdb_transaction_cancel();
757 if (!W_ERROR_IS_OK(err1)) {
758 DEBUG(0, ("reg_deletekey: Error cancelling transaction: %s\n", win_errstr(err1)));
762 done:
763 TALLOC_FREE(mem_ctx);
764 return err;
768 WERROR reg_setvalue(struct registry_key *key, const char *name,
769 const struct registry_value *val)
771 struct regval_blob *existing;
772 WERROR err;
773 int res;
775 if (!(key->key->access_granted & KEY_SET_VALUE)) {
776 return WERR_ACCESS_DENIED;
779 err = regdb_transaction_start();
780 if (!W_ERROR_IS_OK(err)) {
781 DEBUG(0, ("reg_setvalue: Failed to start transaction: %s\n",
782 win_errstr(err)));
783 return err;
786 err = fill_value_cache(key);
787 if (!W_ERROR_IS_OK(err)) {
788 DEBUG(0, ("reg_setvalue: Error filling value cache: %s\n", win_errstr(err)));
789 goto done;
792 existing = regval_ctr_getvalue(key->values, name);
794 if ((existing != NULL) &&
795 (regval_size(existing) == val->data.length) &&
796 (memcmp(regval_data_p(existing), val->data.data,
797 val->data.length) == 0))
799 err = WERR_OK;
800 goto done;
803 res = regval_ctr_addvalue(key->values, name, val->type,
804 val->data.data, val->data.length);
806 if (res == 0) {
807 TALLOC_FREE(key->values);
808 err = WERR_NOMEM;
809 goto done;
812 if (!store_reg_values(key->key, key->values)) {
813 TALLOC_FREE(key->values);
814 DEBUG(0, ("reg_setvalue: store_reg_values failed\n"));
815 err = WERR_REG_IO_FAILURE;
816 goto done;
819 err = WERR_OK;
821 done:
822 if (W_ERROR_IS_OK(err)) {
823 err = regdb_transaction_commit();
824 if (!W_ERROR_IS_OK(err)) {
825 DEBUG(0, ("reg_setvalue: Error committing transaction: %s\n", win_errstr(err)));
827 } else {
828 WERROR err1 = regdb_transaction_cancel();
829 if (!W_ERROR_IS_OK(err1)) {
830 DEBUG(0, ("reg_setvalue: Error cancelling transaction: %s\n", win_errstr(err1)));
834 return err;
837 static WERROR reg_value_exists(struct registry_key *key, const char *name)
839 struct regval_blob *blob;
841 blob = regval_ctr_getvalue(key->values, name);
843 if (blob == NULL) {
844 return WERR_BADFILE;
845 } else {
846 return WERR_OK;
850 WERROR reg_deletevalue(struct registry_key *key, const char *name)
852 WERROR err;
854 if (!(key->key->access_granted & KEY_SET_VALUE)) {
855 return WERR_ACCESS_DENIED;
858 err = regdb_transaction_start();
859 if (!W_ERROR_IS_OK(err)) {
860 DEBUG(0, ("reg_deletevalue: Failed to start transaction: %s\n",
861 win_errstr(err)));
862 return err;
865 err = fill_value_cache(key);
866 if (!W_ERROR_IS_OK(err)) {
867 DEBUG(0, ("reg_deletevalue; Error filling value cache: %s\n",
868 win_errstr(err)));
869 goto done;
872 err = reg_value_exists(key, name);
873 if (!W_ERROR_IS_OK(err)) {
874 goto done;
877 regval_ctr_delvalue(key->values, name);
879 if (!store_reg_values(key->key, key->values)) {
880 TALLOC_FREE(key->values);
881 err = WERR_REG_IO_FAILURE;
882 DEBUG(0, ("reg_deletevalue: store_reg_values failed\n"));
883 goto done;
886 err = WERR_OK;
888 done:
889 if (W_ERROR_IS_OK(err)) {
890 err = regdb_transaction_commit();
891 if (!W_ERROR_IS_OK(err)) {
892 DEBUG(0, ("reg_deletevalue: Error committing transaction: %s\n", win_errstr(err)));
894 } else {
895 WERROR err1 = regdb_transaction_cancel();
896 if (!W_ERROR_IS_OK(err1)) {
897 DEBUG(0, ("reg_deletevalue: Error cancelling transaction: %s\n", win_errstr(err1)));
901 return err;
904 WERROR reg_getkeysecurity(TALLOC_CTX *mem_ctx, struct registry_key *key,
905 struct security_descriptor **psecdesc)
907 return regkey_get_secdesc(mem_ctx, key->key, psecdesc);
910 WERROR reg_setkeysecurity(struct registry_key *key,
911 struct security_descriptor *psecdesc)
913 return regkey_set_secdesc(key->key, psecdesc);
916 WERROR reg_getversion(uint32_t *version)
918 if (version == NULL) {
919 return WERR_INVALID_PARAM;
922 *version = 0x00000005; /* Windows 2000 registry API version */
923 return WERR_OK;
926 /**********************************************************************
927 * Higher level utility functions
928 **********************************************************************/
930 WERROR reg_deleteallvalues(struct registry_key *key)
932 WERROR err;
933 int i;
935 if (!(key->key->access_granted & KEY_SET_VALUE)) {
936 return WERR_ACCESS_DENIED;
939 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
940 return err;
943 for (i=0; i < regval_ctr_numvals(key->values); i++) {
944 struct regval_blob *blob;
945 blob = regval_ctr_specific_value(key->values, i);
946 regval_ctr_delvalue(key->values, regval_name(blob));
949 if (!store_reg_values(key->key, key->values)) {
950 TALLOC_FREE(key->values);
951 return WERR_REG_IO_FAILURE;
954 return WERR_OK;
958 * Utility function to delete a registry key with all its subkeys.
959 * Note that reg_deletekey returns ACCESS_DENIED when called on a
960 * key that has subkeys.
962 static WERROR reg_deletekey_recursive_internal(struct registry_key *parent,
963 const char *path,
964 bool del_key, bool lazy)
966 WERROR werr = WERR_OK;
967 struct registry_key *key;
968 char *subkey_name = NULL;
969 uint32 i;
970 TALLOC_CTX *mem_ctx = talloc_stackframe();
972 DEBUG(5, ("reg_deletekey_recursive_internal: deleting '%s' from '%s'\n",
973 path, parent->key->name));
975 /* recurse through subkeys first */
976 werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
977 if (!W_ERROR_IS_OK(werr)) {
978 DEBUG(3, ("reg_deletekey_recursive_internal: error opening "
979 "subkey '%s' of '%s': '%s'\n",
980 path, parent->key->name, win_errstr(werr)));
981 goto done;
984 werr = fill_subkey_cache(key);
985 W_ERROR_NOT_OK_GOTO_DONE(werr);
988 * loop from top to bottom for perfomance:
989 * this way, we need to rehash the regsubkey containers less
991 for (i = regsubkey_ctr_numkeys(key->subkeys) ; i > 0; i--) {
992 subkey_name = regsubkey_ctr_specific_key(key->subkeys, i-1);
993 werr = reg_deletekey_recursive_internal(key, subkey_name, true, del_key);
994 W_ERROR_NOT_OK_GOTO_DONE(werr);
997 if (del_key) {
998 /* now delete the actual key */
999 werr = reg_deletekey_internal(mem_ctx, parent, path, lazy);
1002 done:
1004 DEBUG(5, ("reg_deletekey_recursive_internal: done deleting '%s' from "
1005 "'%s': %s\n",
1006 path, parent->key->name, win_errstr(werr)));
1007 TALLOC_FREE(mem_ctx);
1008 return werr;
1011 static WERROR reg_deletekey_recursive_trans(struct registry_key *parent,
1012 const char *path,
1013 bool del_key)
1015 WERROR werr;
1017 werr = regdb_transaction_start();
1018 if (!W_ERROR_IS_OK(werr)) {
1019 DEBUG(0, ("reg_deletekey_recursive_trans: "
1020 "error starting transaction: %s\n",
1021 win_errstr(werr)));
1022 return werr;
1025 werr = reg_deletekey_recursive_internal(parent, path, del_key, false);
1027 if (!W_ERROR_IS_OK(werr)) {
1028 WERROR werr2;
1030 DEBUG(1, (__location__ " failed to delete key '%s' from key "
1031 "'%s': %s\n", path, parent->key->name,
1032 win_errstr(werr)));
1034 werr2 = regdb_transaction_cancel();
1035 if (!W_ERROR_IS_OK(werr2)) {
1036 DEBUG(0, ("reg_deletekey_recursive_trans: "
1037 "error cancelling transaction: %s\n",
1038 win_errstr(werr2)));
1040 * return the original werr or the
1041 * error from cancelling the transaction?
1044 } else {
1045 werr = regdb_transaction_commit();
1046 if (!W_ERROR_IS_OK(werr)) {
1047 DEBUG(0, ("reg_deletekey_recursive_trans: "
1048 "error committing transaction: %s\n",
1049 win_errstr(werr)));
1050 } else {
1051 DEBUG(5, ("reg_reletekey_recursive_trans: deleted key '%s' from '%s'\n",
1052 path, parent->key->name));
1057 return werr;
1060 WERROR reg_deletekey_recursive(struct registry_key *parent,
1061 const char *path)
1063 return reg_deletekey_recursive_trans(parent, path, true);
1066 WERROR reg_deletesubkeys_recursive(struct registry_key *parent,
1067 const char *path)
1069 return reg_deletekey_recursive_trans(parent, path, false);