libgpo: only use libgpo/gpext/gpext.h where really needed.
[Samba.git] / source3 / libgpo / gpext / scripts.c
blob16af3a23f933ff3e6106da0590f411b23065da36
1 /*
2 * Unix SMB/CIFS implementation.
3 * Group Policy Support
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/>.
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 "registry/reg_api.h"
26 #include "../libcli/registry/util_reg.h"
27 #include "libgpo/gpext/gpext.h"
29 #define GP_EXT_NAME "scripts"
31 #define KEY_GP_SCRIPTS "Software\\Policies\\Microsoft\\Windows\\System\\Scripts"
33 #define GP_SCRIPTS_INI "Scripts/scripts.ini"
35 #define GP_SCRIPTS_INI_STARTUP "Startup"
36 #define GP_SCRIPTS_INI_SHUTDOWN "Shutdown"
37 #define GP_SCRIPTS_INI_LOGON "Logon"
38 #define GP_SCRIPTS_INI_LOGOFF "Logoff"
40 #define GP_SCRIPTS_SECTION_CMDLINE "cmdline"
41 #define GP_SCRIPTS_SECTION_PARAMETERS "parameters"
43 #define GP_SCRIPTS_REG_VAL_SCRIPT "Script"
44 #define GP_SCRIPTS_REG_VAL_PARAMETERS "Parameters"
45 #define GP_SCRIPTS_REG_VAL_EXECTIME "ExecTime"
47 static TALLOC_CTX *ctx = NULL;
49 /****************************************************************
50 ****************************************************************/
52 static NTSTATUS scripts_get_reg_config(TALLOC_CTX *mem_ctx,
53 struct gp_extension_reg_info **reg_info)
55 NTSTATUS status;
56 struct gp_extension_reg_info *info = NULL;
58 struct gp_extension_reg_table table[] = {
59 { "ProcessGroupPolicy", REG_SZ, "scripts_process_group_policy" },
60 { "NoGPOListChanges", REG_DWORD, "1" },
61 { "NoSlowLink", REG_DWORD, "1" },
62 { "NotifyLinkTransition", REG_DWORD, "1" },
63 { NULL, REG_NONE, NULL },
66 info = talloc_zero(mem_ctx, struct gp_extension_reg_info);
67 NT_STATUS_HAVE_NO_MEMORY(info);
69 status = gpext_info_add_entry(mem_ctx, GP_EXT_NAME,
70 GP_EXT_GUID_SCRIPTS,
71 table, info);
72 NT_STATUS_NOT_OK_RETURN(status);
74 *reg_info = info;
76 return NT_STATUS_OK;
79 /****************************************************************
80 ****************************************************************/
82 static NTSTATUS generate_gp_registry_entry(TALLOC_CTX *mem_ctx,
83 const char *key,
84 const char *value,
85 uint32_t data_type,
86 const void *data_p,
87 enum gp_reg_action action,
88 struct gp_registry_entry **entry_out)
90 struct gp_registry_entry *entry = NULL;
91 struct registry_value *data = NULL;
93 entry = talloc_zero(mem_ctx, struct gp_registry_entry);
94 NT_STATUS_HAVE_NO_MEMORY(entry);
96 data = talloc_zero(mem_ctx, struct registry_value);
97 NT_STATUS_HAVE_NO_MEMORY(data);
99 data->type = data_type;
100 switch (data->type) {
101 case REG_QWORD:
102 data->data = data_blob_talloc(mem_ctx, NULL, 8);
103 SBVAL(data->data.data, 0, *(uint64_t *)data_p);
104 break;
105 case REG_SZ:
106 if (!push_reg_sz(mem_ctx, &data->data, (const char *)data_p)) {
107 return NT_STATUS_NO_MEMORY;
109 break;
110 default:
111 return NT_STATUS_NOT_SUPPORTED;
114 entry->key = key;
115 entry->data = data;
116 entry->action = action;
117 entry->value = talloc_strdup(mem_ctx, value);
118 NT_STATUS_HAVE_NO_MEMORY(entry->value);
120 *entry_out = entry;
122 return NT_STATUS_OK;
125 /****************************************************************
126 ****************************************************************/
128 static NTSTATUS scripts_parse_ini_section(struct gp_inifile_context *ini_ctx,
129 uint32_t flags,
130 const char *section,
131 struct gp_registry_entry **entries,
132 size_t *num_entries)
134 NTSTATUS status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
135 NTSTATUS result;
136 int i = 0;
138 while (1) {
140 const char *key = NULL;
141 char *script = NULL;
142 const char *count = NULL;
143 char *parameters = NULL;
145 count = talloc_asprintf(ini_ctx->mem_ctx, "%d", i);
146 NT_STATUS_HAVE_NO_MEMORY(count);
148 key = talloc_asprintf(ini_ctx->mem_ctx, "%s:%s%s",
149 section, count,
150 GP_SCRIPTS_SECTION_CMDLINE);
151 NT_STATUS_HAVE_NO_MEMORY(key);
153 result = gp_inifile_getstring(ini_ctx, key, &script);
154 if (!NT_STATUS_IS_OK(result)) {
155 break;
158 key = talloc_asprintf(ini_ctx->mem_ctx, "%s:%s%s",
159 section, count,
160 GP_SCRIPTS_SECTION_PARAMETERS);
161 NT_STATUS_HAVE_NO_MEMORY(key);
163 result = gp_inifile_getstring(ini_ctx, key, &parameters);
164 if (!NT_STATUS_IS_OK(result)) {
165 break;
169 struct gp_registry_entry *entry = NULL;
170 status = generate_gp_registry_entry(ini_ctx->mem_ctx,
171 count,
172 GP_SCRIPTS_REG_VAL_SCRIPT,
173 REG_SZ,
174 script,
175 GP_REG_ACTION_ADD_VALUE,
176 &entry);
177 NT_STATUS_NOT_OK_RETURN(status);
178 if (!add_gp_registry_entry_to_array(ini_ctx->mem_ctx,
179 entry,
180 entries,
181 num_entries)) {
182 return NT_STATUS_NO_MEMORY;
186 struct gp_registry_entry *entry = NULL;
187 status = generate_gp_registry_entry(ini_ctx->mem_ctx,
188 count,
189 GP_SCRIPTS_REG_VAL_PARAMETERS,
190 REG_SZ,
191 parameters,
192 GP_REG_ACTION_ADD_VALUE,
193 &entry);
194 NT_STATUS_NOT_OK_RETURN(status);
195 if (!add_gp_registry_entry_to_array(ini_ctx->mem_ctx,
196 entry,
197 entries,
198 num_entries)) {
199 return NT_STATUS_NO_MEMORY;
203 struct gp_registry_entry *entry = NULL;
204 status = generate_gp_registry_entry(ini_ctx->mem_ctx,
205 count,
206 GP_SCRIPTS_REG_VAL_EXECTIME,
207 REG_QWORD,
209 GP_REG_ACTION_ADD_VALUE,
210 &entry);
211 NT_STATUS_NOT_OK_RETURN(status);
212 if (!add_gp_registry_entry_to_array(ini_ctx->mem_ctx,
213 entry,
214 entries,
215 num_entries)) {
216 return NT_STATUS_NO_MEMORY;
219 status = NT_STATUS_OK;
220 i++;
223 return status;
226 /****************************************************************
227 ****************************************************************/
229 static WERROR scripts_store_reg_gpovals(TALLOC_CTX *mem_ctx,
230 struct registry_key *key,
231 struct GROUP_POLICY_OBJECT *gpo)
233 WERROR werr;
235 if (!key || !gpo) {
236 return WERR_INVALID_PARAM;
239 werr = gp_store_reg_val_sz(mem_ctx, key, "DisplayName",
240 gpo->display_name);
241 W_ERROR_NOT_OK_RETURN(werr);
243 werr = gp_store_reg_val_sz(mem_ctx, key, "FileSysPath",
244 gpo->file_sys_path);
245 W_ERROR_NOT_OK_RETURN(werr);
247 werr = gp_store_reg_val_sz(mem_ctx, key, "GPO-ID",
248 gpo->ds_path);
249 W_ERROR_NOT_OK_RETURN(werr);
251 werr = gp_store_reg_val_sz(mem_ctx, key, "GPOName",
252 gpo->name);
253 W_ERROR_NOT_OK_RETURN(werr);
255 werr = gp_store_reg_val_sz(mem_ctx, key, "SOM-ID",
256 gpo->link);
257 W_ERROR_NOT_OK_RETURN(werr);
259 return werr;
262 /****************************************************************
263 ****************************************************************/
265 static WERROR scripts_apply(TALLOC_CTX *mem_ctx,
266 const struct security_token *token,
267 struct registry_key *root_key,
268 uint32_t flags,
269 const char *section,
270 struct GROUP_POLICY_OBJECT *gpo,
271 struct gp_registry_entry *entries,
272 size_t num_entries)
274 struct gp_registry_context *reg_ctx = NULL;
275 WERROR werr;
276 size_t i;
277 const char *keystr = NULL;
278 int count = 0;
280 if (num_entries == 0) {
281 return WERR_OK;
284 #if 0
285 if (flags & GPO_INFO_FLAG_MACHINE) {
286 struct security_token *tmp_token;
288 tmp_token = registry_create_system_token(mem_ctx);
289 W_ERROR_HAVE_NO_MEMORY(tmp_token);
291 werr = gp_init_reg_ctx(mem_ctx, KEY_HKLM, REG_KEY_WRITE,
292 tmp_token,
293 &reg_ctx);
294 } else {
295 werr = gp_init_reg_ctx(mem_ctx, KEY_HKCU, REG_KEY_WRITE,
296 token,
297 &reg_ctx);
299 W_ERROR_NOT_OK_RETURN(werr);
300 #endif
302 keystr = talloc_asprintf(mem_ctx, "%s\\%s\\%d", KEY_GP_SCRIPTS,
303 section, count++);
304 W_ERROR_HAVE_NO_MEMORY(keystr);
306 reg_deletekey_recursive(root_key, keystr);
308 werr = gp_store_reg_subkey(mem_ctx, keystr,
309 root_key, &root_key);
310 if (!W_ERROR_IS_OK(werr)) {
311 goto done;
314 werr = scripts_store_reg_gpovals(mem_ctx, root_key, gpo);
315 if (!W_ERROR_IS_OK(werr)) {
316 goto done;
319 for (i=0; i<num_entries; i++) {
321 werr = reg_apply_registry_entry(mem_ctx, root_key, reg_ctx,
322 &(entries)[i],
323 token, flags);
324 if (!W_ERROR_IS_OK(werr)) {
325 DEBUG(0,("failed to apply registry: %s\n",
326 win_errstr(werr)));
327 goto done;
331 done:
332 gp_free_reg_ctx(reg_ctx);
333 return werr;
336 /****************************************************************
337 ****************************************************************/
339 static NTSTATUS scripts_process_group_policy(TALLOC_CTX *mem_ctx,
340 uint32_t flags,
341 struct registry_key *root_key,
342 const struct security_token *token,
343 struct GROUP_POLICY_OBJECT *deleted_gpo_list,
344 struct GROUP_POLICY_OBJECT *changed_gpo_list)
346 NTSTATUS status;
347 WERROR werr;
348 int i = 0;
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
359 struct GROUP_POLICY_OBJECT *gpo;
361 /* implementation of the policy callback function, see
362 * http://msdn.microsoft.com/en-us/library/aa373494%28v=vs.85%29.aspx
363 * for details - gd */
365 /* for now do not process the list of deleted group policies
367 for (gpo = deleted_gpo_list; gpo; gpo = gpo->next) {
372 for (gpo = changed_gpo_list; gpo; gpo = gpo->next) {
374 gpext_debug_header(0, "scripts_process_group_policy", flags,
375 gpo, GP_EXT_GUID_SCRIPTS, NULL);
377 status = gpo_get_unix_path(mem_ctx, cache_path(GPO_CACHE_DIR),
378 gpo, &unix_path);
379 NT_STATUS_NOT_OK_RETURN(status);
381 status = gp_inifile_init_context(mem_ctx, flags, unix_path,
382 GP_SCRIPTS_INI, &ini_ctx);
383 NT_STATUS_NOT_OK_RETURN(status);
385 for (i = 0; i < ARRAY_SIZE(list); i++) {
387 TALLOC_FREE(entries);
388 num_entries = 0;
390 status = scripts_parse_ini_section(ini_ctx, flags, list[i],
391 &entries, &num_entries);
392 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
393 continue;
396 if (!NT_STATUS_IS_OK(status)) {
397 return status;
400 dump_reg_entries(flags, "READ", entries, num_entries);
402 werr = scripts_apply(ini_ctx->mem_ctx, token, root_key,
403 flags, list[i], gpo, entries, num_entries);
404 if (!W_ERROR_IS_OK(werr)) {
405 continue; /* FIXME: finally fix storing emtpy strings and REG_QWORD! */
406 TALLOC_FREE(ini_ctx);
407 return werror_to_ntstatus(werr);
411 TALLOC_FREE(ini_ctx);
414 return NT_STATUS_OK;
417 /****************************************************************
418 ****************************************************************/
420 static NTSTATUS scripts_initialize(TALLOC_CTX *mem_ctx)
422 return NT_STATUS_OK;
425 /****************************************************************
426 ****************************************************************/
428 static NTSTATUS scripts_shutdown(void)
430 NTSTATUS status;
432 status = gpext_unregister_gp_extension(GP_EXT_NAME);
433 if (NT_STATUS_IS_OK(status)) {
434 return status;
437 TALLOC_FREE(ctx);
439 return NT_STATUS_OK;
442 /****************************************************************
443 ****************************************************************/
445 static struct gp_extension_methods scripts_methods = {
446 .initialize = scripts_initialize,
447 .process_group_policy = scripts_process_group_policy,
448 .get_reg_config = scripts_get_reg_config,
449 .shutdown = scripts_shutdown
452 /****************************************************************
453 ****************************************************************/
455 NTSTATUS gpext_scripts_init(void)
457 NTSTATUS status;
459 ctx = talloc_init("gpext_scripts_init");
460 NT_STATUS_HAVE_NO_MEMORY(ctx);
462 status = gpext_register_gp_extension(ctx, SMB_GPEXT_INTERFACE_VERSION,
463 GP_EXT_NAME, GP_EXT_GUID_SCRIPTS,
464 &scripts_methods);
465 if (!NT_STATUS_IS_OK(status)) {
466 TALLOC_FREE(ctx);
469 return status;