2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Volker Lendecke 2006
5 * Copyright (C) Michael Adam 2007-2008
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
27 * 0x00 winreg_OpenHKCR
28 * 0x01 winreg_OpenHKCU
29 * 0x02 winreg_OpenHKLM
30 * 0x03 winreg_OpenHKPD
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
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
57 * 0x1e winreg_InitiateSystemShutdownEx
58 * 0x1f winreg_SaveKeyEx
59 * 0x20 winreg_OpenHKPT
60 * 0x21 winreg_OpenHKPN
61 * 0x22 winreg_QueryMultipleValues2
69 #define DBGC_CLASS DBGC_REGISTRY
72 /**********************************************************************
74 **********************************************************************/
76 static WERROR fill_value_cache(struct registry_key *key)
78 if (key->values != NULL) {
79 if (!reg_values_need_update(key->key, key->values)) {
84 if (!(key->values = TALLOC_ZERO_P(key, REGVAL_CTR))) {
87 if (fetch_reg_values(key->key, key->values) == -1) {
88 TALLOC_FREE(key->values);
95 static WERROR fill_subkey_cache(struct registry_key *key)
99 if (key->subkeys != NULL) {
100 if (!reg_subkeys_need_update(key->key, key->subkeys)) {
105 werr = regsubkey_ctr_init(key, &(key->subkeys));
106 W_ERROR_NOT_OK_RETURN(werr);
108 if (fetch_reg_keys(key->key, key->subkeys) == -1) {
109 TALLOC_FREE(key->subkeys);
110 return WERR_NO_MORE_ITEMS;
116 static int regkey_destructor(REGISTRY_KEY *key)
118 return regdb_close();
121 static WERROR regkey_open_onelevel(TALLOC_CTX *mem_ctx,
122 struct registry_key *parent,
124 const struct nt_user_token *token,
125 uint32 access_desired,
126 struct registry_key **pregkey)
128 WERROR result = WERR_OK;
129 struct registry_key *regkey;
131 struct regsubkey_ctr *subkeys = NULL;
133 DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name));
135 SMB_ASSERT(strchr(name, '\\') == NULL);
137 if (!(regkey = TALLOC_ZERO_P(mem_ctx, struct registry_key)) ||
138 !(regkey->token = dup_nt_token(regkey, token)) ||
139 !(regkey->key = TALLOC_ZERO_P(regkey, REGISTRY_KEY))) {
144 if ( !(W_ERROR_IS_OK(result = regdb_open())) ) {
149 talloc_set_destructor(key, regkey_destructor);
153 key->type = REG_KEY_GENERIC;
155 if (name[0] == '\0') {
157 * Open a copy of the parent key
160 result = WERR_BADFILE;
163 key->name = talloc_strdup(key, parent->key->name);
169 key->name = talloc_asprintf(key, "%s%s%s",
170 parent ? parent->key->name : "",
175 if (key->name == NULL) {
180 /* Tag this as a Performance Counter Key */
182 if( StrnCaseCmp(key->name, KEY_HKPD, strlen(KEY_HKPD)) == 0 )
183 key->type = REG_KEY_HKPD;
185 /* Look up the table of registry I/O operations */
187 if ( !(key->ops = reghook_cache_find( key->name )) ) {
188 DEBUG(0,("reg_open_onelevel: Failed to assign "
189 "REGISTRY_OPS to [%s]\n", key->name ));
190 result = WERR_BADFILE;
194 /* check if the path really exists; failed is indicated by -1 */
195 /* if the subkey count failed, bail out */
197 result = regsubkey_ctr_init(key, &subkeys);
198 if (!W_ERROR_IS_OK(result)) {
202 if ( fetch_reg_keys( key, subkeys ) == -1 ) {
203 result = WERR_BADFILE;
207 TALLOC_FREE( subkeys );
209 if ( !regkey_access_check( key, access_desired, &key->access_granted,
211 result = WERR_ACCESS_DENIED;
219 if ( !W_ERROR_IS_OK(result) ) {
226 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
227 uint32 desired_access,
228 const struct nt_user_token *token,
229 struct registry_key **pkey)
231 SMB_ASSERT(hive != NULL);
232 SMB_ASSERT(hive[0] != '\0');
233 SMB_ASSERT(strchr(hive, '\\') == NULL);
235 return regkey_open_onelevel(mem_ctx, NULL, hive, token, desired_access,
240 /**********************************************************************
242 **********************************************************************/
244 WERROR reg_openkey(TALLOC_CTX *mem_ctx, struct registry_key *parent,
245 const char *name, uint32 desired_access,
246 struct registry_key **pkey)
248 struct registry_key *direct_parent = parent;
250 char *p, *path, *to_free;
253 if (!(path = SMB_STRDUP(name))) {
260 if ((len > 0) && (path[len-1] == '\\')) {
264 while ((p = strchr(path, '\\')) != NULL) {
265 char *name_component;
266 struct registry_key *tmp;
268 if (!(name_component = SMB_STRNDUP(path, (p - path)))) {
273 err = regkey_open_onelevel(mem_ctx, direct_parent,
274 name_component, parent->token,
275 SEC_RIGHTS_ENUM_SUBKEYS, &tmp);
276 SAFE_FREE(name_component);
278 if (!W_ERROR_IS_OK(err)) {
281 if (direct_parent != parent) {
282 TALLOC_FREE(direct_parent);
289 err = regkey_open_onelevel(mem_ctx, direct_parent, path, parent->token,
290 desired_access, pkey);
292 if (direct_parent != parent) {
293 TALLOC_FREE(direct_parent);
299 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
300 uint32 idx, char **name, NTTIME *last_write_time)
304 if (!(key->key->access_granted & SEC_RIGHTS_ENUM_SUBKEYS)) {
305 return WERR_ACCESS_DENIED;
308 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
312 if (idx >= regsubkey_ctr_numkeys(key->subkeys)) {
313 return WERR_NO_MORE_ITEMS;
316 if (!(*name = talloc_strdup(mem_ctx,
317 regsubkey_ctr_specific_key(key->subkeys, idx))))
322 if (last_write_time) {
323 *last_write_time = 0;
329 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
330 uint32 idx, char **pname, struct registry_value **pval)
332 struct registry_value *val;
335 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
336 return WERR_ACCESS_DENIED;
339 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
343 if (idx >= key->values->num_values) {
344 return WERR_NO_MORE_ITEMS;
347 err = registry_pull_value(mem_ctx, &val,
348 key->values->values[idx]->type,
349 key->values->values[idx]->data_p,
350 key->values->values[idx]->size,
351 key->values->values[idx]->size);
352 if (!W_ERROR_IS_OK(err)) {
357 && !(*pname = talloc_strdup(
358 mem_ctx, key->values->values[idx]->valuename))) {
367 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
368 const char *name, struct registry_value **pval)
373 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
374 return WERR_ACCESS_DENIED;
377 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
381 for (i=0; i<key->values->num_values; i++) {
382 if (strequal(key->values->values[i]->valuename, name)) {
383 return reg_enumvalue(mem_ctx, key, i, NULL, pval);
390 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
391 uint32_t *max_subkeylen, uint32_t *max_subkeysize,
392 uint32_t *num_values, uint32_t *max_valnamelen,
393 uint32_t *max_valbufsize, uint32_t *secdescsize,
394 NTTIME *last_changed_time)
400 struct security_descriptor *secdesc;
402 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
403 return WERR_ACCESS_DENIED;
406 if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
407 !W_ERROR_IS_OK(fill_value_cache(key))) {
412 for (i=0; i< regsubkey_ctr_numkeys(key->subkeys); i++) {
413 max_len = MAX(max_len,
414 strlen(regsubkey_ctr_specific_key(key->subkeys, i)));
417 *num_subkeys = regsubkey_ctr_numkeys(key->subkeys);
418 *max_subkeylen = max_len;
419 *max_subkeysize = 0; /* Class length? */
423 for (i=0; i<key->values->num_values; i++) {
424 max_len = MAX(max_len,
425 strlen(key->values->values[i]->valuename));
426 max_size = MAX(max_size, key->values->values[i]->size);
429 *num_values = key->values->num_values;
430 *max_valnamelen = max_len;
431 *max_valbufsize = max_size;
433 if (!(mem_ctx = talloc_new(key))) {
437 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
438 if (!W_ERROR_IS_OK(err)) {
439 TALLOC_FREE(mem_ctx);
443 *secdescsize = ndr_size_security_descriptor(secdesc, NULL, 0);
444 TALLOC_FREE(mem_ctx);
446 *last_changed_time = 0;
451 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
452 const char *subkeypath, uint32 desired_access,
453 struct registry_key **pkey,
454 enum winreg_CreateAction *paction)
456 struct registry_key *key = parent;
457 struct registry_key *create_parent;
462 if (!(mem_ctx = talloc_new(ctx))) return WERR_NOMEM;
464 if (!(path = talloc_strdup(mem_ctx, subkeypath))) {
469 while ((end = strchr(path, '\\')) != NULL) {
470 struct registry_key *tmp;
471 enum winreg_CreateAction action;
475 err = reg_createkey(mem_ctx, key, path,
476 SEC_RIGHTS_ENUM_SUBKEYS, &tmp, &action);
477 if (!W_ERROR_IS_OK(err)) {
490 * At this point, "path" contains the one-element subkey of "key". We
491 * can try to open it.
494 err = reg_openkey(ctx, key, path, desired_access, pkey);
495 if (W_ERROR_IS_OK(err)) {
496 if (paction != NULL) {
497 *paction = REG_OPENED_EXISTING_KEY;
502 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
504 * Something but "notfound" has happened, so bail out
510 * We have to make a copy of the current key, as we opened it only
511 * with ENUM_SUBKEY access.
514 err = reg_openkey(mem_ctx, key, "", SEC_RIGHTS_CREATE_SUBKEY,
516 if (!W_ERROR_IS_OK(err)) {
521 * Actually create the subkey
524 err = fill_subkey_cache(create_parent);
525 if (!W_ERROR_IS_OK(err)) goto done;
527 err = regsubkey_ctr_addkey(create_parent->subkeys, path);
528 if (!W_ERROR_IS_OK(err)) goto done;
530 if (!store_reg_keys(create_parent->key, create_parent->subkeys)) {
531 TALLOC_FREE(create_parent->subkeys);
532 err = WERR_REG_IO_FAILURE;
537 * Now open the newly created key
540 err = reg_openkey(ctx, create_parent, path, desired_access, pkey);
541 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
542 *paction = REG_CREATED_NEW_KEY;
546 TALLOC_FREE(mem_ctx);
550 WERROR reg_deletekey(struct registry_key *parent, const char *path)
555 struct registry_key *tmp_key, *key;
557 if (!(mem_ctx = talloc_init("reg_createkey"))) return WERR_NOMEM;
559 if (!(name = talloc_strdup(mem_ctx, path))) {
564 /* check if the key has subkeys */
565 err = reg_openkey(mem_ctx, parent, name, REG_KEY_READ, &key);
566 if (!W_ERROR_IS_OK(err)) {
569 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
572 if (regsubkey_ctr_numkeys(key->subkeys) > 0) {
573 err = WERR_ACCESS_DENIED;
577 /* no subkeys - proceed with delete */
578 if ((end = strrchr(name, '\\')) != NULL) {
581 err = reg_openkey(mem_ctx, parent, name,
582 SEC_RIGHTS_CREATE_SUBKEY, &tmp_key);
583 if (!W_ERROR_IS_OK(err)) {
591 if (name[0] == '\0') {
592 err = WERR_INVALID_PARAM;
596 if (!W_ERROR_IS_OK(err = fill_subkey_cache(parent))) {
600 err = regsubkey_ctr_delkey(parent->subkeys, name);
601 if (!W_ERROR_IS_OK(err)) {
605 if (!store_reg_keys(parent->key, parent->subkeys)) {
606 TALLOC_FREE(parent->subkeys);
607 err = WERR_REG_IO_FAILURE;
611 regkey_set_secdesc(key->key, NULL);
616 TALLOC_FREE(mem_ctx);
620 WERROR reg_setvalue(struct registry_key *key, const char *name,
621 const struct registry_value *val)
624 DATA_BLOB value_data;
627 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
628 return WERR_ACCESS_DENIED;
631 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
635 err = registry_push_value(key, val, &value_data);
636 if (!W_ERROR_IS_OK(err)) {
640 res = regval_ctr_addvalue(key->values, name, val->type,
641 (char *)value_data.data, value_data.length);
642 TALLOC_FREE(value_data.data);
645 TALLOC_FREE(key->values);
649 if (!store_reg_values(key->key, key->values)) {
650 TALLOC_FREE(key->values);
651 return WERR_REG_IO_FAILURE;
657 static WERROR reg_value_exists(struct registry_key *key, const char *name)
661 for (i=0; i<key->values->num_values; i++) {
662 if (strequal(key->values->values[i]->valuename, name)) {
670 WERROR reg_deletevalue(struct registry_key *key, const char *name)
674 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
675 return WERR_ACCESS_DENIED;
678 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
682 err = reg_value_exists(key, name);
683 if (!W_ERROR_IS_OK(err)) {
687 regval_ctr_delvalue(key->values, name);
689 if (!store_reg_values(key->key, key->values)) {
690 TALLOC_FREE(key->values);
691 return WERR_REG_IO_FAILURE;
697 WERROR reg_getkeysecurity(TALLOC_CTX *mem_ctx, struct registry_key *key,
698 struct security_descriptor **psecdesc)
700 return regkey_get_secdesc(mem_ctx, key->key, psecdesc);
703 WERROR reg_setkeysecurity(struct registry_key *key,
704 struct security_descriptor *psecdesc)
706 return regkey_set_secdesc(key->key, psecdesc);
709 WERROR reg_getversion(uint32_t *version)
711 if (version == NULL) {
712 return WERR_INVALID_PARAM;
715 *version = 0x00000005; /* Windows 2000 registry API version */
719 /*******************************************************************
720 Note: topkeypat is the *full* path that this *key will be
721 loaded into (including the name of the key)
722 ********************************************************************/
724 static WERROR reg_load_tree(REGF_FILE *regfile, const char *topkeypath,
728 REGISTRY_KEY registry_key;
730 struct regsubkey_ctr *subkeys;
733 WERROR result = WERR_OK;
735 /* initialize the REGISTRY_KEY structure */
737 registry_key.ops = reghook_cache_find(topkeypath);
738 if (!registry_key.ops) {
739 DEBUG(0, ("reg_load_tree: Failed to assign REGISTRY_OPS "
740 "to [%s]\n", topkeypath));
744 registry_key.name = talloc_strdup(regfile->mem_ctx, topkeypath);
745 if (!registry_key.name) {
746 DEBUG(0, ("reg_load_tree: Talloc failed for reg_key.name!\n"));
750 /* now start parsing the values and subkeys */
752 result = regsubkey_ctr_init(regfile->mem_ctx, &subkeys);
753 W_ERROR_NOT_OK_RETURN(result);
755 values = TALLOC_ZERO_P(subkeys, REGVAL_CTR);
756 if (values == NULL) {
760 /* copy values into the REGVAL_CTR */
762 for (i=0; i<key->num_values; i++) {
763 regval_ctr_addvalue(values, key->values[i].valuename,
765 (char*)key->values[i].data,
766 (key->values[i].data_size & ~VK_DATA_IN_OFFSET));
769 /* copy subkeys into the struct regsubkey_ctr */
771 key->subkey_index = 0;
772 while ((subkey = regfio_fetch_subkey( regfile, key ))) {
773 result = regsubkey_ctr_addkey(subkeys, subkey->keyname);
774 if (!W_ERROR_IS_OK(result)) {
775 TALLOC_FREE(subkeys);
780 /* write this key and values out */
782 if (!store_reg_values(®istry_key, values)
783 || !store_reg_keys(®istry_key, subkeys))
785 DEBUG(0,("reg_load_tree: Failed to load %s!\n", topkeypath));
786 result = WERR_REG_IO_FAILURE;
789 TALLOC_FREE(subkeys);
791 if (!W_ERROR_IS_OK(result)) {
795 /* now continue to load each subkey registry tree */
797 key->subkey_index = 0;
798 while ((subkey = regfio_fetch_subkey(regfile, key))) {
799 path = talloc_asprintf(regfile->mem_ctx,
806 result = reg_load_tree(regfile, path, subkey);
807 if (!W_ERROR_IS_OK(result)) {
815 /*******************************************************************
816 ********************************************************************/
818 static WERROR restore_registry_key(REGISTRY_KEY *krecord, const char *fname)
821 REGF_NK_REC *rootkey;
824 /* open the registry file....fail if the file already exists */
826 regfile = regfio_open(fname, (O_RDONLY), 0);
827 if (regfile == NULL) {
828 DEBUG(0, ("restore_registry_key: failed to open \"%s\" (%s)\n",
829 fname, strerror(errno)));
830 return ntstatus_to_werror(map_nt_error_from_unix(errno));
833 /* get the rootkey from the regf file and then load the tree
834 via recursive calls */
836 if (!(rootkey = regfio_rootkey(regfile))) {
837 regfio_close(regfile);
838 return WERR_REG_FILE_INVALID;
841 result = reg_load_tree(regfile, krecord->name, rootkey);
845 regfio_close(regfile);
850 WERROR reg_restorekey(struct registry_key *key, const char *fname)
852 return restore_registry_key(key->key, fname);
855 /********************************************************************
856 ********************************************************************/
858 static WERROR reg_write_tree(REGF_FILE *regfile, const char *keypath,
863 struct regsubkey_ctr *subkeys;
865 char *key_tmp = NULL;
866 char *keyname, *parentpath;
867 char *subkeypath = NULL;
869 REGISTRY_KEY registry_key;
870 WERROR result = WERR_OK;
871 SEC_DESC *sec_desc = NULL;
874 return WERR_GENERAL_FAILURE;
878 return WERR_OBJECT_PATH_INVALID;
881 /* split up the registry key path */
883 key_tmp = talloc_strdup(regfile->mem_ctx, keypath);
887 if (!reg_split_key(key_tmp, &parentpath, &keyname)) {
888 return WERR_OBJECT_PATH_INVALID;
892 keyname = parentpath;
895 /* we need a REGISTRY_KEY object here to enumerate subkeys and values */
897 ZERO_STRUCT(registry_key);
899 registry_key.name = talloc_strdup(regfile->mem_ctx, keypath);
900 if (registry_key.name == NULL) {
904 registry_key.ops = reghook_cache_find(registry_key.name);
905 if (registry_key.ops == NULL) {
909 /* lookup the values and subkeys */
911 result = regsubkey_ctr_init(regfile->mem_ctx, &subkeys);
912 W_ERROR_NOT_OK_RETURN(result);
914 values = TALLOC_ZERO_P(subkeys, REGVAL_CTR);
915 if (values == NULL) {
919 fetch_reg_keys(®istry_key, subkeys);
920 fetch_reg_values(®istry_key, values);
922 result = regkey_get_secdesc(regfile->mem_ctx, ®istry_key, &sec_desc);
923 if (!W_ERROR_IS_OK(result)) {
927 /* write out this key */
929 key = regfio_write_key(regfile, keyname, values, subkeys, sec_desc,
932 result = WERR_CAN_NOT_COMPLETE;
936 /* write each one of the subkeys out */
938 num_subkeys = regsubkey_ctr_numkeys(subkeys);
939 for (i=0; i<num_subkeys; i++) {
940 subkeyname = regsubkey_ctr_specific_key(subkeys, i);
941 subkeypath = talloc_asprintf(regfile->mem_ctx, "%s\\%s",
942 keypath, subkeyname);
943 if (subkeypath == NULL) {
947 result = reg_write_tree(regfile, subkeypath, key);
948 if (!W_ERROR_IS_OK(result))
952 DEBUG(6, ("reg_write_tree: wrote key [%s]\n", keypath));
955 TALLOC_FREE(subkeys);
956 TALLOC_FREE(registry_key.name);
961 static WERROR backup_registry_key(REGISTRY_KEY *krecord, const char *fname)
966 /* open the registry file....fail if the file already exists */
968 regfile = regfio_open(fname, (O_RDWR|O_CREAT|O_EXCL),
970 if (regfile == NULL) {
971 DEBUG(0,("backup_registry_key: failed to open \"%s\" (%s)\n",
972 fname, strerror(errno) ));
973 return ntstatus_to_werror(map_nt_error_from_unix(errno));
976 /* write the registry tree to the file */
978 result = reg_write_tree(regfile, krecord->name, NULL);
982 regfio_close(regfile);
987 WERROR reg_savekey(struct registry_key *key, const char *fname)
989 return backup_registry_key(key->key, fname);
992 /**********************************************************************
993 * Higher level utility functions
994 **********************************************************************/
996 WERROR reg_deleteallvalues(struct registry_key *key)
1001 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
1002 return WERR_ACCESS_DENIED;
1005 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
1009 for (i=0; i<key->values->num_values; i++) {
1010 regval_ctr_delvalue(key->values, key->values->values[i]->valuename);
1013 if (!store_reg_values(key->key, key->values)) {
1014 TALLOC_FREE(key->values);
1015 return WERR_REG_IO_FAILURE;
1022 * Utility function to open a complete registry path including the hive prefix.
1025 WERROR reg_open_path(TALLOC_CTX *mem_ctx, const char *orig_path,
1026 uint32 desired_access, const struct nt_user_token *token,
1027 struct registry_key **pkey)
1029 struct registry_key *hive, *key;
1033 if (!(path = SMB_STRDUP(orig_path))) {
1037 p = strchr(path, '\\');
1039 if ((p == NULL) || (p[1] == '\0')) {
1041 * No key behind the hive, just return the hive
1044 err = reg_openhive(mem_ctx, path, desired_access, token,
1046 if (!W_ERROR_IS_OK(err)) {
1057 err = reg_openhive(mem_ctx, path, SEC_RIGHTS_ENUM_SUBKEYS, token,
1059 if (!W_ERROR_IS_OK(err)) {
1064 err = reg_openkey(mem_ctx, hive, p+1, desired_access, &key);
1069 if (!W_ERROR_IS_OK(err)) {
1078 * Utility function to delete a registry key with all its subkeys.
1079 * Note that reg_deletekey returns ACCESS_DENIED when called on a
1080 * key that has subkeys.
1082 static WERROR reg_deletekey_recursive_internal(TALLOC_CTX *ctx,
1083 struct registry_key *parent,
1087 TALLOC_CTX *mem_ctx = NULL;
1088 WERROR werr = WERR_OK;
1089 struct registry_key *key;
1090 char *subkey_name = NULL;
1092 mem_ctx = talloc_new(ctx);
1093 if (mem_ctx == NULL) {
1098 /* recurse through subkeys first */
1099 werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
1100 if (!W_ERROR_IS_OK(werr)) {
1104 while (W_ERROR_IS_OK(werr = reg_enumkey(mem_ctx, key, 0,
1105 &subkey_name, NULL)))
1107 werr = reg_deletekey_recursive_internal(mem_ctx, key,
1110 if (!W_ERROR_IS_OK(werr)) {
1114 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
1115 DEBUG(1, ("reg_deletekey_recursive_internal: "
1116 "Error enumerating subkeys: %s\n",
1124 /* now delete the actual key */
1125 werr = reg_deletekey(parent, path);
1129 TALLOC_FREE(mem_ctx);
1133 static WERROR reg_deletekey_recursive_trans(TALLOC_CTX *ctx,
1134 struct registry_key *parent,
1140 werr = regdb_transaction_start();
1141 if (!W_ERROR_IS_OK(werr)) {
1142 DEBUG(0, ("reg_deletekey_recursive_trans: "
1143 "error starting transaction: %s\n",
1148 werr = reg_deletekey_recursive_internal(ctx, parent, path, del_key);
1150 if (!W_ERROR_IS_OK(werr)) {
1151 DEBUG(1, (__location__ " failed to delete key '%s' from key "
1152 "'%s': %s\n", path, parent->key->name,
1154 werr = regdb_transaction_cancel();
1155 if (!W_ERROR_IS_OK(werr)) {
1156 DEBUG(0, ("reg_deletekey_recursive_trans: "
1157 "error cancelling transaction: %s\n",
1161 werr = regdb_transaction_commit();
1162 if (!W_ERROR_IS_OK(werr)) {
1163 DEBUG(0, ("reg_deletekey_recursive_trans: "
1164 "error committing transaction: %s\n",
1172 WERROR reg_deletekey_recursive(TALLOC_CTX *ctx,
1173 struct registry_key *parent,
1176 return reg_deletekey_recursive_trans(ctx, parent, path, true);
1179 WERROR reg_deletesubkeys_recursive(TALLOC_CTX *ctx,
1180 struct registry_key *parent,
1183 return reg_deletekey_recursive_trans(ctx, parent, path, false);
1187 /* these two functions are unused. */
1190 * Utility function to create a registry key without opening the hive
1191 * before. Assumes the hive already exists.
1194 WERROR reg_create_path(TALLOC_CTX *mem_ctx, const char *orig_path,
1195 uint32 desired_access,
1196 const struct nt_user_token *token,
1197 enum winreg_CreateAction *paction,
1198 struct registry_key **pkey)
1200 struct registry_key *hive;
1204 if (!(path = SMB_STRDUP(orig_path))) {
1208 p = strchr(path, '\\');
1210 if ((p == NULL) || (p[1] == '\0')) {
1212 * No key behind the hive, just return the hive
1215 err = reg_openhive(mem_ctx, path, desired_access, token,
1217 if (!W_ERROR_IS_OK(err)) {
1223 *paction = REG_OPENED_EXISTING_KEY;
1229 err = reg_openhive(mem_ctx, path,
1230 (strchr(p+1, '\\') != NULL) ?
1231 SEC_RIGHTS_ENUM_SUBKEYS : SEC_RIGHTS_CREATE_SUBKEY,
1233 if (!W_ERROR_IS_OK(err)) {
1238 err = reg_createkey(mem_ctx, hive, p+1, desired_access, pkey, paction);
1245 * Utility function to create a registry key without opening the hive
1246 * before. Will not delete a hive.
1249 WERROR reg_delete_path(const struct nt_user_token *token,
1250 const char *orig_path)
1252 struct registry_key *hive;
1256 if (!(path = SMB_STRDUP(orig_path))) {
1260 p = strchr(path, '\\');
1262 if ((p == NULL) || (p[1] == '\0')) {
1264 return WERR_INVALID_PARAM;
1269 err = reg_openhive(NULL, path,
1270 (strchr(p+1, '\\') != NULL) ?
1271 SEC_RIGHTS_ENUM_SUBKEYS : SEC_RIGHTS_CREATE_SUBKEY,
1273 if (!W_ERROR_IS_OK(err)) {
1278 err = reg_deletekey(hive, p+1);