483b06
From d224655e4b1e218bac19dff5a10bf3e0d83edcb0 Mon Sep 17 00:00:00 2001
483b06
From: Jan Cholasta <jcholast@redhat.com>
483b06
Date: Mon, 5 Jun 2017 12:41:02 +0000
483b06
Subject: [PATCH] pkinit manage: introduce ipa-pkinit-manage
483b06
483b06
Add the ipa-pkinit-manage tool to allow enabling / disabling PKINIT after
483b06
the initial server install.
483b06
483b06
https://pagure.io/freeipa/issue/7000
483b06
483b06
Reviewed-By: Martin Babinsky <mbabinsk@redhat.com>
483b06
---
483b06
 freeipa.spec.in                        |  2 +
483b06
 install/tools/Makefile.am              |  1 +
483b06
 install/tools/ipa-pkinit-manage        |  8 +++
483b06
 install/tools/man/Makefile.am          |  1 +
483b06
 install/tools/man/ipa-pkinit-manage.1  | 34 +++++++++++++
483b06
 ipaserver/install/ipa_pkinit_manage.py | 93 ++++++++++++++++++++++++++++++++++
483b06
 ipaserver/install/krbinstance.py       | 24 +++++++++
483b06
 7 files changed, 163 insertions(+)
483b06
 create mode 100755 install/tools/ipa-pkinit-manage
483b06
 create mode 100644 install/tools/man/ipa-pkinit-manage.1
483b06
 create mode 100644 ipaserver/install/ipa_pkinit_manage.py
483b06
483b06
diff --git a/freeipa.spec.in b/freeipa.spec.in
483b06
index 2cbaa60df0db021a4a1ce10af383cd6a15e1e57c..ae77a9a23645c1490c32195203e2c4f665783a80 100644
483b06
--- a/freeipa.spec.in
483b06
+++ b/freeipa.spec.in
483b06
@@ -1184,6 +1184,7 @@ fi
483b06
 %{_sbindir}/ipa-advise
483b06
 %{_sbindir}/ipa-cacert-manage
483b06
 %{_sbindir}/ipa-winsync-migrate
483b06
+%{_sbindir}/ipa-pkinit-manage
483b06
 %{_libexecdir}/certmonger/dogtag-ipa-ca-renew-agent-submit
483b06
 %{_libexecdir}/certmonger/ipa-server-guard
483b06
 %dir %{_libexecdir}/ipa
483b06
@@ -1247,6 +1248,7 @@ fi
483b06
 %{_mandir}/man1/ipa-otptoken-import.1*
483b06
 %{_mandir}/man1/ipa-cacert-manage.1*
483b06
 %{_mandir}/man1/ipa-winsync-migrate.1*
483b06
+%{_mandir}/man1/ipa-pkinit-manage.1*
483b06
 
483b06
 
483b06
 %files -n python2-ipaserver
483b06
diff --git a/install/tools/Makefile.am b/install/tools/Makefile.am
483b06
index 493e5ff4a8290be8ef076135104a85f8315b7842..47ecc14d7320336315c16587956c4965387853d9 100644
483b06
--- a/install/tools/Makefile.am
483b06
+++ b/install/tools/Makefile.am
483b06
@@ -28,6 +28,7 @@ dist_sbin_SCRIPTS =		\
483b06
 	ipa-advise		\
483b06
 	ipa-cacert-manage	\
483b06
 	ipa-winsync-migrate	\
483b06
+	ipa-pkinit-manage	\
483b06
 	$(NULL)
483b06
 
483b06
 appdir = $(libexecdir)/ipa/
483b06
diff --git a/install/tools/ipa-pkinit-manage b/install/tools/ipa-pkinit-manage
483b06
new file mode 100755
483b06
index 0000000000000000000000000000000000000000..5b2413bd7cdc97632f82a77e18f3424a2ff63309
483b06
--- /dev/null
483b06
+++ b/install/tools/ipa-pkinit-manage
483b06
@@ -0,0 +1,8 @@
483b06
+#! /usr/bin/python2 -E
483b06
+#
483b06
+# Copyright (C) 2017  FreeIPA Contributors see COPYING for license
483b06
+#
483b06
+
483b06
+from ipaserver.install.ipa_pkinit_manage import PKINITManage
483b06
+
483b06
+PKINITManage.run_cli()
483b06
diff --git a/install/tools/man/Makefile.am b/install/tools/man/Makefile.am
483b06
index 0d06ec7306b0cc1e656dac244bcb2c480b0ae61e..2dac9ac716352847aeb0d1fd3c6375ede956c751 100644
483b06
--- a/install/tools/man/Makefile.am
483b06
+++ b/install/tools/man/Makefile.am
483b06
@@ -27,6 +27,7 @@ dist_man1_MANS = 			\
483b06
 	ipa-otptoken-import.1		\
