python: Fix NtVer check for site_dn_for_machine()
[samba.git] / python / samba / gp_smb_conf_ext.py
1 # gp_smb_conf_ext smb.conf gpo policy
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 import os, re, numbers
18 from samba.gpclass import gp_pol_ext
19 from tempfile import NamedTemporaryFile
20
21 def is_number(x):
22     return isinstance(x, numbers.Number) and \
23            type(x) != bool
24
25 class gp_smb_conf_ext(gp_pol_ext):
26     def process_group_policy(self, deleted_gpo_list, changed_gpo_list):
27
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')
32             if smb_conf is None:
33                 continue
34             for key, value in smb_conf.items():
35                 self.set_smb_conf(key, value)
36                 self.gp_db.delete('smb.conf', key)
37                 self.gp_db.commit()
38
39         for gpo in changed_gpo_list:
40             if gpo.file_sys_path:
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)
45                 if not pol_conf:
46                     continue
47                 for e in pol_conf.entries:
48                     if not e.keyname.startswith(section_name):
49                         continue
50                     self.set_smb_conf(e.valuename, e.data)
51                     self.gp_db.commit()
52
53     def set_smb_conf(self, attribute, val):
54         old_val = self.lp.get(attribute)
55
56         if type(val) == bytes:
57             val = val.decode()
58         if is_number(val) and is_number(old_val):
59             val = str(val)
60         elif is_number(val) and type(old_val) == bool:
61             val = bool(val)
62         if type(val) == bool:
63             val = 'yes' if val else 'no'
64
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)
72
73         self.logger.info('smb.conf [global] %s was changed from %s to %s' % \
74                          (attribute, old_val, str(val)))
75
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)
83
84     def __str__(self):
85         return "smb.conf"
86
87     def rsop(self, gpo):
88         output = {}
89         if gpo.file_sys_path:
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)
94             if not pol_conf:
95                 return output
96             for e in pol_conf.entries:
97                 if not e.keyname.startswith(section_name):
98                     continue
99                 if 'smb.conf' not in output.keys():
100                     output['smb.conf'] = {}
101                 output['smb.conf'][e.valuename] = e.data
102         return output