s4:lib/registry/rpc.c - remove a duplicate assignment
[metze/samba/wip.git] / source4 / 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    Copyright (C) 2008 Matthias Dieter Wallnöfer, mwallnoefer@yahoo.de
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "includes.h"
21 #include "registry.h"
22 #include "librpc/gen_ndr/ndr_winreg_c.h"
23
24 #define MAX_NAMESIZE 512
25 #define MAX_VALSIZE 32768
26
27 struct rpc_key {
28         struct registry_key key;
29         struct policy_handle pol;
30         struct dcerpc_binding_handle *binding_handle;
31         const char* classname;  
32         uint32_t num_subkeys;
33         uint32_t max_subkeylen;
34         uint32_t max_classlen;
35         uint32_t num_values;
36         uint32_t max_valnamelen;
37         uint32_t max_valbufsize;
38         uint32_t secdescsize;
39         NTTIME last_changed_time;
40 };
41
42 struct rpc_registry_context {
43         struct registry_context context;
44         struct dcerpc_pipe *pipe;
45         struct dcerpc_binding_handle *binding_handle;
46 };
47
48 static struct registry_operations reg_backend_rpc;
49
50 /**
51  * This is the RPC backend for the registry library.
52  */
53
54 #define openhive(u) static WERROR open_ ## u(struct dcerpc_binding_handle *b, TALLOC_CTX *mem_ctx, struct policy_handle *hnd) \
55 { \
56         struct winreg_Open ## u r; \
57         NTSTATUS status; \
58 \
59         ZERO_STRUCT(r); \
60         r.in.system_name = NULL; \
61         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED; \
62         r.out.handle = hnd;\
63 \
64         status = dcerpc_winreg_Open ## u ## _r(b, mem_ctx, &r); \
65 \
66         if (!NT_STATUS_IS_OK(status)) { \
67                 DEBUG(1, ("OpenHive failed - %s\n", nt_errstr(status))); \
68                 return ntstatus_to_werror(status); \
69         } \
70 \
71         return r.out.result;\
72 }
73
74 openhive(HKLM)
75 openhive(HKCU)
76 openhive(HKPD)
77 openhive(HKU)
78 openhive(HKCR)
79 openhive(HKDD)
80 openhive(HKCC)
81
82 static struct {
83         uint32_t hkey;
84         WERROR (*open) (struct dcerpc_binding_handle *b, TALLOC_CTX *,
85                         struct policy_handle *h);
86 } known_hives[] = {
87         { HKEY_LOCAL_MACHINE, open_HKLM },
88         { HKEY_CURRENT_USER, open_HKCU },
89         { HKEY_CLASSES_ROOT, open_HKCR },
90         { HKEY_PERFORMANCE_DATA, open_HKPD },
91         { HKEY_USERS, open_HKU },
92         { HKEY_DYN_DATA, open_HKDD },
93         { HKEY_CURRENT_CONFIG, open_HKCC },
94         { 0, NULL }
95 };
96
97 static WERROR rpc_query_key(TALLOC_CTX *mem_ctx, const struct registry_key *k);
98
99 static WERROR rpc_get_predefined_key(struct registry_context *ctx,
100                                      uint32_t hkey_type,
101                                      struct registry_key **k)
102 {
103         int n;
104         struct rpc_key *mykeydata;
105         struct rpc_registry_context *rctx = talloc_get_type(ctx, struct rpc_registry_context);
106
107         *k = NULL;
108
109         for(n = 0; known_hives[n].hkey; n++) {
110                 if(known_hives[n].hkey == hkey_type)
111                         break;
112         }
113
114         if (known_hives[n].open == NULL)  {
115                 DEBUG(1, ("No such hive %d\n", hkey_type));
116                 return WERR_NO_MORE_ITEMS;
117         }
118
119         mykeydata = talloc_zero(ctx, struct rpc_key);
120         W_ERROR_HAVE_NO_MEMORY(mykeydata);
121         mykeydata->key.context = ctx;
122         mykeydata->binding_handle = rctx->binding_handle;
123         mykeydata->num_values = -1;
124         mykeydata->num_subkeys = -1;
125         *k = (struct registry_key *)mykeydata;
126         return known_hives[n].open(mykeydata->binding_handle, mykeydata, &mykeydata->pol);
127 }
128
129 #if 0
130 static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k)
131 {
132         struct winreg_OpenKey r;
133         struct rpc_key_data *mykeydata;
134
135         k->backend_data = mykeydata = talloc_zero(mem_ctx, struct rpc_key_data);
136         mykeydata->num_values = -1;
137         mykeydata->num_subkeys = -1;
138
139         /* Then, open the handle using the hive */
140
141         ZERO_STRUCT(r);
142         r.in.handle = &(((struct rpc_key_data *)k->hive->root->backend_data)->pol);
143         r.in.keyname.name = k->path;
144         r.in.unknown = 0x00000000;
145         r.in.access_mask = 0x02000000;
146         r.out.handle = &mykeydata->pol;
147
148         dcerpc_winreg_OpenKey((struct dcerpc_pipe *)k->hive->backend_data,
149                               mem_ctx, &r);
150
151         return r.out.result;
152 }
153 #endif
154
155 static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h,
156                            const char *name, struct registry_key **key)
157 {
158         struct rpc_key *parentkeydata = talloc_get_type(h, struct rpc_key),
159                                                     *mykeydata;
160         struct winreg_OpenKey r;
161         NTSTATUS status;
162
163         mykeydata = talloc_zero(mem_ctx, struct rpc_key);
164         W_ERROR_HAVE_NO_MEMORY(mykeydata);
165         mykeydata->key.context = parentkeydata->key.context;
166         mykeydata->binding_handle = parentkeydata->binding_handle;
167         mykeydata->num_values = -1;
168         mykeydata->num_subkeys = -1;
169         *key = (struct registry_key *)mykeydata;
170
171         /* Then, open the handle using the hive */
172         ZERO_STRUCT(r);
173         r.in.parent_handle = &parentkeydata->pol;
174         r.in.keyname.name = name;
175         r.in.options = 0x00000000;
176         r.in.access_mask = 0x02000000;
177         r.out.handle = &mykeydata->pol;
178
179         status = dcerpc_winreg_OpenKey_r(mykeydata->binding_handle, mem_ctx, &r);
180
181         if (!NT_STATUS_IS_OK(status)) {
182                 DEBUG(1, ("OpenKey failed - %s\n", nt_errstr(status)));
183                 return ntstatus_to_werror(status);
184         }
185
186         return r.out.result;
187 }
188
189 static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx,
190                                      const struct registry_key *parent,
191                                      uint32_t n,
192                                      const char **value_name,
193                                      uint32_t *type,
194                                      DATA_BLOB *data)
195 {
196         struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key);
197         struct winreg_EnumValue r;
198         struct winreg_ValNameBuf name;
199         uint8_t value;
200         uint32_t val_size = MAX_VALSIZE;
201         uint32_t zero = 0;
202         WERROR error;
203         NTSTATUS status;
204
205         if (mykeydata->num_values == -1) {
206                 error = rpc_query_key(mem_ctx, parent);
207                 if(!W_ERROR_IS_OK(error)) return error;
208         }
209
210         name.name = "";
211         name.size = MAX_NAMESIZE;
212
213         ZERO_STRUCT(r);
214         r.in.handle = &mykeydata->pol;
215         r.in.enum_index = n;
216         r.in.name = &name;
217         r.in.type = (enum winreg_Type *) type;
218         r.in.value = &value;
219         r.in.size = &val_size;
220         r.in.length = &zero;
221         r.out.name = &name;
222         r.out.type = (enum winreg_Type *) type;
223         r.out.value = &value;
224         r.out.size = &val_size;
225         r.out.length = &zero;
226
227         status = dcerpc_winreg_EnumValue_r(mykeydata->binding_handle, mem_ctx, &r);
228
229         if (!NT_STATUS_IS_OK(status)) {
230                 DEBUG(1, ("EnumValue failed - %s\n", nt_errstr(status)));
231                 return ntstatus_to_werror(status);
232         }
233
234         *value_name = talloc_steal(mem_ctx, r.out.name->name);
235         *type = *(r.out.type);
236         *data = data_blob_talloc(mem_ctx, r.out.value, *r.out.length);
237
238         return r.out.result;
239 }
240
241 static WERROR rpc_get_value_by_name(TALLOC_CTX *mem_ctx,
242                                      const struct registry_key *parent,
243                                      const char *value_name,
244                                      uint32_t *type,
245                                      DATA_BLOB *data)
246 {
247         struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key);
248         struct winreg_QueryValue r;
249         struct winreg_String name;
250         uint8_t value;
251         uint32_t val_size = MAX_VALSIZE;
252         uint32_t zero = 0;
253         WERROR error;
254         NTSTATUS status;
255
256         if (mykeydata->num_values == -1) {
257                 error = rpc_query_key(mem_ctx, parent);
258                 if(!W_ERROR_IS_OK(error)) return error;
259         }
260
261         name.name = value_name;
262
263         ZERO_STRUCT(r);
264         r.in.handle = &mykeydata->pol;
265         r.in.value_name = &name;
266         r.in.type = (enum winreg_Type *) type;
267         r.in.data = &value;
268         r.in.data_size = &val_size;
269         r.in.data_length = &zero;
270         r.out.type = (enum winreg_Type *) type;
271         r.out.data = &value;
272         r.out.data_size = &val_size;
273         r.out.data_length = &zero;
274
275         status = dcerpc_winreg_QueryValue_r(mykeydata->binding_handle, mem_ctx, &r);
276
277         if (!NT_STATUS_IS_OK(status)) {
278                 DEBUG(1, ("QueryValue failed - %s\n", nt_errstr(status)));
279                 return ntstatus_to_werror(status);
280         }
281
282         *type = *(r.out.type);
283         *data = data_blob_talloc(mem_ctx, r.out.data, *r.out.data_length);
284
285         return r.out.result;
286 }
287
288 static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx,
289                                       const struct registry_key *parent,
290                                       uint32_t n,
291                                       const char **name,
292                                       const char **keyclass,
293                                       NTTIME *last_changed_time)
294 {
295         struct winreg_EnumKey r;
296         struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key);
297         struct winreg_StringBuf namebuf, classbuf;
298         NTTIME change_time = 0;
299         NTSTATUS status;
300
301         namebuf.name = "";
302         namebuf.size = MAX_NAMESIZE;
303         classbuf.name = NULL;
304         classbuf.size = 0;
305
306         ZERO_STRUCT(r);
307         r.in.handle = &mykeydata->pol;
308         r.in.enum_index = n;
309         r.in.name = &namebuf;
310         r.in.keyclass = &classbuf;
311         r.in.last_changed_time = &change_time;
312         r.out.name = &namebuf;
313         r.out.keyclass = &classbuf;
314         r.out.last_changed_time = &change_time;
315
316         status = dcerpc_winreg_EnumKey_r(mykeydata->binding_handle, mem_ctx, &r);
317
318         if (!NT_STATUS_IS_OK(status)) {
319                 DEBUG(1, ("EnumKey failed - %s\n", nt_errstr(status)));
320                 return ntstatus_to_werror(status);
321         }
322
323         if (name != NULL)
324                 *name = talloc_steal(mem_ctx, r.out.name->name);
325         if (keyclass != NULL)
326                 *keyclass = talloc_steal(mem_ctx, r.out.keyclass->name);
327         if (last_changed_time != NULL)
328                 *last_changed_time = *(r.out.last_changed_time);
329
330         return r.out.result;
331 }
332
333 static WERROR rpc_add_key(TALLOC_CTX *mem_ctx,
334                           struct registry_key *parent, const char *path,
335                           const char *key_class,
336                           struct security_descriptor *sec,
337                           struct registry_key **key)
338 {
339         struct winreg_CreateKey r;
340         struct rpc_key *parentkd = talloc_get_type(parent, struct rpc_key);
341         struct rpc_key *rpck = talloc(mem_ctx, struct rpc_key);
342         
343         NTSTATUS status;
344
345         ZERO_STRUCT(r);
346         r.in.handle = &parentkd->pol;
347         r.in.name.name = path;
348         r.in.keyclass.name = NULL;
349         r.in.options = 0;
350         r.in.access_mask = 0x02000000;
351         r.in.secdesc = NULL;
352         r.in.action_taken = NULL;
353         r.out.new_handle = &rpck->pol;
354         r.out.action_taken = NULL;
355
356         status = dcerpc_winreg_CreateKey_r(parentkd->binding_handle, mem_ctx, &r);
357
358         if (!NT_STATUS_IS_OK(status)) {
359                 talloc_free(rpck);
360                 DEBUG(1, ("CreateKey failed - %s\n", nt_errstr(status)));
361                 return ntstatus_to_werror(status);
362         }
363
364         rpck->binding_handle = parentkd->binding_handle;
365         *key = (struct registry_key *)rpck;
366
367         return r.out.result;
368 }
369
370 static WERROR rpc_query_key(TALLOC_CTX *mem_ctx, const struct registry_key *k)
371 {
372         struct winreg_QueryInfoKey r;
373         struct rpc_key *mykeydata = talloc_get_type(k, struct rpc_key);
374         struct winreg_String classname;
375         NTSTATUS status;
376
377         classname.name = NULL;
378
379         ZERO_STRUCT(r);
380         r.in.handle = &mykeydata->pol;
381         r.in.classname = &classname;
382         r.out.classname = &classname;
383         r.out.num_subkeys = &mykeydata->num_subkeys;
384         r.out.max_subkeylen = &mykeydata->max_subkeylen;
385         r.out.max_classlen = &mykeydata->max_classlen;
386         r.out.num_values = &mykeydata->num_values;
387         r.out.max_valnamelen = &mykeydata->max_valnamelen;
388         r.out.max_valbufsize = &mykeydata->max_valbufsize;
389         r.out.secdescsize = &mykeydata->secdescsize;
390         r.out.last_changed_time = &mykeydata->last_changed_time;
391
392         status = dcerpc_winreg_QueryInfoKey_r(mykeydata->binding_handle, mem_ctx, &r);
393
394         if (!NT_STATUS_IS_OK(status)) {
395                 DEBUG(1, ("QueryInfoKey failed - %s\n", nt_errstr(status)));
396                 return ntstatus_to_werror(status);
397         }
398
399         mykeydata->classname = talloc_steal(mem_ctx, r.out.classname->name);
400
401         return r.out.result;
402 }
403
404 static WERROR rpc_del_key(TALLOC_CTX *mem_ctx, struct registry_key *parent,
405                           const char *name)
406 {
407         NTSTATUS status;
408         struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key);
409         struct winreg_DeleteKey r;
410
411         ZERO_STRUCT(r);
412         r.in.handle = &mykeydata->pol;
413         r.in.key.name = name;
414
415         status = dcerpc_winreg_DeleteKey_r(mykeydata->binding_handle, mem_ctx, &r);
416
417         if (!NT_STATUS_IS_OK(status)) {
418                 DEBUG(1, ("DeleteKey failed - %s\n", nt_errstr(status)));
419                 return ntstatus_to_werror(status);
420         }
421
422         return r.out.result;
423 }
424
425 static WERROR rpc_get_info(TALLOC_CTX *mem_ctx, const struct registry_key *key,
426                                                    const char **classname,
427                                                    uint32_t *num_subkeys,
428                                                    uint32_t *num_values,
429                                                    NTTIME *last_changed_time,
430                                                    uint32_t *max_subkeylen,
431                                                    uint32_t *max_valnamelen,
432                                                    uint32_t *max_valbufsize)
433 {
434         struct rpc_key *mykeydata = talloc_get_type(key, struct rpc_key);
435         WERROR error;
436
437         if (mykeydata->num_values == -1) {
438                 error = rpc_query_key(mem_ctx, key);
439                 if(!W_ERROR_IS_OK(error)) return error;
440         }
441
442         if (classname != NULL)
443                 *classname = mykeydata->classname;
444
445         if (num_subkeys != NULL)
446                 *num_subkeys = mykeydata->num_subkeys;
447
448         if (num_values != NULL)
449                 *num_values = mykeydata->num_values;
450
451         if (last_changed_time != NULL)
452                 *last_changed_time = mykeydata->last_changed_time;
453
454         if (max_subkeylen != NULL)
455                 *max_subkeylen = mykeydata->max_subkeylen;
456
457         if (max_valnamelen != NULL)
458                 *max_valnamelen = mykeydata->max_valnamelen;
459
460         if (max_valbufsize != NULL)
461                 *max_valbufsize = mykeydata->max_valbufsize;
462
463         return WERR_OK;
464 }
465
466 static struct registry_operations reg_backend_rpc = {
467         .name = "rpc",
468         .open_key = rpc_open_key,
469         .get_predefined_key = rpc_get_predefined_key,
470         .enum_key = rpc_get_subkey_by_index,
471         .enum_value = rpc_get_value_by_index,
472         .get_value = rpc_get_value_by_name,
473         .create_key = rpc_add_key,
474         .delete_key = rpc_del_key,
475         .get_key_info = rpc_get_info,
476 };
477
478 _PUBLIC_ WERROR reg_open_remote(struct registry_context **ctx,
479                                 struct auth_session_info *session_info,
480                                 struct cli_credentials *credentials,
481                                 struct loadparm_context *lp_ctx,
482                                 const char *location, struct tevent_context *ev)
483 {
484         NTSTATUS status;
485         struct dcerpc_pipe *p;
486         struct rpc_registry_context *rctx;
487
488         dcerpc_init(lp_ctx);
489
490         rctx = talloc(NULL, struct rpc_registry_context);
491         W_ERROR_HAVE_NO_MEMORY(rctx);
492
493         /* Default to local smbd if no connection is specified */
494         if (!location) {
495                 location = talloc_strdup(rctx, "ncalrpc:");
496         }
497
498         status = dcerpc_pipe_connect(rctx /* TALLOC_CTX */,
499                                      &p, location,
500                                      &ndr_table_winreg,
501                                      credentials, ev, lp_ctx);
502         if(NT_STATUS_IS_ERR(status)) {
503                 DEBUG(1, ("Unable to open '%s': %s\n", location,
504                         nt_errstr(status)));
505                 talloc_free(rctx);
506                 *ctx = NULL;
507                 return ntstatus_to_werror(status);
508         }
509
510         rctx->pipe = p;
511         rctx->binding_handle = p->binding_handle;
512
513         *ctx = (struct registry_context *)rctx;
514         (*ctx)->ops = &reg_backend_rpc;
515
516         return WERR_OK;
517 }