Merge commit 'release-4-0-0alpha15' into master4-tmp
[nivanova/samba-autobuild/.git] / source3 / rpc_client / cli_winreg_int.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *
4  *  WINREG internal client routines
5  *
6  *  Copyright (c) 2011      Andreas Schneider <asn@samba.org>
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
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
28 /**
29  * Split path into hive name and subkeyname
30  * normalizations performed:
31  *  - if the path contains no '\\' characters,
32  *    assume that the legacy format of using '/'
33  *    as a separator is used and  convert '/' to '\\'
34  *  - strip trailing '\\' chars
35  */
36 static WERROR _split_hive_key(TALLOC_CTX *mem_ctx,
37                               const char *path,
38                               char **hivename,
39                               char **subkeyname)
40 {
41         char *p;
42         const char *tmp_subkeyname;
43
44         if ((path == NULL) || (hivename == NULL) || (subkeyname == NULL)) {
45                 return WERR_INVALID_PARAM;
46         }
47
48         if (strlen(path) == 0) {
49                 return WERR_INVALID_PARAM;
50         }
51
52         if (strchr(path, '\\') == NULL) {
53                 *hivename = talloc_string_sub(mem_ctx, path, "/", "\\");
54         } else {
55                 *hivename = talloc_strdup(mem_ctx, path);
56         }
57
58         if (*hivename == NULL) {
59                 return WERR_NOMEM;
60         }
61
62         /* strip trailing '\\' chars */
63         p = strrchr(*hivename, '\\');
64         while ((p != NULL) && (p[1] == '\0')) {
65                 *p = '\0';
66                 p = strrchr(*hivename, '\\');
67         }
68
69         p = strchr(*hivename, '\\');
70
71         if ((p == NULL) || (*p == '\0')) {
72                 /* just the hive - no subkey given */
73                 tmp_subkeyname = "";
74         } else {
75                 *p = '\0';
76                 tmp_subkeyname = p+1;
77         }
78         *subkeyname = talloc_strdup(mem_ctx, tmp_subkeyname);
79         if (*subkeyname == NULL) {
80                 return WERR_NOMEM;
81         }
82
83         return WERR_OK;
84 }
85
86 static NTSTATUS _winreg_int_openkey(TALLOC_CTX *mem_ctx,
87                                     const struct auth_serversupplied_info *session_info,
88                                     struct messaging_context *msg_ctx,
89                                     struct dcerpc_binding_handle **h,
90                                     uint32_t reg_type,
91                                     const char *key,
92                                     bool create_key,
93                                     uint32_t access_mask,
94                                     struct policy_handle *hive_handle,
95                                     struct policy_handle *key_handle,
96                                     WERROR *pwerr)
97 {
98         static struct client_address client_id;
99         struct dcerpc_binding_handle *binding_handle;
100         struct winreg_String wkey, wkeyclass;
101         NTSTATUS status;
102         WERROR result = WERR_OK;
103
104         strlcpy(client_id.addr, "127.0.0.1", sizeof(client_id.addr));
105         client_id.name = "127.0.0.1";
106
107         status = rpcint_binding_handle(mem_ctx,
108                                        &ndr_table_winreg,
109                                        &client_id,
110                                        session_info,
111                                        msg_ctx,
112                                        &binding_handle);
113         if (!NT_STATUS_IS_OK(status)) {
114                 DEBUG(0, ("dcerpc_winreg_int_openkey: Could not connect to winreg pipe: %s\n",
115                           nt_errstr(status)));
116                 return status;
117         }
118
119         switch (reg_type) {
120         case HKEY_LOCAL_MACHINE:
121                 status = dcerpc_winreg_OpenHKLM(binding_handle,
122                                                 mem_ctx,
123                                                 NULL,
124                                                 access_mask,
125                                                 hive_handle,
126                                                 &result);
127                 break;
128         case HKEY_CLASSES_ROOT:
129                 status = dcerpc_winreg_OpenHKCR(binding_handle,
130                                                 mem_ctx,
131                                                 NULL,
132                                                 access_mask,
133                                                 hive_handle,
134                                                 &result);
135                 break;
136         case HKEY_USERS:
137                 status = dcerpc_winreg_OpenHKU(binding_handle,
138                                                mem_ctx,
139                                                NULL,
140                                                access_mask,
141                                                hive_handle,
142                                                &result);
143                 break;
144         case HKEY_CURRENT_USER:
145                 status = dcerpc_winreg_OpenHKCU(binding_handle,
146                                                 mem_ctx,
147                                                 NULL,
148                                                 access_mask,
149                                                 hive_handle,
150                                                 &result);
151                 break;
152         case HKEY_PERFORMANCE_DATA:
153                 status = dcerpc_winreg_OpenHKPD(binding_handle,
154                                                 mem_ctx,
155                                                 NULL,
156                                                 access_mask,
157                                                 hive_handle,
158                                                 &result);
159                 break;
160         default:
161                 result = WERR_INVALID_PARAMETER;
162                 status = NT_STATUS_OK;
163         }
164         if (!NT_STATUS_IS_OK(status)) {
165                 talloc_free(binding_handle);
166                 return status;
167         }
168         if (!W_ERROR_IS_OK(result)) {
169                 talloc_free(binding_handle);
170                 *pwerr = result;
171                 return status;
172         }
173
174         ZERO_STRUCT(wkey);
175         wkey.name = key;
176
177         if (create_key) {
178                 enum winreg_CreateAction action = REG_ACTION_NONE;
179
180                 ZERO_STRUCT(wkeyclass);
181                 wkeyclass.name = "";
182
183                 status = dcerpc_winreg_CreateKey(binding_handle,
184                                                  mem_ctx,
185                                                  hive_handle,
186                                                  wkey,
187                                                  wkeyclass,
188                                                  0,
189                                                  access_mask,
190                                                  NULL,
191                                                  key_handle,
192                                                  &action,
193                                                  &result);
194                 switch (action) {
195                         case REG_ACTION_NONE:
196                                 DEBUG(8, ("dcerpc_winreg_int_openkey: createkey"
197                                           " did nothing -- huh?\n"));
198                                 break;
199                         case REG_CREATED_NEW_KEY:
200                                 DEBUG(8, ("dcerpc_winreg_int_openkey: createkey"
201                                           " created %s\n", key));
202                                 break;
203                         case REG_OPENED_EXISTING_KEY:
204                                 DEBUG(8, ("dcerpc_winreg_int_openkey: createkey"
205                                           " opened existing %s\n", key));
206                                 break;
207                 }
208         } else {
209                 status = dcerpc_winreg_OpenKey(binding_handle,
210                                                mem_ctx,
211                                                hive_handle,
212                                                wkey,
213                                                0,
214                                                access_mask,
215                                                key_handle,
216                                                &result);
217         }
218         if (!NT_STATUS_IS_OK(status)) {
219                 talloc_free(binding_handle);
220                 return status;
221         }
222         if (!W_ERROR_IS_OK(result)) {
223                 talloc_free(binding_handle);
224                 *pwerr = result;
225                 return status;
226         }
227
228         *h = binding_handle;
229
230         return status;
231 }
232
233 NTSTATUS dcerpc_winreg_int_openkey(TALLOC_CTX *mem_ctx,
234                                    const struct auth_serversupplied_info *server_info,
235                                    struct messaging_context *msg_ctx,
236                                    struct dcerpc_binding_handle **h,
237                                    const char *key,
238                                    bool create_key,
239                                    uint32_t access_mask,
240                                    struct policy_handle *hive_handle,
241                                    struct policy_handle *key_handle,
242                                    WERROR *pwerr)
243 {
244         char *hivename = NULL;
245         char *subkey = NULL;
246         uint32_t reg_type;
247         WERROR result;
248
249         result = _split_hive_key(mem_ctx, key, &hivename, &subkey);
250         if (!W_ERROR_IS_OK(result)) {
251                 *pwerr = result;
252                 return NT_STATUS_OK;
253         }
254
255         if (strequal(hivename, "HKLM") ||
256             strequal(hivename, "HKEY_LOCAL_MACHINE")) {
257                 reg_type = HKEY_LOCAL_MACHINE;
258         } else if (strequal(hivename, "HKCR") ||
259                    strequal(hivename, "HKEY_CLASSES_ROOT")) {
260                 reg_type = HKEY_CLASSES_ROOT;
261         } else if (strequal(hivename, "HKU") ||
262                    strequal(hivename, "HKEY_USERS")) {
263                 reg_type = HKEY_USERS;
264         } else if (strequal(hivename, "HKCU") ||
265                    strequal(hivename, "HKEY_CURRENT_USER")) {
266                 reg_type = HKEY_CURRENT_USER;
267         } else if (strequal(hivename, "HKPD") ||
268                    strequal(hivename, "HKEY_PERFORMANCE_DATA")) {
269                 reg_type = HKEY_PERFORMANCE_DATA;
270         } else {
271                 DEBUG(10,("dcerpc_winreg_int_openkey: unrecognised hive key %s\n",
272                           key));
273                 *pwerr = WERR_INVALID_PARAMETER;
274                 return NT_STATUS_OK;
275         }
276
277         return _winreg_int_openkey(mem_ctx,
278                                    server_info,
279                                    msg_ctx,
280                                    h,
281                                    reg_type,
282                                    key,
283                                    create_key,
284                                    access_mask,
285                                    hive_handle,
286                                    key_handle,
287                                    pwerr);
288 }
289
290 NTSTATUS dcerpc_winreg_int_hklm_openkey(TALLOC_CTX *mem_ctx,
291                                         const struct auth_serversupplied_info *server_info,
292                                         struct messaging_context *msg_ctx,
293                                         struct dcerpc_binding_handle **h,
294                                         const char *key,
295                                         bool create_key,
296                                         uint32_t access_mask,
297                                         struct policy_handle *hive_handle,
298                                         struct policy_handle *key_handle,
299                                         WERROR *pwerr)
300 {
301         return _winreg_int_openkey(mem_ctx,
302                                    server_info,
303                                    msg_ctx,
304                                    h,
305                                    HKEY_LOCAL_MACHINE,
306                                    key,
307                                    create_key,
308                                    access_mask,
309                                    hive_handle,
310                                    key_handle,
311                                    pwerr);
312 }
313
314 /* vim: set ts=8 sw=8 noet cindent syntax=c.doxygen: */