bindcmd.py 38.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Copyright (C) 2009  Internet Systems Consortium.
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

16
17
18
19
20
"""This module holds the BindCmdInterpreter class. This provides the
   core functionality for bindctl. It maintains a session with
   b10-cmdctl, holds local configuration and module information, and
   handles command line interface commands"""

21
22
import sys
from cmd import Cmd
23
24
from bindctl.exception import *
from bindctl.moduleinfo import *
Jelte Jansen's avatar
Jelte Jansen committed
25
from bindctl.cmdparse import BindCmdParser
Jelte Jansen's avatar
Jelte Jansen committed
26
from bindctl import command_sets
27
from xml.dom import minidom
28
import isc.config
29
import isc.cc.data
30
31
32
33
34
35
import http.client
import json
import inspect
import pprint
import ssl, socket
import os, time, random, re
36
import os.path
37
38
39
import getpass
from hashlib import sha1
import csv
40
41
import pwd
import getpass
42
import copy
43
import errno
44
45
46
47

try:
    from collections import OrderedDict
except ImportError:
48
    from bindctl.mycollections import OrderedDict
49

50
51
52
# if we have readline support, use that, otherwise use normal stdio
try:
    import readline
53
54
55
56
57
    # Only consider spaces as word boundaries; identifiers can contain
    # '/' and '[]', and configuration item names can in theory use any
    # printable  character. See the discussion in tickets #1345 and
    # #2254 for more information.
    readline.set_completer_delims(' ')
58

59
60
    my_readline = readline.get_line_buffer
except ImportError:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
61
    my_readline = sys.stdin.readline
62

63
64
65
# Used for tab-completion of 'identifiers' (i.e. config values)
# If a command parameter has this name, the tab completion hints
# are derived from config data
66
CFGITEM_IDENTIFIER_PARAM = 'identifier'
67

68
CSV_FILE_NAME = 'default_user.csv'
69
CONFIG_MODULE_NAME = 'config'
Likun Zhang's avatar
Likun Zhang committed
70
CONST_BINDCTL_HELP = """
71
usage: <module name> <command name> [param1 = value1 [, param2 = value2]]
Jeremy C. Reed's avatar
Jeremy C. Reed committed
72
Type Tab character to get the hint of module/command/parameters.
73
74
75
76
77
Type \"help(? h)\" for help on bindctl.
Type \"<module_name> help\" for help on the specific module.
Type \"<module_name> <command_name> help\" for help on the specific command.
\nAvailable module names: """

78
class ValidatedHTTPSConnection(http.client.HTTPSConnection):
79
    '''Overrides HTTPSConnection to support certification
80
81
82
83
84
85
    validation. '''
    def __init__(self, host, ca_certs):
        http.client.HTTPSConnection.__init__(self, host)
        self.ca_certs = ca_certs

    def connect(self):
86
        ''' Overrides the connect() so that we do
87
88
89
90
91
92
        certificate validation. '''
        sock = socket.create_connection((self.host, self.port),
                                        self.timeout)
        if self._tunnel_host:
            self.sock = sock
            self._tunnel()
93

94
95
96
97
98
99
100
101
        req_cert = ssl.CERT_NONE
        if self.ca_certs:
            req_cert = ssl.CERT_REQUIRED
        self.sock = ssl.wrap_socket(sock, self.key_file,
                                    self.cert_file,
                                    cert_reqs=req_cert,
                                    ca_certs=self.ca_certs)

102
class BindCmdInterpreter(Cmd):
103
    """simple bindctl example."""
104

105
106
    def __init__(self, server_port='localhost:8080', pem_file=None,
                 csv_file_dir=None):
107
108
109
        Cmd.__init__(self)
        self.location = ""
        self.prompt_end = '> '
110
111
112
113
        if sys.stdin.isatty():
            self.prompt = self.prompt_end
        else:
            self.prompt = ""
114
115
        self.ruler = '-'
        self.modules = OrderedDict()
116
        self.add_module_info(ModuleInfo("help", desc = "Get help for bindctl."))
117
        self.server_port = server_port
118
119
        self.conn = ValidatedHTTPSConnection(self.server_port,
                                             ca_certs=pem_file)
120
        self.session_id = self._get_session_id()
121
        self.config_data = None
122
123
124
125
126
127
        if csv_file_dir is not None:
            self.csv_file_dir = csv_file_dir
        else:
            self.csv_file_dir = pwd.getpwnam(getpass.getuser()).pw_dir + \
                os.sep + '.bind10' + os.sep

