5240cf5696364721ff8b955d873eace089c45ffa
[kamenim/samba.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_t 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_t 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 #if 0
126 static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k)
127 {
128     struct winreg_OpenKey r;
129         struct rpc_key_data *mykeydata;
130
131         k->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data);
132         mykeydata->num_values = -1;
133         mykeydata->num_subkeys = -1;
134
135         /* Then, open the handle using the hive */
136
137         memset(&r, 0, sizeof(struct winreg_OpenKey));
138     r.in.handle = &(((struct rpc_key_data *)k->hive->root->backend_data)->pol);
139     init_winreg_String(&r.in.keyname, k->path);
140     r.in.unknown = 0x00000000;
141     r.in.access_mask = 0x02000000;
142     r.out.handle = &mykeydata->pol;
143
144     dcerpc_winreg_OpenKey((struct dcerpc_pipe *)k->hive->backend_data, mem_ctx, &r);
145
146         return r.out.result;
147 }
148 #endif
149
150 static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h, const char *name, struct registry_key **key)
151 {
152         struct rpc_key_data *mykeydata;
153     struct winreg_OpenKey r;
154
155         *key = talloc(mem_ctx, struct registry_key);
156         (*key)->name = talloc_strdup(mem_ctx, name);
157
158         (*key)->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data);
159         mykeydata->num_values = -1;
160         mykeydata->num_subkeys = -1;
161
162         /* Then, open the handle using the hive */
163
164         memset(&r, 0, sizeof(struct winreg_OpenKey));
165     r.in.handle = &(((struct rpc_key_data *)h->backend_data)->pol);
166     init_winreg_String(&r.in.keyname, name);
167     r.in.unknown = 0x00000000;
168     r.in.access_mask = 0x02000000;
169     r.out.handle = &mykeydata->pol;
170
171     dcerpc_winreg_OpenKey((struct dcerpc_pipe *)(h->hive->backend_data), mem_ctx, &r);
172
173         return r.out.result;
174 }
175
176 static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, struct registry_key *parent, int n, struct registry_value **value)  
177 {
178         struct rpc_key_data *mykeydata = parent->backend_data;
179         WERROR error;
180         struct winreg_EnumValue r;
181         uint32_t type, len1, zero = 0;
182         NTSTATUS status;
183         uint8_t buf8;
184         uint16_t buf16;
185         
186         if(mykeydata->num_values == -1) {
187                 error = rpc_query_key(parent);
188                 if(!W_ERROR_IS_OK(error)) return error;
189         }
190
191         len1 = mykeydata->max_valdatalen;
192         
193         r.in.handle = &mykeydata->pol;
194         r.in.enum_index = n;
195         r.in.name_in.length = 0;
196         r.in.name_in.size = mykeydata->max_valnamelen * 2;
197         r.in.name_in.name = &buf16;
198         r.in.type = &type;
199         r.in.value = &buf8;
200         r.in.length = &zero;
201         r.in.size = &len1;
202         r.out.type = &type;
203
204         
205         status = dcerpc_winreg_EnumValue((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
206         if(NT_STATUS_IS_ERR(status)) {
207                 DEBUG(0, ("Error in EnumValue: %s\n", nt_errstr(status)));
208                 return WERR_GENERAL_FAILURE;
209         }
210         
211         if(NT_STATUS_IS_OK(status) && 
212            W_ERROR_IS_OK(r.out.result) && r.out.length) {
213                 *value = talloc(mem_ctx, struct registry_value);
214                 (*value)->name = talloc_strdup(mem_ctx, r.out.name_out.name);
215                 (*value)->data_type = type;
216                 (*value)->data_len = *r.out.length;
217                 (*value)->data_blk = talloc_memdup(mem_ctx, r.out.value, *r.out.length);
218                 return WERR_OK;
219         }
220         
221         return r.out.result;
222 }
223
224 static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, struct registry_key *parent, int n, struct registry_key **subkey) 
225 {
226         struct winreg_EnumKey r;
227         struct winreg_EnumKeyNameRequest keyname;
228         struct winreg_String classname;
229         struct winreg_Time tm;
230         struct rpc_key_data *mykeydata = parent->backend_data;
231         NTSTATUS status;
232
233         r.in.handle = &mykeydata->pol;
234         keyname.unknown = 0x0000020a;
235         init_winreg_String(&keyname.key_name, NULL);
236         init_winreg_String(&classname, NULL);
237         r.in.in_name = &keyname;
238         r.in.class = &classname;
239         tm.low = tm.high = 0x7fffffff;
240         r.in.last_changed_time = &tm;
241
242         r.in.enum_index = n;
243         r.in.unknown = r.out.unknown = 0x0414;
244         r.in.key_name_len = r.out.key_name_len = 0;
245         status = dcerpc_winreg_EnumKey((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
246         if(NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
247                         return rpc_open_key(mem_ctx, parent, talloc_strdup(mem_ctx, r.out.out_name->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, const char *location)
366 {
367         NTSTATUS status;
368         struct dcerpc_pipe *p;
369
370         *ctx = talloc(NULL, struct registry_context);
371
372         /* Default to local smbd if no connection is specified */
373         if (!location) {
374                 location = talloc_strdup(ctx, "ncalrpc:");
375         }
376
377         status = dcerpc_pipe_connect(*ctx /* TALLOC_CTX */, 
378                                      &p, location, 
379                                      DCERPC_WINREG_UUID,
380                                      DCERPC_WINREG_VERSION,
381                                      credentials);
382         (*ctx)->backend_data = p;
383
384         if(NT_STATUS_IS_ERR(status)) {
385                 DEBUG(1, ("Unable to open '%s': %s\n", location, nt_errstr(status)));
386                 talloc_free(*ctx);
387                 *ctx = NULL;
388                 return ntstatus_to_werror(status);
389         }
390
391         (*ctx)->get_predefined_key = rpc_get_predefined_key;
392
393         return WERR_OK;
394 }
395
396 NTSTATUS registry_rpc_init(void)
397 {
398         return registry_register(&reg_backend_rpc);
399 }