2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Guenther Deschner 2007
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"
26 #define GP_EXT_NAME "scripts"
28 #define KEY_GP_SCRIPTS "Software\\Policies\\Microsoft\\Windows\\System\\Scripts"
30 #define GP_SCRIPTS_INI "Scripts/scripts.ini"
32 #define GP_SCRIPTS_INI_STARTUP "Startup"
33 #define GP_SCRIPTS_INI_SHUTDOWN "Shutdown"
34 #define GP_SCRIPTS_INI_LOGON "Logon"
35 #define GP_SCRIPTS_INI_LOGOFF "Logoff"
37 #define GP_SCRIPTS_SECTION_CMDLINE "cmdline"
38 #define GP_SCRIPTS_SECTION_PARAMETERS "parameters"
40 #define GP_SCRIPTS_REG_VAL_SCRIPT "Script"
41 #define GP_SCRIPTS_REG_VAL_PARAMETERS "Parameters"
42 #define GP_SCRIPTS_REG_VAL_EXECTIME "ExecTime"
44 static TALLOC_CTX *ctx = NULL;
46 /****************************************************************
47 ****************************************************************/
49 static NTSTATUS scripts_get_reg_config(TALLOC_CTX *mem_ctx,
50 struct gp_extension_reg_info **reg_info)
53 struct gp_extension_reg_info *info = NULL;
55 struct gp_extension_reg_table table[] = {
56 { "ProcessGroupPolicy", REG_SZ, "scripts_process_group_policy" },
57 { "NoGPOListChanges", REG_DWORD, "1" },
58 { "NoSlowLink", REG_DWORD, "1" },
59 { "NotifyLinkTransition", REG_DWORD, "1" },
60 { NULL, REG_NONE, NULL },
63 info = TALLOC_ZERO_P(mem_ctx, struct gp_extension_reg_info);
64 NT_STATUS_HAVE_NO_MEMORY(info);
66 status = gp_ext_info_add_entry(mem_ctx, GP_EXT_NAME,
69 NT_STATUS_NOT_OK_RETURN(status);
76 /****************************************************************
77 ****************************************************************/
79 static NTSTATUS generate_gp_registry_entry(TALLOC_CTX *mem_ctx,
84 enum gp_reg_action action,
85 struct gp_registry_entry **entry_out)
87 struct gp_registry_entry *entry = NULL;
88 struct registry_value *data = NULL;
90 entry = TALLOC_ZERO_P(mem_ctx, struct gp_registry_entry);
91 NT_STATUS_HAVE_NO_MEMORY(entry);
93 data = TALLOC_ZERO_P(mem_ctx, struct registry_value);
94 NT_STATUS_HAVE_NO_MEMORY(data);
96 data->type = data_type;
99 data->v.qword = *(uint64_t *)data_p;
102 data->v.sz.str = talloc_strdup(mem_ctx, (char *)data_p);
103 data->v.sz.len = strlen(data->v.sz.str);
106 return NT_STATUS_NOT_SUPPORTED;
111 entry->action = action;
112 entry->value = talloc_strdup(mem_ctx, value);
113 NT_STATUS_HAVE_NO_MEMORY(entry->value);
120 /****************************************************************
121 ****************************************************************/
123 static NTSTATUS scripts_parse_ini_section(struct gp_inifile_context *ini_ctx,
126 struct gp_registry_entry **entries,
129 NTSTATUS status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
135 const char *key = NULL;
137 const char *count = NULL;
138 char *parameters = NULL;
140 count = talloc_asprintf(ini_ctx->mem_ctx, "%d", i);
141 NT_STATUS_HAVE_NO_MEMORY(count);
143 key = talloc_asprintf(ini_ctx->mem_ctx, "%s:%s%s",
145 GP_SCRIPTS_SECTION_CMDLINE);
146 NT_STATUS_HAVE_NO_MEMORY(key);
148 result = gp_inifile_getstring(ini_ctx, key, &script);
149 if (!NT_STATUS_IS_OK(result)) {
153 key = talloc_asprintf(ini_ctx->mem_ctx, "%s:%s%s",
155 GP_SCRIPTS_SECTION_PARAMETERS);
156 NT_STATUS_HAVE_NO_MEMORY(key);
158 result = gp_inifile_getstring(ini_ctx, key, ¶meters);
159 if (!NT_STATUS_IS_OK(result)) {
164 struct gp_registry_entry *entry = NULL;
165 status = generate_gp_registry_entry(ini_ctx->mem_ctx,
167 GP_SCRIPTS_REG_VAL_SCRIPT,
170 GP_REG_ACTION_ADD_VALUE,
172 NT_STATUS_NOT_OK_RETURN(status);
173 if (!add_gp_registry_entry_to_array(ini_ctx->mem_ctx,
177 return NT_STATUS_NO_MEMORY;
181 struct gp_registry_entry *entry = NULL;
182 status = generate_gp_registry_entry(ini_ctx->mem_ctx,
184 GP_SCRIPTS_REG_VAL_PARAMETERS,
187 GP_REG_ACTION_ADD_VALUE,
189 NT_STATUS_NOT_OK_RETURN(status);
190 if (!add_gp_registry_entry_to_array(ini_ctx->mem_ctx,
194 return NT_STATUS_NO_MEMORY;
198 struct gp_registry_entry *entry = NULL;
199 status = generate_gp_registry_entry(ini_ctx->mem_ctx,
201 GP_SCRIPTS_REG_VAL_EXECTIME,
204 GP_REG_ACTION_ADD_VALUE,
206 NT_STATUS_NOT_OK_RETURN(status);
207 if (!add_gp_registry_entry_to_array(ini_ctx->mem_ctx,
211 return NT_STATUS_NO_MEMORY;
214 status = NT_STATUS_OK;
221 /****************************************************************
222 ****************************************************************/
224 static WERROR scripts_store_reg_gpovals(TALLOC_CTX *mem_ctx,
225 struct registry_key *key,
226 struct GROUP_POLICY_OBJECT *gpo)
231 return WERR_INVALID_PARAM;
234 werr = gp_store_reg_val_sz(mem_ctx, key, "DisplayName",
236 W_ERROR_NOT_OK_RETURN(werr);
238 werr = gp_store_reg_val_sz(mem_ctx, key, "FileSysPath",
240 W_ERROR_NOT_OK_RETURN(werr);
242 werr = gp_store_reg_val_sz(mem_ctx, key, "GPO-ID",
244 W_ERROR_NOT_OK_RETURN(werr);
246 werr = gp_store_reg_val_sz(mem_ctx, key, "GPOName",
248 W_ERROR_NOT_OK_RETURN(werr);
250 werr = gp_store_reg_val_sz(mem_ctx, key, "SOM-ID",
252 W_ERROR_NOT_OK_RETURN(werr);
257 /****************************************************************
258 ****************************************************************/
260 static WERROR scripts_apply(TALLOC_CTX *mem_ctx,
261 const struct nt_user_token *token,
262 struct registry_key *root_key,
265 struct GROUP_POLICY_OBJECT *gpo,
266 struct gp_registry_entry *entries,
269 struct gp_registry_context *reg_ctx = NULL;
272 const char *keystr = NULL;
275 if (num_entries == 0) {
280 if (flags & GPO_INFO_FLAG_MACHINE) {
281 struct nt_user_token *tmp_token;
283 tmp_token = registry_create_system_token(mem_ctx);
284 W_ERROR_HAVE_NO_MEMORY(tmp_token);
286 werr = gp_init_reg_ctx(mem_ctx, KEY_HKLM, REG_KEY_WRITE,
290 werr = gp_init_reg_ctx(mem_ctx, KEY_HKCU, REG_KEY_WRITE,
294 W_ERROR_NOT_OK_RETURN(werr);
297 keystr = talloc_asprintf(mem_ctx, "%s\\%s\\%d", KEY_GP_SCRIPTS,
299 W_ERROR_HAVE_NO_MEMORY(keystr);
301 reg_deletekey_recursive(mem_ctx, root_key, keystr);
303 werr = gp_store_reg_subkey(mem_ctx, keystr,
304 root_key, &root_key);
305 if (!W_ERROR_IS_OK(werr)) {
309 werr = scripts_store_reg_gpovals(mem_ctx, root_key, gpo);
310 if (!W_ERROR_IS_OK(werr)) {
314 for (i=0; i<num_entries; i++) {
316 werr = reg_apply_registry_entry(mem_ctx, root_key, reg_ctx,
319 if (!W_ERROR_IS_OK(werr)) {
320 DEBUG(0,("failed to apply registry: %s\n",
327 gp_free_reg_ctx(reg_ctx);
331 /****************************************************************
332 ****************************************************************/
334 static NTSTATUS scripts_process_group_policy(ADS_STRUCT *ads,
337 struct registry_key *root_key,
338 const struct nt_user_token *token,
339 struct GROUP_POLICY_OBJECT *gpo,
340 const char *extension_guid,
341 const char *snapin_guid)
346 char *unix_path = NULL;
347 struct gp_inifile_context *ini_ctx = NULL;
348 struct gp_registry_entry *entries = NULL;
349 size_t num_entries = 0;
350 const char *list[] = {
351 GP_SCRIPTS_INI_STARTUP,
352 GP_SCRIPTS_INI_SHUTDOWN,
353 GP_SCRIPTS_INI_LOGON,
354 GP_SCRIPTS_INI_LOGOFF
357 debug_gpext_header(0, "scripts_process_group_policy", flags, gpo,
358 extension_guid, snapin_guid);
360 status = gpo_get_unix_path(mem_ctx, cache_path(GPO_CACHE_DIR), gpo, &unix_path);
361 NT_STATUS_NOT_OK_RETURN(status);
363 status = gp_inifile_init_context(mem_ctx, flags, unix_path,
364 GP_SCRIPTS_INI, &ini_ctx);
365 NT_STATUS_NOT_OK_RETURN(status);
367 for (i = 0; i < ARRAY_SIZE(list); i++) {
369 TALLOC_FREE(entries);
372 status = scripts_parse_ini_section(ini_ctx, flags, list[i],
373 &entries, &num_entries);
374 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
378 if (!NT_STATUS_IS_OK(status)) {
382 dump_reg_entries(flags, "READ", entries, num_entries);
384 werr = scripts_apply(ini_ctx->mem_ctx, token, root_key,
385 flags, list[i], gpo, entries, num_entries);
386 if (!W_ERROR_IS_OK(werr)) {
387 continue; /* FIXME: finally fix storing emtpy strings and REG_QWORD! */
388 TALLOC_FREE(ini_ctx);
389 return werror_to_ntstatus(werr);
393 TALLOC_FREE(ini_ctx);
397 /****************************************************************
398 ****************************************************************/
400 static NTSTATUS scripts_initialize(TALLOC_CTX *mem_ctx)
405 /****************************************************************
406 ****************************************************************/
408 static NTSTATUS scripts_shutdown(void)
412 status = unregister_gp_extension(GP_EXT_NAME);
413 if (NT_STATUS_IS_OK(status)) {
422 /****************************************************************
423 ****************************************************************/
425 static struct gp_extension_methods scripts_methods = {
426 .initialize = scripts_initialize,
427 .process_group_policy = scripts_process_group_policy,
428 .get_reg_config = scripts_get_reg_config,
429 .shutdown = scripts_shutdown
432 /****************************************************************
433 ****************************************************************/
435 NTSTATUS gpext_scripts_init(void)
439 ctx = talloc_init("gpext_scripts_init");
440 NT_STATUS_HAVE_NO_MEMORY(ctx);
442 status = register_gp_extension(ctx, SMB_GPEXT_INTERFACE_VERSION,
443 GP_EXT_NAME, GP_EXT_GUID_SCRIPTS,
445 if (!NT_STATUS_IS_OK(status)) {