tests samr: Extra tests for samr_QueryDisplayInfo
[amitay/samba.git] / python / samba / gp_ext_loader.py
1 # Group Policy Client Side Extension Loader
2 # Copyright (C) David Mulder <dmulder@suse.com> 2018
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 from samba.gpclass import list_gp_extensions
18 from samba.gpclass import gp_ext
19
20 try:
21     import importlib.util
22
23     def import_file(name, location):
24         spec = importlib.util.spec_from_file_location(name, location)
25         module = importlib.util.module_from_spec(spec)
26         spec.loader.exec_module(module)
27         return module
28 except ImportError:
29     import imp
30
31     def import_file(name, location):
32         return imp.load_source(name, location)
33
34
35 def get_gp_ext_from_module(name, mod):
36     if mod:
37         for k, v in vars(mod).items():
38             if k == name and issubclass(v, gp_ext):
39                 return v
40     return None
41
42
43 def get_gp_client_side_extensions(logger, smb_conf):
44     user_exts = []
45     machine_exts = []
46     gp_exts = list_gp_extensions(smb_conf)
47     for gp_ext in gp_exts.values():
48         module = import_file(gp_ext['ProcessGroupPolicy'], gp_ext['DllName'])
49         ext = get_gp_ext_from_module(gp_ext['ProcessGroupPolicy'], module)
50         if ext and gp_ext['MachinePolicy']:
51             machine_exts.append(ext)
52             logger.info('Loaded machine extension from %s: %s'
53                         % (gp_ext['DllName'], ext.__name__))
54         if ext and gp_ext['UserPolicy']:
55             user_exts.append(ext)
56             logger.info('Loaded user extension from %s: %s'
57                         % (gp_ext['DllName'], ext.__name__))
58     return (machine_exts, user_exts)