2012-11-12 16:44:15 -06:00
|
|
|
# Authors:
|
|
|
|
# Alexander Bokovoy <abokovoy@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012 Red Hat
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
from ipaserver.install import certs
|
2015-03-17 03:23:40 -05:00
|
|
|
from ipalib import api, errors, certstore
|
2015-03-18 09:46:00 -05:00
|
|
|
from ipalib import Updater
|
2014-06-12 03:33:16 -05:00
|
|
|
from ipapython import certdb
|
2012-11-12 16:44:15 -06:00
|
|
|
from ipapython.dn import DN
|
|
|
|
|
2015-03-18 09:46:00 -05:00
|
|
|
class update_upload_cacrt(Updater):
|
2012-11-12 16:44:15 -06:00
|
|
|
"""
|
|
|
|
Upload public CA certificate to LDAP
|
|
|
|
"""
|
|
|
|
|
|
|
|
def execute(self, **options):
|
2014-06-12 03:33:16 -05:00
|
|
|
db = certs.CertDB(self.api.env.realm)
|
|
|
|
ca_cert = None
|
2012-11-12 16:44:15 -06:00
|
|
|
|
2014-10-13 07:30:15 -05:00
|
|
|
ca_enabled = self.api.Command.ca_is_enabled()['result']
|
|
|
|
if ca_enabled:
|
2014-06-12 03:33:16 -05:00
|
|
|
ca_nickname = certdb.get_ca_nickname(self.api.env.realm)
|
|
|
|
else:
|
|
|
|
ca_nickname = None
|
|
|
|
server_certs = db.find_server_certs()
|
|
|
|
if server_certs:
|
|
|
|
ca_chain = db.find_root_cert(server_certs[0][0])[:-1]
|
|
|
|
if ca_chain:
|
|
|
|
ca_nickname = ca_chain[-1]
|
2012-11-12 16:44:15 -06:00
|
|
|
|
2015-03-18 09:46:00 -05:00
|
|
|
ldap = self.api.Backend.ldap2
|
2012-11-12 16:44:15 -06:00
|
|
|
|
2014-06-12 03:33:16 -05:00
|
|
|
for nickname, trust_flags in db.list_certs():
|
|
|
|
if 'u' in trust_flags:
|
|
|
|
continue
|
2014-10-13 07:30:15 -05:00
|
|
|
if nickname == ca_nickname and ca_enabled:
|
2014-06-12 03:33:16 -05:00
|
|
|
trust_flags = 'CT,C,C'
|
|
|
|
cert = db.get_cert_from_db(nickname, pem=False)
|
2015-03-17 03:23:40 -05:00
|
|
|
trust, ca, eku = certstore.trust_flags_to_key_policy(trust_flags)
|
|
|
|
|
|
|
|
dn = DN(('cn', nickname), ('cn', 'certificates'), ('cn', 'ipa'),
|
|
|
|
('cn','etc'), self.api.env.basedn)
|
|
|
|
entry = ldap.make_entry(dn)
|
|
|
|
|
2014-06-12 03:33:16 -05:00
|
|
|
try:
|
2015-03-17 03:23:40 -05:00
|
|
|
certstore.init_ca_entry(entry, cert, nickname, trust, eku)
|
2015-07-30 09:49:29 -05:00
|
|
|
except Exception as e:
|
2014-06-12 03:33:16 -05:00
|
|
|
self.log.warning("Failed to create entry for %s: %s",
|
|
|
|
nickname, e)
|
|
|
|
continue
|
|
|
|
if nickname == ca_nickname:
|
|
|
|
ca_cert = cert
|
2015-03-17 03:23:40 -05:00
|
|
|
config = entry.setdefault('ipaConfigString', [])
|
2014-10-13 07:30:15 -05:00
|
|
|
if ca_enabled:
|
2015-03-17 03:23:40 -05:00
|
|
|
config.append('ipaCa')
|
|
|
|
config.append('ipaCa')
|
|
|
|
|
|
|
|
try:
|
|
|
|
ldap.add_entry(entry)
|
|
|
|
except errors.DuplicateEntry:
|
|
|
|
pass
|
2014-06-12 03:33:16 -05:00
|
|
|
|
|
|
|
if ca_cert:
|
|
|
|
dn = DN(('cn', 'CACert'), ('cn', 'ipa'), ('cn','etc'),
|
|
|
|
self.api.env.basedn)
|
2015-03-17 03:23:40 -05:00
|
|
|
try:
|
|
|
|
entry = ldap.get_entry(dn)
|
|
|
|
except errors.NotFound:
|
|
|
|
entry = ldap.make_entry(dn)
|
|
|
|
entry['objectclass'] = ['nsContainer', 'pkiCA']
|
|
|
|
entry.single_value['cn'] = 'CAcert'
|
|
|
|
entry.single_value['cACertificate;binary'] = ca_cert
|
|
|
|
ldap.add_entry(entry)
|
|
|
|
else:
|
2015-09-01 06:22:04 -05:00
|
|
|
if b'' in entry['cACertificate;binary']:
|
2015-03-17 03:23:40 -05:00
|
|
|
entry.single_value['cACertificate;binary'] = ca_cert
|
|
|
|
ldap.update_entry(entry)
|
2014-06-12 03:33:16 -05:00
|
|
|
|
2015-03-17 11:56:34 -05:00
|
|
|
return False, []
|
2014-06-12 03:33:16 -05:00
|
|
|
|
2012-11-12 16:44:15 -06:00
|
|
|
api.register(update_upload_cacrt)
|