2019-06-21 05:05:19 -05:00
|
|
|
#!/usr/bin/python3
|
2012-07-11 14:51:01 -05:00
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Rob Crittenden <rcritten@redhat.com>
|
2013-10-16 03:40:31 -05:00
|
|
|
# Jan Cholasta <jcholast@redhat.com>
|
2012-07-11 14:51:01 -05:00
|
|
|
#
|
2013-10-16 03:40:31 -05:00
|
|
|
# Copyright (C) 2013 Red Hat
|
2012-07-11 14:51:01 -05:00
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import sys
|
2015-03-16 10:43:10 -05:00
|
|
|
import os
|
2012-07-11 14:51:01 -05:00
|
|
|
import syslog
|
2013-10-16 03:40:31 -05:00
|
|
|
import tempfile
|
|
|
|
import shutil
|
2014-01-23 08:33:26 -06:00
|
|
|
import traceback
|
2013-10-16 03:40:31 -05:00
|
|
|
|
2016-11-23 08:40:21 -06:00
|
|
|
from ipalib.install import certstore
|
2015-11-09 11:28:47 -06:00
|
|
|
from ipapython import ipautil
|
2017-06-16 03:18:07 -05:00
|
|
|
from ipalib import api, errors
|
2017-08-18 11:02:57 -05:00
|
|
|
from ipalib import x509
|
2016-11-23 10:40:47 -06:00
|
|
|
from ipalib.install.kinit import kinit_keytab
|
2018-05-23 03:37:58 -05:00
|
|
|
from ipaserver.install import certs, cainstance
|
2012-07-11 14:51:01 -05:00
|
|
|
from ipaserver.plugins.ldap2 import ldap2
|
2014-05-29 03:37:18 -05:00
|
|
|
from ipaplatform import services
|
2014-03-12 05:32:59 -05:00
|
|
|
from ipaplatform.paths import paths
|
2023-01-10 16:12:32 -06:00
|
|
|
from ipapython.certdb import TrustFlags, get_ca_nickname
|
2014-08-08 15:09:42 -05:00
|
|
|
|
2014-03-18 10:23:30 -05:00
|
|
|
|
2015-01-08 03:06:46 -06:00
|
|
|
def _main():
|
2014-01-23 08:33:26 -06:00
|
|
|
nickname = sys.argv[1]
|
2012-07-11 14:51:01 -05:00
|
|
|
|
2019-09-23 11:23:04 -05:00
|
|
|
api.bootstrap(
|
|
|
|
in_server=True, context='restart', confdir=paths.ETC_IPA, log=None
|
|
|
|
)
|
2014-01-23 08:33:26 -06:00
|
|
|
api.finalize()
|
2012-07-11 14:51:01 -05:00
|
|
|
|
2015-11-09 11:28:47 -06:00
|
|
|
dogtag_service = services.knownservices['pki_tomcatd']
|
2012-08-15 21:53:51 -05:00
|
|
|
|
2022-12-09 20:44:43 -06:00
|
|
|
ca = cainstance.CAInstance(host_name=api.env.host)
|
|
|
|
|
2013-10-16 04:00:44 -05:00
|
|
|
# dogtag opens its NSS database in read/write mode so we need it
|
|
|
|
# shut down so certmonger can open it read/write mode. This avoids
|
|
|
|
# database corruption. It should already be stopped by the pre-command
|
|
|
|
# but lets be sure.
|
2015-11-09 11:28:47 -06:00
|
|
|
if dogtag_service.is_running('pki-tomcat'):
|
2013-10-16 04:00:44 -05:00
|
|
|
syslog.syslog(
|
|
|
|
syslog.LOG_NOTICE, "Stopping %s" % dogtag_service.service_name)
|
|
|
|
try:
|
2015-11-09 11:28:47 -06:00
|
|
|
dogtag_service.stop('pki-tomcat')
|
2015-07-30 09:49:29 -05:00
|
|
|
except Exception as e:
|
2013-10-16 04:00:44 -05:00
|
|
|
syslog.syslog(
|
|
|
|
syslog.LOG_ERR,
|
|
|
|
"Cannot stop %s: %s" % (dogtag_service.service_name, e))
|
|
|
|
else:
|
|
|
|
syslog.syslog(
|
|
|
|
syslog.LOG_NOTICE, "Stopped %s" % dogtag_service.service_name)
|
|
|
|
|
2014-01-23 08:33:26 -06:00
|
|
|
# Fetch the new certificate
|
2023-01-10 16:12:32 -06:00
|
|
|
db = certs.CertDB(api.env.realm, nssdir=paths.PKI_TOMCAT_ALIAS_DIR)
|
2017-06-16 03:18:07 -05:00
|
|
|
cert = db.get_cert_from_db(nickname)
|
2014-01-23 08:33:26 -06:00
|
|
|
if not cert:
|
|
|
|
syslog.syslog(syslog.LOG_ERR, 'No certificate %s found.' % nickname)
|
|
|
|
sys.exit(1)
|
2012-07-11 14:51:01 -05:00
|
|
|
|
2014-07-18 04:01:13 -05:00
|
|
|
tmpdir = tempfile.mkdtemp(prefix="tmp-")
|
|
|
|
try:
|
|
|
|
principal = str('host/%s@%s' % (api.env.host, api.env.realm))
|
2015-03-16 10:43:10 -05:00
|
|
|
ccache_filename = os.path.join(tmpdir, 'ccache')
|
2016-11-23 10:40:47 -06:00
|
|
|
kinit_keytab(principal, paths.KRB5_KEYTAB, ccache_filename)
|
2015-03-16 10:43:10 -05:00
|
|
|
os.environ['KRB5CCNAME'] = ccache_filename
|
2014-03-12 05:32:59 -05:00
|
|
|
|
2017-04-07 00:51:01 -05:00
|
|
|
api.Backend.ldap2.connect()
|
|
|
|
|
2015-11-09 11:28:47 -06:00
|
|
|
ca.update_cert_config(nickname, cert)
|
2014-07-18 04:01:13 -05:00
|
|
|
if ca.is_renewal_master():
|
|
|
|
cainstance.update_people_entry(cert)
|
2016-06-17 00:11:08 -05:00
|
|
|
cainstance.update_authority_entry(cert)
|
2014-03-12 05:32:59 -05:00
|
|
|
|
2023-03-15 09:34:15 -05:00
|
|
|
if nickname in (
|
|
|
|
'auditSigningCert cert-pki-ca',
|
|
|
|
'auditSigningCert cert-pki-kra',
|
|
|
|
):
|
2014-07-18 04:01:13 -05:00
|
|
|
# Fix trust on the audit cert
|
|
|
|
try:
|
|
|
|
db.run_certutil(['-M',
|
|
|
|
'-n', nickname,
|
|
|
|
'-t', 'u,u,Pu'])
|
|
|
|
syslog.syslog(
|
|
|
|
syslog.LOG_NOTICE,
|
|
|
|
"Updated trust on certificate %s in %s" %
|
|
|
|
(nickname, db.secdir))
|
|
|
|
except ipautil.CalledProcessError:
|
|
|
|
syslog.syslog(
|
|
|
|
syslog.LOG_ERR,
|
|
|
|
"Updating trust on certificate %s failed in %s" %
|
|
|
|
(nickname, db.secdir))
|
2014-06-13 07:48:12 -05:00
|
|
|
elif nickname == 'caSigningCert cert-pki-ca':
|
2014-07-23 06:25:22 -05:00
|
|
|
# Remove old external CA certificates
|
|
|
|
for ca_nick, ca_flags in db.list_certs():
|
2023-01-10 16:12:32 -06:00
|
|
|
if ca_flags.has_key or not ca_flags.ca:
|
2014-07-23 06:25:22 -05:00
|
|
|
continue
|
|
|
|
# Delete *all* certificates that use the nickname
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
db.delete_cert(ca_nick)
|
|
|
|
except ipautil.CalledProcessError:
|
|
|
|
syslog.syslog(
|
|
|
|
syslog.LOG_ERR,
|
|
|
|
"Failed to remove certificate %s" % ca_nick)
|
|
|
|
break
|
|
|
|
if not db.has_nickname(ca_nick):
|
|
|
|
break
|
|
|
|
|
|
|
|
conn = None
|
|
|
|
try:
|
2015-06-22 05:58:43 -05:00
|
|
|
conn = ldap2(api)
|
2015-03-16 10:43:10 -05:00
|
|
|
conn.connect(ccache=ccache_filename)
|
2015-07-30 09:49:29 -05:00
|
|
|
except Exception as e:
|
2014-07-23 06:25:22 -05:00
|
|
|
syslog.syslog(
|
|
|
|
syslog.LOG_ERR, "Failed to connect to LDAP: %s" % e)
|
|
|
|
else:
|
|
|
|
# Update CA certificate in LDAP
|
|
|
|
if ca.is_renewal_master():
|
2014-06-13 07:48:12 -05:00
|
|
|
try:
|
|
|
|
certstore.update_ca_cert(conn, api.env.basedn, cert)
|
|
|
|
except errors.EmptyModlist:
|
|
|
|
pass
|
2015-07-30 09:49:29 -05:00
|
|
|
except Exception as e:
|
2014-07-23 06:25:22 -05:00
|
|
|
syslog.syslog(
|
|
|
|
syslog.LOG_ERR,
|
|
|
|
"Updating CA certificate failed: %s" % e)
|
2014-06-13 07:48:12 -05:00
|
|
|
|
2014-07-23 06:25:22 -05:00
|
|
|
# Add external CA certificates
|
|
|
|
try:
|
2016-01-21 01:58:56 -06:00
|
|
|
ca_certs = certstore.get_ca_certs_nss(
|
|
|
|
conn, api.env.basedn, api.env.realm, False)
|
2015-07-30 09:49:29 -05:00
|
|
|
except Exception as e:
|
2014-06-13 07:48:12 -05:00
|
|
|
syslog.syslog(
|
|
|
|
syslog.LOG_ERR,
|
2014-07-23 06:25:22 -05:00
|
|
|
"Failed to get external CA certificates from LDAP: "
|
|
|
|
"%s" % e)
|
|
|
|
ca_certs = []
|
|
|
|
|
2023-01-10 16:12:32 -06:00
|
|
|
realm_nickname = get_ca_nickname(api.env.realm)
|
2016-01-21 01:58:56 -06:00
|
|
|
for ca_cert, ca_nick, ca_flags in ca_certs:
|
2014-07-23 06:25:22 -05:00
|
|
|
try:
|
2023-01-10 16:12:32 -06:00
|
|
|
if ca_nick == realm_nickname:
|
|
|
|
ca_nick = 'caSigningCert cert-pki-ca'
|
2016-01-21 01:58:56 -06:00
|
|
|
db.add_cert(ca_cert, ca_nick, ca_flags)
|
2015-07-30 09:49:29 -05:00
|
|
|
except ipautil.CalledProcessError as e:
|
2014-07-23 06:25:22 -05:00
|
|
|
syslog.syslog(
|
|
|
|
syslog.LOG_ERR,
|
|
|
|
"Failed to add certificate %s" % ca_nick)
|
2016-01-21 01:58:56 -06:00
|
|
|
|
|
|
|
# Pass Dogtag's self-tests
|
|
|
|
for ca_nick in db.find_root_cert(nickname)[-2:-1]:
|
|
|
|
ca_flags = dict(cc[1:] for cc in ca_certs)[ca_nick]
|
2017-08-18 11:02:57 -05:00
|
|
|
usages = ca_flags.usages or set()
|
|
|
|
ca_flags_modified = TrustFlags(ca_flags.has_key,
|
|
|
|
True, True,
|
|
|
|
usages | {x509.EKU_SERVER_AUTH})
|
|
|
|
db.trust_root_cert(ca_nick, ca_flags_modified)
|
2014-07-23 06:25:22 -05:00
|
|
|
finally:
|
|
|
|
if conn is not None and conn.isconnected():
|
|
|
|
conn.disconnect()
|
2014-07-18 04:01:13 -05:00
|
|
|
finally:
|
2017-04-07 00:51:01 -05:00
|
|
|
if api.Backend.ldap2.isconnected():
|
|
|
|
api.Backend.ldap2.disconnect()
|
2014-07-18 04:01:13 -05:00
|
|
|
shutil.rmtree(tmpdir)
|
2014-01-23 08:33:26 -06:00
|
|
|
|
2014-05-29 03:37:18 -05:00
|
|
|
# Now we can start the CA. Using the services start should fire
|
2014-01-23 08:33:26 -06:00
|
|
|
# off the servlet to verify that the CA is actually up and responding so
|
|
|
|
# when this returns it should be good-to-go. The CA was stopped in the
|
|
|
|
# pre-save state.
|
2014-03-18 10:23:30 -05:00
|
|
|
syslog.syslog(
|
|
|
|
syslog.LOG_NOTICE,
|
|
|
|
'Starting %s' % dogtag_service.service_name)
|
2012-07-11 14:51:01 -05:00
|
|
|
try:
|
2015-11-09 11:28:47 -06:00
|
|
|
dogtag_service.start('pki-tomcat')
|
2015-07-30 09:49:29 -05:00
|
|
|
except Exception as e:
|
2014-01-23 08:33:26 -06:00
|
|
|
syslog.syslog(
|
|
|
|
syslog.LOG_ERR,
|
|
|
|
"Cannot start %s: %s" % (dogtag_service.service_name, e))
|
|
|
|
else:
|
|
|
|
syslog.syslog(
|
|
|
|
syslog.LOG_NOTICE, "Started %s" % dogtag_service.service_name)
|
2012-07-11 14:51:01 -05:00
|
|
|
|
2015-01-08 03:06:46 -06:00
|
|
|
|
|
|
|
def main():
|
|
|
|
try:
|
|
|
|
_main()
|
|
|
|
finally:
|
|
|
|
certs.renewal_lock.release('renew_ca_cert')
|
|
|
|
|
|
|
|
|
2012-07-11 14:51:01 -05:00
|
|
|
try:
|
2014-01-23 08:33:26 -06:00
|
|
|
main()
|
|
|
|
except Exception:
|
|
|
|
syslog.syslog(syslog.LOG_ERR, traceback.format_exc())
|