pgreco / rpms / ipa

Forked from forks/areguera/rpms/ipa 4 years ago
Clone

Blame SOURCES/0149-server-install-fix-KDC-PKINIT-configuration.patch

483b06
From 2558a0336e9d61b2b7e321b7dfa32426151b4bbb Mon Sep 17 00:00:00 2001
483b06
From: Jan Cholasta <jcholast@redhat.com>
483b06
Date: Wed, 3 May 2017 06:09:03 +0000
483b06
Subject: [PATCH] server install: fix KDC PKINIT configuration
483b06
483b06
Set `pkinit_pool` in `kdc.conf` to a CA certificate bundle of all CAs known
483b06
to IPA.
483b06
483b06
Make sure `cacert.pem` is exported in all installation code paths.
483b06
483b06
Use the KDC certificate itself as a PKINIT anchor in `login_password`.
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
 install/restart_scripts/Makefile.am    |  1 +
483b06
 install/restart_scripts/renew_kdc_cert | 31 ++++++++++++++++++
483b06
 install/share/kdc.conf.template        |  2 ++
483b06
 ipaclient/install/ipa_certupdate.py    |  1 +
483b06
 ipalib/install/kinit.py                |  7 +++--
483b06
 ipaserver/install/krbinstance.py       | 27 +++++++++-------
483b06
 ipaserver/install/server/upgrade.py    | 57 ++++++++++++++++++++++++++--------
483b06
 ipaserver/rpcserver.py                 |  5 ++-
483b06
 8 files changed, 103 insertions(+), 28 deletions(-)
483b06
 create mode 100755 install/restart_scripts/renew_kdc_cert
483b06
483b06
diff --git a/install/restart_scripts/Makefile.am b/install/restart_scripts/Makefile.am
483b06
index 04881b406b6be92b46e630f30d724918506e2aa8..240cebdee8cae7a0c7bdf88f5300583b4232fc94 100644
483b06
--- a/install/restart_scripts/Makefile.am
483b06
+++ b/install/restart_scripts/Makefile.am
483b06
@@ -5,6 +5,7 @@ app_DATA =                              \
483b06
 	restart_dirsrv			\
483b06
 	restart_httpd			\
483b06
 	renew_ca_cert			\
483b06
+	renew_kdc_cert			\
483b06
 	renew_ra_cert			\
483b06
 	stop_pkicad			\
483b06
 	renew_ra_cert_pre		\
483b06
diff --git a/install/restart_scripts/renew_kdc_cert b/install/restart_scripts/renew_kdc_cert
483b06
new file mode 100755
483b06
index 0000000000000000000000000000000000000000..9247920874fc9540ac3421dd59fd902cc195243f
483b06
--- /dev/null
483b06
+++ b/install/restart_scripts/renew_kdc_cert
483b06
@@ -0,0 +1,31 @@
483b06
+#!/usr/bin/python2 -E
483b06
+#
483b06
+# Copyright (C) 2017  FreeIPA Contributors see COPYING for license
483b06
+#
483b06
+
483b06
+import os
483b06
+import syslog
483b06
+import traceback
483b06
+
483b06
+from ipaplatform import services
483b06
+from ipaplatform.paths import paths
483b06
+from ipaserver.install import certs
483b06
+
483b06
+
483b06
+def main():
483b06
+    with certs.renewal_lock:
483b06
+        os.chmod(paths.KDC_CERT, 0o644)
483b06
+
483b06
+        try:
483b06
+            if services.knownservices.krb5kdc.is_running():
483b06
+                syslog.syslog(syslog.LOG_NOTICE, 'restarting krb5kdc')
483b06
+                services.knownservices.krb5kdc.restart()
483b06
+        except Exception as e:
483b06
+            syslog.syslog(
483b06
+                syslog.LOG_ERR, "cannot restart krb5kdc: {}".format(e))
483b06
+
483b06
+
483b06
+try:
483b06
+    main()
483b06
+except Exception:
483b06
+    syslog.syslog(syslog.LOG_ERR, traceback.format_exc())
483b06
diff --git a/install/share/kdc.conf.template b/install/share/kdc.conf.template
483b06
index ec53a1ff5f7110704143074bc7a5d1dfdc705344..306351b86111eb0e883b2398678f50b821e0ad7f 100644
483b06
--- a/install/share/kdc.conf.template
483b06
+++ b/install/share/kdc.conf.template
483b06
@@ -13,5 +13,7 @@
483b06
   default_principal_flags = +preauth
