2 * Unix SMB/CIFS implementation.
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/>.
22 #define GP_EXT_NAME "registry"
24 /* more info can be found at:
25 * http://msdn2.microsoft.com/en-us/library/aa374407.aspx */
27 #define GP_REGPOL_FILE "Registry.pol"
29 #define GP_REGPOL_FILE_SIGNATURE 0x67655250 /* 'PReg' */
30 #define GP_REGPOL_FILE_VERSION 1
32 static TALLOC_CTX *ctx = NULL;
34 struct gp_registry_file_header {
39 struct gp_registry_file_entry {
42 enum winreg_Type type;
47 struct gp_registry_file {
48 struct gp_registry_file_header header;
50 struct gp_registry_entry *entries;
53 /****************************************************************
54 ****************************************************************/
56 static bool reg_parse_header(const char *desc,
57 struct gp_registry_file_header *header,
64 prs_debug(ps, depth, desc, "reg_parse_header");
67 if (!prs_uint32("signature", ps, depth, &header->signature))
70 if (!prs_uint32("version", ps, depth, &header->version))
76 /****************************************************************
77 ****************************************************************/
79 static bool reg_parse_and_verify_ucs2_char(const char *desc,
86 if (!prs_uint16(desc, ps, depth, &tmp))
89 if (tmp != UCS2_CHAR(character))
95 /****************************************************************
96 ****************************************************************/
98 static bool reg_parse_init(prs_struct *ps, int depth)
100 return reg_parse_and_verify_ucs2_char("initiator '['", '[',
104 /****************************************************************
105 ****************************************************************/
107 static bool reg_parse_sep(prs_struct *ps, int depth)
109 return reg_parse_and_verify_ucs2_char("separator ';'", ';',
113 /****************************************************************
114 ****************************************************************/
116 static bool reg_parse_term(prs_struct *ps, int depth)
118 return reg_parse_and_verify_ucs2_char("terminator ']'", ']',
123 /****************************************************************
124 * [key;value;type;size;data]
125 ****************************************************************/
127 static bool reg_parse_entry(TALLOC_CTX *mem_ctx,
129 struct gp_registry_file_entry *entry,
138 prs_debug(ps, depth, desc, "reg_parse_entry");
143 if (!reg_parse_init(ps, depth))
146 if (!prs_unistr("key", ps, depth, &entry->key))
149 if (!reg_parse_sep(ps, depth))
152 if (!prs_unistr("value", ps, depth, &entry->value))
155 if (!reg_parse_sep(ps, depth))
158 if (!prs_uint32("type", ps, depth, &entry->type))
161 if (!reg_parse_sep(ps, depth))
164 if (!prs_uint32("size", ps, depth, &size))
169 if (!reg_parse_sep(ps, depth))
173 entry->data = TALLOC_ZERO_ARRAY(mem_ctx, uint8, entry->size);
178 if (!prs_uint8s(false, "data", ps, depth, entry->data, entry->size))
181 if (!reg_parse_term(ps, depth))
187 /****************************************************************
188 ****************************************************************/
190 static bool reg_parse_value(TALLOC_CTX *mem_ctx,
192 enum gp_reg_action *action)
195 *action = GP_REG_ACTION_ADD_KEY;
199 if (strncmp(*value, "**", 2) != 0) {
200 *action = GP_REG_ACTION_ADD_VALUE;
204 if (strnequal(*value, "**DelVals.", 10)) {
205 *action = GP_REG_ACTION_DEL_ALL_VALUES;
209 if (strnequal(*value, "**Del.", 6)) {
210 *value = talloc_strdup(mem_ctx, *value + 6);
211 *action = GP_REG_ACTION_DEL_VALUE;
215 if (strnequal(*value, "**SecureKey", 11)) {
216 if (strnequal(*value, "**SecureKey=1", 13)) {
217 *action = GP_REG_ACTION_SEC_KEY_SET;
221 /*************** not tested from here on ***************/
222 if (strnequal(*value, "**SecureKey=0", 13)) {
223 smb_panic("not supported: **SecureKey=0");
224 *action = GP_REG_ACTION_SEC_KEY_RESET;
227 DEBUG(0,("unknown: SecureKey: %s\n", *value));
228 smb_panic("not supported SecureKey method");
232 if (strnequal(*value, "**DeleteValues", strlen("**DeleteValues"))) {
233 smb_panic("not supported: **DeleteValues");
234 *action = GP_REG_ACTION_DEL_VALUES;
238 if (strnequal(*value, "**DeleteKeys", strlen("**DeleteKeys"))) {
239 smb_panic("not supported: **DeleteKeys");
240 *action = GP_REG_ACTION_DEL_KEYS;
244 DEBUG(0,("unknown value: %s\n", *value));
249 /****************************************************************
250 ****************************************************************/
252 static bool gp_reg_entry_from_file_entry(TALLOC_CTX *mem_ctx,
253 struct gp_registry_file_entry *file_entry,
254 struct gp_registry_entry **reg_entry)
256 struct registry_value *data = NULL;
257 struct gp_registry_entry *entry = NULL;
260 enum gp_reg_action action = GP_REG_ACTION_NONE;
262 ZERO_STRUCTP(*reg_entry);
264 data = TALLOC_ZERO_P(mem_ctx, struct registry_value);
268 if (strlen_w((const smb_ucs2_t *)file_entry->key.buffer) <= 0)
271 if (!pull_ucs2_talloc(mem_ctx, &key, file_entry->key.buffer))
274 if (strlen_w((const smb_ucs2_t *)file_entry->value.buffer) > 0) {
275 if (!pull_ucs2_talloc(mem_ctx, &value,
276 file_entry->value.buffer))
280 if (!reg_parse_value(mem_ctx, &value, &action))
283 data->type = file_entry->type;
285 switch (data->type) {
287 data->v.dword = atoi((char *)file_entry->data);
290 data->v.binary = data_blob_talloc(mem_ctx,
297 data->v.sz.len = pull_ucs2_talloc(mem_ctx,
299 (const smb_ucs2_t *)file_entry->data);
301 case REG_DWORD_BIG_ENDIAN:
306 /* case REG_DWORD_LITTLE_ENDIAN: */
307 /* case REG_QWORD_LITTLE_ENDIAN: */
308 printf("not yet implemented: %d\n", data->type);
311 printf("invalid reg type defined: %d\n", data->type);
316 entry = TALLOC_ZERO_P(mem_ctx, struct gp_registry_entry);
321 entry->value = value;
323 entry->action = action;
330 /****************************************************************
331 * [key;value;type;size;data][key;value;type;size;data]...
332 ****************************************************************/
334 static bool reg_parse_entries(TALLOC_CTX *mem_ctx,
336 struct gp_registry_entry **entries,
342 if (!entries || !num_entries)
345 prs_debug(ps, depth, desc, "reg_parse_entries");
351 while (ps->buffer_size > ps->data_offset) {
353 struct gp_registry_file_entry f_entry;
354 struct gp_registry_entry *r_entry = NULL;
356 if (!reg_parse_entry(mem_ctx, desc, &f_entry,
360 if (!gp_reg_entry_from_file_entry(mem_ctx,
365 if (!add_gp_registry_entry_to_array(mem_ctx,
375 /****************************************************************
376 ****************************************************************/
378 static NTSTATUS reg_parse_registry(TALLOC_CTX *mem_ctx,
380 const char *filename,
381 struct gp_registry_entry **entries,
384 uint16_t *buf = NULL;
388 struct gp_registry_file *reg_file;
389 const char *real_filename = NULL;
391 reg_file = TALLOC_ZERO_P(mem_ctx, struct gp_registry_file);
392 NT_STATUS_HAVE_NO_MEMORY(reg_file);
394 status = gp_find_file(mem_ctx,
399 if (!NT_STATUS_IS_OK(status)) {
400 TALLOC_FREE(reg_file);
404 buf = (uint16 *)file_load(real_filename, &n, 0);
406 TALLOC_FREE(reg_file);
407 return NT_STATUS_CANNOT_LOAD_REGISTRY_FILE;
410 if (!prs_init(&ps, n, mem_ctx, UNMARSHALL)) {
411 status = NT_STATUS_NO_MEMORY;
415 if (!prs_copy_data_in(&ps, (char *)buf, n)) {
416 status = NT_STATUS_NO_MEMORY;
420 prs_set_offset(&ps, 0);
422 if (!reg_parse_header("header", ®_file->header, &ps, 0)) {
423 status = NT_STATUS_REGISTRY_IO_FAILED;
427 if (reg_file->header.signature != GP_REGPOL_FILE_SIGNATURE) {
428 status = NT_STATUS_INVALID_PARAMETER;
432 if (reg_file->header.version != GP_REGPOL_FILE_VERSION) {
433 status = NT_STATUS_INVALID_PARAMETER;
437 if (!reg_parse_entries(mem_ctx, "entries", ®_file->entries,
438 ®_file->num_entries, &ps, 0)) {
439 status = NT_STATUS_REGISTRY_IO_FAILED;
443 *entries = reg_file->entries;
444 *num_entries = reg_file->num_entries;
446 status = NT_STATUS_OK;
455 /****************************************************************
456 ****************************************************************/
458 static WERROR reg_apply_registry(TALLOC_CTX *mem_ctx,
459 const struct nt_user_token *token,
460 struct registry_key *root_key,
462 struct gp_registry_entry *entries,
465 struct gp_registry_context *reg_ctx = NULL;
469 if (num_entries == 0) {
474 if (flags & GPO_LIST_FLAG_MACHINE) {
475 werr = gp_init_reg_ctx(mem_ctx, KEY_HKLM, REG_KEY_WRITE,
479 werr = gp_init_reg_ctx(mem_ctx, KEY_HKCU, REG_KEY_WRITE,
483 W_ERROR_NOT_OK_RETURN(werr);
485 for (i=0; i<num_entries; i++) {
487 /* FIXME: maybe we should check here if we attempt to go beyond
488 * the 4 allowed reg keys */
490 werr = reg_apply_registry_entry(mem_ctx, root_key,
494 if (!W_ERROR_IS_OK(werr)) {
495 DEBUG(0,("failed to apply registry: %s\n",
502 gp_free_reg_ctx(reg_ctx);
507 /****************************************************************
508 ****************************************************************/
510 static NTSTATUS registry_process_group_policy(ADS_STRUCT *ads,
513 struct registry_key *root_key,
514 const struct nt_user_token *token,
515 struct GROUP_POLICY_OBJECT *gpo,
516 const char *extension_guid,
517 const char *snapin_guid)
521 struct gp_registry_entry *entries = NULL;
522 size_t num_entries = 0;
523 char *unix_path = NULL;
525 debug_gpext_header(0, "registry_process_group_policy", flags, gpo,
526 extension_guid, snapin_guid);
528 status = gpo_get_unix_path(mem_ctx, gpo, &unix_path);
529 NT_STATUS_NOT_OK_RETURN(status);
531 status = reg_parse_registry(mem_ctx,
536 if (!NT_STATUS_IS_OK(status)) {
537 DEBUG(0,("failed to parse registry: %s\n",
542 dump_reg_entries(flags, "READ", entries, num_entries);
544 werr = reg_apply_registry(mem_ctx, token, root_key, flags,
545 entries, num_entries);
546 if (!W_ERROR_IS_OK(werr)) {
547 DEBUG(0,("failed to apply registry: %s\n",
549 return werror_to_ntstatus(werr);
555 /****************************************************************
556 ****************************************************************/
558 static NTSTATUS registry_get_reg_config(TALLOC_CTX *mem_ctx,
559 struct gp_extension_reg_info **reg_info)
562 struct gp_extension_reg_info *info = NULL;
563 struct gp_extension_reg_table table[] = {
564 { "ProcessGroupPolicy", REG_SZ, "registry_process_group_policy" },
565 { NULL, REG_NONE, NULL }
568 info = TALLOC_ZERO_P(mem_ctx, struct gp_extension_reg_info);
569 NT_STATUS_HAVE_NO_MEMORY(info);
571 status = gp_ext_info_add_entry(mem_ctx, GP_EXT_NAME,
572 GP_EXT_GUID_REGISTRY,
574 NT_STATUS_NOT_OK_RETURN(status);
581 /****************************************************************
582 ****************************************************************/
584 static NTSTATUS registry_initialize(TALLOC_CTX *mem_ctx)
589 /****************************************************************
590 ****************************************************************/
592 static NTSTATUS registry_shutdown(void)
596 status = unregister_gp_extension(GP_EXT_NAME);
597 if (NT_STATUS_IS_OK(status)) {
606 /****************************************************************
607 ****************************************************************/
609 static struct gp_extension_methods registry_methods = {
610 .initialize = registry_initialize,
611 .process_group_policy = registry_process_group_policy,
612 .get_reg_config = registry_get_reg_config,
613 .shutdown = registry_shutdown
616 /****************************************************************
617 ****************************************************************/
619 NTSTATUS gpext_registry_init(void)
623 ctx = talloc_init("gpext_registry_init");
624 NT_STATUS_HAVE_NO_MEMORY(ctx);
626 status = register_gp_extension(ctx, SMB_GPEXT_INTERFACE_VERSION,
627 GP_EXT_NAME, GP_EXT_GUID_REGISTRY,
629 if (!NT_STATUS_IS_OK(status)) {