bindcmd.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. # Copyright (C) 2009 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. """This module holds the BindCmdInterpreter class. This provides the
  16. core functionality for bindctl. It maintains a session with
  17. b10-cmdctl, holds local configuration and module information, and
  18. handles command line interface commands"""
  19. import sys
  20. from cmd import Cmd
  21. from bindctl.exception import *
  22. from bindctl.moduleinfo import *
  23. from bindctl.cmdparse import BindCmdParse
  24. from xml.dom import minidom
  25. import isc
  26. import isc.cc.data
  27. import http.client
  28. import json
  29. import inspect
  30. import pprint
  31. import ssl, socket
  32. import os, time, random, re
  33. import getpass
  34. from hashlib import sha1
  35. import csv
  36. import ast
  37. try:
  38. from collections import OrderedDict
  39. except ImportError:
  40. from bindctl.mycollections import OrderedDict
  41. # if we have readline support, use that, otherwise use normal stdio
  42. try:
  43. import readline
  44. my_readline = readline.get_line_buffer
  45. except ImportError:
  46. my_readline = sys.stding.readline
  47. CONST_BINDCTL_HELP = """BindCtl, version 0.1
  48. usage: <module name> <command name> [param1 = value1 [, param2 = value2]]
  49. Type Tab character to get the hint of module/command/paramters.
  50. Type \"help(? h)\" for help on bindctl.
  51. Type \"<module_name> help\" for help on the specific module.
  52. Type \"<module_name> <command_name> help\" for help on the specific command.
  53. \nAvailable module names: """
  54. CONST_COMMAND_NODE = "command"
  55. class BindCmdInterpreter(Cmd):
  56. """simple bindctl example."""
  57. def __init__(self, server_port = 'localhost:8080', pem_file = "bindctl.pem"):
  58. Cmd.__init__(self)
  59. self.location = ""
  60. self.prompt_end = '> '
  61. self.prompt = self.prompt_end
  62. self.ruler = '-'
  63. self.modules = OrderedDict()
  64. self.add_module_info(ModuleInfo("help", desc = "Get help for bindctl"))
  65. self.server_port = server_port
  66. self.pem_file = pem_file
  67. self._connect_to_cmd_ctrld()
  68. self.session_id = self._get_session_id()
  69. def _connect_to_cmd_ctrld(self):
  70. '''Connect to cmdctl in SSL context. '''
  71. try:
  72. self.conn = http.client.HTTPSConnection(self.server_port,
  73. cert_file=self.pem_file)
  74. except Exception as e:
  75. print(e, "can't connect to %s, please make sure cmd-ctrld is running" %
  76. self.server_port)
  77. def _get_session_id(self):
  78. '''Generate one session id for the connection. '''
  79. rand = os.urandom(16)
  80. now = time.time()
  81. ip = socket.gethostbyname(socket.gethostname())
  82. session_id = sha1(("%s%s%s" %(rand, now, ip)).encode())
  83. digest = session_id.hexdigest()
  84. return digest
  85. def run(self):
  86. '''Parse commands inputted from user and send them to cmdctl. '''
  87. try:
  88. if not self.login_to_cmdctl():
  89. return False
  90. # Get all module information from cmd-ctrld
  91. self.config_data = isc.config.UIModuleCCSession(self)
  92. self._update_commands()
  93. self.cmdloop()
  94. except KeyboardInterrupt:
  95. return True
  96. def login_to_cmdctl(self):
  97. '''Login to cmdctl with the username and password inputted
  98. from user. After the login is sucessful, the username and
  99. password will be saved in 'default_user.csv', when run the next
  100. time, username and password saved in 'default_user.csv' will be
  101. used first.
  102. '''
  103. csvfile = None
  104. bsuccess = False
  105. try:
  106. csvfile = open('default_user.csv')
  107. users = csv.reader(csvfile)
  108. for row in users:
  109. param = {'username': row[0], 'password' : row[1]}
  110. response = self.send_POST('/login', param)
  111. data = response.read().decode()
  112. if response.status == http.client.OK:
  113. print(data + ' login as ' + row[0] )
  114. bsuccess = True
  115. break
  116. except IOError as e:
  117. pass
  118. except Exception as e:
  119. print(e)
  120. finally:
  121. if csvfile:
  122. csvfile.close()
  123. if bsuccess:
  124. return True
  125. count = 0
  126. print("[TEMP MESSAGE]: username :root password :bind10")
  127. while True:
  128. count = count + 1
  129. if count > 3:
  130. print("Too many authentication failures")
  131. return False
  132. username = input("Username:")
  133. passwd = getpass.getpass()
  134. param = {'username': username, 'password' : passwd}
  135. response = self.send_POST('/login', param)
  136. data = response.read().decode()
  137. print(data)
  138. if response.status == http.client.OK:
  139. csvfile = open('default_user.csv', 'w')
  140. writer = csv.writer(csvfile)
  141. writer.writerow([username, passwd])
  142. csvfile.close()
  143. return True
  144. def _update_commands(self):
  145. '''Get the commands of all modules. '''
  146. cmd_spec = self.send_GET('/command_spec')
  147. if not cmd_spec:
  148. return
  149. for module_name in cmd_spec.keys():
  150. self._prepare_module_commands(module_name, cmd_spec[module_name])
  151. def send_GET(self, url, body = None):
  152. '''Send GET request to cmdctl, session id is send with the name
  153. 'cookie' in header.
  154. '''
  155. headers = {"cookie" : self.session_id}
  156. self.conn.request('GET', url, body, headers)
  157. res = self.conn.getresponse()
  158. reply_msg = res.read()
  159. if reply_msg:
  160. return json.loads(reply_msg.decode())
  161. else:
  162. return {}
  163. def send_POST(self, url, post_param = None):
  164. '''Send POST request to cmdctl, session id is send with the name
  165. 'cookie' in header.
  166. Format: /module_name/command_name
  167. parameters of command is encoded as a map
  168. '''
  169. param = None
  170. if (len(post_param) != 0):
  171. param = json.dumps(post_param)
  172. headers = {"cookie" : self.session_id}
  173. self.conn.request('POST', url, param, headers)
  174. return self.conn.getresponse()
  175. def postcmd(self, stop, line):
  176. '''Update the prompt after every command'''
  177. self.prompt = self.location + self.prompt_end
  178. return stop
  179. def _prepare_module_commands(self, module_name, module_commands):
  180. '''Prepare the module commands'''
  181. module = ModuleInfo(name = module_name,
  182. desc = "same here")
  183. for command in module_commands:
  184. cmd = CommandInfo(name = command["command_name"],
  185. desc = command["command_description"])
  186. for arg in command["command_args"]:
  187. param = ParamInfo(name = arg["item_name"],
  188. type = arg["item_type"],
  189. optional = bool(arg["item_optional"]))
  190. if ("item_default" in arg):
  191. param.default = arg["item_default"]
  192. cmd.add_param(param)
  193. module.add_command(cmd)
  194. self.add_module_info(module)
  195. def _validate_cmd(self, cmd):
  196. '''validate the parameters and merge some parameters together,
  197. merge algorithm is based on the command line syntax, later, if
  198. a better command line syntax come out, this function should be
  199. updated first.
  200. '''
  201. if not cmd.module in self.modules:
  202. raise CmdUnknownModuleSyntaxError(cmd.module)
  203. module_info = self.modules[cmd.module]
  204. if not module_info.has_command_with_name(cmd.command):
  205. raise CmdUnknownCmdSyntaxError(cmd.module, cmd.command)
  206. command_info = module_info.get_command_with_name(cmd.command)
  207. manda_params = command_info.get_mandatory_param_names()
  208. all_params = command_info.get_param_names()
  209. # If help is entered, don't do further parameter validation.
  210. for val in cmd.params.keys():
  211. if val == "help":
  212. return
  213. params = cmd.params.copy()
  214. if not params and manda_params:
  215. raise CmdMissParamSyntaxError(cmd.module, cmd.command, manda_params[0])
  216. elif params and not all_params:
  217. raise CmdUnknownParamSyntaxError(cmd.module, cmd.command,
  218. list(params.keys())[0])
  219. elif params:
  220. param_name = None
  221. param_count = len(params)
  222. for name in params:
  223. # either the name of the parameter must be known, or
  224. # the 'name' must be an integer (ie. the position of
  225. # an unnamed argument
  226. if type(name) == int:
  227. # lump all extraneous arguments together as one big final one
  228. # todo: check if last param type is a string?
  229. if (param_count > 2):
  230. while (param_count > len(command_info.params) - 1):
  231. params[param_count - 2] += params[param_count - 1]
  232. del(params[param_count - 1])
  233. param_count = len(params)
  234. cmd.params = params.copy()
  235. # (-1, help is always in the all_params list)
  236. if name >= len(all_params) - 1:
  237. # add to last known param
  238. if param_name:
  239. cmd.params[param_name] += cmd.params[name]
  240. else:
  241. raise CmdUnknownParamSyntaxError(cmd.module, cmd.command, cmd.params[name])
  242. else:
  243. # replace the numbered items by named items
  244. param_name = command_info.get_param_name_by_position(name, param_count)
  245. cmd.params[param_name] = cmd.params[name]
  246. del cmd.params[name]
  247. elif not name in all_params:
  248. raise CmdUnknownParamSyntaxError(cmd.module, cmd.command, name)
  249. param_nr = 0
  250. for name in manda_params:
  251. if not name in params and not param_nr in params:
  252. raise CmdMissParamSyntaxError(cmd.module, cmd.command, name)
  253. param_nr += 1
  254. def _handle_cmd(self, cmd):
  255. '''Handle a command entered by the user'''
  256. if cmd.command == "help" or ("help" in cmd.params.keys()):
  257. self._handle_help(cmd)
  258. elif cmd.module == "config":
  259. self.apply_config_cmd(cmd)
  260. else:
  261. self.apply_cmd(cmd)
  262. def add_module_info(self, module_info):
  263. '''Add the information about one module'''
  264. self.modules[module_info.name] = module_info
  265. def get_module_names(self):
  266. '''Return the names of all known modules'''
  267. return list(self.modules.keys())
  268. #override methods in cmd
  269. def default(self, line):
  270. self._parse_cmd(line)
  271. def emptyline(self):
  272. pass
  273. def do_help(self, name):
  274. print(CONST_BINDCTL_HELP)
  275. for k in self.modules.keys():
  276. print("\t", self.modules[k])
  277. def onecmd(self, line):
  278. if line == 'EOF' or line.lower() == "quit":
  279. self.conn.close()
  280. return True
  281. if line == 'h':
  282. line = 'help'
  283. Cmd.onecmd(self, line)
  284. def complete(self, text, state):
  285. if 0 == state:
  286. text = text.strip()
  287. hints = []
  288. cur_line = my_readline()
  289. try:
  290. cmd = BindCmdParse(cur_line)
  291. if not cmd.params and text:
  292. hints = self._get_command_startswith(cmd.module, text)
  293. else:
  294. hints = self._get_param_startswith(cmd.module, cmd.command,
  295. text)
  296. if cmd.module == "config":
  297. # grm text has been stripped of slashes...
  298. my_text = self.location + "/" + cur_line.rpartition(" ")[2]
  299. print("[XX] completing config part")
  300. list = self.config_data.get_config_item_list(my_text.rpartition("/")[0])
  301. hints.extend([val for val in list if val.startswith(text)])
  302. except CmdModuleNameFormatError:
  303. if not text:
  304. hints = self.get_module_names()
  305. except CmdMissCommandNameFormatError as e:
  306. if not text.strip(): # command name is empty
  307. hints = self.modules[e.module].get_command_names()
  308. else:
  309. hints = self._get_module_startswith(text)
  310. except CmdCommandNameFormatError as e:
  311. if e.module in self.modules:
  312. hints = self._get_command_startswith(e.module, text)
  313. except CmdParamFormatError as e:
  314. hints = self._get_param_startswith(e.module, e.command, text)
  315. except BindCtlException:
  316. hints = []
  317. self.hint = hints
  318. #self._append_space_to_hint()
  319. if state < len(self.hint):
  320. return self.hint[state]
  321. else:
  322. return None
  323. def _get_module_startswith(self, text):
  324. return [module
  325. for module in self.modules
  326. if module.startswith(text)]
  327. def _get_command_startswith(self, module, text):
  328. if module in self.modules:
  329. return [command
  330. for command in self.modules[module].get_command_names()
  331. if command.startswith(text)]
  332. return []
  333. def _get_param_startswith(self, module, command, text):
  334. if module in self.modules:
  335. module_info = self.modules[module]
  336. if command in module_info.get_command_names():
  337. cmd_info = module_info.get_command_with_name(command)
  338. params = cmd_info.get_param_names()
  339. hint = []
  340. if text:
  341. hint = [val for val in params if val.startswith(text)]
  342. else:
  343. hint = list(params)
  344. if len(hint) == 1 and hint[0] != "help":
  345. hint[0] = hint[0] + " ="
  346. return hint
  347. return []
  348. def _parse_cmd(self, line):
  349. try:
  350. cmd = BindCmdParse(line)
  351. self._validate_cmd(cmd)
  352. self._handle_cmd(cmd)
  353. except BindCtlException as e:
  354. print("Error! ", e)
  355. self._print_correct_usage(e)
  356. def _print_correct_usage(self, ept):
  357. if isinstance(ept, CmdUnknownModuleSyntaxError):
  358. self.do_help(None)
  359. elif isinstance(ept, CmdUnknownCmdSyntaxError):
  360. self.modules[ept.module].module_help()
  361. elif isinstance(ept, CmdMissParamSyntaxError) or \
  362. isinstance(ept, CmdUnknownParamSyntaxError):
  363. self.modules[ept.module].command_help(ept.command)
  364. def _append_space_to_hint(self):
  365. """Append one space at the end of complete hint."""
  366. self.hint = [(val + " ") for val in self.hint]
  367. def _handle_help(self, cmd):
  368. if cmd.command == "help":
  369. self.modules[cmd.module].module_help()
  370. else:
  371. self.modules[cmd.module].command_help(cmd.command)
  372. def apply_config_cmd(self, cmd):
  373. '''Handles a configuration command.
  374. Raises a DataTypeError if a wrong value is set.
  375. Raises a DataNotFoundError if a wrong identifier is used.
  376. Raises a KeyError if the command was not complete
  377. '''
  378. identifier = self.location
  379. try:
  380. if 'identifier' in cmd.params:
  381. if not identifier.endswith("/"):
  382. identifier += "/"
  383. if cmd.params['identifier'].startswith("/"):
  384. identifier = cmd.params['identifier']
  385. else:
  386. identifier += cmd.params['identifier']
  387. if cmd.command == "show":
  388. values = self.config_data.get_value_maps(identifier)
  389. for value_map in values:
  390. line = value_map['name']
  391. if value_map['type'] in [ 'module', 'map', 'list' ]:
  392. line += "/"
  393. else:
  394. line += ":\t" + str(value_map['value'])
  395. line += "\t" + value_map['type']
  396. line += "\t"
  397. if value_map['default']:
  398. line += "(default)"
  399. if value_map['modified']:
  400. line += "(modified)"
  401. print(line)
  402. elif cmd.command == "add":
  403. self.config_data.add_value(identifier, cmd.params['value'])
  404. elif cmd.command == "remove":
  405. self.config_data.remove_value(identifier, cmd.params['value'])
  406. elif cmd.command == "set":
  407. if 'identifier' not in cmd.params:
  408. print("Error: missing identifier or value")
  409. else:
  410. parsed_value = None
  411. try:
  412. parsed_value = ast.literal_eval(cmd.params['value'])
  413. except Exception as exc:
  414. # ok could be an unquoted string, interpret as such
  415. parsed_value = cmd.params['value']
  416. self.config_data.set_value(identifier, parsed_value)
  417. elif cmd.command == "unset":
  418. self.config_data.unset(identifier)
  419. elif cmd.command == "revert":
  420. self.config_data.clear_local_changes()
  421. elif cmd.command == "commit":
  422. self.config_data.commit()
  423. elif cmd.command == "diff":
  424. print(self.config_data.get_local_changes());
  425. elif cmd.command == "go":
  426. self.go(identifier)
  427. except isc.cc.data.DataTypeError as dte:
  428. print("Error: " + str(dte))
  429. except isc.cc.data.DataNotFoundError as dnfe:
  430. print("Error: " + identifier + " not found")
  431. except KeyError as ke:
  432. print("Error: missing " + str(ke))
  433. raise ke
  434. def go(self, identifier):
  435. '''Handles the config go command, change the 'current' location
  436. within the configuration tree'''
  437. # this is just to see if it exists
  438. self.config_data.get_value(identifier)
  439. # some sanitizing
  440. identifier = identifier.replace("//", "/")
  441. if not identifier.startswith("/"):
  442. identifier = "/" + identifier
  443. if identifier.endswith("/"):
  444. identifier = identifier[:-1]
  445. self.location = identifier
  446. def apply_cmd(self, cmd):
  447. '''Handles a general module command'''
  448. url = '/' + cmd.module + '/' + cmd.command
  449. cmd_params = None
  450. if (len(cmd.params) != 0):
  451. cmd_params = json.dumps(cmd.params)
  452. print("send the message to cmd-ctrld")
  453. reply = self.send_POST(url, cmd.params)
  454. data = reply.read().decode()
  455. print("received reply:", data)