]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
gp: Fix resource leaks
authorJoseph Sutton <josephsutton@catalyst.net.nz>
Tue, 29 Aug 2023 04:29:55 +0000 (16:29 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 30 Aug 2023 02:15:29 +0000 (02:15 +0000)
Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/gp/gp_gnome_settings_ext.py
python/samba/gp/gp_msgs_ext.py
python/samba/gp/gp_sudoers_ext.py
python/samba/gp/gpclass.py
python/samba/gp/vgp_issue_ext.py
python/samba/gp/vgp_motd_ext.py

index ee0e756e8e28f33995a09ec354365cd2542daf11..567ab941533aa46764e550f2b99f481769dad0ef 100644 (file)
@@ -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'))
index 1c9135a074be646dc8a7e729f00a74dbbc258eb2..9aadddfc7132990c5cf0459391dc868ac24f5dcf 100644 (file)
@@ -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
index 1990962144faa8a611a600c2aeefcb6cbed64892..026aeba547f9eebbf26d8bb0d6bf0883c06d5fb7 100644 (file)
@@ -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,
index 437f9961f3a3e51fff3d07c75ac3d15be6ee2c19..3b6289fe0c0234796705917fa48e6e4825630515 100644 (file)
@@ -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:
index 10d4ea0f77f4856a33a2b07c367fbecea3e06461..266e92bac2ad1953dcae421257a858132b229bd2 100644 (file)
@@ -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)
index 4aff6ec5db03f5646b9f8dcbe7513e1dde957e36..845a5c4698f0901aab2a0ef955f2524d0b69ae0b 100644 (file)
@@ -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)