s4-torture: smb2 copychunk data integrity checks
[Samba/gbeck.git] / source3 / registry / reg_api_util.c
blob045ad21517a5d5ca0009b8581ed44ab6014bb0c5
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Volker Lendecke 2006
5 * Copyright (C) Michael Adam 2007-2010
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 * Higher level utility functions on top of reg_api.c
25 #include "includes.h"
26 #include "registry.h"
27 #include "reg_api.h"
28 #include "reg_api_util.h"
30 /**
31 * Utility function to open a complete registry path including the hive prefix.
33 WERROR reg_open_path(TALLOC_CTX *mem_ctx, const char *orig_path,
34 uint32 desired_access, const struct security_token *token,
35 struct registry_key **pkey)
37 struct registry_key *hive, *key;
38 char *path, *p;
39 WERROR err;
41 if (!(path = SMB_STRDUP(orig_path))) {
42 return WERR_NOMEM;
45 p = strchr(path, '\\');
47 if ((p == NULL) || (p[1] == '\0')) {
49 * No key behind the hive, just return the hive
52 err = reg_openhive(mem_ctx, path, desired_access, token,
53 &hive);
54 if (!W_ERROR_IS_OK(err)) {
55 SAFE_FREE(path);
56 return err;
58 SAFE_FREE(path);
59 *pkey = hive;
60 return WERR_OK;
63 *p = '\0';
65 err = reg_openhive(mem_ctx, path, KEY_ENUMERATE_SUB_KEYS, token,
66 &hive);
67 if (!W_ERROR_IS_OK(err)) {
68 SAFE_FREE(path);
69 return err;
72 err = reg_openkey(mem_ctx, hive, p+1, desired_access, &key);
74 TALLOC_FREE(hive);
75 SAFE_FREE(path);
77 if (!W_ERROR_IS_OK(err)) {
78 return err;
81 *pkey = key;
82 return WERR_OK;
85 #if 0
86 /* these two functions are unused. */
88 /**
89 * Utility function to create a registry key without opening the hive
90 * before. Assumes the hive already exists.
93 WERROR reg_create_path(TALLOC_CTX *mem_ctx, const char *orig_path,
94 uint32 desired_access,
95 const struct security_token *token,
96 enum winreg_CreateAction *paction,
97 struct registry_key **pkey)
99 struct registry_key *hive;
100 char *path, *p;
101 WERROR err;
103 if (!(path = SMB_STRDUP(orig_path))) {
104 return WERR_NOMEM;
107 p = strchr(path, '\\');
109 if ((p == NULL) || (p[1] == '\0')) {
111 * No key behind the hive, just return the hive
114 err = reg_openhive(mem_ctx, path, desired_access, token,
115 &hive);
116 if (!W_ERROR_IS_OK(err)) {
117 SAFE_FREE(path);
118 return err;
120 SAFE_FREE(path);
121 *pkey = hive;
122 *paction = REG_OPENED_EXISTING_KEY;
123 return WERR_OK;
126 *p = '\0';
128 err = reg_openhive(mem_ctx, path,
129 (strchr(p+1, '\\') != NULL) ?
130 KEY_ENUMERATE_SUB_KEYS : KEY_CREATE_SUB_KEY,
131 token, &hive);
132 if (!W_ERROR_IS_OK(err)) {
133 SAFE_FREE(path);
134 return err;
137 err = reg_createkey(mem_ctx, hive, p+1, desired_access, pkey, paction);
138 SAFE_FREE(path);
139 TALLOC_FREE(hive);
140 return err;
144 * Utility function to create a registry key without opening the hive
145 * before. Will not delete a hive.
148 WERROR reg_delete_path(const struct security_token *token,
149 const char *orig_path)
151 struct registry_key *hive;
152 char *path, *p;
153 WERROR err;
155 if (!(path = SMB_STRDUP(orig_path))) {
156 return WERR_NOMEM;
159 p = strchr(path, '\\');
161 if ((p == NULL) || (p[1] == '\0')) {
162 SAFE_FREE(path);
163 return WERR_INVALID_PARAM;
166 *p = '\0';
168 err = reg_openhive(NULL, path,
169 (strchr(p+1, '\\') != NULL) ?
170 KEY_ENUMERATE_SUB_KEYS : KEY_CREATE_SUB_KEY,
171 token, &hive);
172 if (!W_ERROR_IS_OK(err)) {
173 SAFE_FREE(path);
174 return err;
177 err = reg_deletekey(hive, p+1);
178 SAFE_FREE(path);
179 TALLOC_FREE(hive);
180 return err;
182 #endif /* #if 0 */