pgreco / rpms / ipa

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

Blame SOURCES/0190-Prepare-advise-plugin-for-smart-card-auth-configurat.patch

483b06
From f16d9533c7917a8a57a9148dee61df3b12a5e767 Mon Sep 17 00:00:00 2001
483b06
From: Martin Babinsky <mbabinsk@redhat.com>
483b06
Date: Fri, 2 Jun 2017 18:36:29 +0200
483b06
Subject: [PATCH] Prepare advise plugin for smart card auth configuration
483b06
483b06
The plugin contains recipes for configuring Smart Card authentication
483b06
on FreeIPA server and enrolled client.
483b06
483b06
https://www.freeipa.org/page/V4/Smartcard_authentication_ipa-advise_recipes
483b06
https://pagure.io/freeipa/issue/6982
483b06
483b06
Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
483b06
Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
483b06
---
483b06
 ipaserver/advise/plugins/smart_card_auth.py | 266 ++++++++++++++++++++++++++++
483b06
 1 file changed, 266 insertions(+)
483b06
 create mode 100644 ipaserver/advise/plugins/smart_card_auth.py
483b06
483b06
diff --git a/ipaserver/advise/plugins/smart_card_auth.py b/ipaserver/advise/plugins/smart_card_auth.py
483b06
new file mode 100644
483b06
index 0000000000000000000000000000000000000000..5859e350939fdba0a8b258de5285dd10c7b3bc23
483b06
--- /dev/null
483b06
+++ b/ipaserver/advise/plugins/smart_card_auth.py
483b06
@@ -0,0 +1,266 @@
483b06
+#
483b06
+# Copyright (C) 2017 FreeIPA Contributors see COPYING for license
483b06
+#
483b06
+
483b06
+from ipalib.plugable import Registry
483b06
+from ipaplatform.paths import paths
483b06
+from ipaserver.advise.base import Advice
483b06
+from ipaserver.install.httpinstance import NSS_OCSP_ENABLED
483b06
+
483b06
+register = Registry()
483b06
+
483b06
+
483b06
+@register()
483b06
+class config_server_for_smart_card_auth(Advice):
483b06
+    """
483b06
+    Configures smart card authentication via Kerberos (PKINIT) and for WebUI
483b06
+    """
483b06
+
483b06
+    description = ("Instructions for enabling Smart Card authentication on "
483b06
+                   " a single FreeIPA server. Includes Apache configuration, "
483b06
+                   "enabling PKINIT on KDC and configuring WebUI to accept "
483b06
+                   "Smart Card auth requests. To enable the feature in the "
483b06
+                   "whole topology you have to run the script on each master")
483b06
+
483b06
+    nss_conf = paths.HTTPD_NSS_CONF
483b06
+    nss_ocsp_directive = 'NSSOCSP'
483b06
+    nss_nickname_directive = 'NSSNickname'
483b06
+
483b06
+    def get_info(self):
483b06
+        self.log.exit_on_nonroot_euid()
483b06
+        self.check_ccache_not_empty()
483b06
+        self.check_hostname_is_in_masters()
483b06
+        self.resolve_ipaca_records()
483b06
+        self.enable_nss_ocsp()
483b06
+        self.mark_httpd_cert_as_trusted()
483b06
+        self.restart_httpd()
483b06
+        self.record_httpd_ocsp_status()
483b06
+        self.check_and_enable_pkinit()
483b06
+        self.enable_ok_to_auth_as_delegate_on_http_principal()
483b06
+
483b06
+    def check_ccache_not_empty(self):
483b06
+        self.log.comment('Check whether the credential cache is not empty')
483b06
+        self.log.exit_on_failed_command(
483b06
+            'klist',
483b06
+            [
483b06
+                "Credential cache is empty",
483b06
+                'Use kinit as privileged user to obtain Kerberos credentials'
483b06
+            ])
483b06
+
483b06
+    def check_hostname_is_in_masters(self):
483b06
+        self.log.comment('Check whether the host is IPA master')
483b06
+        self.log.exit_on_failed_command(
483b06
+            'ipa server-find $(hostname -f)',
483b06
+            ["This script can be run on IPA master only"])
483b06
+
483b06
+    def resolve_ipaca_records(self):
483b06
+        ipa_domain_name = self.api.env.domain
483b06
+
483b06
+        self.log.comment('make sure bind-utils are installed so that we can '
483b06
+                         'dig for ipa-ca records')
483b06
+        self.log.exit_on_failed_command(
483b06
+            'yum install -y bind-utils',
483b06
+            ['Failed to install bind-utils'])
483b06
+
483b06
+        self.log.comment('make sure ipa-ca records are resolvable, '
483b06
+                         'otherwise error out and instruct')
483b06
+        self.log.comment('the user to update the DNS infrastructure')
483b06
+        self.log.command('ipaca_records=$(dig +short '
483b06
+                         'ipa-ca.{})'.format(ipa_domain_name))
483b06
+
483b06
+        self.log.exit_on_predicate(
483b06
+            '[ -z "$ipaca_records" ]',
483b06
+            [
483b06
+                'Can not resolve ipa-ca records for ${domain_name}',
483b06
+                'Please make sure to update your DNS infrastructure with ',
483b06
+                'ipa-ca record pointing to IP addresses of IPA CA masters'
483b06
+            ])
483b06
+
483b06
+    def enable_nss_ocsp(self):
483b06
+        self.log.comment('look for the OCSP directive in nss.conf')
483b06
+        self.log.comment(' if it is present, switch it on')
483b06
+        self.log.comment(
483b06
+            'if it is absent, append it to the end of VirtualHost section')
483b06
+        predicate = self._interpolate_ocsp_directive_file_into_command(
483b06
+            "grep -q '{directive} ' {filename}")
483b06
+
483b06
+        self.log.commands_on_predicate(
483b06
+            predicate,
483b06
+            [
483b06
+                self._interpolate_ocsp_directive_file_into_command(
483b06
+                    "  sed -i.ipabkp -r "
483b06
+                    "'s/^#*[[:space:]]*{directive}[[:space:]]+(on|off)$"
483b06
+                    "/{directive} on/' {filename}")
483b06
+            ],
483b06
+            commands_to_run_when_false=[
483b06
+                self._interpolate_ocsp_directive_file_into_command(
483b06
+                    "  sed -i.ipabkp '/<\/VirtualHost>/i {directive} on' "
483b06
+                    "{filename}")
483b06
+            ]
483b06
+        )
483b06
+
483b06
+    def _interpolate_ocsp_directive_file_into_command(self, fmt_line):
483b06
+        return self._format_command(
483b06
+            fmt_line, self.nss_ocsp_directive, self.nss_conf)
483b06
+
483b06
+    def _format_command(self, fmt_line, directive, filename):
483b06
+        return fmt_line.format(directive=directive, filename=filename)
483b06
+
483b06
+    def mark_httpd_cert_as_trusted(self):
483b06
+        self.log.comment(
483b06
+            'mark the HTTP certificate as trusted peer to avoid '
483b06
+            'chicken-egg startup issue')
483b06
+        self.log.command(
483b06
+            self._interpolate_nssnickname_directive_file_into_command(
483b06
+                "http_cert_nick=$(grep '{directive}' {filename} |"
483b06
+                " cut -f 2 -d ' ')"))
483b06
+
483b06
+        self.log.exit_on_failed_command(
483b06
+            'certutil -M -n $http_cert_nick -d "{}" -t "Pu,u,u"'.format(
483b06
+                paths.HTTPD_ALIAS_DIR),
483b06
+            ['Can not set trust flags on HTTP certificate'])
483b06
+
483b06
+    def _interpolate_nssnickname_directive_file_into_command(self, fmt_line):
483b06
+        return self._format_command(
483b06
+            fmt_line, self.nss_nickname_directive, self.nss_conf)
483b06
+
483b06
+    def restart_httpd(self):
483b06
+        self.log.comment('finally restart apache')
483b06
+        self.log.command('systemctl restart httpd')
483b06
+
483b06
+    def record_httpd_ocsp_status(self):
483b06
+        self.log.comment('store the OCSP upgrade state')
483b06
+        self.log.command(
483b06
+            "python -c 'from ipaserver.install import sysupgrade; "
483b06
+            "sysupgrade.set_upgrade_state(\"httpd\", "
483b06
+            "\"{}\", True)'".format(NSS_OCSP_ENABLED))
483b06
+
483b06
+    def check_and_enable_pkinit(self):
483b06
+        self.log.comment('check whether PKINIT is configured on the master')
483b06
+        self.log.command(
483b06
+            "if ipa-pkinit-manage status | grep -q 'enabled'")
483b06
+        self.log.command('then')
483b06
+        self.log.command('  echo "PKINIT already enabled"')
483b06
+        self.log.command('else')
483b06
+        self.log.exit_on_failed_command(
483b06
+            'ipa-pkinit-manage enable',
483b06
+            ['Failed to issue PKINIT certificates to local KDC'],
483b06
+            indent_spaces=2)
483b06
+        self.log.command('fi')
483b06
+
483b06
+    def enable_ok_to_auth_as_delegate_on_http_principal(self):
483b06
+        self.log.comment('Enable OK-AS-DELEGATE flag on the HTTP principal')
483b06
+        self.log.comment('This enables smart card login to WebUI')
483b06
+        self.log.command(
483b06
+            'output=$(ipa service-mod HTTP/$(hostname -f) '
483b06
+            '--ok-to-auth-as-delegate=True 2>&1)')
483b06
+        self.log.exit_on_predicate(
483b06
+            '[ "$?" -ne "0" -a '
483b06
+            '-z "$(echo $output | grep \'no modifications\')" ]',
483b06
+            ["Failed to set OK_AS_AUTH_AS_DELEGATE flag on HTTP principal"]
483b06
+        )
483b06
+
483b06
+
483b06
+@register()
483b06
+class config_client_for_smart_card_auth(Advice):
483b06
+    """
483b06
+    Configures smart card authentication on FreeIPA client
483b06
+    """
483b06
+    smart_card_ca_cert_variable_name = "SC_CA_CERT"
483b06
+
483b06
+    description = ("Instructions for enabling Smart Card authentication on "
483b06
+                   " a single FreeIPA client. Configures Smart Card daemon, "
483b06
+                   "set the system-wide trust store and configures SSSD to "
483b06
+                   "allow smart card logins to desktop")
483b06
+
483b06
+    opensc_module_name = "OpenSC"
483b06
+    pkcs11_shared_lib = '/usr/lib64/opensc-pkcs11.so'
483b06
+    smart_card_service_file = 'pcscd.service'
483b06
+    smart_card_socket = 'pcscd.socket'
483b06
+    systemwide_nssdb = paths.NSS_DB_DIR
483b06
+
483b06
+    def get_info(self):
483b06
+        self.log.exit_on_nonroot_euid()
483b06
+        self.check_and_set_ca_cert_path()
483b06
+        self.check_and_remove_pam_pkcs11()
483b06
+        self.install_opensc_and_dconf_packages()
483b06
+        self.start_enable_smartcard_daemon()
483b06
+        self.add_pkcs11_module_to_systemwide_db()
483b06
+        self.upload_smartcard_ca_certificate_to_systemwide_db()
483b06
+        self.run_authconfig_to_configure_smart_card_auth()
483b06
+        self.restart_sssd()
483b06
+
483b06
+    def check_and_set_ca_cert_path(self):
483b06
+        ca_path_variable = self.smart_card_ca_cert_variable_name
483b06
+        self.log.command("{}=$1".format(ca_path_variable))
483b06
+        self.log.exit_on_predicate(
483b06
+            '[ -z "${}" ]'.format(ca_path_variable),
483b06
+            ['You need to provide the path to the PEM file containing CA '
483b06
+             'signing the Smart Cards']
483b06
+        )
483b06
+        self.log.exit_on_predicate(
483b06
+            '[ ! -f "${}" ]'.format(ca_path_variable),
483b06
+            ['Invalid CA certificate filename: ${}'.format(ca_path_variable),
483b06
+             'Please check that the path exists and is a valid file']
483b06
+        )
483b06
+
483b06
+    def check_and_remove_pam_pkcs11(self):
483b06
+        self.log.command('rpm -qi pam_pkcs11 > /dev/null')
483b06
+        self.log.commands_on_predicate(
483b06
+            '[ "$?" -eq "0" ]',
483b06
+            [
483b06
+                'yum remove -y pam_pkcs11'
483b06
+            ]
483b06
+        )
483b06
+
483b06
+    def install_opensc_and_dconf_packages(self):
483b06
+        self.log.comment(
483b06
+            'authconfig often complains about missing dconf, '
483b06
+            'install it explicitly')
483b06
+        self.log.exit_on_failed_command(
483b06
+            'yum install -y {} dconf'.format(self.opensc_module_name.lower()),
483b06
+            ['Could not install OpenSC package']
483b06
+        )
483b06
+
483b06
+    def start_enable_smartcard_daemon(self):
483b06
+        self.log.command(
483b06
+            'systemctl start {service} {socket} '
483b06
+            '&& systemctl enable {service} {socket}'.format(
483b06
+                service=self.smart_card_service_file,
483b06
+                socket=self.smart_card_socket))
483b06
+
483b06
+    def add_pkcs11_module_to_systemwide_db(self):
483b06
+        module_name = self.opensc_module_name
483b06
+        nssdb = self.systemwide_nssdb
483b06
+        shared_lib = self.pkcs11_shared_lib
483b06
+
483b06
+        self.log.commands_on_predicate(
483b06
+            'modutil -dbdir {} -list | grep -q {}'.format(
483b06
+                nssdb, module_name),
483b06
+            [
483b06
+                'echo "{} PKCS#11 module already configured"'.format(
483b06
+                    module_name)
483b06
+            ],
483b06
+            commands_to_run_when_false=[
483b06
+                'echo "" | modutil -dbdir {} -add "{}" -libfile {}'.format(
483b06
+                    nssdb, module_name, shared_lib),
483b06
+            ]
483b06
+        )
483b06
+
483b06
+    def upload_smartcard_ca_certificate_to_systemwide_db(self):
483b06
+        self.log.command(
483b06
+            'certutil -d {} -A -i ${} -n "Smart Card CA" -t CT,C,C'.format(
483b06
+                self.systemwide_nssdb, self.smart_card_ca_cert_variable_name
483b06
+            )
483b06
+        )
483b06
+
483b06
+    def run_authconfig_to_configure_smart_card_auth(self):
483b06
+        self.log.exit_on_failed_command(
483b06
+            'authconfig --enablesmartcard --smartcardmodule=sssd --updateall',
483b06
+            [
483b06
+                'Failed to configure Smart Card authentication in SSSD'
483b06
+            ]
483b06
+        )
483b06
+
483b06
+    def restart_sssd(self):
483b06
+        self.log.command('systemctl restart sssd.service')
483b06
-- 
483b06
2.9.4
483b06