From: Joseph Sutton Date: Tue, 29 Aug 2023 04:29:55 +0000 (+1200) Subject: gp: Fix resource leaks X-Git-Tag: tevent-0.16.0~764 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=34042677b7da79936ed0278a80ec34ee9b93de3e;p=thirdparty%2Fsamba.git gp: Fix resource leaks Signed-off-by: Joseph Sutton Reviewed-by: Andrew Bartlett --- diff --git a/python/samba/gp/gp_gnome_settings_ext.py b/python/samba/gp/gp_gnome_settings_ext.py index ee0e756e8e2..567ab941533 100644 --- a/python/samba/gp/gp_gnome_settings_ext.py +++ b/python/samba/gp/gp_gnome_settings_ext.py @@ -282,7 +282,8 @@ class gp_gnome_settings_ext(gp_pol_ext, gp_file_applier): os.makedirs(os.path.dirname(udisk2_etc), exist_ok=True) xml_data = etree.ElementTree(etree.Element('policyconfig')) if os.path.exists(udisk2): - data = open(udisk2, 'rb').read() + with open(udisk2, 'rb') as f: + data = f.read() existing_xml = etree.ElementTree(etree.fromstring(data)) root = xml_data.getroot() root.append(existing_xml.find('vendor')) diff --git a/python/samba/gp/gp_msgs_ext.py b/python/samba/gp/gp_msgs_ext.py index 1c9135a074b..9aadddfc713 100644 --- a/python/samba/gp/gp_msgs_ext.py +++ b/python/samba/gp/gp_msgs_ext.py @@ -23,7 +23,11 @@ class gp_msgs_ext(gp_pol_ext, gp_misc_applier): raise ValueError('"%s" is not a message attribute' % attribute) data = self.parse_value(value) mfile = os.path.join(cdir, attribute) - current = open(mfile, 'r').read() if os.path.exists(mfile) else '' + if os.path.exists(mfile): + with open(mfile, 'r') as f: + current = f.read() + else: + current = '' # Only overwrite the msg if it hasn't been modified. It may have been # modified by another GPO. if 'new_val' not in data or current.strip() == data['new_val'].strip(): @@ -44,7 +48,8 @@ class gp_msgs_ext(gp_pol_ext, gp_misc_applier): e.valuename) mfile = os.path.join(cdir, e.valuename) if os.path.exists(mfile): - old_val = open(mfile, 'r').read() + with open(mfile, 'r') as f: + old_val = f.read() else: old_val = '' # If policy is already applied, skip application diff --git a/python/samba/gp/gp_sudoers_ext.py b/python/samba/gp/gp_sudoers_ext.py index 1990962144f..026aeba547f 100644 --- a/python/samba/gp/gp_sudoers_ext.py +++ b/python/samba/gp/gp_sudoers_ext.py @@ -51,9 +51,9 @@ def sudo_applier_func(sudo_dir, sudo_entries): w.write(contents) if visudo is None: raise FileNotFoundError('visudo not found, please install it') - sudo_validation = \ - Popen([visudo, '-c', '-f', f.name], - stdout=PIPE, stderr=PIPE).wait() + with Popen([visudo, '-c', '-f', f.name], + stdout=PIPE, stderr=PIPE) as proc: + sudo_validation = proc.wait() if sudo_validation == 0: with NamedTemporaryFile(prefix='gp_', delete=False, diff --git a/python/samba/gp/gpclass.py b/python/samba/gp/gpclass.py index 437f9961f3a..3b6289fe0c0 100644 --- a/python/samba/gp/gpclass.py +++ b/python/samba/gp/gpclass.py @@ -357,7 +357,8 @@ class gp_ext(object): class gp_inf_ext(gp_ext): def read(self, data_file): - policy = open(data_file, 'rb').read() + with open(data_file, 'rb') as f: + policy = f.read() inf_conf = ConfigParser(interpolation=None) inf_conf.optionxform = str try: @@ -369,13 +370,15 @@ class gp_inf_ext(gp_ext): class gp_pol_ext(gp_ext): def read(self, data_file): - raw = open(data_file, 'rb').read() + with open(data_file, 'rb') as f: + raw = f.read() return ndr_unpack(preg.file, raw) class gp_xml_ext(gp_ext): def read(self, data_file): - raw = open(data_file, 'rb').read() + with open(data_file, 'rb') as f: + raw = f.read() try: return etree.fromstring(raw.decode()) except UnicodeDecodeError: diff --git a/python/samba/gp/vgp_issue_ext.py b/python/samba/gp/vgp_issue_ext.py index 10d4ea0f77f..266e92bac2a 100644 --- a/python/samba/gp/vgp_issue_ext.py +++ b/python/samba/gp/vgp_issue_ext.py @@ -23,7 +23,11 @@ class vgp_issue_ext(gp_xml_ext, gp_misc_applier): raise ValueError('"%s" is not a message attribute' % attribute) msg = value data = self.parse_value(value) - current = open(issue, 'r').read() if os.path.exists(issue) else '' + if os.path.exists(issue): + with open(issue, 'r') as f: + current = f.read() + else: + current = '' # Only overwrite the msg if it hasn't been modified. It may have been # modified by another GPO. if 'new_val' not in data or current.strip() == data['new_val'].strip(): @@ -36,7 +40,11 @@ class vgp_issue_ext(gp_xml_ext, gp_misc_applier): self.cache_remove_attribute(guid, attribute) def apply(self, guid, issue, text): - current = open(issue, 'r').read() if os.path.exists(issue) else '' + if os.path.exists(issue): + with open(issue, 'r') as f: + current = f.read() + else: + current = '' if current != text.text: with open(issue, 'w') as w: w.write(text.text) diff --git a/python/samba/gp/vgp_motd_ext.py b/python/samba/gp/vgp_motd_ext.py index 4aff6ec5db0..845a5c4698f 100644 --- a/python/samba/gp/vgp_motd_ext.py +++ b/python/samba/gp/vgp_motd_ext.py @@ -23,7 +23,11 @@ class vgp_motd_ext(gp_xml_ext, gp_misc_applier): raise ValueError('"%s" is not a message attribute' % attribute) msg = value data = self.parse_value(value) - current = open(motd, 'r').read() if os.path.exists(motd) else '' + if os.path.exists(motd): + with open(motd, 'r') as f: + current = f.read() + else: + current = '' # Only overwrite the msg if it hasn't been modified. It may have been # modified by another GPO. if 'new_val' not in data or current.strip() == data['new_val'].strip(): @@ -36,7 +40,11 @@ class vgp_motd_ext(gp_xml_ext, gp_misc_applier): self.cache_remove_attribute(guid, attribute) def apply(self, guid, motd, text): - current = open(motd, 'r').read() if os.path.exists(motd) else '' + if os.path.exists(motd): + with open(motd, 'r') as f: + current = f.read() + else: + current = '' if current != text.text: with open(motd, 'w') as w: w.write(text.text)