483b06
From e5491b62d3ec21feb7809f7f65797151d256c580 Mon Sep 17 00:00:00 2001
483b06
From: Jan Cholasta <jcholast@redhat.com>
483b06
Date: Wed, 3 May 2017 06:48:57 +0000
483b06
Subject: [PATCH] client install: fix client PKINIT configuration
483b06
483b06
Set `pkinit_anchors` in `krb5.conf` to a CA certificate bundle of CAs
483b06
trusted to issue KDC certificates rather than `/etc/ipa/ca.crt`.
483b06
483b06
Set `pkinit_pool` in `krb5.conf` to a CA certificate bundle of all CAs
483b06
known to IPA.
483b06
483b06
Make sure both bundles are exported in all installation code paths.
483b06
483b06
https://pagure.io/freeipa/issue/6831
483b06
483b06
Reviewed-By: Stanislav Laznicka <slaznick@redhat.com>
483b06
Reviewed-By: Martin Babinsky <mbabinsk@redhat.com>
483b06
---
483b06
 client/Makefile.am                         |  1 +
483b06
 freeipa.spec.in                            | 10 ++++++++++
483b06
 install/share/krb5.conf.template           |  3 ++-
483b06
 ipaclient/install/client.py                | 15 ++++++++++++++-
483b06
 ipaclient/install/ipa_certupdate.py        |  2 ++
483b06
 ipaplatform/base/paths.py                  |  2 ++
483b06
 ipaserver/install/cainstance.py            | 11 +++++++----
483b06
 ipaserver/install/ipa_backup.py            |  2 ++
483b06
 ipaserver/install/krbinstance.py           |  4 +++-
483b06
 ipaserver/install/server/install.py        | 10 ++++++++++
483b06
 ipaserver/install/server/replicainstall.py |  4 ++++
483b06
 ipaserver/install/server/upgrade.py        |  4 +++-
483b06
 12 files changed, 60 insertions(+), 8 deletions(-)
483b06
483b06
diff --git a/client/Makefile.am b/client/Makefile.am
483b06
index b6c9dea437460b0f912854a6a2fb9d1f30f3b1e7..e354cb41a4ee0d7da04197abe0e750c5d727bb4d 100644
483b06
--- a/client/Makefile.am
483b06
+++ b/client/Makefile.am
483b06
@@ -101,4 +101,5 @@ EXTRA_DIST =			\
483b06
 
483b06
 install-data-hook:
483b06
 	$(INSTALL) -d -m 755 $(DESTDIR)$(IPA_SYSCONF_DIR)/nssdb
483b06
+	$(INSTALL) -d -m 755 $(DESTDIR)$(localstatedir)/lib/ipa-client/pki
483b06
 	$(INSTALL) -d -m 755 $(DESTDIR)$(localstatedir)/lib/ipa-client/sysrestore
483b06
diff --git a/freeipa.spec.in b/freeipa.spec.in
483b06
index 0335a9970be82e80e98696f3d7fd4ec64894ef5f..6cb37ae53b039aa1d0e0509f62a3237504be6555 100644
483b06
--- a/freeipa.spec.in
483b06
+++ b/freeipa.spec.in
483b06
@@ -1097,6 +1097,15 @@ if [ $1 -gt 1 ] ; then
483b06
         fi
483b06
     fi
483b06
 
483b06
+    if [ $restore -ge 2 ]; then
483b06
+        if grep -E -q '\s*pkinit_anchors = FILE:/etc/ipa/ca.crt$' /etc/krb5.conf 2>/dev/null; then
483b06
+            sed -E 's|(\s*)pkinit_anchors = FILE:/etc/ipa/ca.crt$|\1pkinit_anchors = FILE:/var/lib/ipa-client/pki/kdc-ca-bundle.pem\n\1pkinit_pool = FILE:/var/lib/ipa-client/pki/ca-bundle.pem|' /etc/krb5.conf >/etc/krb5.conf.ipanew
483b06
+            mv -Z /etc/krb5.conf.ipanew /etc/krb5.conf
483b06
+            cp /etc/ipa/ca.crt /var/lib/ipa-client/pki/kdc-ca-bundle.pem
483b06
+            cp /etc/ipa/ca.crt /var/lib/ipa-client/pki/ca-bundle.pem
483b06
+        fi
483b06
+    fi
483b06
+
483b06
     if [ -f '/etc/sysconfig/ntpd' -a $restore -ge 2 ]; then
483b06
         if grep -E -q 'OPTIONS=.*-u ntp:ntp' /etc/sysconfig/ntpd 2>/dev/null; then