128
129
130
131
132
    def _print(self, *args):
        '''Simple wrapper around calls to print that can be overridden in
           unit tests.'''
        print(*args)

133
    def _get_session_id(self):
134
        '''Generate one session id for the connection. '''
135
136
        rand = os.urandom(16)
        now = time.time()
137
        session_id = sha1(("%s%s%s" %(rand, now,
138
                                      socket.gethostname())).encode())
139
140
        digest = session_id.hexdigest()
        return digest
141

142
    def run(self):
143
144
145
146
147
148
149
150
        '''Parse commands from user and send them to cmdctl.'''

        # Show helper warning about a well known issue.  We only do this
        # when stdin is attached to a terminal, because otherwise it doesn't
        # matter and is just noisy, and could even be harmful if the output
        # is processed by a script that expects a specific format.
        if my_readline == sys.stdin.readline and sys.stdin.isatty():
            sys.stdout.write("""\
151
152
153
WARNING: The Python readline module isn't available, so some command line
         editing features (including command history management) will not
         work.  See the BIND 10 guide for more details.\n\n""")
154

155
        try:
156
            if not self.login_to_cmdctl():
Jelte Jansen's avatar
Jelte Jansen committed
157
                return 1
158
159

            self.cmdloop()
160
            self._print('\nExit from bindctl')
161
            return 0
162
        except FailToLogin as err:
163
            # error already printed when this was raised, ignoring
164
            return 1
165
        except KeyboardInterrupt:
166
            self._print('\nExit from bindctl')
167
            return 0
168
        except socket.error as err:
169
            self._print('Failed to send request, the connection is closed')
170
            return 1
171
        except http.client.CannotSendRequest:
172
            self._print('Can not send request, the connection is busy')
173
            return 1
174
175

    def _get_saved_user_info(self, dir, file_name):
176
        ''' Read all the available username and password pairs saved in
177
        file(path is "dir + file_name"), Return value is one list of elements
178
        ['name', 'password'], If get information failed, empty list will be
179
        returned.'''
180
181
        if (not dir) or (not os.path.exists(dir)):
            return []
182

183
184
185
        try:
            csvfile = None
            users = []
186
187
188
189
            csvfile = open(dir + file_name)
            users_info = csv.reader(csvfile)
            for row in users_info:
                users.append([row[0], row[1]])
190
        except (IOError, IndexError) as err:
191
192
            self._print("Error reading saved username and password "
                        "from %s%s: %s" % (dir, file_name, err))
193
194
195
        finally:
            if csvfile:
                csvfile.close()
196
            return users
197
198
199
200
201

    def _save_user_info(self, username, passwd, dir, file_name):
        ''' Save username and password in file "dir + file_name"
        If it's saved properly, return True, or else return False. '''
        try:
202
203
204
            if not os.path.exists(dir):
                os.mkdir(dir, 0o700)

205
            csvfilepath = dir + file_name
206
207
208
209
210
            csvfile = open(csvfilepath, 'w')
            os.chmod(csvfilepath, 0o600)
            writer = csv.writer(csvfile)
            writer.writerow([username, passwd])
            csvfile.close()
211
        except IOError as err:
212
213
            self._print("Error saving user information:", err)
            self._print("user info file name: %s%s" % (dir, file_name))
214
215
216
            return False

        return True
217

218
    def _try_login(self, username, password):
219
        '''
220
221
222
        Attempts to log into cmdctl by sending a POST with the given
        username and password. On success of the POST (not the login,
        but the network operation), it returns a tuple (response, data).
223
224
        We check for some failures such as SSL errors and socket errors
        which could happen due to the environment in which BIND 10 runs.
225
226
227
        On failure, it raises a FailToLogin exception and prints some
        information on the failure.  This call is essentially 'private',
        but made 'protected' for easier testing.
228
        '''
229
230
        param = {'username': username, 'password' : password}
        try:
231
232
            response = self.send_POST('/login', param)
            data = response.read().decode()
233
            # return here (will raise error after try block)
234
            return (response, data)
235
        except (ssl.SSLError, socket.error) as err:
236
            self._print('Error while sending login information:', err)
237
238
            pass
        raise FailToLogin()
239

240
    def login_to_cmdctl(self):
241
242
        '''Login to cmdctl with the username and password given by
        the user. After the login is sucessful, the username and
243
244
245
        password will be saved in 'default_user.csv', when run the next
        time, username and password saved in 'default_user.csv' will be
        used first.
246
        '''
247
        # Look at existing username/password combinations and try to log in
