Blob Blame History Raw
From 474404051fe461a1be5d175b3f13da54ddbe0a37 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Tue, 4 Sep 2018 11:36:43 +0200
Subject: [PATCH 04/16] compat: write only options set on command line to
 pwquality.conf

This will not overwrite pwquality.conf if for exapmle "authconfig --update"
is called. Without this patch the values would get overriden with empty
values.
---
 src/compat/authcompat.py.in.in | 36 ++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/src/compat/authcompat.py.in.in b/src/compat/authcompat.py.in.in
index 1b4f531b021c1e2e8fd99bd081094da365c0c64e..4fa9a6afc1d62aa9dde41b525d473168e6dc2901 100755
--- a/src/compat/authcompat.py.in.in
+++ b/src/compat/authcompat.py.in.in
@@ -166,7 +166,10 @@ class Configuration:
         def getBool(self, name):
             return self.options.getBool(name)
 
-        def getBoolAsValue(self, name, if_true, if_false):
+        def getBoolAsValue(self, name, if_true, if_false, AllowNone=False):
+            if AllowNone and not self.isset(name):
+                return None
+
             value = self.getBool(name)
             if value:
                 return if_true
@@ -318,19 +321,28 @@ class Configuration:
 
         def write(self):
             config = EnvironmentFile(Path.System('pwquality.conf'))
+            value_set = False
 
-            # for each if these options, we want to write a line to the config
-            # *only if* it is set to an actual value, see
-            # https://bugzilla.redhat.com/show_bug.cgi?id=1618865
-            for pwval in ["minlen", "minclass", "maxrepeat", "maxclassrepeat"]:
-                if self.isset("pass{0}".format(pwval)):
-                    config.set(pwval, self.get("pass{0}".format(pwval)))
+            pwopts = {
+                "minlen" : self.get("passminlen"),
+                "minclass" : self.get("passminclass"),
+                "maxrepeat" : self.get("passmaxrepeat"),
+                "maxclassrepeat" : self.get("passmaxclassrepeat"),
+                "lcredit" : self.getBoolAsValue("reqlower", -1, 0, AllowNone=True),
+                "ucredit" : self.getBoolAsValue("requpper", -1, 0, AllowNone=True),
+                "dcredit" : self.getBoolAsValue("reqdigit", -1, 0, AllowNone=True),
+                "ocredit" : self.getBoolAsValue("reqother", -1, 0, AllowNone=True)
+            }
 
-            config.set("lcredit", self.getBoolAsValue("reqlower", -1, 0))
-            config.set("ucredit", self.getBoolAsValue("requpper", -1, 0))
-            config.set("dcredit", self.getBoolAsValue("reqdigit", -1, 0))
-            config.set("ocredit", self.getBoolAsValue("reqother", -1, 0))
-            config.write()
+            # Write options only if their are actually set
+            for opt, value in pwopts.items():
+                if value is not None:
+                    print(opt + "=" + str(value))
+                    config.set(opt, value)
+                    value_set = True
+
+            if value_set:
+                config.write()
 
     class MakeHomedir(Base):
         def __init__(self, options):
-- 
2.17.1