483b06
 ;  admin_keytab = $KRB5KDC_KADM5_KEYTAB
483b06
   pkinit_identity = FILE:$KDC_CERT,$KDC_KEY
483b06
+  pkinit_anchors = FILE:$KDC_CERT
483b06
   pkinit_anchors = FILE:$CACERT_PEM
483b06
+  pkinit_pool = FILE:$CA_BUNDLE_PEM
483b06
  }
483b06
diff --git a/ipaclient/install/ipa_certupdate.py b/ipaclient/install/ipa_certupdate.py
483b06
index 7e8527e1fcb575844e8f4c90016435124b70e381..93da8422b6f503b8c44db678736d7f71f7d7567e 100644
483b06
--- a/ipaclient/install/ipa_certupdate.py
483b06
+++ b/ipaclient/install/ipa_certupdate.py
483b06
@@ -172,6 +172,7 @@ class CertUpdate(admintool.AdminTool):
483b06
             certmonger.modify(request_id, ca='dogtag-ipa-ca-renew-agent')
483b06
 
483b06
         self.update_file(paths.CA_CRT, certs)
483b06
+        self.update_file(paths.CACERT_PEM, certs)
483b06
 
483b06
     def update_file(self, filename, certs, mode=0o444):
483b06
         certs = (c[0] for c in certs if c[2] is not False)
