tdb: Apply some const, just for clarity
[Samba/kamenim.git] / libgpo / gpext / gpext.c
blobb6cc410eec922d44370272952a22417c5e62e143
1 /*
2 * Unix SMB/CIFS implementation.
3 * Group Policy Support
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/>.
20 #include "includes.h"
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)
32 return extensions;
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 },
52 /* new */
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 },
59 { NULL, REG_NONE }
62 /****************************************************************
63 ****************************************************************/
65 static struct gp_extension *get_extension_by_name(struct gp_extension *be,
66 const char *name)
68 struct gp_extension *b;
70 for (b = be; b; b = b->next) {
71 if (strequal(b->name, name)) {
72 return b;
76 return NULL;
79 /****************************************************************
80 ****************************************************************/
82 static struct gp_extension_methods *get_methods_by_name(struct gp_extension *be,
83 const char *name)
85 struct gp_extension *b;
87 for (b = be; b; b = b->next) {
88 if (strequal(b->name, name)) {
89 return b->methods;
93 return NULL;
96 /****************************************************************
97 ****************************************************************/
99 NTSTATUS unregister_gp_extension(const char *name)
101 struct gp_extension *ext;
103 ext = get_extension_by_name(extensions, name);
104 if (!ext) {
105 return NT_STATUS_OK;
108 DLIST_REMOVE(extensions, ext);
109 talloc_free(ext);
111 DEBUG(2,("Successfully removed GP extension '%s'\n", name));
113 return NT_STATUS_OK;
116 /****************************************************************
117 ****************************************************************/
119 NTSTATUS register_gp_extension(TALLOC_CTX *gpext_ctx,
120 int version,
121 const char *name,
122 const char *guid,
123 struct gp_extension_methods *methods)
125 struct gp_extension_methods *test;
126 struct gp_extension *entry;
127 NTSTATUS status;
129 if (!gpext_ctx) {
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);
150 if (test) {
151 DEBUG(0,("GP extension module %s already registered!\n",
152 name));
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)));
173 return NT_STATUS_OK;
176 /****************************************************************
177 ****************************************************************/
179 static NTSTATUS gp_extension_init_module(TALLOC_CTX *mem_ctx,
180 const char *name,
181 struct gp_extension **gpext)
183 NTSTATUS status;
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);
190 if (!ext->methods) {
192 status = smb_probe_module(SAMBA_SUBSYSTEM_GPEXT,
193 name);
194 if (!NT_STATUS_IS_OK(status)) {
195 return status;
198 ext->methods = get_methods_by_name(extensions, name);
199 if (!ext->methods) {
200 return NT_STATUS_DLL_INIT_FAILED;
204 *gpext = ext;
206 return NT_STATUS_OK;
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,
215 size_t *num)
217 *entries = talloc_realloc(mem_ctx, *entries,
218 struct gp_extension_reg_entry,
219 (*num)+1);
220 if (*entries == NULL) {
221 *num = 0;
222 return false;
225 (*entries)[*num].value = entry->value;
226 (*entries)[*num].data = entry->data;
228 *num += 1;
229 return true;
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,
238 size_t *num)
240 *entries = talloc_realloc(mem_ctx, *entries,
241 struct gp_extension_reg_info_entry,
242 (*num)+1);
243 if (*entries == NULL) {
244 *num = 0;
245 return false;
248 (*entries)[*num].guid = entry->guid;
249 (*entries)[*num].num_entries = entry->num_entries;
250 (*entries)[*num].entries = entry->entries;
252 *num += 1;
253 return true;
256 /****************************************************************
257 ****************************************************************/
259 static NTSTATUS gp_ext_info_add_reg(TALLOC_CTX *mem_ctx,
260 struct gp_extension_reg_info_entry *entry,
261 const char *value,
262 enum winreg_Type type,
263 const char *data_s)
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);
274 data->type = type;
276 switch (type) {
277 case REG_SZ:
278 case REG_EXPAND_SZ:
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);
282 break;
283 case REG_DWORD:
284 data->v.dword = atoi(data_s);
285 break;
286 default:
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,
294 &entry->entries,
295 &entry->num_entries)) {
296 return NT_STATUS_NO_MEMORY;
299 return NT_STATUS_OK;
302 /****************************************************************
303 ****************************************************************/
305 static NTSTATUS gp_ext_info_add_reg_table(TALLOC_CTX *mem_ctx,
306 const char *module,
307 struct gp_extension_reg_info_entry *entry,
308 struct gp_extension_reg_table *table)
310 NTSTATUS status;
311 const char *module_name = NULL;
312 int i;
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,
323 table[i].val,
324 table[i].type,
325 table[i].data);
326 NT_STATUS_NOT_OK_RETURN(status);
329 return status;
332 /****************************************************************
333 ****************************************************************/
335 NTSTATUS gp_ext_info_add_entry(TALLOC_CTX *mem_ctx,
336 const char *module,
337 const char *ext_guid,
338 struct gp_extension_reg_table *table,
339 struct gp_extension_reg_info *info)
341 NTSTATUS status;
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,
354 &info->entries,
355 &info->num_entries)) {
356 return NT_STATUS_NO_MEMORY;
359 return NT_STATUS_OK;
362 /****************************************************************
363 ****************************************************************/
365 static bool gp_extension_reg_info_verify_entry(struct gp_extension_reg_entry *entry)
367 int i;
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)) {
373 return true;
377 return false;
380 /****************************************************************
381 ****************************************************************/
383 static bool gp_extension_reg_info_verify(struct gp_extension_reg_info_entry *entry)
385 int i;
387 for (i=0; i < entry->num_entries; i++) {
388 if (!gp_extension_reg_info_verify_entry(&entry->entries[i])) {
389 return false;
393 return true;
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;
404 size_t i;
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);
414 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)
424 WERROR werr;
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,
438 subkeyname,
439 reg_ctx->curr_key,
440 &key);
441 W_ERROR_NOT_OK_RETURN(werr);
443 werr = gp_extension_store_reg_vals(mem_ctx,
444 key,
445 entry);
446 W_ERROR_NOT_OK_RETURN(werr);
448 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;
459 int i;
461 if (!info) {
462 return WERR_OK;
465 for (i=0; i < info->num_entries; i++) {
466 werr = gp_extension_store_reg_entry(mem_ctx,
467 reg_ctx,
468 &info->entries[i]);
469 W_ERROR_NOT_OK_RETURN(werr);
472 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));
486 if (!dir) {
487 return map_nt_error_from_unix(errno);
490 while ((dirent = sys_readdir(dir))) {
492 fstring name; /* forgive me... */
493 char *p;
495 if ((strequal(dirent->d_name, ".")) ||
496 (strequal(dirent->d_name, ".."))) {
497 continue;
500 p = strrchr(dirent->d_name, '.');
501 if (!p) {
502 sys_closedir(dir);
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",
508 dirent->d_name));
509 continue;
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)) {
517 sys_closedir(dir);
518 return NT_STATUS_NO_MEMORY;
522 sys_closedir(dir);
524 return NT_STATUS_OK;
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();
540 return NT_STATUS_OK;
543 /****************************************************************
544 ****************************************************************/
546 NTSTATUS init_gp_extensions(TALLOC_CTX *mem_ctx)
548 NTSTATUS status;
549 WERROR werr;
550 int i = 0;
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()) {
557 return NT_STATUS_OK;
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],
568 &gpext);
569 if (!NT_STATUS_IS_OK(status)) {
570 goto out;
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();
578 goto out;
581 status = gpext->methods->get_reg_config(mem_ctx,
582 &info);
583 if (!NT_STATUS_IS_OK(status)) {
584 gpext->methods->shutdown();
585 goto out;
588 if (!reg_ctx) {
589 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,
596 REG_KEY_WRITE,
597 token,
598 &reg_ctx);
599 if (!W_ERROR_IS_OK(werr)) {
600 status = werror_to_ntstatus(werr);
601 gpext->methods->shutdown();
602 goto out;
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",
609 win_errstr(werr)));
610 TALLOC_FREE(info);
611 gpext->methods->shutdown();
612 status = werror_to_ntstatus(werr);
613 goto out;
615 TALLOC_FREE(info);
620 out:
621 TALLOC_FREE(reg_ctx);
623 return status;
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);
636 TALLOC_FREE(ext);
639 extensions = NULL;
641 return NT_STATUS_OK;
644 /****************************************************************
645 ****************************************************************/
647 void debug_gpext_header(int lvl,
648 const char *name,
649 uint32_t flags,
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,
658 gpo->display_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(NULL, flags);
666 DEBUGADD(lvl,("\tflags: 0x%08x %s\n", flags, flags_str));
667 TALLOC_FREE(flags_str);
670 NTSTATUS process_gpo_list_with_extension(ADS_STRUCT *ads,
671 TALLOC_CTX *mem_ctx,
672 uint32_t flags,
673 const NT_USER_TOKEN *token,
674 struct GROUP_POLICY_OBJECT *gpo_list,
675 const char *extension_guid,
676 const char *snapin_guid)
678 return NT_STATUS_OK;
681 /****************************************************************
682 ****************************************************************/
684 NTSTATUS gpext_process_extension(ADS_STRUCT *ads,
685 TALLOC_CTX *mem_ctx,
686 uint32_t flags,
687 const 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)
693 NTSTATUS status;
694 struct gp_extension *ext = NULL;
695 struct GUID guid;
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",
701 nt_errstr(status)));
702 return status;
705 status = GUID_from_string(extension_guid, &guid);
706 if (!NT_STATUS_IS_OK(status)) {
707 return status;
710 for (ext = extensions; ext; ext = ext->next) {
712 if (GUID_equal(ext->guid, &guid)) {
713 cse_found = true;
714 break;
718 if (!cse_found) {
719 goto no_ext;
722 status = ext->methods->initialize(mem_ctx);
723 NT_STATUS_NOT_OK_RETURN(status);
725 status = ext->methods->process_group_policy(ads,
726 mem_ctx,
727 flags,
728 root_key,
729 token,
730 gpo,
731 extension_guid,
732 snapin_guid);
733 if (!NT_STATUS_IS_OK(status)) {
734 ext->methods->shutdown();
737 return status;
739 no_ext:
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",
743 extension_guid,
744 cse_gpo_guid_string_to_name(extension_guid),
745 snapin_guid));
748 return NT_STATUS_OK;