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
70 #define DBGC_CLASS DBGC_REGISTRY
73 /**********************************************************************
75 **********************************************************************/
77 static WERROR fill_value_cache(struct registry_key *key)
79 if (key->values != NULL) {
80 if (!reg_values_need_update(key->key, key->values)) {
85 if (!(key->values = TALLOC_ZERO_P(key, struct regval_ctr))) {
88 if (fetch_reg_values(key->key, key->values) == -1) {
89 TALLOC_FREE(key->values);
96 static WERROR fill_subkey_cache(struct registry_key *key)
100 if (key->subkeys != NULL) {
101 if (!reg_subkeys_need_update(key->key, key->subkeys)) {
106 werr = regsubkey_ctr_init(key, &(key->subkeys));
107 W_ERROR_NOT_OK_RETURN(werr);
109 if (fetch_reg_keys(key->key, key->subkeys) == -1) {
110 TALLOC_FREE(key->subkeys);
111 return WERR_NO_MORE_ITEMS;
117 static int regkey_destructor(struct registry_key_handle *key)
119 return regdb_close();
122 static WERROR regkey_open_onelevel(TALLOC_CTX *mem_ctx,
123 struct registry_key *parent,
125 const struct nt_user_token *token,
126 uint32 access_desired,
127 struct registry_key **pregkey)
129 WERROR result = WERR_OK;
130 struct registry_key *regkey;
131 struct registry_key_handle *key;
132 struct regsubkey_ctr *subkeys = NULL;
134 DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name));
136 SMB_ASSERT(strchr(name, '\\') == NULL);
138 if (!(regkey = TALLOC_ZERO_P(mem_ctx, struct registry_key)) ||
139 !(regkey->token = dup_nt_token(regkey, token)) ||
140 !(regkey->key = TALLOC_ZERO_P(regkey, struct registry_key_handle)))
146 if ( !(W_ERROR_IS_OK(result = regdb_open())) ) {
151 talloc_set_destructor(key, regkey_destructor);
155 key->type = REG_KEY_GENERIC;
157 if (name[0] == '\0') {
159 * Open a copy of the parent key
162 result = WERR_BADFILE;
165 key->name = talloc_strdup(key, parent->key->name);
171 key->name = talloc_asprintf(key, "%s%s%s",
172 parent ? parent->key->name : "",
177 if (key->name == NULL) {
182 /* Tag this as a Performance Counter Key */
184 if( StrnCaseCmp(key->name, KEY_HKPD, strlen(KEY_HKPD)) == 0 )
185 key->type = REG_KEY_HKPD;
187 /* Look up the table of registry I/O operations */
189 if ( !(key->ops = reghook_cache_find( key->name )) ) {
190 DEBUG(0,("reg_open_onelevel: Failed to assign "
191 "registry_ops to [%s]\n", key->name ));
192 result = WERR_BADFILE;
196 /* check if the path really exists; failed is indicated by -1 */
197 /* if the subkey count failed, bail out */
199 result = regsubkey_ctr_init(key, &subkeys);
200 if (!W_ERROR_IS_OK(result)) {
204 if ( fetch_reg_keys( key, subkeys ) == -1 ) {
205 result = WERR_BADFILE;
209 TALLOC_FREE( subkeys );
211 if ( !regkey_access_check( key, access_desired, &key->access_granted,
213 result = WERR_ACCESS_DENIED;
221 if ( !W_ERROR_IS_OK(result) ) {
228 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
229 uint32 desired_access,
230 const struct nt_user_token *token,
231 struct registry_key **pkey)
233 SMB_ASSERT(hive != NULL);
234 SMB_ASSERT(hive[0] != '\0');
235 SMB_ASSERT(strchr(hive, '\\') == NULL);
237 return regkey_open_onelevel(mem_ctx, NULL, hive, token, desired_access,
242 /**********************************************************************
244 **********************************************************************/
246 WERROR reg_openkey(TALLOC_CTX *mem_ctx, struct registry_key *parent,
247 const char *name, uint32 desired_access,
248 struct registry_key **pkey)
250 struct registry_key *direct_parent = parent;
252 char *p, *path, *to_free;
255 if (!(path = SMB_STRDUP(name))) {
262 if ((len > 0) && (path[len-1] == '\\')) {
266 while ((p = strchr(path, '\\')) != NULL) {
267 char *name_component;
268 struct registry_key *tmp;
270 if (!(name_component = SMB_STRNDUP(path, (p - path)))) {
275 err = regkey_open_onelevel(mem_ctx, direct_parent,
276 name_component, parent->token,
277 KEY_ENUMERATE_SUB_KEYS, &tmp);
278 SAFE_FREE(name_component);
280 if (!W_ERROR_IS_OK(err)) {
283 if (direct_parent != parent) {
284 TALLOC_FREE(direct_parent);
291 err = regkey_open_onelevel(mem_ctx, direct_parent, path, parent->token,
292 desired_access, pkey);
294 if (direct_parent != parent) {
295 TALLOC_FREE(direct_parent);
301 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
302 uint32 idx, char **name, NTTIME *last_write_time)
306 if (!(key->key->access_granted & KEY_ENUMERATE_SUB_KEYS)) {
307 return WERR_ACCESS_DENIED;
310 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
314 if (idx >= regsubkey_ctr_numkeys(key->subkeys)) {
315 return WERR_NO_MORE_ITEMS;
318 if (!(*name = talloc_strdup(mem_ctx,
319 regsubkey_ctr_specific_key(key->subkeys, idx))))
324 if (last_write_time) {
325 *last_write_time = 0;
331 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
332 uint32 idx, char **pname, struct registry_value **pval)
334 struct registry_value *val;
337 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
338 return WERR_ACCESS_DENIED;
341 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
345 if (idx >= key->values->num_values) {
346 return WERR_NO_MORE_ITEMS;
349 err = registry_pull_value(mem_ctx, &val,
350 key->values->values[idx]->type,
351 key->values->values[idx]->data_p,
352 key->values->values[idx]->size,
353 key->values->values[idx]->size);
354 if (!W_ERROR_IS_OK(err)) {
359 && !(*pname = talloc_strdup(
360 mem_ctx, key->values->values[idx]->valuename))) {
369 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
370 const char *name, struct registry_value **pval)
375 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
376 return WERR_ACCESS_DENIED;
379 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
383 for (i=0; i<key->values->num_values; i++) {
384 if (strequal(key->values->values[i]->valuename, name)) {
385 return reg_enumvalue(mem_ctx, key, i, NULL, pval);
392 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
393 uint32_t *max_subkeylen, uint32_t *max_subkeysize,
394 uint32_t *num_values, uint32_t *max_valnamelen,
395 uint32_t *max_valbufsize, uint32_t *secdescsize,
396 NTTIME *last_changed_time)
402 struct security_descriptor *secdesc;
404 if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
405 return WERR_ACCESS_DENIED;
408 if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
409 !W_ERROR_IS_OK(fill_value_cache(key))) {
414 for (i=0; i< regsubkey_ctr_numkeys(key->subkeys); i++) {
415 max_len = MAX(max_len,
416 strlen(regsubkey_ctr_specific_key(key->subkeys, i)));
419 *num_subkeys = regsubkey_ctr_numkeys(key->subkeys);
420 *max_subkeylen = max_len;
421 *max_subkeysize = 0; /* Class length? */
425 for (i=0; i<key->values->num_values; i++) {
426 max_len = MAX(max_len,
427 strlen(key->values->values[i]->valuename));
428 max_size = MAX(max_size, key->values->values[i]->size);
431 *num_values = key->values->num_values;
432 *max_valnamelen = max_len;
433 *max_valbufsize = max_size;
435 if (!(mem_ctx = talloc_new(key))) {
439 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
440 if (!W_ERROR_IS_OK(err)) {
441 TALLOC_FREE(mem_ctx);
445 *secdescsize = ndr_size_security_descriptor(secdesc, 0);
446 TALLOC_FREE(mem_ctx);
448 *last_changed_time = 0;
453 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
454 const char *subkeypath, uint32 desired_access,
455 struct registry_key **pkey,
456 enum winreg_CreateAction *paction)
458 struct registry_key *key = parent;
459 struct registry_key *create_parent;
465 * We must refuse to handle subkey-paths containing
466 * a '/' character because at a lower level, after
467 * normalization, '/' is treated as a key separator
470 if (strchr(subkeypath, '/') != NULL) {
471 return WERR_INVALID_PARAM;
474 if (!(mem_ctx = talloc_new(ctx))) return WERR_NOMEM;
476 if (!(path = talloc_strdup(mem_ctx, subkeypath))) {
481 while ((end = strchr(path, '\\')) != NULL) {
482 struct registry_key *tmp;
483 enum winreg_CreateAction action;
487 err = reg_createkey(mem_ctx, key, path,
488 KEY_ENUMERATE_SUB_KEYS, &tmp, &action);
489 if (!W_ERROR_IS_OK(err)) {
502 * At this point, "path" contains the one-element subkey of "key". We
503 * can try to open it.
506 err = reg_openkey(ctx, key, path, desired_access, pkey);
507 if (W_ERROR_IS_OK(err)) {
508 if (paction != NULL) {
509 *paction = REG_OPENED_EXISTING_KEY;
514 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
516 * Something but "notfound" has happened, so bail out
522 * We have to make a copy of the current key, as we opened it only
523 * with ENUM_SUBKEY access.
526 err = reg_openkey(mem_ctx, key, "", KEY_CREATE_SUB_KEY,
528 if (!W_ERROR_IS_OK(err)) {
533 * Actually create the subkey
536 err = fill_subkey_cache(create_parent);
537 if (!W_ERROR_IS_OK(err)) goto done;
539 err = create_reg_subkey(key->key, path);
540 W_ERROR_NOT_OK_GOTO_DONE(err);
543 * Now open the newly created key
546 err = reg_openkey(ctx, create_parent, path, desired_access, pkey);
547 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
548 *paction = REG_CREATED_NEW_KEY;
552 TALLOC_FREE(mem_ctx);
556 WERROR reg_deletekey(struct registry_key *parent, const char *path)
560 struct registry_key *tmp_key, *key;
561 TALLOC_CTX *mem_ctx = talloc_stackframe();
563 name = talloc_strdup(mem_ctx, path);
569 /* check if the key has subkeys */
570 err = reg_openkey(mem_ctx, parent, name, REG_KEY_READ, &key);
571 W_ERROR_NOT_OK_GOTO_DONE(err);
573 err = fill_subkey_cache(key);
574 W_ERROR_NOT_OK_GOTO_DONE(err);
576 if (regsubkey_ctr_numkeys(key->subkeys) > 0) {
577 err = WERR_ACCESS_DENIED;
581 /* no subkeys - proceed with delete */
582 end = strrchr(name, '\\');
586 err = reg_openkey(mem_ctx, parent, name,
587 KEY_CREATE_SUB_KEY, &tmp_key);
588 W_ERROR_NOT_OK_GOTO_DONE(err);
594 if (name[0] == '\0') {
595 err = WERR_INVALID_PARAM;
599 err = delete_reg_subkey(parent->key, name);
602 TALLOC_FREE(mem_ctx);
606 WERROR reg_setvalue(struct registry_key *key, const char *name,
607 const struct registry_value *val)
610 DATA_BLOB value_data;
613 if (!(key->key->access_granted & KEY_SET_VALUE)) {
614 return WERR_ACCESS_DENIED;
617 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
621 err = registry_push_value(key, val, &value_data);
622 if (!W_ERROR_IS_OK(err)) {
626 res = regval_ctr_addvalue(key->values, name, val->type,
627 (char *)value_data.data, value_data.length);
628 TALLOC_FREE(value_data.data);
631 TALLOC_FREE(key->values);
635 if (!store_reg_values(key->key, key->values)) {
636 TALLOC_FREE(key->values);
637 return WERR_REG_IO_FAILURE;
643 static WERROR reg_value_exists(struct registry_key *key, const char *name)
647 for (i=0; i<key->values->num_values; i++) {
648 if (strequal(key->values->values[i]->valuename, name)) {
656 WERROR reg_deletevalue(struct registry_key *key, const char *name)
660 if (!(key->key->access_granted & KEY_SET_VALUE)) {
661 return WERR_ACCESS_DENIED;
664 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
668 err = reg_value_exists(key, name);
669 if (!W_ERROR_IS_OK(err)) {
673 regval_ctr_delvalue(key->values, name);
675 if (!store_reg_values(key->key, key->values)) {
676 TALLOC_FREE(key->values);
677 return WERR_REG_IO_FAILURE;
683 WERROR reg_getkeysecurity(TALLOC_CTX *mem_ctx, struct registry_key *key,
684 struct security_descriptor **psecdesc)
686 return regkey_get_secdesc(mem_ctx, key->key, psecdesc);
689 WERROR reg_setkeysecurity(struct registry_key *key,
690 struct security_descriptor *psecdesc)
692 return regkey_set_secdesc(key->key, psecdesc);
695 WERROR reg_getversion(uint32_t *version)
697 if (version == NULL) {
698 return WERR_INVALID_PARAM;
701 *version = 0x00000005; /* Windows 2000 registry API version */
705 /*******************************************************************
706 Note: topkeypat is the *full* path that this *key will be
707 loaded into (including the name of the key)
708 ********************************************************************/
710 static WERROR reg_load_tree(REGF_FILE *regfile, const char *topkeypath,
714 struct registry_key_handle registry_key;
715 struct regval_ctr *values;
716 struct regsubkey_ctr *subkeys;
719 WERROR result = WERR_OK;
721 /* initialize the struct registry_key_handle structure */
723 registry_key.ops = reghook_cache_find(topkeypath);
724 if (!registry_key.ops) {
725 DEBUG(0, ("reg_load_tree: Failed to assign registry_ops "
726 "to [%s]\n", topkeypath));
730 registry_key.name = talloc_strdup(regfile->mem_ctx, topkeypath);
731 if (!registry_key.name) {
732 DEBUG(0, ("reg_load_tree: Talloc failed for reg_key.name!\n"));
736 /* now start parsing the values and subkeys */
738 result = regsubkey_ctr_init(regfile->mem_ctx, &subkeys);
739 W_ERROR_NOT_OK_RETURN(result);
741 values = TALLOC_ZERO_P(subkeys, struct regval_ctr);
742 if (values == NULL) {
746 /* copy values into the struct regval_ctr */
748 for (i=0; i<key->num_values; i++) {
749 regval_ctr_addvalue(values, key->values[i].valuename,
751 (char*)key->values[i].data,
752 (key->values[i].data_size & ~VK_DATA_IN_OFFSET));
755 /* copy subkeys into the struct regsubkey_ctr */
757 key->subkey_index = 0;
758 while ((subkey = regfio_fetch_subkey( regfile, key ))) {
759 result = regsubkey_ctr_addkey(subkeys, subkey->keyname);
760 if (!W_ERROR_IS_OK(result)) {
761 TALLOC_FREE(subkeys);
766 /* write this key and values out */
768 if (!store_reg_values(®istry_key, values)
769 || !store_reg_keys(®istry_key, subkeys))
771 DEBUG(0,("reg_load_tree: Failed to load %s!\n", topkeypath));
772 result = WERR_REG_IO_FAILURE;
775 TALLOC_FREE(subkeys);
777 if (!W_ERROR_IS_OK(result)) {
781 /* now continue to load each subkey registry tree */
783 key->subkey_index = 0;
784 while ((subkey = regfio_fetch_subkey(regfile, key))) {
785 path = talloc_asprintf(regfile->mem_ctx,
792 result = reg_load_tree(regfile, path, subkey);
793 if (!W_ERROR_IS_OK(result)) {
801 /*******************************************************************
802 ********************************************************************/
804 static WERROR restore_registry_key(struct registry_key_handle *krecord,
808 REGF_NK_REC *rootkey;
811 /* open the registry file....fail if the file already exists */
813 regfile = regfio_open(fname, (O_RDONLY), 0);
814 if (regfile == NULL) {
815 DEBUG(0, ("restore_registry_key: failed to open \"%s\" (%s)\n",
816 fname, strerror(errno)));
817 return ntstatus_to_werror(map_nt_error_from_unix(errno));
820 /* get the rootkey from the regf file and then load the tree
821 via recursive calls */
823 if (!(rootkey = regfio_rootkey(regfile))) {
824 regfio_close(regfile);
825 return WERR_REG_FILE_INVALID;
828 result = reg_load_tree(regfile, krecord->name, rootkey);
832 regfio_close(regfile);
837 WERROR reg_restorekey(struct registry_key *key, const char *fname)
839 return restore_registry_key(key->key, fname);
842 /********************************************************************
843 ********************************************************************/
845 static WERROR reg_write_tree(REGF_FILE *regfile, const char *keypath,
849 struct regval_ctr *values;
850 struct regsubkey_ctr *subkeys;
852 char *key_tmp = NULL;
853 char *keyname, *parentpath;
854 char *subkeypath = NULL;
856 struct registry_key_handle registry_key;
857 WERROR result = WERR_OK;
858 struct security_descriptor *sec_desc = NULL;
861 return WERR_GENERAL_FAILURE;
865 return WERR_OBJECT_PATH_INVALID;
868 /* split up the registry key path */
870 key_tmp = talloc_strdup(regfile->mem_ctx, keypath);
874 if (!reg_split_key(key_tmp, &parentpath, &keyname)) {
875 return WERR_OBJECT_PATH_INVALID;
879 keyname = parentpath;
882 /* we need a registry_key_handle object here to enumerate subkeys and values */
884 ZERO_STRUCT(registry_key);
886 registry_key.name = talloc_strdup(regfile->mem_ctx, keypath);
887 if (registry_key.name == NULL) {
891 registry_key.ops = reghook_cache_find(registry_key.name);
892 if (registry_key.ops == NULL) {
896 /* lookup the values and subkeys */
898 result = regsubkey_ctr_init(regfile->mem_ctx, &subkeys);
899 W_ERROR_NOT_OK_RETURN(result);
901 values = TALLOC_ZERO_P(subkeys, struct regval_ctr);
902 if (values == NULL) {
906 fetch_reg_keys(®istry_key, subkeys);
907 fetch_reg_values(®istry_key, values);
909 result = regkey_get_secdesc(regfile->mem_ctx, ®istry_key, &sec_desc);
910 if (!W_ERROR_IS_OK(result)) {
914 /* write out this key */
916 key = regfio_write_key(regfile, keyname, values, subkeys, sec_desc,
919 result = WERR_CAN_NOT_COMPLETE;
923 /* write each one of the subkeys out */
925 num_subkeys = regsubkey_ctr_numkeys(subkeys);
926 for (i=0; i<num_subkeys; i++) {
927 subkeyname = regsubkey_ctr_specific_key(subkeys, i);
928 subkeypath = talloc_asprintf(regfile->mem_ctx, "%s\\%s",
929 keypath, subkeyname);
930 if (subkeypath == NULL) {
934 result = reg_write_tree(regfile, subkeypath, key);
935 if (!W_ERROR_IS_OK(result))
939 DEBUG(6, ("reg_write_tree: wrote key [%s]\n", keypath));
942 TALLOC_FREE(subkeys);
943 TALLOC_FREE(registry_key.name);
948 static WERROR backup_registry_key(struct registry_key_handle *krecord,
954 /* open the registry file....fail if the file already exists */
956 regfile = regfio_open(fname, (O_RDWR|O_CREAT|O_EXCL),
958 if (regfile == NULL) {
959 DEBUG(0,("backup_registry_key: failed to open \"%s\" (%s)\n",
960 fname, strerror(errno) ));
961 return ntstatus_to_werror(map_nt_error_from_unix(errno));
964 /* write the registry tree to the file */
966 result = reg_write_tree(regfile, krecord->name, NULL);
970 regfio_close(regfile);
975 WERROR reg_savekey(struct registry_key *key, const char *fname)
977 return backup_registry_key(key->key, fname);
980 /**********************************************************************
981 * Higher level utility functions
982 **********************************************************************/
984 WERROR reg_deleteallvalues(struct registry_key *key)
989 if (!(key->key->access_granted & KEY_SET_VALUE)) {
990 return WERR_ACCESS_DENIED;
993 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
997 for (i=0; i<key->values->num_values; i++) {
998 regval_ctr_delvalue(key->values, key->values->values[i]->valuename);
1001 if (!store_reg_values(key->key, key->values)) {
1002 TALLOC_FREE(key->values);
1003 return WERR_REG_IO_FAILURE;
1010 * Utility function to open a complete registry path including the hive prefix.
1013 WERROR reg_open_path(TALLOC_CTX *mem_ctx, const char *orig_path,
1014 uint32 desired_access, const struct nt_user_token *token,
1015 struct registry_key **pkey)
1017 struct registry_key *hive, *key;
1021 if (!(path = SMB_STRDUP(orig_path))) {
1025 p = strchr(path, '\\');
1027 if ((p == NULL) || (p[1] == '\0')) {
1029 * No key behind the hive, just return the hive
1032 err = reg_openhive(mem_ctx, path, desired_access, token,
1034 if (!W_ERROR_IS_OK(err)) {
1045 err = reg_openhive(mem_ctx, path, KEY_ENUMERATE_SUB_KEYS, token,
1047 if (!W_ERROR_IS_OK(err)) {
1052 err = reg_openkey(mem_ctx, hive, p+1, desired_access, &key);
1057 if (!W_ERROR_IS_OK(err)) {
1066 * Utility function to delete a registry key with all its subkeys.
1067 * Note that reg_deletekey returns ACCESS_DENIED when called on a
1068 * key that has subkeys.
1070 static WERROR reg_deletekey_recursive_internal(TALLOC_CTX *ctx,
1071 struct registry_key *parent,
1075 TALLOC_CTX *mem_ctx = NULL;
1076 WERROR werr = WERR_OK;
1077 struct registry_key *key;
1078 char *subkey_name = NULL;
1081 mem_ctx = talloc_new(ctx);
1082 if (mem_ctx == NULL) {
1087 /* recurse through subkeys first */
1088 werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
1089 if (!W_ERROR_IS_OK(werr)) {
1093 werr = fill_subkey_cache(key);
1094 W_ERROR_NOT_OK_GOTO_DONE(werr);
1097 * loop from top to bottom for perfomance:
1098 * this way, we need to rehash the regsubkey containers less
1100 for (i = regsubkey_ctr_numkeys(key->subkeys) ; i > 0; i--) {
1101 subkey_name = regsubkey_ctr_specific_key(key->subkeys, i-1);
1102 werr = reg_deletekey_recursive_internal(mem_ctx, key,
1105 W_ERROR_NOT_OK_GOTO_DONE(werr);
1109 /* now delete the actual key */
1110 werr = reg_deletekey(parent, path);
1114 TALLOC_FREE(mem_ctx);
1118 static WERROR reg_deletekey_recursive_trans(TALLOC_CTX *ctx,
1119 struct registry_key *parent,
1125 werr = regdb_transaction_start();
1126 if (!W_ERROR_IS_OK(werr)) {
1127 DEBUG(0, ("reg_deletekey_recursive_trans: "
1128 "error starting transaction: %s\n",
1133 werr = reg_deletekey_recursive_internal(ctx, parent, path, del_key);
1135 if (!W_ERROR_IS_OK(werr)) {
1136 DEBUG(1, (__location__ " failed to delete key '%s' from key "
1137 "'%s': %s\n", path, parent->key->name,
1139 werr = regdb_transaction_cancel();
1140 if (!W_ERROR_IS_OK(werr)) {
1141 DEBUG(0, ("reg_deletekey_recursive_trans: "
1142 "error cancelling transaction: %s\n",
1146 werr = regdb_transaction_commit();
1147 if (!W_ERROR_IS_OK(werr)) {
1148 DEBUG(0, ("reg_deletekey_recursive_trans: "
1149 "error committing transaction: %s\n",
1157 WERROR reg_deletekey_recursive(TALLOC_CTX *ctx,
1158 struct registry_key *parent,
1161 return reg_deletekey_recursive_trans(ctx, parent, path, true);
1164 WERROR reg_deletesubkeys_recursive(TALLOC_CTX *ctx,
1165 struct registry_key *parent,
1168 return reg_deletekey_recursive_trans(ctx, parent, path, false);
1172 /* these two functions are unused. */
1175 * Utility function to create a registry key without opening the hive
1176 * before. Assumes the hive already exists.
1179 WERROR reg_create_path(TALLOC_CTX *mem_ctx, const char *orig_path,
1180 uint32 desired_access,
1181 const struct nt_user_token *token,
1182 enum winreg_CreateAction *paction,
1183 struct registry_key **pkey)
1185 struct registry_key *hive;
1189 if (!(path = SMB_STRDUP(orig_path))) {
1193 p = strchr(path, '\\');
1195 if ((p == NULL) || (p[1] == '\0')) {
1197 * No key behind the hive, just return the hive
1200 err = reg_openhive(mem_ctx, path, desired_access, token,
1202 if (!W_ERROR_IS_OK(err)) {
1208 *paction = REG_OPENED_EXISTING_KEY;
1214 err = reg_openhive(mem_ctx, path,
1215 (strchr(p+1, '\\') != NULL) ?
1216 KEY_ENUMERATE_SUB_KEYS : KEY_CREATE_SUB_KEY,
1218 if (!W_ERROR_IS_OK(err)) {
1223 err = reg_createkey(mem_ctx, hive, p+1, desired_access, pkey, paction);
1230 * Utility function to create a registry key without opening the hive
1231 * before. Will not delete a hive.
1234 WERROR reg_delete_path(const struct nt_user_token *token,
1235 const char *orig_path)
1237 struct registry_key *hive;
1241 if (!(path = SMB_STRDUP(orig_path))) {
1245 p = strchr(path, '\\');
1247 if ((p == NULL) || (p[1] == '\0')) {
1249 return WERR_INVALID_PARAM;
1254 err = reg_openhive(NULL, path,
1255 (strchr(p+1, '\\') != NULL) ?
1256 KEY_ENUMERATE_SUB_KEYS : KEY_CREATE_SUB_KEY,
1258 if (!W_ERROR_IS_OK(err)) {
1263 err = reg_deletekey(hive, p+1);