248
        users = self._get_saved_user_info(self.csv_file_dir, CSV_FILE_NAME)
249
        for row in users:
250
            response, data = self._try_login(row[0], row[1])
251

252
            if response.status == http.client.OK:
253
254
                # Is interactive?
                if sys.stdin.isatty():
255
                    self._print(data + ' login as ' + row[0])
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
256
                return True
257

258
        # No valid logins were found, prompt the user for a username/password
259
        count = 0
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
        if not os.path.exists(self.csv_file_dir + CSV_FILE_NAME):
            self._print('\nNo stored password file found.\n\n'
                        'When the system is first set up you need to create '
                        'at least one user account.\n'
                        'For information on how to set up a BIND 10 system, '
                        'please check see the\n'
                        'BIND 10 Guide: \n\n'
                        'http://bind10.isc.org/docs/bind10-guide.html#quick-start-auth-dns\n\n'

                        'If a user account has been set up, please check the '
                        'b10-cmdctl log for other\n'
                        'information.\n')
        else:
            self._print('Login failed: either the user name or password is '
                        'invalid.\n')
275
        while True:
276
            count = count + 1
277
            if count > 3:
278
                self._print("Too many authentication failures")
279
280
                return False

281
            username = input("Username: ")
282
            passwd = getpass.getpass()
283

284
285
            response, data = self._try_login(username, passwd)
            self._print(data)
286

287
            if response.status == http.client.OK:
288
289
                self._save_user_info(username, passwd, self.csv_file_dir,
                                     CSV_FILE_NAME)
290
                return True
291

292
    def _update_commands(self):
Jelte Jansen's avatar
Jelte Jansen committed
293
294
295
        '''Update the commands of all modules. '''
        for module_name in self.config_data.get_config_item_list():
            self._prepare_module_commands(self.config_data.get_module_spec(module_name))
296

JINMEI Tatuya's avatar
JINMEI Tatuya committed
297
298
299
300
301
302
    def _send_message(self, url, body):
        headers = {"cookie" : self.session_id}
        self.conn.request('GET', url, body, headers)
        res = self.conn.getresponse()
        return res.status, res.read()

303
    def send_GET(self, url, body = None):
304
305
306
        '''Send GET request to cmdctl, session id is send with the name
        'cookie' in header.
        '''
JINMEI Tatuya's avatar
JINMEI Tatuya committed
307
308
309
310
311
        status, reply_msg = self._send_message(url, body)
        if status == http.client.UNAUTHORIZED:
            if self.login_to_cmdctl():
                # successful, so try send again
                status, reply_msg = self._send_message(url, body)
312

313
        if reply_msg:
JINMEI Tatuya's avatar
JINMEI Tatuya committed
314
            return json.loads(reply_msg.decode())
315
        else:
316
            return {}
317

318

319
    def send_POST(self, url, post_param=None):
320
        '''Send POST request to cmdctl, session id is send with the name
321
        'cookie' in header.
322
323
324
325
        Format: /module_name/command_name
        parameters of command is encoded as a map
        '''
        param = None
Mukund Sivaraman's avatar
Mukund Sivaraman committed
326
        if post_param is not None and len(post_param) != 0:
327
328
329
330
331
            param = json.dumps(post_param)

        headers = {"cookie" : self.session_id}
        self.conn.request('POST', url, param, headers)
        return self.conn.getresponse()
332
333
334
335
336
337
338

    def _update_all_modules_info(self):
        ''' Get all modules' information from cmdctl, including
        specification file and configuration data. This function
        should be called before interpreting command line or complete-key
        is entered. This may not be the best way to keep bindctl
        and cmdctl share same modules information, but it works.'''
339
340
341
342
        if self.config_data is not None:
            self.config_data.update_specs_and_config()
        else:
            self.config_data = isc.config.UIModuleCCSession(self)
343
344
345
        self._update_commands()

    def precmd(self, line):
346
347
348
        if line != 'EOF':
            self._update_all_modules_info()
        return line
349
350

    def postcmd(self, stop, line):
351
352
353
354
        '''Update the prompt after every command, but only if we
           have a tty as output'''
        if sys.stdin.isatty():
            self.prompt = self.location + self.prompt_end
355
356
        return stop

Jelte Jansen's avatar
Jelte Jansen committed
357
    def _prepare_module_commands(self, module_spec):
358
        '''Prepare the module commands'''
Jelte Jansen's avatar
Jelte Jansen committed
359
360
361
        module = ModuleInfo(name = module_spec.get_module_name(),
                            desc = module_spec.get_module_description())
        for command in module_spec.get_commands_spec():
