libnet: Fix Coverity ID 1634803 Dereference after null check
[samba4-gss.git] / source3 / registry / reg_api.c
blobdd256f213e322948b24245dd9062580bae1530d2
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"
74 #include "libcli/security/security_token.h"
76 #undef DBGC_CLASS
77 #define DBGC_CLASS DBGC_REGISTRY
80 /**********************************************************************
81 * Helper functions
82 **********************************************************************/
84 static WERROR fill_value_cache(struct registry_key *key)
86 WERROR werr;
88 if (key->values != NULL) {
89 if (!reg_values_need_update(key->key, key->values)) {
90 return WERR_OK;
94 TALLOC_FREE(key->values);
95 werr = regval_ctr_init(key, &(key->values));
96 W_ERROR_NOT_OK_RETURN(werr);
98 if (fetch_reg_values(key->key, key->values) == -1) {
99 TALLOC_FREE(key->values);
100 return WERR_FILE_NOT_FOUND;
103 return WERR_OK;
106 static WERROR fill_subkey_cache(struct registry_key *key)
108 WERROR werr;
110 if (key->subkeys != NULL) {
111 if (!reg_subkeys_need_update(key->key, key->subkeys)) {
112 return WERR_OK;
116 TALLOC_FREE(key->subkeys);
117 werr = regsubkey_ctr_init(key, &(key->subkeys));
118 W_ERROR_NOT_OK_RETURN(werr);
120 if (fetch_reg_keys(key->key, key->subkeys) == -1) {
121 TALLOC_FREE(key->subkeys);
122 return WERR_FILE_NOT_FOUND;
125 return WERR_OK;
128 static int regkey_destructor(struct registry_key_handle *key)
130 return regdb_close();
133 static WERROR regkey_open_onelevel(TALLOC_CTX *mem_ctx,
134 struct registry_key *parent,
135 const char *name,
136 const struct security_token *token,
137 uint32_t access_desired,
138 struct registry_key **pregkey)
140 WERROR result;
141 struct registry_key *regkey;
142 struct registry_key_handle *key;
144 DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name));
146 SMB_ASSERT(strchr(name, '\\') == NULL);
148 if (!(regkey = talloc_zero(mem_ctx, struct registry_key)) ||
149 !(regkey->token = security_token_duplicate(regkey, token)) ||
150 !(regkey->key = talloc_zero(regkey, struct registry_key_handle)))
152 result = WERR_NOT_ENOUGH_MEMORY;
153 goto done;
156 result = regdb_open();
157 if (!(W_ERROR_IS_OK(result))) {
158 goto done;
161 key = regkey->key;
162 talloc_set_destructor(key, regkey_destructor);
164 /* initialization */
166 key->type = REG_KEY_GENERIC;
168 if (name[0] == '\0') {
170 * Open a copy of the parent key
172 if (!parent) {
173 result = WERR_FILE_NOT_FOUND;
174 goto done;
176 key->name = talloc_strdup(key, parent->key->name);
178 else {
180 * Normal subkey open
182 key->name = talloc_asprintf(key, "%s%s%s",
183 parent ? parent->key->name : "",
184 parent ? "\\": "",
185 name);
188 if (key->name == NULL) {
189 result = WERR_NOT_ENOUGH_MEMORY;
190 goto done;
193 /* Tag this as a Performance Counter Key */
195 if( strncasecmp_m(key->name, KEY_HKPD, strlen(KEY_HKPD)) == 0 )
196 key->type = REG_KEY_HKPD;
198 /* Look up the table of registry I/O operations */
200 key->ops = reghook_cache_find( key->name );
201 if (key->ops == NULL) {
202 DEBUG(0,("reg_open_onelevel: Failed to assign "
203 "registry_ops to [%s]\n", key->name ));
204 result = WERR_FILE_NOT_FOUND;
205 goto done;
208 /* FIXME: Existence is currently checked by fetching the subkeys */
210 result = fill_subkey_cache(regkey);
211 if (!W_ERROR_IS_OK(result)) {
212 goto done;
215 if ( !regkey_access_check( key, access_desired, &key->access_granted,
216 token ) ) {
217 result = WERR_ACCESS_DENIED;
218 goto done;
221 *pregkey = regkey;
222 result = WERR_OK;
224 done:
225 if ( !W_ERROR_IS_OK(result) ) {
226 TALLOC_FREE(regkey);
229 return result;
232 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
233 uint32_t desired_access,
234 const struct security_token *token,
235 struct registry_key **pkey)
237 const struct hive_info *hi;
238 SMB_ASSERT(hive != NULL);
239 SMB_ASSERT(strchr(hive, '\\') == NULL);
241 hi = hive_info(hive);
242 if (hi == NULL) {
243 return WERR_FILE_NOT_FOUND;
246 return regkey_open_onelevel(mem_ctx, NULL, hi->short_name, token,
247 desired_access, pkey);
251 /**********************************************************************
252 * The API functions
253 **********************************************************************/
255 WERROR reg_openkey(TALLOC_CTX *mem_ctx, struct registry_key *parent,
256 const char *name, uint32_t desired_access,
257 struct registry_key **pkey)
259 struct registry_key *direct_parent = parent;
260 WERROR err;
261 char *p, *path;
262 size_t len;
263 TALLOC_CTX *frame = talloc_stackframe();
265 path = talloc_strdup(frame, name);
266 if (path == NULL) {
267 err = WERR_NOT_ENOUGH_MEMORY;
268 goto error;
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 name_component = talloc_strndup(frame, path, (p - path));
282 if (name_component == NULL) {
283 err = WERR_NOT_ENOUGH_MEMORY;
284 goto error;
287 err = regkey_open_onelevel(frame, direct_parent,
288 name_component, parent->token,
289 KEY_ENUMERATE_SUB_KEYS, &tmp);
291 if (!W_ERROR_IS_OK(err)) {
292 goto error;
295 direct_parent = tmp;
296 path = p+1;
299 err = regkey_open_onelevel(mem_ctx, direct_parent, path, parent->token,
300 desired_access, pkey);
302 error:
303 talloc_free(frame);
304 return err;
307 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
308 uint32_t idx, char **name, NTTIME *last_write_time)
310 WERROR err;
312 if (!(key->key->access_granted & KEY_ENUMERATE_SUB_KEYS)) {
313 return WERR_ACCESS_DENIED;
316 err = fill_subkey_cache(key);
317 if (!W_ERROR_IS_OK(err)) {
318 return err;
321 if (idx >= regsubkey_ctr_numkeys(key->subkeys)) {
322 return WERR_NO_MORE_ITEMS;
325 if (!(*name = talloc_strdup(mem_ctx,
326 regsubkey_ctr_specific_key(key->subkeys, idx))))
328 return WERR_NOT_ENOUGH_MEMORY;
331 if (last_write_time) {
332 *last_write_time = 0;
335 return WERR_OK;
338 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
339 uint32_t idx, char **pname, struct registry_value **pval)
341 struct registry_value *val;
342 struct regval_blob *blob;
343 WERROR err;
345 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
346 return WERR_ACCESS_DENIED;
349 err = fill_value_cache(key);
350 if (!(W_ERROR_IS_OK(err))) {
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_NOT_ENOUGH_MEMORY;
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_NOT_ENOUGH_MEMORY;
375 *pval = val;
376 return WERR_OK;
379 static WERROR reg_enumvalue_nocachefill(TALLOC_CTX *mem_ctx,
380 struct registry_key *key,
381 uint32_t idx, char **pname,
382 struct registry_value **pval)
384 struct registry_value *val;
385 struct regval_blob *blob;
387 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
388 return WERR_ACCESS_DENIED;
391 if (idx >= regval_ctr_numvals(key->values)) {
392 return WERR_NO_MORE_ITEMS;
395 blob = regval_ctr_specific_value(key->values, idx);
397 val = talloc_zero(mem_ctx, struct registry_value);
398 if (val == NULL) {
399 return WERR_NOT_ENOUGH_MEMORY;
402 val->type = regval_type(blob);
403 val->data = data_blob_talloc(mem_ctx, regval_data_p(blob), regval_size(blob));
405 if (pname
406 && !(*pname = talloc_strdup(
407 mem_ctx, regval_name(blob)))) {
408 TALLOC_FREE(val);
409 return WERR_NOT_ENOUGH_MEMORY;
412 *pval = val;
413 return WERR_OK;
416 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
417 const char *name, struct registry_value **pval)
419 WERROR err;
420 uint32_t i;
422 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
423 return WERR_ACCESS_DENIED;
426 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
427 return err;
430 for (i=0; i < regval_ctr_numvals(key->values); i++) {
431 struct regval_blob *blob;
432 blob = regval_ctr_specific_value(key->values, i);
433 if (strequal(regval_name(blob), name)) {
435 * don't use reg_enumvalue here:
436 * re-reading the values from the disk
437 * would change the indexing and break
438 * this function.
440 return reg_enumvalue_nocachefill(mem_ctx, key, i,
441 NULL, pval);
445 return WERR_FILE_NOT_FOUND;
448 WERROR reg_querymultiplevalues(TALLOC_CTX *mem_ctx,
449 struct registry_key *key,
450 uint32_t num_names,
451 const char **names,
452 uint32_t *pnum_vals,
453 struct registry_value **pvals)
455 WERROR err;
456 uint32_t i, n, found = 0;
457 struct registry_value *vals;
459 if (num_names == 0) {
460 return WERR_OK;
463 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
464 return WERR_ACCESS_DENIED;
467 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
468 return err;
471 vals = talloc_zero_array(mem_ctx, struct registry_value, num_names);
472 if (vals == NULL) {
473 return WERR_NOT_ENOUGH_MEMORY;
476 for (n=0; n < num_names; n++) {
477 for (i=0; i < regval_ctr_numvals(key->values); i++) {
478 struct regval_blob *blob;
479 blob = regval_ctr_specific_value(key->values, i);
480 if (strequal(regval_name(blob), names[n])) {
481 struct registry_value *v;
482 err = reg_enumvalue(mem_ctx, key, i, NULL, &v);
483 if (!W_ERROR_IS_OK(err)) {
484 return err;
486 vals[n] = *v;
487 found++;
492 *pvals = vals;
493 *pnum_vals = found;
495 return WERR_OK;
498 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
499 uint32_t *max_subkeylen, uint32_t *max_subkeysize,
500 uint32_t *num_values, uint32_t *max_valnamelen,
501 uint32_t *max_valbufsize, uint32_t *secdescsize,
502 NTTIME *last_changed_time)
504 uint32_t i, max_size;
505 size_t max_len;
506 TALLOC_CTX *mem_ctx;
507 WERROR err;
508 struct security_descriptor *secdesc;
510 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
511 return WERR_ACCESS_DENIED;
514 if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
515 !W_ERROR_IS_OK(fill_value_cache(key))) {
516 return WERR_FILE_NOT_FOUND;
519 max_len = 0;
520 for (i=0; i< regsubkey_ctr_numkeys(key->subkeys); i++) {
521 max_len = MAX(max_len,
522 strlen(regsubkey_ctr_specific_key(key->subkeys, i)));
525 *num_subkeys = regsubkey_ctr_numkeys(key->subkeys);
526 *max_subkeylen = max_len;
527 *max_subkeysize = 0; /* Class length? */
529 max_len = 0;
530 max_size = 0;
531 for (i=0; i < regval_ctr_numvals(key->values); i++) {
532 struct regval_blob *blob;
533 blob = regval_ctr_specific_value(key->values, i);
534 max_len = MAX(max_len, strlen(regval_name(blob)));
535 max_size = MAX(max_size, regval_size(blob));
538 *num_values = regval_ctr_numvals(key->values);
539 *max_valnamelen = max_len;
540 *max_valbufsize = max_size;
542 if (!(mem_ctx = talloc_new(key))) {
543 return WERR_NOT_ENOUGH_MEMORY;
546 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
547 if (!W_ERROR_IS_OK(err)) {
548 TALLOC_FREE(mem_ctx);
549 return err;
552 *secdescsize = ndr_size_security_descriptor(secdesc, 0);
553 TALLOC_FREE(mem_ctx);
555 *last_changed_time = 0;
557 return WERR_OK;
560 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
561 const char *subkeypath, uint32_t desired_access,
562 struct registry_key **pkey,
563 enum winreg_CreateAction *paction)
565 struct registry_key *key = parent;
566 TALLOC_CTX *mem_ctx;
567 char *path, *end;
568 WERROR err;
569 uint32_t access_granted;
571 mem_ctx = talloc_new(ctx);
572 if (mem_ctx == NULL) {
573 return WERR_NOT_ENOUGH_MEMORY;
576 path = talloc_strdup(mem_ctx, subkeypath);
577 if (path == NULL) {
578 err = WERR_NOT_ENOUGH_MEMORY;
579 goto done;
582 err = regdb_transaction_start();
583 if (!W_ERROR_IS_OK(err)) {
584 DEBUG(0, ("reg_createkey: failed to start transaction: %s\n",
585 win_errstr(err)));
586 goto done;
589 while ((end = strchr(path, '\\')) != NULL) {
590 struct registry_key *tmp;
591 enum winreg_CreateAction action;
593 *end = '\0';
595 err = reg_createkey(mem_ctx, key, path,
596 KEY_ENUMERATE_SUB_KEYS, &tmp, &action);
597 if (!W_ERROR_IS_OK(err)) {
598 goto trans_done;
601 if (key != parent) {
602 TALLOC_FREE(key);
605 key = tmp;
606 path = end+1;
610 * At this point, "path" contains the one-element subkey of "key". We
611 * can try to open it.
614 err = reg_openkey(ctx, key, path, desired_access, pkey);
615 if (W_ERROR_IS_OK(err)) {
616 if (paction != NULL) {
617 *paction = REG_OPENED_EXISTING_KEY;
619 goto trans_done;
622 if (!W_ERROR_EQUAL(err, WERR_FILE_NOT_FOUND)) {
624 * Something but "notfound" has happened, so bail out
626 goto trans_done;
630 * We may (e.g. in the iteration) have opened the key with ENUM_SUBKEY.
631 * Instead of re-opening the key with CREATE_SUB_KEY, we simply
632 * duplicate the access check here and skip the expensive full open.
634 if (!regkey_access_check(key->key, KEY_CREATE_SUB_KEY, &access_granted,
635 key->token))
637 err = WERR_ACCESS_DENIED;
638 goto trans_done;
642 * Actually create the subkey
645 err = create_reg_subkey(key->key, path);
646 if (!W_ERROR_IS_OK(err)) {
647 goto trans_done;
651 * Now open the newly created key
654 err = reg_openkey(ctx, key, path, desired_access, pkey);
655 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
656 *paction = REG_CREATED_NEW_KEY;
659 trans_done:
660 if (W_ERROR_IS_OK(err)) {
661 err = regdb_transaction_commit();
662 if (!W_ERROR_IS_OK(err)) {
663 DEBUG(0, ("reg_createkey: Error committing transaction: %s\n", win_errstr(err)));
665 } else {
666 WERROR err1 = regdb_transaction_cancel();
667 if (!W_ERROR_IS_OK(err1)) {
668 DEBUG(0, ("reg_createkey: Error cancelling transaction: %s\n", win_errstr(err1)));
672 done:
673 TALLOC_FREE(mem_ctx);
674 return err;
677 static WERROR reg_deletekey_internal(TALLOC_CTX *mem_ctx,
678 struct registry_key *parent,
679 const char *path, bool lazy)
681 WERROR err;
682 char *name, *end;
683 struct registry_key *key;
684 name = talloc_strdup(mem_ctx, path);
685 if (name == NULL) {
686 err = WERR_NOT_ENOUGH_MEMORY;
687 goto done;
690 /* no subkeys - proceed with delete */
691 end = strrchr(name, '\\');
692 if (end != NULL) {
693 *end = '\0';
695 err = reg_openkey(mem_ctx, parent, name,
696 KEY_CREATE_SUB_KEY, &key);
697 W_ERROR_NOT_OK_GOTO_DONE(err);
699 parent = key;
700 name = end+1;
703 if (name[0] == '\0') {
704 err = WERR_INVALID_PARAMETER;
705 goto done;
708 err = delete_reg_subkey(parent->key, name, lazy);
710 done:
711 return err;
714 WERROR reg_deletekey(struct registry_key *parent, const char *path)
716 WERROR err;
717 struct registry_key *key;
718 TALLOC_CTX *mem_ctx = talloc_stackframe();
720 /* check if the key has subkeys */
721 err = reg_openkey(mem_ctx, parent, path, REG_KEY_READ, &key);
722 W_ERROR_NOT_OK_GOTO_DONE(err);
724 err = regdb_transaction_start();
725 if (!W_ERROR_IS_OK(err)) {
726 DEBUG(0, ("reg_deletekey: Error starting transaction: %s\n",
727 win_errstr(err)));
728 goto done;
731 err = fill_subkey_cache(key);
732 if (!W_ERROR_IS_OK(err)) {
733 goto trans_done;
736 if (regsubkey_ctr_numkeys(key->subkeys) > 0) {
737 err = WERR_ACCESS_DENIED;
738 goto trans_done;
740 err = reg_deletekey_internal(mem_ctx, parent, path, false);
742 trans_done:
743 if (W_ERROR_IS_OK(err)) {
744 err = regdb_transaction_commit();
745 if (!W_ERROR_IS_OK(err)) {
746 DEBUG(0, ("reg_deletekey: Error committing transaction: %s\n", win_errstr(err)));
748 } else {
749 WERROR err1 = regdb_transaction_cancel();
750 if (!W_ERROR_IS_OK(err1)) {
751 DEBUG(0, ("reg_deletekey: Error cancelling transaction: %s\n", win_errstr(err1)));
755 done:
756 TALLOC_FREE(mem_ctx);
757 return err;
761 WERROR reg_setvalue(struct registry_key *key, const char *name,
762 const struct registry_value *val)
764 struct regval_blob *existing;
765 WERROR err;
766 int res;
768 if (!(key->key->access_granted & KEY_SET_VALUE)) {
769 return WERR_ACCESS_DENIED;
772 err = regdb_transaction_start();
773 if (!W_ERROR_IS_OK(err)) {
774 DEBUG(0, ("reg_setvalue: Failed to start transaction: %s\n",
775 win_errstr(err)));
776 return err;
779 err = fill_value_cache(key);
780 if (!W_ERROR_IS_OK(err)) {
781 DEBUG(0, ("reg_setvalue: Error filling value cache: %s\n", win_errstr(err)));
782 goto done;
785 existing = regval_ctr_getvalue(key->values, name);
787 if ((existing != NULL) &&
788 (regval_size(existing) == val->data.length) &&
789 (memcmp(regval_data_p(existing), val->data.data,
790 val->data.length) == 0))
792 err = WERR_OK;
793 goto done;
796 res = regval_ctr_addvalue(key->values, name, val->type,
797 val->data.data, val->data.length);
799 if (res == 0) {
800 TALLOC_FREE(key->values);
801 err = WERR_NOT_ENOUGH_MEMORY;
802 goto done;
805 if (!store_reg_values(key->key, key->values)) {
806 TALLOC_FREE(key->values);
807 DEBUG(0, ("reg_setvalue: store_reg_values failed\n"));
808 err = WERR_REGISTRY_IO_FAILED;
809 goto done;
812 err = WERR_OK;
814 done:
815 if (W_ERROR_IS_OK(err)) {
816 err = regdb_transaction_commit();
817 if (!W_ERROR_IS_OK(err)) {
818 DEBUG(0, ("reg_setvalue: Error committing transaction: %s\n", win_errstr(err)));
820 } else {
821 WERROR err1 = regdb_transaction_cancel();
822 if (!W_ERROR_IS_OK(err1)) {
823 DEBUG(0, ("reg_setvalue: Error cancelling transaction: %s\n", win_errstr(err1)));
827 return err;
830 static WERROR reg_value_exists(struct registry_key *key, const char *name)
832 struct regval_blob *blob;
834 blob = regval_ctr_getvalue(key->values, name);
836 if (blob == NULL) {
837 return WERR_FILE_NOT_FOUND;
838 } else {
839 return WERR_OK;
843 WERROR reg_deletevalue(struct registry_key *key, const char *name)
845 WERROR err;
847 if (!(key->key->access_granted & KEY_SET_VALUE)) {
848 return WERR_ACCESS_DENIED;
851 err = regdb_transaction_start();
852 if (!W_ERROR_IS_OK(err)) {
853 DEBUG(0, ("reg_deletevalue: Failed to start transaction: %s\n",
854 win_errstr(err)));
855 return err;
858 err = fill_value_cache(key);
859 if (!W_ERROR_IS_OK(err)) {
860 DEBUG(0, ("reg_deletevalue; Error filling value cache: %s\n",
861 win_errstr(err)));
862 goto done;
865 err = reg_value_exists(key, name);
866 if (!W_ERROR_IS_OK(err)) {
867 goto done;
870 regval_ctr_delvalue(key->values, name);
872 if (!store_reg_values(key->key, key->values)) {
873 TALLOC_FREE(key->values);
874 err = WERR_REGISTRY_IO_FAILED;
875 DEBUG(0, ("reg_deletevalue: store_reg_values failed\n"));
876 goto done;
879 err = WERR_OK;
881 done:
882 if (W_ERROR_IS_OK(err)) {
883 err = regdb_transaction_commit();
884 if (!W_ERROR_IS_OK(err)) {
885 DEBUG(0, ("reg_deletevalue: Error committing transaction: %s\n", win_errstr(err)));
887 } else {
888 WERROR err1 = regdb_transaction_cancel();
889 if (!W_ERROR_IS_OK(err1)) {
890 DEBUG(0, ("reg_deletevalue: Error cancelling transaction: %s\n", win_errstr(err1)));
894 return err;
897 WERROR reg_getkeysecurity(TALLOC_CTX *mem_ctx, struct registry_key *key,
898 struct security_descriptor **psecdesc)
900 return regkey_get_secdesc(mem_ctx, key->key, psecdesc);
903 WERROR reg_setkeysecurity(struct registry_key *key,
904 struct security_descriptor *psecdesc)
906 return regkey_set_secdesc(key->key, psecdesc);
909 WERROR reg_getversion(uint32_t *version)
911 if (version == NULL) {
912 return WERR_INVALID_PARAMETER;
915 *version = 0x00000005; /* Windows 2000 registry API version */
916 return WERR_OK;
919 /**********************************************************************
920 * Higher level utility functions
921 **********************************************************************/
923 WERROR reg_deleteallvalues(struct registry_key *key)
925 WERROR err;
926 uint32_t i;
928 if (!(key->key->access_granted & KEY_SET_VALUE)) {
929 return WERR_ACCESS_DENIED;
932 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
933 return err;
936 for (i=0; i < regval_ctr_numvals(key->values); i++) {
937 struct regval_blob *blob;
938 blob = regval_ctr_specific_value(key->values, i);
939 regval_ctr_delvalue(key->values, regval_name(blob));
942 if (!store_reg_values(key->key, key->values)) {
943 TALLOC_FREE(key->values);
944 return WERR_REGISTRY_IO_FAILED;
947 return WERR_OK;
951 * Utility function to delete a registry key with all its subkeys.
952 * Note that reg_deletekey returns ACCESS_DENIED when called on a
953 * key that has subkeys.
955 static WERROR reg_deletekey_recursive_internal(struct registry_key *parent,
956 const char *path,
957 bool del_key, bool lazy)
959 WERROR werr;
960 struct registry_key *key;
961 char *subkey_name = NULL;
962 uint32_t i;
963 TALLOC_CTX *mem_ctx = talloc_stackframe();
965 DEBUG(5, ("reg_deletekey_recursive_internal: deleting '%s' from '%s'\n",
966 path, parent->key->name));
968 /* recurse through subkeys first */
969 werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
970 if (!W_ERROR_IS_OK(werr)) {
971 DEBUG(3, ("reg_deletekey_recursive_internal: error opening "
972 "subkey '%s' of '%s': '%s'\n",
973 path, parent->key->name, win_errstr(werr)));
974 goto done;
977 werr = fill_subkey_cache(key);
978 W_ERROR_NOT_OK_GOTO_DONE(werr);
981 * loop from top to bottom for performance:
982 * this way, we need to rehash the regsubkey containers less
984 for (i = regsubkey_ctr_numkeys(key->subkeys) ; i > 0; i--) {
985 subkey_name = regsubkey_ctr_specific_key(key->subkeys, i-1);
986 werr = reg_deletekey_recursive_internal(key, subkey_name, true, del_key);
987 W_ERROR_NOT_OK_GOTO_DONE(werr);
990 if (del_key) {
991 /* now delete the actual key */
992 werr = reg_deletekey_internal(mem_ctx, parent, path, lazy);
995 done:
997 DEBUG(5, ("reg_deletekey_recursive_internal: done deleting '%s' from "
998 "'%s': %s\n",
999 path, parent->key->name, win_errstr(werr)));
1000 TALLOC_FREE(mem_ctx);
1001 return werr;
1004 static WERROR reg_deletekey_recursive_trans(struct registry_key *parent,
1005 const char *path,
1006 bool del_key)
1008 WERROR werr;
1010 werr = regdb_transaction_start();
1011 if (!W_ERROR_IS_OK(werr)) {
1012 DEBUG(0, ("reg_deletekey_recursive_trans: "
1013 "error starting transaction: %s\n",
1014 win_errstr(werr)));
1015 return werr;
1018 werr = reg_deletekey_recursive_internal(parent, path, del_key, false);
1020 if (!W_ERROR_IS_OK(werr)) {
1021 WERROR werr2;
1022 DEBUG(W_ERROR_EQUAL(werr, WERR_FILE_NOT_FOUND) ? 5 : 1,
1023 (__location__ ": failed to delete key '%s' from key "
1024 "'%s': %s\n", path, parent->key->name,
1025 win_errstr(werr)));
1027 werr2 = regdb_transaction_cancel();
1028 if (!W_ERROR_IS_OK(werr2)) {
1029 DEBUG(0, ("reg_deletekey_recursive_trans: "
1030 "error cancelling transaction: %s\n",
1031 win_errstr(werr2)));
1033 * return the original werr or the
1034 * error from cancelling the transaction?
1037 } else {
1038 werr = regdb_transaction_commit();
1039 if (!W_ERROR_IS_OK(werr)) {
1040 DEBUG(0, ("reg_deletekey_recursive_trans: "
1041 "error committing transaction: %s\n",
1042 win_errstr(werr)));
1043 } else {
1044 DEBUG(5, ("reg_deletekey_recursive_trans: deleted key '%s' from '%s'\n",
1045 path, parent->key->name));
1050 return werr;
1053 WERROR reg_deletekey_recursive(struct registry_key *parent,
1054 const char *path)
1056 return reg_deletekey_recursive_trans(parent, path, true);
1059 WERROR reg_deletesubkeys_recursive(struct registry_key *parent,
1060 const char *path)
1062 return reg_deletekey_recursive_trans(parent, path, false);