summaryrefslogtreecommitdiffstats
path: root/base/server/upgrade/10.0.1/01-ReplaceRandomNumberGenerator
blob: 029f295e9fc73b84e79512c6a9a35f24426cd0a2 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/python
# Authors:
#     Endi S. Dewata <edewata@redhat.com>
#
# 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; version 2 of the License.
#
# 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2013 Red Hat, Inc.
# All rights reserved.
#

import grp
import os
import pwd
import shutil
import signal
import sys
from lxml import etree

import pki.upgrade


class ReplaceRandomNumberGenerator(pki.upgrade.PKIUpgradeScriptlet):

    def __init__(self):

        self.message = 'Replace random number generator'

        self.parser = etree.XMLParser(remove_blank_text=True)

    def upgrade_subsystem(self, instance, subsystem):

        context_xml = os.path.join(
            pki.upgrade.INSTANCE_BASE_DIR,
            instance, 'webapps', subsystem,
            'META-INF', 'context.xml')

        document = etree.parse(context_xml, self.parser)

        self.add_manager(document)
        self.update_authenticator(document)

        with open(context_xml, 'w') as f:
            f.write(etree.tostring(document, pretty_print=True))


    def upgrade_instance(self, instance):

        self.update_root_context_xml(instance)
        self.create_pki_context_xml(instance)


    def update_root_context_xml(self, instance):

        context_xml = os.path.join(
            pki.upgrade.INSTANCE_BASE_DIR,
            instance, 'webapps', 'ROOT',
            'META-INF', 'context.xml')

        document = etree.parse(context_xml, self.parser)

        self.add_manager(document)

        with open(context_xml, 'w') as f:
            f.write(etree.tostring(document, pretty_print=True))


    def create_pki_context_xml(self, instance):

        uid = pwd.getpwnam('pkiuser').pw_uid
        gid = grp.getgrnam('pkiuser').gr_gid

        source = '/usr/share/pki/server/webapps/pki/META-INF/context.xml'

        meta_inf_dir = os.path.join(
            pki.upgrade.INSTANCE_BASE_DIR,
            instance, 'webapps', 'pki',
            'META-INF')
        context_xml = os.path.join(meta_inf_dir, 'context.xml')

        if not os.path.exists(meta_inf_dir):
            os.makedirs(meta_inf_dir)

        os.chown(meta_inf_dir, uid, gid)
        os.chmod(meta_inf_dir, 0770)

        if not os.path.exists(context_xml):
            shutil.copyfile(source, context_xml)

        os.chown(context_xml, uid, gid)
        os.chmod(context_xml, 0660)

        document = etree.parse(context_xml, self.parser)

        self.add_manager(document)

        with open(context_xml, 'w') as f:
            f.write(etree.tostring(document, pretty_print=True))


    def add_manager(self, document):

        # Find existing manager
        context = document.getroot()
        manager = context.find('Manager')

        if manager is None:

            # Create new manager
            manager = etree.SubElement(context, 'Manager')

        # Update manager's attributes
        manager.set('secureRandomProvider', 'Mozilla-JSS')
        manager.set('secureRandomAlgorithm', 'pkcs11prng')


    def update_authenticator(self, document):

        context = document.getroot()
        valves = context.findall('Valve')
        authenticator = None

        # Find existing authenticator
        for valve in valves:
            className = valve.get('className')
            if className != 'com.netscape.cms.tomcat.SSLAuthenticatorWithFallback':
                continue

            # Found existing authenticator
            authenticator = valve
            break

        if authenticator is None:

            # Create new authenticator'
            authenticator = etree.SubElement(authenticator, 'Valve')
            authenticator.set('className',
                'com.netscape.cms.tomcat.SSLAuthenticatorWithFallback')

        # Update authenticator's attributes
        authenticator.set('secureRandomProvider', 'Mozilla-JSS')
        authenticator.set('secureRandomAlgorithm', 'pkcs11prng')