r4132: - Bunch of rather large fixes in the registry
[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         struct winreg_OpenUnknown unknown; \
47         NTSTATUS status; \
48         \
49         unknown.unknown0 = 0x84e0; \
50         unknown.unknown1 = 0x0000; \
51         r.in.unknown = &unknown; \
52         r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED; \
53         r.out.handle = hnd;\
54         \
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);\
59         }\
60 \
61         return r.out.result;\
62 }
63
64 openhive(HKLM)
65 openhive(HKCU)
66 openhive(HKPD)
67 openhive(HKU)
68 openhive(HKCR)
69 openhive(HKDD)
70 openhive(HKCC)
71
72 struct rpc_key_data {
73         struct policy_handle pol;
74         int num_subkeys;
75         int num_values;
76         int max_valnamelen;
77         int max_valdatalen;
78 };
79
80 struct {
81         uint32 hkey;
82         WERROR (*open) (struct dcerpc_pipe *p, TALLOC_CTX *, struct policy_handle *h);
83 } known_hives[] = {
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 },
91 { 0, NULL }
92 };
93
94 static WERROR rpc_query_key(struct registry_key *k);
95
96 static WERROR rpc_get_hive (struct registry_context *ctx, uint32 hkey_type, struct registry_key **k)
97 {
98         int n;
99         struct registry_hive *h;
100         struct rpc_key_data *mykeydata;
101
102         for(n = 0; known_hives[n].hkey; n++) 
103         {
104                 if(known_hives[n].hkey == hkey_type) break;
105         }
106         
107         if(!known_hives[n].open)  {
108                 DEBUG(1, ("No such hive %d\n", hkey_type));
109                 return WERR_NO_MORE_ITEMS;
110         }
111
112         h = talloc_p(ctx, struct registry_hive);
113         h->functions = &reg_backend_rpc;
114         h->location = NULL;
115         h->backend_data = ctx->backend_data;
116         h->reg_ctx = ctx;
117         
118         (*k) = h->root = talloc_p(h, struct registry_key);
119         (*k)->hive = h;
120         (*k)->backend_data = mykeydata = talloc_p(*k, struct rpc_key_data);
121         mykeydata->num_values = -1;
122         mykeydata->num_subkeys = -1;
123         return known_hives[n].open((struct dcerpc_pipe *)ctx->backend_data, *k, &(mykeydata->pol));
124 }
125
126 static int rpc_close (void *_h)
127 {
128         struct registry_context *h = _h;
129         dcerpc_pipe_close(h->backend_data);
130         return 0;
131 }
132
133 #if 0
134 static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k)
135 {
136     struct winreg_OpenKey r;
137         struct rpc_key_data *mykeydata;
138
139         k->backend_data = mykeydata = talloc_p(mem_ctx, struct rpc_key_data);
140         mykeydata->num_values = -1;
141         mykeydata->num_subkeys = -1;
142
143         /* Then, open the handle using the hive */
144
145         memset(&r, 0, sizeof(struct winreg_OpenKey));
146     r.in.handle = &(((struct rpc_key_data *)k->hive->root->backend_data)->pol);
147     init_winreg_String(&r.in.keyname, k->path);
148     r.in.unknown = 0x00000000;
149     r.in.access_mask = 0x02000000;
150     r.out.handle = &mykeydata->pol;
151
152     dcerpc_winreg_OpenKey((struct dcerpc_pipe *)k->hive->backend_data, mem_ctx, &r);
153
154         return r.out.result;
155 }
156 #endif
157
158 static WERROR rpc_open_rel_key(TALLOC_CTX *mem_ctx, struct registry_key *h, const char *name, struct registry_key **key)
159 {
160         struct rpc_key_data *mykeydata;
161     struct winreg_OpenKey r;
162
163         *key = talloc_p(mem_ctx, struct registry_key);
164         (*key)->name = talloc_strdup(mem_ctx, name);
165
166         (*key)->backend_data = mykeydata = talloc_p(mem_ctx, struct rpc_key_data);
167         mykeydata->num_values = -1;
168         mykeydata->num_subkeys = -1;
169
170         /* Then, open the handle using the hive */
171
172         memset(&r, 0, sizeof(struct winreg_OpenKey));
173     r.in.handle = &(((struct rpc_key_data *)h->backend_data)->pol);
174     init_winreg_String(&r.in.keyname, name);
175     r.in.unknown = 0x00000000;
176     r.in.access_mask = 0x02000000;
177     r.out.handle = &mykeydata->pol;
178
179     dcerpc_winreg_OpenKey((struct dcerpc_pipe *)(h->hive->backend_data), mem_ctx, &r);
180
181         return r.out.result;
182 }
183
184 static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, struct registry_hive *h, const char *name, struct registry_key **key)
185 {
186         return rpc_open_rel_key(mem_ctx, h->root, name, key);
187 }
188
189 static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, struct registry_key *parent, int n, struct registry_value **value)  
190 {
191         struct rpc_key_data *mykeydata = parent->backend_data;
192         WERROR error;
193         struct winreg_EnumValue r;
194         uint32 type, len1, zero = 0;
195         NTSTATUS status;
196         uint8_t buf8;
197         uint16_t buf16;
198         
199         if(mykeydata->num_values == -1) {
200                 error = rpc_query_key(parent);
201                 if(!W_ERROR_IS_OK(error)) return error;
202         }
203
204         len1 = mykeydata->max_valdatalen;
205         
206         r.in.handle = &mykeydata->pol;
207         r.in.enum_index = n;
208         r.in.name_in.length = 0;
209         r.in.name_in.size = mykeydata->max_valnamelen * 2;
210         r.in.name_in.name = &buf16;
211         r.in.type = &type;
212         r.in.value = &buf8;
213         r.in.length = &zero;
214         r.in.size = &len1;
215         r.out.type = &type;
216
217         
218         status = dcerpc_winreg_EnumValue((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
219         if(NT_STATUS_IS_ERR(status)) {
220                 DEBUG(0, ("Error in EnumValue: %s\n", nt_errstr(status)));
221                 return WERR_GENERAL_FAILURE;
222         }
223         
224         if(NT_STATUS_IS_OK(status) && 
225            W_ERROR_IS_OK(r.out.result) && r.out.length) {
226                 *value = talloc_p(mem_ctx, struct registry_value);
227                 (*value)->parent = parent;
228                 (*value)->name = talloc_strdup(mem_ctx, r.out.name_out.name);
229                 (*value)->data_type = type;
230                 (*value)->data_len = *r.out.length;
231                 (*value)->data_blk = talloc_memdup(mem_ctx, r.out.value, *r.out.length);
232                 return WERR_OK;
233         }
234         
235         return r.out.result;
236 }
237
238 static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, struct registry_key *parent, int n, struct registry_key **subkey) 
239 {
240         struct winreg_EnumKey r;
241         struct winreg_EnumKeyNameRequest keyname;
242         struct winreg_String classname;
243         struct winreg_Time tm;
244         struct rpc_key_data *mykeydata = parent->backend_data;
245         NTSTATUS status;
246
247         r.in.handle = &mykeydata->pol;
248         keyname.unknown = 0x0000020a;
249         init_winreg_String(&keyname.key_name, NULL);
250         init_winreg_String(&classname, NULL);
251         r.in.in_name = &keyname;
252         r.in.class = &classname;
253         tm.low = tm.high = 0x7fffffff;
254         r.in.last_changed_time = &tm;
255
256         r.in.enum_index = n;
257         r.in.unknown = r.out.unknown = 0x0414;
258         r.in.key_name_len = r.out.key_name_len = 0;
259         status = dcerpc_winreg_EnumKey((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
260         if(NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
261                         return rpc_open_rel_key(mem_ctx, parent, talloc_strdup(mem_ctx, r.out.out_name->name), subkey);
262         }
263
264         return r.out.result;
265 }
266
267 static WERROR rpc_add_key(TALLOC_CTX *mem_ctx, struct registry_key *parent, const char *name, uint32_t access_mask, SEC_DESC *sec, struct registry_key **key)
268 {
269         return WERR_NOT_SUPPORTED;
270 }
271
272 static WERROR rpc_query_key(struct registry_key *k)
273 {
274     NTSTATUS status;
275     struct winreg_QueryInfoKey r;
276     struct rpc_key_data *mykeydata = k->backend_data;
277         TALLOC_CTX *mem_ctx = talloc_init("query_key");
278
279     init_winreg_String(&r.in.class, NULL);
280     r.in.handle = &mykeydata->pol;
281         
282     status = dcerpc_winreg_QueryInfoKey((struct dcerpc_pipe *)(k->hive->backend_data), mem_ctx, &r);
283         talloc_destroy(mem_ctx);
284
285     if (!NT_STATUS_IS_OK(status)) {
286         DEBUG(1, ("QueryInfoKey failed - %s\n", nt_errstr(status)));
287         return ntstatus_to_werror(status);
288     }
289                                                                                                        
290     if (W_ERROR_IS_OK(r.out.result)) {
291                 mykeydata->num_subkeys = r.out.num_subkeys;
292                 mykeydata->num_values = r.out.num_values;
293                 mykeydata->max_valnamelen = r.out.max_valnamelen;
294                 mykeydata->max_valdatalen = r.out.max_valbufsize;
295         } 
296
297         return r.out.result;
298 }
299
300 static WERROR rpc_del_key(struct registry_key *k)
301 {
302         NTSTATUS status;
303         struct rpc_key_data *mykeydata = k->backend_data;
304         struct winreg_DeleteKey r;
305         struct registry_key *parent;
306         WERROR error;
307         TALLOC_CTX *mem_ctx = talloc_init("del_key");
308         
309         error = reg_key_get_parent(mem_ctx, k, &parent);
310         if(!W_ERROR_IS_OK(error)) { 
311                 talloc_destroy(mem_ctx); 
312                 return error; 
313         }
314
315         mykeydata = parent->backend_data;
316
317     r.in.handle = &mykeydata->pol;
318     init_winreg_String(&r.in.key, k->name);
319  
320     status = dcerpc_winreg_DeleteKey((struct dcerpc_pipe *)k->hive->backend_data, mem_ctx, &r);
321
322         talloc_destroy(mem_ctx);
323
324         return r.out.result;
325 }
326
327 static WERROR rpc_num_values(struct registry_key *key, int *count) {
328         struct rpc_key_data *mykeydata = key->backend_data;
329         WERROR error;
330                 
331         if(mykeydata->num_values == -1) {
332                 error = rpc_query_key(key);
333                 if(!W_ERROR_IS_OK(error)) return error;
334         }
335                         
336         *count = mykeydata->num_values;
337         return WERR_OK;
338 }
339
340 static WERROR rpc_num_subkeys(struct registry_key *key, int *count) {
341         struct rpc_key_data *mykeydata = key->backend_data;
342         WERROR error;
343
344         if(mykeydata->num_subkeys == -1) {
345                 error = rpc_query_key(key);
346                 if(!W_ERROR_IS_OK(error)) return error;
347         }
348                         
349         *count = mykeydata->num_subkeys;
350         return WERR_OK;
351 }
352
353 static struct hive_operations reg_backend_rpc = {
354         .name = "rpc",
355         .open_key = rpc_open_key,
356         .get_subkey_by_index = rpc_get_subkey_by_index,
357         .get_value_by_index = rpc_get_value_by_index,
358         .add_key = rpc_add_key,
359         .del_key = rpc_del_key,
360         .num_subkeys = rpc_num_subkeys,
361         .num_values = rpc_num_values,
362 };
363
364 WERROR reg_open_remote (struct registry_context **ctx, const char *user, const char *pass, const char *location)
365 {
366         NTSTATUS status;
367         struct dcerpc_pipe *p;
368
369         *ctx = talloc_p(NULL, struct registry_context);
370
371         /* Default to local smbd if no connection is specified */
372         if (!location) {
373                 location = talloc_strdup(ctx, "ncalrpc:");
374         }
375
376         status = dcerpc_pipe_connect(&p, location, 
377                                      DCERPC_WINREG_UUID,
378                                      DCERPC_WINREG_VERSION,
379                                      lp_workgroup(),
380                                      user, pass);
381         (*ctx)->backend_data = p;
382
383         if(NT_STATUS_IS_ERR(status)) {
384                 DEBUG(1, ("Unable to open '%s': %s\n", location, nt_errstr(status)));
385                 return ntstatus_to_werror(status);
386         }
387
388         (*ctx)->get_hive = rpc_get_hive;
389
390         talloc_set_destructor(*ctx, rpc_close);
391
392         return WERR_OK;
393 }
394
395 NTSTATUS registry_rpc_init(void)
396 {
397         return registry_register(&reg_backend_rpc);
398 }