ctdb-tests: Update statd-callout tests to handle both modes
[samba4-gss.git] / source3 / libgpo / gpext / registry.c
blobd04262c4c8437e3418d69c6af1a7efa1c49a2fda
1 /*
2 * Unix SMB/CIFS implementation.
3 * Group Policy Support
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/>.
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 #include "../librpc/gen_ndr/ndr_preg.h"
26 #include "libgpo/gpext/gpext.h"
27 #include "lib/util/util_file.h"
29 #define GP_EXT_NAME "registry"
31 /* more info can be found at:
32 * http://msdn2.microsoft.com/en-us/library/aa374407.aspx */
34 #define GP_REGPOL_FILE "Registry.pol"
36 #define GP_REGPOL_FILE_SIGNATURE 0x67655250 /* 'PReg' */
37 #define GP_REGPOL_FILE_VERSION 1
39 static TALLOC_CTX *ctx = NULL;
41 NTSTATUS gpext_registry_init(TALLOC_CTX *mem_ctx);
43 /****************************************************************
44 ****************************************************************/
46 static bool reg_parse_value(TALLOC_CTX *mem_ctx,
47 const char **value,
48 enum gp_reg_action *action)
50 if (!*value) {
51 *action = GP_REG_ACTION_ADD_KEY;
52 return true;
55 if (strncmp(*value, "**", 2) != 0) {
56 *action = GP_REG_ACTION_ADD_VALUE;
57 return true;
60 if (strnequal(*value, "**DelVals.", 10)) {
61 *action = GP_REG_ACTION_DEL_ALL_VALUES;
62 return true;
65 if (strnequal(*value, "**Del.", 6)) {
66 *value = talloc_strdup(mem_ctx, *value + 6);
67 *action = GP_REG_ACTION_DEL_VALUE;
68 return true;
71 if (strnequal(*value, "**SecureKey", 11)) {
72 if (strnequal(*value, "**SecureKey=1", 13)) {
73 *action = GP_REG_ACTION_SEC_KEY_SET;
74 return true;
77 /*************** not tested from here on ***************/
78 if (strnequal(*value, "**SecureKey=0", 13)) {
79 smb_panic("not supported: **SecureKey=0");
80 *action = GP_REG_ACTION_SEC_KEY_RESET;
81 return true;
83 DEBUG(0,("unknown: SecureKey: %s\n", *value));
84 smb_panic("not supported SecureKey method");
85 return false;
88 if (strnequal(*value, "**DeleteValues", strlen("**DeleteValues"))) {
89 smb_panic("not supported: **DeleteValues");
90 *action = GP_REG_ACTION_DEL_VALUES;
91 return false;
94 if (strnequal(*value, "**DeleteKeys", strlen("**DeleteKeys"))) {
95 smb_panic("not supported: **DeleteKeys");
96 *action = GP_REG_ACTION_DEL_KEYS;
97 return false;
100 DEBUG(0,("unknown value: %s\n", *value));
101 smb_panic(*value);
102 return false;
105 /****************************************************************
106 ****************************************************************/
108 static bool gp_reg_entry_from_file_entry(TALLOC_CTX *mem_ctx,
109 struct preg_entry *r,
110 struct gp_registry_entry **reg_entry)
112 struct registry_value *data = NULL;
113 struct gp_registry_entry *entry = NULL;
114 enum gp_reg_action action = GP_REG_ACTION_NONE;
115 enum ndr_err_code ndr_err;
117 ZERO_STRUCTP(*reg_entry);
119 data = talloc_zero(mem_ctx, struct registry_value);
120 if (!data)
121 return false;
123 data->type = r->type;
125 ndr_err = ndr_push_union_blob(
126 &data->data,
127 mem_ctx,
128 &r->data,
129 r->type,
130 (ndr_push_flags_fn_t)ndr_push_winreg_Data);
131 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
132 DBG_WARNING("ndr_push_winreg_Data failed: %s\n",
133 ndr_errstr(ndr_err));
134 return false;
137 entry = talloc_zero(mem_ctx, struct gp_registry_entry);
138 if (!entry)
139 return false;
141 if (!reg_parse_value(mem_ctx, &r->valuename, &action))
142 return false;
144 entry->key = talloc_strdup(entry, r->keyname);
145 entry->value = talloc_strdup(entry, r->valuename);
146 entry->data = data;
147 entry->action = action;
149 *reg_entry = entry;
151 return true;
154 /****************************************************************
155 ****************************************************************/
157 static NTSTATUS reg_parse_registry(TALLOC_CTX *mem_ctx,
158 uint32_t flags,
159 const char *filename,
160 struct gp_registry_entry **entries_p,
161 size_t *num_entries_p)
163 DATA_BLOB blob;
164 NTSTATUS status;
165 enum ndr_err_code ndr_err;
166 const char *real_filename = NULL;
167 struct preg_file r;
168 struct gp_registry_entry *entries = NULL;
169 size_t num_entries = 0;
170 int i;
172 status = gp_find_file(mem_ctx,
173 flags,
174 filename,
175 GP_REGPOL_FILE,
176 &real_filename);
177 if (!NT_STATUS_IS_OK(status)) {
178 return status;
181 blob.data = (uint8_t *)file_load(real_filename, &blob.length, 0, NULL);
182 if (!blob.data) {
183 return NT_STATUS_CANNOT_LOAD_REGISTRY_FILE;
186 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &r,
187 (ndr_pull_flags_fn_t)ndr_pull_preg_file);
188 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
189 status = ndr_map_error2ntstatus(ndr_err);
190 goto out;
193 if (flags & GPO_INFO_FLAG_VERBOSE) {
194 NDR_PRINT_DEBUG(preg_file, &r);
197 if (!strequal(r.header.signature, "PReg")) {
198 status = NT_STATUS_INVALID_PARAMETER;
199 goto out;
202 if (r.header.version != GP_REGPOL_FILE_VERSION) {
203 status = NT_STATUS_INVALID_PARAMETER;
204 goto out;
207 for (i=0; i < r.num_entries; i++) {
209 struct gp_registry_entry *r_entry = NULL;
211 if (!gp_reg_entry_from_file_entry(mem_ctx,
212 &r.entries[i],
213 &r_entry)) {
214 status = NT_STATUS_NO_MEMORY;
215 goto out;
218 if (!add_gp_registry_entry_to_array(mem_ctx,
219 r_entry,
220 &entries,
221 &num_entries)) {
222 status = NT_STATUS_NO_MEMORY;
223 goto out;
227 *entries_p = entries;
228 *num_entries_p = num_entries;
230 status = NT_STATUS_OK;
232 out:
233 data_blob_free(&blob);
234 return status;
237 /****************************************************************
238 ****************************************************************/
240 static WERROR reg_apply_registry(TALLOC_CTX *mem_ctx,
241 const struct security_token *token,
242 struct registry_key *root_key,
243 uint32_t flags,
244 struct gp_registry_entry *entries,
245 size_t num_entries)
247 struct gp_registry_context *reg_ctx = NULL;
248 WERROR werr;
249 size_t i;
251 if (num_entries == 0) {
252 return WERR_OK;
255 #if 0
256 if (flags & GPO_LIST_FLAG_MACHINE) {
257 werr = gp_init_reg_ctx(mem_ctx, KEY_HKLM, REG_KEY_WRITE,
258 get_system_token(),
259 &reg_ctx);
260 } else {
261 werr = gp_init_reg_ctx(mem_ctx, KEY_HKCU, REG_KEY_WRITE,
262 token,
263 &reg_ctx);
265 W_ERROR_NOT_OK_RETURN(werr);
266 #endif
267 for (i=0; i<num_entries; i++) {
269 /* FIXME: maybe we should check here if we attempt to go beyond
270 * the 4 allowed reg keys */
272 werr = reg_apply_registry_entry(mem_ctx, root_key,
273 reg_ctx,
274 &(entries)[i],
275 token, flags);
276 if (!W_ERROR_IS_OK(werr)) {
277 DEBUG(0,("failed to apply registry: %s\n",
278 win_errstr(werr)));
279 goto done;
283 done:
284 gp_free_reg_ctx(reg_ctx);
285 return werr;
289 /****************************************************************
290 ****************************************************************/
292 static NTSTATUS registry_process_group_policy(TALLOC_CTX *mem_ctx,
293 uint32_t flags,
294 struct registry_key *root_key,
295 const struct security_token *token,
296 const struct GROUP_POLICY_OBJECT *deleted_gpo_list,
297 const struct GROUP_POLICY_OBJECT *changed_gpo_list)
299 NTSTATUS status;
300 WERROR werr;
301 struct gp_registry_entry *entries = NULL;
302 size_t num_entries = 0;
303 char *unix_path = NULL;
304 const struct GROUP_POLICY_OBJECT *gpo;
305 char *gpo_cache_path = cache_path(talloc_tos(), GPO_CACHE_DIR);
306 if (gpo_cache_path == NULL) {
307 return NT_STATUS_NO_MEMORY;
310 /* implementation of the policy callback function, see
311 * http://msdn.microsoft.com/en-us/library/aa373494%28v=vs.85%29.aspx
312 * for details - gd */
314 /* for now do not process the list of deleted group policies
316 for (gpo = deleted_gpo_list; gpo; gpo = gpo->next) {
321 for (gpo = changed_gpo_list; gpo; gpo = gpo->next) {
323 gpext_debug_header(0, "registry_process_group_policy", flags,
324 gpo, GP_EXT_GUID_REGISTRY, NULL);
326 status = gpo_get_unix_path(mem_ctx, gpo_cache_path,
327 gpo, &unix_path);
328 if (!NT_STATUS_IS_OK(status)) {
329 goto err_cache_path_free;
332 status = reg_parse_registry(mem_ctx,
333 flags,
334 unix_path,
335 &entries,
336 &num_entries);
337 if (!NT_STATUS_IS_OK(status)) {
338 DEBUG(0,("failed to parse registry: %s\n",
339 nt_errstr(status)));
340 goto err_cache_path_free;
343 dump_reg_entries(flags, "READ", entries, num_entries);
345 werr = reg_apply_registry(mem_ctx, token, root_key, flags,
346 entries, num_entries);
347 if (!W_ERROR_IS_OK(werr)) {
348 DEBUG(0,("failed to apply registry: %s\n",
349 win_errstr(werr)));
350 status = werror_to_ntstatus(werr);
351 goto err_cache_path_free;
354 status = NT_STATUS_OK;
356 err_cache_path_free:
357 talloc_free(gpo_cache_path);
358 talloc_free(entries);
359 return status;
362 /****************************************************************
363 ****************************************************************/
365 static NTSTATUS registry_get_reg_config(TALLOC_CTX *mem_ctx,
366 struct gp_extension_reg_info **reg_info)
368 NTSTATUS status;
369 struct gp_extension_reg_info *info = NULL;
370 struct gp_extension_reg_table table[] = {
371 { "ProcessGroupPolicy", REG_SZ, "registry_process_group_policy" },
372 { NULL, REG_NONE, NULL }
375 info = talloc_zero(mem_ctx, struct gp_extension_reg_info);
376 NT_STATUS_HAVE_NO_MEMORY(info);
378 status = gpext_info_add_entry(mem_ctx, GP_EXT_NAME,
379 GP_EXT_GUID_REGISTRY,
380 table, info);
381 NT_STATUS_NOT_OK_RETURN(status);
383 *reg_info = info;
385 return NT_STATUS_OK;
388 /****************************************************************
389 ****************************************************************/
391 static NTSTATUS registry_initialize(TALLOC_CTX *mem_ctx)
393 return NT_STATUS_OK;
396 /****************************************************************
397 ****************************************************************/
399 static NTSTATUS registry_shutdown(void)
401 NTSTATUS status;
403 status = gpext_unregister_gp_extension(GP_EXT_NAME);
404 if (NT_STATUS_IS_OK(status)) {
405 return status;
408 TALLOC_FREE(ctx);
410 return NT_STATUS_OK;
413 /****************************************************************
414 ****************************************************************/
416 static struct gp_extension_methods registry_methods = {
417 .initialize = registry_initialize,
418 .process_group_policy = registry_process_group_policy,
419 .get_reg_config = registry_get_reg_config,
420 .shutdown = registry_shutdown
423 /****************************************************************
424 ****************************************************************/
426 NTSTATUS gpext_registry_init(TALLOC_CTX *mem_ctx)
428 NTSTATUS status;
430 ctx = talloc_init("gpext_registry_init");
431 NT_STATUS_HAVE_NO_MEMORY(ctx);
433 status = gpext_register_gp_extension(ctx, SMB_GPEXT_INTERFACE_VERSION,
434 GP_EXT_NAME, GP_EXT_GUID_REGISTRY,
435 &registry_methods);
436 if (!NT_STATUS_IS_OK(status)) {
437 TALLOC_FREE(ctx);
440 return status;