Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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/python3 

 

# 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. 

 

"""This is the main calling class for the bindctl configuration and 

   command tool. It sets up a command interpreter and runs that.""" 

 

import sys; sys.path.append ('@@PYTHONPATH@@') 

 

from bindctl.moduleinfo import * 

from bindctl.bindcmd import * 

from bindctl import command_sets 

import pprint 

from optparse import OptionParser, OptionValueError 

import isc.util.process 

 

isc.util.process.rename() 

 

# This is the version that gets displayed to the user. 

# The VERSION string consists of the module name, the module version 

# number, and the overall BIND 10 version number (set in configure.ac). 

VERSION = "bindctl 20110217 (BIND 10 20120405)" 

 

DEFAULT_IDENTIFIER_DESC = "The identifier specifies the config item. Child elements are separated with the '/' character. List indices can be specified with '[i]', where i is an integer specifying the index, starting with 0. Examples: 'Boss/start_auth', 'Recurse/listen_on[0]/address'. If no identifier is given, shows the item at the current location." 

 

def prepare_config_commands(tool): 

    '''Prepare fixed commands for local configuration editing''' 

    module = ModuleInfo(name = CONFIG_MODULE_NAME, desc = "Configuration commands.") 

    cmd = CommandInfo(name = "show", desc = "Show configuration.") 

    param = ParamInfo(name = "argument", type = "string", optional=True, desc = "If you specify the argument 'all' (before the identifier), recursively show all child elements for the given identifier.") 

    cmd.add_param(param) 

    param = ParamInfo(name = "identifier", type = "string", optional=True, desc = DEFAULT_IDENTIFIER_DESC) 

    cmd.add_param(param) 

    module.add_command(cmd) 

 

    cmd = CommandInfo(name = "show_json", desc = "Show full configuration in JSON format.") 

    param = ParamInfo(name = "identifier", type = "string", optional=True, desc = DEFAULT_IDENTIFIER_DESC) 

    cmd.add_param(param) 

    module.add_command(cmd) 

 

    cmd = CommandInfo(name = "add", desc = 

        "Add an entry to configuration list or a named set. " 

        "When adding to a list, the command has one optional argument, " 

        "a value to add to the list. The value must be in correct JSON " 

        "and complete. When adding to a named set, it has one " 

        "mandatory parameter (the name to add), and an optional " 

        "parameter value, similar to when adding to a list. " 

        "In either case, when no value is given, an entry will be " 

        "constructed with default values.") 

    param = ParamInfo(name = "identifier", type = "string", optional=True, desc = DEFAULT_IDENTIFIER_DESC) 

    cmd.add_param(param) 

    param = ParamInfo(name = "value_or_name", type = "string", optional=True, desc = "Specifies a value to add to the list, or the name when adding to a named set. It must be in correct JSON format and complete.") 

    cmd.add_param(param) 

    module.add_command(cmd) 

    param = ParamInfo(name = "value_for_set", type = "string", optional=True, desc = "Specifies an optional value to add to the named map. It must be in correct JSON format and complete.") 

    cmd.add_param(param) 

    module.add_command(cmd) 

 

    cmd = CommandInfo(name = "remove", desc = "Remove entry from configuration list or named set.") 

    param = ParamInfo(name = "identifier", type = "string", optional=True, desc = DEFAULT_IDENTIFIER_DESC) 

    cmd.add_param(param) 

    param = ParamInfo(name = "value", type = "string", optional=True, desc = "When identifier is a list, specifies a value to remove from the list. It must be in correct JSON format and complete. When it is a named set, specifies the name to remove.") 

    cmd.add_param(param) 

    module.add_command(cmd) 

 

    cmd = CommandInfo(name = "set", desc = "Set a configuration value.") 

    param = ParamInfo(name = "identifier", type = "string", optional=True, desc = DEFAULT_IDENTIFIER_DESC) 

    cmd.add_param(param) 

    param = ParamInfo(name = "value", type = "string", optional=False, desc = "Specifies a value to set. It must be in correct JSON format and complete.") 

    cmd.add_param(param) 

    module.add_command(cmd) 

 

    cmd = CommandInfo(name = "unset", desc = "Unset a configuration value (i.e. revert to the default, if any).") 

    param = ParamInfo(name = "identifier", type = "string", optional=False, desc = DEFAULT_IDENTIFIER_DESC) 

    cmd.add_param(param) 

    module.add_command(cmd) 

 

    cmd = CommandInfo(name = "diff", desc = "Show all local changes that have not been committed.") 

    module.add_command(cmd) 

 

    cmd = CommandInfo(name = "revert", desc = "Revert all local changes.") 

    module.add_command(cmd) 

 

    cmd = CommandInfo(name = "commit", desc = "Commit all local changes.") 

    module.add_command(cmd) 

 

    cmd = CommandInfo(name = "go", desc = "Go to a specific configuration part.") 

    param = ParamInfo(name = "identifier", type="string", optional=False, desc = DEFAULT_IDENTIFIER_DESC) 

    cmd.add_param(param) 

    module.add_command(cmd) 

 

    tool.add_module_info(module) 

 

def check_port(option, opt_str, value, parser): 

    if (value < 0) or (value > 65535): 

        raise OptionValueError('%s requires a port number (0-65535)' % opt_str) 

    parser.values.port = value 

 

def check_addr(option, opt_str, value, parser): 

    ipstr = value 

    ip_family = socket.AF_INET 

    if (ipstr.find(':') != -1): 

        ip_family = socket.AF_INET6 

 

    try: 

        socket.inet_pton(ip_family, ipstr) 

    except: 

        raise OptionValueError("%s invalid ip address" % ipstr) 

 

    parser.values.addr = value 

 

def set_bindctl_options(parser): 

    parser.add_option('-p', '--port', dest='port', type='int', 

                      action='callback', callback=check_port, 

                      default='8080', help='port for cmdctl of bind10') 

 

    parser.add_option('-a', '--address', dest='addr', type='string', 

                      action='callback', callback=check_addr, 

                      default='127.0.0.1', help='IP address for cmdctl of bind10') 

 

    parser.add_option('-c', '--certificate-chain', dest='cert_chain', 

                      type='string', action='store', 

                      help='PEM formatted server certificate validation chain file') 

 

    parser.add_option('--csv-file-dir', dest='csv_file_dir', type='string', 

                      default=None, action='store', 

                      help='Directory to store the password CSV file') 

 

143if __name__ == '__main__': 

    parser = OptionParser(version = VERSION) 

    set_bindctl_options(parser) 

    (options, args) = parser.parse_args() 

    server_addr = options.addr + ':' + str(options.port) 

    tool = BindCmdInterpreter(server_addr, pem_file=options.cert_chain, 

                              csv_file_dir=options.csv_file_dir) 

    prepare_config_commands(tool) 

    command_sets.prepare_execute_commands(tool) 

    result = tool.run() 

    sys.exit(result)