libgpo: fix the build of gpext plugins.
[kai/samba.git] / source3 / libgpo / gpext / registry.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Group Policy Support
4  *  Copyright (C) Guenther Deschner 2007-2008
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
20 #include "includes.h"
21 #include "../libgpo/gpo_ini.h"
22
23 #define GP_EXT_NAME "registry"
24
25 /* more info can be found at:
26  * http://msdn2.microsoft.com/en-us/library/aa374407.aspx */
27
28 #define GP_REGPOL_FILE  "Registry.pol"
29
30 #define GP_REGPOL_FILE_SIGNATURE 0x67655250 /* 'PReg' */
31 #define GP_REGPOL_FILE_VERSION 1
32
33 static TALLOC_CTX *ctx = NULL;
34
35 struct gp_registry_file_header {
36         uint32_t signature;
37         uint32_t version;
38 };
39
40 struct gp_registry_file_entry {
41         UNISTR key;
42         UNISTR value;
43         enum winreg_Type type;
44         size_t size;
45         uint8_t *data;
46 };
47
48 struct gp_registry_file {
49         struct gp_registry_file_header header;
50         size_t num_entries;
51         struct gp_registry_entry *entries;
52 };
53
54 /****************************************************************
55 ****************************************************************/
56
57 static bool reg_parse_header(const char *desc,
58                              struct gp_registry_file_header *header,
59                              prs_struct *ps,
60                              int depth)
61 {
62         if (!header)
63                 return false;
64
65         prs_debug(ps, depth, desc, "reg_parse_header");
66         depth++;
67
68         if (!prs_uint32("signature", ps, depth, &header->signature))
69                 return false;
70
71         if (!prs_uint32("version", ps, depth, &header->version))
72                 return false;
73
74         return true;
75 }
76
77 /****************************************************************
78 ****************************************************************/
79
80 static bool reg_parse_and_verify_ucs2_char(const char *desc,
81                                            char character,
82                                            prs_struct *ps,
83                                            int depth)
84 {
85         uint16_t tmp;
86
87         if (!prs_uint16(desc, ps, depth, &tmp))
88                 return false;
89
90         if (tmp != UCS2_CHAR(character))
91                 return false;
92
93         return true;
94 }
95
96 /****************************************************************
97 ****************************************************************/
98
99 static bool reg_parse_init(prs_struct *ps, int depth)
100 {
101         return reg_parse_and_verify_ucs2_char("initiator '['", '[',
102                                               ps, depth);
103 }
104
105 /****************************************************************
106 ****************************************************************/
107
108 static bool reg_parse_sep(prs_struct *ps, int depth)
109 {
110         return reg_parse_and_verify_ucs2_char("separator ';'", ';',
111                                               ps, depth);
112 }
113
114 /****************************************************************
115 ****************************************************************/
116
117 static bool reg_parse_term(prs_struct *ps, int depth)
118 {
119         return reg_parse_and_verify_ucs2_char("terminator ']'", ']',
120                                               ps, depth);
121 }
122
123
124 /****************************************************************
125 * [key;value;type;size;data]
126 ****************************************************************/
127
128 static bool reg_parse_entry(TALLOC_CTX *mem_ctx,
129                             const char *desc,
130                             struct gp_registry_file_entry *entry,
131                             prs_struct *ps,
132                             int depth)
133 {
134         uint32_t size = 0;
135
136         if (!entry)
137                 return false;
138
139         prs_debug(ps, depth, desc, "reg_parse_entry");
140         depth++;
141
142         ZERO_STRUCTP(entry);
143
144         if (!reg_parse_init(ps, depth))
145                 return false;
146
147         if (!prs_unistr("key", ps, depth, &entry->key))
148                 return false;
149
150         if (!reg_parse_sep(ps, depth))
151                 return false;
152
153         if (!prs_unistr("value", ps, depth, &entry->value))
154                 return false;
155
156         if (!reg_parse_sep(ps, depth))
157                 return false;
158
159         if (!prs_uint32("type", ps, depth, &entry->type))
160                 return false;
161
162         if (!reg_parse_sep(ps, depth))
163                 return false;
164
165         if (!prs_uint32("size", ps, depth, &size))
166                 return false;
167
168         entry->size = size;
169
170         if (!reg_parse_sep(ps, depth))
171                 return false;
172
173         if (entry->size) {
174                 entry->data = TALLOC_ZERO_ARRAY(mem_ctx, uint8, entry->size);
175                 if (!entry->data)
176                         return false;
177         }
178
179         if (!prs_uint8s(false, "data", ps, depth, entry->data, entry->size))
180                 return false;
181
182         if (!reg_parse_term(ps, depth))
183                 return false;
184
185         return true;
186 }
187
188 /****************************************************************
189 ****************************************************************/
190
191 static bool reg_parse_value(TALLOC_CTX *mem_ctx,
192                             char **value,
193                             enum gp_reg_action *action)
194 {
195         if (!*value) {
196                 *action = GP_REG_ACTION_ADD_KEY;
197                 return true;
198         }
199
200         if (strncmp(*value, "**", 2) != 0) {
201                 *action = GP_REG_ACTION_ADD_VALUE;
202                 return true;
203         }
204
205         if (strnequal(*value, "**DelVals.", 10)) {
206                 *action = GP_REG_ACTION_DEL_ALL_VALUES;
207                 return true;
208         }
209
210         if (strnequal(*value, "**Del.", 6)) {
211                 *value = talloc_strdup(mem_ctx, *value + 6);
212                 *action = GP_REG_ACTION_DEL_VALUE;
213                 return true;
214         }
215
216         if (strnequal(*value, "**SecureKey", 11)) {
217                 if (strnequal(*value, "**SecureKey=1", 13)) {
218                         *action = GP_REG_ACTION_SEC_KEY_SET;
219                         return true;
220                 }
221
222  /*************** not tested from here on ***************/
223                 if (strnequal(*value, "**SecureKey=0", 13)) {
224                         smb_panic("not supported: **SecureKey=0");
225                         *action = GP_REG_ACTION_SEC_KEY_RESET;
226                         return true;
227                 }
228                 DEBUG(0,("unknown: SecureKey: %s\n", *value));
229                 smb_panic("not supported SecureKey method");
230                 return false;
231         }
232
233         if (strnequal(*value, "**DeleteValues", strlen("**DeleteValues"))) {
234                 smb_panic("not supported: **DeleteValues");
235                 *action = GP_REG_ACTION_DEL_VALUES;
236                 return false;
237         }
238
239         if (strnequal(*value, "**DeleteKeys", strlen("**DeleteKeys"))) {
240                 smb_panic("not supported: **DeleteKeys");
241                 *action = GP_REG_ACTION_DEL_KEYS;
242                 return false;
243         }
244
245         DEBUG(0,("unknown value: %s\n", *value));
246         smb_panic(*value);
247         return false;
248 }
249
250 /****************************************************************
251 ****************************************************************/
252
253 static bool gp_reg_entry_from_file_entry(TALLOC_CTX *mem_ctx,
254                                          struct gp_registry_file_entry *file_entry,
255                                          struct gp_registry_entry **reg_entry)
256 {
257         struct registry_value *data = NULL;
258         struct gp_registry_entry *entry = NULL;
259         char *key = NULL;
260         char *value = NULL;
261         enum gp_reg_action action = GP_REG_ACTION_NONE;
262         size_t converted_size;
263
264         ZERO_STRUCTP(*reg_entry);
265
266         data = TALLOC_ZERO_P(mem_ctx, struct registry_value);
267         if (!data)
268                 return false;
269
270         if (strlen_w((const smb_ucs2_t *)file_entry->key.buffer) <= 0)
271                 return false;
272
273         if (!pull_ucs2_talloc(mem_ctx, &key, file_entry->key.buffer,
274                               &converted_size))
275         {
276                 return false;
277         }
278
279         if (strlen_w((const smb_ucs2_t *)file_entry->value.buffer) > 0 &&
280             !pull_ucs2_talloc(mem_ctx, &value, file_entry->value.buffer,
281                               &converted_size))
282         {
283                         return false;
284         }
285
286         if (!reg_parse_value(mem_ctx, &value, &action))
287                 return false;
288
289         data->type = file_entry->type;
290
291         switch (data->type) {
292                 case REG_DWORD:
293                         data->v.dword = atoi((char *)file_entry->data);
294                         break;
295                 case REG_BINARY:
296                         data->v.binary = data_blob_talloc(mem_ctx,
297                                                           file_entry->data,
298                                                           file_entry->size);
299                         break;
300                 case REG_NONE:
301                         break;
302                 case REG_SZ:
303                         if (!pull_ucs2_talloc(mem_ctx, &data->v.sz.str,
304                                               (const smb_ucs2_t *)
305                                               file_entry->data,
306                                               &data->v.sz.len)) {
307                                 data->v.sz.len = -1;
308                         }
309
310                         break;
311                 case REG_DWORD_BIG_ENDIAN:
312                 case REG_EXPAND_SZ:
313                 case REG_LINK:
314                 case REG_MULTI_SZ:
315                 case REG_QWORD:
316 /*              case REG_DWORD_LITTLE_ENDIAN: */
317 /*              case REG_QWORD_LITTLE_ENDIAN: */
318                         printf("not yet implemented: %d\n", data->type);
319                         return false;
320                 default:
321                         printf("invalid reg type defined: %d\n", data->type);
322                         return false;
323
324         }
325
326         entry = TALLOC_ZERO_P(mem_ctx, struct gp_registry_entry);
327         if (!entry)
328                 return false;
329
330         entry->key = key;
331         entry->value = value;
332         entry->data = data;
333         entry->action = action;
334
335         *reg_entry = entry;
336
337         return true;
338 }
339
340 /****************************************************************
341 * [key;value;type;size;data][key;value;type;size;data]...
342 ****************************************************************/
343
344 static bool reg_parse_entries(TALLOC_CTX *mem_ctx,
345                               const char *desc,
346                               struct gp_registry_entry **entries,
347                               size_t *num_entries,
348                               prs_struct *ps,
349                               int depth)
350 {
351
352         if (!entries || !num_entries)
353                 return false;
354
355         prs_debug(ps, depth, desc, "reg_parse_entries");
356         depth++;
357
358         *entries = NULL;
359         *num_entries = 0;
360
361         while (ps->buffer_size > ps->data_offset) {
362
363                 struct gp_registry_file_entry f_entry;
364                 struct gp_registry_entry *r_entry = NULL;
365
366                 if (!reg_parse_entry(mem_ctx, desc, &f_entry,
367                                      ps, depth))
368                         return false;
369
370                 if (!gp_reg_entry_from_file_entry(mem_ctx,
371                                                   &f_entry,
372                                                   &r_entry))
373                         return false;
374
375                 if (!add_gp_registry_entry_to_array(mem_ctx,
376                                                     r_entry,
377                                                     entries,
378                                                     num_entries))
379                         return false;
380         }
381
382         return true;
383 }
384
385 /****************************************************************
386 ****************************************************************/
387
388 static NTSTATUS reg_parse_registry(TALLOC_CTX *mem_ctx,
389                                    uint32_t flags,
390                                    const char *filename,
391                                    struct gp_registry_entry **entries,
392                                    size_t *num_entries)
393 {
394         uint16_t *buf = NULL;
395         size_t n = 0;
396         NTSTATUS status;
397         prs_struct ps;
398         struct gp_registry_file *reg_file;
399         const char *real_filename = NULL;
400
401         reg_file = TALLOC_ZERO_P(mem_ctx, struct gp_registry_file);
402         NT_STATUS_HAVE_NO_MEMORY(reg_file);
403
404         status = gp_find_file(mem_ctx,
405                               flags,
406                               filename,
407                               GP_REGPOL_FILE,
408                               &real_filename);
409         if (!NT_STATUS_IS_OK(status)) {
410                 TALLOC_FREE(reg_file);
411                 return status;
412         }
413
414         buf = (uint16 *)file_load(real_filename, &n, 0, NULL);
415         if (!buf) {
416                 TALLOC_FREE(reg_file);
417                 return NT_STATUS_CANNOT_LOAD_REGISTRY_FILE;
418         }
419
420         if (!prs_init(&ps, n, mem_ctx, UNMARSHALL)) {
421                 status = NT_STATUS_NO_MEMORY;
422                 goto out;
423         }
424
425         if (!prs_copy_data_in(&ps, (char *)buf, n)) {
426                 status = NT_STATUS_NO_MEMORY;
427                 goto out;
428         }
429
430         prs_set_offset(&ps, 0);
431
432         if (!reg_parse_header("header", &reg_file->header, &ps, 0)) {
433                 status = NT_STATUS_REGISTRY_IO_FAILED;
434                 goto out;
435         }
436
437         if (reg_file->header.signature != GP_REGPOL_FILE_SIGNATURE) {
438                 status = NT_STATUS_INVALID_PARAMETER;
439                 goto out;
440         }
441
442         if (reg_file->header.version != GP_REGPOL_FILE_VERSION) {
443                 status = NT_STATUS_INVALID_PARAMETER;
444                 goto out;
445         }
446
447         if (!reg_parse_entries(mem_ctx, "entries", &reg_file->entries,
448                                &reg_file->num_entries, &ps, 0)) {
449                 status = NT_STATUS_REGISTRY_IO_FAILED;
450                 goto out;
451         }
452
453         *entries = reg_file->entries;
454         *num_entries = reg_file->num_entries;
455
456         status = NT_STATUS_OK;
457
458  out:
459         TALLOC_FREE(buf);
460         prs_mem_free(&ps);
461
462         return status;
463 }
464
465 /****************************************************************
466 ****************************************************************/
467
468 static WERROR reg_apply_registry(TALLOC_CTX *mem_ctx,
469                                  const struct nt_user_token *token,
470                                  struct registry_key *root_key,
471                                  uint32_t flags,
472                                  struct gp_registry_entry *entries,
473                                  size_t num_entries)
474 {
475         struct gp_registry_context *reg_ctx = NULL;
476         WERROR werr;
477         size_t i;
478
479         if (num_entries == 0) {
480                 return WERR_OK;
481         }
482
483 #if 0
484         if (flags & GPO_LIST_FLAG_MACHINE) {
485                 werr = gp_init_reg_ctx(mem_ctx, KEY_HKLM, REG_KEY_WRITE,
486                                        get_system_token(),
487                                        &reg_ctx);
488         } else {
489                 werr = gp_init_reg_ctx(mem_ctx, KEY_HKCU, REG_KEY_WRITE,
490                                        token,
491                                        &reg_ctx);
492         }
493         W_ERROR_NOT_OK_RETURN(werr);
494 #endif
495         for (i=0; i<num_entries; i++) {
496
497                 /* FIXME: maybe we should check here if we attempt to go beyond
498                  * the 4 allowed reg keys */
499
500                 werr = reg_apply_registry_entry(mem_ctx, root_key,
501                                                 reg_ctx,
502                                                 &(entries)[i],
503                                                 token, flags);
504                 if (!W_ERROR_IS_OK(werr)) {
505                         DEBUG(0,("failed to apply registry: %s\n",
506                                 win_errstr(werr)));
507                         goto done;
508                 }
509         }
510
511 done:
512         gp_free_reg_ctx(reg_ctx);
513         return werr;
514 }
515
516
517 /****************************************************************
518 ****************************************************************/
519
520 static NTSTATUS registry_process_group_policy(ADS_STRUCT *ads,
521                                               TALLOC_CTX *mem_ctx,
522                                               uint32_t flags,
523                                               struct registry_key *root_key,
524                                               const struct nt_user_token *token,
525                                               struct GROUP_POLICY_OBJECT *gpo,
526                                               const char *extension_guid,
527                                               const char *snapin_guid)
528 {
529         NTSTATUS status;
530         WERROR werr;
531         struct gp_registry_entry *entries = NULL;
532         size_t num_entries = 0;
533         char *unix_path = NULL;
534
535         debug_gpext_header(0, "registry_process_group_policy", flags, gpo,
536                            extension_guid, snapin_guid);
537
538         status = gpo_get_unix_path(mem_ctx, cache_path(GPO_CACHE_DIR), gpo, &unix_path);
539         NT_STATUS_NOT_OK_RETURN(status);
540
541         status = reg_parse_registry(mem_ctx,
542                                     flags,
543                                     unix_path,
544                                     &entries,
545                                     &num_entries);
546         if (!NT_STATUS_IS_OK(status)) {
547                 DEBUG(0,("failed to parse registry: %s\n",
548                         nt_errstr(status)));
549                 return status;
550         }
551
552         dump_reg_entries(flags, "READ", entries, num_entries);
553
554         werr = reg_apply_registry(mem_ctx, token, root_key, flags,
555                                   entries, num_entries);
556         if (!W_ERROR_IS_OK(werr)) {
557                 DEBUG(0,("failed to apply registry: %s\n",
558                         win_errstr(werr)));
559                 return werror_to_ntstatus(werr);
560         }
561
562         return NT_STATUS_OK;
563 }
564
565 /****************************************************************
566 ****************************************************************/
567
568 static NTSTATUS registry_get_reg_config(TALLOC_CTX *mem_ctx,
569                                         struct gp_extension_reg_info **reg_info)
570 {
571         NTSTATUS status;
572         struct gp_extension_reg_info *info = NULL;
573         struct gp_extension_reg_table table[] = {
574                 { "ProcessGroupPolicy", REG_SZ, "registry_process_group_policy" },
575                 { NULL, REG_NONE, NULL }
576         };
577
578         info = TALLOC_ZERO_P(mem_ctx, struct gp_extension_reg_info);
579         NT_STATUS_HAVE_NO_MEMORY(info);
580
581         status = gp_ext_info_add_entry(mem_ctx, GP_EXT_NAME,
582                                        GP_EXT_GUID_REGISTRY,
583                                        table, info);
584         NT_STATUS_NOT_OK_RETURN(status);
585
586         *reg_info = info;
587
588         return NT_STATUS_OK;
589 }
590
591 /****************************************************************
592 ****************************************************************/
593
594 static NTSTATUS registry_initialize(TALLOC_CTX *mem_ctx)
595 {
596         return NT_STATUS_OK;
597 }
598
599 /****************************************************************
600 ****************************************************************/
601
602 static NTSTATUS registry_shutdown(void)
603 {
604         NTSTATUS status;
605
606         status = unregister_gp_extension(GP_EXT_NAME);
607         if (NT_STATUS_IS_OK(status)) {
608                 return status;
609         }
610
611         TALLOC_FREE(ctx);
612
613         return NT_STATUS_OK;
614 }
615
616 /****************************************************************
617 ****************************************************************/
618
619 static struct gp_extension_methods registry_methods = {
620         .initialize             = registry_initialize,
621         .process_group_policy   = registry_process_group_policy,
622         .get_reg_config         = registry_get_reg_config,
623         .shutdown               = registry_shutdown
624 };
625
626 /****************************************************************
627 ****************************************************************/
628
629 NTSTATUS gpext_registry_init(void)
630 {
631         NTSTATUS status;
632
633         ctx = talloc_init("gpext_registry_init");
634         NT_STATUS_HAVE_NO_MEMORY(ctx);
635
636         status = register_gp_extension(ctx, SMB_GPEXT_INTERFACE_VERSION,
637                                        GP_EXT_NAME, GP_EXT_GUID_REGISTRY,
638                                        &registry_methods);
639         if (!NT_STATUS_IS_OK(status)) {
640                 TALLOC_FREE(ctx);
641         }
642
643         return status;
644 }