2013-11-27 07:53:57 -06:00
|
|
|
#!/usr/bin/python2 -E
|
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
|
|
|
|
2015-11-09 11:28:47 -06:00
|
|
|
from ipapython import ipautil
|
2014-06-12 04:29:57 -05:00
|
|
|
from ipalib import api, errors, x509, certstore
|
2013-10-16 03:40:31 -05:00
|
|
|
from ipaserver.install import certs, cainstance, installutils
|
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
|
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
|
|
|
|
2016-06-16 23:18:05 -05:00
|
|
|
api.bootstrap(in_server=True, context='restart')
|
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
|
|
|
|
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
|
2015-11-09 11:28:47 -06:00
|
|
|
db = certs.CertDB(api.env.realm, nssdir=paths.PKI_TOMCAT_ALIAS_DIR)
|
2014-01-23 08:33:26 -06:00
|
|
|
cert = db.get_cert_from_db(nickname, pem=False)
|
|
|
|
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')
|
|
|
|
ipautil.kinit_keytab(principal, paths.KRB5_KEYTAB, ccache_filename)
|
|
|
|
os.environ['KRB5CCNAME'] = ccache_filename
|
2014-03-12 05:32:59 -05:00
|
|
|
|
2014-07-18 04:01:13 -05:00
|
|
|
ca = cainstance.CAInstance(host_name=api.env.host, ldapi=False)
|
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
|
|
|
|
2014-07-18 04:01:13 -05:00
|
|
|
if nickname == 'auditSigningCert cert-pki-ca':
|
|
|
|
# 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':
|
|
|
|
# Update CS.cfg
|
2015-11-09 11:28:47 -06:00
|
|
|
cfg_path = paths.CA_CS_CFG_PATH
|
2014-06-13 07:48:12 -05:00
|
|
|
config = installutils.get_directive(
|
|
|
|
cfg_path, 'subsystem.select', '=')
|
|
|
|
if config == 'New':
|
|
|
|
syslog.syslog(syslog.LOG_NOTICE, "Updating CS.cfg")
|
|
|
|
if x509.is_self_signed(cert, x509.DER):
|
|
|
|
installutils.set_directive(
|
|
|
|
cfg_path, 'hierarchy.select', 'Root',
|
|
|
|
quotes=False, separator='=')
|
|
|
|
installutils.set_directive(
|
|
|
|
cfg_path, 'subsystem.count', '1',
|
|
|
|
quotes=False, separator='=')
|
|
|
|
else:
|
|
|
|
installutils.set_directive(
|
|
|
|
cfg_path, 'hierarchy.select', 'Subordinate',
|
|
|
|
quotes=False, separator='=')
|
|
|
|
installutils.set_directive(
|
|
|
|
cfg_path, 'subsystem.count', '0',
|
|
|
|
quotes=False, separator='=')
|
|
|
|
else:
|
|
|
|
syslog.syslog(syslog.LOG_NOTICE, "Not updating CS.cfg")
|
2014-03-12 05:32:59 -05:00
|
|
|
|
2014-07-23 06:25:22 -05:00
|
|
|
# Remove old external CA certificates
|
|
|
|
for ca_nick, ca_flags in db.list_certs():
|
|
|
|
if 'u' in ca_flags:
|
|
|
|
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 = []
|
|
|
|
|
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:
|
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]
|
|
|
|
db.trust_root_cert(ca_nick, 'C' + ca_flags)
|
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:
|
|
|
|
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())
|