362
            cmd = CommandInfo(name = command["command_name"],
363
                              desc = command["command_description"])
364
365
366
            for arg in command["command_args"]:
                param = ParamInfo(name = arg["item_name"],
                                  type = arg["item_type"],
367
368
                                  optional = bool(arg["item_optional"]),
                                  param_spec = arg)
369
370
                if ("item_default" in arg):
                    param.default = arg["item_default"]
371
372
                if ("item_description" in arg):
                    param.desc = arg["item_description"]
373
374
375
376
                cmd.add_param(param)
            module.add_command(cmd)
        self.add_module_info(module)

377
    def _validate_cmd(self, cmd):
378
379
        '''validate the parameters and merge some parameters together,
        merge algorithm is based on the command line syntax, later, if
380
381
        a better command line syntax come out, this function should be
        updated first.
382
        '''
383
384
        if not cmd.module in self.modules:
            raise CmdUnknownModuleSyntaxError(cmd.module)
385

386
387
388
389
390
391
392
        module_info = self.modules[cmd.module]
        if not module_info.has_command_with_name(cmd.command):
            raise CmdUnknownCmdSyntaxError(cmd.module, cmd.command)

        command_info = module_info.get_command_with_name(cmd.command)
        manda_params = command_info.get_mandatory_param_names()
        all_params = command_info.get_param_names()
393

394
        # If help is entered, don't do further parameter validation.
395
396
397
        for val in cmd.params.keys():
            if val == "help":
                return
398
399
400
401

        params = cmd.params.copy()
        if not params and manda_params:
            raise CmdMissParamSyntaxError(cmd.module, cmd.command, manda_params[0])
402
        elif params and not all_params:
403
            raise CmdUnknownParamSyntaxError(cmd.module, cmd.command,
404
405
406
407
408
409
410
411
412
413
414
                                             list(params.keys())[0])
        elif params:
            param_name = None
            param_count = len(params)
            for name in params:
                # either the name of the parameter must be known, or
                # the 'name' must be an integer (ie. the position of
                # an unnamed argument
                if type(name) == int:
                    # lump all extraneous arguments together as one big final one
                    # todo: check if last param type is a string?
415
416
417
418
419
420
                    while (param_count > 2 and
                           param_count > len(command_info.params) - 1):
                        params[param_count - 2] += " " + params[param_count - 1]
                        del(params[param_count - 1])
                        param_count = len(params)
                        cmd.params = params.copy()
421
422
423
424
425
426
427
428
429
430

                    # (-1, help is always in the all_params list)
                    if name >= len(all_params) - 1:
                        # add to last known param
                        if param_name:
                            cmd.params[param_name] += cmd.params[name]
                        else:
                            raise CmdUnknownParamSyntaxError(cmd.module, cmd.command, cmd.params[name])
                    else:
                        # replace the numbered items by named items
431
                        param_name = command_info.get_param_name_by_position(name, param_count)
432
433
                        cmd.params[param_name] = cmd.params[name]
                        del cmd.params[name]
434

435
436
                elif not name in all_params:
                    raise CmdUnknownParamSyntaxError(cmd.module, cmd.command, name)
437

438
439
440
441
442
            param_nr = 0
            for name in manda_params:
                if not name in params and not param_nr in params:
                    raise CmdMissParamSyntaxError(cmd.module, cmd.command, name)
                param_nr += 1
443

444
        # Convert parameter value according parameter spec file.
445
446
        # Ignore check for commands belongs to module 'config' or 'execute
        if cmd.module != CONFIG_MODULE_NAME and\
447
           cmd.module != command_sets.EXECUTE_MODULE_NAME:
448
449
            for param_name in cmd.params:
                param_spec = command_info.get_param_with_name(param_name).param_spec
450
451
452
                try:
                    cmd.params[param_name] = isc.config.config_data.convert_type(param_spec, cmd.params[param_name])
                except isc.cc.data.DataTypeError as e:
453
                    raise isc.cc.data.DataTypeError('Invalid parameter value for \"%s\", the type should be \"%s\" \n'
454
                                                     % (param_name, param_spec['item_type']) + str(e))
455

456
    def _handle_cmd(self, cmd):
457
        '''Handle a command entered by the user'''
458
459
        if cmd.command == "help" or ("help" in cmd.params.keys()):
            self._handle_help(cmd)
460
        elif cmd.module == CONFIG_MODULE_NAME:
461
            self.apply_config_cmd(cmd)
462
        elif cmd.module == command_sets.EXECUTE_MODULE_NAME:
