2 Samba Unix/Linux SMB implementation
3 RPC backend for the registry library
4 Copyright (C) 2003-2007 Jelmer Vernooij, jelmer@samba.org
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "librpc/gen_ndr/ndr_winreg_c.h"
24 struct registry_key key;
25 struct policy_handle pol;
26 struct dcerpc_pipe *pipe;
30 uint32_t max_valnamelen;
31 uint32_t max_valdatalen;
34 struct rpc_registry_context {
35 struct registry_context context;
36 struct dcerpc_pipe *pipe;
39 static struct registry_operations reg_backend_rpc;
42 * This is the RPC backend for the registry library.
45 static void init_winreg_String(struct winreg_String *name, const char *s)
51 #define openhive(u) static WERROR open_ ## u(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *hnd) \
53 struct winreg_Open ## u r; \
56 r.in.system_name = NULL; \
57 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED; \
60 status = dcerpc_winreg_Open ## u(p, mem_ctx, &r); \
61 if (NT_STATUS_IS_ERR(status)) {\
62 DEBUG(0,("Error executing open\n"));\
63 return ntstatus_to_werror(status);\
79 WERROR (*open) (struct dcerpc_pipe *p, TALLOC_CTX *, struct policy_handle *h);
81 { HKEY_LOCAL_MACHINE, open_HKLM },
82 { HKEY_CURRENT_USER, open_HKCU },
83 { HKEY_CLASSES_ROOT, open_HKCR },
84 { HKEY_PERFORMANCE_DATA, open_HKPD },
85 { HKEY_USERS, open_HKU },
86 { HKEY_DYN_DATA, open_HKDD },
87 { HKEY_CURRENT_CONFIG, open_HKCC },
91 static WERROR rpc_query_key(const struct registry_key *k);
93 static WERROR rpc_get_predefined_key(const struct registry_context *ctx,
95 struct registry_key **k)
98 struct rpc_registry_context *rctx = talloc_get_type(ctx,
99 struct rpc_registry_context);
100 struct rpc_key *mykeydata;
102 for(n = 0; known_hives[n].hkey; n++) {
103 if(known_hives[n].hkey == hkey_type)
107 if (known_hives[n].open == NULL) {
108 DEBUG(1, ("No such hive %d\n", hkey_type));
109 return WERR_NO_MORE_ITEMS;
112 mykeydata = talloc(ctx, struct rpc_key);
113 mykeydata->pipe = rctx->pipe;
114 mykeydata->num_values = -1;
115 mykeydata->num_subkeys = -1;
116 return known_hives[n].open(mykeydata->pipe, *k, &(mykeydata->pol));
120 static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k)
122 struct winreg_OpenKey r;
123 struct rpc_key_data *mykeydata;
125 k->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data);
126 mykeydata->num_values = -1;
127 mykeydata->num_subkeys = -1;
129 /* Then, open the handle using the hive */
131 memset(&r, 0, sizeof(struct winreg_OpenKey));
132 r.in.handle = &(((struct rpc_key_data *)k->hive->root->backend_data)->pol);
133 init_winreg_String(&r.in.keyname, k->path);
134 r.in.unknown = 0x00000000;
135 r.in.access_mask = 0x02000000;
136 r.out.handle = &mykeydata->pol;
138 dcerpc_winreg_OpenKey((struct dcerpc_pipe *)k->hive->backend_data, mem_ctx, &r);
144 static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h,
145 const char *name, struct registry_key **key)
147 struct rpc_key *mykeydata = talloc_get_type(h, struct rpc_key),
149 struct winreg_OpenKey r;
151 mykeydata = talloc(mem_ctx, struct rpc_key);
153 /* Then, open the handle using the hive */
154 memset(&r, 0, sizeof(struct winreg_OpenKey));
155 r.in.parent_handle = &mykeydata->pol;
156 init_winreg_String(&r.in.keyname, name);
157 r.in.unknown = 0x00000000;
158 r.in.access_mask = 0x02000000;
159 r.out.handle = &newkeydata->pol;
161 dcerpc_winreg_OpenKey(mykeydata->pipe, mem_ctx, &r);
166 static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx,
167 const struct registry_key *parent,
169 const char **value_name,
173 struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key);
175 struct winreg_EnumValue r;
176 uint32_t len1, zero = 0;
178 struct winreg_StringBuf name;
181 if (mykeydata->num_values == -1) {
182 error = rpc_query_key(parent);
183 if(!W_ERROR_IS_OK(error)) return error;
186 len1 = mykeydata->max_valdatalen;
189 name.size = mykeydata->max_valnamelen * 2;
192 r.in.handle = &mykeydata->pol;
201 status = dcerpc_winreg_EnumValue(mykeydata->pipe, mem_ctx, &r);
202 if(NT_STATUS_IS_ERR(status)) {
203 DEBUG(0, ("Error in EnumValue: %s\n", nt_errstr(status)));
204 return WERR_GENERAL_FAILURE;
207 if(NT_STATUS_IS_OK(status) &&
208 W_ERROR_IS_OK(r.out.result) && r.out.length) {
209 *value_name = talloc_strdup(mem_ctx, r.out.name->name);
210 *data = data_blob_talloc(mem_ctx, r.out.value, *r.out.length);
217 static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx,
218 const struct registry_key *parent,
221 const char **keyclass,
222 NTTIME *last_changed_time)
224 struct winreg_EnumKey r;
225 struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key);
227 struct winreg_StringBuf namebuf, classbuf;
228 NTTIME change_time = 0;
235 classbuf.name = NULL;
237 r.in.handle = &mykeydata->pol;
239 r.in.name = &namebuf;
240 r.in.keyclass = &classbuf;
241 r.in.last_changed_time = &change_time;
242 r.out.name = &namebuf;
244 status = dcerpc_winreg_EnumKey(mykeydata->pipe, mem_ctx, &r);
245 if(NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
246 *name = talloc_strdup(mem_ctx, r.out.name->name);
247 *keyclass = talloc_strdup(mem_ctx, r.out.keyclass->name);
248 *last_changed_time = *r.out.last_changed_time;
254 static WERROR rpc_add_key(TALLOC_CTX *mem_ctx,
255 struct registry_key *parent, const char *name,
256 const char *key_class,
257 struct security_descriptor *sec,
258 struct registry_key **key)
261 struct winreg_CreateKey r;
262 struct rpc_key *parentkd = talloc_get_type(parent, struct rpc_key);
263 struct rpc_key *rpck = talloc(mem_ctx, struct rpc_key);
265 init_winreg_String(&r.in.name, name);
266 init_winreg_String(&r.in.keyclass, NULL);
268 r.in.handle = &parentkd->pol;
269 r.out.new_handle = &rpck->pol;
271 r.in.access_mask = SEC_STD_ALL;
274 status = dcerpc_winreg_CreateKey(parentkd->pipe, mem_ctx, &r);
276 if (!NT_STATUS_IS_OK(status)) {
278 DEBUG(1, ("CreateKey failed - %s\n", nt_errstr(status)));
279 return ntstatus_to_werror(status);
282 if (W_ERROR_IS_OK(r.out.result)) {
283 rpck->pipe = talloc_reference(rpck, parentkd->pipe);
284 *key = (struct registry_key *)rpck;
290 static WERROR rpc_query_key(const struct registry_key *k)
293 struct winreg_QueryInfoKey r;
294 struct rpc_key *mykeydata = talloc_get_type(k, struct rpc_key);
295 TALLOC_CTX *mem_ctx = talloc_init("query_key");
297 r.in.classname = talloc(mem_ctx, struct winreg_String);
298 init_winreg_String(r.in.classname, NULL);
299 r.in.handle = &mykeydata->pol;
301 status = dcerpc_winreg_QueryInfoKey(mykeydata->pipe, mem_ctx, &r);
302 talloc_free(mem_ctx);
304 if (!NT_STATUS_IS_OK(status)) {
305 DEBUG(1, ("QueryInfoKey failed - %s\n", nt_errstr(status)));
306 return ntstatus_to_werror(status);
309 if (W_ERROR_IS_OK(r.out.result)) {
310 mykeydata->num_subkeys = *r.out.num_subkeys;
311 mykeydata->num_values = *r.out.num_values;
312 mykeydata->max_valnamelen = *r.out.max_valnamelen;
313 mykeydata->max_valdatalen = *r.out.max_valbufsize;
319 static WERROR rpc_del_key(struct registry_key *parent, const char *name)
322 struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key);
323 struct winreg_DeleteKey r;
324 TALLOC_CTX *mem_ctx = talloc_init("del_key");
326 r.in.handle = &mykeydata->pol;
327 init_winreg_String(&r.in.key, name);
329 status = dcerpc_winreg_DeleteKey(mykeydata->pipe, mem_ctx, &r);
331 talloc_free(mem_ctx);
336 static WERROR rpc_get_info(TALLOC_CTX *mem_ctx, const struct registry_key *key,
337 const char **classname,
338 uint32_t *numsubkeys,
340 NTTIME *last_changed_time)
342 struct rpc_key *mykeydata = talloc_get_type(key, struct rpc_key);
345 if(mykeydata->num_values == -1) {
346 error = rpc_query_key(key);
347 if(!W_ERROR_IS_OK(error)) return error;
350 /* FIXME: *classname = talloc_strdup(mem_ctx, mykeydata->classname); */
351 /* FIXME: *last_changed_time = mykeydata->last_changed_time */
353 if (numvalue != NULL)
354 *numvalue = mykeydata->num_values;
356 if (numsubkeys != NULL)
357 *numsubkeys = mykeydata->num_subkeys;
362 static struct registry_operations reg_backend_rpc = {
364 .open_key = rpc_open_key,
365 .enum_key = rpc_get_subkey_by_index,
366 .enum_value = rpc_get_value_by_index,
367 .create_key = rpc_add_key,
368 .delete_key = rpc_del_key,
369 .get_key_info = rpc_get_info,
370 .get_predefined_key = rpc_get_predefined_key,
373 _PUBLIC_ WERROR reg_open_remote(struct registry_context **ctx,
374 struct auth_session_info *session_info,
375 struct cli_credentials *credentials,
376 const char *location, struct event_context *ev)
379 struct dcerpc_pipe *p;
380 struct rpc_registry_context *rctx;
384 rctx = talloc(NULL, struct rpc_registry_context);
386 /* Default to local smbd if no connection is specified */
388 location = talloc_strdup(ctx, "ncalrpc:");
391 status = dcerpc_pipe_connect(*ctx /* TALLOC_CTX */,
397 if(NT_STATUS_IS_ERR(status)) {
398 DEBUG(1, ("Unable to open '%s': %s\n", location, nt_errstr(status)));
401 return ntstatus_to_werror(status);
404 *ctx = (struct registry_context *)rctx;
405 (*ctx)->ops = ®_backend_rpc;