summaryrefslogtreecommitdiffstats
path: root/base/java-tools/bin/pki
blob: e476cfcfe0b4476820354ef1ec8a9ddfbf0f734c (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/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) 2014 Red Hat, Inc.
# All rights reserved.
#

from __future__ import absolute_import
from __future__ import print_function
import shlex
import subprocess
import sys
import traceback

import pki.cli
import pki.cli.pkcs12


PYTHON_COMMANDS = ['pkcs12-import']


class PKICLI(pki.cli.CLI):

    def __init__(self):
        super(PKICLI, self).__init__(
            'pki', 'PKI command-line interface')

        self.database = None
        self.password = None
        self.password_file = None
        self.token = None

        self.add_module(pki.cli.pkcs12.PKCS12CLI())

    def get_full_module_name(self, module_name):
        return module_name

    def print_help(self):
        print('Usage: pki [OPTIONS]')
        print()
        print('      --client-type <type>     PKI client type (default: java)')
        print('   -d <path>                   Client security database location ' +
              '(default: ~/.dogtag/nssdb)')
        print('   -c <password>               Client security database password ' +
              '(mutually exclusive to the -C option)')
        print('   -C <path>                   Client-side password file ' +
              '(mutually exclusive to the -c option)')
        print('      --token <name>           Security token name')
        print()
        print('  -v, --verbose                Run in verbose mode.')
        print('      --debug                  Show debug messages.')
        print('      --help                   Show help message.')
        print()

        super(PKICLI, self).print_help()

    def execute_java(self, args, stdout=sys.stdout):

        # read RESTEasy library path
        value = subprocess.check_output(
            '. /usr/share/pki/etc/pki.conf && . /etc/pki/pki.conf && echo $RESTEASY_LIB',
            shell=True)
        resteasy_lib = value.decode(sys.getfilesystemencoding()).strip()

        # read logging configuration path
        value = subprocess.check_output(
            '. /usr/share/pki/etc/pki.conf && . /etc/pki/pki.conf && echo $LOGGING_CONFIG',
            shell=True)
        logging_config = value.decode(sys.getfilesystemencoding()).strip()

        # construct classpath
        classpath = [
            '/usr/share/java/commons-cli.jar',
            '/usr/share/java/commons-codec.jar',
            '/usr/share/java/commons-httpclient.jar',
            '/usr/share/java/commons-io.jar',
            '/usr/share/java/commons-lang.jar',
            '/usr/share/java/commons-logging.jar',
            '/usr/share/java/httpcomponents/httpclient.jar',
            '/usr/share/java/httpcomponents/httpcore.jar',
            '/usr/share/java/jackson/jackson-core-asl.jar',
            '/usr/share/java/jackson/jackson-jaxrs.jar',
            '/usr/share/java/jackson/jackson-mapper-asl.jar',
            '/usr/share/java/jackson/jackson-mrbean.jar',
            '/usr/share/java/jackson/jackson-smile.jar',
            '/usr/share/java/jackson/jackson-xc.jar',
            '/usr/share/java/jaxb-api.jar',
            '/usr/share/java/ldapjdk.jar',
            '/usr/share/java/servlet.jar',
            resteasy_lib + '/jaxrs-api.jar',
            resteasy_lib + '/resteasy-atom-provider.jar',
            resteasy_lib + '/resteasy-client.jar',
            resteasy_lib + '/resteasy-jaxb-provider.jar',
            resteasy_lib + '/resteasy-jaxrs.jar',
            resteasy_lib + '/resteasy-jaxrs-jandex.jar',
            resteasy_lib + '/resteasy-jackson-provider.jar',
            '/usr/share/java/pki/pki-nsutil.jar',
            '/usr/share/java/pki/pki-cmsutil.jar',
            '/usr/share/java/pki/pki-certsrv.jar',
            '/usr/share/java/pki/pki-tools.jar',
            '/usr/lib64/java/jss4.jar',
            '/usr/lib/java/jss4.jar'
        ]

        cmd = [
            'java',
            '-cp',
            ':'.join(classpath),
            '-Djava.util.logging.config.file=' + logging_config,
            'com.netscape.cmstools.cli.MainCLI'
        ]

        # restore options for Java commands

        if self.database:
            cmd.extend(['-d', self.database])

        if self.password:
            cmd.extend(['-c', self.password])

        if self.password_file:
            cmd.extend(['-C', self.password_file])

        if self.token and self.token != 'internal':
            cmd.extend(['--token', self.token])

        cmd.extend(args)

        if self.verbose:
            print('Java command: %s' % ' '.join(cmd))

        subprocess.check_call(cmd, stdout=stdout)

    def execute(self, argv):

        # append global options
        value = subprocess.check_output(
            '. /usr/share/pki/etc/pki.conf && . /etc/pki/pki.conf && echo $PKI_CLI_OPTIONS',
            shell=True)
        value = value.decode(sys.getfilesystemencoding()).strip()
        args = shlex.split(value)
        args.extend(argv[1:])

        client_type = 'java'

        pki_options = []
        command = None
        cmd_args = []

        # read pki options before the command
        # remove options for Python module

        i = 0
        while i < len(args):
            # if arg is a command, stop
            if args[i][0] != '-':
                command = args[i]
                break

            # get database path
            if args[i] == '-d':
                self.database = args[i + 1]
                pki_options.append(args[i])
                pki_options.append(args[i + 1])
                i = i + 2

            # get database password
            elif args[i] == '-c':
                self.password = args[i + 1]
                pki_options.append(args[i])
                pki_options.append(args[i + 1])
                i = i + 2

            # get database password file path
            elif args[i] == '-C':
                self.password_file = args[i + 1]
                pki_options.append(args[i])
                pki_options.append(args[i + 1])
                i = i + 2

            # get token name
            elif args[i] == '--token':
                self.token = args[i + 1]
                pki_options.append(args[i])
                pki_options.append(args[i + 1])
                i = i + 2

            # check verbose option
            elif args[i] == '-v' or args[i] == '--verbose':
                self.set_verbose(True)
                pki_options.append(args[i])
                i = i + 1

            # check debug option
            elif args[i] == '--debug':
                self.set_verbose(True)
                self.set_debug(True)
                pki_options.append(args[i])
                i = i + 1

            # get client type
            elif args[i] == '--client-type':
                client_type = args[i + 1]
                pki_options.append(args[i])
                pki_options.append(args[i + 1])
                i = i + 2

            else:  # otherwise, save the arg for the next module
                cmd_args.append(args[i])
                i = i + 1

        # save the rest of the args
        while i < len(args):
            cmd_args.append(args[i])
            i = i + 1

        if self.verbose:
            print('PKI options: %s' % ' '.join(pki_options))
            print('PKI command: %s %s' % (command, ' '.join(cmd_args)))

        if client_type == 'python' or command in PYTHON_COMMANDS:
            (module, module_args) = self.parse_args(cmd_args)
            module.execute(module_args)

        elif client_type == 'java':
            self.execute_java(cmd_args)

        else:
            raise Exception('Unsupported client type: ' + client_type)


if __name__ == '__main__':

    cli = PKICLI()

    try:
        cli.execute(sys.argv)

    except subprocess.CalledProcessError as e:
        if cli.verbose:
            print('ERROR: %s' % e)
        elif cli.debug:
            traceback.print_exc()
        exit(e.returncode)