483b06
             sed -r '/OPTIONS=/ { s/\s+-u ntp:ntp\s+/ /; s/\s*-u ntp:ntp\s*// }' /etc/sysconfig/ntpd >/etc/sysconfig/ntpd.ipanew
483b06
@@ -1468,6 +1477,7 @@ fi
483b06
 %ghost %config(noreplace) %{_sysconfdir}/ipa/nssdb/pwdfile.txt
483b06
 %ghost %config(noreplace) %{_sysconfdir}/pki/ca-trust/source/ipa.p11-kit
483b06
 %dir %{_localstatedir}/lib/ipa-client
483b06
+%dir %{_localstatedir}/lib/ipa-client/pki
483b06
 %dir %{_localstatedir}/lib/ipa-client/sysrestore
483b06
 %{_mandir}/man5/default.conf.5*
483b06
 
483b06
diff --git a/install/share/krb5.conf.template b/install/share/krb5.conf.template
483b06
index e8b2ad8dace8264cd9345285f55c42422bf81ca3..1f18ff90d34ccccb42c4b64d188e7d70e9892b71 100644
483b06
--- a/install/share/krb5.conf.template
483b06
+++ b/install/share/krb5.conf.template
483b06
@@ -21,7 +21,8 @@ $OTHER_LIBDEFAULTS
483b06
   master_kdc = $FQDN:88
483b06
   admin_server = $FQDN:749
483b06
   default_domain = $DOMAIN
483b06
-  pkinit_anchors = FILE:/etc/ipa/ca.crt
483b06
+  pkinit_anchors = FILE:$KDC_CA_BUNDLE_PEM
483b06
+  pkinit_pool = FILE:$CA_BUNDLE_PEM
483b06
 }
483b06
 
483b06
 [domain_realm]
