2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Guenther Deschner 2007-2008
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/gpext/gpext.h"
22 #include "librpc/gen_ndr/ndr_misc.h"
23 #include "lib/util/dlinklist.h"
25 static struct gp_extension *extensions = NULL;
27 /****************************************************************
28 ****************************************************************/
30 struct gp_extension *get_gp_extension_list(void)
35 /****************************************************************
36 ****************************************************************/
38 /* see http://support.microsoft.com/kb/216358/en-us/ for more info */
40 struct gp_extension_reg_table gpext_reg_vals[] = {
41 { "DllName", REG_EXPAND_SZ },
42 { "ProcessGroupPolicy", REG_SZ },
43 { "NoMachinePolicy", REG_DWORD },
44 { "NoUserPolicy", REG_DWORD },
45 { "NoSlowLink", REG_DWORD },
46 { "NoBackgroundPolicy", REG_DWORD },
47 { "NoGPOListChanges", REG_DWORD },
48 { "PerUserLocalSettings", REG_DWORD },
49 { "RequiresSuccessfulRegistry", REG_DWORD },
50 { "EnableAsynchronousProcessing", REG_DWORD },
51 { "ExtensionDebugLevel", REG_DWORD },
53 { "GenerateGroupPolicy", REG_SZ }, /* not supported on w2k */
54 { "NotifyLinkTransition", REG_DWORD },
55 { "ProcessGroupPolicyEx", REG_SZ }, /* not supported on w2k */
56 { "ExtensionEventSource", REG_MULTI_SZ }, /* not supported on w2k */
57 { "GenerateGroupPolicy", REG_SZ },
58 { "MaxNoGPOListChangesInterval", REG_DWORD },
62 /****************************************************************
63 ****************************************************************/
65 static struct gp_extension *get_extension_by_name(struct gp_extension *be,
68 struct gp_extension *b;
70 for (b = be; b; b = b->next) {
71 if (strequal(b->name, name)) {
79 /****************************************************************
80 ****************************************************************/
82 static struct gp_extension_methods *get_methods_by_name(struct gp_extension *be,
85 struct gp_extension *b;
87 for (b = be; b; b = b->next) {
88 if (strequal(b->name, name)) {
96 /****************************************************************
97 ****************************************************************/
99 NTSTATUS unregister_gp_extension(const char *name)
101 struct gp_extension *ext;
103 ext = get_extension_by_name(extensions, name);
108 DLIST_REMOVE(extensions, ext);
111 DEBUG(2,("Successfully removed GP extension '%s'\n", name));
116 /****************************************************************
117 ****************************************************************/
119 NTSTATUS register_gp_extension(TALLOC_CTX *gpext_ctx,
123 struct gp_extension_methods *methods)
125 struct gp_extension_methods *test;
126 struct gp_extension *entry;
130 return NT_STATUS_INTERNAL_DB_ERROR;
133 if ((version != SMB_GPEXT_INTERFACE_VERSION)) {
134 DEBUG(0,("Failed to register gp extension.\n"
135 "The module was compiled against "
136 "SMB_GPEXT_INTERFACE_VERSION %d,\n"
137 "current SMB_GPEXT_INTERFACE_VERSION is %d.\n"
138 "Please recompile against the current "
139 "version of samba!\n",
140 version, SMB_GPEXT_INTERFACE_VERSION));
141 return NT_STATUS_OBJECT_TYPE_MISMATCH;
144 if (!guid || !name || !name[0] || !methods) {
145 DEBUG(0,("Called with NULL pointer or empty name!\n"));
146 return NT_STATUS_INVALID_PARAMETER;
149 test = get_methods_by_name(extensions, name);
151 DEBUG(0,("GP extension module %s already registered!\n",
153 return NT_STATUS_OBJECT_NAME_COLLISION;
156 entry = talloc_zero(gpext_ctx, struct gp_extension);
157 NT_STATUS_HAVE_NO_MEMORY(entry);
159 entry->name = talloc_strdup(gpext_ctx, name);
160 NT_STATUS_HAVE_NO_MEMORY(entry->name);
162 entry->guid = talloc_zero(gpext_ctx, struct GUID);
163 NT_STATUS_HAVE_NO_MEMORY(entry->guid);
164 status = GUID_from_string(guid, entry->guid);
165 NT_STATUS_NOT_OK_RETURN(status);
167 entry->methods = methods;
168 DLIST_ADD(extensions, entry);
170 DEBUG(2,("Successfully added GP extension '%s' %s\n",
171 name, GUID_string2(gpext_ctx, entry->guid)));
176 /****************************************************************
177 ****************************************************************/
179 static NTSTATUS gp_extension_init_module(TALLOC_CTX *mem_ctx,
181 struct gp_extension **gpext)
184 struct gp_extension *ext = NULL;
186 ext = talloc_zero(mem_ctx, struct gp_extension);
187 NT_STATUS_HAVE_NO_MEMORY(gpext);
189 ext->methods = get_methods_by_name(extensions, name);
192 status = smb_probe_module(SAMBA_SUBSYSTEM_GPEXT,
194 if (!NT_STATUS_IS_OK(status)) {
198 ext->methods = get_methods_by_name(extensions, name);
200 return NT_STATUS_DLL_INIT_FAILED;
209 /****************************************************************
210 ****************************************************************/
212 static bool add_gp_extension_reg_entry_to_array(TALLOC_CTX *mem_ctx,
213 struct gp_extension_reg_entry *entry,
214 struct gp_extension_reg_entry **entries,
217 *entries = talloc_realloc(mem_ctx, *entries,
218 struct gp_extension_reg_entry,
220 if (*entries == NULL) {
225 (*entries)[*num].value = entry->value;
226 (*entries)[*num].data = entry->data;
232 /****************************************************************
233 ****************************************************************/
235 static bool add_gp_extension_reg_info_entry_to_array(TALLOC_CTX *mem_ctx,
236 struct gp_extension_reg_info_entry *entry,
237 struct gp_extension_reg_info_entry **entries,
240 *entries = talloc_realloc(mem_ctx, *entries,
241 struct gp_extension_reg_info_entry,
243 if (*entries == NULL) {
248 (*entries)[*num].guid = entry->guid;
249 (*entries)[*num].num_entries = entry->num_entries;
250 (*entries)[*num].entries = entry->entries;
256 /****************************************************************
257 ****************************************************************/
259 static NTSTATUS gp_ext_info_add_reg(TALLOC_CTX *mem_ctx,
260 struct gp_extension_reg_info_entry *entry,
262 enum winreg_Type type,
265 struct gp_extension_reg_entry *reg_entry = NULL;
266 struct registry_value *data = NULL;
268 reg_entry = talloc_zero(mem_ctx, struct gp_extension_reg_entry);
269 NT_STATUS_HAVE_NO_MEMORY(reg_entry);
271 data = talloc_zero(mem_ctx, struct registry_value);
272 NT_STATUS_HAVE_NO_MEMORY(data);
279 data->v.sz.str = talloc_strdup(mem_ctx, data_s);
280 NT_STATUS_HAVE_NO_MEMORY(data->v.sz.str);
281 data->v.sz.len = strlen(data_s);
284 data->v.dword = atoi(data_s);
287 return NT_STATUS_NOT_SUPPORTED;
290 reg_entry->value = value;
291 reg_entry->data = data;
293 if (!add_gp_extension_reg_entry_to_array(mem_ctx, reg_entry,
295 &entry->num_entries)) {
296 return NT_STATUS_NO_MEMORY;
302 /****************************************************************
303 ****************************************************************/
305 static NTSTATUS gp_ext_info_add_reg_table(TALLOC_CTX *mem_ctx,
307 struct gp_extension_reg_info_entry *entry,
308 struct gp_extension_reg_table *table)
311 const char *module_name = NULL;
314 module_name = talloc_asprintf(mem_ctx, "%s.%s", module, shlib_ext());
315 NT_STATUS_HAVE_NO_MEMORY(module_name);
317 status = gp_ext_info_add_reg(mem_ctx, entry,
318 "DllName", REG_EXPAND_SZ, module_name);
319 NT_STATUS_NOT_OK_RETURN(status);
321 for (i=0; table[i].val; i++) {
322 status = gp_ext_info_add_reg(mem_ctx, entry,
326 NT_STATUS_NOT_OK_RETURN(status);
332 /****************************************************************
333 ****************************************************************/
335 NTSTATUS gp_ext_info_add_entry(TALLOC_CTX *mem_ctx,
337 const char *ext_guid,
338 struct gp_extension_reg_table *table,
339 struct gp_extension_reg_info *info)
342 struct gp_extension_reg_info_entry *entry = NULL;
344 entry = TALLOC_ZERO_P(mem_ctx, struct gp_extension_reg_info_entry);
345 NT_STATUS_HAVE_NO_MEMORY(entry);
347 status = GUID_from_string(ext_guid, &entry->guid);
348 NT_STATUS_NOT_OK_RETURN(status);
350 status = gp_ext_info_add_reg_table(mem_ctx, module, entry, table);
351 NT_STATUS_NOT_OK_RETURN(status);
353 if (!add_gp_extension_reg_info_entry_to_array(mem_ctx, entry,
355 &info->num_entries)) {
356 return NT_STATUS_NO_MEMORY;
362 /****************************************************************
363 ****************************************************************/
365 static bool gp_extension_reg_info_verify_entry(struct gp_extension_reg_entry *entry)
369 for (i=0; gpext_reg_vals[i].val; i++) {
371 if ((strequal(entry->value, gpext_reg_vals[i].val)) &&
372 (entry->data->type == gpext_reg_vals[i].type)) {
380 /****************************************************************
381 ****************************************************************/
383 static bool gp_extension_reg_info_verify(struct gp_extension_reg_info_entry *entry)
387 for (i=0; i < entry->num_entries; i++) {
388 if (!gp_extension_reg_info_verify_entry(&entry->entries[i])) {
396 /****************************************************************
397 ****************************************************************/
399 static WERROR gp_extension_store_reg_vals(TALLOC_CTX *mem_ctx,
400 struct registry_key *key,
401 struct gp_extension_reg_info_entry *entry)
403 WERROR werr = WERR_OK;
406 for (i=0; i < entry->num_entries; i++) {
408 werr = reg_setvalue(key,
409 entry->entries[i].value,
410 entry->entries[i].data);
411 W_ERROR_NOT_OK_RETURN(werr);
417 /****************************************************************
418 ****************************************************************/
420 static WERROR gp_extension_store_reg_entry(TALLOC_CTX *mem_ctx,
421 struct gp_registry_context *reg_ctx,
422 struct gp_extension_reg_info_entry *entry)
425 struct registry_key *key = NULL;
426 const char *subkeyname = NULL;
428 if (!gp_extension_reg_info_verify(entry)) {
429 return WERR_INVALID_PARAM;
432 subkeyname = GUID_string2(mem_ctx, &entry->guid);
433 W_ERROR_HAVE_NO_MEMORY(subkeyname);
435 strupper_m(CONST_DISCARD(char *,subkeyname));
437 werr = gp_store_reg_subkey(mem_ctx,
441 W_ERROR_NOT_OK_RETURN(werr);
443 werr = gp_extension_store_reg_vals(mem_ctx,
446 W_ERROR_NOT_OK_RETURN(werr);
451 /****************************************************************
452 ****************************************************************/
454 static WERROR gp_extension_store_reg(TALLOC_CTX *mem_ctx,
455 struct gp_registry_context *reg_ctx,
456 struct gp_extension_reg_info *info)
458 WERROR werr = WERR_OK;
465 for (i=0; i < info->num_entries; i++) {
466 werr = gp_extension_store_reg_entry(mem_ctx,
469 W_ERROR_NOT_OK_RETURN(werr);
475 /****************************************************************
476 ****************************************************************/
478 static NTSTATUS gp_glob_ext_list(TALLOC_CTX *mem_ctx,
479 const char ***ext_list,
480 size_t *ext_list_len)
482 SMB_STRUCT_DIR *dir = NULL;
483 SMB_STRUCT_DIRENT *dirent = NULL;
485 dir = sys_opendir(modules_path(SAMBA_SUBSYSTEM_GPEXT));
487 return map_nt_error_from_unix(errno);
490 while ((dirent = sys_readdir(dir))) {
492 fstring name; /* forgive me... */
495 if ((strequal(dirent->d_name, ".")) ||
496 (strequal(dirent->d_name, ".."))) {
500 p = strrchr(dirent->d_name, '.');
503 return NT_STATUS_NO_MEMORY;
506 if (!strcsequal(p+1, shlib_ext())) {
507 DEBUG(10,("gp_glob_ext_list: not a *.so file: %s\n",
512 fstrcpy(name, dirent->d_name);
513 name[PTR_DIFF(p, dirent->d_name)] = 0;
515 if (!add_string_to_array(mem_ctx, name, ext_list,
516 (int *)ext_list_len)) {
518 return NT_STATUS_NO_MEMORY;
527 /****************************************************************
528 ****************************************************************/
530 NTSTATUS shutdown_gp_extensions(void)
532 struct gp_extension *ext = NULL;
534 for (ext = extensions; ext; ext = ext->next) {
535 if (ext->methods && ext->methods->shutdown) {
536 ext->methods->shutdown();
543 /****************************************************************
544 ****************************************************************/
546 NTSTATUS init_gp_extensions(TALLOC_CTX *mem_ctx)
551 const char **ext_array = NULL;
552 size_t ext_array_len = 0;
553 struct gp_extension *gpext = NULL;
554 struct gp_registry_context *reg_ctx = NULL;
556 if (get_gp_extension_list()) {
560 status = gp_glob_ext_list(mem_ctx, &ext_array, &ext_array_len);
561 NT_STATUS_NOT_OK_RETURN(status);
563 for (i=0; i<ext_array_len; i++) {
565 struct gp_extension_reg_info *info = NULL;
567 status = gp_extension_init_module(mem_ctx, ext_array[i],
569 if (!NT_STATUS_IS_OK(status)) {
573 if (gpext->methods->get_reg_config) {
575 status = gpext->methods->initialize(mem_ctx);
576 if (!NT_STATUS_IS_OK(status)) {
577 gpext->methods->shutdown();
581 status = gpext->methods->get_reg_config(mem_ctx,
583 if (!NT_STATUS_IS_OK(status)) {
584 gpext->methods->shutdown();
589 struct nt_user_token *token;
591 token = registry_create_system_token(mem_ctx);
592 NT_STATUS_HAVE_NO_MEMORY(token);
594 werr = gp_init_reg_ctx(mem_ctx,
595 KEY_WINLOGON_GPEXT_PATH,
599 if (!W_ERROR_IS_OK(werr)) {
600 status = werror_to_ntstatus(werr);
601 gpext->methods->shutdown();
606 werr = gp_extension_store_reg(mem_ctx, reg_ctx, info);
607 if (!W_ERROR_IS_OK(werr)) {
608 DEBUG(1,("gp_extension_store_reg failed: %s\n",
611 gpext->methods->shutdown();
612 status = werror_to_ntstatus(werr);
621 TALLOC_FREE(reg_ctx);
626 /****************************************************************
627 ****************************************************************/
629 NTSTATUS free_gp_extensions(void)
631 struct gp_extension *ext, *ext_next = NULL;
633 for (ext = extensions; ext; ext = ext_next) {
634 ext_next = ext->next;
635 DLIST_REMOVE(extensions, ext);
644 /****************************************************************
645 ****************************************************************/
647 void debug_gpext_header(int lvl,
650 struct GROUP_POLICY_OBJECT *gpo,
651 const char *extension_guid,
652 const char *snapin_guid)
654 char *flags_str = NULL;
656 DEBUG(lvl,("%s\n", name));
657 DEBUGADD(lvl,("\tgpo: %s (%s)\n", gpo->name,
659 DEBUGADD(lvl,("\tcse extension: %s (%s)\n", extension_guid,
660 cse_gpo_guid_string_to_name(extension_guid)));
661 DEBUGADD(lvl,("\tgplink: %s\n", gpo->link));
662 DEBUGADD(lvl,("\tsnapin: %s (%s)\n", snapin_guid,
663 cse_snapin_gpo_guid_string_to_name(snapin_guid)));
665 flags_str = gpo_flag_str(flags);
666 DEBUGADD(lvl,("\tflags: 0x%08x %s\n", flags, flags_str));
667 SAFE_FREE(flags_str);
670 NTSTATUS process_gpo_list_with_extension(ADS_STRUCT *ads,
673 const struct nt_user_token *token,
674 struct GROUP_POLICY_OBJECT *gpo_list,
675 const char *extension_guid,
676 const char *snapin_guid)
681 /****************************************************************
682 ****************************************************************/
684 NTSTATUS gpext_process_extension(ADS_STRUCT *ads,
687 const struct nt_user_token *token,
688 struct registry_key *root_key,
689 struct GROUP_POLICY_OBJECT *gpo,
690 const char *extension_guid,
691 const char *snapin_guid)
694 struct gp_extension *ext = NULL;
696 bool cse_found = false;
698 status = init_gp_extensions(mem_ctx);
699 if (!NT_STATUS_IS_OK(status)) {
700 DEBUG(1,("init_gp_extensions failed: %s\n",
705 status = GUID_from_string(extension_guid, &guid);
706 if (!NT_STATUS_IS_OK(status)) {
710 for (ext = extensions; ext; ext = ext->next) {
712 if (GUID_equal(ext->guid, &guid)) {
722 status = ext->methods->initialize(mem_ctx);
723 NT_STATUS_NOT_OK_RETURN(status);
725 status = ext->methods->process_group_policy(ads,
733 if (!NT_STATUS_IS_OK(status)) {
734 ext->methods->shutdown();
740 if (flags & GPO_INFO_FLAG_VERBOSE) {
741 DEBUG(0,("process_extension: no extension available for:\n"));
742 DEBUGADD(0,("%s (%s) (snapin: %s)\n",
744 cse_gpo_guid_string_to_name(extension_guid),