2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Guenther Deschner 2007-2008,2010
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/>.
21 #include "../libgpo/gpo_ini.h"
22 #include "../libgpo/gpo.h"
23 #include "libgpo/gpo_proto.h"
25 #include "../librpc/gen_ndr/ndr_preg.h"
26 #include "libgpo/gpext/gpext.h"
28 #define GP_EXT_NAME "registry"
30 /* more info can be found at:
31 * http://msdn2.microsoft.com/en-us/library/aa374407.aspx */
33 #define GP_REGPOL_FILE "Registry.pol"
35 #define GP_REGPOL_FILE_SIGNATURE 0x67655250 /* 'PReg' */
36 #define GP_REGPOL_FILE_VERSION 1
38 static TALLOC_CTX *ctx = NULL;
40 NTSTATUS gpext_registry_init(TALLOC_CTX *mem_ctx);
42 /****************************************************************
43 ****************************************************************/
45 static bool reg_parse_value(TALLOC_CTX *mem_ctx,
47 enum gp_reg_action *action)
50 *action = GP_REG_ACTION_ADD_KEY;
54 if (strncmp(*value, "**", 2) != 0) {
55 *action = GP_REG_ACTION_ADD_VALUE;
59 if (strnequal(*value, "**DelVals.", 10)) {
60 *action = GP_REG_ACTION_DEL_ALL_VALUES;
64 if (strnequal(*value, "**Del.", 6)) {
65 *value = talloc_strdup(mem_ctx, *value + 6);
66 *action = GP_REG_ACTION_DEL_VALUE;
70 if (strnequal(*value, "**SecureKey", 11)) {
71 if (strnequal(*value, "**SecureKey=1", 13)) {
72 *action = GP_REG_ACTION_SEC_KEY_SET;
76 /*************** not tested from here on ***************/
77 if (strnequal(*value, "**SecureKey=0", 13)) {
78 smb_panic("not supported: **SecureKey=0");
79 *action = GP_REG_ACTION_SEC_KEY_RESET;
82 DEBUG(0,("unknown: SecureKey: %s\n", *value));
83 smb_panic("not supported SecureKey method");
87 if (strnequal(*value, "**DeleteValues", strlen("**DeleteValues"))) {
88 smb_panic("not supported: **DeleteValues");
89 *action = GP_REG_ACTION_DEL_VALUES;
93 if (strnequal(*value, "**DeleteKeys", strlen("**DeleteKeys"))) {
94 smb_panic("not supported: **DeleteKeys");
95 *action = GP_REG_ACTION_DEL_KEYS;
99 DEBUG(0,("unknown value: %s\n", *value));
104 /****************************************************************
105 ****************************************************************/
107 static bool gp_reg_entry_from_file_entry(TALLOC_CTX *mem_ctx,
108 struct preg_entry *r,
109 struct gp_registry_entry **reg_entry)
111 struct registry_value *data = NULL;
112 struct gp_registry_entry *entry = NULL;
113 enum gp_reg_action action = GP_REG_ACTION_NONE;
115 ZERO_STRUCTP(*reg_entry);
117 data = talloc_zero(mem_ctx, struct registry_value);
121 data->type = r->type;
122 data->data = data_blob_talloc(data, r->data, r->size);
124 entry = talloc_zero(mem_ctx, struct gp_registry_entry);
128 if (!reg_parse_value(mem_ctx, &r->valuename, &action))
131 entry->key = talloc_strdup(entry, r->keyname);
132 entry->value = talloc_strdup(entry, r->valuename);
134 entry->action = action;
141 /****************************************************************
142 ****************************************************************/
144 static NTSTATUS reg_parse_registry(TALLOC_CTX *mem_ctx,
146 const char *filename,
147 struct gp_registry_entry **entries_p,
148 size_t *num_entries_p)
152 enum ndr_err_code ndr_err;
153 const char *real_filename = NULL;
155 struct gp_registry_entry *entries = NULL;
156 size_t num_entries = 0;
159 status = gp_find_file(mem_ctx,
164 if (!NT_STATUS_IS_OK(status)) {
168 blob.data = (uint8_t *)file_load(real_filename, &blob.length, 0, NULL);
170 return NT_STATUS_CANNOT_LOAD_REGISTRY_FILE;
173 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &r,
174 (ndr_pull_flags_fn_t)ndr_pull_preg_file);
175 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
176 status = ndr_map_error2ntstatus(ndr_err);
180 if (flags & GPO_INFO_FLAG_VERBOSE) {
181 NDR_PRINT_DEBUG(preg_file, &r);
184 if (!strequal(r.header.signature, "PReg")) {
185 status = NT_STATUS_INVALID_PARAMETER;
189 if (r.header.version != GP_REGPOL_FILE_VERSION) {
190 status = NT_STATUS_INVALID_PARAMETER;
194 for (i=0; i < r.num_entries; i++) {
196 struct gp_registry_entry *r_entry = NULL;
198 if (!gp_reg_entry_from_file_entry(mem_ctx,
201 status = NT_STATUS_NO_MEMORY;
205 if (!add_gp_registry_entry_to_array(mem_ctx,
209 status = NT_STATUS_NO_MEMORY;
214 *entries_p = entries;
215 *num_entries_p = num_entries;
217 status = NT_STATUS_OK;
220 data_blob_free(&blob);
224 /****************************************************************
225 ****************************************************************/
227 static WERROR reg_apply_registry(TALLOC_CTX *mem_ctx,
228 const struct security_token *token,
229 struct registry_key *root_key,
231 struct gp_registry_entry *entries,
234 struct gp_registry_context *reg_ctx = NULL;
238 if (num_entries == 0) {
243 if (flags & GPO_LIST_FLAG_MACHINE) {
244 werr = gp_init_reg_ctx(mem_ctx, KEY_HKLM, REG_KEY_WRITE,
248 werr = gp_init_reg_ctx(mem_ctx, KEY_HKCU, REG_KEY_WRITE,
252 W_ERROR_NOT_OK_RETURN(werr);
254 for (i=0; i<num_entries; i++) {
256 /* FIXME: maybe we should check here if we attempt to go beyond
257 * the 4 allowed reg keys */
259 werr = reg_apply_registry_entry(mem_ctx, root_key,
263 if (!W_ERROR_IS_OK(werr)) {
264 DEBUG(0,("failed to apply registry: %s\n",
271 gp_free_reg_ctx(reg_ctx);
276 /****************************************************************
277 ****************************************************************/
279 static NTSTATUS registry_process_group_policy(TALLOC_CTX *mem_ctx,
281 struct registry_key *root_key,
282 const struct security_token *token,
283 const struct GROUP_POLICY_OBJECT *deleted_gpo_list,
284 const struct GROUP_POLICY_OBJECT *changed_gpo_list)
288 struct gp_registry_entry *entries = NULL;
289 size_t num_entries = 0;
290 char *unix_path = NULL;
291 const struct GROUP_POLICY_OBJECT *gpo;
292 char *gpo_cache_path = cache_path(GPO_CACHE_DIR);
293 if (gpo_cache_path == NULL) {
294 return NT_STATUS_NO_MEMORY;
297 /* implementation of the policy callback function, see
298 * http://msdn.microsoft.com/en-us/library/aa373494%28v=vs.85%29.aspx
299 * for details - gd */
301 /* for now do not process the list of deleted group policies
303 for (gpo = deleted_gpo_list; gpo; gpo = gpo->next) {
308 for (gpo = changed_gpo_list; gpo; gpo = gpo->next) {
310 gpext_debug_header(0, "registry_process_group_policy", flags,
311 gpo, GP_EXT_GUID_REGISTRY, NULL);
313 status = gpo_get_unix_path(mem_ctx, gpo_cache_path,
315 if (!NT_STATUS_IS_OK(status)) {
316 goto err_cache_path_free;
319 status = reg_parse_registry(mem_ctx,
324 if (!NT_STATUS_IS_OK(status)) {
325 DEBUG(0,("failed to parse registry: %s\n",
327 goto err_cache_path_free;
330 dump_reg_entries(flags, "READ", entries, num_entries);
332 werr = reg_apply_registry(mem_ctx, token, root_key, flags,
333 entries, num_entries);
334 if (!W_ERROR_IS_OK(werr)) {
335 DEBUG(0,("failed to apply registry: %s\n",
337 status = werror_to_ntstatus(werr);
338 goto err_cache_path_free;
341 status = NT_STATUS_OK;
344 talloc_free(gpo_cache_path);
345 talloc_free(entries);
349 /****************************************************************
350 ****************************************************************/
352 static NTSTATUS registry_get_reg_config(TALLOC_CTX *mem_ctx,
353 struct gp_extension_reg_info **reg_info)
356 struct gp_extension_reg_info *info = NULL;
357 struct gp_extension_reg_table table[] = {
358 { "ProcessGroupPolicy", REG_SZ, "registry_process_group_policy" },
359 { NULL, REG_NONE, NULL }
362 info = talloc_zero(mem_ctx, struct gp_extension_reg_info);
363 NT_STATUS_HAVE_NO_MEMORY(info);
365 status = gpext_info_add_entry(mem_ctx, GP_EXT_NAME,
366 GP_EXT_GUID_REGISTRY,
368 NT_STATUS_NOT_OK_RETURN(status);
375 /****************************************************************
376 ****************************************************************/
378 static NTSTATUS registry_initialize(TALLOC_CTX *mem_ctx)
383 /****************************************************************
384 ****************************************************************/
386 static NTSTATUS registry_shutdown(void)
390 status = gpext_unregister_gp_extension(GP_EXT_NAME);
391 if (NT_STATUS_IS_OK(status)) {
400 /****************************************************************
401 ****************************************************************/
403 static struct gp_extension_methods registry_methods = {
404 .initialize = registry_initialize,
405 .process_group_policy = registry_process_group_policy,
406 .get_reg_config = registry_get_reg_config,
407 .shutdown = registry_shutdown
410 /****************************************************************
411 ****************************************************************/
413 NTSTATUS gpext_registry_init(TALLOC_CTX *mem_ctx)
417 ctx = talloc_init("gpext_registry_init");
418 NT_STATUS_HAVE_NO_MEMORY(ctx);
420 status = gpext_register_gp_extension(ctx, SMB_GPEXT_INTERFACE_VERSION,
421 GP_EXT_NAME, GP_EXT_GUID_REGISTRY,
423 if (!NT_STATUS_IS_OK(status)) {