r5037: got rid of all of the TALLOC_DEPRECATED stuff. My apologies for the
[nivanova/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_predefined_key (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(ctx, struct registry_hive);
113         h->functions = &reg_backend_rpc;
114         h->location = NULL;
115         h->backend_data = ctx->backend_data;
116         
117         (*k) = h->root = talloc(h, struct registry_key);
118         (*k)->hive = h;
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));
123 }
124
125 static int rpc_close (void *_h)
126 {
127         struct registry_context *h = _h;
128         dcerpc_pipe_close(h->backend_data);
129         return 0;
130 }
131
132 #if 0
133 static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k)
134 {
135     struct winreg_OpenKey r;
136         struct rpc_key_data *mykeydata;
137
138         k->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data);
139         mykeydata->num_values = -1;
140         mykeydata->num_subkeys = -1;
141
142         /* Then, open the handle using the hive */
143
144         memset(&r, 0, sizeof(struct winreg_OpenKey));
145     r.in.handle = &(((struct rpc_key_data *)k->hive->root->backend_data)->pol);
146     init_winreg_String(&r.in.keyname, k->path);
147     r.in.unknown = 0x00000000;
148     r.in.access_mask = 0x02000000;
149     r.out.handle = &mykeydata->pol;
150
151     dcerpc_winreg_OpenKey((struct dcerpc_pipe *)k->hive->backend_data, mem_ctx, &r);
152
153         return r.out.result;
154 }
155 #endif
156
157 static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h, const char *name, struct registry_key **key)
158 {
159         struct rpc_key_data *mykeydata;
160     struct winreg_OpenKey r;
161
162         *key = talloc(mem_ctx, struct registry_key);
163         (*key)->name = talloc_strdup(mem_ctx, name);
164
165         (*key)->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data);
166         mykeydata->num_values = -1;
167         mykeydata->num_subkeys = -1;
168
169         /* Then, open the handle using the hive */
170
171         memset(&r, 0, sizeof(struct winreg_OpenKey));
172     r.in.handle = &(((struct rpc_key_data *)h->backend_data)->pol);
173     init_winreg_String(&r.in.keyname, name);
174     r.in.unknown = 0x00000000;
175     r.in.access_mask = 0x02000000;
176     r.out.handle = &mykeydata->pol;
177
178     dcerpc_winreg_OpenKey((struct dcerpc_pipe *)(h->hive->backend_data), mem_ctx, &r);
179
180         return r.out.result;
181 }
182
183 static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, struct registry_key *parent, int n, struct registry_value **value)  
184 {
185         struct rpc_key_data *mykeydata = parent->backend_data;
186         WERROR error;
187         struct winreg_EnumValue r;
188         uint32 type, len1, zero = 0;
189         NTSTATUS status;
190         uint8_t buf8;
191         uint16_t buf16;
192         
193         if(mykeydata->num_values == -1) {
194                 error = rpc_query_key(parent);
195                 if(!W_ERROR_IS_OK(error)) return error;
196         }
197
198         len1 = mykeydata->max_valdatalen;
199         
200         r.in.handle = &mykeydata->pol;
201         r.in.enum_index = n;
202         r.in.name_in.length = 0;
203         r.in.name_in.size = mykeydata->max_valnamelen * 2;
204         r.in.name_in.name = &buf16;
205         r.in.type = &type;
206         r.in.value = &buf8;
207         r.in.length = &zero;
208         r.in.size = &len1;
209         r.out.type = &type;
210
211         
212         status = dcerpc_winreg_EnumValue((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
213         if(NT_STATUS_IS_ERR(status)) {
214                 DEBUG(0, ("Error in EnumValue: %s\n", nt_errstr(status)));
215                 return WERR_GENERAL_FAILURE;
216         }
217         
218         if(NT_STATUS_IS_OK(status) && 
219            W_ERROR_IS_OK(r.out.result) && r.out.length) {
220                 *value = talloc(mem_ctx, struct registry_value);
221                 (*value)->name = talloc_strdup(mem_ctx, r.out.name_out.name);
222                 (*value)->data_type = type;
223                 (*value)->data_len = *r.out.length;
224                 (*value)->data_blk = talloc_memdup(mem_ctx, r.out.value, *r.out.length);
225                 return WERR_OK;
226         }
227         
228         return r.out.result;
229 }
230
231 static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, struct registry_key *parent, int n, struct registry_key **subkey) 
232 {
233         struct winreg_EnumKey r;
234         struct winreg_EnumKeyNameRequest keyname;
235         struct winreg_String classname;
236         struct winreg_Time tm;
237         struct rpc_key_data *mykeydata = parent->backend_data;
238         NTSTATUS status;
239
240         r.in.handle = &mykeydata->pol;
241         keyname.unknown = 0x0000020a;
242         init_winreg_String(&keyname.key_name, NULL);
243         init_winreg_String(&classname, NULL);
244         r.in.in_name = &keyname;
245         r.in.class = &classname;
246         tm.low = tm.high = 0x7fffffff;
247         r.in.last_changed_time = &tm;
248
249         r.in.enum_index = n;
250         r.in.unknown = r.out.unknown = 0x0414;
251         r.in.key_name_len = r.out.key_name_len = 0;
252         status = dcerpc_winreg_EnumKey((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
253         if(NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
254                         return rpc_open_key(mem_ctx, parent, talloc_strdup(mem_ctx, r.out.out_name->name), subkey);
255         }
256
257         return r.out.result;
258 }
259
260 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)
261 {
262         NTSTATUS status;
263         struct winreg_CreateKey r;
264
265         init_winreg_String(&r.in.key, name);
266         init_winreg_String(&r.in.class, NULL);
267
268         r.in.handle = parent->backend_data;
269         r.out.handle = talloc(mem_ctx, struct policy_handle);   
270         r.in.options = 0;
271         r.in.access_mask = access_mask;
272         r.in.sec_desc = NULL;
273
274         status = dcerpc_winreg_CreateKey((struct dcerpc_pipe *)(parent->hive->backend_data), mem_ctx, &r);
275
276     if (!NT_STATUS_IS_OK(status)) {
277         DEBUG(1, ("CreateKey failed - %s\n", nt_errstr(status)));
278         return ntstatus_to_werror(status);
279     }
280
281         if (W_ERROR_IS_OK(r.out.result)) {
282                 *key = talloc(mem_ctx, struct registry_key);
283                 (*key)->name = talloc_strdup(*key, name);
284                 (*key)->backend_data = r.out.handle;
285         }
286
287         return r.out.result;
288 }
289
290 static WERROR rpc_query_key(struct registry_key *k)
291 {
292     NTSTATUS status;
293     struct winreg_QueryInfoKey r;
294     struct rpc_key_data *mykeydata = k->backend_data;
295         TALLOC_CTX *mem_ctx = talloc_init("query_key");
296
297     init_winreg_String(&r.in.class, NULL);
298     r.in.handle = &mykeydata->pol;
299         
300     status = dcerpc_winreg_QueryInfoKey((struct dcerpc_pipe *)(k->hive->backend_data), mem_ctx, &r);
301         talloc_free(mem_ctx);
302
303     if (!NT_STATUS_IS_OK(status)) {
304         DEBUG(1, ("QueryInfoKey failed - %s\n", nt_errstr(status)));
305         return ntstatus_to_werror(status);
306     }
307                                                                                                        
308     if (W_ERROR_IS_OK(r.out.result)) {
309                 mykeydata->num_subkeys = r.out.num_subkeys;
310                 mykeydata->num_values = r.out.num_values;
311                 mykeydata->max_valnamelen = r.out.max_valnamelen;
312                 mykeydata->max_valdatalen = r.out.max_valbufsize;
313         } 
314
315         return r.out.result;
316 }
317
318 static WERROR rpc_del_key(struct registry_key *parent, const char *name)
319 {
320         NTSTATUS status;
321         struct rpc_key_data *mykeydata = parent->backend_data;
322         struct winreg_DeleteKey r;
323         TALLOC_CTX *mem_ctx = talloc_init("del_key");
324         
325     r.in.handle = &mykeydata->pol;
326     init_winreg_String(&r.in.key, name);
327  
328     status = dcerpc_winreg_DeleteKey((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
329
330         talloc_free(mem_ctx);
331
332         return r.out.result;
333 }
334
335 static WERROR rpc_num_values(struct registry_key *key, int *count) {
336         struct rpc_key_data *mykeydata = key->backend_data;
337         WERROR error;
338                 
339         if(mykeydata->num_values == -1) {
340                 error = rpc_query_key(key);
341                 if(!W_ERROR_IS_OK(error)) return error;
342         }
343                         
344         *count = mykeydata->num_values;
345         return WERR_OK;
346 }
347
348 static WERROR rpc_num_subkeys(struct registry_key *key, int *count) {
349         struct rpc_key_data *mykeydata = key->backend_data;
350         WERROR error;
351
352         if(mykeydata->num_subkeys == -1) {
353                 error = rpc_query_key(key);
354                 if(!W_ERROR_IS_OK(error)) return error;
355         }
356                         
357         *count = mykeydata->num_subkeys;
358         return WERR_OK;
359 }
360
361 static struct hive_operations reg_backend_rpc = {
362         .name = "rpc",
363         .open_key = rpc_open_key,
364         .get_subkey_by_index = rpc_get_subkey_by_index,
365         .get_value_by_index = rpc_get_value_by_index,
366         .add_key = rpc_add_key,
367         .del_key = rpc_del_key,
368         .num_subkeys = rpc_num_subkeys,
369         .num_values = rpc_num_values,
370 };
371
372 WERROR reg_open_remote (struct registry_context **ctx, const char *user, const char *pass, const char *location)
373 {
374         NTSTATUS status;
375         struct dcerpc_pipe *p;
376
377         *ctx = talloc(NULL, struct registry_context);
378
379         /* Default to local smbd if no connection is specified */
380         if (!location) {
381                 location = talloc_strdup(ctx, "ncalrpc:");
382         }
383
384         status = dcerpc_pipe_connect(&p, location, 
385                                      DCERPC_WINREG_UUID,
386                                      DCERPC_WINREG_VERSION,
387                                      lp_workgroup(),
388                                      user, pass);
389         (*ctx)->backend_data = p;
390
391         if(NT_STATUS_IS_ERR(status)) {
392                 DEBUG(1, ("Unable to open '%s': %s\n", location, nt_errstr(status)));
393                 return ntstatus_to_werror(status);
394         }
395
396         (*ctx)->get_predefined_key = rpc_get_predefined_key;
397
398         talloc_set_destructor(*ctx, rpc_close);
399
400         return WERR_OK;
401 }
402
403 NTSTATUS registry_rpc_init(void)
404 {
405         return registry_register(&reg_backend_rpc);
406 }