s3: Make adt_tree data definitions private to adt_tree.c
[Samba/gebeck_regimport.git] / source3 / libgpo / gpext / registry.c
blobae2adcc90de6fd54df578ba90f6461dc9a1174ee
1 /*
2 * Unix SMB/CIFS implementation.
3 * Group Policy Support
4 * Copyright (C) Guenther Deschner 2007-2008
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.
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.
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/>.
20 #include "includes.h"
21 #include "../libgpo/gpo_ini.h"
23 #define GP_EXT_NAME "registry"
25 /* more info can be found at:
26 * http://msdn2.microsoft.com/en-us/library/aa374407.aspx */
28 #define GP_REGPOL_FILE "Registry.pol"
30 #define GP_REGPOL_FILE_SIGNATURE 0x67655250 /* 'PReg' */
31 #define GP_REGPOL_FILE_VERSION 1
33 static TALLOC_CTX *ctx = NULL;
35 struct gp_registry_file_header {
36 uint32_t signature;
37 uint32_t version;
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;
48 struct gp_registry_file {
49 struct gp_registry_file_header header;
50 size_t num_entries;
51 struct gp_registry_entry *entries;
54 /****************************************************************
55 ****************************************************************/
57 static bool reg_parse_header(const char *desc,
58 struct gp_registry_file_header *header,
59 prs_struct *ps,
60 int depth)
62 if (!header)
63 return false;
65 prs_debug(ps, depth, desc, "reg_parse_header");
66 depth++;
68 if (!prs_uint32("signature", ps, depth, &header->signature))
69 return false;
71 if (!prs_uint32("version", ps, depth, &header->version))
72 return false;
74 return true;
77 /****************************************************************
78 ****************************************************************/
80 static bool reg_parse_and_verify_ucs2_char(const char *desc,
81 char character,
82 prs_struct *ps,
83 int depth)
85 uint16_t tmp;
87 if (!prs_uint16(desc, ps, depth, &tmp))
88 return false;
90 if (tmp != UCS2_CHAR(character))
91 return false;
93 return true;
96 /****************************************************************
97 ****************************************************************/
99 static bool reg_parse_init(prs_struct *ps, int depth)
101 return reg_parse_and_verify_ucs2_char("initiator '['", '[',
102 ps, depth);
105 /****************************************************************
106 ****************************************************************/
108 static bool reg_parse_sep(prs_struct *ps, int depth)
110 return reg_parse_and_verify_ucs2_char("separator ';'", ';',
111 ps, depth);
114 /****************************************************************
115 ****************************************************************/
117 static bool reg_parse_term(prs_struct *ps, int depth)
119 return reg_parse_and_verify_ucs2_char("terminator ']'", ']',
120 ps, depth);
124 /****************************************************************
125 * [key;value;type;size;data]
126 ****************************************************************/
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)
134 uint32_t size = 0;
136 if (!entry)
137 return false;
139 prs_debug(ps, depth, desc, "reg_parse_entry");
140 depth++;
142 ZERO_STRUCTP(entry);
144 if (!reg_parse_init(ps, depth))
145 return false;
147 if (!prs_unistr("key", ps, depth, &entry->key))
148 return false;
150 if (!reg_parse_sep(ps, depth))
151 return false;
153 if (!prs_unistr("value", ps, depth, &entry->value))
154 return false;
156 if (!reg_parse_sep(ps, depth))
157 return false;
159 if (!prs_uint32("type", ps, depth, &entry->type))
160 return false;
162 if (!reg_parse_sep(ps, depth))
163 return false;
165 if (!prs_uint32("size", ps, depth, &size))
166 return false;
168 entry->size = size;
170 if (!reg_parse_sep(ps, depth))
171 return false;
173 if (entry->size) {
174 entry->data = TALLOC_ZERO_ARRAY(mem_ctx, uint8, entry->size);
175 if (!entry->data)
176 return false;
179 if (!prs_uint8s(false, "data", ps, depth, entry->data, entry->size))
180 return false;
182 if (!reg_parse_term(ps, depth))
183 return false;
185 return true;
188 /****************************************************************
189 ****************************************************************/
191 static bool reg_parse_value(TALLOC_CTX *mem_ctx,
192 char **value,
193 enum gp_reg_action *action)
195 if (!*value) {
196 *action = GP_REG_ACTION_ADD_KEY;
197 return true;
200 if (strncmp(*value, "**", 2) != 0) {
201 *action = GP_REG_ACTION_ADD_VALUE;
202 return true;
205 if (strnequal(*value, "**DelVals.", 10)) {
206 *action = GP_REG_ACTION_DEL_ALL_VALUES;
207 return true;
210 if (strnequal(*value, "**Del.", 6)) {
211 *value = talloc_strdup(mem_ctx, *value + 6);
212 *action = GP_REG_ACTION_DEL_VALUE;
213 return true;
216 if (strnequal(*value, "**SecureKey", 11)) {
217 if (strnequal(*value, "**SecureKey=1", 13)) {
218 *action = GP_REG_ACTION_SEC_KEY_SET;
219 return true;
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;
228 DEBUG(0,("unknown: SecureKey: %s\n", *value));
229 smb_panic("not supported SecureKey method");
230 return false;
233 if (strnequal(*value, "**DeleteValues", strlen("**DeleteValues"))) {
234 smb_panic("not supported: **DeleteValues");
235 *action = GP_REG_ACTION_DEL_VALUES;
236 return false;
239 if (strnequal(*value, "**DeleteKeys", strlen("**DeleteKeys"))) {
240 smb_panic("not supported: **DeleteKeys");
241 *action = GP_REG_ACTION_DEL_KEYS;
242 return false;
245 DEBUG(0,("unknown value: %s\n", *value));
246 smb_panic(*value);
247 return false;
250 /****************************************************************
251 ****************************************************************/
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)
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;
264 ZERO_STRUCTP(*reg_entry);
266 data = TALLOC_ZERO_P(mem_ctx, struct registry_value);
267 if (!data)
268 return false;
270 if (strlen_w((const smb_ucs2_t *)file_entry->key.buffer) <= 0)
271 return false;
273 if (!pull_ucs2_talloc(mem_ctx, &key, file_entry->key.buffer,
274 &converted_size))
276 return false;
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))
283 return false;
286 if (!reg_parse_value(mem_ctx, &value, &action))
287 return false;
289 data->type = file_entry->type;
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;
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;
326 entry = TALLOC_ZERO_P(mem_ctx, struct gp_registry_entry);
327 if (!entry)
328 return false;
330 entry->key = key;
331 entry->value = value;
332 entry->data = data;
333 entry->action = action;
335 *reg_entry = entry;
337 return true;
340 /****************************************************************
341 * [key;value;type;size;data][key;value;type;size;data]...
342 ****************************************************************/
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)
352 if (!entries || !num_entries)
353 return false;
355 prs_debug(ps, depth, desc, "reg_parse_entries");
356 depth++;
358 *entries = NULL;
359 *num_entries = 0;
361 while (ps->buffer_size > ps->data_offset) {
363 struct gp_registry_file_entry f_entry;
364 struct gp_registry_entry *r_entry = NULL;
366 if (!reg_parse_entry(mem_ctx, desc, &f_entry,
367 ps, depth))
368 return false;
370 if (!gp_reg_entry_from_file_entry(mem_ctx,
371 &f_entry,
372 &r_entry))
373 return false;
375 if (!add_gp_registry_entry_to_array(mem_ctx,
376 r_entry,
377 entries,
378 num_entries))
379 return false;
382 return true;
385 /****************************************************************
386 ****************************************************************/
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)
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;
401 reg_file = TALLOC_ZERO_P(mem_ctx, struct gp_registry_file);
402 NT_STATUS_HAVE_NO_MEMORY(reg_file);
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;
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;
420 if (!prs_init(&ps, n, mem_ctx, UNMARSHALL)) {
421 status = NT_STATUS_NO_MEMORY;
422 goto out;
425 if (!prs_copy_data_in(&ps, (char *)buf, n)) {
426 status = NT_STATUS_NO_MEMORY;
427 goto out;
430 prs_set_offset(&ps, 0);
432 if (!reg_parse_header("header", &reg_file->header, &ps, 0)) {
433 status = NT_STATUS_REGISTRY_IO_FAILED;
434 goto out;
437 if (reg_file->header.signature != GP_REGPOL_FILE_SIGNATURE) {
438 status = NT_STATUS_INVALID_PARAMETER;
439 goto out;
442 if (reg_file->header.version != GP_REGPOL_FILE_VERSION) {
443 status = NT_STATUS_INVALID_PARAMETER;
444 goto out;
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;
453 *entries = reg_file->entries;
454 *num_entries = reg_file->num_entries;
456 status = NT_STATUS_OK;
458 out:
459 TALLOC_FREE(buf);
460 prs_mem_free(&ps);
462 return status;
465 /****************************************************************
466 ****************************************************************/
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)
475 struct gp_registry_context *reg_ctx = NULL;
476 WERROR werr;
477 size_t i;
479 if (num_entries == 0) {
480 return WERR_OK;
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);
493 W_ERROR_NOT_OK_RETURN(werr);
494 #endif
495 for (i=0; i<num_entries; i++) {
497 /* FIXME: maybe we should check here if we attempt to go beyond
498 * the 4 allowed reg keys */
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;
511 done:
512 gp_free_reg_ctx(reg_ctx);
513 return werr;
517 /****************************************************************
518 ****************************************************************/
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)
529 NTSTATUS status;
530 WERROR werr;
531 struct gp_registry_entry *entries = NULL;
532 size_t num_entries = 0;
533 char *unix_path = NULL;
535 debug_gpext_header(0, "registry_process_group_policy", flags, gpo,
536 extension_guid, snapin_guid);
538 status = gpo_get_unix_path(mem_ctx, cache_path(GPO_CACHE_DIR), gpo, &unix_path);
539 NT_STATUS_NOT_OK_RETURN(status);
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;
552 dump_reg_entries(flags, "READ", entries, num_entries);
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);
562 return NT_STATUS_OK;
565 /****************************************************************
566 ****************************************************************/
568 static NTSTATUS registry_get_reg_config(TALLOC_CTX *mem_ctx,
569 struct gp_extension_reg_info **reg_info)
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 }
578 info = TALLOC_ZERO_P(mem_ctx, struct gp_extension_reg_info);
579 NT_STATUS_HAVE_NO_MEMORY(info);
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);
586 *reg_info = info;
588 return NT_STATUS_OK;
591 /****************************************************************
592 ****************************************************************/
594 static NTSTATUS registry_initialize(TALLOC_CTX *mem_ctx)
596 return NT_STATUS_OK;
599 /****************************************************************
600 ****************************************************************/
602 static NTSTATUS registry_shutdown(void)
604 NTSTATUS status;
606 status = unregister_gp_extension(GP_EXT_NAME);
607 if (NT_STATUS_IS_OK(status)) {
608 return status;
611 TALLOC_FREE(ctx);
613 return NT_STATUS_OK;
616 /****************************************************************
617 ****************************************************************/
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
626 /****************************************************************
627 ****************************************************************/
629 NTSTATUS gpext_registry_init(void)
631 NTSTATUS status;
633 ctx = talloc_init("gpext_registry_init");
634 NT_STATUS_HAVE_NO_MEMORY(ctx);
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);
643 return status;