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 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"
23 static struct hive_operations reg_backend_rpc;
26 * This is the RPC backend for the registry library.
29 static void init_winreg_String(struct winreg_String *name, const char *s)
35 #define openhive(u) static WERROR open_ ## u(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *hnd) \
37 struct winreg_Open ## u r; \
40 r.in.system_name = NULL; \
41 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED; \
44 status = dcerpc_winreg_Open ## u(p, mem_ctx, &r); \
45 if (NT_STATUS_IS_ERR(status)) {\
46 DEBUG(0,("Error executing open\n"));\
47 return ntstatus_to_werror(status);\
62 struct policy_handle pol;
71 WERROR (*open) (struct dcerpc_pipe *p, TALLOC_CTX *, struct policy_handle *h);
73 { HKEY_LOCAL_MACHINE, open_HKLM },
74 { HKEY_CURRENT_USER, open_HKCU },
75 { HKEY_CLASSES_ROOT, open_HKCR },
76 { HKEY_PERFORMANCE_DATA, open_HKPD },
77 { HKEY_USERS, open_HKU },
78 { HKEY_DYN_DATA, open_HKDD },
79 { HKEY_CURRENT_CONFIG, open_HKCC },
83 static WERROR rpc_query_key(const struct registry_key *k);
85 static WERROR rpc_get_predefined_key (struct registry_context *ctx, uint32_t hkey_type, struct registry_key **k)
88 struct registry_hive *h;
89 struct rpc_key_data *mykeydata;
91 for(n = 0; known_hives[n].hkey; n++)
93 if(known_hives[n].hkey == hkey_type) break;
96 if(!known_hives[n].open) {
97 DEBUG(1, ("No such hive %d\n", hkey_type));
98 return WERR_NO_MORE_ITEMS;
101 h = talloc(ctx, struct registry_hive);
102 h->functions = ®_backend_rpc;
104 h->backend_data = ctx->backend_data;
106 (*k) = h->root = talloc(h, struct registry_key);
108 (*k)->backend_data = mykeydata = talloc(*k, struct rpc_key_data);
109 mykeydata->num_values = -1;
110 mykeydata->num_subkeys = -1;
111 return known_hives[n].open((struct dcerpc_pipe *)ctx->backend_data, *k, &(mykeydata->pol));
115 static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k)
117 struct winreg_OpenKey r;
118 struct rpc_key_data *mykeydata;
120 k->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data);
121 mykeydata->num_values = -1;
122 mykeydata->num_subkeys = -1;
124 /* Then, open the handle using the hive */
126 memset(&r, 0, sizeof(struct winreg_OpenKey));
127 r.in.handle = &(((struct rpc_key_data *)k->hive->root->backend_data)->pol);
128 init_winreg_String(&r.in.keyname, k->path);
129 r.in.unknown = 0x00000000;
130 r.in.access_mask = 0x02000000;
131 r.out.handle = &mykeydata->pol;
133 dcerpc_winreg_OpenKey((struct dcerpc_pipe *)k->hive->backend_data, mem_ctx, &r);
139 static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, const struct registry_key *h, const char *name, struct registry_key **key)
141 struct rpc_key_data *mykeydata;
142 struct winreg_OpenKey r;
144 *key = talloc(mem_ctx, struct registry_key);
145 (*key)->name = talloc_strdup(mem_ctx, name);
147 (*key)->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data);
148 mykeydata->num_values = -1;
149 mykeydata->num_subkeys = -1;
151 /* Then, open the handle using the hive */
153 memset(&r, 0, sizeof(struct winreg_OpenKey));
154 r.in.parent_handle = &(((struct rpc_key_data *)h->backend_data)->pol);
155 init_winreg_String(&r.in.keyname, name);
156 r.in.unknown = 0x00000000;
157 r.in.access_mask = 0x02000000;
158 r.out.handle = &mykeydata->pol;
160 dcerpc_winreg_OpenKey((struct dcerpc_pipe *)(h->hive->backend_data), mem_ctx, &r);
165 static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, const struct registry_key *parent, int n, struct registry_value **value)
167 struct rpc_key_data *mykeydata = parent->backend_data;
169 struct winreg_EnumValue r;
170 uint32_t len1, zero = 0;
171 enum winreg_Type type;
173 struct winreg_StringBuf name;
176 if(mykeydata->num_values == -1) {
177 error = rpc_query_key(parent);
178 if(!W_ERROR_IS_OK(error)) return error;
181 len1 = mykeydata->max_valdatalen;
184 name.size = mykeydata->max_valnamelen * 2;
187 r.in.handle = &mykeydata->pol;
196 status = dcerpc_winreg_EnumValue((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
197 if(NT_STATUS_IS_ERR(status)) {
198 DEBUG(0, ("Error in EnumValue: %s\n", nt_errstr(status)));
199 return WERR_GENERAL_FAILURE;
202 if(NT_STATUS_IS_OK(status) &&
203 W_ERROR_IS_OK(r.out.result) && r.out.length) {
204 *value = talloc(mem_ctx, struct registry_value);
205 (*value)->name = talloc_strdup(mem_ctx, r.out.name->name);
206 (*value)->data_type = type;
207 (*value)->data = data_blob_talloc(mem_ctx, r.out.value, *r.out.length);
214 static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, const struct registry_key *parent, int n, struct registry_key **subkey)
216 struct winreg_EnumKey r;
217 struct rpc_key_data *mykeydata = parent->backend_data;
219 struct winreg_StringBuf namebuf, classbuf;
220 NTTIME change_time = 0;
227 classbuf.name = NULL;
229 r.in.handle = &mykeydata->pol;
231 r.in.name = &namebuf;
232 r.in.keyclass = &classbuf;
233 r.in.last_changed_time = &change_time;
234 r.out.name = &namebuf;
236 status = dcerpc_winreg_EnumKey((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
237 if(NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
238 char *name = talloc_strdup(mem_ctx, r.out.name->name);
239 return rpc_open_key(mem_ctx, parent, name, subkey);
245 static WERROR rpc_add_key(TALLOC_CTX *mem_ctx, const struct registry_key *parent, const char *name, uint32_t access_mask, struct security_descriptor *sec, struct registry_key **key)
248 struct winreg_CreateKey r;
250 init_winreg_String(&r.in.name, name);
251 init_winreg_String(&r.in.keyclass, NULL);
253 r.in.handle = parent->backend_data;
254 r.out.new_handle = talloc(mem_ctx, struct policy_handle);
256 r.in.access_mask = access_mask;
259 status = dcerpc_winreg_CreateKey((struct dcerpc_pipe *)(parent->hive->backend_data), mem_ctx, &r);
261 if (!NT_STATUS_IS_OK(status)) {
262 DEBUG(1, ("CreateKey failed - %s\n", nt_errstr(status)));
263 return ntstatus_to_werror(status);
266 if (W_ERROR_IS_OK(r.out.result)) {
267 *key = talloc(mem_ctx, struct registry_key);
268 (*key)->name = talloc_strdup(*key, name);
269 (*key)->backend_data = r.out.new_handle;
275 static WERROR rpc_query_key(const struct registry_key *k)
278 struct winreg_QueryInfoKey r;
279 struct rpc_key_data *mykeydata = k->backend_data;
280 TALLOC_CTX *mem_ctx = talloc_init("query_key");
282 r.in.classname = talloc(mem_ctx, struct winreg_String);
283 init_winreg_String(r.in.classname, NULL);
284 r.in.handle = &mykeydata->pol;
286 status = dcerpc_winreg_QueryInfoKey((struct dcerpc_pipe *)(k->hive->backend_data), mem_ctx, &r);
287 talloc_free(mem_ctx);
289 if (!NT_STATUS_IS_OK(status)) {
290 DEBUG(1, ("QueryInfoKey failed - %s\n", nt_errstr(status)));
291 return ntstatus_to_werror(status);
294 if (W_ERROR_IS_OK(r.out.result)) {
295 mykeydata->num_subkeys = *r.out.num_subkeys;
296 mykeydata->num_values = *r.out.num_values;
297 mykeydata->max_valnamelen = *r.out.max_valnamelen;
298 mykeydata->max_valdatalen = *r.out.max_valbufsize;
304 static WERROR rpc_del_key(const struct registry_key *parent, const char *name)
307 struct rpc_key_data *mykeydata = parent->backend_data;
308 struct winreg_DeleteKey r;
309 TALLOC_CTX *mem_ctx = talloc_init("del_key");
311 r.in.handle = &mykeydata->pol;
312 init_winreg_String(&r.in.key, name);
314 status = dcerpc_winreg_DeleteKey((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
316 talloc_free(mem_ctx);
321 static WERROR rpc_num_values(const struct registry_key *key, uint32_t *count)
323 struct rpc_key_data *mykeydata = key->backend_data;
326 if(mykeydata->num_values == -1) {
327 error = rpc_query_key(key);
328 if(!W_ERROR_IS_OK(error)) return error;
331 *count = mykeydata->num_values;
335 static WERROR rpc_num_subkeys(const struct registry_key *key, uint32_t *count)
337 struct rpc_key_data *mykeydata = key->backend_data;
340 if(mykeydata->num_subkeys == -1) {
341 error = rpc_query_key(key);
342 if(!W_ERROR_IS_OK(error)) return error;
345 *count = mykeydata->num_subkeys;
349 static struct hive_operations reg_backend_rpc = {
351 .open_key = rpc_open_key,
352 .get_subkey_by_index = rpc_get_subkey_by_index,
353 .get_value_by_index = rpc_get_value_by_index,
354 .add_key = rpc_add_key,
355 .del_key = rpc_del_key,
356 .num_subkeys = rpc_num_subkeys,
357 .num_values = rpc_num_values,
360 _PUBLIC_ WERROR reg_open_remote(struct registry_context **ctx, struct auth_session_info *session_info, struct cli_credentials *credentials,
361 const char *location, struct event_context *ev)
364 struct dcerpc_pipe *p;
366 *ctx = talloc(NULL, struct registry_context);
368 /* Default to local smbd if no connection is specified */
370 location = talloc_strdup(ctx, "ncalrpc:");
373 status = dcerpc_pipe_connect(*ctx /* TALLOC_CTX */,
377 (*ctx)->backend_data = p;
379 if(NT_STATUS_IS_ERR(status)) {
380 DEBUG(1, ("Unable to open '%s': %s\n", location, nt_errstr(status)));
383 return ntstatus_to_werror(status);
386 (*ctx)->get_predefined_key = rpc_get_predefined_key;
391 NTSTATUS registry_rpc_init(void)
394 return registry_register(®_backend_rpc);