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_t 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));
126 static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k)
128 struct winreg_OpenKey r;
129 struct rpc_key_data *mykeydata;
131 k->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data);
132 mykeydata->num_values = -1;
133 mykeydata->num_subkeys = -1;
135 /* Then, open the handle using the hive */
137 memset(&r, 0, sizeof(struct winreg_OpenKey));
138 r.in.handle = &(((struct rpc_key_data *)k->hive->root->backend_data)->pol);
139 init_winreg_String(&r.in.keyname, k->path);
140 r.in.unknown = 0x00000000;
141 r.in.access_mask = 0x02000000;
142 r.out.handle = &mykeydata->pol;
144 dcerpc_winreg_OpenKey((struct dcerpc_pipe *)k->hive->backend_data, mem_ctx, &r);
150 static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h, const char *name, struct registry_key **key)
152 struct rpc_key_data *mykeydata;
153 struct winreg_OpenKey r;
155 *key = talloc(mem_ctx, struct registry_key);
156 (*key)->name = talloc_strdup(mem_ctx, name);
158 (*key)->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data);
159 mykeydata->num_values = -1;
160 mykeydata->num_subkeys = -1;
162 /* Then, open the handle using the hive */
164 memset(&r, 0, sizeof(struct winreg_OpenKey));
165 r.in.handle = &(((struct rpc_key_data *)h->backend_data)->pol);
166 init_winreg_String(&r.in.keyname, name);
167 r.in.unknown = 0x00000000;
168 r.in.access_mask = 0x02000000;
169 r.out.handle = &mykeydata->pol;
171 dcerpc_winreg_OpenKey((struct dcerpc_pipe *)(h->hive->backend_data), mem_ctx, &r);
176 static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, struct registry_key *parent, int n, struct registry_value **value)
178 struct rpc_key_data *mykeydata = parent->backend_data;
180 struct winreg_EnumValue r;
181 uint32_t type, len1, zero = 0;
186 if(mykeydata->num_values == -1) {
187 error = rpc_query_key(parent);
188 if(!W_ERROR_IS_OK(error)) return error;
191 len1 = mykeydata->max_valdatalen;
193 r.in.handle = &mykeydata->pol;
195 r.in.name_in.length = 0;
196 r.in.name_in.size = mykeydata->max_valnamelen * 2;
197 r.in.name_in.name = &buf16;
205 status = dcerpc_winreg_EnumValue((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
206 if(NT_STATUS_IS_ERR(status)) {
207 DEBUG(0, ("Error in EnumValue: %s\n", nt_errstr(status)));
208 return WERR_GENERAL_FAILURE;
211 if(NT_STATUS_IS_OK(status) &&
212 W_ERROR_IS_OK(r.out.result) && r.out.length) {
213 *value = talloc(mem_ctx, struct registry_value);
214 (*value)->name = talloc_strdup(mem_ctx, r.out.name_out.name);
215 (*value)->data_type = type;
216 (*value)->data_len = *r.out.length;
217 (*value)->data_blk = talloc_memdup(mem_ctx, r.out.value, *r.out.length);
224 static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, struct registry_key *parent, int n, struct registry_key **subkey)
226 struct winreg_EnumKey r;
227 struct winreg_EnumKeyNameRequest keyname;
228 struct winreg_String classname;
229 struct winreg_Time tm;
230 struct rpc_key_data *mykeydata = parent->backend_data;
233 r.in.handle = &mykeydata->pol;
234 keyname.unknown = 0x0000020a;
235 init_winreg_String(&keyname.key_name, NULL);
236 init_winreg_String(&classname, NULL);
237 r.in.in_name = &keyname;
238 r.in.class = &classname;
239 tm.low = tm.high = 0x7fffffff;
240 r.in.last_changed_time = &tm;
243 r.in.unknown = r.out.unknown = 0x0414;
244 r.in.key_name_len = r.out.key_name_len = 0;
245 status = dcerpc_winreg_EnumKey((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
246 if(NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
247 return rpc_open_key(mem_ctx, parent, talloc_strdup(mem_ctx, r.out.out_name->name), subkey);
253 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)
256 struct winreg_CreateKey r;
258 init_winreg_String(&r.in.key, name);
259 init_winreg_String(&r.in.class, NULL);
261 r.in.handle = parent->backend_data;
262 r.out.handle = talloc(mem_ctx, struct policy_handle);
264 r.in.access_mask = access_mask;
265 r.in.sec_desc = NULL;
267 status = dcerpc_winreg_CreateKey((struct dcerpc_pipe *)(parent->hive->backend_data), mem_ctx, &r);
269 if (!NT_STATUS_IS_OK(status)) {
270 DEBUG(1, ("CreateKey failed - %s\n", nt_errstr(status)));
271 return ntstatus_to_werror(status);
274 if (W_ERROR_IS_OK(r.out.result)) {
275 *key = talloc(mem_ctx, struct registry_key);
276 (*key)->name = talloc_strdup(*key, name);
277 (*key)->backend_data = r.out.handle;
283 static WERROR rpc_query_key(struct registry_key *k)
286 struct winreg_QueryInfoKey r;
287 struct rpc_key_data *mykeydata = k->backend_data;
288 TALLOC_CTX *mem_ctx = talloc_init("query_key");
290 init_winreg_String(&r.in.class, NULL);
291 r.in.handle = &mykeydata->pol;
293 status = dcerpc_winreg_QueryInfoKey((struct dcerpc_pipe *)(k->hive->backend_data), mem_ctx, &r);
294 talloc_free(mem_ctx);
296 if (!NT_STATUS_IS_OK(status)) {
297 DEBUG(1, ("QueryInfoKey failed - %s\n", nt_errstr(status)));
298 return ntstatus_to_werror(status);
301 if (W_ERROR_IS_OK(r.out.result)) {
302 mykeydata->num_subkeys = r.out.num_subkeys;
303 mykeydata->num_values = r.out.num_values;
304 mykeydata->max_valnamelen = r.out.max_valnamelen;
305 mykeydata->max_valdatalen = r.out.max_valbufsize;
311 static WERROR rpc_del_key(struct registry_key *parent, const char *name)
314 struct rpc_key_data *mykeydata = parent->backend_data;
315 struct winreg_DeleteKey r;
316 TALLOC_CTX *mem_ctx = talloc_init("del_key");
318 r.in.handle = &mykeydata->pol;
319 init_winreg_String(&r.in.key, name);
321 status = dcerpc_winreg_DeleteKey((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
323 talloc_free(mem_ctx);
328 static WERROR rpc_num_values(struct registry_key *key, int *count) {
329 struct rpc_key_data *mykeydata = key->backend_data;
332 if(mykeydata->num_values == -1) {
333 error = rpc_query_key(key);
334 if(!W_ERROR_IS_OK(error)) return error;
337 *count = mykeydata->num_values;
341 static WERROR rpc_num_subkeys(struct registry_key *key, int *count) {
342 struct rpc_key_data *mykeydata = key->backend_data;
345 if(mykeydata->num_subkeys == -1) {
346 error = rpc_query_key(key);
347 if(!W_ERROR_IS_OK(error)) return error;
350 *count = mykeydata->num_subkeys;
354 static struct hive_operations reg_backend_rpc = {
356 .open_key = rpc_open_key,
357 .get_subkey_by_index = rpc_get_subkey_by_index,
358 .get_value_by_index = rpc_get_value_by_index,
359 .add_key = rpc_add_key,
360 .del_key = rpc_del_key,
361 .num_subkeys = rpc_num_subkeys,
362 .num_values = rpc_num_values,
365 WERROR reg_open_remote (struct registry_context **ctx, struct cli_credentials *credentials, const char *location)
368 struct dcerpc_pipe *p;
370 *ctx = talloc(NULL, struct registry_context);
372 /* Default to local smbd if no connection is specified */
374 location = talloc_strdup(ctx, "ncalrpc:");
377 status = dcerpc_pipe_connect(*ctx /* TALLOC_CTX */,
380 DCERPC_WINREG_VERSION,
382 (*ctx)->backend_data = p;
384 if(NT_STATUS_IS_ERR(status)) {
385 DEBUG(1, ("Unable to open '%s': %s\n", location, nt_errstr(status)));
388 return ntstatus_to_werror(status);
391 (*ctx)->get_predefined_key = rpc_get_predefined_key;
396 NTSTATUS registry_rpc_init(void)
398 return registry_register(®_backend_rpc);