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"
23 #define GP_EXT_NAME "scripts"
25 #define KEY_GP_SCRIPTS "Software\\Policies\\Microsoft\\Windows\\System\\Scripts"
27 #define GP_SCRIPTS_INI "Scripts/scripts.ini"
29 #define GP_SCRIPTS_INI_STARTUP "Startup"
30 #define GP_SCRIPTS_INI_SHUTDOWN "Shutdown"
31 #define GP_SCRIPTS_INI_LOGON "Logon"
32 #define GP_SCRIPTS_INI_LOGOFF "Logoff"
34 #define GP_SCRIPTS_SECTION_CMDLINE "cmdline"
35 #define GP_SCRIPTS_SECTION_PARAMETERS "parameters"
37 #define GP_SCRIPTS_REG_VAL_SCRIPT "Script"
38 #define GP_SCRIPTS_REG_VAL_PARAMETERS "Parameters"
39 #define GP_SCRIPTS_REG_VAL_EXECTIME "ExecTime"
41 static TALLOC_CTX *ctx = NULL;
43 /****************************************************************
44 ****************************************************************/
46 static NTSTATUS scripts_get_reg_config(TALLOC_CTX *mem_ctx,
47 struct gp_extension_reg_info **reg_info)
50 struct gp_extension_reg_info *info = NULL;
52 struct gp_extension_reg_table table[] = {
53 { "ProcessGroupPolicy", REG_SZ, "scripts_process_group_policy" },
54 { "NoGPOListChanges", REG_DWORD, "1" },
55 { "NoSlowLink", REG_DWORD, "1" },
56 { "NotifyLinkTransition", REG_DWORD, "1" },
57 { NULL, REG_NONE, NULL },
60 info = TALLOC_ZERO_P(mem_ctx, struct gp_extension_reg_info);
61 NT_STATUS_HAVE_NO_MEMORY(info);
63 status = gp_ext_info_add_entry(mem_ctx, GP_EXT_NAME,
66 NT_STATUS_NOT_OK_RETURN(status);
73 /****************************************************************
74 ****************************************************************/
76 static NTSTATUS generate_gp_registry_entry(TALLOC_CTX *mem_ctx,
81 enum gp_reg_action action,
82 struct gp_registry_entry **entry_out)
84 struct gp_registry_entry *entry = NULL;
85 struct registry_value *data = NULL;
87 entry = TALLOC_ZERO_P(mem_ctx, struct gp_registry_entry);
88 NT_STATUS_HAVE_NO_MEMORY(entry);
90 data = TALLOC_ZERO_P(mem_ctx, struct registry_value);
91 NT_STATUS_HAVE_NO_MEMORY(data);
93 data->type = data_type;
96 data->v.qword = *(uint64_t *)data_p;
99 data->v.sz.str = talloc_strdup(mem_ctx, (char *)data_p);
100 data->v.sz.len = strlen(data->v.sz.str);
103 return NT_STATUS_NOT_SUPPORTED;
108 entry->action = action;
109 entry->value = talloc_strdup(mem_ctx, value);
110 NT_STATUS_HAVE_NO_MEMORY(entry->value);
117 /****************************************************************
118 ****************************************************************/
120 static NTSTATUS scripts_parse_ini_section(struct gp_inifile_context *ini_ctx,
123 struct gp_registry_entry **entries,
126 NTSTATUS status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
131 const char *key = NULL;
132 const char *script = NULL;
133 const char *count = NULL;
134 const char *parameters = NULL;
136 count = talloc_asprintf(ini_ctx->mem_ctx, "%d", i);
137 NT_STATUS_HAVE_NO_MEMORY(count);
139 key = talloc_asprintf(ini_ctx->mem_ctx, "%s:%s%s",
141 GP_SCRIPTS_SECTION_CMDLINE);
142 NT_STATUS_HAVE_NO_MEMORY(key);
144 script = iniparser_getstring(ini_ctx->dict, key, NULL);
149 key = talloc_asprintf(ini_ctx->mem_ctx, "%s:%s%s",
151 GP_SCRIPTS_SECTION_PARAMETERS);
152 NT_STATUS_HAVE_NO_MEMORY(key);
154 parameters = iniparser_getstring(ini_ctx->dict, key, NULL);
157 struct gp_registry_entry *entry = NULL;
158 status = generate_gp_registry_entry(ini_ctx->mem_ctx,
160 GP_SCRIPTS_REG_VAL_SCRIPT,
163 GP_REG_ACTION_ADD_VALUE,
165 NT_STATUS_NOT_OK_RETURN(status);
166 if (!add_gp_registry_entry_to_array(ini_ctx->mem_ctx,
170 return NT_STATUS_NO_MEMORY;
174 struct gp_registry_entry *entry = NULL;
175 status = generate_gp_registry_entry(ini_ctx->mem_ctx,
177 GP_SCRIPTS_REG_VAL_PARAMETERS,
180 GP_REG_ACTION_ADD_VALUE,
182 NT_STATUS_NOT_OK_RETURN(status);
183 if (!add_gp_registry_entry_to_array(ini_ctx->mem_ctx,
187 return NT_STATUS_NO_MEMORY;
191 struct gp_registry_entry *entry = NULL;
192 status = generate_gp_registry_entry(ini_ctx->mem_ctx,
194 GP_SCRIPTS_REG_VAL_EXECTIME,
197 GP_REG_ACTION_ADD_VALUE,
199 NT_STATUS_NOT_OK_RETURN(status);
200 if (!add_gp_registry_entry_to_array(ini_ctx->mem_ctx,
204 return NT_STATUS_NO_MEMORY;
207 status = NT_STATUS_OK;
214 /****************************************************************
215 ****************************************************************/
217 static WERROR scripts_store_reg_gpovals(TALLOC_CTX *mem_ctx,
218 struct registry_key *key,
219 struct GROUP_POLICY_OBJECT *gpo)
224 return WERR_INVALID_PARAM;
227 werr = gp_store_reg_val_sz(mem_ctx, key, "DisplayName",
229 W_ERROR_NOT_OK_RETURN(werr);
231 werr = gp_store_reg_val_sz(mem_ctx, key, "FileSysPath",
233 W_ERROR_NOT_OK_RETURN(werr);
235 werr = gp_store_reg_val_sz(mem_ctx, key, "GPO-ID",
237 W_ERROR_NOT_OK_RETURN(werr);
239 werr = gp_store_reg_val_sz(mem_ctx, key, "GPOName",
241 W_ERROR_NOT_OK_RETURN(werr);
243 werr = gp_store_reg_val_sz(mem_ctx, key, "SOM-ID",
245 W_ERROR_NOT_OK_RETURN(werr);
250 /****************************************************************
251 ****************************************************************/
253 static WERROR scripts_apply(TALLOC_CTX *mem_ctx,
254 const struct nt_user_token *token,
255 struct registry_key *root_key,
258 struct GROUP_POLICY_OBJECT *gpo,
259 struct gp_registry_entry *entries,
262 struct gp_registry_context *reg_ctx = NULL;
265 const char *keystr = NULL;
268 if (num_entries == 0) {
273 if (flags & GPO_INFO_FLAG_MACHINE) {
274 struct nt_user_token *tmp_token;
276 tmp_token = registry_create_system_token(mem_ctx);
277 W_ERROR_HAVE_NO_MEMORY(tmp_token);
279 werr = gp_init_reg_ctx(mem_ctx, KEY_HKLM, REG_KEY_WRITE,
283 werr = gp_init_reg_ctx(mem_ctx, KEY_HKCU, REG_KEY_WRITE,
287 W_ERROR_NOT_OK_RETURN(werr);
290 keystr = talloc_asprintf(mem_ctx, "%s\\%s\\%d", KEY_GP_SCRIPTS,
292 W_ERROR_HAVE_NO_MEMORY(keystr);
294 reg_deletekey_recursive(mem_ctx, root_key, keystr);
296 werr = gp_store_reg_subkey(mem_ctx, keystr,
297 root_key, &root_key);
298 if (!W_ERROR_IS_OK(werr)) {
302 werr = scripts_store_reg_gpovals(mem_ctx, root_key, gpo);
303 if (!W_ERROR_IS_OK(werr)) {
307 for (i=0; i<num_entries; i++) {
309 werr = reg_apply_registry_entry(mem_ctx, root_key, reg_ctx,
312 if (!W_ERROR_IS_OK(werr)) {
313 DEBUG(0,("failed to apply registry: %s\n",
320 gp_free_reg_ctx(reg_ctx);
324 /****************************************************************
325 ****************************************************************/
327 static NTSTATUS scripts_process_group_policy(ADS_STRUCT *ads,
330 struct registry_key *root_key,
331 const struct nt_user_token *token,
332 struct GROUP_POLICY_OBJECT *gpo,
333 const char *extension_guid,
334 const char *snapin_guid)
339 char *unix_path = NULL;
340 struct gp_inifile_context *ini_ctx = NULL;
341 struct gp_registry_entry *entries = NULL;
342 size_t num_entries = 0;
343 const char *list[] = {
344 GP_SCRIPTS_INI_STARTUP,
345 GP_SCRIPTS_INI_SHUTDOWN,
346 GP_SCRIPTS_INI_LOGON,
347 GP_SCRIPTS_INI_LOGOFF
350 debug_gpext_header(0, "scripts_process_group_policy", flags, gpo,
351 extension_guid, snapin_guid);
353 status = gpo_get_unix_path(mem_ctx, gpo, &unix_path);
354 NT_STATUS_NOT_OK_RETURN(status);
356 status = gp_inifile_init_context(mem_ctx, flags, unix_path,
357 GP_SCRIPTS_INI, &ini_ctx);
358 NT_STATUS_NOT_OK_RETURN(status);
360 for (i = 0; i < ARRAY_SIZE(list); i++) {
362 TALLOC_FREE(entries);
365 status = scripts_parse_ini_section(ini_ctx, flags, list[i],
366 &entries, &num_entries);
367 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
371 if (!NT_STATUS_IS_OK(status)) {
375 dump_reg_entries(flags, "READ", entries, num_entries);
377 werr = scripts_apply(ini_ctx->mem_ctx, token, root_key,
378 flags, list[i], gpo, entries, num_entries);
379 if (!W_ERROR_IS_OK(werr)) {
380 continue; /* FIXME: finally fix storing emtpy strings and REG_QWORD! */
381 TALLOC_FREE(ini_ctx);
382 return werror_to_ntstatus(werr);
386 TALLOC_FREE(ini_ctx);
390 /****************************************************************
391 ****************************************************************/
393 static NTSTATUS scripts_initialize(TALLOC_CTX *mem_ctx)
398 /****************************************************************
399 ****************************************************************/
401 static NTSTATUS scripts_shutdown(void)
405 status = unregister_gp_extension(GP_EXT_NAME);
406 if (NT_STATUS_IS_OK(status)) {
415 /****************************************************************
416 ****************************************************************/
418 static struct gp_extension_methods scripts_methods = {
419 .initialize = scripts_initialize,
420 .process_group_policy = scripts_process_group_policy,
421 .get_reg_config = scripts_get_reg_config,
422 .shutdown = scripts_shutdown
425 /****************************************************************
426 ****************************************************************/
428 NTSTATUS gpext_scripts_init(void)
432 ctx = talloc_init("gpext_scripts_init");
433 NT_STATUS_HAVE_NO_MEMORY(ctx);
435 status = register_gp_extension(ctx, SMB_GPEXT_INTERFACE_VERSION,
436 GP_EXT_NAME, GP_EXT_GUID_SCRIPTS,
438 if (!NT_STATUS_IS_OK(status)) {