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; \
48 r.in.system_name = 0; \
49 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED; \
52 status = dcerpc_winreg_Open ## u(p, mem_ctx, &r); \
53 if (NT_STATUS_IS_ERR(status)) {\
54 DEBUG(0,("Error executing open\n"));\
55 return ntstatus_to_werror(status);\
70 struct policy_handle pol;
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(struct registry_key *k);
93 static WERROR rpc_get_predefined_key (struct registry_context *ctx, uint32_t hkey_type, struct registry_key **k)
96 struct registry_hive *h;
97 struct rpc_key_data *mykeydata;
99 for(n = 0; known_hives[n].hkey; n++)
101 if(known_hives[n].hkey == hkey_type) break;
104 if(!known_hives[n].open) {
105 DEBUG(1, ("No such hive %d\n", hkey_type));
106 return WERR_NO_MORE_ITEMS;
109 h = talloc(ctx, struct registry_hive);
110 h->functions = ®_backend_rpc;
112 h->backend_data = ctx->backend_data;
114 (*k) = h->root = talloc(h, struct registry_key);
116 (*k)->backend_data = mykeydata = talloc(*k, struct rpc_key_data);
117 mykeydata->num_values = -1;
118 mykeydata->num_subkeys = -1;
119 return known_hives[n].open((struct dcerpc_pipe *)ctx->backend_data, *k, &(mykeydata->pol));
123 static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k)
125 struct winreg_OpenKey r;
126 struct rpc_key_data *mykeydata;
128 k->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data);
129 mykeydata->num_values = -1;
130 mykeydata->num_subkeys = -1;
132 /* Then, open the handle using the hive */
134 memset(&r, 0, sizeof(struct winreg_OpenKey));
135 r.in.handle = &(((struct rpc_key_data *)k->hive->root->backend_data)->pol);
136 init_winreg_String(&r.in.keyname, k->path);
137 r.in.unknown = 0x00000000;
138 r.in.access_mask = 0x02000000;
139 r.out.handle = &mykeydata->pol;
141 dcerpc_winreg_OpenKey((struct dcerpc_pipe *)k->hive->backend_data, mem_ctx, &r);
147 static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h, const char *name, struct registry_key **key)
149 struct rpc_key_data *mykeydata;
150 struct winreg_OpenKey r;
152 *key = talloc(mem_ctx, struct registry_key);
153 (*key)->name = talloc_strdup(mem_ctx, name);
155 (*key)->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data);
156 mykeydata->num_values = -1;
157 mykeydata->num_subkeys = -1;
159 /* Then, open the handle using the hive */
161 memset(&r, 0, sizeof(struct winreg_OpenKey));
162 r.in.handle = &(((struct rpc_key_data *)h->backend_data)->pol);
163 init_winreg_String(&r.in.keyname, name);
164 r.in.unknown = 0x00000000;
165 r.in.access_mask = 0x02000000;
166 r.out.handle = &mykeydata->pol;
168 dcerpc_winreg_OpenKey((struct dcerpc_pipe *)(h->hive->backend_data), mem_ctx, &r);
173 static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, struct registry_key *parent, int n, struct registry_value **value)
175 struct rpc_key_data *mykeydata = parent->backend_data;
177 struct winreg_EnumValue r;
178 uint32_t type, len1, zero = 0;
180 struct winreg_StringBuf name;
183 if(mykeydata->num_values == -1) {
184 error = rpc_query_key(parent);
185 if(!W_ERROR_IS_OK(error)) return error;
188 len1 = mykeydata->max_valdatalen;
191 name.size = mykeydata->max_valnamelen * 2;
194 r.in.handle = &mykeydata->pol;
203 status = dcerpc_winreg_EnumValue((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
204 if(NT_STATUS_IS_ERR(status)) {
205 DEBUG(0, ("Error in EnumValue: %s\n", nt_errstr(status)));
206 return WERR_GENERAL_FAILURE;
209 if(NT_STATUS_IS_OK(status) &&
210 W_ERROR_IS_OK(r.out.result) && r.out.length) {
211 *value = talloc(mem_ctx, struct registry_value);
212 (*value)->name = talloc_strdup(mem_ctx, r.out.name->name);
213 (*value)->data_type = type;
214 (*value)->data_len = *r.out.length;
215 (*value)->data_blk = talloc_memdup(mem_ctx, r.out.value, *r.out.length);
222 static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, struct registry_key *parent, int n, struct registry_key **subkey)
224 struct winreg_EnumKey r;
225 struct rpc_key_data *mykeydata = parent->backend_data;
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.class = &classbuf;
241 r.in.last_changed_time = &change_time;
242 r.out.name = &namebuf;
244 status = dcerpc_winreg_EnumKey((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
245 if(NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
246 char *name = talloc_strdup(mem_ctx, r.out.name->name);
247 return rpc_open_key(mem_ctx, parent, 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,
366 const char *location, struct event_context *ev)
369 struct dcerpc_pipe *p;
371 *ctx = talloc(NULL, struct registry_context);
373 /* Default to local smbd if no connection is specified */
375 location = talloc_strdup(ctx, "ncalrpc:");
378 status = dcerpc_pipe_connect(*ctx /* TALLOC_CTX */,
381 DCERPC_WINREG_VERSION,
383 (*ctx)->backend_data = p;
385 if(NT_STATUS_IS_ERR(status)) {
386 DEBUG(1, ("Unable to open '%s': %s\n", location, nt_errstr(status)));
389 return ntstatus_to_werror(status);
392 (*ctx)->get_predefined_key = rpc_get_predefined_key;
397 NTSTATUS registry_rpc_init(void)
399 return registry_register(®_backend_rpc);