483b06
diff --git a/ipaclient/install/client.py b/ipaclient/install/client.py
483b06
index e78be904dd6bad491d9f3c1bb1e1410bc1779d45..6f10f5258747881b9af8c6b70b499f9ff7d577ff 100644
483b06
--- a/ipaclient/install/client.py
483b06
+++ b/ipaclient/install/client.py
483b06
@@ -710,7 +710,11 @@ def configure_krb5_conf(
483b06
         kropts.append(krbconf.setOption('default_domain', cli_domain))
483b06
 
483b06
     kropts.append(
483b06
-        krbconf.setOption('pkinit_anchors', 'FILE:%s' % paths.IPA_CA_CRT))
483b06
+        krbconf.setOption('pkinit_anchors',
483b06
+                          'FILE:%s' % paths.KDC_CA_BUNDLE_PEM))
483b06
+    kropts.append(
483b06
+        krbconf.setOption('pkinit_pool',
483b06
+                          'FILE:%s' % paths.CA_BUNDLE_PEM))
483b06
     ropts = [{
483b06
         'name': cli_realm,
483b06
         'type': 'subsection',
483b06
@@ -2770,6 +2774,13 @@ def _install(options):
483b06
     ca_certs_trust = [(c, n, certstore.key_policy_to_trust_flags(t, True, u))
483b06
                       for (c, n, t, u) in ca_certs]
483b06
 
483b06
+    x509.write_certificate_list(
483b06
+        [c for c, n, t, u in ca_certs if t is not False],
483b06
+        paths.KDC_CA_BUNDLE_PEM)
483b06
+    x509.write_certificate_list(
483b06
+        [c for c, n, t, u in ca_certs if t is not False],
483b06
+        paths.CA_BUNDLE_PEM)
483b06
+
483b06
     # Add the CA certificates to the IPA NSS database
483b06
     root_logger.debug("Adding CA certificates to the IPA NSS database.")
483b06
     ipa_db = certdb.NSSDatabase(paths.IPA_NSSDB_DIR)
483b06
@@ -3317,6 +3328,8 @@ def uninstall(options):
483b06
 
483b06
     # Remove the CA cert
483b06
     remove_file(paths.IPA_CA_CRT)
483b06
+    remove_file(paths.KDC_CA_BUNDLE_PEM)
483b06
+    remove_file(paths.CA_BUNDLE_PEM)
483b06
 
483b06
     root_logger.info("Client uninstall complete.")
483b06
 
483b06
diff --git a/ipaclient/install/ipa_certupdate.py b/ipaclient/install/ipa_certupdate.py
483b06
index 7dc88f07ae14e5416f6fe3dc8400b7d4bcabef72..7e8527e1fcb575844e8f4c90016435124b70e381 100644
483b06
--- a/ipaclient/install/ipa_certupdate.py
483b06
+++ b/ipaclient/install/ipa_certupdate.py
483b06
@@ -113,6 +113,8 @@ class CertUpdate(admintool.AdminTool):
483b06
 
483b06
     def update_client(self, certs):
483b06
         self.update_file(paths.IPA_CA_CRT, certs)
483b06
+        self.update_file(paths.KDC_CA_BUNDLE_PEM, certs)
483b06
+        self.update_file(paths.CA_BUNDLE_PEM, certs)
483b06
 
483b06
         ipa_db = certdb.NSSDatabase(api.env.nss_dir)
483b06
 
483b06
diff --git a/ipaplatform/base/paths.py b/ipaplatform/base/paths.py
483b06
index f80c9e95ab875222887e3692ab80151f84345469..804fddee60f787e161947bbe4b1914995257ceb4 100644
483b06
--- a/ipaplatform/base/paths.py
483b06
+++ b/ipaplatform/base/paths.py
483b06
@@ -331,6 +331,8 @@ class BasePathNamespace(object):
483b06
     VAR_RUN_DIRSRV_DIR = "/var/run/dirsrv"
483b06
     IPA_CCACHES = "/var/run/ipa/ccaches"
483b06
     HTTP_CCACHE = "/var/lib/ipa/gssproxy/http.ccache"
483b06
+    CA_BUNDLE_PEM = "/var/lib/ipa-client/pki/ca-bundle.pem"
483b06
+    KDC_CA_BUNDLE_PEM = "/var/lib/ipa-client/pki/kdc-ca-bundle.pem"
483b06
     IPA_RENEWAL_LOCK = "/var/run/ipa/renewal.lock"
483b06
     SVC_LIST_FILE = "/var/run/ipa/services.list"
483b06
     KRB5CC_SAMBA = "/var/run/samba/krb5cc_samba"
483b06
diff --git a/ipaserver/install/cainstance.py b/ipaserver/install/cainstance.py
483b06
index a4aa4f2069277181501ebd92f3795c452b10acd0..b8c8cc4fc4532fc2c911ec174d363f8280ce863b 100644
483b06
--- a/ipaserver/install/cainstance.py
483b06
+++ b/ipaserver/install/cainstance.py
483b06
@@ -794,10 +794,13 @@ class CAInstance(DogtagInstance):
483b06
         certlist = x509.pkcs7_to_pems(data, x509.DER)
483b06
 
483b06
         # We have all the certificates in certlist, write them to a PEM file
483b06
-        with open(paths.IPA_CA_CRT, 'w') as ipaca_pem:
483b06
-            for cert in certlist:
483b06
-                ipaca_pem.write(cert)
483b06
-                ipaca_pem.write('\n')
483b06
+        for path in [paths.IPA_CA_CRT,
483b06
+                     paths.KDC_CA_BUNDLE_PEM,
483b06
+                     paths.CA_BUNDLE_PEM]:
483b06
+            with open(path, 'w') as ipaca_pem:
483b06
+                for cert in certlist:
483b06
+                    ipaca_pem.write(cert)
483b06
+                    ipaca_pem.write('\n')
483b06
 
483b06
     def __request_ra_certificate(self):
483b06
         # create a temp file storing the pwd
483b06
diff --git a/ipaserver/install/ipa_backup.py b/ipaserver/install/ipa_backup.py
483b06
index 40f08d7d727a8b97b5996f15d27c1e20788e1473..f8cdd56d26636678279ba5afb423c5eef10c33d0 100644
483b06
--- a/ipaserver/install/ipa_backup.py
483b06
+++ b/ipaserver/install/ipa_backup.py
483b06
@@ -150,6 +150,8 @@ class Backup(admintool.AdminTool):
483b06
         paths.SSHD_CONFIG,
483b06
         paths.SSH_CONFIG,
483b06
         paths.KRB5_CONF,
483b06
+        paths.KDC_CA_BUNDLE_PEM,
483b06
+        paths.CA_BUNDLE_PEM,
483b06
         paths.IPA_CA_CRT,
483b06
         paths.IPA_DEFAULT_CONF,
483b06
         paths.DS_KEYTAB,
483b06
diff --git a/ipaserver/install/krbinstance.py b/ipaserver/install/krbinstance.py
483b06
index 2f14ff592064d3446f73b31e615b2de88d6d786c..e52577bbaa15064946f9a3c9720aa40ffc3251aa 100644
483b06
--- a/ipaserver/install/krbinstance.py
483b06
+++ b/ipaserver/install/krbinstance.py
483b06
@@ -261,7 +261,9 @@ class KrbInstance(service.Service):
483b06
                              KRB5KDC_KADM5_KEYTAB=paths.KRB5KDC_KADM5_KEYTAB,
483b06
                              KDC_CERT=paths.KDC_CERT,
483b06
                              KDC_KEY=paths.KDC_KEY,
483b06
-                             CACERT_PEM=paths.CACERT_PEM)
483b06
+                             CACERT_PEM=paths.CACERT_PEM,
483b06
+                             KDC_CA_BUNDLE_PEM=paths.KDC_CA_BUNDLE_PEM,
483b06
+                             CA_BUNDLE_PEM=paths.CA_BUNDLE_PEM)
483b06
 
