WHATSNEW: Start release notes for 3.6.0pre3.
[Samba.git] / source3 / rpc_client / cli_winreg_int.c
blob8e11ae832d683905106ebbce252d055ef42cfad4
1 /*
2 * Unix SMB/CIFS implementation.
4 * WINREG internal client routines
6 * Copyright (c) 2011 Andreas Schneider <asn@samba.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "include/registry.h"
24 #include "utils/net_registry_util.h"
25 #include "librpc/gen_ndr/ndr_winreg_c.h"
26 #include "rpc_client/cli_winreg_int.h"
27 #include "rpc_server/rpc_ncacn_np.h"
29 /**
30 * Split path into hive name and subkeyname
31 * normalizations performed:
32 * - if the path contains no '\\' characters,
33 * assume that the legacy format of using '/'
34 * as a separator is used and convert '/' to '\\'
35 * - strip trailing '\\' chars
37 static WERROR _split_hive_key(TALLOC_CTX *mem_ctx,
38 const char *path,
39 char **hivename,
40 char **subkeyname)
42 char *p;
43 const char *tmp_subkeyname;
45 if ((path == NULL) || (hivename == NULL) || (subkeyname == NULL)) {
46 return WERR_INVALID_PARAM;
49 if (strlen(path) == 0) {
50 return WERR_INVALID_PARAM;
53 if (strchr(path, '\\') == NULL) {
54 *hivename = talloc_string_sub(mem_ctx, path, "/", "\\");
55 } else {
56 *hivename = talloc_strdup(mem_ctx, path);
59 if (*hivename == NULL) {
60 return WERR_NOMEM;
63 /* strip trailing '\\' chars */
64 p = strrchr(*hivename, '\\');
65 while ((p != NULL) && (p[1] == '\0')) {
66 *p = '\0';
67 p = strrchr(*hivename, '\\');
70 p = strchr(*hivename, '\\');
72 if ((p == NULL) || (*p == '\0')) {
73 /* just the hive - no subkey given */
74 tmp_subkeyname = "";
75 } else {
76 *p = '\0';
77 tmp_subkeyname = p+1;
79 *subkeyname = talloc_strdup(mem_ctx, tmp_subkeyname);
80 if (*subkeyname == NULL) {
81 return WERR_NOMEM;
84 return WERR_OK;
87 static NTSTATUS _winreg_int_openkey(TALLOC_CTX *mem_ctx,
88 const struct auth_serversupplied_info *session_info,
89 struct messaging_context *msg_ctx,
90 struct dcerpc_binding_handle **h,
91 uint32_t reg_type,
92 const char *key,
93 bool create_key,
94 uint32_t access_mask,
95 struct policy_handle *hive_handle,
96 struct policy_handle *key_handle,
97 WERROR *pwerr)
99 static struct client_address client_id;
100 struct dcerpc_binding_handle *binding_handle;
101 struct winreg_String wkey, wkeyclass;
102 NTSTATUS status;
103 WERROR result = WERR_OK;
105 strlcpy(client_id.addr, "127.0.0.1", sizeof(client_id.addr));
106 client_id.name = "127.0.0.1";
108 status = rpcint_binding_handle(mem_ctx,
109 &ndr_table_winreg,
110 &client_id,
111 session_info,
112 msg_ctx,
113 &binding_handle);
114 if (!NT_STATUS_IS_OK(status)) {
115 DEBUG(0, ("dcerpc_winreg_int_openkey: Could not connect to winreg pipe: %s\n",
116 nt_errstr(status)));
117 return status;
120 switch (reg_type) {
121 case HKEY_LOCAL_MACHINE:
122 status = dcerpc_winreg_OpenHKLM(binding_handle,
123 mem_ctx,
124 NULL,
125 access_mask,
126 hive_handle,
127 &result);
128 break;
129 case HKEY_CLASSES_ROOT:
130 status = dcerpc_winreg_OpenHKCR(binding_handle,
131 mem_ctx,
132 NULL,
133 access_mask,
134 hive_handle,
135 &result);
136 break;
137 case HKEY_USERS:
138 status = dcerpc_winreg_OpenHKU(binding_handle,
139 mem_ctx,
140 NULL,
141 access_mask,
142 hive_handle,
143 &result);
144 break;
145 case HKEY_CURRENT_USER:
146 status = dcerpc_winreg_OpenHKCU(binding_handle,
147 mem_ctx,
148 NULL,
149 access_mask,
150 hive_handle,
151 &result);
152 break;
153 case HKEY_PERFORMANCE_DATA:
154 status = dcerpc_winreg_OpenHKPD(binding_handle,
155 mem_ctx,
156 NULL,
157 access_mask,
158 hive_handle,
159 &result);
160 break;
161 default:
162 result = WERR_INVALID_PARAMETER;
163 status = NT_STATUS_OK;
165 if (!NT_STATUS_IS_OK(status)) {
166 talloc_free(binding_handle);
167 return status;
169 if (!W_ERROR_IS_OK(result)) {
170 talloc_free(binding_handle);
171 *pwerr = result;
172 return status;
175 ZERO_STRUCT(wkey);
176 wkey.name = key;
178 if (create_key) {
179 enum winreg_CreateAction action = REG_ACTION_NONE;
181 ZERO_STRUCT(wkeyclass);
182 wkeyclass.name = "";
184 status = dcerpc_winreg_CreateKey(binding_handle,
185 mem_ctx,
186 hive_handle,
187 wkey,
188 wkeyclass,
190 access_mask,
191 NULL,
192 key_handle,
193 &action,
194 &result);
195 switch (action) {
196 case REG_ACTION_NONE:
197 DEBUG(8, ("dcerpc_winreg_int_openkey: createkey"
198 " did nothing -- huh?\n"));
199 break;
200 case REG_CREATED_NEW_KEY:
201 DEBUG(8, ("dcerpc_winreg_int_openkey: createkey"
202 " created %s\n", key));
203 break;
204 case REG_OPENED_EXISTING_KEY:
205 DEBUG(8, ("dcerpc_winreg_int_openkey: createkey"
206 " opened existing %s\n", key));
207 break;
209 } else {
210 status = dcerpc_winreg_OpenKey(binding_handle,
211 mem_ctx,
212 hive_handle,
213 wkey,
215 access_mask,
216 key_handle,
217 &result);
219 if (!NT_STATUS_IS_OK(status)) {
220 talloc_free(binding_handle);
221 return status;
223 if (!W_ERROR_IS_OK(result)) {
224 talloc_free(binding_handle);
225 *pwerr = result;
226 return status;
229 *h = binding_handle;
231 return status;
234 NTSTATUS dcerpc_winreg_int_openkey(TALLOC_CTX *mem_ctx,
235 const struct auth_serversupplied_info *server_info,
236 struct messaging_context *msg_ctx,
237 struct dcerpc_binding_handle **h,
238 const char *key,
239 bool create_key,
240 uint32_t access_mask,
241 struct policy_handle *hive_handle,
242 struct policy_handle *key_handle,
243 WERROR *pwerr)
245 char *hivename = NULL;
246 char *subkey = NULL;
247 uint32_t reg_type;
248 WERROR result;
250 result = _split_hive_key(mem_ctx, key, &hivename, &subkey);
251 if (!W_ERROR_IS_OK(result)) {
252 *pwerr = result;
253 return NT_STATUS_OK;
256 if (strequal(hivename, "HKLM") ||
257 strequal(hivename, "HKEY_LOCAL_MACHINE")) {
258 reg_type = HKEY_LOCAL_MACHINE;
259 } else if (strequal(hivename, "HKCR") ||
260 strequal(hivename, "HKEY_CLASSES_ROOT")) {
261 reg_type = HKEY_CLASSES_ROOT;
262 } else if (strequal(hivename, "HKU") ||
263 strequal(hivename, "HKEY_USERS")) {
264 reg_type = HKEY_USERS;
265 } else if (strequal(hivename, "HKCU") ||
266 strequal(hivename, "HKEY_CURRENT_USER")) {
267 reg_type = HKEY_CURRENT_USER;
268 } else if (strequal(hivename, "HKPD") ||
269 strequal(hivename, "HKEY_PERFORMANCE_DATA")) {
270 reg_type = HKEY_PERFORMANCE_DATA;
271 } else {
272 DEBUG(10,("dcerpc_winreg_int_openkey: unrecognised hive key %s\n",
273 key));
274 *pwerr = WERR_INVALID_PARAMETER;
275 return NT_STATUS_OK;
278 return _winreg_int_openkey(mem_ctx,
279 server_info,
280 msg_ctx,
282 reg_type,
283 key,
284 create_key,
285 access_mask,
286 hive_handle,
287 key_handle,
288 pwerr);
291 NTSTATUS dcerpc_winreg_int_hklm_openkey(TALLOC_CTX *mem_ctx,
292 const struct auth_serversupplied_info *server_info,
293 struct messaging_context *msg_ctx,
294 struct dcerpc_binding_handle **h,
295 const char *key,
296 bool create_key,
297 uint32_t access_mask,
298 struct policy_handle *hive_handle,
299 struct policy_handle *key_handle,
300 WERROR *pwerr)
302 return _winreg_int_openkey(mem_ctx,
303 server_info,
304 msg_ctx,
306 HKEY_LOCAL_MACHINE,
307 key,
308 create_key,
309 access_mask,
310 hive_handle,
311 key_handle,
312 pwerr);
315 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */