diff options
author | David Mulder <dmulder@samba.org> | 2022-11-29 08:04:35 -0700 |
---|---|---|
committer | Jeremy Allison <jra@samba.org> | 2022-12-21 02:04:36 +0000 |
commit | 64f4930dc0d46c21fefd8a798534b58670036faa (patch) | |
tree | 1ff3b4958170fc8e9c800e1d441dd9211623b615 /python | |
parent | 2953329ba07cb7de6c8df1718779b7c9045d3910 (diff) | |
download | samba-64f4930dc0d46c21fefd8a798534b58670036faa.tar.gz |
gp: Modify Files CSE to use new files applier
Signed-off-by: David Mulder <dmulder@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
Diffstat (limited to 'python')
-rw-r--r-- | python/samba/gp/vgp_files_ext.py | 54 |
1 files changed, 28 insertions, 26 deletions
diff --git a/python/samba/gp/vgp_files_ext.py b/python/samba/gp/vgp_files_ext.py index bd26212793e..947e13c2d25 100644 --- a/python/samba/gp/vgp_files_ext.py +++ b/python/samba/gp/vgp_files_ext.py @@ -15,10 +15,9 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. import os, pwd, grp -from samba.gp.gpclass import gp_xml_ext, check_safe_path +from samba.gp.gpclass import gp_xml_ext, check_safe_path, gp_file_applier from tempfile import NamedTemporaryFile from shutil import copyfile, move -from hashlib import blake2b from samba.gp.util.logging import log def calc_mode(entry): @@ -59,19 +58,19 @@ def stat_from_mode(mode): stat += '-' return stat -class vgp_files_ext(gp_xml_ext): +def source_file_change(fname): + if os.path.exists(fname): + return b'%d' % os.stat(fname).st_ctime + +class vgp_files_ext(gp_xml_ext, gp_file_applier): def __str__(self): return 'VGP/Unix Settings/Files' def process_group_policy(self, deleted_gpo_list, changed_gpo_list): for guid, settings in deleted_gpo_list: - self.gp_db.set_guid(guid) if str(self) in settings: for attribute, _ in settings[str(self)].items(): - if os.path.exists(attribute): - os.unlink(attribute) - self.gp_db.delete(str(self), attribute) - self.gp_db.commit() + self.unapply(guid, attribute, attribute) for gpo in changed_gpo_list: if gpo.file_sys_path: @@ -92,28 +91,31 @@ class vgp_files_ext(gp_xml_ext): if not os.path.exists(source_file): log.warn('Source file does not exist', source_file) continue - source_hash = \ - blake2b(open(source_file, 'rb').read()).hexdigest() target = entry.find('target').text user = entry.find('user').text group = entry.find('group').text mode = calc_mode(entry) - value = '%s:%s:%s:%d' % (source_hash, user, group, mode) - old_val = self.gp_db.retrieve(str(self), target) - if old_val == value: - continue - if os.path.exists(target): - log.warn('Target file already exists', target) - continue - with NamedTemporaryFile(dir=os.path.dirname(target), - delete=False) as f: - copyfile(source_file, f.name) - os.chown(f.name, pwd.getpwnam(user).pw_uid, - grp.getgrnam(group).gr_gid) - os.chmod(f.name, mode) - move(f.name, target) - self.gp_db.store(str(self), target, value) - self.gp_db.commit() + + # The attribute is simply the target file. + attribute = target + # The value hash is generated from the source file last + # change stamp, the user, the group, and the mode, ensuring + # any changes to this GPO will cause the file to be + # rewritten. + value_hash = self.generate_value_hash( + source_file_change(source_file), + user, group, b'%d' % mode) + def applier_func(source_file, target, user, group, mode): + with NamedTemporaryFile(dir=os.path.dirname(target), + delete=False) as f: + copyfile(source_file, f.name) + os.chown(f.name, pwd.getpwnam(user).pw_uid, + grp.getgrnam(group).gr_gid) + os.chmod(f.name, mode) + move(f.name, target) + return [target] + self.apply(gpo.name, attribute, value_hash, applier_func, + source_file, target, user, group, mode) def rsop(self, gpo): output = {} |