483b06
diff --git a/ipalib/install/kinit.py b/ipalib/install/kinit.py
483b06
index fb6caee4d6b5fef27b53753b21ad83572da31ac4..73471f103eabfe39580c8fbd0665157f635fa5c5 100644
483b06
--- a/ipalib/install/kinit.py
483b06
+++ b/ipalib/install/kinit.py
483b06
@@ -96,7 +96,7 @@ def kinit_password(principal, password, ccache_name, config=None,
483b06
         raise RuntimeError(result.error_output)
483b06
 
483b06
 
483b06
-def kinit_armor(ccache_name, pkinit_anchor=None):
483b06
+def kinit_armor(ccache_name, pkinit_anchors=None):
483b06
     """
483b06
     perform anonymous pkinit to obtain anonymous ticket to be used as armor
483b06
     for FAST.
483b06
@@ -113,8 +113,9 @@ def kinit_armor(ccache_name, pkinit_anchor=None):
483b06
     env = {'LC_ALL': 'C'}
483b06
     args = [paths.KINIT, '-n', '-c', ccache_name]
483b06
 
483b06
-    if pkinit_anchor is not None:
483b06
-        args.extend(['-X', 'X509_anchors=FILE:{}'.format(pkinit_anchor)])
483b06
+    if pkinit_anchors is not None:
483b06
+        for pkinit_anchor in pkinit_anchors:
483b06
+            args.extend(['-X', 'X509_anchors=FILE:{}'.format(pkinit_anchor)])
483b06
 
483b06
     # this workaround enables us to capture stderr and put it
483b06
     # into the raised exception in case of unsuccessful authentication
483b06
diff --git a/ipaserver/install/krbinstance.py b/ipaserver/install/krbinstance.py
483b06
index e52577bbaa15064946f9a3c9720aa40ffc3251aa..1692e0b2badb23c18386346a552c83881018cf60 100644
483b06
--- a/ipaserver/install/krbinstance.py
483b06
+++ b/ipaserver/install/krbinstance.py
483b06
@@ -20,7 +20,6 @@
483b06
 from __future__ import absolute_import
483b06
 from __future__ import print_function
483b06
 
483b06
-import shutil
483b06
 import os
483b06
 import pwd
483b06
 import socket
483b06
@@ -28,6 +27,8 @@ import dbus
483b06
 
483b06
 import dns.name
483b06
 
483b06
+from ipalib import x509
483b06
+from ipalib.install import certstore
483b06
 from ipaserver.install import service
483b06
 from ipaserver.install import installutils
483b06
 from ipapython import ipaldap
483b06
@@ -430,7 +431,8 @@ class KrbInstance(service.Service):
483b06
                 ca=certmonger_ca,
483b06
                 dns=self.fqdn,
483b06
                 storage='FILE',
483b06
-                profile=KDC_PROFILE)
483b06
+                profile=KDC_PROFILE,
483b06
+                post_command='renew_kdc_cert')
483b06
         except dbus.DBusException as e:
483b06
             # if the certificate is already tracked, ignore the error
483b06
             name = e.get_dbus_name()
483b06
@@ -448,17 +450,23 @@ class KrbInstance(service.Service):
483b06
         service.set_service_entry_config(
483b06
             'KDC', self.fqdn, [PKINIT_ENABLED], self.suffix)
483b06
 
483b06
+    def _install_pkinit_ca_bundle(self):
483b06
+        ca_certs = certstore.get_ca_certs(self.api.Backend.ldap2,
483b06
+                                          self.api.env.basedn,
483b06
+                                          self.api.env.realm,
483b06
+                                          False)
483b06
+        ca_certs = [c for c, _n, t, _u in ca_certs if t is not False]
483b06
+        x509.write_certificate_list(ca_certs, paths.CACERT_PEM)
483b06
+
483b06
     def issue_selfsigned_pkinit_certs(self):
483b06
         self._call_certmonger(certmonger_ca="SelfSign")
483b06
-        # for self-signed certificate, the certificate is its own CA, copy it
483b06
-        # as CA cert
483b06
-        shutil.copyfile(paths.KDC_CERT, paths.CACERT_PEM)
483b06
+        with open(paths.CACERT_PEM, 'w'):
483b06
+            pass
483b06
 
483b06
     def issue_ipa_ca_signed_pkinit_certs(self):
483b06
         try:
483b06
             self._call_certmonger()
483b06
-            # copy IPA CA bundle to the KDC's CA cert bundle
483b06
-            shutil.copyfile(paths.IPA_CA_CRT, paths.CACERT_PEM)
483b06
+            self._install_pkinit_ca_bundle()
483b06
             self.pkinit_enable()
483b06
         except RuntimeError as e:
483b06
             root_logger.error("PKINIT certificate request failed: %s", e)
483b06
@@ -473,10 +481,7 @@ class KrbInstance(service.Service):
483b06
         certs.install_key_from_p12(self.pkcs12_info[0],
483b06
                                    self.pkcs12_info[1],
483b06
                                    paths.KDC_KEY)
483b06
-        # copy IPA CA bundle to the KDC's CA cert bundle
483b06
-        # NOTE: this may not be the same set of CA certificates trusted by
483b06
-        # externally provided PKINIT cert.
483b06
-        shutil.copyfile(paths.IPA_CA_CRT, paths.CACERT_PEM)
483b06
+        self._install_pkinit_ca_bundle()
483b06
         self.pkinit_enable()
483b06
 
483b06
     def setup_pkinit(self):
483b06
diff --git a/ipaserver/install/server/upgrade.py b/ipaserver/install/server/upgrade.py
483b06
index 648dc1f29c44f89d9fbceb7b50373d93c88b5c1a..db86353165809c57d1ac27bf762393721231fefd 100644
483b06
--- a/ipaserver/install/server/upgrade.py
483b06
+++ b/ipaserver/install/server/upgrade.py
483b06
@@ -11,6 +11,7 @@ import pwd
483b06
 import fileinput
483b06
 import sys
483b06
 
483b06
+from augeas import Augeas
483b06
 import dns.exception
483b06
 
483b06
 import six
483b06
@@ -1527,19 +1528,49 @@ def setup_pkinit(krb):
483b06
         else:
483b06
             krb.issue_selfsigned_pkinit_certs()
483b06
 
483b06
-    # reconfigure KDC just in case in order to handle potentially broken
483b06
-    # 4.5.0 -> 4.5.1 upgrade path
483b06
-    replacevars = dict()
483b06
-    replacevars['pkinit_identity'] = 'FILE:{},{}'.format(
483b06
-        paths.KDC_CERT,paths.KDC_KEY)
483b06
-    appendvars = {}
483b06
-    ipautil.backup_config_and_replace_variables(
483b06
-        krb.fstore, paths.KRB5KDC_KDC_CONF, replacevars=replacevars,
483b06
-        appendvars=appendvars)
483b06
-    tasks.restore_context(paths.KRB5KDC_KDC_CONF)
483b06
-    if krb.is_running():
483b06
-        krb.stop()
483b06
-    krb.start()
483b06
+    aug = Augeas(flags=Augeas.NO_LOAD | Augeas.NO_MODL_AUTOLOAD,
483b06
+                 loadpath=paths.USR_SHARE_IPA_DIR)
483b06
+    try:
483b06
+        aug.transform('IPAKrb5', paths.KRB5KDC_KDC_CONF)
483b06
+        aug.load()
483b06
+
483b06
+        path = '/files{}/realms/{}'.format(paths.KRB5KDC_KDC_CONF, krb.realm)
483b06
+        modified = False
483b06
+
483b06
+        value = 'FILE:{},{}'.format(paths.KDC_CERT, paths.KDC_KEY)
483b06
+        expr = '{}[count(pkinit_identity)=1][pkinit_identity="{}"]'.format(
483b06
+            path, value)
483b06
+        if not aug.match(expr):
483b06
+            aug.remove('{}/pkinit_identity'.format(path))
483b06
+            aug.set('{}/pkinit_identity'.format(path), value)
483b06
+            modified = True
483b06
+
483b06
+        for value in  ['FILE:{}'.format(paths.KDC_CERT),
483b06
+                       'FILE:{}'.format(paths.CACERT_PEM)]:
483b06
+            expr = '{}/pkinit_anchors[.="{}"]'.format(path, value)
483b06
+            if not aug.match(expr):
483b06
+                aug.set('{}/pkinit_anchors[last()+1]'.format(path), value)
483b06
+                modified = True
483b06
+
483b06
+        value = 'FILE:{}'.format(paths.CA_BUNDLE_PEM)
483b06
+        expr = '{}/pkinit_pool[.="{}"]'.format(path, value)
483b06
+        if not aug.match(expr):
483b06
+            aug.set('{}/pkinit_pool[last()+1]'.format(path), value)
483b06
+            modified = True
483b06
+
483b06
+        if modified:
483b06
+            try:
483b06
+                aug.save()
483b06
+            except IOError:
483b06
+                for error_path in aug.match('/augeas//error'):
483b06
+                    root_logger.error('augeas: %s', aug.get(error_path))
483b06
+                raise
483b06
+
483b06
+            if krb.is_running():
483b06
+                krb.stop()
483b06
+            krb.start()
483b06
+    finally:
483b06
+        aug.close()
483b06
 
483b06
 
483b06
 def disable_httpd_system_trust(http):
483b06
diff --git a/ipaserver/rpcserver.py b/ipaserver/rpcserver.py
483b06
index 996a3d29884ca0180c39841f6986abf9b23ff13a..4cde2815a0fe9332d67c84b531f573ff88b1a302 100644
483b06
--- a/ipaserver/rpcserver.py
483b06
+++ b/ipaserver/rpcserver.py
483b06
@@ -945,7 +945,10 @@ class login_password(Backend, KerberosSession):
483b06
         self.debug('Obtaining armor in ccache %s', armor_path)
483b06
 
483b06
         try:
483b06
-            kinit_armor(armor_path, pkinit_anchor=paths.CACERT_PEM)
483b06
+            kinit_armor(
483b06
+                armor_path,
483b06
+                pkinit_anchors=[paths.KDC_CERT, paths.KDC_CA_BUNDLE_PEM],
483b06
+            )
483b06
         except RuntimeError as e:
483b06
             self.error("Failed to obtain armor cache")
483b06
             # We try to continue w/o armor, 2FA will be impacted
483b06
-- 
483b06
2.9.4
483b06