483b06
From c3ee037c2dd92ccb277523919e991471c9caa3c6 Mon Sep 17 00:00:00 2001
483b06
From: Stanislav Laznicka <slaznick@redhat.com>
483b06
Date: Tue, 11 Apr 2017 10:21:15 +0200
483b06
Subject: [PATCH] Fix CA-less to CA-full upgrade
483b06
483b06
CertDB would have always created a directory on initialization. This
483b06
behavior changes here by replacing the truncate argument with create
483b06
which will only create the database when really required.
483b06
483b06
https://pagure.io/freeipa/issue/6853
483b06
483b06
Reviewed-By: Tomas Krizek <tkrizek@redhat.com>
483b06
---
483b06
 ipaserver/install/ca.py           |  2 ++
483b06
 ipaserver/install/certs.py        | 38 ++++++++++++++++++++++++++++----------
483b06
 ipaserver/install/httpinstance.py |  2 +-
483b06
 3 files changed, 31 insertions(+), 11 deletions(-)
483b06
483b06
diff --git a/ipaserver/install/ca.py b/ipaserver/install/ca.py
483b06
index db3b744a51b0ae2ba12f79c155a1bb0698d94bec..8ee0fda23411563c70b7db5f39f43c2869c108b5 100644
483b06
--- a/ipaserver/install/ca.py
483b06
+++ b/ipaserver/install/ca.py
483b06
@@ -183,6 +183,8 @@ def install_check(standalone, replica_config, options):
483b06
             realm_name, nssdir=dirname, subject_base=options._subject_base)
483b06
 
483b06
         for db in (cadb, dsdb):
483b06
+            if not db.exists():
483b06
+                continue
483b06
             for nickname, _trust_flags in db.list_certs():
483b06
                 if nickname == certdb.get_ca_nickname(realm_name):
483b06
                     raise ScriptError(
483b06
diff --git a/ipaserver/install/certs.py b/ipaserver/install/certs.py
483b06
index 16139f81f0d0bd6889a9f38948204bb5bc018028..89e57134f24c505d669057eefffb7862b3b8179a 100644
483b06
--- a/ipaserver/install/certs.py
483b06
+++ b/ipaserver/install/certs.py
483b06
@@ -99,7 +99,7 @@ class CertDB(object):
483b06
     # TODO: Remove all selfsign code
483b06
     def __init__(self, realm, nssdir, fstore=None,
483b06
                  host_name=None, subject_base=None, ca_subject=None,
483b06
-                 user=None, group=None, mode=None, truncate=False):
483b06
+                 user=None, group=None, mode=None, create=False):
483b06
         self.nssdb = NSSDatabase(nssdir)
483b06
 
483b06
         self.secdir = nssdir
483b06
@@ -132,15 +132,16 @@ class CertDB(object):
483b06
         self.uid = 0
483b06
         self.gid = 0
483b06
 
483b06
-        if not truncate and os.path.exists(self.secdir):
483b06
-            # We are going to set the owner of all of the cert
483b06
-            # files to the owner of the containing directory
483b06
-            # instead of that of the process. This works when
483b06
-            # this is called by root for a daemon that runs as
483b06
-            # a normal user
483b06
-            mode = os.stat(self.secdir)
483b06
-            self.uid = mode[stat.ST_UID]
483b06
-            self.gid = mode[stat.ST_GID]
483b06
+        if not create:
483b06
+            if os.path.isdir(self.secdir):
483b06
+                # We are going to set the owner of all of the cert
483b06
+                # files to the owner of the containing directory
483b06
+                # instead of that of the process. This works when
483b06
+                # this is called by root for a daemon that runs as
483b06
+                # a normal user
483b06
+                mode = os.stat(self.secdir)
483b06
+                self.uid = mode[stat.ST_UID]
483b06
+                self.gid = mode[stat.ST_GID]
483b06
         else:
483b06
             if user is not None:
483b06
                 pu = pwd.getpwnam(user)
483b06
@@ -162,6 +163,23 @@ class CertDB(object):
483b06
     def passwd_fname(self):
483b06
         return self.nssdb.pwd_file
483b06
 
483b06
+    def exists(self):
483b06
+        """
483b06
+        Checks whether all NSS database files + our pwd_file exist
483b06
+        """
483b06
+        db_files = (
483b06
+            self.secdir,
483b06
+            self.certdb_fname,
483b06
+            self.keydb_fname,
483b06
+            self.secmod_fname,
483b06
+            self.nssdb.pwd_file,
483b06
+        )
483b06
+
483b06
+        for f in db_files:
483b06
+            if not os.path.exists(f):
483b06
+                return False
483b06
+        return True
483b06
+
483b06
     def __del__(self):
483b06
         if self.reqdir is not None:
483b06
             shutil.rmtree(self.reqdir, ignore_errors=True)
483b06
diff --git a/ipaserver/install/httpinstance.py b/ipaserver/install/httpinstance.py
483b06
index 8e444be2d23ec5e7890d221508bc866de2854c89..aeb5c5e450813469e1b6cd374b30cd4aab338537 100644
483b06
--- a/ipaserver/install/httpinstance.py
483b06
+++ b/ipaserver/install/httpinstance.py
483b06
@@ -366,7 +366,7 @@ class HTTPInstance(service.Service):
483b06
         db = certs.CertDB(self.realm, nssdir=paths.HTTPD_ALIAS_DIR,
483b06
                           subject_base=self.subject_base, user="root",
483b06
                           group=constants.HTTPD_GROUP,
483b06
-                          truncate=True)
483b06
+                          create=True)
483b06
         self.disable_system_trust()
483b06
         self.create_password_conf()
483b06
         if self.pkcs12_info:
483b06
-- 
483b06
2.12.2
483b06