2015-06-08 00:23:56 -05:00
|
|
|
#
|
|
|
|
# Copyright (C) 2015 FreeIPA Contributors see COPYING for license
|
|
|
|
#
|
|
|
|
|
2015-08-12 06:44:11 -05:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2016-10-24 07:33:19 -05:00
|
|
|
import os.path
|
|
|
|
|
|
|
|
from ipaserver.install import cainstance, custodiainstance, dsinstance, bindinstance
|
2015-11-09 11:28:47 -06:00
|
|
|
from ipapython import ipautil, certdb
|
2016-06-17 06:14:49 -05:00
|
|
|
from ipapython.admintool import ScriptError
|
2015-06-08 00:23:56 -05:00
|
|
|
from ipaplatform import services
|
|
|
|
from ipaplatform.paths import paths
|
|
|
|
from ipaserver.install import installutils, certs
|
|
|
|
from ipaserver.install.replication import replica_conn_check
|
|
|
|
from ipalib import api, certstore, x509
|
|
|
|
from ipapython.dn import DN
|
|
|
|
from ipapython.ipa_log_manager import root_logger
|
|
|
|
|
2016-03-17 12:40:04 -05:00
|
|
|
external_cert_file = None
|
|
|
|
external_ca_file = None
|
|
|
|
|
2015-06-08 00:23:56 -05:00
|
|
|
|
|
|
|
def install_check(standalone, replica_config, options):
|
|
|
|
global external_cert_file
|
|
|
|
global external_ca_file
|
|
|
|
|
2016-10-26 07:28:21 -05:00
|
|
|
if replica_config is not None and not replica_config.setup_ca:
|
|
|
|
return
|
|
|
|
|
2015-06-08 00:23:56 -05:00
|
|
|
realm_name = options.realm_name
|
|
|
|
host_name = options.host_name
|
|
|
|
subject_base = options.subject
|
|
|
|
|
|
|
|
if replica_config is not None:
|
|
|
|
if standalone and api.env.ra_plugin == 'selfsign':
|
2016-06-17 06:14:49 -05:00
|
|
|
raise ScriptError('A selfsign CA can not be added')
|
2015-06-08 00:23:56 -05:00
|
|
|
|
2016-10-24 07:33:19 -05:00
|
|
|
cafile = os.path.join(replica_config.dir, 'cacert.p12')
|
|
|
|
if not options.promote and not ipautil.file_exists(cafile):
|
2016-06-17 06:14:49 -05:00
|
|
|
raise ScriptError('CA cannot be installed in CA-less setup.')
|
2015-06-08 00:23:56 -05:00
|
|
|
|
|
|
|
if standalone and not options.skip_conncheck:
|
2015-08-07 14:14:58 -05:00
|
|
|
principal = options.principal
|
2015-06-08 00:23:56 -05:00
|
|
|
replica_conn_check(
|
2016-10-24 06:09:11 -05:00
|
|
|
replica_config.ca_host_name, host_name, realm_name, True,
|
2015-08-07 14:14:58 -05:00
|
|
|
replica_config.ca_ds_port, options.admin_password,
|
2015-12-09 01:18:21 -06:00
|
|
|
principal=principal, ca_cert_file=options.ca_cert_file)
|
2015-06-08 00:23:56 -05:00
|
|
|
|
2016-10-24 07:33:19 -05:00
|
|
|
if options.skip_schema_check:
|
2015-06-08 00:23:56 -05:00
|
|
|
root_logger.info("Skipping CA DS schema check")
|
|
|
|
else:
|
2016-10-24 07:33:19 -05:00
|
|
|
cainstance.replica_ca_install_check(replica_config, options.promote)
|
2015-06-08 00:23:56 -05:00
|
|
|
|
|
|
|
return
|
|
|
|
|
2015-07-15 07:15:49 -05:00
|
|
|
if standalone:
|
2015-10-23 10:21:01 -05:00
|
|
|
if api.Command.ca_is_enabled()['result']:
|
2016-06-17 06:14:49 -05:00
|
|
|
raise ScriptError(
|
2015-07-15 07:15:49 -05:00
|
|
|
"One or more CA masters are already present in IPA realm "
|
|
|
|
"'%s'.\nIf you wish to replicate CA to this host, please "
|
|
|
|
"re-run 'ipa-ca-install'\nwith a replica file generated on "
|
|
|
|
"an existing CA master as argument." % realm_name
|
|
|
|
)
|
2015-06-08 00:23:56 -05:00
|
|
|
|
2015-06-08 03:32:58 -05:00
|
|
|
if options.external_cert_files:
|
2015-06-08 00:23:56 -05:00
|
|
|
if not cainstance.is_step_one_done():
|
|
|
|
# This can happen if someone passes external_ca_file without
|
|
|
|
# already having done the first stage of the CA install.
|
2016-06-17 06:14:49 -05:00
|
|
|
raise ScriptError(
|
|
|
|
"CA is not installed yet. To install with an external CA "
|
2015-06-08 00:23:56 -05:00
|
|
|
"is a two-stage process.\nFirst run the installer with "
|
|
|
|
"--external-ca.")
|
|
|
|
|
|
|
|
external_cert_file, external_ca_file = installutils.load_external_cert(
|
|
|
|
options.external_cert_files, options.subject)
|
2015-06-08 03:32:58 -05:00
|
|
|
elif options.external_ca:
|
|
|
|
if cainstance.is_step_one_done():
|
2016-06-17 06:14:49 -05:00
|
|
|
raise ScriptError(
|
|
|
|
"CA is already installed.\nRun the installer with "
|
|
|
|
"--external-cert-file.")
|
2015-06-08 03:32:58 -05:00
|
|
|
if ipautil.file_exists(paths.ROOT_IPA_CSR):
|
2016-06-17 06:14:49 -05:00
|
|
|
raise ScriptError(
|
|
|
|
"CA CSR file %s already exists.\nIn order to continue "
|
|
|
|
"remove the file and run the installer again." %
|
|
|
|
paths.ROOT_IPA_CSR)
|
2015-06-08 00:23:56 -05:00
|
|
|
|
2015-06-08 03:32:58 -05:00
|
|
|
if not options.external_cert_files:
|
|
|
|
if not cainstance.check_port():
|
|
|
|
print("IPA requires port 8443 for PKI but it is currently in use.")
|
2016-06-17 06:14:49 -05:00
|
|
|
raise ScriptError("Aborting installation")
|
2015-06-08 00:23:56 -05:00
|
|
|
|
|
|
|
if standalone:
|
|
|
|
dirname = dsinstance.config_dirname(
|
|
|
|
installutils.realm_to_serverid(realm_name))
|
|
|
|
cadb = certs.CertDB(realm_name, subject_base=subject_base)
|
|
|
|
dsdb = certs.CertDB(realm_name, nssdir=dirname, subject_base=subject_base)
|
|
|
|
|
|
|
|
for db in (cadb, dsdb):
|
2016-10-04 09:54:44 -05:00
|
|
|
for nickname, _trust_flags in db.list_certs():
|
2015-06-08 00:23:56 -05:00
|
|
|
if nickname in (certdb.get_ca_nickname(realm_name),
|
2016-10-13 22:20:16 -05:00
|
|
|
'ipaCert'):
|
2016-06-17 06:14:49 -05:00
|
|
|
raise ScriptError(
|
|
|
|
"Certificate with nickname %s is present in %s, "
|
|
|
|
"cannot continue." % (nickname, db.secdir))
|
2015-06-08 00:23:56 -05:00
|
|
|
|
|
|
|
cert = db.get_cert_from_db(nickname)
|
|
|
|
if not cert:
|
|
|
|
continue
|
2016-10-13 02:12:31 -05:00
|
|
|
subject = DN(x509.load_certificate(cert).subject)
|
2015-06-08 00:23:56 -05:00
|
|
|
if subject in (DN('CN=Certificate Authority', subject_base),
|
2016-10-13 22:20:16 -05:00
|
|
|
DN('CN=IPA RA', subject_base)):
|
2016-06-17 06:14:49 -05:00
|
|
|
raise ScriptError(
|
|
|
|
"Certificate with subject %s is present in %s, "
|
|
|
|
"cannot continue." % (subject, db.secdir))
|
2015-06-08 00:23:56 -05:00
|
|
|
|
|
|
|
|
|
|
|
def install(standalone, replica_config, options):
|
|
|
|
install_step_0(standalone, replica_config, options)
|
|
|
|
install_step_1(standalone, replica_config, options)
|
|
|
|
|
|
|
|
|
|
|
|
def install_step_0(standalone, replica_config, options):
|
|
|
|
realm_name = options.realm_name
|
|
|
|
dm_password = options.dm_password
|
|
|
|
host_name = options.host_name
|
|
|
|
|
2016-10-24 07:33:19 -05:00
|
|
|
if replica_config is None:
|
|
|
|
subject_base = options.subject
|
2015-06-08 00:23:56 -05:00
|
|
|
|
2016-10-24 07:33:19 -05:00
|
|
|
ca_signing_algorithm = options.ca_signing_algorithm
|
|
|
|
if options.external_ca:
|
|
|
|
ca_type = options.external_ca_type
|
|
|
|
csr_file = paths.ROOT_IPA_CSR
|
|
|
|
else:
|
|
|
|
ca_type = None
|
|
|
|
csr_file = None
|
|
|
|
if options.external_cert_files:
|
|
|
|
cert_file = external_cert_file.name
|
|
|
|
cert_chain_file = external_ca_file.name
|
|
|
|
else:
|
|
|
|
cert_file = None
|
|
|
|
cert_chain_file = None
|
|
|
|
|
|
|
|
pkcs12_info = None
|
|
|
|
master_host = None
|
|
|
|
master_replication_port = None
|
|
|
|
ra_p12 = None
|
2016-10-26 07:28:21 -05:00
|
|
|
ra_only = False
|
2016-10-24 07:33:19 -05:00
|
|
|
promote = False
|
2015-06-08 00:23:56 -05:00
|
|
|
else:
|
2016-10-24 07:33:19 -05:00
|
|
|
cafile = os.path.join(replica_config.dir, 'cacert.p12')
|
|
|
|
if options.promote:
|
|
|
|
custodia = custodiainstance.CustodiaInstance(
|
|
|
|
replica_config.host_name,
|
|
|
|
replica_config.realm_name)
|
|
|
|
custodia.get_ca_keys(
|
|
|
|
replica_config.ca_host_name,
|
|
|
|
cafile,
|
|
|
|
replica_config.dirman_password)
|
|
|
|
|
|
|
|
subject_base = replica_config.subject_base
|
|
|
|
|
|
|
|
ca_signing_algorithm = None
|
|
|
|
ca_type = None
|
|
|
|
csr_file = None
|
|
|
|
cert_file = None
|
|
|
|
cert_chain_file = None
|
|
|
|
|
|
|
|
pkcs12_info = (cafile,)
|
|
|
|
master_host = replica_config.ca_host_name
|
|
|
|
master_replication_port = replica_config.ca_ds_port
|
|
|
|
ra_p12 = os.path.join(replica_config.dir, 'ra.p12')
|
2016-10-26 07:28:21 -05:00
|
|
|
ra_only = not replica_config.setup_ca
|
2016-10-24 07:33:19 -05:00
|
|
|
promote = options.promote
|
|
|
|
|
|
|
|
ca = cainstance.CAInstance(realm_name, certs.NSS_DIR,
|
|
|
|
host_name=host_name)
|
|
|
|
ca.configure_instance(host_name, dm_password, dm_password,
|
|
|
|
subject_base=subject_base,
|
|
|
|
ca_signing_algorithm=ca_signing_algorithm,
|
|
|
|
ca_type=ca_type,
|
|
|
|
csr_file=csr_file,
|
|
|
|
cert_file=cert_file,
|
|
|
|
cert_chain_file=cert_chain_file,
|
|
|
|
pkcs12_info=pkcs12_info,
|
|
|
|
master_host=master_host,
|
|
|
|
master_replication_port=master_replication_port,
|
|
|
|
ra_p12=ra_p12,
|
2016-10-26 07:28:21 -05:00
|
|
|
ra_only=ra_only,
|
2016-11-08 02:53:10 -06:00
|
|
|
promote=promote,
|
|
|
|
use_ldaps=standalone)
|
2015-06-08 00:23:56 -05:00
|
|
|
|
|
|
|
|
|
|
|
def install_step_1(standalone, replica_config, options):
|
2016-10-26 07:28:21 -05:00
|
|
|
if replica_config is not None and not replica_config.setup_ca:
|
|
|
|
return
|
|
|
|
|
2015-06-08 00:23:56 -05:00
|
|
|
realm_name = options.realm_name
|
|
|
|
dm_password = options.dm_password
|
|
|
|
host_name = options.host_name
|
|
|
|
subject_base = options.subject
|
|
|
|
|
|
|
|
basedn = ipautil.realm_to_suffix(realm_name)
|
|
|
|
|
2016-05-31 17:07:33 -05:00
|
|
|
ca = cainstance.CAInstance(realm_name, certs.NSS_DIR, host_name=host_name)
|
2015-06-08 00:23:56 -05:00
|
|
|
|
2016-10-24 07:33:19 -05:00
|
|
|
ca.stop('pki-tomcat')
|
2015-06-08 00:23:56 -05:00
|
|
|
|
|
|
|
# This is done within stopped_service context, which restarts CA
|
2015-11-09 11:28:47 -06:00
|
|
|
ca.enable_client_auth_to_db(paths.CA_CS_CFG_PATH)
|
2015-06-08 00:23:56 -05:00
|
|
|
|
2016-05-31 17:07:33 -05:00
|
|
|
# Lightweight CA key retrieval is configured in step 1 instead
|
|
|
|
# of CAInstance.configure_instance (which is invoked from step
|
|
|
|
# 0) because kadmin_addprinc fails until krb5.conf is installed
|
|
|
|
# by krb.create_instance.
|
|
|
|
#
|
|
|
|
ca.setup_lightweight_ca_key_retrieval()
|
|
|
|
|
2016-10-24 07:33:19 -05:00
|
|
|
serverid = installutils.realm_to_serverid(realm_name)
|
|
|
|
|
2015-06-08 00:23:56 -05:00
|
|
|
if standalone and replica_config is None:
|
|
|
|
dirname = dsinstance.config_dirname(serverid)
|
|
|
|
|
|
|
|
# Store the new IPA CA cert chain in DS NSS database and LDAP
|
|
|
|
cadb = certs.CertDB(realm_name, subject_base=subject_base)
|
|
|
|
dsdb = certs.CertDB(realm_name, nssdir=dirname, subject_base=subject_base)
|
|
|
|
trust_flags = dict(reversed(cadb.list_certs()))
|
|
|
|
trust_chain = cadb.find_root_cert('ipaCert')[:-1]
|
|
|
|
for nickname in trust_chain[:-1]:
|
|
|
|
cert = cadb.get_cert_from_db(nickname, pem=False)
|
|
|
|
dsdb.add_cert(cert, nickname, trust_flags[nickname])
|
|
|
|
certstore.put_ca_cert_nss(api.Backend.ldap2, api.env.basedn,
|
|
|
|
cert, nickname, trust_flags[nickname])
|
|
|
|
|
|
|
|
nickname = trust_chain[-1]
|
|
|
|
cert = cadb.get_cert_from_db(nickname, pem=False)
|
|
|
|
dsdb.add_cert(cert, nickname, trust_flags[nickname])
|
|
|
|
certstore.put_ca_cert_nss(api.Backend.ldap2, api.env.basedn,
|
|
|
|
cert, nickname, trust_flags[nickname],
|
|
|
|
config_ipa=True, config_compat=True)
|
|
|
|
|
|
|
|
# Store DS CA cert in Dogtag NSS database
|
2015-11-09 11:28:47 -06:00
|
|
|
dogtagdb = certs.CertDB(realm_name, nssdir=paths.PKI_TOMCAT_ALIAS_DIR)
|
2015-06-08 00:23:56 -05:00
|
|
|
trust_flags = dict(reversed(dsdb.list_certs()))
|
|
|
|
server_certs = dsdb.find_server_certs()
|
|
|
|
trust_chain = dsdb.find_root_cert(server_certs[0][0])[:-1]
|
|
|
|
nickname = trust_chain[-1]
|
|
|
|
cert = dsdb.get_cert_from_db(nickname)
|
|
|
|
dogtagdb.add_cert(cert, nickname, trust_flags[nickname])
|
|
|
|
|
2016-10-24 07:33:19 -05:00
|
|
|
installutils.restart_dirsrv()
|
2015-06-08 00:23:56 -05:00
|
|
|
|
2016-10-24 07:33:19 -05:00
|
|
|
ca.start('pki-tomcat')
|
2015-06-08 00:23:56 -05:00
|
|
|
|
2016-10-27 06:12:02 -05:00
|
|
|
if standalone or replica_config is not None:
|
|
|
|
# We need to restart apache as we drop a new config file in there
|
|
|
|
services.knownservices.httpd.restart(capture_output=True)
|
2016-10-24 07:33:19 -05:00
|
|
|
|
|
|
|
if standalone:
|
2015-06-08 00:23:56 -05:00
|
|
|
# Install CA DNS records
|
|
|
|
if bindinstance.dns_container_exists(host_name, basedn, dm_password):
|
2016-10-06 10:35:04 -05:00
|
|
|
bind = bindinstance.BindInstance()
|
2016-06-12 11:05:48 -05:00
|
|
|
bind.update_system_records()
|
2015-06-08 00:23:56 -05:00
|
|
|
|
|
|
|
|
2015-11-09 11:28:47 -06:00
|
|
|
def uninstall():
|
2015-06-08 00:23:56 -05:00
|
|
|
ca_instance = cainstance.CAInstance(
|
2015-11-09 11:28:47 -06:00
|
|
|
api.env.realm, certs.NSS_DIR)
|
2015-06-08 00:23:56 -05:00
|
|
|
ca_instance.stop_tracking_certificates()
|
|
|
|
if ca_instance.is_configured():
|
|
|
|
ca_instance.uninstall()
|