463
            self.apply_execute_cmd(cmd)
464
465
466
        else:
            self.apply_cmd(cmd)

467
468
    def add_module_info(self, module_info):
        '''Add the information about one module'''
469
        self.modules[module_info.name] = module_info
470

471
    def get_module_names(self):
472
        '''Return the names of all known modules'''
473
474
475
476
477
478
479
480
481
482
        return list(self.modules.keys())

    #override methods in cmd
    def default(self, line):
        self._parse_cmd(line)

    def emptyline(self):
        pass

    def do_help(self, name):
483
        self._print(CONST_BINDCTL_HELP)
484
485
        for k in self.modules.values():
            n = k.get_name()
486
            if len(n) >= CONST_BINDCTL_HELP_INDENT_WIDTH:
487
488
489
490
491
492
                self._print("    %s" % n)
                self._print(textwrap.fill(k.get_desc(),
                            initial_indent="            ",
                            subsequent_indent="    " +
                            " " * CONST_BINDCTL_HELP_INDENT_WIDTH,
                            width=70))
493
            else:
494
495
496
497
498
499
500
501
502
                self._print(textwrap.fill("%s%s%s" %
                            (k.get_name(),
                            " "*(CONST_BINDCTL_HELP_INDENT_WIDTH -
                                 len(k.get_name())),
                            k.get_desc()),
                            initial_indent="    ",
                            subsequent_indent="    " +
                            " " * CONST_BINDCTL_HELP_INDENT_WIDTH,
                            width=70))
503

504
505
506
507
    def onecmd(self, line):
        if line == 'EOF' or line.lower() == "quit":
            self.conn.close()
            return True
508

509
510
        if line == 'h':
            line = 'help'
511

512
        Cmd.onecmd(self, line)
Jelte Jansen's avatar
Jelte Jansen committed
513

514
515
    def _get_identifier_startswith(self, id_text):
        """Return the tab-completion hints for identifiers starting with
516
517
518
519
520
521
522
523
524
525
           id_text.

           Parameters:
           id_text (string): the currently entered identifier part, which
           is to be completed.
        """
        # Strip starting "/" from id_text
        if id_text.startswith('/'):
            id_text = id_text[1:]
        # Get all items from the given module (up to the first /)
526
        list = self.config_data.get_config_item_list(
527
528
529
530
                        id_text.rpartition("/")[0], recurse=True)
        # filter out all possibilities that don't match currently entered
        # text part
        hints = [val for val in list if val.startswith(id_text)]
531
532
533
534
535
536
        return hints

    def _cmd_has_identifier_param(self, cmd):
        """
        Returns True if the given (parsed) command is known and has a
        parameter which points to a config data identifier
JINMEI Tatuya's avatar
JINMEI Tatuya committed
537
538

        Parameters:
Jelte Jansen's avatar
Jelte Jansen committed
539
        cmd (cmdparse.BindCmdParser): command context, including given params
JINMEI Tatuya's avatar
JINMEI Tatuya committed
540

541
542
543
544
        """
        if cmd.module not in self.modules:
            return False
        command = self.modules[cmd.module].get_command_with_name(cmd.command)
545
        return command.has_param_with_name(CFGITEM_IDENTIFIER_PARAM)
Jelte Jansen's avatar
Jelte Jansen committed
546

547
    def complete(self, text, state):
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
        """
        Returns tab-completion hints. See the python documentation of the
        readline and Cmd modules for more information.

        The first time this is called (within one 'completer' action), it
        has state 0, and a list of possible completions is made. This list
        is stored; complete() will then be called with increasing values of
        state, until it returns None. For each call it returns the state'th
        element of the hints it collected in the first call.

        The hints list contents depend on which part of the full command
        line; if no module is given yet, it will list all modules. If a
        module is given, but no command, it will complete with module
        commands. If both have been given, it will create the hints based on
        the command parameters.

        If module and command have already been specified, and the command
        has a parameter 'identifier', the configuration data is used to
        create the hints list.

        Parameters:
        text (string): The text entered so far in the 'current' part of
                       the command (module, command, parameters)
        state (int): state used in the readline tab-completion logic;
                     0 on first call, increasing by one until there are
                     no (more) hints to return.

        Returns the string value of the hints list with index 'state',
        or None if no (more) hints are available.
        """
578
        if state == 0:
579
            self._update_all_modules_info()
580
581
            text = text.strip()
            hints = []
582
            cur_line = my_readline()
583
            try:
Jelte Jansen's avatar
Jelte Jansen committed
584
                cmd = BindCmdParser(cur_line)
