summaryrefslogtreecommitdiffstats
path: root/base/deploy/src/pkispawn
blob: d665f3c9f1a4e0f50329320cb6eb2bfded277e2a (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/python -t
# Authors:
#     Matthew Harmsen <mharmsen@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) 2011 Red Hat, Inc.
# All rights reserved.
#

# System Imports
import sys
if not hasattr(sys, "hexversion") or sys.hexversion < 0x020700f0:
    print "Python version %s.%s.%s is too old." % sys.version_info[:3]
    print "Please upgrade to at least Python 2.7.0."
    sys.exit(1)
try:
    import argparse
    import logging
    import os
    import pprint
    import random
    import socket
    import string
    import struct
    import time
    from time import strftime as date
    from pki.deployment import pkiconfig as config
    from pki.deployment import pkiparser as parse
    from pki.deployment import pkilogging
    from pki.deployment import pkimessages as log
except ImportError:
    print >> sys.stderr, """\
There was a problem importing one of the required Python modules. The
error was:

    %s
""" % sys.exc_value
    sys.exit(1)


# PKI Deployment Functions
def main(argv):
    "main entry point"

    # Only run this program as "root".
    if not os.geteuid() == 0:
        sys.exit("'%s' must be run as root!" % argv[0])

    # Set the umask
    os.umask(config.PKI_DEPLOYMENT_DEFAULT_UMASK)

    # Set installation time
    ticks = time.time()
    config.pki_install_time = time.asctime(time.localtime(ticks))

    # Generate a timestamp
    config.pki_timestamp = date('%Y%m%d%H%M%S', time.localtime(ticks))
    config.pki_certificate_timestamp =\
        date('%Y-%m-%d %H:%M:%S', time.localtime(ticks))

    # Obtain the architecture bit-size
    config.pki_architecture = struct.calcsize("P") * 8

    # Retrieve hostname
    config.pki_hostname = socket.gethostname()

    # Generate random 'pin's for use as security database passwords
    pin_low  = 100000000000
    pin_high = 999999999999
    config.pki_pin = random.randint(pin_low, pin_high)
    config.pki_client_pin = random.randint(pin_low, pin_high)

    # Generate a one-time pin to be used prior to configuration
    config.pki_one_time_pin =\
        ''.join(random.choice(string.ascii_letters + string.digits)\
        for x in range(20))

    # Initialize 'pretty print' for objects
    pp = pprint.PrettyPrinter(indent=4)

    # Read and process command-line arguments.
    parse.process_command_line_arguments(argv)

    # Enable 'pkispawn' logging.
    rv = 0
    if not config.pki_update_flag:
        if not config.pki_dry_run_flag:
            config.pki_log_dir = config.pki_root_prefix +\
                                 "/var/log"
            config.pki_log_name = "pki" + "-" +\
                                  config.pki_subsystem.lower() +\
                                  "-" + "spawn" + "." +\
                                  config.pki_timestamp + "." + "log"
        else:
            config.pki_log_dir = "/dev"
            config.pki_log_name = "null"
        rv = pkilogging.enable_pki_logger(config.pki_log_dir,
                                          config.pki_log_name,
                                          config.pki_log_level,
                                          config.pki_console_log_level,
                                          "pkispawn")
    else:
        if not config.pki_dry_run_flag:
            config.pki_log_dir = config.pki_root_prefix +\
                                 "/var/log"
            config.pki_log_name = "pki" + "-" +\
                                  config.pki_subsystem.lower() +\
                                  "-" + "respawn" + "." +\
                                  config.pki_timestamp + "." + "log"
        else:
            config.pki_log_dir = "/dev"
            config.pki_log_name = "null"
        rv = pkilogging.enable_pki_logger(config.pki_log_dir,
                                          config.pki_log_name,
                                          config.pki_log_level,
                                          config.pki_console_log_level,
                                          "pkirespawn")
    if rv != OSError:
        config.pki_log = rv
    else:
        print log.PKI_UNABLE_TO_CREATE_LOG_DIRECTORY_1 % config.pki_log_dir
        sys.exit(1)

    # Read the specified PKI configuration file.
    rv = parse.read_pki_configuration_file()
    if rv != 0:
        config.pki_log.error(PKI_UNABLE_TO_PARSE_1, rv,
                             extra=config.PKI_INDENTATION_LEVEL_0)
        sys.exit(1)
    else:
        config.pki_log.debug(log.PKI_DICTIONARY_COMMON,
                             extra=config.PKI_INDENTATION_LEVEL_0)
        config.pki_log.debug(pp.pformat(config.pki_common_dict),
                             extra=config.PKI_INDENTATION_LEVEL_0)
        config.pki_log.debug(log.PKI_DICTIONARY_WEB_SERVER,
                             extra=config.PKI_INDENTATION_LEVEL_0)
        config.pki_log.debug(pp.pformat(config.pki_web_server_dict),
                             extra=config.PKI_INDENTATION_LEVEL_0)
        config.pki_log.debug(log.PKI_DICTIONARY_SUBSYSTEM,
                             extra=config.PKI_INDENTATION_LEVEL_0)
        config.pki_log.debug(pp.pformat(config.pki_subsystem_dict),
                             extra=config.PKI_INDENTATION_LEVEL_0)

    # Override PKI configuration file values with 'custom' command-line values.
    if not config.pki_admin_domain_name is None:
        config.pki_common_dict['pki_admin_domain_name'] =\
            config.pki_admin_domain_name
    if not config.pki_instance_name is None:
        config.pki_common_dict['pki_instance_name'] =\
            config.pki_instance_name
    if not config.pki_http_port is None:
        config.pki_web_server_dict['pki_http_port'] =\
            config.pki_http_port
    if not config.pki_https_port is None:
        config.pki_web_server_dict['pki_https_port'] =\
            config.pki_https_port
    if not config.pki_ajp_port is None:
        config.pki_web_server_dict['pki_ajp_port'] =\
            config.pki_ajp_port
    config.pki_log.debug(log.PKI_DICTIONARY_COMMON,
                         extra=config.PKI_INDENTATION_LEVEL_0)
    config.pki_log.debug(pp.pformat(config.pki_common_dict),
                         extra=config.PKI_INDENTATION_LEVEL_0)
    config.pki_log.debug(log.PKI_DICTIONARY_WEB_SERVER,
                         extra=config.PKI_INDENTATION_LEVEL_0)
    config.pki_log.debug(pp.pformat(config.pki_web_server_dict),
                         extra=config.PKI_INDENTATION_LEVEL_0)
    config.pki_log.debug(log.PKI_DICTIONARY_SUBSYSTEM,
                         extra=config.PKI_INDENTATION_LEVEL_0)
    config.pki_log.debug(pp.pformat(config.pki_subsystem_dict),
                         extra=config.PKI_INDENTATION_LEVEL_0)

    # Read in the PKI slots configuration file.
    parse.compose_pki_slots_dictionary()
    config.pki_log.debug(log.PKI_DICTIONARY_SLOTS,
                         extra=config.PKI_INDENTATION_LEVEL_0)
    config.pki_log.debug(pp.pformat(config.pki_slots_dict),
                         extra=config.PKI_INDENTATION_LEVEL_0)

    # Combine the various sectional dictionaries into a PKI master dictionary
    parse.compose_pki_master_dictionary()
    if not config.pki_update_flag:
        config.pki_master_dict['pki_spawn_log'] = config.pki_log_dir + "/" +\
                                                  config.pki_log_name
    else:
        config.pki_master_dict['pki_respawn_log'] = config.pki_log_dir + "/" +\
                                                    config.pki_log_name
    config.pki_log.debug(log.PKI_DICTIONARY_MASTER,
                         extra=config.PKI_INDENTATION_LEVEL_0)
    config.pki_log.debug(pp.pformat(config.pki_master_dict),
                         extra=config.PKI_INDENTATION_LEVEL_0)

    # Install and configure the specified PKI subsystem.
    pki_scriptlets_path = "/usr/share/pki/deployment/spawn" +\
                          "/" + config.pki_subsystem.lower()
    if not os.path.exists(pki_scriptlets_path) or\
       not os.path.isdir(pki_scriptlets_path):
        config.pki_log.error(log.PKI_DIRECTORY_MISSING_OR_NOT_A_DIRECTORY_1,
                             pki_scriptlets_path,
                             extra=config.PKI_INDENTATION_LEVEL_0)
        sys.exit(1)
    pki_subsystem_scriptlets = os.listdir(pki_scriptlets_path)
    pki_subsystem_scriptlets.sort()

    # Process the various "scriptlets" for the specified PKI subsystem.
    rv = 0
    for pki_scriptlet in pki_subsystem_scriptlets:
        scriptlet = __import__("pki.deployment" +\
                               "." + pki_scriptlet[4:],
                               fromlist = [pki_scriptlet[4:]])
        instance = scriptlet.PkiScriptlet()
        if not config.pki_update_flag:
            rv = instance.spawn()
        else:
            rv = instance.respawn()
        if rv != 0:
            sys.exit(1)
    config.pki_log.debug(log.PKI_DICTIONARY_MASTER,
                         extra=config.PKI_INDENTATION_LEVEL_0)
    config.pki_log.debug(pp.pformat(config.pki_master_dict),
                         extra=config.PKI_INDENTATION_LEVEL_0)


# PKI Deployment Entry Point
if __name__ == "__main__":
    main(sys.argv)