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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
#!/usr/bin/python -tu
# 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
from __future__ import absolute_import
from __future__ import print_function
import sys
import signal
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 os
import socket
import struct
import subprocess
import time
import traceback
from time import strftime as date
from pki.server.deployment import pkiconfig as config
from pki.server.deployment.pkiparser import PKIConfigParser
from pki.server.deployment import pkilogging
from pki.server.deployment import pkimessages as log
import pki.server.deployment.pkihelper as util
except ImportError:
print("""\
There was a problem importing one of the required Python modules. The
error was:
%s
""" % sys.exc_info()[1], file=sys.stderr)
sys.exit(1)
# Handle the Keyboard Interrupt
# pylint: disable=W0613
def interrupt_handler(event, frame):
print()
print('\nUninstallation canceled.')
sys.exit(1)
# PKI Deployment Functions
def main(argv):
"""main entry point"""
config.pki_deployment_executable = os.path.basename(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.getfqdn()
# Retrieve DNS domainname
config.pki_dns_domainname = None
try:
dnsdomainname = subprocess.check_output(["dnsdomainname"])
dnsdomainname = dnsdomainname.decode('ascii').rstrip('\n')
config.pki_dns_domainname = dnsdomainname
if not len(config.pki_dns_domainname):
print(log.PKI_DNS_DOMAIN_NOT_SET)
config.pki_dns_domainname = config.pki_hostname
except subprocess.CalledProcessError as exc:
print(log.PKI_SUBPROCESS_ERROR_1 % exc)
sys.exit(1)
# Read and process command-line arguments.
parser = PKIConfigParser(
'PKI Instance Removal',
log.PKIDESTROY_EPILOG)
parser.optional.add_argument(
'-i',
dest='pki_deployed_instance_name',
action='store',
nargs=1, metavar='<instance>',
help='FORMAT: ${pki_instance_name}')
parser.optional.add_argument(
'-u',
dest='pki_secdomain_user',
action='store',
nargs=1, metavar='<security domain user>',
help='security domain user')
parser.optional.add_argument(
'-W',
dest='pki_secdomain_pass_file',
action='store',
nargs=1, metavar='<security domain password file>',
help='security domain password file path')
args = parser.process_command_line_arguments()
interactive = False
# Only run this program as "root".
if not os.geteuid() == 0:
sys.exit("'%s' must be run as root!" % argv[0])
while True:
# -s <subsystem>
if args.pki_subsystem is None:
interactive = True
config.pki_subsystem = parser.read_text(
'Subsystem (CA/KRA/OCSP/TKS/TPS)',
options=['CA', 'KRA', 'OCSP', 'TKS', 'TPS'],
default='CA', case_sensitive=False).upper()
else:
config.pki_subsystem = str(args.pki_subsystem).strip('[\']')
# -i <instance name>
if args.pki_deployed_instance_name is None:
interactive = True
config.pki_deployed_instance_name = \
parser.read_text('Instance', default='pki-tomcat')
else:
config.pki_deployed_instance_name = \
str(args.pki_deployed_instance_name).strip('[\']')
if interactive:
print()
parser.indent = 0
begin = parser.read_text(
'Begin uninstallation (Yes/No/Quit)',
options=['Yes', 'Y', 'No', 'N', 'Quit', 'Q'],
sign='?', allow_empty=False, case_sensitive=False).lower()
print()
if begin == 'q' or begin == 'quit':
print("Uninstallation canceled.")
sys.exit(0)
elif begin == 'y' or begin == 'yes':
break
else:
break
# '-u'
if args.pki_secdomain_user:
config.pki_secdomain_user = str(args.pki_secdomain_user).strip('[\']')
# '-W' password file
if args.pki_secdomain_pass_file:
with open(str(args.pki_secdomain_pass_file).strip('[\']'), 'r') as \
pwd_file:
config.pki_secdomain_pass = pwd_file.readline().strip('\n')
# verify that previously deployed instance exists
deployed_pki_instance_path = \
config.pki_root_prefix + config.PKI_DEPLOYMENT_BASE_ROOT + "/" + \
config.pki_deployed_instance_name
if not os.path.exists(deployed_pki_instance_path):
print("ERROR: " + log.PKI_INSTANCE_DOES_NOT_EXIST_1 %
deployed_pki_instance_path)
print()
parser.arg_parser.exit(-1)
# verify that previously deployed subsystem for this instance exists
deployed_pki_subsystem_path = \
deployed_pki_instance_path + "/" + config.pki_subsystem.lower()
if not os.path.exists(deployed_pki_subsystem_path):
print("ERROR: " + log.PKI_SUBSYSTEM_DOES_NOT_EXIST_2 %
(config.pki_subsystem, deployed_pki_instance_path))
print()
parser.arg_parser.exit(-1)
config.default_deployment_cfg = \
config.PKI_DEPLOYMENT_DEFAULT_CONFIGURATION_FILE
# establish complete path to previously deployed configuration file
config.user_deployment_cfg =\
deployed_pki_subsystem_path + "/" +\
"registry" + "/" +\
config.pki_subsystem.lower() + "/" +\
config.USER_DEPLOYMENT_CONFIGURATION
parser.validate()
parser.init_config()
# Enable 'pkidestroy' logging.
config.pki_log_dir = \
config.pki_root_prefix + config.PKI_DEPLOYMENT_LOG_ROOT
config.pki_log_name = "pki" + "-" +\
config.pki_subsystem.lower() +\
"-" + "destroy" + "." +\
config.pki_timestamp + "." + "log"
print('Log file: %s/%s' % (config.pki_log_dir, config.pki_log_name))
rv = pkilogging.enable_pki_logger(config.pki_log_dir,
config.pki_log_name,
config.pki_log_level,
config.pki_console_log_level,
"pkidestroy")
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 = parser.read_pki_configuration_file()
if rv != 0:
config.pki_log.error(log.PKI_UNABLE_TO_PARSE_1, rv,
extra=config.PKI_INDENTATION_LEVEL_0)
sys.exit(1)
# Combine the various sectional dictionaries into a PKI master dictionary
parser.compose_pki_master_dictionary()
parser.mdict['pki_destroy_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(pkilogging.log_format(parser.mdict),
extra=config.PKI_INDENTATION_LEVEL_0)
print("Uninstalling " + config.pki_subsystem + " from " +
deployed_pki_instance_path + ".")
# Process the various "scriptlets" to remove the specified PKI subsystem.
pki_subsystem_scriptlets = parser.mdict['destroy_scriplets'].split()
deployer = util.PKIDeployer(parser.mdict)
try:
for scriptlet_name in pki_subsystem_scriptlets:
scriptlet_module = __import__(
"pki.server.deployment.scriptlets." + scriptlet_name,
fromlist=[scriptlet_name])
scriptlet = scriptlet_module.PkiScriptlet()
scriptlet.destroy(deployer)
# pylint: disable=W0703
except Exception as e:
log_error_details()
print()
print("Uninstallation failed: %s" % e)
print()
sys.exit(1)
print()
print("Uninstallation complete.")
def log_error_details():
e_type, e_value, e_stacktrace = sys.exc_info()
config.pki_log.debug(
"Error Type: " + e_type.__name__, extra=config.PKI_INDENTATION_LEVEL_2)
config.pki_log.debug(
"Error Message: " + str(e_value), extra=config.PKI_INDENTATION_LEVEL_2)
stacktrace_list = traceback.format_list(traceback.extract_tb(e_stacktrace))
e_stacktrace = ""
for l in stacktrace_list:
e_stacktrace += l
config.pki_log.debug(e_stacktrace, extra=config.PKI_INDENTATION_LEVEL_2)
del e_type, e_value, e_stacktrace
# PKI Deployment Entry Point
if __name__ == "__main__":
signal.signal(signal.SIGINT, interrupt_handler)
main(sys.argv)
|