r9338: fixed the winreg IDL to be correct for the EnumKey and EnumValue
[bbaumbach/samba-autobuild/.git] / source4 / lib / registry / reg_backend_rpc.c
1 /*
2    Samba Unix/Linux SMB implementation
3    RPC backend for the registry library
4    Copyright (C) 2003-2004 Jelmer Vernooij, jelmer@samba.org
5
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.
10    
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.
15    
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.  */
19  
20 #include "includes.h"
21 #include "registry.h"
22 #include "librpc/gen_ndr/ndr_winreg.h"
23
24 static struct hive_operations reg_backend_rpc;
25
26 /**
27  * This is the RPC backend for the registry library.
28  */
29
30 static void init_winreg_String(struct winreg_String *name, const char *s)
31 {
32     name->name = s;
33     if (s) {
34         name->name_len = 2 * (strlen_m(s) + 1);
35         name->name_size = name->name_len;
36     } else {
37         name->name_len = 0;
38         name->name_size = 0;
39     }
40 }
41
42
43 #define openhive(u) static WERROR open_ ## u(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *hnd) \
44 { \
45         struct winreg_Open ## u r; \
46         NTSTATUS status; \
47         \
48         r.in.system_name = 0; \
49         r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED; \
50         r.out.handle = hnd;\
51         \
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);\
56         }\
57 \
58         return r.out.result;\
59 }
60
61 openhive(HKLM)
62 openhive(HKCU)
63 openhive(HKPD)
64 openhive(HKU)
65 openhive(HKCR)
66 openhive(HKDD)
67 openhive(HKCC)
68
69 struct rpc_key_data {
70         struct policy_handle pol;
71         int num_subkeys;
72         int num_values;
73         int max_valnamelen;
74         int max_valdatalen;
75 };
76
77 static struct {
78         uint32_t hkey;
79         WERROR (*open) (struct dcerpc_pipe *p, TALLOC_CTX *, struct policy_handle *h);
80 } known_hives[] = {
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 },
88 { 0, NULL }
89 };
90
91 static WERROR rpc_query_key(struct registry_key *k);
92
93 static WERROR rpc_get_predefined_key (struct registry_context *ctx, uint32_t hkey_type, struct registry_key **k)
94 {
95         int n;
96         struct registry_hive *h;
97         struct rpc_key_data *mykeydata;
98
99         for(n = 0; known_hives[n].hkey; n++) 
100         {
101                 if(known_hives[n].hkey == hkey_type) break;
102         }
103         
104         if(!known_hives[n].open)  {
105                 DEBUG(1, ("No such hive %d\n", hkey_type));
106                 return WERR_NO_MORE_ITEMS;
107         }
108
109         h = talloc(ctx, struct registry_hive);
110         h->functions = &reg_backend_rpc;
111         h->location = NULL;
112         h->backend_data = ctx->backend_data;
113         
114         (*k) = h->root = talloc(h, struct registry_key);
115         (*k)->hive = h;
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));
120 }
121
122 #if 0
123 static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k)
124 {
125     struct winreg_OpenKey r;
126         struct rpc_key_data *mykeydata;
127
128         k->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data);
129         mykeydata->num_values = -1;
130         mykeydata->num_subkeys = -1;
131
132         /* Then, open the handle using the hive */
133
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;
140
141     dcerpc_winreg_OpenKey((struct dcerpc_pipe *)k->hive->backend_data, mem_ctx, &r);
142
143         return r.out.result;
144 }
145 #endif
146
147 static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h, const char *name, struct registry_key **key)
148 {
149         struct rpc_key_data *mykeydata;
150     struct winreg_OpenKey r;
151
152         *key = talloc(mem_ctx, struct registry_key);
153         (*key)->name = talloc_strdup(mem_ctx, name);
154
155         (*key)->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data);
156         mykeydata->num_values = -1;
157         mykeydata->num_subkeys = -1;
158
159         /* Then, open the handle using the hive */
160
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;
167
168     dcerpc_winreg_OpenKey((struct dcerpc_pipe *)(h->hive->backend_data), mem_ctx, &r);
169
170         return r.out.result;
171 }
172
173 static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, struct registry_key *parent, int n, struct registry_value **value)  
174 {
175         struct rpc_key_data *mykeydata = parent->backend_data;
176         WERROR error;
177         struct winreg_EnumValue r;
178         uint32_t type, len1, zero = 0;
179         NTSTATUS status;
180         struct winreg_StringBuf name;
181         uint8_t u8;
182         
183         if(mykeydata->num_values == -1) {
184                 error = rpc_query_key(parent);
185                 if(!W_ERROR_IS_OK(error)) return error;
186         }
187
188         len1 = mykeydata->max_valdatalen;
189         
190         name.length = 0;
191         name.size   = mykeydata->max_valnamelen * 2;
192         name.name   = "";
193
194         r.in.handle = &mykeydata->pol;
195         r.in.enum_index = n;
196         r.in.name = &name;
197         r.in.type = &type;
198         r.in.value = &u8;
199         r.in.length = &zero;
200         r.in.size = &len1;
201         r.out.name = &name;
202         
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;
207         }
208         
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);
216                 return WERR_OK;
217         }
218         
219         return r.out.result;
220 }
221
222 static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, struct registry_key *parent, int n, struct registry_key **subkey) 
223 {
224         struct winreg_EnumKey r;
225         struct rpc_key_data *mykeydata = parent->backend_data;
226         NTSTATUS status;
227         struct winreg_StringBuf namebuf, classbuf;
228         NTTIME change_time = 0;
229
230         namebuf.length = 0;
231         namebuf.size   = 1024;
232         namebuf.name   = NULL;
233         classbuf.length = 0;
234         classbuf.size   = 0;
235         classbuf.name   = NULL;
236
237         r.in.handle = &mykeydata->pol;
238         r.in.enum_index = n;
239         r.in.name = &namebuf;
240         r.in.class = &classbuf;
241         r.in.last_changed_time = &change_time;
242         r.out.name = &namebuf;
243
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);
248         }
249
250         return r.out.result;
251 }
252
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)
254 {
255         NTSTATUS status;
256         struct winreg_CreateKey r;
257
258         init_winreg_String(&r.in.key, name);
259         init_winreg_String(&r.in.class, NULL);
260
261         r.in.handle = parent->backend_data;
262         r.out.handle = talloc(mem_ctx, struct policy_handle);   
263         r.in.options = 0;
264         r.in.access_mask = access_mask;
265         r.in.sec_desc = NULL;
266
267         status = dcerpc_winreg_CreateKey((struct dcerpc_pipe *)(parent->hive->backend_data), mem_ctx, &r);
268
269     if (!NT_STATUS_IS_OK(status)) {
270         DEBUG(1, ("CreateKey failed - %s\n", nt_errstr(status)));
271         return ntstatus_to_werror(status);
272     }
273
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;
278         }
279
280         return r.out.result;
281 }
282
283 static WERROR rpc_query_key(struct registry_key *k)
284 {
285     NTSTATUS status;
286     struct winreg_QueryInfoKey r;
287     struct rpc_key_data *mykeydata = k->backend_data;
288         TALLOC_CTX *mem_ctx = talloc_init("query_key");
289
290     init_winreg_String(&r.in.class, NULL);
291     r.in.handle = &mykeydata->pol;
292         
293     status = dcerpc_winreg_QueryInfoKey((struct dcerpc_pipe *)(k->hive->backend_data), mem_ctx, &r);
294         talloc_free(mem_ctx);
295
296     if (!NT_STATUS_IS_OK(status)) {
297         DEBUG(1, ("QueryInfoKey failed - %s\n", nt_errstr(status)));
298         return ntstatus_to_werror(status);
299     }
300                                                                                                        
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;
306         } 
307
308         return r.out.result;
309 }
310
311 static WERROR rpc_del_key(struct registry_key *parent, const char *name)
312 {
313         NTSTATUS status;
314         struct rpc_key_data *mykeydata = parent->backend_data;
315         struct winreg_DeleteKey r;
316         TALLOC_CTX *mem_ctx = talloc_init("del_key");
317         
318     r.in.handle = &mykeydata->pol;
319     init_winreg_String(&r.in.key, name);
320  
321     status = dcerpc_winreg_DeleteKey((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
322
323         talloc_free(mem_ctx);
324
325         return r.out.result;
326 }
327
328 static WERROR rpc_num_values(struct registry_key *key, int *count) {
329         struct rpc_key_data *mykeydata = key->backend_data;
330         WERROR error;
331                 
332         if(mykeydata->num_values == -1) {
333                 error = rpc_query_key(key);
334                 if(!W_ERROR_IS_OK(error)) return error;
335         }
336                         
337         *count = mykeydata->num_values;
338         return WERR_OK;
339 }
340
341 static WERROR rpc_num_subkeys(struct registry_key *key, int *count) {
342         struct rpc_key_data *mykeydata = key->backend_data;
343         WERROR error;
344
345         if(mykeydata->num_subkeys == -1) {
346                 error = rpc_query_key(key);
347                 if(!W_ERROR_IS_OK(error)) return error;
348         }
349                         
350         *count = mykeydata->num_subkeys;
351         return WERR_OK;
352 }
353
354 static struct hive_operations reg_backend_rpc = {
355         .name = "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,
363 };
364
365 WERROR reg_open_remote(struct registry_context **ctx, struct cli_credentials *credentials, 
366                        const char *location, struct event_context *ev)
367 {
368         NTSTATUS status;
369         struct dcerpc_pipe *p;
370
371         *ctx = talloc(NULL, struct registry_context);
372
373         /* Default to local smbd if no connection is specified */
374         if (!location) {
375                 location = talloc_strdup(ctx, "ncalrpc:");
376         }
377
378         status = dcerpc_pipe_connect(*ctx /* TALLOC_CTX */, 
379                                      &p, location, 
380                                      DCERPC_WINREG_UUID,
381                                      DCERPC_WINREG_VERSION,
382                                      credentials, ev);
383         (*ctx)->backend_data = p;
384
385         if(NT_STATUS_IS_ERR(status)) {
386                 DEBUG(1, ("Unable to open '%s': %s\n", location, nt_errstr(status)));
387                 talloc_free(*ctx);
388                 *ctx = NULL;
389                 return ntstatus_to_werror(status);
390         }
391
392         (*ctx)->get_predefined_key = rpc_get_predefined_key;
393
394         return WERR_OK;
395 }
396
397 NTSTATUS registry_rpc_init(void)
398 {
399         return registry_register(&reg_backend_rpc);
400 }