585
586
                if not cmd.params and text:
                    hints = self._get_command_startswith(cmd.module, text)
587
                elif self._cmd_has_identifier_param(cmd):
588
589
590
591
592
593
594
595
596
597
598
                    # If the command has an argument that is a configuration
                    # identifier (currently, this is only a subset of
                    # the config commands), then don't tab-complete with
                    # hints derived from command parameters, but from
                    # possible configuration identifiers.
                    #
                    # This solves the issue reported in #2254, where
                    # there were hints such as 'argument' and 'identifier'.
                    #
                    # Since they are replaced, the tab-completion no longer
                    # adds 'help' as an option (but it still works)
Jelte Jansen's avatar
Jelte Jansen committed
599
600
601
602
603
                    #
                    # Also, currently, tab-completion does not work
                    # together with 'config go' (it does not take 'current
                    # position' into account). But config go currently has
                    # problems by itself, unrelated to completion.
604
                    hints = self._get_identifier_startswith(text)
605
                else:
606
607
                    hints = self._get_param_startswith(cmd.module, cmd.command,
                                                       text)
608

609
610
611
612
613
614
            except CmdModuleNameFormatError:
                if not text:
                    hints = self.get_module_names()

            except CmdMissCommandNameFormatError as e:
                if not text.strip(): # command name is empty
615
616
                    hints = self.modules[e.module].get_command_names()
                else:
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
                    hints = self._get_module_startswith(text)

            except CmdCommandNameFormatError as e:
                if e.module in self.modules:
                    hints = self._get_command_startswith(e.module, text)

            except CmdParamFormatError as e:
                hints = self._get_param_startswith(e.module, e.command, text)

            except BindCtlException:
                hints = []

            self.hint = hints

        if state < len(self.hint):
            return self.hint[state]
        else:
            return None

636
637

    def _get_module_startswith(self, text):
638
        return [module
639
                for module in self.modules
640
641
642
643
                if module.startswith(text)]


    def _get_command_startswith(self, module, text):
644
        if module in self.modules:
645
            return [command
646
                    for command in self.modules[module].get_command_names()
647
648
                    if command.startswith(text)]

649
650
651
652
        return []


    def _get_param_startswith(self, module, command, text):
653
        if module in self.modules:
654
655
            module_info = self.modules[module]
            if command in module_info.get_command_names():
656
                cmd_info = module_info.get_command_with_name(command)
657
                params = cmd_info.get_param_names()
658
                hint = []
659
                if text:
660
661
662
                    hint = [val for val in params if val.startswith(text)]
                else:
                    hint = list(params)
663

664
                if len(hint) == 1 and hint[0] != "help":
665
666
                    hint[0] = hint[0] + " ="

667
668
669
670
671
672
                return hint

        return []

    def _parse_cmd(self, line):
        try:
Jelte Jansen's avatar
Jelte Jansen committed
673
            cmd = BindCmdParser(line)
674
            self._validate_cmd(cmd)
675
            self._handle_cmd(cmd)
676
        except (IOError, http.client.HTTPException) as err:
677
            self._print('Error: ', err)
678
        except BindCtlException as err:
679
            self._print("Error! ", err)
680
681
            self._print_correct_usage(err)
        except isc.cc.data.DataTypeError as err:
682
            self._print("Error! ", err)
683
        except isc.cc.data.DataTypeError as dte:
684
            self._print("Error: " + str(dte))
685
        except isc.cc.data.DataNotFoundError as dnfe:
686
            self._print("Error: " + str(dnfe))
687
        except isc.cc.data.DataAlreadyPresentError as dape:
688
            self._print("Error: " + str(dape))
689
        except KeyError as ke:
690
            self._print("Error: missing " + str(ke))
691
692

    def _print_correct_usage(self, ept):
693
694
        if isinstance(ept, CmdUnknownModuleSyntaxError):
            self.do_help(None)
695

696
697
        elif isinstance(ept, CmdUnknownCmdSyntaxError):
            self.modules[ept.module].module_help()
698

699
700
701
        elif isinstance(ept, CmdMissParamSyntaxError) or \
             isinstance(ept, CmdUnknownParamSyntaxError):
             self.modules[ept.module].command_help(ept.command)
702
703


704
705
706
    def _append_space_to_hint(self):
        """Append one space at the end of complete hint."""
        self.hint = [(val + " ") for val in self.hint]
707
708


709
710
711
712
713
714
715
716
    def _handle_help(self, cmd):
        if cmd.command == "help":
            self.modules[cmd.module].module_help()
        else:
            self.modules[cmd.module].command_help(cmd.command)


    def apply_config_cmd(self, cmd):
