r24665: Close file handles properly.
[ira/wip.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 3 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, see <http://www.gnu.org/licenses/>.  */
18  
19 #include "includes.h"
20 #include "registry.h"
21 #include "librpc/gen_ndr/ndr_winreg_c.h"
22
23 static struct hive_operations reg_backend_rpc;
24
25 /**
26  * This is the RPC backend for the registry library.
27  */
28
29 static void init_winreg_String(struct winreg_String *name, const char *s)
30 {
31         name->name = s;
32 }
33
34
35 #define openhive(u) static WERROR open_ ## u(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *hnd) \
36 { \
37         struct winreg_Open ## u r; \
38         NTSTATUS status; \
39         \
40         r.in.system_name = NULL; \
41         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED; \
42         r.out.handle = hnd;\
43         \
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);\
48         }\
49 \
50         return r.out.result;\
51 }
52
53 openhive(HKLM)
54 openhive(HKCU)
55 openhive(HKPD)
56 openhive(HKU)
57 openhive(HKCR)
58 openhive(HKDD)
59 openhive(HKCC)
60
61 struct rpc_key_data {
62         struct policy_handle pol;
63         int num_subkeys;
64         int num_values;
65         int max_valnamelen;
66         int max_valdatalen;
67 };
68
69 static struct {
70         uint32_t hkey;
71         WERROR (*open) (struct dcerpc_pipe *p, TALLOC_CTX *, struct policy_handle *h);
72 } known_hives[] = {
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 },
80 { 0, NULL }
81 };
82
83 static WERROR rpc_query_key(const struct registry_key *k);
84
85 static WERROR rpc_get_predefined_key (struct registry_context *ctx, uint32_t hkey_type, struct registry_key **k)
86 {
87         int n;
88         struct registry_hive *h;
89         struct rpc_key_data *mykeydata;
90
91         for(n = 0; known_hives[n].hkey; n++) 
92         {
93                 if(known_hives[n].hkey == hkey_type) break;
94         }
95         
96         if(!known_hives[n].open)  {
97                 DEBUG(1, ("No such hive %d\n", hkey_type));
98                 return WERR_NO_MORE_ITEMS;
99         }
100
101         h = talloc(ctx, struct registry_hive);
102         h->functions = &reg_backend_rpc;
103         h->location = NULL;
104         h->backend_data = ctx->backend_data;
105         
106         (*k) = h->root = talloc(h, struct registry_key);
107         (*k)->hive = h;
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));
112 }
113
114 #if 0
115 static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k)
116 {
117     struct winreg_OpenKey r;
118         struct rpc_key_data *mykeydata;
119
120         k->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data);
121         mykeydata->num_values = -1;
122         mykeydata->num_subkeys = -1;
123
124         /* Then, open the handle using the hive */
125
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;
132
133     dcerpc_winreg_OpenKey((struct dcerpc_pipe *)k->hive->backend_data, mem_ctx, &r);
134
135         return r.out.result;
136 }
137 #endif
138
139 static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, const struct registry_key *h, const char *name, struct registry_key **key)
140 {
141         struct rpc_key_data *mykeydata;
142     struct winreg_OpenKey r;
143
144         *key = talloc(mem_ctx, struct registry_key);
145         (*key)->name = talloc_strdup(mem_ctx, name);
146
147         (*key)->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data);
148         mykeydata->num_values = -1;
149         mykeydata->num_subkeys = -1;
150
151         /* Then, open the handle using the hive */
152
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;
159
160     dcerpc_winreg_OpenKey((struct dcerpc_pipe *)(h->hive->backend_data), mem_ctx, &r);
161
162         return r.out.result;
163 }
164
165 static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, const struct registry_key *parent, int n, struct registry_value **value)  
166 {
167         struct rpc_key_data *mykeydata = parent->backend_data;
168         WERROR error;
169         struct winreg_EnumValue r;
170         uint32_t len1, zero = 0;
171         enum winreg_Type type;
172         NTSTATUS status;
173         struct winreg_StringBuf name;
174         uint8_t u8;
175         
176         if(mykeydata->num_values == -1) {
177                 error = rpc_query_key(parent);
178                 if(!W_ERROR_IS_OK(error)) return error;
179         }
180
181         len1 = mykeydata->max_valdatalen;
182         
183         name.length = 0;
184         name.size   = mykeydata->max_valnamelen * 2;
185         name.name   = "";
186
187         r.in.handle = &mykeydata->pol;
188         r.in.enum_index = n;
189         r.in.name = &name;
190         r.in.type = &type;
191         r.in.value = &u8;
192         r.in.length = &zero;
193         r.in.size = &len1;
194         r.out.name = &name;
195         
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;
200         }
201         
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);
208                 return WERR_OK;
209         }
210         
211         return r.out.result;
212 }
213
214 static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, const struct registry_key *parent, int n, struct registry_key **subkey) 
215 {
216         struct winreg_EnumKey r;
217         struct rpc_key_data *mykeydata = parent->backend_data;
218         NTSTATUS status;
219         struct winreg_StringBuf namebuf, classbuf;
220         NTTIME change_time = 0;
221
222         namebuf.length = 0;
223         namebuf.size   = 1024;
224         namebuf.name   = NULL;
225         classbuf.length = 0;
226         classbuf.size   = 0;
227         classbuf.name   = NULL;
228
229         r.in.handle = &mykeydata->pol;
230         r.in.enum_index = n;
231         r.in.name = &namebuf;
232         r.in.keyclass = &classbuf;
233         r.in.last_changed_time = &change_time;
234         r.out.name = &namebuf;
235
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);
240         }
241
242         return r.out.result;
243 }
244
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)
246 {
247         NTSTATUS status;
248         struct winreg_CreateKey r;
249
250         init_winreg_String(&r.in.name, name);
251         init_winreg_String(&r.in.keyclass, NULL);
252
253         r.in.handle = parent->backend_data;
254         r.out.new_handle = talloc(mem_ctx, struct policy_handle);       
255         r.in.options = 0;
256         r.in.access_mask = access_mask;
257         r.in.secdesc = NULL;
258
259         status = dcerpc_winreg_CreateKey((struct dcerpc_pipe *)(parent->hive->backend_data), mem_ctx, &r);
260
261     if (!NT_STATUS_IS_OK(status)) {
262         DEBUG(1, ("CreateKey failed - %s\n", nt_errstr(status)));
263         return ntstatus_to_werror(status);
264     }
265
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;
270         }
271
272         return r.out.result;
273 }
274
275 static WERROR rpc_query_key(const struct registry_key *k)
276 {
277     NTSTATUS status;
278     struct winreg_QueryInfoKey r;
279     struct rpc_key_data *mykeydata = k->backend_data;
280         TALLOC_CTX *mem_ctx = talloc_init("query_key");
281
282         r.in.classname = talloc(mem_ctx, struct winreg_String);
283     init_winreg_String(r.in.classname, NULL);
284     r.in.handle = &mykeydata->pol;
285         
286     status = dcerpc_winreg_QueryInfoKey((struct dcerpc_pipe *)(k->hive->backend_data), mem_ctx, &r);
287         talloc_free(mem_ctx);
288
289     if (!NT_STATUS_IS_OK(status)) {
290         DEBUG(1, ("QueryInfoKey failed - %s\n", nt_errstr(status)));
291         return ntstatus_to_werror(status);
292     }
293                                                                                                        
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;
299         } 
300
301         return r.out.result;
302 }
303
304 static WERROR rpc_del_key(const struct registry_key *parent, const char *name)
305 {
306         NTSTATUS status;
307         struct rpc_key_data *mykeydata = parent->backend_data;
308         struct winreg_DeleteKey r;
309         TALLOC_CTX *mem_ctx = talloc_init("del_key");
310         
311     r.in.handle = &mykeydata->pol;
312     init_winreg_String(&r.in.key, name);
313  
314     status = dcerpc_winreg_DeleteKey((struct dcerpc_pipe *)parent->hive->backend_data, mem_ctx, &r);
315
316         talloc_free(mem_ctx);
317
318         return r.out.result;
319 }
320
321 static WERROR rpc_num_values(const struct registry_key *key, uint32_t *count) 
322 {
323         struct rpc_key_data *mykeydata = key->backend_data;
324         WERROR error;
325                 
326         if(mykeydata->num_values == -1) {
327                 error = rpc_query_key(key);
328                 if(!W_ERROR_IS_OK(error)) return error;
329         }
330                         
331         *count = mykeydata->num_values;
332         return WERR_OK;
333 }
334
335 static WERROR rpc_num_subkeys(const struct registry_key *key, uint32_t *count) 
336 {
337         struct rpc_key_data *mykeydata = key->backend_data;
338         WERROR error;
339
340         if(mykeydata->num_subkeys == -1) {
341                 error = rpc_query_key(key);
342                 if(!W_ERROR_IS_OK(error)) return error;
343         }
344                         
345         *count = mykeydata->num_subkeys;
346         return WERR_OK;
347 }
348
349 static struct hive_operations reg_backend_rpc = {
350         .name = "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,
358 };
359
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)
362 {
363         NTSTATUS status;
364         struct dcerpc_pipe *p;
365
366         *ctx = talloc(NULL, struct registry_context);
367
368         /* Default to local smbd if no connection is specified */
369         if (!location) {
370                 location = talloc_strdup(ctx, "ncalrpc:");
371         }
372
373         status = dcerpc_pipe_connect(*ctx /* TALLOC_CTX */, 
374                                      &p, location, 
375                                          &ndr_table_winreg,
376                                      credentials, ev);
377         (*ctx)->backend_data = p;
378
379         if(NT_STATUS_IS_ERR(status)) {
380                 DEBUG(1, ("Unable to open '%s': %s\n", location, nt_errstr(status)));
381                 talloc_free(*ctx);
382                 *ctx = NULL;
383                 return ntstatus_to_werror(status);
384         }
385
386         (*ctx)->get_predefined_key = rpc_get_predefined_key;
387
388         return WERR_OK;
389 }
390
391 NTSTATUS registry_rpc_init(void)
392 {
393         dcerpc_init();
394         return registry_register(&reg_backend_rpc);
395 }