r24718: Fix some compiler warnings.
[ira/wip.git] / source / lib / registry / rpc.c
1 /*
2    Samba Unix/Linux SMB implementation
3    RPC backend for the registry library
4    Copyright (C) 2003-2007 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 struct rpc_key {
24         struct registry_key key;
25         struct policy_handle pol;
26         struct dcerpc_pipe *pipe;
27
28         uint32_t num_values;
29         uint32_t num_subkeys;
30         uint32_t max_valnamelen;
31         uint32_t max_valdatalen;
32 };
33
34 struct rpc_registry_context {
35         struct registry_context context;
36         struct dcerpc_pipe *pipe;
37 };
38
39 static struct registry_operations reg_backend_rpc;
40
41 /**
42  * This is the RPC backend for the registry library.
43  */
44
45 static void init_winreg_String(struct winreg_String *name, const char *s)
46 {
47         name->name = s;
48 }
49
50
51 #define openhive(u) static WERROR open_ ## u(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *hnd) \
52 { \
53         struct winreg_Open ## u r; \
54         NTSTATUS status; \
55         \
56         r.in.system_name = NULL; \
57         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED; \
58         r.out.handle = hnd;\
59         \
60         status = dcerpc_winreg_Open ## u(p, mem_ctx, &r); \
61         if (NT_STATUS_IS_ERR(status)) {\
62                 DEBUG(0,("Error executing open\n"));\
63                 return ntstatus_to_werror(status);\
64         }\
65 \
66         return r.out.result;\
67 }
68
69 openhive(HKLM)
70 openhive(HKCU)
71 openhive(HKPD)
72 openhive(HKU)
73 openhive(HKCR)
74 openhive(HKDD)
75 openhive(HKCC)
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(const struct registry_key *k);
92
93 static WERROR rpc_get_predefined_key(const struct registry_context *ctx, 
94                                                                          uint32_t hkey_type, 
95                                                                          struct registry_key **k)
96 {
97         int n;
98         struct rpc_registry_context *rctx = talloc_get_type(ctx, 
99                                                                                                 struct rpc_registry_context);
100         struct rpc_key *mykeydata;
101
102         for(n = 0; known_hives[n].hkey; n++) {
103                 if(known_hives[n].hkey == hkey_type) 
104                         break;
105         }
106         
107         if (known_hives[n].open == NULL)  {
108                 DEBUG(1, ("No such hive %d\n", hkey_type));
109                 return WERR_NO_MORE_ITEMS;
110         }
111
112         mykeydata = talloc(ctx, struct rpc_key);
113         mykeydata->pipe = rctx->pipe;
114         mykeydata->num_values = -1;
115         mykeydata->num_subkeys = -1;
116         return known_hives[n].open(mykeydata->pipe, *k, &(mykeydata->pol));
117 }
118
119 #if 0
120 static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k)
121 {
122     struct winreg_OpenKey r;
123         struct rpc_key_data *mykeydata;
124
125         k->backend_data = mykeydata = talloc(mem_ctx, struct rpc_key_data);
126         mykeydata->num_values = -1;
127         mykeydata->num_subkeys = -1;
128
129         /* Then, open the handle using the hive */
130
131         memset(&r, 0, sizeof(struct winreg_OpenKey));
132     r.in.handle = &(((struct rpc_key_data *)k->hive->root->backend_data)->pol);
133     init_winreg_String(&r.in.keyname, k->path);
134     r.in.unknown = 0x00000000;
135     r.in.access_mask = 0x02000000;
136     r.out.handle = &mykeydata->pol;
137
138     dcerpc_winreg_OpenKey((struct dcerpc_pipe *)k->hive->backend_data, mem_ctx, &r);
139
140         return r.out.result;
141 }
142 #endif
143
144 static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h, 
145                                                    const char *name, struct registry_key **key)
146 {
147         struct rpc_key *mykeydata = talloc_get_type(h, struct rpc_key),
148                                    *newkeydata;
149     struct winreg_OpenKey r;
150
151         mykeydata = talloc(mem_ctx, struct rpc_key);
152
153         /* Then, open the handle using the hive */
154         memset(&r, 0, sizeof(struct winreg_OpenKey));
155     r.in.parent_handle = &mykeydata->pol;
156     init_winreg_String(&r.in.keyname, name);
157     r.in.unknown = 0x00000000;
158     r.in.access_mask = 0x02000000;
159     r.out.handle = &newkeydata->pol;
160
161     dcerpc_winreg_OpenKey(mykeydata->pipe, mem_ctx, &r);
162
163         return r.out.result;
164 }
165
166 static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx, 
167                                                                          const struct registry_key *parent, 
168                                                                          uint32_t n, 
169                                                                          const char **value_name,
170                                                                          uint32_t *type,
171                                                                          DATA_BLOB *data)
172 {
173         struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key);
174         WERROR error;
175         struct winreg_EnumValue r;
176         uint32_t len1, zero = 0;
177         NTSTATUS status;
178         struct winreg_StringBuf name;
179         uint8_t u8;
180         
181         if (mykeydata->num_values == -1) {
182                 error = rpc_query_key(parent);
183                 if(!W_ERROR_IS_OK(error)) return error;
184         }
185
186         len1 = mykeydata->max_valdatalen;
187         
188         name.length = 0;
189         name.size   = mykeydata->max_valnamelen * 2;
190         name.name   = "";
191
192         r.in.handle = &mykeydata->pol;
193         r.in.enum_index = n;
194         r.in.name = &name;
195         r.in.type = type;
196         r.in.value = &u8;
197         r.in.length = &zero;
198         r.in.size = &len1;
199         r.out.name = &name;
200         
201         status = dcerpc_winreg_EnumValue(mykeydata->pipe, mem_ctx, &r);
202         if(NT_STATUS_IS_ERR(status)) {
203                 DEBUG(0, ("Error in EnumValue: %s\n", nt_errstr(status)));
204                 return WERR_GENERAL_FAILURE;
205         }
206         
207         if(NT_STATUS_IS_OK(status) && 
208            W_ERROR_IS_OK(r.out.result) && r.out.length) {
209                 *value_name = talloc_strdup(mem_ctx, r.out.name->name);
210                 *data = data_blob_talloc(mem_ctx, r.out.value, *r.out.length);
211                 return WERR_OK;
212         }
213         
214         return r.out.result;
215 }
216
217 static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx, 
218                                                                           const struct registry_key *parent, 
219                                                                           uint32_t n, 
220                                                                           const char **name,
221                                                                           const char **keyclass,
222                                                                           NTTIME *last_changed_time)
223 {
224         struct winreg_EnumKey r;
225         struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key);
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.keyclass = &classbuf;
241         r.in.last_changed_time = &change_time;
242         r.out.name = &namebuf;
243
244         status = dcerpc_winreg_EnumKey(mykeydata->pipe, mem_ctx, &r);
245         if(NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
246                 *name = talloc_strdup(mem_ctx, r.out.name->name);
247                 *keyclass = talloc_strdup(mem_ctx, r.out.keyclass->name);
248                 *last_changed_time = *r.out.last_changed_time;
249         }
250
251         return r.out.result;
252 }
253
254 static WERROR rpc_add_key(TALLOC_CTX *mem_ctx, 
255                                                   struct registry_key *parent, const char *name, 
256                                                   const char *key_class,
257                                                   struct security_descriptor *sec, 
258                                                   struct registry_key **key)
259 {
260         NTSTATUS status;
261         struct winreg_CreateKey r;
262         struct rpc_key *parentkd = talloc_get_type(parent, struct rpc_key);
263         struct rpc_key *rpck = talloc(mem_ctx, struct rpc_key);
264
265         init_winreg_String(&r.in.name, name);
266         init_winreg_String(&r.in.keyclass, NULL);
267
268         r.in.handle = &parentkd->pol;
269         r.out.new_handle = &rpck->pol;
270         r.in.options = 0;
271         r.in.access_mask = SEC_STD_ALL;
272         r.in.secdesc = NULL;
273
274         status = dcerpc_winreg_CreateKey(parentkd->pipe, mem_ctx, &r);
275
276     if (!NT_STATUS_IS_OK(status)) {
277                 talloc_free(rpck);
278         DEBUG(1, ("CreateKey failed - %s\n", nt_errstr(status)));
279         return ntstatus_to_werror(status);
280     }
281
282         if (W_ERROR_IS_OK(r.out.result)) {
283                 rpck->pipe = talloc_reference(rpck, parentkd->pipe);
284                 *key = (struct registry_key *)rpck;
285         }
286
287         return r.out.result;
288 }
289
290 static WERROR rpc_query_key(const struct registry_key *k)
291 {
292     NTSTATUS status;
293     struct winreg_QueryInfoKey r;
294     struct rpc_key *mykeydata = talloc_get_type(k, struct rpc_key);
295         TALLOC_CTX *mem_ctx = talloc_init("query_key");
296
297         r.in.classname = talloc(mem_ctx, struct winreg_String);
298     init_winreg_String(r.in.classname, NULL);
299     r.in.handle = &mykeydata->pol;
300         
301     status = dcerpc_winreg_QueryInfoKey(mykeydata->pipe, mem_ctx, &r);
302         talloc_free(mem_ctx);
303
304     if (!NT_STATUS_IS_OK(status)) {
305         DEBUG(1, ("QueryInfoKey failed - %s\n", nt_errstr(status)));
306         return ntstatus_to_werror(status);
307     }
308                                                                                                        
309     if (W_ERROR_IS_OK(r.out.result)) {
310                 mykeydata->num_subkeys = *r.out.num_subkeys;
311                 mykeydata->num_values = *r.out.num_values;
312                 mykeydata->max_valnamelen = *r.out.max_valnamelen;
313                 mykeydata->max_valdatalen = *r.out.max_valbufsize;
314         } 
315
316         return r.out.result;
317 }
318
319 static WERROR rpc_del_key(struct registry_key *parent, const char *name)
320 {
321         NTSTATUS status;
322         struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key);
323         struct winreg_DeleteKey r;
324         TALLOC_CTX *mem_ctx = talloc_init("del_key");
325         
326     r.in.handle = &mykeydata->pol;
327     init_winreg_String(&r.in.key, name);
328  
329     status = dcerpc_winreg_DeleteKey(mykeydata->pipe, mem_ctx, &r);
330
331         talloc_free(mem_ctx);
332
333         return r.out.result;
334 }
335
336 static WERROR rpc_get_info(TALLOC_CTX *mem_ctx, const struct registry_key *key,
337                                                    const char **classname, 
338                                                    uint32_t *numsubkeys,
339                                                    uint32_t *numvalue,
340                                                    NTTIME *last_changed_time)
341 {
342         struct rpc_key *mykeydata = talloc_get_type(key, struct rpc_key);
343         WERROR error;
344                 
345         if(mykeydata->num_values == -1) {
346                 error = rpc_query_key(key);
347                 if(!W_ERROR_IS_OK(error)) return error;
348         }
349                         
350         /* FIXME: *classname = talloc_strdup(mem_ctx, mykeydata->classname); */
351         /* FIXME: *last_changed_time = mykeydata->last_changed_time */
352
353         if (numvalue != NULL)
354                 *numvalue = mykeydata->num_values;
355
356         if (numsubkeys != NULL)
357                 *numsubkeys = mykeydata->num_subkeys;
358
359         return WERR_OK;
360 }
361
362 static struct registry_operations reg_backend_rpc = {
363         .name = "rpc",
364         .open_key = rpc_open_key,
365         .enum_key = rpc_get_subkey_by_index,
366         .enum_value = rpc_get_value_by_index,
367         .create_key = rpc_add_key,
368         .delete_key = rpc_del_key,
369         .get_key_info = rpc_get_info,
370         .get_predefined_key = rpc_get_predefined_key,
371 };
372
373 _PUBLIC_ WERROR reg_open_remote(struct registry_context **ctx, 
374                                                                 struct auth_session_info *session_info, 
375                                                                 struct cli_credentials *credentials, 
376                                                                 const char *location, struct event_context *ev)
377 {
378         NTSTATUS status;
379         struct dcerpc_pipe *p;
380         struct rpc_registry_context *rctx;
381
382         dcerpc_init();
383
384         rctx = talloc(NULL, struct rpc_registry_context);
385
386         /* Default to local smbd if no connection is specified */
387         if (!location) {
388                 location = talloc_strdup(ctx, "ncalrpc:");
389         }
390
391         status = dcerpc_pipe_connect(*ctx /* TALLOC_CTX */, 
392                                      &p, location, 
393                                          &ndr_table_winreg,
394                                      credentials, ev);
395         rctx->pipe = p;
396
397         if(NT_STATUS_IS_ERR(status)) {
398                 DEBUG(1, ("Unable to open '%s': %s\n", location, nt_errstr(status)));
399                 talloc_free(*ctx);
400                 *ctx = NULL;
401                 return ntstatus_to_werror(status);
402         }
403
404         *ctx = (struct registry_context *)rctx;
405         (*ctx)->ops = &reg_backend_rpc;
406
407         return WERR_OK;
408 }