Fix bug #5908 - Samba 3.0.32 - internal change notify on share directory fails"
[Samba/gebeck_regimport.git] / source3 / libgpo / gpext / scripts.c
blobddea35c644481f3159e1d3e13fc492e96aa5400f
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"
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)
49 NTSTATUS status;
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,
64 GP_EXT_GUID_SCRIPTS,
65 table, info);
66 NT_STATUS_NOT_OK_RETURN(status);
68 *reg_info = info;
70 return NT_STATUS_OK;
73 /****************************************************************
74 ****************************************************************/
76 static NTSTATUS generate_gp_registry_entry(TALLOC_CTX *mem_ctx,
77 const char *key,
78 const char *value,
79 uint32_t data_type,
80 const void *data_p,
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;
94 switch (data->type) {
95 case REG_QWORD:
96 data->v.qword = *(uint64_t *)data_p;
97 break;
98 case REG_SZ:
99 data->v.sz.str = talloc_strdup(mem_ctx, (char *)data_p);
100 data->v.sz.len = strlen(data->v.sz.str);
101 break;
102 default:
103 return NT_STATUS_NOT_SUPPORTED;
106 entry->key = key;
107 entry->data = data;
108 entry->action = action;
109 entry->value = talloc_strdup(mem_ctx, value);
110 NT_STATUS_HAVE_NO_MEMORY(entry->value);
112 *entry_out = entry;
114 return NT_STATUS_OK;
117 /****************************************************************
118 ****************************************************************/
120 static NTSTATUS scripts_parse_ini_section(struct gp_inifile_context *ini_ctx,
121 uint32_t flags,
122 const char *section,
123 struct gp_registry_entry **entries,
124 size_t *num_entries)
126 NTSTATUS status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
127 int i = 0;
129 while (1) {
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",
140 section, count,
141 GP_SCRIPTS_SECTION_CMDLINE);
142 NT_STATUS_HAVE_NO_MEMORY(key);
144 script = iniparser_getstring(ini_ctx->dict, key, NULL);
145 if (!script) {
146 break;
149 key = talloc_asprintf(ini_ctx->mem_ctx, "%s:%s%s",
150 section, count,
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,
159 count,
160 GP_SCRIPTS_REG_VAL_SCRIPT,
161 REG_SZ,
162 script,
163 GP_REG_ACTION_ADD_VALUE,
164 &entry);
165 NT_STATUS_NOT_OK_RETURN(status);
166 if (!add_gp_registry_entry_to_array(ini_ctx->mem_ctx,
167 entry,
168 entries,
169 num_entries)) {
170 return NT_STATUS_NO_MEMORY;
174 struct gp_registry_entry *entry = NULL;
175 status = generate_gp_registry_entry(ini_ctx->mem_ctx,
176 count,
177 GP_SCRIPTS_REG_VAL_PARAMETERS,
178 REG_SZ,
179 parameters,
180 GP_REG_ACTION_ADD_VALUE,
181 &entry);
182 NT_STATUS_NOT_OK_RETURN(status);
183 if (!add_gp_registry_entry_to_array(ini_ctx->mem_ctx,
184 entry,
185 entries,
186 num_entries)) {
187 return NT_STATUS_NO_MEMORY;
191 struct gp_registry_entry *entry = NULL;
192 status = generate_gp_registry_entry(ini_ctx->mem_ctx,
193 count,
194 GP_SCRIPTS_REG_VAL_EXECTIME,
195 REG_QWORD,
197 GP_REG_ACTION_ADD_VALUE,
198 &entry);
199 NT_STATUS_NOT_OK_RETURN(status);
200 if (!add_gp_registry_entry_to_array(ini_ctx->mem_ctx,
201 entry,
202 entries,
203 num_entries)) {
204 return NT_STATUS_NO_MEMORY;
207 status = NT_STATUS_OK;
208 i++;
211 return status;
214 /****************************************************************
215 ****************************************************************/
217 static WERROR scripts_store_reg_gpovals(TALLOC_CTX *mem_ctx,
218 struct registry_key *key,
219 struct GROUP_POLICY_OBJECT *gpo)
221 WERROR werr;
223 if (!key || !gpo) {
224 return WERR_INVALID_PARAM;
227 werr = gp_store_reg_val_sz(mem_ctx, key, "DisplayName",
228 gpo->display_name);
229 W_ERROR_NOT_OK_RETURN(werr);
231 werr = gp_store_reg_val_sz(mem_ctx, key, "FileSysPath",
232 gpo->file_sys_path);
233 W_ERROR_NOT_OK_RETURN(werr);
235 werr = gp_store_reg_val_sz(mem_ctx, key, "GPO-ID",
236 gpo->ds_path);
237 W_ERROR_NOT_OK_RETURN(werr);
239 werr = gp_store_reg_val_sz(mem_ctx, key, "GPOName",
240 gpo->name);
241 W_ERROR_NOT_OK_RETURN(werr);
243 werr = gp_store_reg_val_sz(mem_ctx, key, "SOM-ID",
244 gpo->link);
245 W_ERROR_NOT_OK_RETURN(werr);
247 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,
256 uint32_t flags,
257 const char *section,
258 struct GROUP_POLICY_OBJECT *gpo,
259 struct gp_registry_entry *entries,
260 size_t num_entries)
262 struct gp_registry_context *reg_ctx = NULL;
263 WERROR werr;
264 size_t i;
265 const char *keystr = NULL;
266 int count = 0;
268 if (num_entries == 0) {
269 return WERR_OK;
272 #if 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,
280 tmp_token,
281 &reg_ctx);
282 } else {
283 werr = gp_init_reg_ctx(mem_ctx, KEY_HKCU, REG_KEY_WRITE,
284 token,
285 &reg_ctx);
287 W_ERROR_NOT_OK_RETURN(werr);
288 #endif
290 keystr = talloc_asprintf(mem_ctx, "%s\\%s\\%d", KEY_GP_SCRIPTS,
291 section, count++);
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)) {
299 goto done;
302 werr = scripts_store_reg_gpovals(mem_ctx, root_key, gpo);
303 if (!W_ERROR_IS_OK(werr)) {
304 goto done;
307 for (i=0; i<num_entries; i++) {
309 werr = reg_apply_registry_entry(mem_ctx, root_key, reg_ctx,
310 &(entries)[i],
311 token, flags);
312 if (!W_ERROR_IS_OK(werr)) {
313 DEBUG(0,("failed to apply registry: %s\n",
314 win_errstr(werr)));
315 goto done;
319 done:
320 gp_free_reg_ctx(reg_ctx);
321 return werr;
324 /****************************************************************
325 ****************************************************************/
327 static NTSTATUS scripts_process_group_policy(ADS_STRUCT *ads,
328 TALLOC_CTX *mem_ctx,
329 uint32_t flags,
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)
336 NTSTATUS status;
337 WERROR werr;
338 int i = 0;
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);
363 num_entries = 0;
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)) {
368 continue;
371 if (!NT_STATUS_IS_OK(status)) {
372 return 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);
387 return NT_STATUS_OK;
390 /****************************************************************
391 ****************************************************************/
393 static NTSTATUS scripts_initialize(TALLOC_CTX *mem_ctx)
395 return NT_STATUS_OK;
398 /****************************************************************
399 ****************************************************************/
401 static NTSTATUS scripts_shutdown(void)
403 NTSTATUS status;
405 status = unregister_gp_extension(GP_EXT_NAME);
406 if (NT_STATUS_IS_OK(status)) {
407 return status;
410 TALLOC_FREE(ctx);
412 return NT_STATUS_OK;
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)
430 NTSTATUS status;
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,
437 &scripts_methods);
438 if (!NT_STATUS_IS_OK(status)) {
439 TALLOC_FREE(ctx);
442 return status;