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"
25 #define GP_EXT_NAME "scripts"
27 #define KEY_GP_SCRIPTS "Software\\Policies\\Microsoft\\Windows\\System\\Scripts"
29 #define GP_SCRIPTS_INI "Scripts/scripts.ini"
31 #define GP_SCRIPTS_INI_STARTUP "Startup"
32 #define GP_SCRIPTS_INI_SHUTDOWN "Shutdown"
33 #define GP_SCRIPTS_INI_LOGON "Logon"
34 #define GP_SCRIPTS_INI_LOGOFF "Logoff"
36 #define GP_SCRIPTS_SECTION_CMDLINE "cmdline"
37 #define GP_SCRIPTS_SECTION_PARAMETERS "parameters"
39 #define GP_SCRIPTS_REG_VAL_SCRIPT "Script"
40 #define GP_SCRIPTS_REG_VAL_PARAMETERS "Parameters"
41 #define GP_SCRIPTS_REG_VAL_EXECTIME "ExecTime"
43 static TALLOC_CTX *ctx = NULL;
45 /****************************************************************
46 ****************************************************************/
48 static NTSTATUS scripts_get_reg_config(TALLOC_CTX *mem_ctx,
49 struct gp_extension_reg_info **reg_info)
52 struct gp_extension_reg_info *info = NULL;
54 struct gp_extension_reg_table table[] = {
55 { "ProcessGroupPolicy", REG_SZ, "scripts_process_group_policy" },
56 { "NoGPOListChanges", REG_DWORD, "1" },
57 { "NoSlowLink", REG_DWORD, "1" },
58 { "NotifyLinkTransition", REG_DWORD, "1" },
59 { NULL, REG_NONE, NULL },
62 info = TALLOC_ZERO_P(mem_ctx, struct gp_extension_reg_info);
63 NT_STATUS_HAVE_NO_MEMORY(info);
65 status = gp_ext_info_add_entry(mem_ctx, GP_EXT_NAME,
68 NT_STATUS_NOT_OK_RETURN(status);
75 /****************************************************************
76 ****************************************************************/
78 static NTSTATUS generate_gp_registry_entry(TALLOC_CTX *mem_ctx,
83 enum gp_reg_action action,
84 struct gp_registry_entry **entry_out)
86 struct gp_registry_entry *entry = NULL;
87 struct registry_value *data = NULL;
89 entry = TALLOC_ZERO_P(mem_ctx, struct gp_registry_entry);
90 NT_STATUS_HAVE_NO_MEMORY(entry);
92 data = TALLOC_ZERO_P(mem_ctx, struct registry_value);
93 NT_STATUS_HAVE_NO_MEMORY(data);
95 data->type = data_type;
98 data->v.qword = *(uint64_t *)data_p;
101 data->v.sz.str = talloc_strdup(mem_ctx, (char *)data_p);
102 data->v.sz.len = strlen(data->v.sz.str);
105 return NT_STATUS_NOT_SUPPORTED;
110 entry->action = action;
111 entry->value = talloc_strdup(mem_ctx, value);
112 NT_STATUS_HAVE_NO_MEMORY(entry->value);
119 /****************************************************************
120 ****************************************************************/
122 static NTSTATUS scripts_parse_ini_section(struct gp_inifile_context *ini_ctx,
125 struct gp_registry_entry **entries,
128 NTSTATUS status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
134 const char *key = NULL;
136 const char *count = NULL;
137 char *parameters = NULL;
139 count = talloc_asprintf(ini_ctx->mem_ctx, "%d", i);
140 NT_STATUS_HAVE_NO_MEMORY(count);
142 key = talloc_asprintf(ini_ctx->mem_ctx, "%s:%s%s",
144 GP_SCRIPTS_SECTION_CMDLINE);
145 NT_STATUS_HAVE_NO_MEMORY(key);
147 result = gp_inifile_getstring(ini_ctx, key, &script);
148 if (!NT_STATUS_IS_OK(result)) {
152 key = talloc_asprintf(ini_ctx->mem_ctx, "%s:%s%s",
154 GP_SCRIPTS_SECTION_PARAMETERS);
155 NT_STATUS_HAVE_NO_MEMORY(key);
157 result = gp_inifile_getstring(ini_ctx, key, ¶meters);
158 if (!NT_STATUS_IS_OK(result)) {
163 struct gp_registry_entry *entry = NULL;
164 status = generate_gp_registry_entry(ini_ctx->mem_ctx,
166 GP_SCRIPTS_REG_VAL_SCRIPT,
169 GP_REG_ACTION_ADD_VALUE,
171 NT_STATUS_NOT_OK_RETURN(status);
172 if (!add_gp_registry_entry_to_array(ini_ctx->mem_ctx,
176 return NT_STATUS_NO_MEMORY;
180 struct gp_registry_entry *entry = NULL;
181 status = generate_gp_registry_entry(ini_ctx->mem_ctx,
183 GP_SCRIPTS_REG_VAL_PARAMETERS,
186 GP_REG_ACTION_ADD_VALUE,
188 NT_STATUS_NOT_OK_RETURN(status);
189 if (!add_gp_registry_entry_to_array(ini_ctx->mem_ctx,
193 return NT_STATUS_NO_MEMORY;
197 struct gp_registry_entry *entry = NULL;
198 status = generate_gp_registry_entry(ini_ctx->mem_ctx,
200 GP_SCRIPTS_REG_VAL_EXECTIME,
203 GP_REG_ACTION_ADD_VALUE,
205 NT_STATUS_NOT_OK_RETURN(status);
206 if (!add_gp_registry_entry_to_array(ini_ctx->mem_ctx,
210 return NT_STATUS_NO_MEMORY;
213 status = NT_STATUS_OK;
220 /****************************************************************
221 ****************************************************************/
223 static WERROR scripts_store_reg_gpovals(TALLOC_CTX *mem_ctx,
224 struct registry_key *key,
225 struct GROUP_POLICY_OBJECT *gpo)
230 return WERR_INVALID_PARAM;
233 werr = gp_store_reg_val_sz(mem_ctx, key, "DisplayName",
235 W_ERROR_NOT_OK_RETURN(werr);
237 werr = gp_store_reg_val_sz(mem_ctx, key, "FileSysPath",
239 W_ERROR_NOT_OK_RETURN(werr);
241 werr = gp_store_reg_val_sz(mem_ctx, key, "GPO-ID",
243 W_ERROR_NOT_OK_RETURN(werr);
245 werr = gp_store_reg_val_sz(mem_ctx, key, "GPOName",
247 W_ERROR_NOT_OK_RETURN(werr);
249 werr = gp_store_reg_val_sz(mem_ctx, key, "SOM-ID",
251 W_ERROR_NOT_OK_RETURN(werr);
256 /****************************************************************
257 ****************************************************************/
259 static WERROR scripts_apply(TALLOC_CTX *mem_ctx,
260 const struct nt_user_token *token,
261 struct registry_key *root_key,
264 struct GROUP_POLICY_OBJECT *gpo,
265 struct gp_registry_entry *entries,
268 struct gp_registry_context *reg_ctx = NULL;
271 const char *keystr = NULL;
274 if (num_entries == 0) {
279 if (flags & GPO_INFO_FLAG_MACHINE) {
280 struct nt_user_token *tmp_token;
282 tmp_token = registry_create_system_token(mem_ctx);
283 W_ERROR_HAVE_NO_MEMORY(tmp_token);
285 werr = gp_init_reg_ctx(mem_ctx, KEY_HKLM, REG_KEY_WRITE,
289 werr = gp_init_reg_ctx(mem_ctx, KEY_HKCU, REG_KEY_WRITE,
293 W_ERROR_NOT_OK_RETURN(werr);
296 keystr = talloc_asprintf(mem_ctx, "%s\\%s\\%d", KEY_GP_SCRIPTS,
298 W_ERROR_HAVE_NO_MEMORY(keystr);
300 reg_deletekey_recursive(mem_ctx, root_key, keystr);
302 werr = gp_store_reg_subkey(mem_ctx, keystr,
303 root_key, &root_key);
304 if (!W_ERROR_IS_OK(werr)) {
308 werr = scripts_store_reg_gpovals(mem_ctx, root_key, gpo);
309 if (!W_ERROR_IS_OK(werr)) {
313 for (i=0; i<num_entries; i++) {
315 werr = reg_apply_registry_entry(mem_ctx, root_key, reg_ctx,
318 if (!W_ERROR_IS_OK(werr)) {
319 DEBUG(0,("failed to apply registry: %s\n",
326 gp_free_reg_ctx(reg_ctx);
330 /****************************************************************
331 ****************************************************************/
333 static NTSTATUS scripts_process_group_policy(ADS_STRUCT *ads,
336 struct registry_key *root_key,
337 const struct nt_user_token *token,
338 struct GROUP_POLICY_OBJECT *gpo,
339 const char *extension_guid,
340 const char *snapin_guid)
345 char *unix_path = NULL;
346 struct gp_inifile_context *ini_ctx = NULL;
347 struct gp_registry_entry *entries = NULL;
348 size_t num_entries = 0;
349 const char *list[] = {
350 GP_SCRIPTS_INI_STARTUP,
351 GP_SCRIPTS_INI_SHUTDOWN,
352 GP_SCRIPTS_INI_LOGON,
353 GP_SCRIPTS_INI_LOGOFF
356 debug_gpext_header(0, "scripts_process_group_policy", flags, gpo,
357 extension_guid, snapin_guid);
359 status = gpo_get_unix_path(mem_ctx, cache_path(GPO_CACHE_DIR), gpo, &unix_path);
360 NT_STATUS_NOT_OK_RETURN(status);
362 status = gp_inifile_init_context(mem_ctx, flags, unix_path,
363 GP_SCRIPTS_INI, &ini_ctx);
364 NT_STATUS_NOT_OK_RETURN(status);
366 for (i = 0; i < ARRAY_SIZE(list); i++) {
368 TALLOC_FREE(entries);
371 status = scripts_parse_ini_section(ini_ctx, flags, list[i],
372 &entries, &num_entries);
373 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
377 if (!NT_STATUS_IS_OK(status)) {
381 dump_reg_entries(flags, "READ", entries, num_entries);
383 werr = scripts_apply(ini_ctx->mem_ctx, token, root_key,
384 flags, list[i], gpo, entries, num_entries);
385 if (!W_ERROR_IS_OK(werr)) {
386 continue; /* FIXME: finally fix storing emtpy strings and REG_QWORD! */
387 TALLOC_FREE(ini_ctx);
388 return werror_to_ntstatus(werr);
392 TALLOC_FREE(ini_ctx);
396 /****************************************************************
397 ****************************************************************/
399 static NTSTATUS scripts_initialize(TALLOC_CTX *mem_ctx)
404 /****************************************************************
405 ****************************************************************/
407 static NTSTATUS scripts_shutdown(void)
411 status = unregister_gp_extension(GP_EXT_NAME);
412 if (NT_STATUS_IS_OK(status)) {
421 /****************************************************************
422 ****************************************************************/
424 static struct gp_extension_methods scripts_methods = {
425 .initialize = scripts_initialize,
426 .process_group_policy = scripts_process_group_policy,
427 .get_reg_config = scripts_get_reg_config,
428 .shutdown = scripts_shutdown
431 /****************************************************************
432 ****************************************************************/
434 NTSTATUS gpext_scripts_init(void)
438 ctx = talloc_init("gpext_scripts_init");
439 NT_STATUS_HAVE_NO_MEMORY(ctx);
441 status = register_gp_extension(ctx, SMB_GPEXT_INTERFACE_VERSION,
442 GP_EXT_NAME, GP_EXT_GUID_SCRIPTS,
444 if (!NT_STATUS_IS_OK(status)) {