source: vendor/current/libgpo/gpext/gpext.c@ 740

Last change on this file since 740 was 740, checked in by Silvan Scherrer, 12 years ago

Samba Server: update vendor to 3.6.0

File size: 19.6 KB
Line 
1/*
2 * Unix SMB/CIFS implementation.
3 * Group Policy Support
4 * Copyright (C) Guenther Deschner 2007-2008
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
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#include "../libcli/registry/util_reg.h"
26#if _SAMBA_BUILD_ == 3
27#include "libgpo/gpo_proto.h"
28#include "registry.h"
29#include "registry/reg_api.h"
30#endif
31
32static struct gp_extension *extensions = NULL;
33
34/****************************************************************
35****************************************************************/
36
37struct gp_extension *get_gp_extension_list(void)
38{
39 return extensions;
40}
41
42/****************************************************************
43****************************************************************/
44
45/* see http://support.microsoft.com/kb/216358/en-us/ for more info */
46
47struct gp_extension_reg_table gpext_reg_vals[] = {
48 { "DllName", REG_EXPAND_SZ },
49 { "ProcessGroupPolicy", REG_SZ },
50 { "NoMachinePolicy", REG_DWORD },
51 { "NoUserPolicy", REG_DWORD },
52 { "NoSlowLink", REG_DWORD },
53 { "NoBackgroundPolicy", REG_DWORD },
54 { "NoGPOListChanges", REG_DWORD },
55 { "PerUserLocalSettings", REG_DWORD },
56 { "RequiresSuccessfulRegistry", REG_DWORD },
57 { "EnableAsynchronousProcessing", REG_DWORD },
58 { "ExtensionDebugLevel", REG_DWORD },
59 /* new */
60 { "GenerateGroupPolicy", REG_SZ }, /* not supported on w2k */
61 { "NotifyLinkTransition", REG_DWORD },
62 { "ProcessGroupPolicyEx", REG_SZ }, /* not supported on w2k */
63 { "ExtensionEventSource", REG_MULTI_SZ }, /* not supported on w2k */
64 { "GenerateGroupPolicy", REG_SZ },
65 { "MaxNoGPOListChangesInterval", REG_DWORD },
66 { NULL, REG_NONE }
67};
68
69/****************************************************************
70****************************************************************/
71
72static struct gp_extension *get_extension_by_name(struct gp_extension *be,
73 const char *name)
74{
75 struct gp_extension *b;
76
77 for (b = be; b; b = b->next) {
78 if (strequal(b->name, name)) {
79 return b;
80 }
81 }
82
83 return NULL;
84}
85
86/****************************************************************
87****************************************************************/
88
89static struct gp_extension_methods *get_methods_by_name(struct gp_extension *be,
90 const char *name)
91{
92 struct gp_extension *b;
93
94 for (b = be; b; b = b->next) {
95 if (strequal(b->name, name)) {
96 return b->methods;
97 }
98 }
99
100 return NULL;
101}
102
103/****************************************************************
104****************************************************************/
105
106NTSTATUS unregister_gp_extension(const char *name)
107{
108 struct gp_extension *ext;
109
110 ext = get_extension_by_name(extensions, name);
111 if (!ext) {
112 return NT_STATUS_OK;
113 }
114
115 DLIST_REMOVE(extensions, ext);
116 talloc_free(ext);
117
118 DEBUG(2,("Successfully removed GP extension '%s'\n", name));
119
120 return NT_STATUS_OK;
121}
122
123/****************************************************************
124****************************************************************/
125
126NTSTATUS register_gp_extension(TALLOC_CTX *gpext_ctx,
127 int version,
128 const char *name,
129 const char *guid,
130 struct gp_extension_methods *methods)
131{
132 struct gp_extension_methods *test;
133 struct gp_extension *entry;
134 NTSTATUS status;
135
136 if (!gpext_ctx) {
137 return NT_STATUS_INTERNAL_DB_ERROR;
138 }
139
140 if ((version != SMB_GPEXT_INTERFACE_VERSION)) {
141 DEBUG(0,("Failed to register gp extension.\n"
142 "The module was compiled against "
143 "SMB_GPEXT_INTERFACE_VERSION %d,\n"
144 "current SMB_GPEXT_INTERFACE_VERSION is %d.\n"
145 "Please recompile against the current "
146 "version of samba!\n",
147 version, SMB_GPEXT_INTERFACE_VERSION));
148 return NT_STATUS_OBJECT_TYPE_MISMATCH;
149 }
150
151 if (!guid || !name || !name[0] || !methods) {
152 DEBUG(0,("Called with NULL pointer or empty name!\n"));
153 return NT_STATUS_INVALID_PARAMETER;
154 }
155
156 test = get_methods_by_name(extensions, name);
157 if (test) {
158 DEBUG(0,("GP extension module %s already registered!\n",
159 name));
160 return NT_STATUS_OBJECT_NAME_COLLISION;
161 }
162
163 entry = talloc_zero(gpext_ctx, struct gp_extension);
164 NT_STATUS_HAVE_NO_MEMORY(entry);
165
166 entry->name = talloc_strdup(gpext_ctx, name);
167 NT_STATUS_HAVE_NO_MEMORY(entry->name);
168
169 entry->guid = talloc_zero(gpext_ctx, struct GUID);
170 NT_STATUS_HAVE_NO_MEMORY(entry->guid);
171 status = GUID_from_string(guid, entry->guid);
172 NT_STATUS_NOT_OK_RETURN(status);
173
174 entry->methods = methods;
175 DLIST_ADD(extensions, entry);
176
177 DEBUG(2,("Successfully added GP extension '%s' %s\n",
178 name, GUID_string2(gpext_ctx, entry->guid)));
179
180 return NT_STATUS_OK;
181}
182
183/****************************************************************
184****************************************************************/
185
186static NTSTATUS gp_extension_init_module(TALLOC_CTX *mem_ctx,
187 const char *name,
188 struct gp_extension **gpext)
189{
190 NTSTATUS status;
191 struct gp_extension *ext = NULL;
192
193 ext = talloc_zero(mem_ctx, struct gp_extension);
194 NT_STATUS_HAVE_NO_MEMORY(gpext);
195
196 ext->methods = get_methods_by_name(extensions, name);
197 if (!ext->methods) {
198
199 status = smb_probe_module(SAMBA_SUBSYSTEM_GPEXT,
200 name);
201 if (!NT_STATUS_IS_OK(status)) {
202 return status;
203 }
204
205 ext->methods = get_methods_by_name(extensions, name);
206 if (!ext->methods) {
207 return NT_STATUS_DLL_INIT_FAILED;
208 }
209 }
210
211 *gpext = ext;
212
213 return NT_STATUS_OK;
214}
215
216/****************************************************************
217****************************************************************/
218
219static bool add_gp_extension_reg_entry_to_array(TALLOC_CTX *mem_ctx,
220 struct gp_extension_reg_entry *entry,
221 struct gp_extension_reg_entry **entries,
222 size_t *num)
223{
224 *entries = talloc_realloc(mem_ctx, *entries,
225 struct gp_extension_reg_entry,
226 (*num)+1);
227 if (*entries == NULL) {
228 *num = 0;
229 return false;
230 }
231
232 (*entries)[*num].value = entry->value;
233 (*entries)[*num].data = entry->data;
234
235 *num += 1;
236 return true;
237}
238
239/****************************************************************
240****************************************************************/
241
242static bool add_gp_extension_reg_info_entry_to_array(TALLOC_CTX *mem_ctx,
243 struct gp_extension_reg_info_entry *entry,
244 struct gp_extension_reg_info_entry **entries,
245 size_t *num)
246{
247 *entries = talloc_realloc(mem_ctx, *entries,
248 struct gp_extension_reg_info_entry,
249 (*num)+1);
250 if (*entries == NULL) {
251 *num = 0;
252 return false;
253 }
254
255 (*entries)[*num].guid = entry->guid;
256 (*entries)[*num].num_entries = entry->num_entries;
257 (*entries)[*num].entries = entry->entries;
258
259 *num += 1;
260 return true;
261}
262
263/****************************************************************
264****************************************************************/
265
266static NTSTATUS gp_ext_info_add_reg(TALLOC_CTX *mem_ctx,
267 struct gp_extension_reg_info_entry *entry,
268 const char *value,
269 enum winreg_Type type,
270 const char *data_s)
271{
272 struct gp_extension_reg_entry *reg_entry = NULL;
273 struct registry_value *data = NULL;
274
275 reg_entry = talloc_zero(mem_ctx, struct gp_extension_reg_entry);
276 NT_STATUS_HAVE_NO_MEMORY(reg_entry);
277
278 data = talloc_zero(mem_ctx, struct registry_value);
279 NT_STATUS_HAVE_NO_MEMORY(data);
280
281 data->type = type;
282
283 switch (type) {
284 case REG_SZ:
285 case REG_EXPAND_SZ:
286 if (!push_reg_sz(mem_ctx, &data->data, data_s)) {
287 return NT_STATUS_NO_MEMORY;
288 }
289 break;
290 case REG_DWORD: {
291 uint32_t v = atoi(data_s);
292 data->data = data_blob_talloc(mem_ctx, NULL, 4);
293 SIVAL(data->data.data, 0, v);
294 break;
295 }
296 default:
297 return NT_STATUS_NOT_SUPPORTED;
298 }
299
300 reg_entry->value = value;
301 reg_entry->data = data;
302
303 if (!add_gp_extension_reg_entry_to_array(mem_ctx, reg_entry,
304 &entry->entries,
305 &entry->num_entries)) {
306 return NT_STATUS_NO_MEMORY;
307 }
308
309 return NT_STATUS_OK;
310}
311
312/****************************************************************
313****************************************************************/
314
315static NTSTATUS gp_ext_info_add_reg_table(TALLOC_CTX *mem_ctx,
316 const char *module,
317 struct gp_extension_reg_info_entry *entry,
318 struct gp_extension_reg_table *table)
319{
320 NTSTATUS status;
321 const char *module_name = NULL;
322 int i;
323
324 module_name = talloc_asprintf(mem_ctx, "%s.%s", module, shlib_ext());
325 NT_STATUS_HAVE_NO_MEMORY(module_name);
326
327 status = gp_ext_info_add_reg(mem_ctx, entry,
328 "DllName", REG_EXPAND_SZ, module_name);
329 NT_STATUS_NOT_OK_RETURN(status);
330
331 for (i=0; table[i].val; i++) {
332 status = gp_ext_info_add_reg(mem_ctx, entry,
333 table[i].val,
334 table[i].type,
335 table[i].data);
336 NT_STATUS_NOT_OK_RETURN(status);
337 }
338
339 return status;
340}
341
342/****************************************************************
343****************************************************************/
344
345NTSTATUS gp_ext_info_add_entry(TALLOC_CTX *mem_ctx,
346 const char *module,
347 const char *ext_guid,
348 struct gp_extension_reg_table *table,
349 struct gp_extension_reg_info *info)
350{
351 NTSTATUS status;
352 struct gp_extension_reg_info_entry *entry = NULL;
353
354 entry = TALLOC_ZERO_P(mem_ctx, struct gp_extension_reg_info_entry);
355 NT_STATUS_HAVE_NO_MEMORY(entry);
356
357 status = GUID_from_string(ext_guid, &entry->guid);
358 NT_STATUS_NOT_OK_RETURN(status);
359
360 status = gp_ext_info_add_reg_table(mem_ctx, module, entry, table);
361 NT_STATUS_NOT_OK_RETURN(status);
362
363 if (!add_gp_extension_reg_info_entry_to_array(mem_ctx, entry,
364 &info->entries,
365 &info->num_entries)) {
366 return NT_STATUS_NO_MEMORY;
367 }
368
369 return NT_STATUS_OK;
370}
371
372/****************************************************************
373****************************************************************/
374
375static bool gp_extension_reg_info_verify_entry(struct gp_extension_reg_entry *entry)
376{
377 int i;
378
379 for (i=0; gpext_reg_vals[i].val; i++) {
380
381 if ((strequal(entry->value, gpext_reg_vals[i].val)) &&
382 (entry->data->type == gpext_reg_vals[i].type)) {
383 return true;
384 }
385 }
386
387 return false;
388}
389
390/****************************************************************
391****************************************************************/
392
393static bool gp_extension_reg_info_verify(struct gp_extension_reg_info_entry *entry)
394{
395 int i;
396
397 for (i=0; i < entry->num_entries; i++) {
398 if (!gp_extension_reg_info_verify_entry(&entry->entries[i])) {
399 return false;
400 }
401 }
402
403 return true;
404}
405
406/****************************************************************
407****************************************************************/
408
409static WERROR gp_extension_store_reg_vals(TALLOC_CTX *mem_ctx,
410 struct registry_key *key,
411 struct gp_extension_reg_info_entry *entry)
412{
413 WERROR werr = WERR_OK;
414 size_t i;
415
416 for (i=0; i < entry->num_entries; i++) {
417
418 werr = reg_setvalue(key,
419 entry->entries[i].value,
420 entry->entries[i].data);
421 W_ERROR_NOT_OK_RETURN(werr);
422 }
423
424 return werr;
425}
426
427/****************************************************************
428****************************************************************/
429
430static WERROR gp_extension_store_reg_entry(TALLOC_CTX *mem_ctx,
431 struct gp_registry_context *reg_ctx,
432 struct gp_extension_reg_info_entry *entry)
433{
434 WERROR werr;
435 struct registry_key *key = NULL;
436 const char *subkeyname = NULL;
437
438 if (!gp_extension_reg_info_verify(entry)) {
439 return WERR_INVALID_PARAM;
440 }
441
442 subkeyname = GUID_string2(mem_ctx, &entry->guid);
443 W_ERROR_HAVE_NO_MEMORY(subkeyname);
444
445 strupper_m(CONST_DISCARD(char *,subkeyname));
446
447 werr = gp_store_reg_subkey(mem_ctx,
448 subkeyname,
449 reg_ctx->curr_key,
450 &key);
451 W_ERROR_NOT_OK_RETURN(werr);
452
453 werr = gp_extension_store_reg_vals(mem_ctx,
454 key,
455 entry);
456 W_ERROR_NOT_OK_RETURN(werr);
457
458 return werr;
459}
460
461/****************************************************************
462****************************************************************/
463
464static WERROR gp_extension_store_reg(TALLOC_CTX *mem_ctx,
465 struct gp_registry_context *reg_ctx,
466 struct gp_extension_reg_info *info)
467{
468 WERROR werr = WERR_OK;
469 int i;
470
471 if (!info) {
472 return WERR_OK;
473 }
474
475 for (i=0; i < info->num_entries; i++) {
476 werr = gp_extension_store_reg_entry(mem_ctx,
477 reg_ctx,
478 &info->entries[i]);
479 W_ERROR_NOT_OK_RETURN(werr);
480 }
481
482 return werr;
483}
484
485/****************************************************************
486****************************************************************/
487
488static NTSTATUS gp_glob_ext_list(TALLOC_CTX *mem_ctx,
489 const char ***ext_list,
490 size_t *ext_list_len)
491{
492 SMB_STRUCT_DIR *dir = NULL;
493 SMB_STRUCT_DIRENT *dirent = NULL;
494
495 dir = sys_opendir(modules_path(SAMBA_SUBSYSTEM_GPEXT));
496 if (!dir) {
497 return map_nt_error_from_unix(errno);
498 }
499
500 while ((dirent = sys_readdir(dir))) {
501
502 fstring name; /* forgive me... */
503 char *p;
504
505 if ((strequal(dirent->d_name, ".")) ||
506 (strequal(dirent->d_name, ".."))) {
507 continue;
508 }
509
510 p = strrchr(dirent->d_name, '.');
511 if (!p) {
512 sys_closedir(dir);
513 return NT_STATUS_NO_MEMORY;
514 }
515
516 if (!strcsequal(p+1, shlib_ext())) {
517 DEBUG(10,("gp_glob_ext_list: not a *.so file: %s\n",
518 dirent->d_name));
519 continue;
520 }
521
522 fstrcpy(name, dirent->d_name);
523 name[PTR_DIFF(p, dirent->d_name)] = 0;
524
525 if (!add_string_to_array(mem_ctx, name, ext_list,
526 (int *)ext_list_len)) {
527 sys_closedir(dir);
528 return NT_STATUS_NO_MEMORY;
529 }
530 }
531
532 sys_closedir(dir);
533
534 return NT_STATUS_OK;
535}
536
537/****************************************************************
538****************************************************************/
539
540NTSTATUS shutdown_gp_extensions(void)
541{
542 struct gp_extension *ext = NULL;
543
544 for (ext = extensions; ext; ext = ext->next) {
545 if (ext->methods && ext->methods->shutdown) {
546 ext->methods->shutdown();
547 }
548 }
549
550 return NT_STATUS_OK;
551}
552
553/****************************************************************
554****************************************************************/
555
556NTSTATUS init_gp_extensions(TALLOC_CTX *mem_ctx)
557{
558 NTSTATUS status;
559 WERROR werr;
560 int i = 0;
561 const char **ext_array = NULL;
562 size_t ext_array_len = 0;
563 struct gp_extension *gpext = NULL;
564 struct gp_registry_context *reg_ctx = NULL;
565
566 if (get_gp_extension_list()) {
567 return NT_STATUS_OK;
568 }
569
570 status = gp_glob_ext_list(mem_ctx, &ext_array, &ext_array_len);
571 NT_STATUS_NOT_OK_RETURN(status);
572
573 for (i=0; i<ext_array_len; i++) {
574
575 struct gp_extension_reg_info *info = NULL;
576
577 status = gp_extension_init_module(mem_ctx, ext_array[i],
578 &gpext);
579 if (!NT_STATUS_IS_OK(status)) {
580 goto out;
581 }
582
583 if (gpext->methods->get_reg_config) {
584
585 status = gpext->methods->initialize(mem_ctx);
586 if (!NT_STATUS_IS_OK(status)) {
587 gpext->methods->shutdown();
588 goto out;
589 }
590
591 status = gpext->methods->get_reg_config(mem_ctx,
592 &info);
593 if (!NT_STATUS_IS_OK(status)) {
594 gpext->methods->shutdown();
595 goto out;
596 }
597
598 if (!reg_ctx) {
599 struct security_token *token;
600
601 token = registry_create_system_token(mem_ctx);
602 NT_STATUS_HAVE_NO_MEMORY(token);
603
604 werr = gp_init_reg_ctx(mem_ctx,
605 KEY_WINLOGON_GPEXT_PATH,
606 REG_KEY_WRITE,
607 token,
608 &reg_ctx);
609 if (!W_ERROR_IS_OK(werr)) {
610 status = werror_to_ntstatus(werr);
611 gpext->methods->shutdown();
612 goto out;
613 }
614 }
615
616 werr = gp_extension_store_reg(mem_ctx, reg_ctx, info);
617 if (!W_ERROR_IS_OK(werr)) {
618 DEBUG(1,("gp_extension_store_reg failed: %s\n",
619 win_errstr(werr)));
620 TALLOC_FREE(info);
621 gpext->methods->shutdown();
622 status = werror_to_ntstatus(werr);
623 goto out;
624 }
625 TALLOC_FREE(info);
626 }
627
628 }
629
630 out:
631 TALLOC_FREE(reg_ctx);
632
633 return status;
634}
635
636/****************************************************************
637****************************************************************/
638
639NTSTATUS free_gp_extensions(void)
640{
641 struct gp_extension *ext, *ext_next = NULL;
642
643 for (ext = extensions; ext; ext = ext_next) {
644 ext_next = ext->next;
645 DLIST_REMOVE(extensions, ext);
646 TALLOC_FREE(ext);
647 }
648
649 extensions = NULL;
650
651 return NT_STATUS_OK;
652}
653
654/****************************************************************
655****************************************************************/
656
657void debug_gpext_header(int lvl,
658 const char *name,
659 uint32_t flags,
660 struct GROUP_POLICY_OBJECT *gpo,
661 const char *extension_guid,
662 const char *snapin_guid)
663{
664 char *flags_str = NULL;
665
666 DEBUG(lvl,("%s\n", name));
667 DEBUGADD(lvl,("\tgpo: %s (%s)\n", gpo->name,
668 gpo->display_name));
669 DEBUGADD(lvl,("\tcse extension: %s (%s)\n", extension_guid,
670 cse_gpo_guid_string_to_name(extension_guid)));
671 DEBUGADD(lvl,("\tgplink: %s\n", gpo->link));
672 DEBUGADD(lvl,("\tsnapin: %s (%s)\n", snapin_guid,
673 cse_snapin_gpo_guid_string_to_name(snapin_guid)));
674
675 flags_str = gpo_flag_str(NULL, flags);
676 DEBUGADD(lvl,("\tflags: 0x%08x %s\n", flags, flags_str));
677 TALLOC_FREE(flags_str);
678}
679
680NTSTATUS process_gpo_list_with_extension(ADS_STRUCT *ads,
681 TALLOC_CTX *mem_ctx,
682 uint32_t flags,
683 const struct security_token *token,
684 struct GROUP_POLICY_OBJECT *gpo_list,
685 const char *extension_guid,
686 const char *snapin_guid)
687{
688 return NT_STATUS_OK;
689}
690
691/****************************************************************
692****************************************************************/
693
694NTSTATUS gpext_process_extension(ADS_STRUCT *ads,
695 TALLOC_CTX *mem_ctx,
696 uint32_t flags,
697 const struct security_token *token,
698 struct registry_key *root_key,
699 struct GROUP_POLICY_OBJECT *gpo,
700 const char *extension_guid,
701 const char *snapin_guid)
702{
703 NTSTATUS status;
704 struct gp_extension *ext = NULL;
705 struct GUID guid;
706 bool cse_found = false;
707
708 status = init_gp_extensions(mem_ctx);
709 if (!NT_STATUS_IS_OK(status)) {
710 DEBUG(1,("init_gp_extensions failed: %s\n",
711 nt_errstr(status)));
712 return status;
713 }
714
715 status = GUID_from_string(extension_guid, &guid);
716 if (!NT_STATUS_IS_OK(status)) {
717 return status;
718 }
719
720 for (ext = extensions; ext; ext = ext->next) {
721
722 if (GUID_equal(ext->guid, &guid)) {
723 cse_found = true;
724 break;
725 }
726 }
727
728 if (!cse_found) {
729 goto no_ext;
730 }
731
732 status = ext->methods->initialize(mem_ctx);
733 NT_STATUS_NOT_OK_RETURN(status);
734
735 status = ext->methods->process_group_policy(ads,
736 mem_ctx,
737 flags,
738 root_key,
739 token,
740 gpo,
741 extension_guid,
742 snapin_guid);
743 if (!NT_STATUS_IS_OK(status)) {
744 ext->methods->shutdown();
745 }
746
747 return status;
748
749 no_ext:
750 if (flags & GPO_INFO_FLAG_VERBOSE) {
751 DEBUG(0,("process_extension: no extension available for:\n"));
752 DEBUGADD(0,("%s (%s) (snapin: %s)\n",
753 extension_guid,
754 cse_gpo_guid_string_to_name(extension_guid),
755 snapin_guid));
756 }
757
758 return NT_STATUS_OK;
759}
Note: See TracBrowser for help on using the repository browser.