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
67 #include "reg_cachehook.h"
71 #define DBGC_CLASS DBGC_REGISTRY
74 /**********************************************************************
76 **********************************************************************/
78 static WERROR fill_value_cache(struct registry_key *key)
80 if (key->values != NULL) {
81 if (!reg_values_need_update(key->key, key->values)) {
86 if (!(key->values = TALLOC_ZERO_P(key, struct regval_ctr))) {
89 if (fetch_reg_values(key->key, key->values) == -1) {
90 TALLOC_FREE(key->values);
97 static WERROR fill_subkey_cache(struct registry_key *key)
101 if (key->subkeys != NULL) {
102 if (!reg_subkeys_need_update(key->key, key->subkeys)) {
107 werr = regsubkey_ctr_init(key, &(key->subkeys));
108 W_ERROR_NOT_OK_RETURN(werr);
110 if (fetch_reg_keys(key->key, key->subkeys) == -1) {
111 TALLOC_FREE(key->subkeys);
112 return WERR_NO_MORE_ITEMS;
118 static int regkey_destructor(struct registry_key_handle *key)
120 return regdb_close();
123 static WERROR regkey_open_onelevel(TALLOC_CTX *mem_ctx,
124 struct registry_key *parent,
126 const struct nt_user_token *token,
127 uint32 access_desired,
128 struct registry_key **pregkey)
130 WERROR result = WERR_OK;
131 struct registry_key *regkey;
132 struct registry_key_handle *key;
133 struct regsubkey_ctr *subkeys = NULL;
135 DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name));
137 SMB_ASSERT(strchr(name, '\\') == NULL);
139 if (!(regkey = TALLOC_ZERO_P(mem_ctx, struct registry_key)) ||
140 !(regkey->token = dup_nt_token(regkey, token)) ||
141 !(regkey->key = TALLOC_ZERO_P(regkey, struct registry_key_handle)))
147 if ( !(W_ERROR_IS_OK(result = regdb_open())) ) {
152 talloc_set_destructor(key, regkey_destructor);
156 key->type = REG_KEY_GENERIC;
158 if (name[0] == '\0') {
160 * Open a copy of the parent key
163 result = WERR_BADFILE;
166 key->name = talloc_strdup(key, parent->key->name);
172 key->name = talloc_asprintf(key, "%s%s%s",
173 parent ? parent->key->name : "",
178 if (key->name == NULL) {
183 /* Tag this as a Performance Counter Key */
185 if( StrnCaseCmp(key->name, KEY_HKPD, strlen(KEY_HKPD)) == 0 )
186 key->type = REG_KEY_HKPD;
188 /* Look up the table of registry I/O operations */
190 if ( !(key->ops = reghook_cache_find( key->name )) ) {
191 DEBUG(0,("reg_open_onelevel: Failed to assign "
192 "registry_ops to [%s]\n", key->name ));
193 result = WERR_BADFILE;
197 /* check if the path really exists; failed is indicated by -1 */
198 /* if the subkey count failed, bail out */
200 result = regsubkey_ctr_init(key, &subkeys);
201 if (!W_ERROR_IS_OK(result)) {
205 if ( fetch_reg_keys( key, subkeys ) == -1 ) {
206 result = WERR_BADFILE;
210 TALLOC_FREE( subkeys );
212 if ( !regkey_access_check( key, access_desired, &key->access_granted,
214 result = WERR_ACCESS_DENIED;
222 if ( !W_ERROR_IS_OK(result) ) {
229 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
230 uint32 desired_access,
231 const struct nt_user_token *token,
232 struct registry_key **pkey)
234 SMB_ASSERT(hive != NULL);
235 SMB_ASSERT(hive[0] != '\0');
236 SMB_ASSERT(strchr(hive, '\\') == NULL);
238 return regkey_open_onelevel(mem_ctx, NULL, hive, token, desired_access,
243 /**********************************************************************
245 **********************************************************************/
247 WERROR reg_openkey(TALLOC_CTX *mem_ctx, struct registry_key *parent,
248 const char *name, uint32 desired_access,
249 struct registry_key **pkey)
251 struct registry_key *direct_parent = parent;
253 char *p, *path, *to_free;
256 if (!(path = SMB_STRDUP(name))) {
263 if ((len > 0) && (path[len-1] == '\\')) {
267 while ((p = strchr(path, '\\')) != NULL) {
268 char *name_component;
269 struct registry_key *tmp;
271 if (!(name_component = SMB_STRNDUP(path, (p - path)))) {
276 err = regkey_open_onelevel(mem_ctx, direct_parent,
277 name_component, parent->token,
278 KEY_ENUMERATE_SUB_KEYS, &tmp);
279 SAFE_FREE(name_component);
281 if (!W_ERROR_IS_OK(err)) {
284 if (direct_parent != parent) {
285 TALLOC_FREE(direct_parent);
292 err = regkey_open_onelevel(mem_ctx, direct_parent, path, parent->token,
293 desired_access, pkey);
295 if (direct_parent != parent) {
296 TALLOC_FREE(direct_parent);
302 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
303 uint32 idx, char **name, NTTIME *last_write_time)
307 if (!(key->key->access_granted & KEY_ENUMERATE_SUB_KEYS)) {
308 return WERR_ACCESS_DENIED;
311 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
315 if (idx >= regsubkey_ctr_numkeys(key->subkeys)) {
316 return WERR_NO_MORE_ITEMS;
319 if (!(*name = talloc_strdup(mem_ctx,
320 regsubkey_ctr_specific_key(key->subkeys, idx))))
325 if (last_write_time) {
326 *last_write_time = 0;
332 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
333 uint32 idx, char **pname, struct registry_value **pval)
335 struct registry_value *val;
338 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
339 return WERR_ACCESS_DENIED;
342 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
346 if (idx >= key->values->num_values) {
347 return WERR_NO_MORE_ITEMS;
350 err = registry_pull_value(mem_ctx, &val,
351 key->values->values[idx]->type,
352 key->values->values[idx]->data_p,
353 key->values->values[idx]->size,
354 key->values->values[idx]->size);
355 if (!W_ERROR_IS_OK(err)) {
360 && !(*pname = talloc_strdup(
361 mem_ctx, key->values->values[idx]->valuename))) {
370 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
371 const char *name, struct registry_value **pval)
376 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
377 return WERR_ACCESS_DENIED;
380 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
384 for (i=0; i<key->values->num_values; i++) {
385 if (strequal(key->values->values[i]->valuename, name)) {
386 return reg_enumvalue(mem_ctx, key, i, NULL, pval);
393 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
394 uint32_t *max_subkeylen, uint32_t *max_subkeysize,
395 uint32_t *num_values, uint32_t *max_valnamelen,
396 uint32_t *max_valbufsize, uint32_t *secdescsize,
397 NTTIME *last_changed_time)
403 struct security_descriptor *secdesc;
405 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
406 return WERR_ACCESS_DENIED;
409 if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
410 !W_ERROR_IS_OK(fill_value_cache(key))) {
415 for (i=0; i< regsubkey_ctr_numkeys(key->subkeys); i++) {
416 max_len = MAX(max_len,
417 strlen(regsubkey_ctr_specific_key(key->subkeys, i)));
420 *num_subkeys = regsubkey_ctr_numkeys(key->subkeys);
421 *max_subkeylen = max_len;
422 *max_subkeysize = 0; /* Class length? */
426 for (i=0; i<key->values->num_values; i++) {
427 max_len = MAX(max_len,
428 strlen(key->values->values[i]->valuename));
429 max_size = MAX(max_size, key->values->values[i]->size);
432 *num_values = key->values->num_values;
433 *max_valnamelen = max_len;
434 *max_valbufsize = max_size;
436 if (!(mem_ctx = talloc_new(key))) {
440 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
441 if (!W_ERROR_IS_OK(err)) {
442 TALLOC_FREE(mem_ctx);
446 *secdescsize = ndr_size_security_descriptor(secdesc, 0);
447 TALLOC_FREE(mem_ctx);
449 *last_changed_time = 0;
454 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
455 const char *subkeypath, uint32 desired_access,
456 struct registry_key **pkey,
457 enum winreg_CreateAction *paction)
459 struct registry_key *key = parent;
460 struct registry_key *create_parent;
466 * We must refuse to handle subkey-paths containing
467 * a '/' character because at a lower level, after
468 * normalization, '/' is treated as a key separator
471 if (strchr(subkeypath, '/') != NULL) {
472 return WERR_INVALID_PARAM;
475 if (!(mem_ctx = talloc_new(ctx))) return WERR_NOMEM;
477 if (!(path = talloc_strdup(mem_ctx, subkeypath))) {
482 while ((end = strchr(path, '\\')) != NULL) {
483 struct registry_key *tmp;
484 enum winreg_CreateAction action;
488 err = reg_createkey(mem_ctx, key, path,
489 KEY_ENUMERATE_SUB_KEYS, &tmp, &action);
490 if (!W_ERROR_IS_OK(err)) {
503 * At this point, "path" contains the one-element subkey of "key". We
504 * can try to open it.
507 err = reg_openkey(ctx, key, path, desired_access, pkey);
508 if (W_ERROR_IS_OK(err)) {
509 if (paction != NULL) {
510 *paction = REG_OPENED_EXISTING_KEY;
515 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
517 * Something but "notfound" has happened, so bail out
523 * We have to make a copy of the current key, as we opened it only
524 * with ENUM_SUBKEY access.
527 err = reg_openkey(mem_ctx, key, "", KEY_CREATE_SUB_KEY,
529 if (!W_ERROR_IS_OK(err)) {
534 * Actually create the subkey
537 err = fill_subkey_cache(create_parent);
538 if (!W_ERROR_IS_OK(err)) goto done;
540 err = create_reg_subkey(key->key, path);
541 W_ERROR_NOT_OK_GOTO_DONE(err);
544 * Now open the newly created key
547 err = reg_openkey(ctx, create_parent, path, desired_access, pkey);
548 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
549 *paction = REG_CREATED_NEW_KEY;
553 TALLOC_FREE(mem_ctx);
557 WERROR reg_deletekey(struct registry_key *parent, const char *path)
561 struct registry_key *tmp_key, *key;
562 TALLOC_CTX *mem_ctx = talloc_stackframe();
564 name = talloc_strdup(mem_ctx, path);
570 /* check if the key has subkeys */
571 err = reg_openkey(mem_ctx, parent, name, REG_KEY_READ, &key);
572 W_ERROR_NOT_OK_GOTO_DONE(err);
574 err = fill_subkey_cache(key);
575 W_ERROR_NOT_OK_GOTO_DONE(err);
577 if (regsubkey_ctr_numkeys(key->subkeys) > 0) {
578 err = WERR_ACCESS_DENIED;
582 /* no subkeys - proceed with delete */
583 end = strrchr(name, '\\');
587 err = reg_openkey(mem_ctx, parent, name,
588 KEY_CREATE_SUB_KEY, &tmp_key);
589 W_ERROR_NOT_OK_GOTO_DONE(err);
595 if (name[0] == '\0') {
596 err = WERR_INVALID_PARAM;
600 err = delete_reg_subkey(parent->key, name);
603 TALLOC_FREE(mem_ctx);
607 WERROR reg_setvalue(struct registry_key *key, const char *name,
608 const struct registry_value *val)
611 DATA_BLOB value_data;
614 if (!(key->key->access_granted & KEY_SET_VALUE)) {
615 return WERR_ACCESS_DENIED;
618 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
622 err = registry_push_value(key, val, &value_data);
623 if (!W_ERROR_IS_OK(err)) {
627 res = regval_ctr_addvalue(key->values, name, val->type,
628 (char *)value_data.data, value_data.length);
629 TALLOC_FREE(value_data.data);
632 TALLOC_FREE(key->values);
636 if (!store_reg_values(key->key, key->values)) {
637 TALLOC_FREE(key->values);
638 return WERR_REG_IO_FAILURE;
644 static WERROR reg_value_exists(struct registry_key *key, const char *name)
648 for (i=0; i<key->values->num_values; i++) {
649 if (strequal(key->values->values[i]->valuename, name)) {
657 WERROR reg_deletevalue(struct registry_key *key, const char *name)
661 if (!(key->key->access_granted & KEY_SET_VALUE)) {
662 return WERR_ACCESS_DENIED;
665 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
669 err = reg_value_exists(key, name);
670 if (!W_ERROR_IS_OK(err)) {
674 regval_ctr_delvalue(key->values, name);
676 if (!store_reg_values(key->key, key->values)) {
677 TALLOC_FREE(key->values);
678 return WERR_REG_IO_FAILURE;
684 WERROR reg_getkeysecurity(TALLOC_CTX *mem_ctx, struct registry_key *key,
685 struct security_descriptor **psecdesc)
687 return regkey_get_secdesc(mem_ctx, key->key, psecdesc);
690 WERROR reg_setkeysecurity(struct registry_key *key,
691 struct security_descriptor *psecdesc)
693 return regkey_set_secdesc(key->key, psecdesc);
696 WERROR reg_getversion(uint32_t *version)
698 if (version == NULL) {
699 return WERR_INVALID_PARAM;
702 *version = 0x00000005; /* Windows 2000 registry API version */
706 /*******************************************************************
707 Note: topkeypat is the *full* path that this *key will be
708 loaded into (including the name of the key)
709 ********************************************************************/
711 static WERROR reg_load_tree(REGF_FILE *regfile, const char *topkeypath,
715 struct registry_key_handle registry_key;
716 struct regval_ctr *values;
717 struct regsubkey_ctr *subkeys;
720 WERROR result = WERR_OK;
722 /* initialize the struct registry_key_handle structure */
724 registry_key.ops = reghook_cache_find(topkeypath);
725 if (!registry_key.ops) {
726 DEBUG(0, ("reg_load_tree: Failed to assign registry_ops "
727 "to [%s]\n", topkeypath));
731 registry_key.name = talloc_strdup(regfile->mem_ctx, topkeypath);
732 if (!registry_key.name) {
733 DEBUG(0, ("reg_load_tree: Talloc failed for reg_key.name!\n"));
737 /* now start parsing the values and subkeys */
739 result = regsubkey_ctr_init(regfile->mem_ctx, &subkeys);
740 W_ERROR_NOT_OK_RETURN(result);
742 values = TALLOC_ZERO_P(subkeys, struct regval_ctr);
743 if (values == NULL) {
747 /* copy values into the struct regval_ctr */
749 for (i=0; i<key->num_values; i++) {
750 regval_ctr_addvalue(values, key->values[i].valuename,
752 (char*)key->values[i].data,
753 (key->values[i].data_size & ~VK_DATA_IN_OFFSET));
756 /* copy subkeys into the struct regsubkey_ctr */
758 key->subkey_index = 0;
759 while ((subkey = regfio_fetch_subkey( regfile, key ))) {
760 result = regsubkey_ctr_addkey(subkeys, subkey->keyname);
761 if (!W_ERROR_IS_OK(result)) {
762 TALLOC_FREE(subkeys);
767 /* write this key and values out */
769 if (!store_reg_values(®istry_key, values)
770 || !store_reg_keys(®istry_key, subkeys))
772 DEBUG(0,("reg_load_tree: Failed to load %s!\n", topkeypath));
773 result = WERR_REG_IO_FAILURE;
776 TALLOC_FREE(subkeys);
778 if (!W_ERROR_IS_OK(result)) {
782 /* now continue to load each subkey registry tree */
784 key->subkey_index = 0;
785 while ((subkey = regfio_fetch_subkey(regfile, key))) {
786 path = talloc_asprintf(regfile->mem_ctx,
793 result = reg_load_tree(regfile, path, subkey);
794 if (!W_ERROR_IS_OK(result)) {
802 /*******************************************************************
803 ********************************************************************/
805 static WERROR restore_registry_key(struct registry_key_handle *krecord,
809 REGF_NK_REC *rootkey;
812 /* open the registry file....fail if the file already exists */
814 regfile = regfio_open(fname, (O_RDONLY), 0);
815 if (regfile == NULL) {
816 DEBUG(0, ("restore_registry_key: failed to open \"%s\" (%s)\n",
817 fname, strerror(errno)));
818 return ntstatus_to_werror(map_nt_error_from_unix(errno));
821 /* get the rootkey from the regf file and then load the tree
822 via recursive calls */
824 if (!(rootkey = regfio_rootkey(regfile))) {
825 regfio_close(regfile);
826 return WERR_REG_FILE_INVALID;
829 result = reg_load_tree(regfile, krecord->name, rootkey);
833 regfio_close(regfile);
838 WERROR reg_restorekey(struct registry_key *key, const char *fname)
840 return restore_registry_key(key->key, fname);
843 /********************************************************************
844 ********************************************************************/
846 static WERROR reg_write_tree(REGF_FILE *regfile, const char *keypath,
850 struct regval_ctr *values;
851 struct regsubkey_ctr *subkeys;
853 char *key_tmp = NULL;
854 char *keyname, *parentpath;
855 char *subkeypath = NULL;
857 struct registry_key_handle registry_key;
858 WERROR result = WERR_OK;
859 struct security_descriptor *sec_desc = NULL;
862 return WERR_GENERAL_FAILURE;
866 return WERR_OBJECT_PATH_INVALID;
869 /* split up the registry key path */
871 key_tmp = talloc_strdup(regfile->mem_ctx, keypath);
875 if (!reg_split_key(key_tmp, &parentpath, &keyname)) {
876 return WERR_OBJECT_PATH_INVALID;
880 keyname = parentpath;
883 /* we need a registry_key_handle object here to enumerate subkeys and values */
885 ZERO_STRUCT(registry_key);
887 registry_key.name = talloc_strdup(regfile->mem_ctx, keypath);
888 if (registry_key.name == NULL) {
892 registry_key.ops = reghook_cache_find(registry_key.name);
893 if (registry_key.ops == NULL) {
897 /* lookup the values and subkeys */
899 result = regsubkey_ctr_init(regfile->mem_ctx, &subkeys);
900 W_ERROR_NOT_OK_RETURN(result);
902 values = TALLOC_ZERO_P(subkeys, struct regval_ctr);
903 if (values == NULL) {
907 fetch_reg_keys(®istry_key, subkeys);
908 fetch_reg_values(®istry_key, values);
910 result = regkey_get_secdesc(regfile->mem_ctx, ®istry_key, &sec_desc);
911 if (!W_ERROR_IS_OK(result)) {
915 /* write out this key */
917 key = regfio_write_key(regfile, keyname, values, subkeys, sec_desc,
920 result = WERR_CAN_NOT_COMPLETE;
924 /* write each one of the subkeys out */
926 num_subkeys = regsubkey_ctr_numkeys(subkeys);
927 for (i=0; i<num_subkeys; i++) {
928 subkeyname = regsubkey_ctr_specific_key(subkeys, i);
929 subkeypath = talloc_asprintf(regfile->mem_ctx, "%s\\%s",
930 keypath, subkeyname);
931 if (subkeypath == NULL) {
935 result = reg_write_tree(regfile, subkeypath, key);
936 if (!W_ERROR_IS_OK(result))
940 DEBUG(6, ("reg_write_tree: wrote key [%s]\n", keypath));
943 TALLOC_FREE(subkeys);
944 TALLOC_FREE(registry_key.name);
949 static WERROR backup_registry_key(struct registry_key_handle *krecord,
955 /* open the registry file....fail if the file already exists */
957 regfile = regfio_open(fname, (O_RDWR|O_CREAT|O_EXCL),
959 if (regfile == NULL) {
960 DEBUG(0,("backup_registry_key: failed to open \"%s\" (%s)\n",
961 fname, strerror(errno) ));
962 return ntstatus_to_werror(map_nt_error_from_unix(errno));
965 /* write the registry tree to the file */
967 result = reg_write_tree(regfile, krecord->name, NULL);
971 regfio_close(regfile);
976 WERROR reg_savekey(struct registry_key *key, const char *fname)
978 return backup_registry_key(key->key, fname);
981 /**********************************************************************
982 * Higher level utility functions
983 **********************************************************************/
985 WERROR reg_deleteallvalues(struct registry_key *key)
990 if (!(key->key->access_granted & KEY_SET_VALUE)) {
991 return WERR_ACCESS_DENIED;
994 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
998 for (i=0; i<key->values->num_values; i++) {
999 regval_ctr_delvalue(key->values, key->values->values[i]->valuename);
1002 if (!store_reg_values(key->key, key->values)) {
1003 TALLOC_FREE(key->values);
1004 return WERR_REG_IO_FAILURE;
1011 * Utility function to open a complete registry path including the hive prefix.
1014 WERROR reg_open_path(TALLOC_CTX *mem_ctx, const char *orig_path,
1015 uint32 desired_access, const struct nt_user_token *token,
1016 struct registry_key **pkey)
1018 struct registry_key *hive, *key;
1022 if (!(path = SMB_STRDUP(orig_path))) {
1026 p = strchr(path, '\\');
1028 if ((p == NULL) || (p[1] == '\0')) {
1030 * No key behind the hive, just return the hive
1033 err = reg_openhive(mem_ctx, path, desired_access, token,
1035 if (!W_ERROR_IS_OK(err)) {
1046 err = reg_openhive(mem_ctx, path, KEY_ENUMERATE_SUB_KEYS, token,
1048 if (!W_ERROR_IS_OK(err)) {
1053 err = reg_openkey(mem_ctx, hive, p+1, desired_access, &key);
1058 if (!W_ERROR_IS_OK(err)) {
1067 * Utility function to delete a registry key with all its subkeys.
1068 * Note that reg_deletekey returns ACCESS_DENIED when called on a
1069 * key that has subkeys.
1071 static WERROR reg_deletekey_recursive_internal(TALLOC_CTX *ctx,
1072 struct registry_key *parent,
1076 TALLOC_CTX *mem_ctx = NULL;
1077 WERROR werr = WERR_OK;
1078 struct registry_key *key;
1079 char *subkey_name = NULL;
1082 mem_ctx = talloc_new(ctx);
1083 if (mem_ctx == NULL) {
1088 /* recurse through subkeys first */
1089 werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
1090 if (!W_ERROR_IS_OK(werr)) {
1094 werr = fill_subkey_cache(key);
1095 W_ERROR_NOT_OK_GOTO_DONE(werr);
1098 * loop from top to bottom for perfomance:
1099 * this way, we need to rehash the regsubkey containers less
1101 for (i = regsubkey_ctr_numkeys(key->subkeys) ; i > 0; i--) {
1102 subkey_name = regsubkey_ctr_specific_key(key->subkeys, i-1);
1103 werr = reg_deletekey_recursive_internal(mem_ctx, key,
1106 W_ERROR_NOT_OK_GOTO_DONE(werr);
1110 /* now delete the actual key */
1111 werr = reg_deletekey(parent, path);
1115 TALLOC_FREE(mem_ctx);
1119 static WERROR reg_deletekey_recursive_trans(TALLOC_CTX *ctx,
1120 struct registry_key *parent,
1126 werr = regdb_transaction_start();
1127 if (!W_ERROR_IS_OK(werr)) {
1128 DEBUG(0, ("reg_deletekey_recursive_trans: "
1129 "error starting transaction: %s\n",
1134 werr = reg_deletekey_recursive_internal(ctx, parent, path, del_key);
1136 if (!W_ERROR_IS_OK(werr)) {
1137 DEBUG(1, (__location__ " failed to delete key '%s' from key "
1138 "'%s': %s\n", path, parent->key->name,
1140 werr = regdb_transaction_cancel();
1141 if (!W_ERROR_IS_OK(werr)) {
1142 DEBUG(0, ("reg_deletekey_recursive_trans: "
1143 "error cancelling transaction: %s\n",
1147 werr = regdb_transaction_commit();
1148 if (!W_ERROR_IS_OK(werr)) {
1149 DEBUG(0, ("reg_deletekey_recursive_trans: "
1150 "error committing transaction: %s\n",
1158 WERROR reg_deletekey_recursive(TALLOC_CTX *ctx,
1159 struct registry_key *parent,
1162 return reg_deletekey_recursive_trans(ctx, parent, path, true);
1165 WERROR reg_deletesubkeys_recursive(TALLOC_CTX *ctx,
1166 struct registry_key *parent,
1169 return reg_deletekey_recursive_trans(ctx, parent, path, false);
1173 /* these two functions are unused. */
1176 * Utility function to create a registry key without opening the hive
1177 * before. Assumes the hive already exists.
1180 WERROR reg_create_path(TALLOC_CTX *mem_ctx, const char *orig_path,
1181 uint32 desired_access,
1182 const struct nt_user_token *token,
1183 enum winreg_CreateAction *paction,
1184 struct registry_key **pkey)
1186 struct registry_key *hive;
1190 if (!(path = SMB_STRDUP(orig_path))) {
1194 p = strchr(path, '\\');
1196 if ((p == NULL) || (p[1] == '\0')) {
1198 * No key behind the hive, just return the hive
1201 err = reg_openhive(mem_ctx, path, desired_access, token,
1203 if (!W_ERROR_IS_OK(err)) {
1209 *paction = REG_OPENED_EXISTING_KEY;
1215 err = reg_openhive(mem_ctx, path,
1216 (strchr(p+1, '\\') != NULL) ?
1217 KEY_ENUMERATE_SUB_KEYS : KEY_CREATE_SUB_KEY,
1219 if (!W_ERROR_IS_OK(err)) {
1224 err = reg_createkey(mem_ctx, hive, p+1, desired_access, pkey, paction);
1231 * Utility function to create a registry key without opening the hive
1232 * before. Will not delete a hive.
1235 WERROR reg_delete_path(const struct nt_user_token *token,
1236 const char *orig_path)
1238 struct registry_key *hive;
1242 if (!(path = SMB_STRDUP(orig_path))) {
1246 p = strchr(path, '\\');
1248 if ((p == NULL) || (p[1] == '\0')) {
1250 return WERR_INVALID_PARAM;
1255 err = reg_openhive(NULL, path,
1256 (strchr(p+1, '\\') != NULL) ?
1257 KEY_ENUMERATE_SUB_KEYS : KEY_CREATE_SUB_KEY,
1259 if (!W_ERROR_IS_OK(err)) {
1264 err = reg_deletekey(hive, p+1);