Registry client library: Remove two elementar conversion functions
[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_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 SAMBA string into a WINREG string buffer
71  */
72 static void chars_to_winreg_StringBuf(TALLOC_CTX *mem_ctx, struct winreg_StringBuf
73         *winregStrBuf, const char *str, uint16_t size)
74 {
75         winregStrBuf->name = NULL;
76         winregStrBuf->length = 0;
77         if (str != NULL) {
78                 winregStrBuf->name = talloc_strdup(mem_ctx, str);
79                 winregStrBuf->length = strlen(str);
80         }
81         winregStrBuf->size = size;
82 }
83
84 #define openhive(u) static WERROR open_ ## u(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *hnd) \
85 { \
86         struct winreg_Open ## u r; \
87         NTSTATUS status; \
88 \
89         ZERO_STRUCT(r); \
90         r.in.system_name = NULL; \
91         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED; \
92         r.out.handle = hnd;\
93 \
94         status = dcerpc_winreg_Open ## u(p, mem_ctx, &r); \
95 \
96         if (!NT_STATUS_IS_OK(status)) { \
97                 DEBUG(1, ("OpenHive failed - %s\n", nt_errstr(status))); \
98                 return ntstatus_to_werror(status); \
99         } \
100 \
101         return r.out.result;\
102 }
103
104 openhive(HKLM)
105 openhive(HKCU)
106 openhive(HKPD)
107 openhive(HKU)
108 openhive(HKCR)
109 openhive(HKDD)
110 openhive(HKCC)
111
112 static struct {
113         uint32_t hkey;
114         WERROR (*open) (struct dcerpc_pipe *p, TALLOC_CTX *,
115                         struct policy_handle *h);
116 } known_hives[] = {
117         { HKEY_LOCAL_MACHINE, open_HKLM },
118         { HKEY_CURRENT_USER, open_HKCU },
119         { HKEY_CLASSES_ROOT, open_HKCR },
120         { HKEY_PERFORMANCE_DATA, open_HKPD },
121         { HKEY_USERS, open_HKU },
122         { HKEY_DYN_DATA, open_HKDD },
123         { HKEY_CURRENT_CONFIG, open_HKCC },
124         { 0, NULL }
125 };
126
127 static WERROR rpc_query_key(TALLOC_CTX *mem_ctx, const struct registry_key *k);
128
129 static WERROR rpc_get_predefined_key(struct registry_context *ctx,
130                                      uint32_t hkey_type,
131                                      struct registry_key **k)
132 {
133         int n;
134         struct rpc_key *mykeydata;
135         struct rpc_registry_context *rctx = talloc_get_type(ctx, struct rpc_registry_context);
136
137         *k = NULL;
138
139         for(n = 0; known_hives[n].hkey; n++) {
140                 if(known_hives[n].hkey == hkey_type)
141                         break;
142         }
143
144         if (known_hives[n].open == NULL)  {
145                 DEBUG(1, ("No such hive %d\n", hkey_type));
146                 return WERR_NO_MORE_ITEMS;
147         }
148
149         mykeydata = talloc_zero(ctx, struct rpc_key);
150         mykeydata->key.context = ctx;
151         mykeydata->pipe = talloc_reference(mykeydata, rctx->pipe);
152         mykeydata->num_values = -1;
153         mykeydata->num_subkeys = -1;
154         *k = (struct registry_key *)mykeydata;
155         return known_hives[n].open(mykeydata->pipe, mykeydata, &(mykeydata->pol));
156 }
157
158 #if 0
159 static WERROR rpc_key_put_rpc_data(TALLOC_CTX *mem_ctx, struct registry_key *k)
160 {
161         struct winreg_OpenKey r;
162         struct rpc_key_data *mykeydata;
163
164         k->backend_data = mykeydata = talloc_zero(mem_ctx, struct rpc_key_data);
165         mykeydata->num_values = -1;
166         mykeydata->num_subkeys = -1;
167
168         /* Then, open the handle using the hive */
169
170         ZERO_STRUCT(r);
171         r.in.handle = &(((struct rpc_key_data *)k->hive->root->backend_data)->pol);
172         chars_to_winreg_String(mem_ctx, &r.in.keyname, k->path);
173         r.in.unknown = 0x00000000;
174         r.in.access_mask = 0x02000000;
175         r.out.handle = &mykeydata->pol;
176
177         dcerpc_winreg_OpenKey((struct dcerpc_pipe *)k->hive->backend_data,
178                               mem_ctx, &r);
179
180         return r.out.result;
181 }
182 #endif
183
184 static WERROR rpc_open_key(TALLOC_CTX *mem_ctx, struct registry_key *h,
185                            const char *name, struct registry_key **key)
186 {
187         struct rpc_key *parentkeydata = talloc_get_type(h, struct rpc_key),
188                                                     *mykeydata;
189         struct winreg_OpenKey r;
190         NTSTATUS status;
191
192         mykeydata = talloc_zero(mem_ctx, struct rpc_key);
193         mykeydata->key.context = parentkeydata->key.context;
194         mykeydata->pipe = talloc_reference(mykeydata, parentkeydata->pipe);
195         mykeydata->num_values = -1;
196         mykeydata->num_subkeys = -1;
197         *key = (struct registry_key *)mykeydata;
198
199         /* Then, open the handle using the hive */
200         ZERO_STRUCT(r);
201         r.in.parent_handle = &parentkeydata->pol;
202         chars_to_winreg_String(mem_ctx, &r.in.keyname, name);
203         r.in.unknown = 0x00000000;
204         r.in.access_mask = 0x02000000;
205         r.out.handle = &mykeydata->pol;
206
207         status = dcerpc_winreg_OpenKey(mykeydata->pipe, mem_ctx, &r);
208
209         if (!NT_STATUS_IS_OK(status)) {
210                 DEBUG(1, ("OpenKey failed - %s\n", nt_errstr(status)));
211                 return ntstatus_to_werror(status);
212         }
213
214         return r.out.result;
215 }
216
217 static WERROR rpc_get_value_by_index(TALLOC_CTX *mem_ctx,
218                                      const struct registry_key *parent,
219                                      uint32_t n,
220                                      const char **value_name,
221                                      uint32_t *type,
222                                      DATA_BLOB *data)
223 {
224         struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key);
225         struct winreg_EnumValue r;
226         struct winreg_StringBuf name;
227         uint8_t value;
228         uint32_t val_size = MAX_VALSIZE;
229         uint32_t zero = 0;
230         WERROR error;
231         NTSTATUS status;
232
233         if (mykeydata->num_values == -1) {
234                 error = rpc_query_key(mem_ctx, parent);
235                 if(!W_ERROR_IS_OK(error)) return error;
236         }
237
238         chars_to_winreg_StringBuf(mem_ctx, &name, "", MAX_NAMESIZE);
239
240         ZERO_STRUCT(r);
241         r.in.handle = &mykeydata->pol;
242         r.in.enum_index = n;
243         r.in.name = &name;
244         r.in.type = type;
245         r.in.value = &value;
246         r.in.size = &val_size;
247         r.in.length = &zero;
248         r.out.name = &name;
249         r.out.type = type;
250         r.out.value = &value;
251         r.out.size = &val_size;
252         r.out.length = &zero;
253
254         status = dcerpc_winreg_EnumValue(mykeydata->pipe, mem_ctx, &r);
255
256         if (!NT_STATUS_IS_OK(status)) {
257                 DEBUG(1, ("EnumValue failed - %s\n", nt_errstr(status)));
258                 return ntstatus_to_werror(status);
259         }
260
261         *value_name = talloc_strdup(mem_ctx, r.out.name->name);
262         *type = *(r.out.type);
263         *data = data_blob_talloc(mem_ctx, r.out.value, *r.out.length);
264
265         return r.out.result;
266 }
267
268 static WERROR rpc_get_value_by_name(TALLOC_CTX *mem_ctx,
269                                      const struct registry_key *parent,
270                                      const char *value_name,
271                                      uint32_t *type,
272                                      DATA_BLOB *data)
273 {
274         struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key);
275         struct winreg_QueryValue r;
276         struct winreg_String name;
277         uint8_t value;
278         uint32_t val_size = MAX_VALSIZE;
279         uint32_t zero = 0;
280         WERROR error;
281         NTSTATUS status;
282
283         if (mykeydata->num_values == -1) {
284                 error = rpc_query_key(mem_ctx, parent);
285                 if(!W_ERROR_IS_OK(error)) return error;
286         }
287
288         chars_to_winreg_String(mem_ctx, &name, value_name);
289
290         ZERO_STRUCT(r);
291         r.in.handle = &mykeydata->pol;
292         r.in.value_name = name;
293         r.in.type = type;
294         r.in.data = &value;
295         r.in.size = &val_size;
296         r.in.length = &zero;
297         r.out.type = type;
298         r.out.data = &value;
299         r.out.size = &val_size;
300         r.out.length = &zero;
301
302         status = dcerpc_winreg_QueryValue(mykeydata->pipe, mem_ctx, &r);
303
304         if (!NT_STATUS_IS_OK(status)) {
305                 DEBUG(1, ("QueryValue failed - %s\n", nt_errstr(status)));
306                 return ntstatus_to_werror(status);
307         }
308
309         *type = *(r.out.type);
310         *data = data_blob_talloc(mem_ctx, r.out.data, *r.out.length);
311
312         return r.out.result;
313 }
314
315 static WERROR rpc_get_subkey_by_index(TALLOC_CTX *mem_ctx,
316                                       const struct registry_key *parent,
317                                       uint32_t n,
318                                       const char **name,
319                                       const char **keyclass,
320                                       NTTIME *last_changed_time)
321 {
322         struct winreg_EnumKey r;
323         struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key);
324         struct winreg_StringBuf namebuf, classbuf;
325         NTTIME change_time = 0;
326         NTSTATUS status;
327
328         chars_to_winreg_StringBuf(mem_ctx, &namebuf, " ", MAX_NAMESIZE);
329         chars_to_winreg_StringBuf(mem_ctx, &classbuf, NULL, 0);
330
331         ZERO_STRUCT(r);
332         r.in.handle = &mykeydata->pol;
333         r.in.enum_index = n;
334         r.in.name = &namebuf;
335         r.in.keyclass = &classbuf;
336         r.in.last_changed_time = &change_time;
337         r.out.name = &namebuf;
338         r.out.keyclass = &classbuf;
339         r.out.last_changed_time = &change_time;
340
341         status = dcerpc_winreg_EnumKey(mykeydata->pipe, mem_ctx, &r);
342
343         if (!NT_STATUS_IS_OK(status)) {
344                 DEBUG(1, ("EnumKey failed - %s\n", nt_errstr(status)));
345                 return ntstatus_to_werror(status);
346         }
347
348         if (name != NULL)
349                 *name = talloc_strdup(mem_ctx, r.out.name->name);
350         if (keyclass != NULL)
351                 *keyclass = talloc_strdup(mem_ctx, r.out.keyclass->name);
352         if (last_changed_time != NULL)
353                 *last_changed_time = *(r.out.last_changed_time);
354
355         return r.out.result;
356 }
357
358 static WERROR rpc_add_key(TALLOC_CTX *mem_ctx,
359                           struct registry_key *parent, const char *name,
360                           const char *key_class,
361                           struct security_descriptor *sec,
362                           struct registry_key **key)
363 {
364         struct winreg_CreateKey r;
365         struct rpc_key *parentkd = talloc_get_type(parent, struct rpc_key);
366         struct rpc_key *rpck = talloc(mem_ctx, struct rpc_key);
367         
368         NTSTATUS status;
369
370         ZERO_STRUCT(r);
371         r.in.handle = &parentkd->pol;
372         chars_to_winreg_String(mem_ctx, &r.in.name, name);
373         chars_to_winreg_String(mem_ctx, &r.in.keyclass, NULL);
374         r.in.options = 0;
375         r.in.access_mask = 0x02000000;
376         r.in.secdesc = NULL;
377         r.in.action_taken = NULL;
378         r.out.new_handle = &rpck->pol;
379         r.out.action_taken = NULL;
380
381         status = dcerpc_winreg_CreateKey(parentkd->pipe, mem_ctx, &r);
382
383         if (!NT_STATUS_IS_OK(status)) {
384                 talloc_free(rpck);
385                 DEBUG(1, ("CreateKey failed - %s\n", nt_errstr(status)));
386                 return ntstatus_to_werror(status);
387         }
388
389         rpck->pipe = talloc_reference(rpck, parentkd->pipe);
390         *key = (struct registry_key *)rpck;
391
392         return r.out.result;
393 }
394
395 static WERROR rpc_query_key(TALLOC_CTX *mem_ctx, const struct registry_key *k)
396 {
397         struct winreg_QueryInfoKey r;
398         struct rpc_key *mykeydata = talloc_get_type(k, struct rpc_key);
399         struct winreg_String classname;
400         NTSTATUS status;
401
402         chars_to_winreg_String(mem_ctx, &classname, NULL);
403
404         ZERO_STRUCT(r);
405         r.in.handle = &mykeydata->pol;
406         r.in.classname = &classname;
407         r.out.classname = &classname;
408         r.out.num_subkeys = &mykeydata->num_subkeys;
409         r.out.max_subkeylen = &mykeydata->max_subkeylen;
410         r.out.max_subkeysize = &mykeydata->max_subkeysize;
411         r.out.num_values = &mykeydata->num_values;
412         r.out.max_valnamelen = &mykeydata->max_valnamelen;
413         r.out.max_valbufsize = &mykeydata->max_valbufsize;
414         r.out.secdescsize = &mykeydata->secdescsize;
415         r.out.last_changed_time = &mykeydata->last_changed_time;
416
417         status = dcerpc_winreg_QueryInfoKey(mykeydata->pipe, mem_ctx, &r);
418
419         if (!NT_STATUS_IS_OK(status)) {
420                 DEBUG(1, ("QueryInfoKey failed - %s\n", nt_errstr(status)));
421                 return ntstatus_to_werror(status);
422         }
423
424         mykeydata->classname = talloc_strdup(mem_ctx, r.out.classname->name);
425
426         return r.out.result;
427 }
428
429 static WERROR rpc_del_key(struct registry_key *parent, const char *name)
430 {
431         NTSTATUS status;
432         struct rpc_key *mykeydata = talloc_get_type(parent, struct rpc_key);
433         struct winreg_DeleteKey r;
434         TALLOC_CTX *mem_ctx = talloc_init("del_key");
435
436         ZERO_STRUCT(r);
437         r.in.handle = &mykeydata->pol;
438         chars_to_winreg_String(mem_ctx, &r.in.key, name);
439
440         status = dcerpc_winreg_DeleteKey(mykeydata->pipe, mem_ctx, &r);
441
442         talloc_free(mem_ctx);
443
444         if (!NT_STATUS_IS_OK(status)) {
445                 DEBUG(1, ("DeleteKey failed - %s\n", nt_errstr(status)));
446                 return ntstatus_to_werror(status);
447         }
448
449         return r.out.result;
450 }
451
452 static WERROR rpc_get_info(TALLOC_CTX *mem_ctx, const struct registry_key *key,
453                                                    const char **classname,
454                                                    uint32_t *num_subkeys,
455                                                    uint32_t *num_values,
456                                                    NTTIME *last_changed_time,
457                                                    uint32_t *max_subkeylen,
458                                                    uint32_t *max_valnamelen,
459                                                    uint32_t *max_valbufsize)
460 {
461         struct rpc_key *mykeydata = talloc_get_type(key, struct rpc_key);
462         WERROR error;
463
464         if (mykeydata->num_values == -1) {
465                 error = rpc_query_key(mem_ctx, key);
466                 if(!W_ERROR_IS_OK(error)) return error;
467         }
468
469         if (classname != NULL)
470                 *classname = mykeydata->classname;
471
472         if (num_subkeys != NULL)
473                 *num_subkeys = mykeydata->num_subkeys;
474
475         if (num_values != NULL)
476                 *num_values = mykeydata->num_values;
477
478         if (last_changed_time != NULL)
479                 *last_changed_time = mykeydata->last_changed_time;
480
481         if (max_subkeylen != NULL)
482                 *max_subkeylen = mykeydata->max_subkeylen;
483
484         if (max_valnamelen != NULL)
485                 *max_valnamelen = mykeydata->max_valnamelen;
486
487         if (max_valbufsize != NULL)
488                 *max_valbufsize = mykeydata->max_valbufsize;
489
490         return WERR_OK;
491 }
492
493 static struct registry_operations reg_backend_rpc = {
494         .name = "rpc",
495         .open_key = rpc_open_key,
496         .get_predefined_key = rpc_get_predefined_key,
497         .enum_key = rpc_get_subkey_by_index,
498         .enum_value = rpc_get_value_by_index,
499         .get_value = rpc_get_value_by_name,
500         .create_key = rpc_add_key,
501         .delete_key = rpc_del_key,
502         .get_key_info = rpc_get_info,
503         .get_predefined_key = rpc_get_predefined_key,
504 };
505
506 _PUBLIC_ WERROR reg_open_remote(struct registry_context **ctx,
507                                 struct auth_session_info *session_info,
508                                 struct cli_credentials *credentials,
509                                 struct loadparm_context *lp_ctx,
510                                 const char *location, struct event_context *ev)
511 {
512         NTSTATUS status;
513         struct dcerpc_pipe *p;
514         struct rpc_registry_context *rctx;
515
516         dcerpc_init();
517
518         rctx = talloc(NULL, struct rpc_registry_context);
519
520         /* Default to local smbd if no connection is specified */
521         if (!location) {
522                 location = talloc_strdup(rctx, "ncalrpc:");
523         }
524
525         status = dcerpc_pipe_connect(rctx /* TALLOC_CTX */,
526                                      &p, location,
527                                          &ndr_table_winreg,
528                                      credentials, ev, lp_ctx);
529         rctx->pipe = p;
530
531         if(NT_STATUS_IS_ERR(status)) {
532                 DEBUG(1, ("Unable to open '%s': %s\n", location,
533                         nt_errstr(status)));
534                 talloc_free(rctx);
535                 *ctx = NULL;
536                 return ntstatus_to_werror(status);
537         }
538
539         *ctx = (struct registry_context *)rctx;
540         (*ctx)->ops = &reg_backend_rpc;
541
542         return WERR_OK;
543 }