2 Samba Unix/Linux SMB implementation
3 RPC backend for the registry library
4 Copyright (C) 2003-2004 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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include "librpc/gen_ndr/ndr_winreg.h"
24 static struct hive_operations reg_backend_rpc;
27 * This is the RPC backend for the registry library.
30 static void init_winreg_String(struct winreg_String *name, const char *s)
34 name->name_len = 2 * (strlen_m(s) + 1);
35 name->name_size = name->name_len;
43 #define openhive(u) static WERROR open_ ## u(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *hnd) \
45 struct winreg_Open ## u r; \
46 struct winreg_OpenUnknown unknown; \
49 unknown.unknown0 = 0x84e0; \
50 unknown.unknown1 = 0x0000; \
51 r.in.unknown = &unknown; \
52 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED; \
55 status = dcerpc_winreg_Open ## u(p, mem_ctx, &r); \
56 if (NT_STATUS_IS_ERR(status)) {\
57 DEBUG(0,("Error executing open\n"));\
58 return ntstatus_to_werror(status);\
73 struct policy_handle pol;
82 WERROR (*open) (struct dcerpc_pipe *p, TALLOC_CTX *, struct policy_handle *h);
84 { HKEY_LOCAL_MACHINE, open_HKLM },
85 { HKEY_CURRENT_USER, open_HKCU },
86 { HKEY_CLASSES_ROOT, open_HKCR },
87 { HKEY_PERFORMANCE_DATA, open_HKPD },
88 { HKEY_USERS, open_HKU },
89 { HKEY_DYN_DATA, open_HKDD },
90 { HKEY_CURRENT_CONFIG, open_HKCC },
94 static WERROR rpc_query_key(struct registry_key *k);
96 static WERROR rpc_get_predefined_key (struct registry_context *ctx, uint32 hkey_type, struct registry_key **k)
99 struct registry_hive *h;
100 struct rpc_key_data *mykeydata;
102 for(n = 0; known_hives[n].hkey; n++)
104 if(known_hives[n].hkey == hkey_type) break;
107 if(!known_hives[n].open) {
108 DEBUG(1, ("No such hive %d\n", hkey_type));
109 return WERR_NO_MORE_ITEMS;
112 h = talloc(ctx, struct registry_hive);
113 h->functions = ®_backend_rpc;
115 h->backend_data = ctx->backend_data;
117 (*k) = h->root = talloc(h, struct registry_key);
119 (*k)->backend_data = mykeydata = talloc(*k, struct rpc_key_data);
120 mykeydata->num_values = -1;
121 mykeydata->num_subkeys = -1;
122 return known_hives[n].open((struct dcerpc_pipe *)ctx->backend_data, *k, &(mykeydata->pol));
125 static int rpc_close (void *_h)
127 struct registry_context *h = _h;
128 dcerpc_pipe_close(h->backend_data);
133 static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k)
135 struct winreg_OpenKey r;
136 struct rpc_key_data *mykeydata;
138 k->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data);
139 mykeydata->num_values = -1;
140 mykeydata->num_subkeys = -1;
142 /* Then, open the handle using the hive */
144 memset(&r, 0, sizeof(struct winreg_OpenKey));
145 r.in.handle = &(((struct rpc_key_data *)k->hive->root->backend_data)->pol);
146 init_winreg_String(&r.in.keyname, k->path);
147 r.in.unknown = 0x00000000;
148 r.in.access_mask = 0x02000000;
149 r.out.handle = &mykeydata->pol;
151 dcerpc_winreg_OpenKey((struct dcerpc_pipe *)k->hive->backend_data, mem_ctx, &r);
157 static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h, const char *name, struct registry_key **key)
159 struct rpc_key_data *mykeydata;
160 struct winreg_OpenKey r;
162 *key = talloc(mem_ctx, struct registry_key);
163 (*key)->name = talloc_strdup(mem_ctx, name);
165 (*key)->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data);
166 mykeydata->num_values = -1;
167 mykeydata->num_subkeys = -1;
169 /* Then, open the handle using the hive */
171 memset(&r, 0, sizeof(struct winreg_OpenKey));
172 r.in.handle = &(((struct rpc_key_data *)h->backend_data)->pol);
173 init_winreg_String(&r.in.keyname, name);
174 r.in.unknown = 0x00000000;
175 r.in.access_mask = 0x02000000;
176 r.out.handle = &mykeydata->pol;
178 dcerpc_winreg_OpenKey((struct dcerpc_pipe *)(h->hive->backend_data), mem_ctx, &r);
183 static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, struct registry_key *parent, int n, struct registry_value **value)
185 struct rpc_key_data *mykeydata = parent->backend_data;
187 struct winreg_EnumValue r;
188 uint32 type, len1, zero = 0;
193 if(mykeydata->num_values == -1) {
194 error = rpc_query_key(parent);
195 if(!W_ERROR_IS_OK(error)) return error;
198 len1 = mykeydata->max_valdatalen;
200 r.in.handle = &mykeydata->pol;
202 r.in.name_in.length = 0;
203 r.in.name_in.size = mykeydata->max_valnamelen * 2;
204 r.in.name_in.name = &buf16;
212 status = dcerpc_winreg_EnumValue((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
213 if(NT_STATUS_IS_ERR(status)) {
214 DEBUG(0, ("Error in EnumValue: %s\n", nt_errstr(status)));
215 return WERR_GENERAL_FAILURE;
218 if(NT_STATUS_IS_OK(status) &&
219 W_ERROR_IS_OK(r.out.result) && r.out.length) {
220 *value = talloc(mem_ctx, struct registry_value);
221 (*value)->name = talloc_strdup(mem_ctx, r.out.name_out.name);
222 (*value)->data_type = type;
223 (*value)->data_len = *r.out.length;
224 (*value)->data_blk = talloc_memdup(mem_ctx, r.out.value, *r.out.length);
231 static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, struct registry_key *parent, int n, struct registry_key **subkey)
233 struct winreg_EnumKey r;
234 struct winreg_EnumKeyNameRequest keyname;
235 struct winreg_String classname;
236 struct winreg_Time tm;
237 struct rpc_key_data *mykeydata = parent->backend_data;
240 r.in.handle = &mykeydata->pol;
241 keyname.unknown = 0x0000020a;
242 init_winreg_String(&keyname.key_name, NULL);
243 init_winreg_String(&classname, NULL);
244 r.in.in_name = &keyname;
245 r.in.class = &classname;
246 tm.low = tm.high = 0x7fffffff;
247 r.in.last_changed_time = &tm;
250 r.in.unknown = r.out.unknown = 0x0414;
251 r.in.key_name_len = r.out.key_name_len = 0;
252 status = dcerpc_winreg_EnumKey((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
253 if(NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
254 return rpc_open_key(mem_ctx, parent, talloc_strdup(mem_ctx, r.out.out_name->name), subkey);
260 static WERROR rpc_add_key(TALLOC_CTX *mem_ctx, struct registry_key *parent, const char *name, uint32_t access_mask, struct security_descriptor *sec, struct registry_key **key)
263 struct winreg_CreateKey r;
265 init_winreg_String(&r.in.key, name);
266 init_winreg_String(&r.in.class, NULL);
268 r.in.handle = parent->backend_data;
269 r.out.handle = talloc(mem_ctx, struct policy_handle);
271 r.in.access_mask = access_mask;
272 r.in.sec_desc = NULL;
274 status = dcerpc_winreg_CreateKey((struct dcerpc_pipe *)(parent->hive->backend_data), mem_ctx, &r);
276 if (!NT_STATUS_IS_OK(status)) {
277 DEBUG(1, ("CreateKey failed - %s\n", nt_errstr(status)));
278 return ntstatus_to_werror(status);
281 if (W_ERROR_IS_OK(r.out.result)) {
282 *key = talloc(mem_ctx, struct registry_key);
283 (*key)->name = talloc_strdup(*key, name);
284 (*key)->backend_data = r.out.handle;
290 static WERROR rpc_query_key(struct registry_key *k)
293 struct winreg_QueryInfoKey r;
294 struct rpc_key_data *mykeydata = k->backend_data;
295 TALLOC_CTX *mem_ctx = talloc_init("query_key");
297 init_winreg_String(&r.in.class, NULL);
298 r.in.handle = &mykeydata->pol;
300 status = dcerpc_winreg_QueryInfoKey((struct dcerpc_pipe *)(k->hive->backend_data), mem_ctx, &r);
301 talloc_free(mem_ctx);
303 if (!NT_STATUS_IS_OK(status)) {
304 DEBUG(1, ("QueryInfoKey failed - %s\n", nt_errstr(status)));
305 return ntstatus_to_werror(status);
308 if (W_ERROR_IS_OK(r.out.result)) {
309 mykeydata->num_subkeys = r.out.num_subkeys;
310 mykeydata->num_values = r.out.num_values;
311 mykeydata->max_valnamelen = r.out.max_valnamelen;
312 mykeydata->max_valdatalen = r.out.max_valbufsize;
318 static WERROR rpc_del_key(struct registry_key *parent, const char *name)
321 struct rpc_key_data *mykeydata = parent->backend_data;
322 struct winreg_DeleteKey r;
323 TALLOC_CTX *mem_ctx = talloc_init("del_key");
325 r.in.handle = &mykeydata->pol;
326 init_winreg_String(&r.in.key, name);
328 status = dcerpc_winreg_DeleteKey((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
330 talloc_free(mem_ctx);
335 static WERROR rpc_num_values(struct registry_key *key, int *count) {
336 struct rpc_key_data *mykeydata = key->backend_data;
339 if(mykeydata->num_values == -1) {
340 error = rpc_query_key(key);
341 if(!W_ERROR_IS_OK(error)) return error;
344 *count = mykeydata->num_values;
348 static WERROR rpc_num_subkeys(struct registry_key *key, int *count) {
349 struct rpc_key_data *mykeydata = key->backend_data;
352 if(mykeydata->num_subkeys == -1) {
353 error = rpc_query_key(key);
354 if(!W_ERROR_IS_OK(error)) return error;
357 *count = mykeydata->num_subkeys;
361 static struct hive_operations reg_backend_rpc = {
363 .open_key = rpc_open_key,
364 .get_subkey_by_index = rpc_get_subkey_by_index,
365 .get_value_by_index = rpc_get_value_by_index,
366 .add_key = rpc_add_key,
367 .del_key = rpc_del_key,
368 .num_subkeys = rpc_num_subkeys,
369 .num_values = rpc_num_values,
372 WERROR reg_open_remote (struct registry_context **ctx, const char *user, const char *pass, const char *location)
375 struct dcerpc_pipe *p;
377 *ctx = talloc(NULL, struct registry_context);
379 /* Default to local smbd if no connection is specified */
381 location = talloc_strdup(ctx, "ncalrpc:");
384 status = dcerpc_pipe_connect(&p, location,
386 DCERPC_WINREG_VERSION,
389 (*ctx)->backend_data = p;
391 if(NT_STATUS_IS_ERR(status)) {
392 DEBUG(1, ("Unable to open '%s': %s\n", location, nt_errstr(status)));
393 return ntstatus_to_werror(status);
396 (*ctx)->get_predefined_key = rpc_get_predefined_key;
398 talloc_set_destructor(*ctx, rpc_close);
403 NTSTATUS registry_rpc_init(void)
405 return registry_register(®_backend_rpc);