1 # gp_smb_conf_ext smb.conf gpo policy
2 # Copyright (C) David Mulder <dmulder@suse.com> 2018
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.
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.
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/>.
17 import os, re, numbers
18 from samba.gpclass import gp_pol_ext
19 from tempfile import NamedTemporaryFile
22 return isinstance(x, numbers.Number) and \
25 class gp_smb_conf_ext(gp_pol_ext):
26 def process_group_policy(self, deleted_gpo_list, changed_gpo_list):
28 pol_file = 'MACHINE/Registry.pol'
29 for guid, settings in deleted_gpo_list:
30 self.gp_db.set_guid(guid)
31 smb_conf = settings.get('smb.conf')
34 for key, value in smb_conf.items():
35 self.set_smb_conf(key, value)
36 self.gp_db.delete('smb.conf', key)
39 for gpo in changed_gpo_list:
41 section_name = 'Software\\Policies\\Samba\\smb_conf'
42 self.gp_db.set_guid(gpo.name)
43 path = os.path.join(gpo.file_sys_path, pol_file)
44 pol_conf = self.parse(path)
47 for e in pol_conf.entries:
48 if not e.keyname.startswith(section_name):
50 self.set_smb_conf(e.valuename, e.data)
53 def set_smb_conf(self, attribute, val):
54 old_val = self.lp.get(attribute)
56 if type(val) == bytes:
58 if is_number(val) and is_number(old_val):
60 elif is_number(val) and type(old_val) == bool:
63 val = 'yes' if val else 'no'
65 self.lp.set(attribute, val)
66 with NamedTemporaryFile(delete=False,
67 dir=os.path.dirname(self.lp.configfile)) as f:
68 self.lp.dump(False, f.name)
69 mode = os.stat(self.lp.configfile).st_mode
70 os.chmod(f.name, mode)
71 os.rename(f.name, self.lp.configfile)
73 self.logger.info('smb.conf [global] %s was changed from %s to %s' % \
74 (attribute, old_val, str(val)))
76 if is_number(old_val):
77 old_val = str(old_val)
78 elif type(old_val) == bool:
79 old_val = 'yes' if old_val else 'no'
80 elif type(old_val) == list:
81 old_val = ' '.join(old_val)
82 self.gp_db.store(str(self), attribute, old_val)
90 section_name = 'Software\\Policies\\Samba\\smb_conf'
91 pol_file = 'MACHINE/Registry.pol'
92 path = os.path.join(gpo.file_sys_path, pol_file)
93 pol_conf = self.parse(path)
96 for e in pol_conf.entries:
97 if not e.keyname.startswith(section_name):
99 if 'smb.conf' not in output.keys():
100 output['smb.conf'] = {}
101 output['smb.conf'][e.valuename] = e.data