auth.idl fix size_is() reference in IDL
[Samba.git] / libgpo / gpext / gpext.c
blob8c70f65221b97fa17148ae025192e3bbf29cd74f
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/gpo.h"
22 #include "../libgpo/gpext/gpext.h"
23 #include "librpc/gen_ndr/ndr_misc.h"
24 #include "lib/util/dlinklist.h"
25 #if _SAMBA_BUILD_ == 3
26 #include "libgpo/gpo_proto.h"
27 #include "registry.h"
28 #include "registry/reg_api.h"
29 #endif
31 static struct gp_extension *extensions = NULL;
33 /****************************************************************
34 ****************************************************************/
36 struct gp_extension *get_gp_extension_list(void)
38 return extensions;
41 /****************************************************************
42 ****************************************************************/
44 /* see http://support.microsoft.com/kb/216358/en-us/ for more info */
46 struct gp_extension_reg_table gpext_reg_vals[] = {
47 { "DllName", REG_EXPAND_SZ },
48 { "ProcessGroupPolicy", REG_SZ },
49 { "NoMachinePolicy", REG_DWORD },
50 { "NoUserPolicy", REG_DWORD },
51 { "NoSlowLink", REG_DWORD },
52 { "NoBackgroundPolicy", REG_DWORD },
53 { "NoGPOListChanges", REG_DWORD },
54 { "PerUserLocalSettings", REG_DWORD },
55 { "RequiresSuccessfulRegistry", REG_DWORD },
56 { "EnableAsynchronousProcessing", REG_DWORD },
57 { "ExtensionDebugLevel", REG_DWORD },
58 /* new */
59 { "GenerateGroupPolicy", REG_SZ }, /* not supported on w2k */
60 { "NotifyLinkTransition", REG_DWORD },
61 { "ProcessGroupPolicyEx", REG_SZ }, /* not supported on w2k */
62 { "ExtensionEventSource", REG_MULTI_SZ }, /* not supported on w2k */
63 { "GenerateGroupPolicy", REG_SZ },
64 { "MaxNoGPOListChangesInterval", REG_DWORD },
65 { NULL, REG_NONE }
68 /****************************************************************
69 ****************************************************************/
71 static struct gp_extension *get_extension_by_name(struct gp_extension *be,
72 const char *name)
74 struct gp_extension *b;
76 for (b = be; b; b = b->next) {
77 if (strequal(b->name, name)) {
78 return b;
82 return NULL;
85 /****************************************************************
86 ****************************************************************/
88 static struct gp_extension_methods *get_methods_by_name(struct gp_extension *be,
89 const char *name)
91 struct gp_extension *b;
93 for (b = be; b; b = b->next) {
94 if (strequal(b->name, name)) {
95 return b->methods;
99 return NULL;
102 /****************************************************************
103 ****************************************************************/
105 NTSTATUS unregister_gp_extension(const char *name)
107 struct gp_extension *ext;
109 ext = get_extension_by_name(extensions, name);
110 if (!ext) {
111 return NT_STATUS_OK;
114 DLIST_REMOVE(extensions, ext);
115 talloc_free(ext);
117 DEBUG(2,("Successfully removed GP extension '%s'\n", name));
119 return NT_STATUS_OK;
122 /****************************************************************
123 ****************************************************************/
125 NTSTATUS register_gp_extension(TALLOC_CTX *gpext_ctx,
126 int version,
127 const char *name,
128 const char *guid,
129 struct gp_extension_methods *methods)
131 struct gp_extension_methods *test;
132 struct gp_extension *entry;
133 NTSTATUS status;
135 if (!gpext_ctx) {
136 return NT_STATUS_INTERNAL_DB_ERROR;
139 if ((version != SMB_GPEXT_INTERFACE_VERSION)) {
140 DEBUG(0,("Failed to register gp extension.\n"
141 "The module was compiled against "
142 "SMB_GPEXT_INTERFACE_VERSION %d,\n"
143 "current SMB_GPEXT_INTERFACE_VERSION is %d.\n"
144 "Please recompile against the current "
145 "version of samba!\n",
146 version, SMB_GPEXT_INTERFACE_VERSION));
147 return NT_STATUS_OBJECT_TYPE_MISMATCH;
150 if (!guid || !name || !name[0] || !methods) {
151 DEBUG(0,("Called with NULL pointer or empty name!\n"));
152 return NT_STATUS_INVALID_PARAMETER;
155 test = get_methods_by_name(extensions, name);
156 if (test) {
157 DEBUG(0,("GP extension module %s already registered!\n",
158 name));
159 return NT_STATUS_OBJECT_NAME_COLLISION;
162 entry = talloc_zero(gpext_ctx, struct gp_extension);
163 NT_STATUS_HAVE_NO_MEMORY(entry);
165 entry->name = talloc_strdup(gpext_ctx, name);
166 NT_STATUS_HAVE_NO_MEMORY(entry->name);
168 entry->guid = talloc_zero(gpext_ctx, struct GUID);
169 NT_STATUS_HAVE_NO_MEMORY(entry->guid);
170 status = GUID_from_string(guid, entry->guid);
171 NT_STATUS_NOT_OK_RETURN(status);
173 entry->methods = methods;
174 DLIST_ADD(extensions, entry);
176 DEBUG(2,("Successfully added GP extension '%s' %s\n",
177 name, GUID_string2(gpext_ctx, entry->guid)));
179 return NT_STATUS_OK;
182 /****************************************************************
183 ****************************************************************/
185 static NTSTATUS gp_extension_init_module(TALLOC_CTX *mem_ctx,
186 const char *name,
187 struct gp_extension **gpext)
189 NTSTATUS status;
190 struct gp_extension *ext = NULL;
192 ext = talloc_zero(mem_ctx, struct gp_extension);
193 NT_STATUS_HAVE_NO_MEMORY(gpext);
195 ext->methods = get_methods_by_name(extensions, name);
196 if (!ext->methods) {
198 status = smb_probe_module(SAMBA_SUBSYSTEM_GPEXT,
199 name);
200 if (!NT_STATUS_IS_OK(status)) {
201 return status;
204 ext->methods = get_methods_by_name(extensions, name);
205 if (!ext->methods) {
206 return NT_STATUS_DLL_INIT_FAILED;
210 *gpext = ext;
212 return NT_STATUS_OK;
215 /****************************************************************
216 ****************************************************************/
218 static bool add_gp_extension_reg_entry_to_array(TALLOC_CTX *mem_ctx,
219 struct gp_extension_reg_entry *entry,
220 struct gp_extension_reg_entry **entries,
221 size_t *num)
223 *entries = talloc_realloc(mem_ctx, *entries,
224 struct gp_extension_reg_entry,
225 (*num)+1);
226 if (*entries == NULL) {
227 *num = 0;
228 return false;
231 (*entries)[*num].value = entry->value;
232 (*entries)[*num].data = entry->data;
234 *num += 1;
235 return true;
238 /****************************************************************
239 ****************************************************************/
241 static bool add_gp_extension_reg_info_entry_to_array(TALLOC_CTX *mem_ctx,
242 struct gp_extension_reg_info_entry *entry,
243 struct gp_extension_reg_info_entry **entries,
244 size_t *num)
246 *entries = talloc_realloc(mem_ctx, *entries,
247 struct gp_extension_reg_info_entry,
248 (*num)+1);
249 if (*entries == NULL) {
250 *num = 0;
251 return false;
254 (*entries)[*num].guid = entry->guid;
255 (*entries)[*num].num_entries = entry->num_entries;
256 (*entries)[*num].entries = entry->entries;
258 *num += 1;
259 return true;
262 /****************************************************************
263 ****************************************************************/
265 static NTSTATUS gp_ext_info_add_reg(TALLOC_CTX *mem_ctx,
266 struct gp_extension_reg_info_entry *entry,
267 const char *value,
268 enum winreg_Type type,
269 const char *data_s)
271 struct gp_extension_reg_entry *reg_entry = NULL;
272 struct registry_value *data = NULL;
274 reg_entry = talloc_zero(mem_ctx, struct gp_extension_reg_entry);
275 NT_STATUS_HAVE_NO_MEMORY(reg_entry);
277 data = talloc_zero(mem_ctx, struct registry_value);
278 NT_STATUS_HAVE_NO_MEMORY(data);
280 data->type = type;
282 switch (type) {
283 case REG_SZ:
284 case REG_EXPAND_SZ:
285 if (!push_reg_sz(mem_ctx, &data->data, data_s)) {
286 return NT_STATUS_NO_MEMORY;
288 break;
289 case REG_DWORD: {
290 uint32_t v = atoi(data_s);
291 data->data = data_blob_talloc(mem_ctx, NULL, 4);
292 SIVAL(data->data.data, 0, v);
293 break;
295 default:
296 return NT_STATUS_NOT_SUPPORTED;
299 reg_entry->value = value;
300 reg_entry->data = data;
302 if (!add_gp_extension_reg_entry_to_array(mem_ctx, reg_entry,
303 &entry->entries,
304 &entry->num_entries)) {
305 return NT_STATUS_NO_MEMORY;
308 return NT_STATUS_OK;
311 /****************************************************************
312 ****************************************************************/
314 static NTSTATUS gp_ext_info_add_reg_table(TALLOC_CTX *mem_ctx,
315 const char *module,
316 struct gp_extension_reg_info_entry *entry,
317 struct gp_extension_reg_table *table)
319 NTSTATUS status;
320 const char *module_name = NULL;
321 int i;
323 module_name = talloc_asprintf(mem_ctx, "%s.%s", module, shlib_ext());
324 NT_STATUS_HAVE_NO_MEMORY(module_name);
326 status = gp_ext_info_add_reg(mem_ctx, entry,
327 "DllName", REG_EXPAND_SZ, module_name);
328 NT_STATUS_NOT_OK_RETURN(status);
330 for (i=0; table[i].val; i++) {
331 status = gp_ext_info_add_reg(mem_ctx, entry,
332 table[i].val,
333 table[i].type,
334 table[i].data);
335 NT_STATUS_NOT_OK_RETURN(status);
338 return status;
341 /****************************************************************
342 ****************************************************************/
344 NTSTATUS gp_ext_info_add_entry(TALLOC_CTX *mem_ctx,
345 const char *module,
346 const char *ext_guid,
347 struct gp_extension_reg_table *table,
348 struct gp_extension_reg_info *info)
350 NTSTATUS status;
351 struct gp_extension_reg_info_entry *entry = NULL;
353 entry = TALLOC_ZERO_P(mem_ctx, struct gp_extension_reg_info_entry);
354 NT_STATUS_HAVE_NO_MEMORY(entry);
356 status = GUID_from_string(ext_guid, &entry->guid);
357 NT_STATUS_NOT_OK_RETURN(status);
359 status = gp_ext_info_add_reg_table(mem_ctx, module, entry, table);
360 NT_STATUS_NOT_OK_RETURN(status);
362 if (!add_gp_extension_reg_info_entry_to_array(mem_ctx, entry,
363 &info->entries,
364 &info->num_entries)) {
365 return NT_STATUS_NO_MEMORY;
368 return NT_STATUS_OK;
371 /****************************************************************
372 ****************************************************************/
374 static bool gp_extension_reg_info_verify_entry(struct gp_extension_reg_entry *entry)
376 int i;
378 for (i=0; gpext_reg_vals[i].val; i++) {
380 if ((strequal(entry->value, gpext_reg_vals[i].val)) &&
381 (entry->data->type == gpext_reg_vals[i].type)) {
382 return true;
386 return false;
389 /****************************************************************
390 ****************************************************************/
392 static bool gp_extension_reg_info_verify(struct gp_extension_reg_info_entry *entry)
394 int i;
396 for (i=0; i < entry->num_entries; i++) {
397 if (!gp_extension_reg_info_verify_entry(&entry->entries[i])) {
398 return false;
402 return true;
405 /****************************************************************
406 ****************************************************************/
408 static WERROR gp_extension_store_reg_vals(TALLOC_CTX *mem_ctx,
409 struct registry_key *key,
410 struct gp_extension_reg_info_entry *entry)
412 WERROR werr = WERR_OK;
413 size_t i;
415 for (i=0; i < entry->num_entries; i++) {
417 werr = reg_setvalue(key,
418 entry->entries[i].value,
419 entry->entries[i].data);
420 W_ERROR_NOT_OK_RETURN(werr);
423 return werr;
426 /****************************************************************
427 ****************************************************************/
429 static WERROR gp_extension_store_reg_entry(TALLOC_CTX *mem_ctx,
430 struct gp_registry_context *reg_ctx,
431 struct gp_extension_reg_info_entry *entry)
433 WERROR werr;
434 struct registry_key *key = NULL;
435 const char *subkeyname = NULL;
437 if (!gp_extension_reg_info_verify(entry)) {
438 return WERR_INVALID_PARAM;
441 subkeyname = GUID_string2(mem_ctx, &entry->guid);
442 W_ERROR_HAVE_NO_MEMORY(subkeyname);
444 strupper_m(CONST_DISCARD(char *,subkeyname));
446 werr = gp_store_reg_subkey(mem_ctx,
447 subkeyname,
448 reg_ctx->curr_key,
449 &key);
450 W_ERROR_NOT_OK_RETURN(werr);
452 werr = gp_extension_store_reg_vals(mem_ctx,
453 key,
454 entry);
455 W_ERROR_NOT_OK_RETURN(werr);
457 return werr;
460 /****************************************************************
461 ****************************************************************/
463 static WERROR gp_extension_store_reg(TALLOC_CTX *mem_ctx,
464 struct gp_registry_context *reg_ctx,
465 struct gp_extension_reg_info *info)
467 WERROR werr = WERR_OK;
468 int i;
470 if (!info) {
471 return WERR_OK;
474 for (i=0; i < info->num_entries; i++) {
475 werr = gp_extension_store_reg_entry(mem_ctx,
476 reg_ctx,
477 &info->entries[i]);
478 W_ERROR_NOT_OK_RETURN(werr);
481 return werr;
484 /****************************************************************
485 ****************************************************************/
487 static NTSTATUS gp_glob_ext_list(TALLOC_CTX *mem_ctx,
488 const char ***ext_list,
489 size_t *ext_list_len)
491 SMB_STRUCT_DIR *dir = NULL;
492 SMB_STRUCT_DIRENT *dirent = NULL;
494 dir = sys_opendir(modules_path(SAMBA_SUBSYSTEM_GPEXT));
495 if (!dir) {
496 return map_nt_error_from_unix(errno);
499 while ((dirent = sys_readdir(dir))) {
501 fstring name; /* forgive me... */
502 char *p;
504 if ((strequal(dirent->d_name, ".")) ||
505 (strequal(dirent->d_name, ".."))) {
506 continue;
509 p = strrchr(dirent->d_name, '.');
510 if (!p) {
511 sys_closedir(dir);
512 return NT_STATUS_NO_MEMORY;
515 if (!strcsequal(p+1, shlib_ext())) {
516 DEBUG(10,("gp_glob_ext_list: not a *.so file: %s\n",
517 dirent->d_name));
518 continue;
521 fstrcpy(name, dirent->d_name);
522 name[PTR_DIFF(p, dirent->d_name)] = 0;
524 if (!add_string_to_array(mem_ctx, name, ext_list,
525 (int *)ext_list_len)) {
526 sys_closedir(dir);
527 return NT_STATUS_NO_MEMORY;
531 sys_closedir(dir);
533 return NT_STATUS_OK;
536 /****************************************************************
537 ****************************************************************/
539 NTSTATUS shutdown_gp_extensions(void)
541 struct gp_extension *ext = NULL;
543 for (ext = extensions; ext; ext = ext->next) {
544 if (ext->methods && ext->methods->shutdown) {
545 ext->methods->shutdown();
549 return NT_STATUS_OK;
552 /****************************************************************
553 ****************************************************************/
555 NTSTATUS init_gp_extensions(TALLOC_CTX *mem_ctx)
557 NTSTATUS status;
558 WERROR werr;
559 int i = 0;
560 const char **ext_array = NULL;
561 size_t ext_array_len = 0;
562 struct gp_extension *gpext = NULL;
563 struct gp_registry_context *reg_ctx = NULL;
565 if (get_gp_extension_list()) {
566 return NT_STATUS_OK;
569 status = gp_glob_ext_list(mem_ctx, &ext_array, &ext_array_len);
570 NT_STATUS_NOT_OK_RETURN(status);
572 for (i=0; i<ext_array_len; i++) {
574 struct gp_extension_reg_info *info = NULL;
576 status = gp_extension_init_module(mem_ctx, ext_array[i],
577 &gpext);
578 if (!NT_STATUS_IS_OK(status)) {
579 goto out;
582 if (gpext->methods->get_reg_config) {
584 status = gpext->methods->initialize(mem_ctx);
585 if (!NT_STATUS_IS_OK(status)) {
586 gpext->methods->shutdown();
587 goto out;
590 status = gpext->methods->get_reg_config(mem_ctx,
591 &info);
592 if (!NT_STATUS_IS_OK(status)) {
593 gpext->methods->shutdown();
594 goto out;
597 if (!reg_ctx) {
598 struct security_token *token;
600 token = registry_create_system_token(mem_ctx);
601 NT_STATUS_HAVE_NO_MEMORY(token);
603 werr = gp_init_reg_ctx(mem_ctx,
604 KEY_WINLOGON_GPEXT_PATH,
605 REG_KEY_WRITE,
606 token,
607 &reg_ctx);
608 if (!W_ERROR_IS_OK(werr)) {
609 status = werror_to_ntstatus(werr);
610 gpext->methods->shutdown();
611 goto out;
615 werr = gp_extension_store_reg(mem_ctx, reg_ctx, info);
616 if (!W_ERROR_IS_OK(werr)) {
617 DEBUG(1,("gp_extension_store_reg failed: %s\n",
618 win_errstr(werr)));
619 TALLOC_FREE(info);
620 gpext->methods->shutdown();
621 status = werror_to_ntstatus(werr);
622 goto out;
624 TALLOC_FREE(info);
629 out:
630 TALLOC_FREE(reg_ctx);
632 return status;
635 /****************************************************************
636 ****************************************************************/
638 NTSTATUS free_gp_extensions(void)
640 struct gp_extension *ext, *ext_next = NULL;
642 for (ext = extensions; ext; ext = ext_next) {
643 ext_next = ext->next;
644 DLIST_REMOVE(extensions, ext);
645 TALLOC_FREE(ext);
648 extensions = NULL;
650 return NT_STATUS_OK;
653 /****************************************************************
654 ****************************************************************/
656 void debug_gpext_header(int lvl,
657 const char *name,
658 uint32_t flags,
659 struct GROUP_POLICY_OBJECT *gpo,
660 const char *extension_guid,
661 const char *snapin_guid)
663 char *flags_str = NULL;
665 DEBUG(lvl,("%s\n", name));
666 DEBUGADD(lvl,("\tgpo: %s (%s)\n", gpo->name,
667 gpo->display_name));
668 DEBUGADD(lvl,("\tcse extension: %s (%s)\n", extension_guid,
669 cse_gpo_guid_string_to_name(extension_guid)));
670 DEBUGADD(lvl,("\tgplink: %s\n", gpo->link));
671 DEBUGADD(lvl,("\tsnapin: %s (%s)\n", snapin_guid,
672 cse_snapin_gpo_guid_string_to_name(snapin_guid)));
674 flags_str = gpo_flag_str(NULL, flags);
675 DEBUGADD(lvl,("\tflags: 0x%08x %s\n", flags, flags_str));
676 TALLOC_FREE(flags_str);
679 NTSTATUS process_gpo_list_with_extension(ADS_STRUCT *ads,
680 TALLOC_CTX *mem_ctx,
681 uint32_t flags,
682 const struct security_token *token,
683 struct GROUP_POLICY_OBJECT *gpo_list,
684 const char *extension_guid,
685 const char *snapin_guid)
687 return NT_STATUS_OK;
690 /****************************************************************
691 ****************************************************************/
693 NTSTATUS gpext_process_extension(ADS_STRUCT *ads,
694 TALLOC_CTX *mem_ctx,
695 uint32_t flags,
696 const struct security_token *token,
697 struct registry_key *root_key,
698 struct GROUP_POLICY_OBJECT *gpo,
699 const char *extension_guid,
700 const char *snapin_guid)
702 NTSTATUS status;
703 struct gp_extension *ext = NULL;
704 struct GUID guid;
705 bool cse_found = false;
707 status = init_gp_extensions(mem_ctx);
708 if (!NT_STATUS_IS_OK(status)) {
709 DEBUG(1,("init_gp_extensions failed: %s\n",
710 nt_errstr(status)));
711 return status;
714 status = GUID_from_string(extension_guid, &guid);
715 if (!NT_STATUS_IS_OK(status)) {
716 return status;
719 for (ext = extensions; ext; ext = ext->next) {
721 if (GUID_equal(ext->guid, &guid)) {
722 cse_found = true;
723 break;
727 if (!cse_found) {
728 goto no_ext;
731 status = ext->methods->initialize(mem_ctx);
732 NT_STATUS_NOT_OK_RETURN(status);
734 status = ext->methods->process_group_policy(ads,
735 mem_ctx,
736 flags,
737 root_key,
738 token,
739 gpo,
740 extension_guid,
741 snapin_guid);
742 if (!NT_STATUS_IS_OK(status)) {
743 ext->methods->shutdown();
746 return status;
748 no_ext:
749 if (flags & GPO_INFO_FLAG_VERBOSE) {
750 DEBUG(0,("process_extension: no extension available for:\n"));
751 DEBUGADD(0,("%s (%s) (snapin: %s)\n",
752 extension_guid,
753 cse_gpo_guid_string_to_name(extension_guid),
754 snapin_guid));
757 return NT_STATUS_OK;