Registry client library: Use "talloc_zero" to avoid uninitialized values
[sfrench/samba-autobuild/.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_pipe *pipe;
31
32         const char* classname;  
33         uint32_t num_subkeys;
34         uint32_t max_subkeylen;
35         uint32_t max_subkeysize;
36         uint32_t num_values;
37         uint32_t max_valnamelen;
38         uint32_t max_valbufsize;
39         uint32_t secdescsize;
40         NTTIME last_changed_time;
41 };
42
43 struct rpc_registry_context {
44         struct registry_context context;
45         struct dcerpc_pipe *pipe;
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 /*
55  * Converts a SAMBA string into a WINREG string
56  */
57 static void chars_to_winreg_String(TALLOC_CTX *mem_ctx, struct winreg_String
58         *winregStr, const char *str)
59 {
60         winregStr->name = NULL;
61         winregStr->name_len = 0;
62         if (str != NULL) {
63                 winregStr->name = talloc_strdup(mem_ctx, str);
64                 winregStr->name_len = strlen(str);
65         }
66         winregStr->name_size = winregStr->name_len;
67 }
68
69 /*
70  * Converts a WINREG string into a SAMBA string
71  */
72 static void winreg_String_to_chars(TALLOC_CTX *mem_ctx, const char **str,
73         struct winreg_String *winregStr)
74 {
75         *str = NULL;
76         if (winregStr->name != NULL)
77                 *str = talloc_strdup(mem_ctx, winregStr->name);
78 }
79
80 /*
81  * Converts a SAMBA string into a WINREG string buffer
82  */
83 static void chars_to_winreg_StringBuf(TALLOC_CTX *mem_ctx, struct winreg_StringBuf
84         *winregStrBuf, const char *str, uint16_t size)
85 {
86         winregStrBuf->name = NULL;
87         winregStrBuf->length = 0;
88         if (str != NULL) {
89                 winregStrBuf->name = talloc_strdup(mem_ctx, str);
90                 winregStrBuf->length = strlen(str);
91         }
92         winregStrBuf->size = size;
93 }
94
95 /*
96  * Converts a WINREG string buffer into a SAMBA string
97  */
98 static void winreg_StringBuf_to_chars(TALLOC_CTX *mem_ctx, const char **str,
99         struct winreg_StringBuf *winregStrBuf)
100 {
101         *str = NULL;
102         if (winregStrBuf->name != NULL)
103                 *str = talloc_strdup(mem_ctx, winregStrBuf->name);
104 }
105
106 #define openhive(u) static WERROR open_ ## u(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *hnd) \
107 { \
108         struct winreg_Open ## u r; \
109         NTSTATUS status; \
110 \
111         ZERO_STRUCT(r); \
112         r.in.system_name = NULL; \
113         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED; \
114         r.out.handle = hnd;\
115 \
116         status = dcerpc_winreg_Open ## u(p, mem_ctx, &r); \
117 \
118         if (!NT_STATUS_IS_OK(status)) { \
119                 DEBUG(1, ("OpenHive failed - %s\n", nt_errstr(status))); \
120                 return ntstatus_to_werror(status); \
121         } \
122 \
123         return r.out.result;\
124 }
125
126 openhive(HKLM)
127 openhive(HKCU)
128 openhive(HKPD)
129 openhive(HKU)
130 openhive(HKCR)
131 openhive(HKDD)
132 openhive(HKCC)
133
134 static struct {
135         uint32_t hkey;
136         WERROR (*open) (struct dcerpc_pipe *p, TALLOC_CTX *,
137                         struct policy_handle *h);
138 } known_hives[] = {
139         { HKEY_LOCAL_MACHINE, open_HKLM },
140         { HKEY_CURRENT_USER, open_HKCU },
141         { HKEY_CLASSES_ROOT, open_HKCR },
142         { HKEY_PERFORMANCE_DATA, open_HKPD },
143         { HKEY_USERS, open_HKU },
144         { HKEY_DYN_DATA, open_HKDD },
145         { HKEY_CURRENT_CONFIG, open_HKCC },
146         { 0, NULL }
147 };
148
149 static WERROR rpc_query_key(TALLOC_CTX *mem_ctx, const struct registry_key *k);
150
151 static WERROR rpc_get_predefined_key(struct registry_context *ctx,
152                                      uint32_t hkey_type,
153                                      struct registry_key **k)
154 {
155         int n;
156         struct rpc_key *mykeydata;
157         struct rpc_registry_context *rctx = talloc_get_type(ctx, struct rpc_registry_context);
158
159         *k = NULL;
160
161         for(n = 0; known_hives[n].hkey; n++) {
162                 if(known_hives[n].hkey == hkey_type)
163                         break;
164         }
165
166         if (known_hives[n].open == NULL)  {
167                 DEBUG(1, ("No such hive %d\n", hkey_type));
168                 return WERR_NO_MORE_ITEMS;
169         }
170
171         mykeydata = talloc_zero(ctx, struct rpc_key);
172         mykeydata->key.context = ctx;
173         mykeydata->pipe = talloc_reference(mykeydata, rctx->pipe);
174         mykeydata->num_values = -1;
175         mykeydata->num_subkeys = -1;
176         *k = (struct registry_key *)mykeydata;
177         return known_hives[n].open(mykeydata->pipe, mykeydata, &(mykeydata->pol));
178 }
179
180 #if 0
181 static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k)
182 {
183         struct winreg_OpenKey r;
184         struct rpc_key_data *mykeydata;
185
186         k->backend_data = mykeydata = talloc_zero(mem_ctx, struct rpc_key_data);
187         mykeydata->num_values = -1;
188         mykeydata->num_subkeys = -1;
189
190         /* Then, open the handle using the hive */
191
192         ZERO_STRUCT(r);
193         r.in.handle = &(((struct rpc_key_data *)k->hive->root->backend_data)->pol);
194         chars_to_winreg_String(mem_ctx, &r.in.keyname, k->path);
195         r.in.unknown = 0x00000000;
196         r.in.access_mask = 0x02000000;
197         r.out.handle = &mykeydata->pol;
198
199         dcerpc_winreg_OpenKey((struct dcerpc_pipe *)k->hive->backend_data,
200                               mem_ctx, &r);
201
202         return r.out.result;
203 }
204 #endif
205
206 static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h,
207                            const char *name, struct registry_key **key)
208 {
209         struct rpc_key *parentkeydata = talloc_get_type(h, struct rpc_key),
210                                                     *mykeydata;
211         struct winreg_OpenKey r;
212         NTSTATUS status;
213
214         mykeydata = talloc_zero(mem_ctx, struct rpc_key);
215         mykeydata->key.context = parentkeydata->key.context;
216         mykeydata->pipe = talloc_reference(mykeydata, parentkeydata->pipe);
217         mykeydata->num_values = -1;
218         mykeydata->num_subkeys = -1;
219         *key = (struct registry_key *)mykeydata;
220
221         /* Then, open the handle using the hive */
222         ZERO_STRUCT(r);
223         r.in.parent_handle = &parentkeydata->pol;
224         chars_to_winreg_String(mem_ctx, &r.in.keyname, name);
225         r.in.unknown = 0x00000000;
226         r.in.access_mask = 0x02000000;
227         r.out.handle = &mykeydata->pol;
228
229         status = dcerpc_winreg_OpenKey(mykeydata->pipe, mem_ctx, &r);
230
231         if (!NT_STATUS_IS_OK(status)) {
232                 DEBUG(1, ("OpenKey failed - %s\n", nt_errstr(status)));
233                 return ntstatus_to_werror(status);
234         }
235
236         return r.out.result;
237 }
238
239 static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx,
240                                      const struct registry_key *parent,
241                                      uint32_t n,
242                                      const char **value_name,
243                                      uint32_t *type,
244                                      DATA_BLOB *data)
245 {
246         struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key);
247         struct winreg_EnumValue r;
248         struct winreg_StringBuf name;
249         uint8_t value;
250         uint32_t val_size = MAX_VALSIZE;
251         uint32_t zero = 0;
252         WERROR error;
253         NTSTATUS status;
254
255         if (mykeydata->num_values == -1) {
256                 error = rpc_query_key(mem_ctx, parent);
257                 if(!W_ERROR_IS_OK(error)) return error;
258         }
259
260         chars_to_winreg_StringBuf(mem_ctx, &name, "", MAX_NAMESIZE);
261
262         ZERO_STRUCT(r);
263         r.in.handle = &mykeydata->pol;
264         r.in.enum_index = n;
265         r.in.name = &name;
266         r.in.type = type;
267         r.in.value = &value;
268         r.in.size = &val_size;
269         r.in.length = &zero;
270         r.out.name = &name;
271         r.out.type = type;
272         r.out.value = &value;
273         r.out.size = &val_size;
274         r.out.length = &zero;
275
276         status = dcerpc_winreg_EnumValue(mykeydata->pipe, mem_ctx, &r);
277
278         if (!NT_STATUS_IS_OK(status)) {
279                 DEBUG(1, ("EnumValue failed - %s\n", nt_errstr(status)));
280                 return ntstatus_to_werror(status);
281         }
282
283         winreg_StringBuf_to_chars(mem_ctx, value_name, r.out.name);
284         *type = *(r.out.type);
285         *data = data_blob_talloc(mem_ctx, r.out.value, *r.out.length);
286
287         return r.out.result;
288 }
289
290 static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx,
291                                       const struct registry_key *parent,
292                                       uint32_t n,
293                                       const char **name,
294                                       const char **keyclass,
295                                       NTTIME *last_changed_time)
296 {
297         struct winreg_EnumKey r;
298         struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key);
299         struct winreg_StringBuf namebuf, classbuf;
300         NTTIME change_time = 0;
301         NTSTATUS status;
302
303         chars_to_winreg_StringBuf(mem_ctx, &namebuf, " ", MAX_NAMESIZE);
304         chars_to_winreg_StringBuf(mem_ctx, &classbuf, NULL, 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(mykeydata->pipe, 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                 winreg_StringBuf_to_chars(mem_ctx, name, r.out.name);
325         if (keyclass != NULL)
326                 winreg_StringBuf_to_chars(mem_ctx, keyclass, r.out.keyclass);
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 *name,
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         chars_to_winreg_String(mem_ctx, &r.in.name, name);
348         chars_to_winreg_String(mem_ctx, &r.in.keyclass, 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(parentkd->pipe, 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->pipe = talloc_reference(rpck, parentkd->pipe);
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         chars_to_winreg_String(mem_ctx, &classname, 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_subkeysize = &mykeydata->max_subkeysize;
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(mykeydata->pipe, 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         winreg_String_to_chars(mem_ctx, &mykeydata->classname, r.out.classname);
400
401         return r.out.result;
402 }
403
404 static WERROR rpc_del_key(struct registry_key *parent, const char *name)
405 {
406         NTSTATUS status;
407         struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key);
408         struct winreg_DeleteKey r;
409         TALLOC_CTX *mem_ctx = talloc_init("del_key");
410
411         ZERO_STRUCT(r);
412         r.in.handle = &mykeydata->pol;
413         chars_to_winreg_String(mem_ctx, &r.in.key, name);
414
415         status = dcerpc_winreg_DeleteKey(mykeydata->pipe, mem_ctx, &r);
416
417         talloc_free(mem_ctx);
418
419         if (!NT_STATUS_IS_OK(status)) {
420                 DEBUG(1, ("DeleteKey failed - %s\n", nt_errstr(status)));
421                 return ntstatus_to_werror(status);
422         }
423
424         return r.out.result;
425 }
426
427 static WERROR rpc_get_info(TALLOC_CTX *mem_ctx, const struct registry_key *key,
428                                                    const char **classname,
429                                                    uint32_t *num_subkeys,
430                                                    uint32_t *num_values,
431                                                    NTTIME *last_changed_time,
432                                                    uint32_t *max_subkeylen,
433                                                    uint32_t *max_valnamelen,
434                                                    uint32_t *max_valbufsize)
435 {
436         struct rpc_key *mykeydata = talloc_get_type(key, struct rpc_key);
437         WERROR error;
438
439         if (mykeydata->num_values == -1) {
440                 error = rpc_query_key(mem_ctx, key);
441                 if(!W_ERROR_IS_OK(error)) return error;
442         }
443
444         if (classname != NULL)
445                 *classname = mykeydata->classname;
446
447         if (num_subkeys != NULL)
448                 *num_subkeys = mykeydata->num_subkeys;
449
450         if (num_values != NULL)
451                 *num_values = mykeydata->num_values;
452
453         if (last_changed_time != NULL)
454                 *last_changed_time = mykeydata->last_changed_time;
455
456         if (max_subkeylen != NULL)
457                 *max_subkeylen = mykeydata->max_subkeylen;
458
459         if (max_valnamelen != NULL)
460                 *max_valnamelen = mykeydata->max_valnamelen;
461
462         if (max_valbufsize != NULL)
463                 *max_valbufsize = mykeydata->max_valbufsize;
464
465         return WERR_OK;
466 }
467
468 static struct registry_operations reg_backend_rpc = {
469         .name = "rpc",
470         .open_key = rpc_open_key,
471         .get_predefined_key = rpc_get_predefined_key,
472         .enum_key = rpc_get_subkey_by_index,
473         .enum_value = rpc_get_value_by_index,
474         .create_key = rpc_add_key,
475         .delete_key = rpc_del_key,
476         .get_key_info = rpc_get_info,
477         .get_predefined_key = rpc_get_predefined_key,
478 };
479
480 _PUBLIC_ WERROR reg_open_remote(struct registry_context **ctx,
481                                 struct auth_session_info *session_info,
482                                 struct cli_credentials *credentials,
483                                 struct loadparm_context *lp_ctx,
484                                 const char *location, struct event_context *ev)
485 {
486         NTSTATUS status;
487         struct dcerpc_pipe *p;
488         struct rpc_registry_context *rctx;
489
490         dcerpc_init();
491
492         rctx = talloc(NULL, struct rpc_registry_context);
493
494         /* Default to local smbd if no connection is specified */
495         if (!location) {
496                 location = talloc_strdup(rctx, "ncalrpc:");
497         }
498
499         status = dcerpc_pipe_connect(rctx /* TALLOC_CTX */,
500                                      &p, location,
501                                          &ndr_table_winreg,
502                                      credentials, ev, lp_ctx);
503         rctx->pipe = p;
504
505         if(NT_STATUS_IS_ERR(status)) {
506                 DEBUG(1, ("Unable to open '%s': %s\n", location,
507                         nt_errstr(status)));
508                 talloc_free(rctx);
509                 *ctx = NULL;
510                 return ntstatus_to_werror(status);
511         }
512
513         *ctx = (struct registry_context *)rctx;
514         (*ctx)->ops = &reg_backend_rpc;
515
516         return WERR_OK;
517 }