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)
97 if (key->subkeys != NULL) {
98 if (!reg_subkeys_need_update(key->key, key->subkeys)) {
103 if (!(key->subkeys = TALLOC_ZERO_P(key, REGSUBKEY_CTR))) {
107 if (fetch_reg_keys(key->key, key->subkeys) == -1) {
108 TALLOC_FREE(key->subkeys);
109 return WERR_NO_MORE_ITEMS;
115 static int regkey_destructor(REGISTRY_KEY *key)
117 return regdb_close();
120 static WERROR regkey_open_onelevel(TALLOC_CTX *mem_ctx,
121 struct registry_key *parent,
123 const struct nt_user_token *token,
124 uint32 access_desired,
125 struct registry_key **pregkey)
127 WERROR result = WERR_OK;
128 struct registry_key *regkey;
130 REGSUBKEY_CTR *subkeys = NULL;
132 DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name));
134 SMB_ASSERT(strchr(name, '\\') == NULL);
136 if (!(regkey = TALLOC_ZERO_P(mem_ctx, struct registry_key)) ||
137 !(regkey->token = dup_nt_token(regkey, token)) ||
138 !(regkey->key = TALLOC_ZERO_P(regkey, REGISTRY_KEY))) {
143 if ( !(W_ERROR_IS_OK(result = regdb_open())) ) {
148 talloc_set_destructor(key, regkey_destructor);
152 key->type = REG_KEY_GENERIC;
154 if (name[0] == '\0') {
156 * Open a copy of the parent key
159 result = WERR_BADFILE;
162 key->name = talloc_strdup(key, parent->key->name);
168 key->name = talloc_asprintf(key, "%s%s%s",
169 parent ? parent->key->name : "",
174 if (key->name == NULL) {
179 /* Tag this as a Performance Counter Key */
181 if( StrnCaseCmp(key->name, KEY_HKPD, strlen(KEY_HKPD)) == 0 )
182 key->type = REG_KEY_HKPD;
184 /* Look up the table of registry I/O operations */
186 if ( !(key->hook = reghook_cache_find( key->name )) ) {
187 DEBUG(0,("reg_open_onelevel: Failed to assign a "
188 "REGISTRY_HOOK to [%s]\n", key->name ));
189 result = WERR_BADFILE;
193 /* check if the path really exists; failed is indicated by -1 */
194 /* if the subkey count failed, bail out */
196 if ( !(subkeys = TALLOC_ZERO_P( key, REGSUBKEY_CTR )) ) {
201 if ( fetch_reg_keys( key, subkeys ) == -1 ) {
202 result = WERR_BADFILE;
206 TALLOC_FREE( subkeys );
208 if ( !regkey_access_check( key, access_desired, &key->access_granted,
210 result = WERR_ACCESS_DENIED;
218 if ( !W_ERROR_IS_OK(result) ) {
225 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
226 uint32 desired_access,
227 const struct nt_user_token *token,
228 struct registry_key **pkey)
230 SMB_ASSERT(hive != NULL);
231 SMB_ASSERT(hive[0] != '\0');
232 SMB_ASSERT(strchr(hive, '\\') == NULL);
234 return regkey_open_onelevel(mem_ctx, NULL, hive, token, desired_access,
239 /**********************************************************************
241 **********************************************************************/
243 WERROR reg_openkey(TALLOC_CTX *mem_ctx, struct registry_key *parent,
244 const char *name, uint32 desired_access,
245 struct registry_key **pkey)
247 struct registry_key *direct_parent = parent;
249 char *p, *path, *to_free;
252 if (!(path = SMB_STRDUP(name))) {
259 if ((len > 0) && (path[len-1] == '\\')) {
263 while ((p = strchr(path, '\\')) != NULL) {
264 char *name_component;
265 struct registry_key *tmp;
267 if (!(name_component = SMB_STRNDUP(path, (p - path)))) {
272 err = regkey_open_onelevel(mem_ctx, direct_parent,
273 name_component, parent->token,
274 SEC_RIGHTS_ENUM_SUBKEYS, &tmp);
275 SAFE_FREE(name_component);
277 if (!W_ERROR_IS_OK(err)) {
280 if (direct_parent != parent) {
281 TALLOC_FREE(direct_parent);
288 err = regkey_open_onelevel(mem_ctx, direct_parent, path, parent->token,
289 desired_access, pkey);
291 if (direct_parent != parent) {
292 TALLOC_FREE(direct_parent);
298 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
299 uint32 idx, char **name, NTTIME *last_write_time)
303 if (!(key->key->access_granted & SEC_RIGHTS_ENUM_SUBKEYS)) {
304 return WERR_ACCESS_DENIED;
307 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
311 if (idx >= key->subkeys->num_subkeys) {
312 return WERR_NO_MORE_ITEMS;
315 if (!(*name = talloc_strdup(mem_ctx, key->subkeys->subkeys[idx]))) {
319 if (last_write_time) {
320 *last_write_time = 0;
326 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
327 uint32 idx, char **pname, struct registry_value **pval)
329 struct registry_value *val;
332 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
333 return WERR_ACCESS_DENIED;
336 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
340 if (idx >= key->values->num_values) {
341 return WERR_NO_MORE_ITEMS;
344 err = registry_pull_value(mem_ctx, &val,
345 key->values->values[idx]->type,
346 key->values->values[idx]->data_p,
347 key->values->values[idx]->size,
348 key->values->values[idx]->size);
349 if (!W_ERROR_IS_OK(err)) {
354 && !(*pname = talloc_strdup(
355 mem_ctx, key->values->values[idx]->valuename))) {
364 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
365 const char *name, struct registry_value **pval)
370 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
371 return WERR_ACCESS_DENIED;
374 if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
378 for (i=0; i<key->values->num_values; i++) {
379 if (strequal(key->values->values[i]->valuename, name)) {
380 return reg_enumvalue(mem_ctx, key, i, NULL, pval);
387 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
388 uint32_t *max_subkeylen, uint32_t *max_subkeysize,
389 uint32_t *num_values, uint32_t *max_valnamelen,
390 uint32_t *max_valbufsize, uint32_t *secdescsize,
391 NTTIME *last_changed_time)
397 struct security_descriptor *secdesc;
399 if (!(key->key->access_granted & SEC_RIGHTS_QUERY_VALUE)) {
400 return WERR_ACCESS_DENIED;
403 if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
404 !W_ERROR_IS_OK(fill_value_cache(key))) {
409 for (i=0; i<key->subkeys->num_subkeys; i++) {
410 max_len = MAX(max_len, strlen(key->subkeys->subkeys[i]));
413 *num_subkeys = key->subkeys->num_subkeys;
414 *max_subkeylen = max_len;
415 *max_subkeysize = 0; /* Class length? */
419 for (i=0; i<key->values->num_values; i++) {
420 max_len = MAX(max_len,
421 strlen(key->values->values[i]->valuename));
422 max_size = MAX(max_size, key->values->values[i]->size);
425 *num_values = key->values->num_values;
426 *max_valnamelen = max_len;
427 *max_valbufsize = max_size;
429 if (!(mem_ctx = talloc_new(key))) {
433 err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
434 if (!W_ERROR_IS_OK(err)) {
435 TALLOC_FREE(mem_ctx);
439 *secdescsize = ndr_size_security_descriptor(secdesc, 0);
440 TALLOC_FREE(mem_ctx);
442 *last_changed_time = 0;
447 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
448 const char *subkeypath, uint32 desired_access,
449 struct registry_key **pkey,
450 enum winreg_CreateAction *paction)
452 struct registry_key *key = parent;
453 struct registry_key *create_parent;
458 if (!(mem_ctx = talloc_new(ctx))) return WERR_NOMEM;
460 if (!(path = talloc_strdup(mem_ctx, subkeypath))) {
465 while ((end = strchr(path, '\\')) != NULL) {
466 struct registry_key *tmp;
467 enum winreg_CreateAction action;
471 err = reg_createkey(mem_ctx, key, path,
472 SEC_RIGHTS_ENUM_SUBKEYS, &tmp, &action);
473 if (!W_ERROR_IS_OK(err)) {
486 * At this point, "path" contains the one-element subkey of "key". We
487 * can try to open it.
490 err = reg_openkey(ctx, key, path, desired_access, pkey);
491 if (W_ERROR_IS_OK(err)) {
492 if (paction != NULL) {
493 *paction = REG_OPENED_EXISTING_KEY;
498 if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
500 * Something but "notfound" has happened, so bail out
506 * We have to make a copy of the current key, as we opened it only
507 * with ENUM_SUBKEY access.
510 err = reg_openkey(mem_ctx, key, "", SEC_RIGHTS_CREATE_SUBKEY,
512 if (!W_ERROR_IS_OK(err)) {
517 * Actually create the subkey
520 err = fill_subkey_cache(create_parent);
521 if (!W_ERROR_IS_OK(err)) goto done;
523 err = regsubkey_ctr_addkey(create_parent->subkeys, path);
524 if (!W_ERROR_IS_OK(err)) goto done;
526 if (!store_reg_keys(create_parent->key, create_parent->subkeys)) {
527 TALLOC_FREE(create_parent->subkeys);
528 err = WERR_REG_IO_FAILURE;
533 * Now open the newly created key
536 err = reg_openkey(ctx, create_parent, path, desired_access, pkey);
537 if (W_ERROR_IS_OK(err) && (paction != NULL)) {
538 *paction = REG_CREATED_NEW_KEY;
542 TALLOC_FREE(mem_ctx);
546 WERROR reg_deletekey(struct registry_key *parent, const char *path)
552 struct registry_key *tmp_key, *key;
554 if (!(mem_ctx = talloc_init("reg_createkey"))) return WERR_NOMEM;
556 if (!(name = talloc_strdup(mem_ctx, path))) {
561 /* check if the key has subkeys */
562 err = reg_openkey(mem_ctx, parent, name, REG_KEY_READ, &key);
563 if (!W_ERROR_IS_OK(err)) {
566 if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
569 if (key->subkeys->num_subkeys > 0) {
570 err = WERR_ACCESS_DENIED;
574 /* no subkeys - proceed with delete */
575 if ((end = strrchr(name, '\\')) != NULL) {
578 err = reg_openkey(mem_ctx, parent, name,
579 SEC_RIGHTS_CREATE_SUBKEY, &tmp_key);
580 if (!W_ERROR_IS_OK(err)) {
588 if (name[0] == '\0') {
589 err = WERR_INVALID_PARAM;
593 if (!W_ERROR_IS_OK(err = fill_subkey_cache(parent))) {
597 num_subkeys = parent->subkeys->num_subkeys;
599 if (regsubkey_ctr_delkey(parent->subkeys, name) == num_subkeys) {
604 if (!store_reg_keys(parent->key, parent->subkeys)) {
605 TALLOC_FREE(parent->subkeys);
606 err = WERR_REG_IO_FAILURE;
610 regkey_set_secdesc(key->key, NULL);
615 TALLOC_FREE(mem_ctx);
619 WERROR reg_setvalue(struct registry_key *key, const char *name,
620 const struct registry_value *val)
623 DATA_BLOB value_data;
626 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
627 return WERR_ACCESS_DENIED;
630 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
634 err = registry_push_value(key, val, &value_data);
635 if (!W_ERROR_IS_OK(err)) {
639 res = regval_ctr_addvalue(key->values, name, val->type,
640 (char *)value_data.data, value_data.length);
641 TALLOC_FREE(value_data.data);
644 TALLOC_FREE(key->values);
648 if (!store_reg_values(key->key, key->values)) {
649 TALLOC_FREE(key->values);
650 return WERR_REG_IO_FAILURE;
656 WERROR reg_deletevalue(struct registry_key *key, const char *name)
660 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
661 return WERR_ACCESS_DENIED;
664 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
668 regval_ctr_delvalue(key->values, name);
670 if (!store_reg_values(key->key, key->values)) {
671 TALLOC_FREE(key->values);
672 return WERR_REG_IO_FAILURE;
678 WERROR reg_getkeysecurity(TALLOC_CTX *mem_ctx, struct registry_key *key,
679 struct security_descriptor **psecdesc)
681 return regkey_get_secdesc(mem_ctx, key->key, psecdesc);
684 WERROR reg_setkeysecurity(struct registry_key *key,
685 struct security_descriptor *psecdesc)
687 return regkey_set_secdesc(key->key, psecdesc);
690 WERROR reg_getversion(uint32_t *version)
692 if (version == NULL) {
693 return WERR_INVALID_PARAM;
696 *version = 0x00000005; /* Windows 2000 registry API version */
700 /*******************************************************************
701 Note: topkeypat is the *full* path that this *key will be
702 loaded into (including the name of the key)
703 ********************************************************************/
705 static WERROR reg_load_tree(REGF_FILE *regfile, const char *topkeypath,
709 REGISTRY_KEY registry_key;
711 REGSUBKEY_CTR *subkeys;
714 WERROR result = WERR_OK;
716 /* initialize the REGISTRY_KEY structure */
718 registry_key.hook = reghook_cache_find(topkeypath);
719 if (!registry_key.hook) {
720 DEBUG(0, ("reg_load_tree: Failed to assigned a REGISTRY_HOOK "
721 "to [%s]\n", topkeypath));
725 registry_key.name = talloc_strdup(regfile->mem_ctx, topkeypath);
726 if (!registry_key.name) {
727 DEBUG(0, ("reg_load_tree: Talloc failed for reg_key.name!\n"));
731 /* now start parsing the values and subkeys */
733 subkeys = TALLOC_ZERO_P(regfile->mem_ctx, REGSUBKEY_CTR);
734 if (subkeys == NULL) {
738 values = TALLOC_ZERO_P(subkeys, REGVAL_CTR);
739 if (values == NULL) {
743 /* copy values into the REGVAL_CTR */
745 for (i=0; i<key->num_values; i++) {
746 regval_ctr_addvalue(values, key->values[i].valuename,
748 (char*)key->values[i].data,
749 (key->values[i].data_size & ~VK_DATA_IN_OFFSET));
752 /* copy subkeys into the REGSUBKEY_CTR */
754 key->subkey_index = 0;
755 while ((subkey = regfio_fetch_subkey( regfile, key ))) {
756 regsubkey_ctr_addkey(subkeys, subkey->keyname);
759 /* write this key and values out */
761 if (!store_reg_values(®istry_key, values)
762 || !store_reg_keys(®istry_key, subkeys))
764 DEBUG(0,("reg_load_tree: Failed to load %s!\n", topkeypath));
765 result = WERR_REG_IO_FAILURE;
768 TALLOC_FREE(subkeys);
770 if (!W_ERROR_IS_OK(result)) {
774 /* now continue to load each subkey registry tree */
776 key->subkey_index = 0;
777 while ((subkey = regfio_fetch_subkey(regfile, key))) {
778 path = talloc_asprintf(regfile->mem_ctx,
785 result = reg_load_tree(regfile, path, subkey);
786 if (!W_ERROR_IS_OK(result)) {
794 /*******************************************************************
795 ********************************************************************/
797 static WERROR restore_registry_key(REGISTRY_KEY *krecord, const char *fname)
800 REGF_NK_REC *rootkey;
803 /* open the registry file....fail if the file already exists */
805 regfile = regfio_open(fname, (O_RDONLY), 0);
806 if (regfile == NULL) {
807 DEBUG(0, ("restore_registry_key: failed to open \"%s\" (%s)\n",
808 fname, strerror(errno)));
809 return ntstatus_to_werror(map_nt_error_from_unix(errno));
812 /* get the rootkey from the regf file and then load the tree
813 via recursive calls */
815 if (!(rootkey = regfio_rootkey(regfile))) {
816 regfio_close(regfile);
817 return WERR_REG_FILE_INVALID;
820 result = reg_load_tree(regfile, krecord->name, rootkey);
824 regfio_close(regfile);
829 WERROR reg_restorekey(struct registry_key *key, const char *fname)
831 return restore_registry_key(key->key, fname);
834 /********************************************************************
835 ********************************************************************/
837 static WERROR reg_write_tree(REGF_FILE *regfile, const char *keypath,
842 REGSUBKEY_CTR *subkeys;
844 char *key_tmp = NULL;
845 char *keyname, *parentpath;
846 char *subkeypath = NULL;
848 REGISTRY_KEY registry_key;
849 WERROR result = WERR_OK;
850 SEC_DESC *sec_desc = NULL;
853 return WERR_GENERAL_FAILURE;
857 return WERR_OBJECT_PATH_INVALID;
860 /* split up the registry key path */
862 key_tmp = talloc_strdup(regfile->mem_ctx, keypath);
866 if (!reg_split_key(key_tmp, &parentpath, &keyname)) {
867 return WERR_OBJECT_PATH_INVALID;
871 keyname = parentpath;
874 /* we need a REGISTRY_KEY object here to enumerate subkeys and values */
876 ZERO_STRUCT(registry_key);
878 registry_key.name = talloc_strdup(regfile->mem_ctx, keypath);
879 if (registry_key.name == NULL) {
883 registry_key.hook = reghook_cache_find(registry_key.name);
884 if (registry_key.hook == NULL) {
888 /* lookup the values and subkeys */
890 subkeys = TALLOC_ZERO_P(regfile->mem_ctx, REGSUBKEY_CTR);
891 if (subkeys == NULL) {
895 values = TALLOC_ZERO_P(subkeys, REGVAL_CTR);
896 if (values == NULL) {
900 fetch_reg_keys(®istry_key, subkeys);
901 fetch_reg_values(®istry_key, values);
903 result = regkey_get_secdesc(regfile->mem_ctx, ®istry_key, &sec_desc);
904 if (!W_ERROR_IS_OK(result)) {
908 /* write out this key */
910 key = regfio_write_key(regfile, keyname, values, subkeys, sec_desc,
913 result = WERR_CAN_NOT_COMPLETE;
917 /* write each one of the subkeys out */
919 num_subkeys = regsubkey_ctr_numkeys(subkeys);
920 for (i=0; i<num_subkeys; i++) {
921 subkeyname = regsubkey_ctr_specific_key(subkeys, i);
922 subkeypath = talloc_asprintf(regfile->mem_ctx, "%s\\%s",
923 keypath, subkeyname);
924 if (subkeypath == NULL) {
928 result = reg_write_tree(regfile, subkeypath, key);
929 if (!W_ERROR_IS_OK(result))
933 DEBUG(6, ("reg_write_tree: wrote key [%s]\n", keypath));
936 TALLOC_FREE(subkeys);
937 TALLOC_FREE(registry_key.name);
942 static WERROR backup_registry_key(REGISTRY_KEY *krecord, const char *fname)
947 /* open the registry file....fail if the file already exists */
949 regfile = regfio_open(fname, (O_RDWR|O_CREAT|O_EXCL),
951 if (regfile == NULL) {
952 DEBUG(0,("backup_registry_key: failed to open \"%s\" (%s)\n",
953 fname, strerror(errno) ));
954 return ntstatus_to_werror(map_nt_error_from_unix(errno));
957 /* write the registry tree to the file */
959 result = reg_write_tree(regfile, krecord->name, NULL);
963 regfio_close(regfile);
968 WERROR reg_savekey(struct registry_key *key, const char *fname)
970 return backup_registry_key(key->key, fname);
973 /**********************************************************************
974 * Higher level utility functions
975 **********************************************************************/
977 WERROR reg_deleteallvalues(struct registry_key *key)
982 if (!(key->key->access_granted & SEC_RIGHTS_SET_VALUE)) {
983 return WERR_ACCESS_DENIED;
986 if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
990 for (i=0; i<key->values->num_values; i++) {
991 regval_ctr_delvalue(key->values, key->values->values[i]->valuename);
994 if (!store_reg_values(key->key, key->values)) {
995 TALLOC_FREE(key->values);
996 return WERR_REG_IO_FAILURE;
1003 * Utility function to open a complete registry path including the hive prefix.
1006 WERROR reg_open_path(TALLOC_CTX *mem_ctx, const char *orig_path,
1007 uint32 desired_access, const struct nt_user_token *token,
1008 struct registry_key **pkey)
1010 struct registry_key *hive, *key;
1014 if (!(path = SMB_STRDUP(orig_path))) {
1018 p = strchr(path, '\\');
1020 if ((p == NULL) || (p[1] == '\0')) {
1022 * No key behind the hive, just return the hive
1025 err = reg_openhive(mem_ctx, path, desired_access, token,
1027 if (!W_ERROR_IS_OK(err)) {
1038 err = reg_openhive(mem_ctx, path, SEC_RIGHTS_ENUM_SUBKEYS, token,
1040 if (!W_ERROR_IS_OK(err)) {
1045 err = reg_openkey(mem_ctx, hive, p+1, desired_access, &key);
1050 if (!W_ERROR_IS_OK(err)) {
1059 * Utility function to delete a registry key with all its subkeys.
1060 * Note that reg_deletekey returns ACCESS_DENIED when called on a
1061 * key that has subkeys.
1063 static WERROR reg_deletekey_recursive_internal(TALLOC_CTX *ctx,
1064 struct registry_key *parent,
1068 TALLOC_CTX *mem_ctx = NULL;
1069 WERROR werr = WERR_OK;
1070 struct registry_key *key;
1071 char *subkey_name = NULL;
1073 mem_ctx = talloc_new(ctx);
1074 if (mem_ctx == NULL) {
1079 /* recurse through subkeys first */
1080 werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
1081 if (!W_ERROR_IS_OK(werr)) {
1085 while (W_ERROR_IS_OK(werr = reg_enumkey(mem_ctx, key, 0,
1086 &subkey_name, NULL)))
1088 werr = reg_deletekey_recursive_internal(mem_ctx, key,
1091 if (!W_ERROR_IS_OK(werr)) {
1095 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
1096 DEBUG(1, ("reg_deletekey_recursive_internal: "
1097 "Error enumerating subkeys: %s\n",
1105 /* now delete the actual key */
1106 werr = reg_deletekey(parent, path);
1110 TALLOC_FREE(mem_ctx);
1114 WERROR reg_deletekey_recursive(TALLOC_CTX *ctx,
1115 struct registry_key *parent,
1118 return reg_deletekey_recursive_internal(ctx, parent, path, true);
1121 WERROR reg_deletesubkeys_recursive(TALLOC_CTX *ctx,
1122 struct registry_key *parent,
1125 return reg_deletekey_recursive_internal(ctx, parent, path, false);
1129 /* these two functions are unused. */
1132 * Utility function to create a registry key without opening the hive
1133 * before. Assumes the hive already exists.
1136 WERROR reg_create_path(TALLOC_CTX *mem_ctx, const char *orig_path,
1137 uint32 desired_access,
1138 const struct nt_user_token *token,
1139 enum winreg_CreateAction *paction,
1140 struct registry_key **pkey)
1142 struct registry_key *hive;
1146 if (!(path = SMB_STRDUP(orig_path))) {
1150 p = strchr(path, '\\');
1152 if ((p == NULL) || (p[1] == '\0')) {
1154 * No key behind the hive, just return the hive
1157 err = reg_openhive(mem_ctx, path, desired_access, token,
1159 if (!W_ERROR_IS_OK(err)) {
1165 *paction = REG_OPENED_EXISTING_KEY;
1171 err = reg_openhive(mem_ctx, path,
1172 (strchr(p+1, '\\') != NULL) ?
1173 SEC_RIGHTS_ENUM_SUBKEYS : SEC_RIGHTS_CREATE_SUBKEY,
1175 if (!W_ERROR_IS_OK(err)) {
1180 err = reg_createkey(mem_ctx, hive, p+1, desired_access, pkey, paction);
1187 * Utility function to create a registry key without opening the hive
1188 * before. Will not delete a hive.
1191 WERROR reg_delete_path(const struct nt_user_token *token,
1192 const char *orig_path)
1194 struct registry_key *hive;
1198 if (!(path = SMB_STRDUP(orig_path))) {
1202 p = strchr(path, '\\');
1204 if ((p == NULL) || (p[1] == '\0')) {
1206 return WERR_INVALID_PARAM;
1211 err = reg_openhive(NULL, path,
1212 (strchr(p+1, '\\') != NULL) ?
1213 SEC_RIGHTS_ENUM_SUBKEYS : SEC_RIGHTS_CREATE_SUBKEY,
1215 if (!W_ERROR_IS_OK(err)) {
1220 err = reg_deletekey(hive, p+1);