[PATCH 21/22] Set $LIBUSER_CONF early

David Shea dshea at redhat.com
Wed Jun 3 16:03:45 UTC 2015


The contents of the config file may change after the payload is setup
and packages are installed, but the environment variable needs to be set
early. For that reason, treat any failure to access an existing
libuser.conf as an error in createLuserConf.

(cherry picked from commit 637682b6c15dbe2b73e7e1f52a4f8efece7410b7)

Related: rhbz#1188287
---
 anaconda                |  6 ++++++
 pyanaconda/kickstart.py | 11 +++++------
 pyanaconda/users.py     | 25 +++++++++++--------------
 3 files changed, 22 insertions(+), 20 deletions(-)

diff --git a/anaconda b/anaconda
index de2d77a..3c48582 100755
--- a/anaconda
+++ b/anaconda
@@ -368,6 +368,8 @@ def setupPythonPath():
     sys.path.extend(ADDON_PATHS)
 
 def setupEnvironment():
+    from pyanaconda.users import createLuserConf
+
     # This method is run before any threads are started, so this is the one
     # point where it's ok to modify the environment.
     # pylint: disable=environment-modify
@@ -396,6 +398,10 @@ def setupEnvironment():
     else:
         os.environ["DISPLAY"] = ":%s" % constants.X_DISPLAY_NUMBER
 
+    # Call createLuserConf now to setup $LIBUSER_CONF
+    # the config file can change later but the environment variable cannot
+    createLuserConf(iutil.getSysroot())
+
 def setupLoggingFromOpts(options):
     if (options.debug or options.updateSrc) and not options.loglevel:
         # debugging means debug logging if an explicit level hasn't been st
diff --git a/pyanaconda/kickstart.py b/pyanaconda/kickstart.py
index 1904101..56bff3b 100644
--- a/pyanaconda/kickstart.py
+++ b/pyanaconda/kickstart.py
@@ -95,6 +95,9 @@ class AnacondaKSScript(KSScript):
         else:
             scriptRoot = "/"
 
+        # Environment variables that cause problems for %post scripts
+        env_prune = ["LIBUSER_CONF"]
+
         (fd, path) = tempfile.mkstemp("", "ks-script-", scriptRoot + "/tmp")
 
         iutil.eintr_retry_call(os.write, fd, self.script)
@@ -121,7 +124,8 @@ class AnacondaKSScript(KSScript):
         with open(messages, "w") as fp:
             rc = iutil.execWithRedirect(self.interp, ["/tmp/%s" % os.path.basename(path)],
                                         stdout=fp,
-                                        root = scriptRoot)
+                                        root = scriptRoot,
+                                        env_prune = env_prune)
 
         if rc != 0:
             log.error("Error code %s running the kickstart script at line %s", rc, self.lineno)
@@ -1974,11 +1978,6 @@ def runPostScripts(scripts):
     if len(postScripts) == 0:
         return
 
-    # Remove environment variables that cause problems for %post scripts.
-    for var in ["LIBUSER_CONF"]:
-        if var in os.environ:
-            del(os.environ[var])
-
     log.info("Running kickstart %%post script(s)")
     map (lambda s: s.run(iutil.getSysroot()), postScripts)
     log.info("All kickstart %%post script(s) have been run")
diff --git a/pyanaconda/users.py b/pyanaconda/users.py
index 68e497a..67a259a 100644
--- a/pyanaconda/users.py
+++ b/pyanaconda/users.py
@@ -40,21 +40,18 @@ def createLuserConf(instPath, algoname='sha512'):
         This must be called before User() is instantiated the first time
         so that libuser.admin will use the temporary config file.
     """
-    createTmp = False
-    try:
-        fn = os.environ["LIBUSER_CONF"]
-        if os.access(fn, os.F_OK):
-            log.info("removing libuser.conf at %s", os.getenv("LIBUSER_CONF"))
-            os.unlink(fn)
-        log.info("created new libuser.conf at %s with instPath=\"%s\"", fn, instPath)
-        fd = open(fn, 'w')
-    except (OSError, IOError, KeyError):
-        createTmp = True
 
-    if createTmp:
+    # If LIBUSER_CONF is not set, create a new temporary file
+    if "LIBUSER_CONF" not in os.environ:
         (fp, fn) = tempfile.mkstemp(prefix="libuser.")
         log.info("created new libuser.conf at %s with instPath=\"%s\"", fn, instPath)
-        fd = os.fdopen(fp, 'w')
+        fd = os.fdopen(fp, "w")
+        # This is only ok if createLuserConf is first called before threads are started
+        os.environ["LIBUSER_CONF"] = fn # pylint: disable=environment-modify
+    else:
+        fn = os.environ["LIBUSER_CONF"]
+        log.info("Clearing libuser.conf at %s", fn)
+        fd = open(fn, "w")
 
     buf = """
 [defaults]
@@ -79,7 +76,6 @@ login_defs = %(instPath)s/etc/login.defs
 
     fd.write(buf)
     fd.close()
-    os.environ["LIBUSER_CONF"] = fn
 
     return fn
 
@@ -202,7 +198,8 @@ class Users:
             if not root in ["","/"]:
                 os.chroot(root)
                 os.chdir("/")
-                del(os.environ["LIBUSER_CONF"])
+                # This is ok because it's after a fork
+                del(os.environ["LIBUSER_CONF"]) # pylint: disable=environment-modify
 
             self.admin = libuser.admin()
 
-- 
2.1.0



More information about the anaconda-patches mailing list