bindctl-source.py.in 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!@PYTHON@
  2. # Copyright (C) 2009 Internet Systems Consortium.
  3. #
  4. # Permission to use, copy, modify, and distribute this software for any
  5. # purpose with or without fee is hereby granted, provided that the above
  6. # copyright notice and this permission notice appear in all copies.
  7. #
  8. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  9. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  10. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  11. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  13. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  14. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  15. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. """This is the main calling class for the bindctl configuration and
  17. command tool. It sets up a command interpreter and runs that."""
  18. import sys; sys.path.append ('@@PYTHONPATH@@')
  19. from bindctl.moduleinfo import *
  20. from bindctl.bindcmd import *
  21. import pprint
  22. from optparse import OptionParser, OptionValueError
  23. import isc.util.process
  24. isc.util.process.rename()
  25. # This is the version that gets displayed to the user.
  26. # The VERSION string consists of the module name, the module version
  27. # number, and the overall BIND 10 version number (set in configure.ac).
  28. VERSION = "bindctl 20101201 (BIND 10 @PACKAGE_VERSION@)"
  29. def prepare_config_commands(tool):
  30. '''Prepare fixed commands for local configuration editing'''
  31. module = ModuleInfo(name = CONFIG_MODULE_NAME, desc = "Configuration commands")
  32. cmd = CommandInfo(name = "show", desc = "Show configuration")
  33. param = ParamInfo(name = "identifier", type = "string", optional=True)
  34. cmd.add_param(param)
  35. module.add_command(cmd)
  36. cmd = CommandInfo(name = "add", desc = "Add entry to configuration list")
  37. param = ParamInfo(name = "identifier", type = "string", optional=True)
  38. cmd.add_param(param)
  39. param = ParamInfo(name = "value", type = "string", optional=False)
  40. cmd.add_param(param)
  41. module.add_command(cmd)
  42. cmd = CommandInfo(name = "remove", desc = "Remove entry from configuration list")
  43. param = ParamInfo(name = "identifier", type = "string", optional=True)
  44. cmd.add_param(param)
  45. param = ParamInfo(name = "value", type = "string", optional=True)
  46. cmd.add_param(param)
  47. module.add_command(cmd)
  48. cmd = CommandInfo(name = "set", desc = "Set a configuration value")
  49. param = ParamInfo(name = "identifier", type = "string", optional=True)
  50. cmd.add_param(param)
  51. param = ParamInfo(name = "value", type = "string", optional=False)
  52. cmd.add_param(param)
  53. module.add_command(cmd)
  54. cmd = CommandInfo(name = "unset", desc = "Unset a configuration value")
  55. param = ParamInfo(name = "identifier", type = "string", optional=False)
  56. cmd.add_param(param)
  57. module.add_command(cmd)
  58. cmd = CommandInfo(name = "diff", desc = "Show all local changes")
  59. module.add_command(cmd)
  60. cmd = CommandInfo(name = "revert", desc = "Revert all local changes")
  61. module.add_command(cmd)
  62. cmd = CommandInfo(name = "commit", desc = "Commit all local changes")
  63. module.add_command(cmd)
  64. cmd = CommandInfo(name = "go", desc = "Go to a specific configuration part")
  65. param = ParamInfo(name = "identifier", type="string", optional=False)
  66. cmd.add_param(param)
  67. module.add_command(cmd)
  68. tool.add_module_info(module)
  69. def check_port(option, opt_str, value, parser):
  70. if (value < 0) or (value > 65535):
  71. raise OptionValueError('%s requires a port number (0-65535)' % opt_str)
  72. parser.values.port = value
  73. def check_addr(option, opt_str, value, parser):
  74. ipstr = value
  75. ip_family = socket.AF_INET
  76. if (ipstr.find(':') != -1):
  77. ip_family = socket.AF_INET6
  78. try:
  79. socket.inet_pton(ip_family, ipstr)
  80. except:
  81. raise OptionValueError("%s invalid ip address" % ipstr)
  82. parser.values.addr = value
  83. def set_bindctl_options(parser):
  84. parser.add_option('-p', '--port', dest = 'port', type = 'int',
  85. action = 'callback', callback=check_port,
  86. default = '8080', help = 'port for cmdctl of bind10')
  87. parser.add_option('-a', '--address', dest = 'addr', type = 'string',
  88. action = 'callback', callback=check_addr,
  89. default = '127.0.0.1', help = 'IP address for cmdctl of bind10')
  90. parser.add_option('-c', '--certificate-chain', dest = 'cert_chain',
  91. type = 'string', action = 'store',
  92. help = 'PEM formatted server certificate validation chain file')
  93. if __name__ == '__main__':
  94. try:
  95. parser = OptionParser(version = VERSION)
  96. set_bindctl_options(parser)
  97. (options, args) = parser.parse_args()
  98. server_addr = options.addr + ':' + str(options.port)
  99. tool = BindCmdInterpreter(server_addr, pem_file=options.cert_chain)
  100. prepare_config_commands(tool)
  101. tool.run()
  102. except Exception as e:
  103. print(e, "\nFailed to connect with b10-cmdctl module, is it running?")