717
718
719
720
721
        '''Handles a configuration command.
           Raises a DataTypeError if a wrong value is set.
           Raises a DataNotFoundError if a wrong identifier is used.
           Raises a KeyError if the command was not complete
        '''
722
        identifier = self.location
723
724
725
726
727
        if 'identifier' in cmd.params:
            if not identifier.endswith("/"):
                identifier += "/"
            if cmd.params['identifier'].startswith("/"):
                identifier = cmd.params['identifier']
728
            else:
729
730
                if cmd.params['identifier'].startswith('['):
                    identifier = identifier[:-1]
731
                identifier += cmd.params['identifier']
732
733
734
735
736
737
738

            # Check if the module is known; for unknown modules
            # we currently deny setting preferences, as we have
            # no way yet to determine if they are ok.
            module_name = identifier.split('/')[1]
            if module_name != "" and (self.config_data is None or \
               not self.config_data.have_specification(module_name)):
739
740
                self._print("Error: Module '" + module_name +
                            "' unknown or not running")
741
                return
742

743
744
745
746
747
748
749
750
751
752
        if cmd.command == "show":
            # check if we have the 'all' argument
            show_all = False
            if 'argument' in cmd.params:
                if cmd.params['argument'] == 'all':
                    show_all = True
                elif 'identifier' not in cmd.params:
                    # no 'all', no identifier, assume this is the
                    #identifier
                    identifier += cmd.params['argument']
753
                else:
754
755
756
                    self._print("Error: unknown argument " +
                                cmd.params['argument'] +
                                ", or multiple identifiers given")
757
758
759
760
761
762
                    return
            values = self.config_data.get_value_maps(identifier, show_all)
            for value_map in values:
                line = value_map['name']
                if value_map['type'] in [ 'module', 'map' ]:
                    line += "/"
763
764
                elif value_map['type'] == 'list' \
                     and value_map['value'] != []:
765
766
767
                    # do not print content of non-empty lists if
                    # we have more data to show
                    line += "/"
Jelte Jansen's avatar
Jelte Jansen committed
768
                else:
769
                    # if type is named_set, don't print value if None
770
771
772
                    # (it is either {} meaning empty, or None, meaning
                    # there actually is data, but not to be shown with
                    # the current command
773
                    if value_map['type'] == 'named_set' and\
774
775
776
                       value_map['value'] is None:
                        line += "/\t"
                    else:
777
                        line += "\t" + json.dumps(value_map['value'])
778
779
780
781
782
783
                line += "\t" + value_map['type']
                line += "\t"
                if value_map['default']:
                    line += "(default)"
                if value_map['modified']:
                    line += "(modified)"
784
                self._print(line)
785
786
        elif cmd.command == "show_json":
            if identifier == "":
787
788
                self._print("Need at least the module to show the "
                            "configuration in JSON format")
789
790
            else:
                data, default = self.config_data.get_value(identifier)
791
                self._print(json.dumps(data))
792
        elif cmd.command == "add":
793
794
795
            self.config_data.add_value(identifier,
                                       cmd.params.get('value_or_name'),
                                       cmd.params.get('value_for_set'))
796
797
798
799
800
801
802
        elif cmd.command == "remove":
            if 'value' in cmd.params:
                self.config_data.remove_value(identifier, cmd.params['value'])
            else:
                self.config_data.remove_value(identifier, None)
        elif cmd.command == "set":
            if 'identifier' not in cmd.params:
803
                self._print("Error: missing identifier or value")
804
805
806
807
808
809
810
811
812
813
814
815
816
            else:
                parsed_value = None
                try:
                    parsed_value = json.loads(cmd.params['value'])
                except Exception as exc:
                    # ok could be an unquoted string, interpret as such
                    parsed_value = cmd.params['value']
                self.config_data.set_value(identifier, parsed_value)
        elif cmd.command == "unset":
            self.config_data.unset(identifier)
        elif cmd.command == "revert":
            self.config_data.clear_local_changes()
        elif cmd.command == "commit":
817
818
819
            try:
                self.config_data.commit()
            except isc.config.ModuleCCSessionError as mcse:
820
                self._print(str(mcse))
821
        elif cmd.command == "diff":
822
            self._print(self.config_data.get_local_changes())
823
824
        elif cmd.command == "go":
            self.go(identifier)
825
826

    def go(self, identifier):