483b06
         # IPA server/KDC is not a subdomain of default domain
483b06
         # Proper domain-realm mapping needs to be specified
483b06
diff --git a/ipaserver/install/server/install.py b/ipaserver/install/server/install.py
483b06
index 25c21db721c58388ae8fd6ab1fbc443d513a4324..c1bdce6c8459dfeabd0096d105e535ec4ee56a2a 100644
483b06
--- a/ipaserver/install/server/install.py
483b06
+++ b/ipaserver/install/server/install.py
483b06
@@ -796,6 +796,16 @@ def install(installer):
483b06
         x509.write_certificate(http_ca_cert, paths.IPA_CA_CRT)
483b06
         os.chmod(paths.IPA_CA_CRT, 0o444)
483b06
 
483b06
+        if not options.no_pkinit:
483b06
+            x509.write_certificate(http_ca_cert, paths.KDC_CA_BUNDLE_PEM)
483b06
+        else:
483b06
+            with open(paths.KDC_CA_BUNDLE_PEM, 'w'):
483b06
+                pass
483b06
+        os.chmod(paths.KDC_CA_BUNDLE_PEM, 0o444)
483b06
+
483b06
+        x509.write_certificate(http_ca_cert, paths.CA_BUNDLE_PEM)
483b06
+        os.chmod(paths.CA_BUNDLE_PEM, 0o444)
483b06
+
483b06
     # we now need to enable ssl on the ds
483b06
     ds.enable_ssl()
483b06
 
483b06
diff --git a/ipaserver/install/server/replicainstall.py b/ipaserver/install/server/replicainstall.py
483b06
index c19edceec42845f3169adc923762f700739232f2..66d7ba44645aed69b12f0e5ea14f5080492fe5ef 100644
483b06
--- a/ipaserver/install/server/replicainstall.py
483b06
+++ b/ipaserver/install/server/replicainstall.py
483b06
@@ -1390,6 +1390,10 @@ def install(installer):
483b06
 
483b06
         # Update and istall updated CA file
483b06
         cafile = install_ca_cert(conn, api.env.basedn, api.env.realm, cafile)
483b06
+        install_ca_cert(conn, api.env.basedn, api.env.realm, cafile,
483b06
+                        destfile=paths.KDC_CA_BUNDLE_PEM)
483b06
+        install_ca_cert(conn, api.env.basedn, api.env.realm, cafile,
483b06
+                        destfile=paths.CA_BUNDLE_PEM)
483b06
 
483b06
         # Configure dirsrv
483b06
         ds = install_replica_ds(config, options, ca_enabled,
483b06
diff --git a/ipaserver/install/server/upgrade.py b/ipaserver/install/server/upgrade.py
483b06
index c244958f4cddba0d1edded5165a295b1e1ee2b8a..648dc1f29c44f89d9fbceb7b50373d93c88b5c1a 100644
483b06
--- a/ipaserver/install/server/upgrade.py
483b06
+++ b/ipaserver/install/server/upgrade.py
483b06
@@ -1831,7 +1831,9 @@ def upgrade_configuration():
483b06
                         KRB5KDC_KADM5_KEYTAB=paths.KRB5KDC_KADM5_KEYTAB,
483b06
                         KDC_CERT=paths.KDC_CERT,
483b06
                         KDC_KEY=paths.KDC_KEY,
483b06
-                        CACERT_PEM=paths.CACERT_PEM)
483b06
+                        CACERT_PEM=paths.CACERT_PEM,
483b06
+                        KDC_CA_BUNDLE_PEM=paths.KDC_CA_BUNDLE_PEM,
483b06
+                        CA_BUNDLE_PEM=paths.CA_BUNDLE_PEM)
483b06
     krb.add_anonymous_principal()
483b06
     setup_pkinit(krb)
483b06
 
483b06
-- 
483b06
2.9.4
483b06