[PATCH] replace lokkit for selinux settings (#815540)

Brian C. Lane bcl at redhat.com
Thu Jun 28 19:12:25 UTC 2012


From: "Brian C. Lane" <bcl at redhat.com>

lokkit is going to be going away. This adds our own configuration editor
class and switches to using that to edit the selinux setting.
---
 pyanaconda/iutil.py    |   54 ++++++++++++++++++++++++++++++++++++++++++++++++
 pyanaconda/security.py |   12 ++++------
 2 files changed, 59 insertions(+), 7 deletions(-)

diff --git a/pyanaconda/iutil.py b/pyanaconda/iutil.py
index 398ef77..d6a8e76 100644
--- a/pyanaconda/iutil.py
+++ b/pyanaconda/iutil.py
@@ -28,6 +28,7 @@ import os.path
 import errno
 import subprocess
 import threading
+import tempfile
 
 from flags import flags
 from constants import *
@@ -1081,3 +1082,56 @@ def dracut_eject(device):
     except Exception, e:
         log.error("Error writing dracut shutdown eject hook for %s: %s" % (device, e))
 
+class EditConfig(object):
+    """
+    Edit values in a configuration file without changing comments.
+    Supports KEY=VALUE lines and ignores everything else.
+    Does not allow adding or deleting keys.
+    Does not support duplicate key entries.
+
+    ec = EditConfig("/path/to/file")
+    ec.values is a dict of keys from the file, edit as needed then call:
+    ec.write()
+
+    """
+    def __init__(self, filename):
+        """ filename is the full path of the file to edit
+        """
+        self.filename = filename
+        self.values = {}
+        self.read()
+
+    def read(self):
+        """ Read the config file and store the key/value pairs
+        """
+        with open(self.filename) as f:
+            for line in f:
+                if len(line)>0 and line[0] in '#";':
+                    continue
+                (key, sep, var) = [x.strip() for x in line.partition("=")]
+                if key and sep == "=":
+                    self.values[key] = var
+
+    def write(self):
+        """ Rewrite the file with the new key/value pairs
+            pass-thru comments and lines without =
+        """
+        tmpf = tempfile.NamedTemporaryFile(mode="w", delete=False)
+        with open(self.filename) as f:
+            for line in f:
+                if len(line)>0 and line[0] in '#";':
+                    tmpf.write(line)
+                    continue
+                (key, sep, var) = [x.strip() for x in line.partition("=")]
+                if key in self.values and sep == "=":
+                    tmpf.write("%s=%s\n" % (key, self.values[key]))
+                    continue
+                tmpf.write(line)
+        tmpf.close()
+
+        # Move the temporary file (with 0600 permissions) over the top of the
+        # original and preserve the original's permissions
+        m = os.stat(self.filename)
+        shutil.move(tmpf.name, self.filename)
+        os.chmod(self.filename, m.st_mode)
+
diff --git a/pyanaconda/security.py b/pyanaconda/security.py
index 50d92e2..b7d8dcc 100644
--- a/pyanaconda/security.py
+++ b/pyanaconda/security.py
@@ -65,14 +65,12 @@ class Security:
             log.error("unknown selinux state: %s" %(self.selinux,))
             return
 
-        args = args + [ "--selinux=%s" %(selinux_states[self.selinux],) ]
-
         try:
-            iutil.execWithRedirect("/usr/sbin/lokkit", args,
-                                   root=ROOT_PATH, stdout="/dev/null",
-                                   stderr="/dev/null")
-        except (RuntimeError, OSError) as msg:
-            log.error ("lokkit run failed: %s" %(msg,))
+            selinux_cfg = iutil.EditConfig(ROOT_PATH+"/etc/selinux/config")
+            selinux_cfg.values["SELINUX"] = selinux_states[self.selinux]
+            selinux_cfg.write()
+        except IOError as msg:
+            log.error ("Error setting selinux mode: %s" %(msg,))
 
         args = ["--update", "--nostart"] + shlex.split(self.auth)
         if self._addFingerprint():
-- 
1.7.7.6



More information about the anaconda-patches mailing list