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 #include "registry/reg_api.h"
26 #include "../libcli/registry/util_reg.h"
28 #define GP_EXT_NAME "scripts"
30 #define KEY_GP_SCRIPTS "Software\\Policies\\Microsoft\\Windows\\System\\Scripts"
32 #define GP_SCRIPTS_INI "Scripts/scripts.ini"
34 #define GP_SCRIPTS_INI_STARTUP "Startup"
35 #define GP_SCRIPTS_INI_SHUTDOWN "Shutdown"
36 #define GP_SCRIPTS_INI_LOGON "Logon"
37 #define GP_SCRIPTS_INI_LOGOFF "Logoff"
39 #define GP_SCRIPTS_SECTION_CMDLINE "cmdline"
40 #define GP_SCRIPTS_SECTION_PARAMETERS "parameters"
42 #define GP_SCRIPTS_REG_VAL_SCRIPT "Script"
43 #define GP_SCRIPTS_REG_VAL_PARAMETERS "Parameters"
44 #define GP_SCRIPTS_REG_VAL_EXECTIME "ExecTime"
46 static TALLOC_CTX *ctx = NULL;
48 /****************************************************************
49 ****************************************************************/
51 static NTSTATUS scripts_get_reg_config(TALLOC_CTX *mem_ctx,
52 struct gp_extension_reg_info **reg_info)
55 struct gp_extension_reg_info *info = NULL;
57 struct gp_extension_reg_table table[] = {
58 { "ProcessGroupPolicy", REG_SZ, "scripts_process_group_policy" },
59 { "NoGPOListChanges", REG_DWORD, "1" },
60 { "NoSlowLink", REG_DWORD, "1" },
61 { "NotifyLinkTransition", REG_DWORD, "1" },
62 { NULL, REG_NONE, NULL },
65 info = talloc_zero(mem_ctx, struct gp_extension_reg_info);
66 NT_STATUS_HAVE_NO_MEMORY(info);
68 status = gp_ext_info_add_entry(mem_ctx, GP_EXT_NAME,
71 NT_STATUS_NOT_OK_RETURN(status);
78 /****************************************************************
79 ****************************************************************/
81 static NTSTATUS generate_gp_registry_entry(TALLOC_CTX *mem_ctx,
86 enum gp_reg_action action,
87 struct gp_registry_entry **entry_out)
89 struct gp_registry_entry *entry = NULL;
90 struct registry_value *data = NULL;
92 entry = talloc_zero(mem_ctx, struct gp_registry_entry);
93 NT_STATUS_HAVE_NO_MEMORY(entry);
95 data = talloc_zero(mem_ctx, struct registry_value);
96 NT_STATUS_HAVE_NO_MEMORY(data);
98 data->type = data_type;
101 data->data = data_blob_talloc(mem_ctx, NULL, 8);
102 SBVAL(data->data.data, 0, *(uint64_t *)data_p);
105 if (!push_reg_sz(mem_ctx, &data->data, (char *)data_p)) {
106 return NT_STATUS_NO_MEMORY;
110 return NT_STATUS_NOT_SUPPORTED;
115 entry->action = action;
116 entry->value = talloc_strdup(mem_ctx, value);
117 NT_STATUS_HAVE_NO_MEMORY(entry->value);
124 /****************************************************************
125 ****************************************************************/
127 static NTSTATUS scripts_parse_ini_section(struct gp_inifile_context *ini_ctx,
130 struct gp_registry_entry **entries,
133 NTSTATUS status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
139 const char *key = NULL;
141 const char *count = NULL;
142 char *parameters = NULL;
144 count = talloc_asprintf(ini_ctx->mem_ctx, "%d", i);
145 NT_STATUS_HAVE_NO_MEMORY(count);
147 key = talloc_asprintf(ini_ctx->mem_ctx, "%s:%s%s",
149 GP_SCRIPTS_SECTION_CMDLINE);
150 NT_STATUS_HAVE_NO_MEMORY(key);
152 result = gp_inifile_getstring(ini_ctx, key, &script);
153 if (!NT_STATUS_IS_OK(result)) {
157 key = talloc_asprintf(ini_ctx->mem_ctx, "%s:%s%s",
159 GP_SCRIPTS_SECTION_PARAMETERS);
160 NT_STATUS_HAVE_NO_MEMORY(key);
162 result = gp_inifile_getstring(ini_ctx, key, ¶meters);
163 if (!NT_STATUS_IS_OK(result)) {
168 struct gp_registry_entry *entry = NULL;
169 status = generate_gp_registry_entry(ini_ctx->mem_ctx,
171 GP_SCRIPTS_REG_VAL_SCRIPT,
174 GP_REG_ACTION_ADD_VALUE,
176 NT_STATUS_NOT_OK_RETURN(status);
177 if (!add_gp_registry_entry_to_array(ini_ctx->mem_ctx,
181 return NT_STATUS_NO_MEMORY;
185 struct gp_registry_entry *entry = NULL;
186 status = generate_gp_registry_entry(ini_ctx->mem_ctx,
188 GP_SCRIPTS_REG_VAL_PARAMETERS,
191 GP_REG_ACTION_ADD_VALUE,
193 NT_STATUS_NOT_OK_RETURN(status);
194 if (!add_gp_registry_entry_to_array(ini_ctx->mem_ctx,
198 return NT_STATUS_NO_MEMORY;
202 struct gp_registry_entry *entry = NULL;
203 status = generate_gp_registry_entry(ini_ctx->mem_ctx,
205 GP_SCRIPTS_REG_VAL_EXECTIME,
208 GP_REG_ACTION_ADD_VALUE,
210 NT_STATUS_NOT_OK_RETURN(status);
211 if (!add_gp_registry_entry_to_array(ini_ctx->mem_ctx,
215 return NT_STATUS_NO_MEMORY;
218 status = NT_STATUS_OK;
225 /****************************************************************
226 ****************************************************************/
228 static WERROR scripts_store_reg_gpovals(TALLOC_CTX *mem_ctx,
229 struct registry_key *key,
230 struct GROUP_POLICY_OBJECT *gpo)
235 return WERR_INVALID_PARAM;
238 werr = gp_store_reg_val_sz(mem_ctx, key, "DisplayName",
240 W_ERROR_NOT_OK_RETURN(werr);
242 werr = gp_store_reg_val_sz(mem_ctx, key, "FileSysPath",
244 W_ERROR_NOT_OK_RETURN(werr);
246 werr = gp_store_reg_val_sz(mem_ctx, key, "GPO-ID",
248 W_ERROR_NOT_OK_RETURN(werr);
250 werr = gp_store_reg_val_sz(mem_ctx, key, "GPOName",
252 W_ERROR_NOT_OK_RETURN(werr);
254 werr = gp_store_reg_val_sz(mem_ctx, key, "SOM-ID",
256 W_ERROR_NOT_OK_RETURN(werr);
261 /****************************************************************
262 ****************************************************************/
264 static WERROR scripts_apply(TALLOC_CTX *mem_ctx,
265 const struct security_token *token,
266 struct registry_key *root_key,
269 struct GROUP_POLICY_OBJECT *gpo,
270 struct gp_registry_entry *entries,
273 struct gp_registry_context *reg_ctx = NULL;
276 const char *keystr = NULL;
279 if (num_entries == 0) {
284 if (flags & GPO_INFO_FLAG_MACHINE) {
285 struct security_token *tmp_token;
287 tmp_token = registry_create_system_token(mem_ctx);
288 W_ERROR_HAVE_NO_MEMORY(tmp_token);
290 werr = gp_init_reg_ctx(mem_ctx, KEY_HKLM, REG_KEY_WRITE,
294 werr = gp_init_reg_ctx(mem_ctx, KEY_HKCU, REG_KEY_WRITE,
298 W_ERROR_NOT_OK_RETURN(werr);
301 keystr = talloc_asprintf(mem_ctx, "%s\\%s\\%d", KEY_GP_SCRIPTS,
303 W_ERROR_HAVE_NO_MEMORY(keystr);
305 reg_deletekey_recursive(root_key, keystr);
307 werr = gp_store_reg_subkey(mem_ctx, keystr,
308 root_key, &root_key);
309 if (!W_ERROR_IS_OK(werr)) {
313 werr = scripts_store_reg_gpovals(mem_ctx, root_key, gpo);
314 if (!W_ERROR_IS_OK(werr)) {
318 for (i=0; i<num_entries; i++) {
320 werr = reg_apply_registry_entry(mem_ctx, root_key, reg_ctx,
323 if (!W_ERROR_IS_OK(werr)) {
324 DEBUG(0,("failed to apply registry: %s\n",
331 gp_free_reg_ctx(reg_ctx);
335 /****************************************************************
336 ****************************************************************/
338 static NTSTATUS scripts_process_group_policy(TALLOC_CTX *mem_ctx,
340 struct registry_key *root_key,
341 const struct security_token *token,
342 struct GROUP_POLICY_OBJECT *gpo,
343 const char *extension_guid,
344 const char *snapin_guid)
349 char *unix_path = NULL;
350 struct gp_inifile_context *ini_ctx = NULL;
351 struct gp_registry_entry *entries = NULL;
352 size_t num_entries = 0;
353 const char *list[] = {
354 GP_SCRIPTS_INI_STARTUP,
355 GP_SCRIPTS_INI_SHUTDOWN,
356 GP_SCRIPTS_INI_LOGON,
357 GP_SCRIPTS_INI_LOGOFF
360 debug_gpext_header(0, "scripts_process_group_policy", flags, gpo,
361 extension_guid, snapin_guid);
363 status = gpo_get_unix_path(mem_ctx, cache_path(GPO_CACHE_DIR), gpo, &unix_path);
364 NT_STATUS_NOT_OK_RETURN(status);
366 status = gp_inifile_init_context(mem_ctx, flags, unix_path,
367 GP_SCRIPTS_INI, &ini_ctx);
368 NT_STATUS_NOT_OK_RETURN(status);
370 for (i = 0; i < ARRAY_SIZE(list); i++) {
372 TALLOC_FREE(entries);
375 status = scripts_parse_ini_section(ini_ctx, flags, list[i],
376 &entries, &num_entries);
377 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
381 if (!NT_STATUS_IS_OK(status)) {
385 dump_reg_entries(flags, "READ", entries, num_entries);
387 werr = scripts_apply(ini_ctx->mem_ctx, token, root_key,
388 flags, list[i], gpo, entries, num_entries);
389 if (!W_ERROR_IS_OK(werr)) {
390 continue; /* FIXME: finally fix storing emtpy strings and REG_QWORD! */
391 TALLOC_FREE(ini_ctx);
392 return werror_to_ntstatus(werr);
396 TALLOC_FREE(ini_ctx);
400 /****************************************************************
401 ****************************************************************/
403 static NTSTATUS scripts_initialize(TALLOC_CTX *mem_ctx)
408 /****************************************************************
409 ****************************************************************/
411 static NTSTATUS scripts_shutdown(void)
415 status = unregister_gp_extension(GP_EXT_NAME);
416 if (NT_STATUS_IS_OK(status)) {
425 /****************************************************************
426 ****************************************************************/
428 static struct gp_extension_methods scripts_methods = {
429 .initialize = scripts_initialize,
430 .process_group_policy = scripts_process_group_policy,
431 .get_reg_config = scripts_get_reg_config,
432 .shutdown = scripts_shutdown
435 /****************************************************************
436 ****************************************************************/
438 NTSTATUS gpext_scripts_init(void)
442 ctx = talloc_init("gpext_scripts_init");
443 NT_STATUS_HAVE_NO_MEMORY(ctx);
445 status = register_gp_extension(ctx, SMB_GPEXT_INTERFACE_VERSION,
446 GP_EXT_NAME, GP_EXT_GUID_SCRIPTS,
448 if (!NT_STATUS_IS_OK(status)) {