827
        '''Handles the config go command, change the 'current' location
828
829
830
831
832
833
834
835
836
837
838
           within the configuration tree. '..' will be interpreted as
           'up one level'.'''
        id_parts = isc.cc.data.split_identifier(identifier)

        new_location = ""
        for id_part in id_parts:
            if (id_part == ".."):
                # go 'up' one level
                new_location, a, b = new_location.rpartition("/")
            else:
                new_location += "/" + id_part
839
840
841
        # check if exists, if not, revert and error
        v,d = self.config_data.get_value(new_location)
        if v is None:
842
            self._print("Error: " + identifier + " not found")
843
            return
844
845

        self.location = new_location
846

847
848
    def apply_execute_cmd(self, command):
        '''Handles the 'execute' command, which executes a number of
Jelte Jansen's avatar
Jelte Jansen committed
849
850
851
           (preset) statements. The command set to execute is either
           read from a file (e.g. 'execute file <file>'.) or one
           of the sets as defined in command_sets.py'''
852
853
        if command.command == 'file':
            try:
854
855
                with open(command.params['filename']) as command_file:
                    commands = command_file.readlines()
856
            except IOError as ioe:
857
                self._print("Error: " + str(ioe))
858
                return
859
860
        elif command_sets.has_command_set(command.command):
            commands = command_sets.get_commands(command.command)
861
862
863
864
865
866
867
868
869
870
871
872
873
874
        else:
            # Should not be reachable; parser should've caught this
            raise Exception("Unknown execute command type " + command.command)

        # We have our set of commands now, depending on whether 'show' was
        # specified, show or execute them
        if 'show' in command.params and command.params['show'] == 'show':
            self.__show_execute_commands(commands)
        else:
            self.__apply_execute_commands(commands)

    def __show_execute_commands(self, commands):
        '''Prints the command list without executing them'''
        for line in commands:
875
            self._print(line.strip())
876

877
878
    def __apply_execute_commands(self, commands):
        '''Applies the configuration commands from the given iterator.
879
880
881
882
883
884
885
886
887
888
889
890
891
892
           This is the method that catches, comments, echo statements, and
           other directives. All commands not filtered by this method are
           interpreted as if they are directly entered in an active session.
           Lines starting with any of the following characters are not
           passed directly:
           # - These are comments
           ! - These are directives
               !echo: print the rest of the line
               !verbose on/off: print the commands themselves too
               Unknown directives are ignored (with a warning)
           The execution is stopped if there are any errors.
        '''
        verbose = False
        try:
893
            for line in commands:
894
895
                line = line.strip()
                if verbose:
896
                    self._print(line)
897
                if line.startswith('#') or len(line) == 0:
898
899
                    continue
                elif line.startswith('!'):
900
                    if re.match('^!echo ', line, re.I) and len(line) > 6:
901
                        self._print(line[6:])
902
                    elif re.match('^!verbose\s+on\s*$', line, re.I):
903
                        verbose = True
904
                    elif re.match('^!verbose\s+off$', line, re.I):
905
906
                        verbose = False
                    else:
907
908
                        self._print("Warning: ignoring unknown directive: " +
                                    line)
909
                else:
Jelte Jansen's avatar
Jelte Jansen committed
910
                    cmd = BindCmdParser(line)
911
912
                    self._validate_cmd(cmd)
                    self._handle_cmd(cmd)
913
914
        except (isc.config.ModuleCCSessionError,
                IOError, http.client.HTTPException,
915
916
917
918
                BindCtlException, isc.cc.data.DataTypeError,
                isc.cc.data.DataNotFoundError,
                isc.cc.data.DataAlreadyPresentError,
                KeyError) as err:
919
920
921
922
923
924
            self._print('Error: ', err)
            self._print()
            self._print('Depending on the contents of the script, and which')
            self._print('commands it has called, there can be committed and')
            self._print('local changes. It is advised to check your settings')
            self._print(', and revert local changes with "config revert".')
925

926
    def apply_cmd(self, cmd):
927
        '''Handles a general module command'''
928
929
930
931
932
933
934
        url = '/' + cmd.module + '/' + cmd.command
        cmd_params = None
        if (len(cmd.params) != 0):
            cmd_params = json.dumps(cmd.params)

        reply = self.send_POST(url, cmd.params)
        data = reply.read().decode()
JINMEI Tatuya's avatar
JINMEI Tatuya committed
935
936
937
        # The reply is a string containing JSON data,
        # parse it, then prettyprint
        if data != "" and data != "{}":
938
939
            self._print(json.dumps(json.loads(data), sort_keys=True,
                                   indent=4))