483b06
 	ipa-cacert-manage.1		\
483b06
 	ipa-winsync-migrate.1		\
483b06
+	ipa-pkinit-manage.1		\
483b06
         $(NULL)
483b06
 
483b06
 dist_man8_MANS =			\
483b06
diff --git a/install/tools/man/ipa-pkinit-manage.1 b/install/tools/man/ipa-pkinit-manage.1
483b06
new file mode 100644
483b06
index 0000000000000000000000000000000000000000..5018ce8aa3f89470453d9cfc590a0c5f44f78f3c
483b06
--- /dev/null
483b06
+++ b/install/tools/man/ipa-pkinit-manage.1
483b06
@@ -0,0 +1,34 @@
483b06
+.\"
483b06
+.\" Copyright (C) 2017  FreeIPA Contributors see COPYING for license
483b06
+.\"
483b06
+.TH "ipa-pkinit-manage" "1" "Jun 05 2017" "FreeIPA" "FreeIPA Manual Pages"
483b06
+.SH "NAME"
483b06
+ipa\-pkinit\-manage \- Enables or disables PKINIT
483b06
+.SH "SYNOPSIS"
483b06
+ipa\-pkinit\-manage [options] <enable|disable|status>
483b06
+.SH "DESCRIPTION"
483b06
+Run the command with the \fBenable\fR option to enable PKINIT.
483b06
+
483b06
+Run the command with the \fBdisable\fR option to disable PKINIT.
483b06
+
483b06
+Run the command with the \fBstatus\fR to determine the current status of PKINIT.
483b06
+.SH "OPTIONS"
483b06
+.TP
483b06
+\fB\-\-version\fR
483b06
+Show the program's version and exit.
483b06
+.TP
483b06
+\fB\-h\fR, \fB\-\-help\fR
483b06
+Show the help for this program.
483b06
+.TP
483b06
+\fB\-v\fR, \fB\-\-verbose\fR
483b06
+Print debugging information.
483b06
+.TP
483b06
+\fB\-q\fR, \fB\-\-quiet\fR
483b06
+Output only errors.
483b06
+.TP
483b06
+\fB\-\-log\-file\fR=\fIFILE\fR
483b06
+Log to the given file.
483b06
+.SH "EXIT STATUS"
483b06
+0 if the command was successful
483b06
+
483b06
+1 if an error occurred
483b06
diff --git a/ipaserver/install/ipa_pkinit_manage.py b/ipaserver/install/ipa_pkinit_manage.py
483b06
new file mode 100644
483b06
index 0000000000000000000000000000000000000000..428c0e3476b4dbd13a9ee5a40a42447f9fa95f2d
483b06
--- /dev/null
483b06
+++ b/ipaserver/install/ipa_pkinit_manage.py
483b06
@@ -0,0 +1,93 @@
483b06
+#
483b06
+# Copyright (C) 2017  FreeIPA Contributors see COPYING for license
483b06
+#
483b06
+
483b06
+from __future__ import print_function
483b06
+
483b06
+from ipalib import api
483b06
+from ipaplatform.paths import paths
483b06
+from ipapython.admintool import AdminTool
483b06
+from ipaserver.install.krbinstance import KrbInstance, is_pkinit_enabled
483b06
+
483b06
+
483b06
+class PKINITManage(AdminTool):
483b06
+    command_name = "ipa-pkinit-manage"
483b06
+    usage = "%prog <enable|disable|status>"
483b06
+    description = "Manage PKINIT."
483b06
+
483b06
+    def validate_options(self):
483b06
+        super(PKINITManage, self).validate_options(needs_root=True)
483b06
+
483b06
+        option_parser = self.option_parser
483b06
+
483b06
+        if not self.args:
483b06
+            option_parser.error("action not specified")
483b06
+        elif len(self.args) > 1:
483b06
+            option_parser.error("too many arguments")
483b06
+
483b06
+        action = self.args[0]
483b06
+        if action not in {'enable', 'disable', 'status'}:
483b06
+            option_parser.error("unrecognized action '{}'".format(action))
483b06
+
483b06
+    def run(self):
483b06
+        api.bootstrap(in_server=True, confdir=paths.ETC_IPA)
483b06
+        api.finalize()
483b06
+
483b06
+        api.Backend.ldap2.connect()
483b06
+        try:
483b06
+            action = self.args[0]
483b06
+            if action == 'enable':
483b06
+                self.enable()
483b06
+            elif action == 'disable':
483b06
+                self.disable()
483b06
+            elif action == 'status':
483b06
+                self.status()
483b06
+        finally:
483b06
+            api.Backend.ldap2.disconnect()
483b06
+
483b06
+        return 0
483b06
+
483b06
+    def _setup(self, setup_pkinit):
483b06
+        config = api.Command.config_show()['result']
483b06
+        ca_enabled = api.Command.ca_is_enabled()['result']
483b06
+
483b06
+        krb = KrbInstance()
483b06
+        krb.init_info(
483b06
+            realm_name=api.env.realm,
483b06
+            host_name=api.env.host,
483b06
+            setup_pkinit=setup_pkinit,
483b06
+            subject_base=config['ipacertificatesubjectbase'][0],
483b06
+        )
483b06
+
483b06
+        if bool(is_pkinit_enabled()) is not bool(setup_pkinit):
483b06
+            try:
483b06
+                krb.stop_tracking_certs()
483b06
+            except RuntimeError as e:
483b06
+                if ca_enabled:
483b06
+                    self.log.warning(
483b06
+                        "Failed to stop tracking certificates: %s", e)
483b06
+
483b06
+            krb.enable_ssl()
483b06
+
483b06
+        if setup_pkinit:
483b06
+            krb.pkinit_enable()
483b06
+        else:
483b06
+            krb.pkinit_disable()
483b06
+
483b06
+    def enable(self):
483b06
+        if not api.Command.ca_is_enabled()['result']:
483b06
+            self.log.error("Cannot enable PKINIT in CA-less deployment")
483b06
+            self.log.error("Use ipa-server-certinstall to install KDC "
483b06
+                           "certificate manually")
483b06
+            raise RuntimeError("Cannot enable PKINIT in CA-less deployment")
483b06
+
483b06
+        self._setup(True)
483b06
+
483b06
+    def disable(self):
483b06
+        self._setup(False)
483b06
+
483b06
+    def status(self):
483b06
+        if is_pkinit_enabled():
483b06
+            print("PKINIT is enabled")
483b06
+        else:
483b06
+            print("PKINIT is disabled")
483b06
diff --git a/ipaserver/install/krbinstance.py b/ipaserver/install/krbinstance.py
483b06
index a1053d55ccaae17bef93547c036fb9d08d296f0b..6b51e65d1ec985bfc01f167aea3fe3ca11c7ec29 100644
483b06
--- a/ipaserver/install/krbinstance.py
483b06
+++ b/ipaserver/install/krbinstance.py
483b06
@@ -451,6 +451,30 @@ class KrbInstance(service.Service):
483b06
         service.set_service_entry_config(
483b06
             'KDC', self.fqdn, [PKINIT_ENABLED], self.suffix)
483b06
 
483b06
+    def pkinit_disable(self):
483b06
+        """
483b06
+        unadvertise enabled PKINIT feature in master's KDC entry in LDAP
483b06
+        """
483b06
+        ldap = api.Backend.ldap2
483b06
+        dn = DN(('cn', 'KDC'),
483b06
+                ('cn', self.fqdn),
483b06
+                ('cn', 'masters'),
483b06
+                ('cn', 'ipa'),
483b06
+                ('cn', 'etc'),
483b06
+                self.suffix)
483b06
+
483b06
+        entry = ldap.get_entry(dn, ['ipaConfigString'])
483b06
+
483b06
+        config = entry.setdefault('ipaConfigString', [])
483b06
+        config = [value for value in config
483b06
+                  if value.lower() != PKINIT_ENABLED.lower()]
483b06
+        entry['ipaConfigString'][:] = config
483b06
+
483b06
+        try:
483b06
+            ldap.update_entry(entry)
483b06
+        except errors.EmptyModlist:
483b06
+            pass
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
-- 
483b06
2.9.4
483b06