summaryrefslogtreecommitdiffstats
path: root/ipaserver/install/plugins/upload_cacrt.py
blob: 1a13257336ff5a0972d5dbfb8f104890b21446ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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.plugins import MIDDLE
from ipaserver.install.plugins.baseupdate import PostUpdate
from ipaserver.install import certs
from ipalib import api, certstore
from ipapython import certdb
from ipapython.dn import DN

class update_upload_cacrt(PostUpdate):
    """
    Upload public CA certificate to LDAP
    """
    order=MIDDLE

    def execute(self, **options):
        db = certs.CertDB(self.api.env.realm)
        ca_cert = None

        if self.api.env.enable_ra:
            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]

        updates = {}

        for nickname, trust_flags in db.list_certs():
            if 'u' in trust_flags:
                continue
            if nickname == ca_nickname and self.api.env.enable_ra:
                trust_flags = 'CT,C,C'
            cert = db.get_cert_from_db(nickname, pem=False)
            try:
                dn, entry = self._make_entry(cert, nickname, trust_flags)
            except Exception, e:
                self.log.warning("Failed to create entry for %s: %s",
                                 nickname, e)
                continue
            if nickname == ca_nickname:
                ca_cert = cert
                if self.api.env.enable_ra:
                    entry.append('ipaConfigString:ipaCA')
                entry.append('ipaConfigString:compatCA')
            updates[dn] = {'dn': dn, 'default': entry}

        if ca_cert:
            dn = DN(('cn', 'CACert'), ('cn', 'ipa'), ('cn','etc'),
                    self.api.env.basedn)
            entry = ['objectclass:nsContainer',
                     'objectclass:pkiCA',
                     'cn:CAcert',
                     'cACertificate;binary:%s' % ca_cert,
                    ]
            updates[dn] = {'dn': dn, 'default': entry}

        return (False, True, [updates])

    def _make_entry(self, cert, nickname, trust_flags):
        dn = DN(('cn', nickname), ('cn', 'certificates'), ('cn', 'ipa'),
                ('cn','etc'), self.api.env.basedn)

        entry = dict()
        trust, ca, eku = certstore.trust_flags_to_key_policy(trust_flags)
        certstore.init_ca_entry(entry, cert, nickname, trust, eku)
        entry = ['%s:%s' % (a, v) for a, vs in entry.iteritems() for v in vs]

        return dn, entry

api.register(update_upload_cacrt)