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