s3:smbd: only set user_info->auth_description on success
[Samba.git] / source3 / rpc_client / cli_winreg_int.c
blob3ac8380bf7c22ac42a2fb75ad0893bec8960250f
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 "librpc/gen_ndr/ndr_winreg_c.h"
25 #include "rpc_client/cli_winreg_int.h"
26 #include "rpc_server/rpc_ncacn_np.h"
27 #include "../lib/tsocket/tsocket.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_PARAMETER;
49 if (strlen(path) == 0) {
50 return WERR_INVALID_PARAMETER;
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_NOT_ENOUGH_MEMORY;
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_NOT_ENOUGH_MEMORY;
84 return WERR_OK;
87 static NTSTATUS _winreg_int_openkey(TALLOC_CTX *mem_ctx,
88 const struct auth_session_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 struct tsocket_address *local;
100 struct dcerpc_binding_handle *binding_handle;
101 struct winreg_String wkey, wkeyclass;
102 NTSTATUS status;
103 WERROR result = WERR_OK;
104 int rc;
106 rc = tsocket_address_inet_from_strings(mem_ctx,
107 "ip",
108 "127.0.0.1",
110 &local);
111 if (rc < 0) {
112 return NT_STATUS_NO_MEMORY;
115 status = rpcint_binding_handle(mem_ctx,
116 &ndr_table_winreg,
117 local,
118 NULL,
119 session_info,
120 msg_ctx,
121 &binding_handle);
122 if (!NT_STATUS_IS_OK(status)) {
123 DEBUG(0, ("dcerpc_winreg_int_openkey: Could not connect to winreg pipe: %s\n",
124 nt_errstr(status)));
125 return status;
128 switch (reg_type) {
129 case HKEY_LOCAL_MACHINE:
130 status = dcerpc_winreg_OpenHKLM(binding_handle,
131 mem_ctx,
132 NULL,
133 access_mask,
134 hive_handle,
135 &result);
136 break;
137 case HKEY_CLASSES_ROOT:
138 status = dcerpc_winreg_OpenHKCR(binding_handle,
139 mem_ctx,
140 NULL,
141 access_mask,
142 hive_handle,
143 &result);
144 break;
145 case HKEY_USERS:
146 status = dcerpc_winreg_OpenHKU(binding_handle,
147 mem_ctx,
148 NULL,
149 access_mask,
150 hive_handle,
151 &result);
152 break;
153 case HKEY_CURRENT_USER:
154 status = dcerpc_winreg_OpenHKCU(binding_handle,
155 mem_ctx,
156 NULL,
157 access_mask,
158 hive_handle,
159 &result);
160 break;
161 case HKEY_PERFORMANCE_DATA:
162 status = dcerpc_winreg_OpenHKPD(binding_handle,
163 mem_ctx,
164 NULL,
165 access_mask,
166 hive_handle,
167 &result);
168 break;
169 default:
170 result = WERR_INVALID_PARAMETER;
171 status = NT_STATUS_OK;
173 if (!NT_STATUS_IS_OK(status)) {
174 talloc_free(binding_handle);
175 return status;
177 if (!W_ERROR_IS_OK(result)) {
178 talloc_free(binding_handle);
179 *pwerr = result;
180 return status;
183 ZERO_STRUCT(wkey);
184 wkey.name = key;
186 if (create_key) {
187 enum winreg_CreateAction action = REG_ACTION_NONE;
189 ZERO_STRUCT(wkeyclass);
190 wkeyclass.name = "";
192 status = dcerpc_winreg_CreateKey(binding_handle,
193 mem_ctx,
194 hive_handle,
195 wkey,
196 wkeyclass,
198 access_mask,
199 NULL,
200 key_handle,
201 &action,
202 &result);
203 switch (action) {
204 case REG_ACTION_NONE:
205 DEBUG(8, ("dcerpc_winreg_int_openkey: createkey"
206 " did nothing -- huh?\n"));
207 break;
208 case REG_CREATED_NEW_KEY:
209 DEBUG(8, ("dcerpc_winreg_int_openkey: createkey"
210 " created %s\n", key));
211 break;
212 case REG_OPENED_EXISTING_KEY:
213 DEBUG(8, ("dcerpc_winreg_int_openkey: createkey"
214 " opened existing %s\n", key));
215 break;
217 } else {
218 status = dcerpc_winreg_OpenKey(binding_handle,
219 mem_ctx,
220 hive_handle,
221 wkey,
223 access_mask,
224 key_handle,
225 &result);
227 if (!NT_STATUS_IS_OK(status)) {
228 talloc_free(binding_handle);
229 return status;
231 if (!W_ERROR_IS_OK(result)) {
232 talloc_free(binding_handle);
233 *pwerr = result;
234 return status;
237 *h = binding_handle;
239 return status;
242 NTSTATUS dcerpc_winreg_int_openkey(TALLOC_CTX *mem_ctx,
243 const struct auth_session_info *server_info,
244 struct messaging_context *msg_ctx,
245 struct dcerpc_binding_handle **h,
246 const char *key,
247 bool create_key,
248 uint32_t access_mask,
249 struct policy_handle *hive_handle,
250 struct policy_handle *key_handle,
251 WERROR *pwerr)
253 char *hivename = NULL;
254 char *subkey = NULL;
255 uint32_t reg_type;
256 WERROR result;
258 result = _split_hive_key(mem_ctx, key, &hivename, &subkey);
259 if (!W_ERROR_IS_OK(result)) {
260 *pwerr = result;
261 return NT_STATUS_OK;
264 if (strequal(hivename, "HKLM") ||
265 strequal(hivename, "HKEY_LOCAL_MACHINE")) {
266 reg_type = HKEY_LOCAL_MACHINE;
267 } else if (strequal(hivename, "HKCR") ||
268 strequal(hivename, "HKEY_CLASSES_ROOT")) {
269 reg_type = HKEY_CLASSES_ROOT;
270 } else if (strequal(hivename, "HKU") ||
271 strequal(hivename, "HKEY_USERS")) {
272 reg_type = HKEY_USERS;
273 } else if (strequal(hivename, "HKCU") ||
274 strequal(hivename, "HKEY_CURRENT_USER")) {
275 reg_type = HKEY_CURRENT_USER;
276 } else if (strequal(hivename, "HKPD") ||
277 strequal(hivename, "HKEY_PERFORMANCE_DATA")) {
278 reg_type = HKEY_PERFORMANCE_DATA;
279 } else {
280 DEBUG(10,("dcerpc_winreg_int_openkey: unrecognised hive key %s\n",
281 key));
282 *pwerr = WERR_INVALID_PARAMETER;
283 return NT_STATUS_OK;
286 return _winreg_int_openkey(mem_ctx,
287 server_info,
288 msg_ctx,
290 reg_type,
291 key,
292 create_key,
293 access_mask,
294 hive_handle,
295 key_handle,
296 pwerr);
299 NTSTATUS dcerpc_winreg_int_hklm_openkey(TALLOC_CTX *mem_ctx,
300 const struct auth_session_info *server_info,
301 struct messaging_context *msg_ctx,
302 struct dcerpc_binding_handle **h,
303 const char *key,
304 bool create_key,
305 uint32_t access_mask,
306 struct policy_handle *hive_handle,
307 struct policy_handle *key_handle,
308 WERROR *pwerr)
310 return _winreg_int_openkey(mem_ctx,
311 server_info,
312 msg_ctx,
314 HKEY_LOCAL_MACHINE,
315 key,
316 create_key,
317 access_mask,
318 hive_handle,
319 key_handle,
320 pwerr);
323 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */